Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / wmake / src / wmkdependScanner.h
blob6471f46a7e88d11f8ffbff844d07482d2c81f90a
1 /*---------------------------------*- C++ -*---------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
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 #ifndef COCO_wmkdependSCANNER_H__
41 #define COCO_wmkdependSCANNER_H__
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstring>
46 #include <cwchar>
47 #include <string>
48 #include <fstream>
49 #include <iostream>
51 namespace wmake {
53 // * * * * * * * * * * * Miscellaneous String Routines * * * * * * * * * * * //
55 //! Simple lower-case string transformation
56 template<class StringT>
57 inline void coco_string_toLower(StringT& str)
59 for
61 typename StringT::iterator iter = str.begin();
62 iter != str.end();
63 ++iter
66 if (*iter >= 'A' && *iter <= 'Z')
68 *iter += ('a' - 'A'); // lower-case
74 //! Simple string hashing function
75 template<class StringT>
76 inline int coco_string_hash(const StringT& str)
78 int h = 0;
79 for
81 typename StringT::const_iterator iter = str.begin();
82 iter != str.end();
83 ++iter
86 h = (h * 7) ^ *iter;
88 return h < 0 ? -h : h;
93 // String conversions
94 // ~~~~~~~~~~~~~~~~~~
96 //! Convert wide string to double
97 inline double coco_string_toDouble(const wchar_t* str)
99 return str ? wcstod(str, NULL) : 0;
103 //! Convert wide string to long
104 inline long coco_string_toLong(const wchar_t* str)
106 return str ? wcstol(str, NULL, 10) : 0;
110 //! A byte string (restricted to 8bit values) by copying str
111 std::string coco_stdString(const wchar_t* str);
113 //! A byte string (restricted to 8bit values) by copying str,
114 //! up to length characters long
115 std::string coco_stdString(const wchar_t* str, unsigned length);
117 //! A byte substring (restricted to 8bit values) of str,
118 //! starting at index and length characters long
119 std::string coco_stdString(const wchar_t* str, unsigned index, unsigned length);
121 //! A UTF8 byte string by copying str
122 std::string coco_stdStringUTF8(const wchar_t* str);
124 //! A UTF8 byte string by copying str, up to length characters long
125 std::string coco_stdStringUTF8(const wchar_t* str, unsigned length);
127 //! A UTF8 byte substring, starting at index and length characters long
128 std::string coco_stdStringUTF8(const wchar_t* str, unsigned index, unsigned length);
130 // * * * * * * * * * * * * End of String Routines * * * * * * * * * * * * * //
134 /*---------------------------------------------------------------------------*\
135 Class Token Declaration
136 \*---------------------------------------------------------------------------*/
138 * @brief Scanner Token
140 * @note since each Token is allocated by the internal heap mechanism,
141 * the destructor does not clean up the val member.
143 class Token
145 public:
146 int kind; //!< token kind
147 int pos; //!< token position in the source text (starting at 0)
148 int col; //!< token column (starting at 1)
149 int line; //!< token line (starting at 1)
150 wchar_t* val; //!< token value (normally allocated from the internal heap)
151 Token *next; //!< Peek tokens are kept in linked list
153 int length() const; //!< The length of val, or 0 if val is NULL
155 //! Construct null Token, optionally with pointer to a string value
156 Token(wchar_t* value = 0);
157 ~Token(); //!< Destructor - does not cleanup val member
159 //! Token val as byte string (restricted to 8bit values)
160 inline std::string toString() const
162 return coco_stdString(val);
165 //! Token val as byte string (restricted to 8bit values), up to length characters long
166 inline std::string toString(unsigned length) const
168 return coco_stdString(val, length);
171 //! Token val as byte string (restricted to 8bit values), starting at index and length characters long
172 inline std::string toString(unsigned index, unsigned length) const
174 return coco_stdString(val, index, length);
177 //! Token val as UTF8 byte string
178 inline std::string toStringUTF8() const
180 return coco_stdStringUTF8(val);
183 //! Token val as UTF8 byte string, up to length characters long
184 inline std::string toStringUTF8(unsigned length) const
186 return coco_stdStringUTF8(val, length);
189 //! Token val as UTF8 byte substring, starting at index and length characters long
190 inline std::string toStringUTF8(unsigned index, unsigned length) const
192 return coco_stdStringUTF8(this->val, index, length);
198 /*---------------------------------------------------------------------------*\
199 Class Buffer Declaration
200 \*---------------------------------------------------------------------------*/
202 * @brief Scanner Buffer
204 * This Buffer supports the following cases:
205 * -# seekable stream (file)
206 * -# whole stream in buffer
207 * -# part of stream in buffer
208 * -# non seekable stream (network, console)
210 class Buffer
212 unsigned char *buf; //!< input buffer
213 int bufCapacity; //!< capacity of buf
214 int bufLen; //!< length of buffer
215 int bufPos; //!< current position in buffer
216 int bufStart; //!< position of first byte in buffer relative to input stream
217 int fileLen; //!< length of input stream (may change if the stream is no file)
218 FILE* cStream; //!< input stdio stream (normally seekable)
219 std::istream* stdStream; //!< STL std stream (seekable)
220 bool isUserStream_; //!< was the stream opened by the user?
222 int ReadNextStreamChunk();
223 bool CanSeek() const; //!< true if stream can be seeked otherwise false
225 protected:
226 Buffer(Buffer*); //!< for the UTF8Buffer
228 public:
229 //! max unicode characters is 0xFFFF (16bit storage)
230 static const int MaxChar = 65535;
231 static const int EoF = MaxChar + 1;
233 //! Copy buffer contents from constant character string
234 Buffer(const char* chars, int len);
236 //! Copy buffer contents from constant character string
237 Buffer(const unsigned char* chars, int len);
239 //! @brief Attach buffer to a stdio stream.
240 //! User streams are not closed in the destructor
241 Buffer(FILE*, bool isUserStream = true);
243 //! @brief Attach buffer to an STL standard stream
244 //! User streams are not closed in the destructor
245 explicit Buffer(std::istream*, bool isUserStream = true);
247 //! Close stream (but not user streams) and free buf (if any)
248 virtual ~Buffer();
250 virtual void Close(); //!< Close stream (but not user streams)
251 virtual int Read(); //!< Get character from stream or buffer
252 virtual int Peek(); //!< Peek character from stream or buffer
254 virtual int GetPos() const;
255 virtual void SetPos(int value);
256 virtual bool isUTF8() const; //!< Return false - buffer is not UTF8
260 /*---------------------------------------------------------------------------*\
261 Class UTF8Buffer Declaration
262 \*---------------------------------------------------------------------------*/
263 //! A Scanner Buffer variant that decodes UTF-8 characters into 16bit unicode
264 class UTF8Buffer : public Buffer
266 public:
267 UTF8Buffer(Buffer* b) : Buffer(b) {}
268 virtual int Read();
269 virtual bool isUTF8() const; //!< Return true - buffer is UTF8
273 /*---------------------------------------------------------------------------*\
274 Class StartStates Declaration
275 \*---------------------------------------------------------------------------*/
276 //! maps characters (integers) to start states of tokens as a HashTable
277 class StartStates
279 //! HashTable entry
280 struct Entry
282 int key; //<! The lookup key
283 int val; //<! The data
284 Entry *next; //<! Pointer next Entry in sub-list
286 Entry(int k, int v, Entry *n=0)
288 key(k), val(v), next(n)
292 static const int size_ = 128; //<! fixed HashTable size
293 Entry **table_;
295 public:
296 StartStates()
298 table_(new Entry*[size_])
300 memset(table_, 0, size_*sizeof(Entry*));
303 virtual ~StartStates()
305 for (int i = 0; i < size_; ++i)
307 Entry *e = table_[i];
308 while (e)
310 Entry *next = e->next;
311 delete e;
312 e = next;
315 delete[] table_;
318 void set(int key, int val)
320 const int hashIndex = unsigned(key) % size_;
321 table_[hashIndex] = new Entry(key, val, table_[hashIndex]);
324 int state(int key)
326 Entry *e = table_[unsigned(key) % size_];
327 while (e && e->key != key) e = e->next;
328 return e ? e->val : 0;
333 /*---------------------------------------------------------------------------*\
334 Class KeywordMap Declaration
335 \*---------------------------------------------------------------------------*/
336 //! maps strings to integers (identifiers to keyword kinds) as a HashTable
337 class KeywordMap
339 //! HashTable entry
340 struct Entry
342 const std::wstring key; //<! The lookup key
343 int val; //<! The data
344 Entry *next; //<! Pointer next Entry in sub-list
346 Entry(const std::wstring& k, int v, Entry *n=0)
348 key(k), val(v), next(n)
352 static const int size_ = 128; //<! fixed HashTable size
353 Entry **table_;
355 public:
356 KeywordMap()
358 table_(new Entry*[size_])
360 memset(table_, 0, size_*sizeof(Entry*));
363 virtual ~KeywordMap()
365 for (int i = 0; i < size_; ++i)
367 Entry *e = table_[i];
368 while (e)
370 Entry *next = e->next;
371 delete e;
372 e = next;
375 delete[] table_;
378 void set(const std::wstring& key, int val)
380 const int hashIndex = coco_string_hash(key) % size_;
381 table_[hashIndex] = new Entry(key, val, table_[hashIndex]);
384 int get(const std::wstring& key, int defaultVal)
386 Entry *e = table_[coco_string_hash(key) % size_];
387 while (e && e->key != key) e = e->next;
388 return e ? e->val : defaultVal;
393 /*---------------------------------------------------------------------------*\
394 Class Scanner Declaration
395 \*---------------------------------------------------------------------------*/
396 //! A Coco/R Scanner
397 class Scanner
399 static const int maxT = 10;
400 static const int noSym = 10;
401 static const int eofSym = 0; //!< end-of-file token id
402 static const char EOL = '\n'; //!< end-of-line character
404 void *firstHeap; //!< the start of the heap management
405 void *heap; //!< the currently active block
406 void *heapTop; //!< the top of the heap
407 void **heapEnd; //!< the end of the last heap block
409 StartStates start; //!< A map of start states for particular characters
410 KeywordMap keywords; //!< A hash of keyword literals to token kind
412 Token *t; //!< current token
413 wchar_t *tval; //!< text of current token
414 int tvalLength; //!< maximum capacity (length) for tval
415 int tlen; //!< length of tval
417 Token *tokens; //!< list of tokens already peeked (first token is a dummy)
418 Token *pt; //!< current peek token
420 int ch; //!< current input character
421 int pos; //!< byte position of current character
422 int line; //!< line number of current character
423 int col; //!< column number of current character
424 int oldEols; //!< the number of EOLs that appeared in a comment
426 void CreateHeapBlock(); //!< add a heap block, freeing unused ones
427 Token* CreateToken(); //!< fit token on the heap
428 void AppendVal(Token* tok); //!< adjust tok->val to point to the heap and copy tval into it
429 void SetScannerBehindT();
431 void Init(); //!< complete the initialization for the constructors
432 void NextCh(); //!< get the next input character into ch
433 void AddCh(); //!< append the character ch to tval
434 bool Comment0();
435 bool Comment1();
436 Token* NextToken(); //!< get the next token
438 public:
439 //! The scanner buffer
440 Buffer *buffer;
442 //! Attach scanner to an existing character buffer
443 Scanner(const char* chars, int len);
445 //! Attach scanner to an existing character buffer
446 Scanner(const unsigned char* chars, int len);
448 //! Attach scanner to an existing open file handle
449 Scanner(FILE*);
451 #ifdef _WIN32
452 //! Open a file for reading and attach scanner - Windows only
453 explicit Scanner(const std::wstring& fileName);
454 #endif
456 //! Open a file for reading and attach scanner
457 explicit Scanner(const std::string& fileName);
459 //! Attach scanner to an existing open STL standard stream
460 explicit Scanner(std::istream&);
462 ~Scanner(); //!< free heap and allocated memory
463 Token* Scan(); //!< get the next token (possibly a token already seen during peeking)
464 Token* Peek(); //!< peek for the next token, ignore pragmas
465 void ResetPeek(); //!< ensure that peeking starts at the current scan position
467 int Line() const; //!< Return the current line
468 void Line(int lineNo); //!< Define the starting line for reporting errors
470 }; // end Scanner
472 } // End namespace
474 #endif // COCO_wmkdependSCANNER_H__
476 // ************************************************************************* //