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