Debian Codegraph @

src/lisp/lexer.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 // $Id: lexer.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
23 #include "lexer.hpp"
24 #include "translation.hpp"
25 #if defined(WIN32) && !defined(__CYGWIN__)
26 # define snprintf _snprintf
27 #endif
28 namespace lisp
29 {
30 class EOFException
31 {}
32 ;
33
34 Lexer::Lexer(std::istream& newstream)
35 : m_stream(newstream), m_is_eof(false)
36 {
37 try
38 {
39 // trigger a refill of the m_buffer
40 m_c = 0;
41 m_buffer_end = m_c + 1;
42 nextChar();
43 }
44 catch(EOFException& e)
45 {}
46 }
47
48 //-----------------------------------------------------------------------------
49
50 Lexer::~Lexer()
51 {}
52
53 //-----------------------------------------------------------------------------
54
55 void
56 Lexer::nextChar()
57 {
58 ++m_c;
59 if(m_c >= m_buffer_end)
60 {
61 if(m_is_eof)
62 throw EOFException();
63 m_stream.read(m_buffer, LEXER_BUFFER_SIZE);
64 std::streamsize n = m_stream.gcount();
65
66 m_c = m_buffer;
67 m_buffer_end = m_buffer + n;
68
69 // the following is a hack that appends an additional ' ' at the end of
70 // the file to avoid problems when parsing symbols/elements and a sudden
71 // EOF. This is faster than relying on unget and IMO also nicer.
72 if(n < LEXER_BUFFER_SIZE || n == 0)
73 {
74 *m_buffer_end = ' ';
75 ++m_buffer_end;
76 m_is_eof = true;
77 }
78 }
79 }
80
81 //-----------------------------------------------------------------------------
82
83 Lexer::TokenType
84 Lexer::getNextToken()
85 {
86 static const char* delims = "\"();";
87
88 try
89 {
90 while(isspace(*m_c))
91 {
92 nextChar();
93 if(*m_c == '\n')
94 ++m_line_number;
95 };
96
97 m_token_length = 0;
98
99 switch(*m_c)
100 {
101 case ';': // comment
102 while(true)
103 {
104 nextChar();
105 if(*m_c == '\n')
106 {
107 ++m_line_number;
108 break;
109 }
110 }
111 return getNextToken(); // and again
112 case '(':
113 nextChar();
114 return TOKEN_OPEN_PAREN;
115 case ')':
116 nextChar();
117 return TOKEN_CLOSE_PAREN;
118 case '"': // string
119 {
120 const int STARTLINE = m_line_number;
121 try
122 {
123 while(1)
124 {
125 nextChar();
126 if(*m_c == '"')
127 break;
128
129 if(*m_c == '\\')
130 {
131 nextChar();
132 switch(*m_c)
133 {
134 case 'n':
135 *m_c = '\n';
136 break;
137 case 't':
138 *m_c = '\t';
139 break;
140 }
141 }
142 if(m_token_length < MAX_TOKEN_LENGTH)
143 m_token_string[m_token_length++] = *m_c;
144 }
145 m_token_string[m_token_length] = 0;
146 }
147 catch(EOFException& )
148 {
149 char msg[MAX_ERROR_MESSAGE_LENGTH];
150 snprintf(msg, sizeof(msg),
151 "Parse error in line %d: EOF while parsing string.",
152 STARTLINE);
153 throw std::runtime_error(msg);
154 }
155 nextChar();
156 return TOKEN_STRING;
157 }
158 case '#': // constant
159 try
160 {
161 nextChar();
162
163 while(isalnum(*m_c) || *m_c == '_')
164 {
165 if(m_token_length < MAX_TOKEN_LENGTH)
166 m_token_string[m_token_length++] = *m_c;
167 nextChar();
168 }
169 m_token_string[m_token_length] = 0;
170 }
171 catch(EOFException& )
172 {
173 char msg[MAX_ERROR_MESSAGE_LENGTH];
174 snprintf(msg, sizeof(msg),
175 "Parse Error in line %d: EOF while parsing constant.",
176 m_line_number);
177 throw std::runtime_error(msg);
178 }
179
180 if(strcmp(m_token_string, "t") == 0)
181 return TOKEN_TRUE;
182 if(strcmp(m_token_string, "f") == 0)
183 return TOKEN_FALSE;
184
185 // this would be the place to add more sophisticated handling of
186 // constants
187
188 {
189 char msg[MAX_ERROR_MESSAGE_LENGTH];
190 snprintf(msg, sizeof(msg),
191 "Parse Error in line %d: Unknown constant '%s'.",
192 m_line_number, m_token_string);
193 throw std::runtime_error(msg);
194 }
195
196 default:
197 if(isdigit(*m_c) || *m_c == '-')
198 {
199 bool have_nondigits = false;
200 bool have_digits = false;
201 int have_floating_point = 0;
202
203 do
204 {
205 if(isdigit(*m_c))
206 have_digits = true;
207 else if(*m_c == '.')
208 ++have_floating_point;
209 else if(isalnum(*m_c) || *m_c == '_')
210 have_nondigits = true;
211
212 if(m_token_length < MAX_TOKEN_LENGTH)
213 m_token_string[m_token_length++] = *m_c;
214
215 nextChar();
216 }
217 while(!isspace(*m_c) && !strchr(delims, *m_c));
218
219 m_token_string[m_token_length] = 0;
220
221 // no nextChar
222
223 if(have_nondigits || !have_digits || have_floating_point > 1)
224 return TOKEN_SYMBOL;
225 else if(have_floating_point == 1)
226 return TOKEN_REAL;
227 else
228 return TOKEN_INTEGER;
229 }
230 else
231 {
232 do
233 {
234 if(m_token_length < MAX_TOKEN_LENGTH)
235 m_token_string[m_token_length++] = *m_c;
236 nextChar();
237 }
238 while(!isspace(*m_c) && !strchr(delims, *m_c));
239 m_token_string[m_token_length] = 0;
240
241 // no nextChar
242
243 return TOKEN_SYMBOL;
244 }
245 }
246 }
247 catch(EOFException& )
248 {
249 return TOKEN_EOF;
250 }
251 }
252
253 } // end of namespace lisp
254