parser_context.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <dhcp6/dhcp6_parser.h>
  12. #include <dhcp6/parser_context_decl.h>
  13. #include <exceptions/exceptions.h>
  14. // Tell Flex the lexer's prototype ...
  15. #define YY_DECL isc::dhcp::Dhcp6Parser::symbol_type parser6_lex (Parser6Context& 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 Dhcp6ParseError : public isc::Exception {
  24. public:
  25. Dhcp6ParseError(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 Parser6Context
  30. {
  31. public:
  32. /// @brief Defines currently supported scopes
  33. ///
  34. /// Dhcp6Parser 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 Dhcp6 in it and all the parameters within that Dhcp6 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. /// 'Dhcp6' or 'reservations' around it. In such case the parser
  41. /// is being told to start parsing as PARSER_HOST_RESERVATION6.
  42. typedef enum {
  43. /// This parser will parse the content as generic JSON.
  44. PARSER_JSON,
  45. /// This parser will parse the content as Dhcp6 config wrapped in a map
  46. /// (that's the regular config file)
  47. PARSER_DHCP6,
  48. /// This parser will parse the content of Dhcp6 (without outer { } and
  49. /// without "Dhcp6"). It is mostly used in unit-tests as most of the
  50. /// unit-tests do not define the outer map and Dhcp6 entity, just the
  51. /// contents of it.
  52. SUBPARSER_DHCP6,
  53. /// This will parse the input as interfaces content.
  54. PARSER_INTERFACES,
  55. /// This will parse the input as Subnet6 content.
  56. PARSER_SUBNET6,
  57. /// This will parse the input as pool6 content.
  58. PARSER_POOL6,
  59. /// This will parse the input as pd-pool content.
  60. PARSER_PD_POOL,
  61. /// This will parse the input as host-reservation.
  62. PARSER_HOST_RESERVATION,
  63. /// This will parse the input as option definition.
  64. PARSER_OPTION_DEF,
  65. /// This will parse the input as option data.
  66. PARSER_OPTION_DATA,
  67. /// This will parse the input as hooks-library.
  68. PARSER_HOOKS_LIBRARY,
  69. /// This will parse the input as dhcp-ddns. (D2 client config)
  70. PARSER_DHCP_DDNS
  71. } ParserType;
  72. /// @brief Default constructor.
  73. Parser6Context();
  74. /// @brief destructor
  75. virtual ~Parser6Context();
  76. /// @brief JSON elements being parsed.
  77. std::vector<isc::data::ElementPtr> stack_;
  78. /// @brief Method called before scanning starts on a string.
  79. ///
  80. /// @param str string to be parsed
  81. /// @param type specifies expected content
  82. void scanStringBegin(const std::string& str, ParserType type);
  83. /// @brief Method called before scanning starts on a file.
  84. ///
  85. /// @param f stdio FILE pointer
  86. /// @param filename file to be parsed
  87. /// @param type specifies expected content
  88. void scanFileBegin(FILE* f, const std::string& filename, ParserType type);
  89. /// @brief Method called after the last tokens are scanned.
  90. void scanEnd();
  91. /// @brief Divert input to an include file.
  92. ///
  93. /// @param filename file to be included
  94. void includeFile(const std::string& filename);
  95. /// @brief Run the parser on the string specified.
  96. ///
  97. /// This method parses specified string. Depending on the value of
  98. /// parser_type, parser may either check only that the input is valid
  99. /// JSON, or may do more specific syntax checking. See @ref ParserType
  100. /// for supported syntax checkers.
  101. ///
  102. /// @param str string to be parsed
  103. /// @param parser_type specifies expected content (usually DHCP6 or generic JSON)
  104. /// @return Element structure representing parsed text.
  105. isc::data::ElementPtr parseString(const std::string& str,
  106. ParserType parser_type);
  107. /// @brief Run the parser on the file specified.
  108. ///
  109. /// This method parses specified file. Depending on the value of
  110. /// parser_type, parser may either check only that the input is valid
  111. /// JSON, or may do more specific syntax checking. See @ref ParserType
  112. /// for supported syntax checkers.
  113. ///
  114. /// @param filename file to be parsed
  115. /// @param parser_type specifies expected content (usually DHCP6 or generic JSON)
  116. /// @return Element structure representing parsed text.
  117. isc::data::ElementPtr parseFile(const std::string& filename,
  118. ParserType parser_type);
  119. /// @brief Error handler
  120. ///
  121. /// @param loc location within the parsed file when experienced a problem.
  122. /// @param what string explaining the nature of the error.
  123. /// @throw Dhcp6ParseError
  124. void error(const isc::dhcp::location& loc, const std::string& what);
  125. /// @brief Error handler
  126. ///
  127. /// This is a simplified error reporting tool for possible future
  128. /// cases when the Dhcp6Parser is not able to handle the packet.
  129. ///
  130. /// @param what string explaining the nature of the error.
  131. /// @throw Dhcp6ParseError
  132. void error(const std::string& what);
  133. /// @brief Fatal error handler
  134. ///
  135. /// This is for should not happen but fatal errors.
  136. /// Used by YY_FATAL_ERROR macro so required to be static.
  137. ///
  138. /// @param what string explaining the nature of the error.
  139. /// @throw Dhcp6ParseError
  140. static void fatal(const std::string& what);
  141. /// @brief Converts bison's position to one understandable by isc::data::Element
  142. ///
  143. /// Convert a bison location into an element position
  144. /// (take the begin, the end is lost)
  145. ///
  146. /// @param loc location in bison format
  147. /// @return Position in format accepted by Element
  148. isc::data::Element::Position loc2pos(isc::dhcp::location& loc);
  149. /// @brief Defines syntactic contexts for lexical tie-ins
  150. typedef enum {
  151. ///< This one is used in pure JSON mode.
  152. NO_KEYWORD,
  153. ///< Used while parsing top level (that contains Dhcp6, Logging and others)
  154. CONFIG,
  155. ///< Used while parsing content of Dhcp6.
  156. DHCP6,
  157. // not yet Dhcp4, DhcpDdns,
  158. ///< Used while parsing content of Logging
  159. LOGGING,
  160. /// Used while parsing Dhcp6/interfaces structures.
  161. INTERFACES_CONFIG,
  162. /// Used while parsing Dhcp6/lease-database structures.
  163. LEASE_DATABASE,
  164. /// Used while parsing Dhcp6/hosts-database structures.
  165. HOSTS_DATABASE,
  166. /// Used while parsing Dhcp6/*-database/type.
  167. DATABASE_TYPE,
  168. /// Used while parsing Dhcp6/mac-sources structures.
  169. MAC_SOURCES,
  170. /// Used while parsing Dhcp6/host-reservation-identifiers.
  171. HOST_RESERVATION_IDENTIFIERS,
  172. /// Used while parsing Dhcp6/hooks-libraries.
  173. HOOKS_LIBRARIES,
  174. /// Used while parsing Dhcp6/Subnet6 structures.
  175. SUBNET6,
  176. /// Used while parsing Dhcp6/Subnet6/reservation-mode.
  177. RESERVATION_MODE,
  178. /// Used while parsing Dhcp6/option-def structures.
  179. OPTION_DEF,
  180. /// Used while parsing Dhcp6/option-data, Dhcp6/subnet6/option-data
  181. /// or anywhere option-data is present (client classes, host
  182. /// reservations and possibly others).
  183. OPTION_DATA,
  184. /// Used while parsing Dhcp6/client-classes structures.
  185. CLIENT_CLASSES,
  186. /// Used while parsing Dhcp6/expired-leases-processing.
  187. EXPIRED_LEASES_PROCESSING,
  188. /// Used while parsing Dhcp6/server-id structures.
  189. SERVER_ID,
  190. /// Used while parsing Dhcp6/server-id/type structures.
  191. DUID_TYPE,
  192. /// Used while parsing Dhcp6/control-socket structures.
  193. CONTROL_SOCKET,
  194. /// Used while parsing Dhcp6/subnet6/pools structures.
  195. POOLS,
  196. /// Used while parsing Dhcp6/subnet6/pd-pools structures.
  197. PD_POOLS,
  198. /// Used while parsing Dhcp6/reservations structures.
  199. RESERVATIONS,
  200. /// Used while parsing Dhcp6/subnet6/relay structures.
  201. RELAY,
  202. /// Used while parsing Dhcp6/client-classes structures.
  203. CLIENT_CLASS,
  204. /// Used while parsing Logging/loggers structures.
  205. LOGGERS,
  206. /// Used while parsing Logging/loggers/output_options structures.
  207. OUTPUT_OPTIONS,
  208. /// Used while parsing Dhcp6/dhcp-ddns.
  209. DHCP_DDNS,
  210. /// Used while parsing Dhcp6/dhcp-ddns/ncr-protocol
  211. NCR_PROTOCOL,
  212. /// Used while parsing Dhcp6/dhcp-ddns/ncr-format
  213. NCR_FORMAT,
  214. /// Used while parsing Dhcp6/dhcp-ddns/replace-client-name.
  215. REPLACE_CLIENT_NAME
  216. } ParserContext;
  217. /// @brief File name
  218. std::string file_;
  219. /// @brief File name stack
  220. std::vector<std::string> files_;
  221. /// @brief Location of the current token
  222. ///
  223. /// The lexer will keep updating it. This variable will be useful
  224. /// for logging errors.
  225. isc::dhcp::location loc_;
  226. /// @brief Location stack
  227. std::vector<isc::dhcp::location> locs_;
  228. /// @brief Lexer state stack
  229. std::vector<struct yy_buffer_state*> states_;
  230. /// @brief sFile (aka FILE)
  231. FILE* sfile_;
  232. /// @brief sFile (aka FILE) stack
  233. ///
  234. /// This is a stack of files. Typically there's only one file (the
  235. /// one being currently parsed), but there may be more if one
  236. /// file includes another.
  237. std::vector<FILE*> sfiles_;
  238. /// @brief Current syntactic context
  239. ParserContext ctx_;
  240. /// @brief Enter a new syntactic context
  241. ///
  242. /// Entering a new syntactic context is useful in several ways.
  243. /// First, it allows the parser to avoid conflicts. Second, it
  244. /// allows the lexer to return different tokens depending on
  245. /// context (e.g. if "renew-timer" string is detected, the lexer
  246. /// will return STRING token if in JSON mode or RENEW_TIMER if
  247. /// in DHCP6 mode. Finally, the syntactic context allows the
  248. /// error message to be more descriptive if the input string
  249. /// does not parse properly.
  250. ///
  251. /// @param ctx the syntactic context to enter into
  252. void enter(const ParserContext& ctx);
  253. /// @brief Leave a syntactic context
  254. ///
  255. /// @throw isc::Unexpected if unbalanced
  256. void leave();
  257. /// @brief Get the syntactic context name
  258. ///
  259. /// @return printable name of the context.
  260. const std::string contextName();
  261. private:
  262. /// @brief Flag determining scanner debugging.
  263. bool trace_scanning_;
  264. /// @brief Flag determining parser debugging.
  265. bool trace_parsing_;
  266. /// @brief Syntactic context stack
  267. std::vector<ParserContext> cstack_;
  268. /// @brief Common part of parseXXX
  269. ///
  270. /// @return Element structure representing parsed text.
  271. isc::data::ElementPtr parseCommon();
  272. };
  273. }; // end of isc::eval namespace
  274. }; // end of isc namespace
  275. #endif