diff options
author | Jo-Philipp Wich <jo@mein.io> | 2023-07-12 01:02:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-12 01:02:14 +0200 |
commit | e06f656f5043d5b86f6ccfb7f9b8447800623659 (patch) | |
tree | 8cc3508e5c2b10c34190571161f46d879016eed2 | |
parent | 62775f4a4492ec2d6f19f2bc0af882de041b0bb7 (diff) | |
parent | 24f1a5617ae3976313ace91970d1994a1ae7cc89 (diff) |
Merge pull request #158 from jow-/offset-accouting
Fixes for source offset tracking
-rw-r--r-- | lexer.c | 4 | ||||
-rw-r--r-- | source.c | 30 | ||||
-rw-r--r-- | tests/custom/03_stdlib/29_require | 2 | ||||
-rw-r--r-- | tests/custom/03_stdlib/35_include | 2 | ||||
-rw-r--r-- | tests/custom/03_stdlib/36_render | 2 | ||||
-rw-r--r-- | tests/custom/03_stdlib/62_loadfile | 2 | ||||
-rw-r--r-- | tests/custom/99_bugs/14_incomplete_expression_at_eof | 2 | ||||
-rw-r--r-- | tests/custom/99_bugs/25_lexer_shifted_offsets | 2 |
8 files changed, 25 insertions, 21 deletions
@@ -94,9 +94,9 @@ fill_buf(uc_lexer_t *lex) { static int update_line(uc_lexer_t *lex, int ch) { - if (ch == '\n' || ch == EOF) + if (ch == '\n') uc_source_line_next(lex->source); - else + else if (ch != EOF) uc_source_line_update(lex->source, 1); lex->source->off++; @@ -74,22 +74,23 @@ size_t uc_source_get_line(uc_source_t *source, size_t *offset) { uc_lineinfo_t *lines = &source->lineinfo; - size_t i, pos = 0, line = 0, lastoff = 0; + size_t i, pos = 0, line = 1, lastoff = 0; - for (i = 0; i < lines->count; i++) { - if (lines->entries[i] & 0x80) { - lastoff = pos; + for (i = 0; i <= lines->count; i++) { + if (pos >= *offset || i == lines->count) { + *offset = (*offset - lastoff) + 1; + + return line; + } + + /* don't count first line jump as actual byte */ + if (i > 0 && (lines->entries[i] & 0x80)) { line++; pos++; + lastoff = pos; } pos += (lines->entries[i] & 0x7f); - - if (pos >= *offset) { - *offset -= lastoff - 1; - - return line; - } } return 0; @@ -125,10 +126,13 @@ uc_source_type_test(uc_source_t *source) type = UC_SOURCE_TYPE_PRECOMPILED; } else { - uc_source_line_update(source, source->off); - - if (c == '\n') + if (c == '\n') { + uc_source_line_update(source, source->off - 1); uc_source_line_next(source); + } + else { + uc_source_line_update(source, source->off); + } } if (fseek(fp, -(long)rlen, SEEK_CUR) == -1) diff --git a/tests/custom/03_stdlib/29_require b/tests/custom/03_stdlib/29_require index a81edb4..3736339 100644 --- a/tests/custom/03_stdlib/29_require +++ b/tests/custom/03_stdlib/29_require @@ -134,7 +134,7 @@ return { Runtime error: Unable to compile source file './files/require/test/broken.uc': | Syntax error: Expecting label - | In line 2, byte 10: + | In line 3, byte 1: | | `return {` | ^-- Near here diff --git a/tests/custom/03_stdlib/35_include b/tests/custom/03_stdlib/35_include index 83c34bb..823748a 100644 --- a/tests/custom/03_stdlib/35_include +++ b/tests/custom/03_stdlib/35_include @@ -147,7 +147,7 @@ A compilation error in the file triggers an exception. Runtime error: Unable to compile source file './files/broken.uc': | Syntax error: Expecting label - | In line 3, byte 11: + | In line 4, byte 1: | | ` return {` | Near here --^ diff --git a/tests/custom/03_stdlib/36_render b/tests/custom/03_stdlib/36_render index aa2a27b..19824cc 100644 --- a/tests/custom/03_stdlib/36_render +++ b/tests/custom/03_stdlib/36_render @@ -146,7 +146,7 @@ A compilation error in the file triggers an exception. Runtime error: Unable to compile source file './files/broken.uc': | Syntax error: Expecting label - | In line 3, byte 11: + | In line 4, byte 1: | | ` return {` | Near here --^ diff --git a/tests/custom/03_stdlib/62_loadfile b/tests/custom/03_stdlib/62_loadfile index 4926696..9aaa0cf 100644 --- a/tests/custom/03_stdlib/62_loadfile +++ b/tests/custom/03_stdlib/62_loadfile @@ -134,7 +134,7 @@ Compiling a syntax error (should fail with syntax error exception) Runtime error: Unable to compile source file './files/test6.uc': | Syntax error: Expecting expression - | In line 1, byte 5: + | In line 2, byte 1: | | `1 +` | ^-- Near here diff --git a/tests/custom/99_bugs/14_incomplete_expression_at_eof b/tests/custom/99_bugs/14_incomplete_expression_at_eof index 474e87c..fd0ddd6 100644 --- a/tests/custom/99_bugs/14_incomplete_expression_at_eof +++ b/tests/custom/99_bugs/14_incomplete_expression_at_eof @@ -3,7 +3,7 @@ buffer, the source code context line was not properly printed. -- Expect stderr -- Syntax error: Expecting expression -In line 1, byte 7: +In line 2, byte 1: `{% 1+` ^-- Near here diff --git a/tests/custom/99_bugs/25_lexer_shifted_offsets b/tests/custom/99_bugs/25_lexer_shifted_offsets index db10121..bb374ae 100644 --- a/tests/custom/99_bugs/25_lexer_shifted_offsets +++ b/tests/custom/99_bugs/25_lexer_shifted_offsets @@ -12,7 +12,7 @@ to be incorrectly shifted. -- Expect stderr -- Error -In line 3, byte 12: +In line 3, byte 13: ` die("Error");` Near here -----^ |