-
Notifications
You must be signed in to change notification settings - Fork 26
/
token.c
423 lines (397 loc) · 9.94 KB
/
token.c
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
/* token.c -- lexical analyzer for es ($Revision: 1.1.1.1 $) */
#include "es.h"
#include "input.h"
#include "syntax.h"
#include "token.h"
#define isodigit(c) ('0' <= (c) && (c) < '8')
#define BUFSIZE ((size_t) 2048)
#define BUFMAX (8 * BUFSIZE)
typedef enum { NW, RW, KW } State; /* "nonword", "realword", "keyword" */
static State w = NW;
static Boolean newline = FALSE;
static Boolean goterror = FALSE;
static size_t bufsize = 0;
static char *tokenbuf = NULL;
#define InsertFreeCaret() STMT(if (w != NW) { w = NW; UNGETC(c); return '^'; })
/*
* Special characters (i.e., "non-word") in es:
* \t \n # ; & | ^ $ = ` ' ! { } ( ) < > \
*/
const char nw[] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, /* 0 - 15 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16 - 32 */
1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* ' ' - '/' */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* '0' - '?' */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* '@' - 'O' */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 'P' - '_' */
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* '`' - 'o' */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, /* 'p' - DEL */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 128 - 143 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 144 - 159 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 160 - 175 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 176 - 191 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 192 - 207 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 208 - 223 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 224 - 239 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 240 - 255 */
};
const char dnw[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0 - 15 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 16 - 32 */
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, /* ' ' - '/' */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* '0' - '?' */
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* '@' - 'O' */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 'P' - '_' */
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* '`' - 'o' */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, /* 'p' - DEL */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 128 - 143 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 144 - 159 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 160 - 175 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 176 - 191 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 192 - 207 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 208 - 223 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 224 - 239 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 240 - 255 */
};
/* print_prompt2 -- called before all continuation lines */
extern void print_prompt2(void) {
input->lineno++;
#if HAVE_READLINE
prompt = prompt2;
#else
if ((input->runflags & run_interactive) && prompt2 != NULL)
eprint("%s", prompt2);
#endif
}
/* scanerror -- called for lexical errors */
static void scanerror(char c, char *s) {
while (c != '\n' && c != EOF)
c = GETC();
goterror = TRUE;
yyerror(s);
}
/*
* getfds
* Scan in a pair of integers for redirections like >[2=1]. CLOSED represents
* a closed file descriptor (i.e., >[2=]).
*
* This function makes use of unsigned compares to make range tests in
* one compare operation.
*/
#define CLOSED -1
#define DEFAULT -2
static Boolean getfds(int fd[2], int c, int default0, int default1) {
int n;
fd[0] = default0;
fd[1] = default1;
if (c != '[') {
UNGETC(c);
return TRUE;
}
if ((unsigned int) (n = GETC() - '0') > 9) {
scanerror(n + '0', "expected digit after '['");
return FALSE;
}
while ((unsigned int) (c = GETC() - '0') <= 9)
n = n * 10 + c;
fd[0] = n;
switch (c += '0') {
case '=':
if ((unsigned int) (n = GETC() - '0') > 9) {
if (n != ']' - '0') {
scanerror(n + '0', "expected digit or ']' after '='");
return FALSE;
}
fd[1] = CLOSED;
} else {
while ((unsigned int) (c = GETC() - '0') <= 9)
n = n * 10 + c;
if (c != ']' - '0') {
scanerror(c + '0', "expected ']' after digit");
return FALSE;
}
fd[1] = n;
}
break;
case ']':
break;
default:
scanerror(c, "expected '=' or ']' after digit");
return FALSE;
}
return TRUE;
}
extern int yylex(void) {
static Boolean dollar = FALSE;
int c;
size_t i; /* The purpose of all these local assignments is to */
const char *meta; /* allow optimizing compilers like gcc to load these */
char *buf = tokenbuf; /* values into registers. On a sparc this is a */
YYSTYPE *y = &yylval; /* win, in code size *and* execution time */
if (goterror) {
goterror = FALSE;
return NL;
}
/* rc variable-names may contain only alnum, '*' and '_', so use dnw if we are scanning one. */
meta = (dollar ? dnw : nw);
dollar = FALSE;
if (newline) {
--input->lineno; /* slight space optimization; print_prompt2() always increments lineno */
print_prompt2();
newline = FALSE;
}
top: while ((c = GETC()) == ' ' || c == '\t')
w = NW;
if (c == EOF)
return ENDFILE;
if (!meta[(unsigned char) c]) { /* it's a word or keyword. */
InsertFreeCaret();
w = RW;
i = 0;
do {
buf[i++] = c;
if (i >= bufsize)
buf = tokenbuf = erealloc(buf, bufsize *= 2);
} while ((c = GETC()) != EOF && !meta[(unsigned char) c]);
UNGETC(c);
buf[i] = '\0';
w = KW;
if (buf[1] == '\0') {
int k = *buf;
if (k == '@' || k == '~')
return k;
} else if (*buf == 'f') {
if (streq(buf + 1, "n")) return FN;
if (streq(buf + 1, "or")) return FOR;
} else if (*buf == 'l') {
if (streq(buf + 1, "ocal")) return LOCAL;
if (streq(buf + 1, "et")) return LET;
} else if (streq(buf, "~~"))
return EXTRACT;
else if (streq(buf, "%closure"))
return CLOSURE;
else if (streq(buf, "match"))
return MATCH;
w = RW;
y->str = gcdup(buf);
return WORD;
}
if (c == '`' || c == '!' || c == '$' || c == '\'' || c == '=') {
InsertFreeCaret();
if (c == '!' || c == '=')
w = KW;
}
switch (c) {
case '!':
case '=':
return c;
case '`':
c = GETC();
if (c == '`') {
c = GETC();
if (c == '^')
return BBFLAT;
UNGETC(c);
return BACKBACK;
} else if (c == '^')
return BFLAT;
UNGETC(c);
return '`';
case '$':
dollar = TRUE;
switch (c = GETC()) {
case '#': return COUNT;
case '^': return FLAT;
case '&': return PRIM;
default: UNGETC(c); return '$';
}
case '\'':
w = RW;
i = 0;
while ((c = GETC()) != '\'' || (c = GETC()) == '\'') {
buf[i++] = c;
if (c == '\n')
print_prompt2();
if (c == EOF) {
w = NW;
scanerror(c, "eof in quoted string");
return ERROR;
}
if (i >= bufsize)
buf = tokenbuf = erealloc(buf, bufsize *= 2);
}
UNGETC(c);
buf[i] = '\0';
y->str = gcdup(buf);
return QWORD;
case '\\':
if ((c = GETC()) == '\n') {
print_prompt2();
UNGETC(' ');
goto top; /* Pretend it was just another space. */
}
if (c == EOF) {
UNGETC(EOF);
goto badescape;
}
UNGETC(c);
c = '\\';
InsertFreeCaret();
w = RW;
c = GETC();
switch (c) {
case 'a': *buf = '\a'; break;
case 'b': *buf = '\b'; break;
case 'e': *buf = '\033'; break;
case 'f': *buf = '\f'; break;
case 'n': *buf = '\n'; break;
case 'r': *buf = '\r'; break;
case 't': *buf = '\t'; break;
case 'x': case 'X': {
int n = 0;
for (;;) {
c = GETC();
if (!isxdigit(c))
break;
n = (n << 4)
| (c - (isdigit(c) ? '0' : ((islower(c) ? 'a' : 'A') - 0xA)));
}
if (n == 0)
goto badescape;
UNGETC(c);
*buf = n;
break;
}
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': {
int n = 0;
do {
n = (n << 3) | (c - '0');
c = GETC();
} while (isodigit(c));
if (n == 0)
goto badescape;
UNGETC(c);
*buf = n;
break;
}
default:
if (isalnum(c)) {
badescape:
scanerror(c, "bad backslash escape");
return ERROR;
}
*buf = c;
break;
}
buf[1] = 0;
y->str = gcdup(buf);
return QWORD;
case '#':
while ((c = GETC()) != '\n') /* skip comment until newline */
if (c == EOF)
return ENDFILE;
FALLTHROUGH;
case '\n':
input->lineno++;
newline = TRUE;
w = NW;
return NL;
case '(':
if (w == RW) /* not keywords, so let & friends work */
c = SUB;
FALLTHROUGH;
case ';':
case '^':
case ')':
case '{': case '}':
w = NW;
return c;
case '&':
w = NW;
c = GETC();
if (c == '&')
return ANDAND;
UNGETC(c);
return '&';
case '|': {
int p[2];
w = NW;
c = GETC();
if (c == '|')
return OROR;
if (!getfds(p, c, 1, 0))
return ERROR;
if (p[1] == CLOSED) {
scanerror(c, "expected digit after '='"); /* can't close a pipe */
return ERROR;
}
y->tree = mk(nPipe, p[0], p[1]);
return PIPE;
}
{
char *cmd;
int fd[2];
case '<':
fd[0] = 0;
if ((c = GETC()) == '>')
if ((c = GETC()) == '>') {
c = GETC();
cmd = "%open-append";
} else
cmd = "%open-write";
else if (c == '<')
if ((c = GETC()) == '<') {
c = GETC();
cmd = "%here";
} else
cmd = "%heredoc";
else if (c == '=')
return CALL;
else
cmd = "%open";
goto redirection;
case '>':
fd[0] = 1;
if ((c = GETC()) == '>')
if ((c = GETC()) == '<') {
c = GETC();
cmd = "%open-append";
} else
cmd = "%append";
else if (c == '<') {
c = GETC();
cmd = "%open-create";
} else
cmd = "%create";
goto redirection;
redirection:
w = NW;
if (!getfds(fd, c, fd[0], DEFAULT))
return ERROR;
if (fd[1] != DEFAULT) {
y->tree = (fd[1] == CLOSED)
? mkclose(fd[0])
: mkdup(fd[0], fd[1]);
return DUP;
}
y->tree = mkredircmd(cmd, fd[0]);
return REDIR;
}
default:
assert(c != '\0');
w = NW;
return c; /* don't know what it is, let yacc barf on it */
}
}
extern void inityy(void) {
newline = FALSE;
w = NW;
if (bufsize > BUFMAX) { /* return memory to the system if the buffer got too large */
efree(tokenbuf);
tokenbuf = NULL;
}
if (tokenbuf == NULL) {
bufsize = BUFSIZE;
tokenbuf = ealloc(bufsize);
}
}