BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / wmake / src / wmkdependParser.cpp
blob0984a56cb91456b7e46ab2a66309dbb630e968de
1 /*---------------------------------*- C++ -*---------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
6 \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 @file wmkdependParser.atg
26 Description
27 An attributed Coco/R grammar to parse C/C++, Fortran and Java files
28 for include and import statements.
30 SourceFiles
31 generated
33 \*---------------------------------------------------------------------------*/
34 // This file was generated with Coco/R C++ (10 Mar 2010)
35 // http://www.ssw.uni-linz.ac.at/coco/
36 // with these defines:
37 // - FORCE_UTF8
40 #include <cstdio>
41 #include <cstdlib>
42 #include <cstring>
43 #include <cwchar>
44 #include <sstream>
46 #include "wmkdependParser.h"
48 namespace wmake {
51 #include <sys/types.h>
52 #include <dirent.h>
54 std::set<std::string> Parser::visitedDirs_;
56 std::set<std::string> Parser::visitedFiles;
57 std::list<std::string> Parser::includeDirs;
58 std::string Parser::sourceFile;
59 std::string Parser::depFile;
62 void Parser::dotToSlash(std::string& name)
64 std::string::size_type start = 0;
66 while ((start = name.find('.', start)) != std::string::npos)
68 name.replace(start, 1, 1, '/');
69 start++;
74 void Parser::ignoreDir(const std::string& name)
76 visitedDirs_.insert(name);
80 void Parser::includeFile(const std::string& name)
82 if (!visitedFiles.insert(name).second)
84 return; // already existed (did not insert)
87 // use stdio and buffering within Coco/R -- (faster)
88 FILE *fh = fopen(name.c_str(), "r");
89 if (fh)
91 std::cout << depFile << ": " << name << "\n";
93 else
95 for
97 std::list<std::string>::const_iterator iter = includeDirs.begin();
98 iter != includeDirs.end();
99 ++iter
102 const std::string pathName = *iter + name;
104 fh = fopen(pathName.c_str(), "r");
105 if (fh)
107 std::cout << depFile << ": " << pathName << "\n";
108 break;
113 if (fh)
115 Scanner scanner(fh);
116 Parser parser(&scanner);
118 parser.Parse();
119 fclose(fh);
121 else
123 fwprintf
125 stderr,
126 L"could not open file %s for source file %s\n",
127 name.c_str(), sourceFile.c_str()
130 // only report the first occurance
131 visitedFiles.insert(name);
136 void Parser::importFile(const std::string& name)
138 // check if a globbed form was already visited
139 std::string::size_type dotPos = name.find('.');
140 if (dotPos != std::string::npos)
142 std::string dirGlob = name.substr(0, dotPos);
143 dirGlob += ".*";
145 if (visitedDirs_.find(dirGlob) != visitedDirs_.end())
147 return;
151 std::string javaFileName = name;
153 dotToSlash(javaFileName);
154 javaFileName += ".java";
156 includeFile(javaFileName);
160 void Parser::importDir(const std::string& name)
162 if (!visitedDirs_.insert(name).second)
164 return; // already existed (did not insert)
167 std::string dirName = name;
168 dotToSlash(dirName);
170 DIR *source = opendir(dirName.c_str());
172 if (source)
174 struct dirent *list;
176 // Read and parse all the entries in the directory
177 while ((list = readdir(source)) != NULL)
179 const char* ext = strstr(list->d_name, ".java");
181 // avoid matching on something like '.java~'
182 if (ext && strlen(ext) == 5)
184 std::string pathName = dirName + list->d_name;
185 includeFile(pathName);
189 closedir(source);
191 else
193 fwprintf
195 stderr,
196 L"could not open directory %s\n",
197 dirName.c_str()
199 return;
206 //! @cond fileScope
208 // Create by copying str - only used locally
209 inline static wchar_t* coco_string_create(const wchar_t* str)
211 const int len = wcslen(str);
212 wchar_t* dst = new wchar_t[len + 1];
213 wcsncpy(dst, str, len);
214 dst[len] = 0;
215 return dst;
219 // Free storage and nullify the argument
220 inline static void coco_string_delete(wchar_t* &str)
222 delete[] str;
223 str = NULL;
226 //! @endcond
229 // ----------------------------------------------------------------------------
230 // Parser Implementation
231 // ----------------------------------------------------------------------------
233 void Parser::SynErr(int n)
235 if (errDist >= minErrDist) errors->SynErr(la->line, la->col, n);
236 errDist = 0;
240 void Parser::SemErr(const std::wstring& msg)
242 if (errDist >= minErrDist) errors->Error(t->line, t->col, msg);
243 errDist = 0;
247 bool Parser::isUTF8() const
249 return scanner && scanner->buffer && scanner->buffer->isUTF8();
253 void Parser::Get()
255 for (;;)
257 t = la;
258 la = scanner->Scan();
259 if (la->kind <= maxT)
261 ++errDist;
262 break;
264 if (dummyToken != t)
266 dummyToken->kind = t->kind;
267 dummyToken->pos = t->pos;
268 dummyToken->col = t->col;
269 dummyToken->line = t->line;
270 dummyToken->next = NULL;
271 coco_string_delete(dummyToken->val);
272 dummyToken->val = coco_string_create(t->val);
273 t = dummyToken;
275 la = t;
280 void Parser::Expect(int n)
282 if (la->kind == n)
284 Get();
286 else
288 SynErr(n);
293 void Parser::ExpectWeak(int n, int follow)
295 if (la->kind == n)
297 Get();
299 else
301 SynErr(n);
302 while (!StartOf(follow))
304 Get();
310 bool Parser::WeakSeparator(int n, int syFol, int repFol)
312 if (la->kind == n)
314 Get();
315 return true;
317 else if (StartOf(repFol))
319 return false;
321 else
323 SynErr(n);
324 while (!(StartOf(syFol) || StartOf(repFol) || StartOf(0)))
326 Get();
328 return StartOf(syFol);
333 void Parser::wmkdepend()
335 while (StartOf(1)) {
336 if (la->kind == 5) {
337 Get();
338 if (la->kind == 6) {
339 Get();
340 if (la->kind == 1) {
341 Get();
342 if (isUTF8())
344 includeFile(t->toStringUTF8(1, t->length()-2));
346 else
348 includeFile(t->toString(1, t->length()-2));
353 if (StartOf(2)) {
354 Get();
355 while (StartOf(3)) {
356 Get();
359 Expect(7);
360 } else if (la->kind == 6) {
361 Get();
362 if (la->kind == 2) {
363 Get();
364 if (isUTF8())
366 includeFile(t->toStringUTF8(1, t->length()-2));
368 else
370 includeFile(t->toString(1, t->length()-2));
374 if (StartOf(4)) {
375 Get();
376 while (StartOf(3)) {
377 Get();
380 Expect(7);
381 } else if (la->kind == 8) {
382 Get();
383 if (la->kind == 4) {
384 Get();
385 if (isUTF8())
387 importDir(t->toStringUTF8());
389 else
391 importDir(t->toString());
394 } else if (la->kind == 3) {
395 Get();
396 if (isUTF8())
398 importFile(t->toStringUTF8());
400 else
402 importFile(t->toString());
405 } else SynErr(11);
406 Expect(9);
407 if (StartOf(3)) {
408 Get();
409 while (StartOf(3)) {
410 Get();
413 Expect(7);
414 } else {
415 if (StartOf(5)) {
416 Get();
417 while (StartOf(3)) {
418 Get();
421 Expect(7);
428 void Parser::Parse()
430 t = NULL;
431 // might call Parse() twice
432 if (dummyToken) {
433 coco_string_delete(dummyToken->val);
434 delete dummyToken;
436 dummyToken = new Token(coco_string_create(L"Dummy Token"));
437 la = dummyToken;
438 Get();
439 wmkdepend();
440 Expect(0); // expect end-of-file automatically added
444 Parser::Parser(Scanner* scan, Errors* err)
446 dummyToken(NULL),
447 deleteErrorsDestruct_(!err),
448 errDist(minErrDist),
449 scanner(scan),
450 errors(err),
451 t(NULL),
452 la(NULL)
454 if (!errors) // add in default error handling
456 errors = new Errors();
458 // user-defined initializations:
462 bool Parser::StartOf(int s)
464 const bool T = true;
465 const bool x = false;
467 static const bool set[6][12] =
469 {T,x,x,x, x,x,x,x, x,x,x,x},
470 {x,T,T,T, T,T,T,T, T,T,T,x},
471 {x,x,T,T, T,T,x,x, T,T,T,x},
472 {x,T,T,T, T,T,T,x, T,T,T,x},
473 {x,T,x,T, T,T,T,x, T,T,T,x},
474 {x,T,T,T, T,x,x,x, x,T,T,x}
477 return set[s][la->kind];
481 Parser::~Parser()
483 if (deleteErrorsDestruct_) { delete errors; } // delete default error handling
484 if (dummyToken) {
485 coco_string_delete(dummyToken->val);
486 delete dummyToken;
488 // user-defined destruction:
492 // ----------------------------------------------------------------------------
493 // Errors Implementation
494 // ----------------------------------------------------------------------------
496 Errors::Errors()
498 count(0)
502 Errors::~Errors()
506 void Errors::clear()
508 count = 0;
512 std::wstring Errors::strerror(int n)
514 switch (n) {
515 case 0: return L"EOF expected"; break;
516 case 1: return L"string expected"; break;
517 case 2: return L"sqstring expected"; break;
518 case 3: return L"package_name expected"; break;
519 case 4: return L"package_dir expected"; break;
520 case 5: return L"\"#\" expected"; break;
521 case 6: return L"\"include\" expected"; break;
522 case 7: return L"\"\\n\" expected"; break;
523 case 8: return L"\"import\" expected"; break;
524 case 9: return L"\";\" expected"; break;
525 case 10: return L"??? expected"; break;
526 case 11: return L"invalid wmkdepend"; break;
527 default:
529 // std::wostringstream buf; (this typedef might be missing)
530 std::basic_ostringstream<wchar_t> buf;
531 buf << "error " << n;
532 return buf.str();
534 break;
539 void Errors::Warning(const std::wstring& msg)
541 fwprintf(stderr, L"%ls\n", msg.c_str());
545 void Errors::Warning(int line, int col, const std::wstring& msg)
547 fwprintf(stderr, L"-- line %d col %d: %ls\n", line, col, msg.c_str());
551 void Errors::Error(int line, int col, const std::wstring& msg)
553 fwprintf(stderr, L"-- line %d col %d: %ls\n", line, col, msg.c_str());
554 count++;
558 void Errors::SynErr(int line, int col, int n)
560 this->Error(line, col, this->strerror(n));
564 void Errors::Exception(const std::wstring& msg)
566 fwprintf(stderr, L"%ls", msg.c_str());
567 ::exit(1);
571 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
573 } // End namespace
575 // ************************************************************************* //