Improve cut godoc
This commit is contained in:
parent
8a92b5348f
commit
945e9f0ef0
@ -164,19 +164,19 @@ func TestBind(t *testing.T) {
|
|||||||
|
|
||||||
func TestCut(t *testing.T) {
|
func TestCut(t *testing.T) {
|
||||||
t.Run("test any", func(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, "offset 4: expected hello", ps.Error.Error())
|
||||||
require.Equal(t, 0, ps.Pos)
|
require.Equal(t, 0, ps.Pos)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("test many", func(t *testing.T) {
|
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, "offset 12: expected >", ps.Error.Error())
|
||||||
require.Equal(t, 0, ps.Pos)
|
require.Equal(t, 0, ps.Pos)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("test maybe", func(t *testing.T) {
|
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, "offset 3: expected hello", ps.Error.Error())
|
||||||
require.Equal(t, 0, ps.Pos)
|
require.Equal(t, 0, ps.Pos)
|
||||||
})
|
})
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package goparsify_test
|
package goparsify
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
. "github.com/vektah/goparsify"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleCuts() {
|
func ExampleCuts() {
|
||||||
@ -14,7 +12,7 @@ func ExampleCuts() {
|
|||||||
fmt.Println(err.Error())
|
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
|
// 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")
|
_, err = Run(cut, "asdf <foo")
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
|
|
||||||
|
@ -42,12 +42,12 @@ var (
|
|||||||
return Result{Result: attr}
|
return Result{Result: attr}
|
||||||
})
|
})
|
||||||
|
|
||||||
tstart = Seq("<", identifier, Cut, attrs, ">")
|
tstart = Seq("<", identifier, Cut(), attrs, ">")
|
||||||
tend = Seq("</", Cut, identifier, ">")
|
tend = Seq("</", Cut(), identifier, ">")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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]
|
openTag := node.Child[0]
|
||||||
return Result{Result: Tag{
|
return Result{Result: Tag{
|
||||||
Name: openTag.Child[1].Token,
|
Name: openTag.Child[1].Token,
|
||||||
|
@ -13,7 +13,7 @@ var (
|
|||||||
_number = NumberLit()
|
_number = NumberLit()
|
||||||
_properties = Some(Seq(StringLit(`"`), ":", &_value), ",")
|
_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{}{}
|
ret := []interface{}{}
|
||||||
for _, child := range n.Child[2].Child {
|
for _, child := range n.Child[2].Child {
|
||||||
ret = append(ret, child.Result)
|
ret = append(ret, child.Result)
|
||||||
@ -21,7 +21,7 @@ var (
|
|||||||
return Result{Result: ret}
|
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{}{}
|
ret := map[string]interface{}{}
|
||||||
|
|
||||||
for _, prop := range n.Child[2].Child {
|
for _, prop := range n.Child[2].Child {
|
||||||
|
@ -109,9 +109,11 @@ func WS() Parser {
|
|||||||
|
|
||||||
// Cut prevents backtracking beyond this point. Usually used after keywords when you
|
// Cut prevents backtracking beyond this point. Usually used after keywords when you
|
||||||
// are sure this is the correct path. Improves performance and error reporting.
|
// are sure this is the correct path. Improves performance and error reporting.
|
||||||
func Cut(ps *State) Result {
|
func Cut() Parser {
|
||||||
ps.Cut = ps.Pos
|
return func(ps *State) Result {
|
||||||
return Result{}
|
ps.Cut = ps.Pos
|
||||||
|
return Result{}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regex returns a match if the regex successfully matches
|
// Regex returns a match if the regex successfully matches
|
||||||
|
@ -215,7 +215,7 @@ fmt.Println(err.Error())
|
|||||||
// Outputs: left unparsed: <foo
|
// 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
|
// 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")
|
_, err = Run(cut, "asdf <foo")
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
// Outputs: offset 9: expected >
|
// Outputs: offset 9: expected >
|
||||||
|
Loading…
Reference in New Issue
Block a user