ide_lex.c
7.42 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* ide - lexical scanner
*
*/
#include "ide.h"
#include "ide_gram.h"
#ident "gfx/common/ide/ide_lex.c $Revision: 1.1.1.1 $"
#ifdef YYDEBUG
extern int yydebug;
#endif
/* yyinbuff is the incoming stream to parse */
char yyinbuff[MAXLINELEN];
int yyinbp = 0;
int yysave = 0;
int yysflag = 0;
int yyinlen = 0;
int ide_currline = 0; /* current line number */
#define yygetc() (yysflag? yysflag=0, yysave: \
yyinbp < yyinlen ? yyinbuff[yyinbp++] : ide_refill())
#define yyungetc(c) (yysflag = yysave = c)
extern int yychar; /* current parser token */
extern int goteof; /* user type ctrl-d */
extern int badflag;/* after parsing is finished, don't execute the result */
extern int inrawcmd;/* parsing a diag command that doesn't want parsed args */
/* keyword table must be in lexical order */
#define NKEYWORDS 16
struct keyword_s {
char *name;
int type;
} keytable[] = {
"break", KWBREAK,
"case", KWCASE,
"continue", KWCONTINUE,
"default", KWDEFAULT,
"do", KWDO,
"else", KWELSE,
"fi", KWFI,
"for", KWFOR,
"forever", KWFOREVER,
"if", KWIF,
"print", KWPRINT,
"repeat", KWREPEAT,
"return", KWRETURN,
"switch", KWSWITCH,
"while", KWWHILE,
"", 0
};
/*
* called by parser whenever an error occurs
*
*/
yyerror(s)
char *s;
{
extern char *ide_sname;
if ( ! strcmp(s, "syntax error") ) {
/*
* Let's see if we can't get a better error message than this.
*/
if ( yychar == 0 )
printf("unexpected end-of-file");
else {
printf("syntax error");
if ( yychar >= ' ' && yychar < 127 )
printf(" near '%c'", yychar );
}
if ( ide_sname )
printf(" at line %d of %s", ide_currline+1, ide_sname);
putchar('\n');
}
else
printf("%s\n",s);
badflag = 1; /* don't attempt to execute this statement */
return;
}
/*
* yylex - return next token from the input stream
*
* This routine gathers up the next token and returns it via the
* global variable "yylval" and the return code.
*
*/
yylex()
{
int c;
sym_t *psym;
#ifdef YYDEBUG
yydebug = 1;
#endif
skipwhite:
/* skip white space */
while( (c = yygetc()) == ' ' || c == '\t' )
;
if ( c == '\n' ) {
ide_currline++;
return c;
}
if ( c == EOF ) {
goteof = 1;
return EOF;
}
if ( inrawcmd && c != ';' && c != '"' && c != ')' && c != '`') {
char buf[MAXIDENT+1];
char *bp = buf;
int bc = 0;
while ( c != ' ' && c != '\t' && c != '\n' && c != ';' ) {
*bp++ = c;
c = yygetc();
if ( ++bc == MAXIDENT )
break;
}
*bp = '\0';
yyungetc(c);
yylval.strval = makestr(buf);
return STRCONST;
}
/* skip shell-style comment */
if ( c == '#' ) {
while ( (c = yygetc()) != '\n' && c != EOF )
;
ide_currline++;
return '\n';
}
/* skip C-style comment */
if ( c == '/' )
if ( (c = yygetc()) == '*' ) {
while ( (c = yygetc()) != EOF )
if ( c == '\n' )
ide_currline++;
else
if ( c == '*' && (c = yygetc()) == '/' )
break;
return '\n';
}
else {
yyungetc(c);
c = '/';
}
/* unquoted word */
if ( isalpha(c) ) {
char buf[MAXIDENT+1];
char *bp = buf;
int bc = 0;
while ( (isalnum(c) || c == '_') && bc < MAXIDENT ) {
*bp++ = c;
bc++;
c = yygetc();
}
if ( ! c )
printf("unexpected null in input\n");
yyungetc(c);
*bp = '\0';
if ( bc = findkey(buf) )
return bc;
else
if ( ! (psym = findsym(buf)) ) {
yylval.strval = makestr(buf);
return IDENT;
}
yylval.psym = psym;
switch( psym->sym_type ) {
case SYM_GLOBAL:
case SYM_VAR:
return VAR;
case SYM_CMD:
return CMD;
default:
yyerror("internal error: lex: bad sym type\n");
return EOF;
}
}
if ( isdigit(c) || c == '-' ) {
int val = 0;
int base = 10;
int sign;
if ( c == '-' ) {
c = yygetc();
if ( ! isdigit(c) ) {
yyungetc(c);
c = '-';
goto notanumber;
}
sign = -1;
}
else
sign = 1;
if ( c == '0' ) {
base = 8;
c = yygetc();
if ( c == 'x' || c == 'X' ) {
base = 16;
c = yygetc();
}
else
if ( c == 'b' || c == 'B' ) {
base = 2;
c = yygetc();
}
}
while ( isxdigit(c) ) {
if (base == 16 && c >= 'a' && c <= 'f' )
c = (c - 'a')+10;
else
if (base == 16 && c >= 'A' && c <= 'F' )
c = (c - 'A')+10;
else
if ( (c - '0') < base )
c -= '0';
else
break;
val = (val * base) + c;
c = yygetc();
}
yyungetc(c);
yylval.intval = val * sign;
return INTCONST;
}
notanumber:
/* normally quoted string */
if ( c == '"' ) {
char buf[MAXSTRLEN+1];
char *bp = buf;
char bc = 0;
c = yygetc();
while ( c != '"' && bc < MAXSTRLEN ) {
if ( c == '\\' )
switch( c = yygetc() ) {
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 'b':
c = '\b';
break;
case '0': case '1': case '2':
case '3': case '4': case '5':
case '6': case '7':
{
int v;
c -= '0';
v = yygetc();
if ( v >= '0' && v <= '7' ) {
c = (c * 8)+(v-'0');
v = yygetc();
}
else {
yyungetc(v);
break;
}
if ( v >= '0' && v <= '7' )
c = (c * 8)+(v-'0');
else
yyungetc(v);
}
}
*bp++ = c;
bc++;
c = yygetc();
}
*bp = '\0';
yylval.strval = makestr(buf);
return STRCONST;
}
/* now a whole slew of special two character tokens */
if ( c == '!' ) {
if ( (c = yygetc()) == '=' )
return NE;
else
yyungetc(c);
return '!';
}
else
if ( c == '>' ) {
if ( (c = yygetc()) == '=' )
return GE;
else
if ( c == '>' ) {
if ( (c = yygetc()) == '=' )
return SHRASS;
else
yyungetc(c);
return SHR;
}
else
yyungetc(c);
return '>';
}
else
if ( c == '<' ) {
if ( (c = yygetc()) == '=' )
return LE;
else
if ( c == '<' ) {
if ( (c = yygetc()) == '=' )
return SHLASS;
else
yyungetc(c);
return SHL;
}
else
yyungetc(c);
return '<';
}
else
if ( c == '=' ) {
if ( (c = yygetc()) == '=' )
return EQ;
else
yyungetc(c);
return '=';
}
else
if ( c == '|' ) {
if ( (c = yygetc()) == '|' )
return LO;
else
if ( c == '=' )
return ORASS;
else
yyungetc(c);
return '|';
}
else
if ( c == '&' ) {
if ( (c = yygetc()) == '&' )
return LA;
else
if ( c == '=' )
return ANDASS;
else
yyungetc(c);
return '&';
}
else
if ( c == '^' ) {
if ( ( c = yygetc()) == '=' )
return XORASS;
else
yyungetc(c);
return '^';
}
else
if ( c == '+' ) {
if ( (c = yygetc()) == '+' )
return INCR;
else
if ( c == '=' )
return ADDASS;
else
yyungetc(c);
return '+';
}
else
if ( c == '-' ) {
if ( (c = yygetc()) == '-' )
return DECR;
else
if ( c == '=' )
return SUBASS;
else
yyungetc(c);
return '-';
}
else
if ( c == '*' ) {
if ( (c = yygetc()) == '=' )
return MULASS;
else
yyungetc(c);
return '*';
}
else
if ( c == '/' ) {
if ( (c = yygetc()) == '=' )
return DIVASS;
else
yyungetc(c);
return '/';
}
else
if ( c == '%' ) {
if ( (c = yygetc()) == '=' )
return MODASS;
else
yyungetc(c);
return '%';
}
return c;
}
/*
* Binary search for keyword s
*
*/
findkey(s)
char *s;
{
int high, low, test, i;
struct keyword_s *pkey;
low = 0;
high = NKEYWORDS+1;
do {
test = ((high - low) / 2) + low;
pkey = &keytable[test-1];
if ( ! (i = (int)*pkey->name - (int)*s) )
i = strcmp( pkey->name, s );
if ( i == 0 )
return pkey->type;
if ( i < 0 )
low = test;
else
high= test;
} while ( low+1 < high );
return 0;
}