📄 c.stg
字号:
* any deallocation when this token is finally used up. */ lexer ->token = NULL; lexer->rec ->error = ANTLR3_FALSE; /* Start out without an exception */ lexer->rec ->failed = ANTLR3_FALSE; /* Record the start of the token in our input stream. */ lexer->tokenStartCharIndex = lexer->input->istream->index(lexer->input->istream); lexer->tokenStartCharPositionInLine = lexer->input->getCharPositionInLine(lexer->input); lexer->tokenStartLine = lexer->input->getLine(lexer->input); lexer->text = NULL; /* Now call the matching rules and see if we can generate a new token */ for (;;) { if (lexer->input->istream->_LA(lexer->input->istream, 1) == ANTLR3_CHARSTREAM_EOF) { /* Reached the end of the stream, nothing more to do. */ pANTLR3_COMMON_TOKEN teof = &(toksource->eofToken); teof->setStartIndex (teof, lexer->getCharIndex(lexer)); teof->setStopIndex (teof, lexer->getCharIndex(lexer)); teof->setLine (teof, lexer->getLine(lexer)); return teof; } lexer->token = NULL; lexer->rec->error = ANTLR3_FALSE; /* Start out without an exception */ { ANTLR3_UINT64 m; m = lexer->input->istream->mark(lexer->input->istream); lexer->rec->backtracking = 1; /* No exceptions */ lexer->rec->failed = ANTLR3_FALSE; /* Call the generated lexer, see if it can get a new token together. */ lexer->mTokens(lexer->ctx); lexer->rec->backtracking = 0; <! mTokens backtracks with synpred at BACKTRACKING==2 and we set the synpredgate to allow actions at level 1. !> if (lexer->rec->failed == ANTLR3_TRUE) { lexer->input->istream->rewind(lexer->input->istream, m); lexer->input->istream->consume(lexer->input->istream); <! advance one char and try again !> } else { lexer->emit(lexer); /* Assemble the token and emit it to the stream */ return lexer->token; } } }}>>filteringActionGate() ::= "BACKTRACKING==1"/** How to generate a parser */genericParser( grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, inputStreamType, superClass, ASTLabelType="pANTLR3_BASE_TREE", labelType, members ) ::= <</** \brief Table of all token names in symbolic order, mainly used for * error reporting. */static pANTLR3_UINT8 <name>TokenNames[] = { (pANTLR3_UINT8) "\<invalid>", /* String to print to indicate an invalid token */ (pANTLR3_UINT8) "\<EOR>", (pANTLR3_UINT8) "\<DOWN>", (pANTLR3_UINT8) "\<UP>", <tokenNames:{(pANTLR3_UINT8) <it>}; separator=",\n"> }; <@members> <@end>/* Forward declare the locally static matching functions we have generated. */<rules:{r | static <headerReturnType(ruleDescriptor=r.ruleDescriptor)> <r.ruleDescriptor.name> (p<name> ctx<if(r.ruleDescriptor.parameterScope)>, <endif><r.ruleDescriptor.parameterScope:parameterScope(scope=it)>);}; separator="\n";>static void <name>Free(p<name> ctx);/* Function to initialize bitset APIs */static void <name>LoadFollowSets();/* For use in tree output where we are accumulating rule labels via label += ruleRef * we need a function that knows how to free a return scope when the list is destroyed. * We cannot just use ANTLR3_FREE because in debug tracking mode, this is a macro. */static void ANTLR3_CDECL freeScope(void * scope){ ANTLR3_FREE(scope);}/** \brief Name of the gramar file that generated this code */static unsigned char fileName[] = "<fileName>";/** \brief Return the name of the grammar file that generated this code. */static unsigned char * getGrammarFileName(){ 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><if(memoize)> /* Create a LIST for recording rule memos. */<if(TREE_PARSER)> ctx->pTreeParser->rec->ruleMemo = antlr3IntTrieNew(15); /* 16 bit depth is enough for 32768 rules! */<\n><else> ctx->pParser->rec->ruleMemo = antlr3IntTrieNew(15); /* 16 bit depth is enough for 32768 rules! */<\n><endif><endif> /* Install the token table */ RECOGNIZER->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 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;}<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>>>synpred(predname) ::= <<static ANTLR3_BOOLEAN <predname>(p<name> ctx) { ANTLR3_UINT64 start; ANTLR3_BOOLEAN success; BACKTRACKING++; <@start()> start = MARK(); <predname>_fragment(ctx); // can never throw exception success = !(FAILEDFLAG); REWIND(start); <@stop()> BACKTRACKING--; FAILEDFLAG = ANTLR3_FALSE; return success;}<\n>>>lexerSynpred(predname) ::= <<<synpred(predname)>>>ruleMemoization(name) ::= <<<if(memoize)>if ( BACKTRACKING>0 && HAVEPARSEDRULE(<ruleDescriptor.index>) ){ return <ruleReturnValue()>; }<endif>>>/** How to test for failure and return from rule */checkRuleBacktrackFailure() ::= <<<if(backtracking)>if (HASFAILED()){ return <ruleReturnValue()>;}<endif>>>/** This rule has failed, exit indicating failure during backtrack */ruleBacktrackFailure() ::= <<<if(backtracking)>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,finally,memoize) ::= <</** * $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()> <ruleDescriptor.actions.declarations> <ruleLabelDefs()> <ruleInitializations()> <ruleLabelInitializations()> <ruleDescriptor.actions.init> <@preamble()> <ruleMemoization(name=ruleName)> { <block> } <ruleCleanUp()><if(exceptions)> if (HASEXCEPTION()) { <exceptions:{e|<catch(decl=e.decl,action=e.action)><\n>}> }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -