Skip to content

Commit

Permalink
fix the first remaining token is not shown on parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Aug 2, 2021
1 parent 5c45ea5 commit 653ef1e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions expr_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,17 +358,20 @@ func (p *ExprParser) Parse(l *ExprLexer) (ExprNode, *ExprError) {
return nil, err
}

if p.peek().Kind != TokenKindEnd {
if t := p.peek(); t.Kind != TokenKindEnd {
// It did not reach the end of sequence
qb := quotesBuilder{}
qb.append(t.Kind.String())
c := 1
for {
t := l.Next()
if t.Kind == TokenKindEnd {
break
}
qb.append(t.Kind.String())
c++
}
p.errorf("parser did not reach end of input after parsing expression. remaining tokens are %s", qb.build())
p.errorf("parser did not reach end of input after parsing the expression. %d remaining token(s) in the input: %s", c, qb.build())
return nil, p.err
}

Expand Down
2 changes: 1 addition & 1 deletion expr_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ func TestParseExpressionSyntaxError(t *testing.T) {
{
what: "remaining inputs",
input: "42 foo bar",
expected: "parser did not reach end of input after parsing expression",
expected: "2 remaining token(s) in the input: \"IDENT\", \"IDENT\"",
},
{
what: "missing operand in || operator",
Expand Down

0 comments on commit 653ef1e

Please sign in to comment.