summaryrefslogtreecommitdiff
path: root/lex.c
blob: 840c20f803170b8f07885edded1220abed8c0abb (plain)
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "token.h"

#define emit_error(...) fprintf(stderr, "%s at (%zd,%zd): ", filename, line_number, 1 + column_number);\
                        fprintf(stderr, __VA_ARGS__)

#define BUFFER_SIZE 1024

struct keyword {
	char *s; /* human-readable name of the token, as written by a user */
	enum TOKEN_TYPE t;
};


/** Static defs **************************************************************/

static struct keyword keywords[] = {
	{.s = "module", .t = TOK_MODULE },
	{.s = "input" , .t = TOK_INPUT  },
	{.s = "expr"  , .t = TOK_EXPR   },
	{.s = "or"    , .t = TOK_OR     },
	{.s = "and"   , .t = TOK_AND    },
	{.s = "xor"   , .t = TOK_XOR    },
	{.s = "not"   , .t = TOK_NOT    },
	{.s = "nand"  , .t = TOK_NAND   },
	{.s = "nor"   , .t = TOK_NOR    },
	{.s = NULL                      }
};

static struct keyword token_descriptors[] = {
	{.s = "module declaration"    , .t = TOK_MODULE},
	{.s = "input declaration"     , .t = TOK_INPUT },
	{.s = "expression start"      , .t = TOK_EXPR  },
	{.s = "colon"                 , .t = TOK_COLON },
	{.s = "end of line"           , .t = TOK_EOL   },
	{.s = "binary OR expression"  , .t = TOK_OR    },
	{.s = "binary AND expression" , .t = TOK_AND   },
	{.s = "binary XOR expression" , .t = TOK_XOR   },
	{.s = "binary NAND expression", .t = TOK_NAND  },
	{.s = "binary NOR expression" , .t = TOK_NOR   },
	{.s = "unary NOT expression"  , .t = TOK_NOT   },
	{.s = "identifier"            , .t = TOK_IDENT },
	{.s = NULL                                     }
};

static char buf[BUFFER_SIZE];
static FILE* fd;
static const char *filename = NULL;
static size_t line_number = 0;
static size_t column_number = 0;
static ssize_t leading_whitespace_len = 0;
static struct token *tok_start = NULL;
static struct token *tok_cursor = NULL;


/** Helpers ******************************************************************/

static struct location
get_current_loc(void) {
	struct location l;
	l.line = line_number;
	l.column = column_number + 1;
	l.leading_whitespace_len = leading_whitespace_len == -1 ? 0 : leading_whitespace_len;
	return l;
}

static int
expect(const char c) {
	if (buf[column_number] != c) {
		emit_error("Expected '%c', got '%c'\n", c, buf[column_number]);
		return 1;
	}
	column_number++;
	return 0;
}

static void
eat_whitespace(void) {
	while (column_number < BUFFER_SIZE && (
	           buf[column_number] == ' ' ||
	           buf[column_number] == '\t')) {
		column_number++;
	}
}

static void
add_token(struct token t) {
	struct token *last = tok_cursor;

	if (leading_whitespace_len == -1) {
		leading_whitespace_len = 0;
	}

	tok_cursor = malloc(sizeof(*tok_cursor));
	if (tok_cursor == NULL) {
		emit_error("Internal error: malloc failed:");
		perror("malloc");
		/* FIXME return falsey and propagate error up */
		return;
	}

	*tok_cursor = t;

	/* tok_start is NULL on first token only */
	if (tok_start == NULL) {
		tok_start = tok_cursor;
	} else {
		last->next = tok_cursor;
	}
}

const char *
get_token_description(enum TOKEN_TYPE t) {
	size_t i = 0;

	for (i = 0; i < sizeof(token_descriptors) / sizeof(token_descriptors[0]) && token_descriptors[i].s; i++) {
		if (t == token_descriptors[i].t) {
			return token_descriptors[i].s;
		}
	}

	return "(internal error: unknown token)";
}

/** Beans ********************************************************************/

static struct token
lex_alphanum(void) {
	struct token t;
	size_t i = 0;

	t.loc = get_current_loc();

	i = 0;
	while (i < MAX_IDENT_LENGTH - 1 && isalnum(buf[column_number + i])) {
		t.value[i] = buf[column_number + i];
		i++;
	}
	t.value[i] = '\0';

	column_number += i;
	t.span = i;

	if (i == 0) {
		emit_error("Expected alphanumeric, got '%c'\n", buf[i]);
	}

	/* default to identifier, see below for keyword */
	t.type = TOK_IDENT;

	/* figure out if it's a keyword or not */
	for (i = 0; i < sizeof(keywords) / sizeof(struct keyword) && keywords[i].s; i++) {
		if (strcmp(t.value, keywords[i].s) == 0) {
			t.type = keywords[i].t;
			break;
		}
	}

	return t;
}

static struct token
lex_eol(char eolchar) {
	struct token t;

	t.type = TOK_EOL;
	t.loc = get_current_loc();
	t.span = 1;

	expect(eolchar);

	return t;
}

static struct token
lex_colon(void) {
	struct token t;

	t.type = TOK_COLON;
	t.loc = get_current_loc();
	t.span = 1;

	expect(':');

	return t;
}

static int
lex_line(void) {
	size_t length = strlen(buf);
	leading_whitespace_len = -1;

	while (column_number < length && column_number < strlen(buf)) {
		switch (buf[column_number]) {
			case ':':
				add_token(lex_colon());
				break;
			case ' ':
			case '\t':
				eat_whitespace();
				if (leading_whitespace_len == -1) {
					leading_whitespace_len = column_number;
				}
				break;
			case '\n':
				add_token(lex_eol(buf[column_number]));
				break;
			case '#':
				add_token(lex_eol(buf[column_number]));
				return 0;
				break;
			default:
				/* perform more broad checks */
				if (isalnum(buf[column_number])) {
					add_token(lex_alphanum());
				} else {
					/* nope, still no dice */
					emit_error("Unexpected '%c'\n", buf[column_number]);
					return 1;
				}
				break;
		}
	}
	return 0;
}

struct token *
lex_file(const char *filename_local, FILE *fd_local) {
	struct token start;

	filename = filename_local;
	fd = fd_local;

	line_number = 1;
	tok_cursor = tok_start = NULL;

	start.type = TOK_BEGIN;
	add_token(start);

	while (NULL != fgets(buf, sizeof(buf), fd)) {
		column_number = 0;
		if (lex_line()) {
			return NULL;
		}
		line_number++;
	}

	/* Terminate linked list */
	if (tok_cursor) {
		tok_cursor->next = NULL;
	}

	return tok_start;
}