1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmDependsJavaParserHelper.h"
14 #include "cmSystemTools.h"
15 #include "cmDependsJavaLexer.h"
17 int cmDependsJava_yyparse( yyscan_t yyscanner );
19 cmDependsJavaParserHelper::cmDependsJavaParserHelper()
21 this->CurrentDepth = 0;
23 this->UnionsAvailable = 0;
24 this->LastClassId = 0;
28 this->ClassStack.push_back(tl);
32 cmDependsJavaParserHelper::~cmDependsJavaParserHelper()
34 this->CleanupParser();
37 void cmDependsJavaParserHelper::CurrentClass
38 ::AddFileNamesForPrinting(std::vector<cmStdString> *files,
39 const char* prefix, const char* sep)
41 cmStdString rname = "";
48 files->push_back(rname);
49 std::vector<CurrentClass>::iterator it;
50 for ( it = this->NestedClasses->begin();
51 it != this->NestedClasses->end();
54 it->AddFileNamesForPrinting(files, rname.c_str(), sep);
58 void cmDependsJavaParserHelper::DeallocateParserType(char** pt)
69 this->UnionsAvailable --;
72 void cmDependsJavaParserHelper::AddClassFound(const char* sclass)
78 std::vector<cmStdString>::iterator it;
79 for ( it = this->ClassesFound.begin();
80 it != this->ClassesFound.end();
88 this->ClassesFound.push_back(sclass);
91 void cmDependsJavaParserHelper::AddPackagesImport(const char* sclass)
93 std::vector<cmStdString>::iterator it;
94 for ( it = this->PackagesImport.begin();
95 it != this->PackagesImport.end();
103 this->PackagesImport.push_back(sclass);
106 void cmDependsJavaParserHelper::SafePrintMissing(const char* str,
111 std::cout << line << " String " << cnt << " exists: ";
113 for ( cc = 0; cc < strlen(str); cc ++ )
115 unsigned char ch = str[cc];
116 if ( ch >= 32 && ch <= 126 )
118 std::cout << (char)ch;
122 std::cout << "<" << (int)ch << ">";
126 std::cout << "- " << strlen(str) << std::endl;
129 void cmDependsJavaParserHelper::Print(const char* place, const char* str)
133 std::cout << "[" << place << "=" << str << "]" << std::endl;
137 void cmDependsJavaParserHelper::CombineUnions(char** out,
138 const char* in1, char** in2,
154 *out = new char [ len ];
170 this->DeallocateParserType(in2);
172 this->UnionsAvailable ++;
175 void cmDependsJavaParserHelper
176 ::CheckEmpty(int line, int cnt, cmDependsJavaParserHelper::ParserType* pt)
180 for ( cc = 1; cc <= cnt; cc ++)
182 cmDependsJavaParserHelper::ParserType* cpt = pt + kk;
183 this->SafePrintMissing(cpt->str, line, cc);
188 void cmDependsJavaParserHelper
189 ::PrepareElement(cmDependsJavaParserHelper::ParserType* me)
195 void cmDependsJavaParserHelper
196 ::AllocateParserType(cmDependsJavaParserHelper::ParserType* pt,
197 const char* str, int len)
202 len = (int)strlen(str);
208 this->UnionsAvailable ++;
209 pt->str = new char[ len + 1 ];
210 strncpy(pt->str, str, len);
212 this->Allocates.push_back(pt->str);
215 void cmDependsJavaParserHelper::StartClass(const char* cls)
219 this->ClassStack.push_back(cl);
221 this->CurrentDepth ++;
224 void cmDependsJavaParserHelper::EndClass()
226 CurrentClass* parent = 0;
227 CurrentClass* current = 0;
228 if ( this->ClassStack.size() > 0 )
230 current = &(*(this->ClassStack.end() - 1));
231 if ( this->ClassStack.size() > 1 )
233 parent = &(*(this->ClassStack.end() - 2));
238 std::cerr << "Error when parsing. Current class is null" << std::endl;
243 std::cerr << "Error when parsing. Parent class is null" << std::endl;
246 this->CurrentDepth --;
247 parent->NestedClasses->push_back(*current);
248 this->ClassStack.erase(this->ClassStack.end()-1, this->ClassStack.end());
251 void cmDependsJavaParserHelper::PrintClasses()
253 if ( this->ClassStack.size() == 0 )
255 std::cerr << "Error when parsing. No classes on class stack" << std::endl;
258 std::vector<cmStdString> files = this->GetFilesProduced();
259 std::vector<cmStdString>::iterator sit;
260 for ( sit = files.begin();
264 std::cout << " " << sit->c_str() << ".class" << std::endl;
268 std::vector<cmStdString> cmDependsJavaParserHelper::GetFilesProduced()
270 std::vector<cmStdString> files;
271 CurrentClass* toplevel = &(*(this->ClassStack.begin()));
272 std::vector<CurrentClass>::iterator it;
273 for ( it = toplevel->NestedClasses->begin();
274 it != toplevel->NestedClasses->end();
277 it->AddFileNamesForPrinting(&files, 0, "$");
282 int cmDependsJavaParserHelper::ParseString(const char* str, int verb)
288 this->Verbose = verb;
289 this->InputBuffer = str;
290 this->InputBufferPos = 0;
291 this->CurrentLine = 0;
295 cmDependsJava_yylex_init(&yyscanner);
296 cmDependsJava_yyset_extra(this, yyscanner);
297 int res = cmDependsJava_yyparse(yyscanner);
298 cmDependsJava_yylex_destroy(yyscanner);
301 std::cout << "JP_Parse returned: " << res << std::endl;
307 if ( this->CurrentPackage.size() > 0 )
309 std::cout << "Current package is: " <<
310 this->CurrentPackage.c_str() << std::endl;
312 std::cout << "Imports packages:";
313 if ( this->PackagesImport.size() > 0 )
315 std::vector<cmStdString>::iterator it;
316 for ( it = this->PackagesImport.begin();
317 it != this->PackagesImport.end();
320 std::cout << " " << it->c_str();
323 std::cout << std::endl;
324 std::cout << "Depends on:";
325 if ( this->ClassesFound.size() > 0 )
327 std::vector<cmStdString>::iterator it;
328 for ( it = this->ClassesFound.begin();
329 it != this->ClassesFound.end();
332 std::cout << " " << it->c_str();
335 std::cout << std::endl;
336 std::cout << "Generated files:" << std::endl;
337 this->PrintClasses();
338 if ( this->UnionsAvailable != 0 )
340 std::cout << "There are still " <<
341 this->UnionsAvailable << " unions available" << std::endl;
344 this->CleanupParser();
348 void cmDependsJavaParserHelper::CleanupParser()
350 std::vector<char*>::iterator it;
351 for ( it = this->Allocates.begin();
352 it != this->Allocates.end();
357 this->Allocates.erase(this->Allocates.begin(),
358 this->Allocates.end());
361 int cmDependsJavaParserHelper::LexInput(char* buf, int maxlen)
367 if ( this->InputBufferPos < this->InputBuffer.size() )
369 buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
370 if ( buf[0] == '\n' )
372 this->CurrentLine ++;
382 void cmDependsJavaParserHelper::Error(const char* str)
384 unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
385 fprintf(stderr, "JPError: %s (%lu / Line: %d)\n",
386 str, pos, this->CurrentLine);
388 std::cerr << "String: [";
390 cc < 30 && *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
393 std::cerr << *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
395 std::cerr << "]" << std::endl;
398 void cmDependsJavaParserHelper::UpdateCombine(const char* str1,
401 if ( this->CurrentCombine == "" && str1 != 0)
403 this->CurrentCombine = str1;
405 this->CurrentCombine += ".";
406 this->CurrentCombine += str2;
409 int cmDependsJavaParserHelper::ParseFile(const char* file)
411 if ( !cmSystemTools::FileExists(file))
415 std::ifstream ifs(file);
421 cmStdString fullfile = "";
423 while ( cmSystemTools::GetLineFromStream(ifs, line) )
425 fullfile += line + "\n";
427 return this->ParseString(fullfile.c_str(), 0);