From 390017951eb80a2e1b58dbf9217e464f76dd71b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Kesim?= Date: Tue, 25 Feb 2020 21:56:48 +0100 Subject: [PATCH 1/2] Remove superfluous fmt.Println in test --- combinator_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/combinator_test.go b/combinator_test.go index 17a0c11..40e2492 100644 --- a/combinator_test.go +++ b/combinator_test.go @@ -3,7 +3,6 @@ package goparsify import ( "testing" - "fmt" "os" "github.com/stretchr/testify/require" @@ -80,8 +79,6 @@ func TestAny(t *testing.T) { t.Run("ab a", func(t *testing.T) { node, ps := runParser("ab a", p) - fmt.Println(node) - require.False(t, ps.Errored()) require.Equal(t, "ab", node.Child[0].Token) require.Equal(t, "a", node.Child[1].Token) From e2c58411159b77a6135be3731c4a174f01d9d7a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Kesim?= Date: Tue, 25 Feb 2020 21:59:14 +0100 Subject: [PATCH 2/2] Any(): print all possible values When all of the parsers in Any() fail, instead of printing just the longest error, we print the list of all failed/expected values. The position of the error will still remain that of the longest one. --- combinator.go | 8 +++++++- combinator_test.go | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/combinator.go b/combinator.go index b6b888b..ea75589 100644 --- a/combinator.go +++ b/combinator.go @@ -2,6 +2,7 @@ package goparsify import ( "bytes" + "strings" ) // Seq matches all of the given parsers in order and returns their result as .Child[n] @@ -51,11 +52,13 @@ func Any(parsers ...Parserish) Parser { startpos := ps.Pos var longestError Error + expected := []string{} for _, parser := range parserfied { parser(ps, node) if ps.Errored() { if ps.Error.pos >= longestError.pos { longestError = ps.Error + expected = append(expected, ps.Error.expected) } if ps.Cut > startpos { break @@ -68,7 +71,10 @@ func Any(parsers ...Parserish) Parser { return } - ps.Error = longestError + ps.Error = Error{ + pos: longestError.pos, + expected: strings.Join(expected, " or "), + } ps.Pos = startpos }) } diff --git a/combinator_test.go b/combinator_test.go index 40e2492..7b1be68 100644 --- a/combinator_test.go +++ b/combinator_test.go @@ -53,7 +53,7 @@ func TestAny(t *testing.T) { Seq("hello", "world", "."), Seq("hello", "brother"), )) - require.Equal(t, "offset 11: expected .", p2.Error.Error()) + require.Equal(t, "offset 11: expected nope or .", p2.Error.Error()) require.Equal(t, 11, p2.Error.Pos()) require.Equal(t, 0, p2.Pos) })