Fix parsing of \r in quoted strings. Fixes #6984
authorMarek Safar <marek.safar@gmail.com>
Fri, 14 Sep 2012 11:59:46 +0000 (12:59 +0100)
committerMarek Safar <marek.safar@gmail.com>
Fri, 14 Sep 2012 12:01:22 +0000 (13:01 +0100)
mcs/mcs/cs-parser.jay
mcs/mcs/cs-tokenizer.cs
mcs/tests/test-853.cs [new file with mode: 0644]

index 211531ec6680c0709f83bee71f92591baa39b0f1..056694106bc6d5731a29c010f6cc4c8f70accdaf 100644 (file)
@@ -6756,6 +6756,10 @@ void Error_SyntaxError (int error_code, int token, string msg)
        // An error message has been reported by tokenizer
        if (token == Token.ERROR)
                return;
+       
+       // Avoid duplicit error message after unterminated string literals
+       if (token == Token.LITERAL && lexer.Location.Column == 0)
+               return;
 
        string symbol = GetSymbolName (token);
        string expecting = GetExpecting ();
index f0606ec5c8225d48a184c104692df3b23dc406d6..2c957615115c4626d8627f693f68b78319b3b172 100644 (file)
@@ -2863,7 +2863,14 @@ namespace Mono.CSharp
 #endif
 
                        while (true){
-                               c = get_char ();
+                               // Cannot use get_char because of \r in quoted strings
+                               if (putback_char != -1) {
+                                       c = putback_char;
+                                       putback_char = -1;
+                               } else {
+                                       c = reader.Read ();
+                               }
+
                                if (c == '"') {
                                        if (quoted && peek_char () == '"') {
                                                if (pos == value_builder.Length)
@@ -2896,9 +2903,18 @@ namespace Mono.CSharp
                                if (c == '\n') {
                                        if (!quoted) {
                                                Report.Error (1010, Location, "Newline in constant");
+
+                                               advance_line ();
+
+                                               // Don't add \r to string literal
+                                               if (pos > 1 && value_builder [pos - 1] == '\r')
+                                                       --pos;
+
                                                val = new StringLiteral (context.BuiltinTypes, new string (value_builder, 0, pos), start_location);
                                                return Token.LITERAL;
                                        }
+
+                                       advance_line ();
                                } else if (c == '\\' && !quoted) {
                                        int surrogate;
                                        c = escape (c, out surrogate);
@@ -2914,6 +2930,8 @@ namespace Mono.CSharp
                                } else if (c == -1) {
                                        Report.Error (1039, Location, "Unterminated string literal");
                                        return Token.EOF;
+                               } else {
+                                       ++col;
                                }
 
                                if (pos == value_builder.Length)
diff --git a/mcs/tests/test-853.cs b/mcs/tests/test-853.cs
new file mode 100644 (file)
index 0000000..71faf53
--- /dev/null
@@ -0,0 +1,15 @@
+using System;
+
+class Program
+{
+       static int Main ()
+       {
+string s = @"a
+
+";
+               if (s.Length != 5)
+                       return 1;
+                       
+               return 0;
+       }
+}
\ No newline at end of file