ebnf

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package ebnf lints the grammar files under docs/.

They are Wirth syntax notation rather than ISO 14977, so no ISO tool reads them. golang.org/x/exp/ebnf does: it documents this exact dialect, including the convention that an upper-case name is a nonterminal and a lower-case one is lexical. Parsing and reachability come from there.

What is here is the check no library makes. A quoted terminal has to be text lex turns into exactly one token, so a spelling the grammar invents is an error rather than a rule that could never match.

Index

Constants

This section is empty.

Variables

View Source
var GrammarOptions = Options{Start: "File", LexSpellings: true, Annotated: true}

GrammarOptions reads docs/grammar.ebnf.

View Source
var NotationOptions = Options{Start: "Grammar"}

NotationOptions reads docs/notation.ebnf, which describes the notation rather than TDL, so its quoted terminals are not TDL tokens.

Functions

func Walk added in v0.1.8

func Walk(expr ebnf.Expression, fn func(ebnf.Expression))

Walk calls fn on expr and then on everything inside it, in source order.

Types

type Annotations added in v0.1.3

type Annotations struct {
	// Word is the production a generator extracts keywords from.
	Word string

	// Extras may appear between any two tokens. Each is a name with no
	// production, bound by a file-level token directive.
	Extras []string

	// Tokens maps a name to the pattern the named lex symbol holds.
	Tokens map[string]string

	// Conflicts are pairs of productions a generator has to consider
	// together.
	Conflicts [][]string

	// Prods holds what is said about each production, by name.
	Prods map[string]ProdAnnotations
	// contains filtered or unexported fields
}

Annotations are what docs/grammar.ebnf says about itself that the notation cannot: which productions are structure and which are plumbing, where a parser generator needs precedence or has to consider two readings, and which lex symbol defines a name the grammar leaves undefined.

They live in comments, which golang.org/x/exp/ebnf drops, so they are scanned separately and attached by position. See docs/design/treesitter.md.

type File added in v0.1.3

type File struct {
	Grammar     ebnf.Grammar
	Annotations Annotations
}

A File is a grammar and what its annotations say about it.

func Read added in v0.1.3

func Read(filename, src string, opts Options) (*File, []error)

Read parses a grammar and its annotations, reporting every problem it finds along the way.

A file that did not parse returns no grammar and its parse errors alone, since nothing about its contents would be worth saying.

func ReadFile added in v0.1.8

func ReadFile(path string, opts Options) (*File, error)

ReadFile is Read over a file on disk, with every problem joined into one error, for a generator that has nothing to do with a broken grammar but report it.

type Options

type Options struct {
	// Start is the production everything must be reachable from.
	Start string

	// LexSpellings requires every quoted terminal to be text lex turns
	// into exactly one token. It holds for a grammar of TDL and not for a
	// grammar of anything else.
	LexSpellings bool

	// Annotated reads the `/*@ ... */` comments and holds the file to
	// them: every production with no expression needs a token binding,
	// and every name an annotation mentions has to exist. A grammar
	// carrying no annotations is not one of these.
	Annotated bool
}

Options say how to read one grammar file.

type ProdAnnotations added in v0.1.3

type ProdAnnotations struct {
	Hidden   bool   // structure the tree without appearing in it
	Inline   bool   // substitute into callers instead
	External bool   // a terminal a hand-written scanner produces
	Assoc    string // "", "left", or "right"
	Prec     int    // meaningful when Assoc is set or Prec is non-zero
	HasPrec  bool
	Token    string // the pattern the named lex symbol holds
}

ProdAnnotations are the annotations on one production.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL