parser_context.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Copyright (C) 2015-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef PARSER_CONTEXT_H
  7. #define PARSER_CONTEXT_H
  8. #include <string>
  9. #include <map>
  10. #include <vector>
  11. #include <dhcp4/dhcp4_parser.h>
  12. #include <dhcp4/parser_context_decl.h>
  13. #include <exceptions/exceptions.h>
  14. // Tell Flex the lexer's prototype ...
  15. #define YY_DECL isc::dhcp::Dhcp4Parser::symbol_type parser4_lex (Parser4Context& driver)
  16. // ... and declare it for the parser's sake.
  17. YY_DECL;
  18. namespace isc {
  19. namespace dhcp {
  20. /// @brief Evaluation error exception raised when trying to parse.
  21. ///
  22. /// @todo: This probably should be common for Dhcp4 and Dhcp6.
  23. class Dhcp4ParseError : public isc::Exception {
  24. public:
  25. Dhcp4ParseError(const char* file, size_t line, const char* what) :
  26. isc::Exception(file, line, what) { };
  27. };
  28. /// @brief Evaluation context, an interface to the expression evaluation.
  29. class Parser4Context
  30. {
  31. public:
  32. /// @brief Defines currently supported scopes
  33. ///
  34. /// Dhcp4Parser is able to parse several types of scope. Usually,
  35. /// when it parses a config file, it expects the data to have a map
  36. /// with Dhcp4 in it and all the parameters within that Dhcp4 map.
  37. /// However, sometimes the parser is expected to parse only a subset
  38. /// of that information. For example, it may be asked to parse
  39. /// a structure that is host-reservation only, without the global
  40. /// 'Dhcp4' or 'reservations' around it. In such case the parser
  41. /// is being told to start parsing as PARSER_HOST_RESERVATION4.
  42. typedef enum {
  43. /// This parser will parse the content as generic JSON.
  44. PARSER_JSON,
  45. /// This parser will parse the content as Dhcp4 config wrapped in a map
  46. /// (that's the regular config file)
  47. PARSER_DHCP4,
  48. /// This parser will parse the content of Dhcp4 (without outer { } and
  49. /// without "Dhcp4"). It is mostly used in unit-tests as most of the
  50. /// unit-tests do not define the outer map and Dhcp4 entity, just the
  51. /// contents of it.
  52. SUBPARSER_DHCP4,
  53. /// This will parse the input as interfaces content.
  54. PARSER_INTERFACES,
  55. /// This will parse the input as Subnet4 content.
  56. PARSER_SUBNET4,
  57. /// This will parse the input as pool4 content.
  58. PARSER_POOL4,
  59. /// This will parse the input as host-reservation.
  60. PARSER_HOST_RESERVATION,
  61. /// This will parse the input as option definition.
  62. PARSER_OPTION_DEF,
  63. /// This will parse the input as option data.
  64. PARSER_OPTION_DATA,
  65. /// This will parse the input as hooks-library.
  66. PARSER_HOOKS_LIBRARY
  67. } ParserType;
  68. /// @brief Default constructor.
  69. Parser4Context();
  70. /// @brief destructor
  71. virtual ~Parser4Context();
  72. /// @brief JSON elements being parsed.
  73. std::vector<isc::data::ElementPtr> stack_;
  74. /// @brief Method called before scanning starts on a string.
  75. ///
  76. /// @param str string to be parsed
  77. /// @param type specifies expected content
  78. void scanStringBegin(const std::string& str, ParserType type);
  79. /// @brief Method called before scanning starts on a file.
  80. ///
  81. /// @param f stdio FILE pointer
  82. /// @param filename file to be parsed
  83. /// @param type specifies expected content
  84. void scanFileBegin(FILE* f, const std::string& filename, ParserType type);
  85. /// @brief Method called after the last tokens are scanned.
  86. void scanEnd();
  87. /// @brief Divert input to an include file.
  88. ///
  89. /// @param filename file to be included
  90. void includeFile(const std::string& filename);
  91. /// @brief Run the parser on the string specified.
  92. ///
  93. /// This method parses specified string. Depending on the value of
  94. /// parser_type, parser may either check only that the input is valid
  95. /// JSON, or may do more specific syntax checking. See @ref ParserType
  96. /// for supported syntax checkers.
  97. ///
  98. /// @param str string to be parsed
  99. /// @param parser_type specifies expected content (usually DHCP4 or generic JSON)
  100. /// @return Element structure representing parsed text.
  101. isc::data::ElementPtr parseString(const std::string& str,
  102. ParserType parser_type);
  103. /// @brief Run the parser on the file specified.
  104. ///
  105. /// This method parses specified file. Depending on the value of
  106. /// parser_type, parser may either check only that the input is valid
  107. /// JSON, or may do more specific syntax checking. See @ref ParserType
  108. /// for supported syntax checkers.
  109. ///
  110. /// @param filename file to be parsed
  111. /// @param parser_type specifies expected content (usually DHCP4 or generic JSON)
  112. /// @return Element structure representing parsed text.
  113. isc::data::ElementPtr parseFile(const std::string& filename,
  114. ParserType parser_type);
  115. /// @brief Error handler
  116. ///
  117. /// @param loc location within the parsed file when experienced a problem.
  118. /// @param what string explaining the nature of the error.
  119. /// @throw Dhcp4ParseError
  120. void error(const isc::dhcp::location& loc, const std::string& what);
  121. /// @brief Error handler
  122. ///
  123. /// This is a simplified error reporting tool for possible future
  124. /// cases when the Dhcp4Parser is not able to handle the packet.
  125. ///
  126. /// @param what string explaining the nature of the error.
  127. /// @throw Dhcp4ParseError
  128. void error(const std::string& what);
  129. /// @brief Fatal error handler
  130. ///
  131. /// This is for should not happen but fatal errors.
  132. /// Used by YY_FATAL_ERROR macro so required to be static.
  133. ///
  134. /// @param what string explaining the nature of the error.
  135. /// @throw Dhcp4ParseError
  136. static void fatal(const std::string& what);
  137. /// @brief Converts bison's position to one understandable by isc::data::Element
  138. ///
  139. /// Convert a bison location into an element position
  140. /// (take the begin, the end is lost)
  141. ///
  142. /// @param loc location in bison format
  143. /// @return Position in format accepted by Element
  144. isc::data::Element::Position loc2pos(isc::dhcp::location& loc);
  145. /// @brief Defines syntactic contexts for lexical tie-ins
  146. typedef enum {
  147. ///< This one is used in pure JSON mode.
  148. NO_KEYWORD,
  149. ///< Used while parsing top level (that contains Dhcp4, Logging and others)
  150. CONFIG,
  151. ///< Used while parsing content of Dhcp4.
  152. DHCP4,
  153. // not yet DHCP6,
  154. // not yet DHCP_DDNS,
  155. ///< Used while parsing content of Logging
  156. LOGGING,
  157. /// Used while parsing Dhcp4/interfaces structures.
  158. INTERFACES_CONFIG,
  159. /// Used while parsing Dhcp4/lease-database structures.
  160. LEASE_DATABASE,
  161. /// Used while parsing Dhcp4/hosts-database structures.
  162. HOSTS_DATABASE,
  163. /// Used while parsing Dhcp4/host-reservation-identifiers.
  164. HOST_RESERVATION_IDENTIFIERS,
  165. /// Used while parsing Dhcp4/hooks-libraries.
  166. HOOKS_LIBRARIES,
  167. /// Used while parsing Dhcp4/Subnet4 structures.
  168. SUBNET4,
  169. /// Used while parsing Dhcp4/option-def structures.
  170. OPTION_DEF,
  171. /// Used while parsing Dhcp4/option-data, Dhcp4/subnet4/option-data
  172. /// or anywhere option-data is present (client classes, host
  173. /// reservations and possibly others).
  174. OPTION_DATA,
  175. /// Used while parsing Dhcp4/client-classes structures.
  176. CLIENT_CLASSES,
  177. /// Used while parsing Dhcp4/expired-leases-processing.
  178. EXPIRED_LEASES_PROCESSING,
  179. /// Used while parsing Dhcp4/server-id structures.
  180. SERVER_ID,
  181. /// Used while parsing Dhcp4/control-socket structures.
  182. CONTROL_SOCKET,
  183. /// Used while parsing Dhcp4/subnet4/pools structures.
  184. POOLS,
  185. /// Used while parsing Dhcp4/reservations structures.
  186. RESERVATIONS,
  187. /// Used while parsing Dhcp4/subnet4relay structures.
  188. RELAY,
  189. /// Used while parsing Dhcp4/client-classes structures.
  190. CLIENT_CLASS,
  191. /// Used while parsing Logging/loggers structures.
  192. LOGGERS,
  193. /// Used while parsing Logging/loggers/output_options structures.
  194. OUTPUT_OPTIONS
  195. } ParserContext;
  196. /// @brief File name
  197. std::string file_;
  198. /// @brief File name stack
  199. std::vector<std::string> files_;
  200. /// @brief Location of the current token
  201. ///
  202. /// The lexer will keep updating it. This variable will be useful
  203. /// for logging errors.
  204. isc::dhcp::location loc_;
  205. /// @brief Location stack
  206. std::vector<isc::dhcp::location> locs_;
  207. /// @brief Lexer state stack
  208. std::vector<struct yy_buffer_state*> states_;
  209. /// @brief sFile (aka FILE)
  210. FILE* sfile_;
  211. /// @brief sFile (aka FILE) stack
  212. ///
  213. /// This is a stack of files. Typically there's only one file (the
  214. /// one being currently parsed), but there may be more if one
  215. /// file includes another.
  216. std::vector<FILE*> sfiles_;
  217. /// @brief Current syntactic context
  218. ParserContext ctx_;
  219. /// @brief Enter a new syntactic context
  220. ///
  221. /// Entering a new syntactic context is useful in several ways.
  222. /// First, it allows the parser to avoid conflicts. Second, it
  223. /// allows the lexer to return different tokens depending on
  224. /// context (e.g. if "renew-timer" string is detected, the lexer
  225. /// will return STRING token if in JSON mode or RENEW_TIMER if
  226. /// in DHCP4 mode. Finally, the syntactic context allows the
  227. /// error message to be more descriptive if the input string
  228. /// does not parse properly.
  229. ///
  230. /// @param ctx the syntactic context to enter into
  231. void enter(const ParserContext& ctx);
  232. /// @brief Leave a syntactic context
  233. ///
  234. /// @throw isc::Unexpected if unbalanced
  235. void leave();
  236. /// @brief Get the syntactix context name
  237. ///
  238. /// @return printable name of the context.
  239. const std::string contextName();
  240. private:
  241. /// @brief Flag determining scanner debugging.
  242. bool trace_scanning_;
  243. /// @brief Flag determing parser debugging.
  244. bool trace_parsing_;
  245. /// @brief Syntactic context stack
  246. std::vector<ParserContext> cstack_;
  247. /// @brief Common part of parseXXX
  248. ///
  249. /// @return Element structure representing parsed text.
  250. isc::data::ElementPtr parseCommon();
  251. };
  252. }; // end of isc::eval namespace
  253. }; // end of isc namespace
  254. #endif