Skip to content

Ungrammar implementation and API in Go

License

Notifications You must be signed in to change notification settings

eliben/go-ungrammar

Repository files navigation

go-ungrammar

Ungrammar implementation and API in Go. Blog post for background.

Ungrammar is a DSL for concrete syntax trees (CST). This implementation is based on the original ungrammar crate, also borrowing some test files from it.

Ungrammar syntax

The syntax of Ungrammar files is very simple:

//           -- comment
Name =       -- non-terminal definition
'ident'      -- token (terminal)
A B          -- sequence
A | B        -- alternation
A*           -- repetition (zero or more)
A?           -- optional (zero or one)
(A B)        -- grouping elements for precedence control
label:A      -- label hint for naming

For some concrete examples, look at files in the testdata directory.

Usage

Go Reference

Usage example:

func ExampleParseAndExamine() {
input := `
Foo = Bar Baz
Baz = ( Kay Jay )* | 'id'`
// Create an Ungrammar parser and parse input.
p := ungrammar.NewParser(input)
ungram, err := p.ParseGrammar()
if err != nil {
panic(err)
}
// Display the string representation of the parsed ungrammar.
fmt.Println(ungram.Rules["Foo"].String())
fmt.Println(ungram.Rules["Baz"].String())
// Output:
// Seq(Bar, Baz)
// Alt(Rep(Seq(Kay, Jay)), 'id')
}

For somewhat more sophisticated usage, see the cmd/ungrammar2json command.

About

Ungrammar implementation and API in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages