Kind-of worked on the R-Tree; not really enough time to do much.
[aesalon.git] / src / config / Parser.cpp
blob9a668a4c66c379939be086bad36c29c5cad42282
1 /** Aesalon, a tool to visualize program behaviour in real time.
2 Copyright (C) 2009-2011, Aesalon development team.
4 Aesalon is distributed under the terms of the GNU GPLv3. See
5 the included file LICENSE for more information.
7 @file src/config/Parser.cpp
8 */
10 #include <cstring>
11 #include <cstdlib>
12 #include <libgen.h>
13 #include <dirent.h>
14 #include <sys/stat.h>
16 #include "config/Parser.h"
17 #include "util/StreamAsString.h"
18 #include "util/MessageSystem.h"
19 #include "Config.h"
21 namespace Config {
23 void Parser::parse(Vault *vault, const std::string &configFile) {
24 std::string currentModule;
26 openFile(configFile);
28 if(!m_stream->is_open()) return;
30 for(;;) {
31 TokenType tokenType;
32 std::string token = nextToken(tokenType);
33 if(tokenType == END_OF_FILE) break;
35 //std::cout << '"' << token << '"' << " of type " << nameOf(tokenType) << std::endl;
37 if(tokenType == WORD && token == "include") {
38 Parser().parse(vault, expectNextToken(QUOTED_WORD));
39 expectNextSymbol(";");
41 else if(tokenType == WORD && token == "module") {
42 currentModule = expectNextToken(WORD);
43 expectNextSymbol("{");
45 else if(tokenType == WORD && token == "search") {
46 char *dirpath = strdup(configFile.c_str());
47 dirpath = dirname(dirpath);
49 std::string dirname = expectNextToken(QUOTED_WORD);
51 if(dirname[0] != '/' && dirname[0] != '~') {
52 dirname = Util::StreamAsString() << dirpath << "/" << dirname;
55 Parser().parseDirectory(vault, dirname);
57 expectNextSymbol(";");
58 std::free(dirpath);
60 else if(tokenType == SYMBOL && token == "}") {
61 if(currentModule != "") {
62 currentModule = "";
64 else Message(Fatal, "Extra \"}\" in config file.");
66 else if(tokenType == WORD && token == "use") {
67 /*std::cout << "Parser: Using module \"" << expectNextToken(WORD) << "\"\n";*/
68 std::string moduleName = expectNextToken(WORD);
70 vault->set("::modules", moduleName);
72 expectNextSymbol(";");
74 else if(tokenType == WORD) {
75 std::string op = expectNextToken(SYMBOL);
77 if(op == "=") vault->clear(token);
79 do {
80 TokenType nextType;
81 std::string next = nextToken(nextType);
83 //if(nextType == SYMBOL && next == ";") break;
85 if(nextType == WORD || nextType == QUOTED_WORD) {
86 if(currentModule != "") vault->set(
87 Util::StreamAsString() << currentModule << ":" << token, next);
89 else vault->set(token, next);
91 /*std::cout << "Set \"" << currentModule << "::" << token << "\" to \""
92 << next << "\" with operator " << op << std::endl;*/
94 else Message(Fatal, "Invalid RHS");
96 std::string sym = expectNextToken(SYMBOL);
97 if(sym == ";") break;
98 else if(sym == ",") {}
99 else {
100 Message(Fatal, "Expected \",\" or \";\", got \"" << sym << "\"");
102 } while(true);
104 else {
105 Message(Fatal, "Syntax error at token \"" << token << "\"");
107 //std::cout << "\"" << token << "\"\n";
110 closeFile();
113 void Parser::parseDirectory(Vault *vault, const std::string &directory) {
114 DIR *dir = opendir(directory.c_str());
115 if(dir == NULL) return;
117 vault->clear("::directory");
118 vault->set("::directory", directory);
120 struct dirent *dent;
122 while((dent = readdir(dir)) != NULL) {
123 if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue;
124 std::string possible = directory + "/";
125 possible += dent->d_name;
127 struct stat s;
128 if(stat(possible.c_str(), &s) != 0) {
129 /* Cannot stat file, probably don't have access permissions. */
130 continue;
133 possible += "/";
135 if(S_ISDIR(s.st_mode)) {
136 parse(vault, possible + AesalonModuleConfigFileName);
137 std::string path = dent->d_name;
138 path += ":root";
139 vault->clear(path);
140 vault->set(path, possible);
144 closedir(dir);
147 void Parser::openFile(const std::string &configFile) {
148 m_stream = new std::ifstream(configFile.c_str());
151 std::string Parser::nextToken(TokenType &type) {
152 std::ifstream &stream = *m_stream;
154 skipWhitespace();
156 if(stream.eof()) {
157 type = END_OF_FILE;
158 return "";
161 if(stream.peek() == '#') {
162 std::string line;
163 std::getline(stream, line);
164 return nextToken(type);
166 else if(stream.peek() == '"') {
167 stream.get(); // skip "
169 std::string word;
170 while(stream.peek() != '"') {
171 word += stream.get();
174 stream.get(); // skip "
176 type = QUOTED_WORD;
177 return word;
179 else if(std::isalnum(stream.peek())) {
180 std::string word;
181 while(std::isalnum(stream.peek())) {
182 word += stream.get();
185 type = WORD;
186 return word;
188 else if(stream.peek() == '+') {
189 stream.get(); // skip '+'
190 if(stream.peek() == '=') {
191 stream.get(); // skip '='
192 type = SYMBOL;
193 return "+=";
196 std::string rest;
197 stream >> rest;
199 Message(Fatal, "Unrecognized token: \"+" << rest << "\"");
200 return ""; // Never reached.
202 else {
203 std::string token;
204 token += stream.get();
206 type = SYMBOL;
207 return token;
211 std::string Parser::expectNextToken(TokenType expected) {
212 TokenType actual;
213 std::string token = nextToken(actual);
215 if(actual != expected) {
216 Message(Fatal,
217 "Expected " << nameOf(expected) << " token, found " << nameOf(actual)
218 << " token: \"" << token << "\"");
221 return token;
224 void Parser::expectNextSymbol(const std::string &symbol) {
225 std::string s = expectNextToken(SYMBOL);
226 if(symbol != s) {
227 Message(Fatal, "Expected token \"" << symbol << "\", got \"" << s << "\"");
231 void Parser::skipWhitespace() {
232 std::ifstream &stream = *m_stream;
234 while(!stream.fail() && std::isspace(stream.peek())) {
235 stream.get();
239 void Parser::closeFile() {
240 delete m_stream;
241 m_stream = NULL;
244 const char *Parser::nameOf(TokenType type) const {
245 static const char *name[] = {
246 "WORD",
247 "QUOTED_WORD",
248 "SYMBOL",
249 "END_OF_FILE"
252 return name[type];
255 } // namespace Config