From 945e9f0ef0d9e1e678435d3090d4a742f6682440 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Thu, 10 Aug 2017 22:10:30 +1000 Subject: [PATCH] Improve cut godoc --- combinator_test.go | 6 +++--- examples_test.go | 6 ++---- html/html.go | 6 +++--- json/json.go | 4 ++-- parser.go | 8 +++++--- readme.md | 2 +- 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 "), Chars("a-z")))) + _, ps := runParser("hello "), 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 ") - tend = Seq("") + tstart = Seq("<", identifier, Cut(), attrs, ">") + tend = Seq("") ) 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: "), alpha)) +cut := Many(Any(Seq("<", Cut(), alpha, ">"), alpha)) _, err = Run(cut, "asdf