Repository navigation
[JSC] The line starts of a source come with its code, and positions are kept #734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
da7256c
[JSC] Line start table: about one byte per line, an embedder can supp…
dylan-conway 5cdec80
Keep the position in the text, which every source that shares the cod…
dylan-conway 7c233f1
[JSC] The line starts of a source come with its code
dylan-conway 40f2c0f
line-start-table-positions.js: get the line of a syntax error from a …
dylan-conway 4e54cc3
[JSC] U+2028 and U+2029 in a string get to shiftLineTerminator() in e…
dylan-conway 8662e2b
[JSC] The line starts come from a scan of the text when its code is c…
dylan-conway 9e20a36
[JSC] The rest of the last commit: the scan, the table and the tests
dylan-conway 3f5d317
builtin-position-counts-from-the-builtin.js: a build with assertions …
dylan-conway af5518a
[JSC] The lexer notes where the lines start, and nothing scans a sour…
dylan-conway File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // The line and column of an error come from its source's line start table. This compares them with | ||
| // positions counted here, for every kind of line terminator, for line lengths on both sides of what | ||
| // one, two and three bytes of the table's length encoding hold, and for line counts around its block size. | ||
|
|
||
| function shouldBe(actual, expected, message) { | ||
| if (actual !== expected) | ||
| throw new Error(`${message}: expected ${expected} but got ${actual}`); | ||
| } | ||
|
|
||
| let seed = 12345; | ||
| function random(n) { | ||
| seed = (seed * 1103515245 + 12345) & 0x7fffffff; | ||
| return (seed >>> 12) % n; | ||
| } | ||
|
|
||
| globalThis.seen = []; | ||
| globalThis.at = function (error) { | ||
| seen.push(`${error.line}:${error.column}`); | ||
| }; | ||
|
|
||
| function positionsOf(text) { | ||
| seen.length = 0; | ||
| (0, eval)(text); | ||
| return seen.join(" "); | ||
| } | ||
|
|
||
| // One-based, for ascending offsets, by reading the text one character at a time. | ||
| function positionsAt(text, offsets) { | ||
| const positions = []; | ||
| let line = 1; | ||
| let lineStart = 0; | ||
| let i = 0; | ||
| for (const offset of offsets) { | ||
| for (; i < offset; i++) { | ||
| const c = text[i]; | ||
| if (c === "\r" && text[i + 1] === "\n") | ||
| i++; | ||
| else if (c !== "\n" && c !== "\r" && c !== "\u2028" && c !== "\u2029") | ||
| continue; | ||
| line++; | ||
| lineStart = i + 1; | ||
| } | ||
| positions.push(`${line}:${offset - lineStart + 1}`); | ||
| } | ||
| return positions.join(" "); | ||
| } | ||
|
|
||
| // Where in `at(new Error("..."));` the column of the error is. | ||
| const columnInStatement = Number(positionsOf(`at(new Error(""));`).split(":")[1]) - 1; | ||
|
|
||
| const terminators = { | ||
| "LF": ["\n"], | ||
| "CR LF": ["\r\n"], | ||
| "CR": ["\r"], | ||
| "LF, CR LF and CR": ["\n", "\r\n", "\r"], | ||
| "all five, 16-bit": ["\n", "\r\n", "\r", "\u2028", "\u2029"], | ||
| }; | ||
|
|
||
| const lengths = [126, 127, 128, 129, 300, 16382, 16383, 16384, 16385, 40000]; | ||
|
|
||
| for (const [name, choices] of Object.entries(terminators)) { | ||
| const terminator = () => choices[random(choices.length)]; | ||
| let text = ""; | ||
| const offsets = []; | ||
| for (let i = 0; i < 150; i++) { | ||
| for (let blank = random(4); blank > 0; blank--) | ||
| text += terminator(); | ||
| text += " ".repeat(random(12)); | ||
| offsets.push(text.length + columnInStatement); | ||
| text += `at(new Error("${"x".repeat(i < lengths.length ? lengths[i] : random(60))}"));` + terminator(); | ||
| } | ||
| const expected = positionsAt(text, offsets); | ||
| shouldBe(positionsOf(text), expected, name); | ||
|
dylan-conway marked this conversation as resolved.
Resolved
|
||
| // Again: the answers are kept. | ||
| shouldBe(positionsOf(text), expected, `${name}, second time`); | ||
| } | ||
|
|
||
| for (const lines of [1, 2, 63, 64, 65, 127, 128, 129, 130]) { | ||
| for (const endsWithTerminator of [false, true]) { | ||
| const text = "\n".repeat(lines - 1) + `at(new Error(""));` + (endsWithTerminator ? "\n" : ""); | ||
| shouldBe(positionsOf(text), `${lines}:${columnInStatement + 1}`, `${lines} lines`); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.