⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp.stg

📁 antlr最新版本V3源代码
💻 STG
📖 第 1 页 / 共 3 页
字号:
catch(decl,action) ::= <<catch (<e.decl>) {    <e.action>}>>ruleDeclarations() ::= <<<ruleDescriptor.useScopes:{<it>_stack.push(new <it>_scope());}; separator="\n"><ruleDescriptor.ruleScope:{<it.name>_stack.push(new <it.name>_scope());}; separator="\n"><if(ruleDescriptor.hasMultipleReturnValues)><returnType()> retval = new <returnType()>();retval.start = input.LT(1);<\n><else><ruleDescriptor.returnScope.attributes:{ a |<a.type> <a.name> = <if(a.initValue)><a.initValue><else><initValue(a.type)><endif>;}><endif><if(memoize)>int <ruleDescriptor.name>_StartIndex = input.index();<endif>>>ruleLabelDefs() ::= <<<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels]    :{<labelType> <it.label.text>=null;}; separator="\n"><[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels]    :{List list_<it.label.text>=null;}; separator="\n"><[ruleDescriptor.ruleLabels,ruleDescriptor.ruleListLabels]    :ruleLabelDef(label=it); separator="\n"><[ruleDescriptor.allRuleRefsInAltsWithRewrites,ruleDescriptor.allTokenRefsInAltsWithRewrites]    :{List list_<it>=new ArrayList();}; separator="\n">>>ruleReturnValue() ::= <<<if(!ruleDescriptor.isSynPred)><if(ruleDescriptor.hasReturnValue)><if(ruleDescriptor.hasSingleReturnValue)><ruleDescriptor.singleValueReturnName><else>retval<endif><endif><endif>>>ruleCleanUp() ::= <<<ruleDescriptor.useScopes:{<it>_stack.pop();}; separator="\n"><ruleDescriptor.ruleScope:{<it.name>_stack.pop();}; separator="\n"><if(ruleDescriptor.hasMultipleReturnValues)>retval.stop = input.LT(-1);<\n><endif><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) ::= <<void m<ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) throw(antlr3::BaseRecognitionException){<if(trace)>	antlr3::Tracer trace(this,"<ruleName>");<endif>	antlr3::CountScope nestingTracker(this->ruleNestingLevel);	StreamType& input(this->getInput());<if(nakedBlock)>	<ruleDescriptor.actions.init>	<ruleMemoization(name=ruleName)>	<block><\n><else>	tokenid_type type = <tokenPrefix()><ruleName>;	channel_type channel = antlr3::Token::DEFAULT_CHANNEL;	position_type start(input.getPosition());	<ruleDescriptor.actions.init>	<ruleMemoization(name=ruleName)>	<block>	<! create token if none exists *and* we are an outermost token rule !>	<execAction({if ( this->token == 0 && this->ruleNestingLevel == 1 ) {	TokenType *tt = TokenBuilder::build(type,start,input,channel);	std::cout \<\< (*tt) \<\< std::endl;	this->emit(tt);	}<\n>})><endif>}>>/** How to generate code for the implicitly-defined lexer grammar rule *  that chooses between lexer rules. */tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= <<void mTokens() throw(antlr3::BaseRecognitionException){	StreamType& input(this->getInput());	<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) ::= <<// block <fileName>:<description>decision_type 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) ::= <<// ruleBlock <fileName>:<description>decision_type alt<decisionNumber>=<maxAlt>;<decls><@predecision()><decision><@postdecision()>switch (alt<decisionNumber>) {    <alts:altSwitchCase()>}>>ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= <<// ruleBlockSingleAlt <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 0 or more alternatives */positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<// positiveClosureBlock <fileName>:<description>decision_type cnt<decisionNumber>=0;<decls><@preloop()>do {	decision_type alt<decisionNumber>=<maxAlt>;	<@predecision()>	<decision>	<@postdecision()>	switch (alt<decisionNumber>) {	<alts:altSwitchCase()>	default :		if ( cnt<decisionNumber> >= 1 )			goto loop<decisionNumber>;            EarlyExitException eee( input.getPosition(), <decisionNumber> );				<@earlyExitException()>            throw eee;	}	cnt<decisionNumber>++;} while (true);loop<decisionNumber>: ;<@postloop()>>>positiveClosureBlockSingleAlt ::= positiveClosureBlock/** A (..)* block with 1 or more alternatives */closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<// closureBlock <fileName>:<description><decls><@preloop()>do {	decision_type alt<decisionNumber>=<maxAlt>;	<@predecision()>	<decision>	<@postdecision()>	switch (alt<decisionNumber>) {	<alts:altSwitchCase()>	default :		goto loop<decisionNumber>;	}} while (true);loop<decisionNumber>: ;<@postloop()>>>closureBlockSingleAlt ::= closureBlock/** Optional blocks (x)? are translated to (x|) by before code generation *  so we can just use the normal block template */optionalBlock ::= blockoptionalBlockSingleAlt ::= 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) ::= <<// alt <fileName>:<description>{	<@declarations()>	<elements:element()>	<@cleanup()>}>>// E L E M E N T S/** Dump the elements one per line */element() ::= <<// element <fileName>:<description><@prematch()><it.el><\n>>>/** match a token optionally with a label in front */tokenRef(token,label,elementIndex) ::= <<// tokenRef<if(label)><label> = input.LT(1);<\n><endif>this->match(input,<token>,FOLLOW_<token>_in_<ruleName><elementIndex>);<checkRuleBacktrackFailure()>>>/** ids+=ID no AST building */tokenRefAndListLabel(token,label,elementIndex) ::= <<<tokenRef(...)><listLabel(...)>>>listLabel(label) ::= <<if (list_<label>==null) list_<label>=new ArrayList();list_<label>.add(<label>);<\n>>>/** match a character */charRef(char,label) ::= <<// charRef<if(label)><tokenid_type()> <label> = input.LA(1);<\n><endif>this->match(<char>); <checkRuleBacktrackFailure()>>>/** match a character range */charRangeRef(a,b) ::= "this->matchRange(<a>,<b>); <checkRuleBacktrackFailure()>"/** For now, sets are interval tests and must be tested inline */matchSet(s,label,elementIndex,postmatchCode="") ::= <<// matchSet<if(label)><label> = input.LT(1);<\n><endif>if ( <s> ){	<postmatchCode>	input.consume();<if(!LEXER)>	errorRecovery=false;<endif>	<if(backtracking)>failed=false;<endif>}else{	<ruleBacktrackFailure()>	MismatchedSetException mse(input.getPosition(),input.LA(1));	<@mismatchedSetException()><if(LEXER)>	this->recover(mse);<else>	this->recoverFromMismatchedSet(input,mse,FOLLOW_set_in_<ruleName><elementIndex>);<endif>	throw mse;}<\n>>>matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<<matchSet(...)><listLabel(...)>>>/** Match a string literal */lexerStringRef(string,label) ::= <<// lexerStringRef<if(label)>position_type <label>Start(input.getPosition());this->match( <string> ); <checkRuleBacktrackFailure()>TokenType* <label> = TokenBuilder::build(Token.INVALID_TOKEN_TYPE,<label>Start,input,Token.DEFAULT_CHANNEL);<else>this->match( <string> ); <checkRuleBacktrackFailure()><\n><endif>>>wildcard(label,elementIndex) ::= <<<if(label)><label> = input.LT(1);<\n><endif>this->matchAny( input );<checkRuleBacktrackFailure()>>>wildcardAndListLabel(label,elementIndex) ::= <<<wildcard(...)><listLabel(...)>>>/** Match . wildcard */wildcardChar(label, elementIndex) ::= <<<if(label)><tokenid_type()> <label> = input.LA(1);<\n><endif>this->matchAny();<checkRuleBacktrackFailure()>>>tokenid_type() ::= "<if(LEXER)>char_type<else>tokenid_type<endif>"wildcardCharListLabel(label, elementIndex) ::= <<<wildcardChar(...)><listLabel(...)>>>/** Match a rule reference by invoking it possibly with arguments *  and a return value or values. */ruleRef(rule,label,elementIndex,args) ::= <<following.push(FOLLOW_<rule>_in_<ruleName><elementIndex>);<if(label)><label>=<rule>(<args>);<\n><else><rule>(<args>);<\n><endif>following.pop();<checkRuleBacktrackFailure()>>>/** ids+=ID */ruleRefAndListLabel(rule,label,elementIndex,args) ::= <<<ruleRef(...)><listLabel(...)>>>/** A lexer rule reference */lexerRuleRef(rule,label,args) ::= <<<if(label)>position_type <label>Start(input.getPosition());m<rule>(<args>);<checkRuleBacktrackFailure()>TokenType* <label> = TokenBuilder::build(Token.INVALID_TOKEN_TYPE,<label>Start,input,Token.DEFAULT_CHANNEL);<else>m<rule>(<args>);<checkRuleBacktrackFailure()><endif>>>/** EOF in the lexer */lexerMatchEOF(label) ::= <<<if(label)>position_type <label>Start(input.getPosition());match(EOF); <checkRuleBacktrackFailure()>TokenType* <label> = TokenBuilder::build(Token.EOF,<label>Start,input,Token.DEFAULT_CHANNEL);<else>match(EOF); <checkRuleBacktrackFailure()><endif>>>/** match ^(root children) in tree parser */tree(root, children, nullableChildList) ::= <<<root:element()><if(nullableChildList)>if ( input.LA(1)==antlr3::Token::DOWN ) {    match(input, antlr3::Token::DOWN, null);     <checkRuleBacktrackFailure()>    <children:element()>    match(input, antlr3::Token::UP, null);     <checkRuleBacktrackFailure()>}<else>match(input, antlr3::Token::DOWN, null); <checkRuleBacktrackFailure()><children:element()>match(input, antlr3::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) ::= <<<if(!semPredState)><tokenid_type()> LA<decisionNumber>_<stateNumber> = input.LA(<k>);<\n><endif><edges; separator="\nelse ">else {<if(eotPredictsAlt)>	alt<decisionNumber> = <eotPredictsAlt>;<\n><else>	<ruleBacktrackFailure()>	NoViableAltException nvae(input.getPosition(), "<description>", <decisionNumber>, <stateNumber>);<\n>	<@noViableAltException()>	throw nvae;<\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.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -