forked from mailru/confetti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prscfg.l
320 lines (252 loc) · 6.36 KB
/
prscfg.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
%{
#undef yylval
#undef yylloc
#include <stdlib.h>
#include <string.h>
#define YY_EXTRA_TYPE prscfg_yy_extra_type *
static int scan_yyerror(char *s, int lineno);
static int addstring(prscfg_yyscan_t yyscanner, char *s, int l);
static int addchar(prscfg_yyscan_t yyscanner, char s);
static char * strdupn(char *src, size_t size);
static YY_BUFFER_STATE buf = NULL;
%}
%option 8bit
%option never-interactive
%option nodefault
%option noyywrap
%option prefix="prscfg_yy"
%option reentrant
%option noinput
%option noyy_top_state
%option bison-bridge
%option stack
%option warn
%x VALUE
%x QUOTED
%x UNQUOTED
%x CCOMMENT
KEYCHARSTART [a-zA-Z_]
KEYCHAR [a-zA-Z0-9_]
DIGIT [0-9]
WS [ \t\f\r]
/* Default separators */
DEFSEP [\[\]{},.=]
/* Value separators */
VALSEP [\[\]{},=]
%%
<INITIAL,VALUE>\n { yyextra->lineno++; }
<INITIAL,VALUE>{WS}+ { /* ignore whitespace */ }
<INITIAL,VALUE>#[^\n]* { /* ignore single-line comment */ }
<INITIAL,VALUE>\/\* {
/* scan a C-style comment */
yyextra->commentCounter = 1;
yyextra->ostate = YYSTATE;
BEGIN CCOMMENT;
}
<INITIAL>[Oo][Pp][Tt] {
yylval->str = strdup("OPT");
if (!yylval->str)
scan_yyerror("No memory", yyextra->lineno);
return OPT_P;
}
<INITIAL>{KEYCHARSTART}{KEYCHAR}* {
yylval->str = strdupn(yytext, yyleng);
if (!yylval->str)
scan_yyerror("No memory", yyextra->lineno);
return KEY_P;
}
<INITIAL>{DIGIT}+ {
yylval->str = strdupn(yytext, yyleng);
if (!yylval->str)
scan_yyerror("No memory", yyextra->lineno);
return INDEX_P;
}
<INITIAL>{DEFSEP} { return *yytext; }
<INITIAL>. { return scan_yyerror("syntax error: Unknown character", yyextra->lineno); }
<INITIAL><<EOF>> { yyterminate(); }
<VALUE>[Nn][Uu][Ll][Ll] {
yylval->str = strdup("NULL");
if (!yylval->str)
scan_yyerror("No memory", yyextra->lineno);
BEGIN INITIAL;
return NULL_P;
}
<VALUE>\" {
/* Accept a quoted string. */
yyextra->total = 256;
yyextra->strbuf = malloc(yyextra->total);
if (!yyextra->strbuf)
scan_yyerror("No memory", yyextra->lineno);
yyextra->length = 0;
BEGIN QUOTED;
}
<VALUE>{VALSEP} {
BEGIN INITIAL;
return *yytext;
}
<VALUE>. {
/* Accept an unquoted string. */
yymore();
BEGIN UNQUOTED;
}
<VALUE><<EOF>> {
return scan_yyerror("Unexpected end of string (expecting value)", yyextra->lineno);
}
<UNQUOTED># |
<UNQUOTED>\n |
<UNQUOTED>{WS} |
<UNQUOTED>{VALSEP} {
int next = yytext[yyleng - 1];
yylval->str = strdupn(yytext, yyleng - 1);
if (!yylval->str)
scan_yyerror("No memory", yyextra->lineno);
unput(next);
BEGIN INITIAL;
return STRING_P;
}
<UNQUOTED>\/\* {
yylval->str = strdupn(yytext, yyleng - 2);
if (!yylval->str)
scan_yyerror("No memory", yyextra->lineno);
unput('*');
unput('/');
BEGIN INITIAL;
return STRING_P;
}
<UNQUOTED>. { yymore(); }
<UNQUOTED><<EOF>> {
yylval->str = strdupn(yytext, yyleng - 1);
if (!yylval->str)
scan_yyerror("No memory", yyextra->lineno);
BEGIN INITIAL;
return STRING_P;
}
<QUOTED>\\. {
if (addchar(yyscanner, yytext[1]))
scan_yyerror("No memory", yyextra->lineno);
}
<QUOTED>\\\n {
yyextra->lineno++;
}
<QUOTED>\" {
yyextra->strbuf[yyextra->length] = '\0';
yylval->str = yyextra->strbuf;
BEGIN INITIAL;
yyextra->strbuf = NULL;
return STRING_P;
}
<QUOTED>\\ {
/* This is only needed for \ just before EOF */
}
<QUOTED>[^\\\"\n]+ {
if (addstring(yyscanner, yytext, yyleng))
scan_yyerror("No memory", yyextra->lineno);
}
<QUOTED>\n {
if (addchar(yyscanner, yytext[0]))
scan_yyerror("No memory", yyextra->lineno);
yyextra->lineno++;
}
<QUOTED><<EOF>> {
return scan_yyerror("Unexpected end of string (expecting closing quote)", yyextra->lineno);
}
<CCOMMENT>\/\* {
yyextra->commentCounter++;
}
<CCOMMENT>\*\/ {
yyextra->commentCounter--;
if (yyextra->commentCounter == 0)
BEGIN yyextra->ostate;
}
<CCOMMENT>\n { yyextra->lineno++; }
<CCOMMENT>.+ { /* ignore */ }
<CCOMMENT><<EOF>> {
return scan_yyerror("Unexpected end of string (inside comment)", yyextra->lineno);
}
%%
static int
scan_yyerror(char *msg, int lineno) {
out_warning(CNF_SYNTAXERROR, "scan_yyerror: %s at line %d", msg, lineno);
return 0;
}
prscfg_yyscan_t
prscfgScannerInit(FILE *fh, prscfg_yy_extra_type *yyext) {
yyscan_t scanner;
memset(yyext, 0, sizeof(*yyext));
yyext->lineno = 1;
yylex_init_extra(yyext, &scanner);
buf = yy_create_buffer( fh, YY_BUF_SIZE, scanner );
yy_switch_to_buffer( buf, scanner );
return scanner;
}
prscfg_yyscan_t
prscfgScannerInitBuffer(char *buffer, prscfg_yy_extra_type *yyext) {
yyscan_t scanner;
memset(yyext, 0, sizeof(*yyext));
yyext->lineno = 1;
yylex_init_extra(yyext, &scanner);
buf = yy_scan_string( buffer, scanner );
yy_switch_to_buffer( buf, scanner );
return scanner;
}
void
prscfgScannerFinish(prscfg_yyscan_t scanner) {
if (buf)
yy_delete_buffer( buf, scanner );
yylex_destroy(scanner);
buf = NULL;
}
void
prscfgScannerStartValue(prscfg_yyscan_t scanner)
{
yy_push_state(VALUE, scanner);
}
static void
prscfgScannerEndValue(prscfg_yyscan_t scanner)
{
yy_pop_state(scanner);
}
/*
* Arrange access to yyextra for subroutines of the main yylex() function.
* We expect each subroutine to have a yyscanner parameter. Rather than
* use the yyget_xxx functions, which might or might not get inlined by the
* compiler, we cheat just a bit and cast yyscanner to the right type.
*/
#undef yyextra
#define yyextra (((struct yyguts_t *) yyscanner)->yyextra_r)
int
prscfgGetLineNo(prscfg_yyscan_t yyscanner) {
return yyextra->lineno;
}
static int
addstring(prscfg_yyscan_t yyscanner, char *s, int l) {
while( yyextra->length + l + 1 >= yyextra->total ) {
yyextra->total *= 2;
yyextra->strbuf=realloc(yyextra->strbuf, yyextra->total);
}
if (!yyextra->strbuf)
return 1;
memcpy( yyextra->strbuf+yyextra->length, s, l);
yyextra->length+=l;
return 0;
}
static int
addchar(prscfg_yyscan_t yyscanner, char s) {
if( yyextra->length + 2 >= yyextra->total ) {
yyextra->total*=2;
yyextra->strbuf=realloc(yyextra->strbuf, yyextra->total);
}
if (!yyextra->strbuf)
return 1;
yyextra->strbuf[ yyextra->length++ ] = s;
return 0;
}
static char *
strdupn(char *src, size_t size) {
char *dst = malloc(size + 1);
if (!dst)
return NULL;
memcpy(dst, src, size);
dst[size] = '\0';
return dst;
}