Fix compiler errors
[supertux.git] / src / lisp / lexer.cpp
index e1a4220..cd4ab64 100644 (file)
@@ -1,12 +1,10 @@
-//  $Id$
-//
 //  SuperTux
 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
-//  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
 //  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 <config.h>
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#include "lisp/lexer.hpp"
 
+#include <string.h>
 #include <sstream>
-#include <cstring>
 #include <stdexcept>
-#include <iostream>
-
-#include "lexer.hpp"
-
-namespace lisp
-{
-
-Lexer::Lexer(std::istream& newstream)
-    : stream(newstream), eof(false), linenumber(0)
+#include <stdio.h>
+
+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 */