Skip to content

Commit

Permalink
handle error from root Parseable
Browse files Browse the repository at this point in the history
  • Loading branch information
mccolljr authored and alecthomas committed Jun 10, 2022
1 parent b0b8e28 commit 2246ffc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 7 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,13 @@ func (p *Parser) parseInto(ctx *parseContext, rv reflect.Value) error {
}

func (p *Parser) rootParseable(ctx *parseContext, parseable Parseable) error {
err := parseable.Parse(ctx.PeekingLexer)
if err == NextMatch {
token := ctx.Peek()
return ctx.DeepestError(UnexpectedTokenError{Unexpected: token})
if err := parseable.Parse(ctx.PeekingLexer); err != nil {
if err == NextMatch {
err = UnexpectedTokenError{Unexpected: ctx.Peek()}
} else {
err = &parseError{Msg: err.Error(), Pos: ctx.Peek().Pos}
}
return ctx.DeepestError(err)
}
peek := ctx.Peek()
if !peek.EOF() && !ctx.allowTrailing {
Expand Down
15 changes: 15 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package participle_test

import (
"errors"
"fmt"
"math"
"net"
Expand Down Expand Up @@ -1712,3 +1713,17 @@ func TestEmptySequenceMatches(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expected, actual)
}

type RootParseableFail struct{}

func (*RootParseableFail) String() string { return "" }
func (*RootParseableFail) GoString() string { return "" }
func (*RootParseableFail) Parse(lex *lexer.PeekingLexer) error {
return errors.New("always fail immediately")
}

func TestRootParseableFail(t *testing.T) {
p := mustTestParser(t, &RootParseableFail{})
err := p.ParseString("<test>", "blah", &RootParseableFail{})
require.EqualError(t, err, "<test>:1:1: always fail immediately")
}

0 comments on commit 2246ffc

Please sign in to comment.