aboutsummaryrefslogtreecommitdiff
path: root/src/bnf-lexer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bnf-lexer.c')
-rw-r--r--src/bnf-lexer.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/bnf-lexer.c b/src/bnf-lexer.c
new file mode 100644
index 0000000..f859af9
--- /dev/null
+++ b/src/bnf-lexer.c
@@ -0,0 +1,101 @@
1#include "bnf-lexer.h"
2#include <regex.h>
3#include <assert.h>
4#include <stdlib.h>
5#include <stdio.h>
6#include <ctype.h>
7#include "dynamic-array.h"
8
9#define X(SYM, REG, ...) [BNF_LEXEME_KIND_##SYM] = "^(" REG ")",
10static const char *lexeme_regex_strings[BNF_LEXEME_REGEX_COUNT] = { BNF_LEXEME_KINDS () };
11#undef X
12
13static regex_t lexeme_regexes[BNF_LEXEME_REGEX_COUNT];
14
15static void
16print_regex_error (const regex_t *comp, const char *regex, int errcode)
17{
18 size_t length = regerror (errcode, comp, NULL, 0);
19 char *buffer = malloc (length);
20 regerror (errcode, comp, buffer, length);
21 fprintf (stderr, "error: regcomp (\"%s\"): %s\n",
22 regex, buffer);
23 free (buffer);
24}
25
26static void __attribute__((constructor))
27compile_regexes (void)
28{
29 for (enum bnf_lexeme_kind kind = 0; kind < BNF_LEXEME_REGEX_COUNT; ++kind) {
30 regex_t *compiled = &lexeme_regexes[kind];
31 const char *pattern = lexeme_regex_strings[kind];
32 int errcode = regcomp (compiled, pattern, REG_EXTENDED);
33 if (errcode) {
34 print_regex_error (compiled, pattern, errcode);
35 exit (1);
36 }
37 }
38}
39
40struct bnf_lexer
41bnf_lexer_from_string_view (struct string_view sv, const char *filename)
42{
43 return (struct bnf_lexer) {
44 .sv = sv,
45 .cursor = sv.beg,
46 .filename = filename,
47 };
48}
49
50static void
51lexer_skip_whitespace (struct bnf_lexer *lexer)
52{
53 while (lexer->cursor < lexer->sv.end && (*lexer->cursor == ' ' ||
54 *lexer->cursor == '\t'))
55 lexer->cursor += 1;
56}
57
58struct bnf_lexeme
59bnf_lexer_next_lexeme (struct bnf_lexer *lexer)
60{
61 lexer_skip_whitespace (lexer);
62 struct bnf_lexeme lexeme = {
63 .sv.beg = lexer->cursor,
64 .kind = BNF_LEXEME_KIND_INVALID,
65 };
66
67 if (lexer->cursor >= lexer->sv.end) {
68 lexeme.kind = BNF_LEXEME_KIND_END_OF_FILE;
69 lexeme.sv.end = lexer->sv.end;
70 return lexeme;
71 }
72
73 if (*lexer->cursor == '\n') {
74 lexeme.kind = BNF_LEXEME_KIND_END_OF_LINE;
75 lexeme.sv.end = ++lexer->cursor;
76 return lexeme;
77 }
78
79 regmatch_t regmatch;
80 enum bnf_lexeme_kind longest_matching_lexeme_kind = BNF_LEXEME_KIND_INVALID;
81 int longest_matching_lexeme_kind_length = 0;
82 for (enum bnf_lexeme_kind kind = 0; kind < BNF_LEXEME_REGEX_COUNT; ++kind) {
83 if (regexec (&lexeme_regexes[kind], lexer->cursor, 1, &regmatch, 0)) continue;
84 if (regmatch.rm_eo <= longest_matching_lexeme_kind_length) continue;
85 longest_matching_lexeme_kind_length = regmatch.rm_eo;
86 longest_matching_lexeme_kind = kind;
87 }
88
89 if (longest_matching_lexeme_kind != BNF_LEXEME_KIND_INVALID) {
90 lexeme.kind = longest_matching_lexeme_kind;
91 lexeme.sv.end = lexer->cursor += longest_matching_lexeme_kind_length;
92
93 switch (lexeme.kind) {
94 case BNF_LEXEME_KIND_COMMENT: return bnf_lexer_next_lexeme (lexer);
95 default: return lexeme;
96 }
97 }
98
99 lexeme.sv.end = ++lexer->cursor;
100 return lexeme;
101}