antlr3parser.inl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. namespace antlr3 {
  2. template< class ImplTraits >
  3. Parser<ImplTraits>::Parser( ANTLR_UINT32 sizeHint, RecognizerSharedStateType* state )
  4. : RecognizerType( sizeHint, state )
  5. {
  6. m_tstream = NULL;
  7. }
  8. template< class ImplTraits >
  9. Parser<ImplTraits>::Parser( ANTLR_UINT32 sizeHint, TokenStreamType* tstream,
  10. RecognizerSharedStateType* state )
  11. : RecognizerType( sizeHint, state )
  12. {
  13. this->setTokenStream( tstream );
  14. }
  15. template< class ImplTraits >
  16. Parser<ImplTraits>::Parser( ANTLR_UINT32 sizeHint, TokenStreamType* tstream,
  17. DebugEventListenerType* dbg,
  18. RecognizerSharedStateType* state )
  19. : RecognizerType( sizeHint, state )
  20. {
  21. this->setTokenStream( tstream );
  22. this->setDebugListener( dbg );
  23. }
  24. template< class ImplTraits >
  25. ANTLR_INLINE typename Parser<ImplTraits>::TokenStreamType* Parser<ImplTraits>::get_tstream() const
  26. {
  27. return m_tstream;
  28. }
  29. template< class ImplTraits >
  30. ANTLR_INLINE typename Parser<ImplTraits>::IntStreamType* Parser<ImplTraits>::get_istream() const
  31. {
  32. return m_tstream;
  33. }
  34. template< class ImplTraits >
  35. ANTLR_INLINE typename Parser<ImplTraits>::IntStreamType* Parser<ImplTraits>::get_parser_istream() const
  36. {
  37. return m_tstream;
  38. }
  39. template< class ImplTraits >
  40. ANTLR_INLINE typename Parser<ImplTraits>::TokenStreamType* Parser<ImplTraits>::get_input() const
  41. {
  42. return m_tstream;
  43. }
  44. template< class ImplTraits >
  45. void Parser<ImplTraits>::fillExceptionData( ExceptionBaseType* ex )
  46. {
  47. ex->set_token( new CommonTokenType(*(m_tstream->LT(1))) ); /* Current input token (clonned) - held by the exception */
  48. ex->set_line( ex->get_token()->get_line() );
  49. ex->set_charPositionInLine( ex->get_token()->get_charPositionInLine() );
  50. ex->set_index( this->get_istream()->index() );
  51. if( ex->get_token()->get_type() == CommonTokenType::TOKEN_EOF)
  52. {
  53. ex->set_streamName("");
  54. }
  55. else
  56. {
  57. ex->set_streamName( ex->get_token()->get_input()->get_fileName() );
  58. }
  59. ex->set_message("Unexpected token");
  60. }
  61. template< class ImplTraits >
  62. void Parser<ImplTraits>::displayRecognitionError( ANTLR_UINT8** tokenNames, ExceptionBaseType* ex )
  63. {
  64. typename ImplTraits::StringStreamType errtext;
  65. // See if there is a 'filename' we can use
  66. //
  67. if( ex->get_streamName().empty() )
  68. {
  69. if(ex->get_token()->get_type() == CommonTokenType::TOKEN_EOF)
  70. {
  71. errtext << "-end of input-(";
  72. }
  73. else
  74. {
  75. errtext << "-unknown source-(";
  76. }
  77. }
  78. else
  79. {
  80. errtext << ex->get_streamName() << "(";
  81. }
  82. // Next comes the line number
  83. //
  84. errtext << this->get_rec()->get_state()->get_exception()->get_line() << ") ";
  85. errtext << " : error " << this->get_rec()->get_state()->get_exception()->getType()
  86. << " : "
  87. << this->get_rec()->get_state()->get_exception()->get_message();
  88. // Prepare the knowledge we know we have
  89. //
  90. const CommonTokenType* theToken = this->get_rec()->get_state()->get_exception()->get_token();
  91. StringType ttext = theToken->toString();
  92. errtext << ", at offset , "
  93. << this->get_rec()->get_state()->get_exception()->get_charPositionInLine();
  94. if (theToken != NULL)
  95. {
  96. if (theToken->get_type() == CommonTokenType::TOKEN_EOF)
  97. {
  98. errtext << ", at <EOF>";
  99. }
  100. else
  101. {
  102. // Guard against null text in a token
  103. //
  104. errtext << "\n near " << ( ttext.empty()
  105. ? "<no text for the token>" : ttext ) << "\n";
  106. }
  107. }
  108. ex->displayRecognitionError( tokenNames, errtext );
  109. ImplTraits::displayRecognitionError( errtext.str() );
  110. }
  111. template< class ImplTraits >
  112. Parser<ImplTraits>::~Parser()
  113. {
  114. if (this->get_rec() != NULL)
  115. {
  116. // This may have ben a delegate or delegator parser, in which case the
  117. // state may already have been freed (and set to NULL therefore)
  118. // so we ignore the state if we don't have it.
  119. //
  120. RecognizerSharedStateType* state = this->get_rec()->get_state();
  121. if (state != NULL)
  122. {
  123. state->get_following().clear();
  124. }
  125. }
  126. }
  127. template< class ImplTraits >
  128. void Parser<ImplTraits>::setDebugListener(DebugEventListenerType* dbg)
  129. {
  130. // Set the debug listener. There are no methods to override
  131. // because currently the only ones that notify the debugger
  132. // are error reporting and recovery. Hence we can afford to
  133. // check and see if the debugger interface is null or not
  134. // there. If there is ever an occasion for a performance
  135. // sensitive function to use the debugger interface, then
  136. // a replacement function for debug mode should be supplied
  137. // and installed here.
  138. //
  139. this->get_rec()->set_debugger(dbg);
  140. // If there was a tokenstream installed already
  141. // then we need to tell it about the debug interface
  142. //
  143. if (this->get_tstream() != NULL)
  144. {
  145. this->get_tstream()->setDebugListener(dbg);
  146. }
  147. }
  148. template< class ImplTraits >
  149. ANTLR_INLINE void Parser<ImplTraits>::setTokenStream(TokenStreamType* tstream)
  150. {
  151. m_tstream = tstream;
  152. this->get_rec()->reset();
  153. }
  154. template< class ImplTraits >
  155. ANTLR_INLINE typename Parser<ImplTraits>::TokenStreamType* Parser<ImplTraits>::getTokenStream()
  156. {
  157. return m_tstream;
  158. }
  159. template< class ImplTraits >
  160. ANTLR_INLINE typename Parser<ImplTraits>::RecognizerType* Parser<ImplTraits>::get_rec()
  161. {
  162. return this;
  163. }
  164. template< class ImplTraits >
  165. ANTLR_INLINE void Parser<ImplTraits>::exConstruct()
  166. {
  167. new ANTLR_Exception<ImplTraits, MISMATCHED_TOKEN_EXCEPTION, StreamType>( this->get_rec(), "" );
  168. }
  169. template< class ImplTraits >
  170. typename Parser<ImplTraits>::TokenType* Parser<ImplTraits>::getMissingSymbol( IntStreamType* istream,
  171. ExceptionBaseType*,
  172. ANTLR_UINT32 expectedTokenType,
  173. BitsetListType* )
  174. {
  175. // Dereference the standard pointers
  176. //
  177. TokenStreamType *cts = static_cast<TokenStreamType*>(istream);
  178. // Work out what to use as the current symbol to make a line and offset etc
  179. // If we are at EOF, we use the token before EOF
  180. //
  181. const CommonTokenType* current = cts->LT(1);
  182. if (current->get_type() == CommonTokenType::TOKEN_EOF)
  183. {
  184. current = cts->LT(-1);
  185. }
  186. CommonTokenType* token = new CommonTokenType;
  187. // Set some of the token properties based on the current token
  188. //
  189. token->set_line(current->get_line());
  190. token->set_charPositionInLine( current->get_charPositionInLine());
  191. token->set_channel( TOKEN_DEFAULT_CHANNEL );
  192. token->set_type(expectedTokenType);
  193. token->set_lineStart( current->get_lineStart() );
  194. // Create the token text that shows it has been inserted
  195. //
  196. if ( expectedTokenType == CommonTokenType::TOKEN_EOF )
  197. {
  198. token->setText( "<missing EOF>" );
  199. } else {
  200. typename ImplTraits::StringStreamType text;
  201. text << "<missing " << this->get_rec()->get_state()->get_tokenName(expectedTokenType) << ">";
  202. token->setText( text.str().c_str() );
  203. }
  204. // Finally return the pointer to our new token
  205. //
  206. return token;
  207. }
  208. template< class ImplTraits >
  209. void Parser<ImplTraits>::mismatch(ANTLR_UINT32 ttype, BitsetListType* follow)
  210. {
  211. // Install a mismatched token exception in the exception stack
  212. //
  213. new ANTLR_Exception<ImplTraits, MISMATCHED_TOKEN_EXCEPTION, StreamType>(this, "");
  214. //With the statement below, only the parsers are allowed to compile fine
  215. IntStreamType* is = this->get_istream();
  216. if (this->mismatchIsUnwantedToken(is, ttype))
  217. {
  218. // Now update it to indicate this is an unwanted token exception
  219. //
  220. new ANTLR_Exception<ImplTraits, UNWANTED_TOKEN_EXCEPTION, StreamType>(this, "");
  221. return;
  222. }
  223. if ( this->mismatchIsMissingToken(is, follow))
  224. {
  225. // Now update it to indicate this is an unwanted token exception
  226. //
  227. new ANTLR_Exception<ImplTraits, MISSING_TOKEN_EXCEPTION, StreamType>(this, "");
  228. return;
  229. }
  230. // Just a mismatched token is all we can dtermine
  231. //
  232. new ANTLR_Exception<ImplTraits, MISMATCHED_TOKEN_EXCEPTION, StreamType>(this, "");
  233. return;
  234. }
  235. template< class ImplTraits>
  236. ANTLR_INLINE const typename Parser<ImplTraits>::RecognizerType* Parser<ImplTraits>::get_recognizer() const
  237. {
  238. return this;
  239. }
  240. template< class ImplTraits>
  241. ANTLR_INLINE typename Parser<ImplTraits>::RecognizerSharedStateType* Parser<ImplTraits>::get_psrstate() const
  242. {
  243. return this->get_recognizer()->get_state();
  244. }
  245. template< class ImplTraits>
  246. ANTLR_INLINE void Parser<ImplTraits>::set_psrstate(RecognizerSharedStateType* state)
  247. {
  248. this->get_rec()->set_state( state );
  249. }
  250. template< class ImplTraits>
  251. ANTLR_INLINE bool Parser<ImplTraits>::haveParsedRule(ANTLR_MARKER ruleIndex)
  252. {
  253. return this->get_rec()->alreadyParsedRule(ruleIndex);
  254. }
  255. template< class ImplTraits>
  256. ANTLR_INLINE void Parser<ImplTraits>::memoize(ANTLR_MARKER ruleIndex, ANTLR_MARKER ruleParseStart)
  257. {
  258. return this->get_rec()->memoize( ruleIndex, ruleParseStart );
  259. }
  260. template< class ImplTraits>
  261. ANTLR_INLINE ANTLR_MARKER Parser<ImplTraits>::index() const
  262. {
  263. return this->get_istream()->index();
  264. }
  265. template< class ImplTraits>
  266. ANTLR_INLINE bool Parser<ImplTraits>::hasException() const
  267. {
  268. return this->get_psrstate()->get_error();
  269. }
  270. template< class ImplTraits>
  271. ANTLR_INLINE typename Parser<ImplTraits>::ExceptionBaseType* Parser<ImplTraits>::get_exception() const
  272. {
  273. return this->get_psrstate()->get_exception();
  274. }
  275. template< class ImplTraits>
  276. ANTLR_INLINE const typename Parser<ImplTraits>::CommonTokenType* Parser<ImplTraits>::matchToken( ANTLR_UINT32 ttype, BitsetListType* follow )
  277. {
  278. return this->get_rec()->match( ttype, follow );
  279. }
  280. template< class ImplTraits>
  281. ANTLR_INLINE void Parser<ImplTraits>::matchAnyToken()
  282. {
  283. return this->get_rec()->matchAny();
  284. }
  285. template< class ImplTraits>
  286. ANTLR_INLINE const typename Parser<ImplTraits>::FollowingType& Parser<ImplTraits>::get_follow_stack() const
  287. {
  288. return this->get_psrstate()->get_following();
  289. }
  290. template< class ImplTraits>
  291. ANTLR_INLINE void Parser<ImplTraits>::followPush(const BitsetListType& follow)
  292. {
  293. #ifndef SKIP_FOLLOW_SETS
  294. this->get_rec()->get_state()->get_following().push(follow);
  295. #endif
  296. }
  297. template< class ImplTraits>
  298. ANTLR_INLINE void Parser<ImplTraits>::followPop()
  299. {
  300. #ifndef SKIP_FOLLOW_SETS
  301. this->get_rec()->get_state()->get_following().pop();
  302. #endif
  303. }
  304. template< class ImplTraits>
  305. ANTLR_INLINE void Parser<ImplTraits>::precover()
  306. {
  307. return this->get_rec()->recover();
  308. }
  309. template< class ImplTraits>
  310. ANTLR_INLINE void Parser<ImplTraits>::preporterror()
  311. {
  312. return this->get_rec()->reportError();
  313. }
  314. template< class ImplTraits>
  315. ANTLR_INLINE ANTLR_UINT32 Parser<ImplTraits>::LA(ANTLR_INT32 i)
  316. {
  317. return this->get_istream()->LA(i);
  318. }
  319. template< class ImplTraits>
  320. ANTLR_INLINE const typename Parser<ImplTraits>::CommonTokenType* Parser<ImplTraits>::LT(ANTLR_INT32 k)
  321. {
  322. return this->get_input()->LT(k);
  323. }
  324. template< class ImplTraits>
  325. ANTLR_INLINE void Parser<ImplTraits>::constructEx()
  326. {
  327. this->get_rec()->constructEx();
  328. }
  329. template< class ImplTraits>
  330. ANTLR_INLINE void Parser<ImplTraits>::consume()
  331. {
  332. this->get_istream()->consume();
  333. }
  334. template< class ImplTraits>
  335. ANTLR_INLINE ANTLR_MARKER Parser<ImplTraits>::mark()
  336. {
  337. return this->get_istream()->mark();
  338. }
  339. template< class ImplTraits>
  340. ANTLR_INLINE void Parser<ImplTraits>::rewind(ANTLR_MARKER marker)
  341. {
  342. this->get_istream()->rewind(marker);
  343. }
  344. template< class ImplTraits>
  345. ANTLR_INLINE void Parser<ImplTraits>::rewindLast()
  346. {
  347. this->get_istream()->rewindLast();
  348. }
  349. template< class ImplTraits>
  350. ANTLR_INLINE void Parser<ImplTraits>::seek(ANTLR_MARKER index)
  351. {
  352. this->get_istream()->seek(index);
  353. }
  354. template< class ImplTraits>
  355. ANTLR_INLINE bool Parser<ImplTraits>::get_perror_recovery() const
  356. {
  357. return this->get_psrstate()->get_errorRecovery();
  358. }
  359. template< class ImplTraits>
  360. ANTLR_INLINE void Parser<ImplTraits>::set_perror_recovery( bool val )
  361. {
  362. this->get_psrstate()->set_errorRecovery(val);
  363. }
  364. template< class ImplTraits>
  365. ANTLR_INLINE bool Parser<ImplTraits>::hasFailed() const
  366. {
  367. return this->get_psrstate()->get_failed();
  368. }
  369. template< class ImplTraits>
  370. ANTLR_INLINE bool Parser<ImplTraits>::get_failedflag() const
  371. {
  372. return this->get_psrstate()->get_failed();
  373. }
  374. template< class ImplTraits>
  375. ANTLR_INLINE void Parser<ImplTraits>::set_failedflag( bool failed )
  376. {
  377. this->get_psrstate()->set_failed(failed);
  378. }
  379. template< class ImplTraits>
  380. ANTLR_INLINE ANTLR_INT32 Parser<ImplTraits>::get_backtracking() const
  381. {
  382. return this->get_psrstate()->get_backtracking();
  383. }
  384. template< class ImplTraits>
  385. ANTLR_INLINE void Parser<ImplTraits>::inc_backtracking()
  386. {
  387. this->get_psrstate()->inc_backtracking();
  388. }
  389. template< class ImplTraits>
  390. ANTLR_INLINE void Parser<ImplTraits>::dec_backtracking()
  391. {
  392. this->get_psrstate()->dec_backtracking();
  393. }
  394. template< class ImplTraits>
  395. ANTLR_INLINE typename Parser<ImplTraits>::CommonTokenType* Parser<ImplTraits>::recoverFromMismatchedSet(BitsetListType* follow)
  396. {
  397. return this->get_rec()->recoverFromMismatchedSet(follow);
  398. }
  399. template< class ImplTraits>
  400. ANTLR_INLINE bool Parser<ImplTraits>::recoverFromMismatchedElement(BitsetListType* follow)
  401. {
  402. return this->get_rec()->recoverFromMismatchedElement(follow);
  403. }
  404. template< class ImplTraits>
  405. ANTLR_INLINE typename Parser<ImplTraits>::RuleMemoType* Parser<ImplTraits>::getRuleMemo() const
  406. {
  407. return this->get_psrstate()->get_ruleMemo();
  408. }
  409. template< class ImplTraits>
  410. ANTLR_INLINE void Parser<ImplTraits>::setRuleMemo(RuleMemoType* rulememo)
  411. {
  412. this->get_psrstate()->set_ruleMemo(rulememo);
  413. }
  414. template< class ImplTraits>
  415. ANTLR_INLINE typename Parser<ImplTraits>::DebuggerType* Parser<ImplTraits>::get_debugger() const
  416. {
  417. return this->get_rec()->get_debugger();
  418. }
  419. template< class ImplTraits>
  420. ANTLR_INLINE typename Parser<ImplTraits>::TokenStreamType* Parser<ImplTraits>::get_strstream() const
  421. {
  422. return this->get_tstream();
  423. }
  424. template< class ImplTraits>
  425. ANTLR_INLINE RuleReturnValue<ImplTraits>::RuleReturnValue(BaseParserType* /*psr*/)
  426. {
  427. start = NULL;
  428. stop = NULL;
  429. }
  430. template< class ImplTraits>
  431. ANTLR_INLINE RuleReturnValue<ImplTraits>::RuleReturnValue( const RuleReturnValue& val )
  432. {
  433. start = val.start;
  434. stop = val.stop;
  435. }
  436. template< class ImplTraits>
  437. ANTLR_INLINE RuleReturnValue<ImplTraits>& RuleReturnValue<ImplTraits>::operator=( const RuleReturnValue& val )
  438. {
  439. start = val.start;
  440. stop = val.stop;
  441. return *this;
  442. }
  443. template< class ImplTraits>
  444. ANTLR_INLINE RuleReturnValue<ImplTraits>::~RuleReturnValue()
  445. {
  446. }
  447. template< class ImplTraits>
  448. ANTLR_INLINE void RuleReturnValue<ImplTraits>::call_start_placeholder(BaseParserType *parser)
  449. {
  450. start = parser->LT(1);
  451. stop = start;
  452. }
  453. template< class ImplTraits>
  454. ANTLR_INLINE void RuleReturnValue<ImplTraits>::call_stop_placeholder(BaseParserType *parser)
  455. {
  456. stop = parser->LT(-1);
  457. }
  458. template< class ImplTraits>
  459. ANTLR_INLINE RuleReturnValue_1<ImplTraits>::RuleReturnValue_1()
  460. : parser()
  461. {
  462. }
  463. template< class ImplTraits>
  464. RuleReturnValue_1<ImplTraits>::RuleReturnValue_1( BaseParserType* psr )
  465. : RuleReturnValue_1<ImplTraits>::BaseType(psr)
  466. , parser(psr)
  467. {
  468. BaseType::start = psr->LT(1);
  469. BaseType::stop = BaseType::start;
  470. }
  471. template< class ImplTraits>
  472. RuleReturnValue_1<ImplTraits>::RuleReturnValue_1( const RuleReturnValue_1& val )
  473. : RuleReturnValue_1<ImplTraits>::BaseType(val)
  474. , parser(val.parser)
  475. {
  476. }
  477. template< class ImplTraits>
  478. void RuleReturnValue_1<ImplTraits>::call_start_placeholder(BaseParserType*)
  479. {
  480. }
  481. template< class ImplTraits>
  482. RuleReturnValue_1<ImplTraits>::~RuleReturnValue_1()
  483. {
  484. if( parser && parser->get_backtracking() == 0 )
  485. {
  486. if( BaseType::stop == NULL )
  487. BaseType::stop = BaseType::parser->LT(-1);
  488. if( BaseType::stop != NULL )
  489. {
  490. ANTLR_MARKER start_token_idx = BaseType::start->get_index() + 1;
  491. ANTLR_MARKER stop_token_idx = BaseType::stop->get_index() - 1;
  492. if( start_token_idx > stop_token_idx )
  493. return;
  494. parser->getTokenStream()->discardTokens( start_token_idx, stop_token_idx);
  495. }
  496. }
  497. }
  498. }