diff --git a/_examples/expr2/main.go b/_examples/expr2/main.go index 13580dea..755614d9 100644 --- a/_examples/expr2/main.go +++ b/_examples/expr2/main.go @@ -58,11 +58,18 @@ type Unary struct { type Primary struct { Number *float64 ` @Float | @Int` String *string `| @String` - Bool *bool `| ( @"true" | "false" )` + Bool *Boolean `| @( "true" | "false" )` Nil bool `| @"nil"` SubExpression *Expression `| "(" @@ ")" ` } +type Boolean bool + +func (b *Boolean) Capture(values []string) error { + *b = values[0] == "true" + return nil +} + var parser = participle.MustBuild[Expression](participle.UseLookahead(2)) func main() { diff --git a/_examples/expr2/main_test.go b/_examples/expr2/main_test.go index 3df87609..094dc80b 100644 --- a/_examples/expr2/main_test.go +++ b/_examples/expr2/main_test.go @@ -12,3 +12,40 @@ func TestExe(t *testing.T) { repr.Println(expr) require.NoError(t, err) } + +func toPtr[T any](x T) *T { + return &x +} + +func TestExe_BoolFalse(t *testing.T) { + got, err := parser.ParseString("", `1 + false`) + + expected := &Expression{ + Equality: &Equality{ + Comparison: &Comparison{ + Addition: &Addition{ + Multiplication: &Multiplication{ + Unary: &Unary{ + Primary: &Primary{ + Number: toPtr(float64(1)), + }, + }, + }, + Op: "+", + Next: &Addition{ + Multiplication: &Multiplication{ + Unary: &Unary{ + Primary: &Primary{ + Bool: toPtr(Boolean(false)), + }, + }, + }, + }, + }, + }, + }, + } + + require.NoError(t, err) + require.Equal(t, expected, got) +}