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
Rules 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
Lexercomponent 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
Selectorcomponent 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
ConstructResultcomponent is a specification of how to construct theGrammar.Resultof 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
LexerDeclaration
Swift
public let lexer: L -
The selector of this grammar.
Seealso
SelectorDeclaration
Swift
public let selector: S -
The specification on how to compute a result from a successful parse.
Seealso
ConstructResultDeclaration
Swift
public let constructResult: C -
Creates a grammar with the given rules, lexer, selector and result construction specification.
Parameters
rulesThe rules of the grammar.
lexerThe lexer of this grammar.
selectorThe selector of this grammar.
constructResultA specification of how to construct the result of a successful parse.
-
Parses the given
symbolassociated with input parameterinputParamfrom a specifiedpositionininput.Declaration
Parameters
inputThe input which is being parsed.
positionThe position in the input from where to start parsing.
symbolThe start symbol of the parsing process. This can be either a nonterminal or a terminal.
paramThe input parameter associated with the start symbol.
terminalParseModesThe terminal modes for parsing terminals via the grammar (does not affect Lexer!)
Return Value
The parse result (see
ParseResultfor a description on how to interpret this).
Grammar Class Reference