1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <precomp.h>
29 #include <s2_luidl/distrib.hxx>
30 
31 
32 // NOT FULLY DECLARED SERVICES
33 #include <ary/doc/d_oldidldocu.hxx>
34 #include <parser/parserinfo.hxx>
35 #include <s2_luidl/tkp_uidl.hxx>
36 #include <s2_luidl/parsenv2.hxx>
37 #include <s2_luidl/tk_punct.hxx>
38 #include <s2_dsapi/docu_pe2.hxx>
39 #include <adc_cl.hxx>
40 #include <x_parse2.hxx>
41 
42 
43 
44 const uintt	C_nNO_TRY = uintt(-1);
45 
46 
47 namespace csi
48 {
49 namespace uidl
50 {
51 
52 TokenDistributor::TokenDistributor( ary::Repository & io_rRepository,
53 							        ParserInfo &      io_rParserInfo )
54 	:	pTokenSource(0),
55 		aDocumentation(io_rParserInfo),
56 		aProcessingData( io_rRepository, aDocumentation, io_rParserInfo )
57 {
58 }
59 
60 TokenDistributor::~TokenDistributor()
61 {
62 }
63 
64 void
65 TokenDistributor::TradeToken()
66 {
67     bool bGoon = true;
68 	while (bGoon AND NOT aProcessingData.NextTokenExists())
69 	{
70 		bGoon = pTokenSource->GetNextToken();
71 	}
72     if (bGoon)
73     	aProcessingData.ProcessCurToken();
74 }
75 
76 TokenDistributor::ProcessingData::ProcessingData(
77                                         ary::Repository & 	io_rRepository,
78 										Documentation &	        i_rDocuProcessor,
79 										ParserInfo &            io_rParserInfo )
80 	:	// aEnvironments
81 		// aTokenQueue
82 		// itCurToken
83 		// aCurResult
84 		nTryCount(0),
85 		bFinished(false),
86 		rRepository(io_rRepository),
87 		rParserInfo(io_rParserInfo),
88 		pDocuProcessor(&i_rDocuProcessor),
89 		bPublishedRecentlyOn(false)
90 {
91 	itCurToken = aTokenQueue.end();
92 }
93 
94 TokenDistributor::ProcessingData::~ProcessingData()
95 {
96 }
97 
98 void
99 TokenDistributor::ProcessingData::SetTopParseEnvironment( UnoIDL_PE & io_pTopParseEnvironment )
100 {
101 	csv::erase_container(aEnvironments);
102 	aEnvironments.push_back( EnvironmentInfo( &io_pTopParseEnvironment, 0 ) );
103 	io_pTopParseEnvironment.EstablishContacts(0,rRepository,aCurResult);
104 }
105 
106 void
107 TokenDistributor::ProcessingData::Receive( DYN csi::uidl::Token & let_drToken )
108 {
109 	aTokenQueue.push_back( &let_drToken );
110 	itCurToken = aTokenQueue.end()-1;
111 }
112 
113 void
114 TokenDistributor::ProcessingData::Increment_CurLine()
115 {
116     rParserInfo.Increment_CurLine();
117 }
118 
119 void
120 TokenDistributor::ProcessingData::ProcessCurToken()
121 {
122 
123 if (DEBUG_ShowTokens())
124 {
125 	Cout() << (*itCurToken)->Text() << Endl();
126 }
127 
128 	aCurResult.reset();
129 
130 	CurEnvironment().ProcessToken( CurToken() );
131 	AcknowledgeResult();
132 }
133 
134 
135 UnoIDL_PE &
136 TokenDistributor::ProcessingData::CurEnvironment() const
137 {
138 	csv_assert(aEnvironments.size() > 0);
139 	csv_assert(aEnvironments.back().first != 0);
140 
141 	return *aEnvironments.back().first;
142 }
143 
144 bool
145 TokenDistributor::ProcessingData::NextTokenExists() const
146 {
147 	return itCurToken != aTokenQueue.end();
148 }
149 
150 void
151 TokenDistributor::ProcessingData::AcknowledgeResult()
152 {
153 	if (aCurResult.eDone == done)
154 		++itCurToken;
155 
156 	switch ( aCurResult.eStackAction )
157 	{
158 		case stay:
159 				if (aCurResult.eDone != done)
160                 {
161 					csv_assert(false);
162                 }
163 				break;
164 		case push_sure:
165 				CurEnv().Leave(push_sure);
166 				aEnvironments.push_back( EnvironmentInfo(&PushEnv(), C_nNO_TRY) );
167 				PushEnv().Enter(push_sure);
168 				PushEnv().SetDocu(pDocuProcessor->ReleaseLastParsedDocu());
169 				if (bPublishedRecentlyOn)
170 				{
171 				    PushEnv().SetPublished();
172 				    bPublishedRecentlyOn = false;
173 				}
174 
175 				break;
176 		case push_try:
177 				Cout() << "TestInfo: Environment tried." << Endl();
178 				CurEnv().Leave(push_try);
179 				aEnvironments.push_back( EnvironmentInfo(&PushEnv(), CurTokenPosition()) );
180 				nTryCount++;
181 				PushEnv().Enter(push_try);
182 				break;
183 		case pop_success:
184 				CurEnv().Leave(pop_success);
185 				if ( CurEnv_TriedTokenPosition() > 0 )
186 					DecrementTryCount();
187 				aEnvironments.pop_back();
188 				CurEnv().Enter(pop_success);
189 				break;
190 		case pop_failure:
191 		{
192 				CurEnv().Leave(pop_failure);
193 				if (aCurResult.eDone == done)
194 				{
195                 	csv_assert(false);
196                 }
197 
198 				if ( CurEnv_TriedTokenPosition() == C_nNO_TRY )
199 					throw X_AutodocParser( X_AutodocParser::x_UnexpectedToken, (*itCurToken)->Text() );
200 
201 				itCurToken = aTokenQueue.begin() + CurEnv_TriedTokenPosition();
202 				DecrementTryCount();
203 				aEnvironments.pop_back();
204 				CurEnv().Enter(pop_failure);
205 		}		break;
206 		default:
207 			csv_assert(false);
208 	}	// end switch(aCurResult.eStackAction)
209 }
210 
211 void
212 TokenDistributor::ProcessingData::DecrementTryCount()
213 {
214 	nTryCount--;
215 	if (nTryCount == 0)
216 	{
217 		aTokenQueue.erase(aTokenQueue.begin(), itCurToken);
218 		itCurToken = aTokenQueue.begin();
219 	}
220 }
221 
222 TokenDistributor::
223 Documentation::Documentation(ParserInfo & io_rParserInfo)
224 	:	pDocuParseEnv(new csi::dsapi::SapiDocu_PE(io_rParserInfo)),
225 	    rParserInfo(io_rParserInfo),
226 		pMostRecentDocu(0),
227 		bIsPassedFirstDocu(false)
228 {
229 }
230 
231 TokenDistributor::
232 Documentation::~Documentation()
233 {
234 }
235 
236 void
237 TokenDistributor::
238 Documentation::Receive( DYN csi::dsapi::Token & let_drToken )
239 {
240 	csv_assert(pDocuParseEnv);
241 
242 	pDocuParseEnv->ProcessToken(let_drToken);
243 	if ( pDocuParseEnv->IsComplete() )
244 	{
245 		pMostRecentDocu = pDocuParseEnv->ReleaseJustParsedDocu();
246 		if (NOT bIsPassedFirstDocu)
247 		{
248             pMostRecentDocu = 0;			// Deletes the most recent docu.
249 			bIsPassedFirstDocu = true;
250 		}
251 	}
252 }
253 
254 void
255 TokenDistributor::
256 Documentation::Increment_CurLine()
257 {
258     rParserInfo.Increment_CurLine();
259 }
260 
261 
262 }   // namespace uidl
263 }   // namespace csi
264 
265