6eb17da808943b4f5b7b1cbc5e7af1fe58f65968
[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
29 #include "supertux/gameconfig.hpp"
30
31 namespace lisp {
32
33 Parser::Parser(bool translate) :
34   lexer(0),
35   filename(),
36   dictionary_manager(0),
37   dictionary(0),
38   token(),
39   searchpath(),
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   searchpath = PHYSFS_getSearchPath();
51 }
52
53 Parser::~Parser()
54 {
55   obstack_free(&obst, NULL);
56   delete lexer;
57   delete dictionary_manager;
58   PHYSFS_freeList(searchpath);
59 }
60
61 static std::string dirname(const std::string& filename)
62 {
63   std::string::size_type p = filename.find_last_of('/');
64   if(p == std::string::npos)
65     return "";
66
67   return filename.substr(0, p+1);
68 }
69
70 const Lisp*
71 Parser::parse(const std::string& filename_)
72 {
73   IFileStreambuf ins(filename_);
74   std::istream in(&ins);
75
76   if(!in.good()) {
77     std::stringstream msg;
78     msg << "Parser problem: Couldn't open file '" << filename_ << "'.";
79     throw std::runtime_error(msg.str());
80   }
81
82   if(dictionary_manager) {
83     std::string rel_dir = dirname (filename_);
84     for(char** i = searchpath; *i != NULL; i++)
85     {
86       std::string abs_dir = std::string (*i) + PHYSFS_getDirSeparator () + rel_dir;
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       result->v.integer = atoi(lexer->getString());
198       break;
199     case Lexer::TOKEN_REAL:
200       result = new(obst) Lisp(Lisp::TYPE_REAL);
201       result->v.real = strtof(lexer->getString(), NULL);
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 */