From 12fbaf476b1115f4618ce1931578efac54a3daf0 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 18 Apr 2021 21:40:00 +0200 Subject: [PATCH] Small improvements to macro parsing and checking. --- src/compiler/parse_global.c | 18 +++++++++++++++--- src/compiler/sema_expr.c | 3 ++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index 972445f4e..2cb5580f5 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -1524,8 +1524,12 @@ static inline Decl *parse_typedef_declaration(Context *context, Visibility visib return decl; } +static bool next_is_type_and_not_ident(Context *context) +{ + return context->tok.type != TOKEN_IDENT || context->next_tok.type == TOKEN_COLON; +} /** - * macro ::= MACRO type? identifier '(' macro_params ')' compound_statement + * macro ::= MACRO (type '!'?)? identifier '!'? '(' macro_params ')' compound_statement */ static inline Decl *parse_macro_declaration(Context *context, Visibility visibility) { @@ -1533,7 +1537,9 @@ static inline Decl *parse_macro_declaration(Context *context, Visibility visibil TypeInfo *rtype = NULL; bool failable = false; - if (!TOKEN_IS(TOKEN_IDENT)) + + // 1. Return type? + if (next_is_type_and_not_ident(context)) { rtype = TRY_TYPE_OR(parse_type(context), poisoned_decl); failable = try_consume(context, TOKEN_BANG); @@ -1541,7 +1547,13 @@ static inline Decl *parse_macro_declaration(Context *context, Visibility visibil Decl *decl = decl_new(DECL_MACRO, context->tok.id, visibility); decl->macro_decl.rtype = rtype; decl->macro_decl.failable = failable; - TRY_CONSUME_OR(TOKEN_IDENT, "Expected a macro name here", poisoned_decl); + if (rtype && TOKEN_IS(TOKEN_DOT)) + { + SEMA_ERROR(rtype, "Expected a macro name here."); + return poisoned_decl; + } + + TRY_CONSUME_OR(TOKEN_IDENT, "Expected a macro name here.", poisoned_decl); CONSUME_OR(TOKEN_LPAREN, poisoned_decl); Decl **params = NULL; diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 0e72bd61b..5720b444f 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -627,7 +627,8 @@ static inline bool sema_expr_analyse_identifier(Context *context, Type *to, Expr } if (expr->expr_kind == EXPR_MACRO_IDENTIFIER) { - SEMA_ERROR(expr, "Only macro expansions can be prefixed with '@', please try to remove it.", decl->name); + SEMA_ERROR(expr, "Only macro expansions can be prefixed with '@', please try removing it.", decl->name); + return false; } if (decl->resolve_status != RESOLVE_DONE) {