antlr3exception.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /** \file
  2. * Contains the definition of a basic ANTLR3 exception structure created
  3. * by a recognizer when errors are found/predicted.
  4. * Two things to be noted for C++ Target:
  5. a) This is not the C++ Exception. Consider this just as yet another class. This
  6. has to be like this because there is a inbuilt recovery and hence there is a try..catch
  7. block for every new token. This is not how C++ Exceptions work.Still there is exception support, as we are handling things like OutofMemory by
  8. throwing exceptions
  9. b) There is no use in implementing templates here, as all the exceptions are grouped in
  10. one container and hence needs virtual functions. But this would occur only when there is
  11. a exception/ while deleting base recognizer. So shouldn't incur the overhead in normal operation
  12. */
  13. #ifndef _ANTLR3_EXCEPTION_HPP
  14. #define _ANTLR3_EXCEPTION_HPP
  15. // [The "BSD licence"]
  16. // Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB
  17. //
  18. // All rights reserved.
  19. //
  20. // Redistribution and use in source and binary forms, with or without
  21. // modification, are permitted provided that the following conditions
  22. // are met:
  23. // 1. Redistributions of source code must retain the above copyright
  24. // notice, this list of conditions and the following disclaimer.
  25. // 2. Redistributions in binary form must reproduce the above copyright
  26. // notice, this list of conditions and the following disclaimer in the
  27. // documentation and/or other materials provided with the distribution.
  28. // 3. The name of the author may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  32. // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  34. // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  35. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  37. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  38. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  40. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. namespace antlr3 {
  42. /** Base structure for an ANTLR3 exception tracker
  43. */
  44. template<class ImplTraits, class StreamType>
  45. class ANTLR_ExceptionBase
  46. {
  47. public:
  48. typedef typename StreamType::UnitType TokenType;
  49. typedef typename StreamType::IntStreamType IntStreamType;
  50. typedef typename ImplTraits::AllocPolicyType AllocPolicyType;
  51. typedef typename ImplTraits::StringType StringType;
  52. typedef typename ImplTraits::StringStreamType StringStreamType;
  53. typedef typename ImplTraits::BitsetType BitsetType;
  54. typedef typename ImplTraits::BitsetListType BitsetListType;
  55. typedef typename ImplTraits::template ExceptionBaseType<StreamType> ExceptionBaseType;
  56. protected:
  57. /** The printable message that goes with this exception, in your preferred
  58. * encoding format. ANTLR just uses ASCII by default but you can ignore these
  59. * messages or convert them to another format or whatever of course. They are
  60. * really internal messages that you then decide how to print out in a form that
  61. * the users of your product will understand, as they are unlikely to know what
  62. * to do with "Recognition exception at: [[TOK_GERUND..... " ;-)
  63. */
  64. StringType m_message;
  65. /** Name of the file/input source for reporting. Note that this may be empty!!
  66. */
  67. StringType m_streamName;
  68. /** Indicates the index of the 'token' we were looking at when the
  69. * exception occurred.
  70. */
  71. ANTLR_MARKER m_index;
  72. /** Indicates what the current token/tree was when the error occurred. Since not
  73. * all input streams will be able to retrieve the nth token, we track it here
  74. * instead. This is for parsers, and even tree parsers may set this.
  75. */
  76. const TokenType* m_token;
  77. /** Pointer to the next exception in the chain (if any)
  78. */
  79. ExceptionBaseType* m_nextException;
  80. /** Indicates the token we were expecting to see next when the error occurred
  81. */
  82. ANTLR_UINT32 m_expecting;
  83. /** Indicates a set of tokens that we were expecting to see one of when the
  84. * error occurred. It is a following bitset list, so you can use load it and use ->toIntList() on it
  85. * to generate an array of integer tokens that it represents.
  86. */
  87. BitsetListType* m_expectingSet;
  88. /** If this is a tree parser exception then the node is set to point to the node
  89. * that caused the issue.
  90. */
  91. TokenType* m_node;
  92. /** The current character when an error occurred - for lexers.
  93. */
  94. ANTLR_UCHAR m_c;
  95. /** Track the line at which the error occurred in case this is
  96. * generated from a lexer. We need to track this since the
  97. * unexpected char doesn't carry the line info.
  98. */
  99. ANTLR_UINT32 m_line;
  100. /** Character position in the line where the error occurred.
  101. */
  102. ANTLR_INT32 m_charPositionInLine;
  103. /** decision number for NVE
  104. */
  105. ANTLR_UINT32 m_decisionNum;
  106. /** State for NVE
  107. */
  108. ANTLR_UINT32 m_state;
  109. /** Rule name for failed predicate exception
  110. */
  111. StringType m_ruleName;
  112. /** Pointer to the input stream that this exception occurred in.
  113. */
  114. IntStreamType* m_input;
  115. public:
  116. StringType& get_message();
  117. StringType& get_streamName();
  118. ANTLR_MARKER get_index() const;
  119. const TokenType* get_token() const;
  120. ExceptionBaseType* get_nextException() const;
  121. ANTLR_UINT32 get_expecting() const;
  122. BitsetListType* get_expectingSet() const;
  123. TokenType* get_node() const;
  124. ANTLR_UCHAR get_c() const;
  125. ANTLR_UINT32 get_line() const;
  126. ANTLR_INT32 get_charPositionInLine() const;
  127. ANTLR_UINT32 get_decisionNum() const;
  128. ANTLR_UINT32 get_state() const;
  129. StringType& get_ruleName();
  130. IntStreamType* get_input() const;
  131. void set_message( const StringType& message );
  132. void set_streamName( const StringType& streamName );
  133. void set_index( ANTLR_MARKER index );
  134. void set_token( const TokenType* token );
  135. void set_nextException( ExceptionBaseType* nextException );
  136. void set_expecting( ANTLR_UINT32 expecting );
  137. void set_expectingSet( BitsetListType* expectingSet );
  138. void set_node( TokenType* node );
  139. void set_c( ANTLR_UCHAR c );
  140. void set_line( ANTLR_UINT32 line );
  141. void set_charPositionInLine( ANTLR_INT32 charPositionInLine );
  142. void set_decisionNum( ANTLR_UINT32 decisionNum );
  143. void set_state( ANTLR_UINT32 state );
  144. void set_ruleName( const StringType& ruleName );
  145. void set_input( IntStreamType* input );
  146. StringType getDescription() const;
  147. virtual StringType getName() const = 0;
  148. virtual ANTLR_UINT32 getType() const = 0;
  149. virtual void print() const = 0;
  150. virtual void displayRecognitionError( ANTLR_UINT8** tokenNames, StringStreamType& str ) const = 0;
  151. virtual ~ANTLR_ExceptionBase();
  152. protected:
  153. ANTLR_ExceptionBase(const StringType& message);
  154. };
  155. template<class ImplTraits, ExceptionType Ex, class StreamType>
  156. class ANTLR_Exception : public ImplTraits::template ExceptionBaseType<StreamType>
  157. {
  158. public:
  159. typedef typename ImplTraits::StringType StringType;
  160. typedef typename ImplTraits::StringStreamType StringStreamType;
  161. typedef typename ImplTraits::BitsetType BitsetType;
  162. typedef typename ImplTraits::template ExceptionBaseType<StreamType> BaseType;
  163. public:
  164. template<typename BaseRecognizerType>
  165. ANTLR_Exception(BaseRecognizerType* recognizer, const StringType& message);
  166. const StringType& get_name() const;
  167. virtual StringType getName() const;
  168. virtual ANTLR_UINT32 getType() const;
  169. virtual void print() const;
  170. virtual void displayRecognitionError( ANTLR_UINT8** tokenNames, StringStreamType& str_stream) const;
  171. };
  172. }
  173. #include "antlr3exception.inl"
  174. #endif