1 /*---------------------------------*- C++ -*---------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
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
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
27 An attributed Coco/R grammar to parse C/C++, Fortran and Java files
28 for include and import statements.
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:
46 #include "wmkdependParser.h"
51 #include <sys/types.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, '/');
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");
91 std::cout
<< depFile
<< ": " << name
<< "\n";
97 std::list
<std::string
>::const_iterator iter
= includeDirs
.begin();
98 iter
!= includeDirs
.end();
102 const std::string pathName
= *iter
+ name
;
104 fh
= fopen(pathName
.c_str(), "r");
107 std::cout
<< depFile
<< ": " << pathName
<< "\n";
116 Parser
parser(&scanner
);
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
);
145 if (visitedDirs_
.find(dirGlob
) != visitedDirs_
.end())
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
;
170 DIR *source
= opendir(dirName
.c_str());
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
);
196 L
"could not open directory %s\n",
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
);
219 // Free storage and nullify the argument
220 inline static void coco_string_delete(wchar_t* &str
)
229 // ----------------------------------------------------------------------------
230 // Parser Implementation
231 // ----------------------------------------------------------------------------
233 void Parser::SynErr(int n
)
235 if (errDist
>= minErrDist
) errors
->SynErr(la
->line
, la
->col
, n
);
240 void Parser::SemErr(const std::wstring
& msg
)
242 if (errDist
>= minErrDist
) errors
->Error(t
->line
, t
->col
, msg
);
247 bool Parser::isUTF8() const
249 return scanner
&& scanner
->buffer
&& scanner
->buffer
->isUTF8();
258 la
= scanner
->Scan();
259 if (la
->kind
<= maxT
)
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
);
280 void Parser::Expect(int n
)
293 void Parser::ExpectWeak(int n
, int follow
)
302 while (!StartOf(follow
))
310 bool Parser::WeakSeparator(int n
, int syFol
, int repFol
)
317 else if (StartOf(repFol
))
324 while (!(StartOf(syFol
) || StartOf(repFol
) || StartOf(0)))
328 return StartOf(syFol
);
333 void Parser::wmkdepend()
344 includeFile(t
->toStringUTF8(1, t
->length()-2));
348 includeFile(t
->toString(1, t
->length()-2));
360 } else if (la
->kind
== 6) {
366 includeFile(t
->toStringUTF8(1, t
->length()-2));
370 includeFile(t
->toString(1, t
->length()-2));
381 } else if (la
->kind
== 8) {
387 importDir(t
->toStringUTF8());
391 importDir(t
->toString());
394 } else if (la
->kind
== 3) {
398 importFile(t
->toStringUTF8());
402 importFile(t
->toString());
431 // might call Parse() twice
433 coco_string_delete(dummyToken
->val
);
436 dummyToken
= new Token(coco_string_create(L
"Dummy Token"));
440 Expect(0); // expect end-of-file automatically added
444 Parser::Parser(Scanner
* scan
, Errors
* err
)
447 deleteErrorsDestruct_(!err
),
454 if (!errors
) // add in default error handling
456 errors
= new Errors();
458 // user-defined initializations:
462 bool Parser::StartOf(int s
)
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
];
483 if (deleteErrorsDestruct_
) { delete errors
; } // delete default error handling
485 coco_string_delete(dummyToken
->val
);
488 // user-defined destruction:
492 // ----------------------------------------------------------------------------
493 // Errors Implementation
494 // ----------------------------------------------------------------------------
512 std::wstring
Errors::strerror(int 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;
529 // std::wostringstream buf; (this typedef might be missing)
530 std::basic_ostringstream
<wchar_t> buf
;
531 buf
<< "error " << n
;
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());
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());
571 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
575 // ************************************************************************* //