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