-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ast.c
241 lines (179 loc) · 3.63 KB
/
ast.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
/*
* Copyright 2014-2017 Katherine Flavel
*
* See LICENCE for the full copyright terms.
*/
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "txt.h"
#include "ast.h"
#include "xalloc.h"
static int
isalphastr(const struct txt *t)
{
size_t i;
assert(t != NULL);
assert(t->p != NULL);
for (i = 0; i < t->n; i++) {
if (isalpha((unsigned char) t->p[i])) {
return 1;
}
}
return 0;
}
struct ast_term *
ast_make_empty_term(int invisible)
{
struct ast_term *new;
new = xmalloc(sizeof *new);
new->type = TYPE_EMPTY;
new->next = NULL;
new->min = 1;
new->max = 1;
new->invisible = invisible;
return new;
}
struct ast_term *
ast_make_rule_term(int invisible, struct ast_rule *rule)
{
struct ast_term *new;
assert(rule != NULL);
new = xmalloc(sizeof *new);
new->type = TYPE_RULE;
new->next = NULL;
new->u.rule = rule;
new->min = 1;
new->max = 1;
new->invisible = invisible;
return new;
}
struct ast_term *
ast_make_char_term(int invisible, char c)
{
struct ast_term *new;
char *a;
a = xmalloc(1); /* XXX: i don't like this */
a[0] = c;
new = xmalloc(sizeof *new);
new->type = TYPE_CS_LITERAL;
new->next = NULL;
new->u.literal.p = a;
new->u.literal.n = 1;
new->min = 1;
new->max = 1;
new->invisible = invisible;
return new;
}
struct ast_term *
ast_make_literal_term(int invisible, const struct txt *literal, int ci)
{
struct ast_term *new;
assert(literal != NULL);
assert(literal->p != NULL);
new = xmalloc(sizeof *new);
new->type = ci ? TYPE_CI_LITERAL : TYPE_CS_LITERAL;
new->next = NULL;
new->u.literal = *literal;
/* no need for case-insensitive strings if there are no letters */
if (!isalphastr(&new->u.literal)) {
new->type = TYPE_CS_LITERAL;
}
new->min = 1;
new->max = 1;
new->invisible = invisible;
return new;
}
struct ast_term *
ast_make_token_term(int invisible, const char *token)
{
struct ast_term *new;
assert(token != NULL);
new = xmalloc(sizeof *new);
new->type = TYPE_TOKEN;
new->next = NULL;
new->u.token = token;
new->min = 1;
new->max = 1;
new->invisible = invisible;
return new;
}
struct ast_term *
ast_make_prose_term(int invisible, const char *prose)
{
struct ast_term *new;
assert(prose != NULL);
new = xmalloc(sizeof *new);
new->type = TYPE_PROSE;
new->next = NULL;
new->u.prose = prose;
new->min = 1;
new->max = 1;
new->invisible = invisible;
return new;
}
struct ast_term *
ast_make_group_term(int invisible, struct ast_alt *group)
{
struct ast_term *new;
new = xmalloc(sizeof *new);
new->type = TYPE_GROUP;
new->next = NULL;
new->u.group = group;
new->min = 1;
new->max = 1;
new->invisible = invisible;
return new;
}
struct ast_alt *
ast_make_alt(int invisible, struct ast_term *terms)
{
struct ast_alt *new;
new = xmalloc(sizeof *new);
new->terms = terms;
new->next = NULL;
new->invisible = invisible;
return new;
}
struct ast_rule *
ast_make_rule(const char *name, struct ast_alt *alts)
{
struct ast_rule *new;
assert(name != NULL);
new = xmalloc(sizeof *new);
new->name = name;
new->alts = alts;
new->next = NULL;
return new;
}
struct ast_rule *
ast_find_rule(const struct ast_rule *grammar, const char *name)
{
const struct ast_rule *p;
assert(name != NULL);
for (p = grammar; p != NULL; p = p->next) {
if (0 == strcmp(p->name, name)) {
return (struct ast_rule *) p;
}
}
return NULL;
}
void
ast_free_rule(struct ast_rule *rule)
{
/* XXX: free contents */
free(rule);
}
void
ast_free_alt(struct ast_alt *alt)
{
/* XXX: free contents */
free(alt);
}
void
ast_free_term(struct ast_term *term)
{
/* XXX: free contents */
free(term);
}