Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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
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"
25
26 #include "supertux/gameconfig.hpp"
27
28 namespace lisp {
29
30 Parser::Parser(bool translate)
31   : lexer(0), dictionary_manager(0), dictionary(0)
32 {
33   if(translate) {
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);
38   }
39
40   obstack_init(&obst);
41 }
42
43 Parser::~Parser()
44 {
45   obstack_free(&obst, NULL);
46   delete lexer;
47   delete dictionary_manager;
48 }
49
50 static std::string dirname(const std::string& filename)
51 {
52   std::string::size_type p = filename.find_last_of('/');
53   if(p == std::string::npos)
54     return "";
55
56   return filename.substr(0, p+1);
57 }
58
59 const Lisp*
60 Parser::parse(const std::string& filename)
61 {
62   IFileStreambuf ins(filename);
63   std::istream in(&ins);
64
65   if(!in.good()) {
66     std::stringstream msg;
67     msg << "Parser problem: Couldn't open file '" << filename << "'.";
68     throw std::runtime_error(msg.str());
69   }
70
71   if(dictionary_manager) {
72     dictionary_manager->add_directory(dirname(filename));
73     dictionary = & (dictionary_manager->get_dictionary());
74   }
75
76   return parse(in, filename);
77 }
78
79 const Lisp*
80 Parser::parse(std::istream& stream, const std::string& sourcename)
81 {
82   delete lexer;
83   lexer = new Lexer(stream);
84
85   this->filename = sourcename;
86   token = lexer->getNextToken();
87
88   Lisp* result = new(obst) Lisp(Lisp::TYPE_CONS);
89   result->v.cons.car = read();
90   result->v.cons.cdr = 0;
91
92   delete lexer;
93   lexer = 0;
94
95   return result;
96 }
97
98 void
99 Parser::parse_error(const char* msg) const
100 {
101   std::stringstream emsg;
102   emsg << "Parse Error at '" << filename << "' line " << lexer->getLineNumber()
103        << ": " << msg;
104   throw std::runtime_error(emsg.str());
105 }
106
107 const Lisp*
108 Parser::read()
109 {
110   Lisp* result;
111   switch(token) {
112     case Lexer::TOKEN_EOF: {
113       parse_error("Unexpected EOF.");
114     }
115     case Lexer::TOKEN_CLOSE_PAREN: {
116       parse_error("Unexpected ')'.");
117     }
118     case Lexer::TOKEN_OPEN_PAREN: {
119       result = new(obst) Lisp(Lisp::TYPE_CONS);
120
121       token = lexer->getNextToken();
122       if(token == Lexer::TOKEN_CLOSE_PAREN) {
123         result->v.cons.car = 0;
124         result->v.cons.cdr = 0;
125         break;
126       }
127
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 '(_'");
134
135         result = new(obst) Lisp(Lisp::TYPE_STRING);
136         if(dictionary) {
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);
140         } else {
141           size_t len = strlen(lexer->getString()) + 1;
142           result->v.string = new(obst) char[len];
143           memcpy(result->v.string, lexer->getString(), len);
144         }
145         token = lexer->getNextToken();
146         if(token != Lexer::TOKEN_CLOSE_PAREN)
147           parse_error("Expected ')' after '(_ string'");
148         break;
149       }
150
151       Lisp* cur = result;
152       do {
153         cur->v.cons.car = read();
154         if(token == Lexer::TOKEN_CLOSE_PAREN) {
155           cur->v.cons.cdr = 0;
156           break;
157         }
158         Lisp *newcur = new(obst) Lisp(Lisp::TYPE_CONS);
159         cur->v.cons.cdr = newcur;
160         cur = newcur;
161       } while(1);
162
163       break;
164     }
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);
170       break;
171     }
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);
177       break;
178     }
179     case Lexer::TOKEN_INTEGER:
180       result = new(obst) Lisp(Lisp::TYPE_INTEGER);
181       sscanf(lexer->getString(), "%d", &result->v.integer);
182       break;
183     case Lexer::TOKEN_REAL:
184       result = new(obst) Lisp(Lisp::TYPE_REAL);
185       sscanf(lexer->getString(), "%f", &result->v.real);
186       break;
187     case Lexer::TOKEN_TRUE:
188       result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
189       result->v.boolean = true;
190       break;
191     case Lexer::TOKEN_FALSE:
192       result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
193       result->v.boolean = false;
194       break;
195
196     default:
197       // this should never happen
198       assert(false);
199   }
200
201   token = lexer->getNextToken();
202   return result;
203 }
204
205 } // end of namespace lisp
206
207 /* EOF */