diff --git a/combinator.go b/combinator.go index 2b6b8a3..1f1317c 100644 --- a/combinator.go +++ b/combinator.go @@ -1,5 +1,10 @@ package parsec +import ( + "bytes" + "fmt" +) + func Nil(p Pointer) (Node, Pointer) { return nil, p } @@ -29,7 +34,7 @@ func And(parsers ...Parserish) Parser { } nodes = append(nodes, node) } - return NewSequence(p.pos, nodes...), newP + return nodes, newP } } @@ -109,6 +114,53 @@ func manyImpl(min int, op Parserish, until Parserish, sep ...Parserish) Parser { break } } - return NewSequence(p.pos, nodes...), newP + return nodes, newP } } + +func Maybe(parser Parserish) Parser { + realParser := Parsify(parser) + + return func(p Pointer) (Node, Pointer) { + node, newP := realParser(p) + if IsError(node) { + return nil, p + } + return node, newP + } +} + +func Map(parser Parserish, f func(n Node) Node) Parser { + p := Parsify(parser) + + return func(ptr Pointer) (Node, Pointer) { + node, newPtr := p(ptr) + if IsError(node) { + return node, ptr + } + + return f(node), newPtr + } +} + +func flatten(n Node) string { + if s, ok := n.(string); ok { + return s + } + + if nodes, ok := n.([]Node); ok { + sbuf := &bytes.Buffer{} + for _, node := range nodes { + sbuf.WriteString(flatten(node)) + } + return sbuf.String() + } + + panic(fmt.Errorf("Dont know how to flatten %t", n)) +} + +func Merge(parser Parserish) Parser { + return Map(parser, func(n Node) Node { + return flatten(n) + }) +} diff --git a/combinator_test.go b/combinator_test.go index 2bec734..efee7ba 100644 --- a/combinator_test.go +++ b/combinator_test.go @@ -19,7 +19,7 @@ func TestAnd(t *testing.T) { t.Run("matches sequence", func(t *testing.T) { node, p2 := And("hello", WS, "world")(p) - require.Equal(t, NewSequence(0, NewToken(0, "hello"), NewToken(6, "world")), node) + require.Equal(t, []Node{"hello", "world"}, node) require.Equal(t, "", p2.Get()) }) @@ -34,12 +34,28 @@ func TestAnd(t *testing.T) { }) } +func TestMaybe(t *testing.T) { + p := Pointer{"hello world", 0} + + t.Run("matches sequence", func(t *testing.T) { + node, p2 := Maybe("hello")(p) + require.Equal(t, "hello", node) + require.Equal(t, " world", p2.Get()) + }) + + t.Run("returns no errors", func(t *testing.T) { + e, p3 := Maybe("world")(p) + require.Equal(t, nil, e) + require.Equal(t, 0, p3.pos) + }) +} + func TestAny(t *testing.T) { p := Pointer{"hello world!", 0} t.Run("Matches any", func(t *testing.T) { node, p2 := Any("hello", "world")(p) - require.Equal(t, NewToken(0, "hello"), node) + require.Equal(t, "hello", node) require.Equal(t, 5, p2.pos) }) @@ -69,40 +85,19 @@ func TestKleene(t *testing.T) { t.Run("Matches sequence with sep", func(t *testing.T) { node, p2 := Kleene(CharRun("abcdefg"), Exact(","))(p) - require.Equal(t, NewSequence(0, - NewToken(0, "a"), - NewToken(2, "b"), - NewToken(4, "c"), - NewToken(6, "d"), - NewToken(8, "e"), - ), node) + require.Equal(t, []Node{"a", "b", "c", "d", "e"}, node) require.Equal(t, 10, p2.pos) }) t.Run("Matches sequence without sep", func(t *testing.T) { node, p2 := Kleene(Any(CharRun("abcdefg"), Exact(",")))(p) - require.Equal(t, NewSequence(0, - NewToken(0, "a"), - NewToken(1, ","), - NewToken(2, "b"), - NewToken(3, ","), - NewToken(4, "c"), - NewToken(5, ","), - NewToken(6, "d"), - NewToken(7, ","), - NewToken(8, "e"), - NewToken(9, ","), - ), node) + require.Equal(t, []Node{"a", ",", "b", ",", "c", ",", "d", ",", "e", ","}, node) require.Equal(t, 10, p2.pos) }) t.Run("Stops on error", func(t *testing.T) { node, p2 := Kleene(CharRun("abc"), Exact(","))(p) - require.Equal(t, NewSequence(0, - NewToken(0, "a"), - NewToken(2, "b"), - NewToken(4, "c"), - ), node) + require.Equal(t, []Node{"a", "b", "c"}, node) require.Equal(t, 6, p2.pos) require.Equal(t, "d,e,", p2.Get()) }) @@ -113,40 +108,19 @@ func TestMany(t *testing.T) { t.Run("Matches sequence with sep", func(t *testing.T) { node, p2 := Many(CharRun("abcdefg"), Exact(","))(p) - require.Equal(t, NewSequence(0, - NewToken(0, "a"), - NewToken(2, "b"), - NewToken(4, "c"), - NewToken(6, "d"), - NewToken(8, "e"), - ), node) + require.Equal(t, []Node{"a", "b", "c", "d", "e"}, node) require.Equal(t, 10, p2.pos) }) t.Run("Matches sequence without sep", func(t *testing.T) { node, p2 := Many(Any(CharRun("abcdefg"), Exact(",")))(p) - require.Equal(t, NewSequence(0, - NewToken(0, "a"), - NewToken(1, ","), - NewToken(2, "b"), - NewToken(3, ","), - NewToken(4, "c"), - NewToken(5, ","), - NewToken(6, "d"), - NewToken(7, ","), - NewToken(8, "e"), - NewToken(9, ","), - ), node) + require.Equal(t, []Node{"a", ",", "b", ",", "c", ",", "d", ",", "e", ","}, node) require.Equal(t, 10, p2.pos) }) t.Run("Stops on error", func(t *testing.T) { node, p2 := Many(CharRun("abc"), Exact(","))(p) - require.Equal(t, NewSequence(0, - NewToken(0, "a"), - NewToken(2, "b"), - NewToken(4, "c"), - ), node) + require.Equal(t, []Node{"a", "b", "c"}, node) require.Equal(t, 6, p2.pos) require.Equal(t, "d,e,", p2.Get()) }) @@ -164,24 +138,13 @@ func TestKleeneUntil(t *testing.T) { t.Run("Matches sequence with sep", func(t *testing.T) { node, p2 := KleeneUntil(CharRun("abcde"), CharRun("d"), Exact(","))(p) - require.Equal(t, NewSequence(0, - NewToken(0, "a"), - NewToken(2, "b"), - NewToken(4, "c"), - ), node) + require.Equal(t, []Node{"a", "b", "c"}, node) require.Equal(t, 6, p2.pos) }) t.Run("Breaks if separator does not match", func(t *testing.T) { node, p2 := KleeneUntil(Char("abcdefg"), Char("y"), Exact(","))(p) - require.Equal(t, NewSequence(0, - NewToken(0, "a"), - NewToken(2, "b"), - NewToken(4, "c"), - NewToken(6, "d"), - NewToken(8, "e"), - NewToken(10, "f"), - ), node) + require.Equal(t, []Node{"a", "b", "c", "d", "e", "f"}, node) require.Equal(t, 11, p2.pos) }) } @@ -191,11 +154,7 @@ func TestManyUntil(t *testing.T) { t.Run("Matches sequence until", func(t *testing.T) { node, p2 := ManyUntil(CharRun("abcdefg"), Char("d"), Exact(","))(p) - require.Equal(t, NewSequence(0, - NewToken(0, "a"), - NewToken(2, "b"), - NewToken(4, "c"), - ), node) + require.Equal(t, []Node{"a", "b", "c"}, node) require.Equal(t, 6, p2.pos) }) @@ -207,6 +166,48 @@ func TestManyUntil(t *testing.T) { }) } +type htmlTag struct { + Name string +} + +func TestMap(t *testing.T) { + parser := Map(And("<", Range("a-zA-Z0-9"), ">"), func(n Node) Node { + return htmlTag{n.([]Node)[1].(string)} + }) + + t.Run("sucess", func(t *testing.T) { + result, _ := parser(Pointer{"", 0}) + require.Equal(t, htmlTag{"html"}, result) + }) + + t.Run("error", func(t *testing.T) { + result, ptr := parser(Pointer{""), result) + require.Equal(t, 0, ptr.pos) + }) +} + +func TestMerge(t *testing.T) { + var bracer Parser + bracer = And("(", Maybe(&bracer), ")") + parser := Merge(bracer) + + t.Run("sucess", func(t *testing.T) { + result, _ := parser(Pointer{"((()))", 0}) + require.Equal(t, "((()))", result) + }) + + t.Run("error", func(t *testing.T) { + result, ptr := parser(Pointer{"((())", 0}) + require.Equal(t, NewError(5, "Expected )"), result) + require.Equal(t, 0, ptr.pos) + }) + + require.Panics(t, func() { + flatten(1) + }) +} + func assertNilParser(t *testing.T, parser Parser) { p := Pointer{"fff", 0} node, p2 := parser(p) diff --git a/examples/html.go b/examples/html.go deleted file mode 100644 index d1a290a..0000000 --- a/examples/html.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "fmt" - - . "github.com/vektah/goparsify" -) - -func html(p Pointer) (Node, Pointer) { - identifier := And(Range("a-z", 1, 1), Range("a-zA-Z0-9")) - text := CharRunUntil("<>") - - var tag Parser - - element := Any(text, &tag) - elements := Kleene(element) - //attr := And(identifier, equal, String()) - attr := And(identifier, "=", `"test"`) - attrws := And(attr, WS) - attrs := Kleene(attrws) - tstart := And("<", identifier, attrs, ">") - tend := And("", identifier, ">") - tag = And(tstart, elements, tend) - - return element(p) -} - -func main() { - result, _, err := ParseString(html, "