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