summaryrefslogtreecommitdiffhomepage
path: root/lexer.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-05-14 18:45:10 +0200
committerJo-Philipp Wich <jo@mein.io>2021-05-14 18:46:31 +0200
commit1ddf5b68fa60736c1dd9ffda5eba09bbd90309e1 (patch)
tree336e3162fbef90b4f838d2effc616b4a1bb20037 /lexer.c
parent9951a003e53d39a8d00b13b694a3a106e161c552 (diff)
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 <jo@mein.io>
Diffstat (limited to 'lexer.c')
-rw-r--r--lexer.c36
1 files changed, 36 insertions, 0 deletions
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