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