I am learning participle, a parser based on golang. After I went through the tutorial, I decided to create a simple Array parser, here's my construction.
First, I need an AST to represent this array. for the simplicity purpose, I only parse Array of string without quotation mark. ({value1, value_two, value_3_here}, e.t.)
type SimpleArray struct {
Arr []Values `"{" @@* "}"`
}
type Values struct {
Str string `@Ident`
}
and now I create a Lexer for the input string
var Lexer = stateful.MustSimple([]stateful.Rule{
{`Ident`, `[a-zA-Z][a-zA-Z_\d]*`, nil},
{`punct`, `[}{,]`, nil},
{`whitespace`, `\s+`, nil},
})
and I have my main function below, with a simple test case s := "{key, value}"
func main() {
var parser = participle.MustBuild(&SimpleArray{},
participle.Lexer(Lexer),
)
b := &SimpleArray{}
s := "{key, value}"
err := parser.ParseString("", s, b)
if err != nil {panic(err)}
repr.Println(b, repr.Indent(" "), repr.OmitEmpty(true))
}
But I got a panic.
panic: 1:2: unexpected token "key"
I think the @Ident in Values struct will catch the "key", but it isn't, why is it? and how can I fix this?
Thanks!