C# Class Loyc.LLParserGenerator.CodeGenHelperBase

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: - default code snippets such as LA0 and LA(n), the default error branch, and switch statements; - the decision function ShouldGenerateSwitch(); and - alias handling (alias "foo" = bar); note that the derived class's NodeToPred() method is responsible for using _definedAliases.
Inheritance: IPGCodeGenHelper
Show file Open project: qwertie/ecsharp Class Usage Examples

Protected Properties

Property Type Description
F Loyc.Syntax.LNodeFactory
_Check Symbol
_Error Symbol
_LA Symbol
_LA0 Symbol
_Match Symbol
_MatchAny Symbol
_MatchExcept Symbol
_MatchExceptRange Symbol
_MatchRange Symbol
_Skip Symbol
_T Symbol
_TryMatch Symbol
_TryMatchExcept Symbol
_TryMatchExceptRange Symbol
_TryMatchRange Symbol
_alias Symbol
_classBody WList
_currentRule Rule
_definedAliases LNode>.Dictionary
_setNameCounter int
_underscore Symbol

Public Methods

Method Description
Begin ( WList classBody, ISourceFile sourceFile ) : void
BeginRule ( Rule rule ) : void
CallRule ( RuleRef rref, bool recognizerMode ) : LNode
CallTryRecognizer ( RuleRef rref, int lookahead ) : LNode
CodeGenHelperBase ( ) : System
CodeToTerminalPred ( LNode expr, string &errorMsg ) : Pred
CreateRuleMethod ( Rule rule, VList methodBody ) : LNode
CreateTryWrapperForRecognizer ( Rule rule ) : LNode

See IPGCodeGenHelper.CreateTryWrapperForRecognizer for more information.

Done ( ) : void
ErrorBranch ( IPGTerminalSet covered, int laIndex ) : LNode

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

Example ( IPGTerminalSet set ) : string
ExampleChar ( IPGTerminalSet set ) : char?
GenerateAndPredCheck ( AndPred andPred, LNode code, int li ) : LNode

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

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

GenerateMatch ( IPGTerminalSet set, bool savingResult, bool recognizerMode ) : LNode
GenerateMatchExpr ( IPGTerminalSet set, bool savingResult, bool recognizerMode ) : LNode

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

GenerateSkip ( bool savingResult ) : LNode

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

GenerateSwitch ( IPGTerminalSet branchSets, MSet casesToInclude, LNode branchCode, LNode defaultBranch, LNode laVar ) : LNode
GenerateTest ( IPGTerminalSet set, LNode laVar ) : LNode
GetListType ( LNode type ) : LNode
LA ( int k ) : LNode

Generates code to read LA(k).

LAType ( ) : LNode

Returns the data type of LA(k)

MakeInitializedVarDecl ( LNode type, bool wantList, Symbol varName ) : LNode
Optimize ( IPGTerminalSet set, IPGTerminalSet dontcare ) : IPGTerminalSet
ResolveAlias ( LNode expr ) : LNode
SetListInitializer ( LNode varDecl ) : void

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.

ShouldGenerateSwitch ( IPGTerminalSet sets, MSet casesToInclude, bool hasErrorBranch ) : bool

Decides whether to use a switch() and for which cases, using BaseCostForSwitch and GetRelativeCostForSwitch.

VisitInput ( LNode stmt, IMessageSink sink ) : LNode

Protected Methods

Method Description
ApiCall ( Symbol apiName ) : LNode

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').

ApiCall ( Symbol apiName, IEnumerable args, bool isStatic = false ) : LNode

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').

ApiType ( LNode typeName ) : LNode
DefaultOf ( LNode type, bool wantList ) : LNode
EndMayBeReachable ( LNode stmt ) : bool
GenerateSetDecl ( IPGTerminalSet set, Symbol setName ) : LNode

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');

GenerateSetDecl ( IPGTerminalSet set ) : Symbol
GenerateSetName ( Rule currentRule ) : Symbol
GenerateTest ( IPGTerminalSet set, LNode subject, Symbol setName ) : LNode

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

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.

GetCases ( IPGTerminalSet set ) : IEnumerable

Gets the literals or symbols to use for switch cases of a set (just the values, not including the case labels.)

GetRelativeCostForSwitch ( IPGTerminalSet set ) : int

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.

Private Methods

Method Description
AddSwitchHandler ( LNode branch, WList stmts ) : void
ForwardedArgList ( LNode args ) : VList
ReplaceT ( LNode expr, LNode replacement ) : LNode
VarName ( LNode varStmt ) : LNode

Method Details

ApiCall() protected method

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').
protected ApiCall ( Symbol apiName ) : LNode
apiName Symbol
return LNode

ApiCall() protected method

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').
protected ApiCall ( Symbol apiName, IEnumerable args, bool isStatic = false ) : LNode
apiName Symbol
args IEnumerable Parameters to the API call, or null to access a /// property or field.
isStatic bool
return LNode

ApiType() protected method

protected ApiType ( LNode typeName ) : LNode
typeName LNode
return LNode

Begin() public method

public Begin ( WList classBody, ISourceFile sourceFile ) : void
classBody WList
sourceFile ISourceFile
return void

BeginRule() public method

public BeginRule ( Rule rule ) : void
rule Rule
return void

CallRule() public method

public CallRule ( RuleRef rref, bool recognizerMode ) : LNode
rref RuleRef
recognizerMode bool
return LNode

CallTryRecognizer() public method

public CallTryRecognizer ( RuleRef rref, int lookahead ) : LNode
rref RuleRef
lookahead int
return LNode

CodeGenHelperBase() public method

public CodeGenHelperBase ( ) : System
return System

CodeToTerminalPred() public abstract method

public abstract CodeToTerminalPred ( LNode expr, string &errorMsg ) : Pred
expr LNode
errorMsg string
return Pred

CreateRuleMethod() public method

public CreateRuleMethod ( Rule rule, VList methodBody ) : LNode
rule Rule
methodBody VList
return LNode

CreateTryWrapperForRecognizer() public method

See IPGCodeGenHelper.CreateTryWrapperForRecognizer for more information.
public CreateTryWrapperForRecognizer ( Rule rule ) : LNode
rule Rule
return LNode

DefaultOf() protected method

protected DefaultOf ( LNode type, bool wantList ) : LNode
type LNode
wantList bool
return LNode

Done() public method

public Done ( ) : void
return void

EndMayBeReachable() protected static method

protected static EndMayBeReachable ( LNode stmt ) : bool
stmt LNode
return bool

ErrorBranch() public method

Generates code for the default error branch of prediction (called when there is no explicit error branch).
public ErrorBranch ( IPGTerminalSet covered, int laIndex ) : LNode
covered IPGTerminalSet 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 int Location of unexpected input, relative to current position.
return LNode

Example() public abstract method

public abstract Example ( IPGTerminalSet set ) : string
set IPGTerminalSet
return string

ExampleChar() public method

public ExampleChar ( IPGTerminalSet set ) : char?
set IPGTerminalSet
return char?

GenerateAndPredCheck() public method

Generate code to check an and-predicate during or after prediction, e.g. &!{foo} becomes !(foo) during prediction and Check(!(foo)); afterward.
LLLPG substitutes $LI and $LA before it calls this method. This method can return null to suppress the Check statement.
public GenerateAndPredCheck ( AndPred andPred, LNode code, int li ) : LNode
andPred AndPred Predicate for which an expression has already been generated
code LNode The expression to be checked
li int Current lookahead amount. -1 means "prediction is /// complete, generate a Check() statement".
return LNode

GenerateMatch() public method

public GenerateMatch ( IPGTerminalSet set, bool savingResult, bool recognizerMode ) : LNode
set IPGTerminalSet
savingResult bool
recognizerMode bool
return LNode

GenerateMatchExpr() public abstract method

Generate code to match a set, e.g. @{ MatchRange('a', 'z'); or @{ MatchExcept('\n', '\r'); }. If the set is too complex, a declaration for it is created in classBody.
public abstract GenerateMatchExpr ( IPGTerminalSet set, bool savingResult, bool recognizerMode ) : LNode
set IPGTerminalSet
savingResult bool
recognizerMode bool
return LNode

GenerateSetDecl() protected abstract method

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');
protected abstract GenerateSetDecl ( IPGTerminalSet set, Symbol setName ) : LNode
set IPGTerminalSet
setName Symbol
return LNode

GenerateSetDecl() protected method

protected GenerateSetDecl ( IPGTerminalSet set ) : Symbol
set IPGTerminalSet
return Symbol

GenerateSetName() protected method

protected GenerateSetName ( Rule currentRule ) : Symbol
currentRule Rule
return Symbol

GenerateSkip() public method

Returns (Skip()), or (MatchAny()) if the result is to be saved.
public GenerateSkip ( bool savingResult ) : LNode
savingResult bool
return LNode

GenerateSwitch() public method

public GenerateSwitch ( IPGTerminalSet branchSets, MSet casesToInclude, LNode branchCode, LNode defaultBranch, LNode laVar ) : LNode
branchSets IPGTerminalSet
casesToInclude MSet
branchCode LNode
defaultBranch LNode
laVar LNode
return LNode

GenerateTest() public method

public GenerateTest ( IPGTerminalSet set, LNode laVar ) : LNode
set IPGTerminalSet
laVar LNode
return LNode

GenerateTest() protected abstract method

Generates code to test whether a terminal is in the set.
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.
protected abstract GenerateTest ( IPGTerminalSet set, LNode subject, Symbol setName ) : LNode
set IPGTerminalSet
subject LNode Represents the variable to be tested.
setName Symbol Names an external set variable to use for the test.
return LNode

GetCases() protected method

Gets the literals or symbols to use for switch cases of a set (just the values, not including the case labels.)
protected GetCases ( IPGTerminalSet set ) : IEnumerable
set IPGTerminalSet
return IEnumerable

GetListType() public method

public GetListType ( LNode type ) : LNode
type LNode
return LNode

GetRelativeCostForSwitch() protected method

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.
protected GetRelativeCostForSwitch ( IPGTerminalSet set ) : int
set IPGTerminalSet
return int

LA() public method

Generates code to read LA(k).
public LA ( int k ) : LNode
k int
return LNode

LAType() public abstract method

Returns the data type of LA(k)
public abstract LAType ( ) : LNode
return LNode

MakeInitializedVarDecl() public method

public MakeInitializedVarDecl ( LNode type, bool wantList, Symbol varName ) : LNode
type LNode
wantList bool
varName Symbol
return LNode

Optimize() public method

public Optimize ( IPGTerminalSet set, IPGTerminalSet dontcare ) : IPGTerminalSet
set IPGTerminalSet
dontcare IPGTerminalSet
return IPGTerminalSet

ResolveAlias() public method

public ResolveAlias ( LNode expr ) : LNode
expr LNode
return LNode

SetListInitializer() public method

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.
public SetListInitializer ( LNode varDecl ) : void
varDecl LNode
return void

ShouldGenerateSwitch() public method

Decides whether to use a switch() and for which cases, using BaseCostForSwitch and GetRelativeCostForSwitch.
public ShouldGenerateSwitch ( IPGTerminalSet sets, MSet casesToInclude, bool hasErrorBranch ) : bool
sets IPGTerminalSet
casesToInclude MSet
hasErrorBranch bool
return bool

VisitInput() public method

public VisitInput ( LNode stmt, IMessageSink sink ) : LNode
stmt LNode
sink IMessageSink
return LNode

Property Details

F protected property

protected LNodeFactory,Loyc.Syntax F
return Loyc.Syntax.LNodeFactory

_Check protected static property

protected static Symbol _Check
return Symbol

_Error protected static property

protected static Symbol _Error
return Symbol

_LA protected static property

protected static Symbol _LA
return Symbol

_LA0 protected static property

protected static Symbol _LA0
return Symbol

_Match protected static property

protected static Symbol _Match
return Symbol

_MatchAny protected static property

protected static Symbol _MatchAny
return Symbol

_MatchExcept protected static property

protected static Symbol _MatchExcept
return Symbol

_MatchExceptRange protected static property

protected static Symbol _MatchExceptRange
return Symbol

_MatchRange protected static property

protected static Symbol _MatchRange
return Symbol

_Skip protected static property

protected static Symbol _Skip
return Symbol

_T protected static property

protected static Symbol _T
return Symbol

_TryMatch protected static property

protected static Symbol _TryMatch
return Symbol

_TryMatchExcept protected static property

protected static Symbol _TryMatchExcept
return Symbol

_TryMatchExceptRange protected static property

protected static Symbol _TryMatchExceptRange
return Symbol

_TryMatchRange protected static property

protected static Symbol _TryMatchRange
return Symbol

_alias protected static property

protected static Symbol _alias
return Symbol

_classBody protected property

protected WList _classBody
return WList

_currentRule protected property

protected Rule,Loyc.LLParserGenerator _currentRule
return Rule

_definedAliases protected property

protected Dictionary _definedAliases
return LNode>.Dictionary

_setNameCounter protected property

protected int _setNameCounter
return int

_underscore protected static property

protected static Symbol _underscore
return Symbol