2 /* --------------------------------------------------------------------------
3 libconfig - A library for processing structured configuration files
4 Copyright (C) 2005-2010 Mark A Lindner
6 This file is part of libconfig.
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with this library; if not, see
20 <http://www.gnu.org/licenses/>.
21 ----------------------------------------------------------------------------
25 %option never-interactive
31 %option header-file="scanner.h"
32 %option outfile="lex.yy.c"
33 %option extra-type="struct scan_context *"
38 #pragma warning (disable: 4996)
47 #include "wincompat.h"
49 #define YY_NO_INPUT // Suppress generation of useless input() function
51 static unsigned long long fromhex(const char *s)
55 /* MinGW's strtoull() seems to be broken; it only returns the lower
60 unsigned long long val = 0;
67 if(*p != 'x' && *p != 'X')
70 for(++p; isxdigit(*p); ++p)
73 val |= ((*p < 'A') ? (*p & 0xF) : (9 + (*p & 0x7)));
78 #else /* ! __MINGW32__ */
80 return(strtoull(s, NULL, 16));
82 #endif /* __MINGW32__ */
88 false [Ff][Aa][Ll][Ss][Ee]
89 name [A-Za-z\*][-A-Za-z0-9_\*]*
91 integer64 [-+]?[0-9]+L(L)?
93 hex64 0[Xx][0-9A-Fa-f]+L(L)?
94 hexchar \\[Xx][0-9A-Fa-f]{2}
95 float ([-+]?([0-9]*)?\.[0-9]*([eE][-+]?[0-9]+)?)|([-+]?([0-9]+)(\.[0-9]*)?[eE][-+]?[0-9]+)
97 include_open ^[ \t]*@include[ \t]+\"
99 %x COMMENT STRING INCLUDE
103 \/\* { BEGIN COMMENT; }
104 <COMMENT>\*\/ { BEGIN INITIAL; }
105 <COMMENT>. { /* ignore */ }
106 <COMMENT>\n { /* ignore */ }
109 <STRING>[^\"\\]+ { scanctx_append_string(yyextra, yytext); }
110 <STRING>\\n { scanctx_append_string(yyextra, "\n"); }
111 <STRING>\\r { scanctx_append_string(yyextra, "\r"); }
112 <STRING>\\t { scanctx_append_string(yyextra, "\t"); }
113 <STRING>\\f { scanctx_append_string(yyextra, "\f"); }
114 <STRING>\\\\ { scanctx_append_string(yyextra, "\\"); }
115 <STRING>\\\" { scanctx_append_string(yyextra, "\""); }
117 char c[2] = { (char)(strtol(yytext + 2, NULL, 16) & 0xFF),
119 scanctx_append_string(yyextra, c);
121 <STRING>\\ { scanctx_append_string(yyextra, "\\"); }
123 yylval->sval = scanctx_take_string(yyextra);
128 {include_open} { BEGIN INCLUDE; }
129 <INCLUDE>[^\"\\]+ { scanctx_append_string(yyextra, yytext); }
130 <INCLUDE>\\\\ { scanctx_append_string(yyextra, "\\"); }
131 <INCLUDE>\\\" { scanctx_append_string(yyextra, "\""); }
134 FILE *fp = scanctx_push_include(yyextra,
135 (void *)YY_CURRENT_BUFFER,
141 yy_create_buffer(yyin, YY_BUF_SIZE, yyscanner),
146 yyextra->config->error_text = error;
147 yyextra->config->error_file = scanctx_current_filename(
149 yyextra->config->error_line = libconfig_yyget_lineno(
156 \n|\r|\f { /* ignore */ }
157 [ \t]+ { /* ignore */ }
159 \=|\: { return(TOK_EQUALS); }
160 , { return(TOK_COMMA); }
161 \{ { return(TOK_GROUP_START); }
162 \} { return(TOK_GROUP_END); }
163 {true} { yylval->ival = 1; return(TOK_BOOLEAN); }
164 {false} { yylval->ival = 0; return(TOK_BOOLEAN); }
165 {name} { yylval->sval = yytext; return(TOK_NAME); }
166 {float} { yylval->fval = atof(yytext); return(TOK_FLOAT); }
167 {integer} { yylval->ival = atoi(yytext); return(TOK_INTEGER); }
168 {integer64} { yylval->llval = atoll(yytext); return(TOK_INTEGER64); }
170 yylval->ival = strtoul(yytext, NULL, 16);
173 {hex64} { yylval->llval = fromhex(yytext); return(TOK_HEX64); }
174 \[ { return(TOK_ARRAY_START); }
175 \] { return(TOK_ARRAY_END); }
176 \( { return(TOK_LIST_START); }
177 \) { return(TOK_LIST_END); }
178 ; { return(TOK_SEMICOLON); }
179 {comment} { /* ignore */ }
180 . { return(TOK_GARBAGE); }
183 YY_BUFFER_STATE buf = (YY_BUFFER_STATE)scanctx_pop_include(
187 yy_delete_buffer(YY_CURRENT_BUFFER, yyscanner);
188 yy_switch_to_buffer(buf, yyscanner);