xref: /AOO41X/main/autodoc/source/parser/inc/semantic/callf.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef ADC_CPP_CALLF_HXX
29*cdf0e10cSrcweir #define ADC_CPP_CALLF_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // USED SERVICES
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir /**	This represents a function to be called, if a specific kind of token
37*cdf0e10cSrcweir 	arrives in the semantic parser.
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 	@descr This class is only to be used as member of PeStatus<PE>.
40*cdf0e10cSrcweir 	@template PE
41*cdf0e10cSrcweir 		The owning ParseEnvironment.
42*cdf0e10cSrcweir 	@see PeStatus, ParseEnvironment
43*cdf0e10cSrcweir */
44*cdf0e10cSrcweir template <class PE>
45*cdf0e10cSrcweir class CallFunction
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir   public:
48*cdf0e10cSrcweir 	typedef void (PE::*F_Tok)(const char *);
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir 						CallFunction(
51*cdf0e10cSrcweir 							F_Tok				i_f2Call,
52*cdf0e10cSrcweir 							INT16				i_nTokType );
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 	F_Tok				GetF() const;
55*cdf0e10cSrcweir 	INT16				TokType() const;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir   private:
58*cdf0e10cSrcweir 	// DATA
59*cdf0e10cSrcweir 	F_Tok				f2Call;
60*cdf0e10cSrcweir 	INT16				nTokType;
61*cdf0e10cSrcweir };
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir /**	One state within a ParseEnvironment.
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	@template PE
67*cdf0e10cSrcweir 		The owning ParseEnvironment.
68*cdf0e10cSrcweir */
69*cdf0e10cSrcweir template <class PE>
70*cdf0e10cSrcweir class PeStatus
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir   public:
73*cdf0e10cSrcweir 	typedef typename CallFunction<PE>::F_Tok  F_Tok;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 						PeStatus(
76*cdf0e10cSrcweir 							PE &	  			i_rMyPE,
77*cdf0e10cSrcweir 							uintt				i_nSize,
78*cdf0e10cSrcweir 							F_Tok *				i_pFuncArray,
79*cdf0e10cSrcweir 							INT16 *             i_pTokTypeArray,
80*cdf0e10cSrcweir 							F_Tok				i_pDefault );
81*cdf0e10cSrcweir 	virtual             ~PeStatus();
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 	virtual void	   	Call_Handler(
84*cdf0e10cSrcweir 							INT16				i_nTokTypeId,
85*cdf0e10cSrcweir 							const char *		i_sTokenText ) const;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir   private:
88*cdf0e10cSrcweir 	bool				CheckForCall(
89*cdf0e10cSrcweir 							uintt				i_nPos,
90*cdf0e10cSrcweir 							INT16				i_nTokTypeId,
91*cdf0e10cSrcweir 							const char *		i_sTokenText ) const;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	PE *	  			pMyPE;
94*cdf0e10cSrcweir 	std::vector< CallFunction<PE> >
95*cdf0e10cSrcweir 						aBranches;
96*cdf0e10cSrcweir 	F_Tok				fDefault;
97*cdf0e10cSrcweir };
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir template <class PE>
101*cdf0e10cSrcweir class PeStatusArray
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir   public:
104*cdf0e10cSrcweir 	typedef typename PE::E_State	State;
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 						PeStatusArray();
107*cdf0e10cSrcweir 	void				InsertState(
108*cdf0e10cSrcweir 							State				i_ePosition,
109*cdf0e10cSrcweir 							DYN PeStatus<PE> &	let_drState );
110*cdf0e10cSrcweir 						~PeStatusArray();
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	const PeStatus<PE> &
113*cdf0e10cSrcweir 						operator[](
114*cdf0e10cSrcweir 							State				i_ePosition ) const;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 	void				SetCur(
117*cdf0e10cSrcweir 							State				i_eCurState );
118*cdf0e10cSrcweir 	const PeStatus<PE> &
119*cdf0e10cSrcweir 						Cur() const;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir   private:
122*cdf0e10cSrcweir 	DYN PeStatus<PE> *	aStati[PE::size_of_states];
123*cdf0e10cSrcweir 	State				eState;
124*cdf0e10cSrcweir };
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir // IMPLEMENTATION
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir // CallFunction
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir template <class PE>
134*cdf0e10cSrcweir CallFunction<PE>::CallFunction(	F_Tok 	i_f2Call,
135*cdf0e10cSrcweir 								INT16	i_nTokType )
136*cdf0e10cSrcweir 	:	f2Call(i_f2Call),
137*cdf0e10cSrcweir 		nTokType(i_nTokType)
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir template <class PE>
142*cdf0e10cSrcweir inline typename CallFunction<PE>::F_Tok
143*cdf0e10cSrcweir CallFunction<PE>::GetF() const
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	return f2Call;
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir template <class PE>
149*cdf0e10cSrcweir inline INT16
150*cdf0e10cSrcweir CallFunction<PE>::TokType() const
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir 	return nTokType;
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir // PeStatus
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir template <class PE>
160*cdf0e10cSrcweir PeStatus<PE>::PeStatus( PE &	  	i_rMyPE,
161*cdf0e10cSrcweir 						uintt		i_nSize,
162*cdf0e10cSrcweir 						F_Tok *		i_pFuncArray,
163*cdf0e10cSrcweir 						INT16 *     i_pTokTypeArray,
164*cdf0e10cSrcweir 						F_Tok		i_fDefault )
165*cdf0e10cSrcweir 	:	pMyPE(&i_rMyPE),
166*cdf0e10cSrcweir 		fDefault(i_fDefault)
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir 	aBranches.reserve(i_nSize);
169*cdf0e10cSrcweir 	for ( uintt i = 0; i < i_nSize; ++i )
170*cdf0e10cSrcweir 	{
171*cdf0e10cSrcweir //		csv_assert(i > 0 ? i_pTokTypeArray[i] > i_pTokTypeArray[i-1] : true);
172*cdf0e10cSrcweir     	aBranches.push_back( CallFunction<PE>( i_pFuncArray[i], i_pTokTypeArray[i]) );
173*cdf0e10cSrcweir 	}  // end for
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir template <class PE>
177*cdf0e10cSrcweir PeStatus<PE>::~PeStatus()
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir template <class PE>
183*cdf0e10cSrcweir void
184*cdf0e10cSrcweir PeStatus<PE>::Call_Handler( INT16				i_nTokTypeId,
185*cdf0e10cSrcweir 							const char *		i_sTokenText ) const
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir 	uintt nSize = aBranches.size();
188*cdf0e10cSrcweir 	uintt nPos = nSize / 2;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	if ( i_nTokTypeId < aBranches[nPos].TokType() )
191*cdf0e10cSrcweir 	{
192*cdf0e10cSrcweir 		for ( --nPos; intt(nPos) >= 0; --nPos )
193*cdf0e10cSrcweir 		{
194*cdf0e10cSrcweir 			if (CheckForCall(nPos, i_nTokTypeId, i_sTokenText))
195*cdf0e10cSrcweir 				return;
196*cdf0e10cSrcweir 		}
197*cdf0e10cSrcweir 	}
198*cdf0e10cSrcweir 	else
199*cdf0e10cSrcweir 	{
200*cdf0e10cSrcweir 		for ( ; nPos < nSize; ++nPos )
201*cdf0e10cSrcweir 		{
202*cdf0e10cSrcweir 			if (CheckForCall(nPos, i_nTokTypeId, i_sTokenText))
203*cdf0e10cSrcweir 				return;
204*cdf0e10cSrcweir 		}
205*cdf0e10cSrcweir 	}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 	(pMyPE->*fDefault)(i_sTokenText);
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir template <class PE>
211*cdf0e10cSrcweir bool
212*cdf0e10cSrcweir PeStatus<PE>::CheckForCall(	uintt				i_nPos,
213*cdf0e10cSrcweir 							INT16				i_nTokTypeId,
214*cdf0e10cSrcweir 							const char *		i_sTokenText ) const
215*cdf0e10cSrcweir {
216*cdf0e10cSrcweir 	if ( aBranches[i_nPos].TokType() == i_nTokTypeId )
217*cdf0e10cSrcweir 	{
218*cdf0e10cSrcweir 		(pMyPE->*aBranches[i_nPos].GetF())(i_sTokenText);
219*cdf0e10cSrcweir 		return true;
220*cdf0e10cSrcweir 	}
221*cdf0e10cSrcweir 	return false;
222*cdf0e10cSrcweir }
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir // PeStatusArray
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir template <class PE>
227*cdf0e10cSrcweir PeStatusArray<PE>::PeStatusArray()
228*cdf0e10cSrcweir 	:	eState(PE::size_of_states)
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir 	memset(aStati, 0, sizeof aStati);
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir template <class PE>
234*cdf0e10cSrcweir void
235*cdf0e10cSrcweir PeStatusArray<PE>::InsertState(	State 				i_ePosition,
236*cdf0e10cSrcweir 								DYN PeStatus<PE> &	let_drState )
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir 	csv_assert(aStati[i_ePosition] == 0);
239*cdf0e10cSrcweir 	aStati[i_ePosition] = &let_drState;
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir template <class PE>
243*cdf0e10cSrcweir PeStatusArray<PE>::~PeStatusArray()
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir 	int i_max = PE::size_of_states;
246*cdf0e10cSrcweir 	for (int i = 0; i < i_max; i++)
247*cdf0e10cSrcweir 	{
248*cdf0e10cSrcweir 		delete aStati[i];
249*cdf0e10cSrcweir 	}  // end for
250*cdf0e10cSrcweir }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir template <class PE>
253*cdf0e10cSrcweir inline const PeStatus<PE> &
254*cdf0e10cSrcweir PeStatusArray<PE>::operator[]( State i_ePosition ) const
255*cdf0e10cSrcweir {
256*cdf0e10cSrcweir 	csv_assert( uintt(i_ePosition) < PE::size_of_states );
257*cdf0e10cSrcweir 	csv_assert( aStati[i_ePosition] != 0 );
258*cdf0e10cSrcweir 	return *aStati[i_ePosition];
259*cdf0e10cSrcweir }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir template <class PE>
262*cdf0e10cSrcweir inline void
263*cdf0e10cSrcweir PeStatusArray<PE>::SetCur( State i_eCurState )
264*cdf0e10cSrcweir {
265*cdf0e10cSrcweir 	eState = i_eCurState;
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir template <class PE>
270*cdf0e10cSrcweir const PeStatus<PE> &
271*cdf0e10cSrcweir PeStatusArray<PE>::Cur() const
272*cdf0e10cSrcweir {
273*cdf0e10cSrcweir 	return (*this)[eState];
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir #define SEMPARSE_CREATE_STATUS(penv, state, default_function) \
277*cdf0e10cSrcweir 	pStati->InsertState( state, \
278*cdf0e10cSrcweir 						*new PeStatus<penv>( \
279*cdf0e10cSrcweir 		*this, \
280*cdf0e10cSrcweir 		sizeof( stateT_##state ) / sizeof (INT16), \
281*cdf0e10cSrcweir 		stateF_##state, \
282*cdf0e10cSrcweir 		stateT_##state, \
283*cdf0e10cSrcweir 		&penv::default_function ) )
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir #endif
287*cdf0e10cSrcweir 
288