antlr3tokenstream.inl 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. namespace antlr3 {
  2. template<class ImplTraits>
  3. TokenSource<ImplTraits>::TokenSource()
  4. :m_eofToken( ImplTraits::CommonTokenType::TOKEN_EOF),
  5. m_skipToken( ImplTraits::CommonTokenType::TOKEN_INVALID)
  6. {
  7. }
  8. template<class ImplTraits>
  9. ANTLR_INLINE typename TokenSource<ImplTraits>::CommonTokenType& TokenSource<ImplTraits>::get_eofToken()
  10. {
  11. return m_eofToken;
  12. }
  13. template<class ImplTraits>
  14. ANTLR_INLINE const typename TokenSource<ImplTraits>::TokenType& TokenSource<ImplTraits>::get_eofToken() const
  15. {
  16. return m_eofToken;
  17. }
  18. template<class ImplTraits>
  19. ANTLR_INLINE typename TokenSource<ImplTraits>::CommonTokenType& TokenSource<ImplTraits>::get_skipToken()
  20. {
  21. return m_skipToken;
  22. }
  23. template<class ImplTraits>
  24. ANTLR_INLINE typename TokenSource<ImplTraits>::StringType& TokenSource<ImplTraits>::get_fileName()
  25. {
  26. return m_fileName;
  27. }
  28. template<class ImplTraits>
  29. ANTLR_INLINE void TokenSource<ImplTraits>::set_fileName( const StringType& fileName )
  30. {
  31. m_fileName = fileName;
  32. }
  33. template<class ImplTraits>
  34. typename TokenSource<ImplTraits>::LexerType* TokenSource<ImplTraits>::get_super()
  35. {
  36. return static_cast<LexerType*>(this);
  37. }
  38. template<class ImplTraits>
  39. typename TokenSource<ImplTraits>::TokenType* TokenSource<ImplTraits>::nextTokenStr()
  40. {
  41. typedef typename LexerType::RecognizerSharedStateType RecognizerSharedStateType;
  42. typedef typename LexerType::InputStreamType InputStreamType;
  43. typedef typename LexerType::IntStreamType IntStreamType;
  44. LexerType* lexer;
  45. RecognizerSharedStateType* state;
  46. InputStreamType* input;
  47. IntStreamType* istream;
  48. lexer = this->get_super();
  49. state = lexer->get_rec()->get_state();
  50. input = lexer->get_input();
  51. istream = input->get_istream();
  52. /// Loop until we get a non skipped token or EOF
  53. ///
  54. for (;;)
  55. {
  56. // Get rid of any previous token (token factory takes care of
  57. // any de-allocation when this token is finally used up.
  58. //
  59. state->set_token_present(false);
  60. state->set_error(false); // Start out without an exception
  61. state->set_failed(false);
  62. // Now call the matching rules and see if we can generate a new token
  63. //
  64. for (;;)
  65. {
  66. // Record the start of the token in our input stream.
  67. //
  68. state->set_channel( TOKEN_DEFAULT_CHANNEL );
  69. state->set_tokenStartCharIndex( (ANTLR_MARKER)input->get_nextChar() );
  70. state->set_tokenStartCharPositionInLine( input->get_charPositionInLine() );
  71. state->set_tokenStartLine( input->get_line() );
  72. state->set_text("");
  73. if (istream->LA(1) == ANTLR_CHARSTREAM_EOF)
  74. {
  75. // Reached the end of the current stream, nothing more to do if this is
  76. // the last in the stack.
  77. //
  78. TokenType& teof = m_eofToken;
  79. teof.set_startIndex(lexer->getCharIndex());
  80. teof.set_stopIndex(lexer->getCharIndex());
  81. teof.set_line(lexer->getLine());
  82. return &teof;
  83. }
  84. state->set_token_present( false );
  85. state->set_error(false); // Start out without an exception
  86. state->set_failed(false);
  87. // Call the generated lexer, see if it can get a new token together.
  88. //
  89. lexer->mTokens();
  90. if (state->get_error() == true)
  91. {
  92. // Recognition exception, report it and try to recover.
  93. //
  94. state->set_failed(true);
  95. lexer->get_rec()->reportError();
  96. lexer->recover();
  97. if (state->get_token_present())
  98. // Good(or invalid) token factored by custom recover procedure
  99. //
  100. return state->get_token();
  101. }
  102. else
  103. {
  104. if ( !state->get_token_present() )
  105. {
  106. // Emit the real token, which adds it in to the token stream basically
  107. //
  108. lexer->emit();
  109. }
  110. else if ( *(state->get_token()) == m_skipToken )
  111. {
  112. // A real token could have been generated, but "Computer say's naaaaah" and it
  113. // it is just something we need to skip altogether.
  114. //
  115. continue;
  116. }
  117. // Good token, not skipped, not EOF token
  118. //
  119. return state->get_token();
  120. }
  121. }
  122. }
  123. }
  124. template<class ImplTraits>
  125. typename TokenSource<ImplTraits>::TokenType* TokenSource<ImplTraits>::nextToken()
  126. {
  127. return this->nextToken( BoolForwarder<LexerType::IsFiltered>() );
  128. }
  129. template<class ImplTraits>
  130. typename TokenSource<ImplTraits>::CommonTokenType* TokenSource<ImplTraits>::nextToken( BoolForwarder<true> /*isFiltered*/ )
  131. {
  132. LexerType* lexer;
  133. typename LexerType::RecognizerSharedStateType* state;
  134. lexer = this->get_super();
  135. state = lexer->get_lexstate();
  136. /* Get rid of any previous token (token factory takes care of
  137. * any deallocation when this token is finally used up.
  138. */
  139. state->set_token_present( false );
  140. state->set_error( false ); /* Start out without an exception */
  141. state->set_failed(false);
  142. /* Record the start of the token in our input stream.
  143. */
  144. state->set_tokenStartCharIndex( lexer->index() );
  145. state->set_tokenStartCharPositionInLine( lexer->getCharPositionInLine() );
  146. state->set_tokenStartLine( lexer->getLine() );
  147. state->set_text("");
  148. /* Now call the matching rules and see if we can generate a new token
  149. */
  150. for (;;)
  151. {
  152. if (lexer->LA(1) == ANTLR_CHARSTREAM_EOF)
  153. {
  154. /* Reached the end of the stream, nothing more to do.
  155. */
  156. CommonTokenType& teof = m_eofToken;
  157. teof.set_startIndex(lexer->getCharIndex());
  158. teof.set_stopIndex(lexer->getCharIndex());
  159. teof.set_line(lexer->getLine());
  160. return &teof;
  161. }
  162. state->set_token_present(false);
  163. state->set_error(false); /* Start out without an exception */
  164. {
  165. ANTLR_MARKER m;
  166. m = lexer->get_istream()->mark();
  167. state->set_backtracking(1); /* No exceptions */
  168. state->set_failed(false);
  169. /* Call the generated lexer, see if it can get a new token together.
  170. */
  171. lexer->mTokens();
  172. state->set_backtracking(0);
  173. /* mTokens backtracks with synpred at BACKTRACKING==2
  174. and we set the synpredgate to allow actions at level 1. */
  175. if(state->get_failed())
  176. {
  177. lexer->rewind(m);
  178. lexer->consume(); //<! advance one char and try again !>
  179. }
  180. else
  181. {
  182. lexer->emit(); /* Assemble the token and emit it to the stream */
  183. TokenType* tok = state->get_token();
  184. return tok;
  185. }
  186. }
  187. }
  188. }
  189. template<class ImplTraits>
  190. typename TokenSource<ImplTraits>::CommonTokenType* TokenSource<ImplTraits>::nextToken( BoolForwarder<false> /*isFiltered*/ )
  191. {
  192. // Find the next token in the current stream
  193. //
  194. CommonTokenType* tok = this->nextTokenStr();
  195. // If we got to the EOF token then switch to the previous
  196. // input stream if there were any and just return the
  197. // EOF if there are none. We must check the next token
  198. // in any outstanding input stream we pop into the active
  199. // role to see if it was sitting at EOF after PUSHing the
  200. // stream we just consumed, otherwise we will return EOF
  201. // on the reinstalled input stream, when in actual fact
  202. // there might be more input streams to POP before the
  203. // real EOF of the whole logical inptu stream. Hence we
  204. // use a while loop here until we find somethign in the stream
  205. // that isn't EOF or we reach the actual end of the last input
  206. // stream on the stack.
  207. //
  208. while(tok->get_type() == CommonTokenType::TOKEN_EOF)
  209. {
  210. typename ImplTraits::LexerType* lexer;
  211. lexer = static_cast<typename ImplTraits::LexerType*>( this->get_super() );
  212. if ( lexer->get_rec()->get_state()->get_streams().size() > 0)
  213. {
  214. // We have another input stream in the stack so we
  215. // need to revert to it, then resume the loop to check
  216. // it wasn't sitting at EOF itself.
  217. //
  218. lexer->popCharStream();
  219. tok = this->nextTokenStr();
  220. }
  221. else
  222. {
  223. // There were no more streams on the input stack
  224. // so this EOF is the 'real' logical EOF for
  225. // the input stream. So we just exit the loop and
  226. // return the EOF we have found.
  227. //
  228. break;
  229. }
  230. }
  231. // return whatever token we have, which may be EOF
  232. //
  233. return tok;
  234. }
  235. template<class ImplTraits>
  236. TokenStream<ImplTraits>::TokenStream()
  237. {
  238. m_tokenSource = NULL;
  239. m_debugger = NULL;
  240. m_initialStreamState = false;
  241. }
  242. template<class ImplTraits>
  243. typename TokenStream<ImplTraits>::IntStreamType* TokenStream<ImplTraits>::get_istream()
  244. {
  245. return this;
  246. }
  247. template<class ImplTraits>
  248. TokenStream<ImplTraits>::TokenStream(TokenSourceType* source, DebugEventListenerType* debugger)
  249. {
  250. m_initialStreamState = false;
  251. m_tokenSource = source;
  252. m_debugger = debugger;
  253. }
  254. template<class ImplTraits>
  255. CommonTokenStream<ImplTraits>::CommonTokenStream(ANTLR_UINT32 , TokenSourceType* source,
  256. DebugEventListenerType* debugger)
  257. : CommonTokenStream<ImplTraits>::BaseType( source, debugger )
  258. {
  259. m_p = -1;
  260. m_channel = TOKEN_DEFAULT_CHANNEL;
  261. m_discardOffChannel = false;
  262. m_nissued = 0;
  263. }
  264. template<class ImplTraits>
  265. typename CommonTokenStream<ImplTraits>::TokensType& CommonTokenStream<ImplTraits>::get_tokens()
  266. {
  267. return m_tokens;
  268. }
  269. template<class ImplTraits>
  270. const typename CommonTokenStream<ImplTraits>::TokensType& CommonTokenStream<ImplTraits>::get_tokens() const
  271. {
  272. return m_tokens;
  273. }
  274. template<class ImplTraits>
  275. typename CommonTokenStream<ImplTraits>::DiscardSetType& CommonTokenStream<ImplTraits>::get_discardSet()
  276. {
  277. return m_discardSet;
  278. }
  279. template<class ImplTraits>
  280. const typename CommonTokenStream<ImplTraits>::DiscardSetType& CommonTokenStream<ImplTraits>::get_discardSet() const
  281. {
  282. return m_discardSet;
  283. }
  284. template<class ImplTraits>
  285. ANTLR_INLINE ANTLR_INT32 CommonTokenStream<ImplTraits>::get_p() const
  286. {
  287. return m_p;
  288. }
  289. template<class ImplTraits>
  290. ANTLR_INLINE void CommonTokenStream<ImplTraits>::set_p( ANTLR_INT32 p )
  291. {
  292. m_p = p;
  293. }
  294. template<class ImplTraits>
  295. ANTLR_INLINE void CommonTokenStream<ImplTraits>::inc_p()
  296. {
  297. ++m_p;
  298. }
  299. template<class ImplTraits>
  300. ANTLR_INLINE void CommonTokenStream<ImplTraits>::dec_p()
  301. {
  302. --m_p;
  303. }
  304. template<class ImplTraits>
  305. ANTLR_INLINE ANTLR_MARKER CommonTokenStream<ImplTraits>::index_impl()
  306. {
  307. return m_p;
  308. }
  309. // Reset a token stream so it can be used again and can reuse it's
  310. // resources.
  311. //
  312. template<class ImplTraits>
  313. void CommonTokenStream<ImplTraits>::reset()
  314. {
  315. // Free any resources that ar most like specifc to the
  316. // run we just did.
  317. //
  318. m_discardSet.clear();
  319. m_channelOverrides.clear();
  320. // Now, if there were any existing tokens in the stream,
  321. // then we just reset the vector count so that it starts
  322. // again. We must traverse the entries unfortunately as
  323. // there may be free pointers for custom token types and
  324. // so on. However that is just a quick NULL check on the
  325. // vector entries.
  326. //
  327. m_tokens.clear();
  328. // Reset to defaults
  329. //
  330. m_discardOffChannel = false;
  331. m_channel = ImplTraits::CommonTokenType::TOKEN_DEFAULT_CHANNEL;
  332. m_p = -1;
  333. }
  334. template<class ImplTraits>
  335. void TokenStream<ImplTraits>::setDebugListener(DebugEventListenerType* debugger)
  336. {
  337. m_debugger = debugger;
  338. m_initialStreamState = false;
  339. }
  340. template<class ImplTraits>
  341. const typename TokenStream<ImplTraits>::TokenType* TokenStream<ImplTraits>::LT(ANTLR_INT32 k)
  342. {
  343. ANTLR_INT32 i;
  344. ANTLR_INT32 n;
  345. TokenStreamType* cts;
  346. cts = this->get_super();
  347. if(k < 0)
  348. {
  349. return cts->LB(-k);
  350. }
  351. ANTLR_INT32 req_idx = cts->get_p() + k - 1;
  352. ANTLR_INT32 cached_size = static_cast<ANTLR_INT32>(this->get_istream()->get_cachedSize());
  353. if( (cts->get_p() == -1) ||
  354. ( ( req_idx >= cached_size ) && ( (cached_size % ImplTraits::TOKEN_FILL_BUFFER_INCREMENT) == 0 ) )
  355. )
  356. {
  357. cts->fillBuffer();
  358. }
  359. // Here we used to check for k == 0 and return 0, but this seems
  360. // a superfluous check to me. LT(k=0) is therefore just undefined
  361. // and we won't waste the clock cycles on the check
  362. //
  363. cached_size = static_cast<ANTLR_INT32>(this->get_istream()->get_cachedSize());
  364. if ( req_idx >= cached_size )
  365. {
  366. TokenType& teof = cts->get_tokenSource()->get_eofToken();
  367. teof.set_startIndex( this->get_istream()->index());
  368. teof.set_stopIndex( this->get_istream()->index());
  369. return &teof;
  370. }
  371. i = cts->get_p();
  372. n = 1;
  373. /* Need to find k good tokens, skipping ones that are off channel
  374. */
  375. while( n < k)
  376. {
  377. /* Skip off-channel tokens */
  378. i = cts->skipOffTokenChannels(i+1); /* leave p on valid token */
  379. n++;
  380. }
  381. if( ( i >= cached_size ) && ( (cached_size % ImplTraits::TOKEN_FILL_BUFFER_INCREMENT) == 0 ) )
  382. {
  383. cts->fillBuffer();
  384. }
  385. if ( (ANTLR_UINT32) i >= this->get_istream()->get_cachedSize() )
  386. {
  387. TokenType& teof = cts->get_tokenSource()->get_eofToken();
  388. teof.set_startIndex(this->get_istream()->index());
  389. teof.set_stopIndex(this->get_istream()->index());
  390. return &teof;
  391. }
  392. // Here the token must be in the input vector. Rather then incur
  393. // function call penalty, we just return the pointer directly
  394. // from the vector
  395. //
  396. return cts->getToken(i);
  397. }
  398. template<class ImplTraits>
  399. const typename CommonTokenStream<ImplTraits>::TokenType* CommonTokenStream<ImplTraits>::LB(ANTLR_INT32 k)
  400. {
  401. ANTLR_INT32 i;
  402. ANTLR_INT32 n;
  403. if (m_p == -1)
  404. {
  405. this->fillBuffer();
  406. }
  407. if (k == 0)
  408. {
  409. return NULL;
  410. }
  411. if ((m_p - k) < 0)
  412. {
  413. return NULL;
  414. }
  415. i = m_p;
  416. n = 1;
  417. /* Need to find k good tokens, going backwards, skipping ones that are off channel
  418. */
  419. while (n <= k)
  420. {
  421. /* Skip off-channel tokens
  422. */
  423. i = this->skipOffTokenChannelsReverse(i - 1); /* leave p on valid token */
  424. n++;
  425. }
  426. if (i < 0)
  427. {
  428. return NULL;
  429. }
  430. // Here the token must be in the input vector. Rather then incut
  431. // function call penalty, we jsut return the pointer directly
  432. // from the vector
  433. //
  434. return this->getToken(i);
  435. }
  436. template<class ImplTraits>
  437. const typename CommonTokenStream<ImplTraits>::TokenType* CommonTokenStream<ImplTraits>::getToken(ANTLR_MARKER i)
  438. {
  439. return this->get(i);
  440. }
  441. template<class ImplTraits>
  442. const typename CommonTokenStream<ImplTraits>::TokenType* CommonTokenStream<ImplTraits>::get(ANTLR_MARKER i)
  443. {
  444. return this->getToken( static_cast<ANTLR_MARKER>(i),
  445. BoolForwarder<ImplTraits::TOKENS_ACCESSED_FROM_OWNING_RULE>() );
  446. }
  447. template<class ImplTraits>
  448. const typename CommonTokenStream<ImplTraits>::TokenType* CommonTokenStream<ImplTraits>::getToken( ANTLR_MARKER tok_idx,
  449. BoolForwarder<true> /*tokens_accessed_from_owning_rule*/ )
  450. {
  451. typename TokensType::iterator iter = m_tokens.find(tok_idx);
  452. if( iter == m_tokens.end() )
  453. {
  454. TokenAccessException ex;
  455. throw ex;
  456. }
  457. const TokenType& tok = iter->second;
  458. return &tok;
  459. }
  460. template<class ImplTraits>
  461. const typename CommonTokenStream<ImplTraits>::TokenType* CommonTokenStream<ImplTraits>::getToken( ANTLR_MARKER tok_idx, BoolForwarder<false> /*tokens_accessed_from_owning_rule*/ )
  462. {
  463. TokenType& tok = m_tokens.at( static_cast<ANTLR_UINT32>(tok_idx) );
  464. return &tok;
  465. }
  466. template<class ImplTraits>
  467. typename TokenStream<ImplTraits>::TokenSourceType* TokenStream<ImplTraits>::get_tokenSource() const
  468. {
  469. return m_tokenSource;
  470. }
  471. template<class ImplTraits>
  472. void TokenStream<ImplTraits>::set_tokenSource( TokenSourceType* tokenSource )
  473. {
  474. m_tokenSource = tokenSource;
  475. }
  476. template<class ImplTraits>
  477. typename TokenStream<ImplTraits>::StringType TokenStream<ImplTraits>::toString()
  478. {
  479. TokenStreamType* cts = static_cast<TokenStreamType>(this);
  480. if (cts->get_p() == -1)
  481. {
  482. cts->fillBuffer();
  483. }
  484. return this->toStringSS(0, this->get_istream()->size());
  485. }
  486. template<class ImplTraits>
  487. typename TokenStream<ImplTraits>::StringType
  488. TokenStream<ImplTraits>::toStringSS(ANTLR_MARKER start, ANTLR_MARKER stop)
  489. {
  490. StringType string;
  491. TokenSourceType* tsource;
  492. const TokenType* tok;
  493. TokenStreamType* cts;
  494. cts = this->get_super();
  495. if (cts->get_p() == -1)
  496. {
  497. cts->fillBuffer();
  498. }
  499. if (stop >= this->get_istream()->size())
  500. {
  501. stop = this->get_istream()->size() - 1;
  502. }
  503. /* Who is giving us these tokens?
  504. */
  505. tsource = cts->get_tokenSource();
  506. if (tsource != NULL && !cts->get_tokens().empty() )
  507. {
  508. /* Finally, let's get a string
  509. */
  510. for (ANTLR_MARKER i = start; i <= stop; i++)
  511. {
  512. tok = cts->get(i);
  513. if (tok != NULL)
  514. {
  515. string.append( tok->getText() );
  516. }
  517. }
  518. return string;
  519. }
  520. return "";
  521. }
  522. template<class ImplTraits>
  523. typename TokenStream<ImplTraits>::StringType
  524. TokenStream<ImplTraits>::toStringTT(const TokenType* start, const TokenType* stop)
  525. {
  526. if (start != NULL && stop != NULL)
  527. {
  528. return this->toStringSS( start->get_tokenIndex(),
  529. stop->get_tokenIndex());
  530. }
  531. else
  532. {
  533. return "";
  534. }
  535. }
  536. /** A simple filter mechanism whereby you can tell this token stream
  537. * to force all tokens of type ttype to be on channel. For example,
  538. * when interpreting, we cannot execute actions so we need to tell
  539. * the stream to force all WS and NEWLINE to be a different, ignored,
  540. * channel.
  541. */
  542. template<class ImplTraits>
  543. void CommonTokenStream<ImplTraits>::setTokenTypeChannel ( ANTLR_UINT32 ttype, ANTLR_UINT32 channel)
  544. {
  545. /* We add one to the channel so we can distinguish NULL as being no entry in the
  546. * table for a particular token type.
  547. */
  548. m_channelOverrides[ttype] = (ANTLR_UINT32)channel + 1;
  549. }
  550. template<class ImplTraits>
  551. void CommonTokenStream<ImplTraits>::discardTokenType(ANTLR_INT32 ttype)
  552. {
  553. /* We add one to the channel so we can distinguish NULL as being no entry in the
  554. * table for a particular token type. We could use bitsets for this I suppose too.
  555. */
  556. m_discardSet.insert(ttype);
  557. }
  558. template<class ImplTraits>
  559. void CommonTokenStream<ImplTraits>::discardOffChannelToks(bool discard)
  560. {
  561. m_discardOffChannel = discard;
  562. }
  563. template<class ImplTraits>
  564. typename CommonTokenStream<ImplTraits>::TokensType* CommonTokenStream<ImplTraits>::getTokens()
  565. {
  566. if (m_p == -1)
  567. {
  568. this->fillBuffer();
  569. }
  570. return &m_tokens;
  571. }
  572. template<class ImplTraits>
  573. void CommonTokenStream<ImplTraits>::getTokenRange(ANTLR_UINT32 start, ANTLR_UINT32 stop,
  574. TokensListType& tokenRange)
  575. {
  576. return this->getTokensSet(start, stop, NULL, tokenRange);
  577. }
  578. /** Given a start and stop index, return a List of all tokens in
  579. * the token type BitSet. Return null if no tokens were found. This
  580. * method looks at both on and off channel tokens.
  581. */
  582. template<class ImplTraits>
  583. void
  584. CommonTokenStream<ImplTraits>::getTokensSet(ANTLR_UINT32 start, ANTLR_UINT32 stop, BitsetType* types,
  585. TokensListType& filteredList )
  586. {
  587. ANTLR_UINT32 i;
  588. ANTLR_UINT32 n;
  589. TokenType* tok;
  590. if ( m_p == -1)
  591. {
  592. this->fillBuffer();
  593. }
  594. if (stop > this->get_istream()->size())
  595. {
  596. stop = this->get_istream()->size();
  597. }
  598. if (start > stop)
  599. {
  600. return;
  601. }
  602. /* We have the range set, now we need to iterate through the
  603. * installed tokens and create a new list with just the ones we want
  604. * in it. We are just moving pointers about really.
  605. */
  606. for(i = start, n = 0; i<= stop; i++)
  607. {
  608. tok = this->get(i);
  609. if ( types == NULL
  610. || (types->isMember( tok->get_type() ) == true )
  611. )
  612. {
  613. filteredList.push_back(tok);
  614. }
  615. }
  616. return ;
  617. }
  618. template<class ImplTraits>
  619. void
  620. CommonTokenStream<ImplTraits>::getTokensList(ANTLR_UINT32 start, ANTLR_UINT32 stop,
  621. const IntListType& list, TokensListType& newlist)
  622. {
  623. BitsetType* bitSet;
  624. bitSet = Bitset<ImplTraits>::BitsetFromList(list);
  625. this->getTokensSet(start, stop, bitSet, newlist);
  626. delete bitSet;
  627. }
  628. template<class ImplTraits>
  629. void
  630. CommonTokenStream<ImplTraits>::getTokensType(ANTLR_UINT32 start, ANTLR_UINT32 stop, ANTLR_UINT32 type,
  631. TokensListType& newlist )
  632. {
  633. BitsetType* bitSet;
  634. bitSet = BitsetType::BitsetOf(type, -1);
  635. this->getTokensSet(start, stop, bitSet, newlist);
  636. delete bitSet;
  637. }
  638. template<class ImplTraits>
  639. void CommonTokenStream<ImplTraits>::fillBufferExt()
  640. {
  641. this->fillBuffer();
  642. }
  643. template<class ImplTraits>
  644. bool CommonTokenStream<ImplTraits>::hasReachedFillbufferTarget( ANTLR_UINT32 cnt,
  645. BoolForwarder<true> )
  646. {
  647. return ( cnt >= ImplTraits::TOKEN_FILL_BUFFER_INCREMENT );
  648. }
  649. template<class ImplTraits>
  650. bool CommonTokenStream<ImplTraits>::hasReachedFillbufferTarget( ANTLR_UINT32,
  651. BoolForwarder<false> )
  652. {
  653. return false;
  654. }
  655. template<class ImplTraits>
  656. void CommonTokenStream<ImplTraits>::fillBuffer()
  657. {
  658. ANTLR_UINT32 index;
  659. TokenType* tok;
  660. bool discard;
  661. /* Start at index 0 of course
  662. */
  663. ANTLR_UINT32 cached_p = (m_p < 0) ? 0 : m_p;
  664. index = m_nissued;
  665. ANTLR_UINT32 cnt = 0;
  666. /* Pick out the next token from the token source
  667. * Remember we just get a pointer (reference if you like) here
  668. * and so if we store it anywhere, we don't set any pointers to auto free it.
  669. */
  670. tok = this->get_tokenSource()->nextToken();
  671. while ( tok->get_type() != TokenType::TOKEN_EOF )
  672. {
  673. discard = false; /* Assume we are not discarding */
  674. /* I employ a bit of a trick, or perhaps hack here. Rather than
  675. * store a pointer to a structure in the override map and discard set
  676. * we store the value + 1 cast to a void *. Hence on systems where NULL = (void *)0
  677. * we can distinguish "not being there" from "being channel or type 0"
  678. */
  679. if ( m_discardSet.find(tok->get_type()) != m_discardSet.end() )
  680. {
  681. discard = true;
  682. }
  683. else if ( m_discardOffChannel == true
  684. && tok->get_channel() != m_channel
  685. )
  686. {
  687. discard = true;
  688. }
  689. else if (!m_channelOverrides.empty())
  690. {
  691. /* See if this type is in the override map
  692. */
  693. typename ChannelOverridesType::iterator iter = m_channelOverrides.find( tok->get_type() + 1 );
  694. if (iter != m_channelOverrides.end())
  695. {
  696. /* Override found
  697. */
  698. tok->set_channel( ANTLR_UINT32_CAST(iter->second) - 1);
  699. }
  700. }
  701. /* If not discarding it, add it to the list at the current index
  702. */
  703. if (discard == false)
  704. {
  705. /* Add it, indicating that we will delete it and the table should not
  706. */
  707. tok->set_tokenIndex(index);
  708. ++m_p;
  709. this->insertToken(*tok);
  710. index++;
  711. m_nissued++;
  712. cnt++;
  713. }
  714. if( !this->hasReachedFillbufferTarget( cnt,
  715. BoolForwarder<ImplTraits::TOKENS_ACCESSED_FROM_OWNING_RULE>() ) )
  716. tok = this->get_tokenSource()->nextToken();
  717. else
  718. break;
  719. }
  720. /* Cache the size so we don't keep doing indirect method calls. We do this as
  721. * early as possible so that anything after this may utilize the cached value.
  722. */
  723. this->get_istream()->set_cachedSize( m_nissued );
  724. /* Set the consume pointer to the first token that is on our channel, we just read
  725. */
  726. m_p = cached_p;
  727. m_p = this->skipOffTokenChannels( m_p );
  728. }
  729. /// Given a starting index, return the index of the first on-channel
  730. /// token.
  731. ///
  732. template<class ImplTraits>
  733. ANTLR_UINT32 CommonTokenStream<ImplTraits>::skipOffTokenChannels(ANTLR_INT32 i)
  734. {
  735. ANTLR_INT32 n;
  736. n = this->get_istream()->get_cachedSize();
  737. while (i < n)
  738. {
  739. const TokenType* tok = this->getToken(i);
  740. if (tok->get_channel() != m_channel )
  741. {
  742. i++;
  743. }
  744. else
  745. {
  746. return i;
  747. }
  748. }
  749. return i;
  750. }
  751. template<class ImplTraits>
  752. ANTLR_UINT32 CommonTokenStream<ImplTraits>::skipOffTokenChannelsReverse(ANTLR_INT32 x)
  753. {
  754. while (x >= 0)
  755. {
  756. const TokenType* tok = this->getToken(x);
  757. if( tok->get_channel() != m_channel )
  758. {
  759. x--;
  760. }
  761. else
  762. {
  763. return x;
  764. }
  765. }
  766. return x;
  767. }
  768. template<class ImplTraits>
  769. void CommonTokenStream<ImplTraits>::discardTokens( ANTLR_MARKER start, ANTLR_MARKER stop )
  770. {
  771. this->discardTokens( start, stop, BoolForwarder< ImplTraits::TOKENS_ACCESSED_FROM_OWNING_RULE >() );
  772. }
  773. template<class ImplTraits>
  774. void CommonTokenStream<ImplTraits>::discardTokens( ANTLR_MARKER start, ANTLR_MARKER stop,
  775. BoolForwarder<true> /*tokens_accessed_from_owning_rule */ )
  776. {
  777. typename TokensType::iterator iter1 = m_tokens.lower_bound(start);
  778. typename TokensType::iterator iter2 = m_tokens.upper_bound(stop);
  779. m_tokens.erase( iter1, iter2 );
  780. }
  781. template<class ImplTraits>
  782. void CommonTokenStream<ImplTraits>::discardTokens( ANTLR_MARKER start, ANTLR_MARKER stop,
  783. BoolForwarder<false> /*tokens_accessed_from_owning_rule*/ )
  784. {
  785. m_tokens.erase( m_tokens.begin() + start, m_tokens.begin() + stop );
  786. }
  787. template<class ImplTraits>
  788. void CommonTokenStream<ImplTraits>::insertToken( const TokenType& tok )
  789. {
  790. this->insertToken( tok, BoolForwarder< ImplTraits::TOKENS_ACCESSED_FROM_OWNING_RULE >() );
  791. }
  792. template<class ImplTraits>
  793. void CommonTokenStream<ImplTraits>::insertToken( const TokenType& tok, BoolForwarder<true> /*tokens_accessed_from_owning_rule*/ )
  794. {
  795. assert( m_tokens.find( tok.get_index() ) == m_tokens.end() );
  796. assert( tok.get_index() == m_nissued );
  797. m_tokens[ tok.get_index() ] = tok;
  798. }
  799. template<class ImplTraits>
  800. void CommonTokenStream<ImplTraits>::insertToken( const TokenType& tok, BoolForwarder<false> /*tokens_accessed_from_owning_rule*/ )
  801. {
  802. m_tokens.push_back( tok );
  803. }
  804. template<class ImplTraits>
  805. CommonTokenStream<ImplTraits>::~CommonTokenStream()
  806. {
  807. m_tokens.clear();
  808. }
  809. }