2010-06-02 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / cs-parser.jay
index b983dacf0582917c8d058ded794924aa4803c7a8..adccfdf4da46cdb75f6113233ba148aefd1cf8d9 100644 (file)
@@ -54,7 +54,6 @@ namespace Mono.CSharp
 
                Delegate   current_delegate;
                
-               GenericMethod current_generic_method;
                AnonymousMethodExpression current_anonymous_method;
 
                /// <summary>
@@ -135,7 +134,7 @@ namespace Mono.CSharp
                //
                public Undo undo;
                
-               Stack<Block> linq_clause_blocks;
+               Stack<Linq.QueryBlock> linq_clause_blocks;
 
                // A counter to create new class names in interactive mode
                static int class_count;
@@ -1273,7 +1272,6 @@ method_declaration
                        Report.Error (531, method.Location, "`{0}': interface members cannot have a definition", method.GetSignatureForError ());
                }
 
-               current_generic_method = null;
                current_local_parameters = null;
 
                if (RootContext.Documentation != null)
@@ -1320,8 +1318,6 @@ method_header
                                method.GetSignatureForError ());
                }
 
-               current_generic_method = generic;
-
                if (RootContext.Documentation != null)
                        method.DocComment = Lexer.consume_doc_comment ();
 
@@ -1382,8 +1378,6 @@ method_header
                method = new Method (current_class, generic, TypeManager.system_void_expr,
                                     modifiers, name, current_local_parameters, (Attributes) $1);
 
-               current_generic_method = generic;
-
                if (RootContext.Documentation != null)
                        method.DocComment = Lexer.consume_doc_comment ();
 
@@ -2985,14 +2979,6 @@ integral_type
        | CHAR          { $$ = TypeManager.system_char_expr; }
        ;
 
-predefined_type
-       : builtin_types
-       | VOID
-         {
-               $$ = TypeManager.system_void_expr;      
-         }
-       ;
-
 //
 // Expressions, section 7.5
 //
@@ -3082,7 +3068,7 @@ member_access
                var lt = (Tokenizer.LocatedToken) $3;
                $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
          }
-       | predefined_type DOT IDENTIFIER opt_type_argument_list
+       | builtin_types DOT IDENTIFIER opt_type_argument_list
          {
                var lt = (Tokenizer.LocatedToken) $3;
                // TODO: Location is wrong as some predefined types doesn't hold a location
@@ -3102,12 +3088,12 @@ member_access
                var lt = (Tokenizer.LocatedToken) $3;
                $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
          }
-       | predefined_type DOT GENERATE_COMPLETION
+       | builtin_types DOT GENERATE_COMPLETION
          {
                // TODO: Location is wrong as some predefined types doesn't hold a location
                $$ = new CompletionMemberAccess ((Expression) $1, null, lexer.Location);
          }
-       | predefined_type DOT IDENTIFIER GENERATE_COMPLETION {
+       | builtin_types DOT IDENTIFIER GENERATE_COMPLETION {
                var lt = (Tokenizer.LocatedToken) $3;
                $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
          }
@@ -3160,6 +3146,10 @@ member_initializer_list
                a.Add ((Expression) $3);
                $$ = a;
          }
+       | member_initializer_list error {
+               Error_SyntaxError (yyToken);
+               $$ = $1;
+         }
        ;
 
 member_initializer
@@ -3181,7 +3171,10 @@ member_initializer
          }
        | OPEN_BRACE expression_list CLOSE_BRACE
          {
-               $$ = new CollectionElementInitializer ((List<Expression>)$2, GetLocation ($1));
+               if ($2 == null)
+                       $$ = null;
+               else
+                       $$ = new CollectionElementInitializer ((List<Expression>)$2, GetLocation ($1));
          }
        | OPEN_BRACE CLOSE_BRACE
          {
@@ -3233,12 +3226,12 @@ argument_list
        | argument_list COMMA
          {
                Report.Error (839, GetLocation ($2), "An argument is missing");
-               $$ = null;
+               $$ = $1;
          }
        | COMMA argument_or_named_argument
          {
                Report.Error (839, GetLocation ($1), "An argument is missing");
-               $$ = null;
+               $$ = $1;
          }
        ;
 
@@ -3330,6 +3323,10 @@ expression_list
                list.Add ((Expression) $3);
                $$ = list;
          }
+       | expression_list error {
+               Error_SyntaxError (yyToken);
+               $$ = $1;
+         }
        ;
        
 expression_list_arguments
@@ -3358,7 +3355,7 @@ expression_list_argument
 this_access
        : THIS
          {
-               $$ = new This (current_block, GetLocation ($1));
+               $$ = new This (GetLocation ($1));
          }
        ;
 
@@ -3801,7 +3798,7 @@ cast_expression
          {
                $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
          }
-       | OPEN_PARENS predefined_type CLOSE_PARENS prefixed_unary_expression
+       | OPEN_PARENS builtin_types CLOSE_PARENS prefixed_unary_expression
          {
                $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
          }
@@ -5441,66 +5438,78 @@ query_expression
 first_from_clause
        : FROM_FIRST IDENTIFIER IN expression
          {
-               $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
-               var lt = (Tokenizer.LocatedToken) $2;
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+         
+               var lt = (Tokenizer.LocatedToken) $2;     
+               $$ = new Linq.QueryExpression (new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)));
          }
        | FROM_FIRST type IDENTIFIER IN expression
          {
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+         
                var lt = (Tokenizer.LocatedToken) $3;
-               $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
+               $$ = new Linq.QueryExpression (
+                       new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
+                               IdentifierType = (FullNamedExpression)$2
+                       }
+               );
          }
        ;
 
 nested_from_clause
        : FROM IDENTIFIER IN expression
          {
-               $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
-               var lt = (Tokenizer.LocatedToken) $2;
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+         
+               var lt = (Tokenizer.LocatedToken) $2;     
+               $$ = new Linq.QueryExpression (new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)));
          }
        | FROM type IDENTIFIER IN expression
          {
-               $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
+         
                var lt = (Tokenizer.LocatedToken) $3;
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
+               $$ = new Linq.QueryExpression (
+                       new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
+                               IdentifierType = (FullNamedExpression)$2
+                       }
+               );
          }
        ;
        
 from_clause
        : FROM IDENTIFIER IN
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
          }
          expression
          {
                var lt = (Tokenizer.LocatedToken) $2;
                var sn = new SimpleMemberName (lt.Value, lt.Location);
-               $$ = new Linq.SelectMany (current_block.Toplevel, sn, (Expression)$5);
+               $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$5, GetLocation ($1));
                
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
                
-               ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
+               ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
          }       
        | FROM type IDENTIFIER IN
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
          }
          expression
          {
                var lt = (Tokenizer.LocatedToken) $3;
                var sn = new SimpleMemberName (lt.Value, lt.Location);
 
-               FullNamedExpression type = (FullNamedExpression)$2;
-               
-               $$ = new Linq.SelectMany (current_block.Toplevel, sn, new Linq.Cast (type, (FullNamedExpression)$6));
+               $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$6, GetLocation ($1)) {
+                       IdentifierType = (FullNamedExpression)$2
+               };
                
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
                
-               ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
+               ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
          }
        ;       
 
@@ -5530,7 +5539,7 @@ select_or_group_clause
          }
          expression
          {
-               $$ = new Linq.Select (current_block.Toplevel, (Expression)$3, GetLocation ($1));
+               $$ = new Linq.Select ((Linq.QueryBlock)current_block, (Expression)$3, GetLocation ($1));
 
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
@@ -5538,10 +5547,10 @@ select_or_group_clause
        | GROUP
          {
                if (linq_clause_blocks == null)
-                       linq_clause_blocks = new Stack<Block> ();
+                       linq_clause_blocks = new Stack<Linq.QueryBlock> ();
                        
                current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
-               linq_clause_blocks.Push (current_block);
+               linq_clause_blocks.Push ((Linq.QueryBlock)current_block);
          }
          expression
          {
@@ -5552,7 +5561,7 @@ select_or_group_clause
          }
          BY expression
          {
-               $$ = new Linq.GroupBy (current_block.Toplevel, (Expression)$3, (ToplevelBlock) linq_clause_blocks.Pop (), (Expression)$6, GetLocation ($1));
+               $$ = new Linq.GroupBy ((Linq.QueryBlock)current_block, (Expression)$3, linq_clause_blocks.Pop (), (Expression)$6, GetLocation ($1));
                
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
@@ -5584,18 +5593,18 @@ query_body_clause
 let_clause
        : LET IDENTIFIER ASSIGN 
          {
-               current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
          }
          expression
          {
                var lt = (Tokenizer.LocatedToken) $2;
                var sn = new SimpleMemberName (lt.Value, lt.Location);
-               $$ = new Linq.Let (current_block.Toplevel, current_container, sn, (Expression)$5);
+               $$ = new Linq.Let ((Linq.QueryBlock) current_block, sn, (Expression)$5, GetLocation ($1));
                
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
                
-               ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
+               ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
          }
        ;
 
@@ -5606,7 +5615,7 @@ where_clause
          }
          boolean_expression
          {
-               $$ = new Linq.Where (current_block.Toplevel, (BooleanExpression)$3, GetLocation ($1));
+               $$ = new Linq.Where ((Linq.QueryBlock)current_block, (BooleanExpression)$3, GetLocation ($1));
 
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
@@ -5617,10 +5626,10 @@ join_clause
        : JOIN IDENTIFIER IN
          {
                if (linq_clause_blocks == null)
-                       linq_clause_blocks = new Stack<Block> ();
+                       linq_clause_blocks = new Stack<Linq.QueryBlock> ();
                        
                current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
-               linq_clause_blocks.Push (current_block);
+               linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression ON
          {
@@ -5628,7 +5637,7 @@ join_clause
                current_block = current_block.Parent;
 
                current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
-               linq_clause_blocks.Push (current_block);
+               linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression EQUALS
          {
@@ -5636,8 +5645,7 @@ join_clause
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
 
-               var lt = (Tokenizer.LocatedToken) $2;
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), lexer.Location);
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
          }
          expression opt_join_into
          {
@@ -5645,15 +5653,15 @@ join_clause
                var sn = new SimpleMemberName (lt.Value, lt.Location);
                SimpleMemberName sn2 = null;
                
-               ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
-               ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
+               var outer_selector = linq_clause_blocks.Pop ();
+               var block = linq_clause_blocks.Pop ();
 
                if ($12 == null) {
-                       $$ = new Linq.Join (block, sn, (Expression)$5, outer_selector, current_block.Toplevel, GetLocation ($1));
+                       $$ = new Linq.Join (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1));
                } else {
                        var lt2 = (Tokenizer.LocatedToken) $12;
                        sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
-                       $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, current_block.Toplevel,
+                       $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block,
                                sn2, GetLocation ($1));
                }
 
@@ -5661,18 +5669,15 @@ join_clause
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
                        
-               if (sn2 == null)
-                       ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
-               else
-                       ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn2);
+               ((Linq.QueryBlock)current_block).AddRangeVariable (sn2 ?? sn);
          }
        | JOIN type IDENTIFIER IN
          {
                if (linq_clause_blocks == null)
-                       linq_clause_blocks = new Stack<Block> ();
+                       linq_clause_blocks = new Stack<Linq.QueryBlock> ();
                        
                current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
-               linq_clause_blocks.Push (current_block);
+               linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression ON
          {
@@ -5680,7 +5685,7 @@ join_clause
                current_block = current_block.Parent;
 
                current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
-               linq_clause_blocks.Push (current_block);
+               linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
          }
          expression EQUALS
          {
@@ -5688,35 +5693,33 @@ join_clause
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
 
-               var lt = (Tokenizer.LocatedToken) $3;
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), lexer.Location);
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
          }
          expression opt_join_into
          {
                var lt = (Tokenizer.LocatedToken) $3;
                var sn = new SimpleMemberName (lt.Value, lt.Location);
                SimpleMemberName sn2 = null;
-               ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
-               ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
+               var outer_selector = linq_clause_blocks.Pop ();
+               var block = linq_clause_blocks.Pop ();
                
-               Linq.Cast cast = new Linq.Cast ((FullNamedExpression)$2, (Expression)$6);
                if ($13 == null) {
-                       $$ = new Linq.Join (block, sn, cast, outer_selector, current_block.Toplevel, GetLocation ($1));
+                       $$ = new Linq.Join (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1)) {
+                               IdentifierType = (FullNamedExpression)$2
+                       };
                } else {
                        var lt2 = (Tokenizer.LocatedToken) $13;
                        sn2 = new SimpleMemberName (lt2.Value, lt2.Location);
-                       $$ = new Linq.GroupJoin (block, sn, cast, outer_selector, current_block.Toplevel,
-                               sn2, GetLocation ($1));
+                       $$ = new Linq.GroupJoin (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, sn2, GetLocation ($1)) {
+                               IdentifierType = (FullNamedExpression)$2
+                       };                      
                }
                
                current_block.AddStatement (new ContextualReturn ((Expression) $12));
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
                        
-               if (sn2 == null)
-                       ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn);
-               else
-                       ((Linq.QueryBlock)current_block).AddTransparentParameter (compiler, sn2);
+               ((Linq.QueryBlock)current_block).AddRangeVariable (sn2 ?? sn);
          }
        ;
        
@@ -5765,11 +5768,11 @@ orderings_then_by
                current_block.SetEndLocation (lexer.Location);
                current_block = current_block.Parent;
          
-               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);   
+               current_block = new Linq.QueryBlock (compiler, (Linq.QueryBlock) current_block, lexer.Location);         
         }
         then_by
         {
-               ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
+               ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$4;
                $$ = $1;
         }
        ;       
@@ -5777,30 +5780,30 @@ orderings_then_by
 order_by
        : expression
          {
-               $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);        
+               $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);       
          }
        | expression ASCENDING
          {
-               $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);        
+               $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);       
          }
        | expression DESCENDING
          {
-               $$ = new Linq.OrderByDescending (current_block.Toplevel, (Expression)$1);       
+               $$ = new Linq.OrderByDescending ((Linq.QueryBlock) current_block, (Expression)$1);      
          }
        ;
 
 then_by
        : expression
          {
-               $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1); 
+               $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1);        
          }
        | expression ASCENDING
          {
-               $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1); 
+               $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1);        
          }
        | expression DESCENDING
          {
-               $$ = new Linq.ThenByDescending (current_block.Toplevel, (Expression)$1);        
+               $$ = new Linq.ThenByDescending ((Linq.QueryBlock) current_block, (Expression)$1);       
          }     
        ;
 
@@ -5815,14 +5818,21 @@ opt_query_continuation
 
                current_block.SetEndLocation (GetLocation ($1));
                current_block = current_block.Parent;
-
-               var lt = (Tokenizer.LocatedToken) $2;
+       
+               current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
                
-               current_block = new Linq.QueryBlock (compiler, current_block, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
+               if (linq_clause_blocks == null)
+                       linq_clause_blocks = new Stack<Linq.QueryBlock> ();
+                       
+               linq_clause_blocks.Push ((Linq.QueryBlock) current_block);              
          }
          query_body
          {
-               $$ = new Linq.QueryExpression (current_block, (Linq.AQueryClause)$4);
+               var current_block = linq_clause_blocks.Pop ();    
+               var lt = (Tokenizer.LocatedToken) $2;
+               $$ = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, null, new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1)) {
+                       next = (Linq.AQueryClause)$4
+               };
          }
        ;
        
@@ -6297,7 +6307,7 @@ Report Report {
 void start_block (Location loc)
 {
        if (current_block == null || parsing_anonymous_method) {
-               current_block = new ToplevelBlock (compiler, current_block, current_local_parameters, current_generic_method, loc);
+               current_block = new ToplevelBlock (compiler, current_block, current_local_parameters, loc);
                parsing_anonymous_method = false;
        } else {
                current_block = new ExplicitBlock (current_block, loc, Location.Null);