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
319
320
321
322
323
324
325
326
327
328
329
|
%top {
#include <assert.h>
#include <malloc.h>
#include <string.h>
#include <common/extstr.h>
#include "grammar.h"
}
%{
#define PUSH_STATE(s) yy_push_state(s, yyscanner)
#define POP_STATE yy_pop_state(yyscanner)
%}
%option bison-bridge reentrant
%option stack
%option nounput
//%option noinput
%option noyywrap
%option noyy_top_state
%option yylineno
%option never-interactive
%x encoding
%x escaped_str
%x plain_str
%%
%{
/* no init C code */
%}
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return MUL; }
"/" { return DIV; }
"%" { return MOD; }
"<" { return LT; }
"<=" { return LE; }
"==" { return EQ; }
"!=" { return NE; }
">" { return GT; }
">=" { return GE; }
"<<" { return SHIFT_LEFT; }
">>" { return SHIFT_RIGHT; }
"&" { return BIT_AND; }
"|" { return BIT_OR; }
"^" { return BIT_XOR; }
"not" { return NOT; }
"and" { return AND; }
"or" { return OR; }
"(" { return PAREN_O; }
")" { return PAREN_C; }
"[" { return HOOK_O; }
"]" { return HOOK_C; }
"," { return COMMA; }
"." { return DOT; }
"?" { return QMARK; }
":" { return COLON; }
"::" { return DOUBLE_COLON; }
".size" { return METH_SIZE; }
".length" { return METH_LENGTH; }
".reverse" { return METH_REVERSE; }
".substring" { return METH_SUBSTRING; }
".to_i" { return METH_TO_I; }
".to_i(" { return METH_TO_I_RAD; }
".to_s" { return METH_TO_S; }
".to_s(" { PUSH_STATE(encoding); return METH_TO_S_ENC; }
"_root" { return ROOT; }
"_parent" { return PARENT; }
"_" { return LAST; }
"_io" { return IO; }
"._io" { return METH_IO; }
".eof" { return IO_EOF; }
"true" { return TRUE_CONST; }
"false" { return FALSE_CONST; }
%{ /* Lecteurs de valeurs entières */ %}
0[bB][01]+ {
char *__end;
yylval->unsigned_integer = strtoull(yytext + 2, &__end, 2);
if (__end != (yytext + yyleng))
YY_FATAL_ERROR("failed to parse integer");
return UNSIGNED_INTEGER;
}
0[bB][01]{1,4}(_[01]{4})+ {
char *__tmp;
char *__end;
__tmp = strdup(yytext);
__tmp = strrpl(__tmp, "_", "");
yylval->unsigned_integer = strtoull(__tmp + 2, &__end, 2);
if (__end != (__tmp + strlen(__tmp)))
{
free(__tmp);
YY_FATAL_ERROR("failed to parse integer");
}
else free(__tmp);
return UNSIGNED_INTEGER;
}
(0|[1-9][0-9]*) {
char *__end;
yylval->unsigned_integer = strtoull(yytext, &__end, 10);
if (__end != (yytext + yyleng))
YY_FATAL_ERROR("failed to parse integer");
return UNSIGNED_INTEGER;
}
[1-9][0-9]{0,2}(_[1-9][0-9]{2})+ {
char *__tmp;
char *__end;
__tmp = strdup(yytext);
__tmp = strrpl(__tmp, "_", "");
yylval->unsigned_integer = strtoull(__tmp, &__end, 10);
if (__end != (__tmp + strlen(__tmp)))
{
free(__tmp);
YY_FATAL_ERROR("failed to parse integer");
}
else free(__tmp);
return UNSIGNED_INTEGER;
}
-(0|[1-9][0-9]*) {
char *__end;
yylval->signed_integer = strtoll(yytext, &__end, 10);
if (__end != (yytext + yyleng))
YY_FATAL_ERROR("failed to parse integer");
return SIGNED_INTEGER;
}
-[1-9][0-9]{0,2}(_[1-9][0-9]{2})+ {
char *__tmp;
char *__end;
__tmp = strdup(yytext);
__tmp = strrpl(__tmp, "_", "");
yylval->signed_integer = strtoll(__tmp, &__end, 10);
if (__end != (__tmp + strlen(__tmp)))
{
free(__tmp);
YY_FATAL_ERROR("failed to parse integer");
}
else free(__tmp);
return SIGNED_INTEGER;
}
0[xX][0-9a-fA-F]+ {
char *__end;
yylval->unsigned_integer = strtoull(yytext, &__end, 16);
if (__end != (yytext + yyleng))
YY_FATAL_ERROR("failed to parse integer");
return UNSIGNED_INTEGER;
}
0[xX][0-9a-fA-F]{1,4}(_[0-9a-fA-F]{4})+ {
char *__tmp;
char *__end;
__tmp = strdup(yytext);
__tmp = strrpl(__tmp, "_", "");
yylval->unsigned_integer = strtoull(__tmp, &__end, 16);
if (__end != (__tmp + strlen(__tmp)))
{
free(__tmp);
YY_FATAL_ERROR("failed to parse integer");
}
else free(__tmp);
return UNSIGNED_INTEGER;
}
-?(0|[1-9][0-9]*\.[0-9]+) {
char *__end;
yylval->floating_number = strtod(yytext, &__end);
if (__end != (yytext + yyleng))
YY_FATAL_ERROR("failed to parse float");
return FLOAT;
}
%{ /* Paramètre d'encodage */ %}
<encoding>["'][-_A-Za-z0-9 ]+["'] {
yylval->sized_cstring.data = yytext + 1;
yylval->sized_cstring.len = yyleng - 2;
return ENCODING_NAME;
}
<encoding>")" { POP_STATE; return PAREN_C; }
[a-z][a-z0-9_]* {
yylval->sized_cstring.data = yytext;
yylval->sized_cstring.len = yyleng;
return IDENTIFIER;
}
[^\\\[\],"'()\.: ]+ {
yylval->sized_cstring.data = yytext;
yylval->sized_cstring.len = yyleng;
return PLAIN_BYTES;
}
%{ /* Lecteurs des tableaux de définition d'octets */ %}
"\"" { PUSH_STATE(escaped_str); }
<escaped_str>[^\\"]+ {
yylval->sized_cstring.data = yytext;
yylval->sized_cstring.len = yyleng;
return RAW_BYTES;
}
<escaped_str>"\\a" { yylval->byte = '\a'; return RAW_BYTE; }
<escaped_str>"\\b" { yylval->byte = '\b'; return RAW_BYTE; }
<escaped_str>"\\t" { yylval->byte = '\t'; return RAW_BYTE; }
<escaped_str>"\\n" { yylval->byte = '\n'; return RAW_BYTE; }
<escaped_str>"\\v" { yylval->byte = '\v'; return RAW_BYTE; }
<escaped_str>"\\f" { yylval->byte = '\f'; return RAW_BYTE; }
<escaped_str>"\\r" { yylval->byte = '\r'; return RAW_BYTE; }
<escaped_str>"\\e" { yylval->byte = '\e'; return RAW_BYTE; }
<escaped_str>"\\\"" { yylval->byte = '"'; return RAW_BYTE; }
<escaped_str>"\\'" { yylval->byte = '\''; return RAW_BYTE; }
<escaped_str>"\\\\" { yylval->byte = '\\'; return RAW_BYTE; }
<escaped_str>"\\0" { yylval->byte = '\0'; return RAW_BYTE; }
<escaped_str>\\[0-9]{1,3} {
char __tmp[4];
memcpy(__tmp, yytext + 1, yyleng - 1);
__tmp[yyleng] = '\0';
yylval->byte = strtoull(__tmp, NULL, 8);
return RAW_BYTE;
}
<escaped_str>"\"" { POP_STATE; }
"'" { PUSH_STATE(plain_str); }
<plain_str>[^']+ {
yylval->sized_cstring.data = yytext;
yylval->sized_cstring.len = yyleng;
return PLAIN_BYTES;
}
<plain_str>['] { POP_STATE; }
[.]$ {
#ifndef NDEBUG
int ch;
#endif
yylval->sized_cstring.data = yytext;
yylval->sized_cstring.len = yyleng;
#ifndef NDEBUG
ch = input(yyscanner);
assert(ch == '\n');
#else
input(yyscanner);
#endif
return RAW_BYTES_WITH_ENDING_DOT;
}
[^\\\[\],"'()\.: ]+[.]$ {
#ifndef NDEBUG
int ch;
#endif
yylval->sized_cstring.data = yytext;
yylval->sized_cstring.len = yyleng;
#ifndef NDEBUG
ch = input(yyscanner);
assert(ch == '\n');
#else
input(yyscanner);
#endif
return RAW_BYTES_WITH_ENDING_DOT;
}
%{ /* Actions par défaut */ %}
<*>[ \t\n]+ { }
<*>. {
char *msg;
int ret;
ret = asprintf(&msg,
"Unhandled token in rule definition: '%s'",
yytext);
if (ret == -1)
YY_FATAL_ERROR("Unhandled token in undisclosed rule definition");
else
{
YY_FATAL_ERROR(msg);
free(msg);
}
}
%%
|