4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "tinygettext/tinygettext.hpp"
28 #include "physfs/physfs_stream.hpp"
31 #include "obstack/obstackpp.hpp"
36 Parser::Parser(bool translate)
37 : lexer(0), dictionary_manager(0), dictionary(0)
40 dictionary_manager = new TinyGetText::DictionaryManager();
41 dictionary_manager->set_charset("UTF-8");
49 obstack_free(&obst, NULL);
51 delete dictionary_manager;
54 static std::string dirname(const std::string& filename)
56 std::string::size_type p = filename.find_last_of('/');
57 if(p == std::string::npos)
60 return filename.substr(0, p+1);
64 Parser::parse(const std::string& filename)
66 IFileStreambuf ins(filename);
67 std::istream in(&ins);
70 std::stringstream msg;
71 msg << "Parser problem: Couldn't open file '" << filename << "'.";
72 throw std::runtime_error(msg.str());
75 if(dictionary_manager) {
76 dictionary_manager->add_directory(dirname(filename));
77 dictionary = & (dictionary_manager->get_dictionary());
80 return parse(in, filename);
84 Parser::parse(std::istream& stream, const std::string& sourcename)
87 lexer = new Lexer(stream);
89 this->filename = sourcename;
90 token = lexer->getNextToken();
92 Lisp* result = new(obst) Lisp(Lisp::TYPE_CONS);
93 result->v.cons.car = read();
94 result->v.cons.cdr = 0;
103 Parser::parse_error(const char* msg) const
105 std::stringstream emsg;
106 emsg << "Parse Error at '" << filename << "' line " << lexer->getLineNumber()
108 throw std::runtime_error(emsg.str());
116 case Lexer::TOKEN_EOF: {
117 parse_error("Unexpected EOF.");
119 case Lexer::TOKEN_CLOSE_PAREN: {
120 parse_error("Unexpected ')'.");
122 case Lexer::TOKEN_OPEN_PAREN: {
123 result = new(obst) Lisp(Lisp::TYPE_CONS);
125 token = lexer->getNextToken();
126 if(token == Lexer::TOKEN_CLOSE_PAREN) {
127 result->v.cons.car = 0;
128 result->v.cons.cdr = 0;
132 if(token == Lexer::TOKEN_SYMBOL &&
133 strcmp(lexer->getString(), "_") == 0) {
134 // evaluate translation function (_ str) in place here
135 token = lexer->getNextToken();
136 if(token != Lexer::TOKEN_STRING)
137 parse_error("Expected string after '(_'");
139 result = new(obst) Lisp(Lisp::TYPE_STRING);
141 std::string translation = dictionary->translate(lexer->getString());
142 result->v.string = new(obst) char[translation.size()+1];
143 memcpy(result->v.string, translation.c_str(), translation.size()+1);
145 size_t len = strlen(lexer->getString()) + 1;
146 result->v.string = new(obst) char[len];
147 memcpy(result->v.string, lexer->getString(), len);
149 token = lexer->getNextToken();
150 if(token != Lexer::TOKEN_CLOSE_PAREN)
151 parse_error("Expected ')' after '(_ string'");
157 cur->v.cons.car = read();
158 if(token == Lexer::TOKEN_CLOSE_PAREN) {
162 Lisp *newcur = new(obst) Lisp(Lisp::TYPE_CONS);
163 cur->v.cons.cdr = newcur;
169 case Lexer::TOKEN_SYMBOL: {
170 result = new(obst) Lisp(Lisp::TYPE_SYMBOL);
171 size_t len = strlen(lexer->getString()) + 1;
172 result->v.string = new(obst) char[len];
173 memcpy(result->v.string, lexer->getString(), len);
176 case Lexer::TOKEN_STRING: {
177 result = new(obst) Lisp(Lisp::TYPE_STRING);
178 size_t len = strlen(lexer->getString()) + 1;
179 result->v.string = new(obst) char[len];
180 memcpy(result->v.string, lexer->getString(), len);
183 case Lexer::TOKEN_INTEGER:
184 result = new(obst) Lisp(Lisp::TYPE_INTEGER);
185 sscanf(lexer->getString(), "%d", &result->v.integer);
187 case Lexer::TOKEN_REAL:
188 result = new(obst) Lisp(Lisp::TYPE_REAL);
189 sscanf(lexer->getString(), "%f", &result->v.real);
191 case Lexer::TOKEN_TRUE:
192 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
193 result->v.boolean = true;
195 case Lexer::TOKEN_FALSE:
196 result = new(obst) Lisp(Lisp::TYPE_BOOLEAN);
197 result->v.boolean = false;
201 // this should never happen
205 token = lexer->getNextToken();
209 } // end of namespace lisp