diff options
author | Adam Scarr <adam@vektah.net> | 2017-08-10 21:58:14 +1000 |
---|---|---|
committer | Adam Scarr <adam@vektah.net> | 2017-08-10 22:01:06 +1000 |
commit | a0e66b1c46ec57218f8a95a21ace7cbbceb29ec2 (patch) | |
tree | 630056d07ca6b44f7a747b7872ba422c6c301d85 /readme.md | |
parent | af542eff9e1e51561a9efa37685ee07b1d01b53e (diff) |
Document cuts
Diffstat (limited to 'readme.md')
-rw-r--r-- | readme.md | 23 |
1 files changed, 20 insertions, 3 deletions
@@ -7,9 +7,9 @@ A parser-combinator library for building easy to test, read and maintain parsers I dont have many benchmarks set up yet, but the json parser is very promising. Nearly keeping up with the stdlib for raw speed: ``` $ go test -bench=. -benchtime=2s -benchmem ./json -BenchmarkUnmarshalParsec-8 50000 71447 ns/op 50464 B/op 1318 allocs/op -BenchmarkUnmarshalParsify-8 50000 56414 ns/op 43887 B/op 334 allocs/op -BenchmarkUnmarshalStdlib-8 50000 50187 ns/op 13949 B/op 262 allocs/op +BenchmarkUnmarshalParsec-8 20000 65682 ns/op 50460 B/op 1318 allocs/op +BenchmarkUnmarshalParsify-8 30000 51292 ns/op 45104 B/op 334 allocs/op +BenchmarkUnmarshalStdlib-8 30000 46522 ns/op 13953 B/op 262 allocs/op PASS ok github.com/vektah/goparsify/json 10.840s ``` @@ -198,6 +198,23 @@ func init() { Take a look at [calc](calc/calc.go) for a full example. +### preventing backtracking with cuts +A cut is a marker that prevents backtracking past the point it was set. This greatly improves error messages when used correctly: +```go +alpha := Chars("a-z") + +// without a cut if the close tag is left out the parser will backtrack and ignore the rest of the string +nocut := Many(Any(Seq("<", alpha, ">"), alpha)) +_, err := Run(nocut, "asdf <foo") +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)) +_, err = Run(cut, "asdf <foo") +fmt.Println(err.Error()) +// Outputs: offset 9: expected > +``` ### prior art |