goparsify/examples_test.go

23 lines
592 B
Go
Raw Normal View History

2017-08-10 14:10:30 +02:00
package goparsify
2017-08-10 13:58:14 +02:00
import (
"fmt"
)
2017-08-10 14:22:19 +02:00
func ExampleCut() {
2017-08-10 13:58:14 +02:00
// without a cut if the close tag is left out the parser will backtrack and ignore the rest of the string
alpha := Chars("a-z")
nocut := Many(Any(Seq("<", alpha, ">"), alpha))
_, err := Run(nocut, "asdf <foo")
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
2017-08-10 14:10:30 +02:00
cut := Many(Any(Seq("<", Cut(), alpha, ">"), alpha))
2017-08-10 13:58:14 +02:00
_, err = Run(cut, "asdf <foo")
fmt.Println(err.Error())
// Output:
// left unparsed: <foo
// offset 9: expected >
}