Skip to content
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

Improved error reporting #129

Merged
merged 3 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 56 additions & 8 deletions moo.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@
}
}

function pad(s, length) {
if (s.length > length) {
return s
}
return Array(length - s.length + 1).join(" ") + s
}

function lastNLines(string, numLines) {
var position = string.length
var lineBreaks = 0;
while (true) {
var idx = string.lastIndexOf("\n", position - 1)
if (idx === -1) {
break;
} else {
lineBreaks++
}
position = idx
if (lineBreaks === numLines) {
break;
}
if (position === 0) {
break;
}
}
var startPosition =
lineBreaks < numLines ?
0 :
position + 1
return string.substring(startPosition).split("\n")
}

function objectToRules(object) {
var keys = Object.getOwnPropertyNames(object)
var result = []
Expand Down Expand Up @@ -530,7 +562,8 @@

// throw, if no rule with {error: true}
if (group.shouldThrow) {
throw new Error(this.formatError(token, "invalid syntax"))
var err = new Error(this.formatError(token, "invalid syntax"))
throw err;
}

if (group.pop) this.popState()
Expand Down Expand Up @@ -571,13 +604,28 @@
col: this.col,
}
}
var start = Math.max(0, token.offset - token.col + 1)
var eol = token.lineBreaks ? token.text.indexOf('\n') : token.text.length
var firstLine = this.buffer.substring(start, token.offset + eol)
message += " at line " + token.line + " col " + token.col + ":\n\n"
message += " " + firstLine + "\n"
message += " " + Array(token.col).join(" ") + "^"
return message

var numLinesAround = 2
var firstDisplayedLine = Math.max(token.line - numLinesAround, 1)
var lastDisplayedLine = token.line + numLinesAround
var lastLineDigits = String(lastDisplayedLine).length
var displayedLines = lastNLines(
this.buffer,
(this.line - token.line) + numLinesAround + 1
)
.slice(0, 5)
var errorLines = []
errorLines.push(message + " at line " + token.line + " col " + token.col + ":")
errorLines.push("")
for (var i = 0; i < displayedLines.length; i++) {
var line = displayedLines[i]
var lineNo = firstDisplayedLine + i
errorLines.push(pad(String(lineNo), lastLineDigits) + " " + line);
if (lineNo === token.line) {
errorLines.push(pad("", lastLineDigits + token.col + 1) + "^")
}
}
return errorLines.join("\n")
}

Lexer.prototype.clone = function() {
Expand Down
24 changes: 16 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,9 @@ describe('errors', () => {
expect(lexer.next()).toMatchObject({value: '456'})
expect(() => lexer.next()).toThrow(
"invalid syntax at line 2 col 4:\n\n" +
" 456baa\n" +
" ^"
"1 123\n" +
"2 456baa\n" +
" ^"
)
})

Expand All @@ -921,8 +922,12 @@ describe('errors', () => {
expect(tok).toMatchObject({type: 'error', value: ' 12\n345\n6', lineBreaks: 2})
expect(lexer.formatError(tok, "numbers!")).toBe(
"numbers! at line 3 col 2:\n\n" +
" g 12\n" +
" ^"
"1 abc\n" +
"2 def\n" +
"3 g 12\n" +
" ^\n" +
"4 345\n" +
"5 6"
)
})

Expand All @@ -937,8 +942,9 @@ describe('errors', () => {
expect(lexer.col).toBe(9)
expect(lexer.formatError(undefined, "EOF!")).toBe(
"EOF! at line 2 col 9:\n\n" +
" def quxx\n" +
" ^"
"1 abc\n" +
"2 def quxx\n" +
" ^"
)
})

Expand All @@ -954,8 +960,10 @@ describe('errors', () => {
expect(lexer.col).toBe(1)
expect(lexer.formatError(undefined, "oh no!")).toBe(
"oh no! at line 2 col 1:\n\n" +
" def quxx\n" +
" ^"
"1 abc\n" +
"2 def quxx\n" +
" ^\n" +
"3 bar"
)
})

Expand Down