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"
31 #include "file_system.h"
36 Parser::Parser(bool translate)
37 : lexer(0), dictionary_manager(0), dictionary(0)
40 dictionary_manager = new TinyGetText::DictionaryManager();
41 dictionary_manager->set_charset("UTF-8");
48 delete dictionary_manager;
52 Parser::parse(const std::string& filename)
54 std::ifstream in(filename.c_str());
56 std::stringstream msg;
57 msg << "Parser problem: Couldn't open file '" << filename << "'.";
58 throw std::runtime_error(msg.str());
61 if(dictionary_manager) {
62 dictionary_manager->add_directory(FileSystem::dirname(filename));
63 dictionary = & (dictionary_manager->get_dictionary());
70 Parser::parse(std::istream& stream)
73 lexer = new Lexer(stream);
75 token = lexer->getNextToken();
76 Lisp* result = new Lisp(Lisp::TYPE_CONS);
77 result->v.cons.car = read();
78 result->v.cons.cdr = 0;
91 case Lexer::TOKEN_EOF: {
92 std::stringstream msg;
93 msg << "Parse Error at line " << lexer->getLineNumber() << ": "
95 throw std::runtime_error(msg.str());
97 case Lexer::TOKEN_CLOSE_PAREN: {
98 std::stringstream msg;
99 msg << "Parse Error at line " << lexer->getLineNumber() << ": "
100 << "Unexpected ')'.";
101 throw std::runtime_error(msg.str());
103 case Lexer::TOKEN_OPEN_PAREN: {
104 result = new Lisp(Lisp::TYPE_CONS);
106 token = lexer->getNextToken();
107 if(token == Lexer::TOKEN_CLOSE_PAREN) {
108 result->v.cons.car = 0;
109 result->v.cons.cdr = 0;
113 if(token == Lexer::TOKEN_SYMBOL &&
114 strcmp(lexer->getString(), "_") == 0) {
115 // evaluate translation function (_ str) in place here
116 token = lexer->getNextToken();
117 if(token != Lexer::TOKEN_STRING)
118 throw new std::runtime_error("Expected string after '(_'");
120 result = new Lisp(Lisp::TYPE_STRING);
122 std::string translation = dictionary->translate(lexer->getString());
123 result->v.string = new char[translation.size()+1];
124 memcpy(result->v.string, translation.c_str(), translation.size()+1);
126 size_t len = strlen(lexer->getString()) + 1;
127 result->v.string = new char[len];
128 memcpy(result->v.string, lexer->getString(), len);
130 token = lexer->getNextToken();
131 if(token != Lexer::TOKEN_CLOSE_PAREN)
132 throw new std::runtime_error("Expected ')' after '(_ string'");
138 cur->v.cons.car = read();
139 if(token == Lexer::TOKEN_CLOSE_PAREN) {
143 cur->v.cons.cdr = new Lisp(Lisp::TYPE_CONS);
144 cur = cur->v.cons.cdr;
149 case Lexer::TOKEN_SYMBOL: {
150 result = new Lisp(Lisp::TYPE_SYMBOL);
151 size_t len = strlen(lexer->getString()) + 1;
152 result->v.string = new char[len];
153 memcpy(result->v.string, lexer->getString(), len);
156 case Lexer::TOKEN_STRING: {
157 result = new Lisp(Lisp::TYPE_STRING);
158 size_t len = strlen(lexer->getString()) + 1;
159 result->v.string = new char[len];
160 memcpy(result->v.string, lexer->getString(), len);
163 case Lexer::TOKEN_INTEGER:
164 result = new Lisp(Lisp::TYPE_INTEGER);
165 sscanf(lexer->getString(), "%d", &result->v.integer);
167 case Lexer::TOKEN_REAL:
168 result = new Lisp(Lisp::TYPE_REAL);
169 sscanf(lexer->getString(), "%f", &result->v.real);
171 case Lexer::TOKEN_TRUE:
172 result = new Lisp(Lisp::TYPE_BOOLEAN);
173 result->v.boolean = true;
175 case Lexer::TOKEN_FALSE:
176 result = new Lisp(Lisp::TYPE_BOOLEAN);
177 result->v.boolean = false;
181 // this should never happen
185 token = lexer->getNextToken();
189 } // end of namespace lisp