Unable to write a custom lexer for simple key:value format #344
-
Hi, I'm exploring this cool project, but I'm a bit stuck and I was wondering if someone could help me understand why the following snippet is producing the error: package parser
import (
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
"testing"
)
type Spec struct {
Entries []*Entry `@@*`
}
type Entry struct {
Key string `@ Ident ":"`
Value *string `@String`
}
func TestParser_this_fails(t *testing.T) {
customLexer := lexer.MustSimple([]lexer.SimpleRule{
{"Ident", `[a-zA-Z_][a-zA-Z_0-9]*`},
{"String", `"[^"]*"`},
})
p := participle.MustBuild[Spec](participle.Lexer(customLexer))
_, err := p.ParseString("", `key:"value"`)
if err != nil {
t.Fatal(err)
}
}
func TestParser_this_works(t *testing.T) {
p := participle.MustBuild[Spec]()
_, err := p.ParseString("", `key:"value"`)
if err != nil {
t.Fatal(err)
}
} Now if I ditch my custom lexer, it works and produces a correct AST. Using regex101 I can confirm my regex works as I expect, but I presume there's something wrong with my mental model here. Is anyone able to guide me in the right direction? Edit: playing around with the Next method of the Lexer type, I notice that "key" is the first token returned. Given my capture group is Edit-2: I'm comparing this to the TOML parser example, as it looks fairly similar, but I don't seem to figure out whats different.. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I had not added a rule for Adding |
Beta Was this translation helpful? Give feedback.
I had not added a rule for
:
=DAdding
{"Punct", `\[|]|[-!()+/*=:,]`}
resolves it!