Debian Codegraph @

src/lisp/lexer.cpp

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