parser_context.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (C) 2015-2016 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. class Dhcp6ParseError : public isc::Exception {
  22. public:
  23. Dhcp6ParseError(const char* file, size_t line, const char* what) :
  24. isc::Exception(file, line, what) { };
  25. };
  26. /// @brief Evaluation context, an interface to the expression evaluation.
  27. class Parser6Context
  28. {
  29. public:
  30. /// @brief Defines currently support the content supported
  31. typedef enum {
  32. PARSER_GENERIC_JSON, // This will parse the content as generic JSON
  33. PARSER_DHCP6 // This will parse the content as DHCP6 config
  34. } ParserType;
  35. /// @brief Default constructor.
  36. Parser6Context();
  37. /// @brief destructor
  38. virtual ~Parser6Context();
  39. /// @brief JSON elements being parsed.
  40. std::vector<isc::data::ElementPtr> stack_;
  41. /// @brief Method called before scanning starts on a string.
  42. void scanStringBegin(const std::string& str, ParserType type);
  43. /// @brief Method called before scanning starts on a file.
  44. void scanFileBegin(FILE * f, const std::string& filename, ParserType type);
  45. /// @brief Method called after the last tokens are scanned.
  46. void scanEnd();
  47. /// @brief Divert input to an include file.
  48. void includeFile(const std::string& filename);
  49. /// @brief Run the parser on the string specified.
  50. ///
  51. /// @param str string to be parsed
  52. /// @param parser_type specifies expected content (either DHCP6 or generic JSON)
  53. /// @return true on success.
  54. isc::data::ConstElementPtr parseString(const std::string& str,
  55. ParserType parser_type);
  56. /// @brief Run the parser on the file specified.
  57. isc::data::ConstElementPtr parseFile(const std::string& filename,
  58. ParserType parser_type);
  59. /// @brief Error handler
  60. ///
  61. /// @param loc location within the parsed file when experienced a problem.
  62. /// @param what string explaining the nature of the error.
  63. void error(const isc::dhcp::location& loc, const std::string& what);
  64. /// @brief Error handler
  65. ///
  66. /// This is a simplified error reporting tool for possible future
  67. /// cases when the Dhcp6Parser is not able to handle the packet.
  68. void error(const std::string& what);
  69. /// @brief Fatal error handler
  70. ///
  71. /// This is for should not happen but fatal errors.
  72. /// Used by YY_FATAL_ERROR macro so required to be static.
  73. static void fatal(const std::string& what);
  74. /// @brief Defines syntactic contexts for lexical tie-ins
  75. typedef enum {
  76. /// at toplevel
  77. NO_KEYWORD,
  78. CONFIG,
  79. /// in config
  80. DHCP6,
  81. // not yet DHCP4,
  82. // not yet DHCP_DDNS,
  83. LOGGING,
  84. /// Dhcp6
  85. INTERFACES_CONFIG,
  86. LEASE_DATABASE,
  87. HOSTS_DATABASE,
  88. MAC_SOURCES,
  89. HOST_RESERVATION_IDENTIFIERS,
  90. HOOKS_LIBRARIES,
  91. SUBNET6,
  92. OPTION_DATA,
  93. CLIENT_CLASSES,
  94. SERVER_ID,
  95. DHCP_DDNS,
  96. /// subnet6
  97. POOLS,
  98. PD_POOLS,
  99. RESERVATIONS,
  100. /// client-classes
  101. CLIENT_CLASS,
  102. /// Logging
  103. LOGGERS,
  104. /// loggers
  105. OUTPUT_OPTIONS
  106. } ParserContext;
  107. /// @brief File name
  108. std::string file_;
  109. /// @brief File name stack
  110. std::vector<std::string> files_;
  111. /// @brief Location of the current token
  112. ///
  113. /// The lexer will keep updating it. This variable will be useful
  114. /// for logging errors.
  115. isc::dhcp::location loc_;
  116. /// @brief Location stack
  117. std::vector<isc::dhcp::location> locs_;
  118. /// @brief Lexer state stack
  119. std::vector<struct yy_buffer_state*> states_;;
  120. /// @brief sFile (aka FILE)
  121. FILE* sfile_;
  122. /// @brief sFile (aka FILE) stack
  123. std::vector<FILE*> sfiles_;
  124. /// @brief Current syntactic context
  125. ParserContext ctx_;
  126. /// @brief Enter a new syntactic context
  127. void enter(const ParserContext& ctx);
  128. /// @brief Leave a syntactic context
  129. /// @throw isc::Unexpected if unbalanced
  130. void leave();
  131. /// @brief Get the syntactix context name
  132. const std::string context_name();
  133. private:
  134. /// @brief Flag determining scanner debugging.
  135. bool trace_scanning_;
  136. /// @brief Flag determing parser debugging.
  137. bool trace_parsing_;
  138. /// @brief Syntactic context stack
  139. std::vector<ParserContext> cstack_;
  140. /// @brief Common part of parseXXX
  141. isc::data::ConstElementPtr parseCommon();
  142. };
  143. }; // end of isc::eval namespace
  144. }; // end of isc namespace
  145. #endif