dhcp6_parser.yy 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
  2. This Source Code Form is subject to the terms of the Mozilla Public
  3. License, v. 2.0. If a copy of the MPL was not distributed with this
  4. file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. %skeleton "lalr1.cc" /* -*- C++ -*- */
  6. %require "3.0.0"
  7. %defines
  8. %define parser_class_name {Dhcp6Parser}
  9. %define api.token.constructor
  10. %define api.value.type variant
  11. %define api.namespace {isc::dhcp}
  12. %define parse.assert
  13. %code requires
  14. {
  15. #include <string>
  16. #include <cc/data.h>
  17. #include <dhcp/option.h>
  18. #include <boost/lexical_cast.hpp>
  19. #include <dhcp6/parser_context_decl.h>
  20. using namespace isc::dhcp;
  21. using namespace isc::data;
  22. using namespace std;
  23. }
  24. // The parsing context.
  25. %param { isc::dhcp::Parser6Context& ctx }
  26. %locations
  27. %define parse.trace
  28. %define parse.error verbose
  29. %code
  30. {
  31. #include <dhcp6/parser_context.h>
  32. }
  33. %define api.token.prefix {TOKEN_}
  34. // Tokens in an order which makes sense and related to the intented use.
  35. %token
  36. END 0 "end of file"
  37. COMMA ","
  38. COLON ":"
  39. LSQUARE_BRACKET "["
  40. RSQUARE_BRACKET "]"
  41. LCURLY_BRACKET "{"
  42. RCURLY_BRACKET "}"
  43. NULL_TYPE "null"
  44. ;
  45. %token <std::string> STRING "constant string"
  46. %token <int64_t> INTEGER "integer"
  47. %token <double> FLOAT "floating point"
  48. %token <bool> BOOLEAN "boolean"
  49. %type <ElementPtr> value
  50. %printer { yyoutput << $$; } <*>;
  51. %%
  52. // The whole grammar starts with a map, because the config file
  53. // constists of Dhcp, Logger and DhcpDdns entries in one big { }.
  54. %start map;
  55. // Values rule
  56. value : INTEGER { $$ = ElementPtr(new IntElement($1)); }
  57. | FLOAT { $$ = ElementPtr(new DoubleElement($1)); }
  58. | BOOLEAN { $$ = ElementPtr(new BoolElement($1)); }
  59. | STRING { $$ = ElementPtr(new StringElement($1)); }
  60. | NULL_TYPE { $$ = ElementPtr(new NullElement()); }
  61. | map { $$ = ElementPtr(new MapElement()); }
  62. | list { $$ = ElementPtr(new ListElement()); }
  63. ;
  64. map: LCURLY_BRACKET {
  65. ctx.stack_.push_back(ElementPtr(new MapElement()));
  66. } map_content RCURLY_BRACKET {
  67. ctx.stack_.pop_back();
  68. };
  69. // Assignments rule
  70. map_content: { /* do nothing, it's an empty map */ }
  71. | STRING COLON value {
  72. (*ctx.stack_.end())->set($1, $3);
  73. }
  74. | map COMMA STRING COLON value {
  75. (*ctx.stack_.end())->set($3, $5);
  76. }
  77. ;
  78. list: LSQUARE_BRACKET list_content RSQUARE_BRACKET { };
  79. list_content: { /* do nothing, it's an empty list */ }
  80. | value {
  81. // List consisting of a single element.
  82. (*ctx.stack_.end())->add($1);
  83. }
  84. | list COMMA value {
  85. // List ending with , and a value.
  86. (*ctx.stack_.end())->add($3);
  87. }
  88. ;
  89. %%
  90. void
  91. isc::dhcp::Dhcp6Parser::error(const location_type& loc,
  92. const std::string& what)
  93. {
  94. ctx.error(loc, what);
  95. }