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),
43 dictionary_manager = new tinygettext::DictionaryManager();
44 dictionary_manager->set_charset("UTF-8");
45 if (g_config && (g_config->locale != ""))
46 dictionary_manager->set_language(tinygettext::Language::from_name(g_config->locale));
50 searchpath = PHYSFS_getSearchPath();
55 obstack_free(&obst, NULL);
57 delete dictionary_manager;
58 PHYSFS_freeList(searchpath);
61 static std::string dirname(const std::string& filename)
63 std::string::size_type p = filename.find_last_of('/');
64 if(p == std::string::npos)
67 return filename.substr(0, p+1);
71 Parser::parse(const std::string& filename)
73 IFileStreambuf ins(filename);
74 std::istream in(&ins);
77 std::stringstream msg;
78 msg << "Parser problem: Couldn't open file '" << filename << "'.";
79 throw std::runtime_error(msg.str());
82 if(dictionary_manager) {
83 std::string rel_dir = dirname (filename);
84 for(char** i = searchpath; *i != NULL; i++)
86 std::string abs_dir = std::string (*i) + PHYSFS_getDirSeparator () + rel_dir;
87 dictionary_manager->add_directory (abs_dir);
89 dictionary = & (dictionary_manager->get_dictionary());
92 return parse(in, filename);
96 Parser::parse(std::istream& stream, const std::string& sourcename)
99 lexer = new Lexer(stream);
101 this->filename = sourcename;
102 token = lexer->getNextToken();
104 Lisp* result = new(obst) Lisp(Lisp::TYPE_CONS);
105 result->v.cons.car = read();
106 result->v.cons.cdr = 0;
115 Parser::parse_error(const char* msg) const
117 std::stringstream emsg;
118 emsg << "Parse Error at '" << filename << "' line " << lexer->getLineNumber()
120 throw std::runtime_error(emsg.str());
128 case Lexer::TOKEN_EOF: {
129 parse_error("Unexpected EOF.");
131 case Lexer::TOKEN_CLOSE_PAREN: {
132 parse_error("Unexpected ')'.");
134 case Lexer::TOKEN_OPEN_PAREN: {
135 result = new(obst) Lisp(Lisp::TYPE_CONS);
137 token = lexer->getNextToken();
138 if(token == Lexer::TOKEN_CLOSE_PAREN) {
139 result->v.cons.car = 0;
140 result->v.cons.cdr = 0;
144 if(token == Lexer::TOKEN_SYMBOL &&
145 strcmp(lexer->getString(), "_") == 0) {
146 // evaluate translation function (_ str) in place here
147 token = lexer->getNextToken();
148 if(token != Lexer::TOKEN_STRING)
149 parse_error("Expected string after '(_'");
151 result = new(obst) Lisp(Lisp::TYPE_STRING);
153 std::string translation = dictionary->translate(lexer->getString());
154 result->v.string = new(obst) char[translation.size()+1];
155 memcpy(result->v.string, translation.c_str(), translation.size()+1);
157 size_t len = strlen(lexer->getString()) + 1;
158 result->v.string = new(obst) char[len];
159 memcpy(result->v.string, lexer->getString(), len);
161 token = lexer->getNextToken();
162 if(token != Lexer::TOKEN_CLOSE_PAREN)
163 parse_error("Expected ')' after '(_ string'");
169 cur->v.cons.car = read();
170 if(token == Lexer::TOKEN_CLOSE_PAREN) {
174 Lisp *newcur = new(obst) Lisp(Lisp::TYPE_CONS);
175 cur->v.cons.cdr = newcur;
181 case Lexer::TOKEN_SYMBOL: {
182 result = new(obst) Lisp(Lisp::TYPE_SYMBOL);
183 size_t len = strlen(lexer->getString()) + 1;
184 result->v.string = new(obst) char[len];
185 memcpy(result->v.string, lexer->getString(), len);
188 case Lexer::TOKEN_STRING: {
189 result = new(obst) Lisp(Lisp::TYPE_STRING);
190 size_t len = strlen(lexer->getString()) + 1;
191 result->v.string = new(obst) char[len];
192 memcpy(result->v.string, lexer->getString(), len);
195 case Lexer::TOKEN_INTEGER:
196 result = new(obst) Lisp(Lisp::TYPE_INTEGER);
197 result->v.integer = atoi(lexer->getString());
199 case Lexer::TOKEN_REAL:
200 result = new(obst) Lisp(Lisp::TYPE_REAL);
201 result->v.real = strtof(lexer->getString(), NULL);
203 case Lexer::TOKEN_TRUE:
204 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
205 result->v.boolean = true;
207 case Lexer::TOKEN_FALSE:
208 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
209 result->v.boolean = false;
213 // this should never happen
218 token = lexer->getNextToken();
222 } // end of namespace lisp