-
-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: allowing non-string types to Capture tokens #139
Comments
torbenschinke
changed the title
allowing non-string types to Capture tokens
proposal: allowing non-string types to Capture tokens
Apr 9, 2021
That is incorrect. You can implement Capture on any type. See the hcl example where it captures into a |
Well, on structs it does not work: type Boxes struct {
Pos lexer.Position
Boxes Box `@@`
}
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)
}
} This causes a type assertion panic:
|
I found a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can only use the
Capture(values []string) error
interface on types with the underlying string type. It seems like an unneeded restriction.In setField nodes.go:592 the type is asserted to a string to assemble the slice parameter for the Capture method. I wonder if we could use the lexer tokens to build the same just without the type assertion.
The text was updated successfully, but these errors were encountered: