Grammar
public final class Grammar<L, S, C> : GrammarComponent where L : Lexer, S : Selector, C : ConstructResult, L.Char == C.Char, L.Param == S.Param, L.Result == S.Result, S.Param == C.Param, S.Result == C.Result
A Grammar
describes the syntax of the language to be parsed. Parsing is based on the concept of parameterized local lexing.
A Grammar
consists of a list of rules, a lexer, a selector, and a specification of how to construct the result of a successful parse.
- The
Rule
s basically describe a context-free grammar whose nonterminals and terminals are parameterized by an input and an output parameter, each of typeGrammar.Param
. The computation of these parameters is guided via evaluation functions of typeEvalFunc
. Note that although context-free grammars require the symbol on the left hand side of a rule to be a nonterminal, here we also allow it to be a terminal instead. This enables a form of scannerless parsing. - The
Lexer
component makes it possible to associate a terminal with a custom parser. Note that the syntax of terminals can not only be described via this lexer, but can also be described via rules. - The
Selector
component makes it possible to resolve undesired ambiguities arising between terminals starting at the same position, while also allowing to keep desired or unproblematic ambiguities. - The
ConstructResult
component is a specification of how to construct theGrammar.Result
of a successful parse.
Given a list of rules
, a lexer
, a selector
and result construction specification constructResult
, you create a grammar
via
let grammar = Grammar(rules: rules, lexer: lexer, selector: selector, constructResult: constructResult)
You can then use it for parsing:
let parseResult = grammar.parse(input: input, position: 0, symbol: S, inputParam: param)
Here we parse the symbol S
with input parameter param
from the beginning of the input source input
of type Input
. Note that S
can be either a nonterminal or a terminal.
-
Each nonterminal and terminal has an input and an output parameter of this type associated with them.
Declaration
Swift
public typealias Param = C.Param
-
For a successful parse, optionally a result of this type is computed, as specified per
ConstructResult
.Declaration
Swift
public typealias Result = C.Result
-
The type of characters that inputs must have which can be parsed by this grammar.
Declaration
Swift
public typealias Char = L.Char
-
The lexer of this grammar.
Seealso
Lexer
Declaration
Swift
public let lexer: L
-
The selector of this grammar.
Seealso
Selector
Declaration
Swift
public let selector: S
-
The specification on how to compute a result from a successful parse.
Seealso
ConstructResult
Declaration
Swift
public let constructResult: C
-
Creates a grammar with the given rules, lexer, selector and result construction specification.
Parameters
rules
The rules of the grammar.
lexer
The lexer of this grammar.
selector
The selector of this grammar.
constructResult
A specification of how to construct the result of a successful parse.
-
Parses the given
symbol
associated with input parameterinputParam
from a specifiedposition
ininput
.Declaration
Parameters
input
The input which is being parsed.
position
The position in the input from where to start parsing.
symbol
The start symbol of the parsing process. This can be either a nonterminal or a terminal.
param
The input parameter associated with the start symbol.
terminalParseModes
The terminal modes for parsing terminals via the grammar (does not affect Lexer!)
Return Value
The parse result (see
ParseResult
for a description on how to interpret this).