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