Skip to content

Commit

Permalink
feat: better error display
Browse files Browse the repository at this point in the history
Errors generated by l.Error() will now be wrapped with
"lexer(pos=line,pos): " to improve the developer experience.

Fix: bbuck#6

Signed-off-by: Ted van Riel <80752652+tvanriel@users.noreply.github.com>
  • Loading branch information
tvanriel committed Jun 4, 2022
1 parent 15b21f1 commit 7e9fd3d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
package lexer

import (
"errors"
"fmt"
"strings"
"unicode/utf8"
)
Expand Down Expand Up @@ -188,11 +188,22 @@ func (l *L) NextToken() (*Token, bool) {
}
}

// Get the line number and position in that line the lexer position is currently on.
func getPos(l *L) (int, int) {
untilNow := l.source[:l.position]
linenum := strings.Count(untilNow, "\n") + 1
lastNewLineIndex := strings.LastIndex(untilNow, "\n")
posInLine := l.position - lastNewLineIndex
return linenum, posInLine
}

// Partial yyLexer implementation

func (l *L) Error(e string) {
if l.ErrorHandler != nil {
l.Err = errors.New(e)

linenum, pos := getPos(l)
l.Err = fmt.Errorf("lexer (pos=%d,%d): %v", linenum, pos, e)
l.ErrorHandler(e)
} else {
panic(e)
Expand Down
2 changes: 1 addition & 1 deletion lexer_test/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func Test_LexerError(t *testing.T) {
return
}

if l.Err.Error() != "unexpected token '1'" {
if l.Err.Error() != "lexer (pos=1,2): unexpected token '1'" {
t.Errorf("Expected specific message from error, but got %q", l.Err.Error())
return
}
Expand Down

0 comments on commit 7e9fd3d

Please sign in to comment.