diff options
| author | Ian Moffett <ian@mirocom.org> | 2026-05-23 02:21:09 -0400 |
|---|---|---|
| committer | Ian Moffett <ian@mirocom.org> | 2026-05-23 02:21:09 -0400 |
| commit | 74e2e8c772d0f88da6684918f782b37156f10fb3 (patch) | |
| tree | fa5d9431e7e3f8df98f1d7f8e5dc9f8c02d09002 /core/lexer.c | |
| parent | 659dd389326f73e6bcbce5311088c182d7cb580e (diff) | |
core: Add lexer + parser groundwork
Signed-off-by: Ian Moffett <ian@mirocom.org>
Diffstat (limited to 'core/lexer.c')
| -rw-r--r-- | core/lexer.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/core/lexer.c b/core/lexer.c new file mode 100644 index 0000000..d6e5e66 --- /dev/null +++ b/core/lexer.c @@ -0,0 +1,84 @@ +#include <errno.h> +#include <stdbool.h> +#include "cescal/lexer.h" +#include "cescal/log.h" + +/* + * Returns true if the given character is a whitespace + * + * @c: Character to check + */ +static inline bool +lexer_is_ws(char c) +{ + switch (c) { + case '\t': + case '\n': + case ' ': + case '\f': + case '\r': + return true; + } + + return false; +} + +/* + * Consume a single character from the input source file and + * optionally skip whitespace + * + * @state: Compiler state + * @skip_ws: If true skip whitespace + */ +static char +lexer_consume_single(struct cescal_state *state, bool skip_ws) +{ + char c; + + if (state == NULL) { + return '\0'; + } + + while ((c = readbuf_read(&state->rb, state->in_fd)) != '\0') { + if (lexer_is_ws(c)) { + continue; + } + + return c; + } + + return '\0'; +} + +int +lexer_nom(struct cescal_state *state, struct token *res) +{ + char c; + + if (state == NULL || res == NULL) { + errno = EINVAL; + return -1; + } + + if ((c = lexer_consume_single(state, true)) == '\0') { + return -1; + } + + switch (c) { + case '(': + res->type = TT_LPAREN; + res->c = c; + return 0; + case ')': + res->type = TT_RPAREN; + res->c = c; + return 0; + case ',': + res->type = TT_COMMA; + res->c = c; + return 0; + } + + cc_error("got unknown token '%c'\n", c); + return -1; +} |
