Parsing key = value where both can be identical. Unexpected token "key" or unexpected token "value". #436
-
|
Hello participle community, I stumbled upon an issue which I broke down. Whats is my expectation: Whats my assumption of the issue: Questions: Example with participle v2.1.4: package main
import (
"testing"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
type Root struct {
Objects []Pair `parser:"@@*"`
}
type Pair struct {
Key string `parser:"@Ident '='"`
Value float64 `parser:"@Number"`
}
func TestIssue(t *testing.T) {
lexer := lexer.MustSimple([]lexer.SimpleRule{
{Name: "Ident", Pattern: "[0-9_a-zA-Z]+"},
{Name: "Number", Pattern: `[-+]?(\d*\.)?\d+`},
{Name: "Whitespace", Pattern: `[ \t]+`},
{Name: "Other", Pattern: `[-={}@\n\r]`},
})
parser := participle.MustBuild[Root](
participle.Lexer(lexer),
participle.Elide("Whitespace"),
)
config, err := parser.ParseString("", `1 = 2`)
t.Logf("Got config %v", config)
if err != nil {
t.Fatalf("Failed to parse input for issue. Reason: %v", err)
}
if len(config.Objects) != 1 {
t.Fatalf("Failed to find root element. Got %v", config)
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
I'm not sure what the issue is, but something that stands out to me is that your |
Beta Was this translation helpful? Give feedback.
Even with that example, you should not overlap your lexer rules. You solve that at the parser level: