lexer.ll 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. %{ /* -*- C++ -*- */
  13. #include <cerrno>
  14. #include <climits>
  15. #include <cstdlib>
  16. #include <string>
  17. #include <eval/eval_context.h>
  18. #include <eval/parser.h>
  19. #include <boost/lexical_cast.hpp>
  20. // Work around an incompatibility in flex (at least versions
  21. // 2.5.31 through 2.5.33): it generates code that does
  22. // not conform to C89. See Debian bug 333231
  23. // <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.
  24. # undef yywrap
  25. # define yywrap() 1
  26. // The location of the current token. The lexer will keep updating it. This
  27. // variable will be useful for logging errors.
  28. static isc::eval::location loc;
  29. // To avoid the call to exit... oops!
  30. #define YY_FATAL_ERROR(msg) isc::eval::EvalContext::fatal(msg)
  31. %}
  32. /* noyywrap disables automatic rewinding for the next file to parse. Since we
  33. always parse only a single string, there's no need to do any wraps. And
  34. using yywrap requires linking with -lfl, which provides the default yywrap
  35. implementation that always returns 1 anyway. */
  36. %option noyywrap
  37. /* nounput simplifies the lexer, by removing support for putting a character
  38. back into the input stream. We never use such capability anyway. */
  39. %option nounput
  40. /* batch means that we'll never use the generated lexer interactively. */
  41. %option batch
  42. /* Enables debug mode. To see the debug messages, one needs to also set
  43. yy_flex_debug to 1, then the debug messages will be printed on stderr. */
  44. %option debug
  45. /* I have no idea what this option does, except it was specified in the bison
  46. examples and Postgres folks added it to remove gcc 4.3 warnings. Let's
  47. be on the safe side and keep it. */
  48. %option noinput
  49. /* This line tells flex to track the line numbers. It's not really that
  50. useful for client classes, which typically are one-liners, but it may be
  51. useful in more complex cases. */
  52. %option yylineno
  53. /* These are not token expressions yet, just convenience expressions that
  54. can be used during actual token definitions. */
  55. int \-?[0-9]+
  56. hex [0-9a-fA-F]+
  57. blank [ \t]
  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. %}
  69. {blank}+ {
  70. // Ok, we found a with space. Let's ignore it and update loc variable.
  71. loc.step();
  72. }
  73. [\n]+ {
  74. // Newline found. Let's update the location and continue.
  75. loc.lines(yyleng);
  76. loc.step();
  77. }
  78. \'[^\'\n]*\' {
  79. // A string has been matched. It contains the actual string and single quotes.
  80. // We need to get those quotes out of the way and just use its content, e.g.
  81. // for 'foo' we should get foo
  82. std::string tmp(yytext+1);
  83. tmp.resize(tmp.size() - 1);
  84. return isc::eval::EvalParser::make_STRING(tmp, loc);
  85. }
  86. 0[xX]{hex} {
  87. // A hex string has been matched. It contains the '0x' or '0X' header
  88. // followed by at least one hexadecimal digit.
  89. return isc::eval::EvalParser::make_HEXSTRING(yytext, loc);
  90. }
  91. {int} {
  92. // An integer was found.
  93. std::string tmp(yytext);
  94. try {
  95. static_cast<void>(boost::lexical_cast<int>(tmp));
  96. } catch (const boost::bad_lexical_cast &) {
  97. driver.error(loc, "Failed to convert " + tmp + " to an integer.");
  98. }
  99. // The parser needs the string form as double conversion is no lossless
  100. return isc::eval::EvalParser::make_INTEGER(tmp, loc);
  101. }
  102. [A-Za-z]([-_A-Za-z0-9]*[A-Za-z0-9])?/({blank}|\n)*] {
  103. // This string specifies option name starting with a letter
  104. // and further containing letters, digits, hyphens and
  105. // underscores and finishing by letters or digits.
  106. return isc::eval::EvalParser::make_OPTION_NAME(yytext, loc);
  107. }
  108. "==" return isc::eval::EvalParser::make_EQUAL(loc);
  109. "option" return isc::eval::EvalParser::make_OPTION(loc);
  110. "text" return isc::eval::EvalParser::make_TEXT(loc);
  111. "hex" return isc::eval::EvalParser::make_HEX(loc);
  112. "substring" return isc::eval::EvalParser::make_SUBSTRING(loc);
  113. "not" return isc::eval::EvalParser::make_NOT(loc);
  114. "all" return isc::eval::EvalParser::make_ALL(loc);
  115. "." return isc::eval::EvalParser::make_DOT(loc);
  116. "(" return isc::eval::EvalParser::make_LPAREN(loc);
  117. ")" return isc::eval::EvalParser::make_RPAREN(loc);
  118. "[" return isc::eval::EvalParser::make_LBRACKET(loc);
  119. "]" return isc::eval::EvalParser::make_RBRACKET(loc);
  120. "," return isc::eval::EvalParser::make_COMA(loc);
  121. . driver.error (loc, "Invalid character: " + std::string(yytext));
  122. <<EOF>> return isc::eval::EvalParser::make_END(loc);
  123. %%
  124. using namespace isc::eval;
  125. void
  126. EvalContext::scanStringBegin()
  127. {
  128. loc.initialize(&file_);
  129. yy_flex_debug = trace_scanning_;
  130. YY_BUFFER_STATE buffer;
  131. buffer = yy_scan_bytes(string_.c_str(), string_.size());
  132. if (!buffer) {
  133. fatal("cannot scan string");
  134. // fatal() throws an exception so this can't be reached
  135. }
  136. }
  137. void
  138. EvalContext::scanStringEnd()
  139. {
  140. yy_delete_buffer(YY_CURRENT_BUFFER);
  141. }
  142. namespace {
  143. /// To avoid unused function error
  144. class Dummy {
  145. // cppcheck-suppress unusedPrivateFunction
  146. void dummy() { yy_fatal_error("Fix me: how to disable its definition?"); }
  147. };
  148. }