cmDependsJavaParserHelper.cxx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmDependsJavaParserHelper.h"
  14. #include "cmSystemTools.h"
  15. #include "cmDependsJavaLexer.h"
  16. int cmDependsJava_yyparse( yyscan_t yyscanner );
  17. cmDependsJavaParserHelper::cmDependsJavaParserHelper()
  18. {
  19. this->CurrentDepth = 0;
  20. this->UnionsAvailable = 0;
  21. this->LastClassId = 0;
  22. CurrentClass tl;
  23. tl.Name = "*";
  24. this->ClassStack.push_back(tl);
  25. }
  26. cmDependsJavaParserHelper::~cmDependsJavaParserHelper()
  27. {
  28. this->CleanupParser();
  29. }
  30. void cmDependsJavaParserHelper::CurrentClass
  31. ::AddFileNamesForPrinting(std::vector<cmStdString> *files,
  32. const char* prefix, const char* sep)
  33. {
  34. cmStdString rname = "";
  35. if ( prefix )
  36. {
  37. rname += prefix;
  38. rname += sep;
  39. }
  40. rname += this->Name;
  41. files->push_back(rname);
  42. std::vector<CurrentClass>::iterator it;
  43. for ( it = this->NestedClasses.begin();
  44. it != this->NestedClasses.end();
  45. ++ it )
  46. {
  47. it->AddFileNamesForPrinting(files, rname.c_str(), sep);
  48. }
  49. }
  50. void cmDependsJavaParserHelper::DeallocateParserType(char** pt)
  51. {
  52. if (!pt)
  53. {
  54. return;
  55. }
  56. if (!*pt)
  57. {
  58. return;
  59. }
  60. *pt = 0;
  61. this->UnionsAvailable --;
  62. }
  63. void cmDependsJavaParserHelper::AddClassFound(const char* sclass)
  64. {
  65. if( ! sclass )
  66. {
  67. return;
  68. }
  69. std::vector<cmStdString>::iterator it;
  70. for ( it = this->ClassesFound.begin();
  71. it != this->ClassesFound.end();
  72. it ++ )
  73. {
  74. if ( *it == sclass )
  75. {
  76. return;
  77. }
  78. }
  79. this->ClassesFound.push_back(sclass);
  80. }
  81. void cmDependsJavaParserHelper::AddPackagesImport(const char* sclass)
  82. {
  83. std::vector<cmStdString>::iterator it;
  84. for ( it = this->PackagesImport.begin();
  85. it != this->PackagesImport.end();
  86. it ++ )
  87. {
  88. if ( *it == sclass )
  89. {
  90. return;
  91. }
  92. }
  93. this->PackagesImport.push_back(sclass);
  94. }
  95. void cmDependsJavaParserHelper::SafePrintMissing(const char* str,
  96. int line, int cnt)
  97. {
  98. if ( str )
  99. {
  100. std::cout << line << " String " << cnt << " exists: ";
  101. unsigned int cc;
  102. for ( cc = 0; cc < strlen(str); cc ++ )
  103. {
  104. unsigned char ch = str[cc];
  105. if ( ch >= 32 && ch <= 126 )
  106. {
  107. std::cout << (char)ch;
  108. }
  109. else
  110. {
  111. std::cout << "<" << (int)ch << ">";
  112. break;
  113. }
  114. }
  115. std::cout << "- " << strlen(str) << std::endl;
  116. }
  117. }
  118. void cmDependsJavaParserHelper::Print(const char* place, const char* str)
  119. {
  120. if ( this->Verbose )
  121. {
  122. std::cout << "[" << place << "=" << str << "]" << std::endl;
  123. }
  124. }
  125. void cmDependsJavaParserHelper::CombineUnions(char** out,
  126. const char* in1, char** in2,
  127. const char* sep)
  128. {
  129. size_t len = 1;
  130. if ( in1 )
  131. {
  132. len += strlen(in1);
  133. }
  134. if ( *in2 )
  135. {
  136. len += strlen(*in2);
  137. }
  138. if ( sep )
  139. {
  140. len += strlen(sep);
  141. }
  142. *out = new char [ len ];
  143. *out[0] = 0;
  144. if ( in1 )
  145. {
  146. strcat(*out, in1);
  147. }
  148. if ( sep )
  149. {
  150. strcat(*out, sep);
  151. }
  152. if ( *in2 )
  153. {
  154. strcat(*out, *in2);
  155. }
  156. if ( *in2 )
  157. {
  158. this->DeallocateParserType(in2);
  159. }
  160. this->UnionsAvailable ++;
  161. }
  162. void cmDependsJavaParserHelper
  163. ::CheckEmpty(int line, int cnt, cmDependsJavaParserHelper::ParserType* pt)
  164. {
  165. int cc;
  166. int kk = -cnt + 1;
  167. for ( cc = 1; cc <= cnt; cc ++)
  168. {
  169. cmDependsJavaParserHelper::ParserType* cpt = pt + kk;
  170. this->SafePrintMissing(cpt->str, line, cc);
  171. kk ++;
  172. }
  173. }
  174. void cmDependsJavaParserHelper
  175. ::PrepareElement(cmDependsJavaParserHelper::ParserType* me)
  176. {
  177. // Inititalize self
  178. me->str = 0;
  179. }
  180. void cmDependsJavaParserHelper
  181. ::AllocateParserType(cmDependsJavaParserHelper::ParserType* pt,
  182. const char* str, int len)
  183. {
  184. pt->str = 0;
  185. if ( len == 0 )
  186. {
  187. len = (int)strlen(str);
  188. }
  189. if ( len == 0 )
  190. {
  191. return;
  192. }
  193. this->UnionsAvailable ++;
  194. pt->str = new char[ len + 1 ];
  195. strncpy(pt->str, str, len);
  196. pt->str[len] = 0;
  197. this->Allocates.push_back(pt->str);
  198. }
  199. void cmDependsJavaParserHelper::StartClass(const char* cls)
  200. {
  201. CurrentClass cl;
  202. cl.Name = cls;
  203. this->ClassStack.push_back(cl);
  204. this->CurrentDepth ++;
  205. }
  206. void cmDependsJavaParserHelper::EndClass()
  207. {
  208. CurrentClass* parent = 0;
  209. CurrentClass* current = 0;
  210. if ( this->ClassStack.size() > 0 )
  211. {
  212. current = &(*(this->ClassStack.end() - 1));
  213. if ( this->ClassStack.size() > 1 )
  214. {
  215. parent = &(*(this->ClassStack.end() - 2));
  216. }
  217. }
  218. if ( current == 0 )
  219. {
  220. std::cerr << "Error when parsing. Current class is null" << std::endl;
  221. abort();
  222. }
  223. if ( parent == 0 )
  224. {
  225. std::cerr << "Error when parsing. Parent class is null" << std::endl;
  226. abort();
  227. }
  228. this->CurrentDepth --;
  229. parent->NestedClasses.push_back(*current);
  230. this->ClassStack.erase(this->ClassStack.end()-1, this->ClassStack.end());
  231. }
  232. void cmDependsJavaParserHelper::PrintClasses()
  233. {
  234. if ( this->ClassStack.size() == 0 )
  235. {
  236. std::cerr << "Error when parsing. No classes on class stack" << std::endl;
  237. abort();
  238. }
  239. std::vector<cmStdString> files = this->GetFilesProduced();
  240. std::vector<cmStdString>::iterator sit;
  241. for ( sit = files.begin();
  242. sit != files.end();
  243. ++ sit )
  244. {
  245. std::cout << " " << sit->c_str() << ".class" << std::endl;
  246. }
  247. }
  248. std::vector<cmStdString> cmDependsJavaParserHelper::GetFilesProduced()
  249. {
  250. std::vector<cmStdString> files;
  251. CurrentClass* toplevel = &(*(this->ClassStack.begin()));
  252. std::vector<CurrentClass>::iterator it;
  253. for ( it = toplevel->NestedClasses.begin();
  254. it != toplevel->NestedClasses.end();
  255. ++ it )
  256. {
  257. it->AddFileNamesForPrinting(&files, 0, "$");
  258. }
  259. return files;
  260. }
  261. int cmDependsJavaParserHelper::ParseString(const char* str, int verb)
  262. {
  263. if ( !str)
  264. {
  265. return 0;
  266. }
  267. this->Verbose = verb;
  268. this->InputBuffer = str;
  269. this->InputBufferPos = 0;
  270. this->CurrentLine = 0;
  271. yyscan_t yyscanner;
  272. cmDependsJava_yylex_init(&yyscanner);
  273. cmDependsJava_yyset_extra(this, yyscanner);
  274. int res = cmDependsJava_yyparse(yyscanner);
  275. cmDependsJava_yylex_destroy(yyscanner);
  276. if ( res != 0 )
  277. {
  278. std::cout << "JP_Parse returned: " << res << std::endl;
  279. return 0;
  280. }
  281. if ( verb )
  282. {
  283. if ( this->CurrentPackage.size() > 0 )
  284. {
  285. std::cout << "Current package is: " <<
  286. this->CurrentPackage.c_str() << std::endl;
  287. }
  288. std::cout << "Imports packages:";
  289. if ( this->PackagesImport.size() > 0 )
  290. {
  291. std::vector<cmStdString>::iterator it;
  292. for ( it = this->PackagesImport.begin();
  293. it != this->PackagesImport.end();
  294. ++ it )
  295. {
  296. std::cout << " " << it->c_str();
  297. }
  298. }
  299. std::cout << std::endl;
  300. std::cout << "Depends on:";
  301. if ( this->ClassesFound.size() > 0 )
  302. {
  303. std::vector<cmStdString>::iterator it;
  304. for ( it = this->ClassesFound.begin();
  305. it != this->ClassesFound.end();
  306. ++ it )
  307. {
  308. std::cout << " " << it->c_str();
  309. }
  310. }
  311. std::cout << std::endl;
  312. std::cout << "Generated files:" << std::endl;
  313. this->PrintClasses();
  314. if ( this->UnionsAvailable != 0 )
  315. {
  316. std::cout << "There are still " <<
  317. this->UnionsAvailable << " unions available" << std::endl;
  318. }
  319. }
  320. this->CleanupParser();
  321. return 1;
  322. }
  323. void cmDependsJavaParserHelper::CleanupParser()
  324. {
  325. std::vector<char*>::iterator it;
  326. for ( it = this->Allocates.begin();
  327. it != this->Allocates.end();
  328. ++ it )
  329. {
  330. delete [] *it;
  331. }
  332. this->Allocates.erase(this->Allocates.begin(),
  333. this->Allocates.end());
  334. }
  335. int cmDependsJavaParserHelper::LexInput(char* buf, int maxlen)
  336. {
  337. if ( maxlen < 1 )
  338. {
  339. return 0;
  340. }
  341. if ( this->InputBufferPos < this->InputBuffer.size() )
  342. {
  343. buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
  344. if ( buf[0] == '\n' )
  345. {
  346. this->CurrentLine ++;
  347. }
  348. return(1);
  349. }
  350. else
  351. {
  352. buf[0] = '\n';
  353. return( 0 );
  354. }
  355. }
  356. void cmDependsJavaParserHelper::Error(const char* str)
  357. {
  358. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  359. fprintf(stderr, "JPError: %s (%lu / Line: %d)\n",
  360. str, pos, this->CurrentLine);
  361. int cc;
  362. std::cerr << "String: [";
  363. for ( cc = 0;
  364. cc < 30 && *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
  365. cc ++ )
  366. {
  367. std::cerr << *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
  368. }
  369. std::cerr << "]" << std::endl;
  370. }
  371. void cmDependsJavaParserHelper::UpdateCombine(const char* str1,
  372. const char* str2)
  373. {
  374. if ( this->CurrentCombine == "" && str1 != 0)
  375. {
  376. this->CurrentCombine = str1;
  377. }
  378. this->CurrentCombine += ".";
  379. this->CurrentCombine += str2;
  380. }
  381. int cmDependsJavaParserHelper::ParseFile(const char* file)
  382. {
  383. if ( !cmSystemTools::FileExists(file))
  384. {
  385. return 0;
  386. }
  387. std::ifstream ifs(file);
  388. if ( !ifs )
  389. {
  390. return 0;
  391. }
  392. cmStdString fullfile = "";
  393. cmStdString line;
  394. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  395. {
  396. fullfile += line + "\n";
  397. }
  398. return this->ParseString(fullfile.c_str(), 0);
  399. }