Enhanced C#
Language of your choice: library documentation

Documentation moved to ecsharp.net

GitHub doesn't support HTTP redirects, so you'll be redirected in 3 seconds.

 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Public fields | Properties | Public Member Functions | Protected Member Functions | List of all members
Loyc.LLParserGenerator.IntStreamCodeGenHelper Class Reference

Standard code generator for character/integer input streams and is the default code generator for LLParserGenerator. This is the code generator helper for LLLPG lexer {...}. More...


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

Remarks

Standard code generator for character/integer input streams and is the default code generator for LLParserGenerator. This is the code generator helper for LLLPG lexer {...}.

Public fields

const int EOF_int = PGIntSet.EOF_int
 

Properties

LNode SetType [get, set]
 
override IPGTerminalSet EmptySet [get]
 
- Properties inherited from Loyc.LLParserGenerator.CodeGenHelperBase
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]
 
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...
 
- 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

override Pred CodeToTerminalPred (LNode expr, ref string errorMsg)
 Creates a terminal predicate from a code expression. More...
 
override 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...
 
int ExampleInt (PGIntSet set)
 
override 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...
 
override 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...
 
override 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...
 
override LNode LAType ()
 Returns the data type of LA(k) More...
 
- Public Member Functions inherited from Loyc.LLParserGenerator.CodeGenHelperBase
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...
 
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...
 
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...
 
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...
 
- Public Member Functions inherited from Loyc.LLParserGenerator.IPGCodeGenHelper

Protected Member Functions

override LNode GenerateTest (IPGTerminalSet set, LNode subject, Symbol setName)
 Generates code to test whether a terminal is in the set. More...
 
override LNode GenerateSetDecl (IPGTerminalSet set, Symbol setName)
 Generates a declaration for a variable that holds the set. More...
 
LNode GenerateSetDecl (PGIntSet set, Symbol setName)
 
override 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...
 
override 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...
 
- Protected Member Functions inherited from Loyc.LLParserGenerator.CodeGenHelperBase
virtual Symbol GenerateSetName (Rule currentRule)
 
virtual Symbol GenerateSetDecl (IPGTerminalSet set)
 
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)
 

Additional Inherited Members

- Static Protected Member Functions inherited from Loyc.LLParserGenerator.CodeGenHelperBase
static bool EndMayBeReachable (LNode stmt)
 
- Protected fields inherited from Loyc.LLParserGenerator.CodeGenHelperBase
int _setNameCounter = 0
 
LNodeFactory F
 
WList< LNode_classBody
 
Rule _currentRule
 
Dictionary< LNode, LNode_definedAliases = new Dictionary<LNode, LNode>()
 
- Protected static fields inherited from Loyc.LLParserGenerator.CodeGenHelperBase
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")
 

Member Function Documentation

override Pred Loyc.LLParserGenerator.IntStreamCodeGenHelper.CodeToTerminalPred ( LNode  expr,
ref string  errorMsg 
)
inlinevirtual

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.CodeGenHelperBase.

References Loyc.Syntax.LNode.Args, and Loyc.Syntax.LNode.Value.

override string Loyc.LLParserGenerator.IntStreamCodeGenHelper.Example ( IPGTerminalSet  set)
inlinevirtual

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.CodeGenHelperBase.

override char Loyc.LLParserGenerator.IntStreamCodeGenHelper.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.

Reimplemented from Loyc.LLParserGenerator.CodeGenHelperBase.

override LNode Loyc.LLParserGenerator.IntStreamCodeGenHelper.GenerateMatchExpr ( IPGTerminalSet  set,
bool  savingResult,
bool  recognizerMode 
)
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 classBody.

Implements Loyc.LLParserGenerator.CodeGenHelperBase.

override LNode Loyc.LLParserGenerator.IntStreamCodeGenHelper.GenerateSetDecl ( IPGTerminalSet  set,
Symbol  setName 
)
inlineprotectedvirtual

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&lt;int> foo = NewSet('a', 'e', 'i', 'o', 'u', 'y');

Implements Loyc.LLParserGenerator.CodeGenHelperBase.

override LNode Loyc.LLParserGenerator.IntStreamCodeGenHelper.GenerateTest ( IPGTerminalSet  set,
LNode  subject,
Symbol  setName 
)
inlineprotectedvirtual

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 , 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.

Implements Loyc.LLParserGenerator.CodeGenHelperBase.

override IEnumerable<LNode> Loyc.LLParserGenerator.IntStreamCodeGenHelper.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 from Loyc.LLParserGenerator.CodeGenHelperBase.

override int Loyc.LLParserGenerator.IntStreamCodeGenHelper.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 from Loyc.LLParserGenerator.CodeGenHelperBase.

override LNode Loyc.LLParserGenerator.IntStreamCodeGenHelper.LAType ( )
inlinevirtual

Returns the data type of LA(k)

Implements Loyc.LLParserGenerator.CodeGenHelperBase.

override IPGTerminalSet Loyc.LLParserGenerator.IntStreamCodeGenHelper.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.

Reimplemented from Loyc.LLParserGenerator.CodeGenHelperBase.