1*78bc99aaSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*78bc99aaSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*78bc99aaSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*78bc99aaSAndrew Rist * distributed with this work for additional information 6*78bc99aaSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*78bc99aaSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*78bc99aaSAndrew Rist * "License"); you may not use this file except in compliance 9*78bc99aaSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*78bc99aaSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*78bc99aaSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*78bc99aaSAndrew Rist * software distributed under the License is distributed on an 15*78bc99aaSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*78bc99aaSAndrew Rist * KIND, either express or implied. See the License for the 17*78bc99aaSAndrew Rist * specific language governing permissions and limitations 18*78bc99aaSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*78bc99aaSAndrew Rist *************************************************************/ 21cdf0e10cSrcweir 22cdf0e10cSrcweir #include <precomp.h> 23cdf0e10cSrcweir #include "cx_c_pp.hxx" 24cdf0e10cSrcweir 25cdf0e10cSrcweir 26cdf0e10cSrcweir 27cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES 28cdf0e10cSrcweir #include "c_dealer.hxx" 29cdf0e10cSrcweir #include <tokens/parseinc.hxx> 30cdf0e10cSrcweir #include <x_parse.hxx> 31cdf0e10cSrcweir #include "all_toks.hxx" 32cdf0e10cSrcweir 33cdf0e10cSrcweir 34cdf0e10cSrcweir namespace cpp 35cdf0e10cSrcweir { 36cdf0e10cSrcweir 37cdf0e10cSrcweir Context_Preprocessor::Context_Preprocessor( TkpContext & i_rFollowUpContext ) 38cdf0e10cSrcweir : Cx_Base(&i_rFollowUpContext), 39cdf0e10cSrcweir pContext_Parent(&i_rFollowUpContext), 40cdf0e10cSrcweir pContext_PP_MacroParams( 0 ), 41cdf0e10cSrcweir pContext_PP_Definition( new Context_PP_Definition(i_rFollowUpContext) ) 42cdf0e10cSrcweir { 43cdf0e10cSrcweir pContext_PP_MacroParams = new Context_PP_MacroParams(*pContext_PP_Definition); 44cdf0e10cSrcweir } 45cdf0e10cSrcweir 46cdf0e10cSrcweir void 47cdf0e10cSrcweir Context_Preprocessor::ReadCharChain( CharacterSource & io_rText ) 48cdf0e10cSrcweir { 49cdf0e10cSrcweir jumpOverWhite( io_rText ); 50cdf0e10cSrcweir jumpToWhite( io_rText ); 51cdf0e10cSrcweir const char * sPP_Keyword = io_rText.CutToken(); 52cdf0e10cSrcweir if ( strcmp(sPP_Keyword, "define") == 0 ) 53cdf0e10cSrcweir ReadDefine(io_rText); 54cdf0e10cSrcweir else 55cdf0e10cSrcweir ReadDefault(io_rText); 56cdf0e10cSrcweir } 57cdf0e10cSrcweir 58cdf0e10cSrcweir void 59cdf0e10cSrcweir Context_Preprocessor::AssignDealer( Distributor & o_rDealer ) 60cdf0e10cSrcweir { 61cdf0e10cSrcweir Cx_Base::AssignDealer(o_rDealer); 62cdf0e10cSrcweir pContext_PP_MacroParams->AssignDealer(o_rDealer); 63cdf0e10cSrcweir pContext_PP_Definition->AssignDealer(o_rDealer); 64cdf0e10cSrcweir } 65cdf0e10cSrcweir 66cdf0e10cSrcweir void 67cdf0e10cSrcweir Context_Preprocessor::ReadDefault( CharacterSource & io_rText ) 68cdf0e10cSrcweir { 69cdf0e10cSrcweir int o_rCount_BackslashedLineBreaks = 0; 70cdf0e10cSrcweir jumpToEol(io_rText,o_rCount_BackslashedLineBreaks); 71cdf0e10cSrcweir for ( ; o_rCount_BackslashedLineBreaks > 0; --o_rCount_BackslashedLineBreaks ) 72cdf0e10cSrcweir Dealer().Deal_Eol(); 73cdf0e10cSrcweir 74cdf0e10cSrcweir if (io_rText.CurChar() != NULCH) 75cdf0e10cSrcweir jumpOverEol(io_rText); 76cdf0e10cSrcweir io_rText.CutToken(); 77cdf0e10cSrcweir Dealer().Deal_Eol(); 78cdf0e10cSrcweir SetNewToken(0); 79cdf0e10cSrcweir SetFollowUpContext( pContext_Parent ); 80cdf0e10cSrcweir } 81cdf0e10cSrcweir 82cdf0e10cSrcweir void 83cdf0e10cSrcweir Context_Preprocessor::ReadDefine( CharacterSource & io_rText ) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir jumpOverWhite( io_rText ); 86cdf0e10cSrcweir io_rText.CutToken(); 87cdf0e10cSrcweir 88cdf0e10cSrcweir char cNext = '\0'; 89cdf0e10cSrcweir for ( cNext = io_rText.CurChar(); 90cdf0e10cSrcweir static_cast<UINT8>(cNext) > 32 AND cNext != '('; 91cdf0e10cSrcweir cNext = io_rText.MoveOn() ) 92cdf0e10cSrcweir { } 93cdf0e10cSrcweir 94cdf0e10cSrcweir bool bMacro = cNext == '('; 95cdf0e10cSrcweir 96cdf0e10cSrcweir if ( NOT bMacro ) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir SetNewToken( new Tok_DefineName(io_rText.CutToken()) ); 99cdf0e10cSrcweir SetFollowUpContext( pContext_PP_Definition.Ptr() ); 100cdf0e10cSrcweir } 101cdf0e10cSrcweir else 102cdf0e10cSrcweir { 103cdf0e10cSrcweir SetNewToken( new Tok_MacroName(io_rText.CutToken()) ); 104cdf0e10cSrcweir io_rText.MoveOn(); 105cdf0e10cSrcweir io_rText.CutToken(); 106cdf0e10cSrcweir SetFollowUpContext( pContext_PP_MacroParams.Ptr() ); 107cdf0e10cSrcweir } 108cdf0e10cSrcweir } 109cdf0e10cSrcweir 110cdf0e10cSrcweir 111cdf0e10cSrcweir Context_PP_MacroParams::Context_PP_MacroParams( Cx_Base & i_rFollowUpContext ) 112cdf0e10cSrcweir : Cx_Base(&i_rFollowUpContext), 113cdf0e10cSrcweir pContext_PP_Definition(&i_rFollowUpContext) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir void 118cdf0e10cSrcweir Context_PP_MacroParams::ReadCharChain( CharacterSource & io_rText ) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir uintt nLen; 121cdf0e10cSrcweir 122cdf0e10cSrcweir jumpOverWhite( io_rText ); 123cdf0e10cSrcweir // KORR_FUTURE Handling line breaks within macro parameters: 124cdf0e10cSrcweir char cSeparator = jumpTo( io_rText, ',', ')' ); 125cdf0e10cSrcweir csv_assert( cSeparator != 0 ); 126cdf0e10cSrcweir 127cdf0e10cSrcweir static char cBuf[500]; 128cdf0e10cSrcweir // KORR_FUTURE, make it still safer, here: 129cdf0e10cSrcweir strcpy( cBuf, io_rText.CutToken() ); // SAFE STRCPY (#100211# - checked) 130cdf0e10cSrcweir for ( nLen = strlen(cBuf); 131cdf0e10cSrcweir nLen > 0 AND cBuf[nLen-1] < 33; 132cdf0e10cSrcweir --nLen ) 133cdf0e10cSrcweir { } 134cdf0e10cSrcweir cBuf[nLen] = '\0'; 135cdf0e10cSrcweir SetNewToken( new Tok_MacroParameter(cBuf) ); 136cdf0e10cSrcweir 137cdf0e10cSrcweir io_rText.MoveOn(); 138cdf0e10cSrcweir io_rText.CutToken(); 139cdf0e10cSrcweir if ( cSeparator == ',') 140cdf0e10cSrcweir SetFollowUpContext( this ); 141cdf0e10cSrcweir else // Must be ')' 142cdf0e10cSrcweir SetFollowUpContext( pContext_PP_Definition ); 143cdf0e10cSrcweir } 144cdf0e10cSrcweir 145cdf0e10cSrcweir Context_PP_Definition::Context_PP_Definition( TkpContext & i_rFollowUpContext ) 146cdf0e10cSrcweir : Cx_Base(&i_rFollowUpContext), 147cdf0e10cSrcweir pContext_Parent(&i_rFollowUpContext) 148cdf0e10cSrcweir { 149cdf0e10cSrcweir } 150cdf0e10cSrcweir 151cdf0e10cSrcweir void 152cdf0e10cSrcweir Context_PP_Definition::ReadCharChain( CharacterSource & io_rText ) 153cdf0e10cSrcweir { 154cdf0e10cSrcweir int o_rCount_BackslashedLineBreaks = 0; 155cdf0e10cSrcweir jumpToEol(io_rText,o_rCount_BackslashedLineBreaks); 156cdf0e10cSrcweir for ( ; o_rCount_BackslashedLineBreaks > 0; --o_rCount_BackslashedLineBreaks ) 157cdf0e10cSrcweir Dealer().Deal_Eol(); 158cdf0e10cSrcweir SetNewToken( new Tok_PreProDefinition(io_rText.CutToken()) ); 159cdf0e10cSrcweir if (io_rText.CurChar() != NULCH) 160cdf0e10cSrcweir jumpOverEol(io_rText); 161cdf0e10cSrcweir Dealer().Deal_Eol(); 162cdf0e10cSrcweir } 163cdf0e10cSrcweir 164cdf0e10cSrcweir 165cdf0e10cSrcweir } // namespace cpp 166cdf0e10cSrcweir 167cdf0e10cSrcweir 168cdf0e10cSrcweir 169cdf0e10cSrcweir 170cdf0e10cSrcweir 171cdf0e10cSrcweir 172cdf0e10cSrcweir 173cdf0e10cSrcweir 174cdf0e10cSrcweir 175