2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <tinygettext/tinygettext.hpp>
22 #include "lisp/lisp.hpp"
23 #include "lisp/parser.hpp"
24 #include "util/obstackpp.hpp"
25 #include "physfs/ifile_stream.hpp"
26 #include "physfs/ifile_streambuf.hpp"
27 #include "supertux/globals.hpp"
29 #include "supertux/gameconfig.hpp"
33 Parser::Parser(bool translate) :
36 dictionary_manager(0),
42 dictionary_manager = new tinygettext::DictionaryManager();
43 dictionary_manager->set_charset("UTF-8");
44 if (g_config && (g_config->locale != ""))
45 dictionary_manager->set_language(tinygettext::Language::from_name(g_config->locale));
53 obstack_free(&obst, NULL);
55 delete dictionary_manager;
58 static std::string dirname(const std::string& filename)
60 std::string::size_type p = filename.find_last_of('/');
61 if(p == std::string::npos)
64 return filename.substr(0, p+1);
68 Parser::parse(const std::string& filename)
70 IFileStreambuf ins(filename);
71 std::istream in(&ins);
74 std::stringstream msg;
75 msg << "Parser problem: Couldn't open file '" << filename << "'.";
76 throw std::runtime_error(msg.str());
79 if(dictionary_manager) {
80 std::string rel_dir = dirname (filename);
81 char **searchpath = PHYSFS_getSearchPath();
82 for(char** i = searchpath; *i != NULL; i++)
84 std::string abs_dir = std::string (*i) + PHYSFS_getDirSeparator () + rel_dir;
85 dictionary_manager->add_directory (abs_dir);
87 dictionary = & (dictionary_manager->get_dictionary());
90 return parse(in, filename);
94 Parser::parse(std::istream& stream, const std::string& sourcename)
97 lexer = new Lexer(stream);
99 this->filename = sourcename;
100 token = lexer->getNextToken();
102 Lisp* result = new(obst) Lisp(Lisp::TYPE_CONS);
103 result->v.cons.car = read();
104 result->v.cons.cdr = 0;
113 Parser::parse_error(const char* msg) const
115 std::stringstream emsg;
116 emsg << "Parse Error at '" << filename << "' line " << lexer->getLineNumber()
118 throw std::runtime_error(emsg.str());
126 case Lexer::TOKEN_EOF: {
127 parse_error("Unexpected EOF.");
129 case Lexer::TOKEN_CLOSE_PAREN: {
130 parse_error("Unexpected ')'.");
132 case Lexer::TOKEN_OPEN_PAREN: {
133 result = new(obst) Lisp(Lisp::TYPE_CONS);
135 token = lexer->getNextToken();
136 if(token == Lexer::TOKEN_CLOSE_PAREN) {
137 result->v.cons.car = 0;
138 result->v.cons.cdr = 0;
142 if(token == Lexer::TOKEN_SYMBOL &&
143 strcmp(lexer->getString(), "_") == 0) {
144 // evaluate translation function (_ str) in place here
145 token = lexer->getNextToken();
146 if(token != Lexer::TOKEN_STRING)
147 parse_error("Expected string after '(_'");
149 result = new(obst) Lisp(Lisp::TYPE_STRING);
151 std::string translation = dictionary->translate(lexer->getString());
152 result->v.string = new(obst) char[translation.size()+1];
153 memcpy(result->v.string, translation.c_str(), translation.size()+1);
155 size_t len = strlen(lexer->getString()) + 1;
156 result->v.string = new(obst) char[len];
157 memcpy(result->v.string, lexer->getString(), len);
159 token = lexer->getNextToken();
160 if(token != Lexer::TOKEN_CLOSE_PAREN)
161 parse_error("Expected ')' after '(_ string'");
167 cur->v.cons.car = read();
168 if(token == Lexer::TOKEN_CLOSE_PAREN) {
172 Lisp *newcur = new(obst) Lisp(Lisp::TYPE_CONS);
173 cur->v.cons.cdr = newcur;
179 case Lexer::TOKEN_SYMBOL: {
180 result = new(obst) Lisp(Lisp::TYPE_SYMBOL);
181 size_t len = strlen(lexer->getString()) + 1;
182 result->v.string = new(obst) char[len];
183 memcpy(result->v.string, lexer->getString(), len);
186 case Lexer::TOKEN_STRING: {
187 result = new(obst) Lisp(Lisp::TYPE_STRING);
188 size_t len = strlen(lexer->getString()) + 1;
189 result->v.string = new(obst) char[len];
190 memcpy(result->v.string, lexer->getString(), len);
193 case Lexer::TOKEN_INTEGER:
194 result = new(obst) Lisp(Lisp::TYPE_INTEGER);
195 result->v.integer = atoi(lexer->getString());
197 case Lexer::TOKEN_REAL:
198 result = new(obst) Lisp(Lisp::TYPE_REAL);
199 result->v.real = strtof(lexer->getString(), NULL);
201 case Lexer::TOKEN_TRUE:
202 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
203 result->v.boolean = true;
205 case Lexer::TOKEN_FALSE:
206 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
207 result->v.boolean = false;
211 // this should never happen
216 token = lexer->getNextToken();
220 } // end of namespace lisp