Skip to content

Commit

Permalink
Fix Capture into struct value.
Browse files Browse the repository at this point in the history
See #140, fixes second bug.
  • Loading branch information
alecthomas committed Jun 30, 2021
1 parent 38a6f64 commit 0dc0903
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/go
2 changes: 1 addition & 1 deletion bin/gofmt
6 changes: 5 additions & 1 deletion grammar.go
Expand Up @@ -208,7 +208,7 @@ func (g *generatorContext) parseCapture(slexer *structLexer) (node, error) {
return &capture{field, n}, nil
}
ft := indirectType(field.Type)
if ft.Kind() == reflect.Struct && ft != tokenType && ft != tokensType && !field.Type.Implements(captureType) && !field.Type.Implements(textUnmarshalerType) {
if ft.Kind() == reflect.Struct && ft != tokenType && ft != tokensType && !implements(ft, captureType) && !implements(ft, textUnmarshalerType) {
return nil, fmt.Errorf("structs can only be parsed with @@ or by implementing the Capture or encoding.TextUnmarshaler interfaces")
}
n, err := g.parseTermNoModifiers(slexer, false)
Expand Down Expand Up @@ -377,3 +377,7 @@ func indirectType(t reflect.Type) reflect.Type {
}
return t
}

func implements(t, i reflect.Type) bool {
return t.Implements(i) || reflect.PtrTo(t).Implements(i)
}
32 changes: 32 additions & 0 deletions parser_test.go
Expand Up @@ -1648,3 +1648,35 @@ func BenchmarkIssue143(b *testing.B) {
}
}
}

type Boxes struct {
Pos lexer.Position
Boxes Box `@Ident`
}

type Box struct {
Pos lexer.Position
Val string `@Ident`
}

func (b *Box) Capture(values []string) error {
b.Val = values[0]
return nil
}

func TestBoxedCapture(t *testing.T) {
lex := stateful.MustSimple([]stateful.Rule{
{"Ident", `[a-zA-Z](\w|\.|/|:|-)*`, nil},
{"whitespace", `\s+`, nil},
})

parser := participle.MustBuild(&Boxes{},
participle.Lexer(lex),
participle.UseLookahead(2),
)

boxed := &Boxes{}
if err := parser.ParseString("test", "abc::cdef.abc", boxed); err != nil {
t.Fatal(err)
}
}

0 comments on commit 0dc0903

Please sign in to comment.