summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Scarr <adam@vektah.net>2017-08-10 22:10:30 +1000
committerAdam Scarr <adam@vektah.net>2017-08-10 22:10:30 +1000
commit945e9f0ef0d9e1e678435d3090d4a742f6682440 (patch)
tree18d97b26e44715cbf35c0dc7ec98ad6f3f2df95b
parent8a92b5348f8cf6769f30237df906ee9ce71ad237 (diff)
Improve cut godoc
-rw-r--r--combinator_test.go6
-rw-r--r--examples_test.go6
-rw-r--r--html/html.go6
-rw-r--r--json/json.go4
-rw-r--r--parser.go8
-rw-r--r--readme.md2
6 files changed, 16 insertions, 16 deletions
diff --git a/combinator_test.go b/combinator_test.go
index acf0e84..9dbf8e5 100644
--- a/combinator_test.go
+++ b/combinator_test.go
@@ -164,19 +164,19 @@ func TestBind(t *testing.T) {
func TestCut(t *testing.T) {
t.Run("test any", func(t *testing.T) {
- _, ps := runParser("var world", Any(Seq("var", Cut, "hello"), "var world"))
+ _, ps := runParser("var world", Any(Seq("var", Cut(), "hello"), "var world"))
require.Equal(t, "offset 4: expected hello", ps.Error.Error())
require.Equal(t, 0, ps.Pos)
})
t.Run("test many", func(t *testing.T) {
- _, ps := runParser("hello <world", Many(Any(Seq("<", Cut, Chars("a-z"), ">"), Chars("a-z"))))
+ _, ps := runParser("hello <world", Many(Any(Seq("<", Cut(), Chars("a-z"), ">"), Chars("a-z"))))
require.Equal(t, "offset 12: expected >", ps.Error.Error())
require.Equal(t, 0, ps.Pos)
})
t.Run("test maybe", func(t *testing.T) {
- _, ps := runParser("var", Maybe(Seq("var", Cut, "hello")))
+ _, ps := runParser("var", Maybe(Seq("var", Cut(), "hello")))
require.Equal(t, "offset 3: expected hello", ps.Error.Error())
require.Equal(t, 0, ps.Pos)
})
diff --git a/examples_test.go b/examples_test.go
index a1de129..b4a8086 100644
--- a/examples_test.go
+++ b/examples_test.go
@@ -1,9 +1,7 @@
-package goparsify_test
+package goparsify
import (
"fmt"
-
- . "github.com/vektah/goparsify"
)
func ExampleCuts() {
@@ -14,7 +12,7 @@ func ExampleCuts() {
fmt.Println(err.Error())
// with a cut, once we see the open tag we know there must be a close tag that matches it, so the parser will error
- cut := Many(Any(Seq("<", Cut, alpha, ">"), alpha))
+ cut := Many(Any(Seq("<", Cut(), alpha, ">"), alpha))
_, err = Run(cut, "asdf <foo")
fmt.Println(err.Error())
diff --git a/html/html.go b/html/html.go
index e2dff0b..4e2197d 100644
--- a/html/html.go
+++ b/html/html.go
@@ -42,12 +42,12 @@ var (
return Result{Result: attr}
})
- tstart = Seq("<", identifier, Cut, attrs, ">")
- tend = Seq("</", Cut, identifier, ">")
+ tstart = Seq("<", identifier, Cut(), attrs, ">")
+ tend = Seq("</", Cut(), identifier, ">")
)
func init() {
- tag = Map(Seq(tstart, Cut, elements, tend), func(node Result) Result {
+ tag = Map(Seq(tstart, Cut(), elements, tend), func(node Result) Result {
openTag := node.Child[0]
return Result{Result: Tag{
Name: openTag.Child[1].Token,
diff --git a/json/json.go b/json/json.go
index 8129a8a..52166f8 100644
--- a/json/json.go
+++ b/json/json.go
@@ -13,7 +13,7 @@ var (
_number = NumberLit()
_properties = Some(Seq(StringLit(`"`), ":", &_value), ",")
- _array = Map(Seq("[", Cut, Some(&_value, ","), "]"), func(n Result) Result {
+ _array = Map(Seq("[", Cut(), Some(&_value, ","), "]"), func(n Result) Result {
ret := []interface{}{}
for _, child := range n.Child[2].Child {
ret = append(ret, child.Result)
@@ -21,7 +21,7 @@ var (
return Result{Result: ret}
})
- _object = Map(Seq("{", Cut, _properties, "}"), func(n Result) Result {
+ _object = Map(Seq("{", Cut(), _properties, "}"), func(n Result) Result {
ret := map[string]interface{}{}
for _, prop := range n.Child[2].Child {
diff --git a/parser.go b/parser.go
index 12bb858..bc65e6d 100644
--- a/parser.go
+++ b/parser.go
@@ -109,9 +109,11 @@ func WS() Parser {
// Cut prevents backtracking beyond this point. Usually used after keywords when you
// are sure this is the correct path. Improves performance and error reporting.
-func Cut(ps *State) Result {
- ps.Cut = ps.Pos
- return Result{}
+func Cut() Parser {
+ return func(ps *State) Result {
+ ps.Cut = ps.Pos
+ return Result{}
+ }
}
// Regex returns a match if the regex successfully matches
diff --git a/readme.md b/readme.md
index 5d8e78b..87b0f16 100644
--- a/readme.md
+++ b/readme.md
@@ -215,7 +215,7 @@ fmt.Println(err.Error())
// Outputs: left unparsed: <foo
// with a cut, once we see the open tag we know there must be a close tag that matches it, so the parser will error
-cut := Many(Any(Seq("<", Cut, alpha, ">"), alpha))
+cut := Many(Any(Seq("<", Cut(), alpha, ">"), alpha))
_, err = Run(cut, "asdf <foo")
fmt.Println(err.Error())
// Outputs: offset 9: expected >