eval_context.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <eval/eval_context.h>
  15. #include <eval/parser.h>
  16. #include <exceptions/exceptions.h>
  17. #include <fstream>
  18. EvalContext::EvalContext()
  19. : trace_scanning_(false), trace_parsing_(false)
  20. {
  21. }
  22. EvalContext::~EvalContext()
  23. {
  24. }
  25. int
  26. EvalContext::parseFile(const std::string &filename)
  27. {
  28. file = filename;
  29. scanBegin();
  30. isc::eval::EvalParser parser(*this);
  31. parser.set_debug_level(trace_parsing_);
  32. int res = parser.parse();
  33. scanEnd();
  34. return res;
  35. }
  36. int
  37. EvalContext::parseString(const std::string& str)
  38. {
  39. /// @todo: Is there any way for the parser to read from a stream,
  40. /// rather than a file? It would be better to use stringstream,
  41. /// but it seems that the lexer operates on FILE* interface.
  42. // Put the content into a file and then open that file for reading.
  43. remove("/tmp/eval");
  44. std::fstream f("/tmp/eval", std::ios::out);
  45. if (!f.good()) {
  46. isc_throw(isc::Unexpected, "Can't write /tmp/eval file");
  47. }
  48. f << str;
  49. f.close();
  50. return (parseFile("/tmp/eval"));
  51. }
  52. void
  53. EvalContext::error(const isc::eval::location& l, const std::string& m)
  54. {
  55. isc_throw(EvalError, l << ": " << m);
  56. }
  57. void
  58. EvalContext::error (const std::string& m)
  59. {
  60. isc_throw(EvalError, m);
  61. }