Enhanced C#
Language of your choice: library documentation
|
Suggested base class for custom code generators. Each derived class is typically designed for a different kind of token. More...
Suggested base class for custom code generators. Each derived class is typically designed for a different kind of token.
LLPG comes with two derived classes, IntStreamCodeGenHelper for parsing input streams of characters or integers, and GeneralCodeGenHelper for parsing other streams. This class contains common code used by both, for example:
LA0
and LA(n)
, the default error branch, and switch statements;Properties | |
LNode | InputSource [get, set] |
Specifies an object or class on which LLLPG APIs such as Match() and LA() should be called. More... | |
LNode | InputClass [get, set] |
Specifies a class or namespace to use when calling static functions. There is only one currently: NewSet(), which applies only to . More... | |
LNode | TerminalType [get, set] |
The type returned from Match() methods. More... | |
LNode | ListType [get, set] |
Gets or sets the type of lists created with the +: operator (default: List<T>). The identifier "T" should appear in the expression; it will be replaced with the type of items in the list. More... | |
LNode | ListInitializer [get, set] |
Gets or sets the initializer expression for lists created with the +: operator (default: new List<T>()). The identifier "T" should appear in the expression; it will be replaced with the type of items in the list. More... | |
abstract IPGTerminalSet | EmptySet [get] |
Properties inherited from Loyc.LLParserGenerator.IPGCodeGenHelper | |
IPGTerminalSet | EmptySet [get] |
Returns an empty set of the appropriate type for the kind of parser being generated by this code. More... | |
LNode | TerminalType [get] |
Type of variables auto-declared when you use labels in your grammar (e.g. x:Foo (list+:Bar)*) More... | |
Public Member Functions | |
void | SetListInitializer (LNode varDecl) |
Sets ListType and/or ListInitializer based on an expression. A statement like Type x = expr sets ListType = Type and ListInitializer = expr ; A statement like Type x just sets ListType = Type ; and any other expression expr sets ListInitializer = expr . More... | |
LNode | ResolveAlias (LNode expr) |
Returns the node for an alias. If the specified node is not an alias, returns the same node unchanged. More... | |
virtual LNode | VisitInput (LNode stmt, IMessageSink sink) |
In case the IPGCodeGenHelper is interested, the LLLPG macro calls this method on each statement in the body of the macro (as a preprocessing step, before LLLPG looks at it). No action is required. More... | |
abstract Pred | CodeToTerminalPred (LNode expr, ref string errorMsg) |
Creates a terminal predicate from a code expression. More... | |
virtual IPGTerminalSet | Optimize (IPGTerminalSet set, IPGTerminalSet dontcare) |
Simplifies the specified set, if possible, so that GenerateTest() can generate simpler code for an if-else chain in a prediction tree. More... | |
virtual char | ExampleChar (IPGTerminalSet set) |
Returns an example of a character in the set, or null if this is not a set of characters or if EOF is the only member of the set. More... | |
abstract string | Example (IPGTerminalSet set) |
Returns an example of an item in the set. If the example is a character, it should be surrounded by single quotes. More... | |
virtual void | Begin (WList< LNode > classBody, ISourceFile sourceFile) |
Before the parser generator generates code, it calls this method. More... | |
virtual void | BeginRule (Rule rule) |
Notifies the snippet generator that code generation is starting for a new rule. More... | |
virtual void | Done () |
LLParserGenerator calls this method to notify the snippet generator that code generation is complete. More... | |
virtual LNode | GenerateTest (IPGTerminalSet set, LNode laVar) |
Generates code to test whether the terminal denoted 'laVar' is in the set. More... | |
virtual LNode | GenerateSkip (bool savingResult) |
Returns (Skip()) , or (MatchAny()) if the result is to be saved. More... | |
virtual LNode | GenerateAndPredCheck (AndPred andPred, LNode code, int li) |
Generate code to check an and-predicate during or after prediction, e.g. &!{foo} becomes !(foo) during prediction and Check(!(foo)); afterward. More... | |
virtual LNode | GenerateMatch (IPGTerminalSet set, bool savingResult, bool recognizerMode) |
Generate code to match a set, e.g. MatchRange('a', 'z'); or MatchExcept(' . If the set is too complex, a declaration for it is created in the classBody which was passed to Begin() . More... | |
Public Member Functions inherited from Loyc.LLParserGenerator.IPGCodeGenHelper |
Protected Member Functions | |
abstract LNode | GenerateTest (IPGTerminalSet set, LNode subject, Symbol setName) |
Generates code to test whether a terminal is in the set. More... | |
virtual Symbol | GenerateSetName (Rule currentRule) |
virtual Symbol | GenerateSetDecl (IPGTerminalSet set) |
abstract LNode | GenerateSetDecl (IPGTerminalSet set, Symbol setName) |
Generates a declaration for a variable that holds the set. More... | |
Protected fields | |
int | _setNameCounter = 0 |
LNodeFactory | F |
WList< LNode > | _classBody |
Rule | _currentRule |
Dictionary< LNode, LNode > | _definedAliases = new Dictionary<LNode, LNode>() |
Protected static fields | |
static readonly Symbol | _Skip = GSymbol.Get("Skip") |
static readonly Symbol | _MatchAny = GSymbol.Get("MatchAny") |
static readonly Symbol | _Match = GSymbol.Get("Match") |
static readonly Symbol | _MatchExcept = GSymbol.Get("MatchExcept") |
static readonly Symbol | _MatchRange = GSymbol.Get("MatchRange") |
static readonly Symbol | _MatchExceptRange = GSymbol.Get("MatchExceptRange") |
static readonly Symbol | _TryMatch = GSymbol.Get("TryMatch") |
static readonly Symbol | _TryMatchExcept = GSymbol.Get("TryMatchExcept") |
static readonly Symbol | _TryMatchRange = GSymbol.Get("TryMatchRange") |
static readonly Symbol | _TryMatchExceptRange = GSymbol.Get("TryMatchExceptRange") |
static readonly Symbol | _LA = GSymbol.Get("LA") |
static readonly Symbol | _LA0 = GSymbol.Get("LA0") |
static readonly Symbol | _Check = GSymbol.Get("Check") |
static readonly Symbol | _Error = GSymbol.Get("Error") |
static readonly Symbol | _underscore = GSymbol.Get("_") |
static readonly Symbol | _alias = GSymbol.Get("alias") |
static readonly Symbol | _T = GSymbol.Get("T") |
virtual int | BaseCostForSwitch [get] |
Used to help decide whether a "switch" or an if-else chain will be used for prediction. This is the starting cost of a switch (the starting cost of an if-else chain is set to zero). More... | |
abstract LNode | GenerateMatchExpr (IPGTerminalSet set, bool savingResult, bool recognizerMode) |
Generate code to match a set, e.g. MatchRange('a', 'z'); or MatchExcept(' . If the set is too complex, a declaration for it is created in classBody. More... | |
virtual LNode | LA (int k) |
Generates code to read LA(k). More... | |
virtual LNode | ErrorBranch (IPGTerminalSet covered, int laIndex) |
Generates code for the default error branch of prediction (called when there is no explicit error branch). More... | |
abstract LNode | LAType () |
Returns the data type of LA(k) More... | |
virtual bool | ShouldGenerateSwitch (IPGTerminalSet[] sets, MSet< int > casesToInclude, bool hasErrorBranch) |
Decides whether to use a switch() and for which cases, using BaseCostForSwitch and GetRelativeCostForSwitch. More... | |
virtual LNode | GenerateSwitch (IPGTerminalSet[] branchSets, MSet< int > casesToInclude, LNode[] branchCode, LNode defaultBranch, LNode laVar) |
Generates a switch statement with the specified branches where branchCode[i] is the code to run if the input is in the set branchSets[i]. More... | |
virtual LNode | CreateRuleMethod (Rule rule, VList< LNode > methodBody) |
Generates the method for a rule, given the method's contents. More... | |
LNode | CreateTryWrapperForRecognizer (Rule rule) |
See IPGCodeGenHelper.CreateTryWrapperForRecognizer for more information. More... | |
virtual LNode | CallRule (RuleRef rref, bool recognizerMode) |
Generates code to call a rule based on rref.Rule.Name and rref.Params . More... | |
virtual LNode | CallTryRecognizer (RuleRef rref, int lookahead) |
Generates a call to the Try_Scan_*() function that wraps around a Scan_*() recognizer. Called while generating code for an and-pred. More... | |
virtual LNode | GetListType (LNode type) |
Gets the list type for elements of the specified type (e.g. List<type>) More... | |
virtual LNode | MakeInitializedVarDecl (LNode type, bool wantList, Symbol varName) |
Gets a variable declaration for the specified type, e.g. if type is Foo and wantList == true and varName.Name == "x" , the statement returned might be List<Foo> x = new List<Foo>(); More... | |
virtual int | GetRelativeCostForSwitch (IPGTerminalSet set) |
Used to help decide whether a "switch" or an if statement will be used to handle a prediction tree, and if so which branches. This method should calculate the "cost of switch" (which generally represents a code size penalty, as there is a separate case for every element of the set) and the "cost of if" (which generally represents a speed penalty) and return the difference (so that positive numbers favor "switch" and negative numbers favor "if".) More... | |
virtual IEnumerable< LNode > | GetCases (IPGTerminalSet set) |
Gets the literals or symbols to use for switch cases of a set (just the values, not including the case labels.) More... | |
virtual LNode | ApiCall (Symbol apiName, params LNode[] args) |
Returns an LNode representing a call to the specified LLLPG API. For example, if the user used a "inputSource=input" option, then ApiCall(_Match, F.Literal('7')) would generate a node that represents input.Match('7') . More... | |
virtual LNode | ApiCall (Symbol apiName, IEnumerable< LNode > args, bool isStatic=false) |
Returns an LNode representing a call to the specified LLLPG API. For example, if the user used a "inputSource=input" option, then ApiCall(_Match, F.Literal('7')) would generate a node that represents input.Match('7') . More... | |
virtual LNode | ApiType (LNode typeName) |
virtual LNode | DefaultOf (LNode type, bool wantList) |
static bool | EndMayBeReachable (LNode stmt) |
|
inlineprotectedvirtual |
Returns an LNode representing a call to the specified LLLPG API. For example, if the user used a "inputSource=input" option, then ApiCall(_Match, F.Literal('7'))
would generate a node that represents input.Match('7')
.
|
inlineprotectedvirtual |
Returns an LNode representing a call to the specified LLLPG API. For example, if the user used a "inputSource=input" option, then ApiCall(_Match, F.Literal('7'))
would generate a node that represents input.Match('7')
.
args | Parameters to the API call, or null to access a property or field. |
|
inlinevirtual |
Before the parser generator generates code, it calls this method.
classBody | the body (braced block) of the class where the code will be generated, which allows the snippet generator to add code at class level when needed. |
sourceFile | the suggested ISourceFile to assign to generated code snippets. |
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
Notifies the snippet generator that code generation is starting for a new rule.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
|
inlinevirtual |
Generates code to call a rule based on rref.Rule.Name
and rref.Params
.
For a normal rule call, this method should return rref.AutoSaveResult(code)
where code
is the code to invoke the rule.
Recognizer mode is normally implemented by calling the recognizer version of the rule in an "if" statement: if (!Scan_Foo()) return false;
Backtrack mode expects a boolean expression to be returned, normally something like Try_Scan_Foo()
where the name Try_Is_Foo
comes from the recognizer's Rule.TryWrapperName.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
References Loyc.Syntax.LNode.Args, Loyc.LLParserGenerator.Rule.Basis, and Loyc.Syntax.CodeSymbols.Return.
|
inlinevirtual |
Generates a call to the Try_Scan_*() function that wraps around a Scan_*() recognizer. Called while generating code for an and-pred.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
|
pure virtual |
Creates a terminal predicate from a code expression.
expr | A expression provided by the user, such as "a string" , a Token.Type , or a value..range . expr will not be a call to the inversion operator #~ (that's handled internally using IPGTerminalSet.Inverted()). This method also handles the "any token" input, which is an underscore by convention (_). |
errorMsg | An error message to display. If the method returns null, the LLLPG macro shows this as an error; if this method does not return null, the message (if provided) is shown as a warning. |
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
Implemented in Loyc.LLParserGenerator.GeneralCodeGenHelper, and Loyc.LLParserGenerator.IntStreamCodeGenHelper.
|
inlinevirtual |
Generates the method for a rule, given the method's contents.
rule | Rule for which a method is needed. |
methodBody | A list of statements produced by LLParserGenerator inside the method. |
To generate the default method, simply call Rule.CreateMethod(VList{LNode}).
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
See IPGCodeGenHelper.CreateTryWrapperForRecognizer for more information.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
|
inlinevirtual |
LLParserGenerator calls this method to notify the snippet generator that code generation is complete.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
|
inlinevirtual |
Generates code for the default error branch of prediction (called when there is no explicit error branch).
covered | The permitted token set, which the input did not match. NOTE: if the input matched but there were and-predicates that did not match, this parameter will be null (e.g. the input is 'b' in (&{x} 'a' | &{y} 'b') , but y is false. |
laIndex | Location of unexpected input, relative to current position. |
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
|
pure virtual |
Returns an example of an item in the set. If the example is a character, it should be surrounded by single quotes.
This helps produce error messages in LLLPG.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
Implemented in Loyc.LLParserGenerator.IntStreamCodeGenHelper, and Loyc.LLParserGenerator.GeneralCodeGenHelper.
|
inlinevirtual |
Returns an example of a character in the set, or null if this is not a set of characters or if EOF is the only member of the set.
This helps produce error messages in LLLPG.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
Reimplemented in Loyc.LLParserGenerator.IntStreamCodeGenHelper.
|
inlinevirtual |
Generate code to check an and-predicate during or after prediction, e.g. &!{foo} becomes !(foo) during prediction and Check(!(foo)); afterward.
andPred | Predicate for which an expression has already been generated |
code | The expression to be checked |
li | Current lookahead amount. -1 means "prediction is complete, generate a Check() statement". |
LLLPG substitutes $LI and $LA before it calls this method.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
References Loyc.LLParserGenerator.AndPred.Not, and Loyc.LLParserGenerator.AndPred.Pred.
|
inlinevirtual |
Generate code to match a set, e.g. MatchRange('a', 'z');
or MatchExcept('
. If the set is too complex, a declaration for it is created in the
', ''); }classBody
which was passed to Begin()
.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
References Loyc.Syntax.CodeSymbols.Return.
|
pure virtual |
Generate code to match a set, e.g. MatchRange('a', 'z');
or MatchExcept('
. If the set is too complex, a declaration for it is created in classBody.
', ''); }
Implemented in Loyc.LLParserGenerator.GeneralCodeGenHelper, and Loyc.LLParserGenerator.IntStreamCodeGenHelper.
|
protectedpure virtual |
Generates a declaration for a variable that holds the set.
For example, if setName is foo, a set such as [aeiouy] might use an external declaration such as
Implemented in Loyc.LLParserGenerator.GeneralCodeGenHelper, and Loyc.LLParserGenerator.IntStreamCodeGenHelper.
|
inlinevirtual |
Returns (Skip())
, or (MatchAny())
if the result is to be saved.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
|
inlinevirtual |
Generates a switch statement with the specified branches where branchCode[i] is the code to run if the input is in the set branchSets[i].
casesToInclude | The set chosen by ShouldGenerateSwitch. |
defaultBranch | Code to be placed in the default: case (if none, the blank stmt @``; ) |
laVar | The lookahead variable being switched on (e.g. la0) |
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
References Loyc.Syntax.CodeSymbols.Missing.
|
inlinevirtual |
Generates code to test whether the terminal denoted 'laVar' is in the set.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
|
protectedpure virtual |
Generates code to test whether a terminal is in the set.
subject | Represents the variable to be tested. |
setName | Names an external set variable to use for the test. |
(la0 >= '0' && '9' >= la0)
, or null if an external setName is needed and was not provided.At first, LLParserGenerator calls this method with setName == null
. If it returns null, it calls the method a second time, giving the name of an external variable in which the set is held (see GenerateSetDecl(IPGTerminalSet)).
For example, if the subject is , the test for a simple set like [a-z?] might be something like (la0 >= 'a' && 'z' >= la0) || la0 == '?'
. When the setName is foo
, the test might be foo.Contains(la0)
instead.
Implemented in Loyc.LLParserGenerator.IntStreamCodeGenHelper, and Loyc.LLParserGenerator.GeneralCodeGenHelper.
|
inlineprotectedvirtual |
Gets the literals or symbols to use for switch cases of a set (just the values, not including the case labels.)
Reimplemented in Loyc.LLParserGenerator.GeneralCodeGenHelper, and Loyc.LLParserGenerator.IntStreamCodeGenHelper.
Gets the list type for elements of the specified type (e.g. List<type>)
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
|
inlineprotectedvirtual |
Used to help decide whether a "switch" or an if statement will be used to handle a prediction tree, and if so which branches. This method should calculate the "cost of switch" (which generally represents a code size penalty, as there is a separate case for every element of the set) and the "cost of if" (which generally represents a speed penalty) and return the difference (so that positive numbers favor "switch" and negative numbers favor "if".)
If the set is inverted, return a something like -1000000 to ensure 'switch' is not used for that set.
Reimplemented in Loyc.LLParserGenerator.GeneralCodeGenHelper, and Loyc.LLParserGenerator.IntStreamCodeGenHelper.
|
inlinevirtual |
Generates code to read LA(k).
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
Reimplemented in Loyc.LLParserGenerator.GeneralCodeGenHelper.
|
pure virtual |
Returns the data type of LA(k)
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
Implemented in Loyc.LLParserGenerator.GeneralCodeGenHelper, and Loyc.LLParserGenerator.IntStreamCodeGenHelper.
|
inlinevirtual |
Gets a variable declaration for the specified type, e.g. if type is Foo
and wantList == true
and varName.Name == "x"
, the statement returned might be List<Foo> x = new List<Foo>();
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
|
inlinevirtual |
Simplifies the specified set, if possible, so that GenerateTest() can generate simpler code for an if-else chain in a prediction tree.
set | |
dontcare | A set of terminals that have been ruled out, i.e. it is already known that the lookahead value is not in this set. |
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
Reimplemented in Loyc.LLParserGenerator.IntStreamCodeGenHelper.
Returns the node for an alias. If the specified node is not an alias, returns the same node unchanged.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
Sets ListType and/or ListInitializer based on an expression. A statement like Type x = expr
sets ListType = Type
and ListInitializer = expr
; A statement like Type x
just sets ListType = Type
; and any other expression expr
sets ListInitializer = expr
.
References Loyc.Syntax.LNode.Args, Loyc.Syntax.CodeSymbols.Assign, and Loyc.Syntax.CodeSymbols.Var.
|
inlinevirtual |
Decides whether to use a switch() and for which cases, using BaseCostForSwitch and GetRelativeCostForSwitch.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
|
inlinevirtual |
In case the IPGCodeGenHelper is interested, the LLLPG macro calls this method on each statement in the body of the macro (as a preprocessing step, before LLLPG looks at it). No action is required.
Implements Loyc.LLParserGenerator.IPGCodeGenHelper.
References Loyc.Syntax.CodeSymbols.Assign.
|
getprotected |
Used to help decide whether a "switch" or an if-else chain will be used for prediction. This is the starting cost of a switch (the starting cost of an if-else chain is set to zero).
|
getset |
Specifies a class or namespace to use when calling static functions. There is only one currently: NewSet(), which applies only to .
|
getset |
Specifies an object or class on which LLLPG APIs such as Match() and LA() should be called.
|
getset |
Gets or sets the initializer expression for lists created with the +: operator (default: new List<T>()). The identifier "T" should appear in the expression; it will be replaced with the type of items in the list.
|
getset |
Gets or sets the type of lists created with the +: operator (default: List<T>). The identifier "T" should appear in the expression; it will be replaced with the type of items in the list.
|
getset |
The type returned from Match() methods.