parser_context.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (C) 2016-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. #include <dhcp6/parser_context.h>
  7. #include <dhcp6/dhcp6_parser.h>
  8. #include <exceptions/exceptions.h>
  9. #include <cc/data.h>
  10. #include <boost/lexical_cast.hpp>
  11. #include <fstream>
  12. #include <limits>
  13. namespace isc {
  14. namespace dhcp {
  15. Parser6Context::Parser6Context()
  16. : ctx_(NO_KEYWORD), trace_scanning_(false), trace_parsing_(false)
  17. {
  18. }
  19. Parser6Context::~Parser6Context()
  20. {
  21. }
  22. isc::data::ElementPtr
  23. Parser6Context::parseString(const std::string& str, ParserType parser_type)
  24. {
  25. scanStringBegin(str, parser_type);
  26. return (parseCommon());
  27. }
  28. isc::data::ElementPtr
  29. Parser6Context::parseFile(const std::string& filename, ParserType parser_type) {
  30. FILE* f = fopen(filename.c_str(), "r");
  31. if (!f) {
  32. isc_throw(Dhcp6ParseError, "Unable to open file " << filename);
  33. }
  34. scanFileBegin(f, filename, parser_type);
  35. return (parseCommon());
  36. }
  37. isc::data::ElementPtr
  38. Parser6Context::parseCommon() {
  39. isc::dhcp::Dhcp6Parser parser(*this);
  40. // Uncomment this to get detailed parser logs.
  41. // trace_parsing_ = true;
  42. parser.set_debug_level(trace_parsing_);
  43. try {
  44. int res = parser.parse();
  45. if (res != 0) {
  46. isc_throw(Dhcp6ParseError, "Parser abort");
  47. }
  48. scanEnd();
  49. }
  50. catch (...) {
  51. scanEnd();
  52. throw;
  53. }
  54. if (stack_.size() == 1) {
  55. return (stack_[0]);
  56. } else {
  57. isc_throw(Dhcp6ParseError, "Expected exactly one terminal Element expected, found "
  58. << stack_.size());
  59. }
  60. }
  61. void
  62. Parser6Context::error(const isc::dhcp::location& loc, const std::string& what)
  63. {
  64. isc_throw(Dhcp6ParseError, loc << ": " << what);
  65. }
  66. void
  67. Parser6Context::error (const std::string& what)
  68. {
  69. isc_throw(Dhcp6ParseError, what);
  70. }
  71. void
  72. Parser6Context::fatal (const std::string& what)
  73. {
  74. isc_throw(Dhcp6ParseError, what);
  75. }
  76. isc::data::Element::Position
  77. Parser6Context::loc2pos(isc::dhcp::location& loc)
  78. {
  79. const std::string& file = *loc.begin.filename;
  80. const uint32_t line = loc.begin.line;
  81. const uint32_t pos = loc.begin.column;
  82. return (isc::data::Element::Position(file, line, pos));
  83. }
  84. void
  85. Parser6Context::enter(const ParserContext& ctx)
  86. {
  87. cstack_.push_back(ctx_);
  88. ctx_ = ctx;
  89. }
  90. void
  91. Parser6Context::leave()
  92. {
  93. #if 1
  94. if (cstack_.empty()) {
  95. fatal("unbalanced syntactic context");
  96. }
  97. #endif
  98. ctx_ = cstack_.back();
  99. cstack_.pop_back();
  100. }
  101. const std::string
  102. Parser6Context::contextName()
  103. {
  104. switch (ctx_) {
  105. case NO_KEYWORD:
  106. return ("__no keyword__");
  107. case CONFIG:
  108. return ("toplevel");
  109. case DHCP6:
  110. return ("Dhcp6");
  111. case LOGGING:
  112. return ("Logging");
  113. case INTERFACES_CONFIG:
  114. return ("interfaces-config");
  115. case LEASE_DATABASE:
  116. return ("lease-database");
  117. case HOSTS_DATABASE:
  118. return ("hosts-database");
  119. case MAC_SOURCES:
  120. return ("mac-sources");
  121. case HOST_RESERVATION_IDENTIFIERS:
  122. return ("host-reservation-identifiers");
  123. case HOOKS_LIBRARIES:
  124. return ("hooks-librairies");
  125. case SUBNET6:
  126. return ("subnet6");
  127. case OPTION_DEF:
  128. return ("option-def");
  129. case OPTION_DATA:
  130. return ("option-data");
  131. case CLIENT_CLASSES:
  132. return ("client-classes");
  133. case SERVER_ID:
  134. return ("server-id");
  135. case CONTROL_SOCKET:
  136. return ("control-socket");
  137. case POOLS:
  138. return ("pools");
  139. case PD_POOLS:
  140. return ("pd-pools");
  141. case RESERVATIONS:
  142. return ("reservations");
  143. case RELAY:
  144. return ("relay");
  145. case CLIENT_CLASS:
  146. return ("client-class");
  147. case LOGGERS:
  148. return ("loggers");
  149. case OUTPUT_OPTIONS:
  150. return ("output-options");
  151. default:
  152. return ("__unknown__");
  153. }
  154. }
  155. };
  156. };