1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: preproc.cxx,v $
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 ************************************************************************/
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"
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()
65 nBracketInParameterCounter(0)
70 PreProcessor::~PreProcessor()
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
;
85 PreProcessor::Process_Token( cpp::Token
& let_drToken
)
87 csv_assert(pCppExplorer
!= 0); // Implies pSourceText and pCurValidDefines.
89 (this->*aTokProcs
[eState
])(let_drToken
);
93 PreProcessor::On_plain( cpp::Token
& let_drToken
)
95 if ( let_drToken
.TypeId() == Tid_Identifier
)
97 if (CheckForDefine(let_drToken
))
101 pCppExplorer
->Process_Token(let_drToken
);
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
;
114 pCppExplorer
->Process_Token(*dpCurMacroName
);
116 pCppExplorer
->Process_Token(let_drToken
);
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
--;
132 if ( NOT
csv::no_str(aCurParamText
.c_str()) )
134 aCurMacroParams
.push_back( String(aCurParamText
.c_str()) );
136 csv_assert( aCurMacroParams
.size() == pCurMacro
->ParamCount() );
143 else if ( let_drToken
.TypeId() == Tid_Comma AND nBracketInParameterCounter
== 0 )
145 aCurMacroParams
.push_back( String (aCurParamText
.c_str()) );
146 aCurParamText
.seekp(0);
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() << " ";
157 PreProcessor::CheckForDefine( cpp::Token
& let_drToken
)
159 String
sTokenText(let_drToken
.Text());
160 pCurMacro
= csv::value_from_map( *pCurValidDefines
, sTokenText
);
163 for ( StringVector::const_iterator it
= aBlockedMacroNames
.begin();
164 it
!= aBlockedMacroNames
.end();
167 if ( strcmp( (*it
).c_str(), let_drToken
.Text() ) == 0 )
171 if ( pCurMacro
->DefineType() == DefineDescription::type_define
)
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;
194 PreProcessor::UnblockMacro( const char * i_sMacroName
)
196 for ( StringVector::iterator it
= aBlockedMacroNames
.begin();
197 it
!= aBlockedMacroNames
.end();
200 if ( strcmp( (*it
), i_sMacroName
) == 0 )
202 aBlockedMacroNames
.erase(it
);
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
;
227 csv::erase_container(aCurMacroParams
);
228 aCurParamText
.seekp(0);
232 } // end namespace cpp