https://github.com/vektah/goparsify
golang parser-combinator library
https://github.com/vektah/goparsify
ast golang lexer parse parser-combinators
Last synced: 3 months ago
JSON representation
golang parser-combinator library
- Host: GitHub
- URL: https://github.com/vektah/goparsify
- Owner: vektah
- License: mit
- Created: 2017-08-08T13:12:50.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-10-14T01:04:55.000Z (over 2 years ago)
- Last Synced: 2025-03-21T18:21:16.269Z (3 months ago)
- Topics: ast, golang, lexer, parse, parser-combinators
- Language: Go
- Homepage:
- Size: 91.8 KB
- Stars: 75
- Watchers: 5
- Forks: 16
- Open Issues: 4
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
- awesome-list - goparsify - combinator library | vektah | 56 | (Go)
README
goparsify [](https://circleci.com/gh/Vektah/goparsify/tree/master) [](https://godoc.org/github.com/Vektah/goparsify) [](https://goreportcard.com/report/github.com/vektah/goparsify)
=========A parser-combinator library for building easy to test, read and maintain parsers using functional composition.
Everything should be unicode safe by default, but you can opt out of unicode whitespace for a decent ~20% performance boost.
```go
Run(parser, input, ASCIIWhitespace)
```### benchmarks
I dont have many benchmarks set up yet, its pretty quick:
```
$ go test -benchmem -bench=. ./json
BenchmarkUnmarshalParsec-8 20000 74880 ns/op 50846 B/op 1318 allocs/op
BenchmarkUnmarshalParsify-8 30000 50631 ns/op 45055 B/op 233 allocs/op
BenchmarkUnmarshalStdlib-8 30000 46989 ns/op 14210 B/op 260 allocs/op
PASS
ok github.com/vektah/goparsify/json 6.124s
```Most of the remaining small allocs are from putting things in `interface{}` and are pretty unavoidable. https://www.darkcoding.net/software/go-the-price-of-interface/ is a good read.
### debugging parsers
When a parser isnt working as you intended you can build with debugging and enable logging to get a detailed log of exactly what the parser is doing.
1. First build with debug using `-tags debug`
2. enable logging by calling `EnableLogging(os.Stdout)` in your codeThis works great with tests, eg in the goparsify source tree
```
adam:goparsify(master)$ go test -tags debug ./html -v
=== RUN TestParse
html.go:48 | hellohello
hello
hello
hello
hello
hello
hello
hello
found >
html.go:43 | hello]
html.go:24 | hello
html.go:48 || < found <
worldworldworldworldworldworld found >
html.go:20 | color="blue">w | identifier found p
html.go:33 | color="blue">w | attrs {
html.go:32 | color="blue">w | attr {
html.go:20 | ="blue">world | identifier found color
html.go:32 | "blue">world
html.go:43 | world