[mcs] Better detection of null operator inside conditional expression. Fixes #57232
[mono.git] / mcs / mcs / cs-tokenizer.cs
index 518e2c5ab8f6d659c4aa15361c6eb008b935d96f..1be3309b1bfdd611d404408e743e0414f81f2860 100644 (file)
@@ -240,6 +240,7 @@ namespace Mono.CSharp
                public bool parsing_catch_when;
 
                int parsing_string_interpolation;
+               int string_interpolation_section;
                Stack<bool> parsing_string_interpolation_quoted;
 
                public bool parsing_interpolation_format;
@@ -411,6 +412,7 @@ namespace Mono.CSharp
                        public int current_token;
                        public object val;
                        public int parsing_string_interpolation;
+                       public int string_interpolation_section;
                        public Stack<bool> parsing_string_interpolation_quoted;
 
                        public Position (Tokenizer t)
@@ -430,9 +432,11 @@ namespace Mono.CSharp
                                        ifstack = new Stack<int> (clone);
                                }
                                parsing_generic_less_than = t.parsing_generic_less_than;
+                               string_interpolation_section = t.string_interpolation_section;
                                current_token = t.current_token;
                                val = t.val;
                                parsing_string_interpolation = t.parsing_string_interpolation;
+                               string_interpolation_section = t.string_interpolation_section;
                                if (t.parsing_string_interpolation_quoted != null && t.parsing_string_interpolation_quoted.Count != 0) {
                                        var clone = t.parsing_string_interpolation_quoted.ToArray ();
                                        Array.Reverse (clone);
@@ -889,6 +893,16 @@ namespace Mono.CSharp
                                if (parsing_block == 0)
                                        res = -1;
 
+                               break;
+                       case Token.THROW:
+                               switch (current_token) {
+                               case Token.ARROW:
+                               case Token.OP_COALESCING:
+                               case Token.INTERR:
+                                       res = Token.THROW_EXPR;
+                                       break;
+                               }
+
                                break;
                        }
 
@@ -1091,6 +1105,7 @@ namespace Mono.CSharp
                                                case Token.DECIMAL:
                                                case Token.BOOL:
                                                case Token.STRING:
+                                               case Token.SBYTE:
                                                        return Token.OPEN_PARENS_CAST;
                                                }
                                        }
@@ -1262,7 +1277,8 @@ namespace Mono.CSharp
                        else if (the_token == Token.INTERR_NULLABLE || the_token == Token.STAR)
                                goto again;
                        else if (the_token == Token.OP_GENERICS_LT) {
-                               if (!parse_less_than (ref genericDimension))
+                               int unused = 0;
+                               if (!parse_less_than (ref unused))
                                        return false;
                                goto again;
                        } else if (the_token == Token.OPEN_BRACKET) {
@@ -1309,7 +1325,8 @@ namespace Mono.CSharp
                        }
 
                        if (d == '.') {
-                               return Token.INTERR_OPERATOR;
+                               d = reader.Peek ();
+                               return d >= '0' && d <= '9' ? Token.INTERR : Token.INTERR_OPERATOR;
                        }
 
                        if (d != ' ') {
@@ -1339,6 +1356,7 @@ namespace Mono.CSharp
                        case Token.THIS:
                        case Token.NEW:
                        case Token.INTERPOLATED_STRING:
+                       case Token.THROW:
                                next_token = Token.INTERR;
                                break;
                                
@@ -2460,6 +2478,12 @@ namespace Mono.CSharp
                                case '\"':
                                        ++str_quote;
                                        break;
+                               case '\\':
+                                       // Skip escaped " character
+                                       c = reader.Read ();
+                                       if (c == -1)
+                                               res = false;
+                                       break;
                                case -1:
                                        res = false;
                                        break;
@@ -2518,7 +2542,6 @@ namespace Mono.CSharp
 
                int TokenizePragmaWarningIdentifier (ref int c, ref bool identifier)
                {
-
                        if ((c >= '0' && c <= '9') || is_identifier_start_character (c)) {
                                int number;
 
@@ -2543,7 +2566,7 @@ namespace Mono.CSharp
                                                id_builder [pos] = (char)c;
 
                                                if (c >= '0' && c <= '9') {
-                                                       if (pos == 6 && id_builder [0] == 'C' && id_builder [1] == 'S') {
+                                                       if (pos == 5 && id_builder [0] == 'C' && id_builder [1] == 'S') {
                                                                // Recognize CSXXXX as C# XXXX warning
                                                                number = 0;
                                                                int pow = 1000;
@@ -2558,6 +2581,9 @@ namespace Mono.CSharp
                                                                        pow /= 10;
                                                                }
                                                        }
+                                               } else if (c == '\n' || c == UnicodeLS || c == UnicodePS) {
+                                                       advance_line ();
+                                                       break;
                                                } else if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && c != '_') {
                                                        break;
                                                }
@@ -3373,18 +3399,26 @@ namespace Mono.CSharp
 
                                case '{':
                                        val = ltb.Create (current_source, ref_line, col);
+
+                                       if (parsing_string_interpolation > 0)
+                                               ++string_interpolation_section;
+
                                        return Token.OPEN_BRACE;
                                case '}':
                                        if (parsing_string_interpolation > 0) {
-                                               --parsing_string_interpolation;
-                                               bool quoted;
-                                               if (parsing_string_interpolation_quoted != null && parsing_string_interpolation_quoted.Count > 0) {
-                                                       quoted = parsing_string_interpolation_quoted.Pop ();
-                                               } else {
-                                                       quoted = false;
+                                               if (string_interpolation_section == 0) {
+                                                       --parsing_string_interpolation;
+                                                       bool quoted;
+                                                       if (parsing_string_interpolation_quoted != null && parsing_string_interpolation_quoted.Count > 0) {
+                                                               quoted = parsing_string_interpolation_quoted.Pop ();
+                                                       } else {
+                                                               quoted = false;
+                                                       }
+
+                                                       return TokenizeInterpolatedString (quoted);
                                                }
 
-                                               return TokenizeInterpolatedString (quoted);
+                                               --string_interpolation_section;
                                        }
 
                                        val = ltb.Create (current_source, ref_line, col);
@@ -4045,6 +4079,9 @@ namespace Mono.CSharp
                                        --braces;
                                        break;
                                case '\\':
+                                       if (parsing_string_interpolation_quoted != null && parsing_string_interpolation_quoted.Peek ())
+                                               break;
+
                                        ++col;
                                        int surrogate;
                                        ch = escape (ch, out surrogate);
@@ -4089,8 +4126,6 @@ namespace Mono.CSharp
                private void handle_one_line_xml_comment ()
                {
                        int c;
-                       while ((c = peek_char ()) == ' ')
-                               get_char (); // skip heading whitespaces.
                        while ((c = peek_char ()) != -1 && c != '\n' && c != '\r') {
                                xml_comment_buffer.Append ((char) get_char ());
                        }