Merge branch 'master' of github.com:/oec/goparsify

This commit is contained in:
Özgür Kesim 2021-05-07 21:52:26 +02:00
commit afd70fad39
2 changed files with 8 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package goparsify
import ( import (
"bytes" "bytes"
"strings"
) )
// Seq matches all of the given parsers in order and returns their result as .Child[n] // 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 startpos := ps.Pos
var longestError Error var longestError Error
expected := []string{}
for _, parser := range parserfied { for _, parser := range parserfied {
parser(ps, node) parser(ps, node)
if ps.Errored() { if ps.Errored() {
if ps.Error.pos >= longestError.pos { if ps.Error.pos >= longestError.pos {
longestError = ps.Error longestError = ps.Error
expected = append(expected, ps.Error.expected)
} }
if ps.Cut > startpos { if ps.Cut > startpos {
break break
@ -68,7 +71,10 @@ func Any(parsers ...Parserish) Parser {
return return
} }
ps.Error = longestError ps.Error = Error{
pos: longestError.pos,
expected: strings.Join(expected, " or "),
}
ps.Pos = startpos ps.Pos = startpos
}) })
} }

View File

@ -3,7 +3,6 @@ package goparsify
import ( import (
"testing" "testing"
"fmt"
"os" "os"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -54,7 +53,7 @@ func TestAny(t *testing.T) {
Seq("hello", "world", "."), Seq("hello", "world", "."),
Seq("hello", "brother"), 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, 11, p2.Error.Pos())
require.Equal(t, 0, p2.Pos) require.Equal(t, 0, p2.Pos)
}) })
@ -80,8 +79,6 @@ func TestAny(t *testing.T) {
t.Run("ab a", func(t *testing.T) { t.Run("ab a", func(t *testing.T) {
node, ps := runParser("ab a", p) node, ps := runParser("ab a", p)
fmt.Println(node)
require.False(t, ps.Errored()) require.False(t, ps.Errored())
require.Equal(t, "ab", node.Child[0].Token) require.Equal(t, "ab", node.Child[0].Token)
require.Equal(t, "a", node.Child[1].Token) require.Equal(t, "a", node.Child[1].Token)