Debian Codegraph @

src/lisp/parser.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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
24 #include <cassert>
25
26 #include "parser.hpp"
27 #include "lisp.hpp"
28 #include "translation.hpp"
29 #if defined(WIN32) && !defined(__CYGWIN__)
30 # define snprintf _snprintf
31 #endif
32
33 namespace lisp
34 {
35
36 Parser::Parser()
37 : m_lexer(0)
38 {}
39
40 //-----------------------------------------------------------------------------
41
42 Parser::~Parser()
43 {
44 delete m_lexer;
45 }
46
47 //-----------------------------------------------------------------------------
48
49 Lisp*
50 Parser::parse(const std::string& filename)
51 {
52 std::ifstream in(filename.c_str());
53 if(!in.good())
54 {
55 char msg[MAX_ERROR_MESSAGE_LENGTH];
56 snprintf(msg, sizeof(msg),
57 _("Couldn't open file '%s'."),
58 filename.c_str());
59 throw std::runtime_error(msg);
60 }
61 return parse(in);
62 }
63
64 //-----------------------------------------------------------------------------
65
66 Lisp*
67 Parser::parse(std::istream& m_stream)
68 {
69 delete m_lexer;
70 m_lexer = new Lexer(m_stream);
71
72 m_token = m_lexer->getNextToken();
73 Lisp* result = new Lisp(Lisp::TYPE_CONS);
74 result->m_v.m_cons.m_car = 0;
75 result->m_v.m_cons.m_cdr = readList();
76
77 delete m_lexer;
78 m_lexer = 0;
79
80 return result;
81 }
82
83 //-----------------------------------------------------------------------------
84
85 Lisp*
86 Parser::read()
87 {
88 Lisp* result;
89 switch(m_token)
90 {
91 case Lexer::TOKEN_EOF:
92 {
93 char msg[MAX_ERROR_MESSAGE_LENGTH];
94 snprintf(msg, sizeof(msg),
95 "Parse Error at line %d: Unexpected EOF.",
96 m_lexer->getLineNumber());
97 throw std::runtime_error(msg);
98 }
99 case Lexer::TOKEN_CLOSE_PAREN:
100 {
101 char msg[MAX_ERROR_MESSAGE_LENGTH];
102 snprintf(msg, sizeof(msg),
103 "Parse Error at line %d: Unexpected ')'.",
104 m_lexer->getLineNumber());
105 throw std::runtime_error(msg);
106 }
107 case Lexer::TOKEN_OPEN_PAREN:
108 result = new Lisp(Lisp::TYPE_CONS);
109
110 m_token = m_lexer->getNextToken();
111
112 result->m_v.m_cons.m_car = read();
113 result->m_v.m_cons.m_cdr = readList();
114
115 if(m_token != Lexer::TOKEN_CLOSE_PAREN)
116 {
117 char msg[MAX_ERROR_MESSAGE_LENGTH];
118 snprintf(msg, sizeof(msg),
119 "Parse Error at line %d: Expected ')'.",
120 m_lexer->getLineNumber());
121 throw std::runtime_error(msg);
122 }
123 break;
124 case Lexer::TOKEN_SYMBOL:
125 {
126 result = new Lisp(Lisp::TYPE_SYMBOL);
127 const size_t LEN = strlen(m_lexer->getString()) + 1;
128 result->m_v.m_string = new char[LEN];
129 memcpy(result->m_v.m_string, m_lexer->getString(), LEN);
130 break;
131 }
132 case Lexer::TOKEN_STRING:
133 {
134 result = new Lisp(Lisp::TYPE_STRING);
135 const size_t LEN = strlen(m_lexer->getString()) + 1;
136 result->m_v.m_string = new char[LEN];
137 memcpy(result->m_v.m_string, m_lexer->getString(), LEN);
138 break;
139 }
140 case Lexer::TOKEN_INTEGER:
141 result = new Lisp(Lisp::TYPE_INTEGER);
142 sscanf(m_lexer->getString(), "%d", &result->m_v.m_integer);
143 break;
144 case Lexer::TOKEN_REAL:
145 result = new Lisp(Lisp::TYPE_REAL);
146 sscanf(m_lexer->getString(), "%f", &result->m_v.m_real);
147 break;
148 case Lexer::TOKEN_TRUE:
149 result = new Lisp(Lisp::TYPE_BOOLEAN);
150 result->m_v.m_boolean = true;
151 break;
152 case Lexer::TOKEN_FALSE:
153 result = new Lisp(Lisp::TYPE_BOOLEAN);
154 result->m_v.m_boolean = false;
155 break;
156
157 default:
158 // this should never happen
159 assert(false);
160 }
161
162 m_token = m_lexer->getNextToken();
163 return result;
164 }
165
166 //-----------------------------------------------------------------------------
167
168 Lisp*
169 Parser::readList()
170 {
171 Lisp* result = 0;
172
173 while(m_token != Lexer::TOKEN_CLOSE_PAREN && m_token != Lexer::TOKEN_EOF)
174 {
175 Lisp* newlisp = new Lisp(Lisp::TYPE_CONS);
176 newlisp->m_v.m_cons.m_car = read();
177 newlisp->m_v.m_cons.m_cdr = result;
178 result = newlisp;
179 }
180
181 return result;
182 }
183
184 } // end of namespace lisp