scanner.l 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* -*- mode: C -*- */
  2. /* --------------------------------------------------------------------------
  3. libconfig - A library for processing structured configuration files
  4. Copyright (C) 2005-2010 Mark A Lindner
  5. This file is part of libconfig.
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public License
  8. as published by the Free Software Foundation; either version 2.1 of
  9. the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, see
  16. <http://www.gnu.org/licenses/>.
  17. ----------------------------------------------------------------------------
  18. */
  19. %option nounistd
  20. %option reentrant
  21. %option noyywrap
  22. %option yylineno
  23. %option nounput
  24. %option bison-bridge
  25. %option header-file="scanner.h"
  26. %option outfile="lex.yy.c"
  27. %option extra-type="struct scan_context *"
  28. %{
  29. #ifdef _MSC_VER
  30. #pragma warning (disable: 4996)
  31. #endif
  32. #include <stdlib.h>
  33. #include <ctype.h>
  34. #include <string.h>
  35. #include "grammar.h"
  36. #include "wincompat.h"
  37. #include "parsectx.h"
  38. #include "scanctx.h"
  39. #define YY_NO_INPUT // Suppress generation of useless input() function
  40. static unsigned long long fromhex(const char *s)
  41. {
  42. #ifdef __MINGW32__
  43. /* MinGW's strtoull() seems to be broken; it only returns the lower
  44. * 32 bits...
  45. */
  46. const char *p = s;
  47. unsigned long long val = 0;
  48. if(*p != '0')
  49. return(0);
  50. ++p;
  51. if(*p != 'x' && *p != 'X')
  52. return(0);
  53. for(++p; isxdigit(*p); ++p)
  54. {
  55. val <<= 4;
  56. val |= ((*p < 'A') ? (*p & 0xF) : (9 + (*p & 0x7)));
  57. }
  58. return(val);
  59. #else /* ! __MINGW32__ */
  60. return(strtoull(s, NULL, 16));
  61. #endif /* __MINGW32__ */
  62. }
  63. %}
  64. true [Tt][Rr][Uu][Ee]
  65. false [Ff][Aa][Ll][Ss][Ee]
  66. name [A-Za-z\*][-A-Za-z0-9_\*]*
  67. integer [-+]?[0-9]+
  68. integer64 [-+]?[0-9]+L(L)?
  69. hex 0[Xx][0-9A-Fa-f]+
  70. hex64 0[Xx][0-9A-Fa-f]+L(L)?
  71. hexchar \\[Xx][0-9A-Fa-f]{2}
  72. float ([-+]?([0-9]*)?\.[0-9]*([eE][-+]?[0-9]+)?)|([-+]?([0-9]+)(\.[0-9]*)?[eE][-+]?[0-9]+)
  73. comment (#|\/\/).*$
  74. include_open ^[ \t]*@include[ \t]+\"
  75. %x COMMENT STRING INCLUDE
  76. %%
  77. \/\* { BEGIN COMMENT; }
  78. <COMMENT>\*\/ { BEGIN INITIAL; }
  79. <COMMENT>. { /* ignore */ }
  80. <COMMENT>\n { /* ignore */ }
  81. \" { BEGIN STRING; }
  82. <STRING>[^\"\\]+ { scanctx_append_string(yyextra, yytext); }
  83. <STRING>\\n { scanctx_append_string(yyextra, "\n"); }
  84. <STRING>\\r { scanctx_append_string(yyextra, "\r"); }
  85. <STRING>\\t { scanctx_append_string(yyextra, "\t"); }
  86. <STRING>\\f { scanctx_append_string(yyextra, "\f"); }
  87. <STRING>\\\\ { scanctx_append_string(yyextra, "\\"); }
  88. <STRING>\\\" { scanctx_append_string(yyextra, "\""); }
  89. <STRING>{hexchar} {
  90. char c[2] = { (char)(strtol(yytext + 2, NULL, 16) & 0xFF),
  91. 0 };
  92. scanctx_append_string(yyextra, c);
  93. }
  94. <STRING>\\ { scanctx_append_string(yyextra, "\\"); }
  95. <STRING>\" {
  96. yylval->sval = scanctx_take_string(yyextra);
  97. BEGIN INITIAL;
  98. return(TOK_STRING);
  99. }
  100. {include_open} { BEGIN INCLUDE; }
  101. <INCLUDE>[^\"\\]+ { scanctx_append_string(yyextra, yytext); }
  102. <INCLUDE>\\\\ { scanctx_append_string(yyextra, "\\"); }
  103. <INCLUDE>\\\" { scanctx_append_string(yyextra, "\""); }
  104. <INCLUDE>\" {
  105. const char *error;
  106. FILE *fp = scanctx_push_include(yyextra,
  107. (void *)YY_CURRENT_BUFFER,
  108. &error);
  109. if(fp)
  110. {
  111. yyin = fp;
  112. yy_switch_to_buffer(
  113. yy_create_buffer(yyin, YY_BUF_SIZE, yyscanner),
  114. yyscanner);
  115. }
  116. else
  117. {
  118. yyextra->config->error_text = error;
  119. yyextra->config->error_file = scanctx_current_filename(
  120. yyextra);
  121. yyextra->config->error_line = libconfig_yyget_lineno(
  122. yyscanner);
  123. return TOK_ERROR;
  124. }
  125. BEGIN INITIAL;
  126. }
  127. \n|\r|\f { /* ignore */ }
  128. [ \t]+ { /* ignore */ }
  129. \=|\: { return(TOK_EQUALS); }
  130. , { return(TOK_COMMA); }
  131. \{ { return(TOK_GROUP_START); }
  132. \} { return(TOK_GROUP_END); }
  133. {true} { yylval->ival = 1; return(TOK_BOOLEAN); }
  134. {false} { yylval->ival = 0; return(TOK_BOOLEAN); }
  135. {name} { yylval->sval = yytext; return(TOK_NAME); }
  136. {float} { yylval->fval = atof(yytext); return(TOK_FLOAT); }
  137. {integer} { yylval->ival = atoi(yytext); return(TOK_INTEGER); }
  138. {integer64} { yylval->llval = atoll(yytext); return(TOK_INTEGER64); }
  139. {hex} {
  140. yylval->ival = strtoul(yytext, NULL, 16);
  141. return(TOK_HEX);
  142. }
  143. {hex64} { yylval->llval = fromhex(yytext); return(TOK_HEX64); }
  144. \[ { return(TOK_ARRAY_START); }
  145. \] { return(TOK_ARRAY_END); }
  146. \( { return(TOK_LIST_START); }
  147. \) { return(TOK_LIST_END); }
  148. ; { return(TOK_SEMICOLON); }
  149. {comment} { /* ignore */ }
  150. . { return(TOK_GARBAGE); }
  151. <<EOF>> {
  152. YY_BUFFER_STATE buf = (YY_BUFFER_STATE)scanctx_pop_include(
  153. yyextra);
  154. if(buf)
  155. {
  156. yy_delete_buffer(YY_CURRENT_BUFFER, yyscanner);
  157. yy_switch_to_buffer(buf, yyscanner);
  158. }
  159. else
  160. yyterminate();
  161. }