parser.yy 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  2. Permission to use, copy, modify, and/or distribute this software for any
  3. purpose with or without fee is hereby granted, provided that the above
  4. copyright notice and this permission notice appear in all copies.
  5. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  6. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  7. AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  8. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  9. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  10. OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  11. PERFORMANCE OF THIS SOFTWARE. */
  12. %skeleton "lalr1.cc" /* -*- C++ -*- */
  13. %require "3.0.0"
  14. %defines
  15. %define parser_class_name {EvalParser}
  16. %define api.token.constructor
  17. %define api.value.type variant
  18. %define api.namespace {isc::eval}
  19. %define parse.assert
  20. %code requires
  21. {
  22. #include <string>
  23. #include <eval/token.h>
  24. #include <eval/eval_context_decl.h>
  25. using namespace isc::dhcp;
  26. using namespace isc::eval;
  27. }
  28. // The parsing context.
  29. %param { EvalContext& ctx }
  30. %locations
  31. %initial-action
  32. {
  33. // Initialize the initial location.
  34. @$.begin.filename = @$.end.filename = &ctx.file_;
  35. };
  36. %define parse.trace
  37. %define parse.error verbose
  38. %code
  39. {
  40. # include "eval_context.h"
  41. }
  42. %define api.token.prefix {TOKEN_}
  43. %token
  44. END 0 "end of file"
  45. EQUAL "=="
  46. OPTION "option"
  47. SUBSTRING "substring"
  48. COMA ","
  49. LPAREN "("
  50. RPAREN ")"
  51. LBRACKET "["
  52. RBRACKET "]"
  53. ;
  54. %token <std::string> STRING "constant string"
  55. %token <std::string> HEXSTRING "constant hexstring"
  56. %token <uint16_t> CODE "option code"
  57. %printer { yyoutput << $$; } <*>;
  58. %%
  59. // The whole grammar starts with an expression.
  60. %start expression;
  61. // Expression can either be a single token or a (something == something) expression
  62. expression:
  63. token EQUAL token {
  64. TokenPtr eq(new TokenEqual());
  65. ctx.expression.push_back(eq);
  66. }
  67. | token
  68. ;
  69. token:
  70. STRING {
  71. TokenPtr str(new TokenString($1));
  72. ctx.expression.push_back(str);
  73. }
  74. | HEXSTRING {
  75. TokenPtr hex(new TokenHexString($1));
  76. ctx.expression.push_back(hex);
  77. }
  78. | OPTION "[" CODE "]" {
  79. TokenPtr opt(new TokenOption($3));
  80. ctx.expression.push_back(opt);
  81. }
  82. | SUBSTRING "(" token "," token "," token ")" {
  83. TokenPtr sub(new TokenSubstring());
  84. ctx.expression.push_back(sub);
  85. }
  86. ;
  87. %%
  88. void
  89. isc::eval::EvalParser::error(const location_type& loc,
  90. const std::string& what)
  91. {
  92. ctx.error(loc, what);
  93. }