X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Flisp%2Flexer.cpp;h=cd4ab641cc2f72ffe6381b697741885608006acd;hb=b49cbc5f242edb7023153f8bae9ceb444a2460da;hp=e1a4220f51bc3c2279bb8d91c1e74275dd3e1428;hpb=ea0629af91963ee2e4ab6b41124b5a7075ddd309;p=supertux.git diff --git a/src/lisp/lexer.cpp b/src/lisp/lexer.cpp index e1a4220f5..cd4ab641c 100644 --- a/src/lisp/lexer.cpp +++ b/src/lisp/lexer.cpp @@ -1,12 +1,10 @@ -// $Id$ -// // SuperTux // Copyright (C) 2006 Matthias Braun // -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -14,22 +12,26 @@ // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#include +// along with this program. If not, see . + +#include "lisp/lexer.hpp" +#include #include -#include #include -#include - -#include "lexer.hpp" - -namespace lisp -{ - -Lexer::Lexer(std::istream& newstream) - : stream(newstream), eof(false), linenumber(0) +#include + +namespace lisp { + +Lexer::Lexer(std::istream& newstream) : + stream(newstream), + eof(false), + linenumber(0), + bufend(), + bufpos(), + c(), + token_string(), + token_length() { // trigger a refill of the buffer bufpos = NULL; @@ -44,7 +46,7 @@ Lexer::~Lexer() void Lexer::nextChar() { - if(bufpos >= bufend) { + if(bufpos >= bufend || (bufpos == NULL && bufend == NULL) /* Initial refill trigger */) { if(eof) { c = EOF; return; @@ -65,6 +67,16 @@ Lexer::nextChar() } } c = *bufpos++; + if(c == '\n') + ++linenumber; +} + +void +Lexer::addChar() +{ + if(token_length < MAX_TOKEN_LENGTH) + token_string[token_length++] = c; + nextChar(); } Lexer::TokenType @@ -73,21 +85,15 @@ Lexer::getNextToken() static const char* delims = "\"();"; while(isspace(c)) { - if(c == '\n') - ++linenumber; nextChar(); - }; + } token_length = 0; switch(c) { case ';': // comment - while(true) { + while(c != '\n') { nextChar(); - if(c == '\n') { - ++linenumber; - break; - } } return getNextToken(); // and again case '(': @@ -101,38 +107,37 @@ Lexer::getNextToken() while(1) { nextChar(); switch(c) { - case '"': - nextChar(); - goto string_finished; - case '\r': - continue; - case '\n': - linenumber++; - break; - case '\\': - nextChar(); - switch(c) { - case 'n': - c = '\n'; + case '"': + nextChar(); + goto string_finished; + case '\r': + continue; + case '\n': break; - case 't': - c = '\t'; + case '\\': + nextChar(); + switch(c) { + case 'n': + c = '\n'; + break; + case 't': + c = '\t'; + break; + } break; + case EOF: { + std::stringstream msg; + msg << "Parse error in line " << startline << ": " + << "EOF while parsing string."; + throw std::runtime_error(msg.str()); } - break; - case EOF: { - std::stringstream msg; - msg << "Parse error in line " << startline << ": " - << "EOF while parsing string."; - throw std::runtime_error(msg.str()); - } - default: - break; + default: + break; } if(token_length < MAX_TOKEN_LENGTH) token_string[token_length++] = c; } -string_finished: + string_finished: token_string[token_length] = 0; return TOKEN_STRING; } @@ -140,9 +145,7 @@ string_finished: nextChar(); while(isalnum(c) || c == '_') { - if(token_length < MAX_TOKEN_LENGTH) - token_string[token_length++] = c; - nextChar(); + addChar(); } token_string[token_length] = 0; @@ -176,10 +179,7 @@ string_finished: else if(isalnum(c) || c == '_') have_nondigits = true; - if(token_length < MAX_TOKEN_LENGTH) - token_string[token_length++] = c; - - nextChar(); + addChar(); } while(!isspace(c) && !strchr(delims, c)); token_string[token_length] = 0; @@ -194,9 +194,7 @@ string_finished: return TOKEN_INTEGER; } else { do { - if(token_length < MAX_TOKEN_LENGTH) - token_string[token_length++] = c; - nextChar(); + addChar(); } while(!isspace(c) && !strchr(delims, c)); token_string[token_length] = 0; @@ -208,3 +206,5 @@ string_finished: } } // end of namespace lisp + +/* EOF */