1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmDependsJavaParserHelper.cxx,v $
6 Date: $Date: 2006-05-10 19:01:22 $
7 Version: $Revision: 1.5 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmDependsJavaParserHelper.h"
19 #include "cmSystemTools.h"
20 #include "cmDependsJavaLexer.h"
22 int cmDependsJava_yyparse( yyscan_t yyscanner
);
24 cmDependsJavaParserHelper::cmDependsJavaParserHelper()
26 this->CurrentDepth
= 0;
28 this->UnionsAvailable
= 0;
29 this->LastClassId
= 0;
33 this->ClassStack
.push_back(tl
);
37 cmDependsJavaParserHelper::~cmDependsJavaParserHelper()
39 this->CleanupParser();
42 void cmDependsJavaParserHelper::CurrentClass
43 ::AddFileNamesForPrinting(std::vector
<cmStdString
> *files
,
44 const char* prefix
, const char* sep
)
46 cmStdString rname
= "";
53 files
->push_back(rname
);
54 std::vector
<CurrentClass
>::iterator it
;
55 for ( it
= this->NestedClasses
.begin();
56 it
!= this->NestedClasses
.end();
59 it
->AddFileNamesForPrinting(files
, rname
.c_str(), sep
);
63 void cmDependsJavaParserHelper::DeallocateParserType(char** pt
)
74 this->UnionsAvailable
--;
77 void cmDependsJavaParserHelper::AddClassFound(const char* sclass
)
83 std::vector
<cmStdString
>::iterator it
;
84 for ( it
= this->ClassesFound
.begin();
85 it
!= this->ClassesFound
.end();
93 this->ClassesFound
.push_back(sclass
);
96 void cmDependsJavaParserHelper::AddPackagesImport(const char* sclass
)
98 std::vector
<cmStdString
>::iterator it
;
99 for ( it
= this->PackagesImport
.begin();
100 it
!= this->PackagesImport
.end();
108 this->PackagesImport
.push_back(sclass
);
111 void cmDependsJavaParserHelper::SafePrintMissing(const char* str
,
116 std::cout
<< line
<< " String " << cnt
<< " exists: ";
118 for ( cc
= 0; cc
< strlen(str
); cc
++ )
120 unsigned char ch
= str
[cc
];
121 if ( ch
>= 32 && ch
<= 126 )
123 std::cout
<< (char)ch
;
127 std::cout
<< "<" << (int)ch
<< ">";
131 std::cout
<< "- " << strlen(str
) << std::endl
;
134 void cmDependsJavaParserHelper::Print(const char* place
, const char* str
)
138 std::cout
<< "[" << place
<< "=" << str
<< "]" << std::endl
;
142 void cmDependsJavaParserHelper::CombineUnions(char** out
,
143 const char* in1
, char** in2
,
159 *out
= new char [ len
];
175 this->DeallocateParserType(in2
);
177 this->UnionsAvailable
++;
180 void cmDependsJavaParserHelper
181 ::CheckEmpty(int line
, int cnt
, cmDependsJavaParserHelper::ParserType
* pt
)
185 for ( cc
= 1; cc
<= cnt
; cc
++)
187 cmDependsJavaParserHelper::ParserType
* cpt
= pt
+ kk
;
188 this->SafePrintMissing(cpt
->str
, line
, cc
);
193 void cmDependsJavaParserHelper
194 ::PrepareElement(cmDependsJavaParserHelper::ParserType
* me
)
200 void cmDependsJavaParserHelper
201 ::AllocateParserType(cmDependsJavaParserHelper::ParserType
* pt
,
202 const char* str
, int len
)
207 len
= (int)strlen(str
);
213 this->UnionsAvailable
++;
214 pt
->str
= new char[ len
+ 1 ];
215 strncpy(pt
->str
, str
, len
);
217 this->Allocates
.push_back(pt
->str
);
220 void cmDependsJavaParserHelper::StartClass(const char* cls
)
224 this->ClassStack
.push_back(cl
);
226 this->CurrentDepth
++;
229 void cmDependsJavaParserHelper::EndClass()
231 CurrentClass
* parent
= 0;
232 CurrentClass
* current
= 0;
233 if ( this->ClassStack
.size() > 0 )
235 current
= &(*(this->ClassStack
.end() - 1));
236 if ( this->ClassStack
.size() > 1 )
238 parent
= &(*(this->ClassStack
.end() - 2));
243 std::cerr
<< "Error when parsing. Current class is null" << std::endl
;
248 std::cerr
<< "Error when parsing. Parent class is null" << std::endl
;
251 this->CurrentDepth
--;
252 parent
->NestedClasses
.push_back(*current
);
253 this->ClassStack
.erase(this->ClassStack
.end()-1, this->ClassStack
.end());
256 void cmDependsJavaParserHelper::PrintClasses()
258 if ( this->ClassStack
.size() == 0 )
260 std::cerr
<< "Error when parsing. No classes on class stack" << std::endl
;
263 std::vector
<cmStdString
> files
= this->GetFilesProduced();
264 std::vector
<cmStdString
>::iterator sit
;
265 for ( sit
= files
.begin();
269 std::cout
<< " " << sit
->c_str() << ".class" << std::endl
;
273 std::vector
<cmStdString
> cmDependsJavaParserHelper::GetFilesProduced()
275 std::vector
<cmStdString
> files
;
276 CurrentClass
* toplevel
= &(*(this->ClassStack
.begin()));
277 std::vector
<CurrentClass
>::iterator it
;
278 for ( it
= toplevel
->NestedClasses
.begin();
279 it
!= toplevel
->NestedClasses
.end();
282 it
->AddFileNamesForPrinting(&files
, 0, "$");
287 int cmDependsJavaParserHelper::ParseString(const char* str
, int verb
)
293 this->Verbose
= verb
;
294 this->InputBuffer
= str
;
295 this->InputBufferPos
= 0;
296 this->CurrentLine
= 0;
300 cmDependsJava_yylex_init(&yyscanner
);
301 cmDependsJava_yyset_extra(this, yyscanner
);
302 int res
= cmDependsJava_yyparse(yyscanner
);
303 cmDependsJava_yylex_destroy(yyscanner
);
306 std::cout
<< "JP_Parse returned: " << res
<< std::endl
;
312 if ( this->CurrentPackage
.size() > 0 )
314 std::cout
<< "Current package is: " <<
315 this->CurrentPackage
.c_str() << std::endl
;
317 std::cout
<< "Imports packages:";
318 if ( this->PackagesImport
.size() > 0 )
320 std::vector
<cmStdString
>::iterator it
;
321 for ( it
= this->PackagesImport
.begin();
322 it
!= this->PackagesImport
.end();
325 std::cout
<< " " << it
->c_str();
328 std::cout
<< std::endl
;
329 std::cout
<< "Depends on:";
330 if ( this->ClassesFound
.size() > 0 )
332 std::vector
<cmStdString
>::iterator it
;
333 for ( it
= this->ClassesFound
.begin();
334 it
!= this->ClassesFound
.end();
337 std::cout
<< " " << it
->c_str();
340 std::cout
<< std::endl
;
341 std::cout
<< "Generated files:" << std::endl
;
342 this->PrintClasses();
343 if ( this->UnionsAvailable
!= 0 )
345 std::cout
<< "There are still " <<
346 this->UnionsAvailable
<< " unions available" << std::endl
;
349 this->CleanupParser();
353 void cmDependsJavaParserHelper::CleanupParser()
355 std::vector
<char*>::iterator it
;
356 for ( it
= this->Allocates
.begin();
357 it
!= this->Allocates
.end();
362 this->Allocates
.erase(this->Allocates
.begin(),
363 this->Allocates
.end());
366 int cmDependsJavaParserHelper::LexInput(char* buf
, int maxlen
)
372 if ( this->InputBufferPos
< this->InputBuffer
.size() )
374 buf
[0] = this->InputBuffer
[ this->InputBufferPos
++ ];
375 if ( buf
[0] == '\n' )
377 this->CurrentLine
++;
387 void cmDependsJavaParserHelper::Error(const char* str
)
389 unsigned long pos
= static_cast<unsigned long>(this->InputBufferPos
);
390 fprintf(stderr
, "JPError: %s (%lu / Line: %d)\n",
391 str
, pos
, this->CurrentLine
);
393 std::cerr
<< "String: [";
395 cc
< 30 && *(this->InputBuffer
.c_str() + this->InputBufferPos
+ cc
);
398 std::cerr
<< *(this->InputBuffer
.c_str() + this->InputBufferPos
+ cc
);
400 std::cerr
<< "]" << std::endl
;
403 void cmDependsJavaParserHelper::UpdateCombine(const char* str1
,
406 if ( this->CurrentCombine
== "" && str1
!= 0)
408 this->CurrentCombine
= str1
;
410 this->CurrentCombine
+= ".";
411 this->CurrentCombine
+= str2
;
414 int cmDependsJavaParserHelper::ParseFile(const char* file
)
416 if ( !cmSystemTools::FileExists(file
))
420 std::ifstream
ifs(file
);
426 cmStdString fullfile
= "";
428 while ( cmSystemTools::GetLineFromStream(ifs
, line
) )
430 fullfile
+= line
+ "\n";
432 return this->ParseString(fullfile
.c_str(), 0);