summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorChloe M. <chloe@aptel.org>2026-08-23 09:26:07 +0000
committerChloe M. <chloe@aptel.org>2026-08-23 09:26:07 +0000
commitcac4fb215d6469a5b26d9e1e37e7db16b478ade6 (patch)
tree7613588692f2c80e64c681c42d5853ffcb970fd3 /core
parentf3df011c18821ee6b09f9c2ee8fd48163ed92096 (diff)
core: lexer+parser: Add support for comments
Signed-off-by: Chloe M. <chloe@aptel.org>
Diffstat (limited to 'core')
-rw-r--r--core/lexer.c36
-rw-r--r--core/parser.c5
2 files changed, 40 insertions, 1 deletions
diff --git a/core/lexer.c b/core/lexer.c
index 59aaa13..4f1b4aa 100644
--- a/core/lexer.c
+++ b/core/lexer.c
@@ -213,6 +213,29 @@ lexer_check_kw(const char *kw, struct token *result)
return -1;
}
+/*
+ * Skips an entire line, this is useful for comments
+ *
+ * @state: Defconf state
+ */
+static void
+lexer_skip_line(struct defconf_state *state)
+{
+ int c;
+
+ if (state == NULL) {
+ return;
+ }
+
+ for (;;) {
+ c = lexer_nom(state, false);
+ if (c == EOF)
+ return;
+ if (c == '\n')
+ return;
+ }
+}
+
int
lexer_scan(struct defconf_state *state, struct token *result)
{
@@ -228,6 +251,19 @@ lexer_scan(struct defconf_state *state, struct token *result)
return -1;
}
+ /* Check for single characters */
+ switch (c) {
+ case '/':
+ /* Is this a comment? */
+ if ((c = lexer_nom(state, false)) == '/') {
+ lexer_skip_line(state);
+ result->type = TT_COMMENT;
+ return 0;
+ }
+
+ break;
+ }
+
/* Scan for an identifier */
if ((ident = lexer_scan_ident(state, c)) != NULL) {
if (lexer_check_kw(ident, result) == 0) {
diff --git a/core/parser.c b/core/parser.c
index 79fda93..b5dc915 100644
--- a/core/parser.c
+++ b/core/parser.c
@@ -31,7 +31,8 @@ static const char *toktab[_TT_MAX] = {
[TT_TRUE] = TOKLIT("true"),
[TT_FALSE] = TOKLIT("false"),
[TT_INTLIT] = SYMTOK("int"),
- [TT_IDENT] = SYMTOK("ident")
+ [TT_IDENT] = SYMTOK("ident"),
+ [TT_COMMENT] = SYMTOK("comment")
};
static int
@@ -157,6 +158,8 @@ parse_begin(struct defconf_state *state)
}
break;
+ case TT_COMMENT:
+ break;
default:
trace_fatal(state, "got unexpected token: %s\n", TOKSTR(tok.type));
return -1;