Kea 2.7.5
|
Parser context is a wrapper around flex/bison instances dedicated to Netconf-agent config file parser. More...
#include <parser_context.h>
Public Types | |
enum | LexerContext { NO_KEYWORDS , CONFIG , NETCONF , MANAGED_SERVERS , SERVER , CONTROL_SOCKET , SOCKET_TYPE , HOOKS_LIBRARIES , LOGGERS , OUTPUT_OPTIONS } |
Defines syntactic contexts for lexical tie-ins. More... | |
enum | ParserType { PARSER_JSON , PARSER_NETCONF , PARSER_SUB_NETCONF } |
Defines currently supported scopes. More... | |
Public Member Functions | |
ParserContext () | |
Default constructor. | |
virtual | ~ParserContext () |
destructor | |
const std::string | contextName () |
Get the syntactic context name. | |
void | enter (const LexerContext &ctx) |
Enter a new syntactic context. | |
void | error (const isc::netconf::location &loc, const std::string &what, size_t pos=0) |
Error handler. | |
void | error (const std::string &what) |
Error handler. | |
void | includeFile (const std::string &filename) |
Divert input to an include file. | |
void | leave () |
Leave a syntactic context. | |
isc::data::Element::Position | loc2pos (isc::netconf::location &loc) |
Converts bison's position to one understandable by isc::data::Element. | |
isc::data::ElementPtr | parseFile (const std::string &filename, ParserType parser_type) |
Run the parser on the file specified. | |
isc::data::ElementPtr | parseString (const std::string &str, ParserType parser_type) |
Run the parser on the string specified. | |
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. | |
void | scanEnd () |
Method called after the last tokens are scanned. | |
void | scanFileBegin (FILE *f, const std::string &filename, ParserType type) |
Method called before scanning starts on a file. | |
void | scanStringBegin (const std::string &str, ParserType type) |
Method called before scanning starts on a string. | |
void | unique (const std::string &name, isc::data::Element::Position loc) |
Check if a parameter is already present. | |
void | warnAboutExtraCommas (const isc::netconf::location &loc) |
Warning for extra commas. | |
void | warning (const isc::netconf::location &loc, const std::string &what) |
Warning handler. | |
Static Public Member Functions | |
static void | fatal (const std::string &what) |
Fatal error handler. | |
Public Attributes | |
LexerContext | ctx_ |
Current syntactic context. | |
std::string | file_ |
File name. | |
std::vector< std::string > | files_ |
File name stack. | |
isc::netconf::location | loc_ |
Location of the current token. | |
std::vector< isc::netconf::location > | locs_ |
Location stack. | |
FILE * | sfile_ |
sFile (aka FILE) | |
std::vector< FILE * > | sfiles_ |
sFile (aka FILE) stack | |
std::vector< isc::data::ElementPtr > | stack_ |
JSON elements being parsed. | |
std::vector< struct yy_buffer_state * > | states_ |
Lexer state stack. | |
Parser context is a wrapper around flex/bison instances dedicated to Netconf-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 Netconf-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 37 of file netconf/parser_context.h.
Defines syntactic contexts for lexical tie-ins.
Definition at line 197 of file netconf/parser_context.h.
Defines currently supported scopes.
NetconfParser is able to parse several types of scope. Usually, when it parses a config file, it expects the data to have a map with Netconf-agent in it and all the parameters within that map. However, sometimes the parser is expected to parse only a subset of that information.
Definition at line 48 of file netconf/parser_context.h.
isc::netconf::ParserContext::ParserContext | ( | ) |
Default constructor.
Definition at line 22 of file netconf/parser_context.cc.
|
virtual |
destructor
Definition at line 27 of file netconf/parser_context.cc.
string const isc::netconf::ParserContext::contextName | ( | ) |
Get the syntactic context name.
Definition at line 156 of file netconf/parser_context.cc.
References CONFIG, CONTROL_SOCKET, ctx_, HOOKS_LIBRARIES, LOGGERS, MANAGED_SERVERS, NETCONF, NO_KEYWORDS, OUTPUT_OPTIONS, SERVER, and SOCKET_TYPE.
Referenced by isc::netconf::NetconfParser::parse(), require(), and unique().
void isc::netconf::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. Netconf 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.
ctx | the syntactic context to enter into |
Definition at line 139 of file netconf/parser_context.cc.
References ctx_.
Referenced by isc::netconf::NetconfParser::parse().
void isc::netconf::ParserContext::error | ( | const isc::netconf::location & | loc, |
const std::string & | what, | ||
size_t | pos = 0 ) |
Error handler.
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. |
ParseError |
Definition at line 73 of file netconf/parser_context.cc.
References isc_throw.
void isc::netconf::ParserContext::error | ( | const std::string & | what | ) |
Error handler.
This is a simplified error reporting tool for possible future cases when the NetconfParser is not able to handle the packet.
what | string explaining the nature of the error. |
ParseError |
Definition at line 85 of file netconf/parser_context.cc.
References isc_throw.
|
static |
Fatal error handler.
This is for should not happen but fatal errors. Used by YY_FATAL_ERROR macro so required to be static.
what | string explaining the nature of the error. |
ParseError |
Definition at line 91 of file netconf/parser_context.cc.
References isc_throw.
Referenced by leave().
void ParserContext::includeFile | ( | const std::string & | filename | ) |
Divert input to an include file.
filename | file to be included |
Definition at line 3942 of file netconf_lexer.cc.
References BEGIN, isc::db::fatal, file_, files_, INITIAL, loc_, locs_, sfile_, sfiles_, states_, and YY_CURRENT_BUFFER.
void isc::netconf::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.
isc::Unexpected | if unbalanced (more leave() than enter() calls) |
Definition at line 146 of file netconf/parser_context.cc.
Referenced by isc::netconf::NetconfParser::parse().
isc::data::Element::Position isc::netconf::ParserContext::loc2pos | ( | isc::netconf::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)
loc | location in bison format |
Definition at line 97 of file netconf/parser_context.cc.
Referenced by isc::netconf::NetconfParser::parse().
isc::data::ElementPtr isc::netconf::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.
filename | file to be parsed |
parser_type | specifies expected content (usually PARSER_NETCONF or PARSER_JSON) |
Definition at line 39 of file netconf/parser_context.cc.
References isc_throw, and scanFileBegin().
Referenced by isc::netconf::NetconfController::parseFile().
isc::data::ElementPtr isc::netconf::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.
str | string to be parsed |
parser_type | specifies expected content (usually NETCONF or generic JSON) |
Definition at line 32 of file netconf/parser_context.cc.
References scanStringBegin().
void isc::netconf::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.
name | name of the parameter to check |
open_loc | location of the opening curly bracket |
close_loc | location of the closing curly bracket |
ParseError |
Definition at line 106 of file netconf/parser_context.cc.
References contextName(), isc_throw, and stack_.
void ParserContext::scanEnd | ( | ) |
Method called after the last tokens are scanned.
Definition at line 3921 of file netconf_lexer.cc.
void ParserContext::scanFileBegin | ( | FILE * | f, |
const std::string & | filename, | ||
ParserType | type ) |
Method called before scanning starts on a file.
f | stdio FILE pointer |
filename | file to be parsed |
type | specifies expected content |
Definition at line 3899 of file netconf_lexer.cc.
References isc::db::fatal, file_, loc_, sfile_, and yy_flex_debug.
Referenced by parseFile().
void ParserContext::scanStringBegin | ( | const std::string & | str, |
ParserType | type ) |
Method called before scanning starts on a string.
str | string to be parsed |
type | specifies expected content |
Definition at line 3881 of file netconf_lexer.cc.
References isc::db::fatal, file_, loc_, sfile_, and yy_flex_debug.
Referenced by parseString().
void isc::netconf::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.
name | name of the parameter to check |
loc | location of the current parameter |
ParseError |
Definition at line 121 of file netconf/parser_context.cc.
References contextName(), ctx_, isc_throw, NO_KEYWORDS, and stack_.
Referenced by isc::netconf::NetconfParser::parse().
void isc::netconf::ParserContext::warnAboutExtraCommas | ( | const isc::netconf::location & | loc | ) |
Warning for extra commas.
loc | location within the parsed file of the extra comma |
ParseError |
Definition at line 194 of file netconf/parser_context.cc.
References warning().
Referenced by isc::netconf::NetconfParser::parse().
void isc::netconf::ParserContext::warning | ( | const isc::netconf::location & | loc, |
const std::string & | what ) |
Warning handler.
loc | location within the parsed file where the problem was experienced |
what | string explaining the nature of the error |
ParseError |
Definition at line 185 of file netconf/parser_context.cc.
References LOG_WARN, isc::netconf::NETCONF_CONFIG_SYNTAX_WARNING, and isc::netconf::netconf_logger.
Referenced by warnAboutExtraCommas().
LexerContext isc::netconf::ParserContext::ctx_ |
Current syntactic context.
Definition at line 259 of file netconf/parser_context.h.
Referenced by contextName(), enter(), leave(), isc::netconf::NetconfParser::parse(), and unique().
std::string isc::netconf::ParserContext::file_ |
File name.
Definition at line 231 of file netconf/parser_context.h.
Referenced by includeFile(), scanFileBegin(), and scanStringBegin().
std::vector<std::string> isc::netconf::ParserContext::files_ |
File name stack.
Definition at line 234 of file netconf/parser_context.h.
Referenced by includeFile().
isc::netconf::location isc::netconf::ParserContext::loc_ |
Location of the current token.
The lexer will keep updating it. This variable will be useful for logging errors.
Definition at line 240 of file netconf/parser_context.h.
Referenced by includeFile(), scanFileBegin(), and scanStringBegin().
std::vector<isc::netconf::location> isc::netconf::ParserContext::locs_ |
Location stack.
Definition at line 243 of file netconf/parser_context.h.
Referenced by includeFile().
FILE* isc::netconf::ParserContext::sfile_ |
sFile (aka FILE)
Definition at line 249 of file netconf/parser_context.h.
Referenced by includeFile(), scanEnd(), scanFileBegin(), and scanStringBegin().
std::vector<FILE*> isc::netconf::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 256 of file netconf/parser_context.h.
Referenced by includeFile(), and scanEnd().
std::vector<isc::data::ElementPtr> isc::netconf::ParserContext::stack_ |
JSON elements being parsed.
Definition at line 67 of file netconf/parser_context.h.
Referenced by isc::netconf::NetconfParser::parse(), require(), and unique().
std::vector<struct yy_buffer_state*> isc::netconf::ParserContext::states_ |
Lexer state stack.
Definition at line 246 of file netconf/parser_context.h.
Referenced by includeFile(), and scanEnd().