diff options
| author | 2026-11-11 22:28:13 +0000 | |
|---|---|---|
| committer | 2026-11-11 22:47:34 +0000 | |
| commit | cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch) | |
| tree | c7d90c0abe9533c87d51b5196871846855efa1ba /src/rs274ngc_lexer.c | |
| download | thesis-cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f.tar.gz | |
Diffstat (limited to 'src/rs274ngc_lexer.c')
| -rw-r--r-- | src/rs274ngc_lexer.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/src/rs274ngc_lexer.c b/src/rs274ngc_lexer.c new file mode 100644 index 0000000..cbc7ade --- /dev/null +++ b/src/rs274ngc_lexer.c | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | /// src/rs274ngc_lexer.c --- RS274NGC lexer | ||
| 2 | // | ||
| 3 | // Copyright (C) 2026 by Martin Michalec <martin@michalec.dev> | ||
| 4 | // | ||
| 5 | // SPDX-License-Identifier: MIT | ||
| 6 | // | ||
| 7 | /// Commentary: | ||
| 8 | // | ||
| 9 | /// Code: | ||
| 10 | |||
| 11 | #include "rs274ngc_lexer.h" | ||
| 12 | #include <regex.h> | ||
| 13 | #include <assert.h> | ||
| 14 | #include <stdlib.h> | ||
| 15 | #include <stdio.h> | ||
| 16 | #include <stdbool.h> | ||
| 17 | #include <ctype.h> | ||
| 18 | |||
| 19 | #define X(SYM, REG, ...) \ | ||
| 20 | [RS274NGC_LEXEME_KIND_##SYM - RS274NGC_LEXEME_REGEX_OFFSET] = "^(" REG ")", | ||
| 21 | static const char *lexeme_regex_strings[RS274NGC_LEXEME_REGEX_COUNT] = { | ||
| 22 | RS274NGC_LEXEME_KINDS () | ||
| 23 | }; | ||
| 24 | #undef X | ||
| 25 | |||
| 26 | static regex_t lexeme_regexes[RS274NGC_LEXEME_REGEX_COUNT]; | ||
| 27 | |||
| 28 | static void | ||
| 29 | print_regex_error (const regex_t *comp, const char *regex, int errcode) | ||
| 30 | { | ||
| 31 | size_t length = regerror (errcode, comp, NULL, 0); | ||
| 32 | char *buffer = malloc (length); | ||
| 33 | regerror (errcode, comp, buffer, length); | ||
| 34 | fprintf (stderr, "error: regcomp (\"%s\"): %s\n", regex, buffer); | ||
| 35 | free (buffer); | ||
| 36 | } | ||
| 37 | |||
| 38 | void | ||
| 39 | rs274ngc_lexer_compile_regexes (void) | ||
| 40 | { | ||
| 41 | bool errored = false; | ||
| 42 | |||
| 43 | for (int i = 0; i < RS274NGC_LEXEME_REGEX_COUNT; ++i) { | ||
| 44 | regex_t *compiled = &lexeme_regexes[i]; | ||
| 45 | const char *pattern = lexeme_regex_strings[i]; | ||
| 46 | int errcode = regcomp (compiled, pattern, REG_EXTENDED); | ||
| 47 | if (errcode) { | ||
| 48 | print_regex_error (compiled, pattern, errcode); | ||
| 49 | errored = true; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | if (errored) | ||
| 54 | exit (EXIT_FAILURE); | ||
| 55 | } | ||
| 56 | |||
| 57 | struct rs274ngc_lexer | ||
| 58 | rs274ngc_lexer_from_sv (struct sv sv, const char *filename) | ||
| 59 | { | ||
| 60 | struct rs274ngc_lexer lexer = { | ||
| 61 | .sv = sv, | ||
| 62 | .cursor = sv.beg, | ||
| 63 | .filename = filename, | ||
| 64 | }; | ||
| 65 | |||
| 66 | rs274ngc_lexer_next_lexeme (&lexer); | ||
| 67 | return lexer; | ||
| 68 | } | ||
| 69 | |||
| 70 | static void | ||
| 71 | skip_whitespace (struct rs274ngc_lexer *lexer) | ||
| 72 | { | ||
| 73 | while (lexer->cursor < lexer->sv.end && (*lexer->cursor == ' ' || | ||
| 74 | *lexer->cursor == '\t')) | ||
| 75 | lexer->cursor += 1; | ||
| 76 | } | ||
| 77 | |||
| 78 | void | ||
| 79 | rs274ngc_lexer_next_lexeme (struct rs274ngc_lexer *lexer) | ||
| 80 | { | ||
| 81 | skip_whitespace (lexer); | ||
| 82 | lexer->lexeme = (struct rs274ngc_lexeme) { .sv.beg = lexer->cursor }; | ||
| 83 | |||
| 84 | if (lexer->cursor >= lexer->sv.end) { | ||
| 85 | lexer->lexeme.kind = RS274NGC_LEXEME_KIND_END_OF_FILE; | ||
| 86 | lexer->lexeme.sv.end = lexer->sv.end; | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | if (*lexer->cursor == '\r') { | ||
| 91 | lexer->cursor += 1; | ||
| 92 | if (*lexer->cursor == '\n') | ||
| 93 | lexer->cursor += 1; | ||
| 94 | |||
| 95 | lexer->lexeme.kind = RS274NGC_LEXEME_KIND_END_OF_LINE; | ||
| 96 | lexer->lexeme.sv.end = lexer->cursor; | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | |||
| 100 | if (*lexer->cursor == '\n') { | ||
| 101 | lexer->cursor += 1; | ||
| 102 | lexer->lexeme.kind = RS274NGC_LEXEME_KIND_END_OF_LINE; | ||
| 103 | lexer->lexeme.sv.end = lexer->cursor; | ||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
| 107 | regmatch_t regmatch; | ||
| 108 | int longest_matching_regex = RS274NGC_LEXEME_REGEX_COUNT; | ||
| 109 | int longest_matching_regex_length = 0; | ||
| 110 | for (int i = 0; i < RS274NGC_LEXEME_REGEX_COUNT; ++i) { | ||
| 111 | if (regexec (&lexeme_regexes[i], lexer->cursor, 1, ®match, 0)) continue; | ||
| 112 | if (regmatch.rm_eo <= longest_matching_regex_length) continue; | ||
| 113 | |||
| 114 | longest_matching_regex_length = regmatch.rm_eo; | ||
| 115 | longest_matching_regex = i; | ||
| 116 | |||
| 117 | #ifdef RS274NGC_LEXER_FIRST_MATCH | ||
| 118 | break; | ||
| 119 | #endif | ||
| 120 | } | ||
| 121 | |||
| 122 | if (longest_matching_regex != RS274NGC_LEXEME_REGEX_COUNT) { | ||
| 123 | lexer->lexeme.kind = longest_matching_regex + RS274NGC_LEXEME_REGEX_OFFSET; | ||
| 124 | lexer->lexeme.sv.end = lexer->cursor += longest_matching_regex_length; | ||
| 125 | |||
| 126 | #ifdef RS274NGC_LEXER_TRIM | ||
| 127 | while (lexer->lexeme.sv.end > lexer->lexeme.sv.beg && | ||
| 128 | (lexer->lexeme.sv.end[-1] == ' ' || | ||
| 129 | lexer->lexeme.sv.end[-1] == '\t')) { | ||
| 130 | lexer->lexeme.sv.end -= 1; | ||
| 131 | } | ||
| 132 | #endif | ||
| 133 | |||
| 134 | switch (lexer->lexeme.kind) { | ||
| 135 | #ifdef RS274NGC_LEXER_FILTER_COMMENTS | ||
| 136 | case RS274NGC_LEXEME_KIND_COMMENT: | ||
| 137 | return rs274ngc_lexer_next_lexeme (lexer); | ||
| 138 | #endif | ||
| 139 | default: return; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | // INVALID | ||
| 144 | lexer->lexeme.sv.end = ++lexer->cursor; | ||
| 145 | } | ||
| 146 | |||
| 147 | /// src/rs274ngc_lexer.c ends here | ||
