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 "c_rcode.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir 
26cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES
27cdf0e10cSrcweir #include <ary/cpp/c_gate.hxx>
28cdf0e10cSrcweir #include <ary/cpp/c_namesp.hxx>
29cdf0e10cSrcweir // #include <ary/cpp/c_groups.hxx>
30cdf0e10cSrcweir #include <ary/loc/locp_le.hxx>
31cdf0e10cSrcweir #include "cpp_pe.hxx"
32cdf0e10cSrcweir #include <adc_cl.hxx>
33cdf0e10cSrcweir #include <x_parse.hxx>
34cdf0e10cSrcweir #include "pe_file.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir const uintt	C_nNO_TRY = uintt(-1);
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace cpp {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
CodeExplorer(ary::cpp::Gate & io_rAryGate)42cdf0e10cSrcweir CodeExplorer::CodeExplorer( ary::cpp::Gate & io_rAryGate )
43cdf0e10cSrcweir 	:   aGlobalParseContext(io_rAryGate),
44cdf0e10cSrcweir         // aEnvironments,
45cdf0e10cSrcweir         pPE_File(0),
46cdf0e10cSrcweir         pGate(&io_rAryGate),
47cdf0e10cSrcweir 		dpCurToken(0)
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	pPE_File = new PE_File( aGlobalParseContext );
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
~CodeExplorer()52cdf0e10cSrcweir CodeExplorer::~CodeExplorer()
53cdf0e10cSrcweir {
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir void
StartNewFile()57cdf0e10cSrcweir CodeExplorer::StartNewFile()
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 	csv::erase_container(aEnvironments);
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	aEnvironments.push_back( pPE_File.MutablePtr() );
62cdf0e10cSrcweir     pPE_File->Enter(push);
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir void
Process_Token(DYN cpp::Token & let_drToken)66cdf0e10cSrcweir CodeExplorer::Process_Token( DYN cpp::Token & let_drToken )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir if (DEBUG_ShowTokens())
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 	Cout() << let_drToken.Text() << Endl();
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 	dpCurToken = &let_drToken;
73cdf0e10cSrcweir 	aGlobalParseContext.ResetResult();
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	do {
76cdf0e10cSrcweir 		CurToken().Trigger( CurEnv() );
77cdf0e10cSrcweir 		AcknowledgeResult();
78cdf0e10cSrcweir 	} while ( dpCurToken );
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir void
AcknowledgeResult()82cdf0e10cSrcweir CodeExplorer::AcknowledgeResult()
83cdf0e10cSrcweir {
84cdf0e10cSrcweir 	if (CurResult().eDone == done)
85cdf0e10cSrcweir 		dpCurToken = 0;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	switch ( CurResult().eStackAction )
88cdf0e10cSrcweir 	{
89cdf0e10cSrcweir 		case stay:
90cdf0e10cSrcweir 				break;
91cdf0e10cSrcweir 		case push:
92cdf0e10cSrcweir 				CurEnv().Leave(push);
93cdf0e10cSrcweir 				aEnvironments.push_back( &PushEnv() );
94cdf0e10cSrcweir 				PushEnv().Enter(push);
95cdf0e10cSrcweir 				break;
96cdf0e10cSrcweir 		case pop_success:
97cdf0e10cSrcweir 				CurEnv().Leave(pop_success);
98cdf0e10cSrcweir 				aEnvironments.pop_back();
99cdf0e10cSrcweir 				CurEnv().Enter(pop_success);
100cdf0e10cSrcweir 				break;
101cdf0e10cSrcweir 		case pop_failure:
102cdf0e10cSrcweir 		{
103cdf0e10cSrcweir                 Cpp_PE * pRecover = 0;
104cdf0e10cSrcweir                 do {
105cdf0e10cSrcweir                     CurEnv().Leave(pop_failure);
106cdf0e10cSrcweir         			aEnvironments.pop_back();
107cdf0e10cSrcweir                     if ( aEnvironments.empty() )
108cdf0e10cSrcweir                         break;
109cdf0e10cSrcweir                     pRecover = CurEnv().Handle_ChildFailure();
110cdf0e10cSrcweir                 } while ( pRecover == 0 );
111cdf0e10cSrcweir                 if ( pRecover != 0 )
112cdf0e10cSrcweir                 {
113cdf0e10cSrcweir                     aEnvironments.push_back(pRecover);
114cdf0e10cSrcweir                     pRecover->Enter(push);
115cdf0e10cSrcweir                 }
116cdf0e10cSrcweir                 else
117cdf0e10cSrcweir                 {
118cdf0e10cSrcweir     				throw X_Parser( X_Parser::x_UnexpectedToken, CurToken().Text(), aGlobalParseContext.CurFileName(), aGlobalParseContext.LineCount() );
119cdf0e10cSrcweir                 }
120cdf0e10cSrcweir 		}		break;
121cdf0e10cSrcweir 		default:
122cdf0e10cSrcweir 			csv_assert(false);
123cdf0e10cSrcweir 	}	// end switch(CurResult().eStackAction)
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir const Token &
CurToken() const127cdf0e10cSrcweir CodeExplorer::CurToken() const
128cdf0e10cSrcweir {
129cdf0e10cSrcweir 	csv_assert(dpCurToken);
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	return *dpCurToken;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir Cpp_PE &
CurEnv() const135cdf0e10cSrcweir CodeExplorer::CurEnv() const
136cdf0e10cSrcweir {
137cdf0e10cSrcweir 	csv_assert(aEnvironments.size() > 0);
138cdf0e10cSrcweir 	csv_assert(aEnvironments.back() != 0);
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	return *aEnvironments.back();
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir Cpp_PE &
PushEnv() const144cdf0e10cSrcweir CodeExplorer::PushEnv() const
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     TokenProcessing_Result & rCurResult = const_cast< TokenProcessing_Result& >(aGlobalParseContext.CurResult());
147cdf0e10cSrcweir 	Cpp_PE * ret = dynamic_cast< Cpp_PE* >(rCurResult.pEnv2Push);
148cdf0e10cSrcweir 	csv_assert( ret != 0 );
149cdf0e10cSrcweir 	return *ret;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 
154cdf0e10cSrcweir }   // namespace cpp
155cdf0e10cSrcweir 
156