Documentation
¶
Overview ¶
Package ebnf provides a parser for Extended Backus-Naur Form (EBNF) grammars.
This package supports standard EBNF syntax with modern extensions including regex patterns, PEG-style ordered choice, and hidden tokens.
Basic Usage ¶
Parse a grammar from a string:
grammar, err := ebnf.ParseString(`
expr = term ( "+" term | "-" term )* ;
term = number ;
number = #"[0-9]+" ;
`)
Load a grammar from a file:
grammar, err := ebnf.LoadGrammar("path/to/grammar.ebnf")
Access rules:
rule := grammar.GetRule("expr")
if rule != nil {
fmt.Printf("Rule type: %s\n", ebnf.ExpressionType(rule.Expression))
}
EBNF Syntax Supported ¶
Terminals:
- "text" or 'text' - literal strings
- #"regex" - regular expression patterns
Non-terminals:
- identifier - reference to another rule
Character classes:
- [a-z] - character ranges
- [abc] - character set
- [^abc] - negated character class
Repetition:
- expr* - zero or more
- expr+ - one or more
- expr? - optional
- {expr} - zero or more (alternative syntax)
Choices:
- a | b - unordered choice
- a / b - ordered choice (PEG-style, first match wins)
Grouping:
- (expr) - group expressions
Lookahead:
- !expr - negative lookahead
- &expr - positive lookahead
Hidden tokens (omitted from AST):
- <"token"> - hidden terminal
- <rule> - hidden non-terminal
- <expr> - hidden expression
Comments:
- (* comment *) - C-style comments
Assignment operators:
- =, :=, ::=, <- - all supported
AST Structure ¶
The parser produces a typed AST with the following node types:
- Grammar: Container for all rules
- Rule: A named production rule
- Terminal: Literal string or character
- NonTerminal: Reference to another rule
- Sequence: Concatenation of expressions
- Choice: Unordered alternatives (|)
- OrderedChoice: Ordered alternatives (/)
- Optional: Optional expression (?)
- Repetition: Zero or more (*)
- OneOrMore: One or more (+)
- CharClass: Character class [...]
- Regex: Regular expression #"..."
- Predicate: Negative lookahead (!)
- PositiveLookahead: Positive lookahead (&)
- Hidden: Marks hidden expressions
All AST node types implement the Expression interface.
Examples ¶
See the examples/ directory for complete grammar examples including:
- arithmetic.ebnf - Simple expression grammar
- json.ebnf - Complete JSON grammar
- regex_demo.ebnf - Regex pattern examples
- instaparse_demo.ebnf - PEG-style features
Package ebnf implements a parser for EBNF grammars
Index ¶
- func ExpressionType(expr Expression) string
- func PrintGrammarSummary(g *Grammar)
- type CharClass
- type CharRange
- type Choice
- type Empty
- type Expression
- type Grammar
- type Group
- type Hidden
- type Lexer
- type NonTerminal
- type OneOrMore
- type Optional
- type OrderedChoice
- type Parser
- type PositiveLookahead
- type Predicate
- type Regex
- type Repetition
- type Rule
- type Sequence
- type Terminal
- type Token
- type TokenType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExpressionType ¶
func ExpressionType(expr Expression) string
ExpressionType returns a string describing the type of expression
func PrintGrammarSummary ¶
func PrintGrammarSummary(g *Grammar)
PrintGrammarSummary prints a summary of the grammar
Types ¶
type Expression ¶
type Expression interface {
// contains filtered or unexported methods
}
Expression is the interface for all EBNF expressions
type Grammar ¶
type Grammar struct {
Rules []*Rule
}
func LoadGrammar ¶
LoadGrammar loads and parses an EBNF grammar from a file
Example ¶
package main
import (
"github.com/wbrown/ebnf"
)
func main() {
// Load a grammar from a file
grammar, err := ebnf.LoadGrammar("examples/arithmetic.ebnf")
if err != nil {
panic(err)
}
// Access a specific rule
exprRule := grammar.GetRule("expr")
if exprRule != nil {
// Process the rule...
_ = exprRule
}
}
Output:
func ParseString ¶
ParseString parses an EBNF grammar from a string
Example ¶
package main
import (
"github.com/wbrown/ebnf"
)
func main() {
// Define a simple grammar inline
grammarText := `
greeting = "Hello" <ws> name "!" ;
name = #"[A-Z][a-z]+" ;
ws = #"\s+" ;
`
grammar, err := ebnf.ParseString(grammarText)
if err != nil {
panic(err)
}
// Use the grammar...
_ = grammar
}
Output:
type Group ¶
type Group struct {
Expr Expression
}
Group represents a grouped expression in parentheses
type NonTerminal ¶
NonTerminal represents a reference to another rule
type Optional ¶
type Optional struct {
Expr Expression
}
Optional represents an optional expression (?)
type OrderedChoice ¶
type OrderedChoice struct {
Alternatives []Expression
}
OrderedChoice represents ordered alternatives (/)
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser for EBNF grammars
func (*Parser) ParseGrammar ¶
ParseGrammar parses the entire EBNF grammar
type PositiveLookahead ¶
type PositiveLookahead struct {
Expr Expression
}
PositiveLookahead represents a positive lookahead (&expr)
type Predicate ¶
type Predicate struct {
Expr Expression
}
Predicate represents a negative lookahead (!expr)
type Rule ¶
type Rule struct {
Name string
Expression Expression
Hidden bool // true if rule name is wrapped in < >
}
type Sequence ¶
type Sequence struct {
Elements []Expression
}
Sequence represents concatenation of expressions
type Terminal ¶
type Terminal struct {
Value string
Hidden bool // true if wrapped in < >
CaseInsensitive bool // true for case-insensitive matching ('x'i or "x"i)
}
Terminal represents a literal string or character
type TokenType ¶
type TokenType int
Token types
const ( TokenEOF TokenType = iota TokenIdent TokenString TokenChar TokenStringCI // Case-insensitive string "..."i TokenCharCI // Case-insensitive char '...'i TokenComment TokenEquals TokenPipe TokenSemi TokenLParen TokenRParen TokenLBracket TokenRBracket TokenLBrace TokenRBrace TokenLAngle TokenRAngle TokenPlus TokenStar TokenQuestion TokenExclam TokenComma TokenCaret TokenMinus TokenRegex // New token type for regex literals #"..." TokenSlash // New token type for ordered choice / TokenAmpersand // New token type for positive lookahead & )
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
ebnf-inspect
command
Command ebnf-inspect loads and displays information about EBNF grammar files.
|
Command ebnf-inspect loads and displays information about EBNF grammar files. |
|
ebnf-parse
command
|
|
|
examples
|
|
|
calculator
command
|
|
|
edn
command
Package main demonstrates parsing EDN (Extensible Data Notation) using the EBNF parser framework, with transforms that produce typed Go values.
|
Package main demonstrates parsing EDN (Extensible Data Notation) using the EBNF parser framework, with transforms that produce typed Go values. |
|
markdown
command
|
|