-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtoken.c
More file actions
450 lines (426 loc) · 9.99 KB
/
token.c
File metadata and controls
450 lines (426 loc) · 9.99 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
447
448
449
#include <stdio.h>
#include <string.h> /* for strchr() */
#include <ctype.h>
#include <math.h>
#include "token.h"
#include "eprintf.h"
#include "db.h"
#include "xwin.h"
#include "rlgetc.h"
#include "ev.h"
#include "expr.h" /* for evaluating expression */
static char promptbuf[128];
void token_set_mode(LEXER *lp, int mode) {
lp->parse = mode;
}
LEXER *token_stream_open(FILE *fp, char *name) {
LEXER *lp;
lp = (LEXER *) emalloc(sizeof(struct lexer));
lp->name = strsave(name);
lp->word[0] = '\0'; /* place to store token return value */
lp->bufp = 0; /* no tokens in token pushback buf */
lp->token_stream = fp;
lp->pbufp = 0; /* no characters in pushback buffer */
lp->mode = MAIN;
lp->line = 1; /* keep track of line number for stream */
lp->parse = 0; /* parsing mode: 0=normal, 1=raw */
return (lp);
}
int token_err(char *modulename, LEXER *lp, char *expected, TOKEN token)
{
if (strcmp(lp->name, "STDIN") == 0) {
printf("%s: %s, got %s\n", modulename, expected, tok2str(token));
} else {
printf("%s: in %s, line %d: %s, got %s\n",
modulename, lp->name, lp->line, expected, tok2str(token));
}
return(0);
}
int token_stream_close(LEXER *lp) {
int retcode;
retcode=fclose(lp->token_stream);
free(lp->name);
free(lp);
return(retcode);
}
/* lookahead to see the next token */
TOKEN token_look(LEXER *lp, char **word)
{
TOKEN token;
token=token_get(lp, word);
token_unget(lp, token, *word);
return token;
}
/* stuff back a token */
int token_unget(LEXER *lp, TOKEN token, char *word)
{
int debug=0;
if (debug) printf("ungetting %s\n", word);
if (lp->bufp >= BUFSIZE) {
eprintf("ungettoken: too many characters");
return(-1);
} else {
lp->tokbuf[lp->bufp].word = (char *) estrdup(word);
lp->tokbuf[lp->bufp++].tok = token;
return(0);
}
}
int token_flush_EOL(LEXER *lp)
{
char *word;
TOKEN token;
while ((token=token_get(lp, &word) != EOL) && (token != EOF)) {
;
}
return(0);
}
TOKEN token_get(LEXER *lp, char **word) /* collect and classify token */
{
enum {NEUTRAL,INQUOTE,INWORD,INOPT,INNUM,INVAR,INEXPR,COMMENT} state = NEUTRAL;
int c,d,retval;
char *w;
int debug=0;
char *str;
double val;
int parencount=0;
*word = lp->word; /* set up token text return value */
if (lp->bufp > 0) { /* characters in pushback buffer */
strcpy(*word, lp->tokbuf[--(lp->bufp)].word);
if ((lp->tokbuf[lp->bufp].word) != NULL) {
free(lp->tokbuf[lp->bufp].word); // FIXME: have seen crashes here...
}
lp->tokbuf[lp->bufp].word = (char *) NULL;
return(lp->tokbuf[lp->bufp].tok);
}
w=lp->word;
if (lp->parse) { /* raw mode */
*w++ = (char) procXevent();
*w = '\0';
return(RAW); /* simply bypass readline */
}
w=lp->word;
while((c=rlgetc(lp)) != EOF) {
switch(state) {
case NEUTRAL:
switch(c) {
case ' ':
case '\t':
continue;
case '\n':
case '\r':
*w++ = c;
*w = '\0';
if (debug) printf("returning EOL: %s \n", lp->word);
lp->line++;
return(EOL);
case ',':
*w++ = c;
*w = '\0';
if (debug) printf("returning COMMA: %s \n", lp->word);
return(COMMA);
case '"':
state = INQUOTE;
strcpy(promptbuf, rl_saveprompt());
strcat(promptbuf, "(in quote)> ");
rl_setprompt(promptbuf);
continue;
case '^':
*w++ = c;
*w = '\0';
if (debug) printf("returning BACK: %s \n", lp->word);
return(BACK);
continue;
case ':':
state = INOPT;
*w++ = c;
continue;
case ';':
*w++ = c;
*w = '\0';
if (debug) printf("returning EOC: %s \n", lp->word);
return(EOC);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
*w++ = c;
state = INNUM;
continue;
case '+':
case '-':
case '#':
*w++ = c;
c = rlgetc(lp);
rl_ungetc(lp,c);
if (isdigit(c) || c=='.') {
state = INNUM;
} else {
state = INOPT;
}
continue;
case '@':
*w++ = c;
state = INOPT;
continue;
case '(':
state = INEXPR;
parencount=1;
strcpy(promptbuf, rl_saveprompt());
strcat(promptbuf, "(in a parenthesized expression)> ");
rl_setprompt(promptbuf);
continue;
case '$':
// *w++ = c; /* don't save the dollar */
c = rlgetc(lp);
if (c=='$') {
state = COMMENT;
} else {
rl_ungetc(lp,c);
state = INVAR;
}
continue;
case '.':
*w++ = c;
c = rlgetc(lp);
if (isdigit(c)) {
rl_ungetc(lp,c);
state = INNUM;
} else {
rl_ungetc(lp,c);
state = INOPT;
}
continue;
default:
// printf("in default with c=%d\n", c);
state = INWORD;
*w++ = c;
continue;
}
case INNUM:
if (isdigit(c) || c=='.') {
*w++ = c;
continue;
/* took this out because of ambiguity with mouse */
/* clicks like "ADD L1MOV", 1MOV became ident rather */
/* than 1 and MOV being parsed separately */
/* } else if (isalnum(c) || (c=='_') || (c=='.') ) {
*w++ = c;
state = INWORD;
continue; */
} else {
rl_ungetc(lp,c);
*w = '\0';
if (debug) printf("returning NUMBER: %s \n", lp->word);
return(NUMBER);
}
case INVAR:
if (isalnum(c)) {
*w++ = c;
continue;
} else if (strchr("_+-.",c) != NULL) {
*w++ = c;
continue;
} else {
rl_ungetc(lp,c);
*w = '\0';
if (strncasecmp(lp->word,"FILES",5) == 0) {
return(CMD);
} else if ((str=EVget(lp->word)) == NULL) {
lp->word[0]='\0';
} else {
// rl_ungets(lp, str);
strcpy(lp->word, str); // substitute definition
}
// w=lp->word;
// *w='\0';
// state = NEUTRAL;
return(QUOTE);
}
case INOPT:
if (isalnum(c)) {
*w++ = c;
continue;
} else if (strchr("+-.,",c) != NULL) {
*w++ = c;
continue;
} else {
rl_ungetc(lp,c);
*w = '\0';
if (debug) printf("returning OPT: %s \n", lp->word);
return(OPT);
}
case INEXPR:
switch(c) {
case ')':
if (--parencount == 0) {
*w = '\0';
// if (debug) printf("got EXPR: %s \n", lp->word);
rl_restoreprompt();
retval = parse_exp(&val, lp->word);
// printf("got EXPR:\"%s\" %f (%d)\n",lp->word,val,retval);
if (retval) {
strcpy(lp->word, "BAD-EXPRESSION");
return(UNKNOWN);
}
sprintf(lp->word, "%g", val-fmod(val,1.0/pow(10.0,RES)));
return(NUMBER);
} else {
*w++ = c;
continue;
}
case '(':
parencount++;
// fall through
default:
*w++ = c;
continue;
}
case INQUOTE:
switch(c) {
case '\\':
/* escape quotes, but pass everything else along */
if ((d = rlgetc(lp)) == '"') {
*w++ = d;
} else {
*w++ = c;
*w++ = d;
}
continue;
case '"':
*w = '\0';
if (debug) printf("returning QUOTE: %s \n", lp->word);
rl_restoreprompt();
return(QUOTE);
default:
*w++ = c;
continue;
}
case INWORD:
if (!isalnum(c) && (c!='_') && (c!='.') && (c!='/') ) {
rl_ungetc(lp,c);
*w = '\0';
// printf("looking up <%s>\n", lp->word);
if (lookup_command(lp->word)) {
if (debug) printf("lookup returns CMD: %s\n", lp->word);
return(CMD);
} else {
if (debug) printf("lookup returns IDENT: %s\n", lp->word);
return(IDENT);
}
}
*w++ = c;
continue;
case COMMENT:
switch(c) {
case '\n':
case '\r':
*w++ = c;
*w = '\0';
if (debug) printf("returning EOL: %s \n", lp->word);
lp->line++;
return(EOL);
default:
continue;
}
default:
fprintf(stderr,"pig: error in lex loop\n");
return(UNKNOWN);
break;
} /* switch state */
} /* while loop */
return(EOF);
}
char *tok2str(TOKEN token)
{
switch (token) {
case IDENT: return("IDENT"); break;
case CMD: return("CMD"); break;
case QUOTE: return("QUOTE"); break;
case NUMBER: return("NUMBER"); break;
case OPT: return("OPT"); break;
case EOL: return("EOL"); break;
case EOC: return("EOC"); break;
case COMMA: return("COMMA"); break;
case BACK: return("BACK"); break;
case END: return("END"); break;
case COMMENT: return("COMMENT"); break;
default: return("UNKNOWN"); break;
}
}
int getnum(LEXER *lp, char *cmd, double *px, double *py)
{
int done=0;
TOKEN token;
char *word;
int state = 0;
int debug = 0;
if (debug) printf("in getnum\n");
while(!done) {
token = token_look(lp,&word);
if (debug) printf("got %s: %s\n", tok2str(token), word);
switch(state) {
case 0:
if (token == NUMBER) {
state = 1;
} else if (token == EOL) {
token_get(lp,&word); /* just ignore it */
} else {
return(0);
}
break;
case 1: /* get pair of xy coordinates */
if (debug) printf("in NUM1\n");
if (token == NUMBER) {
token_get(lp,&word);
sscanf(word, "%lf", px); /* scan it in */
state = 2;
} else if (token == EOL) {
token_get(lp,&word); /* just ignore it */
} else if (token == EOC || token == CMD) {
state = 4;
} else {
token_err(cmd, lp, "expected NUMBER", token);
state = 4;
}
break;
case 2:
if (debug) printf("in COM1\n");
if (token == EOL) {
token_get(lp,&word); /* just ignore it */
} else if (token == COMMA) {
token_get(lp,&word);
state = 3;
} else if (token == NUMBER) { /* this line makes the comma optional */
state = 3;
} else {
token_err(cmd, lp, "expected COMMA", token);
state = 4;
}
break;
case 3:
if (debug) printf("in NUM2\n");
if (token == NUMBER) {
token_get(lp,&word);
sscanf(word, "%lf", py); /* scan it in */
return(1);
} else if (token == EOL) {
token_get(lp,&word); /* just ignore it */
} else if (token == EOC || token == CMD) {
state = 4;
} else {
token_err(cmd, lp, "expected NUMBER", token);
state = 4;
}
break;
case 4:
done++;
}
}
return(0);
}