cmDependsJavaParserHelper.cxx 9.0 KB

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