| 1 |
// $Id: parser.cpp 1025 2007-04-30 14:48:32Z hiker $ |
| 2 |
// |
| 3 |
// TuxKart - a fun racing game with go-kart |
| 4 |
// Copyright (C) 2004 Matthias Braun <[email protected]> |
| 5 |
// code in this file based on lispreader from Mark Probst |
| 6 |
// |
| 7 |
// This program is free software; you can redistribute it and/or |
| 8 |
// modify it under the terms of the GNU General Public License |
| 9 |
// as published by the Free Software Foundation; either version 2 |
| 10 |
// of the License, or (at your option) any later version. |
| 11 |
// |
| 12 |
// This program is distributed in the hope that it will be useful, |
| 13 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
// GNU General Public License for more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU General Public License |
| 18 |
// along with this program; if not, write to the Free Software |
| 19 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 |
#include <sstream> |
| 21 |
#include <stdexcept> |
| 22 |
#include <fstream> |
| •23 |
#include <cstring> |
| 24 |
|
| 25 |
#include <cassert> |
| 26 |
|
| 27 |
#include "parser.hpp" |
| 28 |
#include "lisp.hpp" |
| 29 |
#include "translation.hpp" |
| 30 |
#if defined(WIN32) && !defined(__CYGWIN__) |
| 31 |
# define snprintf _snprintf |
| 32 |
#endif |
| 33 |
|
| 34 |
namespace lisp |
| 35 |
{ |
| 36 |
|
| 37 |
Parser::Parser() |
| 38 |
: m_lexer(0) |
| 39 |
{} |
| 40 |
|
| 41 |
//----------------------------------------------------------------------------- |
| 42 |
|
| 43 |
Parser::~Parser() |
| 44 |
{ |
| 45 |
delete m_lexer; |
| 46 |
} |
| 47 |
|
| 48 |
//----------------------------------------------------------------------------- |
| 49 |
|
| 50 |
Lisp* |
| 51 |
Parser::parse(const std::string& filename) |
| 52 |
{ |
| 53 |
std::ifstream in(filename.c_str()); |
| 54 |
if(!in.good()) |
| 55 |
{ |
| 56 |
char msg[MAX_ERROR_MESSAGE_LENGTH]; |
| 57 |
snprintf(msg, sizeof(msg), |
| 58 |
_("Couldn't open file '%s'."), |
| 59 |
filename.c_str()); |
| 60 |
throw std::runtime_error(msg); |
| 61 |
} |
| 62 |
return parse(in); |
| 63 |
} |
| 64 |
|
| 65 |
//----------------------------------------------------------------------------- |
| 66 |
|
| 67 |
Lisp* |
| 68 |
Parser::parse(std::istream& m_stream) |
| 69 |
{ |
| 70 |
delete m_lexer; |
| 71 |
m_lexer = new Lexer(m_stream); |
| 72 |
|
| 73 |
m_token = m_lexer->getNextToken(); |
| 74 |
Lisp* result = new Lisp(Lisp::TYPE_CONS); |
| 75 |
result->m_v.m_cons.m_car = 0; |
| 76 |
result->m_v.m_cons.m_cdr = readList(); |
| 77 |
|
| 78 |
delete m_lexer; |
| 79 |
m_lexer = 0; |
| 80 |
|
| 81 |
return result; |
| 82 |
} |
| 83 |
|
| 84 |
//----------------------------------------------------------------------------- |
| 85 |
|
| 86 |
Lisp* |
| 87 |
Parser::read() |
| 88 |
{ |
| 89 |
Lisp* result; |
| 90 |
switch(m_token) |
| 91 |
{ |
| 92 |
case Lexer::TOKEN_EOF: |
| 93 |
{ |
| 94 |
char msg[MAX_ERROR_MESSAGE_LENGTH]; |
| 95 |
snprintf(msg, sizeof(msg), |
| 96 |
"Parse Error at line %d: Unexpected EOF.", |
| 97 |
m_lexer->getLineNumber()); |
| 98 |
throw std::runtime_error(msg); |
| 99 |
} |
| 100 |
case Lexer::TOKEN_CLOSE_PAREN: |
| 101 |
{ |
| 102 |
char msg[MAX_ERROR_MESSAGE_LENGTH]; |
| 103 |
snprintf(msg, sizeof(msg), |
| 104 |
"Parse Error at line %d: Unexpected ')'.", |
| 105 |
m_lexer->getLineNumber()); |
| 106 |
throw std::runtime_error(msg); |
| 107 |
} |
| 108 |
case Lexer::TOKEN_OPEN_PAREN: |
| 109 |
result = new Lisp(Lisp::TYPE_CONS); |
| 110 |
|
| 111 |
m_token = m_lexer->getNextToken(); |
| 112 |
|
| 113 |
result->m_v.m_cons.m_car = read(); |
| 114 |
result->m_v.m_cons.m_cdr = readList(); |
| 115 |
|
| 116 |
if(m_token != Lexer::TOKEN_CLOSE_PAREN) |
| 117 |
{ |
| 118 |
char msg[MAX_ERROR_MESSAGE_LENGTH]; |
| 119 |
snprintf(msg, sizeof(msg), |
| 120 |
"Parse Error at line %d: Expected ')'.", |
| 121 |
m_lexer->getLineNumber()); |
| 122 |
throw std::runtime_error(msg); |
| 123 |
} |
| 124 |
break; |
| 125 |
case Lexer::TOKEN_SYMBOL: |
| 126 |
{ |
| 127 |
result = new Lisp(Lisp::TYPE_SYMBOL); |
| 128 |
const size_t LEN = strlen(m_lexer->getString()) + 1; |
| 129 |
result->m_v.m_string = new char[LEN]; |
| 130 |
memcpy(result->m_v.m_string, m_lexer->getString(), LEN); |
| 131 |
break; |
| 132 |
} |
| 133 |
case Lexer::TOKEN_STRING: |
| 134 |
{ |
| 135 |
result = new Lisp(Lisp::TYPE_STRING); |
| 136 |
const size_t LEN = strlen(m_lexer->getString()) + 1; |
| 137 |
result->m_v.m_string = new char[LEN]; |
| 138 |
memcpy(result->m_v.m_string, m_lexer->getString(), LEN); |
| 139 |
break; |
| 140 |
} |
| 141 |
case Lexer::TOKEN_INTEGER: |
| 142 |
result = new Lisp(Lisp::TYPE_INTEGER); |
| 143 |
sscanf(m_lexer->getString(), "%d", &result->m_v.m_integer); |
| 144 |
break; |
| 145 |
case Lexer::TOKEN_REAL: |
| 146 |
result = new Lisp(Lisp::TYPE_REAL); |
| 147 |
sscanf(m_lexer->getString(), "%f", &result->m_v.m_real); |
| 148 |
break; |
| 149 |
case Lexer::TOKEN_TRUE: |
| 150 |
result = new Lisp(Lisp::TYPE_BOOLEAN); |
| 151 |
result->m_v.m_boolean = true; |
| 152 |
break; |
| 153 |
case Lexer::TOKEN_FALSE: |
| 154 |
result = new Lisp(Lisp::TYPE_BOOLEAN); |
| 155 |
result->m_v.m_boolean = false; |
| 156 |
break; |
| 157 |
|
| 158 |
default: |
| 159 |
// this should never happen |
| 160 |
assert(false); |
| 161 |
} |
| 162 |
|
| 163 |
m_token = m_lexer->getNextToken(); |
| 164 |
return result; |
| 165 |
} |
| 166 |
|
| 167 |
//----------------------------------------------------------------------------- |
| 168 |
|
| 169 |
Lisp* |
| 170 |
Parser::readList() |
| 171 |
{ |
| 172 |
Lisp* result = 0; |
| 173 |
|
| 174 |
while(m_token != Lexer::TOKEN_CLOSE_PAREN && m_token != Lexer::TOKEN_EOF) |
| 175 |
{ |
| 176 |
Lisp* newlisp = new Lisp(Lisp::TYPE_CONS); |
| 177 |
newlisp->m_v.m_cons.m_car = read(); |
| 178 |
newlisp->m_v.m_cons.m_cdr = result; |
| 179 |
result = newlisp; |
| 180 |
} |
| 181 |
|
| 182 |
return result; |
| 183 |
} |
| 184 |
|
| 185 |
} // end of namespace lisp |