core: lexer: Add preprocessor tokens

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-05-23 08:49:17 -04:00
parent 82e68e92a9
commit 62451acdd4
3 changed files with 58 additions and 0 deletions
+52
View File
@@ -189,6 +189,45 @@ lexer_check_kw(struct cescal_state *state, struct token *res)
}
}
/*
* Check if an identifier token is actually a directive
*
* @state: Compiler state
* @res: Token result is written here
*/
static int
lexer_check_direc(struct cescal_state *state, struct token *res)
{
if (state == NULL || res == NULL) {
errno = EINVAL;
return -1;
}
switch (*res->s) {
case 'd':
if (strcmp(res->s, "define") == 0) {
res->type = TT_DEFINE;
return 0;
}
break;
case 'i':
if (strcmp(res->s, "ifndef") == 0) {
res->type = TT_IFNDEF;
return 0;
}
if (strcmp(res->s, "ifdef") == 0) {
res->type = TT_IFDEF;
return 0;
}
break;
}
return -1;
}
/*
* Skip anything after a comment
*
@@ -236,6 +275,19 @@ lexer_nom(struct cescal_state *state, struct token *res)
case ',':
res->type = TT_COMMA;
res->c = c;
return 0;
case '#':
if ((c = lexer_consume_single(state, true)) == '\0') {
return -1;
}
if (lexer_scan_ident(state, c, res) == 0) {
if (lexer_check_direc(state, res) != 0) {
cc_error("bad directive '%s'\n", res->s);
return -1;
}
}
return 0;
case '/':
if (lexer_consume_single(state, true) == '/') {