dhcp6_lexer.ll 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. %{ /* -*- C++ -*- */
  6. #include <cerrno>
  7. #include <climits>
  8. #include <cstdlib>
  9. #include <string>
  10. #include <dhcp6/parser_context.h>
  11. #include <asiolink/io_address.h>
  12. #include <boost/lexical_cast.hpp>
  13. #include <exceptions/exceptions.h>
  14. // Work around an incompatibility in flex (at least versions
  15. // 2.5.31 through 2.5.33): it generates code that does
  16. // not conform to C89. See Debian bug 333231
  17. // <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.
  18. # undef yywrap
  19. # define yywrap() 1
  20. // The location of the current token. The lexer will keep updating it. This
  21. // variable will be useful for logging errors.
  22. static isc::dhcp::location loc;
  23. // To avoid the call to exit... oops!
  24. #define YY_FATAL_ERROR(msg) isc::dhcp::Parser6Context::fatal(msg)
  25. %}
  26. /* noyywrap disables automatic rewinding for the next file to parse. Since we
  27. always parse only a single string, there's no need to do any wraps. And
  28. using yywrap requires linking with -lfl, which provides the default yywrap
  29. implementation that always returns 1 anyway. */
  30. %option noyywrap
  31. /* nounput simplifies the lexer, by removing support for putting a character
  32. back into the input stream. We never use such capability anyway. */
  33. %option nounput
  34. /* batch means that we'll never use the generated lexer interactively. */
  35. %option batch
  36. /* Enables debug mode. To see the debug messages, one needs to also set
  37. yy_flex_debug to 1, then the debug messages will be printed on stderr. */
  38. %option debug
  39. /* I have no idea what this option does, except it was specified in the bison
  40. examples and Postgres folks added it to remove gcc 4.3 warnings. Let's
  41. be on the safe side and keep it. */
  42. %option noinput
  43. /* This line tells flex to track the line numbers. It's not really that
  44. useful for client classes, which typically are one-liners, but it may be
  45. useful in more complex cases. */
  46. %option yylineno
  47. %x COMMENT
  48. /* These are not token expressions yet, just convenience expressions that
  49. can be used during actual token definitions. Note some can match
  50. incorrect inputs (e.g., IP addresses) which must be checked. */
  51. int \-?[0-9]+
  52. blank [ \t]
  53. UnicodeEscapeSequence u[0-9A-Fa-f]{4}
  54. JSONEscapeCharacter ["\\/bfnrt]
  55. JSONEscapeSequence {JSONEscapeCharacter}|{UnicodeEscapeSequence}
  56. JSONStringCharacter [^"\\]|\\{JSONEscapeSequence}
  57. JSONString \"{JSONStringCharacter}*\"
  58. %{
  59. // This code run each time a pattern is matched. It updates the location
  60. // by moving it ahead by yyleng bytes. yyleng specifies the length of the
  61. // currently matched token.
  62. #define YY_USER_ACTION loc.columns(yyleng);
  63. %}
  64. %%
  65. %{
  66. // Code run each time yylex is called.
  67. loc.step();
  68. int comment_start_line = 0;
  69. %}
  70. #.* ;
  71. "//"(.*) ;
  72. "/*" {
  73. BEGIN(COMMENT);
  74. comment_start_line = yylineno;
  75. }
  76. <COMMENT>"*/" BEGIN(INITIAL);
  77. <COMMENT>.|"\n" ;
  78. <COMMENT><<EOF>> {
  79. isc_throw(isc::BadValue, "Comment not closed. (/* in line " << comment_start_line);
  80. }
  81. {blank}+ {
  82. // Ok, we found a with space. Let's ignore it and update loc variable.
  83. loc.step();
  84. }
  85. [\n]+ {
  86. // Newline found. Let's update the location and continue.
  87. loc.lines(yyleng);
  88. loc.step();
  89. }
  90. \"Dhcp6\" { return isc::dhcp::Dhcp6Parser::make_DHCP6(loc); }
  91. \"interfaces-config\" { return isc::dhcp::Dhcp6Parser::make_INTERFACES_CONFIG(loc); }
  92. \"interfaces\" { return isc::dhcp::Dhcp6Parser::make_INTERFACES(loc); }
  93. \"lease-database\" { return isc::dhcp::Dhcp6Parser::make_LEASE_DATABASE(loc); }
  94. \"hosts-database\" { return isc::dhcp::Dhcp6Parser::make_HOSTS_DATABASE(loc); }
  95. \"type\" { return isc::dhcp::Dhcp6Parser::make_TYPE(loc); }
  96. \"user\" { return isc::dhcp::Dhcp6Parser::make_USER(loc); }
  97. \"password\" { return isc::dhcp::Dhcp6Parser::make_PASSWORD(loc); }
  98. \"host\" { return isc::dhcp::Dhcp6Parser::make_HOST(loc); }
  99. \"preferred-lifetime\" { return isc::dhcp::Dhcp6Parser::make_PREFERRED_LIFETIME(loc); }
  100. \"valid-lifetime\" { return isc::dhcp::Dhcp6Parser::make_VALID_LIFETIME(loc); }
  101. \"renew-timer\" { return isc::dhcp::Dhcp6Parser::make_RENEW_TIMER(loc); }
  102. \"rebind-timer\" { return isc::dhcp::Dhcp6Parser::make_REBIND_TIMER(loc); }
  103. \"subnet6\" { return isc::dhcp::Dhcp6Parser::make_SUBNET6(loc); }
  104. \"option-data\" { return isc::dhcp::Dhcp6Parser::make_OPTION_DATA(loc); }
  105. \"name\" { return isc::dhcp::Dhcp6Parser::make_NAME(loc); }
  106. \"data\" { return isc::dhcp::Dhcp6Parser::make_DATA(loc); }
  107. \"pools\" { return isc::dhcp::Dhcp6Parser::make_POOLS(loc); }
  108. \"pd-pools\" { return isc::dhcp::Dhcp6Parser::make_PD_POOLS(loc); }
  109. \"prefix\" { return isc::dhcp::Dhcp6Parser::make_PREFIX(loc); }
  110. \"prefix-len\" { return isc::dhcp::Dhcp6Parser::make_PREFIX_LEN(loc); }
  111. \"delegated-len\" { return isc::dhcp::Dhcp6Parser::make_DELEGATED_LEN(loc); }
  112. \"pool\" { return isc::dhcp::Dhcp6Parser::make_POOL(loc); }
  113. \"subnet\" { return isc::dhcp::Dhcp6Parser::make_SUBNET(loc); }
  114. \"interface\" { return isc::dhcp::Dhcp6Parser::make_INTERFACE(loc); }
  115. \"id\" { return isc::dhcp::Dhcp6Parser::make_ID(loc); }
  116. \"code\" { return isc::dhcp::Dhcp6Parser::make_CODE(loc); }
  117. \"mac-sources\" { return isc::dhcp::Dhcp6Parser::make_MAC_SOURCES(loc); }
  118. \"relay-supplied-options\" { return isc::dhcp::Dhcp6Parser::make_RELAY_SUPPLIED_OPTIONS(loc); }
  119. \"host-reservation-identifiers\" { return isc::dhcp::Dhcp6Parser::make_HOST_RESERVATION_IDENTIFIERS(loc); }
  120. \"Logging\" { return isc::dhcp::Dhcp6Parser::make_LOGGING(loc); }
  121. \"loggers\" { return isc::dhcp::Dhcp6Parser::make_LOGGERS(loc); }
  122. \"output_options\" { return isc::dhcp::Dhcp6Parser::make_OUTPUT_OPTIONS(loc); }
  123. \"output\" { return isc::dhcp::Dhcp6Parser::make_OUTPUT(loc); }
  124. \"debuglevel\" { return isc::dhcp::Dhcp6Parser::make_DEBUGLEVEL(loc); }
  125. \"severity\" { return isc::dhcp::Dhcp6Parser::make_SEVERITY(loc); }
  126. \"client-classes\" { return isc::dhcp::Dhcp6Parser::make_CLIENT_CLASSES(loc); }
  127. \"client-class\" { return isc::dhcp::Dhcp6Parser::make_CLIENT_CLASS(loc); }
  128. \"test\" { return isc::dhcp::Dhcp6Parser::make_TEST(loc); }
  129. \"reservations\" { return isc::dhcp::Dhcp6Parser::make_RESERVATIONS(loc); }
  130. \"ip-addresses\" { return isc::dhcp::Dhcp6Parser::make_IP_ADDRESSES(loc); }
  131. \"prefixes\" { return isc::dhcp::Dhcp6Parser::make_PREFIXES(loc); }
  132. \"duid\" { return isc::dhcp::Dhcp6Parser::make_DUID(loc); }
  133. \"hw-address\" { return isc::dhcp::Dhcp6Parser::make_HW_ADDRESS(loc); }
  134. \"hostname\" { return isc::dhcp::Dhcp6Parser::make_HOSTNAME(loc); }
  135. \"space\" { return isc::dhcp::Dhcp6Parser::make_SPACE(loc); }
  136. {JSONString} {
  137. // A string has been matched. It contains the actual string and single quotes.
  138. // We need to get those quotes out of the way and just use its content, e.g.
  139. // for 'foo' we should get foo
  140. std::string tmp(yytext+1);
  141. tmp.resize(tmp.size() - 1);
  142. return isc::dhcp::Dhcp6Parser::make_STRING(tmp, loc);
  143. }
  144. "[" { return isc::dhcp::Dhcp6Parser::make_LSQUARE_BRACKET(loc); }
  145. "]" { return isc::dhcp::Dhcp6Parser::make_RSQUARE_BRACKET(loc); }
  146. "{" { return isc::dhcp::Dhcp6Parser::make_LCURLY_BRACKET(loc); }
  147. "}" { return isc::dhcp::Dhcp6Parser::make_RCURLY_BRACKET(loc); }
  148. "," { return isc::dhcp::Dhcp6Parser::make_COMMA(loc); }
  149. ":" { return isc::dhcp::Dhcp6Parser::make_COLON(loc); }
  150. {int} {
  151. // An integer was found.
  152. std::string tmp(yytext);
  153. int64_t integer = 0;
  154. try {
  155. // In substring we want to use negative values (e.g. -1).
  156. // In enterprise-id we need to use values up to 0xffffffff.
  157. // To cover both of those use cases, we need at least
  158. // int64_t.
  159. integer = boost::lexical_cast<int64_t>(tmp);
  160. } catch (const boost::bad_lexical_cast &) {
  161. driver.error(loc, "Failed to convert " + tmp + " to an integer.");
  162. }
  163. // The parser needs the string form as double conversion is no lossless
  164. return isc::dhcp::Dhcp6Parser::make_INTEGER(integer, loc);
  165. }
  166. [-+]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)? {
  167. // A floating point was found.
  168. std::string tmp(yytext);
  169. double fp = 0.0;
  170. try {
  171. // In substring we want to use negative values (e.g. -1).
  172. // In enterprise-id we need to use values up to 0xffffffff.
  173. // To cover both of those use cases, we need at least
  174. // int64_t.
  175. fp = boost::lexical_cast<double>(tmp);
  176. } catch (const boost::bad_lexical_cast &) {
  177. driver.error(loc, "Failed to convert " + tmp + " to a floating point.");
  178. }
  179. return isc::dhcp::Dhcp6Parser::make_FLOAT(fp, loc);
  180. }
  181. true|false {
  182. string tmp(yytext);
  183. return isc::dhcp::Dhcp6Parser::make_BOOLEAN(tmp == "true", loc);
  184. }
  185. null {
  186. return isc::dhcp::Dhcp6Parser::make_NULL_TYPE(loc);
  187. }
  188. . driver.error (loc, "Invalid character: " + std::string(yytext));
  189. <<EOF>> return isc::dhcp::Dhcp6Parser::make_END(loc);
  190. %%
  191. using namespace isc::dhcp;
  192. void
  193. Parser6Context::scanStringBegin()
  194. {
  195. loc.initialize(&file_);
  196. yy_flex_debug = trace_scanning_;
  197. YY_BUFFER_STATE buffer;
  198. buffer = yy_scan_bytes(string_.c_str(), string_.size());
  199. if (!buffer) {
  200. fatal("cannot scan string");
  201. // fatal() throws an exception so this can't be reached
  202. }
  203. }
  204. void
  205. Parser6Context::scanStringEnd()
  206. {
  207. yy_delete_buffer(YY_CURRENT_BUFFER);
  208. }
  209. void
  210. Parser6Context::scanFileBegin(FILE * f) {
  211. loc.initialize(&file_);
  212. yy_flex_debug = trace_scanning_;
  213. YY_BUFFER_STATE buffer;
  214. // See dhcp6_lexer.cc header for available definitions
  215. buffer = parser6__create_buffer(f, 65536 /*buffer size*/);
  216. if (!buffer) {
  217. fatal("cannot scan file " + file_);
  218. }
  219. }
  220. void
  221. Parser6Context::scanFileEnd(FILE * f) {
  222. fclose(f);
  223. yy_delete_buffer(YY_CURRENT_BUFFER);
  224. }
  225. namespace {
  226. /// To avoid unused function error
  227. class Dummy {
  228. // cppcheck-suppress unusedPrivateFunction
  229. void dummy() { yy_fatal_error("Fix me: how to disable its definition?"); }
  230. };
  231. }