antlr3baserecognizer.inl 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. namespace antlr3 {
  2. template< class ImplTraits, class StreamType >
  3. BaseRecognizer<ImplTraits, StreamType>::BaseRecognizer(ANTLR_UINT32 sizeHint,
  4. RecognizerSharedStateType* state)
  5. {
  6. m_debugger = NULL;
  7. // If we have been supplied with a pre-existing recognizer state
  8. // then we just install it, otherwise we must create one from scratch
  9. //
  10. if (state == NULL)
  11. {
  12. m_state = new RecognizerSharedStateType();
  13. m_state->set_sizeHint( sizeHint );
  14. }
  15. else
  16. {
  17. // Install the one we were given, and do not reset it here
  18. // as it will either already have been initialized or will
  19. // be in a state that needs to be preserved.
  20. //
  21. m_state = state;
  22. }
  23. }
  24. template< class ImplTraits, class StreamType >
  25. ANTLR_INLINE typename BaseRecognizer<ImplTraits, StreamType>::SuperType* BaseRecognizer<ImplTraits, StreamType>::get_super()
  26. {
  27. return static_cast<SuperType*>(this);
  28. }
  29. template< class ImplTraits, class StreamType >
  30. ANTLR_INLINE typename BaseRecognizer<ImplTraits, StreamType>::RecognizerSharedStateType* BaseRecognizer<ImplTraits, StreamType>::get_state() const
  31. {
  32. return m_state;
  33. }
  34. template< class ImplTraits, class StreamType >
  35. ANTLR_INLINE typename BaseRecognizer<ImplTraits, StreamType>::DebugEventListenerType* BaseRecognizer<ImplTraits, StreamType>::get_debugger() const
  36. {
  37. return m_debugger;
  38. }
  39. template< class ImplTraits, class StreamType >
  40. ANTLR_INLINE void BaseRecognizer<ImplTraits, StreamType>::set_state( RecognizerSharedStateType* state )
  41. {
  42. m_state = state;
  43. }
  44. template< class ImplTraits, class StreamType >
  45. ANTLR_INLINE void BaseRecognizer<ImplTraits, StreamType>::set_debugger( DebugEventListenerType* debugger )
  46. {
  47. m_debugger = debugger;
  48. }
  49. template< class ImplTraits, class StreamType >
  50. const typename BaseRecognizer<ImplTraits, StreamType>::UnitType*
  51. BaseRecognizer<ImplTraits, StreamType>::match(ANTLR_UINT32 ttype, BitsetListType* follow)
  52. {
  53. SuperType* super = static_cast<SuperType*>(this);
  54. IntStreamType* is = super->get_istream();
  55. // Pick up the current input token/node for assignment to labels
  56. //
  57. const UnitType* matchedSymbol = this->getCurrentInputSymbol(is);
  58. //if (is->LA(1) == ttype)
  59. if (matchedSymbol->get_type() == ttype)
  60. {
  61. // The token was the one we were told to expect
  62. //
  63. is->consume(); // Consume that token from the stream
  64. m_state->set_errorRecovery(false); // Not in error recovery now (if we were)
  65. m_state->set_failed(false); // The match was a success
  66. return matchedSymbol; // We are done
  67. }
  68. // We did not find the expected token type, if we are backtracking then
  69. // we just set the failed flag and return.
  70. //
  71. if ( m_state->get_backtracking() > 0)
  72. {
  73. // Backtracking is going on
  74. //
  75. m_state->set_failed(true);
  76. return matchedSymbol;
  77. }
  78. // We did not find the expected token and there is no backtracking
  79. // going on, so we mismatch, which creates an exception in the recognizer exception
  80. // stack.
  81. //
  82. matchedSymbol = this->recoverFromMismatchedToken(ttype, follow);
  83. return matchedSymbol;
  84. }
  85. template< class ImplTraits, class StreamType >
  86. void BaseRecognizer<ImplTraits, StreamType>::matchAny()
  87. {
  88. SuperType* super = static_cast<SuperType*>(this);
  89. IntStreamType* is = super->get_istream();
  90. is->consume();
  91. m_state->set_errorRecovery(false);
  92. m_state->set_failed(false);
  93. return;
  94. }
  95. template< class ImplTraits, class StreamType >
  96. bool BaseRecognizer<ImplTraits, StreamType>::mismatchIsUnwantedToken(IntStreamType* is, ANTLR_UINT32 ttype)
  97. {
  98. ANTLR_UINT32 nextt = is->LA(2);
  99. if (nextt == ttype)
  100. {
  101. if(m_state->get_exception() != NULL)
  102. m_state->get_exception()->set_expecting(nextt);
  103. return true; // This token is unknown, but the next one is the one we wanted
  104. }
  105. else
  106. return false; // Neither this token, nor the one following is the one we wanted
  107. }
  108. template< class ImplTraits, class StreamType >
  109. bool BaseRecognizer<ImplTraits, StreamType>::mismatchIsMissingToken(IntStreamType* is, BitsetListType* follow)
  110. {
  111. bool retcode;
  112. BitsetType* followClone;
  113. BitsetType* viableTokensFollowingThisRule;
  114. if (follow == NULL)
  115. {
  116. // There is no information about the tokens that can follow the last one
  117. // hence we must say that the current one we found is not a member of the
  118. // follow set and does not indicate a missing token. We will just consume this
  119. // single token and see if the parser works it out from there.
  120. //
  121. return false;
  122. }
  123. followClone = NULL;
  124. viableTokensFollowingThisRule = NULL;
  125. // The C bitset maps are laid down at compile time by the
  126. // C code generation. Hence we cannot remove things from them
  127. // and so on. So, in order to remove EOR (if we need to) then
  128. // we clone the static bitset.
  129. //
  130. followClone = follow->bitsetLoad();
  131. if (followClone == NULL)
  132. return false;
  133. // Compute what can follow this grammar reference
  134. //
  135. if (followClone->isMember( ImplTraits::CommonTokenType::EOR_TOKEN_TYPE))
  136. {
  137. // EOR can follow, but if we are not the start symbol, we
  138. // need to remove it.
  139. //
  140. followClone->remove(ImplTraits::CommonTokenType::EOR_TOKEN_TYPE);
  141. // Now compute the visiable tokens that can follow this rule, according to context
  142. // and make them part of the follow set.
  143. //
  144. viableTokensFollowingThisRule = this->computeCSRuleFollow();
  145. followClone->borInPlace(viableTokensFollowingThisRule);
  146. }
  147. /// if current token is consistent with what could come after set
  148. /// then we know we're missing a token; error recovery is free to
  149. /// "insert" the missing token
  150. ///
  151. /// BitSet cannot handle negative numbers like -1 (EOF) so I leave EOR
  152. /// in follow set to indicate that the fall of the start symbol is
  153. /// in the set (EOF can follow).
  154. ///
  155. if ( followClone->isMember(is->LA(1))
  156. || followClone->isMember(ImplTraits::CommonTokenType::EOR_TOKEN_TYPE)
  157. )
  158. {
  159. retcode = true;
  160. }
  161. else
  162. {
  163. retcode = false;
  164. }
  165. if (viableTokensFollowingThisRule != NULL)
  166. {
  167. delete viableTokensFollowingThisRule;
  168. }
  169. if (followClone != NULL)
  170. {
  171. delete followClone;
  172. }
  173. return retcode;
  174. }
  175. template< class ImplTraits, class StreamType >
  176. void BaseRecognizer<ImplTraits, StreamType>::mismatch(ANTLR_UINT32 ttype, BitsetListType* follow)
  177. {
  178. this->get_super()->mismatch( ttype, follow );
  179. }
  180. template< class ImplTraits, class StreamType >
  181. void BaseRecognizer<ImplTraits, StreamType>::reportError()
  182. {
  183. this->reportError( ClassForwarder<SuperType>() );
  184. }
  185. template< class ImplTraits, class StreamType >
  186. void BaseRecognizer<ImplTraits, StreamType>::reportError( ClassForwarder<LexerType> )
  187. {
  188. // Indicate this recognizer had an error while processing.
  189. //
  190. m_state->inc_errorCount();
  191. this->displayRecognitionError(m_state->get_tokenNames());
  192. }
  193. template< class ImplTraits, class StreamType >
  194. template<typename CompType>
  195. void BaseRecognizer<ImplTraits, StreamType>::reportError(ClassForwarder<CompType> )
  196. {
  197. // Invoke the debugger event if there is a debugger listening to us
  198. //
  199. if ( m_debugger != NULL)
  200. {
  201. m_debugger->recognitionException( m_state->get_exception() );
  202. }
  203. if ( m_state->get_errorRecovery() == true)
  204. {
  205. // Already in error recovery so don't display another error while doing so
  206. //
  207. return;
  208. }
  209. // Signal we are in error recovery now
  210. //
  211. m_state->set_errorRecovery(true);
  212. // Indicate this recognizer had an error while processing.
  213. //
  214. m_state->inc_errorCount();
  215. // Call the error display routine
  216. //
  217. this->displayRecognitionError( m_state->get_tokenNames() );
  218. }
  219. template< class ImplTraits, class StreamType >
  220. void BaseRecognizer<ImplTraits, StreamType>::displayRecognitionError(ANTLR_UINT8** tokenNames)
  221. {
  222. // Retrieve some info for easy reading.
  223. //
  224. ExceptionBaseType* ex = m_state->get_exception();
  225. StringType ttext;
  226. // See if there is a 'filename' we can use
  227. //
  228. SuperType* super = static_cast<SuperType*>(this);
  229. super->displayRecognitionError(tokenNames, ex);
  230. }
  231. template< class ImplTraits, class StreamType >
  232. ANTLR_UINT32 BaseRecognizer<ImplTraits, StreamType>::getNumberOfSyntaxErrors()
  233. {
  234. return m_state->get_errorCount();
  235. }
  236. template< class ImplTraits, class StreamType >
  237. void BaseRecognizer<ImplTraits, StreamType>::recover()
  238. {
  239. SuperType* super = static_cast<SuperType*>(this);
  240. IntStreamType* is = super->get_parser_istream();
  241. // Are we about to repeat the same error?
  242. //
  243. if ( m_state->get_lastErrorIndex() == is->index())
  244. {
  245. // The last error was at the same token index point. This must be a case
  246. // where LT(1) is in the recovery token set so nothing is
  247. // consumed. Consume a single token so at least to prevent
  248. // an infinite loop; this is a failsafe.
  249. //
  250. is->consume();
  251. }
  252. // Record error index position
  253. //
  254. m_state->set_lastErrorIndex( is->index() );
  255. // Work out the follows set for error recovery
  256. //
  257. BitsetType* followSet = this->computeErrorRecoverySet();
  258. // Call resync hook (for debuggers and so on)
  259. //
  260. this->beginResync();
  261. // Consume tokens until we have resynced to something in the follows set
  262. //
  263. this->consumeUntilSet(followSet);
  264. // End resync hook
  265. //
  266. this->endResync();
  267. // Destroy the temporary bitset we produced.
  268. //
  269. delete followSet;
  270. // Reset the inError flag so we don't re-report the exception
  271. //
  272. m_state->set_error(false);
  273. m_state->set_failed(false);
  274. }
  275. template< class ImplTraits, class StreamType >
  276. void BaseRecognizer<ImplTraits, StreamType>::beginResync()
  277. {
  278. if (m_debugger != NULL)
  279. {
  280. m_debugger->beginResync();
  281. }
  282. }
  283. template< class ImplTraits, class StreamType >
  284. void BaseRecognizer<ImplTraits, StreamType>::endResync()
  285. {
  286. if (m_debugger != NULL)
  287. {
  288. m_debugger->endResync();
  289. }
  290. }
  291. template< class ImplTraits, class StreamType >
  292. void BaseRecognizer<ImplTraits, StreamType>::beginBacktrack(ANTLR_UINT32 level)
  293. {
  294. if (m_debugger != NULL)
  295. {
  296. m_debugger->beginBacktrack(level);
  297. }
  298. }
  299. template< class ImplTraits, class StreamType >
  300. void BaseRecognizer<ImplTraits, StreamType>::endBacktrack(ANTLR_UINT32 level, bool /*successful*/)
  301. {
  302. if (m_debugger != NULL)
  303. {
  304. m_debugger->endBacktrack(level);
  305. }
  306. }
  307. template< class ImplTraits, class StreamType >
  308. typename BaseRecognizer<ImplTraits, StreamType>::BitsetType* BaseRecognizer<ImplTraits, StreamType>::computeErrorRecoverySet()
  309. {
  310. return this->combineFollows(false);
  311. }
  312. template< class ImplTraits, class StreamType >
  313. typename BaseRecognizer<ImplTraits, StreamType>::BitsetType* BaseRecognizer<ImplTraits, StreamType>::computeCSRuleFollow()
  314. {
  315. return this->combineFollows(false);
  316. }
  317. template< class ImplTraits, class StreamType >
  318. typename BaseRecognizer<ImplTraits, StreamType>::BitsetType* BaseRecognizer<ImplTraits, StreamType>::combineFollows(bool exact)
  319. {
  320. BitsetType* followSet;
  321. BitsetType* localFollowSet;
  322. ANTLR_UINT32 top;
  323. ANTLR_UINT32 i;
  324. top = static_cast<ANTLR_UINT32>( m_state->get_following().size() );
  325. followSet = new BitsetType(0);
  326. localFollowSet = NULL;
  327. for (i = top; i>0; i--)
  328. {
  329. localFollowSet = m_state->get_following().at(i-1).bitsetLoad();
  330. if (localFollowSet != NULL)
  331. {
  332. followSet->borInPlace(localFollowSet);
  333. if (exact == true)
  334. {
  335. if (localFollowSet->isMember( ImplTraits::CommonTokenType::EOR_TOKEN_TYPE) == false)
  336. {
  337. // Only leave EOR in the set if at top (start rule); this lets us know
  338. // if we have to include the follow(start rule); I.E., EOF
  339. //
  340. if (i>1)
  341. {
  342. followSet->remove(ImplTraits::CommonTokenType::EOR_TOKEN_TYPE);
  343. }
  344. }
  345. else
  346. {
  347. break; // Cannot see End Of Rule from here, just drop out
  348. }
  349. }
  350. delete localFollowSet;
  351. localFollowSet = NULL;
  352. }
  353. }
  354. if (localFollowSet != NULL)
  355. {
  356. delete localFollowSet;
  357. }
  358. return followSet;
  359. }
  360. template< class ImplTraits, class StreamType >
  361. const typename BaseRecognizer<ImplTraits, StreamType>::UnitType*
  362. BaseRecognizer<ImplTraits, StreamType>::recoverFromMismatchedToken( ANTLR_UINT32 ttype, BitsetListType* follow)
  363. {
  364. SuperType* super = static_cast<SuperType*>(this);
  365. IntStreamType* is = super->get_parser_istream();
  366. const UnitType* matchedSymbol;
  367. // If the next token after the one we are looking at in the input stream
  368. // is what we are looking for then we remove the one we have discovered
  369. // from the stream by consuming it, then consume this next one along too as
  370. // if nothing had happened.
  371. //
  372. if ( this->mismatchIsUnwantedToken( is, ttype) == true)
  373. {
  374. // Create an exception if we need one
  375. //
  376. new ANTLR_Exception<ImplTraits, UNWANTED_TOKEN_EXCEPTION, StreamType>(this, "");
  377. // Call resync hook (for debuggers and so on)
  378. //
  379. if (m_debugger != NULL)
  380. {
  381. m_debugger->beginResync();
  382. }
  383. // "delete" the extra token
  384. //
  385. this->beginResync();
  386. is->consume();
  387. this->endResync();
  388. // End resync hook
  389. //
  390. if (m_debugger != NULL)
  391. {
  392. m_debugger->endResync();
  393. }
  394. // Print out the error after we consume so that ANTLRWorks sees the
  395. // token in the exception.
  396. //
  397. this->reportError();
  398. // Return the token we are actually matching
  399. //
  400. matchedSymbol = this->getCurrentInputSymbol(is);
  401. // Consume the token that the rule actually expected to get as if everything
  402. // was hunky dory.
  403. //
  404. is->consume();
  405. m_state->set_error(false); // Exception is not outstanding any more
  406. return matchedSymbol;
  407. }
  408. // Single token deletion (Unwanted above) did not work
  409. // so we see if we can insert a token instead by calculating which
  410. // token would be missing
  411. //
  412. if ( this->mismatchIsMissingToken(is, follow))
  413. {
  414. // We can fake the missing token and proceed
  415. //
  416. new ANTLR_Exception<ImplTraits, MISSING_TOKEN_EXCEPTION, StreamType>(this, "");
  417. matchedSymbol = this->getMissingSymbol( is, m_state->get_exception(), ttype, follow);
  418. m_state->get_exception()->set_token( matchedSymbol );
  419. m_state->get_exception()->set_expecting(ttype);
  420. // Print out the error after we insert so that ANTLRWorks sees the
  421. // token in the exception.
  422. //
  423. this->reportError();
  424. m_state->set_error(false); // Exception is not outstanding any more
  425. return matchedSymbol;
  426. }
  427. // Create an exception if we need one
  428. //
  429. new ANTLR_Exception<ImplTraits, RECOGNITION_EXCEPTION, StreamType>(this, "");
  430. // Neither deleting nor inserting tokens allows recovery
  431. // must just report the exception.
  432. //
  433. m_state->set_error(true);
  434. return NULL;
  435. }
  436. template< class ImplTraits, class StreamType >
  437. const typename BaseRecognizer<ImplTraits, StreamType>::UnitType*
  438. BaseRecognizer<ImplTraits, StreamType>::recoverFromMismatchedSet(BitsetListType* follow)
  439. {
  440. SuperType* super = static_cast<SuperType*>(this);
  441. IntStreamType* is = super->get_parser_istream();
  442. const UnitType* matchedSymbol;
  443. if (this->mismatchIsMissingToken(is, follow) == true)
  444. {
  445. // We can fake the missing token and proceed
  446. //
  447. new ANTLR_Exception<ImplTraits, MISSING_TOKEN_EXCEPTION, StreamType>(this);
  448. matchedSymbol = this->getMissingSymbol(is, m_state->get_exception(), follow);
  449. m_state->get_exception()->set_token(matchedSymbol);
  450. // Print out the error after we insert so that ANTLRWorks sees the
  451. // token in the exception.
  452. //
  453. this->reportError();
  454. m_state->set_error(false); // Exception is not outstanding any more
  455. return matchedSymbol;
  456. }
  457. // TODO - Single token deletion like in recoverFromMismatchedToken()
  458. //
  459. m_state->set_error(true);
  460. m_state->set_failed(true);
  461. return NULL;
  462. }
  463. template< class ImplTraits, class StreamType >
  464. bool BaseRecognizer<ImplTraits, StreamType>::recoverFromMismatchedElement(BitsetListType* followBits)
  465. {
  466. SuperType* super = static_cast<SuperType*>(this);
  467. IntStreamType* is = super->get_parser_istream();
  468. BitsetType* follow = followBits->load();
  469. BitsetType* viableToksFollowingRule;
  470. if (follow == NULL)
  471. {
  472. /* The follow set is NULL, which means we don't know what can come
  473. * next, so we "hit and hope" by just signifying that we cannot
  474. * recover, which will just cause the next token to be consumed,
  475. * which might dig us out.
  476. */
  477. return false;
  478. }
  479. /* We have a bitmap for the follow set, hence we can compute
  480. * what can follow this grammar element reference.
  481. */
  482. if (follow->isMember( ImplTraits::CommonTokenType::EOR_TOKEN_TYPE) == true)
  483. {
  484. /* First we need to know which of the available tokens are viable
  485. * to follow this reference.
  486. */
  487. viableToksFollowingRule = this->computeCSRuleFollow();
  488. /* Remove the EOR token, which we do not wish to compute with
  489. */
  490. follow->remove( ImplTraits::CommonTokenType::EOR_TOKEN_TYPE);
  491. delete viableToksFollowingRule;
  492. /* We now have the computed set of what can follow the current token
  493. */
  494. }
  495. /* We can now see if the current token works with the set of tokens
  496. * that could follow the current grammar reference. If it looks like it
  497. * is consistent, then we can "insert" that token by not throwing
  498. * an exception and assuming that we saw it.
  499. */
  500. if ( follow->isMember(is->LA(1)) == true)
  501. {
  502. /* report the error, but don't cause any rules to abort and stuff
  503. */
  504. this->reportError();
  505. if (follow != NULL)
  506. {
  507. delete follow;
  508. }
  509. m_state->set_error(false);
  510. m_state->set_failed(false);
  511. return true; /* Success in recovery */
  512. }
  513. if (follow != NULL)
  514. {
  515. delete follow;
  516. }
  517. /* We could not find anything viable to do, so this is going to
  518. * cause an exception.
  519. */
  520. return false;
  521. }
  522. template< class ImplTraits, class StreamType >
  523. void BaseRecognizer<ImplTraits, StreamType>::consumeUntil(ANTLR_UINT32 tokenType)
  524. {
  525. SuperType* super = static_cast<SuperType*>(this);
  526. IntStreamType* is = super->get_parser_istream();
  527. // What do have at the moment?
  528. //
  529. ANTLR_UINT32 ttype = is->LA(1);
  530. // Start eating tokens until we get to the one we want.
  531. //
  532. while (ttype != ImplTraits::CommonTokenType::TOKEN_EOF && ttype != tokenType)
  533. {
  534. is->consume();
  535. ttype = is->LA(1);
  536. }
  537. }
  538. template< class ImplTraits, class StreamType >
  539. void BaseRecognizer<ImplTraits, StreamType>::consumeUntilSet(BitsetType* set)
  540. {
  541. ANTLR_UINT32 ttype;
  542. SuperType* super = static_cast<SuperType*>(this);
  543. IntStreamType* is = super->get_parser_istream();
  544. // What do have at the moment?
  545. //
  546. ttype = is->LA(1);
  547. // Start eating tokens until we get to one we want.
  548. //
  549. while (ttype != ImplTraits::CommonTokenType::TOKEN_EOF && set->isMember(ttype) == false)
  550. {
  551. is->consume();
  552. ttype = is->LA(1);
  553. }
  554. }
  555. template< class ImplTraits, class StreamType >
  556. ANTLR_MARKER BaseRecognizer<ImplTraits, StreamType>::getRuleMemoization( ANTLR_INTKEY ruleIndex, ANTLR_MARKER ruleParseStart)
  557. {
  558. /* The rule memos are an ANTLR3_LIST of ANTLR3_LIST.
  559. */
  560. typedef IntTrie<ImplTraits, ANTLR_MARKER> RuleListType;
  561. typedef TrieEntry<ImplTraits, std::shared_ptr<RuleListType>> EntryType;
  562. typedef TrieEntry<ImplTraits, ANTLR_MARKER> SubEntryType;
  563. ANTLR_MARKER stopIndex;
  564. EntryType* entry;
  565. /* See if we have a list in the ruleMemos for this rule, and if not, then create one
  566. * as we will need it eventually if we are being asked for the memo here.
  567. */
  568. entry = m_state->get_ruleMemo()->get(ruleIndex);
  569. if (entry == NULL)
  570. {
  571. /* Did not find it, so create a new one for it, with a bit depth based on the
  572. * size of the input stream. We need the bit depth to incorporate the number if
  573. * bits required to represent the largest possible stop index in the input, which is the
  574. * last character. An int stream is free to return the largest 64 bit offset if it has
  575. * no idea of the size, but you should remember that this will cause the leftmost
  576. * bit match algorithm to run to 63 bits, which will be the whole time spent in the trie ;-)
  577. */
  578. m_state->get_ruleMemo()->add( ruleIndex, std::make_shared<RuleListType>(63) );
  579. /* We cannot have a stopIndex in a trie we have just created of course
  580. */
  581. return MEMO_RULE_UNKNOWN;
  582. }
  583. std::shared_ptr<RuleListType> ruleList = entry->get_data();
  584. /* See if there is a stop index associated with the supplied start index.
  585. */
  586. stopIndex = 0;
  587. SubEntryType* sub_entry = ruleList->get(ruleParseStart);
  588. if (sub_entry != NULL)
  589. {
  590. stopIndex = sub_entry->get_data();
  591. }
  592. if (stopIndex == 0)
  593. {
  594. return MEMO_RULE_UNKNOWN;
  595. }
  596. return stopIndex;
  597. }
  598. template< class ImplTraits, class StreamType >
  599. bool BaseRecognizer<ImplTraits, StreamType>::alreadyParsedRule(ANTLR_MARKER ruleIndex)
  600. {
  601. SuperType* super = static_cast<SuperType*>(this);
  602. IntStreamType* is = super->get_istream();
  603. /* See if we have a memo marker for this.
  604. */
  605. ANTLR_MARKER stopIndex = this->getRuleMemoization( ruleIndex, is->index() );
  606. if (stopIndex == MEMO_RULE_UNKNOWN)
  607. {
  608. return false;
  609. }
  610. if (stopIndex == MEMO_RULE_FAILED)
  611. {
  612. m_state->set_failed(true);
  613. }
  614. else
  615. {
  616. is->seek(stopIndex+1);
  617. }
  618. /* If here then the rule was executed for this input already
  619. */
  620. return true;
  621. }
  622. template< class ImplTraits, class StreamType >
  623. void BaseRecognizer<ImplTraits, StreamType>::memoize(ANTLR_MARKER ruleIndex, ANTLR_MARKER ruleParseStart)
  624. {
  625. /* The rule memos are an ANTLR3_LIST of ANTLR3_LIST.
  626. */
  627. typedef IntTrie<ImplTraits, ANTLR_MARKER> RuleListType;
  628. typedef TrieEntry<ImplTraits, std::shared_ptr<RuleListType>> EntryType;
  629. EntryType* entry;
  630. ANTLR_MARKER stopIndex;
  631. SuperType* super = static_cast<SuperType*>(this);
  632. IntStreamType* is = super->get_istream();
  633. stopIndex = (m_state->get_failed() == true) ? MEMO_RULE_FAILED : is->index() - 1;
  634. entry = m_state->get_ruleMemo()->get(ruleIndex);
  635. if (entry != NULL)
  636. {
  637. std::shared_ptr<RuleListType> ruleList = entry->get_data();
  638. /* If we don't already have this entry, append it. The memoize trie does not
  639. * accept duplicates so it won't add it if already there and we just ignore the
  640. * return code as we don't care if it is there already.
  641. */
  642. ruleList->add(ruleParseStart, stopIndex);
  643. }
  644. }
  645. template< class ImplTraits, class StreamType >
  646. const typename BaseRecognizer<ImplTraits, StreamType>::UnitType*
  647. BaseRecognizer<ImplTraits, StreamType>::getCurrentInputSymbol( IntStreamType* istream )
  648. {
  649. return this->getCurrentInputSymbol( istream, ClassForwarder<SuperType>() );
  650. }
  651. template< class ImplTraits, class StreamType >
  652. const typename BaseRecognizer<ImplTraits, StreamType>::UnitType*
  653. BaseRecognizer<ImplTraits, StreamType>::getCurrentInputSymbol(IntStreamType* /*istream*/, ClassForwarder<LexerType>)
  654. {
  655. return NULL;
  656. }
  657. template< class ImplTraits, class StreamType >
  658. const typename BaseRecognizer<ImplTraits, StreamType>::UnitType*
  659. BaseRecognizer<ImplTraits, StreamType>::getCurrentInputSymbol(IntStreamType* istream, ClassForwarder<ParserType>)
  660. {
  661. typedef typename ImplTraits::TokenStreamType TokenStreamType;
  662. TokenStreamType* token_stream = static_cast<TokenStreamType*>(istream);
  663. return token_stream->LT(1);
  664. }
  665. template< class ImplTraits, class StreamType >
  666. const typename BaseRecognizer<ImplTraits, StreamType>::UnitType*
  667. BaseRecognizer<ImplTraits, StreamType>::getCurrentInputSymbol(IntStreamType* istream, ClassForwarder<TreeParserType>)
  668. {
  669. typedef typename ImplTraits::TreeNodeStreamType TreeNodeStreamType;
  670. TreeNodeStreamType* ctns = static_cast<TreeNodeStreamType*>(istream);
  671. return ctns->LT(1);
  672. }
  673. template< class ImplTraits, class StreamType >
  674. typename BaseRecognizer<ImplTraits, StreamType>::UnitType* BaseRecognizer<ImplTraits, StreamType>::getMissingSymbol( IntStreamType* istream,
  675. ExceptionBaseType* e,
  676. ANTLR_UINT32 expectedTokenType,
  677. BitsetListType* follow)
  678. {
  679. return this->get_super()->getMissingSymbol( istream, e, expectedTokenType, follow );
  680. }
  681. template< class ImplTraits, class StreamType >
  682. template<typename Predicate>
  683. bool BaseRecognizer<ImplTraits, StreamType>::synpred(ClassForwarder<Predicate> pred)
  684. {
  685. ANTLR_MARKER start;
  686. SuperType* super = static_cast<SuperType*>(this);
  687. IntStreamType* is = super->get_istream();
  688. /* Begin backtracking so we can get back to where we started after trying out
  689. * the syntactic predicate.
  690. */
  691. start = is->mark();
  692. m_state->inc_backtracking();
  693. /* Try the syntactical predicate
  694. */
  695. this->get_super()->synpred( pred );
  696. /* Reset
  697. */
  698. is->rewind(start);
  699. m_state->dec_backtracking();
  700. if ( m_state->get_failed() == true)
  701. {
  702. /* Predicate failed
  703. */
  704. m_state->set_failed(false);
  705. return false;
  706. }
  707. else
  708. {
  709. /* Predicate was successful
  710. */
  711. m_state->set_failed(false);
  712. return true;
  713. }
  714. }
  715. template< class ImplTraits, class StreamType >
  716. void BaseRecognizer<ImplTraits, StreamType>::exConstruct()
  717. {
  718. this->get_super()->exConstruct();
  719. }
  720. template< class ImplTraits, class StreamType >
  721. void BaseRecognizer<ImplTraits, StreamType>::reset()
  722. {
  723. this->reset( ClassForwarder<SuperType>() );
  724. }
  725. template< class ImplTraits, class StreamType >
  726. template< typename CompType >
  727. void BaseRecognizer<ImplTraits, StreamType>::reset( ClassForwarder<CompType> )
  728. {
  729. typedef typename RecognizerSharedStateType::RuleMemoType RuleMemoType;
  730. m_state->get_following().clear();
  731. // Reset the state flags
  732. //
  733. m_state->set_errorRecovery(false);
  734. m_state->set_lastErrorIndex(-1);
  735. m_state->set_failed(false);
  736. m_state->set_errorCount(0);
  737. m_state->set_backtracking(0);
  738. if (m_state->get_ruleMemo() != NULL)
  739. {
  740. delete m_state->get_ruleMemo();
  741. m_state->set_ruleMemo( new RuleMemoType(15) ); /* 16 bit depth is enough for 32768 rules! */
  742. }
  743. }
  744. template< class ImplTraits, class StreamType >
  745. void BaseRecognizer<ImplTraits, StreamType>::reset( ClassForwarder<LexerType> )
  746. {
  747. m_state->set_token_present( false );
  748. m_state->set_type( ImplTraits::CommonTokenType::TOKEN_INVALID );
  749. m_state->set_channel( TOKEN_DEFAULT_CHANNEL );
  750. m_state->set_tokenStartCharIndex( -1 );
  751. m_state->set_tokenStartCharPositionInLine(-1);
  752. m_state->set_tokenStartLine( -1 );
  753. m_state->set_text("");
  754. }
  755. template< class ImplTraits, class StreamType >
  756. BaseRecognizer<ImplTraits, StreamType>::~BaseRecognizer()
  757. {
  758. // Did we have a state allocated?
  759. //
  760. if (m_state != NULL)
  761. {
  762. // Free any rule memoization we set up
  763. //
  764. if (m_state->get_ruleMemo() != NULL)
  765. {
  766. delete m_state->get_ruleMemo();
  767. m_state->set_ruleMemo(NULL);
  768. }
  769. // Free any exception space we have left around
  770. //
  771. ExceptionBaseType* thisE = m_state->get_exception();
  772. if (thisE != NULL)
  773. {
  774. delete thisE;
  775. }
  776. // Free the shared state memory
  777. //
  778. delete m_state;
  779. }
  780. // Free the actual recognizer space
  781. //
  782. }
  783. }