cmDependsJavaParserHelper.cxx 8.9 KB

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