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 "tinygettext/tinygettext.h"
29 #include "physfs/physfs_stream.h"
30 #include "resources.h"
33 #include "file_system.h"
38 Parser::Parser(bool translate)
39 : lexer(0), dictionary_manager(0), dictionary(0)
42 dictionary_manager = new TinyGetText::DictionaryManager();
43 dictionary_manager->set_charset("UTF-8");
50 delete dictionary_manager;
54 Parser::parse(const std::string& filename)
56 IFileStream in(filename);
58 std::stringstream msg;
59 msg << "Parser problem: Couldn't open file '" << filename << "'.";
60 throw std::runtime_error(msg.str());
63 if(dictionary_manager) {
64 dictionary_manager->add_directory(FileSystem::dirname(filename));
65 dictionary = & (dictionary_manager->get_dictionary());
72 Parser::parse(std::istream& stream)
75 lexer = new Lexer(stream);
77 token = lexer->getNextToken();
78 Lisp* result = new Lisp(Lisp::TYPE_CONS);
79 result->v.cons.car = read();
80 result->v.cons.cdr = 0;
93 case Lexer::TOKEN_EOF: {
94 std::stringstream msg;
95 msg << "Parse Error at line " << lexer->getLineNumber() << ": "
97 throw std::runtime_error(msg.str());
99 case Lexer::TOKEN_CLOSE_PAREN: {
100 std::stringstream msg;
101 msg << "Parse Error at line " << lexer->getLineNumber() << ": "
102 << "Unexpected ')'.";
103 throw std::runtime_error(msg.str());
105 case Lexer::TOKEN_OPEN_PAREN: {
106 result = new Lisp(Lisp::TYPE_CONS);
108 token = lexer->getNextToken();
109 if(token == Lexer::TOKEN_CLOSE_PAREN) {
110 result->v.cons.car = 0;
111 result->v.cons.cdr = 0;
115 if(token == Lexer::TOKEN_SYMBOL &&
116 strcmp(lexer->getString(), "_") == 0) {
117 // evaluate translation function (_ str) in place here
118 token = lexer->getNextToken();
119 if(token != Lexer::TOKEN_STRING)
120 throw std::runtime_error("Expected string after '(_'");
122 result = new Lisp(Lisp::TYPE_STRING);
124 std::string translation = dictionary->translate(lexer->getString());
125 result->v.string = new char[translation.size()+1];
126 memcpy(result->v.string, translation.c_str(), translation.size()+1);
128 size_t len = strlen(lexer->getString()) + 1;
129 result->v.string = new char[len];
130 memcpy(result->v.string, lexer->getString(), len);
132 token = lexer->getNextToken();
133 if(token != Lexer::TOKEN_CLOSE_PAREN)
134 throw std::runtime_error("Expected ')' after '(_ string'");
140 cur->v.cons.car = read();
141 if(token == Lexer::TOKEN_CLOSE_PAREN) {
145 cur->v.cons.cdr = new Lisp(Lisp::TYPE_CONS);
146 cur = cur->v.cons.cdr;
151 case Lexer::TOKEN_SYMBOL: {
152 result = new Lisp(Lisp::TYPE_SYMBOL);
153 size_t len = strlen(lexer->getString()) + 1;
154 result->v.string = new char[len];
155 memcpy(result->v.string, lexer->getString(), len);
158 case Lexer::TOKEN_STRING: {
159 result = new Lisp(Lisp::TYPE_STRING);
160 size_t len = strlen(lexer->getString()) + 1;
161 result->v.string = new char[len];
162 memcpy(result->v.string, lexer->getString(), len);
165 case Lexer::TOKEN_INTEGER:
166 result = new Lisp(Lisp::TYPE_INTEGER);
167 sscanf(lexer->getString(), "%d", &result->v.integer);
169 case Lexer::TOKEN_REAL:
170 result = new Lisp(Lisp::TYPE_REAL);
171 sscanf(lexer->getString(), "%f", &result->v.real);
173 case Lexer::TOKEN_TRUE:
174 result = new Lisp(Lisp::TYPE_BOOLEAN);
175 result->v.boolean = true;
177 case Lexer::TOKEN_FALSE:
178 result = new Lisp(Lisp::TYPE_BOOLEAN);
179 result->v.boolean = false;
183 // this should never happen
187 token = lexer->getNextToken();
191 } // end of namespace lisp