/
rexp.c
423 lines (373 loc) · 9.73 KB
/
rexp.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
/********************************************
rexp.c
copyright 2008-2020,2023, Thomas E. Dickey
copyright 1991-1993,1996, Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/
/*
* $MawkId: rexp.c,v 1.39 2023/12/10 15:09:09 Paul.Eggert Exp $
*/
/* op precedence parser for regular expressions */
#include "rexp.h"
#include "regexp.h"
/* DATA */
int REerrno;
const char *const REerrlist[] =
{(char *) 0,
/* 1 */ "missing '('",
/* 2 */ "missing ')'",
/* 3 */ "bad class -- [], [^] or [",
/* 4 */ "missing operand",
/* 5 */ "resource exhaustion -- regular expression too large",
/* 6 */ "syntax error ^* or ^+",
/* 7 */ "bad interval expression",
/* 8 */ ""
};
/* ERR_5 is very unlikely to occur */
/* This table drives the operator precedence parser */
/* *INDENT-OFF* */
#ifdef NO_INTERVAL_EXPR
static short table[8][8] = {
/* 0 | CAT * + ? ( ) */
/* 0 */ {0, OP_L, OP_L, OP_L, OP_L, OP_L, OP_L, ERR_1},
/* | */ {OP_G, OP_G, OP_L, OP_L, OP_L, OP_L, OP_L, OP_G},
/* CAT*/ {OP_G, OP_G, OP_G, OP_L, OP_L, OP_L, OP_L, OP_G},
/* * */ {OP_G, OP_G, OP_G, OP_G, OP_G, OP_G, ERR_7, OP_G},
/* + */ {OP_G, OP_G, OP_G, OP_G, OP_G, OP_G, ERR_7, OP_G},
/* ? */ {OP_G, OP_G, OP_G, OP_G, OP_G, OP_G, ERR_7, OP_G},
/* ( */ {ERR_2, OP_L, OP_L, OP_L, OP_L, OP_L, OP_L, OP_EQ},
/* ) */ {OP_G , OP_G, OP_G, OP_G, OP_G, OP_G, ERR_7, OP_G}};
#else
static short table[10][10] = {
/* 0 | CAT * + ? ( ) { } */
/* 0 */ {0, OP_L, OP_L, OP_L, OP_L, OP_L, OP_L, ERR_1, ERR_7, OP_L},
/* | */ {OP_G, OP_G, OP_L, OP_L, OP_L, OP_L, OP_L, OP_G, OP_G, OP_G},
/* CAT*/ {OP_G, OP_G, OP_G, OP_L, OP_L, OP_L, OP_L, OP_G, OP_L, OP_G},
/* * */ {OP_G, OP_G, OP_G, OP_G, OP_G, OP_G, ERR_7, OP_G, OP_G, OP_G},
/* + */ {OP_G, OP_G, OP_G, OP_G, OP_G, OP_G, ERR_7, OP_G, OP_G, OP_G},
/* ? */ {OP_G, OP_G, OP_G, OP_G, OP_G, OP_G, ERR_7, OP_G, OP_G, OP_G},
/* ( */ {ERR_2, OP_L, OP_L, OP_L, OP_L, OP_L, OP_L, OP_EQ, OP_G, OP_G},
/* ) */ {OP_G , OP_G, OP_G, OP_G, OP_G, OP_G, ERR_7, OP_G, ERR_7, OP_G},
/* { */ {OP_G, OP_G, OP_G, OP_G, OP_G, OP_G, ERR_7, OP_G, OP_G, OP_EQ},
/* } */ {OP_G , OP_G, OP_G, OP_G, OP_G, OP_G, ERR_7, OP_G, ERR_7, OP_G} };
#endif
/* *INDENT-ON* */
#define STACKSZ 64
static jmp_buf err_buf; /* used to trap on error */
#if OPT_TRACE > 0
static const char *
token_name(int token)
{
const char *result;
#define CASE(name) case name: result = #name; break
switch (token) {
CASE(T_NONE);
CASE(T_OR);
CASE(T_CAT);
CASE(T_STAR);
CASE(T_PLUS);
CASE(T_Q);
CASE(T_LP);
CASE(T_RP);
CASE(T_START);
CASE(T_END);
CASE(T_ANY);
CASE(T_CLASS);
CASE(T_SLASH);
CASE(T_CHAR);
CASE(T_STR);
#ifndef NO_INTERVAL_EXPR
CASE(T_LB);
CASE(T_RB);
#endif
CASE(T_U);
default:
result = "?";
break;
}
#undef CASE
return result;
}
#endif
void
RE_error_trap(int x)
{
REerrno = x;
longjmp(err_buf, 1);
}
typedef struct {
int token;
int prec;
} OPS;
/* duplicate_m() relies upon copying machines whose size is 1, i.e., atoms */
#define BigMachine(mp) (((mp)->stop - (mp)->start) > 1)
STATE *
REcompile(char *re, size_t len)
{
MACHINE m_stack[STACKSZ];
OPS op_stack[STACKSZ];
register MACHINE *m_ptr;
register OPS *op_ptr;
register int t;
/* do this first because it also checks if we have a
run time stack */
RE_lex_init(re, len);
if (len == 0) {
STATE *p = (STATE *) RE_malloc(sizeof(STATE));
p->s_type = M_ACCEPT;
return p;
}
if (setjmp(err_buf))
return NULL;
/* we used to try to recover memory left on machine stack ;
but now m_ptr is in a register so it won't be right unless
we force it out of a register which isn't worth the trouble */
/* initialize the stacks */
m_ptr = m_stack - 1;
op_ptr = op_stack;
op_ptr->token = 0;
t = RE_lex(m_stack);
while (1) {
TRACE(("RE_lex token %s\n", token_name(t)));
switch (t) {
case T_STR:
case T_ANY:
case T_U:
case T_START:
case T_END:
case T_CLASS:
m_ptr++;
break;
#ifndef NO_INTERVAL_EXPR
case T_RB:
if (!repetitions_flag) {
goto default_case;
}
/* interval expression {n,m}
* eg,
* convert m{3} to mmm
* convert m{3,} to mmm* (with a limit of MAX_INT)
* convert m{3,10} to mmm* with a limit of 10
*/
if (intrvalmin == 0) { /* zero or more */
switch (intrvalmax) {
case 0:
/* user stupidity: m{0} or m{0,0}
* don't add this re token
*/
if (op_ptr != op_stack) {
/* no previous re */
RE_free(m_ptr->start);
m_ptr--;
switch (op_ptr->token) {
case T_RP:
while (op_ptr != op_stack) {
--op_ptr;
if (op_ptr->token == T_LP) {
if (op_ptr == op_stack) {
op_ptr->token = T_NONE;
} else {
--op_ptr;
}
break;
}
}
op_ptr = op_stack + 1;
break;
case T_LP:
break;
default:
op_ptr--;
break;
}
} else if (*re_exp == '\0') {
/* this was the only re expr
so leave one M_ACCEPT as the machine */
m_ptr->start->s_type = M_ACCEPT;
} else {
RE_free(m_ptr->start);
m_ptr--;
}
TRACE(("RE_lex token %s\n",
"of zero interval is ignored!"));
break;
case 1:
RE_01(m_ptr); /* m{0,1} which is m? */
TRACE(("RE_lex token %s\n", token_name(T_Q)));
break;
default:
RE_close_limit(m_ptr, intrvalmin, intrvalmax);
TRACE(("RE_lex token %s\n", token_name(T_Q)));
}
} else if (BigMachine(m_ptr)) {
RE_poscl_limit(m_ptr, intrvalmin, intrvalmax);
#ifdef NO_RI_LOOP_UNROLL
} else if (intrvalmin >= 1) { /* one or more */
RE_poscl_limit(m_ptr, intrvalmin, intrvalmax);
TRACE(("RE_lex token %s\n", token_name(T_PLUS)));
#else
} else if (intrvalmin == 1) { /* one or more */
RE_poscl_limit(m_ptr, intrvalmin, intrvalmax);
TRACE(("RE_lex token %s\n", token_name(T_PLUS)));
#endif
} else { /* n or more */
register Int i;
/* copy 2 copies of m_ptr, use 2nd copy to replace
the first copy that gets swallowed by concat */
MACHINE *result_mp = m_ptr;
MACHINE *concat_mp = (m_ptr + 1);
MACHINE *new_mp = (m_ptr + 2);
duplicate_m(concat_mp, result_mp);
duplicate_m(new_mp, result_mp);
for (i = 2; i <= intrvalmin; i++) {
RE_cat(result_mp, concat_mp);
duplicate_m(concat_mp, new_mp);
}
/* don't need 2nd copy in new_mp */
RE_free(new_mp->start);
}
break;
#endif /* ! NO_INTERVAL_EXPR */
case 0: /* end of reg expr */
if (op_ptr->token == 0) {
/* done */
if (m_ptr == m_stack) {
return m_ptr->start;
} else {
/* machines still on the stack */
RE_panic("values still on machine stack for %s", re);
}
}
/* FALLTHRU */
/* otherwise, default is operator case */
default:
#ifndef NO_INTERVAL_EXPR
default_case:
#endif
if ((op_ptr->prec = table[op_ptr->token][t]) == OP_G) {
do { /* op_pop */
if (op_ptr->token <= T_CAT) { /*binary op */
if (m_ptr == m_stack
&& op_ptr->token == T_CAT) {
TRACE(("...ignoring empty T_CAT\n"));
op_ptr--;
continue;
}
m_ptr--;
}
/* if not enough values on machine stack
then we have a missing operand */
if (m_ptr < m_stack)
RE_error_trap(-ERR_4);
switch (op_ptr->token) {
case T_CAT:
RE_cat(m_ptr, m_ptr + 1);
break;
case T_OR:
RE_or(m_ptr, m_ptr + 1);
break;
case T_STAR:
RE_close(m_ptr);
break;
case T_PLUS:
RE_poscl(m_ptr);
break;
case T_Q:
RE_01(m_ptr);
break;
default:
/*nothing on ( or ) */
break;
}
op_ptr--;
}
while (op_ptr->prec != OP_L);
continue; /* back thru switch at top */
}
if (op_ptr->prec < 0) {
if (op_ptr->prec == ERR_7)
RE_panic("parser returns ERR_7");
else
RE_error_trap(-op_ptr->prec);
}
if (++op_ptr == op_stack + STACKSZ) {
/* stack overflow */
RE_error_trap(-ERR_5);
}
op_ptr->token = t;
} /* end of switch */
if (m_ptr == m_stack + (STACKSZ - 1)) {
/*overflow */
RE_error_trap(-ERR_5);
}
t = RE_lex(m_ptr + 1);
}
}
void
REdestroy(STATE * ptr)
{
int done = 0;
int n = 0;
STATE *q = ptr;
TRACE(("REdestroy %p\n", (void *) ptr));
while (!done) {
TRACE(("...destroy[%d] %p type %s\n", n, q, REs_type(q)));
switch (q->s_type) {
case M_ACCEPT:
done = 1;
break;
case M_STR:
RE_free(q->s_data.str);
break;
default:
if (q->s_type < 0 || q->s_type > END_ON)
done = -1;
break;
}
++q;
++n;
}
RE_free(ptr);
}
/* getting here means a logic flaw or unforeseen case */
void
RE_panic(const char *format, ...)
{
const char *where = "REcompile() - panic: ";
va_list args;
fflush(stdout);
#if OPT_TRACE > 0
va_start(args, format);
Trace("?? %s", where);
TraceVA(format, args);
Trace("\n");
va_end(args);
#endif
fputs(where, stderr);
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr, "\n");
mawk_exit(100);
}
/* getting regexp error message */
const char *
REerror(void)
{
return REerrlist[REerrno];
}
#ifndef NO_INTERVAL_EXPR
/* duplicate a machine, oldmp into newmp */
void
duplicate_m(MACHINE * newmp, MACHINE * oldmp)
{
register STATE *p;
p = (STATE *) RE_malloc(2 * STATESZ);
RE_copy_states(p, oldmp->start, 2);
newmp->start = (STATE *) p;
newmp->stop = (STATE *) (p + 1);
}
#endif /* NO_INTERVAL_EXPR */