📄 csharp.stg
字号:
ruleDescriptor.ruleListLabels,
ruleDescriptor.ruleListLabels]
:{IList list_<it.label.text> = null;}; separator="\n"
>
>>
ruleReturnValue() ::= <<
<if(!ruleDescriptor.isSynPred)>
<if(ruleDescriptor.hasReturnValue)>
<if(ruleDescriptor.hasSingleReturnValue)>
<ruleDescriptor.singleValueReturnName>
<else>
retval
<endif>
<endif>
<endif>
>>
ruleCleanUp() ::= <<
<if(ruleDescriptor.hasMultipleReturnValues)>
<if(!TREE_PARSER)>
retval.stop = input.LT(-1);<\n>
<endif>
<endif>
>>
memoize() ::= <<
<if(memoize)>
<if(backtracking)>
if ( backtracking > 0 )
{
Memoize(input, <ruleDescriptor.index>, <ruleDescriptor.name>_StartIndex);
}
<endif>
<endif>
>>
/** How to generate a rule in the lexer; naked blocks are used for
* fragment rules.
*/
lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= <<
// $ANTLR start <ruleName>
public void m<ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) // throws RecognitionException [2]
{
<if(trace)>TraceIn("<ruleName>", <ruleDescriptor.index>);<endif>
<ruleDeclarations()>
try
{
<if(nakedBlock)>
<ruleMemoization(name=ruleName)>
<lexerRuleLabelDefs()>
<ruleDescriptor.actions.init>
<block><\n>
<else>
int _type = <ruleName>;
<ruleMemoization(name=ruleName)>
<lexerRuleLabelDefs()>
<ruleDescriptor.actions.init>
<block>
<ruleCleanUp()>
this.type = _type;
<(ruleDescriptor.actions.after):execAction()>
<endif>
}
finally
{
<if(trace)>TraceOut("<ruleName>", <ruleDescriptor.index>);<endif>
<memoize()>
}
}
// $ANTLR end <ruleName>
>>
/** How to generate code for the implicitly-defined lexer grammar rule
* that chooses between lexer rules.
*/
tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= <<
override public void mTokens() // throws RecognitionException
{
<block><\n>
}
>>
// S U B R U L E S
/** A (...) subrule with multiple alternatives */
block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
// <fileName>:<description>
int alt<decisionNumber> = <maxAlt>;
<decls>
<@predecision()>
<decision>
<@postdecision()>
<@prebranch()>
switch (alt<decisionNumber>)
{
<alts:altSwitchCase()>
}
<@postbranch()>
>>
/** A rule block with multiple alternatives */
ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
// <fileName>:<description>
int alt<decisionNumber> = <maxAlt>;
<decls>
<@predecision()>
<decision>
<@postdecision()>
switch (alt<decisionNumber>)
{
<alts:altSwitchCase()>
}
>>
ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= <<
// <fileName>:<description>
<decls>
<@prealt()>
<alts>
<@postalt()>
>>
/** A special case of a (...) subrule with a single alternative */
blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= <<
// <fileName>:<description>
<decls>
<@prealt()>
<alts>
<@postalt()>
>>
/** A (..)+ block with 1 or more alternatives */
positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
// <fileName>:<description>
int cnt<decisionNumber> = 0;
<decls>
<@preloop()>
do
{
int alt<decisionNumber> = <maxAlt>;
<@predecision()>
<decision>
<@postdecision()>
switch (alt<decisionNumber>)
{
<alts:altSwitchCase()>
default:
if ( cnt<decisionNumber> >= 1 ) goto loop<decisionNumber>;
<ruleBacktrackFailure()>
EarlyExitException eee =
new EarlyExitException(<decisionNumber>, input);
<@earlyExitException()>
throw eee;
}
cnt<decisionNumber>++;
} while (true);
loop<decisionNumber>:
; // Stops C# compiler whinging that label 'loop<decisionNumber>' has no statements
<@postloop()>
>>
positiveClosureBlockSingleAlt ::= positiveClosureBlock
/** A (..)* block with 1 or more alternatives */
closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
// <fileName>:<description>
<decls>
<@preloop()>
do
{
int alt<decisionNumber> = <maxAlt>;
<@predecision()>
<decision>
<@postdecision()>
switch (alt<decisionNumber>)
{
<alts:altSwitchCase()>
default:
goto loop<decisionNumber>;
}
} while (true);
loop<decisionNumber>:
; // Stops C# compiler whinging that label 'loop<decisionNumber>' has no statements
<@postloop()>
>>
closureBlockSingleAlt ::= closureBlock
/** Optional blocks (x)? are translated to (x|) by before code generation
* so we can just use the normal block template
*/
optionalBlock ::= block
optionalBlockSingleAlt ::= block
/** A case in a switch that jumps to an alternative given the alternative
* number. A DFA predicts the alternative and then a simple switch
* does the jump to the code that actually matches that alternative.
*/
altSwitchCase() ::= <<
case <i> :
<@prealt()>
<it>
break;<\n>
>>
/** An alternative is just a list of elements; at outermost level */
alt(elements,altNum,description,autoAST,outerAlt) ::= <<
// <fileName>:<description>
{
<@declarations()>
<elements:element()>
<@cleanup()>
}
>>
// E L E M E N T S
/** Dump the elements one per line */
element() ::= <<
<@prematch()>
<it.el><\n>
>>
/** match a token optionally with a label in front */
tokenRef(token,label,elementIndex) ::= <<
<if(label)>
<label> = (<labelType>)input.LT(1);<\n>
<endif>
Match(input,<token>,FOLLOW_<token>_in_<ruleName><elementIndex>); <checkRuleBacktrackFailure()>
>>
/** ids+=ID */
tokenRefAndListLabel(token,label,elementIndex) ::= <<
<tokenRef(...)>
<listLabel(elem=label,...)>
>>
listLabel(label,elem) ::= <<
if (list_<label> == null) list_<label> = new ArrayList();
list_<label>.Add(<elem>);<\n>
>>
/** match a character */
charRef(char,label) ::= <<
<if(label)>
<label> = input.LA(1);<\n>
<endif>
Match(<char>); <checkRuleBacktrackFailure()>
>>
/** match a character range */
charRangeRef(a,b,label) ::= <<
<if(label)>
<label> = input.LA(1);<\n>
<endif>
MatchRange(<a>,<b>); <checkRuleBacktrackFailure()>
>>
/** For now, sets are interval tests and must be tested inline */
matchSet(s,label,elementIndex,postmatchCode="") ::= <<
<if(label)>
<if(LEXER)>
<label>= input.LA(1);<\n>
<else>
<label> = (<labelType>)input.LT(1);<\n>
<endif>
<endif>
if ( <s> )
{
input.Consume();
<postmatchCode>
<if(!LEXER)>
errorRecovery = false;
<endif>
<if(backtracking)>failed = false;<endif>
}
else
{
<ruleBacktrackFailure()>
MismatchedSetException mse =
new MismatchedSetException(null,input);
<@mismatchedSetException()>
<if(LEXER)>
Recover(mse);
<else>
RecoverFromMismatchedSet(input,mse,FOLLOW_set_in_<ruleName><elementIndex>);
<endif>
throw mse;
}<\n>
>>
matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
<matchSet(...)>
<listLabel(elem=label,...)>
>>
/** Match a string literal */
lexerStringRef(string,label) ::= <<
<if(label)>
int <label>Start = CharIndex;
Match(<string>); <checkRuleBacktrackFailure()>
<labelType> <label> = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, <label>Start, CharIndex-1);
<else>
Match(<string>); <checkRuleBacktrackFailure()><\n>
<endif>
>>
wildcard(label,elementIndex) ::= <<
<if(label)>
<label> = (<labelType>)input.LT(1);<\n>
<endif>
MatchAny(input); <checkRuleBacktrackFailure()>
>>
wildcardAndListLabel(label,elementIndex) ::= <<
<wildcard(...)>
<listLabel(elem=label,...)>
>>
/** Match . wildcard in lexer */
wildcardChar(label, elementIndex) ::= <<
<if(label)>
<label> = input.LA(1);<\n>
<endif>
MatchAny(); <checkRuleBacktrackFailure()>
>>
wildcardCharListLabel(label, elementIndex) ::= <<
<wildcardChar(...)>
<listLabel(elem=label,...)>
>>
/** Match a rule reference by invoking it possibly with arguments
* and a return value or values.
*/
ruleRef(rule,label,elementIndex,args) ::= <<
PushFollow(FOLLOW_<rule>_in_<ruleName><elementIndex>);
<if(label)>
<label> = <rule>(<args; separator=", ">);<\n>
<else>
<rule>(<args; separator=", ">);<\n>
<endif>
followingStackPointer_--;
<checkRuleBacktrackFailure()>
>>
/** ids+=r */
ruleRefAndListLabel(rule,label,elementIndex,args) ::= <<
<ruleRef(...)>
<listLabel(elem=label,...)>
>>
/** A lexer rule reference */
lexerRuleRef(rule,label,args,elementIndex) ::= <<
<if(label)>
int <label>Start<elementIndex> = CharIndex;
m<rule>(<args; separator=", ">); <checkRuleBacktrackFailure()>
<label> = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, <label>Start<elementIndex>, CharIndex-1);
<else>
m<rule>(<args; separator=", ">); <checkRuleBacktrackFailure()>
<endif>
>>
/** i+=INT in lexer */
lexerRuleRefAndListLabel(rule,label,args,elementIndex) ::= <<
<lexerRuleRef(...)>
<listLabel(elem=label,...)>
>>
/** EOF in the lexer */
lexerMatchEOF(label,elementIndex) ::= <<
<if(label)>
int <label>Start<elementIndex> = CharIndex;
Match(EOF); <checkRuleBacktrackFailure()>
<labelType> <label> = new CommonToken(input, EOF, Token.DEFAULT_CHANNEL, <label>Start<elementIndex>, CharIndex-1);
<else>
Match(EOF); <checkRuleBacktrackFailure()>
<endif>
>>
/** match ^(root children) in tree parser */
tree(root, actionsAfterRoot, children, nullableChildList) ::= <<
<root:element()>
<actionsAfterRoot:element()>
<if(nullableChildList)>
if ( input.LA(1) == Token.DOWN )
{
Match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
<children:element()>
Match(input, Token.UP, null); <checkRuleBacktrackFailure()>
}
<else>
Match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
<children:element()>
Match(input, Token.UP, null); <checkRuleBacktrackFailure()>
<endif>
>>
/** Every predicate is used as a validating predicate (even when it is
* also hoisted into a prediction expression).
*/
validateSemanticPredicate(pred,description) ::= <<
if ( !(<evalPredicate(...)>) )
{
<ruleBacktrackFailure()>
throw new FailedPredicateException(input, "<ruleName>", "<description>");
}
>>
// F i x e d D F A (if-then-else)
dfaState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
int LA<decisionNumber>_<stateNumber> = input.LA(<k>);<\n>
<edges; separator="\nelse ">
else
{
<if(eotPredictsAlt)>
alt<decisionNumber> = <eotPredictsAlt>;
<else>
<ruleBacktrackFailure()>
NoViableAltException nvae_d<decisionNumber>s<stateNumber> =
new NoViableAltException("<description>", <decisionNumber>, <stateNumber>, input);<\n>
<@noViableAltException()>
throw nvae_d<decisionNumber>s<stateNumber>;<\n>
<endif>
}
>>
/** Same as a normal DFA state except that we don't examine lookahead
* for the bypass alternative. It delays error detection but this
* is faster, smaller, and more what people expect. For (X)? people
* expect "if ( LA(1)==X ) match(X);" and that's it.
*/
dfaOptionalBlockState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
int LA<decisionNumber>_<stateNumber> = input.LA(<k>);<\n>
<edges; separator="\nelse ">
>>
/** A DFA state that is actually the loopback decision of a closure
* loop. If end-of-token (EOT) predicts any of the targets then it
* should act like a default clause (i.e., no error can be generated).
* This is used only in the lexer so that for ('a')* on the end of a rule
* anything other than 'a' predicts exiting.
*/
dfaLoopbackState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
int LA<decisionNumber>_<stateNumber> = input.LA(<k>);<\n>
<edges; separator="\nelse "><\n>
<if(eotPredictsAlt)>
<if(!edges)>
alt<decisionNumber>=<eotPredictsAlt>; <! if no edges, don't gen ELSE !>
<else>
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -