3 // TuxKart - a fun racing game with go-kart
4 // Copyright (C) 2004 Matthias Braun <matze@braunis.de>
5 // code in this file based on lispreader from Mark Probst
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "app/setup.h"
29 #include "app/tinygettext.h"
36 Parser::Parser(bool translate)
37 : lexer(0), dictionary_manager(0), dictionary(0)
40 dictionary_manager = new TinyGetText::DictionaryManager();
47 delete dictionary_manager;
51 Parser::parse(const std::string& filename)
53 std::ifstream in(filename.c_str());
55 std::stringstream msg;
56 msg << "Parser problem: Couldn't open file '" << filename << "'.";
57 throw std::runtime_error(msg.str());
60 if(dictionary_manager) {
61 dictionary_manager->add_directory(SuperTux::FileSystem::dirname(filename));
62 dictionary = & (dictionary_manager->get_dictionary());
69 Parser::parse(std::istream& stream)
72 lexer = new Lexer(stream);
74 token = lexer->getNextToken();
75 Lisp* result = new Lisp(Lisp::TYPE_CONS);
76 result->v.cons.car = read();
77 result->v.cons.cdr = 0;
90 case Lexer::TOKEN_EOF: {
91 std::stringstream msg;
92 msg << "Parse Error at line " << lexer->getLineNumber() << ": "
94 throw std::runtime_error(msg.str());
96 case Lexer::TOKEN_CLOSE_PAREN: {
97 std::stringstream msg;
98 msg << "Parse Error at line " << lexer->getLineNumber() << ": "
100 throw std::runtime_error(msg.str());
102 case Lexer::TOKEN_OPEN_PAREN: {
103 result = new Lisp(Lisp::TYPE_CONS);
105 token = lexer->getNextToken();
106 if(token == Lexer::TOKEN_CLOSE_PAREN) {
107 result->v.cons.car = 0;
108 result->v.cons.cdr = 0;
112 if(token == Lexer::TOKEN_SYMBOL &&
113 strcmp(lexer->getString(), "_") == 0) {
114 // evaluate translation function (_ str) in place here
115 token = lexer->getNextToken();
116 if(token != Lexer::TOKEN_STRING)
117 throw new std::runtime_error("Expected string after '(_'");
119 result = new Lisp(Lisp::TYPE_STRING);
121 std::string translation = dictionary->translate(lexer->getString());
122 result->v.string = new char[translation.size()+1];
123 memcpy(result->v.string, translation.c_str(), translation.size()+1);
125 size_t len = strlen(lexer->getString()) + 1;
126 result->v.string = new char[len];
127 memcpy(result->v.string, lexer->getString(), len);
129 token = lexer->getNextToken();
130 if(token != Lexer::TOKEN_CLOSE_PAREN)
131 throw new std::runtime_error("Expected ')' after '(_ string'");
137 cur->v.cons.car = read();
138 if(token == Lexer::TOKEN_CLOSE_PAREN) {
142 cur->v.cons.cdr = new Lisp(Lisp::TYPE_CONS);
143 cur = cur->v.cons.cdr;
148 case Lexer::TOKEN_SYMBOL: {
149 result = new Lisp(Lisp::TYPE_SYMBOL);
150 size_t len = strlen(lexer->getString()) + 1;
151 result->v.string = new char[len];
152 memcpy(result->v.string, lexer->getString(), len);
155 case Lexer::TOKEN_STRING: {
156 result = new Lisp(Lisp::TYPE_STRING);
157 size_t len = strlen(lexer->getString()) + 1;
158 result->v.string = new char[len];
159 memcpy(result->v.string, lexer->getString(), len);
162 case Lexer::TOKEN_INTEGER:
163 result = new Lisp(Lisp::TYPE_INTEGER);
164 sscanf(lexer->getString(), "%d", &result->v.integer);
166 case Lexer::TOKEN_REAL:
167 result = new Lisp(Lisp::TYPE_REAL);
168 sscanf(lexer->getString(), "%f", &result->v.real);
170 case Lexer::TOKEN_TRUE:
171 result = new Lisp(Lisp::TYPE_BOOLEAN);
172 result->v.boolean = true;
174 case Lexer::TOKEN_FALSE:
175 result = new Lisp(Lisp::TYPE_BOOLEAN);
176 result->v.boolean = false;
180 // this should never happen
184 token = lexer->getNextToken();
188 } // end of namespace lisp