diff options
author | Quinn Stephens <quinn@osmora.org> | 2025-06-09 21:17:22 -0400 |
---|---|---|
committer | Quinn Stephens <quinn@osmora.org> | 2025-06-09 21:17:22 -0400 |
commit | 6ff31c0dbf2f5485604936de3e6457794554fb75 (patch) | |
tree | 641fa03dd65b24776baf49468e1d23c52763b72c /src | |
parent | 750c26fedb02db025bcb86176a7d94cb386b6b18 (diff) |
parser: Introduce parser context struct
Signed-off-by: Quinn Stephens <quinn@osmora.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/parser/parser.c | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/src/parser/parser.c b/src/parser/parser.c index 6f795cc..d8b1982 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -47,14 +47,14 @@ tok_error(struct token *tok, const char *fmt, ...) } static bool -parse_func_decl(struct lexer *lexer, struct token *tok, struct ast_node *func) +parse_func_decl(struct parser *ctx, struct ast_node *func) { /* * TODO: Parse parameters. */ - lexer_next(lexer, tok); - if (tok->kind != TOK_RPAREN) { - tok_error(tok, "expected \")\" after \"(\"\n"); + parser_advance(ctx); + if (ctx->tok.kind != TOK_RPAREN) { + tok_error(&ctx->tok, "expected \")\" after \"(\"\n"); free(func); return false; } @@ -62,9 +62,9 @@ parse_func_decl(struct lexer *lexer, struct token *tok, struct ast_node *func) /* * TODO: Parse body. */ - lexer_next(lexer, tok); - if (tok->kind != TOK_SEMICOLON) { - tok_error(tok, "expected \";\" after \")\"\n"); + parser_advance(ctx); + if (ctx->tok.kind != TOK_SEMICOLON) { + tok_error(&ctx->tok, "expected \";\" after \")\"\n"); free(func); return false; } @@ -75,12 +75,12 @@ parse_func_decl(struct lexer *lexer, struct token *tok, struct ast_node *func) log_debug("Parsed function \"%.*s\" (return type %s)\n", func->name_len, func->name, func->type->name); free(func); - lexer_next(lexer, tok); + parser_advance(ctx); return true; } static bool -parse_decl(struct lexer *lexer, struct token *tok) +parse_decl(struct parser *ctx) { struct type *type; struct ast_node *node; @@ -88,19 +88,19 @@ parse_decl(struct lexer *lexer, struct token *tok) /* * TODO: Parse custom types. */ - if (!(tok->flags & TF_BUILTIN_TYPE)) { - tok_error(tok, "expected type\n"); + if (!(ctx->tok.flags & TF_BUILTIN_TYPE)) { + tok_error(&ctx->tok, "expected type\n"); return false; } - type = types_find_builtin(tok->kind); + type = types_find_builtin(ctx->tok.kind); if (type == NULL) { return false; } - lexer_next(lexer, tok); - if (tok->kind != TOK_IDENTIFIER) { - tok_error(tok, "expected identifier after type\n"); + parser_advance(ctx); + if (ctx->tok.kind != TOK_IDENTIFIER) { + tok_error(&ctx->tok, "expected identifier after type\n"); return false; } @@ -111,20 +111,20 @@ parse_decl(struct lexer *lexer, struct token *tok) } node->kind = NOK_UNKNOWN; - node->name = tok->pos; - node->name_len = tok->len; + node->name = ctx->tok.pos; + node->name_len = ctx->tok.len; node->type = type; node->ptr_levels = 0; - lexer_next(lexer, tok); - if (tok->kind == TOK_LPAREN) { - return parse_func_decl(lexer, tok, node); + parser_advance(ctx); + if (ctx->tok.kind == TOK_LPAREN) { + return parse_func_decl(ctx, node); } /* * TODO: Parse variable declarations. */ - tok_error(tok, "expected \"(\" after identifier\n"); + tok_error(&ctx->tok, "expected \"(\" after identifier\n"); free(node); return false; } @@ -132,24 +132,25 @@ parse_decl(struct lexer *lexer, struct token *tok) bool parser_parse(struct lexer *lexer) { - struct token tok; + struct parser ctx; bool success; log_debug("parsing...\n"); - if (!lexer_next(lexer, &tok)) { + ctx.lexer = lexer; + if (!parser_advance(&ctx)) { log_error("failed to get first token\n"); return false; } - while (tok.kind != TOK_EOF) { - if (tok.kind == TOK_UNKNOWN) { - tok_error(&tok, "unrecognized token\n"); + while (ctx.tok.kind != TOK_EOF) { + if (ctx.tok.kind == TOK_UNKNOWN) { + tok_error(&ctx.tok, "unrecognized token\n"); return false; } - if ((tok.flags & TF_BUILTIN_TYPE) || tok.kind == TOK_IDENTIFIER) { - success = parse_decl(lexer, &tok); + if ((ctx.tok.flags & TF_BUILTIN_TYPE) || ctx.tok.kind == TOK_IDENTIFIER) { + success = parse_decl(&ctx); } if (!success) { |