mrsh/parser/parser.c -rw-r--r-- 7.4 KiB View raw
                                                                                
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
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <mrsh/buffer.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ast.h"
#include "parser.h"

#define READ_SIZE 4096

// Keep sorted from the longest to the shortest
const struct symbol operators[] = {
	{ DLESSDASH, "<<-" },
	{ AND_IF, "&&" },
	{ OR_IF, "||" },
	{ DSEMI, ";;" },
	{ DLESS, "<<" },
	{ DGREAT, ">>" },
	{ LESSAND, "<&" },
	{ GREATAND, ">&" },
	{ LESSGREAT, "<>" },
	{ CLOBBER, ">|" },
};

const size_t operators_len = sizeof(operators) / sizeof(operators[0]);
const size_t operators_max_str_len = 3;

const char *keywords[] = {
	"if",
	"then",
	"else",
	"elif",
	"fi",
	"do",
	"done",

	"case",
	"esac",
	"while",
	"until",
	"for",

	"{",
	"}",
	"!",
	"in",
};

const size_t keywords_len = sizeof(keywords) / sizeof(keywords[0]);

static struct mrsh_parser *parser_create(void) {
	struct mrsh_parser *parser = calloc(1, sizeof(struct mrsh_parser));
	parser->fd = -1;
	parser->pos.line = parser->pos.column = 1;
	return parser;
}

struct mrsh_parser *mrsh_parser_with_fd(int fd) {
	struct mrsh_parser *parser = parser_create();
	parser->fd = fd;
	return parser;
}

struct mrsh_parser *mrsh_parser_with_data(const char *buf, size_t len) {
	struct mrsh_parser *parser = parser_create();
	mrsh_buffer_append(&parser->buf, buf, len);
	mrsh_buffer_append_char(&parser->buf, '\0');
	return parser;
}

struct mrsh_parser *mrsh_parser_with_buffer(struct mrsh_buffer *buf) {
	struct mrsh_parser *parser = parser_create();
	parser->in_buf = buf;
	return parser;
}

void mrsh_parser_destroy(struct mrsh_parser *parser) {
	if (parser == NULL) {
		return;
	}
	mrsh_buffer_finish(&parser->buf);
	free(parser->error.msg);
	free(parser);
}

static ssize_t parser_peek_fd(struct mrsh_parser *parser, size_t size) {
	assert(parser->fd >= 0);

	size_t n_read = 0;
	while (n_read < size) {
		char *dst = mrsh_buffer_reserve(&parser->buf, READ_SIZE);

		errno = 0;
		ssize_t n = read(parser->fd, dst, READ_SIZE);
		if (n < 0 && errno == EINTR) {
			continue;
		} else if (n < 0) {
			return -1;
		} else if (n == 0) {
			break;
		}

		parser->buf.len += n;
		n_read += n;
	}

	return n_read;
}

static ssize_t parser_peek_buffer(struct mrsh_parser *parser, size_t size) {
	assert(parser->in_buf != NULL);

	size_t n_read = parser->in_buf->len;
	if (n_read == 0) {
		return 0;
	}

	if (parser->buf.len == 0) {
		// Move data from one buffer to the other
		mrsh_buffer_finish(&parser->buf);
		memcpy(&parser->buf, parser->in_buf, sizeof(struct mrsh_buffer));
		memset(parser->in_buf, 0, sizeof(struct mrsh_buffer));
	} else {
		mrsh_buffer_append(&parser->buf, parser->in_buf->data, n_read);
		parser->in_buf->len = 0;
	}

	return n_read;
}

size_t parser_peek(struct mrsh_parser *parser, char *buf, size_t size) {
	if (size > parser->buf.len) {
		size_t n_more = size - parser->buf.len;

		ssize_t n_read;
		if (parser->fd >= 0) {
			n_read = parser_peek_fd(parser, n_more);
		} else if (parser->in_buf != NULL) {
			n_read = parser_peek_buffer(parser, n_more);
		} else {
			n_read = 0;
		}

		if (n_read < 0) {
			parser_set_error(parser, "failed to read");
			return 0; // TODO: better error handling
		}

		if ((size_t)n_read < n_more) {
			if (!parser->eof) {
				mrsh_buffer_append_char(&parser->buf, '\0');
				parser->eof = true;
			}
			size = parser->buf.len;
		}
	}

	if (buf != NULL) {
		memcpy(buf, parser->buf.data, size);
	}
	return size;
}

char parser_peek_char(struct mrsh_parser *parser) {
	char c = '\0';
	parser_peek(parser, &c, sizeof(char));
	return c;
}

size_t parser_read(struct mrsh_parser *parser, char *buf, size_t size) {
	size_t n = parser_peek(parser, buf, size);
	if (n > 0) {
		for (size_t i = 0; i < n; ++i) {
			assert(parser->buf.data[i] != '\0');
			++parser->pos.offset;
			if (parser->buf.data[i] == '\n') {
				++parser->pos.line;
				parser->pos.column = 1;
			} else {
				++parser->pos.column;
			}
		}
		memmove(parser->buf.data, parser->buf.data + n, parser->buf.len - n);
		parser->buf.len -= n;

		parser->continuation_line = false;
	}
	return n;
}

char parser_read_char(struct mrsh_parser *parser) {
	char c = '\0';
	parser_read(parser, &c, sizeof(char));
	return c;
}

void read_continuation_line(struct mrsh_parser *parser) {
	char c = parser_read_char(parser);
	assert(c == '\n');
	parser->continuation_line = true;
}

bool is_operator_start(char c) {
	switch (c) {
	case '&':
	case '|':
	case ';':
	case '<':
	case '>':
		return true;
	default:
		return false;
	}
}

void parser_set_error(struct mrsh_parser *parser, const char *msg) {
	if (msg != NULL) {
		if (parser->error.msg != NULL) {
			return;
		}

		parser->here_documents.len = 0;
		parser->error.pos = parser->pos;
		parser->error.msg = strdup(msg);
	} else {
		free(parser->error.msg);

		parser->error.pos = (struct mrsh_position){0};
		parser->error.msg = NULL;
	}
}

const char *mrsh_parser_error(struct mrsh_parser *parser,
		struct mrsh_position *pos) {
	if (pos != NULL) {
		*pos = parser->error.pos;
	}
	return parser->error.msg;
}

void parser_begin(struct mrsh_parser *parser) {
	parser_set_error(parser, NULL);
	parser->continuation_line = false;
}

// See section 2.3 Token Recognition
static void next_symbol(struct mrsh_parser *parser) {
	parser->has_sym = true;

	char c = parser_peek_char(parser);

	if (c == '\0') {
		parser->sym = EOF_TOKEN;
		return;
	}
	if (c == '\n') {
		parser->sym = NEWLINE;
		return;
	}

	if (is_operator_start(c)) {
		for (size_t i = 0; i < operators_len; ++i) {
			const char *str = operators[i].str;

			size_t j;
			for (j = 0; str[j] != '\0'; ++j) {
				size_t n = j + 1;
				size_t n_read = parser_peek(parser, NULL, n);
				if (n != n_read || parser->buf.data[j] != str[j]) {
					break;
				}
			}

			if (str[j] == '\0') {
				parser->sym = operators[i].name;
				return;
			}
		}
	}

	if (isblank(c)) {
		parser_read_char(parser);
		next_symbol(parser);
		return;
	}

	if (c == '#') {
		while (true) {
			char c = parser_peek_char(parser);
			if (c == '\0' || c == '\n') {
				break;
			}
			parser_read_char(parser);
		}
		next_symbol(parser);
		return;
	}

	parser->sym = TOKEN;
}

enum symbol_name get_symbol(struct mrsh_parser *parser) {
	if (!parser->has_sym) {
		next_symbol(parser);
	}
	return parser->sym;
}

void consume_symbol(struct mrsh_parser *parser) {
	parser->has_sym = false;
}

bool symbol(struct mrsh_parser *parser, enum symbol_name sym) {
	return get_symbol(parser) == sym;
}

bool eof(struct mrsh_parser *parser) {
	return symbol(parser, EOF_TOKEN);
}

bool newline(struct mrsh_parser *parser) {
	if (!symbol(parser, NEWLINE)) {
		return false;
	}
	char c = parser_read_char(parser);
	assert(c == '\n');
	consume_symbol(parser);
	return true;
}

void linebreak(struct mrsh_parser *parser) {
	while (newline(parser)) {
		// This space is intentionally left blank
	}
}

bool newline_list(struct mrsh_parser *parser) {
	if (!newline(parser)) {
		return false;
	}

	linebreak(parser);
	return true;
}

bool mrsh_parser_eof(struct mrsh_parser *parser) {
	return parser->has_sym && parser->sym == EOF_TOKEN;
}

void mrsh_parser_set_alias_func(struct mrsh_parser *parser,
		mrsh_parser_alias_func alias, void *user_data) {
	parser->alias = alias;
	parser->alias_user_data = user_data;
}

bool mrsh_parser_continuation_line(struct mrsh_parser *parser) {
	return parser->continuation_line;
}

void mrsh_parser_reset(struct mrsh_parser *parser) {
	parser->buf.len = 0;
	parser->has_sym = false;
	parser->pos = (struct mrsh_position){0};
}