cmDependsJavaParserHelper.cxx 9.6 KB

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