lexer.ll 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. namespace {
  24. bool start_token_flag = false;
  25. isc::eval::EvalContext::ParserType start_token_value;
  26. };
  27. // To avoid the call to exit... oops!
  28. #define YY_FATAL_ERROR(msg) isc::eval::EvalContext::fatal(msg)
  29. %}
  30. /* noyywrap disables automatic rewinding for the next file to parse. Since we
  31. always parse only a single string, there's no need to do any wraps. And
  32. using yywrap requires linking with -lfl, which provides the default yywrap
  33. implementation that always returns 1 anyway. */
  34. %option noyywrap
  35. /* nounput simplifies the lexer, by removing support for putting a character
  36. back into the input stream. We never use such capability anyway. */
  37. %option nounput
  38. /* batch means that we'll never use the generated lexer interactively. */
  39. %option batch
  40. /* Enables debug mode. To see the debug messages, one needs to also set
  41. eval_flex_debug to 1, then the debug messages will be printed on stderr. */
  42. %option debug
  43. /* I have no idea what this option does, except it was specified in the bison
  44. examples and Postgres folks added it to remove gcc 4.3 warnings. Let's
  45. be on the safe side and keep it. */
  46. %option noinput
  47. /* This line tells flex to track the line numbers. It's not really that
  48. useful for client classes, which typically are one-liners, but it may be
  49. useful in more complex cases. */
  50. %option yylineno
  51. /* These are not token expressions yet, just convenience expressions that
  52. can be used during actual token definitions. Note some can match
  53. incorrect inputs (e.g., IP addresses) which must be checked. */
  54. int \-?[0-9]+
  55. hex [0-9a-fA-F]+
  56. blank [ \t]
  57. addr4 [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+
  58. addr6 [0-9a-fA-F]*\:[0-9a-fA-F]*\:[0-9a-fA-F:.]*
  59. %{
  60. // This code run each time a pattern is matched. It updates the location
  61. // by moving it ahead by yyleng bytes. yyleng specifies the length of the
  62. // currently matched token.
  63. #define YY_USER_ACTION loc.columns(evalleng);
  64. %}
  65. %%
  66. %{
  67. // Code run each time evallex is called.
  68. loc.step();
  69. if (start_token_flag) {
  70. start_token_flag = false;
  71. switch (start_token_value) {
  72. case EvalContext::PARSER_BOOL:
  73. return isc::eval::EvalParser::make_TOPLEVEL_BOOL(loc);
  74. default:
  75. case EvalContext::PARSER_STRING:
  76. return isc::eval::EvalParser::make_TOPLEVEL_STRING(loc);
  77. }
  78. }
  79. %}
  80. {blank}+ {
  81. // Ok, we found a with space. Let's ignore it and update loc variable.
  82. loc.step();
  83. }
  84. [\n]+ {
  85. // Newline found. Let's update the location and continue.
  86. loc.lines(evalleng);
  87. loc.step();
  88. }
  89. \'[^\'\n]*\' {
  90. // A string has been matched. It contains the actual string and single quotes.
  91. // We need to get those quotes out of the way and just use its content, e.g.
  92. // for 'foo' we should get foo
  93. std::string tmp(evaltext+1);
  94. tmp.resize(tmp.size() - 1);
  95. return isc::eval::EvalParser::make_STRING(tmp, loc);
  96. }
  97. 0[xX]{hex} {
  98. // A hex string has been matched. It contains the '0x' or '0X' header
  99. // followed by at least one hexadecimal digit.
  100. return isc::eval::EvalParser::make_HEXSTRING(evaltext, loc);
  101. }
  102. {int} {
  103. // An integer was found.
  104. std::string tmp(evaltext);
  105. try {
  106. // In substring we want to use negative values (e.g. -1).
  107. // In enterprise-id we need to use values up to 0xffffffff.
  108. // To cover both of those use cases, we need at least
  109. // int64_t.
  110. static_cast<void>(boost::lexical_cast<int64_t>(tmp));
  111. } catch (const boost::bad_lexical_cast &) {
  112. driver.error(loc, "Failed to convert " + tmp + " to an integer.");
  113. }
  114. // The parser needs the string form as double conversion is no lossless
  115. return isc::eval::EvalParser::make_INTEGER(tmp, loc);
  116. }
  117. [A-Za-z]([-_A-Za-z0-9]*[A-Za-z0-9])?/({blank}|\n)*] {
  118. // This string specifies option name starting with a letter
  119. // and further containing letters, digits, hyphens and
  120. // underscores and finishing by letters or digits.
  121. return isc::eval::EvalParser::make_OPTION_NAME(evaltext, loc);
  122. }
  123. {addr4}|{addr6} {
  124. // IPv4 or IPv6 address
  125. std::string tmp(evaltext);
  126. // Some incorrect addresses can match so we have to check.
  127. try {
  128. isc::asiolink::IOAddress ip(tmp);
  129. } catch (...) {
  130. driver.error(loc, "Failed to convert " + tmp + " to an IP address.");
  131. }
  132. return isc::eval::EvalParser::make_IP_ADDRESS(evaltext, loc);
  133. }
  134. "==" return isc::eval::EvalParser::make_EQUAL(loc);
  135. "option" return isc::eval::EvalParser::make_OPTION(loc);
  136. "relay4" return isc::eval::EvalParser::make_RELAY4(loc);
  137. "relay6" return isc::eval::EvalParser::make_RELAY6(loc);
  138. "peeraddr" return isc::eval::EvalParser::make_PEERADDR(loc);
  139. "linkaddr" return isc::eval::EvalParser::make_LINKADDR(loc);
  140. "text" return isc::eval::EvalParser::make_TEXT(loc);
  141. "hex" return isc::eval::EvalParser::make_HEX(loc);
  142. "exists" return isc::eval::EvalParser::make_EXISTS(loc);
  143. "pkt" return isc::eval::EvalParser::make_PKT(loc);
  144. "iface" return isc::eval::EvalParser::make_IFACE(loc);
  145. "src" return isc::eval::EvalParser::make_SRC(loc);
  146. "dst" return isc::eval::EvalParser::make_DST(loc);
  147. "len" return isc::eval::EvalParser::make_LEN(loc);
  148. "pkt4" return isc::eval::EvalParser::make_PKT4(loc);
  149. "mac" return isc::eval::EvalParser::make_CHADDR(loc);
  150. "hlen" return isc::eval::EvalParser::make_HLEN(loc);
  151. "htype" return isc::eval::EvalParser::make_HTYPE(loc);
  152. "ciaddr" return isc::eval::EvalParser::make_CIADDR(loc);
  153. "giaddr" return isc::eval::EvalParser::make_GIADDR(loc);
  154. "yiaddr" return isc::eval::EvalParser::make_YIADDR(loc);
  155. "siaddr" return isc::eval::EvalParser::make_SIADDR(loc);
  156. "pkt6" return isc::eval::EvalParser::make_PKT6(loc);
  157. "msgtype" return isc::eval::EvalParser::make_MSGTYPE(loc);
  158. "transid" return isc::eval::EvalParser::make_TRANSID(loc);
  159. "vendor" return isc::eval::EvalParser::make_VENDOR(loc);
  160. "vendor-class" return isc::eval::EvalParser::make_VENDOR_CLASS(loc);
  161. "data" return isc::eval::EvalParser::make_DATA(loc);
  162. "enterprise" return isc::eval::EvalParser::make_ENTERPRISE(loc);
  163. "substring" return isc::eval::EvalParser::make_SUBSTRING(loc);
  164. "all" return isc::eval::EvalParser::make_ALL(loc);
  165. "concat" return isc::eval::EvalParser::make_CONCAT(loc);
  166. "not" return isc::eval::EvalParser::make_NOT(loc);
  167. "and" return isc::eval::EvalParser::make_AND(loc);
  168. "or" return isc::eval::EvalParser::make_OR(loc);
  169. "." return isc::eval::EvalParser::make_DOT(loc);
  170. "(" return isc::eval::EvalParser::make_LPAREN(loc);
  171. ")" return isc::eval::EvalParser::make_RPAREN(loc);
  172. "[" return isc::eval::EvalParser::make_LBRACKET(loc);
  173. "]" return isc::eval::EvalParser::make_RBRACKET(loc);
  174. "," return isc::eval::EvalParser::make_COMA(loc);
  175. "*" return isc::eval::EvalParser::make_ANY(loc);
  176. . driver.error (loc, "Invalid character: " + std::string(evaltext));
  177. <<EOF>> return isc::eval::EvalParser::make_END(loc);
  178. %%
  179. using namespace isc::eval;
  180. void
  181. EvalContext::scanStringBegin(ParserType type)
  182. {
  183. start_token_flag = true;
  184. start_token_value = type;
  185. loc.initialize(&file_);
  186. eval_flex_debug = trace_scanning_;
  187. YY_BUFFER_STATE buffer;
  188. buffer = eval_scan_bytes(string_.c_str(), string_.size());
  189. if (!buffer) {
  190. fatal("cannot scan string");
  191. // fatal() throws an exception so this can't be reached
  192. }
  193. }
  194. void
  195. EvalContext::scanStringEnd()
  196. {
  197. eval_delete_buffer(YY_CURRENT_BUFFER);
  198. }
  199. namespace {
  200. /// To avoid unused function error
  201. class Dummy {
  202. // cppcheck-suppress unusedPrivateFunction
  203. void dummy() { yy_fatal_error("Fix me: how to disable its definition?"); }
  204. };
  205. }