2009-06-17 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / cs-parser.jay
index a7f6a1830ad44def51573592ca1c308cb3291edd..b30fc6224fb72f9df45ced19615d85b20bd2c59c 100644 (file)
@@ -119,9 +119,9 @@ namespace Mono.CSharp
                bool global_attrs_enabled = true;
                bool has_get, has_set;
                bool parameter_modifiers_not_allowed;
-               bool params_modifiers_not_allowed;
+               bool complex_parameters_modifiers_not_allowed;
                bool arglist_allowed;
-
+               bool default_parameter_used;
 
                /// When using the interactive parser, this holds the
                /// resulting expression
@@ -330,7 +330,15 @@ namespace Mono.CSharp
 %token EVAL_COMPILATION_UNIT_PARSER
 %token EVAL_USING_DECLARATIONS_UNIT_PARSER
 
+// 
+// This token is generated to trigger the completion engine at this point
+//
 %token GENERATE_COMPLETION
+
+//
+// This token is return repeatedly after the first GENERATE_COMPLETION
+// token is produced and before the final EOF
+//
 %token COMPLETE_COMPLETION
 
 /* Add precedence rules to solve dangling else s/r conflict */
@@ -1315,18 +1323,23 @@ method_header
          opt_modifiers
          PARTIAL
          VOID method_declaration_name
-         OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
+         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) $5;
-               current_local_parameters = (ParametersCompiled) $7;
+               current_local_parameters = (ParametersCompiled) $8;
 
-               if ($9 != null && name.TypeArguments == null)
+               if ($10 != null && name.TypeArguments == null)
                        Report.Error (80, lexer.Location,
                                      "Constraints are not allowed on non-generic declarations");
 
@@ -1336,7 +1349,7 @@ method_header
                        generic = new GenericMethod (current_namespace, current_class, name,
                                                     TypeManager.system_void_expr, current_local_parameters);
 
-                       generic.SetParameterInfo ((ArrayList) $10);
+                       generic.SetParameterInfo ((ArrayList) $11);
                }
 
                int modifiers = (int) $2;
@@ -1355,7 +1368,7 @@ method_header
                        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,
@@ -1413,7 +1426,7 @@ opt_parameter_list_no_mod
        ;
 
 formal_parameter_list
-       : fixed_parameters              
+       : fixed_parameters
          { 
                ArrayList pars_list = (ArrayList) $1;
 
@@ -1478,17 +1491,23 @@ fixed_parameters
        : fixed_parameter       
          {
                ArrayList pars = new ArrayList (4);
-
-               pars.Add ($1);
+               Parameter p = (Parameter) $1;
+               pars.Add (p);
+               
+               default_parameter_used = p.HasDefaultValue;
                $$ = pars;
          }
        | fixed_parameters COMMA fixed_parameter
          {
                ArrayList pars = (ArrayList) $1;
-               Parameter p = (Parameter)$3;
+               Parameter p = (Parameter) $3;
                if (p != null) {
                        if (p.HasExtensionMethodModifier)
                                Report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
+                       else if (!p.HasDefaultValue && default_parameter_used)
+                               Report.Error (1737, p.Location, "Optional parameter cannot precede required parameters");
+
+                       default_parameter_used |= p.HasDefaultValue;
                        pars.Add (p);
                }
                $$ = $1;
@@ -1516,16 +1535,11 @@ fixed_parameter
        | opt_attributes
          opt_parameter_modifier
          type
+         error
          {
-               Report.Error (1001, GetLocation ($3), "Identifier expected");
-               $$ = new Parameter ((FullNamedExpression) $3, "NeedSomeGeneratorHere", (Parameter.Modifier) $2, (Attributes) $1, lexer.Location);               
-         }
-       | opt_attributes
-         opt_parameter_modifier
-         type
-         error {
-               CheckIdentifierToken (yyToken, GetLocation ($4));
-               $$ = null;
+               Location l = GetLocation ($4);
+               CheckIdentifierToken (yyToken, l);
+               $$ = new Parameter ((FullNamedExpression) $3, "NeedSomeGeneratorHere", (Parameter.Modifier) $2, (Attributes) $1, l);
          }
        | opt_attributes
          opt_parameter_modifier
@@ -1533,11 +1547,39 @@ fixed_parameter
          IDENTIFIER
          ASSIGN
          constant_expression
-          {
+         {
+               if (RootContext.Version <= LanguageVersion.V_3) {
+                       Report.FeatureIsNotAvailable (GetLocation ($5), "optional parameter");
+               }
+               
+               Parameter.Modifier mod = (Parameter.Modifier) $2;
+               if (mod != Parameter.Modifier.NONE) {
+                       switch (mod) {
+                       case Parameter.Modifier.REF:
+                       case Parameter.Modifier.OUT:
+                               Report.Error (1741, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
+                                       Parameter.GetModifierSignature (mod));
+                               break;
+                               
+                       case Parameter.Modifier.This:
+                               Report.Error (1743, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
+                                       Parameter.GetModifierSignature (mod));
+                               break;
+                       default:
+                               throw new NotImplementedException (mod.ToString ());
+                       }
+                               
+                       mod = Parameter.Modifier.NONE;
+               }
+               
+               if (complex_parameters_modifiers_not_allowed)
+                       Report.Error (1065, GetLocation ($6), "Optional parameter is not valid in this context");
+               
                LocatedToken lt = (LocatedToken) $4;
-               Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
-                $$ = null;
-          }
+               $$ = new Parameter ((FullNamedExpression) $3, lt.Value, mod, (Attributes) $1, lt.Location);
+               if ($6 != null)
+                       ((Parameter) $$).DefaultValue = (Expression) $6;
+         }
        ;
 
 opt_parameter_modifier
@@ -1602,10 +1644,17 @@ parameter_modifier
 
 parameter_array
        : opt_attributes params_modifier type IDENTIFIER
-         { 
+         {
                LocatedToken lt = (LocatedToken) $4;
                $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
          }
+       | opt_attributes params_modifier type IDENTIFIER ASSIGN constant_expression
+         {
+               Report.Error (1751, GetLocation ($2), "Cannot specify a default value for a parameter array");
+               
+               LocatedToken lt = (LocatedToken) $4;
+               $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);            
+         }
        | opt_attributes params_modifier type error {
                CheckIdentifierToken (yyToken, GetLocation ($4));
                $$ = null;
@@ -1615,7 +1664,7 @@ parameter_array
 params_modifier
        : PARAMS
          {
-               if (params_modifiers_not_allowed)
+               if (complex_parameters_modifiers_not_allowed)
                        Report.Error (1670, ((Location) $1), "The `params' modifier is not allowed in current context");
          }
        | PARAMS parameter_modifier
@@ -1939,11 +1988,11 @@ operator_type
 operator_declarator
        : operator_type OPERATOR overloadable_operator OPEN_PARENS
          {
-               params_modifiers_not_allowed = true;
+               complex_parameters_modifiers_not_allowed = true;
          }
          opt_parameter_list_no_mod CLOSE_PARENS
          {
-               params_modifiers_not_allowed = false;
+               complex_parameters_modifiers_not_allowed = false;
 
                Location loc = (Location) $2;
                Operator.OpType op = (Operator.OpType) $3;
@@ -2014,11 +2063,11 @@ overloadable_operator
 conversion_operator_declarator
        : IMPLICIT OPERATOR type OPEN_PARENS
          {
-               params_modifiers_not_allowed = true;
+               complex_parameters_modifiers_not_allowed = true;
          }
          opt_parameter_list_no_mod CLOSE_PARENS
          {
-               params_modifiers_not_allowed = false;
+               complex_parameters_modifiers_not_allowed = false;
 
                Location loc = (Location) $2;
                current_local_parameters = (ParametersCompiled)$6;  
@@ -2032,11 +2081,11 @@ conversion_operator_declarator
          }
        | EXPLICIT OPERATOR type OPEN_PARENS
          {
-               params_modifiers_not_allowed = true;
+               complex_parameters_modifiers_not_allowed = true;
          }
          opt_parameter_list_no_mod CLOSE_PARENS
          {
-               params_modifiers_not_allowed = false;
+               complex_parameters_modifiers_not_allowed = false;
                
                Location loc = (Location) $2;
                current_local_parameters = (ParametersCompiled)$6;  
@@ -2087,9 +2136,11 @@ constructor_declarator
                        tmpComment = Lexer.consume_doc_comment ();
                        Lexer.doc_state = XmlCommentState.Allowed;
                }
+               arglist_allowed = true;
          }
          OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
          {
+               arglist_allowed = false;
                current_local_parameters = (ParametersCompiled) $6;  
                
                //
@@ -2359,8 +2410,13 @@ indexer_declaration
                implicit_value_parameter_type = (FullNamedExpression) $3;
                indexer_parameters = (ParametersCompiled) $6;
                
-               if (indexer_parameters.IsEmpty)
+               if (indexer_parameters.IsEmpty) {
                        Report.Error (1551, GetLocation ($5), "Indexers must have at least one parameter");
+               } else if (indexer_parameters.Count == 1) {
+                       Parameter p = indexer_parameters [0];
+                       if (p.HasDefaultValue)
+                               p.Warning_UselessOptionalParameter ();
+               }
 
                if (RootContext.Documentation != null) {
                        tmpComment = Lexer.consume_doc_comment ();
@@ -2593,7 +2649,9 @@ opt_nullable
          }
        | INTERR_NULLABLE
          {
-               if (RootContext.Version < LanguageVersion.ISO_2)
+               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
+                       Report.FeatureIsNotSupported (lexer.Location, "nullable types");
+               else if (RootContext.Version < LanguageVersion.ISO_2)
                        Report.FeatureIsNotAvailable (lexer.Location, "nullable types");
          
                $$ = this;
@@ -2635,7 +2693,9 @@ opt_type_argument_list
        : /* empty */                { $$ = null; } 
        | OP_GENERICS_LT type_arguments OP_GENERICS_GT
          {
-               if (RootContext.Version < LanguageVersion.ISO_2)
+               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
+                       Report.FeatureIsNotSupported (lexer.Location, "generics");
+               else if (RootContext.Version < LanguageVersion.ISO_2)
                        Report.FeatureIsNotAvailable (GetLocation ($1), "generics");      
          
                $$ = $2;
@@ -2735,7 +2795,9 @@ opt_type_parameter_list
        : /* empty */                { $$ = null; } 
        | OP_GENERICS_LT_DECL type_parameters OP_GENERICS_GT
          {
-               if (RootContext.Version < LanguageVersion.ISO_2)
+               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
+                       Report.FeatureIsNotSupported (lexer.Location, "generics");
+               else if (RootContext.Version < LanguageVersion.ISO_2)
                        Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
          
                $$ = $2;
@@ -2761,7 +2823,7 @@ type_parameter
        : opt_attributes opt_type_parameter_variance IDENTIFIER
          {
                LocatedToken lt = (LocatedToken)$3;
-               $$ = new TypeParameterName (lt.Value, (Attributes)$1, (Variance)$2, lt.Location);
+               $$ = new TypeParameterName (lt.Value, (Attributes)$1, (Variance) $2, lt.Location);
          }
        | error
          {
@@ -2830,8 +2892,7 @@ type_expression
                if ($2 != null) {
                        $$ = new ComposedCast (name.GetTypeExpression (), "?", lexer.Location);
                } else {
-                       if (name.Left == null && name.Name == "var" &&
-                           (RootContext.Version > LanguageVersion.ISO_2 || RootContext.Version == LanguageVersion.Default_MCS))
+                       if (name.Left == null && name.Name == "var" && RootContext.Version > LanguageVersion.ISO_2)
                                $$ = current_array_type = new VarExpr (name.Location);
                        else
                                $$ = name.GetTypeExpression ();
@@ -3069,12 +3130,12 @@ opt_object_or_collection_initializer
        ;
 
 object_or_collection_initializer
-       : OPEN_BRACE opt_member_initializer_list CLOSE_BRACE
+       : OPEN_BRACE opt_member_initializer_list close_brace_or_complete_completion
          {
                if ($2 == null)
-                 $$ = CollectionOrObjectInitializers.Empty;
+                       $$ = CollectionOrObjectInitializers.Empty;
                else
-                 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
+                       $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
          }
        | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
          {
@@ -3091,7 +3152,7 @@ opt_member_initializer_list
        ;
 
 member_initializer_list
-       : member_initializer
+       : member_initializer 
          {
                ArrayList a = new ArrayList ();
                a.Add ($1);
@@ -3111,9 +3172,16 @@ member_initializer
                LocatedToken lt = $1 as LocatedToken;
                $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
          }
-       | non_assignment_expression
+       | GENERATE_COMPLETION 
          {
-               $$ = new CollectionElementInitializer ((Expression)$1);
+               $$ = new CompletionElementInitializer (null, GetLocation ($1));
+         }
+       | non_assignment_expression opt_COMPLETE_COMPLETION  {
+               CompletionSimpleName csn = $1 as CompletionSimpleName;
+               if (csn == null)
+                       $$ = new CollectionElementInitializer ((Expression)$1);
+               else
+                       $$ = new CompletionElementInitializer (csn.Prefix, csn.Location);
          }
        | OPEN_BRACE expression_list CLOSE_BRACE
          {
@@ -3353,7 +3421,9 @@ new_expr_start
 anonymous_type_expression
        : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
          {
-               if (RootContext.Version <= LanguageVersion.ISO_2)
+               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
+                       Report.FeatureIsNotSupported (lexer.Location, "anonymous types");
+               else if (RootContext.Version <= LanguageVersion.ISO_2)
                        Report.FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
 
                $$ = new AnonymousTypeDeclaration ((ArrayList) $3, current_container, GetLocation ($1));
@@ -3587,7 +3657,9 @@ unbound_type_name
 generic_dimension
        : GENERIC_DIMENSION
          {
-               if (RootContext.Version < LanguageVersion.ISO_2)
+               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
+                       Report.FeatureIsNotSupported (lexer.Location, "generics");
+               else if (RootContext.Version < LanguageVersion.ISO_2)
                        Report.FeatureIsNotAvailable (lexer.Location, "generics");
 
                $$ = $1;
@@ -3658,11 +3730,11 @@ opt_anonymous_method_signature
 anonymous_method_signature
        : OPEN_PARENS
          {
-               params_modifiers_not_allowed = true; 
+               complex_parameters_modifiers_not_allowed = true; 
          }
          opt_formal_parameter_list CLOSE_PARENS
          {
-               params_modifiers_not_allowed = false;
+               complex_parameters_modifiers_not_allowed = false;
                $$ = $3;
          }
        ;
@@ -4222,8 +4294,11 @@ opt_type_parameter_variance
          }
        | type_parameter_variance
          {
-               if (RootContext.Version < LanguageVersion.Future)
-                       Report.FeatureIsNotAvailable (lexer.Location, "generic variance");
+               if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)        
+                       Report.FeatureIsNotSupported (lexer.Location, "generic type variance");
+               else if (RootContext.Version <= LanguageVersion.V_3)
+                       Report.FeatureIsNotAvailable (lexer.Location, "generic type variance");
+
                $$ = $1;
          }
        ;
@@ -4467,8 +4542,7 @@ variable_type
                        //
                        if (rank_or_nullable.Length == 0) {
                                SimpleName sn = expr as SimpleName;
-                               if (sn != null && sn.Name == "var" &&
-                                   (RootContext.Version > LanguageVersion.ISO_2 || RootContext.Version == LanguageVersion.Default_MCS))
+                               if (sn != null && sn.Name == "var" && RootContext.Version > LanguageVersion.ISO_2)
                                        $$ = current_array_type = new VarExpr (sn.Location);
                                else
                                        $$ = $1;
@@ -5726,6 +5800,11 @@ opt_COMPLETE_COMPLETION
        : /* nothing */
        | COMPLETE_COMPLETION
        ;
+
+close_brace_or_complete_completion
+       : CLOSE_BRACE
+       | COMPLETE_COMPLETION
+       ;
 %%
 
 // <summary>
@@ -6486,7 +6565,7 @@ static string GetTokenName (int token)
        case Token.WHILE:
                return "while";
        case Token.ARGLIST:
-               return "arglist";
+               return "__arglist";
        case Token.PARTIAL:
                return "partial";
        case Token.ARROW: