Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ There are a few areas where Participle can provide useful feedback to users of y
1. Errors returned by [Parser.Parse*()](https://pkg.go.dev/github.com/alecthomas/participle/v2#Parser.Parse) will be:
1. Of type [Error](https://pkg.go.dev/github.com/alecthomas/participle/v2#Error). This will contain positional information where available.
2. May either be [ParseError](https://pkg.go.dev/github.com/alecthomas/participle/v2#ParseError) or [lexer.Error](https://pkg.go.dev/github.com/alecthomas/participle/v2/lexer#Error)
3. [UnexpectedTokenError](https://pkg.go.dev/github.com/alecthomas/participle/v2#UnexpectedTokenError) messages include the production name when available. The name follows the selected error through backtracking. Anonymous productions and errors caused only by trailing input after a complete parse have no production name.
2. Participle will make a best effort to return as much of the AST up to the error location as possible.
3. Any node in the AST containing a field `Pos lexer.Position` [^1] will be automatically
populated from the nearest matching token.
Expand Down
4 changes: 4 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ type UnexpectedTokenError struct {
// the token type has no symbolic name (eg. literal token types).
TokenType string
expectNode node // used instead of Expect, delays creating the string representation until necessary
production string
}

func (u *UnexpectedTokenError) Error() string { return FormatError(u) }

func (u *UnexpectedTokenError) Message() string {
message := fmt.Sprintf("unexpected token %q", u.Unexpected)
if u.production != "" {
message = u.production + ": " + message
}
if u.TokenType != "" {
message += " of type " + u.TokenType
}
Expand Down
Loading