From 0dd893225bebffd9a25f1345ec49922aa961b882 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Sun, 6 Feb 2022 21:18:04 -0500 Subject: [C]: Add `here` keyword that evaluates to a string with it's location It's not really a keyword like the other ones, but just handled completely at the lexer level since it already knows the location of the token, so it injects a string literal instead. We also use this in the self-hosted compiler now for better error reporting for where the error happened internally. --- src/lexer.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src') diff --git a/src/lexer.c b/src/lexer.c index 0d34eea..7bf426a 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -200,6 +200,15 @@ Token Lexer_next(Lexer *lexer) default: { + // Hack to support getting source locations + if (Lexer_starts_with(lexer, "here")) { + char *loc_string = calloc(sizeof(char), 100); + sprintf(loc_string, "%s:%lld:%lld", lexer->filename, lexer->line+1, lexer->col+1); + Token token = Token_from_string(loc_string, Lexer_loc(lexer)); + advance(lexer, 4); + return token; + } + // Handle keywords explicitly #define LEX_KEYWORD(token_type, str) if (Lexer_starts_with(lexer, str)) return Lexer_make_token(lexer, token_type, strlen(str)); ENUM_KEYWORDS(LEX_KEYWORD) -- cgit v1.2.3