A Universal Dynamic LL(*) Parser
written in and for the Go programming language.
romshark/llparser is a dynamic recursive-descent top-down parser which parses any given input stream by trying to recursively match the root rule. It's universal in that it supports any LL(*) grammar.
This library allows building parsers in Go with relatively good error messages and flexible, even dynamic LL(*) grammars which may mutate at runtime. It parses the input stream into a typed parse-tree and allows action hooks to be executed when a particular rule is matched.
A grammar always begins with a root rule. A rule is a non-terminal symbol. Non-terminals are nodes of the parse-tree that consist of other non-terminals or terminals while terminals are leaf-nodes. A rule consists of a Designation, a Pattern, a Kind and an Action:
mainRule := &llparser.Rule{
Designation: "name of the rule",
Kind: 100,
Pattern: &llparser.Exact{
Kind: 101,
Expectation: []rune("string"),
},
Action: func(f llparser.Fragment) error {
log.Print("the rule was successfuly matched!")
return nil
},
}Designationdefines the optional logical name of the rule and is used for debugging and error reporting purposes.Kinddefines the type identifier of the rule. If this field isn't set then zero (untyped) is used by default.Patterndefines the expected pattern of the rule. This field is required.Actiondefines the optional callback which is executed when this rule is matched. The action callback may return an error which will make the parser stop and fail immediately.
Rules can be nested:
ruleTwo := &llparser.Rule{
Pattern: &llparser.Exact{
Kind: 101,
Expectation: []rune("string"),
},
}
ruleOne := &llparser.Rule{
Pattern: ruleTwo,
}Rules can also recurse:
rule := &llparser.Rule{Kind: 1}
rule.Pattern = llparser.Sequence{
&llparser.Exact{Expectation: []rune("=")},
&llparser.Repeated{
Min: 0,
Max: 1,
Pattern: rule,
}, // potential recursion
}Exact expects a particular sequence of characters to be lexed:
Pattern: &llparser.Exact{
Kind: SomeKindConstant,
Expectation: []rune("some string"),
},Lexed tries to lex an arbitrary sequence of characters according to Fn:
Pattern: