diff options
| author | Mustafa Quraish <[email protected]> | 2022-02-06 21:18:04 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-02-06 21:19:45 -0500 |
| commit | 0dd893225bebffd9a25f1345ec49922aa961b882 (patch) | |
| tree | e843cef529e0e1d6cdba85c2268742e3aed68749 /src | |
| parent | Add some information to the README (diff) | |
| download | cup-0dd893225bebffd9a25f1345ec49922aa961b882.tar.xz cup-0dd893225bebffd9a25f1345ec49922aa961b882.zip | |
[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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lexer.c | 9 |
1 files changed, 9 insertions, 0 deletions
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) |