| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License |
| 7 | as published by the Free Software Foundation; either version 2 |
| 8 | of the License, or (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | //------------------------------------------------------------------------- |
| 21 | // |
| 22 | // Scanner class for FaCT++ |
| 23 | // |
| 24 | //------------------------------------------------------------------------- |
| 25 | #ifndef COMSCANNER_H |
| 26 | #define COMSCANNER_H |
| 27 | |
| 28 | #include <cstdlib> |
| 29 | #include <cstring> |
| 30 | #include <iostream> |
| 31 | |
| 32 | /// max ID length for scanned objects |
| 33 | const unsigned int MaxIDLength = 10240; |
| 34 | |
| 35 | /// more-or-less general simple scanner implementation |
| 36 | class CommonScanner |
| 37 | { |
| 38 | protected: // members |
| 39 | /// input stream |
| 40 | std::istream* InFile; |
| 41 | /// buffer for names |
| 42 | char LexBuff [ MaxIDLength + 1 ]; |
| 43 | /// currently processed line of input (used in error diagnosis) |
| 44 | unsigned int CurLine; |
| 45 | |
| 46 | protected: // methods |
| 47 | /// get next symbol from the stream |
| 48 | char NextChar ( void ) const { return (char)InFile->get(); } |
| 49 | /// return given symbol back to stream |
| 50 | void PutBack ( char c ) const { InFile->putback(c); } |
| 51 | /// check whether C is a EOF char |
| 52 | static bool eof ( char c ) { return c == std::char_traits<char>::eof(); } |
| 53 | |
| 54 | public: // interface |
| 55 | /// c'tor |
| 56 | explicit CommonScanner ( std::istream* inp ) |
| 57 | : InFile(inp) |
| 58 | , CurLine(1) |
| 59 | {} |
| 60 | /// d'tor |
| 61 | virtual ~CommonScanner() = default; |
| 62 | |
| 63 | /// get string collected in buffer |
| 64 | const char* GetName ( void ) const { return LexBuff; } |
| 65 | /// get number by string from buffer |
| 66 | unsigned long GetNumber ( void ) const { return (unsigned long) atol(LexBuff); } |
| 67 | /// get current input line |
| 68 | unsigned int Line ( void ) const { return CurLine; } |
| 69 | /// check if Buffer contains given Word (in any register) |
| 70 | bool isKeyword ( const char* Word ) const |
| 71 | { return strlen(Word) == strlen(LexBuff) ? !strcmp ( Word, LexBuff ) : false; } |
| 72 | |
| 73 | /// reset scanner on the same file |
| 74 | void ReSet ( void ) |
| 75 | { |
| 76 | InFile->clear(); |
| 77 | InFile->seekg ( 0L, std::ios::beg ); |
| 78 | CurLine = 1; |
| 79 | } |
| 80 | /// reset scanner to a given file |
| 81 | void reIn ( std::istream* in ) { InFile = in; CurLine = 1; } |
| 82 | |
| 83 | /// output an error message |
| 84 | void error ( const char* msg = nullptr ) const |
| 85 | { |
| 86 | std::cerr << "\nError at input line " << Line() << ": " |
| 87 | << (msg?msg:"illegal syntax") << "\n"; |
| 88 | exit (1); |
| 89 | } |
| 90 | }; // CommonScanner |
| 91 | |
| 92 | #endif |