From 1ddf5b68fa60736c1dd9ffda5eba09bbd90309e1 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 14 May 2021 18:45:10 +0200 Subject: lexer: skip interpreter line in any source buffer Skip interpreter lines in any source buffer and handle the skipping in the lexer itself, to avoid reporting wrongly shifted token offsets to the compiler, resulting in wrong error locations and source contexts. Signed-off-by: Jo-Philipp Wich --- lexer.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lexer.c') diff --git a/lexer.c b/lexer.c index d9d657e..25a5cf4 100644 --- a/lexer.c +++ b/lexer.c @@ -1141,6 +1141,38 @@ lex_step(uc_lexer *lex, FILE *fp) return NULL; } +static void +uc_lexer_skip_shebang(uc_lexer *lex) +{ + uc_source *source = lex->source; + FILE *fp = source->fp; + int c1, c2; + + c1 = fgetc(fp); + c2 = fgetc(fp); + + if (c1 == '#' && c2 == '!') { + next_lineinfo(lex); + + source->off += 2; + + while ((c1 = fgetc(fp)) != EOF) { + source->off++; + + if (c1 == '\n') { + update_lineinfo(lex, source->off); + next_lineinfo(lex); + + break; + } + } + } + else { + ungetc(c2, fp); + ungetc(c1, fp); + } +} + void uc_lexer_init(uc_lexer *lex, uc_parse_config *config, uc_source *source) { @@ -1171,6 +1203,10 @@ uc_lexer_init(uc_lexer *lex, uc_parse_config *config, uc_source *source) lex->lead_surrogate = 0; lex->lastoff = 0; + + /* Skip any potential interpreter line */ + if (lex->source->off == 0) + uc_lexer_skip_shebang(lex); } void -- cgit v1.2.3