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/gettext.hpp"
25 #include "util/obstackpp.hpp"
26 #include "physfs/ifile_stream.hpp"
27 #include "physfs/ifile_streambuf.hpp"
28 #include "supertux/globals.hpp"
30 #include "supertux/gameconfig.hpp"
34 Parser::Parser(bool translate) :
37 dictionary_manager(0),
44 dictionary_manager = new tinygettext::DictionaryManager();
45 dictionary_manager->set_charset("UTF-8");
47 if (g_config->locale != "") {
48 dictionary_manager->set_language(tinygettext::Language::from_name(g_config->locale));
50 else if(g_dictionary_manager && g_dictionary_manager->get_language()) {
51 // Language set to auto-detect?
52 dictionary_manager->set_language(g_dictionary_manager->get_language());
58 searchpath = PHYSFS_getSearchPath();
63 obstack_free(&obst, NULL);
65 delete dictionary_manager;
66 PHYSFS_freeList(searchpath);
69 static std::string dirname(const std::string& filename)
71 std::string::size_type p = filename.find_last_of('/');
72 if(p == std::string::npos)
75 return filename.substr(0, p+1);
79 Parser::parse(const std::string& filename_)
81 IFileStreambuf ins(filename_);
82 std::istream in(&ins);
85 std::stringstream msg;
86 msg << "Parser problem: Couldn't open file '" << filename_ << "'.";
87 throw std::runtime_error(msg.str());
90 if(dictionary_manager) {
91 std::string rel_dir = dirname (filename_);
92 for(char** i = searchpath; *i != NULL; i++)
94 std::string abs_dir = std::string (*i) + PHYSFS_getDirSeparator () + rel_dir;
95 dictionary_manager->add_directory (abs_dir);
97 dictionary = & (dictionary_manager->get_dictionary());
100 return parse(in, filename_);
104 Parser::parse(std::istream& stream, const std::string& sourcename)
107 lexer = new Lexer(stream);
109 this->filename = sourcename;
110 token = lexer->getNextToken();
112 Lisp* result = new(obst) Lisp(Lisp::TYPE_CONS);
113 result->v.cons.car = read();
114 result->v.cons.cdr = 0;
123 Parser::parse_error(const char* msg) const
125 std::stringstream emsg;
126 emsg << "Parse Error at '" << filename << "' line " << lexer->getLineNumber()
128 throw std::runtime_error(emsg.str());
136 case Lexer::TOKEN_EOF: {
137 parse_error("Unexpected EOF.");
139 case Lexer::TOKEN_CLOSE_PAREN: {
140 parse_error("Unexpected ')'.");
142 case Lexer::TOKEN_OPEN_PAREN: {
143 result = new(obst) Lisp(Lisp::TYPE_CONS);
145 token = lexer->getNextToken();
146 if(token == Lexer::TOKEN_CLOSE_PAREN) {
147 result->v.cons.car = 0;
148 result->v.cons.cdr = 0;
152 if(token == Lexer::TOKEN_SYMBOL &&
153 strcmp(lexer->getString(), "_") == 0) {
154 // evaluate translation function (_ str) in place here
155 token = lexer->getNextToken();
156 if(token != Lexer::TOKEN_STRING)
157 parse_error("Expected string after '(_'");
159 result = new(obst) Lisp(Lisp::TYPE_STRING);
161 std::string translation = dictionary->translate(lexer->getString());
162 result->v.string = new(obst) char[translation.size()+1];
163 memcpy(result->v.string, translation.c_str(), translation.size()+1);
165 size_t len = strlen(lexer->getString()) + 1;
166 result->v.string = new(obst) char[len];
167 memcpy(result->v.string, lexer->getString(), len);
169 token = lexer->getNextToken();
170 if(token != Lexer::TOKEN_CLOSE_PAREN)
171 parse_error("Expected ')' after '(_ string'");
177 cur->v.cons.car = read();
178 if(token == Lexer::TOKEN_CLOSE_PAREN) {
182 Lisp *newcur = new(obst) Lisp(Lisp::TYPE_CONS);
183 cur->v.cons.cdr = newcur;
189 case Lexer::TOKEN_SYMBOL: {
190 result = new(obst) Lisp(Lisp::TYPE_SYMBOL);
191 size_t len = strlen(lexer->getString()) + 1;
192 result->v.string = new(obst) char[len];
193 memcpy(result->v.string, lexer->getString(), len);
196 case Lexer::TOKEN_STRING: {
197 result = new(obst) Lisp(Lisp::TYPE_STRING);
198 size_t len = strlen(lexer->getString()) + 1;
199 result->v.string = new(obst) char[len];
200 memcpy(result->v.string, lexer->getString(), len);
203 case Lexer::TOKEN_INTEGER:
204 result = new(obst) Lisp(Lisp::TYPE_INTEGER);
205 result->v.integer = atoi(lexer->getString());
207 case Lexer::TOKEN_REAL:
208 result = new(obst) Lisp(Lisp::TYPE_REAL);
209 result->v.real = strtof(lexer->getString(), NULL);
211 case Lexer::TOKEN_TRUE:
212 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
213 result->v.boolean = true;
215 case Lexer::TOKEN_FALSE:
216 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
217 result->v.boolean = false;
221 // this should never happen
226 token = lexer->getNextToken();
230 } // end of namespace lisp