1*1c78a5d6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*1c78a5d6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*1c78a5d6SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*1c78a5d6SAndrew Rist * distributed with this work for additional information
6*1c78a5d6SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*1c78a5d6SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*1c78a5d6SAndrew Rist * "License"); you may not use this file except in compliance
9*1c78a5d6SAndrew Rist * with the License. You may obtain a copy of the License at
10*1c78a5d6SAndrew Rist *
11*1c78a5d6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*1c78a5d6SAndrew Rist *
13*1c78a5d6SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*1c78a5d6SAndrew Rist * software distributed under the License is distributed on an
15*1c78a5d6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1c78a5d6SAndrew Rist * KIND, either express or implied. See the License for the
17*1c78a5d6SAndrew Rist * specific language governing permissions and limitations
18*1c78a5d6SAndrew Rist * under the License.
19*1c78a5d6SAndrew Rist *
20*1c78a5d6SAndrew Rist *************************************************************/
21*1c78a5d6SAndrew Rist
22*1c78a5d6SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #ifndef ADC_CPP_CALLF_HXX
25cdf0e10cSrcweir #define ADC_CPP_CALLF_HXX
26cdf0e10cSrcweir
27cdf0e10cSrcweir // USED SERVICES
28cdf0e10cSrcweir
29cdf0e10cSrcweir
30cdf0e10cSrcweir
31cdf0e10cSrcweir
32cdf0e10cSrcweir /** This represents a function to be called, if a specific kind of token
33cdf0e10cSrcweir arrives in the semantic parser.
34cdf0e10cSrcweir
35cdf0e10cSrcweir @descr This class is only to be used as member of PeStatus<PE>.
36cdf0e10cSrcweir @template PE
37cdf0e10cSrcweir The owning ParseEnvironment.
38cdf0e10cSrcweir @see PeStatus, ParseEnvironment
39cdf0e10cSrcweir */
40cdf0e10cSrcweir template <class PE>
41cdf0e10cSrcweir class CallFunction
42cdf0e10cSrcweir {
43cdf0e10cSrcweir public:
44cdf0e10cSrcweir typedef void (PE::*F_Tok)(const char *);
45cdf0e10cSrcweir
46cdf0e10cSrcweir CallFunction(
47cdf0e10cSrcweir F_Tok i_f2Call,
48cdf0e10cSrcweir INT16 i_nTokType );
49cdf0e10cSrcweir
50cdf0e10cSrcweir F_Tok GetF() const;
51cdf0e10cSrcweir INT16 TokType() const;
52cdf0e10cSrcweir
53cdf0e10cSrcweir private:
54cdf0e10cSrcweir // DATA
55cdf0e10cSrcweir F_Tok f2Call;
56cdf0e10cSrcweir INT16 nTokType;
57cdf0e10cSrcweir };
58cdf0e10cSrcweir
59cdf0e10cSrcweir
60cdf0e10cSrcweir /** One state within a ParseEnvironment.
61cdf0e10cSrcweir
62cdf0e10cSrcweir @template PE
63cdf0e10cSrcweir The owning ParseEnvironment.
64cdf0e10cSrcweir */
65cdf0e10cSrcweir template <class PE>
66cdf0e10cSrcweir class PeStatus
67cdf0e10cSrcweir {
68cdf0e10cSrcweir public:
69cdf0e10cSrcweir typedef typename CallFunction<PE>::F_Tok F_Tok;
70cdf0e10cSrcweir
71cdf0e10cSrcweir PeStatus(
72cdf0e10cSrcweir PE & i_rMyPE,
73cdf0e10cSrcweir uintt i_nSize,
74cdf0e10cSrcweir F_Tok * i_pFuncArray,
75cdf0e10cSrcweir INT16 * i_pTokTypeArray,
76cdf0e10cSrcweir F_Tok i_pDefault );
77cdf0e10cSrcweir virtual ~PeStatus();
78cdf0e10cSrcweir
79cdf0e10cSrcweir virtual void Call_Handler(
80cdf0e10cSrcweir INT16 i_nTokTypeId,
81cdf0e10cSrcweir const char * i_sTokenText ) const;
82cdf0e10cSrcweir
83cdf0e10cSrcweir private:
84cdf0e10cSrcweir bool CheckForCall(
85cdf0e10cSrcweir uintt i_nPos,
86cdf0e10cSrcweir INT16 i_nTokTypeId,
87cdf0e10cSrcweir const char * i_sTokenText ) const;
88cdf0e10cSrcweir
89cdf0e10cSrcweir PE * pMyPE;
90cdf0e10cSrcweir std::vector< CallFunction<PE> >
91cdf0e10cSrcweir aBranches;
92cdf0e10cSrcweir F_Tok fDefault;
93cdf0e10cSrcweir };
94cdf0e10cSrcweir
95cdf0e10cSrcweir
96cdf0e10cSrcweir template <class PE>
97cdf0e10cSrcweir class PeStatusArray
98cdf0e10cSrcweir {
99cdf0e10cSrcweir public:
100cdf0e10cSrcweir typedef typename PE::E_State State;
101cdf0e10cSrcweir
102cdf0e10cSrcweir PeStatusArray();
103cdf0e10cSrcweir void InsertState(
104cdf0e10cSrcweir State i_ePosition,
105cdf0e10cSrcweir DYN PeStatus<PE> & let_drState );
106cdf0e10cSrcweir ~PeStatusArray();
107cdf0e10cSrcweir
108cdf0e10cSrcweir const PeStatus<PE> &
109cdf0e10cSrcweir operator[](
110cdf0e10cSrcweir State i_ePosition ) const;
111cdf0e10cSrcweir
112cdf0e10cSrcweir void SetCur(
113cdf0e10cSrcweir State i_eCurState );
114cdf0e10cSrcweir const PeStatus<PE> &
115cdf0e10cSrcweir Cur() const;
116cdf0e10cSrcweir
117cdf0e10cSrcweir private:
118cdf0e10cSrcweir DYN PeStatus<PE> * aStati[PE::size_of_states];
119cdf0e10cSrcweir State eState;
120cdf0e10cSrcweir };
121cdf0e10cSrcweir
122cdf0e10cSrcweir
123cdf0e10cSrcweir
124cdf0e10cSrcweir // IMPLEMENTATION
125cdf0e10cSrcweir
126cdf0e10cSrcweir
127cdf0e10cSrcweir // CallFunction
128cdf0e10cSrcweir
129cdf0e10cSrcweir template <class PE>
CallFunction(F_Tok i_f2Call,INT16 i_nTokType)130cdf0e10cSrcweir CallFunction<PE>::CallFunction( F_Tok i_f2Call,
131cdf0e10cSrcweir INT16 i_nTokType )
132cdf0e10cSrcweir : f2Call(i_f2Call),
133cdf0e10cSrcweir nTokType(i_nTokType)
134cdf0e10cSrcweir {
135cdf0e10cSrcweir }
136cdf0e10cSrcweir
137cdf0e10cSrcweir template <class PE>
138cdf0e10cSrcweir inline typename CallFunction<PE>::F_Tok
GetF() const139cdf0e10cSrcweir CallFunction<PE>::GetF() const
140cdf0e10cSrcweir {
141cdf0e10cSrcweir return f2Call;
142cdf0e10cSrcweir }
143cdf0e10cSrcweir
144cdf0e10cSrcweir template <class PE>
145cdf0e10cSrcweir inline INT16
TokType() const146cdf0e10cSrcweir CallFunction<PE>::TokType() const
147cdf0e10cSrcweir {
148cdf0e10cSrcweir return nTokType;
149cdf0e10cSrcweir }
150cdf0e10cSrcweir
151cdf0e10cSrcweir
152cdf0e10cSrcweir
153cdf0e10cSrcweir // PeStatus
154cdf0e10cSrcweir
155cdf0e10cSrcweir template <class PE>
PeStatus(PE & i_rMyPE,uintt i_nSize,F_Tok * i_pFuncArray,INT16 * i_pTokTypeArray,F_Tok i_fDefault)156cdf0e10cSrcweir PeStatus<PE>::PeStatus( PE & i_rMyPE,
157cdf0e10cSrcweir uintt i_nSize,
158cdf0e10cSrcweir F_Tok * i_pFuncArray,
159cdf0e10cSrcweir INT16 * i_pTokTypeArray,
160cdf0e10cSrcweir F_Tok i_fDefault )
161cdf0e10cSrcweir : pMyPE(&i_rMyPE),
162cdf0e10cSrcweir fDefault(i_fDefault)
163cdf0e10cSrcweir {
164cdf0e10cSrcweir aBranches.reserve(i_nSize);
165cdf0e10cSrcweir for ( uintt i = 0; i < i_nSize; ++i )
166cdf0e10cSrcweir {
167cdf0e10cSrcweir // csv_assert(i > 0 ? i_pTokTypeArray[i] > i_pTokTypeArray[i-1] : true);
168cdf0e10cSrcweir aBranches.push_back( CallFunction<PE>( i_pFuncArray[i], i_pTokTypeArray[i]) );
169cdf0e10cSrcweir } // end for
170cdf0e10cSrcweir }
171cdf0e10cSrcweir
172cdf0e10cSrcweir template <class PE>
~PeStatus()173cdf0e10cSrcweir PeStatus<PE>::~PeStatus()
174cdf0e10cSrcweir {
175cdf0e10cSrcweir
176cdf0e10cSrcweir }
177cdf0e10cSrcweir
178cdf0e10cSrcweir template <class PE>
179cdf0e10cSrcweir void
Call_Handler(INT16 i_nTokTypeId,const char * i_sTokenText) const180cdf0e10cSrcweir PeStatus<PE>::Call_Handler( INT16 i_nTokTypeId,
181cdf0e10cSrcweir const char * i_sTokenText ) const
182cdf0e10cSrcweir {
183cdf0e10cSrcweir uintt nSize = aBranches.size();
184cdf0e10cSrcweir uintt nPos = nSize / 2;
185cdf0e10cSrcweir
186cdf0e10cSrcweir if ( i_nTokTypeId < aBranches[nPos].TokType() )
187cdf0e10cSrcweir {
188cdf0e10cSrcweir for ( --nPos; intt(nPos) >= 0; --nPos )
189cdf0e10cSrcweir {
190cdf0e10cSrcweir if (CheckForCall(nPos, i_nTokTypeId, i_sTokenText))
191cdf0e10cSrcweir return;
192cdf0e10cSrcweir }
193cdf0e10cSrcweir }
194cdf0e10cSrcweir else
195cdf0e10cSrcweir {
196cdf0e10cSrcweir for ( ; nPos < nSize; ++nPos )
197cdf0e10cSrcweir {
198cdf0e10cSrcweir if (CheckForCall(nPos, i_nTokTypeId, i_sTokenText))
199cdf0e10cSrcweir return;
200cdf0e10cSrcweir }
201cdf0e10cSrcweir }
202cdf0e10cSrcweir
203cdf0e10cSrcweir (pMyPE->*fDefault)(i_sTokenText);
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
206cdf0e10cSrcweir template <class PE>
207cdf0e10cSrcweir bool
CheckForCall(uintt i_nPos,INT16 i_nTokTypeId,const char * i_sTokenText) const208cdf0e10cSrcweir PeStatus<PE>::CheckForCall( uintt i_nPos,
209cdf0e10cSrcweir INT16 i_nTokTypeId,
210cdf0e10cSrcweir const char * i_sTokenText ) const
211cdf0e10cSrcweir {
212cdf0e10cSrcweir if ( aBranches[i_nPos].TokType() == i_nTokTypeId )
213cdf0e10cSrcweir {
214cdf0e10cSrcweir (pMyPE->*aBranches[i_nPos].GetF())(i_sTokenText);
215cdf0e10cSrcweir return true;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir return false;
218cdf0e10cSrcweir }
219cdf0e10cSrcweir
220cdf0e10cSrcweir // PeStatusArray
221cdf0e10cSrcweir
222cdf0e10cSrcweir template <class PE>
PeStatusArray()223cdf0e10cSrcweir PeStatusArray<PE>::PeStatusArray()
224cdf0e10cSrcweir : eState(PE::size_of_states)
225cdf0e10cSrcweir {
226cdf0e10cSrcweir memset(aStati, 0, sizeof aStati);
227cdf0e10cSrcweir }
228cdf0e10cSrcweir
229cdf0e10cSrcweir template <class PE>
230cdf0e10cSrcweir void
InsertState(State i_ePosition,DYN PeStatus<PE> & let_drState)231cdf0e10cSrcweir PeStatusArray<PE>::InsertState( State i_ePosition,
232cdf0e10cSrcweir DYN PeStatus<PE> & let_drState )
233cdf0e10cSrcweir {
234cdf0e10cSrcweir csv_assert(aStati[i_ePosition] == 0);
235cdf0e10cSrcweir aStati[i_ePosition] = &let_drState;
236cdf0e10cSrcweir }
237cdf0e10cSrcweir
238cdf0e10cSrcweir template <class PE>
~PeStatusArray()239cdf0e10cSrcweir PeStatusArray<PE>::~PeStatusArray()
240cdf0e10cSrcweir {
241cdf0e10cSrcweir int i_max = PE::size_of_states;
242cdf0e10cSrcweir for (int i = 0; i < i_max; i++)
243cdf0e10cSrcweir {
244cdf0e10cSrcweir delete aStati[i];
245cdf0e10cSrcweir } // end for
246cdf0e10cSrcweir }
247cdf0e10cSrcweir
248cdf0e10cSrcweir template <class PE>
249cdf0e10cSrcweir inline const PeStatus<PE> &
operator [](State i_ePosition) const250cdf0e10cSrcweir PeStatusArray<PE>::operator[]( State i_ePosition ) const
251cdf0e10cSrcweir {
252cdf0e10cSrcweir csv_assert( uintt(i_ePosition) < PE::size_of_states );
253cdf0e10cSrcweir csv_assert( aStati[i_ePosition] != 0 );
254cdf0e10cSrcweir return *aStati[i_ePosition];
255cdf0e10cSrcweir }
256cdf0e10cSrcweir
257cdf0e10cSrcweir template <class PE>
258cdf0e10cSrcweir inline void
SetCur(State i_eCurState)259cdf0e10cSrcweir PeStatusArray<PE>::SetCur( State i_eCurState )
260cdf0e10cSrcweir {
261cdf0e10cSrcweir eState = i_eCurState;
262cdf0e10cSrcweir }
263cdf0e10cSrcweir
264cdf0e10cSrcweir
265cdf0e10cSrcweir template <class PE>
266cdf0e10cSrcweir const PeStatus<PE> &
Cur() const267cdf0e10cSrcweir PeStatusArray<PE>::Cur() const
268cdf0e10cSrcweir {
269cdf0e10cSrcweir return (*this)[eState];
270cdf0e10cSrcweir }
271cdf0e10cSrcweir
272cdf0e10cSrcweir #define SEMPARSE_CREATE_STATUS(penv, state, default_function) \
273cdf0e10cSrcweir pStati->InsertState( state, \
274cdf0e10cSrcweir *new PeStatus<penv>( \
275cdf0e10cSrcweir *this, \
276cdf0e10cSrcweir sizeof( stateT_##state ) / sizeof (INT16), \
277cdf0e10cSrcweir stateF_##state, \
278cdf0e10cSrcweir stateT_##state, \
279cdf0e10cSrcweir &penv::default_function ) )
280cdf0e10cSrcweir
281cdf0e10cSrcweir
282cdf0e10cSrcweir #endif
283cdf0e10cSrcweir
284