2 /// Default implementation of CommonTokenStream
6 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
7 // http://www.temporal-wave.com
8 // http://www.linkedin.com/in/jimidle
10 // All rights reserved.
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 // 3. The name of the author may not be used to endorse or promote products
21 // derived from this software without specific prior written permission.
23 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <antlr3tokenstream.h>
37 #pragma warning( disable : 4100 )
40 // COMMON_TOKEN_STREAM API
42 static void setTokenTypeChannel (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_UINT32 ttype, ANTLR3_UINT32 channel);
43 static void discardTokenType (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_INT32 ttype);
44 static void discardOffChannel (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_BOOLEAN discard);
45 static pANTLR3_VECTOR getTokens (pANTLR3_COMMON_TOKEN_STREAM cts);
46 static pANTLR3_LIST getTokenRange (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop);
47 static pANTLR3_LIST getTokensSet (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, pANTLR3_BITSET types);
48 static pANTLR3_LIST getTokensList (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, pANTLR3_LIST list);
49 static pANTLR3_LIST getTokensType (pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, ANTLR3_UINT32 type);
53 static pANTLR3_COMMON_TOKEN tokLT (pANTLR3_TOKEN_STREAM ts, ANTLR3_INT32 k);
54 static pANTLR3_COMMON_TOKEN dbgTokLT (pANTLR3_TOKEN_STREAM ts, ANTLR3_INT32 k);
55 static pANTLR3_COMMON_TOKEN get (pANTLR3_TOKEN_STREAM ts, ANTLR3_UINT32 i);
56 static pANTLR3_TOKEN_SOURCE getTokenSource (pANTLR3_TOKEN_STREAM ts);
57 static void setTokenSource (pANTLR3_TOKEN_STREAM ts, pANTLR3_TOKEN_SOURCE tokenSource);
58 static pANTLR3_STRING toString (pANTLR3_TOKEN_STREAM ts);
59 static pANTLR3_STRING toStringSS (pANTLR3_TOKEN_STREAM ts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop);
60 static pANTLR3_STRING toStringTT (pANTLR3_TOKEN_STREAM ts, pANTLR3_COMMON_TOKEN start, pANTLR3_COMMON_TOKEN stop);
61 static void setDebugListener (pANTLR3_TOKEN_STREAM ts, pANTLR3_DEBUG_EVENT_LISTENER debugger);
65 static void consume (pANTLR3_INT_STREAM is);
66 static void dbgConsume (pANTLR3_INT_STREAM is);
67 static ANTLR3_UINT32 _LA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i);
68 static ANTLR3_UINT32 dbgLA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i);
69 static ANTLR3_MARKER mark (pANTLR3_INT_STREAM is);
70 static ANTLR3_MARKER dbgMark (pANTLR3_INT_STREAM is);
71 static void release (pANTLR3_INT_STREAM is, ANTLR3_MARKER mark);
72 static ANTLR3_UINT32 size (pANTLR3_INT_STREAM is);
73 static ANTLR3_MARKER tindex (pANTLR3_INT_STREAM is);
74 static void rewindStream (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker);
75 static void dbgRewindStream (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker);
76 static void rewindLast (pANTLR3_INT_STREAM is);
77 static void dbgRewindLast (pANTLR3_INT_STREAM is);
78 static void seek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index);
79 static void dbgSeek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index);
80 static pANTLR3_STRING getSourceName (pANTLR3_INT_STREAM is);
81 static void antlr3TokenStreamFree (pANTLR3_TOKEN_STREAM stream);
82 static void antlr3CTSFree (pANTLR3_COMMON_TOKEN_STREAM stream);
86 static void fillBuffer (pANTLR3_COMMON_TOKEN_STREAM tokenStream);
87 static ANTLR3_UINT32 skipOffTokenChannels (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 i);
88 static ANTLR3_UINT32 skipOffTokenChannelsReverse (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 i);
89 static pANTLR3_COMMON_TOKEN LB (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 i);
91 ANTLR3_API pANTLR3_TOKEN_STREAM
92 antlr3TokenStreamNew()
94 pANTLR3_TOKEN_STREAM stream;
96 // Memory for the interface structure
98 stream = (pANTLR3_TOKEN_STREAM) ANTLR3_MALLOC(sizeof(ANTLR3_TOKEN_STREAM));
107 stream->free = antlr3TokenStreamFree;
114 antlr3TokenStreamFree(pANTLR3_TOKEN_STREAM stream)
120 antlr3CTSFree (pANTLR3_COMMON_TOKEN_STREAM stream)
122 // We only free up our subordinate interfaces if they belong
123 // to us, otherwise we let whoever owns them deal with them.
125 if (stream->tstream->super == stream)
127 if (stream->tstream->istream->super == stream->tstream)
129 stream->tstream->istream->free(stream->tstream->istream);
130 stream->tstream->istream = NULL;
132 stream->tstream->free(stream->tstream);
135 // Now we free our own resources
137 if (stream->tokens != NULL)
139 stream->tokens->free(stream->tokens);
140 stream->tokens = NULL;
142 if (stream->discardSet != NULL)
144 stream->discardSet->free(stream->discardSet);
145 stream->discardSet = NULL;
147 if (stream->channelOverrides != NULL)
149 stream->channelOverrides->free(stream->channelOverrides);
150 stream->channelOverrides = NULL;
153 // Free our memory now
158 ANTLR3_API pANTLR3_COMMON_TOKEN_STREAM
159 antlr3CommonTokenDebugStreamSourceNew(ANTLR3_UINT32 hint, pANTLR3_TOKEN_SOURCE source, pANTLR3_DEBUG_EVENT_LISTENER debugger)
161 pANTLR3_COMMON_TOKEN_STREAM stream;
163 // Create a standard token stream
165 stream = antlr3CommonTokenStreamSourceNew(hint, source);
167 // Install the debugger object
169 stream->tstream->debugger = debugger;
171 // Override standard token stream methods with debugging versions
173 stream->tstream->initialStreamState = ANTLR3_FALSE;
175 stream->tstream->_LT = dbgTokLT;
177 stream->tstream->istream->consume = dbgConsume;
178 stream->tstream->istream->_LA = dbgLA;
179 stream->tstream->istream->mark = dbgMark;
180 stream->tstream->istream->rewind = dbgRewindStream;
181 stream->tstream->istream->rewindLast = dbgRewindLast;
182 stream->tstream->istream->seek = dbgSeek;
187 ANTLR3_API pANTLR3_COMMON_TOKEN_STREAM
188 antlr3CommonTokenStreamSourceNew(ANTLR3_UINT32 hint, pANTLR3_TOKEN_SOURCE source)
190 pANTLR3_COMMON_TOKEN_STREAM stream;
192 stream = antlr3CommonTokenStreamNew(hint);
194 stream->channel = ANTLR3_TOKEN_DEFAULT_CHANNEL;
196 stream->channelOverrides = NULL;
197 stream->discardSet = NULL;
198 stream->discardOffChannel = ANTLR3_FALSE;
200 stream->tstream->setTokenSource(stream->tstream, source);
202 stream->free = antlr3CTSFree;
206 ANTLR3_API pANTLR3_COMMON_TOKEN_STREAM
207 antlr3CommonTokenStreamNew(ANTLR3_UINT32 hint)
209 pANTLR3_COMMON_TOKEN_STREAM stream;
211 /* Memory for the interface structure
213 stream = (pANTLR3_COMMON_TOKEN_STREAM) ANTLR3_MALLOC(sizeof(ANTLR3_COMMON_TOKEN_STREAM));
220 /* Create space for the token stream interface
222 stream->tstream = antlr3TokenStreamNew();
223 stream->tstream->super = stream;
225 /* Create space for the INT_STREAM interfacce
227 stream->tstream->istream = antlr3IntStreamNew();
228 stream->tstream->istream->super = (stream->tstream);
229 stream->tstream->istream->type = ANTLR3_TOKENSTREAM;
231 /* Install the token tracking tables
233 stream->tokens = antlr3VectorNew(0);
239 /* Install the common token stream API
241 stream->setTokenTypeChannel = setTokenTypeChannel;
242 stream->discardTokenType = discardTokenType;
243 stream->discardOffChannelToks = discardOffChannel;
244 stream->getTokens = getTokens;
245 stream->getTokenRange = getTokenRange;
246 stream->getTokensSet = getTokensSet;
247 stream->getTokensList = getTokensList;
248 stream->getTokensType = getTokensType;
250 /* Install the token stream API
252 stream->tstream->_LT = tokLT;
253 stream->tstream->get = get;
254 stream->tstream->getTokenSource = getTokenSource;
255 stream->tstream->setTokenSource = setTokenSource;
256 stream->tstream->toString = toString;
257 stream->tstream->toStringSS = toStringSS;
258 stream->tstream->toStringTT = toStringTT;
259 stream->tstream->setDebugListener = setDebugListener;
261 /* Install INT_STREAM interface
263 stream->tstream->istream->_LA = _LA;
264 stream->tstream->istream->mark = mark;
265 stream->tstream->istream->release = release;
266 stream->tstream->istream->size = size;
267 stream->tstream->istream->index = tindex;
268 stream->tstream->istream->rewind = rewindStream;
269 stream->tstream->istream->rewindLast= rewindLast;
270 stream->tstream->istream->seek = seek;
271 stream->tstream->istream->consume = consume;
272 stream->tstream->istream->getSourceName = getSourceName;
277 // Install a debug listener adn switch to debug mode methods
280 setDebugListener (pANTLR3_TOKEN_STREAM ts, pANTLR3_DEBUG_EVENT_LISTENER debugger)
282 // Install the debugger object
284 ts->debugger = debugger;
286 // Override standard token stream methods with debugging versions
288 ts->initialStreamState = ANTLR3_FALSE;
292 ts->istream->consume = dbgConsume;
293 ts->istream->_LA = dbgLA;
294 ts->istream->mark = dbgMark;
295 ts->istream->rewind = dbgRewindStream;
296 ts->istream->rewindLast = dbgRewindLast;
297 ts->istream->seek = dbgSeek;
300 /** Get the ith token from the current position 1..n where k=1 is the
301 * first symbol of lookahead.
303 static pANTLR3_COMMON_TOKEN
304 tokLT (pANTLR3_TOKEN_STREAM ts, ANTLR3_INT32 k)
308 pANTLR3_COMMON_TOKEN_STREAM cts;
310 cts = (pANTLR3_COMMON_TOKEN_STREAM)ts->super;
326 if ((cts->p + k - 1) >= (ANTLR3_INT32)ts->istream->cachedSize)
328 pANTLR3_COMMON_TOKEN teof = &(ts->tokenSource->eofToken);
330 teof->setStartIndex (teof, ts->istream->index(ts->istream));
331 teof->setStopIndex (teof, ts->istream->index(ts->istream));
338 /* Need to find k good tokens, skipping ones that are off channel
342 /* Skip off-channel tokens */
343 i = skipOffTokenChannels(cts, i+1); /* leave p on valid token */
346 if ( (ANTLR3_UINT32) i >= ts->istream->cachedSize)
348 pANTLR3_COMMON_TOKEN teof = &(ts->tokenSource->eofToken);
350 teof->setStartIndex (teof, ts->istream->index(ts->istream));
351 teof->setStopIndex (teof, ts->istream->index(ts->istream));
355 // Here the token must be in the input vector. Rather then incut
356 // function call penalty, we jsut return the pointer directly
359 return (pANTLR3_COMMON_TOKEN)cts->tokens->elements[i].element;
360 //return (pANTLR3_COMMON_TOKEN)cts->tokens->get(cts->tokens, i);
363 /// Debug only method to flag consumption of initial off-channel
364 /// tokens in the input stream
367 consumeInitialHiddenTokens(pANTLR3_INT_STREAM is)
371 pANTLR3_TOKEN_STREAM ts;
373 ts = (pANTLR3_TOKEN_STREAM) is->super;
374 first = is->index(is);
376 for (i=0; i<first; i++)
378 ts->debugger->consumeHiddenToken(ts->debugger, ts->get(ts, i));
381 ts->initialStreamState = ANTLR3_FALSE;
385 /// As per the normal tokLT but sends information to the debugger
387 static pANTLR3_COMMON_TOKEN
388 dbgTokLT (pANTLR3_TOKEN_STREAM ts, ANTLR3_INT32 k)
390 if (ts->initialStreamState == ANTLR3_TRUE)
392 consumeInitialHiddenTokens(ts->istream);
397 #ifdef ANTLR3_WINDOWS
398 /* When fully optimized VC7 complains about non reachable code.
399 * Not yet sure if this is an optimizer bug, or a bug in the flow analysis
401 #pragma warning( disable : 4702 )
404 static pANTLR3_COMMON_TOKEN
405 LB(pANTLR3_COMMON_TOKEN_STREAM cts, ANTLR3_INT32 k)
418 if ((cts->p - k) < 0)
426 /* Need to find k good tokens, going backwards, skipping ones that are off channel
428 while (n <= (ANTLR3_INT32) k)
430 /* Skip off-channel tokens
433 i = skipOffTokenChannelsReverse(cts, i - 1); /* leave p on valid token */
440 // Here the token must be in the input vector. Rather then incut
441 // function call penalty, we jsut return the pointer directly
444 return (pANTLR3_COMMON_TOKEN)cts->tokens->elements[i].element;
447 static pANTLR3_COMMON_TOKEN
448 get (pANTLR3_TOKEN_STREAM ts, ANTLR3_UINT32 i)
450 pANTLR3_COMMON_TOKEN_STREAM cts;
452 cts = (pANTLR3_COMMON_TOKEN_STREAM)ts->super;
454 return (pANTLR3_COMMON_TOKEN)(cts->tokens->get(cts->tokens, i)); /* Token index is zero based but vectors are 1 based */
457 static pANTLR3_TOKEN_SOURCE
458 getTokenSource (pANTLR3_TOKEN_STREAM ts)
460 return ts->tokenSource;
464 setTokenSource ( pANTLR3_TOKEN_STREAM ts,
465 pANTLR3_TOKEN_SOURCE tokenSource)
467 ts->tokenSource = tokenSource;
470 static pANTLR3_STRING
471 toString (pANTLR3_TOKEN_STREAM ts)
473 pANTLR3_COMMON_TOKEN_STREAM cts;
475 cts = (pANTLR3_COMMON_TOKEN_STREAM)ts->super;
482 return ts->toStringSS(ts, 0, ts->istream->size(ts->istream));
485 static pANTLR3_STRING
486 toStringSS(pANTLR3_TOKEN_STREAM ts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop)
488 pANTLR3_STRING string;
489 pANTLR3_TOKEN_SOURCE tsource;
490 pANTLR3_COMMON_TOKEN tok;
492 pANTLR3_COMMON_TOKEN_STREAM cts;
494 cts = (pANTLR3_COMMON_TOKEN_STREAM) ts->super;
500 if (stop >= ts->istream->size(ts->istream))
502 stop = ts->istream->size(ts->istream) - 1;
505 /* Who is giving us these tokens?
507 tsource = ts->getTokenSource(ts);
509 if (tsource != NULL && cts->tokens != NULL)
511 /* Finally, let's get a string
513 string = tsource->strFactory->newRaw(tsource->strFactory);
515 for (i = start; i <= stop; i++)
517 tok = ts->get(ts, i);
520 string->appendS(string, tok->getText(tok));
530 static pANTLR3_STRING
531 toStringTT (pANTLR3_TOKEN_STREAM ts, pANTLR3_COMMON_TOKEN start, pANTLR3_COMMON_TOKEN stop)
533 if (start != NULL && stop != NULL)
535 return ts->toStringSS(ts, (ANTLR3_UINT32)start->getTokenIndex(start), (ANTLR3_UINT32)stop->getTokenIndex(stop));
543 /** Move the input pointer to the next incoming token. The stream
544 * must become active with LT(1) available. consume() simply
545 * moves the input pointer so that LT(1) points at the next
546 * input symbol. Consume at least one token.
548 * Walk past any token not on the channel the parser is listening to.
551 consume (pANTLR3_INT_STREAM is)
553 pANTLR3_COMMON_TOKEN_STREAM cts;
554 pANTLR3_TOKEN_STREAM ts;
556 ts = (pANTLR3_TOKEN_STREAM) is->super;
557 cts = (pANTLR3_COMMON_TOKEN_STREAM) ts->super;
559 if ((ANTLR3_UINT32)cts->p < cts->tokens->size(cts->tokens))
562 cts->p = skipOffTokenChannels(cts, cts->p);
567 /// As per ordinary consume but notifies the debugger about hidden
568 /// tokens and so on.
571 dbgConsume (pANTLR3_INT_STREAM is)
573 pANTLR3_TOKEN_STREAM ts;
576 pANTLR3_COMMON_TOKEN t;
578 ts = (pANTLR3_TOKEN_STREAM) is->super;
580 if (ts->initialStreamState == ANTLR3_TRUE)
582 consumeInitialHiddenTokens(is);
585 a = is->index(is); // Where are we right now?
586 t = ts->_LT(ts, 1); // Current token from stream
588 consume(is); // Standard consumer
590 b = is->index(is); // Where are we after consuming 1 on channel token?
592 ts->debugger->consumeToken(ts->debugger, t); // Tell the debugger that we consumed the first token
596 // The standard consume caused the index to advance by more than 1,
597 // which can only happen if it skipped some off-channel tokens.
598 // we need to tell the debugger about those tokens.
602 for (i = a+1; i<b; i++)
604 ts->debugger->consumeHiddenToken(ts->debugger, ts->get(ts, (ANTLR3_UINT32)i));
610 /** A simple filter mechanism whereby you can tell this token stream
611 * to force all tokens of type ttype to be on channel. For example,
612 * when interpreting, we cannot execute actions so we need to tell
613 * the stream to force all WS and NEWLINE to be a different, ignored,
617 setTokenTypeChannel (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_UINT32 ttype, ANTLR3_UINT32 channel)
619 if (tokenStream->channelOverrides == NULL)
621 tokenStream->channelOverrides = antlr3ListNew(10);
624 /* We add one to the channel so we can distinguish NULL as being no entry in the
625 * table for a particular token type.
627 tokenStream->channelOverrides->put(tokenStream->channelOverrides, ttype, ANTLR3_FUNC_PTR((ANTLR3_UINT32)channel + 1), NULL);
631 discardTokenType (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 ttype)
633 if (tokenStream->discardSet == NULL)
635 tokenStream->discardSet = antlr3ListNew(31);
638 /* We add one to the channel so we can distinguish NULL as being no entry in the
639 * table for a particular token type. We could use bitsets for this I suppose too.
641 tokenStream->discardSet->put(tokenStream->discardSet, ttype, ANTLR3_FUNC_PTR((ANTLR3_UINT32)ttype + 1), NULL);
645 discardOffChannel (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_BOOLEAN discard)
647 tokenStream->discardOffChannel = discard;
650 static pANTLR3_VECTOR
651 getTokens (pANTLR3_COMMON_TOKEN_STREAM tokenStream)
653 if (tokenStream->p == -1)
655 fillBuffer(tokenStream);
658 return tokenStream->tokens;
662 getTokenRange (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_UINT32 start, ANTLR3_UINT32 stop)
664 return tokenStream->getTokensSet(tokenStream, start, stop, NULL);
666 /** Given a start and stop index, return a List of all tokens in
667 * the token type BitSet. Return null if no tokens were found. This
668 * method looks at both on and off channel tokens.
671 getTokensSet (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, pANTLR3_BITSET types)
673 pANTLR3_LIST filteredList;
676 pANTLR3_COMMON_TOKEN tok;
678 if (tokenStream->p == -1)
680 fillBuffer(tokenStream);
682 if (stop > tokenStream->tstream->istream->size(tokenStream->tstream->istream))
684 stop = tokenStream->tstream->istream->size(tokenStream->tstream->istream);
691 /* We have the range set, now we need to iterate through the
692 * installed tokens and create a new list with just the ones we want
693 * in it. We are just moving pointers about really.
695 filteredList = antlr3ListNew((ANTLR3_UINT32)tokenStream->tstream->istream->size(tokenStream->tstream->istream));
697 for (i = start, n = 0; i<= stop; i++)
699 tok = tokenStream->tstream->get(tokenStream->tstream, i);
702 || types->isMember(types, tok->getType(tok) == ANTLR3_TRUE)
705 filteredList->put(filteredList, n++, (void *)tok, NULL);
709 /* Did we get any then?
711 if (filteredList->size(filteredList) == 0)
713 filteredList->free(filteredList);
721 getTokensList (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, pANTLR3_LIST list)
723 pANTLR3_BITSET bitSet;
724 pANTLR3_LIST newlist;
726 bitSet = antlr3BitsetList(list->table);
728 newlist = tokenStream->getTokensSet(tokenStream, start, stop, bitSet);
730 bitSet->free(bitSet);
737 getTokensType (pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_UINT32 start, ANTLR3_UINT32 stop, ANTLR3_UINT32 type)
739 pANTLR3_BITSET bitSet;
740 pANTLR3_LIST newlist;
742 bitSet = antlr3BitsetOf(type, -1);
743 newlist = tokenStream->getTokensSet(tokenStream, start, stop, bitSet);
745 bitSet->free(bitSet);
751 _LA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i)
753 pANTLR3_TOKEN_STREAM ts;
754 pANTLR3_COMMON_TOKEN tok;
756 ts = (pANTLR3_TOKEN_STREAM) is->super;
758 tok = ts->_LT(ts, i);
762 return tok->getType(tok);
766 return ANTLR3_TOKEN_INVALID;
770 /// As per _LA() but for debug mode.
773 dbgLA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i)
775 pANTLR3_TOKEN_STREAM ts;
777 ts = (pANTLR3_TOKEN_STREAM) is->super;
779 if (ts->initialStreamState == ANTLR3_TRUE)
781 consumeInitialHiddenTokens(is);
783 ts->debugger->LT(ts->debugger, i, tokLT(ts, i));
788 mark (pANTLR3_INT_STREAM is)
790 is->lastMarker = is->index(is);
791 return is->lastMarker;
794 /// As per mark() but with a call to tell the debugger we are doing this
797 dbgMark (pANTLR3_INT_STREAM is)
799 pANTLR3_TOKEN_STREAM ts;
801 ts = (pANTLR3_TOKEN_STREAM) is->super;
803 is->lastMarker = is->index(is);
804 ts->debugger->mark(ts->debugger, is->lastMarker);
806 return is->lastMarker;
810 release (pANTLR3_INT_STREAM is, ANTLR3_MARKER mark)
816 size (pANTLR3_INT_STREAM is)
818 pANTLR3_COMMON_TOKEN_STREAM cts;
819 pANTLR3_TOKEN_STREAM ts;
821 if (is->cachedSize > 0)
823 return is->cachedSize;
825 ts = (pANTLR3_TOKEN_STREAM) is->super;
826 cts = (pANTLR3_COMMON_TOKEN_STREAM) ts->super;
828 is->cachedSize = cts->tokens->count;
829 return is->cachedSize;
833 tindex (pANTLR3_INT_STREAM is)
835 pANTLR3_COMMON_TOKEN_STREAM cts;
836 pANTLR3_TOKEN_STREAM ts;
838 ts = (pANTLR3_TOKEN_STREAM) is->super;
839 cts = (pANTLR3_COMMON_TOKEN_STREAM) ts->super;
845 dbgRewindLast (pANTLR3_INT_STREAM is)
847 pANTLR3_TOKEN_STREAM ts;
849 ts = (pANTLR3_TOKEN_STREAM) is->super;
851 ts->debugger->rewindLast(ts->debugger);
853 is->rewind(is, is->lastMarker);
856 rewindLast (pANTLR3_INT_STREAM is)
858 is->rewind(is, is->lastMarker);
861 rewindStream (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker)
863 is->seek(is, (ANTLR3_UINT32)(marker));
866 dbgRewindStream (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker)
868 pANTLR3_TOKEN_STREAM ts;
870 ts = (pANTLR3_TOKEN_STREAM) is->super;
872 ts->debugger->rewind(ts->debugger, marker);
874 is->seek(is, (ANTLR3_UINT32)(marker));
878 seek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index)
880 pANTLR3_COMMON_TOKEN_STREAM cts;
881 pANTLR3_TOKEN_STREAM ts;
883 ts = (pANTLR3_TOKEN_STREAM) is->super;
884 cts = (pANTLR3_COMMON_TOKEN_STREAM) ts->super;
886 cts->p = (ANTLR3_UINT32)index;
889 dbgSeek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index)
891 // TODO: Implement seek in debugger when Ter adds it to Java
896 fillBufferExt(pANTLR3_COMMON_TOKEN_STREAM tokenStream)
898 fillBuffer(tokenStream);
901 fillBuffer(pANTLR3_COMMON_TOKEN_STREAM tokenStream) {
903 pANTLR3_COMMON_TOKEN tok;
904 ANTLR3_BOOLEAN discard;
907 /* Start at index 0 of course
911 /* Pick out the next token from the token source
912 * Remember we just get a pointer (reference if you like) here
913 * and so if we store it anywhere, we don't set any pointers to auto free it.
915 tok = tokenStream->tstream->tokenSource->nextToken(tokenStream->tstream->tokenSource);
917 while (tok != NULL && tok->type != ANTLR3_TOKEN_EOF)
919 discard = ANTLR3_FALSE; /* Assume we are not discarding */
921 /* I employ a bit of a trick, or perhaps hack here. Rather than
922 * store a pointer to a structure in the override map and discard set
923 * we store the value + 1 cast to a void *. Hence on systems where NULL = (void *)0
924 * we can distinguish "not being there" from "being channel or type 0"
927 if (tokenStream->discardSet != NULL
928 && tokenStream->discardSet->get(tokenStream->discardSet, tok->getType(tok)) != NULL)
930 discard = ANTLR3_TRUE;
932 else if ( tokenStream->discardOffChannel == ANTLR3_TRUE
933 && tok->getChannel(tok) != tokenStream->channel
936 discard = ANTLR3_TRUE;
938 else if (tokenStream->channelOverrides != NULL)
940 /* See if this type is in the override map
942 channelI = tokenStream->channelOverrides->get(tokenStream->channelOverrides, tok->getType(tok) + 1);
944 if (channelI != NULL)
948 tok->setChannel(tok, ANTLR3_UINT32_CAST(channelI) - 1);
952 /* If not discarding it, add it to the list at the current index
954 if (discard == ANTLR3_FALSE)
956 /* Add it, indicating that we will delete it and the table should not
958 tok->setTokenIndex(tok, index);
960 tokenStream->tokens->add(tokenStream->tokens, (void *) tok, NULL);
964 tok = tokenStream->tstream->tokenSource->nextToken(tokenStream->tstream->tokenSource);
967 /* Cache the size so we don't keep doing indirect method calls. We do this as
968 * early as possible so that anything after this may utilize the cached value.
970 tokenStream->tstream->istream->cachedSize = tokenStream->tokens->count;
972 /* Set the consume pointer to the first token that is on our channel
975 tokenStream->p = skipOffTokenChannels(tokenStream, tokenStream->p);
979 /// Given a starting index, return the index of the first on-channel
983 skipOffTokenChannels(pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 i) {
985 pANTLR3_COMMON_TOKEN tok;
987 n = tokenStream->tstream->istream->cachedSize;
991 tok = (pANTLR3_COMMON_TOKEN)tokenStream->tokens->elements[i].element;
993 if (tok->channel!= tokenStream->channel)
1005 static ANTLR3_UINT32
1006 skipOffTokenChannelsReverse(pANTLR3_COMMON_TOKEN_STREAM tokenStream, ANTLR3_INT32 x)
1008 pANTLR3_COMMON_TOKEN tok;
1012 tok = (pANTLR3_COMMON_TOKEN)tokenStream->tokens->elements[x].element;
1014 if ((tok->channel != tokenStream->channel))
1026 /// Return a string that represents the name assoicated with the input source
1028 /// /param[in] is The ANTLR3_INT_STREAM interface that is representing this token stream.
1031 /// /implements ANTLR3_INT_STREAM_struct::getSourceName()
1033 static pANTLR3_STRING
1034 getSourceName (pANTLR3_INT_STREAM is)
1036 // Slightly convoluted as we must trace back to the lexer's input source
1037 // via the token source. The streamName that is here is not initialized
1038 // because this is a token stream, not a file or string stream, which are the
1039 // only things that have a context for a source name.
1041 return ((pANTLR3_TOKEN_STREAM)(is->super))->tokenSource->fileName;