-
Notifications
You must be signed in to change notification settings - Fork 10
/
prscfl.l
288 lines (223 loc) · 5.59 KB
/
prscfl.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
%{
#include <stdlib.h>
#include <string.h>
#include <prscfl.h>
#include <prscfl_gram.h>
#define YY_EXTRA_TYPE prscfl_yy_extra_type *
static int scan_yyerror(char *s, int lineno);
static void addstring(prscfl_yyscan_t yyscanner, char *s, int l);
static void addchar(prscfl_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 nounput
%option prefix="prscfl_yy"
%option reentrant
%option noinput
%option bison-bridge
%option warn
%x xQUOTED
%x COMMENT
%x BUILTIN
KEYCHARSTART [a-zA-Z_]
KEYCHAR [a-zA-Z0-9_]
DIGIT [0-9]
%%
<INITIAL>[Nn][Uu][Ll][Ll] {
yylval->str = strdupn(yytext, yyleng);
return NULL_P;
}
<INITIAL>[Tt][Rr][Uu][Ee] {
yylval->str = strdupn(yytext, yyleng);
return TRUE_P;
}
<INITIAL>[Ff][Aa][Ll][Ss][Ee] {
yylval->str = strdupn(yytext, yyleng);
return FALSE_P;
}
<INITIAL>[Rr][Oo] {
yylval->str = strdupn(yytext, yyleng);
return RDONLY_P;
}
<INITIAL>[Rr][Ww] {
yylval->str = strdupn(yytext, yyleng);
return RDWR_P;
}
<INITIAL>[Rr][Ee][Qq][Uu][Ii][Rr][Ee][Dd] {
yylval->str = strdupn(yytext, yyleng);
return REQUIRED_P;
}
<INITIAL>{KEYCHARSTART}{KEYCHAR}* {
yylval->str = strdupn(yytext, yyleng);
return KEY_P;
}
<INITIAL>#[ \t]* {
BEGIN COMMENT;
yyextra->total = 256;
yyextra->strbuf = malloc(yyextra->total);
yyextra->length = 0;
}
<INITIAL>[\=\[\]\{\}\,] { return *yytext; }
<INITIAL>\%\{ {
yyextra->total = 256;
yyextra->strbuf = malloc(yyextra->total);
yyextra->length = 0;
BEGIN BUILTIN;
}
<INITIAL>\" {
yyextra->total = 256;
yyextra->strbuf = malloc(yyextra->total);
yyextra->length = 0;
BEGIN xQUOTED;
}
<INITIAL>[\-\+]?{DIGIT}+L {
yylval->int64val = strtoll(yytext, NULL, 10);
return INT64_P;
}
<INITIAL>{DIGIT}+UL {
yylval->uint64val = strtoull(yytext, NULL, 10);
return UINT64_P;
}
<INITIAL>{DIGIT}+U {
yylval->uint32val = strtoul(yytext, NULL, 10);
return UINT32_P;
}
<INITIAL>[\-\+]?{DIGIT}+ {
yylval->int32val = strtol(yytext, NULL, 10);
return INT32_P;
}
<INITIAL>[\-\+]?{DIGIT}+"."{DIGIT}+ {
yylval->doubleval = strtod(yytext, NULL);
return DOUBLE_P;
}
<INITIAL>[\-\+]?{DIGIT}+[eE][\+\-]?{DIGIT}+ {
yylval->doubleval = strtod(yytext, NULL);
return DOUBLE_P;
}
<INITIAL>[\-\+]?{DIGIT}+"."{DIGIT}+[eE][\+\-]?{DIGIT}+ {
yylval->doubleval = strtod(yytext, NULL);
return DOUBLE_P;
}
<xQUOTED>\\. {
addchar(yyscanner, yytext[1]);
}
<xQUOTED>\\\n {
yyextra->lineno++;
}
<xQUOTED>\" {
yyextra->strbuf[yyextra->length] = '\0';
yylval->str = yyextra->strbuf;
BEGIN INITIAL;
yyextra->strbuf = NULL;
return STRING_P;
}
<xQUOTED>\\ {
/* This is only needed for \ just before EOF */
}
<xQUOTED><<EOF>> {
return scan_yyerror("Unexpected end of string", yyextra->lineno);
}
<xQUOTED>[^\\\"\n]+ {
addstring(yyscanner, yytext, yyleng);
}
<xQUOTED>\n {
addchar(yyscanner, yytext[0]);
yyextra->lineno++;
}
<COMMENT>\r { /* ignore */ }
<COMMENT>.+ {
addstring(yyscanner, yytext, yyleng);
}
<COMMENT>\n {
yyextra->strbuf[yyextra->length] = '\0';
yylval->str = yyextra->strbuf;
BEGIN INITIAL;
yyextra->strbuf = NULL;
yyextra->lineno++;
return COMMENT_P;
}
<BUILTIN>\n {
yyextra->lineno++;
addchar(yyscanner, yytext[0]);
}
<BUILTIN>\%\} {
yyextra->strbuf[yyextra->length] = '\0';
yylval->str = yyextra->strbuf;
BEGIN INITIAL;
yyextra->strbuf = NULL;
return BUILTIN_P;
}
<BUILTIN>. {
addchar(yyscanner, yytext[0]);
}
<BUILTIN><<EOF>> { return scan_yyerror("Unexpected end of file", yyextra->lineno); }
<INITIAL,COMMENT><<EOF>> {
yyterminate();
}
<INITIAL>[ \t\r\f]+ { /* ignore */ }
<INITIAL>\n { yyextra->lineno++; }
<INITIAL>. { return scan_yyerror("syntax error: Unknown character", yyextra->lineno); }
%%
static int
scan_yyerror(char *msg, int lineno) {
fprintf(stderr, "scan_yyerror: %s at line %d\n", msg, lineno);
return 0;
}
prscfl_yyscan_t
prscflScannerInit(FILE *fh, prscfl_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;
}
void
prscflScannerFinish(prscfl_yyscan_t scanner) {
if (buf)
yy_delete_buffer( buf, scanner );
buf = NULL;
}
/*
* 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
prscflGetLineNo(prscfl_yyscan_t yyscanner) {
return yyextra->lineno;
}
static void
addstring(prscfl_yyscan_t yyscanner, char *s, int l) {
while( yyextra->length + l + 1 >= yyextra->total ) {
yyextra->total *= 2;
yyextra->strbuf=realloc(yyextra->strbuf, yyextra->total);
}
memcpy( yyextra->strbuf+yyextra->length, s, l);
yyextra->length+=l;
}
static void
addchar(prscfl_yyscan_t yyscanner, char s) {
if( yyextra->length + 2 >= yyextra->total ) {
yyextra->total*=2;
yyextra->strbuf=realloc(yyextra->strbuf, yyextra->total);
}
yyextra->strbuf[ yyextra->length++ ] = s;
}
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;
}