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"
12 #include <cm/string_view>
14 #include "cmsys/FStream.hxx"
16 #include "cmDependsJavaLexer.h"
17 #include "cmSystemTools.h"
19 int cmDependsJava_yyparse(yyscan_t yyscanner);
21 cmDependsJavaParserHelper::cmDependsJavaParserHelper()
23 this->CurrentDepth = 0;
25 this->UnionsAvailable = 0;
26 this->LastClassId = 0;
30 this->ClassStack.push_back(std::move(tl));
33 cmDependsJavaParserHelper::~cmDependsJavaParserHelper()
35 this->CleanupParser();
38 void cmDependsJavaParserHelper::CurrentClass::AddFileNamesForPrinting(
39 std::vector<std::string>* files, const char* prefix, const char* sep) const
47 files->push_back(rname);
48 for (CurrentClass const& nc : this->NestedClasses) {
49 nc.AddFileNamesForPrinting(files, rname.c_str(), sep);
53 void cmDependsJavaParserHelper::DeallocateParserType(char** pt)
62 this->UnionsAvailable--;
65 void cmDependsJavaParserHelper::AddClassFound(const char* sclass)
70 for (std::string const& cf : this->ClassesFound) {
75 this->ClassesFound.emplace_back(sclass);
78 void cmDependsJavaParserHelper::AddPackagesImport(const char* sclass)
80 for (std::string const& pi : this->PackagesImport) {
85 this->PackagesImport.emplace_back(sclass);
88 void cmDependsJavaParserHelper::SafePrintMissing(const char* str, int line,
92 std::cout << line << " String " << cnt << " exists: ";
94 for (cc = 0; cc < strlen(str); cc++) {
95 unsigned char ch = str[cc];
96 if (ch >= 32 && ch <= 126) {
97 std::cout << static_cast<char>(ch);
99 std::cout << "<" << static_cast<int>(ch) << ">";
103 std::cout << "- " << strlen(str) << std::endl;
106 void cmDependsJavaParserHelper::Print(const char* place, const char* str) const
109 std::cout << "[" << place << "=" << str << "]" << std::endl;
113 void cmDependsJavaParserHelper::CombineUnions(char** out, const char* in1,
114 char** in2, const char* sep)
126 *out = new char[len];
138 this->DeallocateParserType(in2);
140 this->UnionsAvailable++;
143 void cmDependsJavaParserHelper::CheckEmpty(
144 int line, int cnt, cmDependsJavaParserHelper::ParserType* pt)
148 for (cc = 1; cc <= cnt; cc++) {
149 cmDependsJavaParserHelper::ParserType* cpt = pt + kk;
150 this->SafePrintMissing(cpt->str, line, cc);
155 void cmDependsJavaParserHelper::PrepareElement(
156 cmDependsJavaParserHelper::ParserType* me)
162 void cmDependsJavaParserHelper::AllocateParserType(
163 cmDependsJavaParserHelper::ParserType* pt, const char* str, int len)
167 len = static_cast<int>(strlen(str));
172 this->UnionsAvailable++;
173 auto up = cm::make_unique<char[]>(len + 1);
175 strncpy(pt->str, str, len);
177 this->Allocates.push_back(std::move(up));
180 void cmDependsJavaParserHelper::StartClass(const char* cls)
184 this->ClassStack.push_back(std::move(cl));
186 this->CurrentDepth++;
189 void cmDependsJavaParserHelper::EndClass()
191 if (this->ClassStack.empty()) {
192 std::cerr << "Error when parsing. Current class is null" << std::endl;
195 if (this->ClassStack.size() <= 1) {
196 std::cerr << "Error when parsing. Parent class is null" << std::endl;
199 CurrentClass& current = this->ClassStack.back();
200 CurrentClass& parent = this->ClassStack[this->ClassStack.size() - 2];
201 this->CurrentDepth--;
202 parent.NestedClasses.push_back(current);
203 this->ClassStack.pop_back();
206 void cmDependsJavaParserHelper::PrintClasses()
208 if (this->ClassStack.empty()) {
209 std::cerr << "Error when parsing. No classes on class stack" << std::endl;
212 for (std::string const& f : this->GetFilesProduced()) {
213 std::cout << " " << f << ".class" << std::endl;
217 std::vector<std::string> cmDependsJavaParserHelper::GetFilesProduced()
219 std::vector<std::string> files;
220 CurrentClass const& toplevel = this->ClassStack.front();
221 for (CurrentClass const& nc : toplevel.NestedClasses) {
222 nc.AddFileNamesForPrinting(&files, nullptr, "$");
227 int cmDependsJavaParserHelper::ParseString(const char* str, int verb)
232 this->Verbose = verb;
233 this->InputBuffer = str;
234 this->InputBufferPos = 0;
235 this->CurrentLine = 0;
238 cmDependsJava_yylex_init(&yyscanner);
239 cmDependsJava_yyset_extra(this, yyscanner);
240 int res = cmDependsJava_yyparse(yyscanner);
241 cmDependsJava_yylex_destroy(yyscanner);
243 std::cout << "JP_Parse returned: " << res << std::endl;
248 if (!this->CurrentPackage.empty()) {
249 std::cout << "Current package is: " << this->CurrentPackage << std::endl;
251 std::cout << "Imports packages:";
252 if (!this->PackagesImport.empty()) {
253 std::vector<std::string>::iterator it;
254 for (it = this->PackagesImport.begin(); it != this->PackagesImport.end();
256 std::cout << " " << *it;
259 std::cout << std::endl;
260 std::cout << "Depends on:";
261 if (!this->ClassesFound.empty()) {
262 for (std::string const& cf : this->ClassesFound) {
263 std::cout << " " << cf;
266 std::cout << std::endl;
267 std::cout << "Generated files:" << std::endl;
268 this->PrintClasses();
269 if (this->UnionsAvailable != 0) {
270 std::cout << "There are still " << this->UnionsAvailable
271 << " unions available" << std::endl;
274 this->CleanupParser();
278 void cmDependsJavaParserHelper::CleanupParser()
280 this->Allocates.clear();
283 int cmDependsJavaParserHelper::LexInput(char* buf, int maxlen)
288 if (this->InputBufferPos < this->InputBuffer.size()) {
289 buf[0] = this->InputBuffer[this->InputBufferPos++];
290 if (buf[0] == '\n') {
298 void cmDependsJavaParserHelper::Error(const char* str)
300 unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
301 fprintf(stderr, "JPError: %s (%lu / Line: %d)\n", str, pos,
303 std::cerr << "String: ["
304 << cm::string_view{ this->InputBuffer }.substr(
305 this->InputBufferPos, 30)
309 void cmDependsJavaParserHelper::UpdateCombine(const char* str1,
312 if (this->CurrentCombine.empty() && str1 != nullptr) {
313 this->CurrentCombine = str1;
315 this->CurrentCombine += ".";
316 this->CurrentCombine += str2;
319 int cmDependsJavaParserHelper::ParseFile(const char* file)
321 if (!cmSystemTools::FileExists(file)) {
324 cmsys::ifstream ifs(file);
329 std::string fullfile;
331 while (cmSystemTools::GetLineFromStream(ifs, line)) {
332 fullfile += line + "\n";
334 return this->ParseString(fullfile.c_str(), 0);