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
|
/// src/rs274ngc_lexer.c --- RS274NGC lexer
//
// Copyright (C) 2026 by Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
/// Commentary:
//
/// Code:
#include "rs274ngc_lexer.h"
#include <regex.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#define X(SYM, REG, ...) \
[RS274NGC_LEXEME_KIND_##SYM - RS274NGC_LEXEME_REGEX_OFFSET] = "^(" REG ")",
static const char *lexeme_regex_strings[RS274NGC_LEXEME_REGEX_COUNT] = {
RS274NGC_LEXEME_KINDS ()
};
#undef X
static regex_t lexeme_regexes[RS274NGC_LEXEME_REGEX_COUNT];
static void
print_regex_error (const regex_t *comp, const char *regex, int errcode)
{
size_t length = regerror (errcode, comp, NULL, 0);
char *buffer = malloc (length);
regerror (errcode, comp, buffer, length);
fprintf (stderr, "error: regcomp (\"%s\"): %s\n", regex, buffer);
free (buffer);
}
void
rs274ngc_lexer_compile_regexes (void)
{
bool errored = false;
for (int i = 0; i < RS274NGC_LEXEME_REGEX_COUNT; ++i) {
regex_t *compiled = &lexeme_regexes[i];
const char *pattern = lexeme_regex_strings[i];
int errcode = regcomp (compiled, pattern, REG_EXTENDED);
if (errcode) {
print_regex_error (compiled, pattern, errcode);
errored = true;
}
}
if (errored)
exit (EXIT_FAILURE);
}
struct rs274ngc_lexer
rs274ngc_lexer_from_sv (struct sv sv, const char *filename)
{
struct rs274ngc_lexer lexer = {
.sv = sv,
.cursor = sv.beg,
.filename = filename,
};
rs274ngc_lexer_next_lexeme (&lexer);
return lexer;
}
static void
skip_whitespace (struct rs274ngc_lexer *lexer)
{
while (lexer->cursor < lexer->sv.end && (*lexer->cursor == ' ' ||
*lexer->cursor == '\t'))
lexer->cursor += 1;
}
void
rs274ngc_lexer_next_lexeme (struct rs274ngc_lexer *lexer)
{
skip_whitespace (lexer);
lexer->lexeme = (struct rs274ngc_lexeme) { .sv.beg = lexer->cursor };
if (lexer->cursor >= lexer->sv.end) {
lexer->lexeme.kind = RS274NGC_LEXEME_KIND_END_OF_FILE;
lexer->lexeme.sv.end = lexer->sv.end;
return;
}
if (*lexer->cursor == '\r') {
lexer->cursor += 1;
if (*lexer->cursor == '\n')
lexer->cursor += 1;
lexer->lexeme.kind = RS274NGC_LEXEME_KIND_END_OF_LINE;
lexer->lexeme.sv.end = lexer->cursor;
return;
}
if (*lexer->cursor == '\n') {
lexer->cursor += 1;
lexer->lexeme.kind = RS274NGC_LEXEME_KIND_END_OF_LINE;
lexer->lexeme.sv.end = lexer->cursor;
return;
}
regmatch_t regmatch;
int longest_matching_regex = RS274NGC_LEXEME_REGEX_COUNT;
int longest_matching_regex_length = 0;
for (int i = 0; i < RS274NGC_LEXEME_REGEX_COUNT; ++i) {
if (regexec (&lexeme_regexes[i], lexer->cursor, 1, ®match, 0)) continue;
if (regmatch.rm_eo <= longest_matching_regex_length) continue;
longest_matching_regex_length = regmatch.rm_eo;
longest_matching_regex = i;
#ifdef RS274NGC_LEXER_FIRST_MATCH
break;
#endif
}
if (longest_matching_regex != RS274NGC_LEXEME_REGEX_COUNT) {
lexer->lexeme.kind = longest_matching_regex + RS274NGC_LEXEME_REGEX_OFFSET;
lexer->lexeme.sv.end = lexer->cursor += longest_matching_regex_length;
#ifdef RS274NGC_LEXER_TRIM
while (lexer->lexeme.sv.end > lexer->lexeme.sv.beg &&
(lexer->lexeme.sv.end[-1] == ' ' ||
lexer->lexeme.sv.end[-1] == '\t')) {
lexer->lexeme.sv.end -= 1;
}
#endif
switch (lexer->lexeme.kind) {
#ifdef RS274NGC_LEXER_FILTER_COMMENTS
case RS274NGC_LEXEME_KIND_COMMENT:
return rs274ngc_lexer_next_lexeme (lexer);
#endif
default: return;
}
}
// INVALID
lexer->lexeme.sv.end = ++lexer->cursor;
}
/// src/rs274ngc_lexer.c ends here
|