diff options
| author | Chloe M. <chloe@aptel.org> | 2026-08-23 06:03:49 +0000 |
|---|---|---|
| committer | Chloe M. <chloe@aptel.org> | 2026-08-23 06:05:14 +0000 |
| commit | 4f3f4cb26d9eee09aebdd37765208943e9ef6214 (patch) | |
| tree | 236256a8488f2c946006661337b81d8ad9231b5d | |
| parent | 6153bf6edb45e4ad1759e3db69e3a7358ae28b4e (diff) | |
core: parser: Add parser groundwork
Signed-off-by: Chloe M. <chloe@aptel.org>
| -rw-r--r-- | core/defconf.c | 8 | ||||
| -rw-r--r-- | core/parser.c | 48 | ||||
| -rw-r--r-- | head/defconf/parser.h | 20 | ||||
| -rw-r--r-- | head/defconf/token.h | 1 |
4 files changed, 77 insertions, 0 deletions
diff --git a/core/defconf.c b/core/defconf.c index 4444179..be55708 100644 --- a/core/defconf.c +++ b/core/defconf.c @@ -8,6 +8,7 @@ #include <stdlib.h> #include <string.h> #include "defconf/state.h" +#include "defconf/parser.h" /* Globals */ static char *config_path = NULL; @@ -24,11 +25,18 @@ static int defconf_run(void) { struct defconf_state state; + int error; if (defconf_state_init(config_path, &state) < 0) { return -1; } + /* Begin parsing the config */ + if ((error = parse_begin(&state)) < 0) { + defconf_state_destroy(&state); + return error; + } + defconf_state_destroy(&state); return 0; } diff --git a/core/parser.c b/core/parser.c new file mode 100644 index 0000000..727d5e1 --- /dev/null +++ b/core/parser.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + +#include "defconf/parser.h" +#include "defconf/lexer.h" +#include "defconf/token.h" + +/* Symbolic token */ +#define SYMTOK(TOKSTR) \ + "[" TOKSTR "]" + +/* Literal token */ +#define TOKLIT(TOKSTR) \ + "'" TOKSTR "'" + +/* Safe macro to convert token type to string */ +#define TOKSTR(TT) \ + ((TT) >= _TT_MAX) \ + ? "bad" \ + : toktab[(TT)] + +/* Token to string table */ +static const char *toktab[_TT_MAX] = { + [TT_NONE] = SYMTOK("none"), + [TT_DEFINE] = SYMTOK("define"), + [TT_TRUE] = TOKLIT("true"), + [TT_FALSE] = TOKLIT("false"), + [TT_INTLIT] = SYMTOK("int"), + [TT_IDENT] = SYMTOK("ident") +}; + +int +parse_begin(struct defconf_state *state) +{ + struct token tok; + + if (state == NULL) { + return -1; + } + + while (lexer_scan(state, &tok) == 0) { + printf("got %s\n", TOKSTR(tok.type)); + } + + return 0; +} diff --git a/head/defconf/parser.h b/head/defconf/parser.h new file mode 100644 index 0000000..8306cd5 --- /dev/null +++ b/head/defconf/parser.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + +#ifndef DEFCONF_PARSER_H +#define DEFCONF_PARSER_H 1 + +#include "defconf/state.h" + +/* + * Begin parsing the configuration + * + * @state: Defconf state + * + * Returns zero on success + */ +int parse_begin(struct defconf_state *state); + +#endif /* !DEFCONF_PARSER_H */ diff --git a/head/defconf/token.h b/head/defconf/token.h index e1abd0a..4a69311 100644 --- a/head/defconf/token.h +++ b/head/defconf/token.h @@ -16,6 +16,7 @@ typedef enum { TT_FALSE, /* 'false' */ TT_INTLIT, /* [0-9]+ */ TT_IDENT, /* [ident] */ + _TT_MAX } tt_t; /* |
