cmDependsJavaParserHelper.cxx 8.0 KB

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