From d685df75915a62acd770ada86edb028e7b554137 Mon Sep 17 00:00:00 2001 From: liam Date: Sat, 21 Dec 2024 07:11:43 -0800 Subject: [PATCH] Fix or nil pointer when first match fails --- core/or.go | 4 ++-- core/or_test.go | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/or.go b/core/or.go index a060ae0..ffe7538 100644 --- a/core/or.go +++ b/core/or.go @@ -24,8 +24,8 @@ func Or[A any, B any](a Parser[A], b Parser[B]) Parser[Tuple2[Match[A], Match[B] if errA != nil { return res, false, errA } + res.A = NewMatch(matchA, okA) if okA { - res.A = NewMatch(matchA, true) return res, true, nil } @@ -33,8 +33,8 @@ func Or[A any, B any](a Parser[A], b Parser[B]) Parser[Tuple2[Match[A], Match[B] if errB != nil { return res, false, errB } + res.B = NewMatch(matchB, okB) if okB { - res.B = NewMatch(matchB, true) return res, true, nil } diff --git a/core/or_test.go b/core/or_test.go index 58ae732..f6eb900 100644 --- a/core/or_test.go +++ b/core/or_test.go @@ -17,32 +17,32 @@ package core_test import ( "testing" - "github.com/liamawhite/parse/core" + . "github.com/liamawhite/parse/core" . "github.com/liamawhite/parse/test" ) func TestOr(t *testing.T) { - tests := []ParserTest[core.Tuple2[core.Match[string], core.Match[string]]]{ + tests := []ParserTest[Tuple2[Match[string], Match[string]]]{ { Name: "no match", Input: "C", - Parser: core.Or(core.Rune('A'), core.Rune('B')), - ExpectedMatch: core.NewTuple2[core.Match[string], core.Match[string]](nil, nil), + Parser: Or(Rune('A'), Rune('B')), + ExpectedMatch: NewTuple2(NewMatch("", false), NewMatch("", false)), ExpectedOK: false, RemainingInput: "C", }, { Name: "first match", Input: "A", - Parser: core.Or(core.Rune('A'), core.Rune('B')), - ExpectedMatch: core.NewTuple2[core.Match[string], core.Match[string]](core.NewMatch("A", true), nil), + Parser: Or(Rune('A'), Rune('B')), + ExpectedMatch: NewTuple2[Match[string], Match[string]](NewMatch("A", true), nil), ExpectedOK: true, }, { Name: "second match", Input: "B", - Parser: core.Or(core.Rune('A'), core.Rune('B')), - ExpectedMatch: core.NewTuple2[core.Match[string]](nil, core.NewMatch("B", true)), + Parser: Or(Rune('A'), Rune('B')), + ExpectedMatch: NewTuple2(NewMatch("", false), NewMatch("B", true)), ExpectedOK: true, }, }