aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.c
blob: 7938b2800eb45b5d8da7e59d8460c39e38d230b0 (plain) (blame)
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
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "alloc.h"
#include "common.h"
#include "errors.h"
#include "lexer.h"
#include "strview.h"
#include "unicode.h"

#if DEBUG
#	define LEXEMES_DFLT_CAP (8)
#else
#	define LEXEMES_DFLT_CAP (2048)
#endif
#define SIZE_WDTH (sizeof(size_t) * CHAR_BIT)

static lexemes_t mklexemes(void);

/* Resize TOKS to the next power-of-2 capacity */
static void lexemesresz(lexemes_t *toks)
	__attribute__((nonnull));

/* Advance PTR (which points to the start of a comment) to the end of the
   comment, or END.  Returns true if the comment was well-formed and
   false if the comment was unterminated.  Handles nested comments. */
bool skpcmnt(const uchar **ptr, const uchar *end)
	__attribute__((nonnull));

static const bool is_numeric_lookup[UCHAR_MAX + 1] = {
	['0'] = true, ['1'] = true, ['2'] = true,  ['3'] = true,
	['4'] = true, ['5'] = true, ['6'] = true,  ['7'] = true,
	['8'] = true, ['9'] = true, ['\''] = true, ['.'] = true,
};

lexemes_t
lexstring(const uchar *code, size_t codesz)
{
#if ORYX_SIMD
	if (!utf8_validate_simd(code, codesz)) {
#endif
		size_t loc = utf8_validate_off(code, codesz);
		if (loc != 0) {
			err("Invalid byte ‘0x%02" PRIx8 "’ in UTF-8 input at byte %zu",
			    code[loc - 1], loc);
		}
#if ORYX_SIMD
	}
#endif

	lexemes_t data = mklexemes();

	const uchar *start = code, *end = start + codesz;
	while (likely(code < end)) {
		const uchar *spnbeg = code, *spnend;
		rune ch = utf8_decode(&code);

		switch (ch) {
		/* Whitespace */
		case '\t': case '\n': case '\v':
		case '\f': case '\r': case ' ':
		case RUNE_C(0x0085): /* NEXT LINE */
		case RUNE_C(0x200E): /* LEFT-TO-RIGHT MARK */
		case RUNE_C(0x200F): /* RIGHT-TO-LEFT MARK */
		case RUNE_C(0x2028): /* LINE SEPARATOR */
		case RUNE_C(0x2029): /* PARAGRAPH SEPARATOR */
			break;

		/* Single-byte literals */
		case '&': case '(': case ')': case '*': case '+':
		case '-': case ':': case ';': case '=': case '[':
		case ']': case '{': case '|': case '}': case '~':
			data.kinds[data.len++] = ch;
			break;

		case RUNE_C(0x2026): /* HORIZONTAL ELLIPSIS */
			data.kinds[data.len++] = LEXELIP;
			break;

		/* Single- or double-byte literals */
		case '/':
			if (code < end && code[0] == '*') {
				if (!skpcmnt(&code, end))
					err("Unterminated comment at byte %td", code - start);
				continue;
			}

			data.kinds[data.len++] = ch;
			break;
		case '<': case '>':
			data.kinds[data.len++] = ch;

			/* See the comment in lexer.h for where 193 comes from */
			if (code < end && code[0] == ch) {
				code++;
				data.kinds[data.len - 1] += 193;
			}
			break;

		case '.':
			if (unlikely(end - code < 2) || code[0] != '.' || code[1] != '.') {
				if (likely(end - code) >= 1 && isdigit(code[0]))
					goto number;
				goto fallback;
			}
			code += 2;
			data.kinds[data.len++] = LEXELIP;
			break;

		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
number:
		{
			bool saw_dot = false;
			data.kinds[data.len] = LEXNUM;
			data.strs[data.len].p = spnbeg;

			while (likely(code < end) && is_numeric_lookup[code[0]]) {
				if (unlikely(code[0] == '.')) {
					if (saw_dot)
						err("lexer: Decimal separator given multiple times in numeric literal");
					saw_dot = true;
				}
				if (unlikely(code[0] == '\'' && code[-1] == '\'')) {
					err("Adjacent numeric separators at byte %td",
					    code - start);
				}
				code++;
			}
			if (unlikely(code < end && code[-1] == '\'')) {
				err("Numeric literal ends with numeric separator at byte %td",
				    code - start);
			}

			data.strs[data.len++].len = code - spnbeg;
			break;
		}

		default:
fallback:
			if (unlikely(!rune_is_xids(ch)))
				err("lexer: Unexpected rune U+%04" PRIXRUNE, ch);

			data.kinds[data.len] = LEXIDENT;
			data.strs[data.len].p = spnbeg;

			spnend = code;
			while (likely(code < end) && rune_is_xidc(ch)) {
				spnend = code;
				ch = utf8_decode(&code);
			}
			if (likely(code < end))
				code = spnend;

			data.strs[data.len++].len = spnend - spnbeg;
		}

		if (unlikely(data.len == data.cap))
			lexemesresz(&data);
	}

	if (unlikely(data.len == data.cap))
		lexemesresz(&data);
	data.kinds[data.len++] = LEXEOF;
	return data;
}

lexemes_t
mklexemes(void)
{
	lexemes_t soa;

	static_assert(offsetof(lexemes_t, kinds) < offsetof(lexemes_t, strs),
	              "KINDS is not the first field before STRS");
	static_assert(LEXEMES_DFLT_CAP * sizeof(*soa.kinds) % alignof(strview_t)
	                  == 0,
	              "Additional padding is required to properly align STRS");

	soa.len = 0;
	soa.cap = LEXEMES_DFLT_CAP;
	soa.kinds = bufalloc(NULL, soa.cap, LEXEMES_BLKSZ);
	soa.strs = (void *)((char *)soa.kinds + soa.cap * sizeof(*soa.kinds));

	return soa;
}

void
lexemesresz(lexemes_t *soa)
{
	static_assert(offsetof(lexemes_t, kinds) < offsetof(lexemes_t, strs),
	              "KINDS is not the first field before STRS");

	size_t ncap, pad, newsz;
	ptrdiff_t off = (char *)soa->strs - (char *)soa->kinds;

	/* The capacity is always going to be a power of 2, so checking for
	   overflow becomes pretty trivial */
	if (unlikely((soa->cap >> (SIZE_WDTH - 1)) != 0)) {
		errno = ENOMEM;
		err("%s:", __func__);
	}
	ncap = soa->cap << 1;

	/* Ensure that soa->strs is properly aligned */
	pad = alignof(strview_t)
	    - ncap * sizeof(*soa->kinds) % alignof(strview_t);
	if (pad == alignof(strview_t))
		pad = 0;

	newsz = ncap * LEXEMES_BLKSZ + pad;

	soa->kinds = bufalloc(soa->kinds, newsz, 1);
	soa->strs = (void *)((char *)soa->kinds + ncap * sizeof(*soa->kinds) + pad);
	memmove(soa->strs, (char *)soa->kinds + off, soa->len * sizeof(*soa->strs));
	soa->cap = ncap;
}