cmDependsJavaParserHelper.cxx 9.6 KB

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