2026-05-23 02:21:09 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2026, Chloe M.
|
|
|
|
|
* Provided under the BSD-3 clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include "cescal/log.h"
|
|
|
|
|
#include "cescal/parser.h"
|
|
|
|
|
#include "cescal/state.h"
|
|
|
|
|
#include "cescal/lexer.h"
|
|
|
|
|
|
|
|
|
|
/* Symbolic token */
|
|
|
|
|
#define symtok(tok) \
|
|
|
|
|
"[" tok "]"
|
|
|
|
|
|
|
|
|
|
/* Quoted token */
|
|
|
|
|
#define qtok(tok) \
|
|
|
|
|
"'" tok "'"
|
|
|
|
|
|
|
|
|
|
/* Convert token to string */
|
|
|
|
|
#define tokstr1(tt) \
|
|
|
|
|
toktab[(tt)]
|
|
|
|
|
|
|
|
|
|
/* Convert token to string */
|
|
|
|
|
#define tokstr(tok) \
|
|
|
|
|
toktab[(tok)->type]
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Converts numeric tokens into human readable strings
|
|
|
|
|
*/
|
|
|
|
|
static const char *toktab[] = {
|
2026-05-23 08:09:05 -04:00
|
|
|
[TT_NONE] = symtok("none"),
|
|
|
|
|
[TT_IDENT] = symtok("ident"),
|
|
|
|
|
[TT_COMMENT] = symtok("comment"),
|
|
|
|
|
[TT_INTLIT] = symtok("number"),
|
|
|
|
|
[TT_LPAREN] = qtok("("),
|
|
|
|
|
[TT_RPAREN] = qtok(")"),
|
|
|
|
|
[TT_COMMA] = qtok(","),
|
2026-05-23 08:54:14 -04:00
|
|
|
[TT_COLON] = qtok(":"),
|
2026-05-23 08:10:57 -04:00
|
|
|
[TT_ARROW] = qtok("->"),
|
2026-05-23 09:01:18 -04:00
|
|
|
[TT_PLUS] = qtok("+"),
|
|
|
|
|
[TT_MINUS] = qtok("-"),
|
|
|
|
|
[TT_SLASH] = qtok("/"),
|
|
|
|
|
[TT_STAR] = qtok("*"),
|
|
|
|
|
[TT_EQUALS] = qtok("="),
|
2026-05-23 08:49:17 -04:00
|
|
|
[TT_DEFINE] = qtok("#define"),
|
|
|
|
|
[TT_IFNDEF] = qtok("#ifndef"),
|
|
|
|
|
[TT_IFDEF] = qtok("#ifdef"),
|
2026-05-23 08:09:05 -04:00
|
|
|
[TT_RETURN] = qtok("return"),
|
|
|
|
|
[TT_PUB] = qtok("pub"),
|
|
|
|
|
[TT_PROC] = qtok("proc"),
|
|
|
|
|
[TT_BEGIN] = qtok("begin"),
|
2026-05-23 08:52:56 -04:00
|
|
|
[TT_END] = qtok("end"),
|
|
|
|
|
[TT_U8] = qtok("u8"),
|
|
|
|
|
[TT_U16] = qtok("u16"),
|
|
|
|
|
[TT_U32] = qtok("u32"),
|
|
|
|
|
[TT_U64] = qtok("u64")
|
2026-05-23 02:21:09 -04:00
|
|
|
};
|
|
|
|
|
|
2026-05-23 14:36:55 +00:00
|
|
|
static int
|
|
|
|
|
parser_nom(struct cescal_state *state, struct token *res)
|
|
|
|
|
{
|
|
|
|
|
struct token tok;
|
|
|
|
|
|
|
|
|
|
if (state == NULL || res == NULL) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lexer_nom(state, &tok) < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tokbuf_push(&state->tokbuf, &tok) < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*res = tok;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 02:21:09 -04:00
|
|
|
int
|
|
|
|
|
parser_parse(struct cescal_state *state)
|
|
|
|
|
{
|
|
|
|
|
struct token tok;
|
|
|
|
|
|
|
|
|
|
if (state == NULL) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 14:36:55 +00:00
|
|
|
while (parser_nom(state, &tok) == 0) {
|
2026-05-23 02:21:09 -04:00
|
|
|
cc_trace("got token %s\n", tokstr(&tok));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|