Skip to content

Commit

Permalink
fixing alecthomas#137 error returned from Capture interface does not …
Browse files Browse the repository at this point in the history
…contain position and is not wrapped
  • Loading branch information
torbenschinke committed Apr 9, 2021
1 parent 85bc851 commit 8a3641a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion grammar.go
Expand Up @@ -52,7 +52,7 @@ func (g *generatorContext) parseType(t reflect.Type) (_ node, returnedError erro
if slexer.NumField() == 0 {
return nil, fmt.Errorf("can not parse into empty struct %s", t)
}
defer decorate(&returnedError, func() string { return slexer.Field().Name })
defer decorate(&returnedError, nil, func() string { return slexer.Field().Name })
e, err := g.parseDisjunction(slexer)
if err != nil {
return nil, err
Expand Down
22 changes: 16 additions & 6 deletions nodes.go
Expand Up @@ -38,15 +38,25 @@ type node interface {
String() string
}

func decorate(err *error, name func() string) {
func nearestPos(tokens []lexer.Token) *lexer.Position {
if len(tokens) == 0 {
return nil
}

return &tokens[0].Pos
}

func decorate(err *error, pos *lexer.Position, name func() string) {
if *err == nil {
return
}
if perr, ok := (*err).(Error); ok {
*err = Errorf(perr.Position(), "%s: %s", name(), perr.Message())
} else {
*err = &parseError{Msg: fmt.Sprintf("%s: %s", name(), *err)}

if pos == nil {
*err = fmt.Errorf("%s: %w", name(), err)
return
}

*err = Wrapf(*pos, *err, "%s", name())
}

// A node that proxies to an implementation that implements the Parseable interface.
Expand Down Expand Up @@ -550,7 +560,7 @@ func sizeOfKind(kind reflect.Kind) int {
// For all other types, an attempt will be made to convert the string to the corresponding
// type (int, float32, etc.).
func setField(tokens []lexer.Token, strct reflect.Value, field structLexerField, fieldValue []reflect.Value) (err error) { // nolint: gocognit
defer decorate(&err, func() string { return strct.Type().Name() + "." + field.Name })
defer decorate(&err, nearestPos(tokens), func() string { return strct.Type().Name() + "." + field.Name })

f := strct.FieldByIndex(field.Index)

Expand Down
2 changes: 1 addition & 1 deletion struct.go
Expand Up @@ -122,7 +122,7 @@ func collectFieldIndexes(s reflect.Type) (out [][]int, err error) {
if s.Kind() != reflect.Struct {
return nil, fmt.Errorf("expected a struct but got %q", s)
}
defer decorate(&err, s.String)
defer decorate(&err, nil, s.String)
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
switch {
Expand Down

0 comments on commit 8a3641a

Please sign in to comment.