parser_context.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <dhcp4/parser_context.h>
  7. #include <dhcp4/dhcp4_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. Parser4Context::Parser4Context()
  16. : trace_scanning_(false), trace_parsing_(false), ctx_(NO_KEYWORD)
  17. {
  18. }
  19. Parser4Context::~Parser4Context()
  20. {
  21. }
  22. isc::data::ElementPtr
  23. Parser4Context::parseString(const std::string& str, ParserType parser_type)
  24. {
  25. scanStringBegin(str, parser_type);
  26. return (parseCommon());
  27. }
  28. isc::data::ElementPtr
  29. Parser4Context::parseFile(const std::string& filename, ParserType parser_type) {
  30. FILE* f = fopen(filename.c_str(), "r");
  31. if (!f) {
  32. isc_throw(Dhcp4ParseError, "Unable to open file " << filename);
  33. }
  34. scanFileBegin(f, filename, parser_type);
  35. return (parseCommon());
  36. }
  37. isc::data::ElementPtr
  38. Parser4Context::parseCommon() {
  39. isc::dhcp::Dhcp4Parser 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(Dhcp4ParseError, "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(Dhcp4ParseError, "Expected exactly one terminal Element expected, found "
  58. << stack_.size());
  59. }
  60. }
  61. void
  62. Parser4Context::error(const isc::dhcp::location& loc, const std::string& what)
  63. {
  64. isc_throw(Dhcp4ParseError, loc << ": " << what);
  65. }
  66. void
  67. Parser4Context::error (const std::string& what)
  68. {
  69. isc_throw(Dhcp4ParseError, what);
  70. }
  71. void
  72. Parser4Context::fatal (const std::string& what)
  73. {
  74. isc_throw(Dhcp4ParseError, what);
  75. }
  76. isc::data::Element::Position
  77. Parser4Context::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. Parser4Context::enter(const ParserContext& ctx)
  86. {
  87. cstack_.push_back(ctx_);
  88. ctx_ = ctx;
  89. }
  90. void
  91. Parser4Context::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. Parser4Context::contextName()
  103. {
  104. switch (ctx_) {
  105. case NO_KEYWORD:
  106. return ("__no keyword__");
  107. case CONFIG:
  108. return ("toplevel");
  109. case DHCP4:
  110. return ("Dhcp4");
  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 HOST_RESERVATION_IDENTIFIERS:
  120. return ("host-reservation-identifiers");
  121. case HOOKS_LIBRARIES:
  122. return ("hooks-librairies");
  123. case SUBNET4:
  124. return ("subnet4");
  125. case OPTION_DEF:
  126. return ("option-def");
  127. case OPTION_DATA:
  128. return ("option-data");
  129. case CLIENT_CLASSES:
  130. return ("client-classes");
  131. case SERVER_ID:
  132. return ("server-id");
  133. case CONTROL_SOCKET:
  134. return ("control-socket");
  135. case POOLS:
  136. return ("pools");
  137. case RESERVATIONS:
  138. return ("reservations");
  139. case RELAY:
  140. return ("relay");
  141. case CLIENT_CLASS:
  142. return ("client-class");
  143. case LOGGERS:
  144. return ("loggers");
  145. case OUTPUT_OPTIONS:
  146. return ("output-options");
  147. default:
  148. return ("__unknown__");
  149. }
  150. }
  151. };
  152. };