[svn] r6454: nemerle/trunk/ncc/parsing/Lexer.n

VladD2 svnadmin at nemerle.org
Fri Jul 21 11:20:29 CEST 2006


Log:
Has removed throwing exceptions in case of end of file is reached. 
Now the method read () returns ' \0 ’ in case of end of file is reached.

Author: VladD2
Date: Fri Jul 21 11:20:28 2006
New Revision: 6454

Modified:
   nemerle/trunk/ncc/parsing/Lexer.n

Modified: nemerle/trunk/ncc/parsing/Lexer.n
==============================================================================
--- nemerle/trunk/ncc/parsing/Lexer.n	(original)
+++ nemerle/trunk/ncc/parsing/Lexer.n	Fri Jul 21 11:20:28 2006
@@ -328,6 +328,12 @@
     is_check_wrong_chars = Manager.Options.Warnings.IsEnabled (10002);
   }
 
+  protected read_or_eol () : char
+  {
+    def ch = read ();
+    if (ch == '\0') '\n' else ch
+  }
+
   protected read () : char
   {
     mutable ch = read_from_input ();
@@ -349,6 +355,8 @@
           when (is_check_wrong_chars)
             Message.Warning (10002, this.Location, "CR character found in input stream");
 
+      | '\0' => ()
+
       | _ => ++col;
     };
    
@@ -774,7 +782,10 @@
           _ = buf.Append ('$');
           loop ()
           
-        | '\n' => throw Error ("newline before end of string")
+        | '\0' => Message.Error (this.Location, "Unterminated string literal") // like MS csc
+        
+        | '\n' => Message.Error (this.Location, "Newline in constant") // like MS csc
+        
         | ch when ch != end_ch => 
           _ = buf.Append (ch);
           loop ();
@@ -807,6 +818,9 @@
             | _ =>
               Token.StringLiteral (buf.ToString ())
           }
+        | '\0' => 
+          Message.Error (this.Location, "Unterminated string literal"); // like MS csc
+          Token.StringLiteral (buf.ToString ())
         | ch =>
           _ = buf.Append (ch); loop ()
       }
@@ -876,7 +890,7 @@
             else {
               Message.Error (this.Location, "preprocessor directives must occur only in"
                              " lines beginning with whitespaces");
-              while (read () != '\n') ();
+              while (read_or_eol () != '\n') ();
               loop (false);
             }
         
@@ -1006,6 +1020,8 @@
       // (try..catch above)
       | ' ' => Token.EndOfFile ()
         
+      | '\0' => Token.EndOfFile ()
+
       | _ =>
         if (Char.IsDigit (ch))
           get_number (ch)
@@ -1052,8 +1068,7 @@
     def line = StringBuilder (80);
     while (c != '\n') {
       ignore (line.Append (c));
-      try { c = read () }
-      catch { _ is LexerBase.Error => c = '\n' }
+      c = read_or_eol ();
     };
     line.ToString ()
   }
@@ -1065,7 +1080,7 @@
     def eat_spaces () : char {
       mutable c = ' ';
       while (Char.IsWhiteSpace (c) && c != '\n') 
-        c = read ();
+        c = read_or_eol ();
       c
     };
     def read_word () : string {
@@ -1415,7 +1430,7 @@
     if (inp >= 0)
       inp :> char
     else
-      throw LexerBase.Error ("unexpected end of file")
+      '\0'
   }
 
   override protected peek_or_white () : char
@@ -1433,12 +1448,14 @@
     if (inp >= 0)
       (inp :> char)
     else 
-      throw LexerBase.Error ("unexpected end of file")
+      '\0'
   }
   
 
   override protected comment_beginning () : char
   {
+    def startLocation = this.Location;
+    
     match (peek_or_white ()) {
       | '/' =>
         // we are for sure in one line comment
@@ -1450,13 +1467,13 @@
             mutable cc = ' ';
             do
             {
-              cc = read ();                
+              cc = read_or_eol ();                
               _ = comment_store.Append (cc)
             } while (cc != '\n');
             comment_loc = comment_loc + Location (file_idx, line, col);
           }
           else
-            while (read () != '\n') {};
+            while (read_or_eol () != '\n') {};
         }
         catch { _ is LexerBase.Error => () };
           
@@ -1475,6 +1492,8 @@
           match (cc) {
             | '*' => loop1 (true, store)
             | '/' when seen_star => ()
+            | '\0' => 
+              Message.Error (startLocation, "End-of-file found, '*/' expected!") // like MS csc
             | _ => loop1 (false, store)
           }
         };
@@ -1549,7 +1568,7 @@
     if (pos < reader.Length)
       reader[pos]
     else
-      throw LexerBase.Error ("unexpected end of file")
+      '\0'
   }
 
   override protected peek_or_white () : char
@@ -1566,7 +1585,9 @@
       def ch = reader[pos];
       ++pos;
       ch
-    } else throw LexerBase.Error ("unexpected end of code text")
+    }
+    else
+      '\0'
   }
 
   override protected comment_beginning () : char
@@ -1576,7 +1597,7 @@
         // we are for sure in one line comment
         try
         {
-          while (read () != '\n')
+          while (read_or_eol () != '\n')
             ();
         } 
         catch { _ is LexerBase.Error => () };
@@ -1592,6 +1613,9 @@
           match (cc) {
             | '*' => loop1 (true)
             | '/' when seen_star => ()
+            | '\0' =>
+              Message.Error (this.Location, 
+                "Unexpected end of file. Multiline comment not closed!")
             | _ => loop1 (false)
           }
         };



More information about the svn mailing list