]> git.tdb.fi Git - libs/datafile.git/commitdiff
Fix EOF handling
authorMikko Rasa <tdb@tdb.fi>
Wed, 23 Jul 2008 13:02:32 +0000 (13:02 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 23 Jul 2008 13:02:32 +0000 (13:02 +0000)
source/input.cpp
source/input.h
source/textparser.cpp

index e03a92126944703234a8d6fa330c4eba66cacb5e..f3f8f23f3343fa9352a618c82f8c151f2811573e 100644 (file)
@@ -34,5 +34,10 @@ int Input::peek()
        return next;
 }
 
+Input::operator bool() const
+{
+       return next>=0 || !in.eof();
+}
+
 } // namespace DataFile
 } // namespace Msp
index eeb18407ec2848927cdef93507180987c71d4cbe..ad921ec62963883da68a66092b740b7256553187 100644 (file)
@@ -19,7 +19,7 @@ public:
        int get();
        int peek();
        unsigned get_line_number() const { return line; }
-       operator bool() const { return !in.eof(); }
+       operator bool() const;
 private:
        IO::Base &in;
        unsigned line;
index 78d2f1df6334b32dbf67a7871975deda4dafb0a4..f7c830d0030ebcf5428c3c0abf06c63ba043ce60 100644 (file)
@@ -104,10 +104,10 @@ Statement TextParser::parse_statement(const Token *t)
 Token TextParser::parse_token()
 {
        int c=0;
-       unsigned comment=0;
+       int comment=0;
 
        // Skip over comments and whitespace
-       while(in)
+       while(in && comment>=0)
        {
                c=in.get();
                int next=in.peek();
@@ -123,11 +123,13 @@ Token TextParser::parse_token()
                else if(comment==3)   // Skip the second character of block comment end
                        comment=0;
                else if(!isspace(c) && !comment)
-                       break;
+                       comment=-1;
        }
 
-       if(comment)  // Didn't hit any non-whitespace
+       if(comment>0)  // EOF while in comment
                throw ParseError(src+": Unfinished comment at end of input", src, in.get_line_number());
+       else if(comment==0)  // Didn't hit any non-whitespace
+               return Token(Token::SPECIAL, "");
 
        enum ParseState
        {
@@ -167,7 +169,7 @@ Token TextParser::parse_token()
        string     buf;
        bool       escape=false;
 
-       while(in)
+       while(in || state==INIT)
        {
                if(state!=INIT)
                        c=in.get();