~emersion/mrsh

ref: fb1a6e2ce995f61ca8e41e8ac3f591bc50922592 mrsh/parser/parser.c -rw-r--r-- 5.1 KiB
fb1a6e2cemersion readme: add link to overall project status 3 years ago
                                                                                
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
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ast.h"
#include "buffer.h"
#include "parser.h"

// 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 *state = calloc(1, sizeof(struct mrsh_parser));
	state->pos.line = state->pos.column = 1;
	return state;
}

struct mrsh_parser *mrsh_parser_create(FILE *f) {
	struct mrsh_parser *state = parser_create();
	state->f = f;
	return state;
}

struct mrsh_parser *mrsh_parser_create_from_buffer(const char *buf, size_t len) {
	struct mrsh_parser *state = parser_create();
	buffer_append(&state->buf, buf, len);
	buffer_append_char(&state->buf, '\0');
	return state;
}

void mrsh_parser_destroy(struct mrsh_parser *state) {
	if (state == NULL) {
		return;
	}
	buffer_finish(&state->buf);
	free(state);
}

size_t parser_peek(struct mrsh_parser *state, char *buf, size_t size) {
	if (state->f != NULL && size > state->buf.len) {
		size_t n_more = size - state->buf.len;
		char *dst = buffer_reserve(&state->buf, n_more);
		size_t n_read = fread(dst, 1, n_more, state->f);
		state->buf.len += n_read;
		if (n_read < n_more) {
			if (feof(state->f)) {
				buffer_append_char(&state->buf, '\0');
				size = state->buf.len;
			} else {
				// TODO: better error handling
				return 0;
			}
		}
	}
	if (state->f == NULL && size > state->buf.len) {
		size = state->buf.len;
	}

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

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

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

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

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 *state, const char *msg) {
	state->here_documents.len = 0;

	fprintf(stderr, "mrsh:%d:%d: syntax error: %s\n",
		state->pos.line, state->pos.column, msg);
	exit(EXIT_FAILURE);
}

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

	char c = parser_peek_char(state);

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

	if (is_operator_start(c)) {
		char next[operators_max_str_len];
		for (size_t i = 0; i < operators_len; ++i) {
			const char *str = operators[i].str;
			size_t n = strlen(str);
			size_t n_read = parser_peek(state, next, n);
			if (n_read == n && strncmp(next, str, n) == 0) {
				state->sym = operators[i].name;
				return;
			}
		}
	}

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

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

	state->sym = TOKEN;
}

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

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

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

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

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

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

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

	linebreak(state);
	return true;
}

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

void mrsh_parser_set_alias(struct mrsh_parser *state,
		mrsh_parser_alias_func_t alias, void *user_data) {
	state->alias = alias;
	state->alias_user_data = user_data;
}