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/ifile_stream.hpp"
24 #include "physfs/ifile_streambuf.hpp"
25 #include "tinygettext/tinygettext.hpp"
27 #include "supertux/gameconfig.hpp"
31 Parser::Parser(bool translate) :
34 dictionary_manager(0),
40 dictionary_manager = new TinyGetText::DictionaryManager();
41 dictionary_manager->set_charset("UTF-8");
42 if (g_config && (g_config->locale != ""))
43 dictionary_manager->set_language(g_config->locale);
51 obstack_free(&obst, NULL);
53 delete dictionary_manager;
56 static std::string dirname(const std::string& filename)
58 std::string::size_type p = filename.find_last_of('/');
59 if(p == std::string::npos)
62 return filename.substr(0, p+1);
66 Parser::parse(const std::string& filename)
68 IFileStreambuf ins(filename);
69 std::istream in(&ins);
72 std::stringstream msg;
73 msg << "Parser problem: Couldn't open file '" << filename << "'.";
74 throw std::runtime_error(msg.str());
77 if(dictionary_manager) {
78 dictionary_manager->add_directory(dirname(filename));
79 dictionary = & (dictionary_manager->get_dictionary());
82 return parse(in, filename);
86 Parser::parse(std::istream& stream, const std::string& sourcename)
89 lexer = new Lexer(stream);
91 this->filename = sourcename;
92 token = lexer->getNextToken();
94 Lisp* result = new(obst) Lisp(Lisp::TYPE_CONS);
95 result->v.cons.car = read();
96 result->v.cons.cdr = 0;
105 Parser::parse_error(const char* msg) const
107 std::stringstream emsg;
108 emsg << "Parse Error at '" << filename << "' line " << lexer->getLineNumber()
110 throw std::runtime_error(emsg.str());
118 case Lexer::TOKEN_EOF: {
119 parse_error("Unexpected EOF.");
121 case Lexer::TOKEN_CLOSE_PAREN: {
122 parse_error("Unexpected ')'.");
124 case Lexer::TOKEN_OPEN_PAREN: {
125 result = new(obst) Lisp(Lisp::TYPE_CONS);
127 token = lexer->getNextToken();
128 if(token == Lexer::TOKEN_CLOSE_PAREN) {
129 result->v.cons.car = 0;
130 result->v.cons.cdr = 0;
134 if(token == Lexer::TOKEN_SYMBOL &&
135 strcmp(lexer->getString(), "_") == 0) {
136 // evaluate translation function (_ str) in place here
137 token = lexer->getNextToken();
138 if(token != Lexer::TOKEN_STRING)
139 parse_error("Expected string after '(_'");
141 result = new(obst) Lisp(Lisp::TYPE_STRING);
143 std::string translation = dictionary->translate(lexer->getString());
144 result->v.string = new(obst) char[translation.size()+1];
145 memcpy(result->v.string, translation.c_str(), translation.size()+1);
147 size_t len = strlen(lexer->getString()) + 1;
148 result->v.string = new(obst) char[len];
149 memcpy(result->v.string, lexer->getString(), len);
151 token = lexer->getNextToken();
152 if(token != Lexer::TOKEN_CLOSE_PAREN)
153 parse_error("Expected ')' after '(_ string'");
159 cur->v.cons.car = read();
160 if(token == Lexer::TOKEN_CLOSE_PAREN) {
164 Lisp *newcur = new(obst) Lisp(Lisp::TYPE_CONS);
165 cur->v.cons.cdr = newcur;
171 case Lexer::TOKEN_SYMBOL: {
172 result = new(obst) Lisp(Lisp::TYPE_SYMBOL);
173 size_t len = strlen(lexer->getString()) + 1;
174 result->v.string = new(obst) char[len];
175 memcpy(result->v.string, lexer->getString(), len);
178 case Lexer::TOKEN_STRING: {
179 result = new(obst) Lisp(Lisp::TYPE_STRING);
180 size_t len = strlen(lexer->getString()) + 1;
181 result->v.string = new(obst) char[len];
182 memcpy(result->v.string, lexer->getString(), len);
185 case Lexer::TOKEN_INTEGER:
186 result = new(obst) Lisp(Lisp::TYPE_INTEGER);
187 sscanf(lexer->getString(), "%d", &result->v.integer);
189 case Lexer::TOKEN_REAL:
190 result = new(obst) Lisp(Lisp::TYPE_REAL);
191 sscanf(lexer->getString(), "%f", &result->v.real);
193 case Lexer::TOKEN_TRUE:
194 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
195 result->v.boolean = true;
197 case Lexer::TOKEN_FALSE:
198 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
199 result->v.boolean = false;
203 // this should never happen
207 token = lexer->getNextToken();
211 } // end of namespace lisp