Fix #354663
[mono.git] / mcs / mcs / cs-parser.jay
index b4748fa1d68c58e02c6dff4d623d44af38b5c1df..a1f555a4528e3e265e9707e24413659b9787c82a 100644 (file)
@@ -4,6 +4,7 @@
 //
 // Authors: Miguel de Icaza (miguel@gnu.org)
 //          Ravi Pratap     (ravi@ximian.com)
+//          Marek Safar                (marek.safar@gmail.com)
 //
 // Licensed under the terms of the GNU GPL
 //
@@ -33,15 +34,20 @@ namespace Mono.CSharp
        public class CSharpParser {
                NamespaceEntry  current_namespace;
                TypeContainer   current_container;
-               TypeContainer   current_class;
+               DeclSpace       current_class;
        
-               IIteratorContainer iterator_container;
+               IAnonymousHost anonymous_host;
 
                /// <summary>
                ///   Current block is used to add statements as we find
                ///   them.  
                /// </summary>
-               Block      current_block, top_current_block;
+               Block      current_block;
+
+               Delegate   current_delegate;
+               
+               GenericMethod current_generic_method;
+               AnonymousMethodExpression current_anonymous_method;
 
                /// <summary>
                ///   This is used by the unary_expression code to resolve
@@ -57,16 +63,24 @@ namespace Mono.CSharp
                Expression implicit_value_parameter_type;
                Parameters indexer_parameters;
 
+               /// <summary>
+               ///   Hack to help create non-typed array initializer
+               /// </summary>
+               public static Expression current_array_type;
+               Expression pushed_current_array_type;
+
                /// <summary>
                ///   Used to determine if we are parsing the get/set pair
                ///   of an indexer or a property
                /// </summmary>
-               bool  parsing_indexer;
+               bool parsing_indexer;
+
+               bool parsing_anonymous_method;
 
                ///
                /// An out-of-band stack.
                ///
-               Stack oob_stack;
+               static Stack oob_stack;
 
                ///
                /// Switch stack.
@@ -93,10 +107,12 @@ namespace Mono.CSharp
                /// Current attribute target
                string current_attr_target;
                
-               /// assembly and module attribute definition is enabled
+               /// assembly and module attribute definitions are enabled
                bool global_attrs_enabled = true;
-
                bool has_get, has_set;
+               bool parameter_modifiers_not_allowed;
+               bool params_modifiers_not_allowed;
+               bool arglist_allowed;
 
 %}
 
@@ -188,9 +204,25 @@ namespace Mono.CSharp
 %token VIRTUAL 
 %token VOID    
 %token VOLATILE
+%token WHERE
 %token WHILE   
 %token ARGLIST
 %token PARTIAL
+%token ARROW
+%token QUERY_FIRST_TOKEN
+%token FROM
+%token JOIN
+%token ON
+%token EQUALS
+%token SELECT
+%token GROUP
+%token BY
+%token LET
+%token ORDERBY
+%token ASCENDING
+%token DESCENDING
+%token INTO
+%token QUERY_LAST_TOKEN
 
 /* C# keywords which are not really keywords */
 %token GET           "get"
@@ -216,7 +248,9 @@ namespace Mono.CSharp
 %token BANG           "!"
 %token ASSIGN         "="
 %token OP_LT          "<"
+%token OP_GENERICS_LT "<"
 %token OP_GT          ">"
+%token OP_GENERICS_GT ">"
 %token BITWISE_AND    "&"
 %token BITWISE_OR     "|"
 %token STAR           "*"
@@ -226,6 +260,7 @@ namespace Mono.CSharp
 %token INTERR         "?"
 
 /* C# multi-character operators. */
+%token DOUBLE_COLON          "::"
 %token OP_INC                 "++"
 %token OP_DEC                 "--"
 %token OP_SHIFT_LEFT          "<<"
@@ -247,6 +282,7 @@ namespace Mono.CSharp
 %token OP_XOR_ASSIGN          "^="
 %token OP_OR_ASSIGN           "|="
 %token OP_PTR                 "->"
+%token OP_COALESCING          "??"
 
 /* Numbers */
 %token LITERAL_INTEGER           "int literal"
@@ -257,10 +293,14 @@ namespace Mono.CSharp
 %token LITERAL_STRING            "string literal"
 
 %token IDENTIFIER
+%token OPEN_PARENS_LAMBDA
 %token CLOSE_PARENS_CAST
 %token CLOSE_PARENS_NO_CAST
 %token CLOSE_PARENS_OPEN_PARENS
 %token CLOSE_PARENS_MINUS
+%token DEFAULT_OPEN_PARENS
+%token GENERIC_DIMENSION
+%token DEFAULT_COLON
 
 /* Add precedence rules to solve dangling else s/r conflict */
 %nonassoc LOWPREC
@@ -293,13 +333,13 @@ compilation_unit
        
 opt_EOF
        : /* empty */
-       {
+         {
                Lexer.check_incorrect_doc_comment ();
-       }
+         }
        | EOF
-       {
+         {
                Lexer.check_incorrect_doc_comment ();
-       }
+         }
        ;
 
 outer_declarations
@@ -308,10 +348,31 @@ outer_declarations
         ;
  
 outer_declaration
-        : using_directive
+       : extern_alias_directive
+        | using_directive 
         | namespace_member_declaration
         ;
-  
+
+extern_alias_directives
+       : extern_alias_directive
+       | extern_alias_directives extern_alias_directive;
+
+extern_alias_directive
+       : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
+         {
+               LocatedToken lt = (LocatedToken) $2;
+               string s = lt.Value;
+               if (s != "alias"){
+                       Report.Error (1003, lt.Location, "'alias' expected");
+               } else if (RootContext.Version == LanguageVersion.ISO_1) {
+                       Report.FeatureIsNotAvailable (lt.Location, "external alias");
+               } else {
+                       lt = (LocatedToken) $3; 
+                       current_namespace.UsingExternalAlias (lt.Value, lt.Location);
+               }
+         }
+       ;
 using_directives
        : using_directive 
        | using_directives using_directive
@@ -319,32 +380,33 @@ using_directives
 
 using_directive
        : using_alias_directive
-       {
+         {
                if (RootContext.Documentation != null)
                        Lexer.doc_state = XmlCommentState.Allowed;
-       }
+         }
        | using_namespace_directive
-       {
+         {
                if (RootContext.Documentation != null)
                        Lexer.doc_state = XmlCommentState.Allowed;
-       }
+         }
        ;
 
 using_alias_directive
        : USING IDENTIFIER ASSIGN 
          namespace_or_type_name SEMICOLON
          {
-               current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
+               LocatedToken lt = (LocatedToken) $2;
+               current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
          }
        | USING error {
-               CheckIdentifierToken (yyToken);
+               CheckIdentifierToken (yyToken, GetLocation ($2));
          }
        ;
 
 using_namespace_directive
        : USING namespace_name SEMICOLON 
          {
-               current_namespace.Using ((MemberName) $2, lexer.Location);
+               current_namespace.Using ((MemberName) $2, (Location) $1);
           }
        ;
 
@@ -355,19 +417,26 @@ using_namespace_directive
 // 
 namespace_declaration
        : opt_attributes NAMESPACE namespace_or_type_name
-       {
+         {
+               MemberName name = (MemberName) $3;
+
                if ($1 != null) {
-                       Report.Error(1671, Lexer.Location, "A namespace declaration cannot have modifiers or attributes");
+                       Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
                }
 
-               MemberName name = (MemberName) $3;
+               if (name.TypeArguments != null)
+                       syntax_error (lexer.Location, "namespace name expected");
 
                current_namespace = new NamespaceEntry (
-                       current_namespace, file, name.GetName (), lexer.Location);
+                       current_namespace, file, name.GetName ());
+               current_class = current_namespace.SlaveDeclSpace;
+               current_container = current_class.PartialContainer;
          } 
          namespace_body opt_semicolon
          { 
                current_namespace = current_namespace.Parent;
+               current_class = current_namespace.SlaveDeclSpace;
+               current_container = current_class.PartialContainer;
          }
        ;
 
@@ -382,7 +451,14 @@ opt_comma
        ;
 
 namespace_name
-       : namespace_or_type_name
+       : namespace_or_type_name {
+               MemberName name = (MemberName) $1;
+
+               if (name.TypeArguments != null)
+                       syntax_error (lexer.Location, "namespace name expected");
+
+               $$ = name;
+         }
        ;
 
 namespace_body
@@ -391,10 +467,25 @@ namespace_body
                if (RootContext.Documentation != null)
                        Lexer.doc_state = XmlCommentState.Allowed;
          }
+         namespace_body_body
+       ;
+       
+namespace_body_body
+       : opt_extern_alias_directives
          opt_using_directives
          opt_namespace_member_declarations
          CLOSE_BRACE
+       | error
+         {
+               Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
+         }
+         CLOSE_BRACE
+       | opt_extern_alias_directives
+         opt_using_directives
+         opt_namespace_member_declarations
+         EOF
          {
+               Report.Error (1513, lexer.Location, "} expected");
          }
        ;
 
@@ -403,6 +494,11 @@ opt_using_directives
        | using_directives
        ;
 
+opt_extern_alias_directives
+       : /* empty */
+       | extern_alias_directives
+       ;
+
 opt_namespace_member_declarations
        : /* empty */
        | namespace_member_declarations
@@ -420,7 +516,7 @@ namespace_member_declaration
                        DeclSpace ds = (DeclSpace)$1;
 
                        if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
-                               Report.Error (1527, lexer.Location, 
+                               Report.Error (1527, ds.Location, 
                                "Namespace elements cannot be explicitly declared as private, protected or protected internal");
                        }
                }
@@ -431,10 +527,10 @@ namespace_member_declaration
          }
 
        | field_declaration {
-               Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
+               Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
          }
        | method_declaration {
-               Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
+               Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
          }
        ;
 
@@ -449,7 +545,7 @@ type_declaration
 //
 //     | error {
 //             Console.WriteLine ("Token=" + yyToken);
-//             Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
+//             Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
 //       }
        ;
 
@@ -460,8 +556,16 @@ type_declaration
 global_attributes
        : attribute_sections
 {
-       if ($1 != null)
-               CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
+       if ($1 != null) {
+               Attributes attrs = (Attributes)$1;
+               if (global_attrs_enabled) {
+                       CodeGen.Assembly.AddAttributes (attrs.Attrs);
+               } else {
+                       foreach (Attribute a in attrs.Attrs) {
+                               Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
+                       }
+               }
+       }
 
        $$ = $1;
 }
@@ -483,55 +587,63 @@ opt_attributes
 attribute_sections
        : attribute_section
          {
-               ArrayList sect = (ArrayList) $1;
+               if (current_attr_target != String.Empty) {
+                       ArrayList sect = (ArrayList) $1;
 
-               if (global_attrs_enabled) {
-                       if (current_attr_target == "module") {
-                               CodeGen.Module.AddAttributes (sect);
-                               $$ = null;
-                       } else if (current_attr_target == "assembly") {
-                               CodeGen.Assembly.AddAttributes (sect);
-                               $$ = null;
+                       if (global_attrs_enabled) {
+                               if (current_attr_target == "module") {
+                                       CodeGen.Module.AddAttributes (sect);
+                                       $$ = null;
+                               } else if (current_attr_target != null && current_attr_target.Length > 0) {
+                                       CodeGen.Assembly.AddAttributes (sect);
+                                       $$ = null;
+                               } else {
+                                       $$ = new Attributes (sect);
+                               }
+                               if ($$ == null) {
+                                       if (RootContext.Documentation != null) {
+                                               Lexer.check_incorrect_doc_comment ();
+                                               Lexer.doc_state =
+                                                       XmlCommentState.Allowed;
+                                       }
+                               }
                        } else {
                                $$ = new Attributes (sect);
-                       }
-                       if ($$ == null) {
-                               if (RootContext.Documentation != null) {
-                                       Lexer.check_incorrect_doc_comment ();
-                                       Lexer.doc_state =
-                                               XmlCommentState.Allowed;
-                               }
-                       }
-               } else {
-                       $$ = new Attributes (sect);
-               }               
+                       }               
+               }
+               else
+                       $$ = null;
                current_attr_target = null;
          }
        | attribute_sections attribute_section
          {
-               Attributes attrs = $1 as Attributes;
-               ArrayList sect = (ArrayList) $2;
-
-               if (global_attrs_enabled) {
-                       if (current_attr_target == "module") {
-                               CodeGen.Module.AddAttributes (sect);
-                               $$ = null;
-                       } else if (current_attr_target == "assembly") {
-                               CodeGen.Assembly.AddAttributes (sect);
-                               $$ = null;
+               if (current_attr_target != String.Empty) {
+                       Attributes attrs = $1 as Attributes;
+                       ArrayList sect = (ArrayList) $2;
+
+                       if (global_attrs_enabled) {
+                               if (current_attr_target == "module") {
+                                       CodeGen.Module.AddAttributes (sect);
+                                       $$ = null;
+                               } else if (current_attr_target == "assembly") {
+                                       CodeGen.Assembly.AddAttributes (sect);
+                                       $$ = null;
+                               } else {
+                                       if (attrs == null)
+                                               attrs = new Attributes (sect);
+                                       else
+                                               attrs.AddAttributes (sect);                     
+                               }
                        } else {
                                if (attrs == null)
                                        attrs = new Attributes (sect);
                                else
-                                       attrs.AddAttributes (sect);                     
-                       }
-               } else {
-                       if (attrs == null)
-                               attrs = new Attributes (sect);
-                       else
-                               attrs.AddAttributes (sect);
-               }               
-               $$ = attrs;
+                                       attrs.AddAttributes (sect);
+                       }               
+                       $$ = attrs;
+               }
+               else
+                       $$ = null;
                current_attr_target = null;
          }
        ;
@@ -558,11 +670,16 @@ attribute_target_specifier
 attribute_target
        : IDENTIFIER
          {
-               CheckAttributeTarget ((string) $1);
-               $$ = $1;
+               LocatedToken lt = (LocatedToken) $1;
+               $$ = CheckAttributeTarget (lt.Value, lt.Location);
+         }
+       | EVENT  { $$ = "event"; }
+       | RETURN { $$ = "return"; }
+       | error
+         {
+               string name = yyNames [yyToken].ToLower ();
+               $$ = CheckAttributeTarget (name, GetLocation ($1));
          }
-        | EVENT  { $$ = "event"; }       
-        | RETURN { $$ = "return"; }
        ;
 
 attribute_list
@@ -584,24 +701,28 @@ attribute_list
        ;
 
 attribute
-       : attribute_name
-         {
-               $$ = lexer.Location;
-         }
-         opt_attribute_arguments
+       : attribute_name opt_attribute_arguments
          {
-                 Location loc = (Location) $2;
                MemberName mname = (MemberName) $1;
+               if (mname.IsGeneric) {
+                       Report.Error (404, lexer.Location,
+                                     "'<' unexpected: attributes cannot be generic");
+               }
+
+               object [] arguments = (object []) $2;
                MemberName left = mname.Left;
                string identifier = mname.Name;
 
-               Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
+               Expression left_expr = left == null ? null : left.GetTypeExpression ();
 
-               if (current_attr_target == "assembly" || current_attr_target == "module")
-                       $$ = new GlobalAttribute (current_class, current_attr_target,
-                                                 left_expr, identifier, (ArrayList) $3, loc);
+               if (current_attr_target == String.Empty)
+                       $$ = null;
+               else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
+                       // FIXME: supply "nameEscaped" parameter here.
+                       $$ = new GlobalAttribute (current_namespace, current_attr_target,
+                                                 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
                else
-                       $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
+                       $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
          }
        ;
 
@@ -624,29 +745,18 @@ attribute_arguments
                if ($1 == null)
                        $$ = null;
                else {
-                       ArrayList args = new ArrayList (4);
-                       args.Add ($1);
-               
-                       $$ = args;
+                       $$ = new object [] { $1, null };
                }
          }
-        | positional_argument_list COMMA named_argument_list
+    | positional_argument_list COMMA named_argument_list
          {
-               ArrayList args = new ArrayList (4);
-               args.Add ($1);
-               args.Add ($3);
-
-               $$ = args;
+               $$ = new object[] { $1, $3 };
          }
-        | named_argument_list
+    | named_argument_list
          {
-               ArrayList args = new ArrayList (4);
-               args.Add (null);
-               args.Add ($1);
-               
-               $$ = args;
+               $$ = new object [] { null, $1 };
          }
-        ;
+    ;
 
 
 opt_positional_argument_list
@@ -688,7 +798,7 @@ named_argument_list
          }
          | named_argument_list COMMA expression
            {
-                 Report.Error (1016, lexer.Location, "Named attribute argument expected");
+                 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
                  $$ = null;
                }
         ;
@@ -696,8 +806,9 @@ named_argument_list
 named_argument
        : IDENTIFIER ASSIGN expression
          {
+               // FIXME: keep location
                $$ = new DictionaryEntry (
-                       (string) $1
+                       ((LocatedToken) $1).Value
                        new Argument ((Expression) $3, Argument.AType.Expression));
          }
        ;
@@ -735,35 +846,24 @@ struct_declaration
        : opt_attributes
          opt_modifiers
          opt_partial
-         STRUCT member_name
+         STRUCT
+         {
+               lexer.ConstraintsParsing = true;
+         }
+         type_name
          { 
-               MemberName name = MakeName ((MemberName) $5);
-               bool partial = (bool) $3;
-
-               if (partial) {
-                       ClassPart part = PartialContainer.CreatePart (
-                               current_namespace, current_class, name, (int) $2,
-                               (Attributes) $1, Kind.Struct, lexer.Location);
-
-                       current_container = part.PartialContainer;
-                       current_class = part;
-               } else {
-                       current_class = new Struct (
-                               current_namespace, current_class, name, (int) $2,
-                               (Attributes) $1, lexer.Location);
-
-                       current_container.AddClassOrStruct (current_class);
-                       current_container = current_class;
-                       RootContext.Tree.RecordDecl (name, current_class);
-               }
+               MemberName name = MakeName ((MemberName) $6);
+               push_current_class (new Struct (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
          }
          opt_class_base
+         opt_type_parameter_constraints_clauses
          {
-               if ($7 != null)
-                       current_class.Bases = (ArrayList) $7;
+               lexer.ConstraintsParsing = false;
+
+               current_class.SetParameterInfo ((ArrayList) $9);
 
                if (RootContext.Documentation != null)
-                       current_class.DocComment = Lexer.consume_doc_comment ();
+                       current_container.DocComment = Lexer.consume_doc_comment ();
          }
          struct_body
          {
@@ -775,7 +875,7 @@ struct_declaration
                $$ = pop_current_class ();
          }
        | opt_attributes opt_modifiers opt_partial STRUCT error {
-               CheckIdentifierToken (yyToken);
+               CheckIdentifierToken (yyToken, GetLocation ($5));
          }
        ;
 
@@ -828,7 +928,7 @@ constant_declaration
                foreach (VariableDeclaration constant in (ArrayList) $5){
                        Location l = constant.Location;
                        if ((modflags & Modifiers.STATIC) != 0) {
-                               Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
+                               Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
                                continue;
                        }
 
@@ -866,12 +966,12 @@ constant_declarators
 constant_declarator
        : IDENTIFIER ASSIGN constant_expression
          {
-               $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
+               $$ = new VariableDeclaration ((LocatedToken) $1, $3);
          }
        | IDENTIFIER
          {
                // A const field requires a value to be provided
-               Report.Error (145, lexer.Location, "A const field requires a value to be provided");
+               Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
                $$ = null;
          }
        ;
@@ -886,16 +986,20 @@ field_declaration
                Expression type = (Expression) $3;
                int mod = (int) $2;
 
+               current_array_type = null;
+
                foreach (VariableDeclaration var in (ArrayList) $4){
                        Field field = new Field (current_class, type, mod, var.identifier, 
-                                                var.expression_or_array_initializer, 
                                                 (Attributes) $1, var.Location);
 
+                       field.Initializer = var.expression_or_array_initializer;
+
                        if (RootContext.Documentation != null) {
                                field.DocComment = Lexer.consume_doc_comment ();
                                Lexer.doc_state = XmlCommentState.Allowed;
                        }
                        current_container.AddField (field);
+                       $$ = field; // FIXME: might be better if it points to the top item
                }
          }
        | opt_attributes
@@ -908,6 +1012,8 @@ field_declaration
                        Expression type = (Expression) $4;
                        int mod = (int) $2;
 
+                       current_array_type = null;
+
                        foreach (VariableDeclaration var in (ArrayList) $5) {
                                FixedField field = new FixedField (current_class, type, mod, var.identifier,
                                        (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
@@ -917,44 +1023,60 @@ field_declaration
                                        Lexer.doc_state = XmlCommentState.Allowed;
                                }
                                current_container.AddField (field);
+                               $$ = field; // FIXME: might be better if it points to the top item
                        }
          }
+       | opt_attributes
+         opt_modifiers
+         FIXED
+         type 
+         error
+         {
+               Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
+         }
        | opt_attributes
          opt_modifiers
          VOID  
          variable_declarators
          SEMICOLON {
-               Report.Error (670, lexer.Location, "void type is not allowed for fields");
+               current_array_type = null;
+               Report.Error (670, (Location) $3, "Fields cannot have void type");
          }
        ;
 
 fixed_variable_declarators
        : fixed_variable_declarator
-               {
-                       ArrayList decl = new ArrayList (2);
-                       decl.Add ($1);
-                       $$ = decl;
-               }
+         {
+               ArrayList decl = new ArrayList (2);
+               decl.Add ($1);
+               $$ = decl;
+         }
        | fixed_variable_declarators COMMA fixed_variable_declarator
-               {
-                       ArrayList decls = (ArrayList) $1;
-                       decls.Add ($3);
-                       $$ = $1;
-               }
+         {
+               ArrayList decls = (ArrayList) $1;
+               decls.Add ($3);
+               $$ = $1;
+         }
        ;
 
 fixed_variable_declarator
        : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
-               {
-                       $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
-               }
+         {
+               $$ = new VariableDeclaration ((LocatedToken) $1, $3);
+         }
+       | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
+         {
+               Report.Error (443, lexer.Location, "Value or constant expected");
+               $$ = new VariableDeclaration ((LocatedToken) $1, null);
+         }
        ;
 
 variable_declarators
        : variable_declarator 
          {
                ArrayList decl = new ArrayList (4);
-               decl.Add ($1);
+               if ($1 != null)
+                       decl.Add ($1);
                $$ = decl;
          }
        | variable_declarators COMMA variable_declarator
@@ -968,16 +1090,17 @@ variable_declarators
 variable_declarator
        : IDENTIFIER ASSIGN variable_initializer
          {
-               $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
+               $$ = new VariableDeclaration ((LocatedToken) $1, $3);
          }
        | IDENTIFIER
          {
-               $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
+               $$ = new VariableDeclaration ((LocatedToken) $1, null);
          }
        | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
          {
-               Report.Error (650, lexer.Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
-                       "To declare a fixed buffer field, use the fixed keyword before the field type");
+               Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
+                       "To declare a fixed size buffer field, use the fixed keyword before the field type");
+               $$ = null;
          }
        ;
 
@@ -992,46 +1115,34 @@ variable_initializer
          }
        | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
          {
-               $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
+               $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
+         }
+       | ARGLIST
+         {
+               $$ = new ArglistAccess ((Location) $1);
          }
        | STACKALLOC type
          {
-               Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
+               Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
                 $$ = null;
          }
        ;
 
 method_declaration
        : method_header {
-               iterator_container = (IIteratorContainer) $1;
+               anonymous_host = (IAnonymousHost) $1;
                if (RootContext.Documentation != null)
                        Lexer.doc_state = XmlCommentState.NotAllowed;
          }
          method_body
          {
                Method method = (Method) $1;
-               Block b = (Block) $3;
-               const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
-
-               if (b == null){
-                       if ((method.ModFlags & extern_abstract) == 0){
-                               Report.Error (
-                                       501, lexer.Location,  current_container.MakeName (method.Name) +
-                                       "must declare a body because it is not marked abstract or extern");
-                       }
-               } else {
-                       if ((method.ModFlags & Modifiers.EXTERN) != 0){
-                               Report.Error (
-                                       179, lexer.Location, current_container.MakeName (method.Name) +
-                                       " is declared extern, but has a body");
-                       }
-               }
-
                method.Block = (ToplevelBlock) $3;
                current_container.AddMethod (method);
 
+               anonymous_host = null;
+               current_generic_method = null;
                current_local_parameters = null;
-               iterator_container = null;
 
                if (RootContext.Documentation != null)
                        Lexer.doc_state = XmlCommentState.Allowed;
@@ -1047,9 +1158,9 @@ opt_error_modifier
 
                while (m != 0){
                        if ((i & m) != 0){
-                               Report.Error (
-                                       1585, lexer.Location, "Member modifier `" + 
-                                       Modifiers.Name (i) + "' must precede member type and name");
+                               Report.Error (1585, lexer.Location,
+                                       "Member modifier `{0}' must precede the member type and name",
+                                       Modifiers.Name (i));
                        }
                        m &= ~i;
                        i = i << 1;
@@ -1057,19 +1168,55 @@ opt_error_modifier
          }
        ;
 
+       // 
+       // This rule is used to handle the cases where OPEN_PARENS_LAMBDA
+       // is returned (type followed by an identifier), these are the
+       // declarations (methods, overloads, constructors, etc) and a handful
+       // of expressions ("using", "fixed") or "catch".
+       //
+open_parens
+       : OPEN_PARENS
+       | OPEN_PARENS_LAMBDA
+       ;
+
 method_header
        : opt_attributes
          opt_modifiers
-         type namespace_or_type_name
-         OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
+         type member_name
+         open_parens
+         {
+               arglist_allowed = true;
+         }
+         opt_formal_parameter_list CLOSE_PARENS 
          {
+               lexer.ConstraintsParsing = true;
+         }
+         opt_type_parameter_constraints_clauses
+         {
+               lexer.ConstraintsParsing = false;
+               arglist_allowed = false;
                MemberName name = (MemberName) $4;
+               current_local_parameters = (Parameters) $7;
+
+               if ($10 != null && name.TypeArguments == null)
+                       Report.Error (80, lexer.Location,
+                                     "Constraints are not allowed on non-generic declarations");
 
-               Method method = new Method (current_class, (Expression) $3, (int) $2,
-                                           false, name,  (Parameters) $6, (Attributes) $1,
-                                           lexer.Location);
+               Method method;
 
-               current_local_parameters = (Parameters) $6;
+               GenericMethod generic = null;
+               if (name.TypeArguments != null) {
+                       generic = new GenericMethod (current_namespace, current_class, name,
+                                                    (Expression) $3, current_local_parameters);
+
+                       generic.SetParameterInfo ((ArrayList) $10);
+               }
+
+               method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
+                                    name, current_local_parameters, (Attributes) $1);
+
+               anonymous_host = method;
+               current_generic_method = generic;
 
                if (RootContext.Documentation != null)
                        method.DocComment = Lexer.consume_doc_comment ();
@@ -1078,16 +1225,99 @@ method_header
          }
        | opt_attributes
          opt_modifiers
-         VOID namespace_or_type_name
-         OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
+         VOID member_name
+         open_parens
          {
+               arglist_allowed = true;
+         }       
+         opt_formal_parameter_list CLOSE_PARENS
+         {
+               lexer.ConstraintsParsing = true;
+         }
+         opt_type_parameter_constraints_clauses
+         {
+               lexer.ConstraintsParsing = false;
+               arglist_allowed = false;
+
                MemberName name = (MemberName) $4;
+               current_local_parameters = (Parameters) $7;
+
+               if ($10 != null && name.TypeArguments == null)
+                       Report.Error (80, lexer.Location,
+                                     "Constraints are not allowed on non-generic declarations");
+
+               Method method;
+               GenericMethod generic = null;
+               if (name.TypeArguments != null) {
+                       generic = new GenericMethod (current_namespace, current_class, name,
+                                                    TypeManager.system_void_expr, current_local_parameters);
+
+                       generic.SetParameterInfo ((ArrayList) $10);
+               }
+
+               method = new Method (current_class, generic, TypeManager.system_void_expr,
+                                    (int) $2, false, name, current_local_parameters, (Attributes) $1);
+
+               anonymous_host = method;
+               current_generic_method = generic;
+
+               if (RootContext.Documentation != null)
+                       method.DocComment = Lexer.consume_doc_comment ();
+
+               $$ = method;
+       }
+       | opt_attributes
+         opt_modifiers
+         PARTIAL
+         VOID member_name
+         open_parens opt_formal_parameter_list CLOSE_PARENS 
+         {
+               lexer.ConstraintsParsing = true;
+         }
+         opt_type_parameter_constraints_clauses
+         {
+               lexer.ConstraintsParsing = false;
+
+               MemberName name = (MemberName) $5;
+               current_local_parameters = (Parameters) $7;
+
+               if ($9 != null && name.TypeArguments == null)
+                       Report.Error (80, lexer.Location,
+                                     "Constraints are not allowed on non-generic declarations");
+
+               Method method;
+               GenericMethod generic = null;
+               if (name.TypeArguments != null) {
+                       generic = new GenericMethod (current_namespace, current_class, name,
+                                                    TypeManager.system_void_expr, current_local_parameters);
+
+                       generic.SetParameterInfo ((ArrayList) $10);
+               }
 
-               Method method = new Method (current_class, TypeManager.system_void_expr,
-                                           (int) $2, false, name, (Parameters) $6,
-                                           (Attributes) $1, lexer.Location);
+               int modifiers = (int) $2;
 
-               current_local_parameters = (Parameters) $6;
+
+               const int invalid_partial_mod = Modifiers.Accessibility | Modifiers.ABSTRACT | Modifiers.EXTERN |
+                       Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
+
+               if ((modifiers & invalid_partial_mod) != 0) {
+                       Report.Error (750, name.Location, "A partial method cannot define access modifier or " +
+                               "any of abstract, extern, new, override, sealed, or virtual modifiers");
+                       modifiers &= ~invalid_partial_mod;
+               }
+
+               if ((current_class.ModFlags & Modifiers.PARTIAL) == 0) {
+                       Report.Error (751, name.Location, "A partial method must be declared within a " +
+                               "partial class or partial struct");
+               }
+
+               modifiers |= Modifiers.PARTIAL | Modifiers.PRIVATE;
+               
+               method = new Method (current_class, generic, TypeManager.system_void_expr,
+                                    modifiers, false, name, current_local_parameters, (Attributes) $1);
+
+               anonymous_host = method;
+               current_generic_method = generic;
 
                if (RootContext.Documentation != null)
                        method.DocComment = Lexer.consume_doc_comment ();
@@ -1097,23 +1327,21 @@ method_header
        | opt_attributes
          opt_modifiers
          type 
-         modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
+         modifiers member_name open_parens opt_formal_parameter_list CLOSE_PARENS
          {
-               Report.Error (1585, lexer.Location, 
-                       String.Format ("Modifier {0} should appear before type", 
-                               Modifiers.Name ((int) $4)));
-               MemberName name = (MemberName) $4;
+               MemberName name = (MemberName) $5;
+               Report.Error (1585, name.Location, 
+                       "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
 
-               Method method = new Method (current_class, TypeManager.system_void_expr,
-                                           0, false, name, (Parameters) $7, (Attributes) $1,
-                                           lexer.Location);
+               Method method = new Method (current_class, null, TypeManager.system_void_expr,
+                                           0, false, name, (Parameters) $7, (Attributes) $1);
 
                current_local_parameters = (Parameters) $7;
 
                if (RootContext.Documentation != null)
                        method.DocComment = Lexer.consume_doc_comment ();
 
-               $$ = method;
+               $$ = null;
          }
        ;
 
@@ -1126,6 +1354,19 @@ opt_formal_parameter_list
        : /* empty */                   { $$ = Parameters.EmptyReadOnlyParameters; }
        | formal_parameter_list
        ;
+       
+opt_parameter_list_no_mod
+       : /* empty */                   { $$ = Parameters.EmptyReadOnlyParameters; }
+       | 
+         {
+               parameter_modifiers_not_allowed = true;
+         }
+         formal_parameter_list
+         {
+               parameter_modifiers_not_allowed = false;
+               $$ = $2;
+         }
+       ;
 
 formal_parameter_list
        : fixed_parameters              
@@ -1135,43 +1376,57 @@ formal_parameter_list
                Parameter [] pars = new Parameter [pars_list.Count];
                pars_list.CopyTo (pars);
 
-               $$ = new Parameters (pars, null, lexer.Location); 
+               $$ = new Parameters (pars); 
          } 
        | fixed_parameters COMMA parameter_array
          {
                ArrayList pars_list = (ArrayList) $1;
+               pars_list.Add ($3);
 
                Parameter [] pars = new Parameter [pars_list.Count];
                pars_list.CopyTo (pars);
 
-               $$ = new Parameters (pars, (Parameter) $3, lexer.Location); 
+               $$ = new Parameters (pars); 
          }
-       | fixed_parameters COMMA ARGLIST
+       | fixed_parameters COMMA arglist_modifier
          {
                ArrayList pars_list = (ArrayList) $1;
+               //pars_list.Add (new ArglistParameter (GetLocation ($3)));
 
                Parameter [] pars = new Parameter [pars_list.Count];
                pars_list.CopyTo (pars);
 
-               $$ = new Parameters (pars, true, lexer.Location);
+               $$ = new Parameters (pars, true);
+         }
+       | parameter_array COMMA error
+         {
+               if ($1 != null)
+                       Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
+               $$ = null;
+         }
+       | fixed_parameters COMMA parameter_array COMMA error
+         {
+               if ($3 != null)
+                       Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
+               $$ = null;
          }
-       | parameter_array COMMA fixed_parameters
+       | arglist_modifier COMMA error
          {
-               Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
+               Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
                $$ = null;
          }
-       | ARGLIST COMMA fixed_parameters
+       | fixed_parameters COMMA ARGLIST COMMA error 
          {
-               Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
+               Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
                $$ = null;
          }
        | parameter_array 
          {
-               $$ = new Parameters (null, (Parameter) $1, lexer.Location);
+               $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
          }
-       | ARGLIST
+       | arglist_modifier
          {
-               $$ = new Parameters (null, true, lexer.Location);
+               $$ = new Parameters (new Parameter[0], true);
          }
        ;
 
@@ -1186,8 +1441,12 @@ fixed_parameters
        | fixed_parameters COMMA fixed_parameter
          {
                ArrayList pars = (ArrayList) $1;
-
-               pars.Add ($3);
+               Parameter p = (Parameter)$3;
+               if (p != null) {
+                       if ((p.modFlags & Parameter.Modifier.This) != 0)
+                               Report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
+                       pars.Add (p);
+               }
                $$ = $1;
          }
        ;
@@ -1198,20 +1457,30 @@ fixed_parameter
          type
          IDENTIFIER
          {
-               $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
+               LocatedToken lt = (LocatedToken) $4;
+               $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
+         }
+       | opt_attributes
+         opt_parameter_modifier
+         type
+         IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
+         {
+               LocatedToken lt = (LocatedToken) $4;
+               Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
+               $$ = null;
          }
        | opt_attributes
          opt_parameter_modifier
          type
          {
-               Report.Error (1001, lexer.Location, "Identifier expected");
+               Report.Error (1001, GetLocation ($3), "Identifier expected");
                $$ = null;
          }
        | opt_attributes
          opt_parameter_modifier
          type
          error {
-               CheckIdentifierToken (yyToken);
+               CheckIdentifierToken (yyToken, GetLocation ($4));
                $$ = null;
          }
        | opt_attributes
@@ -1221,35 +1490,110 @@ fixed_parameter
          ASSIGN
          constant_expression
           {
-                Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
+               LocatedToken lt = (LocatedToken) $4;
+               Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
                 $$ = null;
           }
        ;
 
 opt_parameter_modifier
        : /* empty */           { $$ = Parameter.Modifier.NONE; }
-       | parameter_modifier
+       | parameter_modifiers
+       ;
+
+parameter_modifiers
+       : parameter_modifier
+         {
+               $$ = $1;
+         }
+       | parameter_modifiers parameter_modifier
+         {
+               Parameter.Modifier p2 = (Parameter.Modifier)$2;
+               Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
+               if (((Parameter.Modifier)$1 & p2) == p2) {
+                       Error_DuplicateParameterModifier (lexer.Location, p2);
+               } else {
+                       switch (mod & ~Parameter.Modifier.This) {
+                               case Parameter.Modifier.REF:
+                                       Report.Error (1101, lexer.Location, "The parameter modifiers `this' and `ref' cannot be used altogether");
+                                       break;
+                               case Parameter.Modifier.OUT:
+                                       Report.Error (1102, lexer.Location, "The parameter modifiers `this' and `out' cannot be used altogether");
+                                       break;
+                               default:
+                                       Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
+                                       break;
+                       }
+               }
+               $$ = mod;
+         }
        ;
 
 parameter_modifier
-       : REF                   { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
-       | OUT                   { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
+       : REF
+         {
+               if (parameter_modifiers_not_allowed)
+                       Error_ParameterModifierNotValid ("ref", (Location)$1);
+                       
+               $$ = Parameter.Modifier.REF;
+         }
+       | OUT
+         {
+               if (parameter_modifiers_not_allowed)
+                       Error_ParameterModifierNotValid ("out", (Location)$1);
+         
+               $$ = Parameter.Modifier.OUT;
+         }
+       | THIS
+         {
+               if (parameter_modifiers_not_allowed)
+                       Error_ParameterModifierNotValid ("this", (Location)$1);
+
+               if (RootContext.Version <= LanguageVersion.ISO_2)
+                       Report.FeatureIsNotAvailable (GetLocation ($1), "extension methods");
+                               
+               $$ = Parameter.Modifier.This;
+         }
        ;
 
 parameter_array
-       : opt_attributes PARAMS type IDENTIFIER
+       : opt_attributes params_modifier type IDENTIFIER
          { 
-               $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
-               note ("type must be a single-dimension array type"); 
+               LocatedToken lt = (LocatedToken) $4;
+               $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
+         }
+       | opt_attributes params_modifier type error {
+               CheckIdentifierToken (yyToken, GetLocation ($4));
+               $$ = null;
          }
-       | opt_attributes PARAMS parameter_modifier type IDENTIFIER 
+       ;
+       
+params_modifier
+       : PARAMS
          {
-               Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
-                $$ = null;
+               if (params_modifiers_not_allowed)
+                       Report.Error (1670, ((Location) $1), "The `params' modifier is not allowed in current context");
          }
-       | opt_attributes PARAMS type error {
-               CheckIdentifierToken (yyToken);
-               $$ = null;
+       | PARAMS parameter_modifier
+         {
+               Parameter.Modifier mod = (Parameter.Modifier)$2;
+               if ((mod & Parameter.Modifier.This) != 0) {
+                       Report.Error (1104, (Location)$1, "The parameter modifiers `this' and `params' cannot be used altogether");
+               } else {
+                       Report.Error (1611, (Location)$1, "The params parameter cannot be declared as ref or out");
+               }         
+         }
+       | PARAMS params_modifier
+         {
+               Error_DuplicateParameterModifier ((Location)$1, Parameter.Modifier.PARAMS);
+         }
+       ;
+       
+arglist_modifier
+       : ARGLIST
+         {
+               if (!arglist_allowed)
+                       Report.Error (1669, (Location) $1, "__arglist is not valid in this context");
          }
        ;
 
@@ -1267,10 +1611,6 @@ property_declaration
                implicit_value_parameter_type = (Expression) $3;
 
                lexer.PropertyParsing = true;
-
-               $$ = lexer.Location;
-
-               iterator_container = SimpleIteratorContainer.GetSimple ();
          }
          accessor_declarations 
          {
@@ -1283,21 +1623,20 @@ property_declaration
                        break;
 
                Property prop;
-               Pair pair = (Pair) $8;
-               Accessor get_block = (Accessor) pair.First;
-               Accessor set_block = (Accessor) pair.Second;
+               Accessors accessors = (Accessors) $8;
+               Accessor get_block = accessors.get_or_add;
+               Accessor set_block = accessors.set_or_remove;
 
-               Location loc = (Location) $7;
                MemberName name = (MemberName) $4;
 
+               if (name.TypeArguments != null)
+                       syntax_error (lexer.Location, "a property can't have type arguments");
+
                prop = new Property (current_class, (Expression) $3, (int) $2, false,
-                                    name, (Attributes) $1, get_block, set_block, loc);
-               if (SimpleIteratorContainer.Simple.Yields)
-                       prop.SetYields ();
-               
+                                    name, (Attributes) $1, get_block, set_block, accessors.declared_in_reverse, current_block);
+
                current_container.AddProperty (prop);
                implicit_value_parameter_type = null;
-               iterator_container = null;
 
                if (RootContext.Documentation != null)
                        prop.DocComment = ConsumeStoredComment ();
@@ -1308,27 +1647,28 @@ property_declaration
 accessor_declarations
        : get_accessor_declaration
         {
-               $$ = new Pair ($1, null);
+               $$ = new Accessors ((Accessor) $1, null);
         }
        | get_accessor_declaration accessor_declarations
         { 
-               Pair pair = (Pair) $2;
-               pair.First = $1;
-               $$ = pair;
+               Accessors accessors = (Accessors) $2;
+               accessors.get_or_add = (Accessor) $1;
+               $$ = accessors;
         }
        | set_accessor_declaration
         {
-               $$ = new Pair (null, $1);
+               $$ = new Accessors (null, (Accessor) $1);
         }
        | set_accessor_declaration accessor_declarations
         { 
-               Pair pair = (Pair) $2;
-               pair.Second = $1;
-               $$ = pair;
+               Accessors accessors = (Accessors) $2;
+               accessors.set_or_remove = (Accessor) $1;
+               accessors.declared_in_reverse = true;
+               $$ = accessors;
         }
        | error
          {
-               Report.Error (1014, lexer.Location, "A get or set accessor expected");
+               Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
                $$ = null;
          }
        ;
@@ -1343,21 +1683,28 @@ get_accessor_declaration
                else 
                        current_local_parameters = indexer_parameters;
                lexer.PropertyParsing = false;
+
+               anonymous_host = SimpleAnonymousHost.GetSimple ();
          }
           accessor_body
          {
                if (has_get) {
-                       Report.Error (1007, lexer.Location, "Property accessor already defined");
+                       Report.Error (1007, (Location) $3, "Property accessor already defined");
                        break;
                }
-               $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
+               Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
                has_get = true;
                current_local_parameters = null;
                lexer.PropertyParsing = true;
 
+               SimpleAnonymousHost.Simple.Propagate (accessor);
+               anonymous_host = null;
+
                if (RootContext.Documentation != null)
                        if (Lexer.doc_state == XmlCommentState.Error)
                                Lexer.doc_state = XmlCommentState.NotAllowed;
+
+               $$ = accessor;
          }
        ;
 
@@ -1367,12 +1714,12 @@ set_accessor_declaration
                Parameter [] args;
                Parameter implicit_value_parameter = new Parameter (
                        implicit_value_parameter_type, "value", 
-                       Parameter.Modifier.NONE, null);
+                       Parameter.Modifier.NONE, null, (Location) $3);
 
                if (parsing_indexer == false) {
                        args  = new Parameter [1];
                        args [0] = implicit_value_parameter;
-                       current_local_parameters = new Parameters (args, null, lexer.Location);
+                       current_local_parameters = new Parameters (args);
                } else {
                        Parameter [] fpars = indexer_parameters.FixedParameters;
 
@@ -1385,25 +1732,32 @@ set_accessor_declaration
                        } else 
                                args = null;
                        current_local_parameters = new Parameters (
-                               args, indexer_parameters.ArrayParameter, lexer.Location);
+                               args);
                }
                
                lexer.PropertyParsing = false;
+
+               anonymous_host = SimpleAnonymousHost.GetSimple ();
          }
          accessor_body
          {
                if (has_set) {
-                       Report.Error (1007, lexer.Location, "Property accessor already defined");
+                       Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
                        break;
                }
-               $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
+               Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
                has_set = true;
                current_local_parameters = null;
                lexer.PropertyParsing = true;
 
+               SimpleAnonymousHost.Simple.Propagate (accessor);
+               anonymous_host = null;
+
                if (RootContext.Documentation != null
                        && Lexer.doc_state == XmlCommentState.Error)
                        Lexer.doc_state = XmlCommentState.NotAllowed;
+
+               $$ = accessor;
          }
        ;
 
@@ -1416,38 +1770,28 @@ interface_declaration
        : opt_attributes
          opt_modifiers
          opt_partial
-         INTERFACE member_name
+         INTERFACE
          {
-               MemberName name = MakeName ((MemberName) $5);
-               bool partial = (bool) $3;
-
-               if (partial) {
-                       ClassPart part = PartialContainer.CreatePart (
-                               current_namespace, current_class, name, (int) $2,
-                               (Attributes) $1, Kind.Interface, lexer.Location);
-
-                       current_container = part.PartialContainer;
-                       current_class = part;
-               } else {
-                       current_class = new Interface (
-                               current_namespace, current_class, name, (int) $2,
-                               (Attributes) $1, lexer.Location);
-
-                       current_container.AddInterface (current_class);
-                       current_container = current_class;
-                       RootContext.Tree.RecordDecl (name, current_class);
-               }
+               lexer.ConstraintsParsing = true;
+         }
+         type_name
+         {
+               MemberName name = MakeName ((MemberName) $6);
+               push_current_class (new Interface (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
          }
          opt_class_base
+         opt_type_parameter_constraints_clauses
          {
-               current_class.Bases = (ArrayList) $7;
+               lexer.ConstraintsParsing = false;
+
+               current_class.SetParameterInfo ((ArrayList) $9);
 
                if (RootContext.Documentation != null) {
-                       current_class.DocComment = Lexer.consume_doc_comment ();
+                       current_container.DocComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
          }
-         interface_body 
+         interface_body
          {
                if (RootContext.Documentation != null)
                        Lexer.doc_state = XmlCommentState.Allowed;
@@ -1457,7 +1801,7 @@ interface_declaration
                $$ = pop_current_class ();
          }
        | opt_attributes opt_modifiers opt_partial INTERFACE error {
-               CheckIdentifierToken (yyToken);
+               CheckIdentifierToken (yyToken, GetLocation ($5));
          }
        ;
 
@@ -1486,8 +1830,8 @@ interface_member_declaration
                Method m = (Method) $1;
 
                if (m.IsExplicitImpl)
-                       Report.Error (541, lexer.Location, 
-                               "Explicit interface declaration can only be declared in a class or struct");
+                       Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
+                               m.GetSignatureForError ());
 
                current_container.AddMethod (m);
 
@@ -1502,22 +1846,22 @@ interface_member_declaration
                Property p = (Property) $1;
 
                if (p.IsExplicitImpl)
-                       Report.Error (541, lexer.Location, 
-                               "Explicit interface declaration can only be declared in a class or struct");
+                       Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
+                               p.GetSignatureForError ());
 
                current_container.AddProperty (p);
 
                if (RootContext.Documentation != null)
                        Lexer.doc_state = XmlCommentState.Allowed;
-       }
+         }
        | interface_event_declaration 
           { 
                if ($1 != null){
                        Event e = (Event) $1;
 
                        if (e.IsExplicitImpl)
-                               Report.Error (541, lexer.Location, 
-                                   "Explicit interface declaration can only be declared in a class or struct");
+                       Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
+                               e.GetSignatureForError ());
                        
                        current_container.AddEvent (e);
                }
@@ -1533,8 +1877,8 @@ interface_member_declaration
                Indexer i = (Indexer) $1;
 
                if (i.IsExplicitImpl)
-                       Report.Error (541, lexer.Location, 
-                               "Explicit interface declaration can only be declared in a class or struct");
+                       Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
+                               i.GetSignatureForError ());
 
                current_container.AddIndexer (i);
 
@@ -1543,27 +1887,42 @@ interface_member_declaration
          }
        | delegate_declaration
          {
-               Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
+               if ($1 != null) {
+                       Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
+                               ((MemberCore)$1).GetSignatureForError ());
+               }
          }
        | class_declaration
          {
-               Report.Error (524, lexer.Location, "Interfaces can not declare classes");
+               if ($1 != null) {
+                       Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
+                               ((MemberCore)$1).GetSignatureForError ());
+               }
          }
        | struct_declaration
          {
-               Report.Error (524, lexer.Location, "Interfaces can not declare structures");
+               if ($1 != null) {
+                       Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
+                               ((MemberCore)$1).GetSignatureForError ());
+               }
          }
        | enum_declaration 
          {
-               Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
+               if ($1 != null) {
+                       Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
+                               ((MemberCore)$1).GetSignatureForError ());
+               }
          }
        | interface_declaration 
          {
-               Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
+               if ($1 != null) {
+                       Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
+                               ((MemberCore)$1).GetSignatureForError ());
+               }
          } 
        | constant_declaration
          {
-               Report.Error (525, lexer.Location, "Interfaces cannot contain constants");
+               Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
          }
        ;
 
@@ -1571,29 +1930,94 @@ opt_new
        : opt_modifiers 
          {
                int val = (int) $1;
-               val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
+               val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
                $$ = val;
          }
        ;
 
+interface_method_declaration_body
+       : OPEN_BRACE 
+         {
+               Report.Error (531, (Location)$1,
+                             "`{0}.{1}{2}': interface members cannot have a definition",
+                             current_class.GetSignatureForError (),
+                             ((MemberName) $-0).GetSignatureForError (),
+                             ((Parameters)$-4).GetSignatureForError ());
+         
+               lexer.ConstraintsParsing = false;
+         }
+         opt_statement_list CLOSE_BRACE
+         {
+               $$ = null;
+         }
+       | SEMICOLON
+       ;
+
 interface_method_declaration
        : opt_attributes opt_new type namespace_or_type_name
-         OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
-         SEMICOLON
+         open_parens opt_formal_parameter_list CLOSE_PARENS
          {
+               lexer.ConstraintsParsing = true;
+         }
+         opt_type_parameter_constraints_clauses
+         {
+               // Refer to the name as $-1 in interface_method_declaration_body          
+               $$ = $4;
+         }
+         interface_method_declaration_body
+         {
+               lexer.ConstraintsParsing = false;
+
                MemberName name = (MemberName) $4;
 
-               $$ = new Method (current_class, (Expression) $3, (int) $2, true,
-                                name, (Parameters) $6, (Attributes) $1, lexer.Location);
+               if ($9 != null && name.TypeArguments == null)
+                       Report.Error (80, lexer.Location,
+                                     "Constraints are not allowed on non-generic declarations");
+
+               GenericMethod generic = null;
+               if (name.TypeArguments != null) {
+                       generic = new GenericMethod (current_namespace, current_class, name,
+                                                    (Expression) $3, (Parameters) $6);
+
+                       generic.SetParameterInfo ((ArrayList) $9);
+               }
+
+               $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
+                                (Parameters) $6, (Attributes) $1);
                if (RootContext.Documentation != null)
                        ((Method) $$).DocComment = Lexer.consume_doc_comment ();
          }
-       | opt_attributes opt_new type namespace_or_type_name
-         OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
-         OPEN_BRACE opt_statement_list CLOSE_BRACE
+       | opt_attributes opt_new VOID namespace_or_type_name
+         open_parens opt_formal_parameter_list CLOSE_PARENS
          {
-               Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", $4);
-               $$ = null;
+               lexer.ConstraintsParsing = true;
+         }
+         opt_type_parameter_constraints_clauses
+         {
+               $$ = $4;
+         }
+         interface_method_declaration_body
+         {
+               lexer.ConstraintsParsing = false;
+
+               MemberName name = (MemberName) $4;
+
+               if ($9 != null && name.TypeArguments == null)
+                       Report.Error (80, lexer.Location,
+                                     "Constraints are not allowed on non-generic declarations");
+
+               GenericMethod generic = null;
+               if (name.TypeArguments != null) {
+                       generic = new GenericMethod (current_namespace, current_class, name,
+                                                    TypeManager.system_void_expr, (Parameters) $6);
+
+                       generic.SetParameterInfo ((ArrayList) $9);
+               }
+
+               $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
+                                true, name, (Parameters) $6, (Attributes) $1);
+               if (RootContext.Documentation != null)
+                       ((Method) $$).DocComment = Lexer.consume_doc_comment ();
          }
        ;
 
@@ -1602,113 +2026,158 @@ interface_property_declaration
          opt_new
          type IDENTIFIER 
          OPEN_BRACE 
-         { lexer.PropertyParsing = true; }
-         interface_accessors 
-         { lexer.PropertyParsing = false; }
+         {
+               lexer.PropertyParsing = true;
+               implicit_value_parameter_type = (Expression)$3;
+         }
+         accessor_declarations 
+         {
+               has_get = has_set = false; 
+               lexer.PropertyParsing = false;
+               implicit_value_parameter_type = null;
+         }
          CLOSE_BRACE
          {
+               LocatedToken lt = (LocatedToken) $4;
+               MemberName name = new MemberName (lt.Value, lt.Location);
+
                if ($3 == TypeManager.system_void_expr) {
-                       Report.Error (547, lexer.Location, "'{0}': property cannot have void type", $4);
+                       Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
                        break;
                }
 
-               if ($7 == null)
+               Property p = null;
+               if ($7 == null) {
+                       p = new Property (current_class, (Expression) $3, (int) $2, true,
+                                  name, (Attributes) $1,
+                                  null, null, false);
+
+                       Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
+                       break;
+               }
+
+               Accessors accessor = (Accessors) $7;
+               p = new Property (current_class, (Expression) $3, (int) $2, true,
+                                  name, (Attributes) $1,
+                                  accessor.get_or_add, accessor.set_or_remove, accessor.declared_in_reverse);
+
+               if (accessor.get_or_add != null && accessor.get_or_add.Block != null) {
+                       Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
+                       $$ = null;
                        break;
+               }
 
-                InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
+               if (accessor.set_or_remove != null && accessor.set_or_remove.Block != null) {
+                       Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
+                       $$ = null;
+                       break;
+               }
 
-               $$ = new Property (current_class, (Expression) $3, (int) $2, true,
-                                  new MemberName ((string) $4), (Attributes) $1,
-                                  pinfo.Get, pinfo.Set, lexer.Location);
                if (RootContext.Documentation != null)
-                       ((Property) $$).DocComment = Lexer.consume_doc_comment ();
+                       p.DocComment = Lexer.consume_doc_comment ();
+
+               $$ = p;
          }
        | opt_attributes
          opt_new
          type error {
-               CheckIdentifierToken (yyToken);
+               CheckIdentifierToken (yyToken, GetLocation ($4));
                $$ = null;
          }
        ;
 
-interface_accessors
-       : opt_attributes opt_modifiers GET SEMICOLON    
-       { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
-       | opt_attributes opt_modifiers GET OPEN_BRACE
-        {  
-               Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", ".get");
-               $$ = null;
-        }
-       | opt_attributes opt_modifiers SET SEMICOLON            
-       { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
-       | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON 
-         { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
-       | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
-         { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
-       |
-         {
-               Report.Error (548, lexer.Location, "'{0}' : property or indexer must have at least one accessor", "");
-         }
-       ;
 
 interface_event_declaration
        : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
          {
+               LocatedToken lt = (LocatedToken) $5;
                $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
-                                    new MemberName ((string) $5), null,
-                                    (Attributes) $1, lexer.Location);
+                                    new MemberName (lt.Value, lt.Location),
+                                    (Attributes) $1);
                if (RootContext.Documentation != null)
                        ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
          }
        | opt_attributes opt_new EVENT type error {
-               CheckIdentifierToken (yyToken);
+               CheckIdentifierToken (yyToken, GetLocation ($5));
                $$ = null;
          }
        | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN  {
-               Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
+               LocatedToken lt = (LocatedToken) $5;
+               Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
                $$ = null;
          }
        | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
-       {
+         {
+               implicit_value_parameter_type = (Expression) $4;
                lexer.EventParsing = true;
-       }
-       event_accessor_declarations
-       {
+         }
+         event_accessor_declarations
+         {
                lexer.EventParsing = false;
-       }
-       CLOSE_BRACE {
-               Report.Error (69, lexer.Location, "Event in interface cannot have add or remove accessors");
+               implicit_value_parameter_type = null;
+         }
+         CLOSE_BRACE {
+               Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
                $$ = null;
          }
        ;
 
 interface_indexer_declaration 
        : opt_attributes opt_new type THIS 
-         OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
-         OPEN_BRACE 
-         { lexer.PropertyParsing = true; }
-         interface_accessors 
-         { lexer.PropertyParsing = false; }
+         OPEN_BRACKET opt_parameter_list_no_mod CLOSE_BRACKET
+         OPEN_BRACE
+         {
+               lexer.PropertyParsing = true;
+               implicit_value_parameter_type = (Expression)$3;
+         }
+         accessor_declarations 
+         { 
+               has_get = has_set = false;
+               lexer.PropertyParsing = false;
+               implicit_value_parameter_type = null;
+         }
          CLOSE_BRACE
          {
-               if ($10 == null)
-                       break;
+               Indexer i = null;
+               if ($10 == null) {
+                       i = new Indexer (current_class, (Expression) $3,
+                                 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
+                                 (int) $2, true, (Parameters) $6, (Attributes) $1,
+                                 null, null, false);
 
-               InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
+                       Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
+                       break;
+               }
 
-               $$ = new Indexer (current_class, (Expression) $3,
-                                 new MemberName (TypeContainer.DefaultIndexerName),
+               Accessors accessors = (Accessors) $10;
+               i = new Indexer (current_class, (Expression) $3,
+                                 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
                                  (int) $2, true, (Parameters) $6, (Attributes) $1,
-                                 info.Get, info.Set, lexer.Location);
+                                  accessors.get_or_add, accessors.set_or_remove, accessors.declared_in_reverse);
+
+               if (accessors.get_or_add != null && accessors.get_or_add.Block != null) {
+                       Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
+                       $$ = null;
+                       break;
+               }
+
+               if (accessors.set_or_remove != null && accessors.set_or_remove.Block != null) {
+                       Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
+                       $$ = null;
+                       break;
+               }
+
                if (RootContext.Documentation != null)
-                       ((Indexer) $$).DocComment = ConsumeStoredComment ();
+                       i.DocComment = ConsumeStoredComment ();
+
+               $$ = i;
          }
        ;
 
 operator_declaration
        : opt_attributes opt_modifiers operator_declarator 
          {
-               iterator_container = SimpleIteratorContainer.GetSimple ();
+               anonymous_host = SimpleAnonymousHost.GetSimple ();
          }
          operator_body
          {
@@ -1716,16 +2185,9 @@ operator_declaration
                        break;
 
                OperatorDeclaration decl = (OperatorDeclaration) $3;
-               
-               Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
-
-               param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
-               if (decl.arg2type != null)
-                       param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
-
                Operator op = new Operator (
                        current_class, decl.optype, decl.ret_type, (int) $2, 
-                       new Parameters (param_list, null, decl.location),
+                       current_local_parameters,
                        (ToplevelBlock) $5, (Attributes) $1, decl.location);
 
                if (RootContext.Documentation != null) {
@@ -1733,14 +2195,13 @@ operator_declaration
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
 
-               if (SimpleIteratorContainer.Simple.Yields)
-                       op.SetYields ();
+               SimpleAnonymousHost.Simple.Propagate (op);
+               anonymous_host = null;
 
                // Note again, checking is done in semantic analysis
                current_container.AddOperator (op);
 
                current_local_parameters = null;
-               iterator_container = null;
          }
        ;
 
@@ -1748,73 +2209,52 @@ operator_body
        : block
        | SEMICOLON { $$ = null; }
        ; 
-operator_declarator
-       : type OPERATOR overloadable_operator 
-         OPEN_PARENS type IDENTIFIER CLOSE_PARENS
-       {
-               Operator.OpType op = (Operator.OpType) $3;
-               CheckUnaryOperator (op);
-
-               if (op == Operator.OpType.Addition)
-                       op = Operator.OpType.UnaryPlus;
-
-               if (op == Operator.OpType.Subtraction)
-                       op = Operator.OpType.UnaryNegation;
 
-               Parameter [] pars = new Parameter [1];
-               Expression type = (Expression) $5;
-
-               pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
-
-               current_local_parameters = new Parameters (pars, null, lexer.Location);
+operator_declarator
+       : type OPERATOR overloadable_operator open_parens
+         {
+               params_modifiers_not_allowed = true;
+         }
+         opt_parameter_list_no_mod CLOSE_PARENS
+         {
+               params_modifiers_not_allowed = false;
 
-               if (RootContext.Documentation != null) {
-                       tmpComment = Lexer.consume_doc_comment ();
-                       Lexer.doc_state = XmlCommentState.NotAllowed;
+               Location loc = (Location) $2;
+               Operator.OpType op = (Operator.OpType) $3;
+               current_local_parameters = (Parameters)$6;
+               
+               int p_count = current_local_parameters.Count;
+               if (p_count == 1) {
+                       if (op == Operator.OpType.Addition)
+                               op = Operator.OpType.UnaryPlus;
+                       else if (op == Operator.OpType.Subtraction)
+                               op = Operator.OpType.UnaryNegation;
                }
-
-               $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
-                                             null, null, lexer.Location);
-       }
-       | type OPERATOR overloadable_operator
-         OPEN_PARENS 
-               type IDENTIFIER COMMA
-               type IDENTIFIER 
-         CLOSE_PARENS
-        {
-               CheckBinaryOperator ((Operator.OpType) $3);
-
-               Parameter [] pars = new Parameter [2];
-
-               Expression typeL = (Expression) $5;
-               Expression typeR = (Expression) $8;
-
-              pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
-              pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
-
-              current_local_parameters = new Parameters (pars, null, lexer.Location);
-
+               
+               if (IsUnaryOperator (op)) {
+                       if (p_count == 2) {
+                               Report.Error (1020, loc, "Overloadable binary operator expected");
+                       } else if (p_count != 1) {
+                               Report.Error (1535, loc, "Overloaded unary operator `{0}' takes one parameter",
+                                       Operator.GetName (op));
+                       }
+               } else {
+                       if (p_count > 2) {
+                               Report.Error (1534, loc, "Overloaded binary operator `{0}' takes two parameters",
+                                       Operator.GetName (op));
+                       } else if (p_count != 2) {
+                               Report.Error (1019, loc, "Overloadable unary operator expected");
+                       }
+               }
+               
                if (RootContext.Documentation != null) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.NotAllowed;
                }
-              
-              $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1, 
-                                            typeL, (string) $6,
-                                            typeR, (string) $9, lexer.Location);
-        }
-       | conversion_operator_declarator
-       | type OPERATOR overloadable_operator
-         OPEN_PARENS 
-               type IDENTIFIER COMMA
-               type IDENTIFIER COMMA
-               type IDENTIFIER
-         CLOSE_PARENS
-       {
-               Report.Error (1534, lexer.Location, "Overloaded binary operator '{0}' takes two parameters", $3);
-               $$ = null;
-       }
 
+               $$ = new OperatorDeclaration (op, (Expression) $1, loc);
+         }
+       | conversion_operator_declarator
        ;
 
 overloadable_operator
@@ -1846,45 +2286,49 @@ overloadable_operator
        ;
 
 conversion_operator_declarator
-       : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
+       : IMPLICIT OPERATOR type open_parens
          {
-               Parameter [] pars = new Parameter [1];
-
-               pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
+               params_modifiers_not_allowed = true;
+         }
+         opt_parameter_list_no_mod CLOSE_PARENS
+         {
+               params_modifiers_not_allowed = false;
 
-               current_local_parameters = new Parameters (pars, null, lexer.Location);  
+               Location loc = (Location) $2;
+               current_local_parameters = (Parameters)$6;  
                  
                if (RootContext.Documentation != null) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.NotAllowed;
                }
 
-               $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
-                                             null, null, lexer.Location);
+               $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, loc);
          }
-       | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
+       | EXPLICIT OPERATOR type open_parens
          {
-               Parameter [] pars = new Parameter [1];
-
-               pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
-
-               current_local_parameters = new Parameters (pars, null, lexer.Location);  
+               params_modifiers_not_allowed = true;
+         }
+         opt_parameter_list_no_mod CLOSE_PARENS
+         {
+               params_modifiers_not_allowed = false;
+               
+               Location loc = (Location) $2;
+               current_local_parameters = (Parameters)$6;  
                  
                if (RootContext.Documentation != null) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.NotAllowed;
                }
 
-               $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
-                                             null, null, lexer.Location);
+               $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, loc);
          }
        | IMPLICIT error 
          {
-               syntax_error (lexer.Location, "'operator' expected");
+               syntax_error ((Location) $1, "'operator' expected");
          }
        | EXPLICIT error 
          {
-               syntax_error (lexer.Location, "'operator' expected");
+               syntax_error ((Location) $1, "'operator' expected");
          }
        ;
 
@@ -1897,6 +2341,7 @@ constructor_declaration
                Constructor c = (Constructor) $3;
                c.Block = (ToplevelBlock) $4;
                c.OptAttributes = (Attributes) $1;
+               int yield_method = c.ModFlags & Modifiers.METHOD_YIELDS;
                c.ModFlags = (int) $2;
        
                if (RootContext.Documentation != null)
@@ -1905,24 +2350,17 @@ constructor_declaration
                if (c.Name == current_container.Basename){
                        if ((c.ModFlags & Modifiers.STATIC) != 0){
                                if ((c.ModFlags & Modifiers.Accessibility) != 0){
-                                       Report.Error (
-                                               515, c.Location, String.Format (
-                                               "`{0}.{1}': static constructor can not have access modifiers",
-                                               c.Name, current_container.Name));
+                                       Report.Error (515, c.Location,
+                                               "`{0}': access modifiers are not allowed on static constructors",
+                                               c.GetSignatureForError ());
                                }
        
                                c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);   
        
                                if (c.Initializer != null){
-                                       Report.Error (
-                                               514, c.Location, 
-                                               "Static constructors can not have an explicit this or base " +
-                                               "constructor invocations");
-                               }
-       
-                               if (!c.Parameters.Empty){
-                                       Report.Error (
-                                               132, c.Location, "Static constructors should not have parameters");
+                                       Report.Error (514, c.Location,
+                                               "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
+                                               c.GetSignatureForError ());
                                }
                        } else {
                                c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
@@ -1932,6 +2370,7 @@ constructor_declaration
                        //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
                }
 
+               c.ModFlags |= yield_method;
                current_container.AddConstructor (c);
 
                current_local_parameters = null;
@@ -1941,6 +2380,18 @@ constructor_declaration
        ;
 
 constructor_declarator
+       : constructor_header
+         {
+               $$ = $1;
+         }
+       | constructor_header constructor_initializer
+         {
+               ((Constructor)$1).Initializer = (ConstructorInitializer) $2;
+               $$ = $1;
+         }
+       ;
+
+constructor_header
        : IDENTIFIER
          {
                if (RootContext.Documentation != null) {
@@ -1948,38 +2399,35 @@ constructor_declarator
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
          }
-         OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
+         open_parens opt_formal_parameter_list CLOSE_PARENS
          {
+               LocatedToken lt = (LocatedToken) $1;
                current_local_parameters = (Parameters) $4;
-         }
-         opt_constructor_initializer
-         {
-               $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
-                                     (ConstructorInitializer) $8, (Location) $6);
+               current_block = new ToplevelBlock (null, current_local_parameters, null, lt.Location);
+
+               $$ = new Constructor (current_class, lt.Value, 0, current_local_parameters,
+                                     null, lt.Location);
+
+               anonymous_host = (IAnonymousHost) $$;
          }
        ;
 
 constructor_body
-       : block
-       | SEMICOLON             { $$ = null; }
-       ;
-
-opt_constructor_initializer
-       : /* empty */                   { $$ = null; }
-       | constructor_initializer
+       : block_prepared
+       | SEMICOLON             { current_block = null; $$ = null; }
        ;
 
 constructor_initializer
-       : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
+       : COLON BASE open_parens opt_argument_list CLOSE_PARENS
          {
-               $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
+               $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
          }
-       | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
+       | COLON THIS open_parens opt_argument_list CLOSE_PARENS
          {
-               $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
+               $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
          }
        | COLON error {
-               Report.Error (1018, lexer.Location, "Keyword this or base expected");
+               Report.Error (1018, (Location) $1, "Keyword this or base expected");
                $$ = null;
          }
        ;
@@ -2000,12 +2448,13 @@ destructor_declaration
          }
          IDENTIFIER OPEN_PARENS CLOSE_PARENS block
          {
-               if ((string) $5 != current_container.Basename){
-                       Report.Error (574, lexer.Location, "Name of destructor must match name of class");
+               LocatedToken lt = (LocatedToken) $5;
+               if (lt.Value != current_container.MemberName.Name){
+                       Report.Error (574, lt.Location, "Name of destructor must match name of class");
                } else if (current_container.Kind != Kind.Class){
-                       Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
+                       Report.Error (575, lt.Location, "Only class types can contain destructor");
                } else {
-                       Location l = lexer.Location;
+                       Location l = lt.Location;
 
                        int m = (int) $2;
                        if (!RootContext.StdLib && current_container.Name == "System.Object")
@@ -2013,17 +2462,9 @@ destructor_declaration
                        else
                                m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
                         
-                        if ((m & Modifiers.UNSAFE) != 0){
-                                if (!RootContext.Unsafe){
-                                        Report.Error (227, l,
-                                             "Unsafe code requires the -unsafe command " +
-                                             "line option to be specified");
-                                }
-                        }
-                        
                        Method d = new Destructor (
                                current_class, TypeManager.system_void_expr, m, "Finalize", 
-                               new Parameters (null, null, l), (Attributes) $1, l);
+                               Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
                        if (RootContext.Documentation != null)
                                d.DocComment = ConsumeStoredComment ();
                  
@@ -2038,14 +2479,17 @@ event_declaration
          opt_modifiers
          EVENT type variable_declarators SEMICOLON
          {
+               current_array_type = null;
                foreach (VariableDeclaration var in (ArrayList) $5) {
 
-                       MemberName name = new MemberName (var.identifier);
+                       MemberName name = new MemberName (var.identifier,
+                               var.Location);
 
-                       Event e = new EventField (
+                       EventField e = new EventField (
                                current_class, (Expression) $4, (int) $2, false, name,
-                               var.expression_or_array_initializer, (Attributes) $1,
-                               lexer.Location);
+                               (Attributes) $1);
+
+                       e.Initializer = var.expression_or_array_initializer;
 
                        current_container.AddEvent (e);
 
@@ -2058,7 +2502,7 @@ event_declaration
        | opt_attributes
          opt_modifiers
          EVENT type namespace_or_type_name
-         OPEN_BRACE _mark_
+         OPEN_BRACE
          {
                implicit_value_parameter_type = (Expression) $4;  
                lexer.EventParsing = true;
@@ -2069,36 +2513,42 @@ event_declaration
          }
          CLOSE_BRACE
          {
-               Location loc = (Location) $7;
+               MemberName name = (MemberName) $5;
 
-               if ($9 == null){
-                       Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
+               if ($8 == null){
+                       Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
+                               current_container.Name, name.GetSignatureForError ());
                        $$ = null;
                } else {
-                       Pair pair = (Pair) $9;
+                       Accessors accessors = (Accessors) $8;
                        
-                       MemberName name = (MemberName) $5;
+                       if (name.TypeArguments != null)
+                               syntax_error (lexer.Location, "an event can't have type arguments");
 
-                       Event e = new EventProperty (
-                               current_class, (Expression) $4, (int) $2, false, name, null,
-                               (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
-                               loc);
-                       if (RootContext.Documentation != null) {
-                               e.DocComment = Lexer.consume_doc_comment ();
-                               Lexer.doc_state = XmlCommentState.Allowed;
-                       }
+                       if (accessors.get_or_add == null || accessors.set_or_remove == null)
+                               // CS0073 is already reported, so no CS0065 here.
+                               $$ = null;
+                       else {
+                               Event e = new EventProperty (
+                                       current_class, (Expression) $4, (int) $2, false, name,
+                                       (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
+                               if (RootContext.Documentation != null) {
+                                       e.DocComment = Lexer.consume_doc_comment ();
+                                       Lexer.doc_state = XmlCommentState.Allowed;
+                               }
 
-                       current_container.AddEvent (e);
-                       implicit_value_parameter_type = null;
+                               current_container.AddEvent (e);
+                               implicit_value_parameter_type = null;
+                       }
                }
          }
-       | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
+       | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
                MemberName mn = (MemberName) $5;
 
                if (mn.Left != null)
-                       Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
+                       Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
                else 
-                       Report.Error (71, lexer.Location, "Event declaration should use property syntax");
+                       Report.Error (71, mn.Location, "Event declaration should use property syntax");
 
                if (RootContext.Documentation != null)
                        Lexer.doc_state = XmlCommentState.Allowed;
@@ -2107,20 +2557,22 @@ event_declaration
 
 event_accessor_declarations
        : add_accessor_declaration remove_accessor_declaration
-       {
-               $$ = new Pair ($1, $2);
-       }
+         {
+               $$ = new Accessors ((Accessor) $1, (Accessor) $2);
+         }
        | remove_accessor_declaration add_accessor_declaration
-       {
-               $$ = new Pair ($2, $1);
-       }       
+         {
+               Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
+               accessors.declared_in_reverse = true;
+               $$ = accessors;
+         }     
        | add_accessor_declaration  { $$ = null; } 
        | remove_accessor_declaration { $$ = null; } 
        | error
-       { 
-               Report.Error (1055, lexer.Location, "An add or remove accessor expected");
+         
+               Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
                $$ = null;
-       }
+         }
        | { $$ = null; }
        ;
 
@@ -2130,24 +2582,32 @@ add_accessor_declaration
                Parameter [] args = new Parameter [1];
                Parameter implicit_value_parameter = new Parameter (
                        implicit_value_parameter_type, "value", 
-                       Parameter.Modifier.NONE, null);
+                       Parameter.Modifier.NONE, null, (Location) $2);
 
                args [0] = implicit_value_parameter;
                
-               current_local_parameters = new Parameters (args, null, lexer.Location);  
+               current_local_parameters = new Parameters (args);  
                lexer.EventParsing = false;
+               
+               anonymous_host = SimpleAnonymousHost.GetSimple ();
          }
-          block
+         block
          {
-               $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
+               Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
                lexer.EventParsing = true;
+               
+               current_local_parameters = null;
+               SimpleAnonymousHost.Simple.Propagate (accessor);
+               anonymous_host = null;
+               
+               $$ = accessor;
          }
        | opt_attributes ADD error {
-               Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
+               Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
                $$ = null;
          }
        | opt_attributes modifiers ADD {
-               Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
+               Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
                $$ = null;
          }
        ;
@@ -2158,31 +2618,31 @@ remove_accessor_declaration
                Parameter [] args = new Parameter [1];
                Parameter implicit_value_parameter = new Parameter (
                        implicit_value_parameter_type, "value", 
-                       Parameter.Modifier.NONE, null);
+                       Parameter.Modifier.NONE, null, (Location) $2);
 
                args [0] = implicit_value_parameter;
                
-               current_local_parameters = new Parameters (args, null, lexer.Location);  
+               current_local_parameters = new Parameters (args);  
                lexer.EventParsing = false;
          }
           block
          {
-               $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
+               $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
                lexer.EventParsing = true;
          }
        | opt_attributes REMOVE error {
-               Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
+               Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
                $$ = null;
          }
        | opt_attributes modifiers REMOVE {
-               Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
+               Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
                $$ = null;
          }
        ;
 
 indexer_declaration
        : opt_attributes opt_modifiers indexer_declarator 
-         OPEN_BRACE _mark_
+         OPEN_BRACE
          {
                IndexerDeclaration decl = (IndexerDeclaration) $3;
 
@@ -2192,6 +2652,7 @@ indexer_declaration
                parsing_indexer  = true;
                
                indexer_parameters = decl.param_list;
+               anonymous_host = SimpleAnonymousHost.GetSimple ();
          }
           accessor_declarations 
          {
@@ -2201,28 +2662,28 @@ indexer_declaration
          }
          CLOSE_BRACE
          { 
-               if ($7 == null)
+               if ($6 == null)
                        break;
 
                // The signature is computed from the signature of the indexer.  Look
                // at section 3.6 on the spec
-               Location loc = (Location) $5;
                Indexer indexer;
                IndexerDeclaration decl = (IndexerDeclaration) $3;
-               Pair pair = (Pair) $7;
-               Accessor get_block = (Accessor) pair.First;
-               Accessor set_block = (Accessor) pair.Second;
+               Location loc = decl.location;
+               Accessors accessors = (Accessors) $6;
+               Accessor get_block = accessors.get_or_add;
+               Accessor set_block = accessors.set_or_remove;
 
                MemberName name;
                if (decl.interface_type != null)
-                       name = new MemberName (decl.interface_type,
-                                              TypeContainer.DefaultIndexerName);
+                       name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
                else
-                       name = new MemberName (TypeContainer.DefaultIndexerName);
+                       name = new MemberName (TypeContainer.DefaultIndexerName, loc);
 
                indexer = new Indexer (current_class, decl.type, name,
                                       (int) $2, false, decl.param_list, (Attributes) $1,
-                                      get_block, set_block, loc);
+                                      get_block, set_block, accessors.declared_in_reverse);
+
                if (RootContext.Documentation != null)
                        indexer.DocComment = ConsumeStoredComment ();
 
@@ -2235,35 +2696,28 @@ indexer_declaration
        ;
 
 indexer_declarator
-       : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
+       : type THIS OPEN_BRACKET opt_parameter_list_no_mod CLOSE_BRACKET
          {
                Parameters pars = (Parameters) $4;
-               if (pars.HasArglist) {
-                       // "__arglist is not valid in this context"
-                       Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
-               } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
-                       Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
+               if (pars.Empty){
+                       Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
                }
                if (RootContext.Documentation != null) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
 
-               $$ = new IndexerDeclaration ((Expression) $1, null, pars);
+               $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
          }
        | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
          {
                Parameters pars = (Parameters) $6;
-
-               if (pars.HasArglist) {
-                       // "__arglist is not valid in this context"
-                       Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
-               } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
-                       Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
+               if (pars.Empty){
+                       Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
                }
 
                MemberName name = (MemberName) $2;
-               $$ = new IndexerDeclaration ((Expression) $1, name, pars);
+               $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
 
                if (RootContext.Documentation != null) {
                        tmpComment = Lexer.consume_doc_comment ();
@@ -2275,7 +2729,6 @@ indexer_declarator
 enum_declaration
        : opt_attributes
          opt_modifiers
-         opt_partial
          ENUM IDENTIFIER 
          opt_enum_base {
                if (RootContext.Documentation != null)
@@ -2284,31 +2737,30 @@ enum_declaration
          enum_body
          opt_semicolon
          {
-               bool partial = (bool) $3;
-
-               if (partial) {
-                       Report.Error (267, lexer.Location, "The partial modifier can only appear before a 'class', 'struct', or 'interface'");
-                       break;  // assumes that the parser put us in a switch
-               }
+               LocatedToken lt = (LocatedToken) $4;
+               Location enum_location = lt.Location;
 
-               Location enum_location = lexer.Location;
-
-               MemberName name = MakeName (new MemberName ((string) $5));
-               Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
-                                  name, (Attributes) $1, enum_location);
+               MemberName name = MakeName (new MemberName (lt.Value, enum_location));
+               Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
+                                  name, (Attributes) $1);
                
                if (RootContext.Documentation != null)
                        e.DocComment = enumTypeComment;
 
-               foreach (VariableDeclaration ev in (ArrayList) $8) {
-                       e.AddEnumMember (ev.identifier, 
-                                        (Expression) ev.expression_or_array_initializer,
-                                        ev.Location, ev.OptAttributes,
-                                        ev.DocComment);
+
+               EnumMember em = null;
+               foreach (VariableDeclaration ev in (ArrayList) $7) {
+                       em = new EnumMember (
+                               e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
+                               ev.OptAttributes, ev.Location);
+
+//                     if (RootContext.Documentation != null)
+                               em.DocComment = ev.DocComment;
+
+                       e.AddEnumMember (em);
                }
 
-               current_container.AddEnum (e);
-               RootContext.Tree.RecordDecl (name, e);
+               current_container.AddTypeContainer (e);
                $$ = e;
 
          }
@@ -2363,7 +2815,8 @@ enum_member_declarations
 enum_member_declaration
        : opt_attributes IDENTIFIER 
          {
-               VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
+               VariableDeclaration vd = new VariableDeclaration (
+                       (LocatedToken) $2, null, (Attributes) $1);
 
                if (RootContext.Documentation != null) {
                        vd.DocComment = Lexer.consume_doc_comment ();
@@ -2374,7 +2827,6 @@ enum_member_declaration
          }
        | opt_attributes IDENTIFIER
          {
-               $$ = lexer.Location;
                if (RootContext.Documentation != null) {
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.NotAllowed;
@@ -2382,7 +2834,8 @@ enum_member_declaration
          }
           ASSIGN expression
          { 
-               VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
+               VariableDeclaration vd = new VariableDeclaration (
+                       (LocatedToken) $2, $5, (Attributes) $1);
 
                if (RootContext.Documentation != null)
                        vd.DocComment = ConsumeStoredComment ();
@@ -2394,14 +2847,15 @@ enum_member_declaration
 delegate_declaration
        : opt_attributes
          opt_modifiers
-         DELEGATE type member_name
-         OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
-         SEMICOLON
+         DELEGATE
+         type type_name
+         open_parens opt_formal_parameter_list CLOSE_PARENS
          {
-               Location l = lexer.Location;
                MemberName name = MakeName ((MemberName) $5);
+               Parameters p = (Parameters) $7;
+
                Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
-                                            (int) $2, name, (Parameters) $7, (Attributes) $1, l);
+                                            (int) $2, name, p, (Attributes) $1);
 
                if (RootContext.Documentation != null) {
                        del.DocComment = Lexer.consume_doc_comment ();
@@ -2409,40 +2863,166 @@ delegate_declaration
                }
 
                current_container.AddDelegate (del);
-               RootContext.Tree.RecordDecl (name, del);
-               $$ = del;
-         }     
+               current_delegate = del;
+               lexer.ConstraintsParsing = true;
+         }
+         opt_type_parameter_constraints_clauses
+         {
+               lexer.ConstraintsParsing = false;
+         }
+         SEMICOLON
+         {
+               current_delegate.SetParameterInfo ((ArrayList) $10);
+               $$ = current_delegate;
+
+               current_delegate = null;
+         }
+       ;
+
+opt_nullable
+       : /* empty */
+         {
+               lexer.CheckNullable (false);
+               $$ = false;
+         }
+       | INTERR
+         {
+               // FIXME: A hack with parsing conditional operator as nullable type
+               //if (RootContext.Version < LanguageVersion.ISO_2)
+               //      Report.FeatureIsNotAvailable (lexer.Location, "nullable types");
+                       
+               lexer.CheckNullable (true);
+               $$ = true;
+         }
        ;
 
 namespace_or_type_name
-       : member_name
-       | namespace_or_type_name DOT IDENTIFIER {
-               $$ = new MemberName ((MemberName) $1, (string) $3);
+       : IDENTIFIER opt_type_argument_list
+         {
+               LocatedToken lt = (LocatedToken) $1;
+               $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
+         }
+       | IDENTIFIER DOUBLE_COLON IDENTIFIER opt_type_argument_list {
+               LocatedToken lt1 = (LocatedToken) $1;
+               LocatedToken lt2 = (LocatedToken) $3;
+               $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $4, lt2.Location);
+         }
+       | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
+               LocatedToken lt = (LocatedToken) $3;
+               $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
          }
        ;
 
 member_name
-       : IDENTIFIER {
-               $$ = new MemberName ((string) $1);
+       : IDENTIFIER opt_type_parameter_list
+         {
+               LocatedToken lt = (LocatedToken) $1;
+               $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
+         }
+       | namespace_or_type_name DOT IDENTIFIER opt_type_parameter_list 
+         {
+               LocatedToken lt = (LocatedToken) $3;
+               $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
          }
        ;
 
-/* 
- * Before you think of adding a return_type, notice that we have been
- * using two rules in the places where it matters (one rule using type
- * and another identical one that uses VOID as the return type).  This
- * gets rid of a shift/reduce couple
- */
-type
-       : namespace_or_type_name
+type_name
+       : IDENTIFIER opt_type_parameter_list
          {
-               $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
+               LocatedToken lt = (LocatedToken) $1;
+               $$ = new MemberName (lt.Value, (TypeArguments)$2, lt.Location);   
          }
-       | builtin_types
-       | array_type
-       | pointer_type    
        ;
 
+//
+// Generics arguments  (any type, without attributes)
+//
+
+opt_type_argument_list
+       : /* empty */                { $$ = null; } 
+       | OP_GENERICS_LT type_arguments OP_GENERICS_GT
+         {
+               if (RootContext.Version < LanguageVersion.ISO_2)
+                       Report.FeatureIsNotAvailable (GetLocation ($1), "generics");      
+         
+               $$ = $2;
+         }
+       ;
+
+//
+// Generics parameters (identifiers only, with attributes), used in type, method declarations
+//
+
+opt_type_parameter_list
+       : /* empty */                { $$ = null; } 
+       | OP_GENERICS_LT type_arguments OP_GENERICS_GT
+         {
+               if (RootContext.Version < LanguageVersion.ISO_2)
+                       Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
+         
+               $$ = $2;
+         }
+       ;
+
+type_arguments
+       : type_argument
+         {
+               TypeArguments type_args = new TypeArguments (lexer.Location);
+               type_args.Add ((Expression) $1);
+               $$ = type_args;
+         }
+       | type_arguments COMMA type_argument
+         {
+               TypeArguments type_args = (TypeArguments) $1;
+               type_args.Add ((Expression) $3);
+               $$ = type_args;
+         }       
+       ;
+
+type_argument
+       : type
+         {
+               $$ = $1;
+         }
+       | attribute_sections type
+         {
+               SimpleName sn = $2 as SimpleName;
+               if (sn == null)
+                       Error_TypeExpected (GetLocation ($2));
+               else
+                       $2 = new TypeParameterName (sn.Name, (Attributes) $1, lexer.Location);
+               $$ = $2;          
+         }
+       ;
+       
+/* 
+ * Before you think of adding a return_type, notice that we have been
+ * using two rules in the places where it matters (one rule using type
+ * and another identical one that uses VOID as the return type).  This
+ * gets rid of a shift/reduce couple
+ */
+type
+       : namespace_or_type_name opt_nullable
+         {
+               MemberName name = (MemberName) $1;
+
+               if ((bool) $2) {
+                       $$ = new ComposedCast (name.GetTypeExpression (), "?", lexer.Location);
+               } else {
+                       if (RootContext.Version > LanguageVersion.ISO_2 && name.Name == "var")
+                               $$ = new VarExpr (name.Location);
+                       else
+                               $$ = name.GetTypeExpression ();
+               }
+         }
+       | builtin_types opt_nullable
+         {
+               if ((bool) $2)
+                       $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
+         }
+       | array_type
+       | pointer_type
+       ;
 
 pointer_type
        : type STAR
@@ -2452,31 +3032,41 @@ pointer_type
                // can't perform checks during this phase - we do it during
                // semantic analysis.
                //
-               $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
+               $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
          }
        | VOID STAR
          {
-               $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
+               $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
          }
        ;
 
 non_expression_type
-       : builtin_types 
+       : builtin_types opt_nullable
+         {
+               if ((bool) $2)
+                       $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
+         }
        | non_expression_type rank_specifier
          {
-               $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
+               Location loc = GetLocation ($1);
+               if (loc.IsNull)
+                       loc = lexer.Location;
+               $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
          }
        | non_expression_type STAR
          {
-               $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
+               Location loc = GetLocation ($1);
+               if (loc.IsNull)
+                       loc = lexer.Location;
+               $$ = new ComposedCast ((Expression) $1, "*", loc);
          }
        | expression rank_specifiers 
          {
-               $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
+               $$ = new ComposedCast ((Expression) $1, (string) $2);
          }
        | expression STAR 
          {
-               $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
+               $$ = new ComposedCast ((Expression) $1, "*");
          }
        
        //
@@ -2485,7 +3075,7 @@ non_expression_type
        //
        | multiplicative_expression STAR 
          {
-               $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
+               $$ = new ComposedCast ((Expression) $1, "*");
          }
        ;
 
@@ -2534,9 +3124,13 @@ integral_type
        ;
 
 array_type
-       : type rank_specifiers
+       : type rank_specifiers opt_nullable
          {
-               $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
+               string rank_specifiers = (string) $2;
+               if ((bool) $3)
+                       rank_specifiers += "?";
+
+               $$ = current_array_type = new ComposedCast ((Expression) $1, rank_specifiers);
          }
        ;
 
@@ -2548,12 +3142,22 @@ primary_expression
          {
                // 7.5.1: Literals
          }
-       | member_name
+       | type_name
          {
-               $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
+               MemberName mn = (MemberName) $1;
+               $$ = mn.GetTypeExpression ();
+         }
+       | IDENTIFIER DOUBLE_COLON IDENTIFIER
+         {
+               LocatedToken lt1 = (LocatedToken) $1;
+               LocatedToken lt2 = (LocatedToken) $3;
+               if (RootContext.Version == LanguageVersion.ISO_1)
+                       Report.FeatureIsNotAvailable (lt1.Location, "namespace alias qualifier");
+
+               $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
          }
        | parenthesized_expression
+       | default_value_expression
        | member_access
        | invocation_expression
        | element_access
@@ -2574,15 +3178,15 @@ literal
        : boolean_literal
        | integer_literal
        | real_literal
-       | LITERAL_CHARACTER     { $$ = new CharLiteral ((char) lexer.Value); }
-       | LITERAL_STRING        { $$ = new StringLiteral ((string) lexer.Value); }
-       | NULL                  { $$ = NullLiteral.Null; }
+       | LITERAL_CHARACTER     { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
+       | LITERAL_STRING        { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); } 
+       | NULL                  { $$ = new NullLiteral (lexer.Location); }
        ;
 
 real_literal
-       : LITERAL_FLOAT         { $$ = new FloatLiteral ((float) lexer.Value); }
-       | LITERAL_DOUBLE        { $$ = new DoubleLiteral ((double) lexer.Value); }
-       | LITERAL_DECIMAL       { $$ = new DecimalLiteral ((decimal) lexer.Value); }
+       : LITERAL_FLOAT         { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
+       | LITERAL_DOUBLE        { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
+       | LITERAL_DECIMAL       { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
        ;
 
 integer_literal
@@ -2590,63 +3194,64 @@ integer_literal
                object v = lexer.Value;
 
                if (v is int){
-                       int i = (int) v;
-
-                       if (i == 0)
-                               $$ = IntLiteral.Zero;
-                       else if (i == 1)
-                               $$ = IntLiteral.One;
-                       else
-                               $$ = new IntLiteral (i);
+                       $$ = new IntLiteral ((int) v, lexer.Location);
                } else if (v is uint)
-                       $$ = new UIntLiteral ((UInt32) v);
+                       $$ = new UIntLiteral ((UInt32) v, lexer.Location);
                else if (v is long)
-                       $$ = new LongLiteral ((Int64) v);
+                       $$ = new LongLiteral ((Int64) v, lexer.Location);
                else if (v is ulong)
-                       $$ = new ULongLiteral ((UInt64) v);
+                       $$ = new ULongLiteral ((UInt64) v, lexer.Location);
                else
                        Console.WriteLine ("OOPS.  Unexpected result from scanner");
          }
        ;
 
 boolean_literal
-       : TRUE                  { $$ = new BoolLiteral (true); }
-       | FALSE                 { $$ = new BoolLiteral (false); }
+       : TRUE                  { $$ = new BoolLiteral (true, lexer.Location); }
+       | FALSE                 { $$ = new BoolLiteral (false, lexer.Location); }
        ;
 
 parenthesized_expression_0
        : OPEN_PARENS expression CLOSE_PARENS
          {
                $$ = $2;
-               lexer.Deambiguate_CloseParens ();
+               lexer.Deambiguate_CloseParens ($$);
                // After this, the next token returned is one of
-               // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
+               // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
                // or CLOSE_PARENS_MINUS.
          }
+       | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
        ;
 
 parenthesized_expression
        : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
          {
                $$ = $1;
-         }
+         }  
+       | parenthesized_expression_0 CLOSE_PARENS
+         {
+               $$ = $1;
+         }       
        | parenthesized_expression_0 CLOSE_PARENS_MINUS
          {
                // If a parenthesized expression is followed by a minus, we need to wrap
                // the expression inside a ParenthesizedExpression for the CS0075 check
                // in Binary.DoResolve().
-               $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
+               $$ = new ParenthesizedExpression ((Expression) $1);
          }
-       ;;
+       ;
 
 member_access
-       : primary_expression DOT IDENTIFIER
+       : primary_expression DOT IDENTIFIER opt_type_argument_list
          {
-               $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
+               LocatedToken lt = (LocatedToken) $3;
+               $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
          }
-       | predefined_type DOT IDENTIFIER
+       | predefined_type DOT IDENTIFIER opt_type_argument_list
          {
-               $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
+               LocatedToken lt = (LocatedToken) $3;
+               // TODO: Location is wrong as some predefined types doesn't hold a location
+               $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
          }
        ;
 
@@ -2657,34 +3262,100 @@ predefined_type
 invocation_expression
        : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
          {
-               if ($1 == null) {
-                       Location l = lexer.Location;
-                       Report.Error (1, l, "Parse error");
-               }
-               $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
+               if ($1 == null)
+                       Report.Error (1, (Location) $2, "Parse error");
+               else
+                       $$ = new Invocation ((Expression) $1, (ArrayList) $3);
          }
        | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
          {
-               $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
+               $$ = new Invocation ((Expression) $1, new ArrayList ());
          }
        | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
          {
-               $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
+               $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
          }
        | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
          {
                ArrayList args = new ArrayList (1);
                args.Add ($4);
-               $$ = new Invocation ((Expression) $1, args, lexer.Location);
+               $$ = new Invocation ((Expression) $1, args);
          }
        | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
          {
                ArrayList args = ((ArrayList) $4);
                args.Add ($6);
-               $$ = new Invocation ((Expression) $1, args, lexer.Location);
+               $$ = new Invocation ((Expression) $1, args);
          }
        ;
 
+opt_object_or_collection_initializer
+       : /* empty */           { $$ = null; }
+       | object_or_collection_initializer
+       ;
+
+object_or_collection_initializer
+       : OPEN_BRACE opt_member_initializer_list CLOSE_BRACE
+         {
+               if ($2 == null)
+                 $$ = CollectionOrObjectInitializers.Empty;
+               else
+                 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
+         }
+       | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
+         {
+               $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
+         }
+       ;
+
+opt_member_initializer_list
+       : /* empty */           { $$ = null; }
+       | member_initializer_list
+       {
+               $$ = $1;
+       }
+       ;
+
+member_initializer_list
+       : member_initializer
+         {
+               ArrayList a = new ArrayList ();
+               a.Add ($1);
+               $$ = a;
+         }
+       | member_initializer_list COMMA member_initializer
+         {
+               ArrayList a = (ArrayList)$1;
+               a.Add ($3);
+               $$ = a;
+         }
+       ;
+
+member_initializer
+       : IDENTIFIER ASSIGN initializer_value
+         {
+               LocatedToken lt = $1 as LocatedToken;
+               $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
+         }
+       | non_assignment_expression
+         {
+               $$ = new CollectionElementInitializer ((Expression)$1);
+         }
+       | OPEN_BRACE expression_list CLOSE_BRACE
+         {
+               $$ = new CollectionElementInitializer ((ArrayList)$2, GetLocation ($1));
+         }
+       | OPEN_BRACE CLOSE_BRACE
+         {
+               Report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
+         }       
+       ;
+
+initializer_value
+       : expression
+       | object_or_collection_initializer
+       ;
+
 opt_argument_list
        : /* empty */           { $$ = null; }
        | argument_list
@@ -2704,7 +3375,8 @@ argument_list
                $$ = list;
          }
        | argument_list error {
-               CheckToken (1026, yyToken, ", or ) expected");
+               CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
+               $$ = null;
          }
        ;
 
@@ -2734,12 +3406,16 @@ non_simple_argument
                Argument[] args = new Argument [list.Count];
                list.CopyTo (args, 0);
 
-               Expression expr = new Arglist (args, lexer.Location);
+               Expression expr = new Arglist (args, (Location) $1);
                $$ = new Argument (expr, Argument.AType.Expression);
          }
+       | ARGLIST OPEN_PARENS CLOSE_PARENS
+         {
+               $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
+         }       
        | ARGLIST
          {
-               $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
+               $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
          }
        ;
 
@@ -2750,7 +3426,7 @@ variable_reference
 element_access
        : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET 
          {
-               $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
+               $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
          }
        | primary_expression rank_specifiers
          {
@@ -2763,17 +3439,18 @@ element_access
                  
                Expression expr = (Expression) $1;  
                if (expr is ComposedCast){
-                       $$ = new ComposedCast (expr, (string) $2, lexer.Location);
-               } else if (!(expr is SimpleName || expr is MemberAccess)){
-                       Error_ExpectingTypeName (lexer.Location, expr);
+                       $$ = new ComposedCast (expr, (string) $2);
+               } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType || expr is QualifiedAliasMember)){
+                       Error_ExpectingTypeName (expr);
                        $$ = TypeManager.system_object_expr;
                } else {
                        //
                        // So we extract the string corresponding to the SimpleName
                        // or MemberAccess
                        // 
-                       $$ = new ComposedCast (expr, (string) $2, lexer.Location);
+                       $$ = new ComposedCast (expr, (string) $2);
                }
+               current_array_type = (Expression)$$;
          }
        ;
 
@@ -2795,21 +3472,22 @@ expression_list
 this_access
        : THIS
          {
-               $$ = new This (current_block, lexer.Location);
+               $$ = new This (current_block, (Location) $1);
          }
        ;
 
 base_access
-       : BASE DOT IDENTIFIER
+       : BASE DOT IDENTIFIER opt_type_argument_list
          {
-               $$ = new BaseAccess ((string) $3, lexer.Location);
+               LocatedToken lt = (LocatedToken) $3;
+               $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
          }
        | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
          {
-               $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
+               $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
          }
        | BASE error {
-               Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
+               Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
                $$ = null;
          }
        ;
@@ -2818,7 +3496,7 @@ post_increment_expression
        : primary_expression OP_INC
          {
                $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
-                                      (Expression) $1, lexer.Location);
+                                      (Expression) $1, (Location) $2);
          }
        ;
 
@@ -2826,19 +3504,34 @@ post_decrement_expression
        : primary_expression OP_DEC
          {
                $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
-                                      (Expression) $1, lexer.Location);
+                                      (Expression) $1, (Location) $2);
          }
        ;
 
 new_expression
        : object_or_delegate_creation_expression
        | array_creation_expression
+       | anonymous_type_expression
        ;
 
 object_or_delegate_creation_expression
-       : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
+       : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
+         {
+               if ($6 != null) {
+                       if (RootContext.Version <= LanguageVersion.ISO_2)
+                               Report.FeatureIsNotAvailable (GetLocation ($1), "object initializers");
+                               
+                       $$ = new NewInitialize ((Expression) $2, (ArrayList) $4, (CollectionOrObjectInitializers) $6, (Location) $1);
+               }
+               else
+                       $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
+         }
+       | NEW type object_or_collection_initializer
          {
-               $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
+               if (RootContext.Version <= LanguageVersion.ISO_2)
+                       Report.FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
+         
+               $$ = new NewInitialize ((Expression) $2, null, (CollectionOrObjectInitializers) $3, (Location) $1);
          }
        ;
 
@@ -2847,20 +3540,75 @@ array_creation_expression
          opt_rank_specifier
          opt_array_initializer
          {
-               $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
+               $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
          }
        | NEW type rank_specifiers array_initializer
          {
-               $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
+               $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
+         }
+       | NEW rank_specifiers array_initializer
+         {
+               $$ = new ImplicitlyTypedArrayCreation ((string) $2, (ArrayList) $3, (Location) $1);
          }
        | NEW error
          {
-               Report.Error (1031, lexer.Location, "Type expected");
+               Report.Error (1031, (Location) $1, "Type expected");
                 $$ = null;
          }          
        | NEW type error 
          {
-               Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
+               Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
+               $$ = null;
+         }
+       ;
+
+anonymous_type_expression
+       : NEW OPEN_BRACE anonymous_type_parameters CLOSE_BRACE
+         {
+               if (RootContext.Version <= LanguageVersion.ISO_2)
+                       Report.FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
+
+               $$ = new AnonymousTypeDeclaration ((ArrayList) $3, current_container, GetLocation ($1));
+         }
+       ;
+
+anonymous_type_parameters
+       : { $$ = null; }
+       | anonymous_type_parameter
+         {
+               ArrayList a = new ArrayList (4);
+               a.Add ($1);
+               $$ = a;
+         }
+       | anonymous_type_parameters COMMA anonymous_type_parameter
+         {
+               ArrayList a = (ArrayList) $1;
+               a.Add ($3);
+               $$ = a;
+         }
+       ;
+
+anonymous_type_parameter
+       : IDENTIFIER ASSIGN variable_initializer
+         {
+               LocatedToken lt = (LocatedToken)$1;
+               $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
+         }
+       | IDENTIFIER
+         {
+               LocatedToken lt = (LocatedToken)$1;
+               $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
+                       lt.Value, lt.Location);
+         }
+       | member_access
+         {
+               MemberAccess ma = (MemberAccess) $1;
+               $$ = new AnonymousTypeParameter (ma, ma.Identifier, ma.Location);
+         }
+       | error
+         {
+               Report.Error (746, lexer.Location, "Invalid anonymous type member declarator. " +
+               "Anonymous type members must be a member assignment, simple name or member access expression");
          }
        ;
 
@@ -2875,6 +3623,31 @@ opt_rank_specifier
          }
        ;
 
+opt_rank_specifier_or_nullable
+       : /* empty */
+         {
+               $$ = "";
+         }
+       | INTERR
+         {
+               $$ = "?";
+         }
+       | opt_nullable rank_specifiers
+         {
+               if ((bool) $1)
+                       $$ = "?" + $2;
+               else
+                       $$ = $2;
+         }
+       | opt_nullable rank_specifiers INTERR
+         {
+               if ((bool) $1)
+                       $$ = "?" + $2 + "?";
+               else
+                       $$ = $2 + "?";
+         }
+       ;
+
 rank_specifiers
        : rank_specifier opt_rank_specifier
          {
@@ -2950,33 +3723,88 @@ variable_initializer_list
        ;
 
 typeof_expression
-       : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
+       : TYPEOF
+      {
+               pushed_current_array_type = current_array_type;
+               lexer.TypeOfParsing = true;
+         }
+         OPEN_PARENS typeof_type_expression CLOSE_PARENS
+         {
+               lexer.TypeOfParsing = false;
+               Expression type = (Expression)$4;
+               if (type == TypeManager.system_void_expr)
+                       $$ = new TypeOfVoid ((Location) $1);
+               else
+                       $$ = new TypeOf (type, (Location) $1);
+               current_array_type = pushed_current_array_type;
+         }
+       ;
+       
+typeof_type_expression
+       : type
+         {
+               $$ = $1;
+         }
+       | unbound_type_name
+         {
+               $$ = new UnboundTypeExpression ((MemberName)$1, lexer.Location);
+         }
+       ;
+       
+unbound_type_name
+       : IDENTIFIER GENERIC_DIMENSION
+         {
+               if (RootContext.Version < LanguageVersion.ISO_1)
+                       Report.FeatureIsNotAvailable (lexer.Location, "generics");
+         
+               LocatedToken lt = (LocatedToken) $1;
+               TypeArguments ta = new TypeArguments ((int)$2, lt.Location);
+
+               $$ = new MemberName (lt.Value, ta, lt.Location);
+         }
+       | IDENTIFIER DOUBLE_COLON IDENTIFIER GENERIC_DIMENSION
+         {
+               LocatedToken lt = (LocatedToken) $1;
+               MemberName left = new MemberName (lt.Value, lt.Location);
+               lt = (LocatedToken) $3;
+               TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
+               
+               $$ = new MemberName (left, lt.Value, ta, lt.Location);
+         }
+       | unbound_type_name DOT IDENTIFIER GENERIC_DIMENSION
          {
-               $$ = new TypeOfVoid (lexer.Location);
+               LocatedToken lt = (LocatedToken) $3;
+               TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
+               
+               $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
          }
-       | TYPEOF OPEN_PARENS type CLOSE_PARENS
+       | namespace_or_type_name DOT IDENTIFIER GENERIC_DIMENSION
          {
-               $$ = new TypeOf ((Expression) $3, lexer.Location);
+               LocatedToken lt = (LocatedToken) $3;
+               TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
+               
+               $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
          }
        ;
 
+
 sizeof_expression
        : SIZEOF OPEN_PARENS type CLOSE_PARENS { 
-               $$ = new SizeOf ((Expression) $3, lexer.Location);
+               $$ = new SizeOf ((Expression) $3, (Location) $1);
          }
        ;
 
 checked_expression
        : CHECKED OPEN_PARENS expression CLOSE_PARENS
          {
-               $$ = new CheckedExpr ((Expression) $3, lexer.Location);
+               $$ = new CheckedExpr ((Expression) $3, (Location) $1);
          }
        ;
 
 unchecked_expression
        : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
          {
-               $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
+               $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
          }
        ;
 
@@ -2984,40 +3812,22 @@ pointer_member_access
        : primary_expression OP_PTR IDENTIFIER
          {
                Expression deref;
+               LocatedToken lt = (LocatedToken) $3;
 
-               deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
-               $$ = new MemberAccess (deref, (string) $3, lexer.Location);
+               deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
+               $$ = new MemberAccess (deref, lt.Value);
          }
        ;
 
 anonymous_method_expression
-       : DELEGATE opt_anonymous_method_signature _mark_
+       : DELEGATE opt_anonymous_method_signature
          {
-               oob_stack.Push (current_local_parameters);
-               current_local_parameters = (Parameters)$2;
-
-               // Force the next block to be created as a ToplevelBlock
-               oob_stack.Push (current_block);
-               oob_stack.Push (top_current_block);
-               current_block = null;
-         } 
+               start_anonymous (false, (Parameters) $2, (Location) $1);
+         }
          block
          {
-               Location loc = (Location) $3;
-               top_current_block = (Block) oob_stack.Pop ();
-               current_block = (Block) oob_stack.Pop ();
-               if (RootContext.Version == LanguageVersion.ISO_1){
-                       Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
-                       $$ = null;
-               } else  {
-                       ToplevelBlock anon_block = (ToplevelBlock) $5;
-
-                       anon_block.Parent = current_block;
-                       $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block, 
-                               anon_block, loc);
-               }
-               current_local_parameters = (Parameters) oob_stack.Pop ();
-         }
+               $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
+       }
        ;
 
 opt_anonymous_method_signature
@@ -3026,46 +3836,24 @@ opt_anonymous_method_signature
        ;
 
 anonymous_method_signature
-       : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS 
+       : open_parens
          {
-               if ($2 == null)
-                       $$ = Parameters.EmptyReadOnlyParameters;
-               else {
-                       ArrayList par_list = (ArrayList) $2;
-                       Parameter [] pars = new Parameter [par_list.Count];
-                       par_list.CopyTo (pars);
-                       $$ = new Parameters (pars, null, lexer.Location);
-               }
+               params_modifiers_not_allowed = true; 
          }
-       ;
-
-opt_anonymous_method_parameter_list
-       : /* empty */                      { $$ = null; } 
-       | anonymous_method_parameter_list  { $$ = $1; }
-       ;
-
-anonymous_method_parameter_list
-       : anonymous_method_parameter 
+         opt_formal_parameter_list CLOSE_PARENS
          {
-               ArrayList a = new ArrayList (4);
-               a.Add ($1);
-               $$ = a;
+               params_modifiers_not_allowed = false;
+               $$ = $3;
          }
-       | anonymous_method_parameter_list COMMA anonymous_method_parameter 
+       ;
+
+default_value_expression
+       : DEFAULT_OPEN_PARENS type CLOSE_PARENS
          {
-               ArrayList a = (ArrayList) $1;
-               a.Add ($3);
-               $$ = a;
-         }
-       ; 
+               if (RootContext.Version < LanguageVersion.ISO_2)
+                       Report.FeatureIsNotAvailable (lexer.Location, "default value expression");
 
-anonymous_method_parameter
-       : opt_parameter_modifier type IDENTIFIER {
-               $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
-         }
-       | PARAMS type IDENTIFIER {
-               Report.Error (1670, lexer.Location, "params modifier not allowed in anonymous method declaration");
-               $$ = null;
+               $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
          }
        ;
 
@@ -3073,11 +3861,11 @@ unary_expression
        : primary_expression
        | BANG prefixed_unary_expression
          {
-               $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
+               $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
          }
        | TILDE prefixed_unary_expression
          {
-               $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
+               $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
          }
        | cast_expression
        ;
@@ -3085,11 +3873,15 @@ unary_expression
 cast_list
        : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
          {
-               $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
+               $$ = new Cast ((Expression) $1, (Expression) $3);
+         }
+       | parenthesized_expression_0 CLOSE_PARENS_NO_CAST default_value_expression
+         {
+               $$ = new Cast ((Expression) $1, (Expression) $3);
          }
        | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
          {
-               $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
+               $$ = new Cast ((Expression) $1, (Expression) $3);
          }     
        ;
 
@@ -3097,6 +3889,7 @@ cast_expression
        : cast_list
        | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
          {
+               // TODO: wrong location
                $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
          }
        ;
@@ -3109,45 +3902,29 @@ prefixed_unary_expression
        : unary_expression
        | PLUS prefixed_unary_expression
          { 
-               $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
+               $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
          } 
        | MINUS prefixed_unary_expression 
          { 
-               $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
+               $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
          }
        | OP_INC prefixed_unary_expression 
          {
                $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
-                                      (Expression) $2, lexer.Location);
+                                      (Expression) $2, (Location) $1);
          }
        | OP_DEC prefixed_unary_expression 
          {
                $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
-                                      (Expression) $2, lexer.Location);
+                                      (Expression) $2, (Location) $1);
          }
        | STAR prefixed_unary_expression
          {
-               $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
+               $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
          }
        | BITWISE_AND prefixed_unary_expression
          {
-               $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
-         }
-       ;
-
-pre_increment_expression
-       : OP_INC prefixed_unary_expression 
-         {
-               $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
-                                      (Expression) $2, lexer.Location);
-         }
-       ;
-
-pre_decrement_expression
-       : OP_DEC prefixed_unary_expression 
-         {
-               $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
-                                      (Expression) $2, lexer.Location);
+               $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
          }
        ;
 
@@ -3156,17 +3933,17 @@ multiplicative_expression
        | multiplicative_expression STAR prefixed_unary_expression
          {
                $$ = new Binary (Binary.Operator.Multiply, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        | multiplicative_expression DIV prefixed_unary_expression
          {
                $$ = new Binary (Binary.Operator.Division, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        | multiplicative_expression PERCENT prefixed_unary_expression 
          {
                $$ = new Binary (Binary.Operator.Modulus, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        ;
 
@@ -3175,12 +3952,12 @@ additive_expression
        | additive_expression PLUS multiplicative_expression 
          {
                $$ = new Binary (Binary.Operator.Addition, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        | additive_expression MINUS multiplicative_expression
          {
                $$ = new Binary (Binary.Operator.Subtraction, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        ;
 
@@ -3189,44 +3966,72 @@ shift_expression
        | shift_expression OP_SHIFT_LEFT additive_expression
          {
                $$ = new Binary (Binary.Operator.LeftShift, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        | shift_expression OP_SHIFT_RIGHT additive_expression
          {
                $$ = new Binary (Binary.Operator.RightShift, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        ; 
 
+opt_error
+       : /* empty */
+         {
+               $$ = false;
+         }
+       | error
+         {
+               lexer.PutbackNullable ();
+               $$ = true;
+         }
+       ;
+
+nullable_type_or_conditional
+       : type opt_error
+         {
+               if (((bool) $2) && ($1 is ComposedCast))
+                       $$ = ((ComposedCast) $1).RemoveNullable ();
+               else
+                       $$ = $1;
+         }
+       ;
+
 relational_expression
        : shift_expression
        | relational_expression OP_LT shift_expression
          {
                $$ = new Binary (Binary.Operator.LessThan, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        | relational_expression OP_GT shift_expression
          {
                $$ = new Binary (Binary.Operator.GreaterThan, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        | relational_expression OP_LE shift_expression
          {
                $$ = new Binary (Binary.Operator.LessThanOrEqual, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        | relational_expression OP_GE shift_expression
          {
                $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
-       | relational_expression IS type
+       | relational_expression IS
+         {
+               yyErrorFlag = 3;
+         } nullable_type_or_conditional
          {
-               $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
+               $$ = new Is ((Expression) $1, (Expression) $4, (Location) $2);
          }
-       | relational_expression AS type
+       | relational_expression AS
          {
-               $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
+               yyErrorFlag = 3;
+         } nullable_type_or_conditional
+         {
+               $$ = new As ((Expression) $1, (Expression) $4, (Location) $2);
          }
        ;
 
@@ -3235,12 +4040,12 @@ equality_expression
        | equality_expression OP_EQ relational_expression
          {
                $$ = new Binary (Binary.Operator.Equality, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        | equality_expression OP_NE relational_expression
          {
                $$ = new Binary (Binary.Operator.Inequality, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        ; 
 
@@ -3249,7 +4054,7 @@ and_expression
        | and_expression BITWISE_AND equality_expression
          {
                $$ = new Binary (Binary.Operator.BitwiseAnd, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        ;
 
@@ -3258,7 +4063,7 @@ exclusive_or_expression
        | exclusive_or_expression CARRET and_expression
          {
                $$ = new Binary (Binary.Operator.ExclusiveOr, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        ;
 
@@ -3267,7 +4072,7 @@ inclusive_or_expression
        | inclusive_or_expression BITWISE_OR exclusive_or_expression
          {
                $$ = new Binary (Binary.Operator.BitwiseOr, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        ;
 
@@ -3276,7 +4081,7 @@ conditional_and_expression
        | conditional_and_expression OP_AND inclusive_or_expression
          {
                $$ = new Binary (Binary.Operator.LogicalAnd, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        ;
 
@@ -3285,7 +4090,7 @@ conditional_or_expression
        | conditional_or_expression OP_OR conditional_and_expression
          {
                $$ = new Binary (Binary.Operator.LogicalOr, 
-                                (Expression) $1, (Expression) $3, lexer.Location);
+                                (Expression) $1, (Expression) $3);
          }
        ;
 
@@ -3293,93 +4098,174 @@ conditional_expression
        : conditional_or_expression
        | conditional_or_expression INTERR expression COLON expression 
          {
-               $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
+               $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
+         }
+       | conditional_or_expression OP_COALESCING expression
+         {
+               $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, lexer.Location);
+         }
+       // We'll be resolved into a `parenthesized_expression_0' later on.
+       | conditional_or_expression INTERR CLOSE_PARENS
+         {
+               $$ = new ComposedCast ((Expression) $1, "?", lexer.Location);
+               lexer.PutbackCloseParens ();
          }
        ;
 
 assignment_expression
        : prefixed_unary_expression ASSIGN expression
          {
-               $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
+               $$ = new Assign ((Expression) $1, (Expression) $3);
          }
        | prefixed_unary_expression OP_MULT_ASSIGN expression
          {
-               Location l = lexer.Location;
-
                $$ = new CompoundAssign (
-                       Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
+                       Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
          }
        | prefixed_unary_expression OP_DIV_ASSIGN expression
          {
-               Location l = lexer.Location;
-
                $$ = new CompoundAssign (
-                       Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
+                       Binary.Operator.Division, (Expression) $1, (Expression) $3);
          }
        | prefixed_unary_expression OP_MOD_ASSIGN expression
          {
-               Location l = lexer.Location;
-
                $$ = new CompoundAssign (
-                       Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
+                       Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
          }
        | prefixed_unary_expression OP_ADD_ASSIGN expression
          {
-               Location l = lexer.Location;
-
                $$ = new CompoundAssign (
-                       Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
+                       Binary.Operator.Addition, (Expression) $1, (Expression) $3);
          }
        | prefixed_unary_expression OP_SUB_ASSIGN expression
          {
-               Location l = lexer.Location;
-
                $$ = new CompoundAssign (
-                       Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
+                       Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
          }
        | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
          {
-               Location l = lexer.Location;
-
                $$ = new CompoundAssign (
-                       Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
+                       Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
          }
        | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
          {
-               Location l = lexer.Location;
-
                $$ = new CompoundAssign (
-                       Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
+                       Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
          }
        | prefixed_unary_expression OP_AND_ASSIGN expression
          {
-               Location l = lexer.Location;
-
                $$ = new CompoundAssign (
-                       Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
+                       Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
          }
        | prefixed_unary_expression OP_OR_ASSIGN expression
          {
-               Location l = lexer.Location;
-
                $$ = new CompoundAssign (
-                       Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
+                       Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
          }
        | prefixed_unary_expression OP_XOR_ASSIGN expression
          {
-               Location l = lexer.Location;
-
                $$ = new CompoundAssign (
-                       Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
+                       Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
          }
        ;
 
-expression
-       : conditional_expression
-       | assignment_expression
+lambda_parameter_list
+       : lambda_parameter
+         {
+               ArrayList pars = new ArrayList (4);
+               pars.Add ($1);
+
+               $$ = pars;
+         }
+       | lambda_parameter_list COMMA lambda_parameter
+         {
+               ArrayList pars = (ArrayList) $1;
+               Parameter p = (Parameter)$3;
+               if (pars[0].GetType () != p.GetType ()) {
+                       Report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
+               }
+               
+               pars.Add (p);
+               $$ = pars;
+         }
        ;
 
-constant_expression
+lambda_parameter
+       : parameter_modifier type IDENTIFIER
+         {
+               LocatedToken lt = (LocatedToken) $3;
+
+               $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
+         }
+       | type IDENTIFIER
+         {
+               LocatedToken lt = (LocatedToken) $2;
+
+               $$ = new Parameter ((Expression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
+         }
+       | IDENTIFIER
+         {
+               LocatedToken lt = (LocatedToken) $1;
+               $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
+         }
+       ;
+
+opt_lambda_parameter_list
+       : /* empty */                   { $$ = Parameters.EmptyReadOnlyParameters; }
+       | lambda_parameter_list         { 
+               ArrayList pars_list = (ArrayList) $1;
+               $$ = new Parameters ((Parameter[])pars_list.ToArray (typeof (Parameter)));
+         }
+       ;
+
+lambda_expression_body
+       : {
+               start_block (lexer.Location);
+         }
+         expression 
+         {
+               Block b = end_block (lexer.Location);
+               b.AddStatement (new ContextualReturn ((Expression) $2));
+               $$ = b;
+         } 
+       | block { 
+               $$ = $1; 
+         } 
+       ;
+
+lambda_expression
+       : IDENTIFIER ARROW 
+         {
+               LocatedToken lt = (LocatedToken) $1;
+               Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
+               start_anonymous (true, new Parameters (p), (Location) $2);
+         }
+         lambda_expression_body
+         {
+               $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
+         }
+       | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW 
+         {
+               start_anonymous (true, (Parameters) $2, (Location) $4);
+         }
+         lambda_expression_body 
+         {
+               $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
+         }
+       ;
+
+expression
+       : assignment_expression
+       | non_assignment_expression
+       ;
+       
+non_assignment_expression
+       : conditional_expression
+       | lambda_expression
+       | query_expression
+       ;
+
+constant_expression
        : expression
        ;
 
@@ -3394,48 +4280,24 @@ class_declaration
        : opt_attributes
          opt_modifiers
          opt_partial
-         CLASS member_name
+         CLASS
          {
-               MemberName name = MakeName ((MemberName) $5);
-               bool partial = (bool) $3;
-               int mod_flags = (int) $2;
-
-               if (partial) {
-                       ClassPart part = PartialContainer.CreatePart (
-                               current_namespace, current_class, name, mod_flags,
-                               (Attributes) $1, Kind.Class, lexer.Location);
-
-                       current_container = part.PartialContainer;
-                       current_class = part;
-               } else {
-                       if ((mod_flags & Modifiers.STATIC) != 0) {
-                               current_class = new StaticClass (
-                                       current_namespace, current_class, name,
-                                       mod_flags, (Attributes) $1, lexer.Location);
-                       } else {
-                               current_class = new Class (
-                                       current_namespace, current_class, name,
-                                       mod_flags, (Attributes) $1, lexer.Location);
-                       }
-
-                       current_container.AddClassOrStruct (current_class);
-                       current_container = current_class;
-                       RootContext.Tree.RecordDecl (name, current_class);
-               }
+               lexer.ConstraintsParsing = true;
+         }
+         type_name
+         {
+               MemberName name = MakeName ((MemberName) $6);
+               push_current_class (new Class (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
          }
          opt_class_base
+         opt_type_parameter_constraints_clauses
          {
-               if ($7 != null) {
-                       if (current_class.Name == "System.Object") {
-                               Report.Error (537, current_class.Location,
-                                             "The class System.Object cannot have a base " +
-                                             "class or implement an interface.");
-                       }
-                       current_class.Bases = (ArrayList) $7;
-               }
+               lexer.ConstraintsParsing = false;
+
+               current_class.SetParameterInfo ((ArrayList) $9);
 
                if (RootContext.Documentation != null) {
-                       current_class.DocComment = Lexer.consume_doc_comment ();
+                       current_container.DocComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
          }
@@ -3452,9 +4314,9 @@ class_declaration
 
 opt_partial
        : /* empty */
-         { $$ = (bool) false; }
+         { $$ = null; }
        | PARTIAL
-         { $$ = (bool) true; }
+         { $$ = $1; } // location
        ;
 
 opt_modifiers
@@ -3471,11 +4333,11 @@ modifiers
 
                if ((m1 & m2) != 0) {
                        Location l = lexer.Location;
-                       Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
+                       Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
                }
                $$ = (int) (m1 | m2);
          }
-        ;
+       ;
 
 modifier
        : NEW                   { $$ = Modifiers.NEW; }
@@ -3495,12 +4357,74 @@ modifier
        ;
 
 opt_class_base
-       : /* empty */           { $$ = null; }
-       | class_base            { $$ = $1;   }
+       : /* empty */
+       | class_base
        ;
 
 class_base
-       : COLON type_list { $$ = $2; }
+       : COLON type_list       { current_container.AddBasesForPart (current_class, (ArrayList) $2); }
+       ;
+
+opt_type_parameter_constraints_clauses
+       : /* empty */           { $$ = null; }
+       | type_parameter_constraints_clauses 
+         { $$ = $1; }
+       ;
+
+type_parameter_constraints_clauses
+       : type_parameter_constraints_clause {
+               ArrayList constraints = new ArrayList (1);
+               constraints.Add ($1);
+               $$ = constraints;
+         }
+       | type_parameter_constraints_clauses type_parameter_constraints_clause {
+               ArrayList constraints = (ArrayList) $1;
+               Constraints new_constraint = (Constraints)$2;
+
+               foreach (Constraints c in constraints) {
+                       if (new_constraint.TypeParameter == c.TypeParameter) {
+                               Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
+                                       new_constraint.TypeParameter);
+                       }
+               }
+
+               constraints.Add (new_constraint);
+               $$ = constraints;
+         }
+       ; 
+
+type_parameter_constraints_clause
+       : WHERE IDENTIFIER COLON type_parameter_constraints {
+               LocatedToken lt = (LocatedToken) $2;
+               $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
+         }
+       ; 
+
+type_parameter_constraints
+       : type_parameter_constraint {
+               ArrayList constraints = new ArrayList (1);
+               constraints.Add ($1);
+               $$ = constraints;
+         }
+       | type_parameter_constraints COMMA type_parameter_constraint {
+               ArrayList constraints = (ArrayList) $1;
+
+               constraints.Add ($3);
+               $$ = constraints;
+         }
+       ;
+
+type_parameter_constraint
+       : type
+       | NEW OPEN_PARENS CLOSE_PARENS {
+               $$ = SpecialConstraint.Constructor;
+         }
+       | CLASS {
+               $$ = SpecialConstraint.ReferenceType;
+         }
+       | STRUCT {
+               $$ = SpecialConstraint.ValueType;
+         }
        ;
 
 //
@@ -3517,24 +4441,27 @@ class_base
 //     event_declaration as part of add_accessor_declaration or remove_accessor_declaration
 //      
 block
-       : OPEN_BRACE 
+       : OPEN_BRACE  
          {
-               if (current_block == null){
-                       current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
-                       top_current_block = current_block;
-               } else {
-               current_block = new Block (current_block, lexer.Location, Location.Null);
-               }
+               ++lexer.parsing_block;
+               start_block ((Location) $1);
          } 
          opt_statement_list CLOSE_BRACE 
-         { 
-               while (current_block.Implicit)
-                       current_block = current_block.Parent;
-               $$ = current_block;
-               current_block.SetEndLocation (lexer.Location);
-               current_block = current_block.Parent;
-               if (current_block == null)
-                       top_current_block = null;
+         {
+               --lexer.parsing_block;
+               $$ = end_block ((Location) $4);
+         }
+       ;
+
+block_prepared
+       : OPEN_BRACE
+         {
+               ++lexer.parsing_block;
+         }
+         opt_statement_list CLOSE_BRACE 
+         {
+               --lexer.parsing_block;
+               $$ = end_block ((Location) $4);
          }
        ;
 
@@ -3583,12 +4510,12 @@ embedded_statement
        : valid_declaration_statement
        | declaration_statement
          {
-                 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
+                 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
                  $$ = null;
          }
        | labeled_statement
          {
-                 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
+                 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
                  $$ = null;
          }
        ;
@@ -3603,9 +4530,10 @@ empty_statement
 labeled_statement
        : IDENTIFIER COLON 
          {
-               LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
+               LocatedToken lt = (LocatedToken) $1;
+               LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
 
-               if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
+               if (current_block.AddLabel (labeled))
                        current_block.AddStatement (labeled);
          }
          statement
@@ -3614,15 +4542,18 @@ labeled_statement
 declaration_statement
        : local_variable_declaration SEMICOLON
          {
+               current_array_type = null;
                if ($1 != null){
                        DictionaryEntry de = (DictionaryEntry) $1;
+                       Expression e = (Expression) de.Key;
 
-                       $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
+                       $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
                }
          }
 
        | local_constant_declaration SEMICOLON
          {
+               current_array_type = null;
                if ($1 != null){
                        DictionaryEntry de = (DictionaryEntry) $1;
 
@@ -3638,7 +4569,7 @@ declaration_statement
  * > The expressions are converted into types during semantic analysis.
  */
 local_variable_type
-       : primary_expression opt_rank_specifier
+       : primary_expression opt_rank_specifier_or_nullable
          { 
                // FIXME: Do something smart here regarding the composition of the type.
 
@@ -3656,9 +4587,10 @@ local_variable_type
                // SimpleName is when you have
                // Blah i;
                  
-               Expression expr = (Expression) $1;  
-               if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
-                       Error_ExpectingTypeName (lexer.Location, expr);
+               Expression expr = (Expression) $1;
+               SimpleName sn = expr as SimpleName;
+               if (!(sn != null || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
+                       Error_ExpectingTypeName (expr);
                        $$ = null;
                } else {
                        //
@@ -3666,18 +4598,21 @@ local_variable_type
                        // or MemberAccess
                        // 
 
-                       if ((string) $2 == "")
-                               $$ = $1;
-                       else
-                               $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
+                       if ((string) $2 == "") {
+                               if (sn != null && RootContext.Version > LanguageVersion.ISO_2 && sn.Name == "var")
+                                       $$ = new VarExpr (sn.Location);
+                               else
+                                       $$ = $1;
+                       } else
+                               $$ = new ComposedCast ((Expression) $1, (string) $2);
                }
          }
-       | builtin_types opt_rank_specifier
+       | builtin_types opt_rank_specifier_or_nullable
          {
                if ((string) $2 == "")
                        $$ = $1;
                else
-                       $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
+                       $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
          }
         ;
 
@@ -3685,50 +4620,53 @@ local_variable_pointer_type
        : primary_expression STAR
          {
                Expression expr = (Expression) $1;  
-               Location l = lexer.Location;
 
-               if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
-                       Error_ExpectingTypeName (l, expr);
+               if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType || expr is QualifiedAliasMember)) {
+                       Error_ExpectingTypeName (expr);
 
                        $$ = null;
                } else 
-                       $$ = new ComposedCast ((Expression) $1, "*", l);
+                       $$ = new ComposedCast ((Expression) $1, "*");
          }
         | builtin_types STAR
          {
-               $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
+               $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
          }
         | VOID STAR
          {
-               $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
+               $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
          }
        | local_variable_pointer_type STAR
           {
-               $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
+               $$ = new ComposedCast ((Expression) $1, "*");
          }
         ;
 
 local_variable_declaration
        : local_variable_type variable_declarators
          {
-               if ($1 != null)
+               if ($1 != null) {
+                       VarExpr ve = $1 as VarExpr;
+                       if (ve != null)
+                               ve.VariableInitializer = (ArrayList)$2;
+                               
                        $$ = new DictionaryEntry ($1, $2);
-               else
+               else
                        $$ = null;
          }
-        | local_variable_pointer_type opt_rank_specifier variable_declarators
-       {
+       | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
+         {
                if ($1 != null){
                        Expression t;
 
                        if ((string) $2 == "")
                                t = (Expression) $1;
                        else
-                               t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
+                               t = new ComposedCast ((Expression) $1, (string) $2);
                        $$ = new DictionaryEntry (t, $3);
                } else 
                        $$ = null;
-       }
+         }
        ;
 
 local_constant_declaration
@@ -3742,10 +4680,7 @@ local_constant_declaration
        ;
 
 expression_statement
-       : statement_expression SEMICOLON
-         {
-               $$ = $1;
-         }
+       : statement_expression SEMICOLON { $$ = $1; }
        ;
 
        //
@@ -3753,61 +4688,67 @@ expression_statement
        // because statement_expression is used for example in for_statement
        //
 statement_expression
-       : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
-       | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
-       | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
-       | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
-       | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
-       | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
-       | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
-       | error {
-               Report.Error (1002, lexer.Location, "Expecting `;'");
+       : expression
+         {
+               Expression expr = (Expression) $1;
+               ExpressionStatement s = expr as ExpressionStatement;
+               if (s == null) {
+                       expr.Error_InvalidExpressionStatement ();
+                       $$ = null;
+               }
+               $$ = new StatementExpression (s);
+         }
+       | error
+         {
+               Report.Error (1002, GetLocation ($1), "Expecting `;'");
                $$ = null;
          }
        ;
 
-object_creation_expression
-       : object_or_delegate_creation_expression
-         { note ("complain if this is a delegate maybe?"); } 
-       ;
-
 selection_statement
        : if_statement
        | switch_statement
        ; 
 
 if_statement
-       : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS 
+       : IF OPEN_PARENS boolean_expression CLOSE_PARENS 
          embedded_statement
          { 
-               Location l = (Location) $3;
+               Location l = (Location) $1;
 
-               $$ = new If ((Expression) $4, (Statement) $6, l);
+               $$ = new If ((Expression) $3, (Statement) $5, l);
 
-               if (RootContext.WarningLevel >= 4){
-                       if ($6 == EmptyStatement.Value)
-                               Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
-               }
+               // FIXME: location for warning should be loc property of $5.
+               if ($5 == EmptyStatement.Value)
+                       Report.Warning (642, 3, l, "Possible mistaken empty statement");
 
          }
-       | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
+       | IF OPEN_PARENS boolean_expression CLOSE_PARENS
          embedded_statement ELSE embedded_statement
          {
-               Location l = (Location) $3;
+               Location l = (Location) $1;
+
+               $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
 
-               $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
+               // FIXME: location for warning should be loc property of $5 and $7.
+               if ($5 == EmptyStatement.Value)
+                       Report.Warning (642, 3, l, "Possible mistaken empty statement");
+               if ($7 == EmptyStatement.Value)
+                       Report.Warning (642, 3, l, "Possible mistaken empty statement");
          }
        ;
 
 switch_statement
-       : SWITCH OPEN_PARENS _mark_
+       : SWITCH OPEN_PARENS
          { 
+               if (switch_stack == null)
+                       switch_stack = new Stack (2);
                switch_stack.Push (current_block);
          }
          expression CLOSE_PARENS 
          switch_block
          {
-               $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
+               $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
                current_block = (Block) switch_stack.Pop ();
          }
        ;
@@ -3824,7 +4765,8 @@ switch_block
 opt_switch_sections
        : /* empty */           
           {
-               Report.Error (1522, lexer.Location, "Empty switch block"); 
+               Report.Warning (1522, 1, lexer.Location, "Empty switch block"); 
+               $$ = new ArrayList ();
          }
        | switch_sections
        ;
@@ -3853,11 +4795,7 @@ switch_section
          }
          statement_list 
          {
-               Block topmost = current_block;
-
-               while (topmost.Implicit)
-                       topmost = topmost.Parent;
-               $$ = new SwitchSection ((ArrayList) $1, topmost);
+               $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
          }
        ;
 
@@ -3879,11 +4817,11 @@ switch_labels
        ;
 
 switch_label
-       : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
-       | DEFAULT COLON                         { $$ = new SwitchLabel (null, lexer.Location); }
+       : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
+       | DEFAULT_COLON                         { $$ = new SwitchLabel (null, (Location) $1); }
        | error {
                Report.Error (
-                       1523, lexer.Location
+                       1523, GetLocation ($1)
                        "The keyword case or default must precede code in switch block");
          }
        ;
@@ -3896,34 +4834,30 @@ iteration_statement
        ;
 
 while_statement
-       : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
-       {
-               Location l = (Location) $3;
-               $$ = new While ((Expression) $4, (Statement) $6, l);
-       
-               if (RootContext.WarningLevel >= 4){
-                       if ($6 == EmptyStatement.Value)
-                               Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
-               }
-       }
+       : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
+         {
+               Location l = (Location) $1;
+               $$ = new While ((Expression) $3, (Statement) $5, l);
+         }
        ;
 
 do_statement
        : DO embedded_statement 
-         WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
+         WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
          {
-               Location l = (Location) $5;
+               Location l = (Location) $1;
 
-               $$ = new Do ((Statement) $2, (Expression) $6, l);
+               $$ = new Do ((Statement) $2, (Expression) $5, l);
          }
        ;
 
 for_statement
-       : FOR OPEN_PARENS 
-         opt_for_initializer SEMICOLON _mark_
+       : FOR open_parens 
+         opt_for_initializer SEMICOLON
          {
-               Block assign_block = new Block (current_block);
-               current_block = assign_block;
+               Location l = lexer.Location;
+               start_block (l);  
+               Block assign_block = current_block;
 
                if ($3 is DictionaryEntry){
                        DictionaryEntry de = (DictionaryEntry) $3;
@@ -3939,16 +4873,7 @@ for_statement
                                if (vi == null)
                                        continue;
 
-                               Location l = lexer.Location;
-                               Expression expr;
-                               if (decl.expression_or_array_initializer is Expression){
-                                       expr = (Expression) decl.expression_or_array_initializer;
-                               } else if (decl.expression_or_array_initializer == null) {
-                                       expr = null;
-                               } else {
-                                       ArrayList init = (ArrayList) decl.expression_or_array_initializer;
-                                       expr = new ArrayCreation (type, "", init, decl.Location);
-                               }
+                               Expression expr = decl.expression_or_array_initializer;
                                        
                                LocalVariableReference var;
                                var = new LocalVariableReference (assign_block, decl.identifier, l);
@@ -3956,12 +4881,12 @@ for_statement
                                if (expr != null) {
                                        Assign a = new Assign (var, expr, decl.Location);
                                        
-                                       assign_block.AddStatement (new StatementExpression (a, lexer.Location));
+                                       assign_block.AddStatement (new StatementExpression (a));
                                }
                        }
                        
                        // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
-                       // This can be referred to as $6 below.
+                       // This can be referred to as $5 below.
                        $$ = null;
                } else {
                        $$ = $3;
@@ -3971,20 +4896,13 @@ for_statement
          opt_for_iterator CLOSE_PARENS 
          embedded_statement
          {
-               Location l = (Location) $5;
-
-               For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
+               Location l = (Location) $1;
 
-               if (RootContext.WarningLevel >= 4){
-                       if ($11 == EmptyStatement.Value)
-                               Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
-               }
+               For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
 
                current_block.AddStatement (f);
-               while (current_block.Implicit)
-                       current_block = current_block.Parent;
-               $$ = current_block;
-               current_block = current_block.Parent;
+
+               $$ = end_block (lexer.Location);
          }
        ;
 
@@ -4016,9 +4934,10 @@ statement_expression_list
        : statement_expression  
          {
                // CHANGE: was `null'
-               Block b = new Block (current_block, Block.Flags.Implicit);   
+               Statement s = (Statement) $1;
+               Block b = new Block (current_block, s.loc, lexer.Location);   
 
-               b.AddStatement ((Statement) $1);
+               b.AddStatement (s);
                $$ = b;
          }
        | statement_expression_list COMMA statement_expression
@@ -4031,47 +4950,43 @@ statement_expression_list
        ;
 
 foreach_statement
-       : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
-       {
-               Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
+       : FOREACH open_parens type IN expression CLOSE_PARENS
+         {
+               Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
                $$ = null;
-       }
-       | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
+         }
+       | FOREACH open_parens type IDENTIFIER IN
          expression CLOSE_PARENS 
          {
-               Block foreach_block = new Block (current_block);
-               current_block = foreach_block;
-
-               Location l = lexer.Location;
-               LocalInfo vi;
+               start_block (lexer.Location);
+               Block foreach_block = current_block;
 
-               vi = foreach_block.AddVariable ((Expression) $3, (string) $4, l);
+               LocatedToken lt = (LocatedToken) $4;
+               Location l = lt.Location;
+               LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
                if (vi != null) {
-                       vi.ReadOnly = true;
+                       vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
 
                        // Get a writable reference to this read-only variable.
                        //
                        // Note that the $$ here refers to the value of _this_ code block,
-                       // not the value of the LHS non-terminal.  This can be referred to as $9 below.
-                       $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
+                       // not the value of the LHS non-terminal.  This can be referred to as $8 below.
+                       $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
                } else {
                        $$ = null;
                }
          } 
          embedded_statement 
          {
-               LocalVariableReference v = (LocalVariableReference) $9;
-               Location l = (Location) $6;
+               LocalVariableReference v = (LocalVariableReference) $8;
+               Location l = (Location) $1;
 
                if (v != null) {
-                       Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
+                       Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
                        current_block.AddStatement (f);
                }
 
-               while (current_block.Implicit)
-                         current_block = current_block.Parent;
-               $$ = current_block;
-               current_block = current_block.Parent;
+               $$ = end_block (lexer.Location);
          }
        ;
 
@@ -4087,88 +5002,91 @@ jump_statement
 break_statement
        : BREAK SEMICOLON
          {
-               $$ = new Break (lexer.Location);
+               $$ = new Break ((Location) $1);
          }
        ;
 
 continue_statement
        : CONTINUE SEMICOLON
          {
-               $$ = new Continue (lexer.Location);
+               $$ = new Continue ((Location) $1);
          }
        ;
 
 goto_statement
        : GOTO IDENTIFIER SEMICOLON 
          {
-               $$ = new Goto ((string) $2, lexer.Location);
+               LocatedToken lt = (LocatedToken) $2;
+               $$ = new Goto (lt.Value, lt.Location);
          }
        | GOTO CASE constant_expression SEMICOLON
          {
-               $$ = new GotoCase ((Expression) $3, lexer.Location);
+               $$ = new GotoCase ((Expression) $3, (Location) $1);
          }
        | GOTO DEFAULT SEMICOLON 
          {
-               $$ = new GotoDefault (lexer.Location);
+               $$ = new GotoDefault ((Location) $1);
          }
        ; 
 
 return_statement
        : RETURN opt_expression SEMICOLON
          {
-               $$ = new Return ((Expression) $2, lexer.Location);
+               $$ = new Return ((Expression) $2, (Location) $1);
          }
        ;
 
 throw_statement
        : THROW opt_expression SEMICOLON
          {
-               $$ = new Throw ((Expression) $2, lexer.Location);
+               $$ = new Throw ((Expression) $2, (Location) $1);
          }
        ;
 
 yield_statement 
        : IDENTIFIER RETURN expression SEMICOLON
          {
-               string s = (string) $1;
+               LocatedToken lt = (LocatedToken) $1;
+               string s = lt.Value;
                if (s != "yield"){
-                       Report.Error (1003, lexer.Location, "; expected");
+                       Report.Error (1003, lt.Location, "; expected");
                        $$ = null;
                }
                if (RootContext.Version == LanguageVersion.ISO_1){
-                       Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
+                       Report.FeatureIsNotAvailable (lt.Location, "yield statement");
                        $$ = null;
                }
-               if (iterator_container == null){
-                       Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
+               if (anonymous_host == null){
+                       Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
                        $$ = null;
                } else {
-                       iterator_container.SetYields ();
-                       $$ = new Yield ((Expression) $3, lexer.Location); 
+                       anonymous_host.SetYields ();
+                       $$ = new Yield ((Expression) $3, lt.Location); 
                }
          }
        | IDENTIFIER RETURN SEMICOLON
-       {
-               Report.Error (1627, lexer.Location, "Expression expected after yield return");
+         {
+               Report.Error (1627, (Location) $2, "Expression expected after yield return");
                $$ = null;
-       }
+         }
        | IDENTIFIER BREAK SEMICOLON
          {
-               string s = (string) $1;
+               LocatedToken lt = (LocatedToken) $1;
+               string s = lt.Value;
                if (s != "yield"){
-                       Report.Error (1003, lexer.Location, "; expected");
+                       Report.Error (1003, lt.Location, "; expected");
                        $$ = null;
                }
                if (RootContext.Version == LanguageVersion.ISO_1){
-                       Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
+                       Report.FeatureIsNotAvailable (lt.Location, "yield statement");
                        $$ = null;
                }
-               if (iterator_container == null){
-                       Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
+               if (anonymous_host == null){
+                       Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
                        $$ = null;
                } else {
-                       iterator_container.SetYields ();
-                       $$ = new YieldBreak (lexer.Location);
+                       anonymous_host.SetYields ();
+                       $$ = new YieldBreak (lt.Location);
                }
          }
        ;
@@ -4180,7 +5098,7 @@ opt_expression
 
 try_statement
        : TRY block catch_clauses 
-       {
+         {
                Catch g = null;
                
                ArrayList c = (ArrayList)$3;
@@ -4188,7 +5106,7 @@ try_statement
                        Catch cc = (Catch) c [i];
                        if (cc.IsGeneral) {
                                if (i != c.Count - 1)
-                                       Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
+                                       Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
                                g = cc;
                                c.RemoveAt (i);
                                i--;
@@ -4199,7 +5117,7 @@ try_statement
                // and g contains the general one.
                
                $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
-       }
+         }
        | TRY block opt_catch_clauses FINALLY block
          {
                Catch g = null;
@@ -4219,7 +5137,8 @@ try_statement
          }
        | TRY block error 
          {
-               Report.Error (1524, lexer.Location, "Expected catch or finally");
+               Report.Error (1524, (Location) $1, "Expected catch or finally");
+               $$ = null;
          }
        ;
 
@@ -4252,44 +5171,41 @@ opt_identifier
 
 catch_clause 
        : CATCH opt_catch_args 
-       {
+         {
                Expression type = null;
-               string id = null;
                
                if ($2 != null) {
                        DictionaryEntry cc = (DictionaryEntry) $2;
                        type = (Expression) cc.Key;
-                       id   = (string) cc.Value;
+                       LocatedToken lt = (LocatedToken) cc.Value;
 
-                       if (id != null){
+                       if (lt != null){
                                ArrayList one = new ArrayList (4);
-                               Location loc = lexer.Location;
 
-                               one.Add (new VariableDeclaration (id, null, loc));
+                               one.Add (new VariableDeclaration (lt, null));
 
-                               current_block = new Block (current_block);
-                               Block b = declare_local_variables (type, one, loc);
-                               current_block = b;
+                               start_block (lexer.Location);
+                               current_block = declare_local_variables (type, one, lt.Location);
                        }
                }
-       } block {
+         } block {
                Expression type = null;
                string id = null;
+               Block var_block = null;
 
                if ($2 != null){
                        DictionaryEntry cc = (DictionaryEntry) $2;
                        type = (Expression) cc.Key;
-                       id   = (string) cc.Value;
+                       LocatedToken lt = (LocatedToken) cc.Value;
 
-                       if (id != null){
-                               while (current_block.Implicit)
-                                       current_block = current_block.Parent;
-                               current_block = current_block.Parent;
+                       if (lt != null){
+                               id = lt.Value;
+                               var_block = end_block (lexer.Location);
                        }
                }
 
-               $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
-       }
+               $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
+         }
         ;
 
 opt_catch_args
@@ -4298,12 +5214,13 @@ opt_catch_args
        ;         
 
 catch_args 
-        : OPEN_PARENS type opt_identifier CLOSE_PARENS 
-        {
+        : open_parens type opt_identifier CLOSE_PARENS 
+          {
                $$ = new DictionaryEntry ($2, $3);
-       }
+         }
         ;
 
+
 checked_statement
        : CHECKED block
          {
@@ -4320,28 +5237,24 @@ unchecked_statement
 
 unsafe_statement
        : UNSAFE 
-       {
-               if (!RootContext.Unsafe){
-                       Report.Error (227, lexer.Location, 
-                               "Unsafe code can only be used if --unsafe is used");
-               }
-       } block {
+         {
+               RootContext.CheckUnsafeOption ((Location) $1);
+         } block {
                $$ = new Unsafe ((Block) $3);
-       }
+         }
        ;
 
 fixed_statement
-       : FIXED OPEN_PARENS 
+       : FIXED open_parens 
          type fixed_pointer_declarators 
-         CLOSE_PARENS _mark_
+         CLOSE_PARENS
          {
                ArrayList list = (ArrayList) $4;
                Expression type = (Expression) $3;
-               Location l = lexer.Location;
+               Location l = (Location) $1;
                int top = list.Count;
 
-               Block assign_block = new Block (current_block);
-               current_block = assign_block;
+               start_block (lexer.Location);
 
                for (int i = 0; i < top; i++){
                        Pair p = (Pair) list [i];
@@ -4351,7 +5264,7 @@ fixed_statement
                        if (v == null)
                                continue;
 
-                       v.ReadOnly = true;
+                       v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
                        v.Pinned = true;
                        p.First = v;
                        list [i] = p;
@@ -4359,20 +5272,13 @@ fixed_statement
          }
          embedded_statement 
          {
-               Location l = (Location) $6;
-
-               Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
+               Location l = (Location) $1;
 
-               if (RootContext.WarningLevel >= 4){
-                       if ($8 == EmptyStatement.Value)
-                               Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
-               }
+               Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
 
                current_block.AddStatement (f);
-               while (current_block.Implicit)
-                       current_block = current_block.Parent;
-               $$ = current_block;
-               current_block = current_block.Parent;
+
+               $$ = end_block (lexer.Location);
          }
        ;
 
@@ -4394,12 +5300,14 @@ fixed_pointer_declarators
 
 fixed_pointer_declarator
        : IDENTIFIER ASSIGN expression
-         {     
-               $$ = new Pair ($1, $3);
+         {
+               LocatedToken lt = (LocatedToken) $1;
+               // FIXME: keep location
+               $$ = new Pair (lt.Value, $3);
          }
        | IDENTIFIER
          {
-               Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
+               Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
                $$ = null;
          }
        ;
@@ -4411,19 +5319,19 @@ lock_statement
          } 
          embedded_statement
          {
-               $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
+               $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
          }
        ;
 
 using_statement
-       : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
+       : USING open_parens resource_acquisition CLOSE_PARENS
          {
-               Block assign_block = new Block (current_block);
-               current_block = assign_block;
+               start_block (lexer.Location);
+               Block assign_block = current_block;
 
                if ($3 is DictionaryEntry){
                        DictionaryEntry de = (DictionaryEntry) $3;
-                       Location l = lexer.Location;
+                       Location l = (Location) $1;
 
                        Expression type = (Expression) de.Key;
                        ArrayList var_declarators = (ArrayList) de.Value;
@@ -4435,18 +5343,11 @@ using_statement
                                LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
                                if (vi == null)
                                        continue;
-                               vi.ReadOnly = true;
+                               vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
 
-                               Expression expr;
-                               if (decl.expression_or_array_initializer is Expression){
-                                       expr = (Expression) decl.expression_or_array_initializer;
-                               } else {
-                                       ArrayList init = (ArrayList) decl.expression_or_array_initializer;
-                                       if (init == null) {
-                                               Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
-                                       }
-                                       
-                                       expr = new ArrayCreation (type, "", init, decl.Location);
+                               Expression expr = decl.expression_or_array_initializer;
+                               if (expr == null) {
+                                       Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
                                }
 
                                LocalVariableReference var;
@@ -4460,11 +5361,11 @@ using_statement
                                vars.Add (new DictionaryEntry (var, expr));                             
 
                                // Assign a = new Assign (var, expr, decl.Location);
-                               // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
+                               // assign_block.AddStatement (new StatementExpression (a));
                        }
 
                        // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
-                       // It can be referred to as $6 below.
+                       // It can be referred to as $5 below.
                        $$ = new DictionaryEntry (type, vars);
                 } else {
                        $$ = $3;
@@ -4472,12 +5373,9 @@ using_statement
          } 
          embedded_statement
          {
-               Using u = new Using ($6, (Statement) $7, (Location) $5);
+               Using u = new Using ($5, (Statement) $6, (Location) $1);
                current_block.AddStatement (u);
-               while (current_block.Implicit)
-                       current_block = current_block.Parent;
-               $$ = current_block;
-               current_block = current_block.Parent;
+               $$ = end_block (lexer.Location);
          }
        ; 
 
@@ -4486,11 +5384,262 @@ resource_acquisition
        | expression
        ;
 
-// Utility rule to save location information
-_mark_
+
+// LINQ
+
+query_expression
+       : first_from_clause
+         {
+               ++lexer.query_parsing;
+         }
+         query_body
+         {
+               if (--lexer.query_parsing == 1)
+                       lexer.query_parsing = 0;
+                       
+               Linq.AQueryClause from = $1 as Linq.AQueryClause;
+                       
+               from.Tail.Next = (Linq.AQueryClause)$3;
+               $$ = from;
+               
+               current_block.SetEndLocation (lexer.Location);
+               current_block = current_block.Parent;
+         }     
+       ;
+       
+first_from_clause
+       : FROM IDENTIFIER IN expression
+         {
+               current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
+               LocatedToken lt = (LocatedToken) $2;
+               
+               current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance, lt.Value, lt.Location);
+               $$ = new Linq.QueryExpression (lt, new Linq.QueryStartClause ((Expression)$4));
+         }
+       | FROM type IDENTIFIER IN expression
+         {
+               current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
+               LocatedToken lt = (LocatedToken) $3;
+
+               Expression type = (Expression)$2;
+               current_block.AddVariable (type, lt.Value, lt.Location);
+               $$ = new Linq.QueryExpression (lt, new Linq.Cast (type, (Expression)$5));
+         }
+       ;
+       
+from_clause
+       : FROM IDENTIFIER IN expression
+         {
+               LocatedToken lt = (LocatedToken) $2;
+               
+               current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
+                       lt.Value, lt.Location);
+                       
+               $$ = new Linq.SelectMany (lt, (Expression)$4);                  
+         }
+       | FROM type IDENTIFIER IN expression
+         {
+               LocatedToken lt = (LocatedToken) $3;
+
+               Expression type = (Expression)$2;
+               current_block.AddVariable (type, lt.Value, lt.Location);
+               $$ = new Linq.SelectMany (lt, new Linq.Cast (type, (Expression)$5));
+         }
+       ;       
+
+query_body
+       : opt_query_body_clauses select_or_group_clause opt_query_continuation
+         {
+               Linq.AQueryClause head = (Linq.AQueryClause)$2;
+               
+               if ($3 != null)
+                       head.Next = (Linq.AQueryClause)$3;
+                               
+               if ($1 != null) {
+                       Linq.AQueryClause clause = (Linq.AQueryClause)$1;
+                       clause.Tail.Next = head;
+                       head = clause;
+               }
+               
+               $$ = head;
+         }
+       ;
+       
+select_or_group_clause
+       : SELECT expression
+         {
+               $$ = new Linq.Select ((Expression)$2, GetLocation ($1));
+         }
+       | GROUP expression BY expression
+         {
+           $$ = new Linq.GroupBy ((Expression)$2, (Expression)$4, GetLocation ($1));
+         }
+       ;
+       
+opt_query_body_clauses
        : /* empty */
-       { $$ = lexer.Location; }
+       | query_body_clauses
+       ;
+       
+query_body_clauses
+       : query_body_clause
+       | query_body_clauses query_body_clause
+         {
+               ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
+               $$ = $1;
+         }
+       ;
+       
+query_body_clause
+       : from_clause
+       | let_clause
+       | where_clause
+       | join_clause
+       | orderby_clause
+       ;
+       
+let_clause
+       : LET IDENTIFIER ASSIGN expression
+         {
+               LocatedToken lt = (LocatedToken) $2;
+               current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
+                       lt.Value, lt.Location);   
+               $$ = new Linq.Let (lt, (Expression)$4, GetLocation ($1));
+         }
+       ;
 
+where_clause
+       : WHERE boolean_expression
+         {
+               $$ = new Linq.Where ((Expression)$2, GetLocation ($1));
+         }
+       ;
+       
+join_clause
+       : JOIN IDENTIFIER IN expression ON expression EQUALS expression opt_join_into
+         {
+               Location loc = GetLocation ($1);
+               LocatedToken lt = (LocatedToken) $2;
+               current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
+                       lt.Value, lt.Location);
+               
+               if ($9 == null) {
+                       $$ = new Linq.Join (lt, (Expression)$4, (Expression)$6,
+                               (Expression)$8, loc);
+               } else {
+                       LocatedToken lt_into = (LocatedToken) $9;
+                       $$ = new Linq.GroupJoin (lt, (Expression)$4, (Expression)$6,
+                               (Expression)$8, lt_into, loc);
+               }
+         }
+       | JOIN type IDENTIFIER IN expression ON expression EQUALS expression opt_join_into
+         {
+               Location loc = GetLocation ($1);
+               LocatedToken lt = (LocatedToken) $3;
+               current_block.AddVariable ((Expression)$2, lt.Value, lt.Location);
+               
+               Linq.Cast cast = new Linq.Cast ((Expression)$2, (Expression)$5);
+               if ($10 == null) {
+                       $$ = new Linq.Join (lt, cast, (Expression)$7,
+                               (Expression)$9, loc);
+               } else {
+                       LocatedToken lt_into = (LocatedToken) $10;
+                       $$ = new Linq.GroupJoin (lt, cast, (Expression)$7,
+                               (Expression)$9, lt_into, loc);
+               }
+         }
+       ;
+       
+opt_join_into
+       : /* empty */
+       | INTO IDENTIFIER
+         {
+               $$ = $2;
+         }
+       ;
+       
+orderby_clause
+       : ORDERBY orderings
+         {
+               $$ = $2;
+         }
+       ;
+       
+orderings
+       : order_by
+       | order_by COMMA orderings_then_by
+         {
+               ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$3;
+               $$ = $1;
+         }
+       ;
+       
+orderings_then_by
+       : then_by
+         {
+               $$ = $1;
+         }
+       | orderings_then_by COMMA then_by
+         {
+               ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
+               $$ = $1;
+         }
+       ;       
+       
+order_by
+       : expression
+         {
+               $$ = new Linq.OrderByAscending ((Expression)$1);        
+         }
+       | expression ASCENDING
+         {
+               $$ = new Linq.OrderByAscending ((Expression)$1);        
+         }
+       | expression DESCENDING
+         {
+               $$ = new Linq.OrderByDescending ((Expression)$1);       
+         }
+       ;
+
+then_by
+       : expression
+         {
+               $$ = new Linq.ThenByAscending ((Expression)$1); 
+         }
+       | expression ASCENDING
+         {
+               $$ = new Linq.ThenByAscending ((Expression)$1); 
+         }
+       | expression DESCENDING
+         {
+               $$ = new Linq.ThenByDescending ((Expression)$1);        
+         }     
+       ;
+
+
+opt_query_continuation
+       : /* empty */
+       | INTO IDENTIFIER
+         {
+               // query continuation block is not linked with query block but with block
+               // before. This means each query can use same range variable names for
+               // different identifiers.
+
+               current_block.SetEndLocation (GetLocation ($1));
+               current_block = current_block.Parent;
+               current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
+               
+               LocatedToken lt = (LocatedToken) $2;
+               current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
+                       lt.Value, lt.Location);
+         }
+         query_body
+         {
+               $$ = new Linq.QueryExpression ((LocatedToken) $2,
+                       (Linq.AQueryClause)$4);
+         }
+       ;
+       
 %%
 
 // <summary>
@@ -4498,48 +5647,28 @@ _mark_
 // </summary>
 public class VariableDeclaration {
        public string identifier;
-       public object expression_or_array_initializer;
+       public Expression expression_or_array_initializer;
        public Location Location;
        public Attributes OptAttributes;
        public string DocComment;
 
-       public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
+       public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
        {
-               this.identifier = id;
-               this.expression_or_array_initializer = eoai;
-               this.Location = l;
+               this.identifier = lt.Value;
+               if (eoai is ArrayList) {
+                       this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
+               } else {
+                       this.expression_or_array_initializer = (Expression)eoai;
+               }
+               this.Location = lt.Location;
                this.OptAttributes = opt_attrs;
        }
 
-       public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
+       public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
        {
        }
 }
 
-/// <summary>
-///  Used to pass around interface property information
-/// </summary>
-public class InterfaceAccessorInfo {
-
-        public readonly Accessor Get, Set;
-
-        public InterfaceAccessorInfo (bool has_get, bool has_set,
-                                      Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
-        {
-               if (get_mod != 0)
-                       Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
-               if (set_mod != 0)
-                       Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
-                       
-               if (has_get)
-                       Get = new Accessor (null, 0, get_attrs, get_loc);
-               if (has_set)
-                       Set = new Accessor (null, 0, set_attrs, set_loc);
-        }
-
-}
-
-
 // <summary>
 //   A class used to hold info about an indexer declarator
 // </summary>
@@ -4547,81 +5676,113 @@ public class IndexerDeclaration {
        public Expression type;
        public MemberName interface_type;
        public Parameters param_list;
+       public Location location;
 
        public IndexerDeclaration (Expression type, MemberName interface_type,
-                                  Parameters param_list)
+                                  Parameters param_list, Location loc)
        {
                this.type = type;
                this.interface_type = interface_type;
                this.param_list = param_list;
+               this.location = loc;
        }
 }
 
 //
-// We use this when we do not have an object in advance that is an IIteratorContainer
+// We use this when we do not have an object in advance that is an IAnonymousHost
 //
-public class SimpleIteratorContainer : IIteratorContainer {
-       public bool Yields;
+public class SimpleAnonymousHost : IAnonymousHost {
+       public static readonly SimpleAnonymousHost Simple = new SimpleAnonymousHost ();
 
-       public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
+       bool yields;
+       ArrayList anonymous_methods;
 
-       //
-       // Reset and return
-       //
-       public static SimpleIteratorContainer GetSimple () { 
-               Simple.Yields = false;
+       public static SimpleAnonymousHost GetSimple () {
+               Simple.yields = false;
+               Simple.anonymous_methods = null;
                return Simple;
        }
 
-       public void SetYields () { Yields = true; } 
+       public void SetYields ()
+       {
+               yields = true;
+       }
+
+       public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
+       {
+               if (anonymous_methods == null)
+                       anonymous_methods = new ArrayList ();
+               anonymous_methods.Add (anonymous);
+       }
+
+       public void Propagate (IAnonymousHost real_host)
+       {
+               if (yields)
+                       real_host.SetYields ();
+               if (anonymous_methods != null) {
+                       foreach (AnonymousMethodExpression ame in anonymous_methods)
+                               real_host.AddAnonymousMethod (ame);
+               }
+       }
 }
 
 // <summary>
 //  A class used to hold info about an operator declarator
 // </summary>
-public class OperatorDeclaration {
-       public Operator.OpType optype;
-       public Expression ret_type, arg1type, arg2type;
-       public string arg1name, arg2name;
-       public Location location;
+struct OperatorDeclaration {
+       public readonly Operator.OpType optype;
+       public readonly Expression ret_type;
+       public readonly Location location;
 
-       public OperatorDeclaration (Operator.OpType op, Expression ret_type, 
-                                   Expression arg1type, string arg1name,
-                                   Expression arg2type, string arg2name, Location location)
+       public OperatorDeclaration (Operator.OpType op, Expression ret_type, Location location)
        {
                optype = op;
                this.ret_type = ret_type;
-               this.arg1type = arg1type;
-               this.arg1name = arg1name;
-               this.arg2type = arg2type;
-               this.arg2name = arg2name;
                this.location = location;
        }
-
 }
 
-void Error_ExpectingTypeName (Location l, Expression expr)
+void Error_ExpectingTypeName (Expression expr)
 {
        if (expr is Invocation){
-               Report.Error (1002, l, "; expected");
+               Report.Error (1002, expr.Location, "Expecting `;'");
        } else {
-               Report.Error (-1, l, "Invalid Type definition");
+               expr.Error_InvalidExpressionStatement ();
        }
 }
 
-TypeContainer pop_current_class ()
+static void Error_ParameterModifierNotValid (string modifier, Location loc)
 {
-       TypeContainer retval = current_class;
+       Report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
+                                     modifier);
+}
+
+static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
+{
+       Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
+               Parameter.GetModifierSignature (mod));
+}
+
+static void Error_TypeExpected (Location loc)
+{
+       Report.Error (1031, loc, "Type expected");
+}
+
+void push_current_class (TypeContainer tc, object partial_token)
+{
+       if (partial_token != null)
+               current_container = current_container.AddPartial (tc);
+       else
+               current_container = current_container.AddTypeContainer (tc);
+       current_class = tc;
+}
+
+DeclSpace pop_current_class ()
+{
+       DeclSpace retval = current_class;
 
        current_class = current_class.Parent;
-       current_container = current_container.Parent;
-       
-       if (current_class != current_container) {
-               if (!(current_class is ClassPart) ||
-                   ((ClassPart) current_class).PartialContainer != current_container)
-                       throw new InternalErrorException ();
-       } else if (current_container is ClassPart)
-               current_container = ((ClassPart) current_class).PartialContainer;
+       current_container = current_class.PartialContainer;
 
        return retval;
 }
@@ -4635,8 +5796,8 @@ MakeName (MemberName class_name)
 {
        Namespace ns = current_namespace.NS;
 
-       if (current_container.Name == ""){
-               if (ns.Name != "")
+       if (current_container.Name.Length == 0){
+               if (ns.Name.Length != 0)
                        return new MemberName (ns.MemberName, class_name);
                else
                        return class_name;
@@ -4666,7 +5827,7 @@ Block declare_local_variables (Expression type, ArrayList variable_declarators,
        // int j = 1;  int k = j + 1;
        //
        if (current_block.Used)
-               implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
+               implicit_block = new Block (current_block, loc, Location.Null);
        else
                implicit_block = current_block;
 
@@ -4686,23 +5847,14 @@ Block declare_local_variables (Expression type, ArrayList variable_declarators,
 
        foreach (VariableDeclaration decl in inits){
                Assign assign;
-               Expression expr;
+               Expression expr = decl.expression_or_array_initializer;
                
-               if (decl.expression_or_array_initializer is Expression){
-                       expr = (Expression) decl.expression_or_array_initializer;
-
-               } else {
-                       ArrayList init = (ArrayList) decl.expression_or_array_initializer;
-                       
-                       expr = new ArrayCreation (type, "", init, decl.Location);
-               }
-
                LocalVariableReference var;
                var = new LocalVariableReference (implicit_block, decl.identifier, loc);
 
                assign = new Assign (var, expr, decl.Location);
 
-               implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
+               implicit_block.AddStatement (new StatementExpression (assign));
        }
        
        return implicit_block;
@@ -4713,7 +5865,7 @@ Block declare_local_constants (Expression type, ArrayList declarators)
        Block implicit_block;
 
        if (current_block.Used)
-               implicit_block = new Block (current_block, Block.Flags.Implicit);
+               implicit_block = new Block (current_block);
        else
                implicit_block = current_block;
 
@@ -4724,22 +5876,19 @@ Block declare_local_constants (Expression type, ArrayList declarators)
        return implicit_block;
 }
 
-void CheckAttributeTarget (string a)
+string CheckAttributeTarget (string a, Location l)
 {
        switch (a) {
-
        case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
-               return;
-               
-       default :
-               Location l = lexer.Location;
-               Report.Error (658, l, "`" + a + "' is an invalid attribute target");
-               break;
+                       return a;
        }
 
+       Report.Warning (658, 1, l,
+                "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
+       return string.Empty;
 }
 
-void CheckUnaryOperator (Operator.OpType op)
+static bool IsUnaryOperator (Operator.OpType op)
 {
        switch (op) {
                
@@ -4749,47 +5898,11 @@ void CheckUnaryOperator (Operator.OpType op)
        case Operator.OpType.Decrement:
        case Operator.OpType.True: 
        case Operator.OpType.False: 
-       case Operator.OpType.Addition: 
-       case Operator.OpType.Subtraction:
-               
-               break;
-               
-       default :
-               Location l = lexer.Location;
-               Report.Error (1019, l, "Overloadable unary operator expected"); 
-               break;
-               
+       case Operator.OpType.UnaryPlus: 
+       case Operator.OpType.UnaryNegation:
+               return true;
        }
-}
-
-void CheckBinaryOperator (Operator.OpType op)
-{
-       switch (op) {
-               
-       case Operator.OpType.Addition: 
-       case Operator.OpType.Subtraction: 
-       case Operator.OpType.Multiply:
-       case Operator.OpType.Division:
-       case Operator.OpType.Modulus: 
-       case Operator.OpType.BitwiseAnd: 
-       case Operator.OpType.BitwiseOr:
-       case Operator.OpType.ExclusiveOr: 
-       case Operator.OpType.LeftShift: 
-       case Operator.OpType.RightShift:
-       case Operator.OpType.Equality: 
-       case Operator.OpType.Inequality:
-       case Operator.OpType.GreaterThan: 
-       case Operator.OpType.LessThan: 
-       case Operator.OpType.GreaterThanOrEqual:
-       case Operator.OpType.LessThanOrEqual:
-               break;
-               
-       default :
-               Location l = lexer.Location;
-               Report.Error (1020, l, "Overloadable binary operator expected");
-               break;
-       }
-       
+       return false;
 }
 
 void syntax_error (Location l, string msg)
@@ -4810,23 +5923,25 @@ public Tokenizer Lexer {
        }
 }                 
 
+static CSharpParser ()
+{
+       oob_stack = new Stack ();
+}
+
 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
 {
-       current_namespace = new NamespaceEntry (null, file, null, Location.Null);
        this.name = file.Name;
        this.file = file;
-       current_container = RootContext.Tree.Types;
-       // TODO: Make RootContext.Tree.Types a PartialContainer.
-       current_class = current_container;
-       current_container.NamespaceEntry = current_namespace;
-       oob_stack = new Stack ();
-       switch_stack = new Stack ();
-
+       current_namespace = new NamespaceEntry (null, file, null);
+       current_class = current_namespace.SlaveDeclSpace;
+       current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
+       oob_stack.Clear ();
        lexer = new Tokenizer (reader, file, defines);
 }
 
 public void parse ()
 {
+       int errors = Report.Errors;
        try {
                if (yacc_verbose_flag > 1)
                        yyparse (lexer, new yydebug.yyDebugSimple ());
@@ -4835,30 +5950,31 @@ public void parse ()
                Tokenizer tokenizer = lexer as Tokenizer;
                tokenizer.cleanup ();
        } catch (Exception e){
-               // 
+               //
                // Removed for production use, use parser verbose to get the output.
                //
                // Console.WriteLine (e);
-               Report.Error (-25, lexer.Location, "Parsing error");
+               if (Report.Errors == errors)
+                       Report.Error (-25, lexer.Location, "Parsing error");
                if (yacc_verbose_flag > 0)
                        Console.WriteLine (e);
        }
 
-       RootContext.Tree.Types.NamespaceEntry = null;
+       if (RootContext.ToplevelTypes.NamespaceEntry != null)
+               throw new InternalErrorException ("who set it?");
 }
 
-void CheckToken (int error, int yyToken, string msg)
+static void CheckToken (int error, int yyToken, string msg, Location loc)
 {
-       if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
-               Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
-               return;
-       }               
-       Report.Error (error, lexer.Location, msg);
+       if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
+               Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
+       else
+               Report.Error (error, loc, msg);
 }
 
-void CheckIdentifierToken (int yyToken)
+void CheckIdentifierToken (int yyToken, Location loc)
 {
-       CheckToken (1041, yyToken, "Identifier expected");
+       CheckToken (1041, yyToken, "Identifier expected", loc);
 }
 
 string ConsumeStoredComment ()
@@ -4869,5 +5985,85 @@ string ConsumeStoredComment ()
        return s;
 }
 
+Location GetLocation (object obj)
+{
+       if (obj is MemberCore)
+               return ((MemberCore) obj).Location;
+       if (obj is MemberName)
+               return ((MemberName) obj).Location;
+       if (obj is LocatedToken)
+               return ((LocatedToken) obj).Location;
+       if (obj is Location)
+               return (Location) obj;
+       return lexer.Location;
+}
+
+void start_block (Location loc)
+{
+       if (current_block == null || parsing_anonymous_method) {
+               current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
+               parsing_anonymous_method = false;
+       } else {
+               current_block = new ExplicitBlock (current_block, loc, Location.Null);
+       }
+}
+
+Block
+end_block (Location loc)
+{
+       Block retval = current_block.Explicit;
+       retval.SetEndLocation (loc);
+       current_block = retval.Parent;
+       return retval;
+}
+
+void
+start_anonymous (bool lambda, Parameters parameters, Location loc)
+{
+       oob_stack.Push (current_anonymous_method);
+       oob_stack.Push (current_local_parameters);
+
+       current_local_parameters = parameters;
+
+       ToplevelBlock top_current_block = current_block == null ? null : current_block.Toplevel;
+
+       current_anonymous_method = lambda 
+               ? new LambdaExpression (
+                       current_anonymous_method, current_generic_method, current_container,
+                       parameters, top_current_block, loc) 
+               : new AnonymousMethodExpression (
+                       current_anonymous_method, current_generic_method, current_container,
+                       parameters, top_current_block, loc);
+
+       // Force the next block to be created as a ToplevelBlock
+       parsing_anonymous_method = true;
+}
+
+/*
+ * Completes the anonymous method processing, if lambda_expr is null, this
+ * means that we have a Statement instead of an Expression embedded 
+ */
+AnonymousMethodExpression 
+end_anonymous (ToplevelBlock anon_block, Location loc)
+{
+       AnonymousMethodExpression retval;
+
+       if (RootContext.Version == LanguageVersion.ISO_1){
+               Report.FeatureIsNotAvailable (loc, "anonymous methods");
+               retval = null;
+       } else  {
+               current_anonymous_method.Block = anon_block;
+               if ((anonymous_host != null) && (current_anonymous_method.Parent == null))
+                       anonymous_host.AddAnonymousMethod (current_anonymous_method);
+
+               retval = current_anonymous_method;
+       }
+
+       current_local_parameters = (Parameters) oob_stack.Pop ();
+       current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
+
+       return retval;
+}
+
 /* end end end */
 }