1ANTLR_BEGIN_NAMESPACE()
2
3template<class ImplTraits, class CtxType>
4CyclicDFA<ImplTraits, CtxType>::CyclicDFA( ANTLR_INT32	decisionNumber
5				, const ANTLR_UCHAR*	description
6				, const ANTLR_INT32* const	eot
7				, const ANTLR_INT32* const	eof
8				, const ANTLR_INT32* const	min
9				, const ANTLR_INT32* const	max
10				, const ANTLR_INT32* const	accept
11				, const ANTLR_INT32* const	special
12				, const ANTLR_INT32* const *const	transition )
13				:m_decisionNumber(decisionNumber)
14				, m_eot(eot)
15				, m_eof(eof)
16				, m_min(min)
17				, m_max(max)
18				, m_accept(accept)
19				, m_special(special)
20				, m_transition(transition)
21{
22	m_description = description;
23}
24
25template<class ImplTraits, class CtxType>
26CyclicDFA<ImplTraits, CtxType>::CyclicDFA( const CyclicDFA& dfa )
27{
28	m_decisionNumber = dfa.m_decisionNumber;
29	m_description = dfa.m_description;
30	m_eot = dfa.m_eot;
31	m_eof = dfa.m_eof;
32	m_min = dfa.m_min;
33	m_max = dfa.m_max;
34	m_accept = dfa.m_accept;
35	m_special = dfa.m_special;
36	m_transition = dfa.m_transition;
37}
38
39template<class ImplTraits, class CtxType>
40CyclicDFA<ImplTraits, CtxType>& CyclicDFA<ImplTraits, CtxType>::operator=( const CyclicDFA& dfa)
41{
42	m_decisionNumber = dfa.m_decisionNumber;
43	m_description = dfa.m_description;
44	m_eot = dfa.m_eot;
45	m_eof = dfa.m_eof;
46	m_min = dfa.m_min;
47	m_max = dfa.m_max;
48	m_accept = dfa.m_accept;
49	m_special = dfa.m_special;
50	m_transition = dfa.m_transition;
51	return *this;
52}
53
54template<class ImplTraits, class CtxType>
55ANTLR_INT32	CyclicDFA<ImplTraits, CtxType>::specialStateTransition(CtxType * ,
56																	RecognizerType* ,
57																	IntStreamType* , ANTLR_INT32 )
58{
59	return -1;
60}
61
62template<class ImplTraits, class CtxType>
63ANTLR_INT32	CyclicDFA<ImplTraits, CtxType>::specialTransition(CtxType * ctx,
64																	RecognizerType* recognizer,
65																	IntStreamType* is, ANTLR_INT32 s)
66{
67	return 0;
68}
69
70template<class ImplTraits, class CtxType>
71  template<typename SuperType>
72ANTLR_INT32	CyclicDFA<ImplTraits, CtxType>::predict(CtxType * ctx,
73															RecognizerType* recognizer,
74															IntStreamType* is, SuperType& super)
75{
76	ANTLR_MARKER	mark;
77    ANTLR_INT32	s;
78    ANTLR_INT32	specialState;
79    ANTLR_INT32	c;
80
81    mark	= is->mark();	    /* Store where we are right now	*/
82    s		= 0;		    /* Always start with state 0	*/
83
84	for (;;)
85	{
86		/* Pick out any special state entry for this state
87		 */
88		specialState	= m_special[s];
89
90		/* Transition the special state and consume an input token
91		 */
92		if  (specialState >= 0)
93		{
94			s = super.specialStateTransition(ctx, recognizer, is, specialState);
95
96			// Error?
97			//
98			if	(s<0)
99			{
100				// If the predicate/rule raised an exception then we leave it
101				// in tact, else we have an NVA.
102				//
103				if	(recognizer->get_state()->get_error() != true)
104				{
105					this->noViableAlt(recognizer, s);
106				}
107				is->rewind(mark);
108				return	0;
109			}
110			is->consume();
111			continue;
112		}
113
114		/* Accept state?
115		 */
116		if  (m_accept[s] >= 1)
117		{
118			is->rewind(mark);
119			return  m_accept[s];
120		}
121
122		/* Look for a normal transition state based upon the input token element
123		 */
124		c = is->_LA(1);
125
126		/* Check against min and max for this state
127		 */
128		if  (c>= m_min[s] && c <= m_max[s])
129		{
130			ANTLR_INT32   snext;
131
132			/* What is the next state?
133			 */
134			snext = m_transition[s][c - m_min[s]];
135
136			if	(snext < 0)
137			{
138				/* Was in range but not a normal transition
139				 * must check EOT, which is like the else clause.
140				 * eot[s]>=0 indicates that an EOT edge goes to another
141				 * state.
142				 */
143				if  ( m_eot[s] >= 0)
144				{
145					s = m_eot[s];
146					is->consume();
147					continue;
148				}
149				this->noViableAlt(recognizer, s);
150				is->rewind(mark);
151				return	0;
152			}
153
154			/* New current state - move to it
155			 */
156			s	= snext;
157			is->consume();
158			continue;
159		}
160		/* EOT Transition?
161		 */
162		if  ( m_eot[s] >= 0)
163		{
164			s	= m_eot[s];
165			is->consume();
166			continue;
167		}
168		/* EOF transition to accept state?
169		 */
170		if  ( c == ImplTraits::CommonTokenType::TOKEN_EOF && m_eof[s] >= 0)
171		{
172			is->rewind(mark);
173			return  m_accept[m_eof[s]];
174		}
175
176		/* No alt, so bomb
177		 */
178		this->noViableAlt(recognizer, s);
179		is->rewind(mark);
180		return 0;
181	}
182}
183
184template<class ImplTraits, class CtxType>
185void CyclicDFA<ImplTraits, CtxType>::noViableAlt(RecognizerType* rec, ANTLR_UINT32 s)
186{
187	// In backtracking mode, we just set the failed flag so that the
188	// alt can just exit right now. If we are parsing though, then
189	// we want the exception to be raised.
190	//
191    if	(rec->get_state()->get_backtracking() > 0)
192    {
193		rec->get_state()->set_failed(true);
194    }
195	else
196	{
197		ANTLR_Exception<ImplTraits, NO_VIABLE_ALT_EXCEPTION, StreamType>* ex
198			= new ANTLR_Exception<ImplTraits, NO_VIABLE_ALT_EXCEPTION, StreamType>( rec, (const char*)m_description );
199		ex->set_decisionNum( m_decisionNumber );
200		ex->set_state(s);
201	}
202}
203
204ANTLR_END_NAMESPACE()
205