4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 Lexer::Lexer(std::istream& newstream)
35 : stream(newstream), eof(false), linenumber(0)
38 // trigger a refill of the buffer
42 } catch(EOFException& ) {
57 stream.read(buffer, BUFFER_SIZE);
58 size_t bytes_read = stream.gcount();
61 bufend = buffer + bytes_read;
63 // the following is a hack that appends an additional ' ' at the end of
64 // the file to avoid problems when parsing symbols/elements and a sudden
65 // EOF. This is faster than relying on unget and IMO also nicer.
66 if(bytes_read == 0 || stream.eof()) {
77 static const char* delims = "\"();";
97 return getNextToken(); // and again
100 return TOKEN_OPEN_PAREN;
103 return TOKEN_CLOSE_PAREN;
104 case '"': { // string
105 int startline = linenumber;
111 else if (*c == '\r') // XXX this breaks with pure \r EOL
115 else if(*c == '\\') {
126 if(token_length < MAX_TOKEN_LENGTH)
127 token_string[token_length++] = *c;
129 token_string[token_length] = 0;
130 } catch(EOFException& ) {
131 std::stringstream msg;
132 msg << "Parse error in line " << startline << ": "
133 << "EOF while parsing string.";
134 throw std::runtime_error(msg.str());
139 case '#': // constant
143 while(isalnum(*c) || *c == '_') {
144 if(token_length < MAX_TOKEN_LENGTH)
145 token_string[token_length++] = *c;
148 token_string[token_length] = 0;
149 } catch(EOFException& ) {
150 std::stringstream msg;
151 msg << "Parse Error in line " << linenumber << ": "
152 << "EOF while parsing constant.";
153 throw std::runtime_error(msg.str());
156 if(strcmp(token_string, "t") == 0)
158 if(strcmp(token_string, "f") == 0)
161 // we only handle #t and #f constants at the moment...
164 std::stringstream msg;
165 msg << "Parse Error in line " << linenumber << ": "
166 << "Unknown constant '" << token_string << "'.";
167 throw std::runtime_error(msg.str());
171 if(isdigit(*c) || *c == '-') {
172 bool have_nondigits = false;
173 bool have_digits = false;
174 int have_floating_point = 0;
180 ++have_floating_point;
181 else if(isalnum(*c) || *c == '_')
182 have_nondigits = true;
184 if(token_length < MAX_TOKEN_LENGTH)
185 token_string[token_length++] = *c;
188 } while(!isspace(*c) && !strchr(delims, *c));
190 token_string[token_length] = 0;
194 if(have_nondigits || !have_digits || have_floating_point > 1)
196 else if(have_floating_point == 1)
199 return TOKEN_INTEGER;
202 if(token_length < MAX_TOKEN_LENGTH)
203 token_string[token_length++] = *c;
205 } while(!isspace(*c) && !strchr(delims, *c));
206 token_string[token_length] = 0;
213 } catch(EOFException& ) {
218 } // end of namespace lisp