Documentation ¶
Overview ¶
Package llx is a general-purpose LL(*) parser library.
Consists of subpackages:
- cmd/llxgen: console utility converting grammar description to Go source file containing grammar definition structure;
- grammar: defines structure that contains definition of lexemes and finite state machine used by parser;
- langdef: converts grammar description (written in EBNF-like language) to grammar definition;
- lexer: lexical analyzer;
- parser: defines parser;
- source: defines source file and source queue used by lexer;
- tree: types and functions to create, traverse, and modify syntax trees.
Typical usage is:
1. Describe grammar in EBNF-like language. Description does not contain Go code, the same grammar can be used for different purposes (translators, linters, formatters, etc.).
2. Parse grammar description using either langdef subpackage "on the fly" or llxgen utility to generate Go file.
3. Define hooks to handle tokens and/or syntax tree nodes emitted by parser.
4. Create new parser for desired grammar and feed it source files and hooks.
Example ¶
package main import ( "fmt" "github.com/ava12/llx/langdef" "github.com/ava12/llx/parser" ) func main() { input := ` foo = hello bar = world [sec] baz = [sec.subsec] qux = ! ` grammar := ` $space = /[ \t\r]+/; $nl = /\n/; $op = /[=\[\]]/; $name = /[a-z]+/; $sec-name = /[a-z]+(?:\.[a-z]+)*/; $value = /[^\n]+/; !aside $space; !group $sec-name; !group $value $nl; !group $op $name $nl; config = {section | value | $nl}; section = '[', $sec-name, ']', $nl; value = $name, '=', [$value], $nl; ` configGrammar, e := langdef.ParseString("example grammar", grammar) if e != nil { fmt.Println(e) return } configParser, e := parser.New(configGrammar) if e != nil { panic(e) } result := make(map[string]string) prefix, name, value := "", "", "" hooks := parser.Hooks{Tokens: parser.TokenHooks{ parser.AnyToken: func(t *parser.Token, pc *parser.ParseContext) (emit bool, e error) { switch t.TypeName() { case "sec-name": prefix = t.Text() + "." case "name": name = prefix + t.Text() case "value": value = t.Text() case "nl": if name != "" { result[name] = value name, value = "", "" } } return true, nil }, }} _, e = configParser.ParseString("input", input, &hooks) if e == nil { fmt.Println(result) } else { fmt.Println(e) } }
Output:
Index ¶
Examples ¶
Constants ¶
const ( LangDefErrors = 1 // used by langdef LexicalErrors = 101 // used by lexer SyntaxErrors = 201 // used by parser ParserErrors = 301 // used by parser )
Error classes used by subpackages, each class contains up to 99 error codes:
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error struct { // Code contains non-zero error code. Code int // Message contains non-empty error message including source name and position information if provided. Message string // SourceName contains source name that caused this error or empty string. SourceName string // Line contains line number in source file or 0. Line int // Col contains column number in source file or 0. Col int }
Error is the error type used by llx subpackages.
func FormatError ¶
FormatError creates Error structure with no source and position information. params will be added to error message using fmt.Sprintf function.
func FormatErrorPos ¶
FormatErrorPos creates Error structure with source and position information. pos must not be nil. params will be added to error message using fmt.Sprintf function.
type SourcePos ¶
type SourcePos interface { // SourceName returns source file name or empty string. SourceName() string // Line returns line number or 0. Line() int // Col returns column number or 0. Col() int }
SourcePos is used to retrieve source name and position information when constructing an error; source.Pos and lexer.Token implement this interface.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
llxgen
llxgen is a console utility translating grammar description to Go or JSON file.
|
llxgen is a console utility translating grammar description to Go or JSON file. |
examples
|
|
Package grammar contains data types used by LL(*) parser.
|
Package grammar contains data types used by LL(*) parser. |
internal
|
|
bmap
Package bmap implements basic map with []byte key type.
|
Package bmap implements basic map with []byte key type. |
Package langdef converts textual grammar description to grammar.Grammar structure.
|
Package langdef converts textual grammar description to grammar.Grammar structure. |
Package lexer defines lexical analyzer.
|
Package lexer defines lexical analyzer. |
Package parser defines generic LL(*) parser.
|
Package parser defines generic LL(*) parser. |
Package source defines source file an source queue for parsers.
|
Package source defines source file an source queue for parsers. |
Package tree provides basic functions for building, manipulating, and traversing syntax trees.
|
Package tree provides basic functions for building, manipulating, and traversing syntax trees. |