C# Класс 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.
Наследование: IPGCodeGenHelper
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
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

Открытые методы

Метод Описание
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

Защищенные методы

Метод Описание
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.

Приватные методы

Метод Описание
AddSwitchHandler ( LNode branch, WList stmts ) : void
ForwardedArgList ( LNode args ) : VList
ReplaceT ( LNode expr, LNode replacement ) : LNode
VarName ( LNode varStmt ) : LNode

Описание методов

ApiCall() защищенный Метод

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
Результат LNode

ApiCall() защищенный Метод

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
Результат LNode

ApiType() защищенный Метод

protected ApiType ( LNode typeName ) : LNode
typeName LNode
Результат LNode

Begin() публичный Метод

public Begin ( WList classBody, ISourceFile sourceFile ) : void
classBody WList
sourceFile ISourceFile
Результат void

BeginRule() публичный Метод

public BeginRule ( Rule rule ) : void
rule Rule
Результат void

CallRule() публичный Метод

public CallRule ( RuleRef rref, bool recognizerMode ) : LNode
rref RuleRef
recognizerMode bool
Результат LNode

CallTryRecognizer() публичный Метод

public CallTryRecognizer ( RuleRef rref, int lookahead ) : LNode
rref RuleRef
lookahead int
Результат LNode

CodeGenHelperBase() публичный Метод

public CodeGenHelperBase ( ) : System
Результат System

CodeToTerminalPred() публичный абстрактный Метод

public abstract CodeToTerminalPred ( LNode expr, string &errorMsg ) : Pred
expr LNode
errorMsg string
Результат Pred

CreateRuleMethod() публичный Метод

public CreateRuleMethod ( Rule rule, VList methodBody ) : LNode
rule Rule
methodBody VList
Результат LNode

CreateTryWrapperForRecognizer() публичный Метод

See IPGCodeGenHelper.CreateTryWrapperForRecognizer for more information.
public CreateTryWrapperForRecognizer ( Rule rule ) : LNode
rule Rule
Результат LNode

DefaultOf() защищенный Метод

protected DefaultOf ( LNode type, bool wantList ) : LNode
type LNode
wantList bool
Результат LNode

Done() публичный Метод

public Done ( ) : void
Результат void

EndMayBeReachable() защищенный статический Метод

protected static EndMayBeReachable ( LNode stmt ) : bool
stmt LNode
Результат bool

ErrorBranch() публичный Метод

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.
Результат LNode

Example() публичный абстрактный Метод

public abstract Example ( IPGTerminalSet set ) : string
set IPGTerminalSet
Результат string

ExampleChar() публичный Метод

public ExampleChar ( IPGTerminalSet set ) : char?
set IPGTerminalSet
Результат char?

GenerateAndPredCheck() публичный Метод

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".
Результат LNode

GenerateMatch() публичный Метод

public GenerateMatch ( IPGTerminalSet set, bool savingResult, bool recognizerMode ) : LNode
set IPGTerminalSet
savingResult bool
recognizerMode bool
Результат LNode

GenerateMatchExpr() публичный абстрактный Метод

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
Результат LNode

GenerateSetDecl() защищенный абстрактный Метод

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
Результат LNode

GenerateSetDecl() защищенный Метод

protected GenerateSetDecl ( IPGTerminalSet set ) : Symbol
set IPGTerminalSet
Результат Symbol

GenerateSetName() защищенный Метод

protected GenerateSetName ( Rule currentRule ) : Symbol
currentRule Rule
Результат Symbol

GenerateSkip() публичный Метод

Returns (Skip()), or (MatchAny()) if the result is to be saved.
public GenerateSkip ( bool savingResult ) : LNode
savingResult bool
Результат LNode

GenerateSwitch() публичный Метод

public GenerateSwitch ( IPGTerminalSet branchSets, MSet casesToInclude, LNode branchCode, LNode defaultBranch, LNode laVar ) : LNode
branchSets IPGTerminalSet
casesToInclude MSet
branchCode LNode
defaultBranch LNode
laVar LNode
Результат LNode

GenerateTest() публичный Метод

public GenerateTest ( IPGTerminalSet set, LNode laVar ) : LNode
set IPGTerminalSet
laVar LNode
Результат LNode

GenerateTest() защищенный абстрактный Метод

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.
Результат LNode

GetCases() защищенный Метод

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
Результат IEnumerable

GetListType() публичный Метод

public GetListType ( LNode type ) : LNode
type LNode
Результат LNode

GetRelativeCostForSwitch() защищенный Метод

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
Результат int

LA() публичный Метод

Generates code to read LA(k).
public LA ( int k ) : LNode
k int
Результат LNode

LAType() публичный абстрактный Метод

Returns the data type of LA(k)
public abstract LAType ( ) : LNode
Результат LNode

MakeInitializedVarDecl() публичный Метод

public MakeInitializedVarDecl ( LNode type, bool wantList, Symbol varName ) : LNode
type LNode
wantList bool
varName Symbol
Результат LNode

Optimize() публичный Метод

public Optimize ( IPGTerminalSet set, IPGTerminalSet dontcare ) : IPGTerminalSet
set IPGTerminalSet
dontcare IPGTerminalSet
Результат IPGTerminalSet

ResolveAlias() публичный Метод

public ResolveAlias ( LNode expr ) : LNode
expr LNode
Результат LNode

SetListInitializer() публичный Метод

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
Результат void

ShouldGenerateSwitch() публичный Метод

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
Результат bool

VisitInput() публичный Метод

public VisitInput ( LNode stmt, IMessageSink sink ) : LNode
stmt LNode
sink IMessageSink
Результат LNode

Описание свойств

F защищенное свойство

protected LNodeFactory,Loyc.Syntax F
Результат Loyc.Syntax.LNodeFactory

_Check защищенное статическое свойство

protected static Symbol _Check
Результат Symbol

_Error защищенное статическое свойство

protected static Symbol _Error
Результат Symbol

_LA защищенное статическое свойство

protected static Symbol _LA
Результат Symbol

_LA0 защищенное статическое свойство

protected static Symbol _LA0
Результат Symbol

_Match защищенное статическое свойство

protected static Symbol _Match
Результат Symbol

_MatchAny защищенное статическое свойство

protected static Symbol _MatchAny
Результат Symbol

_MatchExcept защищенное статическое свойство

protected static Symbol _MatchExcept
Результат Symbol

_MatchExceptRange защищенное статическое свойство

protected static Symbol _MatchExceptRange
Результат Symbol

_MatchRange защищенное статическое свойство

protected static Symbol _MatchRange
Результат Symbol

_Skip защищенное статическое свойство

protected static Symbol _Skip
Результат Symbol

_T защищенное статическое свойство

protected static Symbol _T
Результат Symbol

_TryMatch защищенное статическое свойство

protected static Symbol _TryMatch
Результат Symbol

_TryMatchExcept защищенное статическое свойство

protected static Symbol _TryMatchExcept
Результат Symbol

_TryMatchExceptRange защищенное статическое свойство

protected static Symbol _TryMatchExceptRange
Результат Symbol

_TryMatchRange защищенное статическое свойство

protected static Symbol _TryMatchRange
Результат Symbol

_alias защищенное статическое свойство

protected static Symbol _alias
Результат Symbol

_classBody защищенное свойство

protected WList _classBody
Результат WList

_currentRule защищенное свойство

protected Rule,Loyc.LLParserGenerator _currentRule
Результат Rule

_definedAliases защищенное свойство

protected Dictionary _definedAliases
Результат LNode>.Dictionary

_setNameCounter защищенное свойство

protected int _setNameCounter
Результат int

_underscore защищенное статическое свойство

protected static Symbol _underscore
Результат Symbol