1 /* 2 Copyright (C) 2010-2014 Kristian Duske 3 4 This file is part of TrenchBroom. 5 6 TrenchBroom is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 TrenchBroom is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef TrenchBroom_ConfigParser 21 #define TrenchBroom_ConfigParser 22 23 #include "ConfigTypes.h" 24 #include "IO/Parser.h" 25 #include "IO/Token.h" 26 #include "IO/Tokenizer.h" 27 28 namespace TrenchBroom { 29 namespace IO { 30 namespace ConfigToken { 31 typedef size_t Type; 32 static const Type Identifier = 1 << 1; // string 33 static const Type String = 1 << 2; // "string" 34 static const Type OBrace = 1 << 3; // opening brace: { 35 static const Type CBrace = 1 << 4; // closing brace: } 36 static const Type Comma = 1 << 5; // comma: , 37 static const Type Equals = 1 << 6; // equals: = 38 static const Type Comment = 1 << 7; // line comment starting with // 39 static const Type Eof = 1 << 8; // end of file 40 } 41 42 class ConfigTokenizer : public Tokenizer<ConfigToken::Type> { 43 public: 44 ConfigTokenizer(const char* begin, const char* end); 45 ConfigTokenizer(const String& str); 46 private: 47 Token emitToken(); 48 }; 49 50 class ConfigParser : public Parser<ConfigToken::Type> { 51 private: 52 ConfigTokenizer m_tokenizer; 53 typedef ConfigTokenizer::Token Token; 54 public: 55 ConfigParser(const char* begin, const char* end); 56 ConfigParser(const String& str); 57 58 ConfigEntry::Ptr parse(); 59 private: 60 ConfigEntry::Ptr parseEntry(); 61 ConfigEntry::Type detectEntryType(); 62 ConfigEntry::Ptr parseValue(); 63 ConfigEntry::Ptr parseList(); 64 ConfigEntry::Ptr parseTable(); 65 TokenNameMap tokenNames() const; 66 }; 67 } 68 } 69 70 #endif /* defined(TrenchBroom_ConfigParser) */ 71