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/>.
20 #include "lisp/lisp.hpp"
21 #include "lisp/parser.hpp"
22 #include "obstack/obstackpp.hpp"
23 #include "physfs/physfs_stream.hpp"
24 #include "tinygettext/tinygettext.hpp"
26 #include "supertux/gameconfig.hpp"
30 Parser::Parser(bool translate) :
33 dictionary_manager(0),
39 dictionary_manager = new TinyGetText::DictionaryManager();
40 dictionary_manager->set_charset("UTF-8");
41 if (g_config && (g_config->locale != ""))
42 dictionary_manager->set_language(g_config->locale);
50 obstack_free(&obst, NULL);
52 delete dictionary_manager;
55 static std::string dirname(const std::string& filename)
57 std::string::size_type p = filename.find_last_of('/');
58 if(p == std::string::npos)
61 return filename.substr(0, p+1);
65 Parser::parse(const std::string& filename)
67 IFileStreambuf ins(filename);
68 std::istream in(&ins);
71 std::stringstream msg;
72 msg << "Parser problem: Couldn't open file '" << filename << "'.";
73 throw std::runtime_error(msg.str());
76 if(dictionary_manager) {
77 dictionary_manager->add_directory(dirname(filename));
78 dictionary = & (dictionary_manager->get_dictionary());
81 return parse(in, filename);
85 Parser::parse(std::istream& stream, const std::string& sourcename)
88 lexer = new Lexer(stream);
90 this->filename = sourcename;
91 token = lexer->getNextToken();
93 Lisp* result = new(obst) Lisp(Lisp::TYPE_CONS);
94 result->v.cons.car = read();
95 result->v.cons.cdr = 0;
104 Parser::parse_error(const char* msg) const
106 std::stringstream emsg;
107 emsg << "Parse Error at '" << filename << "' line " << lexer->getLineNumber()
109 throw std::runtime_error(emsg.str());
117 case Lexer::TOKEN_EOF: {
118 parse_error("Unexpected EOF.");
120 case Lexer::TOKEN_CLOSE_PAREN: {
121 parse_error("Unexpected ')'.");
123 case Lexer::TOKEN_OPEN_PAREN: {
124 result = new(obst) Lisp(Lisp::TYPE_CONS);
126 token = lexer->getNextToken();
127 if(token == Lexer::TOKEN_CLOSE_PAREN) {
128 result->v.cons.car = 0;
129 result->v.cons.cdr = 0;
133 if(token == Lexer::TOKEN_SYMBOL &&
134 strcmp(lexer->getString(), "_") == 0) {
135 // evaluate translation function (_ str) in place here
136 token = lexer->getNextToken();
137 if(token != Lexer::TOKEN_STRING)
138 parse_error("Expected string after '(_'");
140 result = new(obst) Lisp(Lisp::TYPE_STRING);
142 std::string translation = dictionary->translate(lexer->getString());
143 result->v.string = new(obst) char[translation.size()+1];
144 memcpy(result->v.string, translation.c_str(), translation.size()+1);
146 size_t len = strlen(lexer->getString()) + 1;
147 result->v.string = new(obst) char[len];
148 memcpy(result->v.string, lexer->getString(), len);
150 token = lexer->getNextToken();
151 if(token != Lexer::TOKEN_CLOSE_PAREN)
152 parse_error("Expected ')' after '(_ string'");
158 cur->v.cons.car = read();
159 if(token == Lexer::TOKEN_CLOSE_PAREN) {
163 Lisp *newcur = new(obst) Lisp(Lisp::TYPE_CONS);
164 cur->v.cons.cdr = newcur;
170 case Lexer::TOKEN_SYMBOL: {
171 result = new(obst) Lisp(Lisp::TYPE_SYMBOL);
172 size_t len = strlen(lexer->getString()) + 1;
173 result->v.string = new(obst) char[len];
174 memcpy(result->v.string, lexer->getString(), len);
177 case Lexer::TOKEN_STRING: {
178 result = new(obst) Lisp(Lisp::TYPE_STRING);
179 size_t len = strlen(lexer->getString()) + 1;
180 result->v.string = new(obst) char[len];
181 memcpy(result->v.string, lexer->getString(), len);
184 case Lexer::TOKEN_INTEGER:
185 result = new(obst) Lisp(Lisp::TYPE_INTEGER);
186 sscanf(lexer->getString(), "%d", &result->v.integer);
188 case Lexer::TOKEN_REAL:
189 result = new(obst) Lisp(Lisp::TYPE_REAL);
190 sscanf(lexer->getString(), "%f", &result->v.real);
192 case Lexer::TOKEN_TRUE:
193 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
194 result->v.boolean = true;
196 case Lexer::TOKEN_FALSE:
197 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
198 result->v.boolean = false;
202 // this should never happen
206 token = lexer->getNextToken();
210 } // end of namespace lisp