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