parser_context.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. file_ = "<string>";
  26. scanStringBegin(str, parser_type);
  27. isc::dhcp::Dhcp6Parser parser(*this);
  28. // Uncomment this to get detailed parser logs.
  29. // trace_parsing_ = true;
  30. parser.set_debug_level(trace_parsing_);
  31. int res = parser.parse();
  32. if (res != 0) {
  33. // @todo: handle exception here
  34. }
  35. scanStringEnd();
  36. if (stack_.size() == 1) {
  37. return (stack_[0]);
  38. } else {
  39. isc_throw(BadValue, "Expected exactly one terminal Element expected, found "
  40. << stack_.size());
  41. }
  42. }
  43. isc::data::ConstElementPtr
  44. Parser6Context::parseFile(const std::string& filename, ParserType parser_type) {
  45. FILE* f = fopen(filename.c_str(), "r");
  46. if (!f) {
  47. isc_throw(BadValue, "Unable to open file " << filename);
  48. }
  49. file_ = filename;
  50. scanFileBegin(f, parser_type);
  51. isc::dhcp::Dhcp6Parser parser(*this);
  52. // Uncomment this to get detailed parser logs.
  53. // trace_parsing_ = true;
  54. parser.set_debug_level(trace_parsing_);
  55. int res = parser.parse();
  56. if (res != 0) {
  57. // @todo: handle exception here
  58. }
  59. scanFileEnd(f);
  60. if (stack_.size() == 1) {
  61. return (stack_[0]);
  62. } else {
  63. isc_throw(BadValue, "Expected exactly one terminal Element expected, found "
  64. << stack_.size());
  65. }
  66. }
  67. void
  68. Parser6Context::error(const isc::dhcp::location& loc, const std::string& what)
  69. {
  70. isc_throw(EvalParseError, loc << ": " << what);
  71. }
  72. void
  73. Parser6Context::error (const std::string& what)
  74. {
  75. isc_throw(EvalParseError, what);
  76. }
  77. void
  78. Parser6Context::fatal (const std::string& what)
  79. {
  80. isc_throw(Unexpected, what);
  81. }
  82. };
  83. };