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 #include <precomp.h>
29*cdf0e10cSrcweir #include "cx_c_pp.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES
34*cdf0e10cSrcweir #include "c_dealer.hxx"
35*cdf0e10cSrcweir #include <tokens/parseinc.hxx>
36*cdf0e10cSrcweir #include <x_parse.hxx>
37*cdf0e10cSrcweir #include "all_toks.hxx"
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir namespace cpp
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir Context_Preprocessor::Context_Preprocessor( TkpContext & i_rFollowUpContext )
44*cdf0e10cSrcweir     : 	Cx_Base(&i_rFollowUpContext),
45*cdf0e10cSrcweir         pContext_Parent(&i_rFollowUpContext),
46*cdf0e10cSrcweir         pContext_PP_MacroParams( 0 ),
47*cdf0e10cSrcweir         pContext_PP_Definition( new Context_PP_Definition(i_rFollowUpContext) )
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir     pContext_PP_MacroParams = new Context_PP_MacroParams(*pContext_PP_Definition);
50*cdf0e10cSrcweir }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir void
53*cdf0e10cSrcweir Context_Preprocessor::ReadCharChain( CharacterSource &	io_rText )
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir 	jumpOverWhite( io_rText );
56*cdf0e10cSrcweir 	jumpToWhite( io_rText );
57*cdf0e10cSrcweir     const char * sPP_Keyword = io_rText.CutToken();
58*cdf0e10cSrcweir     if ( strcmp(sPP_Keyword, "define") == 0 )
59*cdf0e10cSrcweir         ReadDefine(io_rText);
60*cdf0e10cSrcweir     else
61*cdf0e10cSrcweir         ReadDefault(io_rText);
62*cdf0e10cSrcweir }
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir void
65*cdf0e10cSrcweir Context_Preprocessor::AssignDealer( Distributor & o_rDealer )
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir     Cx_Base::AssignDealer(o_rDealer);
68*cdf0e10cSrcweir     pContext_PP_MacroParams->AssignDealer(o_rDealer);
69*cdf0e10cSrcweir     pContext_PP_Definition->AssignDealer(o_rDealer);
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir void
73*cdf0e10cSrcweir Context_Preprocessor::ReadDefault( CharacterSource & io_rText )
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir     int o_rCount_BackslashedLineBreaks = 0;
76*cdf0e10cSrcweir 	jumpToEol(io_rText,o_rCount_BackslashedLineBreaks);
77*cdf0e10cSrcweir     for ( ; o_rCount_BackslashedLineBreaks > 0; --o_rCount_BackslashedLineBreaks )
78*cdf0e10cSrcweir         Dealer().Deal_Eol();
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	if (io_rText.CurChar() != NULCH)
81*cdf0e10cSrcweir 		jumpOverEol(io_rText);
82*cdf0e10cSrcweir 	io_rText.CutToken();
83*cdf0e10cSrcweir     Dealer().Deal_Eol();
84*cdf0e10cSrcweir 	SetNewToken(0);
85*cdf0e10cSrcweir     SetFollowUpContext( pContext_Parent );
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir void
89*cdf0e10cSrcweir Context_Preprocessor::ReadDefine( CharacterSource &	io_rText )
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir 	jumpOverWhite( io_rText );
92*cdf0e10cSrcweir     io_rText.CutToken();
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	char cNext = '\0';
95*cdf0e10cSrcweir 	for ( cNext = io_rText.CurChar();
96*cdf0e10cSrcweir 		  static_cast<UINT8>(cNext) > 32 AND cNext != '(';
97*cdf0e10cSrcweir 		  cNext = io_rText.MoveOn() )
98*cdf0e10cSrcweir 	{ }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     bool bMacro = cNext == '(';
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     if ( NOT bMacro )
103*cdf0e10cSrcweir     {
104*cdf0e10cSrcweir     	SetNewToken( new Tok_DefineName(io_rText.CutToken()) );
105*cdf0e10cSrcweir         SetFollowUpContext( pContext_PP_Definition.Ptr() );
106*cdf0e10cSrcweir     }
107*cdf0e10cSrcweir     else
108*cdf0e10cSrcweir     {
109*cdf0e10cSrcweir     	SetNewToken( new Tok_MacroName(io_rText.CutToken()) );
110*cdf0e10cSrcweir         io_rText.MoveOn();
111*cdf0e10cSrcweir         io_rText.CutToken();
112*cdf0e10cSrcweir         SetFollowUpContext( pContext_PP_MacroParams.Ptr() );
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir Context_PP_MacroParams::Context_PP_MacroParams( Cx_Base & i_rFollowUpContext )
118*cdf0e10cSrcweir     : 	Cx_Base(&i_rFollowUpContext),
119*cdf0e10cSrcweir         pContext_PP_Definition(&i_rFollowUpContext)
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir void
124*cdf0e10cSrcweir Context_PP_MacroParams::ReadCharChain( CharacterSource & io_rText )
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir     uintt nLen;
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	jumpOverWhite( io_rText );
129*cdf0e10cSrcweir     // KORR_FUTURE Handling line breaks within macro parameters:
130*cdf0e10cSrcweir 	char cSeparator = jumpTo( io_rText, ',', ')' );
131*cdf0e10cSrcweir     csv_assert( cSeparator != 0 );
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     static char cBuf[500];
134*cdf0e10cSrcweir     // KORR_FUTURE, make it still safer, here:
135*cdf0e10cSrcweir     strcpy( cBuf, io_rText.CutToken() );    // SAFE STRCPY (#100211# - checked)
136*cdf0e10cSrcweir     for ( nLen = strlen(cBuf);
137*cdf0e10cSrcweir           nLen > 0 AND cBuf[nLen-1] < 33;
138*cdf0e10cSrcweir           --nLen )
139*cdf0e10cSrcweir     { }
140*cdf0e10cSrcweir     cBuf[nLen] = '\0';
141*cdf0e10cSrcweir 	SetNewToken( new Tok_MacroParameter(cBuf) );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     io_rText.MoveOn();
144*cdf0e10cSrcweir     io_rText.CutToken();
145*cdf0e10cSrcweir     if ( cSeparator == ',')
146*cdf0e10cSrcweir         SetFollowUpContext( this );
147*cdf0e10cSrcweir     else    // Must be ')'
148*cdf0e10cSrcweir         SetFollowUpContext( pContext_PP_Definition );
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir Context_PP_Definition::Context_PP_Definition( TkpContext & i_rFollowUpContext )
152*cdf0e10cSrcweir     : 	Cx_Base(&i_rFollowUpContext),
153*cdf0e10cSrcweir         pContext_Parent(&i_rFollowUpContext)
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir void
158*cdf0e10cSrcweir Context_PP_Definition::ReadCharChain( CharacterSource & io_rText )
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir     int o_rCount_BackslashedLineBreaks = 0;
161*cdf0e10cSrcweir 	jumpToEol(io_rText,o_rCount_BackslashedLineBreaks);
162*cdf0e10cSrcweir     for ( ; o_rCount_BackslashedLineBreaks > 0; --o_rCount_BackslashedLineBreaks )
163*cdf0e10cSrcweir         Dealer().Deal_Eol();
164*cdf0e10cSrcweir 	SetNewToken( new Tok_PreProDefinition(io_rText.CutToken()) );
165*cdf0e10cSrcweir 	if (io_rText.CurChar() != NULCH)
166*cdf0e10cSrcweir 		jumpOverEol(io_rText);
167*cdf0e10cSrcweir     Dealer().Deal_Eol();
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir }   // namespace cpp
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 
181