Debian Codegraph @

src/lisp/lexer.cpp

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