merge the formfield patch from ooo-build
[ooovba.git] / autodoc / source / parser / cpp / preproc.cxx
blobee5e9ca817e52a6b6bf114115ca2fa53808ec8b2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: preproc.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <precomp.h>
32 #include "preproc.hxx"
35 // NOT FULLY DEFINED SERVICES
36 #include <cosv/tpl/tpltools.hxx>
37 #include "all_toks.hxx"
38 #include "defdescr.hxx"
39 #include <tools/tkpchars.hxx>
40 #include "c_rcode.hxx"
43 namespace cpp
47 PreProcessor::F_TOKENPROC PreProcessor::aTokProcs[PreProcessor::state_MAX] =
49 &PreProcessor::On_plain,
50 &PreProcessor::On_expect_macro_bracket_left,
51 &PreProcessor::On_expect_macro_param
55 PreProcessor::PreProcessor()
56 : pCppExplorer(0),
57 pSourceText(0),
58 pCurValidDefines(0),
59 // aTokens,
60 eState(plain),
61 pCurMacro(0),
62 dpCurMacroName(0),
63 // aCurMacroParams,
64 aCurParamText(60000),
65 nBracketInParameterCounter(0)
66 // aBlockedMacroNames
70 PreProcessor::~PreProcessor()
74 void
75 PreProcessor::AssignPartners( CodeExplorer & o_rCodeExplorer,
76 CharacterSource & o_rCharSource,
77 const MacroMap & i_rCurValidDefines )
79 pCppExplorer = &o_rCodeExplorer;
80 pSourceText = &o_rCharSource;
81 pCurValidDefines = &i_rCurValidDefines;
84 void
85 PreProcessor::Process_Token( cpp::Token & let_drToken )
87 csv_assert(pCppExplorer != 0); // Implies pSourceText and pCurValidDefines.
89 (this->*aTokProcs[eState])(let_drToken);
92 void
93 PreProcessor::On_plain( cpp::Token & let_drToken )
95 if ( let_drToken.TypeId() == Tid_Identifier )
97 if (CheckForDefine(let_drToken))
98 return;
101 pCppExplorer->Process_Token(let_drToken);
104 void
105 PreProcessor::On_expect_macro_bracket_left( cpp::Token & let_drToken )
107 if ( let_drToken.TypeId() == Tid_Bracket_Left )
109 aCurParamText.seekp(0);
110 eState = expect_macro_param;
112 else
114 pCppExplorer->Process_Token(*dpCurMacroName);
115 dpCurMacroName = 0;
116 pCppExplorer->Process_Token(let_drToken);
117 eState = plain;
121 void
122 PreProcessor::On_expect_macro_param( cpp::Token & let_drToken )
124 if ( let_drToken.TypeId() == Tid_Bracket_Left )
125 nBracketInParameterCounter++;
126 else if ( let_drToken.TypeId() == Tid_Bracket_Right )
128 if ( nBracketInParameterCounter > 0 )
129 nBracketInParameterCounter--;
130 else
132 if ( NOT csv::no_str(aCurParamText.c_str()) )
134 aCurMacroParams.push_back( String(aCurParamText.c_str()) );
136 csv_assert( aCurMacroParams.size() == pCurMacro->ParamCount() );
138 InterpretMacro();
139 eState = plain;
140 return;
143 else if ( let_drToken.TypeId() == Tid_Comma AND nBracketInParameterCounter == 0 )
145 aCurMacroParams.push_back( String (aCurParamText.c_str()) );
146 aCurParamText.seekp(0);
147 return;
150 // KORR_FUTURE:
151 // If in future whitespace is parsed also, that should match exactly and the
152 // safety spaces, " ", here should be removed.
153 aCurParamText << let_drToken.Text() << " ";
156 bool
157 PreProcessor::CheckForDefine( cpp::Token & let_drToken )
159 String sTokenText(let_drToken.Text());
160 pCurMacro = csv::value_from_map( *pCurValidDefines, sTokenText );
161 if (pCurMacro == 0 )
162 return false;
163 for ( StringVector::const_iterator it = aBlockedMacroNames.begin();
164 it != aBlockedMacroNames.end();
165 ++it )
167 if ( strcmp( (*it).c_str(), let_drToken.Text() ) == 0 )
168 return false;
171 if ( pCurMacro->DefineType() == DefineDescription::type_define )
173 delete &let_drToken;
175 aCurParamText.seekp(0);
176 pCurMacro->GetDefineText(aCurParamText);
178 if ( aCurParamText.tellp() > 1 )
179 pSourceText->InsertTextAtCurPos(aCurParamText.c_str());
181 else // ( pCurMacro->DefineType() == DefineDescription::type_macro )
183 dpCurMacroName = &let_drToken;
184 eState = expect_macro_bracket_left;
185 csv::erase_container( aCurMacroParams );
186 aCurParamText.seekp(0);
187 nBracketInParameterCounter = 0;
188 } // endif
190 return true;
193 void
194 PreProcessor::UnblockMacro( const char * i_sMacroName )
196 for ( StringVector::iterator it = aBlockedMacroNames.begin();
197 it != aBlockedMacroNames.end();
198 ++it )
200 if ( strcmp( (*it), i_sMacroName ) == 0 )
202 aBlockedMacroNames.erase(it);
203 break;
205 } /// end for
208 void
209 PreProcessor::InterpretMacro()
211 aCurParamText.seekp(0);
212 pCurMacro->GetMacroText(aCurParamText, aCurMacroParams);
214 if ( NOT csv::no_str(aCurParamText.c_str()) )
216 aCurParamText.seekp(-1, csv::cur);
217 aCurParamText << " #unblock-" << dpCurMacroName->Text() << " ";
219 pSourceText->InsertTextAtCurPos(aCurParamText.c_str());
220 String sCurMacroName(dpCurMacroName->Text());
221 aBlockedMacroNames.insert( aBlockedMacroNames.begin(), sCurMacroName );
224 delete dpCurMacroName;
225 dpCurMacroName = 0;
226 pCurMacro = 0;
227 csv::erase_container(aCurMacroParams);
228 aCurParamText.seekp(0);
232 } // end namespace cpp