-
Notifications
You must be signed in to change notification settings - Fork 0
/
Executive.cpp
executable file
·221 lines (180 loc) · 7.94 KB
/
Executive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Executive.cpp (ver 1.0) - Executive Module that Starts the Analysis & Prints Summay output. //
// //
// Language : Visual C++, 2010 //
// Platform : Toshiba Satellite (R25-S3503), Windows 7 Professional //
// Application : CSE687 - Project #1 - CodeMetricsAnalyzer, Spring 2011 //
// Author : Harshal Bhakta, Syracuse University. //
// E-mail : harshal.c.bhakta@gmail.com //
// Cell : 714-876-4273 //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "Executive.h"
#include "Tokenizer.h"
#include "SemiExpression.h"
#include "Parser.h"
#include "ActionsAndRules.h"
#include "ConfigureParser.h"
#include "Executive.h"
#include "DataStore.h"
#include "Show.h"
/////////////////////////////////////////////////////////////////////////////////////
// Constructor - Uses a vector of file names ( .h & .cpp files )
Executive::Executive(std::vector<std::string> ptrFileArg) : ptrFiles(ptrFileArg)
{ }
/////////////////////////////////////////////////////////////////////////////////////
// Function to perform First Pass. Type Table Created.
void Executive::FirstPass()
{
for each ( std::string file in (ptrFiles) )
{
try
{
if ( file.length() < 4 || (( file.substr(file.length() - 4 ) != ".cpp" && file.substr(file.length() - 2 ) != ".h" )) )
{ continue; } // Invalid File Format.
ConfigParseToAnalyze configure; Parser* pParser = configure.Build();
DataStore::setDepth_scope_nesting(0); DataStore::setCurrent_File_Size(0);
std::string currentPackage = ExtractPackageNamePassOne(file);
DataStore::setcurrentPackageName(currentPackage); DataStore::setcurrentFileName(file.substr(file.find_last_of("\\") + 1));
while ( ScopeStack<std::string,ScopeElement>::size() > 0 )
{ ScopeStack<std::string,ScopeElement>::pop(); } // Clear Stack Before Starting.
if(pParser)
{
if(!configure.Attach(file))
{ std::cout << "\n could not open file " << file << std::endl; continue; }
}
else
{ std::cout << "\n\n Parser not built\n\n"; }
while(pParser->next()) // now that parser is built, use it
pParser->parse();
if ( ScopeStack<std::string,ScopeElement>::size() != 0 )
{ std::cout<<"\n\n Error Processing Package : "<<currentPackage<<" Error Code : 00000"; }
else
{ PackageHolder::setDepthOfNesting(currentPackage,DataStore::getDepth_scope_nesting());
DataStore::InsertFileSize(DataStore::getcurrentFileName(),DataStore::getCurrent_File_Size()); }
}
catch(std::exception& ex)
{ std::cout << "\n\n Error Processing Package : "<<DataStore::getcurrentPackageName()<<" Error Code : 00001 - "<<DataStore::getCurrent_File_Size()<<ex.what()<<"\n\n"; } }
}
/////////////////////////////////////////////////////////////////////////////////////
// Function to perform Second Pass on a package. Variable detecting matched against Type Table.
// Package Analyzed and results displayed on screen.
void Executive::SecondPass()
{
std::vector<std::pair<std::string,std::string>> filePackages = PackageHolder::getFilePackages();
for each ( std::pair<std::string,std::string> package in (filePackages) )
{
try
{
std::string file = "";
for ( int i = 0 ; i < 2 ; i++ )
{
if ( i == 0 )
file = package.first; // Header File.
else
file = package.second; // Source File.
if ( file.length() < 4 || (( file.substr(file.length() - 4 ) != ".cpp" && file.substr(file.length() - 2 ) != ".h" )) )
continue; // Invalid File Format.
std::string currentPackage = ExtractPackageNamePassTwo(file);
DataStore::setcurrentPackageName(currentPackage); DataStore::setcurrentFileName(file.substr(file.find_last_of("\\") + 1));
while ( ScopeStack<std::string,ScopeElement>::size() > 0 )
{ ScopeStack<std::string,ScopeElement>::pop(); }
SecondPassParse(file);
}
if ( ScopeStack<std::string,ScopeElement>::size() != 0 )
{ std::cout<<"\n\n Error Processing Package : "<<DataStore::getcurrentPackageName()<<" Error Code : 00000"; }
else
{ Show::printNormal(DataStore::getcurrentPackageName());
//Show::printVerbose(DataStore::getcurrentPackageName());
}
}
catch ( std::exception ex )
{ std::cout<<"\n\n Error Processing Package : "<<DataStore::getcurrentPackageName()<<" Error Code : 00001 -"<<DataStore::getCurrent_File_Size()<<ex.what()<<"\n\n"; }
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Function to extract package name from the filename. ( Parser -> parser.h / parser.cpp )
std::string Executive::ExtractPackageNamePassTwo(std::string file)
{
std::string currentPackage = file.substr(file.find_last_of("\\") + 1);
if ( currentPackage.substr(currentPackage.length() - 2 ) == ".h" )
{ currentPackage = currentPackage.substr(0,currentPackage.length()-2); }
else if ( currentPackage.substr(currentPackage.length() - 4 ) == ".cpp" )
{ currentPackage = currentPackage.substr(0,currentPackage.length()-4); }
return currentPackage;
}
/////////////////////////////////////////////////////////////////////////////////////
// Function to extract package Name and add initial package to Repository.
std::string Executive::ExtractPackageNamePassOne(std::string file)
{
std::string currentPackage = file.substr(file.find_last_of("\\") + 1);
if ( currentPackage.substr(currentPackage.length() - 2 ) == ".h" )
{
currentPackage = currentPackage.substr(0,currentPackage.length()-2);
Package pk;
pk.setHeaderFile(file);
pk.setSourceFile("");// Add initial package with Source File Blank.
PackageHolder::addPackage(currentPackage,pk);
}
else if ( currentPackage.substr(currentPackage.length() - 4 ) == ".cpp" )
{
currentPackage = currentPackage.substr(0,currentPackage.length()-4);
Package pk;
pk.setSourceFile(file);
pk.setHeaderFile("");// Add initial package with Header File Blank.
PackageHolder::addPackage(currentPackage,pk);
}
return currentPackage;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Function to perform Second Parse. Called once for each file. ( .h - .cpp seperately.)
void Executive::SecondPassParse(std::string file)
{
ConfigParsePassTwo configure;
Parser* pParser = configure.Build();
try
{
if(pParser)
{
if(!configure.Attach(file))
{
std::cout << "\n could not open file " << file << std::endl;
return;
}
}
else
{
std::cout << "\n\n Parser not built\n\n";
return;
}
// now that parser is built, use it
while(pParser->next())
pParser->parse();
}
catch(std::exception& ex)
{
{ std::cout<<"\n\n Error Processing Package : "<<DataStore::getcurrentPackageName()<<" Error Code : 00001 -"<<DataStore::getCurrent_File_Size()<<ex.what()<<"\n\n"; }
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Controller function to perform 2 pass parsing.
void Executive::AnalyzeFiles()
{
FirstPass();
DataStore::setParseMode(DataStore::SECOND_PASS); // Change mode to ignore 1st Pass Operations not required in 2nd pass.
SecondPass();
}
/////////////////////////////////////////////////////////////////////////////////////
// Test Stub
#ifdef TEST_EXECUTIVE
void main ( int argc , char *argv[] )
{
std::vector<std::string> files;
files.push_back("..\\Parser.cpp");
files.push_back("..\\Tokenizer.cpp");
std::vector<std::string> ptrFiles = files;
Executive ex(ptrFiles);
ex.AnalyzeFiles();
std::getchar();
}
#endif