-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
266 lines (231 loc) · 7.93 KB
/
parser.c
File metadata and controls
266 lines (231 loc) · 7.93 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
#include "parser.h"
char *filter(char *content)
{
return content;
}
unsigned int char_index = 0;
unsigned int line = 1, col = 1;
/* Not part of the parser, just for convenience */
int formatting_depth = 0;
void format_sexp(struct sexp exp)
{
int pw;
for (pw = 0; pw < formatting_depth; pw++) {
printf(" ");
}
printf("### car=<%s> ", exp.car);
if (exp.id != NULL) {
printf("id=<%s> ", exp.id);
}
if (exp.cdr_subnodes) {
printf("(%u bytes, %u subexps)\n", exp.cdr_atom_length, exp.cdr_subnodes);
int e;
for (e = 0; e < exp.cdr_subnodes; e++) {
formatting_depth += 1;
format_sexp(exp.cdr_list[e]);
formatting_depth -= 1;
}
} else {
printf("cdr=<%s>, %u bytes\n", exp.cdr_atom, exp.cdr_atom_length);
}
}
struct sexp parse_document(char *content)
{
/* Expression buffer and index */
struct sexp *exp_list = malloc(sizeof(struct sexp) * EXP_LIST_LENGTH);
unsigned int exp_index = 0;
while (content[char_index]) {
struct sexp node = parse_expression(content);
if (node.car != "empty-expression") {
format_sexp(node);
exp_list[exp_index++] = node;
}
}
struct sexp root = {
.car = "document",
.cdr_subnodes = exp_index,
};
root.cdr_list = malloc(sizeof(struct sexp) * exp_index);
int e;
for (e = 0; e < exp_index; e++) {
root.cdr_list[e] = exp_list[e];
}
return root;
}
struct sexp parse_expression(char *content)
{
debug("parse_expression\n");
enum parser_state state = START;
/* Parse buffer and index */
char parse_buffer[PARSE_BUFFER_LENGTH] = {0};
unsigned int i = 0;
/* Expression buffer and index */
struct sexp *exp_list = malloc(sizeof(struct sexp) * EXP_LIST_LENGTH);
unsigned int exp_index = 0;
char *car = NULL, *id = NULL, *cdr = NULL;
/* Length of raw string in cdr */
unsigned int atom_length = 0;
/* Indices for error reporting */
unsigned int opening_line, opening_col;
unsigned int closing_line, closing_col;
/* Parsing conditions */
char complex_cdr = 0, whitespace_cdr = 1;
char c = ' ';
while (c) {
c = content[char_index];
debug("Reading #%d [%c], line %d, col %d: ", char_index, c, line, col);
/* Special cases */
if (c == '}' && (state == CAR || state == WHITESPACE)) {
abort_parsing("Syntax error", "%s expression has an empty body", car);
}
if (c == '\\') {
/* Escape next character */
char_index++;
parse_buffer[i++] = content[char_index++];
continue;
}
/* State machine */
if (state == START) {
debug("START\n");
if (c == '{') {
opening_line = line; opening_col = col;
state = CAR;
}
} else if (state == CAR) {
debug("CAR\n");
if (isspace(c)) {
parse_buffer[i++] = '\0';
car = strdup(parse_buffer);
state = WHITESPACE;
i = 0;
} else if (c == '#') {
parse_buffer[i++] = '\0';
car = strdup(parse_buffer);
state = ID;
i = 0;
char_index++;
continue;
} else {
parse_buffer[i++] = c;
}
} else if (state == ID) {
debug("ID\n");
if (!isspace(c)) {
parse_buffer[i++] = c;
} else {
parse_buffer[i++] = '\0';
id = strdup(parse_buffer);
state = WHITESPACE;
i = 0;
}
} else if (state == WHITESPACE) {
debug("WHITESPACE\n");
/* Skip all whitespace between car and cdr */
if (!isspace(c)) {
state = CDR;
continue;
}
} else if (state == CDR) {
debug("CDR sub %d\n", atom_length);
if (c == '{') {
complex_cdr = 1;
/* Convert dangling strings into text expressions */
if (i > 0) {
parse_buffer[i++] = '\0';
i = 0;
cdr = strdup(parse_buffer);
/* All-whitespace cdr */
int w;
atom_length = strlen(cdr);
for (w = 0; w < atom_length; w++) {
if (!isspace(cdr[w])) {
whitespace_cdr = 0;
break;
}
}
/* Avoid creating text expression if cdr is all-whitespace */
if (!whitespace_cdr) {
struct sexp promoted_car = {
.car = "\'",
.cdr_atom = cdr,
.cdr_subnodes = 0,
.cdr_atom_length = atom_length,
};
exp_list[exp_index++] = promoted_car;
whitespace_cdr = 1;
}
}
/* Recursive parsing */
struct sexp cons = parse_expression(content);
exp_list[exp_index++] = cons;
} else if (c == '}') {
parse_buffer[i++] = '\0';
cdr = strdup(parse_buffer);
atom_length = strlen(cdr);
/* All-whitespace cdr */
int w;
for (w = 0; w < atom_length; w++) {
if (!isspace(cdr[w])) {
whitespace_cdr = 0;
break;
}
}
/* Same as before, promote dangling strings to text expressions */
if (complex_cdr && !whitespace_cdr) {
struct sexp promoted_car = {
.car = "\'",
.cdr_atom = cdr,
.cdr_subnodes = 0,
.cdr_atom_length = atom_length,
};
exp_list[exp_index++] = promoted_car;
}
closing_line = line; closing_col = col;
state = FINISH;
i = 0;
continue;
} else {
/* Normal cdr atom parsing */
parse_buffer[i++] = c;
}
} else if (state == FINISH) {
debug("FINISH\n");
atom_length = strlen(cdr);
struct sexp exp = {
.car = car,
.id = id,
.cdr_atom = cdr,
.cdr_atom_length = atom_length,
.cdr_subnodes = exp_index,
.opening_line = opening_line,
.opening_col = opening_col,
.closing_line = closing_line,
.closing_col = closing_col,
};
if (exp_index > 0) {
exp.cdr_list = malloc(sizeof(struct sexp) * exp_index);
exp.cdr_atom_length = 0;
int e;
for (e = 0; e < exp_index; e++) {
exp.cdr_list[e] = exp_list[e];
exp.cdr_atom_length += exp_list[e].cdr_atom_length;
}
} else {
exp.cdr_list = NULL;
exp.cdr_subnodes = 0;
}
car = NULL;
id = NULL;
cdr = NULL;
exp_index = 0;
return exp;
}
get_token();
}
/* State machine should be in FINISH state at this point */
if (state == START) {
return (struct sexp) {.car = "empty-expression"};
} else if (state == CDR) {
abort_parsing("Syntax error", "Missing closing brace in %s expression", car);
}
}