Skip to content

Commit

Permalink
Make UnexpectedError a pointer for consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Jun 27, 2022
1 parent 1261c4d commit 53e7ca9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ type UnexpectedTokenError struct {
at node
}

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

func (u UnexpectedTokenError) Message() string { // nolint: golint
func (u *UnexpectedTokenError) Message() string { // nolint: golint
var expected string
if u.at != nil {
expected = fmt.Sprintf(" (expected %s)", u.at)
}
return fmt.Sprintf("unexpected token %q%s", u.Unexpected, expected)
}
func (u UnexpectedTokenError) Position() lexer.Position { return u.Unexpected.Pos } // nolint: golint
func (u *UnexpectedTokenError) Position() lexer.Position { return u.Unexpected.Pos } // nolint: golint

type ParseError struct {
Msg string
Expand Down
2 changes: 1 addition & 1 deletion nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func (s *sequence) Parse(ctx *parseContext, parent reflect.Value) (out []reflect
return nil, nil
}
token := ctx.Peek()
return out, UnexpectedTokenError{Unexpected: token, at: n}
return out, &UnexpectedTokenError{Unexpected: token, at: n}
}
// Special-case for when children return an empty match.
// Appending an empty, non-nil slice to a nil slice returns a nil slice.
Expand Down
8 changes: 4 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (p *Parser[G]) parseOne(ctx *parseContext, parseNode node, rv reflect.Value
}
token := ctx.Peek()
if !token.EOF() && !ctx.allowTrailing {
return ctx.DeepestError(UnexpectedTokenError{Unexpected: token})
return ctx.DeepestError(&UnexpectedTokenError{Unexpected: token})
}
return nil
}
Expand All @@ -262,23 +262,23 @@ func (p *Parser[G]) parseInto(ctx *parseContext, parseNode node, rv reflect.Valu
}
if pv == nil {
token := ctx.Peek()
return ctx.DeepestError(UnexpectedTokenError{Unexpected: token})
return ctx.DeepestError(&UnexpectedTokenError{Unexpected: token})
}
return nil
}

func (p *Parser[G]) rootParseable(ctx *parseContext, parseable Parseable) error {
if err := parseable.Parse(ctx.PeekingLexer); err != nil {
if err == NextMatch {
err = UnexpectedTokenError{Unexpected: ctx.Peek()}
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 {
return ctx.DeepestError(UnexpectedTokenError{Unexpected: peek})
return ctx.DeepestError(&UnexpectedTokenError{Unexpected: peek})
}
return nil
}
Expand Down

0 comments on commit 53e7ca9

Please sign in to comment.