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
|
#include "parse.h"
#include "fn.h"
#include "errloc.h"
#include "lex.h"
#include "lit.h"
#include "prv.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
void ast_free(struct ast *ast)
{
for (struct lit *l = ast->lits, *next; l; l = next)
{
next = l->next;
lit_free(l);
free(l);
}
for (size_t i = 0; i < ast->nfns; i++)
fn_free(&ast->fns[i]);
free(ast->fns);
}
static const char *expected(const struct step *s)
{
static const char *tmap[] =
{
[ID] = "identifier", [LIT] = "literal", [NUM] = "number"
};
return s->id ? s->id : tmap[s->type];
}
static int iter(const struct lex *l, struct prv *p)
{
struct pos *pos = p->pos;
const struct step *s;
const struct tk *tk = p->tk;
if (!pos)
return lex_eof(l, tk) ? 0 : fn(l, p);
pos = &p->pos[p->i];
if (!(s = pos->step))
{
if (lex_eof(l, tk))
return 0;
else if (p->n > 1)
return pop(l, p) ? -1 : 1;
else
{
switch (tk->type)
{
case LIT:
errloc(tk, "unexpected literal \"%s\"", tk->s);
break;
case NUM:
errloc(tk, "unexpected number %s", tk->s);
break;
case ID:
errloc(tk, "unexpected %s %s", kw(tk->s) ?
"keyword" : "identifier", tk->s);
break;
case ANY:
case UNDEF:
errloc(tk, "%s: unreachable", __func__);
break;
}
return -1;
}
}
else if (lex_eof(l, tk))
{
if (p->stk == p->tk)
return pop(l, p) ? -1 : 1;
else if (s->type)
{
tk--;
if (lex_eof(l, p->stk))
{
errloc(tk, "expected %s", expected(pos->step));
return -1;
}
}
}
if (!s->type)
return pos->seq->fn(l, p);
else if ((s->type == ID || s->type == ANY) && !s->id && kw(tk->s)
&& !s->chain)
{
if (tk != p->stk || !p->n)
{
errloc(tk, "unexpected keyword %s", tk->s);
return -1;
}
else if (pop(l, p))
return -1;
pos = &p->pos[p->i];
}
else if ((s->type != tk->type && s->type != ANY)
|| (s->type == ID && s->id && strcmp(s->id, tk->s))
|| (s->type == ID && kw(tk->s) && s->chain))
{
pos->step = (++pos->seq)->steps;
p->tk = p->stk;
}
else
{
const struct tk *next = tk + 1;
if (!s->chain)
pos->step++;
else if (!lex_eof(l, next))
{
if (s->chain && kw(next->s))
pos->step++;
}
else
pos->step++;
p->tk++;
}
return 1;
}
static void free_prv(struct prv *p)
{
free(p->levels);
free(p->pos);
}
int parse(const struct lex *l, struct ast *ast)
{
int ret;
struct prv prv = {0};
prv.ast = ast;
if (!(prv.tk = l->tokens))
return 0;
while ((ret = iter(l, &prv)) > 0)
;
free_prv(&prv);
return ret;
}
|