core: lexer: Add preprocessor tokens
Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
@@ -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
|
* Skip anything after a comment
|
||||||
*
|
*
|
||||||
@@ -236,6 +275,19 @@ lexer_nom(struct cescal_state *state, struct token *res)
|
|||||||
case ',':
|
case ',':
|
||||||
res->type = TT_COMMA;
|
res->type = TT_COMMA;
|
||||||
res->c = c;
|
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;
|
return 0;
|
||||||
case '/':
|
case '/':
|
||||||
if (lexer_consume_single(state, true) == '/') {
|
if (lexer_consume_single(state, true) == '/') {
|
||||||
|
|||||||
@@ -38,6 +38,9 @@ static const char *toktab[] = {
|
|||||||
[TT_RPAREN] = qtok(")"),
|
[TT_RPAREN] = qtok(")"),
|
||||||
[TT_COMMA] = qtok(","),
|
[TT_COMMA] = qtok(","),
|
||||||
[TT_ARROW] = qtok("->"),
|
[TT_ARROW] = qtok("->"),
|
||||||
|
[TT_DEFINE] = qtok("#define"),
|
||||||
|
[TT_IFNDEF] = qtok("#ifndef"),
|
||||||
|
[TT_IFDEF] = qtok("#ifdef"),
|
||||||
[TT_RETURN] = qtok("return"),
|
[TT_RETURN] = qtok("return"),
|
||||||
[TT_PUB] = qtok("pub"),
|
[TT_PUB] = qtok("pub"),
|
||||||
[TT_PROC] = qtok("proc"),
|
[TT_PROC] = qtok("proc"),
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ typedef enum {
|
|||||||
TT_RPAREN, /* '( */
|
TT_RPAREN, /* '( */
|
||||||
TT_COMMA, /* ',' */
|
TT_COMMA, /* ',' */
|
||||||
TT_ARROW, /* '->' */
|
TT_ARROW, /* '->' */
|
||||||
|
TT_DEFINE, /* '#define' */
|
||||||
|
TT_IFNDEF, /* '#ifndef' */
|
||||||
|
TT_IFDEF, /* '#ifdef' */
|
||||||
TT_RETURN, /* 'return' */
|
TT_RETURN, /* 'return' */
|
||||||
TT_PUB, /* 'pub' */
|
TT_PUB, /* 'pub' */
|
||||||
TT_PROC, /* 'proc' */
|
TT_PROC, /* 'proc' */
|
||||||
|
|||||||
Reference in New Issue
Block a user