📄 c.stg
字号:
return fileName;}/** \brief Create a new <name> parser and retrun a context for it. * * \param[in] instream Pointer to an input stream interface. * * \return Pointer to new parser context upon success. */ANTLR3_API p<name><name>New (<inputStreamType> instream){ p<name> ctx; /* Context structure we will build and return */ ctx = (p<name>) ANTLR3_MALLOC(sizeof(<name>)); if (ctx == NULL) { /* Failed to allocate memory for parser context */ return (p<name>)ANTLR3_ERR_NOMEM; } /* ------------------------------------------------------------------- * Memory for basic structure is allocated, now to fill in * the base ANTLR3 structures. We intialize the function pointers * for the standard ANTLR3 parser function set, but upon return * from here, the programmer may set the pointers to provide custom * implementations of each function. * * We don't use the macros defined in <name>.h here, in order that you can get a sense * of what goes where. */<if(PARSER)> /* Create a base parser/recognizer, using the supplied token stream */ ctx->pParser = antlr3ParserNewStream(ANTLR3_SIZE_HINT, instream->tstream);<endif><if(TREE_PARSER)> /* Create a base Tree parser/recognizer, using the supplied tree node stream */ ctx->pTreeParser = antlr3TreeParserNewStream(ANTLR3_SIZE_HINT, instream);<endif> /* Install the implementation of our <name> interface */ <rules:{r | ctx-><r.ruleDescriptor.name> = <r.ruleDescriptor.name>;}; separator="\n";> ctx->free = <name>Free; ctx->getGrammarFileName = getGrammarFileName; /* Install the scope pushing methods. */ <rules: {r |<if(r.ruleDescriptor.ruleScope)><ruleAttributeScope(scope=r.ruleDescriptor.ruleScope)><\n><endif>}> <recognizer.scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScope(scope=it)><\n><endif>}> <@apifuncs> <@end> <actions.parser.apifuncs> <actions.treeparser.apifuncs> /* Install the token table */ rec->tokenNames = <name>TokenNames; /* Initialize the follow bit sets */ <name>LoadFollowSets(); /* Return the newly built parser to the caller */ return ctx;}/** Free the parser resources */ static void <name>Free(p<name> ctx) { /* Free any follow sets we created for error recovery */ <name>FreeFollowSets(); /* Free any scope memory */ <rules: {r |<if(r.ruleDescriptor.ruleScope)><ruleAttributeScopeFree(scope=r.ruleDescriptor.ruleScope)><\n><endif>}> <recognizer.scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeFree(scope=it)><\n><endif>}> <@cleanup> <@end><if(TREE_PARSER)> ctx->pTreeParser->free(ctx->pTreeParser);<\n><else> ctx->pParser->free(ctx->pParser);<\n><endif> ANTLR3_FREE(ctx); /* Everything is released, so we can return */ return; } /** Return token names used by this <grammarType()> * * The returned pointer is used as an index into the token names table (using the token * number as the index). * * \return Pointer to first char * in the table. */static pANTLR3_UINT8 *getTokenNames() { return <name>TokenNames; } <members> /* Declare the bitsets */<bitsets:bitsetDeclare(name={FOLLOW_<it.name>_in_<it.inName><it.tokenIndex>}, words64=it.bits)> /** Load up the static bitsets for following set for error recovery. * \remark * These are static after the parser is generated, hence they are static * delcarations in the parser and are thread safe after initialization. */staticvoid <name>LoadFollowSets(){ <bitsets:bitset(name={FOLLOW_<it.name>_in_<it.inName><it.tokenIndex>}, words64=it.bits)> return;}/** Free the bitsets used for error recovery and prediction. */staticvoid <name>FreeFollowSets(){ <bitsets:bitsetFree(name={FOLLOW_<it.name>_in_<it.inName><it.tokenIndex>})> return;}<if(cyclicDFAs)>/* ========================================================================= * DFA tables for the parser */<cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !>/* ========================================================================= * End of DFA tables for the parser */<endif> /* ============================================== * Parsing rules */<rules; separator="\n\n">/* End of parsing rules * ============================================== *//* ============================================== * Syntactic predicates */<synpreds:{p | <synpred(predname=p)>}>/* End of syntactic predicates * ============================================== */ >>parser( grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, ASTLabelType, superClass="Parser", labelType="pANTLR3_COMMON_TOKEN", members={<actions.parser.members>} ) ::= <<<genericParser(inputStreamType="pANTLR3_COMMON_TOKEN_STREAM", ...)>>>/** How to generate a tree parser; same as parser except the input * stream is a different type. */treeParser( grammar, name, scopes, tokens, tokenNames, globalAction, rules, numRules, bitsets, labelType={<ASTLabelType>}, ASTLabelType="pANTLR3_BASE_TREE", superClass="TreeParser", members={<actions.treeparser.members>} ) ::= <<<genericParser(inputStreamType="pANTLR3_COMMON_TREE_NODE_STREAM", ...)>>>/** A simpler version of a rule template that is specific to the imaginary * rules created for syntactic predicates. As they never have return values * nor parameters etc..., just give simplest possible method. Don't do * any of the normal memoization stuff in here either; it's a waste. * As predicates cannot be inlined into the invoking rule, they need to * be in a rule by themselves. */synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::=<<// $ANTLR start <ruleName>static void <ruleName>_fragment(p<name> ctx <ruleDescriptor.parameterScope:parameterScope(scope=it)>) { <if(trace)> printf("enter <ruleName> %d failed = %d, backtracking = %d\\n",input.LT(1),failed,backtracking); <block> printf("exit <ruleName> %d, failed = %d, backtracking = %d\\n",input.LT(1),failed,backtracking); <else> <block><endif><ruleCleanUp()>}// $ANTLR end <ruleName>/* End synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) */>>synpred(predname) ::= <<static ANTLR3_BOOLEAN <predname>(p<name> ctx) { ANTLR3_UINT64 start; ANTLR3_BOOLEAN success; backtracking++; <@start()> start = markMyWords(); <predname>_fragment(ctx); // can never throw exception success = !(failedFlag); rewindInput(start); <@stop()> backtracking--; failedFlag = ANTLR3_FALSE; return success;}<\n>>>lexerSynpred(predname) ::= <<<synpred(predname)>>>ruleMemoization(name) ::= <<<if(memoize)>if ( backtracking>0 && haveAlreadyParsedRule(<ruleDescriptor.index>) ){ return <ruleReturnValue()>; }<endif>>>/** How to test for failure and return from rule */checkRuleBacktrackFailure() ::= <<<if(backtracking)>/* checkRuleBacktrackFailure() */if (hasFailed()){ return <ruleReturnValue()>;}<endif>>>/** This rule has failed, exit indicating failure during backtrack */ruleBacktrackFailure() ::= <<<if(backtracking)>/* ruleBacktrackFailure() */if (backtracking>0){ failedFlag = <true()>; return <ruleReturnValue()>;}<endif>>>/** How to generate code for a rule. This includes any return type * data aggregates required for multiple return values. */rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,memoize) ::= <</* rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions) * * $ANTLR start <ruleName> * <fileName>:<description> */static <returnType()><ruleName>(p<name> ctx<if(ruleDescriptor.parameterScope)>, <endif><ruleDescriptor.parameterScope:parameterScope(scope=it)>){ <if(trace)>printf("enter <ruleName> %s failed=%d, backtracking=%d\n", LT(1), backtracking);<endif> <ruleDeclarations()> <ruleLabelDefs()> <ruleInitializations()> <ruleLabelInitializations()> <ruleDescriptor.actions.init> <@preamble()> <ruleMemoization(name=ruleName)> { <block> } <ruleCleanUp()><if(exceptions)> <exceptions:{e|<catch(decl=e.decl,action=e.action)><\n>}><else><if(!emptyRule)><if(actions.(actionScope).rulecatch)> <actions.(actionScope).rulecatch><else> if (hasException()) { preportError(); precover(); }<\n><endif><endif><endif> <if(trace)>System.out.println("exit <ruleName> "+LT(1)+" failed="+failed+" backtracking="+backtracking);<endif> <ruleDescriptor.actions.finally:execAction()> <@postamble()> return <ruleReturnValue()>;}// $ANTLR end <ruleName>>>catch(decl,action) ::= <</* catch(decl,action) */if (hasException() && theException()->type == <e.decl> ){ <e.action>}>>ruleDeclarations() ::= <</* ruleDeclarations() */<if(ruleDescriptor.hasMultipleReturnValues)><returnType()> retval;<else><ruleDescriptor.returnScope.attributes:{ a |<a.type> <a.name> = <if(a.initValue)><a.initValue><else><initValue(a.type)><endif>;}><endif><if(memoize)>ANTLR3_UINT64 <ruleDescriptor.name>_StartIndex;<endif>>>ruleInitializations() ::= <</* Initilalize rule variables */<if(memoize)><ruleDescriptor.name>_StartIndex = theInput()->index(theInput());<endif><ruleDescriptor.useScopes:{<scopeTop(sname=it.name)> = <scopePush(sname=it.name)>;}; separator="\n"><ruleDescriptor.ruleScope:{<scopeTop(sname=it.name)> = <scopePush(sname=it.name)>;}; separator="\n">/* End ruleDeclarations() */>>ruleLabelDefs() ::= <</* ruleLabelDefs() */<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels] :{<labelType> <it.label.text>;}; separator="\n"><[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels] :{pANTLR3_VECTOR list_<it.label.text>;}; separator="\n"><[ruleDescriptor.ruleLabels,ruleDescriptor.ruleListLabels] :ruleLabelDef(label=it); separator="\n"><[ruleDescriptor.allRuleRefsInAltsWithRewrites,ruleDescriptor.allTokenRefsInAltsWithRewrites] :{pANTLR3_VECTOR list_<it>;}; separator="\n">>>ruleLabelInitializations() ::= <</* ruleLabelInitializations() */<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels] :{<it.label.text> = NULL;}; separator="\n"><[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels] :{list_<it.label.text> = NULL;}; separator="\n"><[ruleDescriptor.ruleLabels,ruleDescriptor.ruleListLabels] :ruleLabelInitVal(label=it); separator="\n"><[ruleDescriptor.allRuleRefsInAltsWithRewrites,ruleDescriptor.allTokenRefsInAltsWithRewrites] :{list_<it>=ctx->vectors->newVector(ctx->vectors);}; separator="\n">>>ruleReturnValue() ::= <<<if(!ruleDescriptor.isSynPred)><if(ruleDescriptor.hasReturnValue)><if(ruleDescriptor.hasSingleReturnValue)><ruleDescriptor.singleValueReturnName><else>retval<endif><endif><endif>>>ruleCleanUp() ::= <</* ruleCleanUp() */<if(memoize)><if(backtracking)>if ( backtracking>0 ) { memoize(input, <ruleDescriptor.index>, <ruleDescriptor.name>_StartIndex); }<endif><endif>goto rule<ruleDescriptor.name>Ex; /* Prevent compiler warnings */rule<ruleDescriptor.name>Ex: ;<ruleDescriptor.useScopes:{<scopePop(sname=it)>;}; separator="\n"><ruleDescriptor.ruleScope:{<scopePop(sname=it.name)>;}; separator="\n"><if(ruleDescriptor.hasMultipleReturnValues)><endif>>>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -