Kea: isc::agent::ParserContext Class Reference

| Kea 2.2.0

Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
isc::agent::ParserContext Class Reference

Parser context is a wrapper around flex/bison instances dedicated to Control-agent config file parser. More...

`#include <parser_context.h>`

## Public Types
enum | LexerContext { NO_KEYWORDS , CONFIG , AGENT , AUTHENTICATION , AUTH_TYPE , CLIENTS , CONTROL_SOCKETS , SERVER , SOCKET_TYPE , HOOKS_LIBRARIES , LOGGERS , OUTPUT_OPTIONS }
| Defines syntactic contexts for lexical tie-ins. More...
enum | ParserType { PARSER_JSON , PARSER_AGENT , PARSER_SUB_AGENT }
| Defines currently supported scopes. More...

## Public Member Functions
| ParserContext ()
| Default constructor. More...
virtual | ~ParserContext ()
| destructor More...
const std::string | contextName ()
| Get the syntactic context name. More...
void | enter (const LexerContext &ctx)
| Enter a new syntactic context. More...
void | error (const isc::agent::location &loc, const std::string &what, size_t pos=0)
| Error handler. More...
void | error (const std::string &what)
| Error handler. More...
void | includeFile (const std::string &filename)
| Divert input to an include file. More...
void | leave ()
| Leave a syntactic context. More...
isc::data::Element::Position | loc2pos (isc::agent::location &loc)
| Converts bison's position to one understandable by isc::data::Element. More...
isc::data::ElementPtr | parseFile (const std::string &filename, ParserType parser_type)
| Run the parser on the file specified. More...
isc::data::ElementPtr | parseString (const std::string &str, ParserType parser_type)
| Run the parser on the string specified. More...
void | require (const std::string &name, isc::data::Element::Position open_loc, isc::data::Element::Position close_loc)
| Check if a required parameter is present. More...
void | scanEnd ()
| Method called after the last tokens are scanned. More...
void | scanFileBegin (FILE *f, const std::string &filename, ParserType type)
| Method called before scanning starts on a file. More...
void | scanStringBegin (const std::string &str, ParserType type)
| Method called before scanning starts on a string. More...
void | unique (const std::string &name, isc::data::Element::Position loc)
| Check if a parameter is already present. More...
void | warnAboutExtraCommas (const isc::agent::location &loc)
| Warning for extra commas. More...
void | warning (const isc::agent::location &loc, const std::string &what)
| Warning handler. More...

## Static Public Member Functions
static void | fatal (const std::string &what)
| Fatal error handler. More...

## Public Attributes
LexerContext | ctx_
| Current syntactic context. More...
std::string | file_
| File name. More...
std::vector< std::string > | files_
| File name stack. More...
isc::agent::location | loc_
| Location of the current token. More...
std::vector< isc::agent::location > | locs_
| Location stack. More...
FILE * | sfile_
| sFile (aka FILE) More...
std::vector< FILE * > | sfiles_
| sFile (aka FILE) stack More...
std::vector< isc::data::ElementPtr > | stack_
| JSON elements being parsed. More...
std::vector< struct yy_buffer_state * > | states_
| Lexer state stack. More...

## Detailed Description

Parser context is a wrapper around flex/bison instances dedicated to Control-agent config file parser.

It follows the same principle as other components. The primary interface are parseString and parseFile methods. All other methods are public for testing purposes only. This interface allows parsing the whole configuration with syntactic checking (which is by far the most frequent use), but it also allows parsing input as generic JSON or parse only content of the Control-agent object, which is a subset of full grammar (this will be very useful for unit-tests to not duplicate unnecessary parts of the config file).

Definition at line 36 of file agent/parser_context.h.

## Member Enumeration Documentation

## ◆ LexerContext

enum isc::agent::ParserContext::LexerContext

Defines syntactic contexts for lexical tie-ins.

Enumerator
NO_KEYWORDS | This one is used in pure JSON mode. Used while parsing top level (that contains Control-agent)
CONFIG | Used while parsing content of Agent.
AGENT | Used while parsing Control-agent/Authentication.
AUTHENTICATION | Used while parsing Control-agent/Authentication/type.
AUTH_TYPE | Used while parsing Control-agent/Authentication/clients.
CLIENTS | Used while parsing Control-agent/control-sockets.
CONTROL_SOCKETS | Used while parsing Control-agent/control-socket/*-server.
SERVER | Used while parsing Control-agent/control-socket/*-server/socket-type.
SOCKET_TYPE | Used while parsing Control-agent/hooks-libraries.
HOOKS_LIBRARIES | Used while parsing Control-agent/loggers structures.
LOGGERS | Used while parsing Control-agent/loggers/output_options structures.
OUTPUT_OPTIONS |

Definition at line 196 of file agent/parser_context.h.

## ◆ ParserType

enum isc::agent::ParserContext::ParserType

Defines currently supported scopes.

AgentParser is able to parse several types of scope. Usually, when it parses a config file, it expects the data to have a map with Control-agent in it and all the parameters within that map. However, sometimes the parser is expected to parse only a subset of that information.

Enumerator
PARSER_JSON | This parser will parse the content as generic JSON.
PARSER_AGENT | This parser will expect the content as Control-agent config wrapped in a map (that's the regular config file)
PARSER_SUB_AGENT | This parser will expect only the content of Control-agent.

Definition at line 47 of file agent/parser_context.h.

## Constructor & Destructor Documentation

## ◆ ParserContext()

isc::agent::ParserContext::ParserContext | ( |  | ) |

Default constructor.

Definition at line 22 of file agent/parser_context.cc.

## ◆ ~ParserContext()

isc::agent::ParserContext::~ParserContext | ( |  | ) |
| virtual

destructor

Definition at line 27 of file agent/parser_context.cc.

## Member Function Documentation

## ◆ contextName()

const std::string isc::agent::ParserContext::contextName | ( |  | ) |

Get the syntactic context name.

Returns
  printable name of the context.

Definition at line 157 of file agent/parser_context.cc.

References AGENT, AUTH_TYPE, AUTHENTICATION, CLIENTS, CONFIG, CONTROL_SOCKETS, ctx_, HOOKS_LIBRARIES, LOGGERS, NO_KEYWORDS, OUTPUT_OPTIONS, SERVER, and SOCKET_TYPE.

Referenced by isc::agent::AgentParser::parse(), require(), and unique().

## ◆ enter()

void isc::agent::ParserContext::enter | ( | const LexerContext & | ctx | ) |

Enter a new syntactic context.

Entering a new syntactic context is useful in several ways. First, it allows the parser to avoid conflicts. Second, it allows the lexer to return different tokens depending on context (e.g. if "renew-timer" string is detected, the lexer will return STRING token if in JSON mode or RENEW_TIMER if in DHCP6 mode. Finally, the syntactic context allows the error message to be more descriptive if the input string does not parse properly. Control Agent parser uses simplified contexts: either it recognizes keywords (value set to KEYWORDS) or not (value set to NO_KEYWORDS).

Make sure to call leave() once the parsing of your context is complete.

Parameters

  ctx | the syntactic context to enter into

Definition at line 140 of file agent/parser_context.cc.

References ctx_.

Referenced by isc::agent::AgentParser::parse().

## ◆ error() [1/2]

void isc::agent::ParserContext::error | ( | const isc::agent::location & | loc,
|  | const std::string & | what,
|  | size_t | pos = `0`
| ) |  |

Error handler.

Note
  The optional position for an error in a string begins by 1 so the caller should add 1 to the position of the C++ string.

Parameters

  loc | location within the parsed file where the problem was experienced.
  what | string explaining the nature of the error.
  pos | optional position for in string errors.

Exceptions

  ParseError |

Definition at line 74 of file agent/parser_context.cc.

References isc_throw.

## ◆ error() [2/2]

void isc::agent::ParserContext::error | ( | const std::string & | what | ) |

Error handler.

This is a simplified error reporting tool for possible future cases when the AgentParser is not able to handle the packet.

Parameters

  what | string explaining the nature of the error.

Exceptions

  ParseError |

Definition at line 86 of file agent/parser_context.cc.

References isc_throw.

## ◆ fatal()

void isc::agent::ParserContext::fatal | ( | const std::string & | what | ) |
| static

Fatal error handler.

This is for should not happen but fatal errors. Used by YY_FATAL_ERROR macro so required to be static.

Parameters

  what | string explaining the nature of the error.

Exceptions

  ParseError |

Definition at line 92 of file agent/parser_context.cc.

References isc_throw.

Referenced by includeFile(), leave(), scanFileBegin(), and scanStringBegin().

## ◆ includeFile()

void ParserContext::includeFile | ( | const std::string & | filename | ) |

Divert input to an include file.

Parameters

  filename | file to be included

Definition at line 4076 of file agent_lexer.cc.

References BEGIN, fatal(), file_, files_, INITIAL, loc_, locs_, sfile_, sfiles_, states_, and YY_CURRENT_BUFFER.

Here is the call graph for this function:

## ◆ leave()

void isc::agent::ParserContext::leave | ( |  | ) |

Leave a syntactic context.

enter() must be called before (when entering a new scope or context). Once you complete the parsing, this method should be called.

Exceptions

  isc::Unexpected | if unbalanced (more leave() than enter() calls)

Definition at line 147 of file agent/parser_context.cc.

References ctx_, and fatal().

Referenced by isc::agent::AgentParser::parse().

Here is the call graph for this function:

## ◆ loc2pos()

isc::data::Element::Position isc::agent::ParserContext::loc2pos | ( | isc::agent::location & | loc | ) |

Converts bison's position to one understandable by isc::data::Element.

Convert a bison location into an element position (take the begin, the end is lost)

Parameters

  loc | location in bison format

Returns
  Position in format accepted by Element

Definition at line 98 of file agent/parser_context.cc.

Referenced by isc::agent::AgentParser::parse().

## ◆ parseFile()

isc::data::ElementPtr isc::agent::ParserContext::parseFile | ( | const std::string & | filename,
|  | ParserType | parser_type
| ) |  |

Run the parser on the file specified.

This method parses specified file. Depending on the value of parser_type, parser may either check only that the input is valid JSON, or may do more specific syntax checking. See ParserType for supported syntax checkers.

Parameters

  filename | file to be parsed
  parser_type | specifies expected content (usually PARSER_AGENT or PARSER_JSON)

Returns
  Element structure representing parsed text.

Definition at line 39 of file agent/parser_context.cc.

References isc_throw, and scanFileBegin().

Referenced by isc::agent::CtrlAgentController::parseFile().

Here is the call graph for this function:

## ◆ parseString()

isc::data::ElementPtr isc::agent::ParserContext::parseString | ( | const std::string & | str,
|  | ParserType | parser_type
| ) |  |

Run the parser on the string specified.

This method parses specified string. Depending on the value of parser_type, parser may either check only that the input is valid JSON, or may do more specific syntax checking. See ParserType for supported syntax checkers.

Parameters

  str | string to be parsed
  parser_type | specifies expected content (usually AGENT or generic JSON)

Returns
  Element structure representing parsed text.

Definition at line 32 of file agent/parser_context.cc.

References scanStringBegin().

Here is the call graph for this function:

## ◆ require()

void isc::agent::ParserContext::require | ( | const std::string & | name,
|  | isc::data::Element::Position | open_loc,
|  | isc::data::Element::Position | close_loc
| ) |  |

Check if a required parameter is present.

Check if a required parameter is present in the map at the top of the stack and raise an error when it is not.

Parameters

  name | name of the parameter to check
  open_loc | location of the opening curly bracket
  close_loc | location of the closing curly bracket

Exceptions

  ParseError |

Definition at line 107 of file agent/parser_context.cc.

References contextName(), isc_throw, and stack_.

Referenced by isc::agent::AgentParser::parse().

Here is the call graph for this function:

## ◆ scanEnd()

void ParserContext::scanEnd | ( |  | ) |

Method called after the last tokens are scanned.

Definition at line 4055 of file agent_lexer.cc.

References sfile_, sfiles_, and states_.

## ◆ scanFileBegin()

void ParserContext::scanFileBegin | ( | FILE * | f,
|  | const std::string & | filename,
|  | ParserType | type
| ) |  |

Method called before scanning starts on a file.

Parameters

  f | stdio FILE pointer
  filename | file to be parsed
  type | specifies expected content

Definition at line 4033 of file agent_lexer.cc.

References fatal(), file_, loc_, sfile_, and yy_flex_debug.

Referenced by parseFile().

Here is the call graph for this function:

## ◆ scanStringBegin()

void ParserContext::scanStringBegin | ( | const std::string & | str,
|  | ParserType | type
| ) |  |

Method called before scanning starts on a string.

Parameters

  str | string to be parsed
  type | specifies expected content

Definition at line 4015 of file agent_lexer.cc.

References fatal(), file_, loc_, sfile_, and yy_flex_debug.

Referenced by parseString().

Here is the call graph for this function:

## ◆ unique()

void isc::agent::ParserContext::unique | ( | const std::string & | name,
|  | isc::data::Element::Position | loc
| ) |  |

Check if a parameter is already present.

Check if a parameter is already present in the map at the top of the stack and raise an error when it is.

Parameters

  name | name of the parameter to check
  loc | location of the current parameter

Exceptions

  ParseError |

Definition at line 122 of file agent/parser_context.cc.

References contextName(), ctx_, isc_throw, NO_KEYWORDS, and stack_.

Referenced by isc::agent::AgentParser::parse().

Here is the call graph for this function:

## ◆ warnAboutExtraCommas()

void isc::agent::ParserContext::warnAboutExtraCommas | ( | const isc::agent::location & | loc | ) |

Warning for extra commas.

Parameters

  loc | location within the parsed file of the extra comma

Exceptions

  ParseError |

Definition at line 199 of file agent/parser_context.cc.

References warning().

Referenced by isc::agent::AgentParser::parse().

Here is the call graph for this function:

## ◆ warning()

void isc::agent::ParserContext::warning | ( | const isc::agent::location & | loc,
|  | const std::string & | what
| ) |  |

Warning handler.

Parameters

  loc | location within the parsed file where the problem was experienced
  what | string explaining the nature of the error

Exceptions

  ParseError |

Definition at line 190 of file agent/parser_context.cc.

References isc::agent::agent_logger, isc::agent::CTRL_AGENT_CONFIG_SYNTAX_WARNING, and LOG_WARN.

Referenced by warnAboutExtraCommas().

## Member Data Documentation

## ◆ ctx_

LexerContext isc::agent::ParserContext::ctx_

Current syntactic context.

Definition at line 263 of file agent/parser_context.h.

Referenced by contextName(), enter(), leave(), isc::agent::AgentParser::parse(), and unique().

## ◆ file_

std::string isc::agent::ParserContext::file_

File name.

Definition at line 235 of file agent/parser_context.h.

Referenced by includeFile(), scanFileBegin(), and scanStringBegin().

## ◆ files_

std::vector<std::string> isc::agent::ParserContext::files_

File name stack.

Definition at line 238 of file agent/parser_context.h.

Referenced by includeFile().

## ◆ loc_

isc::agent::location isc::agent::ParserContext::loc_

Location of the current token.

The lexer will keep updating it. This variable will be useful for logging errors.

Definition at line 244 of file agent/parser_context.h.

Referenced by includeFile(), scanFileBegin(), and scanStringBegin().

## ◆ locs_

std::vector<isc::agent::location> isc::agent::ParserContext::locs_

Location stack.

Definition at line 247 of file agent/parser_context.h.

Referenced by includeFile().

## ◆ sfile_

FILE* isc::agent::ParserContext::sfile_

sFile (aka FILE)

Definition at line 253 of file agent/parser_context.h.

Referenced by includeFile(), scanEnd(), scanFileBegin(), and scanStringBegin().

## ◆ sfiles_

std::vector<FILE*> isc::agent::ParserContext::sfiles_

sFile (aka FILE) stack

This is a stack of files. Typically there's only one file (the one being currently parsed), but there may be more if one file includes another.

Definition at line 260 of file agent/parser_context.h.

Referenced by includeFile(), and scanEnd().

## ◆ stack_

std::vector<isc::data::ElementPtr> isc::agent::ParserContext::stack_

JSON elements being parsed.

Definition at line 66 of file agent/parser_context.h.

Referenced by isc::agent::AgentParser::parse(), require(), and unique().

## ◆ states_

std::vector<struct yy_buffer_state*> isc::agent::ParserContext::states_

Lexer state stack.

Definition at line 250 of file agent/parser_context.h.

Referenced by includeFile(), and scanEnd().

The documentation for this class was generated from the following files:

- agent/parser_context.h
- agent_lexer.cc
- agent/parser_context.cc