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