/* [The "BSD licence"] Copyright (c) 2005-2006 Terence Parr Templates and C runtime Copyright (c) 2006-2007 Jim Idle All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * This code generating template and the associated C runtime was produced by: * Jim Idle jimi|hereisanat|idle|dotgoeshere|ws. * If it causes the destruction of the Universe, it will be pretty cool so long as * I am in a different one at the time. */ group C implements ANTLRCore ; cTypeInitMap ::= [ "int" : "0", // Integers start out being 0 "long" : "0", // Longs start out being 0 "float" : "0.0F", // Floats start out being 0 "double" : "0.0D", // Doubles start out being 0 "ANTLR3_BOOLEAN" : "ANTLR3_FALSE", // Booleans start out being Antlr C for false "byte" : "0", // Bytes start out being 0 "short" : "0", // Shorts start out being 0 "char" : "0", // Chars start out being 0 default : "NULL" // Anything other than an atomic type (above) is a NULL (probably NULL pointer). ] leadIn(type) ::= << /** \file * This file was generated by $ANTLR version * * - From the grammar source file : * - On : * - for the lexer : Lexer * - for the parser : Parser * - for the tree parser : TreeParser * * Editing it, at least manually, is not wise. * * C language generator and runtime by Jim Idle, jimi|hereisanat|idle|dotgoeshere|ws. * * >> /** The overall file structure of a recognizer; stores methods for rules * and cyclic DFAs plus support code. */ outputFile( LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment, recognizer, name, tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, buildAST, rewriteMode, profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass, literals ) ::= << */ /* ============================================================================= * This is what the grammar programmer asked us to put at the top of every file. */ /* End of Header action. * ============================================================================= */ /* ----------------------------------------- * Include the ANTLR3 generated header file. */ #include ".h" /* ----------------------------------------- */ /** String literals used by that we must do things like MATCHS() with. * C will normally just lay down 8 bit characters, and you can use L"xxx" to * get wchar_t, but wchar_t is 16 bits on Windows, which is not UTF32 and so * we perform this little trick of defining the literals as arrays of UINT32 * and passing in the address of these. */ [] = ;}; separator="\n"> /* MACROS that hide the C interface implementations from the * generated code, which makes it a little more understandable to the human eye. * I am very much against using C pre-processor macros for function calls and bits * of code as you cannot see what is happening when single stepping in debuggers * and so on. The exception (in my book at least) is for generated code, where you are * not maintaining it, but may wish to read and understand it. If you single step it, you know that input() * hides some indirect calls, but is always referring to the input stream. This is * probably more readable than ctx->input->istream->input(snarfle0->blarg) and allows me to rejig * the runtime interfaces without changing the generated code too often, without * confusing the reader of the generated output, who may not wish to know the gory * details of the interface inheritance. */ #define CTX ctx /* Aids in accessing scopes for grammar programmers */ #undef SCOPE_TYPE #undef SCOPE_STACK #undef SCOPE_TOP #define SCOPE_TYPE(scope) p_##scope##_SCOPE #define SCOPE_STACK(scope) p_##scope##Stack #define SCOPE_TOP(scope) ctx->p_##scope##Top #define SCOPE_SIZE(scope) (ctx->SCOPE_STACK(scope)->size(ctx->SCOPE_STACK(scope))) #define SCOPE_INSTANCE(scope, i) (ctx->SCOPE_STACK(scope)->get(ctx->SCOPE_STACK(scope),i)) /* Macros for accessing things in a lexer */ #undef LEXER #undef RECOGNIZER #undef RULEMEMO #undef GETCHARINDEX #undef GETLINE #undef GETCHARPOSITIONINLINE #undef EMIT #undef EMITNEW #undef MATCHC #undef MATCHS #undef MATCHRANGE #undef LTOKEN #undef HASFAILED #undef FAILEDFLAG #undef INPUT #undef STRSTREAM #undef LA #undef HASEXCEPTION #undef EXCEPTION #undef CONSTRUCTEX #undef CONSUME #undef LRECOVER #undef MARK #undef REWIND #undef REWINDLAST #undef BACKTRACKING #undef MATCHANY #undef MEMOIZE #undef HAVEPARSEDRULE #undef GETTEXT #undef INDEX #undef SEEK #undef PUSHSTREAM #undef POPSTREAM #undef SETTEXT #undef SETTEXT8 #define LEXER ctx->pLexer #define RECOGNIZER LEXER->rec #define LEXSTATE RECOGNIZER->state #define TOKSOURCE LEXSTATE->tokSource #define GETCHARINDEX() LEXER->getCharIndex(LEXER) #define GETLINE() LEXER->getLine(LEXER) #define GETTEXT() LEXER->getText(LEXER) #define GETCHARPOSITIONINLINE() LEXER->getCharPositionInLine(LEXER) #define EMIT() LEXSTATE->type = _type; LEXER->emit(LEXER) #define EMITNEW(t) LEXER->emitNew(LEXER, t) #define MATCHC(c) LEXER->matchc(LEXER, c) #define MATCHS(s) LEXER->matchs(LEXER, s) #define MATCHRANGE(c1,c2) LEXER->matchRange(LEXER, c1, c2) #define MATCHANY() LEXER->matchAny(LEXER) #define LTOKEN LEXSTATE->token #define HASFAILED() (LEXSTATE->failed == ANTLR3_TRUE) #define BACKTRACKING LEXSTATE->backtracking #define FAILEDFLAG LEXSTATE->failed #define INPUT LEXER->input #define STRSTREAM INPUT #define ISTREAM INPUT->istream #define INDEX() ISTREAM->index(ISTREAM) #define SEEK(n) ISTREAM->seek(ISTREAM, n) #define EOF_TOKEN &(LEXSTATE->tokSource->eofToken) #define HASEXCEPTION() (LEXSTATE->error == ANTLR3_TRUE) #define EXCEPTION LEXSTATE->exception #define CONSTRUCTEX() RECOGNIZER->exConstruct(RECOGNIZER) #define LRECOVER() LEXER->recover(LEXER) #define MARK() ISTREAM->mark(ISTREAM) #define REWIND(m) ISTREAM->rewind(ISTREAM, m) #define REWINDLAST() ISTREAM->rewindLast(ISTREAM) #define MEMOIZE(ri,si) RECOGNIZER->memoize(RECOGNIZER, ri, si) #define HAVEPARSEDRULE(r) RECOGNIZER->alreadyParsedRule(RECOGNIZER, r) #define PUSHSTREAM(str) LEXER->pushCharStream(LEXER, str) #define POPSTREAM() LEXER->popCharStream(LEXER) #define SETTEXT(str) LEXSTATE->text = str #define SKIP() LEXSTATE->token = &(TOKSOURCE->skipToken) #define USER1 LEXSTATE->user1 #define USER2 LEXSTATE->user2 #define USER3 LEXSTATE->user3 #define CUSTOM LEXSTATE->custom #define RULEMEMO LEXSTATE->ruleMemo #define DBG RECOGNIZER->debugger /* If we have been told we can rely on the standard 8 bit or 16 bit input * stream, then we can define our macros to use the direct pointers * in the input object, which is much faster than indirect calls. This * is really only significant to lexers with a lot of fragment rules (which * do not place LA(1) in a temporary at the moment) and even then * only if there is a lot of input (order of say 1M or so). */ #if defined(ANTLR3_INLINE_INPUT_ASCII) || defined(ANTLR3_INLINE_INPUT_UTF16) # ifdef ANTLR3_INLINE_INPUT_ASCII /* 8 bit "ASCII" (actually any 8 bit character set) */ # define NEXTCHAR ((pANTLR3_UINT8)(INPUT->nextChar)) # define DATAP ((pANTLR3_UINT8)(INPUT->data)) # else # define NEXTCHAR ((pANTLR3_UINT16)(INPUT->nextChar)) # define DATAP ((pANTLR3_UINT16)(INPUT->data)) # endif # define LA(n) ((NEXTCHAR + n) > (DATAP + INPUT->sizeBuf) ? ANTLR3_CHARSTREAM_EOF : (ANTLR3_UCHAR)(*(NEXTCHAR + n - 1))) # define CONSUME() \ { \ if (NEXTCHAR \< (DATAP + INPUT->sizeBuf)) \ { \ INPUT->charPositionInLine++; \ if ((ANTLR3_UCHAR)(*NEXTCHAR) == INPUT->newlineChar) \ { \ INPUT->line++; \ INPUT->charPositionInLine = 0; \ INPUT->currentLine = (void *)(NEXTCHAR + 1); \ } \ INPUT->nextChar = (void *)(NEXTCHAR + 1); \ } \ } #else // Pick up the input character by calling the input stream implementation. // #define CONSUME() INPUT->istream->consume(INPUT->istream) #define LA(n) INPUT->istream->_LA(INPUT->istream, n) #endif /* Macros for accessing things in the parser */ #undef PARSER #undef RECOGNIZER #undef HAVEPARSEDRULE #undef MEMOIZE #undef INPUT #undef STRSTREAM #undef HASEXCEPTION #undef EXCEPTION #undef MATCHT #undef MATCHANYT #undef FOLLOWSTACK #undef FOLLOWPUSH #undef FOLLOWPOP #undef PRECOVER #undef PREPORTERROR #undef LA #undef LT #undef CONSTRUCTEX #undef CONSUME #undef MARK #undef REWIND #undef REWINDLAST #undef PERRORRECOVERY #undef HASFAILED #undef FAILEDFLAG #undef RECOVERFROMMISMATCHEDSET #undef RECOVERFROMMISMATCHEDELEMENT #undef INDEX #undef ADAPTOR #undef SEEK #undef RULEMEMO #undef DBG #define PARSER ctx->pParser #define RECOGNIZER PARSER->rec #define PSRSTATE RECOGNIZER->state #define HAVEPARSEDRULE(r) RECOGNIZER->alreadyParsedRule(RECOGNIZER, r) #define MEMOIZE(ri,si) RECOGNIZER->memoize(RECOGNIZER, ri, si) #define INPUT PARSER->tstream #define STRSTREAM INPUT #define ISTREAM INPUT->istream #define INDEX() ISTREAM->index(INPUT->istream) #define HASEXCEPTION() (PSRSTATE->error == ANTLR3_TRUE) #define EXCEPTION PSRSTATE->exception #define MATCHT(t, fs) RECOGNIZER->match(RECOGNIZER, t, fs) #define MATCHANYT() RECOGNIZER->matchAny(RECOGNIZER) #define FOLLOWSTACK PSRSTATE->following #define FOLLOWPUSH(x) FOLLOWSTACK->push(FOLLOWSTACK, ((void *)(&(x))), NULL) #define FOLLOWPOP() FOLLOWSTACK->pop(FOLLOWSTACK) #define PRECOVER() RECOGNIZER->recover(RECOGNIZER) #define PREPORTERROR() RECOGNIZER->reportError(RECOGNIZER) #define LA(n) INPUT->istream->_LA(ISTREAM, n) #define LT(n) INPUT->_LT(INPUT, n) #define CONSTRUCTEX() RECOGNIZER->exConstruct(RECOGNIZER) #define CONSUME() ISTREAM->consume(ISTREAM) #define MARK() ISTREAM->mark(ISTREAM) #define REWIND(m) ISTREAM->rewind(ISTREAM, m) #define REWINDLAST() ISTREAM->rewindLast(ISTREAM) #define SEEK(n) ISTREAM->seek(ISTREAM, n) #define PERRORRECOVERY PSRSTATE->errorRecovery #define FAILEDFLAG PSRSTATE->failed #define HASFAILED() (FAILEDFLAG == ANTLR3_TRUE) #define BACKTRACKING PSRSTATE->backtracking #define RECOVERFROMMISMATCHEDSET(s) RECOGNIZER->recoverFromMismatchedSet(RECOGNIZER, s) #define RECOVERFROMMISMATCHEDELEMENT(e) RECOGNIZER->recoverFromMismatchedElement(RECOGNIZER, s) #define ADAPTOR ctx->adaptor #define RULEMEMO PSRSTATE->ruleMemo #define DBG RECOGNIZER->debugger /* Macros for accessing things in the parser */ #undef PARSER #undef RECOGNIZER #undef HAVEPARSEDRULE #undef INPUT #undef STRSTREAM #undef HASEXCEPTION #undef EXCEPTION #undef MATCHT #undef MATCHANYT #undef FOLLOWSTACK #undef FOLLOWPUSH #undef FOLLOWPOP #undef PRECOVER #undef PREPORTERROR #undef LA #undef LT #undef CONSTRUCTEX #undef CONSUME #undef MARK #undef REWIND #undef REWINDLAST #undef PERRORRECOVERY #undef HASFAILED #undef FAILEDFLAG #undef RECOVERFROMMISMATCHEDSET #undef RECOVERFROMMISMATCHEDELEMENT #undef BACKTRACKING #undef ADAPTOR #undef RULEMEMO #undef SEEK #undef INDEX #undef DBG #define PARSER ctx->pTreeParser #define RECOGNIZER PARSER->rec #define PSRSTATE RECOGNIZER->state #define HAVEPARSEDRULE(r) RECOGNIZER->alreadyParsedRule(RECOGNIZER, r) #define INPUT PARSER->ctnstream #define ISTREAM INPUT->tnstream->istream #define STRSTREAM INPUT->tnstream #define HASEXCEPTION() (PSRSTATE->error == ANTLR3_TRUE) #define EXCEPTION PSRSTATE->exception #define MATCHT(t, fs) RECOGNIZER->match(RECOGNIZER, t, fs) #define MATCHANYT() RECOGNIZER->matchAny(RECOGNIZER) #define FOLLOWSTACK PSRSTATE->following #define FOLLOWPUSH(x) FOLLOWSTACK->push(FOLLOWSTACK, ((void *)(&(x))), NULL) #define FOLLOWPOP() FOLLOWSTACK->pop(FOLLOWSTACK) #define PRECOVER() RECOGNIZER->recover(RECOGNIZER) #define PREPORTERROR() RECOGNIZER->reportError(RECOGNIZER) #define LA(n) ISTREAM->_LA(ISTREAM, n) #define LT(n) INPUT->tnstream->_LT(INPUT->tnstream, n) #define CONSTRUCTEX() RECOGNIZER->exConstruct(RECOGNIZER) #define CONSUME() ISTREAM->consume(ISTREAM) #define MARK() ISTREAM->mark(ISTREAM) #define REWIND(m) ISTREAM->rewind(ISTREAM, m) #define REWINDLAST() ISTREAM->rewindLast(ISTREAM) #define PERRORRECOVERY PSRSTATE->errorRecovery #define FAILEDFLAG PSRSTATE->failed #define HASFAILED() (FAILEDFLAG == ANTLR3_TRUE) #define BACKTRACKING PSRSTATE->backtracking #define RECOVERFROMMISMATCHEDSET(s) RECOGNIZER->recoverFromMismatchedSet(RECOGNIZER, s) #define RECOVERFROMMISMATCHEDELEMENT(e) RECOGNIZER->recoverFromMismatchedElement(RECOGNIZER, s) #define ADAPTOR INPUT->adaptor #define RULEMEMO PSRSTATE->ruleMemo #define SEEK(n) ISTREAM->seek(ISTREAM, n) #define INDEX() ISTREAM->index(ISTREAM) #define DBG RECOGNIZER->debugger #define TOKTEXT(tok, txt) tok, (pANTLR3_UINT8)txt /* The 4 tokens defined below may well clash with your own #defines or token types. If so * then for the present you must use different names for your defines as these are hard coded * in the code generator. It would be better not to use such names internally, and maybe * we can change this in a forthcoming release. I deliberately do not #undef these * here as this will at least give you a redefined error somewhere if they clash. */ #define UP ANTLR3_TOKEN_UP #define DOWN ANTLR3_TOKEN_DOWN #define EOR ANTLR3_TOKEN_EOR #define INVALID ANTLR3_TOKEN_INVALID /* ============================================================================= * Functions to create and destroy scopes. First come the rule scopes, followed * by the global declared scopes. */ }> }> /* ============================================================================= */ /* ============================================================================= * Start of recognizer */ /* End of code * ============================================================================= */ >> headerFileExtension() ::= ".h" headerFile( LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment, recognizer, name, tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, buildAST, rewriteMode, profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass, literals ) ::= << * The parser * The lexer * The tree parser has the callable functions (rules) shown below, * which will invoke the code for the associated rule in the source grammar * assuming that the input stream is pointing to a token/text stream that could begin * this rule. * * For instance if you call the first (topmost) rule in a parser grammar, you will * get the results of a full parse, but calling a rule half way through the grammar will * allow you to pass part of a full token stream to the parser, such as for syntax checking * in editors and so on. * * The parser entry points are called indirectly (by function pointer to function) via * a parser context typedef p, which is returned from a call to New(). * * As this is a generated lexer, it is unlikely you will call it 'manually'. However * the methods are provided anyway. * * The methods in p are as follows: * * - p->(p)}; separator="\n * "> * * The return type for any particular rule is of course determined by the source * grammar file. */ #ifndef __H #define __H /* ============================================================================= * Standard antlr3 C runtime definitions */ #include \ /* End of standard antlr 3 runtime definitions * ============================================================================= */ #ifdef __cplusplus extern "C" { #endif // Forward declare the context typedef so that we can use it before it is // properly defined. Delegators and delegates (from import statements) are // interdependent and their context structures contain pointers to each other // C only allows such things to be declared if you pre-declare the typedef. // typedef struct _Ctx_struct , * p; // Include delegate definition header files // .h>}; separator="\n"> #ifdef ANTLR3_WINDOWS // Disable: Unreferenced parameter, - Rules with parameters that are not used // constant conditional, - ANTLR realizes that a prediction is always true (synpred usually) // initialized but unused variable - tree rewrite variables declared but not needed // Unreferenced local variable - lexer rule declares but does not always use _type // potentially unitialized variable used - retval always returned from a rule // unreferenced local function has been removed - susually getTokenNames or freeScope, they can go without warnigns // // These are only really displayed at warning level /W4 but that is the code ideal I am aiming at // and the codegen must generate some of these warnings by necessity, apart from 4100, which is // usually generated when a parser rule is given a parameter that it does not use. Mostly though // this is a matter of orthogonality hence I disable that one. // #pragma warning( disable : 4100 ) #pragma warning( disable : 4101 ) #pragma warning( disable : 4127 ) #pragma warning( disable : 4189 ) #pragma warning( disable : 4505 ) #endif /* ======================== * BACKTRACKING IS ENABLED * ======================== */ }> }> }> }> }> // Include delegator definition header files // .h>}; separator="\n"> /** Context tracking structure for */ struct _Ctx_struct { /** Built in ANTLR3 context tracker contains all the generic elements * required for context tracking. */ pANTLR3_PARSER pParser; pANTLR3_LEXER pLexer; pANTLR3_TREE_PARSER pTreeParser; ;}; separator="\n"> ;}; separator="\n"> }; separator="\n\n"> }> (*m) (struct _Ctx_struct * ctx, );}; separator="\n";> (*) (struct _Ctx_struct * ctx, );}; separator="\n";> // Delegated rules (*)(struct _Ctx_struct * ctx, );}; separator="\n";> const char * (*getGrammarFileName)(); void (*free) (struct _Ctx_struct * ctx); <@members> <@end> }; // Function protoypes for the constructor functions that external translation units // such as delegators and delegates may wish to call. // ANTLR3_API p New ( instream }>); ANTLR3_API p NewSSD ( instream, pANTLR3_RECOGNIZER_SHARED_STATE state }>); extern pANTLR3_UINT8 TokenNames[]; /** Symbolic definitions of all the tokens that the will work with. * \{ * * Antlr will define EOF, but we can't use that as it it is too common in * in C header files and that would be confusing. There is no way to filter this out at the moment * so we just undef it here for now. That isn't the value we get back from C recognizers * anyway. We are looking for ANTLR3_TOKEN_EOF. */ #ifdef EOF #undef EOF #endif #ifdef Tokens #undef Tokens #endif }; separator="\n"> #ifdef EOF #undef EOF #define EOF ANTLR3_TOKEN_EOF #endif #ifndef TOKENSOURCE #define TOKENSOURCE(lxr) lxr->pLexer->rec->state->tokSource #endif /* End of token definitions for * ============================================================================= */ /** \} */ #ifdef __cplusplus } #endif #endif /* END - Note:Keep extra line feed to satisfy UNIX systems */ >> inputType() ::=<< pANTLR3_INPUT_STREAM pANTLR3_COMMON_TOKEN_STREAM pANTLR3_COMMON_TREE_NODE_STREAM >> grammarType() ::= << parser lexer tree parser >> mainName() ::= << >> headerReturnScope(ruleDescriptor) ::= "" headerReturnType(ruleDescriptor) ::= << void >> // Produce the lexer output // lexer( grammar, name, tokens, scopes, rules, numRules, labelType="pANTLR3_COMMON_TOKEN", filterMode, superClass) ::= << /* Forward declare implementation function for ANTLR3_TOKEN_SOURCE interface when * this is a filter mode lexer. */ static pANTLR3_COMMON_TOKEN NextToken (pANTLR3_TOKEN_SOURCE toksource); /* Override the normal MEMOIZE and HAVEALREADYPARSED macros as this is a filtering * lexer. In filter mode, the memoizing and backtracking are gated at BACKTRACKING > 1 rather * than just BACKTRACKING. In some cases this might generate code akin to: * if (BACKTRACKING) if (BACKTRACKING > 1) memoize. * However, I assume that the C compilers/optimizers are smart enough to work this one out * these days - Jim */ #undef MEMOIZE #define MEMOIZE(ri,si) if (BACKTRACKING>1) { RECOGNIZER->memoize(RECOGNIZER, ri, si) } #undef HAVEPARSEDRULE #define HAVEPARSEDRULE(r) if (BACKTRACKING>1) { RECOGNIZER->alreadyParsedRule(RECOGNIZER, r) } /* Forward declare the locally static matching functions we have generated and any predicate functions. */ m (p ctx, );}; separator="\n";> static void Free(p ctx); /* ========================================================================= * Lexer matching rules end. * ========================================================================= */ }> static void Free (p ctx) { if (RULEMEMO != NULL) { RULEMEMO->free(RULEMEMO); RULEMEMO = NULL; } // Free the lexers that we delegated to // functions to. NULL the state so we only free it once. // ->pLexer->rec->state = NULL; ctx->->free(ctx->);}; separator="\n"> LEXER->free(LEXER); ANTLR3_FREE(ctx); } /** \brief Name of the grammar file that generated this code */ static const char fileName[] = ""; /** \brief Return the name of the grammar file that generated this code. */ static const char * getGrammarFileName() { return fileName; } /** \brief Create a new lexer called * * \param[in] instream Pointer to an initialized input stream * \return * - Success p initialized for the lex start * - Fail NULL */ ANTLR3_API p New ( instream }>) { // See if we can create a new lexer with the standard constructor // return NewSSD(instream, NULL}>); } /** \brief Create a new lexer called * * \param[in] instream Pointer to an initialized input stream * \param[state] state Previously created shared recognizer stat * \return * - Success p initialized for the lex start * - Fail NULL */ ANTLR3_API p NewSSD (pANTLR3_INPUT_STREAM instream, pANTLR3_RECOGNIZER_SHARED_STATE state }>) { p ctx; // Context structure we will build and return ctx = (p) ANTLR3_CALLOC(1, sizeof()); if (ctx == NULL) { // Failed to allocate memory for lexer context return NULL; } /* ------------------------------------------------------------------- * Memory for basic structure is allocated, now to fill in * in base ANTLR3 structures. We initialize the function pointers * for the standard ANTLR3 lexer 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 .h here so you can get a sense * of what goes where. */ /* Create a base lexer, using the supplied input stream */ ctx->pLexer = antlr3LexerNewStream(ANTLR3_SIZE_HINT, instream, state); /* Check that we allocated the memory correctly */ if (ctx->pLexer == NULL) { ANTLR3_FREE(ctx); return NULL; } // Create a LIST for recording rule memos. // ctx->pLexer->rec->ruleMemo = antlr3IntTrieNew(15); /* 16 bit depth is enough for 32768 rules! */ /* Install the implementation of our interface */ ctx->m = m;}; separator="\n";> /** When the nextToken() call is made to this lexer's pANTLR3_TOKEN_SOURCE * it will call mTokens() in this generated code, and will pass it the ctx * pointer of this lexer, not the context of the base lexer, so store that now. */ ctx->pLexer->ctx = ctx; /**Install the token matching function */ ctx->pLexer->mTokens = (void (*) (void *))(mTokens); ctx->getGrammarFileName = getGrammarFileName; ctx->free = Free; // Initialize the lexers that we are going to delegate some // functions to. // = NewSSD(instream, ctx->pLexer->rec->state, ctx}>);}; separator="\n"> // Install the pointers back to lexers that will delegate us to perform certain functions // for them. // = ;}; separator="\n"> /* We have filter mode turned on, so install the filtering nextToken function */ ctx->pLexer->rec->state->tokSource->nextToken = NextToken; /* Return the newly built lexer to the caller */ return ctx; } /* ========================================================================= * DFA tables for the lexer */ /* ========================================================================= * End of DFA tables for the lexer */ /* ========================================================================= * Functions to match the lexer grammar defined tokens from the input stream */ /* ========================================================================= * Lexer matching rules end. * ========================================================================= */ /* ========================================================================= * Lexer syntactic predicates */ }> /* ========================================================================= * Lexer syntactic predicates end. * ========================================================================= */ /* End of Lexer code * ================================================ * ================================================ */ >> filteringNextToken() ::= << /** An override of the lexer's nextToken() method that backtracks over mTokens() looking * for matches in lexer filterMode. No error can be generated upon error; just rewind, consume * a token and then try again. BACKTRACKING needs to be set as well. * Make rule memoization happen only at levels above 1 as we start mTokens * at BACKTRACKING==1. */ static pANTLR3_COMMON_TOKEN NextToken(pANTLR3_TOKEN_SOURCE toksource) { pANTLR3_LEXER lexer; pANTLR3_RECOGNIZER_SHARED_STATE state; lexer = (pANTLR3_LEXER)(toksource->super); state = lexer->rec->state; /* Get rid of any previous token (token factory takes care of * any deallocation when this token is finally used up. */ state ->token = NULL; state ->error = ANTLR3_FALSE; /* Start out without an exception */ state ->failed = ANTLR3_FALSE; /* Record the start of the token in our input stream. */ state->tokenStartCharIndex = lexer->input->istream->index(lexer->input->istream); state->tokenStartCharPositionInLine = lexer->input->getCharPositionInLine(lexer->input); state->tokenStartLine = lexer->input->getLine(lexer->input); state->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; } state->token = NULL; state->error = ANTLR3_FALSE; /* Start out without an exception */ { ANTLR3_MARKER m; m = lexer->input->istream->mark(lexer->input->istream); state->backtracking = 1; /* No exceptions */ state->failed = ANTLR3_FALSE; /* Call the generated lexer, see if it can get a new token together. */ lexer->mTokens(lexer->ctx); state->backtracking = 0; if (state->failed == ANTLR3_TRUE) { lexer->input->istream->rewind(lexer->input->istream, m); lexer->input->istream->consume(lexer->input->istream); } else { lexer->emit(lexer); /* Assemble the token and emit it to the stream */ return state->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, rewriteElementType ) ::= << /** \brief Table of all token names in symbolic order, mainly used for * error reporting. */ pANTLR3_UINT8 TokenNames[+4] = { (pANTLR3_UINT8) "\", /* String to print to indicate an invalid token */ (pANTLR3_UINT8) "\", (pANTLR3_UINT8) "\", (pANTLR3_UINT8) "\", }; separator=",\n"> }; <@members> <@end> // Forward declare the locally static matching functions we have generated. // (p ctx, );}; separator="\n";> static void Free(p ctx); // Delegated rules // (p ctx, );}; separator="\n";> /* 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 grammar file that generated this code */ static const char fileName[] = ""; /** \brief Return the name of the grammar file that generated this code. */ static const char * getGrammarFileName() { return fileName; } /** \brief Create a new parser and return a context for it. * * \param[in] instream Pointer to an input stream interface. * * \return Pointer to new parser context upon success. */ ANTLR3_API p New ( instream }>) { // See if we can create a new parser with the standard constructor // return NewSSD(instream, NULL}>); } /** \brief Create a new parser and return a context for it. * * \param[in] instream Pointer to an input stream interface. * * \return Pointer to new parser context upon success. */ ANTLR3_API p NewSSD ( instream, pANTLR3_RECOGNIZER_SHARED_STATE state }>) { p ctx; /* Context structure we will build and return */ ctx = (p) ANTLR3_CALLOC(1, sizeof()); if (ctx == NULL) { // Failed to allocate memory for parser context // return NULL; } /* ------------------------------------------------------------------- * Memory for basic structure is allocated, now to fill in * the base ANTLR3 structures. We initialize 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 .h here, in order that you can get a sense * of what goes where. */ /* Create a base parser/recognizer, using the supplied token stream */ ctx->pParser = antlr3ParserNewStream(ANTLR3_SIZE_HINT, instream->tstream, state); /* Create a base Tree parser/recognizer, using the supplied tree node stream */ ctx->pTreeParser = antlr3TreeParserNewStream(ANTLR3_SIZE_HINT, instream, state); /* Install the implementation of our interface */ = ;}; separator="\n";> // Install the delegated methods so that they appear to be a part of this // parser // = ;}; separator="\n";> ctx->free = Free; ctx->getGrammarFileName = getGrammarFileName; /* Install the scope pushing methods. */ <\n> }> <\n> }> <@apifuncs> <@end> // Initialize the parsers that we are going to delegate some // functions to. // = NewSSD(instream, PSRSTATE, ctx}>);}; separator="\n"> // Install the pointers back to parsers that will delegate us to perform certain functions // for them. // = ;}; separator="\n"> /* Create a LIST for recording rule memos. */ RULEMEMO = antlr3IntTrieNew(15); /* 16 bit depth is enough for 32768 rules! */<\n> /* Install the token table */ PSRSTATE->tokenNames = TokenNames; <@debugStuff()> /* Return the newly built parser to the caller */ return ctx; } /** Free the parser resources */ static void Free(p ctx) { /* Free any scope memory */ <\n>}> <\n>}> <@cleanup> <@end> // Free the parsers that we delegated to // functions to.NULL the state so we only free it once. // ->pParser->rec->state = NULL; ctx->->free(ctx->);}; separator="\n"> if (RULEMEMO != NULL) { RULEMEMO->free(RULEMEMO); RULEMEMO = NULL; } // Free this parser // ctx->pTreeParser->free(ctx->pTreeParser);<\n> ctx->pParser->free(ctx->pParser);<\n> ANTLR3_FREE(ctx); /* Everything is released, so we can return */ return; } /** Return token names used by this * * 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 TokenNames; } /* Declare the bitsets */ _in_}, words64=it.bits)> /* ========================================================================= * DFA tables for the parser */ /* ========================================================================= * End of DFA tables for the parser */ /* ============================================== * Parsing rules */ // Delegated methods that appear to be a part of this // parser // (p ctx, ) \{ return ctx->->(ctx->, }; separator=", ">); \}}; separator="\n"> /* End of parsing rules * ============================================== */ /* ============================================== * Syntactic predicates */ }> /* End of syntactic predicates * ============================================== */ >> parser( grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, ASTLabelType, superClass="Parser", labelType="pANTLR3_COMMON_TOKEN", members={} ) ::= << >> /** 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="pANTLR3_BASE_TREE", superClass="TreeParser", members={} ) ::= << >> /** 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 static void _fragment(p ctx ) { ANTLR3_PRINTF("enter %d failed = %d, backtracking = %d\\n",LT(1),failed,BACKTRACKING); ANTLR3_PRINTF("exit %d, failed = %d, backtracking = %d\\n",LT(1),failed,BACKTRACKING); } // $ANTLR end >> synpred(predname) ::= << static ANTLR3_BOOLEAN (p ctx) { ANTLR3_MARKER start; ANTLR3_BOOLEAN success; BACKTRACKING++; <@start()> start = MARK(); _fragment(ctx); // can never throw exception success = !(FAILEDFLAG); REWIND(start); <@stop()> BACKTRACKING--; FAILEDFLAG = ANTLR3_FALSE; return success; }<\n> >> lexerSynpred(predname) ::= << >> ruleMemoization(name) ::= << if ( (BACKTRACKING>0) && (HAVEPARSEDRULE()) ) { retval.start = 0; <\n> return ; } >> /** How to test for failure and return from rule */ checkRuleBacktrackFailure() ::= << if (HASEXCEPTION()) { goto ruleEx; } if (HASFAILED()) { return ; } >> /** This rule has failed, exit indicating failure during backtrack */ ruleBacktrackFailure() ::= << if (BACKTRACKING>0) { FAILEDFLAG = ; return ; } >> /** 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 * : */ static (p ctx, ) { ANTLR3_PRINTF("enter %s failed=%d, backtracking=%d\n", LT(1), BACKTRACKING); <@preamble()> { } if (HASEXCEPTION()) { <\n>}> } else { <(ruleDescriptor.actions.after):execAction()> } if (HASEXCEPTION()) { PREPORTERROR(); PRECOVER(); <@setErrorReturnValue()> } else { <(ruleDescriptor.actions.after):execAction()> }<\n> System.out.println("exit "+LT(1)+" failed="+failed+" backtracking="+BACKTRACKING); <@postamble()> return ; } /* $ANTLR end */ >> catch(decl,action) ::= << /* catch(decl,action) */ if ((HASEXCEPTION()) && (EXCEPTION->type == ) ) { } >> ruleDeclarations() ::= << retval;<\n> = ; }> ANTLR3_UINT32 _StartIndex; >> ruleInitializations() ::= << /* Initialize rule variables */ _StartIndex = INDEX();<\n> = ;}; separator="\n"> = ;}; separator="\n"> >> ruleLabelDefs() ::= << <[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels] :{ ;}; separator="\n" > <[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels] :{pANTLR3_VECTOR list_;}; separator="\n" > <[ruleDescriptor.ruleLabels,ruleDescriptor.ruleListLabels] :ruleLabelDef(label=it); separator="\n" > >> ruleLabelInitializations() ::= << <[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels] :{ = NULL;}; separator="\n" > <[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels] :{list_ = NULL;}; separator="\n" > <[ruleDescriptor.ruleLabels,ruleDescriptor.ruleListLabels] :ruleLabelInitVal(label=it); separator="\n" > retval.start = LT(1);<\n> >> lexerRuleLabelDefs() ::= << <[ruleDescriptor.tokenLabels, ruleDescriptor.tokenListLabels, ruleDescriptor.ruleLabels] :{ ;}; separator="\n" > ;}; separator="\n"> <[ruleDescriptor.tokenListLabels, ruleDescriptor.ruleListLabels, ruleDescriptor.ruleListLabels] :{pANTLR3_INT_TRIE list_;}; separator="\n" > >> lexerRuleLabelInit() ::= << <[ruleDescriptor.tokenLabels, ruleDescriptor.tokenListLabels, ruleDescriptor.ruleLabels] :{ = NULL;}; separator="\n" > <[ruleDescriptor.tokenListLabels, ruleDescriptor.ruleListLabels, ruleDescriptor.ruleListLabels] :{list_ = antlr3IntTrieNew(31);}; separator="\n" > >> lexerRuleLabelFree() ::= << <[ruleDescriptor.tokenLabels, ruleDescriptor.tokenListLabels, ruleDescriptor.ruleLabels] :{ = NULL;}; separator="\n" > <[ruleDescriptor.tokenListLabels, ruleDescriptor.ruleListLabels, ruleDescriptor.ruleListLabels] :{list_->free(list_);}; separator="\n" > >> ruleReturnValue() ::= << retval >> memoize() ::= << if ( BACKTRACKING>0 ) { MEMOIZE(, _StartIndex); } >> ruleCleanUp() ::= << // This is where rules clean up and exit // goto ruleEx; /* Prevent compiler warnings */ ruleEx: ; retval.stop = LT(-1);<\n> >> scopeClean() ::= << }; separator="\n"> }; separator="\n"> >> /** How to generate a rule in the lexer; naked blocks are used for * fragment rules, which do not produce tokens. */ lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= << // Comes from: /** \brief Lexer rule generated by ANTLR3 * * $ANTLR start * * Looks to match the characters the constitute the token * from the attached input stream. * * * \remark * - lexer->error == ANTLR3_TRUE if an exception was thrown. */ static ANTLR3_INLINE void m(p ctx, ) { ANTLR3_UINT32 _type; System.out.println("enter '"+(char)LA(1)+"' line="+GETLINE()+":"+GETCHARPOSITIONINLINE()+" failed="+failed+" backtracking="+BACKTRACKING); <\n> _type = ; LEXSTATE->type = _type; ANTLR3_FPRINTF(stderr, "exit '%c' line=%d:%d failed = %d, backtracking =%d\n",LA(1),GETLINE(),GETCHARPOSITIONINLINE(),failed,BACKTRACKING); <(ruleDescriptor.actions.after):execAction()> } // $ANTLR end >> /** How to generate code for the implicitly-defined lexer grammar rule * that chooses between lexer rules. */ tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= << /** This is the entry point in to the lexer from an object that * wants to generate the next token, such as a pCOMMON_TOKEN_STREAM */ static void mTokens(p ctx) { <\n> goto ruleTokensEx; /* Prevent compiler warnings */ ruleTokensEx: ; } >> // S U B R U L E S /** A (...) subrule with multiple alternatives */ block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << // : { int alt=; <@predecision()> <@postdecision()> <@prebranch()> switch (alt) { } <@postbranch()> } >> /** A rule block with multiple alternatives */ ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << { // : ANTLR3_UINT32 alt; alt=; <@predecision()> <@postdecision()> switch (alt) { } } >> ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= << // : <@prealt()> <@postalt()> >> /** A special case of a (...) subrule with a single alternative */ blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= << // : <@prealt()> <@postalt()> >> /** A (..)+ block with 1 or more alternatives */ positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << // : { int cnt=0; <@preloop()> for (;;) { int alt=; <@predecision()> <@postdecision()> switch (alt) { default: if ( cnt >= 1 ) { goto loop; } <@earlyExitException()> goto ruleEx; } cnt++; } loop: ; /* Jump to here if this rule does not match */ <@postloop()> } >> earlyExitEx() ::= << /* mismatchedSetEx() */ CONSTRUCTEX(); EXCEPTION->type = ANTLR3_EARLY_EXIT_EXCEPTION; EXCEPTION->name = (void *)ANTLR3_EARLY_EXIT_NAME; <\n> >> positiveClosureBlockSingleAlt ::= positiveClosureBlock /** A (..)* block with 1 or more alternatives */ closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << // : <@preloop()> for (;;) { int alt=; <@predecision()> <@postdecision()> switch (alt) { default: goto loop; /* break out of the loop */ break; } } loop: ; /* Jump out to here if this rule does not match */ <@postloop()> >> closureBlockSingleAlt ::= closureBlock /** Optional blocks (x)? are translated to (x|) by antlr 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 : <@prealt()> break;<\n> >> /** An alternative is just a list of elements; at outermost level */ alt(elements,altNum,description,autoAST,outerAlt,treeLevel,rew) ::= << // : { <@declarations()> <@initializations()> <@cleanup()> } >> // E L E M E N T S /** What to emit when there is no rewrite. For auto build * mode, does nothing. */ noRewrite(rewriteBlockLevel, treeLevel) ::= "" /** Dump the elements one per line */ element() ::= << <@prematch()> <\n> >> /** match a token optionally with a label in front */ tokenRef(token,label,elementIndex,hetero) ::= <<