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 <s2_luidl/distrib.hxx>
24 
25 
26 // NOT FULLY DECLARED SERVICES
27 #include <ary/doc/d_oldidldocu.hxx>
28 #include <parser/parserinfo.hxx>
29 #include <s2_luidl/tkp_uidl.hxx>
30 #include <s2_luidl/parsenv2.hxx>
31 #include <s2_luidl/tk_punct.hxx>
32 #include <s2_dsapi/docu_pe2.hxx>
33 #include <adc_cl.hxx>
34 #include <x_parse2.hxx>
35 
36 
37 
38 const uintt	C_nNO_TRY = uintt(-1);
39 
40 
41 namespace csi
42 {
43 namespace uidl
44 {
45 
46 TokenDistributor::TokenDistributor( ary::Repository & io_rRepository,
47 							        ParserInfo &      io_rParserInfo )
48 	:	pTokenSource(0),
49 		aDocumentation(io_rParserInfo),
50 		aProcessingData( io_rRepository, aDocumentation, io_rParserInfo )
51 {
52 }
53 
54 TokenDistributor::~TokenDistributor()
55 {
56 }
57 
58 void
59 TokenDistributor::TradeToken()
60 {
61     bool bGoon = true;
62 	while (bGoon AND NOT aProcessingData.NextTokenExists())
63 	{
64 		bGoon = pTokenSource->GetNextToken();
65 	}
66     if (bGoon)
67     	aProcessingData.ProcessCurToken();
68 }
69 
70 TokenDistributor::ProcessingData::ProcessingData(
71                                         ary::Repository & 	io_rRepository,
72 										Documentation &	        i_rDocuProcessor,
73 										ParserInfo &            io_rParserInfo )
74 	:	// aEnvironments
75 		// aTokenQueue
76 		// itCurToken
77 		// aCurResult
78 		nTryCount(0),
79 		bFinished(false),
80 		rRepository(io_rRepository),
81 		rParserInfo(io_rParserInfo),
82 		pDocuProcessor(&i_rDocuProcessor),
83 		bPublishedRecentlyOn(false)
84 {
85 	itCurToken = aTokenQueue.end();
86 }
87 
88 TokenDistributor::ProcessingData::~ProcessingData()
89 {
90 }
91 
92 void
93 TokenDistributor::ProcessingData::SetTopParseEnvironment( UnoIDL_PE & io_pTopParseEnvironment )
94 {
95 	csv::erase_container(aEnvironments);
96 	aEnvironments.push_back( EnvironmentInfo( &io_pTopParseEnvironment, 0 ) );
97 	io_pTopParseEnvironment.EstablishContacts(0,rRepository,aCurResult);
98 }
99 
100 void
101 TokenDistributor::ProcessingData::Receive( DYN csi::uidl::Token & let_drToken )
102 {
103 	aTokenQueue.push_back( &let_drToken );
104 	itCurToken = aTokenQueue.end()-1;
105 }
106 
107 void
108 TokenDistributor::ProcessingData::Increment_CurLine()
109 {
110     rParserInfo.Increment_CurLine();
111 }
112 
113 void
114 TokenDistributor::ProcessingData::ProcessCurToken()
115 {
116 
117 if (DEBUG_ShowTokens())
118 {
119 	Cout() << (*itCurToken)->Text() << Endl();
120 }
121 
122 	aCurResult.reset();
123 
124 	CurEnvironment().ProcessToken( CurToken() );
125 	AcknowledgeResult();
126 }
127 
128 
129 UnoIDL_PE &
130 TokenDistributor::ProcessingData::CurEnvironment() const
131 {
132 	csv_assert(aEnvironments.size() > 0);
133 	csv_assert(aEnvironments.back().first != 0);
134 
135 	return *aEnvironments.back().first;
136 }
137 
138 bool
139 TokenDistributor::ProcessingData::NextTokenExists() const
140 {
141 	return itCurToken != aTokenQueue.end();
142 }
143 
144 void
145 TokenDistributor::ProcessingData::AcknowledgeResult()
146 {
147 	if (aCurResult.eDone == done)
148 		++itCurToken;
149 
150 	switch ( aCurResult.eStackAction )
151 	{
152 		case stay:
153 				if (aCurResult.eDone != done)
154                 {
155 					csv_assert(false);
156                 }
157 				break;
158 		case push_sure:
159 				CurEnv().Leave(push_sure);
160 				aEnvironments.push_back( EnvironmentInfo(&PushEnv(), C_nNO_TRY) );
161 				PushEnv().Enter(push_sure);
162 				PushEnv().SetDocu(pDocuProcessor->ReleaseLastParsedDocu());
163 				if (bPublishedRecentlyOn)
164 				{
165 				    PushEnv().SetPublished();
166 				    bPublishedRecentlyOn = false;
167 				}
168 
169 				break;
170 		case push_try:
171 				Cout() << "TestInfo: Environment tried." << Endl();
172 				CurEnv().Leave(push_try);
173 				aEnvironments.push_back( EnvironmentInfo(&PushEnv(), CurTokenPosition()) );
174 				nTryCount++;
175 				PushEnv().Enter(push_try);
176 				break;
177 		case pop_success:
178 				CurEnv().Leave(pop_success);
179 				if ( CurEnv_TriedTokenPosition() > 0 )
180 					DecrementTryCount();
181 				aEnvironments.pop_back();
182 				CurEnv().Enter(pop_success);
183 				break;
184 		case pop_failure:
185 		{
186 				CurEnv().Leave(pop_failure);
187 				if (aCurResult.eDone == done)
188 				{
189                 	csv_assert(false);
190                 }
191 
192 				if ( CurEnv_TriedTokenPosition() == C_nNO_TRY )
193 					throw X_AutodocParser( X_AutodocParser::x_UnexpectedToken, (*itCurToken)->Text() );
194 
195 				itCurToken = aTokenQueue.begin() + CurEnv_TriedTokenPosition();
196 				DecrementTryCount();
197 				aEnvironments.pop_back();
198 				CurEnv().Enter(pop_failure);
199 		}		break;
200 		default:
201 			csv_assert(false);
202 	}	// end switch(aCurResult.eStackAction)
203 }
204 
205 void
206 TokenDistributor::ProcessingData::DecrementTryCount()
207 {
208 	nTryCount--;
209 	if (nTryCount == 0)
210 	{
211 		aTokenQueue.erase(aTokenQueue.begin(), itCurToken);
212 		itCurToken = aTokenQueue.begin();
213 	}
214 }
215 
216 TokenDistributor::
217 Documentation::Documentation(ParserInfo & io_rParserInfo)
218 	:	pDocuParseEnv(new csi::dsapi::SapiDocu_PE(io_rParserInfo)),
219 	    rParserInfo(io_rParserInfo),
220 		pMostRecentDocu(0),
221 		bIsPassedFirstDocu(false)
222 {
223 }
224 
225 TokenDistributor::
226 Documentation::~Documentation()
227 {
228 }
229 
230 void
231 TokenDistributor::
232 Documentation::Receive( DYN csi::dsapi::Token & let_drToken )
233 {
234 	csv_assert(pDocuParseEnv);
235 
236 	pDocuParseEnv->ProcessToken(let_drToken);
237 	if ( pDocuParseEnv->IsComplete() )
238 	{
239 		pMostRecentDocu = pDocuParseEnv->ReleaseJustParsedDocu();
240 		if (NOT bIsPassedFirstDocu)
241 		{
242             pMostRecentDocu = 0;			// Deletes the most recent docu.
243 			bIsPassedFirstDocu = true;
244 		}
245 	}
246 }
247 
248 void
249 TokenDistributor::
250 Documentation::Increment_CurLine()
251 {
252     rParserInfo.Increment_CurLine();
253 }
254 
255 
256 }   // namespace uidl
257 }   // namespace csi
258 
259