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