lexer.ll 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <eval/eval_context.h>
  11. #include <eval/parser.h>
  12. #include <asiolink/io_address.h>
  13. #include <boost/lexical_cast.hpp>
  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::eval::location loc;
  23. // To avoid the call to exit... oops!
  24. #define YY_FATAL_ERROR(msg) isc::eval::EvalContext::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. /* These are not token expressions yet, just convenience expressions that
  48. can be used during actual token definitions. Note some can match
  49. incorrect inputs (e.g., IP addresses) which must be checked. */
  50. int \-?[0-9]+
  51. hex [0-9a-fA-F]+
  52. blank [ \t]
  53. addr4 [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+
  54. addr6 [0-9a-fA-F]*\:[0-9a-fA-F]*\:[0-9a-fA-F:.]*
  55. %{
  56. // This code run each time a pattern is matched. It updates the location
  57. // by moving it ahead by yyleng bytes. yyleng specifies the length of the
  58. // currently matched token.
  59. #define YY_USER_ACTION loc.columns(yyleng);
  60. %}
  61. %%
  62. %{
  63. // Code run each time yylex is called.
  64. loc.step();
  65. %}
  66. {blank}+ {
  67. // Ok, we found a with space. Let's ignore it and update loc variable.
  68. loc.step();
  69. }
  70. [\n]+ {
  71. // Newline found. Let's update the location and continue.
  72. loc.lines(yyleng);
  73. loc.step();
  74. }
  75. \'[^\'\n]*\' {
  76. // A string has been matched. It contains the actual string and single quotes.
  77. // We need to get those quotes out of the way and just use its content, e.g.
  78. // for 'foo' we should get foo
  79. std::string tmp(yytext+1);
  80. tmp.resize(tmp.size() - 1);
  81. return isc::eval::EvalParser::make_STRING(tmp, loc);
  82. }
  83. 0[xX]{hex} {
  84. // A hex string has been matched. It contains the '0x' or '0X' header
  85. // followed by at least one hexadecimal digit.
  86. return isc::eval::EvalParser::make_HEXSTRING(yytext, loc);
  87. }
  88. {int} {
  89. // An integer was found.
  90. std::string tmp(yytext);
  91. try {
  92. static_cast<void>(boost::lexical_cast<int>(tmp));
  93. } catch (const boost::bad_lexical_cast &) {
  94. driver.error(loc, "Failed to convert " + tmp + " to an integer.");
  95. }
  96. // The parser needs the string form as double conversion is no lossless
  97. return isc::eval::EvalParser::make_INTEGER(tmp, loc);
  98. }
  99. [A-Za-z]([-_A-Za-z0-9]*[A-Za-z0-9])?/({blank}|\n)*] {
  100. // This string specifies option name starting with a letter
  101. // and further containing letters, digits, hyphens and
  102. // underscores and finishing by letters or digits.
  103. return isc::eval::EvalParser::make_OPTION_NAME(yytext, loc);
  104. }
  105. {addr4}|{addr6} {
  106. // IPv4 or IPv6 address
  107. std::string tmp(yytext);
  108. // Some incorrect addresses can match so we have to check.
  109. try {
  110. isc::asiolink::IOAddress ip(tmp);
  111. } catch (...) {
  112. driver.error(loc, "Failed to convert " + tmp + " to an IP address.");
  113. }
  114. return isc::eval::EvalParser::make_IP_ADDRESS(yytext, loc);
  115. }
  116. "==" return isc::eval::EvalParser::make_EQUAL(loc);
  117. "option" return isc::eval::EvalParser::make_OPTION(loc);
  118. "relay4" return isc::eval::EvalParser::make_RELAY4(loc);
  119. "text" return isc::eval::EvalParser::make_TEXT(loc);
  120. "hex" return isc::eval::EvalParser::make_HEX(loc);
  121. "exists" return isc::eval::EvalParser::make_EXISTS(loc);
  122. "substring" return isc::eval::EvalParser::make_SUBSTRING(loc);
  123. "all" return isc::eval::EvalParser::make_ALL(loc);
  124. "concat" return isc::eval::EvalParser::make_CONCAT(loc);
  125. "not" return isc::eval::EvalParser::make_NOT(loc);
  126. "and" return isc::eval::EvalParser::make_AND(loc);
  127. "or" return isc::eval::EvalParser::make_OR(loc);
  128. "." return isc::eval::EvalParser::make_DOT(loc);
  129. "(" return isc::eval::EvalParser::make_LPAREN(loc);
  130. ")" return isc::eval::EvalParser::make_RPAREN(loc);
  131. "[" return isc::eval::EvalParser::make_LBRACKET(loc);
  132. "]" return isc::eval::EvalParser::make_RBRACKET(loc);
  133. "," return isc::eval::EvalParser::make_COMA(loc);
  134. . driver.error (loc, "Invalid character: " + std::string(yytext));
  135. <<EOF>> return isc::eval::EvalParser::make_END(loc);
  136. %%
  137. using namespace isc::eval;
  138. void
  139. EvalContext::scanStringBegin()
  140. {
  141. loc.initialize(&file_);
  142. yy_flex_debug = trace_scanning_;
  143. YY_BUFFER_STATE buffer;
  144. buffer = yy_scan_bytes(string_.c_str(), string_.size());
  145. if (!buffer) {
  146. fatal("cannot scan string");
  147. // fatal() throws an exception so this can't be reached
  148. }
  149. }
  150. void
  151. EvalContext::scanStringEnd()
  152. {
  153. yy_delete_buffer(YY_CURRENT_BUFFER);
  154. }
  155. namespace {
  156. /// To avoid unused function error
  157. class Dummy {
  158. // cppcheck-suppress unusedPrivateFunction
  159. void dummy() { yy_fatal_error("Fix me: how to disable its definition?"); }
  160. };
  161. }