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:55
6 * - for the lexer : LangLexerLexer *
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 "LangLexer.h"
46 /* ----------------------------------------- */
49 /** String literals used by LangLexer that we must do things like MATCHS() with.
50 * C will normally just lay down 8 bit characters, and you can use L"xxx" to
51 * get wchar_t, but wchar_t is 16 bits on Windows, which is not UTF32 and so
52 * we perform this little trick of defining the literals as arrays of UINT32
53 * and passing in the address of these.
55 static ANTLR3_UCHAR lit_1[] = { 0x69, 0x6E, 0x74, ANTLR3_STRING_TERMINATOR};
56 static ANTLR3_UCHAR lit_2[] = { 0x66, 0x6C, 0x6F, 0x61, 0x74, ANTLR3_STRING_TERMINATOR};
61 /* MACROS that hide the C interface implementations from the
62 * generated code, which makes it a little more understandable to the human eye.
63 * I am very much against using C pre-processor macros for function calls and bits
64 * of code as you cannot see what is happening when single stepping in debuggers
65 * and so on. The exception (in my book at least) is for generated code, where you are
66 * not maintaining it, but may wish to read and understand it. If you single step it, you know that input()
67 * hides some indirect calls, but is always referring to the input stream. This is
68 * probably more readable than ctx->input->istream->input(snarfle0->blarg) and allows me to rejig
69 * the runtime interfaces without changing the generated code too often, without
70 * confusing the reader of the generated output, who may not wish to know the gory
71 * details of the interface inheritance.
76 /* Aids in accessing scopes for grammar programmers
81 #define SCOPE_TYPE(scope) pLangLexer_##scope##_SCOPE
82 #define SCOPE_STACK(scope) pLangLexer_##scope##Stack
83 #define SCOPE_TOP(scope) ctx->pLangLexer_##scope##Top
84 #define SCOPE_SIZE(scope) ctx->pLangLexer_##scope##Stack_limit
85 #define SCOPE_INSTANCE(scope, i) (ctx->SCOPE_STACK(scope)->get(ctx->SCOPE_STACK(scope),i))
88 /* Macros for accessing things in a lexer
95 #undef GETCHARPOSITIONINLINE
118 #undef HAVEPARSEDRULE
127 #define LEXER ctx->pLexer
128 #define RECOGNIZER LEXER->rec
129 #define LEXSTATE RECOGNIZER->state
130 #define TOKSOURCE LEXSTATE->tokSource
131 #define GETCHARINDEX() LEXER->getCharIndex(LEXER)
132 #define GETLINE() LEXER->getLine(LEXER)
133 #define GETTEXT() LEXER->getText(LEXER)
134 #define GETCHARPOSITIONINLINE() LEXER->getCharPositionInLine(LEXER)
135 #define EMIT() LEXSTATE->type = _type; LEXER->emit(LEXER)
136 #define EMITNEW(t) LEXER->emitNew(LEXER, t)
137 #define MATCHC(c) LEXER->matchc(LEXER, c)
138 #define MATCHS(s) LEXER->matchs(LEXER, s)
139 #define MATCHRANGE(c1,c2) LEXER->matchRange(LEXER, c1, c2)
140 #define MATCHANY() LEXER->matchAny(LEXER)
141 #define LTOKEN LEXSTATE->token
142 #define HASFAILED() (LEXSTATE->failed == ANTLR3_TRUE)
143 #define BACKTRACKING LEXSTATE->backtracking
144 #define FAILEDFLAG LEXSTATE->failed
145 #define INPUT LEXER->input
146 #define STRSTREAM INPUT
147 #define ISTREAM INPUT->istream
148 #define INDEX() ISTREAM->index(ISTREAM)
149 #define SEEK(n) ISTREAM->seek(ISTREAM, n)
150 #define EOF_TOKEN &(LEXSTATE->tokSource->eofToken)
151 #define HASEXCEPTION() (LEXSTATE->error == ANTLR3_TRUE)
152 #define EXCEPTION LEXSTATE->exception
153 #define CONSTRUCTEX() RECOGNIZER->exConstruct(RECOGNIZER)
154 #define LRECOVER() LEXER->recover(LEXER)
155 #define MARK() ISTREAM->mark(ISTREAM)
156 #define REWIND(m) ISTREAM->rewind(ISTREAM, m)
157 #define REWINDLAST() ISTREAM->rewindLast(ISTREAM)
158 #define MEMOIZE(ri,si) RECOGNIZER->memoize(RECOGNIZER, ri, si)
159 #define HAVEPARSEDRULE(r) RECOGNIZER->alreadyParsedRule(RECOGNIZER, r)
160 #define PUSHSTREAM(str) LEXER->pushCharStream(LEXER, str)
161 #define POPSTREAM() LEXER->popCharStream(LEXER)
162 #define SETTEXT(str) LEXSTATE->text = str
163 #define SKIP() LEXSTATE->token = &(TOKSOURCE->skipToken)
164 #define USER1 LEXSTATE->user1
165 #define USER2 LEXSTATE->user2
166 #define USER3 LEXSTATE->user3
167 #define CUSTOM LEXSTATE->custom
168 #define RULEMEMO LEXSTATE->ruleMemo
169 #define DBG RECOGNIZER->debugger
171 /* If we have been told we can rely on the standard 8 bit or 16 bit input
172 * stream, then we can define our macros to use the direct pointers
173 * in the input object, which is much faster than indirect calls. This
174 * is really only significant to lexers with a lot of fragment rules (which
175 * do not place LA(1) in a temporary at the moment) and even then
176 * only if there is a lot of input (order of say 1M or so).
178 #if defined(ANTLR3_INLINE_INPUT_ASCII) || defined(ANTLR3_INLINE_INPUT_UTF16)
180 # ifdef ANTLR3_INLINE_INPUT_ASCII
182 /* 8 bit "ASCII" (actually any 8 bit character set) */
184 # define NEXTCHAR ((pANTLR3_UINT8)(INPUT->nextChar))
185 # define DATAP ((pANTLR3_UINT8)(INPUT->data))
189 # define NEXTCHAR ((pANTLR3_UINT16)(INPUT->nextChar))
190 # define DATAP ((pANTLR3_UINT16)(INPUT->data))
194 # define LA(n) ((NEXTCHAR + n) > (DATAP + INPUT->sizeBuf) ? ANTLR3_CHARSTREAM_EOF : (ANTLR3_UCHAR)(*(NEXTCHAR + n - 1)))
197 if (NEXTCHAR < (DATAP + INPUT->sizeBuf)) \
199 INPUT->charPositionInLine++; \
200 if ((ANTLR3_UCHAR)(*NEXTCHAR) == INPUT->newlineChar) \
203 INPUT->charPositionInLine = 0; \
204 INPUT->currentLine = (void *)(NEXTCHAR + 1); \
206 INPUT->nextChar = (void *)(NEXTCHAR + 1); \
212 // Pick up the input character by calling the input stream implementation.
214 #define CONSUME() INPUT->istream->consume(INPUT->istream)
215 #define LA(n) INPUT->istream->_LA(INPUT->istream, n)
218 #define TOKTEXT(tok, txt) tok, (pANTLR3_UINT8)txt
220 /* The 4 tokens defined below may well clash with your own #defines or token types. If so
221 * then for the present you must use different names for your defines as these are hard coded
222 * in the code generator. It would be better not to use such names internally, and maybe
223 * we can change this in a forthcoming release. I deliberately do not #undef these
224 * here as this will at least give you a redefined error somewhere if they clash.
226 #define UP ANTLR3_TOKEN_UP
227 #define DOWN ANTLR3_TOKEN_DOWN
228 #define EOR ANTLR3_TOKEN_EOR
229 #define INVALID ANTLR3_TOKEN_INVALID
232 /* =============================================================================
233 * Functions to create and destroy scopes. First come the rule scopes, followed
234 * by the global declared scopes.
239 /* ============================================================================= */
241 /* =============================================================================
242 * Start of recognizer
246 /* Forward declare the locally static matching functions we have generated and any predicate functions.
248 static ANTLR3_INLINE void mT__10 (pLangLexer ctx);
249 static ANTLR3_INLINE void mINTTYPE (pLangLexer ctx);
250 static ANTLR3_INLINE void mFLOATTYPE (pLangLexer ctx);
251 static ANTLR3_INLINE void mID (pLangLexer ctx);
252 static ANTLR3_INLINE void mINT (pLangLexer ctx);
253 static ANTLR3_INLINE void mWS (pLangLexer ctx);
254 static ANTLR3_INLINE void mTokens (pLangLexer ctx);
255 static void LangLexerFree(pLangLexer ctx);
257 /* =========================================================================
258 * Lexer matching rules end.
259 * =========================================================================
265 LangLexerFree (pLangLexer ctx)
272 /** \brief Name of the grammar file that generated this code
274 static const char fileName[] = "Lang.g";
276 /** \brief Return the name of the grammar file that generated this code.
278 static const char * getGrammarFileName()
283 /** \brief Create a new lexer called LangLexer
285 * \param[in] instream Pointer to an initialized input stream
287 * - Success pLangLexer initialized for the lex start
290 ANTLR3_API pLangLexer LangLexerNew
291 (pANTLR3_INPUT_STREAM instream)
293 // See if we can create a new lexer with the standard constructor
295 return LangLexerNewSSD(instream, NULL);
298 /** \brief Create a new lexer called LangLexer
300 * \param[in] instream Pointer to an initialized input stream
301 * \param[state] state Previously created shared recognizer stat
303 * - Success pLangLexer initialized for the lex start
306 ANTLR3_API pLangLexer LangLexerNewSSD
307 (pANTLR3_INPUT_STREAM instream, pANTLR3_RECOGNIZER_SHARED_STATE state)
309 pLangLexer ctx; // Context structure we will build and return
311 ctx = (pLangLexer) ANTLR3_CALLOC(1, sizeof(LangLexer));
315 // Failed to allocate memory for lexer context
319 /* -------------------------------------------------------------------
320 * Memory for basic structure is allocated, now to fill in
321 * in base ANTLR3 structures. We initialize the function pointers
322 * for the standard ANTLR3 lexer function set, but upon return
323 * from here, the programmer may set the pointers to provide custom
324 * implementations of each function.
326 * We don't use the macros defined in LangLexer.h here so you can get a sense
327 * of what goes where.
330 /* Create a base lexer, using the supplied input stream
332 ctx->pLexer = antlr3LexerNewStream(ANTLR3_SIZE_HINT, instream, state);
334 /* Check that we allocated the memory correctly
336 if (ctx->pLexer == NULL)
341 /* Install the implementation of our LangLexer interface
343 ctx->mT__10 = mT__10;
344 ctx->mINTTYPE = mINTTYPE;
345 ctx->mFLOATTYPE = mFLOATTYPE;
349 ctx->mTokens = mTokens;
351 /** When the nextToken() call is made to this lexer's pANTLR3_TOKEN_SOURCE
352 * it will call mTokens() in this generated code, and will pass it the ctx
353 * pointer of this lexer, not the context of the base lexer, so store that now.
355 ctx->pLexer->ctx = ctx;
357 /**Install the token matching function
359 ctx->pLexer->mTokens = (void (*) (void *))(mTokens);
361 ctx->getGrammarFileName = getGrammarFileName;
362 ctx->free = LangLexerFree;
368 /* Return the newly built lexer to the caller
374 /* =========================================================================
375 * Functions to match the lexer grammar defined tokens from the input stream
378 // Comes from: 7:7: ( ';' )
379 /** \brief Lexer rule generated by ANTLR3
383 * Looks to match the characters the constitute the token T__10
384 * from the attached input stream.
388 * - lexer->error == ANTLR3_TRUE if an exception was thrown.
391 void mT__10(pLangLexer ctx)
398 // Lang.g:7:7: ( ';' )
410 LEXSTATE->type = _type;
412 // This is where rules clean up and exit
414 goto ruleT__10Ex; /* Prevent compiler warnings */
420 // Comes from: 30:13: ( 'int' )
421 /** \brief Lexer rule generated by ANTLR3
423 * $ANTLR start INTTYPE
425 * Looks to match the characters the constitute the token INTTYPE
426 * from the attached input stream.
430 * - lexer->error == ANTLR3_TRUE if an exception was thrown.
433 void mINTTYPE(pLangLexer ctx)
440 // Lang.g:30:13: ( 'int' )
441 // Lang.g:30:15: 'int'
453 LEXSTATE->type = _type;
455 // This is where rules clean up and exit
457 goto ruleINTTYPEEx; /* Prevent compiler warnings */
461 // $ANTLR end INTTYPE
463 // Comes from: 31:13: ( 'float' )
464 /** \brief Lexer rule generated by ANTLR3
466 * $ANTLR start FLOATTYPE
468 * Looks to match the characters the constitute the token FLOATTYPE
469 * from the attached input stream.
473 * - lexer->error == ANTLR3_TRUE if an exception was thrown.
476 void mFLOATTYPE(pLangLexer ctx)
483 // Lang.g:31:13: ( 'float' )
484 // Lang.g:31:15: 'float'
489 goto ruleFLOATTYPEEx;
496 LEXSTATE->type = _type;
498 // This is where rules clean up and exit
500 goto ruleFLOATTYPEEx; /* Prevent compiler warnings */
504 // $ANTLR end FLOATTYPE
506 // Comes from: 32:8: ( ( 'a' .. 'z' )+ )
507 /** \brief Lexer rule generated by ANTLR3
511 * Looks to match the characters the constitute the token ID
512 * from the attached input stream.
516 * - lexer->error == ANTLR3_TRUE if an exception was thrown.
519 void mID(pLangLexer ctx)
526 // Lang.g:32:8: ( ( 'a' .. 'z' )+ )
527 // Lang.g:32:10: ( 'a' .. 'z' )+
529 // Lang.g:32:10: ( 'a' .. 'z' )+
574 // Lang.g:32:10: 'a' .. 'z'
576 MATCHRANGE('a', 'z');
595 EXCEPTION->type = ANTLR3_EARLY_EXIT_EXCEPTION;
596 EXCEPTION->name = (void *)ANTLR3_EARLY_EXIT_NAME;
603 loop1: ; /* Jump to here if this rule does not match */
608 LEXSTATE->type = _type;
610 // This is where rules clean up and exit
612 goto ruleIDEx; /* Prevent compiler warnings */
618 // Comes from: 33:9: ( ( '0' .. '9' )+ )
619 /** \brief Lexer rule generated by ANTLR3
623 * Looks to match the characters the constitute the token INT
624 * from the attached input stream.
628 * - lexer->error == ANTLR3_TRUE if an exception was thrown.
631 void mINT(pLangLexer ctx)
638 // Lang.g:33:9: ( ( '0' .. '9' )+ )
639 // Lang.g:33:11: ( '0' .. '9' )+
641 // Lang.g:33:11: ( '0' .. '9' )+
670 // Lang.g:33:11: '0' .. '9'
672 MATCHRANGE('0', '9');
691 EXCEPTION->type = ANTLR3_EARLY_EXIT_EXCEPTION;
692 EXCEPTION->name = (void *)ANTLR3_EARLY_EXIT_NAME;
699 loop2: ; /* Jump to here if this rule does not match */
704 LEXSTATE->type = _type;
706 // This is where rules clean up and exit
708 goto ruleINTEx; /* Prevent compiler warnings */
714 // Comes from: 34:8: ( ( ' ' | '\\n' | '\\r' ) )
715 /** \brief Lexer rule generated by ANTLR3
719 * Looks to match the characters the constitute the token WS
720 * from the attached input stream.
724 * - lexer->error == ANTLR3_TRUE if an exception was thrown.
727 void mWS(pLangLexer ctx)
734 // Lang.g:34:8: ( ( ' ' | '\\n' | '\\r' ) )
735 // Lang.g:34:10: ( ' ' | '\\n' | '\\r' )
737 if ( LA(1) == '\n' || LA(1) == '\r' || LA(1) == ' ' )
745 EXCEPTION->type = ANTLR3_MISMATCHED_SET_EXCEPTION;
746 EXCEPTION->name = (void *)ANTLR3_MISMATCHED_SET_NAME;
748 LRECOVER(); goto ruleWSEx;
752 LEXSTATE->channel=HIDDEN;
757 LEXSTATE->type = _type;
759 // This is where rules clean up and exit
761 goto ruleWSEx; /* Prevent compiler warnings */
767 /** This is the entry point in to the lexer from an object that
768 * wants to generate the next token, such as a pCOMMON_TOKEN_STREAM
771 mTokens(pLangLexer ctx)
774 // Lang.g:1:8: ( T__10 | INTTYPE | FLOATTYPE | ID | INT | WS )
979 EXCEPTION->type = ANTLR3_NO_VIABLE_ALT_EXCEPTION;
980 EXCEPTION->message = (void *)"";
981 EXCEPTION->decisionNum = 3;
982 EXCEPTION->state = 0;
991 // Lang.g:1:10: T__10
1004 // Lang.g:1:16: INTTYPE
1017 // Lang.g:1:24: FLOATTYPE
1019 /* 1:24: FLOATTYPE */
1073 goto ruleTokensEx; /* Prevent compiler warnings */
1077 /* =========================================================================
1078 * Lexer matching rules end.
1079 * =========================================================================
1081 /* End of Lexer code
1082 * ================================================
1083 * ================================================
1088 * =============================================================================