Improve cut godoc

This commit is contained in:
Adam Scarr 2017-08-10 22:10:30 +10:00
parent 8a92b5348f
commit 945e9f0ef0
6 changed files with 16 additions and 16 deletions

View File

@ -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)
})

View File

@ -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())

View File

@ -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,

View File

@ -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 {

View File

@ -109,10 +109,12 @@ 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 {
func Cut() Parser {
return func(ps *State) Result {
ps.Cut = ps.Pos
return Result{}
}
}
// Regex returns a match if the regex successfully matches
func Regex(pattern string) Parser {

View File

@ -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 >