Use NDEBUG a bit less.
[supertux.git] / src / lisp / parser.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include <sstream>
18 #include <stdexcept>
19 #include <tinygettext/tinygettext.hpp>
20
21 #include "lisp/lisp.hpp"
22 #include "lisp/parser.hpp"
23 #include "util/obstackpp.hpp"
24 #include "physfs/ifile_stream.hpp"
25 #include "physfs/ifile_streambuf.hpp"
26 #include "supertux/globals.hpp"
27
28 #include "supertux/gameconfig.hpp"
29
30 namespace lisp {
31
32 Parser::Parser(bool translate) :
33   lexer(0), 
34   filename(),
35   dictionary_manager(0), 
36   dictionary(0),
37   token(),
38   obst()
39 {
40   if(translate) {
41     dictionary_manager = new tinygettext::DictionaryManager();
42     dictionary_manager->set_charset("UTF-8");
43     if (g_config && (g_config->locale != "")) 
44       dictionary_manager->set_language(tinygettext::Language::from_name(g_config->locale));
45   }
46
47   obstack_init(&obst);
48 }
49
50 Parser::~Parser()
51 {
52   obstack_free(&obst, NULL);
53   delete lexer;
54   delete dictionary_manager;
55 }
56
57 static std::string dirname(const std::string& filename)
58 {
59   std::string::size_type p = filename.find_last_of('/');
60   if(p == std::string::npos)
61     return "";
62
63   return filename.substr(0, p+1);
64 }
65
66 const Lisp*
67 Parser::parse(const std::string& filename)
68 {
69   IFileStreambuf ins(filename);
70   std::istream in(&ins);
71
72   if(!in.good()) {
73     std::stringstream msg;
74     msg << "Parser problem: Couldn't open file '" << filename << "'.";
75     throw std::runtime_error(msg.str());
76   }
77
78   if(dictionary_manager) {
79     dictionary_manager->add_directory(dirname(filename));
80     dictionary = & (dictionary_manager->get_dictionary());
81   }
82
83   return parse(in, filename);
84 }
85
86 const Lisp*
87 Parser::parse(std::istream& stream, const std::string& sourcename)
88 {
89   delete lexer;
90   lexer = new Lexer(stream);
91
92   this->filename = sourcename;
93   token = lexer->getNextToken();
94
95   Lisp* result = new(obst) Lisp(Lisp::TYPE_CONS);
96   result->v.cons.car = read();
97   result->v.cons.cdr = 0;
98
99   delete lexer;
100   lexer = 0;
101
102   return result;
103 }
104
105 void
106 Parser::parse_error(const char* msg) const
107 {
108   std::stringstream emsg;
109   emsg << "Parse Error at '" << filename << "' line " << lexer->getLineNumber()
110        << ": " << msg;
111   throw std::runtime_error(emsg.str());
112 }
113
114 const Lisp*
115 Parser::read()
116 {
117   Lisp* result;
118   switch(token) {
119     case Lexer::TOKEN_EOF: {
120       parse_error("Unexpected EOF.");
121     }
122     case Lexer::TOKEN_CLOSE_PAREN: {
123       parse_error("Unexpected ')'.");
124     }
125     case Lexer::TOKEN_OPEN_PAREN: {
126       result = new(obst) Lisp(Lisp::TYPE_CONS);
127
128       token = lexer->getNextToken();
129       if(token == Lexer::TOKEN_CLOSE_PAREN) {
130         result->v.cons.car = 0;
131         result->v.cons.cdr = 0;
132         break;
133       }
134
135       if(token == Lexer::TOKEN_SYMBOL &&
136          strcmp(lexer->getString(), "_") == 0) {
137         // evaluate translation function (_ str) in place here
138         token = lexer->getNextToken();
139         if(token != Lexer::TOKEN_STRING)
140           parse_error("Expected string after '(_'");
141
142         result = new(obst) Lisp(Lisp::TYPE_STRING);
143         if(dictionary) {
144           std::string translation = dictionary->translate(lexer->getString());
145           result->v.string = new(obst) char[translation.size()+1];
146           memcpy(result->v.string, translation.c_str(), translation.size()+1);
147         } else {
148           size_t len = strlen(lexer->getString()) + 1;
149           result->v.string = new(obst) char[len];
150           memcpy(result->v.string, lexer->getString(), len);
151         }
152         token = lexer->getNextToken();
153         if(token != Lexer::TOKEN_CLOSE_PAREN)
154           parse_error("Expected ')' after '(_ string'");
155         break;
156       }
157
158       Lisp* cur = result;
159       do {
160         cur->v.cons.car = read();
161         if(token == Lexer::TOKEN_CLOSE_PAREN) {
162           cur->v.cons.cdr = 0;
163           break;
164         }
165         Lisp *newcur = new(obst) Lisp(Lisp::TYPE_CONS);
166         cur->v.cons.cdr = newcur;
167         cur = newcur;
168       } while(1);
169
170       break;
171     }
172     case Lexer::TOKEN_SYMBOL: {
173       result = new(obst) Lisp(Lisp::TYPE_SYMBOL);
174       size_t len = strlen(lexer->getString()) + 1;
175       result->v.string = new(obst) char[len];
176       memcpy(result->v.string, lexer->getString(), len);
177       break;
178     }
179     case Lexer::TOKEN_STRING: {
180       result = new(obst) Lisp(Lisp::TYPE_STRING);
181       size_t len = strlen(lexer->getString()) + 1;
182       result->v.string = new(obst) char[len];
183       memcpy(result->v.string, lexer->getString(), len);
184       break;
185     }
186     case Lexer::TOKEN_INTEGER:
187       result = new(obst) Lisp(Lisp::TYPE_INTEGER);
188       sscanf(lexer->getString(), "%d", &result->v.integer);
189       break;
190     case Lexer::TOKEN_REAL:
191       result = new(obst) Lisp(Lisp::TYPE_REAL);
192       sscanf(lexer->getString(), "%f", &result->v.real);
193       break;
194     case Lexer::TOKEN_TRUE:
195       result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
196       result->v.boolean = true;
197       break;
198     case Lexer::TOKEN_FALSE:
199       result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
200       result->v.boolean = false;
201       break;
202
203     default:
204       // this should never happen
205       result = NULL;
206       assert(false);
207   }
208
209   token = lexer->getNextToken();
210   return result;
211 }
212
213 } // end of namespace lisp
214
215 /* EOF */