parser_context.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (C) 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. #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. : trace_scanning_(false), trace_parsing_(false)
  17. {
  18. }
  19. Parser6Context::~Parser6Context()
  20. {
  21. }
  22. isc::data::ConstElementPtr
  23. Parser6Context::parseString(const std::string& str, ParserType parser_type)
  24. {
  25. scanStringBegin(str, parser_type);
  26. isc::dhcp::Dhcp6Parser parser(*this);
  27. // Uncomment this to get detailed parser logs.
  28. // trace_parsing_ = true;
  29. parser.set_debug_level(trace_parsing_);
  30. int res = parser.parse();
  31. if (res != 0) {
  32. // @todo: handle exception here
  33. }
  34. scanStringEnd();
  35. if (stack_.size() == 1) {
  36. return (stack_[0]);
  37. } else {
  38. isc_throw(BadValue, "Expected exactly one terminal Element expected, found "
  39. << stack_.size());
  40. }
  41. }
  42. isc::data::ConstElementPtr
  43. Parser6Context::parseFile(const std::string& filename, ParserType parser_type) {
  44. FILE* f = fopen(filename.c_str(), "r");
  45. if (!f) {
  46. isc_throw(BadValue, "Unable to open file " << filename);
  47. }
  48. scanFileBegin(f, filename, parser_type);
  49. isc::dhcp::Dhcp6Parser parser(*this);
  50. // Uncomment this to get detailed parser logs.
  51. // trace_parsing_ = true;
  52. parser.set_debug_level(trace_parsing_);
  53. int res = parser.parse();
  54. if (res != 0) {
  55. // @todo: handle exception here
  56. }
  57. scanFileEnd(f);
  58. if (stack_.size() == 1) {
  59. return (stack_[0]);
  60. } else {
  61. isc_throw(BadValue, "Expected exactly one terminal Element expected, found "
  62. << stack_.size());
  63. }
  64. }
  65. void
  66. Parser6Context::error(const isc::dhcp::location& loc, const std::string& what)
  67. {
  68. isc_throw(EvalParseError, loc << ": " << what);
  69. }
  70. void
  71. Parser6Context::error (const std::string& what)
  72. {
  73. isc_throw(EvalParseError, what);
  74. }
  75. void
  76. Parser6Context::fatal (const std::string& what)
  77. {
  78. isc_throw(Unexpected, what);
  79. }
  80. void
  81. Parser6Context::enter(const ParserContext& ctx)
  82. {
  83. cstack_.push_back(ctx_);
  84. ctx_ = ctx;
  85. }
  86. void
  87. Parser6Context::leave()
  88. {
  89. #if 1
  90. if (cstack_.empty()) {
  91. fatal("unbalanced syntactic context");
  92. }
  93. #endif
  94. ctx_ = cstack_.back();
  95. cstack_.pop_back();
  96. }
  97. };
  98. };