1ANTLR_BEGIN_NAMESPACE()
2
3template<class ImplTraits, class SuperType>
4RewriteRuleElementStream<ImplTraits, SuperType>::RewriteRuleElementStream(TreeAdaptorType* adaptor,
5													RecognizerType* rec, ANTLR_UINT8* description)
6{
7	this->init(adaptor, rec, description);
8}
9
10template<class ImplTraits, class SuperType>
11RewriteRuleElementStream<ImplTraits, SuperType>::RewriteRuleElementStream(TreeAdaptorType* adaptor,
12								RecognizerType* rec, ANTLR_UINT8* description, TokenType* oneElement)
13{
14	this->init(adaptor, rec, description);
15	if( oneElement != NULL )
16		this->add( oneElement );
17}
18
19template<class ImplTraits, class SuperType>
20RewriteRuleElementStream<ImplTraits, SuperType>::RewriteRuleElementStream(TreeAdaptorType* adaptor,
21						RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements)
22						:m_elements(elements)
23{
24	this->init(adaptor, rec, description);
25}
26
27template<class ImplTraits, class SuperType>
28void RewriteRuleElementStream<ImplTraits, SuperType>::init(TreeAdaptorType* adaptor,
29								RecognizerType* rec, ANTLR_UINT8* description)
30{
31	m_rec = rec;
32	m_adaptor = adaptor;
33	m_cursor  = 0;
34	m_dirty	  = false;
35	m_singleElement = NULL;
36}
37
38template<class ImplTraits>
39RewriteRuleTokenStream<ImplTraits>::RewriteRuleTokenStream(TreeAdaptorType* adaptor,
40							    RecognizerType* rec, ANTLR_UINT8* description)
41                                                              :BaseType(adaptor, rec, description)
42{
43}
44
45template<class ImplTraits>
46RewriteRuleTokenStream<ImplTraits>::RewriteRuleTokenStream(TreeAdaptorType* adaptor, RecognizerType* rec,
47							    ANTLR_UINT8* description, TokenType* oneElement)
48							:BaseType(adaptor, rec, description, oneElement)
49{
50}
51
52template<class ImplTraits>
53RewriteRuleTokenStream<ImplTraits>::RewriteRuleTokenStream(TreeAdaptorType* adaptor,
54						RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements)
55						:BaseType(adaptor, rec, description, elements)
56{
57}
58
59template<class ImplTraits>
60RewriteRuleSubtreeStream<ImplTraits>::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor,
61								RecognizerType* rec, ANTLR_UINT8* description)
62						 :BaseType(adaptor, rec, description)
63{
64}
65
66template<class ImplTraits>
67RewriteRuleSubtreeStream<ImplTraits>::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, RecognizerType* rec,
68								ANTLR_UINT8* description, TokenType* oneElement)
69							:BaseType(adaptor, rec, description, oneElement)
70{
71}
72
73template<class ImplTraits>
74RewriteRuleSubtreeStream<ImplTraits>::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor,
75						RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements)
76						:BaseType(adaptor, rec, description, elements)
77{
78}
79
80template<class ImplTraits>
81RewriteRuleNodeStream<ImplTraits>::RewriteRuleNodeStream(TreeAdaptorType* adaptor,
82							 RecognizerType* rec, ANTLR_UINT8* description)
83						:BaseType(adaptor, rec, description)
84{
85}
86
87template<class ImplTraits>
88RewriteRuleNodeStream<ImplTraits>::RewriteRuleNodeStream(TreeAdaptorType* adaptor, RecognizerType* rec,
89							ANTLR_UINT8* description, TokenType* oneElement)
90						:BaseType(adaptor, rec, description, oneElement)
91{
92}
93
94template<class ImplTraits>
95RewriteRuleNodeStream<ImplTraits>::RewriteRuleNodeStream(TreeAdaptorType* adaptor,
96						RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements)
97						:BaseType(adaptor, rec, description, elements)
98{
99}
100
101template<class ImplTraits, class SuperType>
102void	RewriteRuleElementStream<ImplTraits, SuperType>::reset()
103{
104	m_dirty = true;
105	m_cursor = 0;
106}
107
108template<class ImplTraits, class SuperType>
109void	RewriteRuleElementStream<ImplTraits, SuperType>::add(TokenType* el)
110{
111	if ( el== NULL )
112		return;
113
114	if ( !m_elements.empty() )
115	{
116		// if in list, just add
117		m_elements.push_back(el);
118		return;
119	}
120
121	if ( m_singleElement == NULL )
122	{
123		// no elements yet, track w/o list
124		m_singleElement = el;
125		return;
126	}
127
128	// adding 2nd element, move to list
129	m_elements.push_back(m_singleElement);
130	m_singleElement = NULL;
131	m_elements.push_back(el);
132}
133
134template<class ImplTraits, class SuperType>
135typename RewriteRuleElementStream<ImplTraits, SuperType>::TokenType*
136RewriteRuleElementStream<ImplTraits, SuperType>::_next()
137{
138	ANTLR_UINT32		n;
139	TreeType*	t;
140
141	n = this->size();
142
143	if (n == 0)
144	{
145		// This means that the stream is empty
146		//
147		return NULL;	// Caller must cope with this
148	}
149
150	// Traversed all the available elements already?
151	//
152	if ( m_cursor >= n)
153	{
154		if (n == 1)
155		{
156			// Special case when size is single element, it will just dup a lot
157			//
158			return this->toTree(m_singleElement);
159		}
160
161		// Out of elements and the size is not 1, so we cannot assume
162		// that we just duplicate the entry n times (such as ID ent+ -> ^(ID ent)+)
163		// This means we ran out of elements earlier than was expected.
164		//
165		return NULL;	// Caller must cope with this
166	}
167
168	// Elements available either for duping or just available
169	//
170	if ( m_singleElement != NULL)
171	{
172		m_cursor++;   // Cursor advances even for single element as this tells us to dup()
173		return this->toTree(m_singleElement);
174	}
175
176	// More than just a single element so we extract it from the
177	// vector.
178	//
179	t = this->toTree( m_elements.at(m_cursor));
180	m_cursor++;
181	return t;
182}
183
184template<class ImplTraits, class SuperType>
185typename RewriteRuleElementStream<ImplTraits, SuperType>::TreeType*
186RewriteRuleElementStream<ImplTraits, SuperType>::nextTree()
187{
188	ANTLR_UINT32		n;
189	TreeType*  el;
190
191	n = this->size();
192
193	if ( m_dirty || ( (m_cursor >=n) && (n==1)) )
194	{
195		// if out of elements and size is 1, dup
196		//
197		el = this->_next();
198		return this->dup(el);
199	}
200
201	// test size above then fetch
202	//
203	el = this->_next();
204	return el;
205}
206
207template<class ImplTraits, class SuperType>
208typename RewriteRuleElementStream<ImplTraits, SuperType>::TokenType*
209RewriteRuleElementStream<ImplTraits, SuperType>::nextToken()
210{
211	return this->_next();
212}
213
214template<class ImplTraits, class SuperType>
215typename RewriteRuleElementStream<ImplTraits, SuperType>::TokenType*
216RewriteRuleElementStream<ImplTraits, SuperType>::next()
217{
218	ANTLR_UINT32   s;
219	s = this->size();
220	if ( (m_cursor >= s) && (s == 1) )
221	{
222		TreeType* el;
223		el = this->_next();
224		return	this->dup(el);
225	}
226	return this->_next();
227}
228
229template<class ImplTraits>
230typename RewriteRuleSubtreeStream<ImplTraits>::TreeType*
231RewriteRuleSubtreeStream<ImplTraits>::dup(TreeType* element)
232{
233	return this->dupTree(element);
234}
235
236template<class ImplTraits>
237typename RewriteRuleSubtreeStream<ImplTraits>::TreeType*
238RewriteRuleSubtreeStream<ImplTraits>::dupTree(TreeType* element)
239{
240	return BaseType::m_adaptor->dupNode(element);
241}
242
243template<class ImplTraits, class SuperType>
244typename RewriteRuleElementStream<ImplTraits, SuperType>::TreeType*
245RewriteRuleElementStream<ImplTraits, SuperType>::toTree( TreeType* element)
246{
247	return element;
248}
249
250template<class ImplTraits>
251typename RewriteRuleNodeStream<ImplTraits>::TreeType*
252RewriteRuleNodeStream<ImplTraits>::toTree(TreeType* element)
253{
254	return this->toTreeNode(element);
255}
256
257template<class ImplTraits>
258typename RewriteRuleNodeStream<ImplTraits>::TreeType*
259RewriteRuleNodeStream<ImplTraits>::toTreeNode(TreeType* element)
260{
261	return BaseType::m_adaptor->dupNode(element);
262}
263
264template<class ImplTraits, class SuperType>
265bool RewriteRuleElementStream<ImplTraits, SuperType>::hasNext()
266{
267	if (	((m_singleElement != NULL) && (m_cursor < 1))
268		||	 ( !m_elements.empty() && m_cursor < m_elements.size()))
269	{
270		return true;
271	}
272	else
273	{
274		return false;
275	}
276}
277
278template<class ImplTraits >
279typename RewriteRuleTokenStream<ImplTraits>::TreeType*
280RewriteRuleTokenStream<ImplTraits>::nextNode()
281{
282	return this->nextNodeToken();
283}
284
285template<class ImplTraits>
286typename RewriteRuleTokenStream<ImplTraits>::TreeType*
287RewriteRuleTokenStream<ImplTraits>::nextNodeToken()
288{
289	return BaseType::m_adaptor->create(this->_next());
290}
291
292/// Number of elements available in the stream
293///
294template<class ImplTraits, class SuperType>
295ANTLR_UINT32	RewriteRuleElementStream<ImplTraits, SuperType>::size()
296{
297	ANTLR_UINT32   n = 0;
298
299	/// Should be a count of one if singleElement is set. I copied this
300	/// logic from the java implementation, which I suspect is just guarding
301	/// against someone setting singleElement and forgetting to NULL it out
302	///
303	if ( m_singleElement != NULL)
304	{
305		n = 1;
306	}
307	else
308	{
309		if ( !m_elements.empty() )
310		{
311			return (ANTLR_UINT32)(m_elements.size());
312		}
313	}
314	return n;
315
316}
317
318template<class ImplTraits, class SuperType>
319typename RewriteRuleElementStream<ImplTraits, SuperType>::StringType
320RewriteRuleElementStream<ImplTraits, SuperType>::getDescription()
321{
322	if ( m_elementDescription.empty() )
323	{
324		m_elementDescription = "<unknown source>";
325	}
326	return  m_elementDescription;
327}
328
329template<class ImplTraits, class SuperType>
330RewriteRuleElementStream<ImplTraits, SuperType>::~RewriteRuleElementStream()
331{
332	TreeType* tree;
333
334    // Before placing the stream back in the pool, we
335	// need to clear any vector it has. This is so any
336	// free pointers that are associated with the
337	// entires are called. However, if this particular function is called
338    // then we know that the entries in the stream are definately
339    // tree nodes. Hence we check to see if any of them were nilNodes as
340    // if they were, we can reuse them.
341	//
342	if	( !m_elements.empty() )
343	{
344        // We have some elements to traverse
345        //
346        ANTLR_UINT32 i;
347
348        for (i = 1; i<= m_elements.size(); i++)
349        {
350            tree = m_elements.at(i-1);
351            if  ( (tree != NULL) && tree->isNilNode() )
352            {
353                // Had to remove this for now, check is not comprehensive enough
354                // tree->reuse(tree);
355            }
356        }
357		m_elements.clear();
358	}
359	else
360	{
361        if  (m_singleElement != NULL)
362        {
363            tree = m_singleElement;
364            if  (tree->isNilNode())
365            {
366                // Had to remove this for now, check is not comprehensive enough
367              //   tree->reuse(tree);
368            }
369        }
370        m_singleElement = NULL;
371	}
372}
373
374ANTLR_END_NAMESPACE()
375