-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathscanner.l
executable file
·320 lines (299 loc) · 7.82 KB
/
scanner.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
319
320
%{
/*
* Bill's ABNF parser.
* Copyright 2002-2007 William C. Fenner <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY WILLIAM C. FENNER ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WILLIAM C. FENNER OR HIS
* BROTHER B1FF BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "y.tab.h"
int mylineno = 1;
int mycolumn = 0;
int myerrors = 0;
extern int permissive;
int indent = -1;
char badchar;
static void scanrange(char *, int, struct range *);
static char *scanstr(char *, int);
static char *get_charval(char *);
static void gotcr(void);
%}
bit [01]
digit [0-9]
hexdig [0-9A-Fa-f]
rulename [A-Za-z][-0-9A-Za-z]*
wsp [ \t]
/* *(%x20-21 / %x23-7E) */
charval [ !#-~]*
/* *(%x20-3D / %x3F-7E) */
proseval [ -=?-~]*
mycrlf (\n\r|\r\n|\r|\n)
/* %x may be a flex feature; %s works but sometimes results in
* confusing error messages */
%x SKIP
/* line number isn't quite being updated properly.
suspect unterminated charval and prosevals. */
%%
<SKIP>.*{mycrlf} { char q = (badchar == '\'') ? '"' : '\'';
mywarn(MYERROR, "Illegal character %c%c%c - skipping to end of line", q, badchar, q);
gotcr();
BEGIN(INITIAL);
return CRLF; }
\"{charval}["\r\n] {
char *p;
mycolumn += strlen(yytext);
yylval.string = get_charval(yytext);
return CHARVAL;
}
%[Ii]\"{charval}["\r\n] {
char *p;
mycolumn += strlen(yytext);
yylval.string = get_charval(yytext+2);
return CHARVAL_I;
}
%[Ss]\"{charval}["\r\n] {
char *p;
mycolumn += strlen(yytext);
yylval.string = get_charval(yytext+2);
return CHARVAL_S;
}
\<{proseval}[>\r\n] {
char *p;
mycolumn += strlen(yytext);
yylval.string = strdup(yytext + 1);
p = &yylval.string[strlen(yylval.string) - 1];
if (*p != '>') {
mywarn(MYERROR, "unterminated prose-val");
unput(*p); /* put the cr or lf back */
}
*p = '\0';
return PROSEVAL;
}
{rulename} {
/* record the indentation of the first rule name. */
if (indent == -1)
indent = mycolumn;
mycolumn += strlen(yytext);
yylval.string = strdup(yytext);
return RULENAME;
}
%[Bb]{bit}+(-|\.\.){bit}+ {
mycolumn += strlen(yytext);
scanrange(yytext + 2, 2, &yylval.range);
return BINVALRANGE;
}
%[Bb]{bit}+(\.{bit}+)* {
mycolumn += strlen(yytext);
yylval.string = scanstr(yytext + 2, 2);
return BINVAL;
}
%[Bb]. { mywarn(MYERROR, "bad bit value");
badchar = yytext[2]; BEGIN(SKIP); }
%[Dd]{digit}+(-|\.\.){digit}+ {
mycolumn += strlen(yytext);
scanrange(yytext + 2, 10, &yylval.range);
return DECVALRANGE;
}
%[Dd]{digit}+(\.{digit}+)* {
mycolumn += strlen(yytext);
yylval.string = scanstr(yytext + 2, 10);
return DECVAL;
}
%[Dd]. { mywarn(MYERROR, "bad decimal value");
badchar = yytext[2]; BEGIN(SKIP); }
%[Xx]{hexdig}+(-|\.\.){hexdig}+ {
mycolumn += strlen(yytext);
scanrange(yytext + 2, 16, &yylval.range);
return HEXVALRANGE;
}
%[Xx]{hexdig}+(\.{hexdig}+)* {
mycolumn += strlen(yytext);
yylval.string = scanstr(yytext + 2, 16);
return HEXVAL;
}
%[Xx]. { mywarn(MYERROR, "bad hex value");
badchar = yytext[2]; BEGIN(SKIP); }
{digit}*\*{digit}* {
char *ep;
mycolumn += strlen(yytext);
yylval.range.lo = strtoul(yytext, &ep, 10);
if (*ep != '*') {
mywarn(MYERROR, "internal scanner error 1");
yylval.range.hi = -1;
} else {
yylval.range.hi = strtoul(ep + 1, &ep, 10);
if (*ep) {
mywarn(MYERROR, "internal scanner error 2");
yylval.range.hi = -1;
} else if (yylval.range.hi == 0)
yylval.range.hi = -1;
}
return REPEAT;
}
{digit}+ {
char *ep;
mycolumn += strlen(yytext);
yylval.range.hi = yylval.range.lo = strtoul(yytext, &ep, 10);
if (*ep) {
mywarn(MYERROR, "internal scanner error 3");
yylval.range.hi = yylval.range.lo = 42;
}
return REPEAT;
}
=\/ { mycolumn += 2; return EQSLASH; }
({wsp}+|(;[^\r\n]*)|{mycrlf}{wsp}+)+ { char *p = yytext;
while (*p) {
/* TO DO:
* deal with indent if we
* have one set - if a blank
* line or a comment is indented
* less than enough, we warn
* about it. */
if (*p == '\r') {
gotcr();
if (*(++p) == '\n')
p++;
continue;
}
if (*p == '\n') {
gotcr();
if (*(++p) == '\r')
p++;
continue;
}
p++;
mycolumn++;
}
/* If we don't know the indent yet, then just
ignore leading whitespace. */
if (indent == -1)
continue;
/* If there is more whitespace than
the initial indent, then tell the parser
about the leading whitespace. */
if (mycolumn > indent)
return CWSP;
if (mycolumn < indent) {
indent = mycolumn;
mywarn(MYERROR, "adjusting indentation");
}
/* Since we didn't have more whitespace than
indent, tell the parser it was just
a CR. */
return CRLF; }
{mycrlf} { gotcr(); return CRLF; }
[][()=/] { mycolumn++; return yytext[0]; }
\| { mycolumn++;
if (!permissive) {
badchar = yytext[0];
BEGIN(SKIP);
}
return yytext[0]; }
. { mycolumn++; badchar = yytext[0]; BEGIN(SKIP); }
%%
static void
scanrange(char *p, int base, struct range *r)
{
char *ep;
r->lo = strtoul(p, &ep, base);
if (*ep != '-' && *ep != '.') {
mywarn(MYERROR, "internal scanner error 4");
r->hi = r->lo;
return;
}
if (*ep == '.') {
if (!permissive) {
badchar = '.';
BEGIN(SKIP);
}
mywarn(MYERROR, "Ranges use \"-\", not \"..\".");
ep++;
}
r->hi = strtoul(ep + 1, &ep, base);
if (*ep) {
mywarn(MYERROR, "internal scanner error 5");
}
if (r->hi < r->lo) {
mywarn(MYERROR, "inverted range");
}
return;
}
static char *
scanstr(char *p, int base)
{
char *ep;
char buf[512]; /*XXX*/
char *b = buf;
int i;
do {
i = strtoul(p, &ep, base);
if (i > 255) { /* XXX */
mywarn(MYWARNING, "I can't handle this legal ABNF char value");
i = 255;
}
if (i == 0) {
mywarn(MYERROR, "This parser will truncate strings at %%x00");
}
*b++ = i;
p = ep + 1;
} while (*ep == '.');
if (*ep)
mywarn(MYERROR, "internal scanner error 6");
*b++ = '\0';
return strdup(buf);
}
static char *
get_charval(char *qp)
{
char *p;
char *r;
r = strdup(qp + 1);
p = &r[strlen(r) - 1];
if (*p != '"') {
mywarn(MYERROR, "unterminated char-val");
unput(*p); /* put the cr or lf back */
}
*p = '\0';
if (*r == '\0')
mywarn(MYWARNING, "zero-length char-val");
return r;
}
static void
gotcr(void)
{
mylineno++;
mycolumn = 0;
}
void
scanreset(void) {
mylineno = 0;
mycolumn = 0;
indent = -1;
}