Enhanced C#
Language of your choice: library documentation
Properties | Public Member Functions | Protected Member Functions | Protected fields | Protected static fields | List of all members
Loyc.LLParserGenerator.CodeGenHelperBase Class Referenceabstract

Suggested base class for custom code generators. Each derived class is typically designed for a different kind of token. More...


Source file:
Inheritance diagram for Loyc.LLParserGenerator.CodeGenHelperBase:
Loyc.LLParserGenerator.IPGCodeGenHelper Loyc.LLParserGenerator.GeneralCodeGenHelper Loyc.LLParserGenerator.IntStreamCodeGenHelper

Remarks

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:

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...
 
bool NoCheckByDefault [get, set]
 If true, calls to Check() are suppressed when AndPred.CheckErrorMessage is null. 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('
', '\r'); }
. 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('
', '\r'); }
. 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, LNode[] branchCode, MSet< int > casesToInclude, 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, LNodeList 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< LNodeGetCases (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)
 

Member Function Documentation

◆ ApiCall() [1/2]

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.ApiCall ( Symbol  apiName,
IEnumerable< LNode args,
bool  isStatic = false 
)
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').

Parameters
argsParameters to the API call, or null to access a property or field.

References Loyc.LLParserGenerator.CodeGenHelperBase.InputClass, and Loyc.LLParserGenerator.CodeGenHelperBase.InputSource.

◆ ApiCall() [2/2]

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.ApiCall ( Symbol  apiName,
params LNode[]  args 
)
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').

Referenced by Loyc.LLParserGenerator.CodeGenHelperBase.ErrorBranch(), Loyc.LLParserGenerator.CodeGenHelperBase.GenerateAndPredCheck(), Loyc.LLParserGenerator.IntStreamCodeGenHelper.GenerateMatchExpr(), Loyc.LLParserGenerator.GeneralCodeGenHelper.GenerateMatchExpr(), Loyc.LLParserGenerator.CodeGenHelperBase.GenerateSkip(), and Loyc.LLParserGenerator.CodeGenHelperBase.LA().

◆ Begin()

virtual void Loyc.LLParserGenerator.CodeGenHelperBase.Begin ( WList< LNode classBody,
ISourceFile  sourceFile 
)
inlinevirtual

Before the parser generator generates code, it calls this method.

Parameters
classBodythe 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.
sourceFilethe suggested ISourceFile to assign to generated code snippets.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

◆ BeginRule()

virtual void Loyc.LLParserGenerator.CodeGenHelperBase.BeginRule ( Rule  rule)
inlinevirtual

Notifies the snippet generator that code generation is starting for a new rule.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

◆ CallRule()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.CallRule ( RuleRef  rref,
bool  recognizerMode 
)
inlinevirtual

Generates code to call a rule based on rref.Rule.Name and rref.Params.

Returns

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, Loyc.Syntax.LNode.CallsMin(), Loyc.Syntax.CodeSymbols.If, Loyc.Syntax.CodeSymbols.Not, and Loyc.Syntax.CodeSymbols.Return.

◆ CallTryRecognizer()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.CallTryRecognizer ( RuleRef  rref,
int  lookahead 
)
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.

◆ CodeToTerminalPred()

abstract Pred Loyc.LLParserGenerator.CodeGenHelperBase.CodeToTerminalPred ( LNode  expr,
ref string  errorMsg 
)
pure virtual

Creates a terminal predicate from a code expression.

Parameters
exprA 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 (_).
errorMsgAn 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.
Returns
If successful, a terminal predicate; otherwise null.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

Implemented in Loyc.LLParserGenerator.GeneralCodeGenHelper, and Loyc.LLParserGenerator.IntStreamCodeGenHelper.

◆ CreateRuleMethod()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.CreateRuleMethod ( Rule  rule,
LNodeList  methodBody 
)
inlinevirtual

Generates the method for a rule, given the method's contents.

Parameters
ruleRule for which a method is needed.
methodBodyA list of statements produced by LLParserGenerator inside the method.
Returns
A method definition for the rule.

To generate the default method, simply call Rule.CreateMethod(VList<LNode>).

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

References Loyc.LLParserGenerator.Rule.CreateMethod().

◆ CreateTryWrapperForRecognizer()

LNode Loyc.LLParserGenerator.CodeGenHelperBase.CreateTryWrapperForRecognizer ( Rule  rule)
inline

◆ Done()

virtual void Loyc.LLParserGenerator.CodeGenHelperBase.Done ( )
inlinevirtual

LLParserGenerator calls this method to notify the snippet generator that code generation is complete.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

◆ ErrorBranch()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.ErrorBranch ( IPGTerminalSet  covered,
int  laIndex 
)
inlinevirtual

Generates code for the default error branch of prediction (called when there is no explicit error branch).

Parameters
coveredThe 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.
laIndexLocation of unexpected input, relative to current position.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

References Loyc.LLParserGenerator.CodeGenHelperBase.ApiCall(), and Loyc.Symbol.Name.

◆ Example()

abstract string Loyc.LLParserGenerator.CodeGenHelperBase.Example ( IPGTerminalSet  set)
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.

◆ ExampleChar()

virtual ? char Loyc.LLParserGenerator.CodeGenHelperBase.ExampleChar ( IPGTerminalSet  set)
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.

◆ GenerateAndPredCheck()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.GenerateAndPredCheck ( AndPred  andPred,
LNode  code,
int  li 
)
inlinevirtual

Generate code to check an and-predicate during or after prediction, e.g. &!{foo} becomes !(foo) during prediction and Check(!(foo)); afterward.

Parameters
andPredPredicate for which an expression has already been generated
codeThe expression to be checked
liCurrent lookahead amount. -1 means "prediction is complete, generate a Check() statement".

LLLPG substitutes $LI and $LA before it calls this method. This method can return null to suppress the Check statement.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

References Loyc.LLParserGenerator.CodeGenHelperBase.ApiCall(), Loyc.LLParserGenerator.AndPred.CheckErrorMessage, Loyc.Syntax.ParsingMode.Expressions, Loyc.Syntax.CodeSymbols.If, Loyc.LLParserGenerator.CodeGenHelperBase.NoCheckByDefault, Loyc.Syntax.CodeSymbols.Not, Loyc.LLParserGenerator.AndPred.Not, Loyc.LLParserGenerator.AndPred.Pred, and Loyc.Syntax.CodeSymbols.Return.

◆ GenerateMatch()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.GenerateMatch ( IPGTerminalSet  set_,
bool  savingResult,
bool  recognizerMode 
)
inlinevirtual

Generate code to match a set, e.g. MatchRange('a', 'z'); or MatchExcept('
', '\r'); }
. 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.LLParserGenerator.CodeGenHelperBase.GenerateMatchExpr(), Loyc.Syntax.CodeSymbols.If, Loyc.Syntax.CodeSymbols.Not, and Loyc.Syntax.CodeSymbols.Return.

◆ GenerateMatchExpr()

abstract LNode Loyc.LLParserGenerator.CodeGenHelperBase.GenerateMatchExpr ( IPGTerminalSet  set,
bool  savingResult,
bool  recognizerMode 
)
pure virtual

Generate code to match a set, e.g. MatchRange('a', 'z'); or MatchExcept('
', '\r'); }
. If the set is too complex, a declaration for it is created in classBody.

Implemented in Loyc.LLParserGenerator.GeneralCodeGenHelper, and Loyc.LLParserGenerator.IntStreamCodeGenHelper.

Referenced by Loyc.LLParserGenerator.CodeGenHelperBase.GenerateMatch().

◆ GenerateSetDecl()

abstract LNode Loyc.LLParserGenerator.CodeGenHelperBase.GenerateSetDecl ( IPGTerminalSet  set,
Symbol  setName 
)
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

HashSet<int> foo = NewSet('a', 'e', 'i', 'o', 'u', 'y');

Implemented in Loyc.LLParserGenerator.GeneralCodeGenHelper, and Loyc.LLParserGenerator.IntStreamCodeGenHelper.

◆ GenerateSkip()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.GenerateSkip ( bool  savingResult)
inlinevirtual

Returns (Skip()), or (MatchAny()) if the result is to be saved.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

References Loyc.LLParserGenerator.CodeGenHelperBase.ApiCall().

◆ GenerateSwitch()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.GenerateSwitch ( IPGTerminalSet[]  branchSets,
LNode[]  branchCode,
MSet< int >  casesToInclude,
LNode  defaultBranch,
LNode  laVar 
)
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].

Parameters
casesToIncludeThe set chosen by ShouldGenerateSwitch.
defaultBranchCode to be placed in the default: case, or the empty identifier (@``) if none
laVarThe lookahead variable being switched on (e.g. la0)
Returns
The generated switch block.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

References Loyc.Syntax.CodeSymbols.Case, Loyc.Syntax.CodeSymbols.Default, Loyc.LLParserGenerator.CodeGenHelperBase.GetCases(), Loyc.Syntax.CodeSymbols.Label, and Loyc.Syntax.CodeSymbols.SwitchStmt.

◆ GenerateTest() [1/2]

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.GenerateTest ( IPGTerminalSet  set,
LNode  laVar 
)
inlinevirtual

Generates code to test whether the terminal denoted 'laVar' is in the set.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

◆ GenerateTest() [2/2]

abstract LNode Loyc.LLParserGenerator.CodeGenHelperBase.GenerateTest ( IPGTerminalSet  set,
LNode  subject,
Symbol  setName 
)
protectedpure virtual

Generates code to test whether a terminal is in the set.

Parameters
subjectRepresents the variable to be tested.
setNameNames an external set variable to use for the test.
Returns
A test expression such as (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 @la0, 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.GeneralCodeGenHelper, and Loyc.LLParserGenerator.IntStreamCodeGenHelper.

◆ GetCases()

virtual IEnumerable<LNode> Loyc.LLParserGenerator.CodeGenHelperBase.GetCases ( IPGTerminalSet  set)
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.

Referenced by Loyc.LLParserGenerator.CodeGenHelperBase.GenerateSwitch().

◆ GetListType()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.GetListType ( LNode  type)
inlinevirtual

Gets the list type for elements of the specified type (e.g. List<type>)

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

References Loyc.LLParserGenerator.CodeGenHelperBase.ListType.

Referenced by Loyc.LLParserGenerator.CodeGenHelperBase.MakeInitializedVarDecl().

◆ GetRelativeCostForSwitch()

virtual int Loyc.LLParserGenerator.CodeGenHelperBase.GetRelativeCostForSwitch ( IPGTerminalSet  set)
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.

Referenced by Loyc.LLParserGenerator.CodeGenHelperBase.ShouldGenerateSwitch().

◆ LA()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.LA ( int  k)
inlinevirtual

Generates code to read LA(k).

Returns
Default implementation returns LA0 for k==0, LA(k) otherwise.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

Reimplemented in Loyc.LLParserGenerator.GeneralCodeGenHelper.

References Loyc.LLParserGenerator.CodeGenHelperBase.ApiCall().

◆ LAType()

abstract LNode Loyc.LLParserGenerator.CodeGenHelperBase.LAType ( )
pure virtual

◆ MakeInitializedVarDecl()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.MakeInitializedVarDecl ( LNode  type,
bool  wantList,
Symbol  varName 
)
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.

References Loyc.LLParserGenerator.CodeGenHelperBase.GetListType().

◆ Optimize()

virtual IPGTerminalSet Loyc.LLParserGenerator.CodeGenHelperBase.Optimize ( IPGTerminalSet  set,
IPGTerminalSet  dontcare 
)
inlinevirtual

Simplifies the specified set, if possible, so that GenerateTest() can generate simpler code for an if-else chain in a prediction tree.

Parameters
set
dontcareA set of terminals that have been ruled out, i.e. it is already known that the lookahead value is not in this set.
Returns
An optimized set, or this.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

Reimplemented in Loyc.LLParserGenerator.IntStreamCodeGenHelper.

◆ ResolveAlias()

LNode Loyc.LLParserGenerator.CodeGenHelperBase.ResolveAlias ( LNode  node)
inline

Returns the node for an alias. If the specified node is not an alias, returns the same node unchanged.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

Referenced by Loyc.LLParserGenerator.GeneralCodeGenHelper.CodeToTerminalPred().

◆ SetListInitializer()

void Loyc.LLParserGenerator.CodeGenHelperBase.SetListInitializer ( LNode  varDecl)
inline

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.LLParserGenerator.CodeGenHelperBase.ListInitializer, and Loyc.LLParserGenerator.CodeGenHelperBase.ListType.

◆ ShouldGenerateSwitch()

virtual bool Loyc.LLParserGenerator.CodeGenHelperBase.ShouldGenerateSwitch ( IPGTerminalSet[]  sets,
MSet< int >  casesToInclude,
bool  hasErrorBranch 
)
inlinevirtual

◆ VisitInput()

virtual LNode Loyc.LLParserGenerator.CodeGenHelperBase.VisitInput ( LNode  stmt,
IMessageSink  sink 
)
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.

Returns
a new statement to replace the original statement, or null to do nothing.

Implements Loyc.LLParserGenerator.IPGCodeGenHelper.

References Loyc.Syntax.CodeSymbols.Alias, Loyc.Syntax.LNode.Args, Loyc.Syntax.CodeSymbols.Assign, Loyc.Syntax.LNode.Calls(), Loyc.Syntax.LNode.CallsMin(), Loyc.Syntax.LNode.Equals(), and Loyc.Syntax.CodeSymbols.Splice.

Property Documentation

◆ BaseCostForSwitch

virtual int Loyc.LLParserGenerator.CodeGenHelperBase.BaseCostForSwitch
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).

Referenced by Loyc.LLParserGenerator.CodeGenHelperBase.ShouldGenerateSwitch().

◆ InputClass

LNode Loyc.LLParserGenerator.CodeGenHelperBase.InputClass
getset

Specifies a class or namespace to use when calling static functions. There is only one currently: NewSet(), which applies only to .

Referenced by Loyc.LLParserGenerator.CodeGenHelperBase.ApiCall(), and Loyc.LLParserGenerator.GeneralCodeGenHelper.GenerateTest().

◆ InputSource

LNode Loyc.LLParserGenerator.CodeGenHelperBase.InputSource
getset

Specifies an object or class on which LLLPG APIs such as Match() and LA() should be called.

Referenced by Loyc.LLParserGenerator.CodeGenHelperBase.ApiCall(), and Loyc.LLParserGenerator.CodeGenHelperBase.CreateTryWrapperForRecognizer().

◆ ListInitializer

LNode Loyc.LLParserGenerator.CodeGenHelperBase.ListInitializer
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.

Referenced by Loyc.LLParserGenerator.CodeGenHelperBase.SetListInitializer().

◆ ListType

LNode Loyc.LLParserGenerator.CodeGenHelperBase.ListType
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.

Referenced by Loyc.LLParserGenerator.CodeGenHelperBase.GetListType(), and Loyc.LLParserGenerator.CodeGenHelperBase.SetListInitializer().

◆ NoCheckByDefault

bool Loyc.LLParserGenerator.CodeGenHelperBase.NoCheckByDefault
getset

If true, calls to Check() are suppressed when AndPred.CheckErrorMessage is null.

Referenced by Loyc.LLParserGenerator.CodeGenHelperBase.GenerateAndPredCheck().

◆ TerminalType

LNode Loyc.LLParserGenerator.CodeGenHelperBase.TerminalType
getset

The type returned from Match() methods.