Documentation
¶
Overview ¶
Package tabnasabnf is an ABNF -> tabnas GrammarSpec compiler for the tabnas parsing engine (github.com/tabnas/parser/go). It is a faithful Go port of the @tabnas/abnf TypeScript package.
Given a small ABNF dialect it produces a function-free (when requested) GrammarSpec that, installed on a tabnas engine, parses inputs in that grammar and builds a {rule, src, kids} AST. It also emits "pure-data" jsonic (recognition / pure specs) and supports user actions.
The package mirrors the TS sources:
- converter.ts -> converter.go (AST, parseAbnf, abnfRules, desugar, core rules, left-recursion elimination, probe-dispatch rewriter, FIRST sets, emitGrammarSpec, token allocation, Abnf entry) and parser_abnf.go (the ABNF-for-ABNF parser grammar).
- compile.ts -> compile.go (AbnfCompile, ToRecognitionSpec, ToPureSpec, ToJsonic, AttachActions, MarkListing).
- abnf.ts -> facade.go (Abnf, ParseAbnf, EmitGrammarSpec, EliminateLeftRecursion, Install — the public facade).
Index ¶
- Constants
- func Abnf(src string, opts *AbnfConvertOptions) (*tabnas.GrammarSpec, error)
- func AbnfCompile(src string, opts *AbnfCompileOptions) (string, error)
- func AttachActionSlots(spec *tabnas.GrammarSpec, refNames []string) error
- func AttachActions(spec *tabnas.GrammarSpec, actions ActionsMap) error
- func EliminateLeftRecursion(grammar *abnfGrammar) *abnfGrammar
- func EmitGrammarSpec(grammar *abnfGrammar, opts *AbnfConvertOptions) (*tabnas.GrammarSpec, error)
- func Install(j *tabnas.Tabnas, src string, opts *AbnfConvertOptions, actions ActionsMap) (*tabnas.GrammarSpec, error)
- func MarkListing(spec *tabnas.GrammarSpec) string
- func ParseAbnf(src string) (*abnfGrammar, error)
- func Plugin(j *tabnas.Tabnas, _ map[string]any) error
- func SpecToData(spec *tabnas.GrammarSpec) map[string]any
- func SpecToJSON(spec *tabnas.GrammarSpec, indent int) string
- func ToJsonic(value any, strict bool, indent int) string
- type AbnfActionError
- type AbnfCompileError
- type AbnfCompileOptions
- type AbnfConvertOptions
- type AbnfParseError
- type ActionFn
- type ActionsMap
Constants ¶
const Version = "0.2.0"
Version is the current version of the module.
Variables ¶
This section is empty.
Functions ¶
func Abnf ¶
func Abnf(src string, opts *AbnfConvertOptions) (*tabnas.GrammarSpec, error)
Abnf converts ABNF source into a tabnas GrammarSpec.
func AbnfCompile ¶
func AbnfCompile(src string, opts *AbnfCompileOptions) (string, error)
AbnfCompile compiles ABNF source into pure-data jsonic text.
func AttachActionSlots ¶
func AttachActionSlots(spec *tabnas.GrammarSpec, refNames []string) error
AttachActionSlots declares user-action slots by name on a pure-data spec, without supplying functions.
func AttachActions ¶
func AttachActions(spec *tabnas.GrammarSpec, actions ActionsMap) error
AttachActions attaches user semantic actions to a spec in place.
func EliminateLeftRecursion ¶
func EliminateLeftRecursion(grammar *abnfGrammar) *abnfGrammar
EliminateLeftRecursion rewrites direct + indirect left recursion via Paull's algorithm (exported to mirror the TS export).
func EmitGrammarSpec ¶
func EmitGrammarSpec(grammar *abnfGrammar, opts *AbnfConvertOptions) (*tabnas.GrammarSpec, error)
EmitGrammarSpec converts an already-parsed grammar into a GrammarSpec.
func Install ¶
func Install(j *tabnas.Tabnas, src string, opts *AbnfConvertOptions, actions ActionsMap) (*tabnas.GrammarSpec, error)
Install converts src and installs the resulting grammar on j. With actions supplied, conversion runs in closure mode with marks and the actions are attached. Mirrors the TS `tn.abnf(src, opts)` callable.
func MarkListing ¶
func MarkListing(spec *tabnas.GrammarSpec) string
MarkListing returns a human-readable listing of compiler-assigned marks.
func ParseAbnf ¶
ParseAbnf parses ABNF source into a grammar AST (exported for callers inspecting productions). Returns *abnfGrammar via the internal type; for external use prefer Abnf.
func Plugin ¶
Plugin is the tabnas Plugin form: it installs a default grammar only if AbnfSource is set via options; primarily Abnf/Install are used directly. Provided for parity with the TS plugin shape.
func SpecToData ¶
func SpecToData(spec *tabnas.GrammarSpec) map[string]any
SpecToData converts a *GrammarSpec into a plain data tree (map/slice/scalar/regexHolder). Action/condition refs are emitted as their `@`-name strings. Closures in spec.Ref are dropped — like the TS CLI's JSON output, which serialises actions as FuncRef strings. Used by the CLI's default (spec-dump) mode.
func SpecToJSON ¶
func SpecToJSON(spec *tabnas.GrammarSpec, indent int) string
SpecToJSON renders a spec as JSON text (the CLI default output).
Types ¶
type AbnfActionError ¶
type AbnfActionError struct{ Message string }
AbnfActionError is raised for a malformed or unresolvable action ref.
func (*AbnfActionError) Error ¶
func (e *AbnfActionError) Error() string
type AbnfCompileError ¶
AbnfCompileError is raised when a grammar can't be compiled to a pure-data spec.
func (*AbnfCompileError) Error ¶
func (e *AbnfCompileError) Error() string
type AbnfCompileOptions ¶
type AbnfCompileOptions struct {
Start string
Tag string
Strict bool
Indent int
Recognition *bool // default true
}
AbnfCompileOptions controls compilation. Mirrors AbnfCompileOptions.
type AbnfConvertOptions ¶
AbnfConvertOptions controls conversion. Mirrors the TS type.
type AbnfParseError ¶
AbnfParseError is raised when the ABNF source itself can't be parsed.
func (*AbnfParseError) Error ¶
func (e *AbnfParseError) Error() string
func (*AbnfParseError) Unwrap ¶
func (e *AbnfParseError) Unwrap() error
type ActionsMap ¶
ActionsMap maps action refs to a function or list of functions.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
tabnas-abnf
command
Command tabnas-abnf converts an ABNF grammar into a tabnas grammar spec (JSON), a pure-data recognition/AST grammar (jsonic text), lists the per-alt action marks, or parses sample inputs against the grammar.
|
Command tabnas-abnf converts an ABNF grammar into a tabnas grammar spec (JSON), a pure-data recognition/AST grammar (jsonic text), lists the per-alt action marks, or parses sample inputs against the grammar. |