antlr3exception.inl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. namespace antlr3 {
  2. template<class ImplTraits, class StreamType>
  3. ANTLR_ExceptionBase<ImplTraits, StreamType>::ANTLR_ExceptionBase(const StringType& message)
  4. :m_message(message)
  5. ,m_input(NULL)
  6. {
  7. m_index = 0;
  8. m_token = NULL;
  9. m_expecting = 0;
  10. m_expectingSet = NULL;
  11. m_node = NULL;
  12. m_c = 0;
  13. m_line = 0;
  14. m_charPositionInLine = 0;
  15. m_decisionNum = 0;
  16. m_state = 0;
  17. m_nextException = NULL;
  18. }
  19. template<class ImplTraits, class StreamType>
  20. ANTLR_INLINE typename ANTLR_ExceptionBase<ImplTraits, StreamType>::StringType& ANTLR_ExceptionBase<ImplTraits, StreamType>::get_message()
  21. {
  22. return m_message;
  23. }
  24. template<class ImplTraits, class StreamType>
  25. ANTLR_INLINE typename ANTLR_ExceptionBase<ImplTraits, StreamType>::StringType& ANTLR_ExceptionBase<ImplTraits, StreamType>::get_streamName()
  26. {
  27. return m_streamName;
  28. }
  29. template<class ImplTraits, class StreamType>
  30. ANTLR_INLINE ANTLR_MARKER ANTLR_ExceptionBase<ImplTraits, StreamType>::get_index() const
  31. {
  32. return m_index;
  33. }
  34. template<class ImplTraits, class StreamType>
  35. ANTLR_INLINE const typename ANTLR_ExceptionBase<ImplTraits, StreamType>::TokenType* ANTLR_ExceptionBase<ImplTraits, StreamType>::get_token() const
  36. {
  37. return m_token;
  38. }
  39. template<class ImplTraits, class StreamType>
  40. ANTLR_INLINE typename ANTLR_ExceptionBase<ImplTraits, StreamType>::ExceptionBaseType* ANTLR_ExceptionBase<ImplTraits, StreamType>::get_nextException() const
  41. {
  42. return m_nextException;
  43. }
  44. template<class ImplTraits, class StreamType>
  45. ANTLR_INLINE ANTLR_UINT32 ANTLR_ExceptionBase<ImplTraits, StreamType>::get_expecting() const
  46. {
  47. return m_expecting;
  48. }
  49. template<class ImplTraits, class StreamType>
  50. ANTLR_INLINE typename ANTLR_ExceptionBase<ImplTraits, StreamType>::BitsetListType* ANTLR_ExceptionBase<ImplTraits, StreamType>::get_expectingSet() const
  51. {
  52. return m_expectingSet;
  53. }
  54. template<class ImplTraits, class StreamType>
  55. ANTLR_INLINE typename ANTLR_ExceptionBase<ImplTraits, StreamType>::TokenType* ANTLR_ExceptionBase<ImplTraits, StreamType>::get_node() const
  56. {
  57. return m_node;
  58. }
  59. template<class ImplTraits, class StreamType>
  60. ANTLR_INLINE ANTLR_UCHAR ANTLR_ExceptionBase<ImplTraits, StreamType>::get_c() const
  61. {
  62. return m_c;
  63. }
  64. template<class ImplTraits, class StreamType>
  65. ANTLR_INLINE ANTLR_UINT32 ANTLR_ExceptionBase<ImplTraits, StreamType>::get_line() const
  66. {
  67. return m_line;
  68. }
  69. template<class ImplTraits, class StreamType>
  70. ANTLR_INLINE ANTLR_INT32 ANTLR_ExceptionBase<ImplTraits, StreamType>::get_charPositionInLine() const
  71. {
  72. return m_charPositionInLine;
  73. }
  74. template<class ImplTraits, class StreamType>
  75. ANTLR_INLINE ANTLR_UINT32 ANTLR_ExceptionBase<ImplTraits, StreamType>::get_decisionNum() const
  76. {
  77. return m_decisionNum;
  78. }
  79. template<class ImplTraits, class StreamType>
  80. ANTLR_INLINE ANTLR_UINT32 ANTLR_ExceptionBase<ImplTraits, StreamType>::get_state() const
  81. {
  82. return m_state;
  83. }
  84. template<class ImplTraits, class StreamType>
  85. ANTLR_INLINE typename ANTLR_ExceptionBase<ImplTraits, StreamType>::StringType& ANTLR_ExceptionBase<ImplTraits, StreamType>::get_ruleName()
  86. {
  87. return m_ruleName;
  88. }
  89. template<class ImplTraits, class StreamType>
  90. ANTLR_INLINE typename ANTLR_ExceptionBase<ImplTraits, StreamType>::IntStreamType* ANTLR_ExceptionBase<ImplTraits, StreamType>::get_input() const
  91. {
  92. return m_input;
  93. }
  94. template<class ImplTraits, class StreamType>
  95. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_message( const StringType& message )
  96. {
  97. m_message = message;
  98. }
  99. template<class ImplTraits, class StreamType>
  100. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_streamName( const StringType& streamName )
  101. {
  102. m_streamName = streamName;
  103. }
  104. template<class ImplTraits, class StreamType>
  105. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_index( ANTLR_MARKER index )
  106. {
  107. m_index = index;
  108. }
  109. template<class ImplTraits, class StreamType>
  110. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_token( const TokenType* token )
  111. {
  112. if (m_token)
  113. delete m_token;
  114. m_token = token;
  115. }
  116. template<class ImplTraits, class StreamType>
  117. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_nextException( ExceptionBaseType* nextException )
  118. {
  119. m_nextException = nextException;
  120. }
  121. template<class ImplTraits, class StreamType>
  122. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_expecting( ANTLR_UINT32 expecting )
  123. {
  124. m_expecting = expecting;
  125. }
  126. template<class ImplTraits, class StreamType>
  127. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_expectingSet( BitsetListType* expectingSet )
  128. {
  129. m_expectingSet = expectingSet;
  130. }
  131. template<class ImplTraits, class StreamType>
  132. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_node( TokenType* node )
  133. {
  134. m_node = node;
  135. }
  136. template<class ImplTraits, class StreamType>
  137. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_c( ANTLR_UCHAR c )
  138. {
  139. m_c = c;
  140. }
  141. template<class ImplTraits, class StreamType>
  142. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_line( ANTLR_UINT32 line )
  143. {
  144. m_line = line;
  145. }
  146. template<class ImplTraits, class StreamType>
  147. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_charPositionInLine( ANTLR_INT32 charPositionInLine )
  148. {
  149. m_charPositionInLine = charPositionInLine;
  150. }
  151. template<class ImplTraits, class StreamType>
  152. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_decisionNum( ANTLR_UINT32 decisionNum )
  153. {
  154. m_decisionNum = decisionNum;
  155. }
  156. template<class ImplTraits, class StreamType>
  157. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_state( ANTLR_UINT32 state )
  158. {
  159. m_state = state;
  160. }
  161. template<class ImplTraits, class StreamType>
  162. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_ruleName( const StringType& ruleName )
  163. {
  164. m_ruleName = ruleName;
  165. }
  166. template<class ImplTraits, class StreamType>
  167. ANTLR_INLINE void ANTLR_ExceptionBase<ImplTraits, StreamType>::set_input( IntStreamType* input )
  168. {
  169. m_input = input;
  170. }
  171. template<class ImplTraits, ExceptionType Ex, class StreamType>
  172. template<typename BaseRecognizerType>
  173. ANTLR_Exception<ImplTraits, Ex, StreamType>::ANTLR_Exception(BaseRecognizerType* recognizer, const StringType& message)
  174. :BaseType( message )
  175. {
  176. recognizer->get_super()->fillExceptionData( this );
  177. BaseType::m_input = recognizer->get_super()->get_istream();
  178. BaseType::m_nextException = recognizer->get_state()->get_exception(); /* So we don't leak the memory */
  179. recognizer->get_state()->set_exception(this);
  180. recognizer->get_state()->set_error( true ); /* Exception is outstanding */
  181. }
  182. template<class ImplTraits, ExceptionType Ex, class StreamType>
  183. ANTLR_UINT32 ANTLR_Exception<ImplTraits, Ex, StreamType>::getType() const
  184. {
  185. return static_cast<ANTLR_UINT32>(Ex);
  186. }
  187. template<class ImplTraits, ExceptionType Ex, class StreamType>
  188. void ANTLR_Exception<ImplTraits, Ex, StreamType>::print() const
  189. {
  190. /* Ensure valid pointer
  191. */
  192. /* Number if no message, else the message
  193. */
  194. if ( BaseType::m_message.empty() )
  195. {
  196. fprintf(stderr, "ANTLR3_EXCEPTION number %d (%08X).\n", Ex, Ex);
  197. }
  198. else
  199. {
  200. fprintf(stderr, "ANTLR3_EXCEPTION: %s\n", BaseType::m_message.c_str() );
  201. }
  202. }
  203. template<class ImplTraits, ExceptionType Ex, class StreamType>
  204. typename ANTLR_Exception<ImplTraits, Ex, StreamType>::StringType
  205. ANTLR_Exception<ImplTraits, Ex, StreamType>::getName() const
  206. {
  207. const char* exArray[] = {
  208. "org.antlr.runtime.RecognitionException"
  209. , "org.antlr.runtime.MismatchedTokenException"
  210. , "org.antlr.runtime.NoViableAltException"
  211. , "org.antlr.runtime.MismatchedSetException"
  212. , "org.antlr.runtime.EarlyExitException"
  213. , "org.antlr.runtime.FailedPredicateException"
  214. , "org.antlr.runtime.MismatchedTreeNodeException"
  215. , "org.antlr.runtime.tree.RewriteEarlyExitException"
  216. , "org.antlr.runtime.UnwantedTokenException"
  217. , "org.antlr.runtime.MissingTokenException"
  218. };
  219. return StringType(exArray[Ex]);
  220. }
  221. template<class ImplTraits, ExceptionType Ex, class StreamType>
  222. void ANTLR_Exception<ImplTraits, Ex, StreamType>::displayRecognitionError( ANTLR_UINT8** tokenNames,
  223. StringStreamType& str_stream ) const
  224. {
  225. switch( Ex )
  226. {
  227. case RECOGNITION_EXCEPTION:
  228. // Indicates that the recognizer received a token
  229. // in the input that was not predicted. This is the basic exception type
  230. // from which all others are derived. So we assume it was a syntax error.
  231. // You may get this if there are not more tokens and more are needed
  232. // to complete a parse for instance.
  233. //
  234. str_stream << " : syntax error...\n";
  235. break;
  236. case UNWANTED_TOKEN_EXCEPTION:
  237. // Indicates that the recognizer was fed a token which seesm to be
  238. // spurious input. We can detect this when the token that follows
  239. // this unwanted token would normally be part of the syntactically
  240. // correct stream. Then we can see that the token we are looking at
  241. // is just something that should not be there and throw this exception.
  242. //
  243. if (tokenNames == NULL)
  244. {
  245. str_stream << " : Extraneous input...";
  246. }
  247. else
  248. {
  249. if ( BaseType::m_expecting == ImplTraits::CommonTokenType::TOKEN_EOF)
  250. {
  251. str_stream << " : Extraneous input - expected <EOF>\n";
  252. }
  253. else
  254. {
  255. str_stream << " : Extraneous input - expected "
  256. << tokenNames[ BaseType::m_expecting] << " ...\n";
  257. }
  258. }
  259. break;
  260. case MISSING_TOKEN_EXCEPTION:
  261. // Indicates that the recognizer detected that the token we just
  262. // hit would be valid syntactically if preceeded by a particular
  263. // token. Perhaps a missing ';' at line end or a missing ',' in an
  264. // expression list, and such like.
  265. //
  266. if (tokenNames == NULL)
  267. {
  268. str_stream << " : Missing token ("
  269. << BaseType::m_expecting << ")...\n";
  270. }
  271. else
  272. {
  273. if ( BaseType::m_expecting == ImplTraits::CommonTokenType::TOKEN_EOF )
  274. {
  275. str_stream <<" : Missing <EOF>\n";
  276. }
  277. else
  278. {
  279. str_stream << " : Missing " << tokenNames[BaseType::m_expecting] <<" \n";
  280. }
  281. }
  282. break;
  283. case NO_VIABLE_ALT_EXCEPTION:
  284. // We could not pick any alt decision from the input given
  285. // so god knows what happened - however when you examine your grammar,
  286. // you should. It means that at the point where the current token occurred
  287. // that the DFA indicates nowhere to go from here.
  288. //
  289. str_stream << " : cannot match to any predicted input...\n";
  290. break;
  291. case MISMATCHED_SET_EXCEPTION:
  292. {
  293. ANTLR_UINT32 count;
  294. ANTLR_UINT32 bit;
  295. ANTLR_UINT32 size;
  296. ANTLR_UINT32 numbits;
  297. // This means we were able to deal with one of a set of
  298. // possible tokens at this point, but we did not see any
  299. // member of that set.
  300. //
  301. str_stream << " : unexpected input :";
  302. // What tokens could we have accepted at this point in the
  303. // parse?
  304. //
  305. count = 0;
  306. size = 0;
  307. if (BaseType::m_expectingSet != NULL) {
  308. std::unique_ptr<BitsetType> errBits(BaseType::m_expectingSet->bitsetLoad());
  309. numbits = errBits->numBits();
  310. size = errBits->size();
  311. }
  312. if (size > 0)
  313. {
  314. // However many tokens we could have dealt with here, it is usually
  315. // not useful to print ALL of the set here. I arbitrarily chose 8
  316. // here, but you should do whatever makes sense for you of course.
  317. // No token number 0, so look for bit 1 and on.
  318. //
  319. str_stream << " expected one of : ";
  320. for (bit = 1; bit < numbits && count < 8 && count < size; bit++)
  321. {
  322. // TODO: This doesn;t look right - should be asking if the bit is set!!
  323. //
  324. if (tokenNames[bit])
  325. {
  326. str_stream << ( count > 0 ? ", " : "" )
  327. << tokenNames[bit];
  328. count++;
  329. }
  330. }
  331. str_stream << "\n";
  332. }
  333. else
  334. {
  335. str_stream << " nothing is expected here\n";
  336. }
  337. }
  338. break;
  339. case EARLY_EXIT_EXCEPTION:
  340. str_stream << " : missing elements...\n";
  341. break;
  342. default:
  343. str_stream << " : syntax not recognized...\n";
  344. break;
  345. }
  346. }
  347. template<class ImplTraits, class StreamType>
  348. ANTLR_ExceptionBase<ImplTraits,StreamType>::~ANTLR_ExceptionBase()
  349. {
  350. ANTLR_ExceptionBase<ImplTraits,StreamType>* next;
  351. ANTLR_ExceptionBase<ImplTraits,StreamType>* ex = m_nextException;
  352. /* Ensure valid pointer
  353. */
  354. while (ex != NULL)
  355. {
  356. /* Pick up anythign following now, before we free the
  357. * current memory block.
  358. */
  359. next = ex->m_nextException;
  360. ex->m_nextException = NULL;
  361. /* Free the actual structure itself
  362. */
  363. delete ex;
  364. ex = next;
  365. }
  366. if ( m_token)
  367. delete m_token;
  368. }
  369. }