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)
31 : lexer(0), dictionary_manager(0), dictionary(0)
34 dictionary_manager = new TinyGetText::DictionaryManager();
35 dictionary_manager->set_charset("UTF-8");
36 if (g_config && (g_config->locale != ""))
37 dictionary_manager->set_language(g_config->locale);
45 obstack_free(&obst, NULL);
47 delete dictionary_manager;
50 static std::string dirname(const std::string& filename)
52 std::string::size_type p = filename.find_last_of('/');
53 if(p == std::string::npos)
56 return filename.substr(0, p+1);
60 Parser::parse(const std::string& filename)
62 IFileStreambuf ins(filename);
63 std::istream in(&ins);
66 std::stringstream msg;
67 msg << "Parser problem: Couldn't open file '" << filename << "'.";
68 throw std::runtime_error(msg.str());
71 if(dictionary_manager) {
72 dictionary_manager->add_directory(dirname(filename));
73 dictionary = & (dictionary_manager->get_dictionary());
76 return parse(in, filename);
80 Parser::parse(std::istream& stream, const std::string& sourcename)
83 lexer = new Lexer(stream);
85 this->filename = sourcename;
86 token = lexer->getNextToken();
88 Lisp* result = new(obst) Lisp(Lisp::TYPE_CONS);
89 result->v.cons.car = read();
90 result->v.cons.cdr = 0;
99 Parser::parse_error(const char* msg) const
101 std::stringstream emsg;
102 emsg << "Parse Error at '" << filename << "' line " << lexer->getLineNumber()
104 throw std::runtime_error(emsg.str());
112 case Lexer::TOKEN_EOF: {
113 parse_error("Unexpected EOF.");
115 case Lexer::TOKEN_CLOSE_PAREN: {
116 parse_error("Unexpected ')'.");
118 case Lexer::TOKEN_OPEN_PAREN: {
119 result = new(obst) Lisp(Lisp::TYPE_CONS);
121 token = lexer->getNextToken();
122 if(token == Lexer::TOKEN_CLOSE_PAREN) {
123 result->v.cons.car = 0;
124 result->v.cons.cdr = 0;
128 if(token == Lexer::TOKEN_SYMBOL &&
129 strcmp(lexer->getString(), "_") == 0) {
130 // evaluate translation function (_ str) in place here
131 token = lexer->getNextToken();
132 if(token != Lexer::TOKEN_STRING)
133 parse_error("Expected string after '(_'");
135 result = new(obst) Lisp(Lisp::TYPE_STRING);
137 std::string translation = dictionary->translate(lexer->getString());
138 result->v.string = new(obst) char[translation.size()+1];
139 memcpy(result->v.string, translation.c_str(), translation.size()+1);
141 size_t len = strlen(lexer->getString()) + 1;
142 result->v.string = new(obst) char[len];
143 memcpy(result->v.string, lexer->getString(), len);
145 token = lexer->getNextToken();
146 if(token != Lexer::TOKEN_CLOSE_PAREN)
147 parse_error("Expected ')' after '(_ string'");
153 cur->v.cons.car = read();
154 if(token == Lexer::TOKEN_CLOSE_PAREN) {
158 Lisp *newcur = new(obst) Lisp(Lisp::TYPE_CONS);
159 cur->v.cons.cdr = newcur;
165 case Lexer::TOKEN_SYMBOL: {
166 result = new(obst) Lisp(Lisp::TYPE_SYMBOL);
167 size_t len = strlen(lexer->getString()) + 1;
168 result->v.string = new(obst) char[len];
169 memcpy(result->v.string, lexer->getString(), len);
172 case Lexer::TOKEN_STRING: {
173 result = new(obst) Lisp(Lisp::TYPE_STRING);
174 size_t len = strlen(lexer->getString()) + 1;
175 result->v.string = new(obst) char[len];
176 memcpy(result->v.string, lexer->getString(), len);
179 case Lexer::TOKEN_INTEGER:
180 result = new(obst) Lisp(Lisp::TYPE_INTEGER);
181 sscanf(lexer->getString(), "%d", &result->v.integer);
183 case Lexer::TOKEN_REAL:
184 result = new(obst) Lisp(Lisp::TYPE_REAL);
185 sscanf(lexer->getString(), "%f", &result->v.real);
187 case Lexer::TOKEN_TRUE:
188 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
189 result->v.boolean = true;
191 case Lexer::TOKEN_FALSE:
192 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
193 result->v.boolean = false;
197 // this should never happen
201 token = lexer->getNextToken();
205 } // end of namespace lisp