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