2 * This C source file was generated by $ANTLR version 3.2 Sep 23, 2009 12:02:23
4 * - From the grammar source file : Lang.g
5 * - On : 2010-02-24 13:27:52
6 * - for the parser : LangParserParser *
7 * Editing it, at least manually, is not wise.
9 * C language generator and runtime by Jim Idle, jimi|hereisanat|idle|dotgoeshere|ws.
13 // [The "BSD licence"]
14 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
15 // http://www.temporal-wave.com
16 // http://www.linkedin.com/in/jimidle
18 // All rights reserved.
20 // Redistribution and use in source and binary forms, with or without
21 // modification, are permitted provided that the following conditions
23 // 1. Redistributions of source code must retain the above copyright
24 // notice, this list of conditions and the following disclaimer.
25 // 2. Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 // 3. The name of the author may not be used to endorse or promote products
29 // derived from this software without specific prior written permission.
31 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 /* -----------------------------------------
43 * Include the ANTLR3 generated header file.
45 #include "LangParser.h"
46 /* ----------------------------------------- */
52 /* MACROS that hide the C interface implementations from the
53 * generated code, which makes it a little more understandable to the human eye.
54 * I am very much against using C pre-processor macros for function calls and bits
55 * of code as you cannot see what is happening when single stepping in debuggers
56 * and so on. The exception (in my book at least) is for generated code, where you are
57 * not maintaining it, but may wish to read and understand it. If you single step it, you know that input()
58 * hides some indirect calls, but is always referring to the input stream. This is
59 * probably more readable than ctx->input->istream->input(snarfle0->blarg) and allows me to rejig
60 * the runtime interfaces without changing the generated code too often, without
61 * confusing the reader of the generated output, who may not wish to know the gory
62 * details of the interface inheritance.
67 /* Aids in accessing scopes for grammar programmers
72 #define SCOPE_TYPE(scope) pLangParser_##scope##_SCOPE
73 #define SCOPE_STACK(scope) pLangParser_##scope##Stack
74 #define SCOPE_TOP(scope) ctx->pLangParser_##scope##Top
75 #define SCOPE_SIZE(scope) ctx->pLangParser_##scope##Stack_limit
76 #define SCOPE_INSTANCE(scope, i) (ctx->SCOPE_STACK(scope)->get(ctx->SCOPE_STACK(scope),i))
78 /* Macros for accessing things in the parser
103 #undef PERRORRECOVERY
106 #undef RECOVERFROMMISMATCHEDSET
107 #undef RECOVERFROMMISMATCHEDELEMENT
114 #define PARSER ctx->pParser
115 #define RECOGNIZER PARSER->rec
116 #define PSRSTATE RECOGNIZER->state
117 #define HAVEPARSEDRULE(r) RECOGNIZER->alreadyParsedRule(RECOGNIZER, r)
118 #define MEMOIZE(ri,si) RECOGNIZER->memoize(RECOGNIZER, ri, si)
119 #define INPUT PARSER->tstream
120 #define STRSTREAM INPUT
121 #define ISTREAM INPUT->istream
122 #define INDEX() ISTREAM->index(INPUT->istream)
123 #define HASEXCEPTION() (PSRSTATE->error == ANTLR3_TRUE)
124 #define EXCEPTION PSRSTATE->exception
125 #define MATCHT(t, fs) RECOGNIZER->match(RECOGNIZER, t, fs)
126 #define MATCHANYT() RECOGNIZER->matchAny(RECOGNIZER)
127 #define FOLLOWSTACK PSRSTATE->following
128 #define FOLLOWPUSH(x) FOLLOWSTACK->push(FOLLOWSTACK, ((void *)(&(x))), NULL)
129 #define FOLLOWPOP() FOLLOWSTACK->pop(FOLLOWSTACK)
130 #define PRECOVER() RECOGNIZER->recover(RECOGNIZER)
131 #define PREPORTERROR() RECOGNIZER->reportError(RECOGNIZER)
132 #define LA(n) INPUT->istream->_LA(ISTREAM, n)
133 #define LT(n) INPUT->_LT(INPUT, n)
134 #define CONSTRUCTEX() RECOGNIZER->exConstruct(RECOGNIZER)
135 #define CONSUME() ISTREAM->consume(ISTREAM)
136 #define MARK() ISTREAM->mark(ISTREAM)
137 #define REWIND(m) ISTREAM->rewind(ISTREAM, m)
138 #define REWINDLAST() ISTREAM->rewindLast(ISTREAM)
139 #define SEEK(n) ISTREAM->seek(ISTREAM, n)
140 #define PERRORRECOVERY PSRSTATE->errorRecovery
141 #define FAILEDFLAG PSRSTATE->failed
142 #define HASFAILED() (FAILEDFLAG == ANTLR3_TRUE)
143 #define BACKTRACKING PSRSTATE->backtracking
144 #define RECOVERFROMMISMATCHEDSET(s) RECOGNIZER->recoverFromMismatchedSet(RECOGNIZER, s)
145 #define RECOVERFROMMISMATCHEDELEMENT(e) RECOGNIZER->recoverFromMismatchedElement(RECOGNIZER, s)
146 #define ADAPTOR ctx->adaptor
147 #define RULEMEMO PSRSTATE->ruleMemo
148 #define DBG RECOGNIZER->debugger
150 #define TOKTEXT(tok, txt) tok, (pANTLR3_UINT8)txt
152 /* The 4 tokens defined below may well clash with your own #defines or token types. If so
153 * then for the present you must use different names for your defines as these are hard coded
154 * in the code generator. It would be better not to use such names internally, and maybe
155 * we can change this in a forthcoming release. I deliberately do not #undef these
156 * here as this will at least give you a redefined error somewhere if they clash.
158 #define UP ANTLR3_TOKEN_UP
159 #define DOWN ANTLR3_TOKEN_DOWN
160 #define EOR ANTLR3_TOKEN_EOR
161 #define INVALID ANTLR3_TOKEN_INVALID
164 /* =============================================================================
165 * Functions to create and destroy scopes. First come the rule scopes, followed
166 * by the global declared scopes.
171 /* ============================================================================= */
173 /* =============================================================================
174 * Start of recognizer
179 /** \brief Table of all token names in symbolic order, mainly used for
182 pANTLR3_UINT8 LangParserTokenNames[7+4]
184 (pANTLR3_UINT8) "<invalid>", /* String to print to indicate an invalid token */
185 (pANTLR3_UINT8) "<EOR>",
186 (pANTLR3_UINT8) "<DOWN>",
187 (pANTLR3_UINT8) "<UP>",
188 (pANTLR3_UINT8) "DECL",
189 (pANTLR3_UINT8) "ID",
190 (pANTLR3_UINT8) "INTTYPE",
191 (pANTLR3_UINT8) "FLOATTYPE",
192 (pANTLR3_UINT8) "INT",
193 (pANTLR3_UINT8) "WS",
194 (pANTLR3_UINT8) "';'"
199 // Forward declare the locally static matching functions we have generated.
201 static LangParser_start_return start (pLangParser ctx);
202 static LangParser_decl_return decl (pLangParser ctx);
203 static LangParser_type_return type (pLangParser ctx);
204 static void LangParserFree(pLangParser ctx);
205 /* For use in tree output where we are accumulating rule labels via label += ruleRef
206 * we need a function that knows how to free a return scope when the list is destroyed.
207 * We cannot just use ANTLR3_FREE because in debug tracking mode, this is a macro.
209 static void ANTLR3_CDECL freeScope(void * scope)
214 /** \brief Name of the grammar file that generated this code
216 static const char fileName[] = "Lang.g";
218 /** \brief Return the name of the grammar file that generated this code.
220 static const char * getGrammarFileName()
224 /** \brief Create a new LangParser parser and return a context for it.
226 * \param[in] instream Pointer to an input stream interface.
228 * \return Pointer to new parser context upon success.
230 ANTLR3_API pLangParser
231 LangParserNew (pANTLR3_COMMON_TOKEN_STREAM instream)
233 // See if we can create a new parser with the standard constructor
235 return LangParserNewSSD(instream, NULL);
238 /** \brief Create a new LangParser parser and return a context for it.
240 * \param[in] instream Pointer to an input stream interface.
242 * \return Pointer to new parser context upon success.
244 ANTLR3_API pLangParser
245 LangParserNewSSD (pANTLR3_COMMON_TOKEN_STREAM instream, pANTLR3_RECOGNIZER_SHARED_STATE state)
247 pLangParser ctx; /* Context structure we will build and return */
249 ctx = (pLangParser) ANTLR3_CALLOC(1, sizeof(LangParser));
253 // Failed to allocate memory for parser context
258 /* -------------------------------------------------------------------
259 * Memory for basic structure is allocated, now to fill in
260 * the base ANTLR3 structures. We initialize the function pointers
261 * for the standard ANTLR3 parser function set, but upon return
262 * from here, the programmer may set the pointers to provide custom
263 * implementations of each function.
265 * We don't use the macros defined in LangParser.h here, in order that you can get a sense
266 * of what goes where.
269 /* Create a base parser/recognizer, using the supplied token stream
271 ctx->pParser = antlr3ParserNewStream(ANTLR3_SIZE_HINT, instream->tstream, state);
272 /* Install the implementation of our LangParser interface
277 ctx->free = LangParserFree;
278 ctx->getGrammarFileName = getGrammarFileName;
280 /* Install the scope pushing methods.
282 ADAPTOR = ANTLR3_TREE_ADAPTORNew(instream->tstream->tokenSource->strFactory);
283 ctx->vectors = antlr3VectorFactoryNew(0);
287 /* Install the token table
289 PSRSTATE->tokenNames = LangParserTokenNames;
292 /* Return the newly built parser to the caller
297 /** Free the parser resources
300 LangParserFree(pLangParser ctx)
302 /* Free any scope memory
305 ctx->vectors->close(ctx->vectors);
306 /* We created the adaptor so we must free it
308 ADAPTOR->free(ADAPTOR);
311 ctx->pParser->free(ctx->pParser);
314 /* Everything is released, so we can return
319 /** Return token names used by this parser
321 * The returned pointer is used as an index into the token names table (using the token
322 * number as the index).
324 * \return Pointer to first char * in the table.
326 static pANTLR3_UINT8 *getTokenNames()
328 return LangParserTokenNames;
332 /* Declare the bitsets
335 /** Bitset defining follow set for error recovery in rule state: FOLLOW_decl_in_start91 */
336 static ANTLR3_BITWORD FOLLOW_decl_in_start91_bits[] = { ANTLR3_UINT64_LIT(0x00000000000000C2) };
337 static ANTLR3_BITSET_LIST FOLLOW_decl_in_start91 = { FOLLOW_decl_in_start91_bits, 1 };
338 /** Bitset defining follow set for error recovery in rule state: FOLLOW_type_in_decl103 */
339 static ANTLR3_BITWORD FOLLOW_type_in_decl103_bits[] = { ANTLR3_UINT64_LIT(0x0000000000000020) };
340 static ANTLR3_BITSET_LIST FOLLOW_type_in_decl103 = { FOLLOW_type_in_decl103_bits, 1 };
341 /** Bitset defining follow set for error recovery in rule state: FOLLOW_ID_in_decl105 */
342 static ANTLR3_BITWORD FOLLOW_ID_in_decl105_bits[] = { ANTLR3_UINT64_LIT(0x0000000000000400) };
343 static ANTLR3_BITSET_LIST FOLLOW_ID_in_decl105 = { FOLLOW_ID_in_decl105_bits, 1 };
344 /** Bitset defining follow set for error recovery in rule state: FOLLOW_10_in_decl107 */
345 static ANTLR3_BITWORD FOLLOW_10_in_decl107_bits[] = { ANTLR3_UINT64_LIT(0x0000000000000002) };
346 static ANTLR3_BITSET_LIST FOLLOW_10_in_decl107 = { FOLLOW_10_in_decl107_bits, 1 };
347 /** Bitset defining follow set for error recovery in rule state: FOLLOW_set_in_type0 */
348 static ANTLR3_BITWORD FOLLOW_set_in_type0_bits[] = { ANTLR3_UINT64_LIT(0x0000000000000002) };
349 static ANTLR3_BITSET_LIST FOLLOW_set_in_type0 = { FOLLOW_set_in_type0_bits, 1 };
354 /* ==============================================
359 * Lang.g:20:1: start : ( decl )+ ;
361 static LangParser_start_return
362 start(pLangParser ctx)
364 LangParser_start_return retval;
366 pANTLR3_BASE_TREE root_0;
368 LangParser_decl_return decl1;
369 #undef RETURN_TYPE_decl1
370 #define RETURN_TYPE_decl1 LangParser_decl_return
373 /* Initialize rule variables
381 retval.start = LT(1); retval.stop = retval.start;
387 // Lang.g:20:7: ( ( decl )+ )
388 // Lang.g:20:9: ( decl )+
390 root_0 = (pANTLR3_BASE_TREE)(ADAPTOR->nilNode(ADAPTOR));
392 // Lang.g:20:9: ( decl )+
415 FOLLOWPUSH(FOLLOW_decl_in_start91);
424 ADAPTOR->addChild(ADAPTOR, root_0, decl1.tree);
438 EXCEPTION->type = ANTLR3_EARLY_EXIT_EXCEPTION;
439 EXCEPTION->name = (void *)ANTLR3_EARLY_EXIT_NAME;
446 loop1: ; /* Jump to here if this rule does not match */
454 // This is where rules clean up and exit
456 goto rulestartEx; /* Prevent compiler warnings */
458 retval.stop = LT(-1);
460 retval.stop = LT(-1);
461 retval.tree = (pANTLR3_BASE_TREE)(ADAPTOR->rulePostProcessing(ADAPTOR, root_0));
462 ADAPTOR->setTokenBoundaries(ADAPTOR, retval.tree, retval.start, retval.stop);
468 retval.tree = (pANTLR3_BASE_TREE)(ADAPTOR->errorNode(ADAPTOR, INPUT, retval.start, LT(-1), EXCEPTION));
474 /* $ANTLR end start */
478 * Lang.g:23:1: decl : type ID ';' -> ^( DECL type ID ) ;
480 static LangParser_decl_return
481 decl(pLangParser ctx)
483 LangParser_decl_return retval;
485 pANTLR3_BASE_TREE root_0;
487 pANTLR3_COMMON_TOKEN ID3;
488 pANTLR3_COMMON_TOKEN char_literal4;
489 LangParser_type_return type2;
490 #undef RETURN_TYPE_type2
491 #define RETURN_TYPE_type2 LangParser_type_return
493 pANTLR3_BASE_TREE ID3_tree;
494 pANTLR3_BASE_TREE char_literal4_tree;
495 pANTLR3_REWRITE_RULE_TOKEN_STREAM stream_10;
496 pANTLR3_REWRITE_RULE_TOKEN_STREAM stream_ID;
497 pANTLR3_REWRITE_RULE_SUBTREE_STREAM stream_type;
498 /* Initialize rule variables
505 char_literal4 = NULL;
508 retval.start = LT(1); retval.stop = retval.start;
511 char_literal4_tree = NULL;
514 #define CREATE_stream_10 if (stream_10 == NULL) {stream_10 = antlr3RewriteRuleTOKENStreamNewAE(ADAPTOR, RECOGNIZER, (pANTLR3_UINT8)"token 10"); }
516 #define CREATE_stream_ID if (stream_ID == NULL) {stream_ID = antlr3RewriteRuleTOKENStreamNewAE(ADAPTOR, RECOGNIZER, (pANTLR3_UINT8)"token ID"); }
518 #define CREATE_stream_type if (stream_type == NULL) {stream_type = antlr3RewriteRuleSubtreeStreamNewAE(ADAPTOR, RECOGNIZER, (pANTLR3_UINT8)"rule type"); }
522 // Lang.g:23:6: ( type ID ';' -> ^( DECL type ID ) )
523 // Lang.g:23:8: type ID ';'
525 FOLLOWPUSH(FOLLOW_type_in_decl103);
534 CREATE_stream_type; stream_type->add(stream_type, type2.tree, NULL);
535 ID3 = (pANTLR3_COMMON_TOKEN) MATCHT(ID, &FOLLOW_ID_in_decl105);
541 CREATE_stream_ID; stream_ID->add(stream_ID, ID3, NULL);
543 char_literal4 = (pANTLR3_COMMON_TOKEN) MATCHT(10, &FOLLOW_10_in_decl107);
549 CREATE_stream_10; stream_10->add(stream_10, char_literal4, NULL);
554 * elements : ID, type
556 * rule labels : retval
557 * token list labels :
561 pANTLR3_REWRITE_RULE_SUBTREE_STREAM stream_retval;
563 stream_retval=antlr3RewriteRuleSubtreeStreamNewAEE(ADAPTOR, RECOGNIZER, (pANTLR3_UINT8)"token retval", retval.tree != NULL ? retval.tree : NULL);
565 root_0 = (pANTLR3_BASE_TREE)(ADAPTOR->nilNode(ADAPTOR));
566 retval.tree = root_0;
567 // 23:20: -> ^( DECL type ID )
569 // Lang.g:23:23: ^( DECL type ID )
571 pANTLR3_BASE_TREE root_1 = (pANTLR3_BASE_TREE)(ADAPTOR->nilNode(ADAPTOR));
572 root_1 = (pANTLR3_BASE_TREE)(ADAPTOR->becomeRoot(ADAPTOR, (pANTLR3_BASE_TREE)ADAPTOR->createTypeText(ADAPTOR, DECL, (pANTLR3_UINT8)"DECL"), root_1));
574 ADAPTOR->addChild(ADAPTOR, root_1, stream_type == NULL ? NULL : stream_type->nextTree(stream_type));
575 ADAPTOR->addChild(ADAPTOR, root_1, stream_ID == NULL ? NULL : stream_ID->nextNode(stream_ID));
577 ADAPTOR->addChild(ADAPTOR, root_0, root_1);
582 retval.tree = root_0; // set result root
583 if (stream_retval != NULL) stream_retval->free(stream_retval);
592 // This is where rules clean up and exit
594 goto ruledeclEx; /* Prevent compiler warnings */
596 retval.stop = LT(-1);
598 retval.stop = LT(-1);
599 retval.tree = (pANTLR3_BASE_TREE)(ADAPTOR->rulePostProcessing(ADAPTOR, root_0));
600 ADAPTOR->setTokenBoundaries(ADAPTOR, retval.tree, retval.start, retval.stop);
601 if (stream_10 != NULL) stream_10->free(stream_10);
602 if (stream_ID != NULL) stream_ID->free(stream_ID);
603 if (stream_type != NULL) stream_type->free(stream_type);
609 retval.tree = (pANTLR3_BASE_TREE)(ADAPTOR->errorNode(ADAPTOR, INPUT, retval.start, LT(-1), EXCEPTION));
615 /* $ANTLR end decl */
619 * Lang.g:26:1: type : ( INTTYPE | FLOATTYPE );
621 static LangParser_type_return
622 type(pLangParser ctx)
624 LangParser_type_return retval;
626 pANTLR3_BASE_TREE root_0;
628 pANTLR3_COMMON_TOKEN set5;
630 pANTLR3_BASE_TREE set5_tree;
632 /* Initialize rule variables
639 retval.start = LT(1); retval.stop = retval.start;
646 // Lang.g:26:6: ( INTTYPE | FLOATTYPE )
649 root_0 = (pANTLR3_BASE_TREE)(ADAPTOR->nilNode(ADAPTOR));
651 set5=(pANTLR3_COMMON_TOKEN)LT(1);
652 if ( ((LA(1) >= INTTYPE) && (LA(1) <= FLOATTYPE)) )
655 ADAPTOR->addChild(ADAPTOR, root_0, (pANTLR3_BASE_TREE)(ADAPTOR->create(ADAPTOR, set5)));
656 PERRORRECOVERY=ANTLR3_FALSE;
661 EXCEPTION->type = ANTLR3_MISMATCHED_SET_EXCEPTION;
662 EXCEPTION->name = (void *)ANTLR3_MISMATCHED_SET_NAME;
663 EXCEPTION->expectingSet = &FOLLOW_set_in_type0;
664 RECOVERFROMMISMATCHEDSET(&FOLLOW_set_in_type0); goto ruletypeEx;
673 // This is where rules clean up and exit
675 goto ruletypeEx; /* Prevent compiler warnings */
677 retval.stop = LT(-1);
679 retval.stop = LT(-1);
680 retval.tree = (pANTLR3_BASE_TREE)(ADAPTOR->rulePostProcessing(ADAPTOR, root_0));
681 ADAPTOR->setTokenBoundaries(ADAPTOR, retval.tree, retval.start, retval.stop);
687 retval.tree = (pANTLR3_BASE_TREE)(ADAPTOR->errorNode(ADAPTOR, INPUT, retval.start, LT(-1), EXCEPTION));
693 /* $ANTLR end type */
694 /* End of parsing rules
695 * ==============================================
698 /* ==============================================
699 * Syntactic predicates
701 /* End of syntactic predicates
702 * ==============================================
711 * =============================================================================