TatSu takes grammars in a variation of EBNF and parses text with them: a memoizing (Packrat) PEG parser with left recursion, cut, lookaheads, joins and gathers, named elements, constants, rule parameters and rule inheritance.
This repository is the Go port of TatSu: the module github.com/neogeny/tatsu-go (package
tatsu) compiles TatSu grammars and parses with them, with the results, errors, semantic actions,
model builder and walkers of the original Python library.
g, err := tatsu.Compile(`
start: expr $
expr: expr '+' term | term
term: /\d+/
`)
if err != nil {
log.Fatal(err)
}
ast, err := g.Parse("1 + 2 + 3")
if err != nil {
log.Fatal(err)
}
fmt.Println(tatsu.Repr(ast)) // [['1', '+', '2'], '+', '3']
tatsu.Parse(grammar, text) compiles and parses in one call. Options such as tatsu.Start,
tatsu.WithSemantics, tatsu.AsModel and tatsu.Setting give the keyword arguments of the
original's compile() and parse(). Results are Python values in Go (see the package
documentation): tatsu.Repr prints them as Python's repr() does and tatsu.AsJSONS writes
their JSON.
The grammar language and the behaviour of the parser are documented in docs/. Python code
generation, railroad diagrams and the command-line tools of the original are not part of the port.
go build ./...
go vet ./...
go test ./...
The module uses the standard library only. internal/pyre reproduces CPython's re (the
grammar's patterns are Python regular expressions), internal/pyuni the Unicode tables of CPython
3.12 and internal/pyval Python's repr(), int() and float().
TatSu is Copyright (C) 2017-2026 Juancarlo Añez and is distributed under the license in
LICENSE.