3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Licensed under the terms of the GNU GPL
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (C) 2004 Novell, Inc
15 // (1) Figure out why error productions dont work. `type-declaration' is a
16 // great spot to put an `error' because you can reproduce it with this input:
19 // Possible optimization:
20 // Run memory profiler with parsing only, and consider dropping
21 // arraylists where not needed. Some pieces can use linked lists.
30 using System.Collections;
35 public class CSharpParser {
36 NamespaceEntry current_namespace;
37 TypeContainer current_container;
38 DeclSpace current_class;
41 /// Current block is used to add statements as we find
46 Delegate current_delegate;
48 GenericMethod current_generic_method;
49 AnonymousMethodExpression current_anonymous_method;
52 /// This is used by the unary_expression code to resolve
53 /// a name against a parameter.
56 // FIXME: This is very ugly and it's very hard to reset it correctly
57 // on all places, especially when some parameters are autogenerated.
58 Parameters current_local_parameters;
61 /// Using during property parsing to describe the implicit
62 /// value parameter that is passed to the "set" and "get"accesor
63 /// methods (properties and indexers).
65 FullNamedExpression implicit_value_parameter_type;
66 Parameters indexer_parameters;
69 /// Hack to help create non-typed array initializer
71 public static FullNamedExpression current_array_type;
72 FullNamedExpression pushed_current_array_type;
75 /// Used to determine if we are parsing the get/set pair
76 /// of an indexer or a property
80 bool parsing_anonymous_method;
83 /// An out-of-band stack.
85 static Stack oob_stack;
93 /// Controls the verbosity of the errors produced by the parser
95 static public int yacc_verbose_flag;
98 /// Used by the interactive shell, flags whether EOF was reached
99 /// and an error was produced
101 public bool UnexpectedEOF;
104 /// The current file.
106 CompilationUnit file;
109 /// Temporary Xml documentation cache.
110 /// For enum types, we need one more temporary store.
113 string enumTypeComment;
115 /// Current attribute target
116 string current_attr_target;
118 /// assembly and module attribute definitions are enabled
119 bool global_attrs_enabled = true;
120 bool has_get, has_set;
121 bool parameter_modifiers_not_allowed;
122 bool params_modifiers_not_allowed;
123 bool arglist_allowed;
126 /// When using the interactive parser, this holds the
127 /// resulting expression
128 public object InteractiveResult;
131 // Keeps track of global data changes to undo on parser error
135 // A counter to create new class names in interactive mode
136 static int class_count;
140 %token NONE /* This token is never returned by our lexer */
141 %token ERROR // This is used not by the parser, but by the tokenizer.
145 *These are the C# keywords
243 %token INTERR_NULLABLE
246 /* Generics <,> tokens */
247 %token OP_GENERICS_LT
248 %token OP_GENERICS_LT_DECL
249 %token OP_GENERICS_GT
251 /* C# keywords which are not really keywords */
257 /* C# single character operators/punctuation. */
285 /* C# multi-character operators. */
290 %token OP_SHIFT_RIGHT
297 %token OP_MULT_ASSIGN
302 %token OP_SHIFT_LEFT_ASSIGN
303 %token OP_SHIFT_RIGHT_ASSIGN
311 %token LITERAL_INTEGER
313 %token LITERAL_DOUBLE
314 %token LITERAL_DECIMAL
315 %token LITERAL_CHARACTER
316 %token LITERAL_STRING
319 %token OPEN_PARENS_LAMBDA
320 %token OPEN_PARENS_CAST
321 %token GENERIC_DIMENSION
324 // Make the parser go into eval mode parsing (statements and compilation units).
325 %token EVAL_STATEMENT_PARSER
326 %token EVAL_COMPILATION_UNIT_PARSER
327 %token EVAL_USING_DECLARATIONS_UNIT_PARSER
329 /* Add precedence rules to solve dangling else s/r conflict */
338 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
340 %left STAR DIV PERCENT
341 %right BANG CARRET UMINUS
342 %nonassoc OP_INC OP_DEC
344 %left OPEN_BRACKET OPEN_BRACE
348 %start compilation_unit
352 : outer_declarations opt_EOF
353 | outer_declarations global_attributes opt_EOF
354 | global_attributes opt_EOF
355 | opt_EOF /* allow empty files */
356 | interactive_parsing opt_EOF
362 Lexer.check_incorrect_doc_comment ();
366 Lexer.check_incorrect_doc_comment ();
372 | outer_declarations outer_declaration
376 : extern_alias_directive
378 | namespace_member_declaration
381 extern_alias_directives
382 : extern_alias_directive
383 | extern_alias_directives extern_alias_directive
386 extern_alias_directive
387 : EXTERN_ALIAS IDENTIFIER IDENTIFIER SEMICOLON
389 LocatedToken lt = (LocatedToken) $2;
392 syntax_error (lt.Location, "`alias' expected");
393 } else if (RootContext.Version == LanguageVersion.ISO_1) {
394 Report.FeatureIsNotAvailable (lt.Location, "external alias");
396 lt = (LocatedToken) $3;
397 current_namespace.AddUsingExternalAlias (lt.Value, lt.Location);
402 syntax_error (GetLocation ($1), "`alias' expected"); // TODO: better
408 | using_directives using_directive
412 : using_alias_directive
414 if (RootContext.Documentation != null)
415 Lexer.doc_state = XmlCommentState.Allowed;
417 | using_namespace_directive
419 if (RootContext.Documentation != null)
420 Lexer.doc_state = XmlCommentState.Allowed;
424 using_alias_directive
425 : USING IDENTIFIER ASSIGN namespace_or_type_name SEMICOLON
427 LocatedToken lt = (LocatedToken) $2;
428 current_namespace.AddUsingAlias (lt.Value, (MemberName) $4, (Location) $1);
431 CheckIdentifierToken (yyToken, GetLocation ($2));
436 using_namespace_directive
437 : USING namespace_name SEMICOLON
439 current_namespace.AddUsing ((MemberName) $2, (Location) $1);
444 // Strictly speaking, namespaces don't have attributes but
445 // we parse global attributes along with namespace declarations and then
448 namespace_declaration
449 : opt_attributes NAMESPACE qualified_identifier
451 MemberName name = (MemberName) $3;
454 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
457 current_namespace = new NamespaceEntry (
458 current_namespace, file, name.GetName ());
459 current_class = current_namespace.SlaveDeclSpace;
460 current_container = current_class.PartialContainer;
462 namespace_body opt_semicolon
464 current_namespace = current_namespace.Parent;
465 current_class = current_namespace.SlaveDeclSpace;
466 current_container = current_class.PartialContainer;
473 LocatedToken lt = (LocatedToken) $1;
474 $$ = new MemberName (lt.Value, lt.Location);
476 | qualified_identifier DOT IDENTIFIER
478 LocatedToken lt = (LocatedToken) $3;
479 $$ = new MemberName ((MemberName) $1, lt.Value, lt.Location);
483 syntax_error (lexer.Location, "`.' expected");
498 : namespace_or_type_name
500 MemberName name = (MemberName) $1;
502 if (name.TypeArguments != null)
503 syntax_error (lexer.Location, "namespace name expected");
512 if (RootContext.Documentation != null)
513 Lexer.doc_state = XmlCommentState.Allowed;
519 : opt_extern_alias_directives
521 opt_namespace_member_declarations
525 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
528 | opt_extern_alias_directives
530 opt_namespace_member_declarations
533 Report.Error (1513, lexer.Location, "Expected `}'");
542 opt_extern_alias_directives
544 | extern_alias_directives
547 opt_namespace_member_declarations
549 | namespace_member_declarations
552 namespace_member_declarations
553 : namespace_member_declaration
554 | namespace_member_declarations namespace_member_declaration
557 namespace_member_declaration
561 DeclSpace ds = (DeclSpace)$1;
563 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
564 Report.Error (1527, ds.Location,
565 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
568 current_namespace.DeclarationFound = true;
570 | namespace_declaration {
571 current_namespace.DeclarationFound = true;
574 | field_declaration {
575 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
577 | method_declaration {
578 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
585 | interface_declaration
587 | delegate_declaration
589 // Enable this when we have handled all errors, because this acts as a generic fallback
592 // Console.WriteLine ("Token=" + yyToken);
593 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
605 Attributes attrs = (Attributes)$1;
606 if (global_attrs_enabled) {
607 CodeGen.Assembly.AddAttributes (attrs.Attrs);
609 foreach (Attribute a in attrs.Attrs) {
610 Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
621 global_attrs_enabled = false;
626 global_attrs_enabled = false;
635 if (current_attr_target != String.Empty) {
636 ArrayList sect = (ArrayList) $1;
638 if (global_attrs_enabled) {
639 if (current_attr_target == "module") {
640 CodeGen.Module.AddAttributes (sect);
642 } else if (current_attr_target != null && current_attr_target.Length > 0) {
643 CodeGen.Assembly.AddAttributes (sect);
646 $$ = new Attributes (sect);
649 if (RootContext.Documentation != null) {
650 Lexer.check_incorrect_doc_comment ();
652 XmlCommentState.Allowed;
656 $$ = new Attributes (sect);
661 current_attr_target = null;
663 | attribute_sections attribute_section
665 if (current_attr_target != String.Empty) {
666 Attributes attrs = $1 as Attributes;
667 ArrayList sect = (ArrayList) $2;
669 if (global_attrs_enabled) {
670 if (current_attr_target == "module") {
671 CodeGen.Module.AddAttributes (sect);
673 } else if (current_attr_target == "assembly") {
674 CodeGen.Assembly.AddAttributes (sect);
678 attrs = new Attributes (sect);
680 attrs.AddAttributes (sect);
684 attrs = new Attributes (sect);
686 attrs.AddAttributes (sect);
692 current_attr_target = null;
697 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
701 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
707 attribute_target_specifier
708 : attribute_target COLON
710 current_attr_target = (string)$1;
718 LocatedToken lt = (LocatedToken) $1;
719 $$ = CheckAttributeTarget (lt.Value, lt.Location);
721 | EVENT { $$ = "event"; }
722 | RETURN { $$ = "return"; }
725 string name = GetTokenName (yyToken);
726 $$ = CheckAttributeTarget (name, GetLocation ($1));
733 ArrayList attrs = new ArrayList (4);
739 | attribute_list COMMA attribute
741 ArrayList attrs = (ArrayList) $1;
751 ++lexer.parsing_block;
753 opt_attribute_arguments
755 --lexer.parsing_block;
756 MemberName mname = (MemberName) $1;
757 if (mname.IsGeneric) {
758 Report.Error (404, lexer.Location,
759 "'<' unexpected: attributes cannot be generic");
762 object [] arguments = (object []) $3;
763 MemberName left = mname.Left;
764 string identifier = mname.Name;
766 Expression left_expr = left == null ? null : left.GetTypeExpression ();
768 if (current_attr_target == String.Empty)
770 else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
771 // FIXME: supply "nameEscaped" parameter here.
772 $$ = new GlobalAttribute (current_namespace, current_attr_target,
773 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
775 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
780 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
783 opt_attribute_arguments
784 : /* empty */ { $$ = null; }
785 | OPEN_PARENS attribute_arguments CLOSE_PARENS
793 : opt_positional_argument_list
798 $$ = new object [] { $1, null };
801 | positional_argument_list COMMA named_argument_list
803 $$ = new object[] { $1, $3 };
805 | named_argument_list
807 $$ = new object [] { null, $1 };
812 opt_positional_argument_list
813 : /* empty */ { $$ = null; }
814 | positional_argument_list
817 positional_argument_list
820 ArrayList args = new ArrayList (4);
821 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
825 | positional_argument_list COMMA expression
827 ArrayList args = (ArrayList) $1;
828 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
837 ArrayList args = new ArrayList (4);
842 | named_argument_list COMMA named_argument
844 ArrayList args = (ArrayList) $1;
849 | named_argument_list COMMA expression
851 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
857 : IDENTIFIER ASSIGN expression
859 // FIXME: keep location
860 $$ = new DictionaryEntry (
861 ((LocatedToken) $1).Value,
862 new Argument ((Expression) $3, Argument.AType.Expression));
868 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
871 opt_class_member_declarations
873 | class_member_declarations
876 class_member_declarations
877 : class_member_declaration
878 | class_member_declarations
879 class_member_declaration
882 class_member_declaration
883 : constant_declaration // done
884 | field_declaration // done
885 | method_declaration // done
886 | property_declaration // done
887 | event_declaration // done
888 | indexer_declaration // done
889 | operator_declaration // done
890 | constructor_declaration // done
891 | destructor_declaration // done
895 Report.Error (1519, lexer.Location, "Unexpected symbol `{0}' in class, struct, or interface member declaration",
896 GetSymbolName (yyToken));
907 lexer.ConstraintsParsing = true;
909 type_declaration_name
911 MemberName name = MakeName ((MemberName) $6);
912 push_current_class (new Struct (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
915 opt_type_parameter_constraints_clauses
917 lexer.ConstraintsParsing = false;
919 current_class.SetParameterInfo ((ArrayList) $9);
921 if (RootContext.Documentation != null)
922 current_container.DocComment = Lexer.consume_doc_comment ();
926 --lexer.parsing_declaration;
927 if (RootContext.Documentation != null)
928 Lexer.doc_state = XmlCommentState.Allowed;
932 $$ = pop_current_class ();
934 | opt_attributes opt_modifiers opt_partial STRUCT error {
935 CheckIdentifierToken (yyToken, GetLocation ($5));
942 if (RootContext.Documentation != null)
943 Lexer.doc_state = XmlCommentState.Allowed;
945 opt_struct_member_declarations CLOSE_BRACE
948 opt_struct_member_declarations
950 | struct_member_declarations
953 struct_member_declarations
954 : struct_member_declaration
955 | struct_member_declarations struct_member_declaration
958 struct_member_declaration
959 : constant_declaration
962 | property_declaration
964 | indexer_declaration
965 | operator_declaration
966 | constructor_declaration
970 * This is only included so we can flag error 575:
971 * destructors only allowed on class types
973 | destructor_declaration
984 int modflags = (int) $2;
985 foreach (VariableDeclaration constant in (ArrayList) $5){
986 Location l = constant.Location;
987 if ((modflags & Modifiers.STATIC) != 0) {
988 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
992 Const c = new Const (
993 current_class, (FullNamedExpression) $4, (string) constant.identifier,
994 (Expression) constant.expression_or_array_initializer, modflags,
997 if (RootContext.Documentation != null) {
998 c.DocComment = Lexer.consume_doc_comment ();
999 Lexer.doc_state = XmlCommentState.Allowed;
1001 current_container.AddConstant (c);
1006 constant_declarators
1007 : constant_declarator
1009 ArrayList constants = new ArrayList (4);
1014 | constant_declarators COMMA constant_declarator
1017 ArrayList constants = (ArrayList) $1;
1026 ++lexer.parsing_block;
1028 constant_initializer
1030 --lexer.parsing_block;
1031 $$ = new VariableDeclaration ((LocatedToken) $1, $4);
1035 // A const field requires a value to be provided
1036 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
1041 constant_initializer
1042 : constant_expression
1050 variable_declarators
1053 FullNamedExpression type = (FullNamedExpression) $3;
1054 if (type == TypeManager.system_void_expr)
1055 Report.Error (670, GetLocation ($3), "Fields cannot have void type");
1059 current_array_type = null;
1061 foreach (VariableMemberDeclaration var in (ArrayList) $4){
1062 Field field = new Field (current_class, type, mod, var.MemberName, (Attributes) $1);
1064 field.Initializer = var.expression_or_array_initializer;
1066 if (RootContext.Documentation != null) {
1067 field.DocComment = Lexer.consume_doc_comment ();
1068 Lexer.doc_state = XmlCommentState.Allowed;
1070 current_container.AddField (field);
1071 $$ = field; // FIXME: might be better if it points to the top item
1078 fixed_variable_declarators
1081 FullNamedExpression type = (FullNamedExpression) $4;
1085 current_array_type = null;
1087 foreach (VariableDeclaration var in (ArrayList) $5) {
1088 FixedField field = new FixedField (current_class, type, mod, var.identifier,
1089 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
1091 if (RootContext.Documentation != null) {
1092 field.DocComment = Lexer.consume_doc_comment ();
1093 Lexer.doc_state = XmlCommentState.Allowed;
1095 current_container.AddField (field);
1096 $$ = field; // FIXME: might be better if it points to the top item
1105 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
1109 fixed_variable_declarators
1110 : fixed_variable_declarator
1112 ArrayList decl = new ArrayList (2);
1116 | fixed_variable_declarators COMMA fixed_variable_declarator
1118 ArrayList decls = (ArrayList) $1;
1124 fixed_variable_declarator
1125 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1127 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1129 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1131 Report.Error (443, lexer.Location, "Value or constant expected");
1132 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1137 local_variable_declarators
1138 : local_variable_declarator
1140 ArrayList decl = new ArrayList (4);
1145 | local_variable_declarators COMMA local_variable_declarator
1147 ArrayList decls = (ArrayList) $1;
1153 local_variable_declarator
1154 : IDENTIFIER ASSIGN local_variable_initializer
1156 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1160 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1162 | IDENTIFIER variable_bad_array
1168 local_variable_initializer
1171 | STACKALLOC simple_type OPEN_BRACKET expression CLOSE_BRACKET
1173 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1177 $$ = new ArglistAccess ((Location) $1);
1179 | STACKALLOC simple_type
1181 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1182 $$ = new StackAlloc ((Expression) $2, null, (Location) $1);
1186 variable_declarators
1187 : variable_declarator
1189 ArrayList decl = new ArrayList (4);
1194 | variable_declarators COMMA variable_declarator
1196 ArrayList decls = (ArrayList) $1;
1203 : member_declaration_name ASSIGN
1205 ++lexer.parsing_block;
1206 lexer.parsing_generic_declaration = false;
1208 variable_initializer
1210 --lexer.parsing_block;
1211 $$ = new VariableMemberDeclaration ((MemberName) $1, $4);
1213 | member_declaration_name
1215 lexer.parsing_generic_declaration = false;
1216 $$ = new VariableMemberDeclaration ((MemberName) $1, null);
1218 | member_declaration_name variable_bad_array
1220 lexer.parsing_generic_declaration = false;
1226 : OPEN_BRACKET opt_expression CLOSE_BRACKET
1228 Report.Error (650, GetLocation ($1), "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1229 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1233 variable_initializer
1240 if (RootContext.Documentation != null)
1241 Lexer.doc_state = XmlCommentState.NotAllowed;
1245 Method method = (Method) $1;
1246 method.Block = (ToplevelBlock) $3;
1247 current_container.AddMethod (method);
1249 if (current_container.Kind == Kind.Interface && method.Block != null) {
1250 Report.Error (531, method.Location, "`{0}': interface members cannot have a definition", method.GetSignatureForError ());
1253 current_generic_method = null;
1254 current_local_parameters = null;
1256 if (RootContext.Documentation != null)
1257 Lexer.doc_state = XmlCommentState.Allowed;
1265 method_declaration_name OPEN_PARENS
1267 arglist_allowed = true;
1269 opt_formal_parameter_list CLOSE_PARENS
1271 lexer.ConstraintsParsing = true;
1273 opt_type_parameter_constraints_clauses
1275 lexer.ConstraintsParsing = false;
1276 arglist_allowed = false;
1277 MemberName name = (MemberName) $4;
1278 current_local_parameters = (Parameters) $7;
1280 if ($10 != null && name.TypeArguments == null)
1281 Report.Error (80, lexer.Location,
1282 "Constraints are not allowed on non-generic declarations");
1286 GenericMethod generic = null;
1287 if (name.TypeArguments != null) {
1288 generic = new GenericMethod (current_namespace, current_class, name,
1289 (FullNamedExpression) $3, current_local_parameters);
1291 generic.SetParameterInfo ((ArrayList) $10);
1294 method = new Method (current_class, generic, (FullNamedExpression) $3, (int) $2,
1295 name, current_local_parameters, (Attributes) $1);
1297 current_generic_method = generic;
1299 if (RootContext.Documentation != null)
1300 method.DocComment = Lexer.consume_doc_comment ();
1307 VOID method_declaration_name
1308 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1310 lexer.ConstraintsParsing = true;
1312 opt_type_parameter_constraints_clauses
1314 lexer.ConstraintsParsing = false;
1316 MemberName name = (MemberName) $5;
1317 current_local_parameters = (Parameters) $7;
1319 if ($9 != null && name.TypeArguments == null)
1320 Report.Error (80, lexer.Location,
1321 "Constraints are not allowed on non-generic declarations");
1324 GenericMethod generic = null;
1325 if (name.TypeArguments != null) {
1326 generic = new GenericMethod (current_namespace, current_class, name,
1327 TypeManager.system_void_expr, current_local_parameters);
1329 generic.SetParameterInfo ((ArrayList) $10);
1332 int modifiers = (int) $2;
1335 const int invalid_partial_mod = Modifiers.Accessibility | Modifiers.ABSTRACT | Modifiers.EXTERN |
1336 Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
1338 if ((modifiers & invalid_partial_mod) != 0) {
1339 Report.Error (750, name.Location, "A partial method cannot define access modifier or " +
1340 "any of abstract, extern, new, override, sealed, or virtual modifiers");
1341 modifiers &= ~invalid_partial_mod;
1344 if ((current_class.ModFlags & Modifiers.PARTIAL) == 0) {
1345 Report.Error (751, name.Location, "A partial method must be declared within a " +
1346 "partial class or partial struct");
1349 modifiers |= Modifiers.PARTIAL | Modifiers.PRIVATE;
1351 method = new Method (current_class, generic, TypeManager.system_void_expr,
1352 modifiers, name, current_local_parameters, (Attributes) $1);
1354 current_generic_method = generic;
1356 if (RootContext.Documentation != null)
1357 method.DocComment = Lexer.consume_doc_comment ();
1364 modifiers method_declaration_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1366 MemberName name = (MemberName) $5;
1367 Report.Error (1585, name.Location,
1368 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1370 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1371 0, name, (Parameters) $7, (Attributes) $1);
1373 current_local_parameters = (Parameters) $7;
1375 if (RootContext.Documentation != null)
1376 method.DocComment = Lexer.consume_doc_comment ();
1384 | SEMICOLON { $$ = null; }
1387 opt_formal_parameter_list
1388 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1389 | formal_parameter_list
1392 opt_parameter_list_no_mod
1393 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1396 parameter_modifiers_not_allowed = true;
1398 formal_parameter_list
1400 parameter_modifiers_not_allowed = false;
1405 formal_parameter_list
1408 ArrayList pars_list = (ArrayList) $1;
1410 Parameter [] pars = new Parameter [pars_list.Count];
1411 pars_list.CopyTo (pars);
1413 $$ = new Parameters (pars);
1415 | fixed_parameters COMMA parameter_array
1417 ArrayList pars_list = (ArrayList) $1;
1420 Parameter [] pars = new Parameter [pars_list.Count];
1421 pars_list.CopyTo (pars);
1423 $$ = new Parameters (pars);
1425 | fixed_parameters COMMA arglist_modifier
1427 ArrayList pars_list = (ArrayList) $1;
1428 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1430 Parameter [] pars = new Parameter [pars_list.Count];
1431 pars_list.CopyTo (pars);
1433 $$ = new Parameters (pars, true);
1435 | parameter_array COMMA error
1438 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1441 | fixed_parameters COMMA parameter_array COMMA error
1444 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1447 | arglist_modifier COMMA error
1449 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1452 | fixed_parameters COMMA ARGLIST COMMA error
1454 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1459 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1463 $$ = new Parameters (new Parameter [] { new ArglistParameter ((Location) $1) }, true);
1470 ArrayList pars = new ArrayList (4);
1475 | fixed_parameters COMMA fixed_parameter
1477 ArrayList pars = (ArrayList) $1;
1478 Parameter p = (Parameter)$3;
1480 if (p.HasExtensionMethodModifier)
1481 Report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
1490 opt_parameter_modifier
1494 LocatedToken lt = (LocatedToken) $4;
1495 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1498 opt_parameter_modifier
1500 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1502 LocatedToken lt = (LocatedToken) $4;
1503 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1507 opt_parameter_modifier
1510 Report.Error (1001, GetLocation ($3), "Identifier expected");
1511 $$ = new Parameter ((FullNamedExpression) $3, "NeedSomeGeneratorHere", (Parameter.Modifier) $2, (Attributes) $1, lexer.Location);
1514 opt_parameter_modifier
1517 CheckIdentifierToken (yyToken, GetLocation ($4));
1521 opt_parameter_modifier
1527 LocatedToken lt = (LocatedToken) $4;
1528 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1533 opt_parameter_modifier
1534 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1535 | parameter_modifiers
1539 : parameter_modifier
1543 | parameter_modifiers parameter_modifier
1545 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1546 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1547 if (((Parameter.Modifier)$1 & p2) == p2) {
1548 Error_DuplicateParameterModifier (lexer.Location, p2);
1550 switch (mod & ~Parameter.Modifier.This) {
1551 case Parameter.Modifier.REF:
1552 Report.Error (1101, lexer.Location, "The parameter modifiers `this' and `ref' cannot be used altogether");
1554 case Parameter.Modifier.OUT:
1555 Report.Error (1102, lexer.Location, "The parameter modifiers `this' and `out' cannot be used altogether");
1558 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1569 if (parameter_modifiers_not_allowed)
1570 Error_ParameterModifierNotValid ("ref", (Location)$1);
1572 $$ = Parameter.Modifier.REF;
1576 if (parameter_modifiers_not_allowed)
1577 Error_ParameterModifierNotValid ("out", (Location)$1);
1579 $$ = Parameter.Modifier.OUT;
1583 if (parameter_modifiers_not_allowed)
1584 Error_ParameterModifierNotValid ("this", (Location)$1);
1586 if (RootContext.Version <= LanguageVersion.ISO_2)
1587 Report.FeatureIsNotAvailable (GetLocation ($1), "extension methods");
1589 $$ = Parameter.Modifier.This;
1594 : opt_attributes params_modifier type IDENTIFIER
1596 LocatedToken lt = (LocatedToken) $4;
1597 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1599 | opt_attributes params_modifier type error {
1600 CheckIdentifierToken (yyToken, GetLocation ($4));
1608 if (params_modifiers_not_allowed)
1609 Report.Error (1670, ((Location) $1), "The `params' modifier is not allowed in current context");
1611 | PARAMS parameter_modifier
1613 Parameter.Modifier mod = (Parameter.Modifier)$2;
1614 if ((mod & Parameter.Modifier.This) != 0) {
1615 Report.Error (1104, (Location)$1, "The parameter modifiers `this' and `params' cannot be used altogether");
1617 Report.Error (1611, (Location)$1, "The params parameter cannot be declared as ref or out");
1620 | PARAMS params_modifier
1622 Error_DuplicateParameterModifier ((Location)$1, Parameter.Modifier.PARAMS);
1629 if (!arglist_allowed)
1630 Report.Error (1669, (Location) $1, "__arglist is not valid in this context");
1634 property_declaration
1638 member_declaration_name
1640 if (RootContext.Documentation != null)
1641 tmpComment = Lexer.consume_doc_comment ();
1645 implicit_value_parameter_type = (FullNamedExpression) $3;
1646 lexer.PropertyParsing = true;
1648 accessor_declarations
1650 lexer.PropertyParsing = false;
1651 has_get = has_set = false;
1656 Accessors accessors = (Accessors) $8;
1657 Accessor get_block = accessors != null ? accessors.get_or_add : null;
1658 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
1659 bool order = accessors != null ? accessors.declared_in_reverse : false;
1661 MemberName name = (MemberName) $4;
1662 FullNamedExpression ptype = (FullNamedExpression) $3;
1664 prop = new Property (current_class, ptype, (int) $2,
1665 name, (Attributes) $1, get_block, set_block, order, current_block);
1667 if (ptype == TypeManager.system_void_expr)
1668 Report.Error (547, name.Location, "`{0}': property or indexer cannot have void type", prop.GetSignatureForError ());
1670 if (accessors == null)
1671 Report.Error (548, prop.Location, "`{0}': property or indexer must have at least one accessor", prop.GetSignatureForError ());
1673 if (current_container.Kind == Kind.Interface) {
1674 if (prop.Get.Block != null)
1675 Report.Error (531, prop.Location, "`{0}.get': interface members cannot have a definition", prop.GetSignatureForError ());
1677 if (prop.Set.Block != null)
1678 Report.Error (531, prop.Location, "`{0}.set': interface members cannot have a definition", prop.GetSignatureForError ());
1681 current_container.AddProperty (prop);
1682 implicit_value_parameter_type = null;
1684 if (RootContext.Documentation != null)
1685 prop.DocComment = ConsumeStoredComment ();
1690 accessor_declarations
1691 : get_accessor_declaration
1693 $$ = new Accessors ((Accessor) $1, null);
1695 | get_accessor_declaration accessor_declarations
1697 Accessors accessors = (Accessors) $2;
1698 accessors.get_or_add = (Accessor) $1;
1701 | set_accessor_declaration
1703 $$ = new Accessors (null, (Accessor) $1);
1705 | set_accessor_declaration accessor_declarations
1707 Accessors accessors = (Accessors) $2;
1708 accessors.set_or_remove = (Accessor) $1;
1709 accessors.declared_in_reverse = true;
1714 if (yyToken == Token.CLOSE_BRACE) {
1717 if (yyToken == Token.SEMICOLON)
1718 Report.Error (1597, lexer.Location, "Semicolon after method or accessor block is not valid");
1720 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1722 $$ = new Accessors (null, null);
1727 get_accessor_declaration
1728 : opt_attributes opt_modifiers GET
1730 // If this is not the case, then current_local_parameters has already
1731 // been set in indexer_declaration
1732 if (parsing_indexer == false)
1733 current_local_parameters = null;
1735 current_local_parameters = indexer_parameters;
1736 lexer.PropertyParsing = false;
1741 Report.Error (1007, (Location) $3, "Property accessor already defined");
1744 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, current_local_parameters, (Location) $3);
1746 current_local_parameters = null;
1747 lexer.PropertyParsing = true;
1749 if (RootContext.Documentation != null)
1750 if (Lexer.doc_state == XmlCommentState.Error)
1751 Lexer.doc_state = XmlCommentState.NotAllowed;
1757 set_accessor_declaration
1758 : opt_attributes opt_modifiers SET
1760 Parameter implicit_value_parameter = new Parameter (
1761 implicit_value_parameter_type, "value",
1762 Parameter.Modifier.NONE, null, (Location) $3);
1764 if (!parsing_indexer) {
1765 current_local_parameters = new Parameters (new Parameter [] { implicit_value_parameter });
1767 current_local_parameters = Parameters.MergeGenerated (
1768 indexer_parameters, true, implicit_value_parameter, null);
1771 lexer.PropertyParsing = false;
1776 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1779 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, current_local_parameters, (Location) $3);
1781 current_local_parameters = null;
1782 lexer.PropertyParsing = true;
1784 if (RootContext.Documentation != null
1785 && Lexer.doc_state == XmlCommentState.Error)
1786 Lexer.doc_state = XmlCommentState.NotAllowed;
1800 Error_SyntaxError (1043, yyToken);
1805 interface_declaration
1811 lexer.ConstraintsParsing = true;
1813 type_declaration_name
1815 MemberName name = MakeName ((MemberName) $6);
1816 push_current_class (new Interface (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
1819 opt_type_parameter_constraints_clauses
1821 lexer.ConstraintsParsing = false;
1823 current_class.SetParameterInfo ((ArrayList) $9);
1825 if (RootContext.Documentation != null) {
1826 current_container.DocComment = Lexer.consume_doc_comment ();
1827 Lexer.doc_state = XmlCommentState.Allowed;
1832 --lexer.parsing_declaration;
1833 if (RootContext.Documentation != null)
1834 Lexer.doc_state = XmlCommentState.Allowed;
1838 $$ = pop_current_class ();
1840 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1841 CheckIdentifierToken (yyToken, GetLocation ($5));
1847 opt_interface_member_declarations
1851 opt_interface_member_declarations
1853 | interface_member_declarations
1856 interface_member_declarations
1857 : interface_member_declaration
1858 | interface_member_declarations interface_member_declaration
1861 interface_member_declaration
1862 : constant_declaration
1864 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1868 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1870 | method_declaration
1871 | property_declaration
1873 | indexer_declaration
1874 | operator_declaration
1876 Report.Error (567, GetLocation ($1), "Interfaces cannot contain operators");
1878 | constructor_declaration
1880 Report.Error (526, GetLocation ($1), "Interfaces cannot contain contructors");
1884 Report.Error (524, GetLocation ($1), "Interfaces cannot declare classes, structs, interfaces, delegates, or enumerations");
1888 operator_declaration
1889 : opt_attributes opt_modifiers operator_declarator
1897 OperatorDeclaration decl = (OperatorDeclaration) $3;
1898 Operator op = new Operator (
1899 current_class, decl.optype, decl.ret_type, (int) $2,
1900 current_local_parameters,
1901 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1903 if (RootContext.Documentation != null) {
1904 op.DocComment = tmpComment;
1905 Lexer.doc_state = XmlCommentState.Allowed;
1908 // Note again, checking is done in semantic analysis
1909 current_container.AddOperator (op);
1911 current_local_parameters = null;
1917 | SEMICOLON { $$ = null; }
1921 : type_expression_or_array
1924 Report.Error (590, lexer.Location, "User-defined operators cannot return void");
1925 $$ = TypeManager.system_void_expr;
1930 : operator_type OPERATOR overloadable_operator OPEN_PARENS
1932 params_modifiers_not_allowed = true;
1934 opt_parameter_list_no_mod CLOSE_PARENS
1936 params_modifiers_not_allowed = false;
1938 Location loc = (Location) $2;
1939 Operator.OpType op = (Operator.OpType) $3;
1940 current_local_parameters = (Parameters)$6;
1942 int p_count = current_local_parameters.Count;
1944 if (op == Operator.OpType.Addition)
1945 op = Operator.OpType.UnaryPlus;
1946 else if (op == Operator.OpType.Subtraction)
1947 op = Operator.OpType.UnaryNegation;
1950 if (IsUnaryOperator (op)) {
1952 Report.Error (1020, loc, "Overloadable binary operator expected");
1953 } else if (p_count != 1) {
1954 Report.Error (1535, loc, "Overloaded unary operator `{0}' takes one parameter",
1955 Operator.GetName (op));
1959 Report.Error (1534, loc, "Overloaded binary operator `{0}' takes two parameters",
1960 Operator.GetName (op));
1961 } else if (p_count != 2) {
1962 Report.Error (1019, loc, "Overloadable unary operator expected");
1966 if (RootContext.Documentation != null) {
1967 tmpComment = Lexer.consume_doc_comment ();
1968 Lexer.doc_state = XmlCommentState.NotAllowed;
1971 $$ = new OperatorDeclaration (op, (FullNamedExpression) $1, loc);
1973 | conversion_operator_declarator
1976 overloadable_operator
1978 : BANG { $$ = Operator.OpType.LogicalNot; }
1979 | TILDE { $$ = Operator.OpType.OnesComplement; }
1980 | OP_INC { $$ = Operator.OpType.Increment; }
1981 | OP_DEC { $$ = Operator.OpType.Decrement; }
1982 | TRUE { $$ = Operator.OpType.True; }
1983 | FALSE { $$ = Operator.OpType.False; }
1984 // Unary and binary:
1985 | PLUS { $$ = Operator.OpType.Addition; }
1986 | MINUS { $$ = Operator.OpType.Subtraction; }
1988 | STAR { $$ = Operator.OpType.Multiply; }
1989 | DIV { $$ = Operator.OpType.Division; }
1990 | PERCENT { $$ = Operator.OpType.Modulus; }
1991 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1992 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1993 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1994 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1995 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1996 | OP_EQ { $$ = Operator.OpType.Equality; }
1997 | OP_NE { $$ = Operator.OpType.Inequality; }
1998 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1999 | OP_LT { $$ = Operator.OpType.LessThan; }
2000 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2001 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2004 conversion_operator_declarator
2005 : IMPLICIT OPERATOR type OPEN_PARENS
2007 params_modifiers_not_allowed = true;
2009 opt_parameter_list_no_mod CLOSE_PARENS
2011 params_modifiers_not_allowed = false;
2013 Location loc = (Location) $2;
2014 current_local_parameters = (Parameters)$6;
2016 if (RootContext.Documentation != null) {
2017 tmpComment = Lexer.consume_doc_comment ();
2018 Lexer.doc_state = XmlCommentState.NotAllowed;
2021 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (FullNamedExpression) $3, loc);
2023 | EXPLICIT OPERATOR type OPEN_PARENS
2025 params_modifiers_not_allowed = true;
2027 opt_parameter_list_no_mod CLOSE_PARENS
2029 params_modifiers_not_allowed = false;
2031 Location loc = (Location) $2;
2032 current_local_parameters = (Parameters)$6;
2034 if (RootContext.Documentation != null) {
2035 tmpComment = Lexer.consume_doc_comment ();
2036 Lexer.doc_state = XmlCommentState.NotAllowed;
2039 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (FullNamedExpression) $3, loc);
2043 syntax_error ((Location) $1, "'operator' expected");
2047 syntax_error ((Location) $1, "'operator' expected");
2051 constructor_declaration
2052 : constructor_declarator
2055 Constructor c = (Constructor) $1;
2056 c.Block = (ToplevelBlock) $2;
2058 if (RootContext.Documentation != null)
2059 c.DocComment = ConsumeStoredComment ();
2061 current_container.AddConstructor (c);
2063 current_local_parameters = null;
2064 if (RootContext.Documentation != null)
2065 Lexer.doc_state = XmlCommentState.Allowed;
2069 constructor_declarator
2074 if (RootContext.Documentation != null) {
2075 tmpComment = Lexer.consume_doc_comment ();
2076 Lexer.doc_state = XmlCommentState.Allowed;
2079 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2081 current_local_parameters = (Parameters) $6;
2084 // start block here, so possible anonymous methods inside
2085 // constructor initializer can get correct parent block
2087 start_block (lexer.Location);
2089 opt_constructor_initializer
2091 LocatedToken lt = (LocatedToken) $3;
2092 int mods = (int) $2;
2093 ConstructorInitializer ci = (ConstructorInitializer) $9;
2095 Constructor c = new Constructor (current_class, lt.Value, mods,
2096 (Attributes) $1, current_local_parameters, ci, lt.Location);
2098 if (lt.Value != current_container.MemberName.Name) {
2099 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
2100 } else if ((mods & Modifiers.STATIC) != 0) {
2101 if ((mods & Modifiers.Accessibility) != 0){
2102 Report.Error (515, c.Location,
2103 "`{0}': static constructor cannot have an access modifier",
2104 c.GetSignatureForError ());
2107 Report.Error (514, c.Location,
2108 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2109 c.GetSignatureForError ());
2120 | SEMICOLON { current_block = null; $$ = null; }
2123 opt_constructor_initializer
2125 | constructor_initializer
2128 constructor_initializer
2129 : COLON BASE OPEN_PARENS
2131 ++lexer.parsing_block;
2133 opt_argument_list CLOSE_PARENS
2135 --lexer.parsing_block;
2136 $$ = new ConstructorBaseInitializer ((ArrayList) $5, (Location) $2);
2138 | COLON THIS OPEN_PARENS
2140 ++lexer.parsing_block;
2142 opt_argument_list CLOSE_PARENS
2144 --lexer.parsing_block;
2145 $$ = new ConstructorThisInitializer ((ArrayList) $5, (Location) $2);
2148 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2153 destructor_declaration
2154 : opt_attributes opt_modifiers TILDE
2156 if (RootContext.Documentation != null) {
2157 tmpComment = Lexer.consume_doc_comment ();
2158 Lexer.doc_state = XmlCommentState.NotAllowed;
2161 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2163 LocatedToken lt = (LocatedToken) $5;
2164 if (lt.Value != current_container.MemberName.Name){
2165 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2166 } else if (current_container.Kind != Kind.Class){
2167 Report.Error (575, lt.Location, "Only class types can contain destructor");
2169 Method d = new Destructor (
2170 current_class, TypeManager.system_void_expr, (int) $2, "Finalize",
2171 Parameters.EmptyReadOnlyParameters, (Attributes) $1, lt.Location);
2172 if (RootContext.Documentation != null)
2173 d.DocComment = ConsumeStoredComment ();
2175 d.Block = (ToplevelBlock) $8;
2176 current_container.AddMethod (d);
2184 EVENT type variable_declarators SEMICOLON
2186 current_array_type = null;
2187 foreach (VariableMemberDeclaration var in (ArrayList) $5) {
2189 EventField e = new EventField (
2190 current_class, (FullNamedExpression) $4, (int) $2, var.MemberName, (Attributes) $1);
2192 if (var.expression_or_array_initializer != null) {
2193 if (current_container.Kind == Kind.Interface) {
2194 Report.Error (68, e.Location, "`{0}': event in interface cannot have initializer", e.GetSignatureForError ());
2197 e.Initializer = var.expression_or_array_initializer;
2200 if (var.MemberName.Left != null) {
2201 Report.Error (71, e.Location,
2202 "`{0}': An explicit interface implementation of an event must use property syntax",
2203 e.GetSignatureForError ());
2206 current_container.AddEvent (e);
2208 if (RootContext.Documentation != null) {
2209 e.DocComment = Lexer.consume_doc_comment ();
2210 Lexer.doc_state = XmlCommentState.Allowed;
2216 EVENT type member_declaration_name
2219 implicit_value_parameter_type = (FullNamedExpression) $4;
2220 current_local_parameters = new Parameters (
2221 new Parameter (implicit_value_parameter_type, "value",
2222 Parameter.Modifier.NONE, null, GetLocation ($3)));
2224 lexer.EventParsing = true;
2226 event_accessor_declarations
2228 lexer.EventParsing = false;
2232 MemberName name = (MemberName) $5;
2234 if (current_container.Kind == Kind.Interface) {
2235 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
2239 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2240 current_container.Name, name.GetSignatureForError ());
2243 Accessors accessors = (Accessors) $8;
2245 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2246 // CS0073 is already reported, so no CS0065 here.
2249 Event e = new EventProperty (
2250 current_class, (FullNamedExpression) $4, (int) $2, name,
2251 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2252 if (RootContext.Documentation != null) {
2253 e.DocComment = Lexer.consume_doc_comment ();
2254 Lexer.doc_state = XmlCommentState.Allowed;
2257 current_container.AddEvent (e);
2258 implicit_value_parameter_type = null;
2261 current_local_parameters = null;
2263 | opt_attributes opt_modifiers EVENT type member_declaration_name error
2265 MemberName mn = (MemberName) $5;
2266 if (mn.Left != null)
2267 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2269 if (RootContext.Documentation != null)
2270 Lexer.doc_state = XmlCommentState.Allowed;
2272 Error_SyntaxError (yyToken);
2277 event_accessor_declarations
2278 : add_accessor_declaration remove_accessor_declaration
2280 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2282 | remove_accessor_declaration add_accessor_declaration
2284 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2285 accessors.declared_in_reverse = true;
2288 | add_accessor_declaration { $$ = null; }
2289 | remove_accessor_declaration { $$ = null; }
2292 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2298 add_accessor_declaration
2299 : opt_attributes ADD
2301 lexer.EventParsing = false;
2305 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, (Location) $2);
2306 lexer.EventParsing = true;
2309 | opt_attributes ADD error {
2310 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2313 | opt_attributes modifiers ADD {
2314 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2319 remove_accessor_declaration
2320 : opt_attributes REMOVE
2322 lexer.EventParsing = false;
2326 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, (Location) $2);
2327 lexer.EventParsing = true;
2329 | opt_attributes REMOVE error {
2330 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2333 | opt_attributes modifiers REMOVE {
2334 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2340 : opt_attributes opt_modifiers
2341 member_type indexer_declaration_name OPEN_BRACKET opt_parameter_list_no_mod CLOSE_BRACKET
2344 implicit_value_parameter_type = (FullNamedExpression) $3;
2345 indexer_parameters = (Parameters) $6;
2347 if (indexer_parameters.IsEmpty)
2348 Report.Error (1551, GetLocation ($5), "Indexers must have at least one parameter");
2350 if (RootContext.Documentation != null) {
2351 tmpComment = Lexer.consume_doc_comment ();
2352 Lexer.doc_state = XmlCommentState.Allowed;
2355 lexer.PropertyParsing = true;
2356 parsing_indexer = true;
2359 accessor_declarations
2361 lexer.PropertyParsing = false;
2362 has_get = has_set = false;
2363 parsing_indexer = false;
2367 Accessors accessors = (Accessors) $10;
2368 Accessor get_block = accessors != null ? accessors.get_or_add : null;
2369 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
2370 bool order = accessors != null ? accessors.declared_in_reverse : false;
2372 Indexer indexer = new Indexer (current_class, (FullNamedExpression) $3,
2373 (MemberName)$4, (int) $2, (Parameters) $6, (Attributes) $1,
2374 get_block, set_block, order);
2376 if ($3 == TypeManager.system_void_expr)
2377 Report.Error (620, GetLocation ($3), "`{0}': indexer return type cannot be `void'", indexer.GetSignatureForError ());
2379 if (accessors == null)
2380 Report.Error (548, indexer.Location, "`{0}': property or indexer must have at least one accessor", indexer.GetSignatureForError ());
2382 if (current_container.Kind == Kind.Interface) {
2383 if (indexer.Get.Block != null)
2384 Report.Error (531, indexer.Location, "`{0}.get': interface members cannot have a definition", indexer.GetSignatureForError ());
2386 if (indexer.Set.Block != null)
2387 Report.Error (531, indexer.Location, "`{0}.set': interface members cannot have a definition", indexer.GetSignatureForError ());
2390 if (RootContext.Documentation != null)
2391 indexer.DocComment = ConsumeStoredComment ();
2393 current_container.AddIndexer (indexer);
2395 current_local_parameters = null;
2396 implicit_value_parameter_type = null;
2397 indexer_parameters = null;
2404 ENUM type_declaration_name
2406 if (RootContext.Documentation != null)
2407 enumTypeComment = Lexer.consume_doc_comment ();
2412 MemberName name = (MemberName) $4;
2413 if (name.IsGeneric) {
2414 Report.Error (1675, name.Location, "Enums cannot have type parameters");
2417 name = MakeName (name);
2418 Enum e = new Enum (current_namespace, current_class, (TypeExpr) $5, (int) $2,
2419 name, (Attributes) $1);
2421 if (RootContext.Documentation != null)
2422 e.DocComment = enumTypeComment;
2425 EnumMember em = null;
2426 foreach (VariableDeclaration ev in (ArrayList) $7) {
2427 em = new EnumMember (
2428 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2429 ev.OptAttributes, ev.Location);
2431 // if (RootContext.Documentation != null)
2432 em.DocComment = ev.DocComment;
2434 e.AddEnumMember (em);
2436 if (RootContext.EvalMode)
2437 undo.AddTypeContainer (current_container, e);
2439 current_container.AddTypeContainer (e);
2447 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2450 if ($2 != TypeManager.system_int32_expr && $2 != TypeManager.system_uint32_expr &&
2451 $2 != TypeManager.system_int64_expr && $2 != TypeManager.system_uint64_expr &&
2452 $2 != TypeManager.system_int16_expr && $2 != TypeManager.system_uint16_expr &&
2453 $2 != TypeManager.system_byte_expr && $2 != TypeManager.system_sbyte_expr)
2454 Enum.Error_1008 (GetLocation ($2));
2460 Error_TypeExpected (lexer.Location);
2467 if (RootContext.Documentation != null)
2468 Lexer.doc_state = XmlCommentState.Allowed;
2470 opt_enum_member_declarations
2472 // here will be evaluated after CLOSE_BLACE is consumed.
2473 if (RootContext.Documentation != null)
2474 Lexer.doc_state = XmlCommentState.Allowed;
2482 opt_enum_member_declarations
2483 : /* empty */ { $$ = new ArrayList (4); }
2484 | enum_member_declarations opt_comma { $$ = $1; }
2487 enum_member_declarations
2488 : enum_member_declaration
2490 ArrayList l = new ArrayList (4);
2495 | enum_member_declarations COMMA enum_member_declaration
2497 ArrayList l = (ArrayList) $1;
2505 enum_member_declaration
2506 : opt_attributes IDENTIFIER
2508 VariableDeclaration vd = new VariableDeclaration (
2509 (LocatedToken) $2, null, (Attributes) $1);
2511 if (RootContext.Documentation != null) {
2512 vd.DocComment = Lexer.consume_doc_comment ();
2513 Lexer.doc_state = XmlCommentState.Allowed;
2518 | opt_attributes IDENTIFIER
2520 ++lexer.parsing_block;
2521 if (RootContext.Documentation != null) {
2522 tmpComment = Lexer.consume_doc_comment ();
2523 Lexer.doc_state = XmlCommentState.NotAllowed;
2526 ASSIGN constant_expression
2528 --lexer.parsing_block;
2529 VariableDeclaration vd = new VariableDeclaration (
2530 (LocatedToken) $2, $5, (Attributes) $1);
2532 if (RootContext.Documentation != null)
2533 vd.DocComment = ConsumeStoredComment ();
2539 delegate_declaration
2543 member_type type_declaration_name
2544 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2546 MemberName name = MakeName ((MemberName) $5);
2547 Parameters p = (Parameters) $7;
2549 Delegate del = new Delegate (current_namespace, current_class, (FullNamedExpression) $4,
2550 (int) $2, name, p, (Attributes) $1);
2552 if (RootContext.Documentation != null) {
2553 del.DocComment = Lexer.consume_doc_comment ();
2554 Lexer.doc_state = XmlCommentState.Allowed;
2557 current_container.AddDelegate (del);
2558 current_delegate = del;
2559 lexer.ConstraintsParsing = true;
2561 opt_type_parameter_constraints_clauses
2563 lexer.ConstraintsParsing = false;
2567 current_delegate.SetParameterInfo ((ArrayList) $10);
2568 $$ = current_delegate;
2570 current_delegate = null;
2581 if (RootContext.Version < LanguageVersion.ISO_2)
2582 Report.FeatureIsNotAvailable (lexer.Location, "nullable types");
2588 namespace_or_type_name
2590 | qualified_alias_member IDENTIFIER opt_type_argument_list
2592 LocatedToken lt1 = (LocatedToken) $1;
2593 LocatedToken lt2 = (LocatedToken) $2;
2595 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2601 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list
2603 LocatedToken lt = (LocatedToken) $3;
2604 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2609 : IDENTIFIER opt_type_argument_list
2611 LocatedToken lt = (LocatedToken) $1;
2612 $$ = new MemberName (lt.Value, (TypeArguments)$2, lt.Location);
2617 // Generics arguments (any type, without attributes)
2619 opt_type_argument_list
2620 : /* empty */ { $$ = null; }
2621 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2623 if (RootContext.Version < LanguageVersion.ISO_2)
2624 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2628 | OP_GENERICS_LT error
2630 Error_TypeExpected (lexer.Location);
2631 $$ = new TypeArguments (lexer.Location);
2638 TypeArguments type_args = new TypeArguments (lexer.Location);
2639 type_args.Add ((Expression) $1);
2642 | type_arguments COMMA type
2644 TypeArguments type_args = (TypeArguments) $1;
2645 type_args.Add ((Expression) $3);
2651 // Generics parameters (identifiers only, with attributes), used in type or method declarations
2653 type_declaration_name
2656 lexer.parsing_generic_declaration = true;
2658 opt_type_parameter_list
2660 lexer.parsing_generic_declaration = false;
2661 LocatedToken lt = (LocatedToken) $1;
2662 $$ = new MemberName (lt.Value, (TypeArguments)$3, lt.Location);
2666 member_declaration_name
2667 : method_declaration_name
2669 MemberName mn = (MemberName)$1;
2670 if (mn.TypeArguments != null)
2671 syntax_error (mn.Location, string.Format ("Member `{0}' cannot declare type arguments",
2672 mn.GetSignatureForError ()));
2676 method_declaration_name
2677 : type_declaration_name
2678 | explicit_interface IDENTIFIER opt_type_parameter_list
2680 lexer.parsing_generic_declaration = false;
2681 LocatedToken lt = (LocatedToken) $2;
2682 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $3, lt.Location);
2686 indexer_declaration_name
2689 lexer.parsing_generic_declaration = false;
2690 $$ = new MemberName (TypeContainer.DefaultIndexerName, GetLocation ($1));
2692 | explicit_interface THIS
2694 lexer.parsing_generic_declaration = false;
2695 $$ = new MemberName ((MemberName) $1, TypeContainer.DefaultIndexerName, null, GetLocation ($1));
2700 : IDENTIFIER opt_type_argument_list DOT
2702 LocatedToken lt = (LocatedToken) $1;
2703 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2705 | qualified_alias_member IDENTIFIER opt_type_argument_list DOT
2707 LocatedToken lt1 = (LocatedToken) $1;
2708 LocatedToken lt2 = (LocatedToken) $2;
2710 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2712 | explicit_interface IDENTIFIER opt_type_argument_list DOT
2714 LocatedToken lt = (LocatedToken) $2;
2715 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $3, lt.Location);
2719 opt_type_parameter_list
2720 : /* empty */ { $$ = null; }
2721 | OP_GENERICS_LT_DECL type_parameters OP_GENERICS_GT
2723 if (RootContext.Version < LanguageVersion.ISO_2)
2724 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2733 TypeArguments type_args = new TypeArguments (lexer.Location);
2734 type_args.Add ((Expression)$1);
2737 | type_parameters COMMA type_parameter
2739 TypeArguments type_args = (TypeArguments) $1;
2740 type_args.Add ((Expression)$3);
2746 : opt_attributes IDENTIFIER
2748 LocatedToken lt = (LocatedToken)$2;
2749 $$ = new TypeParameterName (lt.Value, (Attributes)$1, lt.Location);
2753 if (GetTokenName (yyToken) == "type")
2754 Report.Error (81, GetLocation ($1), "Type parameter declaration must be an identifier not a type");
2756 Error_SyntaxError (yyToken);
2758 $$ = new TypeParameterName ("", null, lexer.Location);
2763 // All types where void is allowed
2766 : type_expression_or_array
2769 $$ = TypeManager.system_void_expr;
2776 lexer.parsing_generic_declaration = true;
2781 // A type which does not allow `void' to be used
2784 : type_expression_or_array
2787 Expression.Error_VoidInvalidInTheContext (lexer.Location);
2788 $$ = TypeManager.system_void_expr;
2796 Expression.Error_VoidInvalidInTheContext (lexer.Location);
2797 $$ = TypeManager.system_void_expr;
2801 type_expression_or_array
2803 | type_expression rank_specifiers
2805 string rank_specifiers = (string) $2;
2806 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank_specifiers);
2811 : namespace_or_type_name opt_nullable
2813 MemberName name = (MemberName) $1;
2816 $$ = new ComposedCast (name.GetTypeExpression (), "?", lexer.Location);
2818 if (name.Left == null && name.Name == "var" &&
2819 (RootContext.Version > LanguageVersion.ISO_2 || RootContext.Version == LanguageVersion.Default_MCS))
2820 $$ = current_array_type = new VarExpr (name.Location);
2822 $$ = name.GetTypeExpression ();
2825 | builtin_types opt_nullable
2828 $$ = new ComposedCast ((FullNamedExpression) $1, "?", lexer.Location);
2830 | type_expression STAR
2833 // Note that here only unmanaged types are allowed but we
2834 // can't perform checks during this phase - we do it during
2835 // semantic analysis.
2837 $$ = new ComposedCast ((FullNamedExpression) $1, "*", Lexer.Location);
2841 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2848 ArrayList types = new ArrayList (2);
2852 | type_list COMMA base_type_name
2854 ArrayList types = (ArrayList) $1;
2863 if ($1 is ComposedCast)
2864 Report.Error (1521, GetLocation ($1), "Invalid base type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
2869 Error_TypeExpected (lexer.Location);
2874 * replaces all the productions for isolating the various
2875 * simple types, but we need this to reuse it easily in local_variable_type
2878 : OBJECT { $$ = TypeManager.system_object_expr; }
2879 | STRING { $$ = TypeManager.system_string_expr; }
2880 | BOOL { $$ = TypeManager.system_boolean_expr; }
2881 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2882 | FLOAT { $$ = TypeManager.system_single_expr; }
2883 | DOUBLE { $$ = TypeManager.system_double_expr; }
2888 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2889 | BYTE { $$ = TypeManager.system_byte_expr; }
2890 | SHORT { $$ = TypeManager.system_int16_expr; }
2891 | USHORT { $$ = TypeManager.system_uint16_expr; }
2892 | INT { $$ = TypeManager.system_int32_expr; }
2893 | UINT { $$ = TypeManager.system_uint32_expr; }
2894 | LONG { $$ = TypeManager.system_int64_expr; }
2895 | ULONG { $$ = TypeManager.system_uint64_expr; }
2896 | CHAR { $$ = TypeManager.system_char_expr; }
2903 $$ = TypeManager.system_void_expr;
2908 // Expressions, section 7.5
2913 : primary_expression_no_array_creation
2914 | array_creation_expression
2917 primary_expression_no_array_creation
2919 | IDENTIFIER opt_type_argument_list
2921 LocatedToken lt = (LocatedToken) $1;
2922 $$ = new SimpleName (MemberName.MakeName (lt.Value, (TypeArguments)$2), (TypeArguments)$2, lt.Location);
2924 | parenthesized_expression
2925 | default_value_expression
2927 | invocation_expression
2931 | post_increment_expression
2932 | post_decrement_expression
2933 | object_or_delegate_creation_expression
2934 | anonymous_type_expression
2937 | checked_expression
2938 | unchecked_expression
2939 | pointer_member_access
2940 | anonymous_method_expression
2947 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2948 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2949 | NULL { $$ = new NullLiteral (lexer.Location); }
2953 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2954 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2955 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2960 object v = lexer.Value;
2963 $$ = new IntLiteral ((int) v, lexer.Location);
2964 } else if (v is uint)
2965 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2967 $$ = new LongLiteral ((Int64) v, lexer.Location);
2968 else if (v is ulong)
2969 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2971 Console.WriteLine ("OOPS. Unexpected result from scanner");
2976 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2977 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2982 // Here is the trick, tokenizer may think that parens is a special but
2983 // parser is interested in open parens only, so we merge them.
2984 // Consider: if (a)foo ();
2989 | OPEN_PARENS_LAMBDA
2992 parenthesized_expression
2993 : OPEN_PARENS expression CLOSE_PARENS
2995 $$ = new ParenthesizedExpression ((Expression) $2);
3000 : primary_expression DOT IDENTIFIER opt_type_argument_list
3002 LocatedToken lt = (LocatedToken) $3;
3003 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3005 | predefined_type DOT IDENTIFIER opt_type_argument_list
3007 LocatedToken lt = (LocatedToken) $3;
3008 // TODO: Location is wrong as some predefined types doesn't hold a location
3009 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3011 | qualified_alias_member IDENTIFIER opt_type_argument_list
3013 LocatedToken lt1 = (LocatedToken) $1;
3014 LocatedToken lt2 = (LocatedToken) $2;
3016 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
3020 invocation_expression
3021 : primary_expression open_parens_any opt_argument_list CLOSE_PARENS
3023 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
3027 opt_object_or_collection_initializer
3028 : /* empty */ { $$ = null; }
3029 | object_or_collection_initializer
3032 object_or_collection_initializer
3033 : OPEN_BRACE opt_member_initializer_list CLOSE_BRACE
3036 $$ = CollectionOrObjectInitializers.Empty;
3038 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3040 | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3042 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3046 opt_member_initializer_list
3047 : /* empty */ { $$ = null; }
3048 | member_initializer_list
3054 member_initializer_list
3055 : member_initializer
3057 ArrayList a = new ArrayList ();
3061 | member_initializer_list COMMA member_initializer
3063 ArrayList a = (ArrayList)$1;
3070 : IDENTIFIER ASSIGN initializer_value
3072 LocatedToken lt = $1 as LocatedToken;
3073 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3075 | non_assignment_expression
3077 $$ = new CollectionElementInitializer ((Expression)$1);
3079 | OPEN_BRACE expression_list CLOSE_BRACE
3081 $$ = new CollectionElementInitializer ((ArrayList)$2, GetLocation ($1));
3083 | OPEN_BRACE CLOSE_BRACE
3085 Report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3091 | object_or_collection_initializer
3095 : /* empty */ { $$ = null; }
3102 ArrayList list = new ArrayList (4);
3106 | argument_list COMMA argument
3108 ArrayList list = (ArrayList) $1;
3112 | argument_list COMMA
3114 Report.Error (839, GetLocation ($2), "An argument is missing");
3119 Report.Error (839, GetLocation ($1), "An argument is missing");
3127 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3129 | non_simple_argument
3136 : REF variable_reference
3138 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3140 | OUT variable_reference
3142 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3144 | ARGLIST open_parens_any argument_list CLOSE_PARENS
3146 ArrayList list = (ArrayList) $3;
3147 Argument[] args = new Argument [list.Count];
3148 list.CopyTo (args, 0);
3150 Expression expr = new Arglist (args, (Location) $1);
3151 $$ = new Argument (expr, Argument.AType.Expression);
3153 | ARGLIST open_parens_any CLOSE_PARENS
3155 $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
3159 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3164 : expression { note ("section 5.4"); $$ = $1; }
3168 : primary_expression_no_array_creation OPEN_BRACKET expression_list CLOSE_BRACKET
3170 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3172 | array_creation_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3174 // LAMESPEC: Not allowed according to specification
3175 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3177 | primary_expression_no_array_creation rank_specifiers
3179 // So the super-trick is that primary_expression
3180 // can only be either a SimpleName or a MemberAccess.
3181 // The MemberAccess case arises when you have a fully qualified type-name like :
3183 // SimpleName is when you have
3186 Expression expr = (Expression) $1;
3187 if (expr is ComposedCast){
3188 $$ = new ComposedCast ((ComposedCast)expr, (string) $2);
3189 } else if (expr is ATypeNameExpression){
3191 // So we extract the string corresponding to the SimpleName
3194 $$ = new ComposedCast ((ATypeNameExpression)expr, (string) $2);
3196 Error_ExpectingTypeName (expr);
3197 $$ = TypeManager.system_object_expr;
3200 current_array_type = (FullNamedExpression)$$;
3207 ArrayList list = new ArrayList (4);
3211 | expression_list COMMA expression
3213 ArrayList list = (ArrayList) $1;
3222 $$ = new This (current_block, (Location) $1);
3227 : BASE DOT IDENTIFIER opt_type_argument_list
3229 LocatedToken lt = (LocatedToken) $3;
3230 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3232 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3234 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3237 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3242 post_increment_expression
3243 : primary_expression OP_INC
3245 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3246 (Expression) $1, (Location) $2);
3250 post_decrement_expression
3251 : primary_expression OP_DEC
3253 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3254 (Expression) $1, (Location) $2);
3258 object_or_delegate_creation_expression
3259 : NEW simple_type open_parens_any opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3262 if (RootContext.Version <= LanguageVersion.ISO_2)
3263 Report.FeatureIsNotAvailable (GetLocation ($1), "object initializers");
3265 $$ = new NewInitialize ((Expression) $2, (ArrayList) $4, (CollectionOrObjectInitializers) $6, (Location) $1);
3268 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3270 | NEW simple_type object_or_collection_initializer
3272 if (RootContext.Version <= LanguageVersion.ISO_2)
3273 Report.FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
3275 $$ = new NewInitialize ((Expression) $2, null, (CollectionOrObjectInitializers) $3, (Location) $1);
3280 array_creation_expression
3281 : NEW simple_type OPEN_BRACKET expression_list CLOSE_BRACKET
3282 opt_rank_specifier // shift/reduce on OPEN_BRACE
3283 opt_array_initializer
3285 $$ = new ArrayCreation ((FullNamedExpression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3287 | NEW simple_type rank_specifiers opt_array_initializer
3290 Report.Error (1586, GetLocation ($1), "Array creation must have array size or array initializer");
3292 $$ = new ArrayCreation ((FullNamedExpression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3294 | NEW rank_specifiers array_initializer
3296 $$ = new ImplicitlyTypedArrayCreation ((string) $2, (ArrayList) $3, (Location) $1);
3298 | NEW simple_type error
3300 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3305 anonymous_type_expression
3306 : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
3308 if (RootContext.Version <= LanguageVersion.ISO_2)
3309 Report.FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
3311 $$ = new AnonymousTypeDeclaration ((ArrayList) $3, current_container, GetLocation ($1));
3315 anonymous_type_parameters_opt_comma
3316 : anonymous_type_parameters_opt
3317 | anonymous_type_parameters COMMA
3320 anonymous_type_parameters_opt
3322 | anonymous_type_parameters
3325 anonymous_type_parameters
3326 : anonymous_type_parameter
3328 ArrayList a = new ArrayList (4);
3332 | anonymous_type_parameters COMMA anonymous_type_parameter
3334 ArrayList a = (ArrayList) $1;
3340 anonymous_type_parameter
3341 : IDENTIFIER ASSIGN variable_initializer
3343 LocatedToken lt = (LocatedToken)$1;
3344 $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
3348 LocatedToken lt = (LocatedToken)$1;
3349 $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
3350 lt.Value, lt.Location);
3352 | BASE DOT IDENTIFIER opt_type_argument_list
3354 LocatedToken lt = (LocatedToken) $3;
3355 BaseAccess ba = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3356 $$ = new AnonymousTypeParameter (ba, lt.Value, lt.Location);
3360 MemberAccess ma = (MemberAccess) $1;
3361 $$ = new AnonymousTypeParameter (ma, ma.Name, ma.Location);
3365 Report.Error (746, lexer.Location, "Invalid anonymous type member declarator. " +
3366 "Anonymous type members must be a member assignment, simple name or member access expression");
3381 opt_rank_specifier_or_nullable
3389 | opt_nullable rank_specifiers
3392 $$ = "?" + (string) $2;
3400 | rank_specifier rank_specifiers
3402 $$ = (string) $2 + (string) $1;
3407 : OPEN_BRACKET CLOSE_BRACKET
3411 | OPEN_BRACKET dim_separators CLOSE_BRACKET
3413 $$ = "[" + (string) $2 + "]";
3415 | OPEN_BRACKET error CLOSE_BRACKET
3417 ArrayCreation.Error_IncorrectArrayInitializer (GetLocation ($1));
3427 | dim_separators COMMA
3429 $$ = (string) $1 + ",";
3433 opt_array_initializer
3445 : OPEN_BRACE CLOSE_BRACE
3447 ArrayList list = new ArrayList (4);
3450 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3452 $$ = (ArrayList) $2;
3456 variable_initializer_list
3457 : variable_initializer
3459 ArrayList list = new ArrayList (4);
3463 | variable_initializer_list COMMA variable_initializer
3465 ArrayList list = (ArrayList) $1;
3471 Error_SyntaxError (yyToken);
3472 $$ = new ArrayList ();
3479 pushed_current_array_type = current_array_type;
3480 lexer.TypeOfParsing = true;
3482 open_parens_any typeof_type_expression CLOSE_PARENS
3484 lexer.TypeOfParsing = false;
3485 Expression type = (Expression)$4;
3486 if (type == TypeManager.system_void_expr)
3487 $$ = new TypeOfVoid ((Location) $1);
3489 $$ = new TypeOf (type, (Location) $1);
3490 current_array_type = pushed_current_array_type;
3494 typeof_type_expression
3501 $$ = new UnboundTypeExpression ((MemberName)$1, lexer.Location);
3505 Error_TypeExpected (lexer.Location);
3511 : IDENTIFIER generic_dimension
3513 LocatedToken lt = (LocatedToken) $1;
3514 TypeArguments ta = (TypeArguments)$2;
3516 $$ = new MemberName (lt.Value, ta, lt.Location);
3518 | qualified_alias_member IDENTIFIER generic_dimension
3520 LocatedToken lt1 = (LocatedToken) $1;
3521 LocatedToken lt2 = (LocatedToken) $2;
3522 TypeArguments ta = (TypeArguments)$3;
3524 $$ = new MemberName (new MemberName (lt1.Value, lt1.Location), lt2.Value, ta, lt2.Location);
3526 | unbound_type_name DOT IDENTIFIER generic_dimension
3528 LocatedToken lt = (LocatedToken) $3;
3529 TypeArguments ta = (TypeArguments)$4;
3531 $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
3533 | namespace_or_type_name DOT IDENTIFIER generic_dimension
3535 LocatedToken lt = (LocatedToken) $3;
3536 TypeArguments ta = (TypeArguments)$4;
3538 $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
3545 if (RootContext.Version < LanguageVersion.ISO_2)
3546 Report.FeatureIsNotAvailable (lexer.Location, "generics");
3548 $$ = new TypeArguments ((int)$1, lexer.Location);
3552 qualified_alias_member
3553 : IDENTIFIER DOUBLE_COLON
3555 LocatedToken lt = (LocatedToken) $1;
3556 if (RootContext.Version == LanguageVersion.ISO_1)
3557 Report.FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
3564 : SIZEOF open_parens_any type CLOSE_PARENS {
3565 $$ = new SizeOf ((Expression) $3, (Location) $1);
3570 : CHECKED open_parens_any expression CLOSE_PARENS
3572 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3576 unchecked_expression
3577 : UNCHECKED open_parens_any expression CLOSE_PARENS
3579 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3583 pointer_member_access
3584 : primary_expression OP_PTR IDENTIFIER
3587 LocatedToken lt = (LocatedToken) $3;
3589 deref = new Indirection ((Expression) $1, lt.Location);
3590 $$ = new MemberAccess (deref, lt.Value);
3594 anonymous_method_expression
3595 : DELEGATE opt_anonymous_method_signature
3597 start_anonymous (false, (Parameters) $2, (Location) $1);
3601 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
3605 opt_anonymous_method_signature
3606 : /* empty */ { $$ = null; }
3607 | anonymous_method_signature
3610 anonymous_method_signature
3613 params_modifiers_not_allowed = true;
3615 opt_formal_parameter_list CLOSE_PARENS
3617 params_modifiers_not_allowed = false;
3622 default_value_expression
3623 : DEFAULT open_parens_any type CLOSE_PARENS
3625 if (RootContext.Version < LanguageVersion.ISO_2)
3626 Report.FeatureIsNotAvailable (lexer.Location, "default value expression");
3628 $$ = new DefaultValueExpression ((Expression) $3, GetLocation ($1));
3633 : primary_expression
3634 | BANG prefixed_unary_expression
3636 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3638 | TILDE prefixed_unary_expression
3640 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3646 : OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression
3648 $$ = new Cast ((Expression) $2, (Expression) $4, GetLocation ($1));
3650 | OPEN_PARENS predefined_type CLOSE_PARENS prefixed_unary_expression
3652 $$ = new Cast ((Expression) $2, (Expression) $4, GetLocation ($1));
3657 // The idea to split this out is from Rhys' grammar
3658 // to solve the problem with casts.
3660 prefixed_unary_expression
3662 | PLUS prefixed_unary_expression
3664 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3666 | MINUS prefixed_unary_expression
3668 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3670 | OP_INC prefixed_unary_expression
3672 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3673 (Expression) $2, (Location) $1);
3675 | OP_DEC prefixed_unary_expression
3677 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3678 (Expression) $2, (Location) $1);
3680 | STAR prefixed_unary_expression
3682 $$ = new Indirection ((Expression) $2, (Location) $1);
3684 | BITWISE_AND prefixed_unary_expression
3686 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3690 multiplicative_expression
3691 : prefixed_unary_expression
3692 | multiplicative_expression STAR prefixed_unary_expression
3694 $$ = new Binary (Binary.Operator.Multiply,
3695 (Expression) $1, (Expression) $3);
3697 | multiplicative_expression DIV prefixed_unary_expression
3699 $$ = new Binary (Binary.Operator.Division,
3700 (Expression) $1, (Expression) $3);
3702 | multiplicative_expression PERCENT prefixed_unary_expression
3704 $$ = new Binary (Binary.Operator.Modulus,
3705 (Expression) $1, (Expression) $3);
3710 : multiplicative_expression
3711 | additive_expression PLUS multiplicative_expression
3713 $$ = new Binary (Binary.Operator.Addition,
3714 (Expression) $1, (Expression) $3);
3716 | additive_expression MINUS multiplicative_expression
3718 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3720 | parenthesized_expression MINUS multiplicative_expression
3722 // Shift/Reduce conflict
3723 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3725 | additive_expression AS type
3727 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3729 | additive_expression IS type
3731 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3736 : additive_expression
3737 | shift_expression OP_SHIFT_LEFT additive_expression
3739 $$ = new Binary (Binary.Operator.LeftShift,
3740 (Expression) $1, (Expression) $3);
3742 | shift_expression OP_SHIFT_RIGHT additive_expression
3744 $$ = new Binary (Binary.Operator.RightShift,
3745 (Expression) $1, (Expression) $3);
3749 relational_expression
3751 | relational_expression OP_LT shift_expression
3753 $$ = new Binary (Binary.Operator.LessThan,
3754 (Expression) $1, (Expression) $3);
3756 | relational_expression OP_GT shift_expression
3758 $$ = new Binary (Binary.Operator.GreaterThan,
3759 (Expression) $1, (Expression) $3);
3761 | relational_expression OP_LE shift_expression
3763 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3764 (Expression) $1, (Expression) $3);
3766 | relational_expression OP_GE shift_expression
3768 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3769 (Expression) $1, (Expression) $3);
3774 : relational_expression
3775 | equality_expression OP_EQ relational_expression
3777 $$ = new Binary (Binary.Operator.Equality,
3778 (Expression) $1, (Expression) $3);
3780 | equality_expression OP_NE relational_expression
3782 $$ = new Binary (Binary.Operator.Inequality,
3783 (Expression) $1, (Expression) $3);
3788 : equality_expression
3789 | and_expression BITWISE_AND equality_expression
3791 $$ = new Binary (Binary.Operator.BitwiseAnd,
3792 (Expression) $1, (Expression) $3);
3796 exclusive_or_expression
3798 | exclusive_or_expression CARRET and_expression
3800 $$ = new Binary (Binary.Operator.ExclusiveOr,
3801 (Expression) $1, (Expression) $3);
3805 inclusive_or_expression
3806 : exclusive_or_expression
3807 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3809 $$ = new Binary (Binary.Operator.BitwiseOr,
3810 (Expression) $1, (Expression) $3);
3814 conditional_and_expression
3815 : inclusive_or_expression
3816 | conditional_and_expression OP_AND inclusive_or_expression
3818 $$ = new Binary (Binary.Operator.LogicalAnd,
3819 (Expression) $1, (Expression) $3);
3823 conditional_or_expression
3824 : conditional_and_expression
3825 | conditional_or_expression OP_OR conditional_and_expression
3827 $$ = new Binary (Binary.Operator.LogicalOr,
3828 (Expression) $1, (Expression) $3);
3832 conditional_expression
3833 : conditional_or_expression
3834 | conditional_or_expression INTERR expression COLON expression
3836 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3838 | conditional_or_expression OP_COALESCING expression
3840 if (RootContext.Version < LanguageVersion.ISO_2)
3841 Report.FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
3843 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, lexer.Location);
3845 | primary_expression INTERR_NULLABLE opt_rank_specifier
3848 // TODO: Is this the best place to handle `= (foo?)a;' ?
3850 if (RootContext.Version < LanguageVersion.ISO_2)
3851 Report.FeatureIsNotAvailable (GetLocation ($1), "nullable types");
3853 $$ = new ComposedCast ((FullNamedExpression) $1, "?" + (string)$3, lexer.Location);
3857 assignment_expression
3858 : prefixed_unary_expression ASSIGN expression
3860 $$ = new SimpleAssign ((Expression) $1, (Expression) $3);
3862 | prefixed_unary_expression OP_MULT_ASSIGN expression
3864 $$ = new CompoundAssign (
3865 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3867 | prefixed_unary_expression OP_DIV_ASSIGN expression
3869 $$ = new CompoundAssign (
3870 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3872 | prefixed_unary_expression OP_MOD_ASSIGN expression
3874 $$ = new CompoundAssign (
3875 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3877 | prefixed_unary_expression OP_ADD_ASSIGN expression
3879 $$ = new CompoundAssign (
3880 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3882 | prefixed_unary_expression OP_SUB_ASSIGN expression
3884 $$ = new CompoundAssign (
3885 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3887 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3889 $$ = new CompoundAssign (
3890 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3892 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3894 $$ = new CompoundAssign (
3895 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3897 | prefixed_unary_expression OP_AND_ASSIGN expression
3899 $$ = new CompoundAssign (
3900 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3902 | prefixed_unary_expression OP_OR_ASSIGN expression
3904 $$ = new CompoundAssign (
3905 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3907 | prefixed_unary_expression OP_XOR_ASSIGN expression
3909 $$ = new CompoundAssign (
3910 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3914 lambda_parameter_list
3917 ArrayList pars = new ArrayList (4);
3922 | lambda_parameter_list COMMA lambda_parameter
3924 ArrayList pars = (ArrayList) $1;
3925 Parameter p = (Parameter)$3;
3926 if (pars[0].GetType () != p.GetType ()) {
3927 Report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
3936 : parameter_modifier type IDENTIFIER
3938 LocatedToken lt = (LocatedToken) $3;
3940 $$ = new Parameter ((FullNamedExpression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3944 LocatedToken lt = (LocatedToken) $2;
3946 $$ = new Parameter ((FullNamedExpression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3950 LocatedToken lt = (LocatedToken) $1;
3951 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
3955 opt_lambda_parameter_list
3956 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
3957 | lambda_parameter_list {
3958 ArrayList pars_list = (ArrayList) $1;
3959 $$ = new Parameters ((Parameter[])pars_list.ToArray (typeof (Parameter)));
3963 lambda_expression_body
3965 start_block (lexer.Location);
3969 Block b = end_block (lexer.Location);
3970 b.AddStatement (new ContextualReturn ((Expression) $2));
3981 LocatedToken lt = (LocatedToken) $1;
3982 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
3983 start_anonymous (true, new Parameters (p), (Location) $2);
3985 lambda_expression_body
3987 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
3989 | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW
3991 start_anonymous (true, (Parameters) $2, (Location) $4);
3993 lambda_expression_body
3995 $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
4000 : assignment_expression
4001 | non_assignment_expression
4004 non_assignment_expression
4005 : conditional_expression
4027 lexer.ConstraintsParsing = true;
4029 type_declaration_name
4031 MemberName name = MakeName ((MemberName) $6);
4032 push_current_class (new Class (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
4035 opt_type_parameter_constraints_clauses
4037 lexer.ConstraintsParsing = false;
4039 current_class.SetParameterInfo ((ArrayList) $9);
4041 if (RootContext.Documentation != null) {
4042 current_container.DocComment = Lexer.consume_doc_comment ();
4043 Lexer.doc_state = XmlCommentState.Allowed;
4048 --lexer.parsing_declaration;
4049 if (RootContext.Documentation != null)
4050 Lexer.doc_state = XmlCommentState.Allowed;
4054 $$ = pop_current_class ();
4062 { $$ = $1; } // location
4066 : /* empty */ { $$ = (int) 0; }
4072 | modifiers modifier
4077 if ((m1 & m2) != 0) {
4078 Location l = lexer.Location;
4079 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
4081 $$ = (int) (m1 | m2);
4089 if (current_container == RootContext.ToplevelTypes)
4090 Report.Error (1530, lexer.Location, "Keyword `new' is not allowed on namespace elements");
4092 | PUBLIC { $$ = Modifiers.PUBLIC; }
4093 | PROTECTED { $$ = Modifiers.PROTECTED; }
4094 | INTERNAL { $$ = Modifiers.INTERNAL; }
4095 | PRIVATE { $$ = Modifiers.PRIVATE; }
4096 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
4097 | SEALED { $$ = Modifiers.SEALED; }
4098 | STATIC { $$ = Modifiers.STATIC; }
4099 | READONLY { $$ = Modifiers.READONLY; }
4100 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
4101 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
4102 | EXTERN { $$ = Modifiers.EXTERN; }
4103 | VOLATILE { $$ = Modifiers.VOLATILE; }
4104 | UNSAFE { $$ = Modifiers.UNSAFE; }
4113 : COLON type_list { current_container.AddBasesForPart (current_class, (ArrayList) $2); }
4116 opt_type_parameter_constraints_clauses
4117 : /* empty */ { $$ = null; }
4118 | type_parameter_constraints_clauses
4122 type_parameter_constraints_clauses
4123 : type_parameter_constraints_clause {
4124 ArrayList constraints = new ArrayList (1);
4125 constraints.Add ($1);
4128 | type_parameter_constraints_clauses type_parameter_constraints_clause {
4129 ArrayList constraints = (ArrayList) $1;
4130 Constraints new_constraint = (Constraints)$2;
4132 foreach (Constraints c in constraints) {
4133 if (new_constraint.TypeParameter == c.TypeParameter) {
4134 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
4135 new_constraint.TypeParameter);
4139 constraints.Add (new_constraint);
4144 type_parameter_constraints_clause
4145 : WHERE IDENTIFIER COLON type_parameter_constraints {
4146 LocatedToken lt = (LocatedToken) $2;
4147 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
4151 type_parameter_constraints
4152 : type_parameter_constraint {
4153 ArrayList constraints = new ArrayList (1);
4154 constraints.Add ($1);
4157 | type_parameter_constraints COMMA type_parameter_constraint {
4158 ArrayList constraints = (ArrayList) $1;
4160 constraints.Add ($3);
4165 type_parameter_constraint
4167 | NEW OPEN_PARENS CLOSE_PARENS {
4168 $$ = SpecialConstraint.Constructor;
4171 $$ = SpecialConstraint.ReferenceType;
4174 $$ = SpecialConstraint.ValueType;
4183 // A block is "contained" on the following places:
4185 // property_declaration as part of the accessor body (get/set)
4186 // operator_declaration
4187 // constructor_declaration
4188 // destructor_declaration
4189 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4194 ++lexer.parsing_block;
4195 start_block ((Location) $1);
4197 opt_statement_list CLOSE_BRACE
4199 --lexer.parsing_block;
4200 $$ = end_block ((Location) $4);
4207 ++lexer.parsing_block;
4208 current_block.StartLocation = GetLocation ($1);
4210 opt_statement_list CLOSE_BRACE
4212 --lexer.parsing_block;
4213 $$ = end_block ((Location) $4);
4224 | statement_list statement
4228 : declaration_statement
4230 if ($1 != null && (Block) $1 != current_block){
4231 current_block.AddStatement ((Statement) $1);
4232 current_block = (Block) $1;
4235 | valid_declaration_statement
4237 current_block.AddStatement ((Statement) $1);
4243 // The interactive_statement and its derivatives are only
4244 // used to provide a special version of `expression_statement'
4245 // that has a side effect of assigning the expression to
4248 interactive_statement_list
4249 : interactive_statement
4250 | interactive_statement_list interactive_statement
4253 interactive_statement
4254 : declaration_statement
4256 if ($1 != null && (Block) $1 != current_block){
4257 current_block.AddStatement ((Statement) $1);
4258 current_block = (Block) $1;
4261 | interactive_valid_declaration_statement
4263 current_block.AddStatement ((Statement) $1);
4268 valid_declaration_statement
4271 | expression_statement
4272 | selection_statement
4273 | iteration_statement
4277 | unchecked_statement
4284 interactive_valid_declaration_statement
4287 | interactive_expression_statement
4288 | selection_statement
4289 | iteration_statement
4293 | unchecked_statement
4301 : valid_declaration_statement
4302 | declaration_statement
4304 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4309 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4317 $$ = EmptyStatement.Value;
4324 LocatedToken lt = (LocatedToken) $1;
4325 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4327 if (current_block.AddLabel (labeled))
4328 current_block.AddStatement (labeled);
4333 declaration_statement
4334 : local_variable_declaration SEMICOLON
4336 current_array_type = null;
4338 DictionaryEntry de = (DictionaryEntry) $1;
4339 Expression e = (Expression) de.Key;
4341 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4345 | local_constant_declaration SEMICOLON
4347 current_array_type = null;
4349 DictionaryEntry de = (DictionaryEntry) $1;
4351 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4357 * The following is from Rhys' grammar:
4358 * > Types in local variable declarations must be recognized as
4359 * > expressions to prevent reduce/reduce errors in the grammar.
4360 * > The expressions are converted into types during semantic analysis.
4363 : primary_expression_no_array_creation opt_rank_specifier_or_nullable
4365 // FIXME: Do something smart here regarding the composition of the type.
4367 // Ok, the above "primary_expression" is there to get rid of
4368 // both reduce/reduce and shift/reduces in the grammar, it should
4369 // really just be "type_name". If you use type_name, a reduce/reduce
4370 // creeps up. If you use namespace_or_type_name (which is all we need
4371 // really) two shift/reduces appear.
4374 // So the super-trick is that primary_expression
4375 // can only be either a SimpleName or a MemberAccess.
4376 // The MemberAccess case arises when you have a fully qualified type-name like :
4378 // SimpleName is when you have
4381 Expression expr = (Expression) $1;
4382 string rank_or_nullable = (string) $2;
4384 if (expr is ComposedCast){
4385 $$ = new ComposedCast ((ComposedCast)expr, rank_or_nullable);
4386 } else if (expr is ATypeNameExpression){
4388 // So we extract the string corresponding to the SimpleName
4391 if (rank_or_nullable.Length == 0) {
4392 SimpleName sn = expr as SimpleName;
4393 if (sn != null && sn.Name == "var" &&
4394 (RootContext.Version > LanguageVersion.ISO_2 || RootContext.Version == LanguageVersion.Default_MCS))
4395 $$ = current_array_type = new VarExpr (sn.Location);
4399 $$ = new ComposedCast ((ATypeNameExpression)expr, rank_or_nullable);
4402 Error_ExpectingTypeName (expr);
4403 $$ = TypeManager.system_object_expr;
4406 | builtin_types opt_rank_specifier_or_nullable
4408 if ((string) $2 == "")
4411 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, (string) $2, lexer.Location);
4413 | VOID opt_rank_specifier
4415 Expression.Error_VoidInvalidInTheContext (lexer.Location);
4416 $$ = TypeManager.system_void_expr;
4420 local_variable_pointer_type
4421 : primary_expression_no_array_creation STAR
4423 ATypeNameExpression expr = $1 as ATypeNameExpression;
4426 $$ = new ComposedCast (expr, "*");
4428 Error_ExpectingTypeName ((Expression)$1);
4432 | builtin_types STAR
4434 $$ = new ComposedCast ((FullNamedExpression) $1, "*", lexer.Location);
4438 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4440 | local_variable_pointer_type STAR
4442 $$ = new ComposedCast ((FullNamedExpression) $1, "*");
4446 local_variable_declaration
4447 : local_variable_type local_variable_declarators
4450 VarExpr ve = $1 as VarExpr;
4452 ve.VariableInitializer = (ArrayList)$2;
4454 $$ = new DictionaryEntry ($1, $2);
4458 | local_variable_pointer_type opt_rank_specifier local_variable_declarators
4463 if ((string) $2 == "")
4464 t = (Expression) $1;
4466 t = new ComposedCast ((FullNamedExpression) $1, (string) $2);
4467 $$ = new DictionaryEntry (t, $3);
4473 local_constant_declaration
4474 : CONST local_variable_type constant_declarators
4477 $$ = new DictionaryEntry ($2, $3);
4483 expression_statement
4484 : statement_expression SEMICOLON { $$ = $1; }
4487 interactive_expression_statement
4488 : interactive_statement_expression SEMICOLON { $$ = $1; }
4492 // We have to do the wrapping here and not in the case above,
4493 // because statement_expression is used for example in for_statement
4495 statement_expression
4498 Expression expr = (Expression) $1;
4499 ExpressionStatement s;
4501 s = expr as ExpressionStatement;
4504 expr.Error_InvalidExpressionStatement ();
4507 $$ = new StatementExpression (s);
4512 Error_SyntaxError (yyToken);
4517 interactive_statement_expression
4520 Expression expr = (Expression) $1;
4521 ExpressionStatement s;
4523 s = new OptionalAssign (new SimpleName ("$retval", lexer.Location), expr, lexer.Location);
4524 $$ = new StatementExpression (s);
4528 Error_SyntaxError (yyToken);
4539 : IF open_parens_any boolean_expression CLOSE_PARENS
4542 Location l = (Location) $1;
4544 $$ = new If ((Expression) $3, (Statement) $5, l);
4546 // FIXME: location for warning should be loc property of $5.
4547 if ($5 == EmptyStatement.Value)
4548 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4551 | IF open_parens_any boolean_expression CLOSE_PARENS
4552 embedded_statement ELSE embedded_statement
4554 Location l = (Location) $1;
4556 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4558 // FIXME: location for warning should be loc property of $5 and $7.
4559 if ($5 == EmptyStatement.Value)
4560 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4561 if ($7 == EmptyStatement.Value)
4562 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4567 : SWITCH open_parens_any
4569 if (switch_stack == null)
4570 switch_stack = new Stack (2);
4571 switch_stack.Push (current_block);
4573 expression CLOSE_PARENS
4576 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4577 current_block = (Block) switch_stack.Pop ();
4593 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4594 $$ = new ArrayList ();
4602 ArrayList sections = new ArrayList (4);
4607 | switch_sections switch_section
4609 ArrayList sections = (ArrayList) $1;
4619 current_block = current_block.CreateSwitchBlock (lexer.Location);
4623 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4630 ArrayList labels = new ArrayList (4);
4635 | switch_labels switch_label
4637 ArrayList labels = (ArrayList) ($1);
4645 : CASE constant_expression COLON
4647 $$ = new SwitchLabel ((Expression) $2, (Location) $1);
4651 $$ = new SwitchLabel (null, (Location) $1);
4663 : WHILE open_parens_any boolean_expression CLOSE_PARENS embedded_statement
4665 Location l = (Location) $1;
4666 $$ = new While ((Expression) $3, (Statement) $5, l);
4671 : DO embedded_statement
4672 WHILE open_parens_any boolean_expression CLOSE_PARENS SEMICOLON
4674 Location l = (Location) $1;
4676 $$ = new Do ((Statement) $2, (Expression) $5, l);
4681 : FOR open_parens_any opt_for_initializer SEMICOLON
4683 Location l = lexer.Location;
4685 Block assign_block = current_block;
4687 if ($3 is DictionaryEntry){
4688 DictionaryEntry de = (DictionaryEntry) $3;
4690 Expression type = (Expression) de.Key;
4691 ArrayList var_declarators = (ArrayList) de.Value;
4693 foreach (VariableDeclaration decl in var_declarators){
4697 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4701 Expression expr = decl.expression_or_array_initializer;
4703 LocalVariableReference var;
4704 var = new LocalVariableReference (assign_block, decl.identifier, l);
4707 Assign a = new SimpleAssign (var, expr, decl.Location);
4709 assign_block.AddStatement (new StatementExpression (a));
4713 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4714 // This can be referred to as $5 below.
4720 opt_for_condition SEMICOLON
4721 opt_for_iterator CLOSE_PARENS
4724 Location l = (Location) $1;
4726 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4728 current_block.AddStatement (f);
4730 $$ = end_block (lexer.Location);
4735 : /* empty */ { $$ = EmptyStatement.Value; }
4740 : local_variable_declaration
4741 | statement_expression_list
4745 : /* empty */ { $$ = null; }
4746 | boolean_expression
4750 : /* empty */ { $$ = EmptyStatement.Value; }
4755 : statement_expression_list
4758 statement_expression_list
4759 : statement_expression
4761 // CHANGE: was `null'
4762 Statement s = (Statement) $1;
4763 Block b = new Block (current_block, s.loc, lexer.Location);
4768 | statement_expression_list COMMA statement_expression
4770 Block b = (Block) $1;
4772 b.AddStatement ((Statement) $3);
4778 : FOREACH open_parens_any type IN expression CLOSE_PARENS
4780 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4783 | FOREACH open_parens_any type IDENTIFIER IN
4784 expression CLOSE_PARENS
4786 start_block (lexer.Location);
4787 Block foreach_block = current_block;
4789 LocatedToken lt = (LocatedToken) $4;
4790 Location l = lt.Location;
4791 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4793 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4795 // Get a writable reference to this read-only variable.
4797 // Note that the $$ here refers to the value of _this_ code block,
4798 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4799 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4806 LocalVariableReference v = (LocalVariableReference) $8;
4807 Location l = (Location) $1;
4810 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4811 current_block.AddStatement (f);
4814 $$ = end_block (lexer.Location);
4820 | continue_statement
4830 $$ = new Break ((Location) $1);
4835 : CONTINUE SEMICOLON
4837 $$ = new Continue ((Location) $1);
4842 : GOTO IDENTIFIER SEMICOLON
4844 LocatedToken lt = (LocatedToken) $2;
4845 $$ = new Goto (lt.Value, lt.Location);
4847 | GOTO CASE constant_expression SEMICOLON
4849 $$ = new GotoCase ((Expression) $3, (Location) $1);
4851 | GOTO DEFAULT SEMICOLON
4853 $$ = new GotoDefault ((Location) $1);
4858 : RETURN opt_expression SEMICOLON
4860 $$ = new Return ((Expression) $2, (Location) $1);
4865 : THROW opt_expression SEMICOLON
4867 $$ = new Throw ((Expression) $2, (Location) $1);
4872 : IDENTIFIER RETURN expression SEMICOLON
4874 LocatedToken lt = (LocatedToken) $1;
4875 string s = lt.Value;
4877 Report.Error (1003, lt.Location, "; expected");
4880 if (RootContext.Version == LanguageVersion.ISO_1){
4881 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
4884 current_block.Toplevel.IsIterator = true;
4885 $$ = new Yield ((Expression) $3, lt.Location);
4887 | IDENTIFIER RETURN SEMICOLON
4889 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4892 | IDENTIFIER BREAK SEMICOLON
4894 LocatedToken lt = (LocatedToken) $1;
4895 string s = lt.Value;
4897 Report.Error (1003, lt.Location, "; expected");
4900 if (RootContext.Version == LanguageVersion.ISO_1){
4901 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
4905 current_block.Toplevel.IsIterator = true;
4906 $$ = new YieldBreak (lt.Location);
4916 : TRY block catch_clauses
4918 $$ = new TryCatch ((Block) $2, (ArrayList) $3, (Location) $1, false);
4920 | TRY block FINALLY block
4922 $$ = new TryFinally ((Statement) $2, (Block) $4, (Location) $1);
4924 | TRY block catch_clauses FINALLY block
4926 $$ = new TryFinally (new TryCatch ((Block) $2, (ArrayList) $3, (Location) $1, true), (Block) $5, (Location) $1);
4930 Report.Error (1524, (Location) $1, "Expected catch or finally");
4938 ArrayList l = new ArrayList (4);
4943 | catch_clauses catch_clause
4945 ArrayList l = (ArrayList) $1;
4953 : /* empty */ { $$ = null; }
4958 : CATCH opt_catch_args
4960 Expression type = null;
4963 DictionaryEntry cc = (DictionaryEntry) $2;
4964 type = (Expression) cc.Key;
4965 LocatedToken lt = (LocatedToken) cc.Value;
4968 ArrayList one = new ArrayList (4);
4970 one.Add (new VariableDeclaration (lt, null));
4972 start_block (lexer.Location);
4973 current_block = declare_local_variables (type, one, lt.Location);
4977 Expression type = null;
4979 Block var_block = null;
4982 DictionaryEntry cc = (DictionaryEntry) $2;
4983 type = (Expression) cc.Key;
4984 LocatedToken lt = (LocatedToken) cc.Value;
4988 var_block = end_block (lexer.Location);
4992 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4997 : /* empty */ { $$ = null; }
5002 : open_parens_any type opt_identifier CLOSE_PARENS
5004 $$ = new DictionaryEntry ($2, $3);
5006 | open_parens_any CLOSE_PARENS
5008 Report.Error (1015, GetLocation ($1), "A type that derives from `System.Exception', `object', or `string' expected");
5015 $$ = new Checked ((Block) $2);
5022 $$ = new Unchecked ((Block) $2);
5029 RootContext.CheckUnsafeOption ((Location) $1);
5031 $$ = new Unsafe ((Block) $3);
5036 : FIXED open_parens_any
5037 type_and_void fixed_pointer_declarators
5040 ArrayList list = (ArrayList) $4;
5041 Expression type = (Expression) $3;
5042 Location l = (Location) $1;
5043 int top = list.Count;
5045 start_block (lexer.Location);
5047 for (int i = 0; i < top; i++){
5048 Pair p = (Pair) list [i];
5051 v = current_block.AddVariable (type, (string) p.First, l);
5055 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
5063 Location l = (Location) $1;
5065 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
5067 current_block.AddStatement (f);
5069 $$ = end_block (lexer.Location);
5073 fixed_pointer_declarators
5074 : fixed_pointer_declarator {
5075 ArrayList declarators = new ArrayList (4);
5077 declarators.Add ($1);
5080 | fixed_pointer_declarators COMMA fixed_pointer_declarator
5082 ArrayList declarators = (ArrayList) $1;
5084 declarators.Add ($3);
5089 fixed_pointer_declarator
5090 : IDENTIFIER ASSIGN expression
5092 LocatedToken lt = (LocatedToken) $1;
5093 // FIXME: keep location
5094 $$ = new Pair (lt.Value, $3);
5098 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
5104 : LOCK open_parens_any expression CLOSE_PARENS
5110 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
5115 : USING open_parens_any local_variable_declaration CLOSE_PARENS
5117 start_block (lexer.Location);
5118 Block assign_block = current_block;
5120 DictionaryEntry de = (DictionaryEntry) $3;
5121 Location l = (Location) $1;
5123 Expression type = (Expression) de.Key;
5124 ArrayList var_declarators = (ArrayList) de.Value;
5126 Stack vars = new Stack ();
5128 foreach (VariableDeclaration decl in var_declarators) {
5129 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
5132 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
5134 Expression expr = decl.expression_or_array_initializer;
5136 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
5139 LocalVariableReference var;
5141 // Get a writable reference to this read-only variable.
5142 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
5144 // This is so that it is not a warning on using variables
5147 vars.Push (new DictionaryEntry (var, expr));
5149 // Assign a = new SimpleAssign (var, expr, decl.Location);
5150 // assign_block.AddStatement (new StatementExpression (a));
5153 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
5154 // It can be referred to as $5 below.
5159 Statement stmt = (Statement) $6;
5160 Stack vars = (Stack) $5;
5161 Location l = (Location) $1;
5163 while (vars.Count > 0) {
5164 DictionaryEntry de = (DictionaryEntry) vars.Pop ();
5165 stmt = new Using ((Expression) de.Key, (Expression) de.Value, stmt, l);
5167 current_block.AddStatement (stmt);
5168 $$ = end_block (lexer.Location);
5170 | USING open_parens_any expression CLOSE_PARENS
5172 start_block (lexer.Location);
5176 current_block.AddStatement (new UsingTemporary ((Expression) $3, (Statement) $6, (Location) $1));
5177 $$ = end_block (lexer.Location);
5187 ++lexer.query_parsing;
5191 if (--lexer.query_parsing == 1)
5192 lexer.query_parsing = 0;
5194 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5196 from.Tail.Next = (Linq.AQueryClause)$3;
5199 current_block.SetEndLocation (lexer.Location);
5200 current_block = current_block.Parent;
5205 : FROM IDENTIFIER IN expression
5207 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5208 LocatedToken lt = (LocatedToken) $2;
5210 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance, lt.Value, lt.Location);
5211 $$ = new Linq.QueryExpression (lt, new Linq.QueryStartClause ((Expression)$4));
5213 | FROM type IDENTIFIER IN expression
5215 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5216 LocatedToken lt = (LocatedToken) $3;
5218 Expression type = (Expression)$2;
5219 current_block.AddVariable (type, lt.Value, lt.Location);
5220 $$ = new Linq.QueryExpression (lt, new Linq.Cast (type, (Expression)$5));
5225 : FROM IDENTIFIER IN expression
5227 LocatedToken lt = (LocatedToken) $2;
5229 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5230 lt.Value, lt.Location);
5232 $$ = new Linq.SelectMany (lt, (Expression)$4);
5234 | FROM type IDENTIFIER IN expression
5236 LocatedToken lt = (LocatedToken) $3;
5238 Expression type = (Expression)$2;
5239 current_block.AddVariable (type, lt.Value, lt.Location);
5240 $$ = new Linq.SelectMany (lt, new Linq.Cast (type, (Expression)$5));
5245 : opt_query_body_clauses select_or_group_clause opt_query_continuation
5247 Linq.AQueryClause head = (Linq.AQueryClause)$2;
5250 head.Next = (Linq.AQueryClause)$3;
5253 Linq.AQueryClause clause = (Linq.AQueryClause)$1;
5254 clause.Tail.Next = head;
5262 select_or_group_clause
5265 $$ = new Linq.Select ((Expression)$2, GetLocation ($1));
5267 | GROUP expression BY expression
5269 $$ = new Linq.GroupBy ((Expression)$2, (Expression)$4, GetLocation ($1));
5273 opt_query_body_clauses
5275 | query_body_clauses
5280 | query_body_clauses query_body_clause
5282 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
5296 : LET IDENTIFIER ASSIGN expression
5298 LocatedToken lt = (LocatedToken) $2;
5299 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5300 lt.Value, lt.Location);
5301 $$ = new Linq.Let (lt, (Expression)$4, GetLocation ($1));
5306 : WHERE boolean_expression
5308 $$ = new Linq.Where ((Expression)$2, GetLocation ($1));
5313 : JOIN IDENTIFIER IN expression ON expression EQUALS expression opt_join_into
5315 Location loc = GetLocation ($1);
5316 LocatedToken lt = (LocatedToken) $2;
5317 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5318 lt.Value, lt.Location);
5321 $$ = new Linq.Join (lt, (Expression)$4, (Expression)$6,
5322 (Expression)$8, loc);
5324 LocatedToken lt_into = (LocatedToken) $9;
5325 $$ = new Linq.GroupJoin (lt, (Expression)$4, (Expression)$6,
5326 (Expression)$8, lt_into, loc);
5329 | JOIN type IDENTIFIER IN expression ON expression EQUALS expression opt_join_into
5331 Location loc = GetLocation ($1);
5332 LocatedToken lt = (LocatedToken) $3;
5333 current_block.AddVariable ((Expression)$2, lt.Value, lt.Location);
5335 Linq.Cast cast = new Linq.Cast ((Expression)$2, (Expression)$5);
5337 $$ = new Linq.Join (lt, cast, (Expression)$7,
5338 (Expression)$9, loc);
5340 LocatedToken lt_into = (LocatedToken) $10;
5341 $$ = new Linq.GroupJoin (lt, cast, (Expression)$7,
5342 (Expression)$9, lt_into, loc);
5364 | order_by COMMA orderings_then_by
5366 ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$3;
5376 | orderings_then_by COMMA then_by
5378 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
5386 $$ = new Linq.OrderByAscending ((Expression)$1);
5388 | expression ASCENDING
5390 $$ = new Linq.OrderByAscending ((Expression)$1);
5392 | expression DESCENDING
5394 $$ = new Linq.OrderByDescending ((Expression)$1);
5401 $$ = new Linq.ThenByAscending ((Expression)$1);
5403 | expression ASCENDING
5405 $$ = new Linq.ThenByAscending ((Expression)$1);
5407 | expression DESCENDING
5409 $$ = new Linq.ThenByDescending ((Expression)$1);
5414 opt_query_continuation
5418 // query continuation block is not linked with query block but with block
5419 // before. This means each query can use same range variable names for
5420 // different identifiers.
5422 current_block.SetEndLocation (GetLocation ($1));
5423 current_block = current_block.Parent;
5424 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5426 LocatedToken lt = (LocatedToken) $2;
5427 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5428 lt.Value, lt.Location);
5432 $$ = new Linq.QueryExpression ((LocatedToken) $2,
5433 (Linq.AQueryClause)$4);
5438 // Support for using the compiler as an interactive parser
5440 // The INTERACTIVE_PARSER token is first sent to parse our
5441 // productions; If the result is a Statement, the parsing
5442 // is repeated, this time with INTERACTIVE_PARSE_WITH_BLOCK
5443 // to setup the blocks in advance.
5445 // This setup is here so that in the future we can add
5446 // support for other constructs (type parsing, namespaces, etc)
5447 // that do not require a block to be setup in advance
5451 : EVAL_STATEMENT_PARSER EOF
5452 | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives
5453 | EVAL_STATEMENT_PARSER {
5454 Evaluator.LoadAliases (current_namespace);
5456 push_current_class (new Class (current_namespace, current_class, new MemberName ("Class" + class_count++),
5457 Modifiers.PUBLIC, null), null);
5459 ArrayList baseclass_list = new ArrayList ();
5460 baseclass_list.Add (new TypeExpression (Evaluator.InteractiveBaseClass, lexer.Location));
5461 current_container.AddBasesForPart (current_class, baseclass_list);
5463 // (ref object retval)
5464 Parameter [] mpar = new Parameter [1];
5465 mpar [0] = new Parameter (TypeManager.system_object_expr, "$retval", Parameter.Modifier.REF, null, Location.Null);
5467 Parameters pars = new Parameters (mpar);
5468 current_local_parameters = pars;
5469 Method method = new Method (
5472 TypeManager.system_void_expr,
5473 Modifiers.PUBLIC | Modifiers.STATIC,
5474 new MemberName ("Host"),
5476 null /* attributes */);
5478 oob_stack.Push (method);
5479 ++lexer.parsing_block;
5480 start_block (lexer.Location);
5482 interactive_statement_list
5484 --lexer.parsing_block;
5485 Method method = (Method) oob_stack.Pop ();
5487 method.Block = (ToplevelBlock) end_block(lexer.Location);
5488 current_container.AddMethod (method);
5490 --lexer.parsing_declaration;
5491 InteractiveResult = pop_current_class ();
5492 current_local_parameters = null;
5494 | EVAL_COMPILATION_UNIT_PARSER {
5495 Evaluator.LoadAliases (current_namespace);
5497 interactive_compilation_unit
5500 interactive_compilation_unit
5501 : outer_declarations
5502 | outer_declarations global_attributes
5509 // A class used to pass around variable declarations and constants
5511 public class VariableDeclaration {
5512 public string identifier;
5513 public Expression expression_or_array_initializer;
5514 public Location Location;
5515 public Attributes OptAttributes;
5516 public string DocComment;
5518 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
5520 this.identifier = lt.Value;
5521 if (eoai is ArrayList) {
5522 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
5524 this.expression_or_array_initializer = (Expression)eoai;
5526 this.Location = lt.Location;
5527 this.OptAttributes = opt_attrs;
5530 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
5535 class VariableMemberDeclaration
5537 public readonly MemberName MemberName;
5538 public Expression expression_or_array_initializer;
5540 public VariableMemberDeclaration (MemberName mn, object initializer)
5544 if (initializer is ArrayList) {
5545 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)initializer, mn.Location);
5547 this.expression_or_array_initializer = (Expression)initializer;
5554 // A class used to hold info about an operator declarator
5556 struct OperatorDeclaration {
5557 public readonly Operator.OpType optype;
5558 public readonly FullNamedExpression ret_type;
5559 public readonly Location location;
5561 public OperatorDeclaration (Operator.OpType op, FullNamedExpression ret_type, Location location)
5564 this.ret_type = ret_type;
5565 this.location = location;
5569 void Error_ExpectingTypeName (Expression expr)
5571 if (expr is Invocation){
5572 Report.Error (1002, expr.Location, "Expecting `;'");
5574 expr.Error_InvalidExpressionStatement ();
5578 static void Error_ParameterModifierNotValid (string modifier, Location loc)
5580 Report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
5584 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
5586 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
5587 Parameter.GetModifierSignature (mod));
5590 static void Error_TypeExpected (Location loc)
5592 Report.Error (1031, loc, "Type expected");
5595 void push_current_class (TypeContainer tc, object partial_token)
5597 if (RootContext.EvalMode){
5598 tc.ModFlags = (tc.ModFlags & ~(Modifiers.PRIVATE|Modifiers.INTERNAL)) | Modifiers.PUBLIC;
5599 undo.AddTypeContainer (current_container, tc);
5602 if (partial_token != null)
5603 current_container = current_container.AddPartial (tc);
5605 current_container = current_container.AddTypeContainer (tc);
5607 ++lexer.parsing_declaration;
5611 DeclSpace pop_current_class ()
5613 DeclSpace retval = current_class;
5615 current_class = current_class.Parent;
5616 current_container = current_class.PartialContainer;
5622 // Given the @class_name name, it creates a fully qualified name
5623 // based on the containing declaration space
5626 MakeName (MemberName class_name)
5628 Namespace ns = current_namespace.NS;
5630 if (current_container == RootContext.ToplevelTypes) {
5631 if (ns.Name.Length != 0)
5632 return new MemberName (ns.MemberName, class_name);
5636 return new MemberName (current_container.MemberName, class_name);
5640 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5642 Block implicit_block;
5643 ArrayList inits = null;
5646 // If we are doing interactive editing, we want variable declarations
5647 // that are in the top block to be added instead to the class as
5650 if (RootContext.StatementMode){
5653 for (Block b = current_block; b != null; b = b.Parent){
5654 if (b is ExplicitBlock && !(b is ToplevelBlock)){
5655 // There has been an explicit block, we cant add to the class
5662 // We can use "current_block" since we know there are no explicit blocks
5664 foreach (VariableDeclaration decl in variable_declarators){
5665 // We can not use the super-handy f.Initializer, because
5666 // multiple lines would force code to be executed out of sync
5667 if (decl.expression_or_array_initializer != null){
5668 string id = "$" + decl.identifier;
5669 current_block.AddVariable (type, id, decl.Location);
5671 LocalVariableReference var;
5672 var = new LocalVariableReferenceWithClassSideEffect (current_container, decl.identifier, current_block, id, decl.Location);
5673 Assign assign = new SimpleAssign (var, decl.expression_or_array_initializer, decl.Location);
5674 current_block.AddStatement (new StatementExpression (assign));
5675 assign = new SimpleAssign (new SimpleName (decl.identifier, decl.Location), var);
5676 current_block.AddStatement (new StatementExpression (assign));
5678 Field f = new Field (current_container, (FullNamedExpression) type, Modifiers.PUBLIC | Modifiers.STATIC,
5679 new MemberName (decl.identifier, loc), null);
5680 current_container.AddField (f);
5682 // Register the field to be visible later as a global variable
5683 Evaluator.QueueField (f);
5687 return current_block;
5692 // We use the `Used' property to check whether statements
5693 // have been added to the current block. If so, we need
5694 // to create another block to contain the new declaration
5695 // otherwise, as an optimization, we use the same block to
5696 // add the declaration.
5698 // FIXME: A further optimization is to check if the statements
5699 // that were added were added as part of the initialization
5700 // below. In which case, no other statements have been executed
5701 // and we might be able to reduce the number of blocks for
5702 // situations like this:
5704 // int j = 1; int k = j + 1;
5706 if (current_block.Used)
5707 implicit_block = new Block (current_block, loc, lexer.Location);
5709 implicit_block = current_block;
5711 foreach (VariableDeclaration decl in variable_declarators){
5713 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5714 if (decl.expression_or_array_initializer != null){
5716 inits = new ArrayList (4);
5723 return implicit_block;
5725 foreach (VariableDeclaration decl in inits){
5727 Expression expr = decl.expression_or_array_initializer;
5729 LocalVariableReference var;
5730 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5732 assign = new SimpleAssign (var, expr, decl.Location);
5734 implicit_block.AddStatement (new StatementExpression (assign));
5737 return implicit_block;
5740 Block declare_local_constants (Expression type, ArrayList declarators)
5742 Block implicit_block;
5744 if (current_block.Used)
5745 implicit_block = new Block (current_block);
5747 implicit_block = current_block;
5749 foreach (VariableDeclaration decl in declarators){
5750 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5753 return implicit_block;
5756 string CheckAttributeTarget (string a, Location l)
5759 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5763 Report.Warning (658, 1, l,
5764 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
5765 return string.Empty;
5768 static bool IsUnaryOperator (Operator.OpType op)
5772 case Operator.OpType.LogicalNot:
5773 case Operator.OpType.OnesComplement:
5774 case Operator.OpType.Increment:
5775 case Operator.OpType.Decrement:
5776 case Operator.OpType.True:
5777 case Operator.OpType.False:
5778 case Operator.OpType.UnaryPlus:
5779 case Operator.OpType.UnaryNegation:
5785 void syntax_error (Location l, string msg)
5787 Report.Error (1003, l, "Syntax error, " + msg);
5790 void note (string s)
5792 // Used to put annotations
5797 public Tokenizer Lexer {
5803 static CSharpParser ()
5805 oob_stack = new Stack ();
5808 public CSharpParser (SeekableStreamReader reader, CompilationUnit file)
5810 if (RootContext.EvalMode)
5814 current_namespace = new NamespaceEntry (null, file, null);
5815 current_class = current_namespace.SlaveDeclSpace;
5816 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5818 lexer = new Tokenizer (reader, file);
5821 public void parse ()
5823 eof_token = Token.EOF;
5826 if (yacc_verbose_flag > 1)
5827 yyparse (lexer, new yydebug.yyDebugSimple ());
5830 } catch (Exception e){
5831 if (e is yyParser.yyUnexpectedEof)
5832 UnexpectedEOF = true;
5833 else if (yacc_verbose_flag > 0)
5834 Console.WriteLine (e);
5835 if (e is yyParser.yyException)
5836 Report.Error (-25, lexer.Location, "Parsing error");
5838 Report.Error (-32, lexer.Location, "Internal compiler error during parsing, Run with -v for details");
5840 Tokenizer tokenizer = lexer as Tokenizer;
5841 tokenizer.cleanup ();
5843 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5844 throw new InternalErrorException ("who set it?");
5847 static void CheckToken (int error, int yyToken, string msg, Location loc)
5849 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5850 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, GetTokenName (yyToken));
5852 Report.Error (error, loc, msg);
5855 void CheckIdentifierToken (int yyToken, Location loc)
5857 CheckToken (1041, yyToken, "Identifier expected", loc);
5860 string ConsumeStoredComment ()
5862 string s = tmpComment;
5864 Lexer.doc_state = XmlCommentState.Allowed;
5868 Location GetLocation (object obj)
5870 if (obj is MemberCore)
5871 return ((MemberCore) obj).Location;
5872 if (obj is MemberName)
5873 return ((MemberName) obj).Location;
5874 if (obj is LocatedToken)
5875 return ((LocatedToken) obj).Location;
5876 if (obj is Location)
5877 return (Location) obj;
5878 return lexer.Location;
5881 void start_block (Location loc)
5883 if (current_block == null || parsing_anonymous_method) {
5884 current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
5885 parsing_anonymous_method = false;
5887 current_block = new ExplicitBlock (current_block, loc, Location.Null);
5892 end_block (Location loc)
5894 Block retval = current_block.Explicit;
5895 retval.SetEndLocation (loc);
5896 current_block = retval.Parent;
5901 start_anonymous (bool lambda, Parameters parameters, Location loc)
5903 oob_stack.Push (current_anonymous_method);
5904 oob_stack.Push (current_local_parameters);
5906 current_local_parameters = parameters;
5908 current_anonymous_method = lambda
5909 ? new LambdaExpression (parameters, loc)
5910 : new AnonymousMethodExpression (parameters, loc);
5912 // Force the next block to be created as a ToplevelBlock
5913 parsing_anonymous_method = true;
5917 * Completes the anonymous method processing, if lambda_expr is null, this
5918 * means that we have a Statement instead of an Expression embedded
5920 AnonymousMethodExpression end_anonymous (ToplevelBlock anon_block, Location loc)
5922 AnonymousMethodExpression retval;
5924 if (RootContext.Version == LanguageVersion.ISO_1){
5925 Report.FeatureIsNotAvailable (loc, "anonymous methods");
5928 current_anonymous_method.Block = anon_block;
5929 retval = current_anonymous_method;
5932 current_local_parameters = (Parameters) oob_stack.Pop ();
5933 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
5938 public NamespaceEntry CurrentNamespace {
5940 return current_namespace;
5945 void Error_SyntaxError (int token)
5947 Error_SyntaxError (0, token);
5950 void Error_SyntaxError (int error_code, int token)
5952 string symbol = GetSymbolName (token);
5953 string expecting = GetExpecting ();
5955 if (error_code == 0) {
5956 if (expecting == "`)'")
5962 if (expecting != null)
5963 Report.Error (error_code, lexer.Location, "Unexpected symbol `{0}', expecting {1}",
5966 Report.Error (error_code, lexer.Location, "Unexpected symbol `{0}'", symbol);
5969 string GetExpecting ()
5971 int [] tokens = yyExpectingTokens (yyExpectingState);
5972 ArrayList names = new ArrayList (tokens.Length);
5973 bool has_type = false;
5974 bool has_identifier = false;
5975 for (int i = 0; i < tokens.Length; i++){
5976 int token = tokens [i];
5977 has_identifier |= token == Token.IDENTIFIER;
5979 string name = GetTokenName (token);
5980 if (name == "<internal>")
5983 has_type |= name == "type";
5984 if (names.Contains (name))
5991 // Too many tokens to enumerate
5993 if (names.Count > 8)
5996 if (has_type && has_identifier)
5997 names.Remove ("identifier");
5999 if (names.Count == 1)
6000 return "`" + GetTokenName (tokens [0]) + "'";
6002 StringBuilder sb = new StringBuilder ();
6004 int count = names.Count;
6005 for (int i = 0; i < count; i++){
6006 bool last = i + 1 == count;
6010 sb.Append (names [i]);
6011 sb.Append (last ? "'" : "', ");
6013 return sb.ToString ();
6017 string GetSymbolName (int token)
6020 case Token.LITERAL_FLOAT:
6021 case Token.LITERAL_INTEGER:
6022 case Token.LITERAL_DOUBLE:
6023 case Token.LITERAL_DECIMAL:
6024 case Token.LITERAL_CHARACTER:
6025 case Token.LITERAL_STRING:
6026 return lexer.Value.ToString ();
6027 case Token.IDENTIFIER:
6028 return ((LocatedToken)lexer.Value).Value;
6070 case Token.BITWISE_AND:
6072 case Token.BITWISE_OR:
6086 case Token.OP_SHIFT_LEFT:
6088 case Token.OP_SHIFT_RIGHT:
6108 case Token.OP_COALESCING:
6110 case Token.OP_MULT_ASSIGN:
6112 case Token.OP_DIV_ASSIGN:
6114 case Token.OP_MOD_ASSIGN:
6116 case Token.OP_ADD_ASSIGN:
6118 case Token.OP_SUB_ASSIGN:
6120 case Token.OP_SHIFT_LEFT_ASSIGN:
6122 case Token.OP_SHIFT_RIGHT_ASSIGN:
6124 case Token.OP_AND_ASSIGN:
6126 case Token.OP_XOR_ASSIGN:
6128 case Token.OP_OR_ASSIGN:
6132 return GetTokenName (token);
6135 static string GetTokenName (int token)
6138 case Token.ABSTRACT:
6158 case Token.CONTINUE:
6162 case Token.DELEGATE:
6172 case Token.EXPLICIT:
6190 case Token.IMPLICIT:
6194 case Token.INTERFACE:
6196 case Token.INTERNAL:
6202 case Token.NAMESPACE:
6208 case Token.OPERATOR:
6212 case Token.OVERRIDE:
6218 case Token.PROTECTED:
6222 case Token.READONLY:
6234 case Token.STACKALLOC:
6235 return "stackalloc";
6252 case Token.UNCHECKED:
6260 case Token.VOLATILE:
6290 case Token.ASCENDING:
6292 case Token.DESCENDING:
6293 return "descending";
6300 case Token.OPEN_BRACE:
6302 case Token.CLOSE_BRACE:
6304 case Token.OPEN_BRACKET:
6306 case Token.CLOSE_BRACKET:
6308 case Token.OPEN_PARENS_CAST:
6309 case Token.OPEN_PARENS_LAMBDA:
6310 case Token.OPEN_PARENS:
6312 case Token.CLOSE_PARENS:
6318 case Token.DEFAULT_COLON:
6322 case Token.SEMICOLON:
6333 case Token.BITWISE_AND:
6334 case Token.BITWISE_OR:
6341 case Token.OP_SHIFT_LEFT:
6342 case Token.OP_SHIFT_RIGHT:
6350 case Token.OP_COALESCING:
6351 case Token.OP_MULT_ASSIGN:
6352 case Token.OP_DIV_ASSIGN:
6353 case Token.OP_MOD_ASSIGN:
6354 case Token.OP_ADD_ASSIGN:
6355 case Token.OP_SUB_ASSIGN:
6356 case Token.OP_SHIFT_LEFT_ASSIGN:
6357 case Token.OP_SHIFT_RIGHT_ASSIGN:
6358 case Token.OP_AND_ASSIGN:
6359 case Token.OP_XOR_ASSIGN:
6360 case Token.OP_OR_ASSIGN:
6361 return "<operator>";
6383 case Token.OP_GENERICS_LT:
6384 case Token.GENERIC_DIMENSION:
6386 case Token.OP_GENERICS_GT:
6389 case Token.INTERR_NULLABLE:
6391 case Token.DOUBLE_COLON:
6393 case Token.LITERAL_FLOAT:
6394 case Token.LITERAL_INTEGER:
6395 case Token.LITERAL_DOUBLE:
6396 case Token.LITERAL_DECIMAL:
6397 case Token.LITERAL_CHARACTER:
6398 case Token.LITERAL_STRING:
6400 case Token.IDENTIFIER:
6401 return "identifier";
6403 // All of these are internal.
6406 case Token.FIRST_KEYWORD:
6408 case Token.EVAL_COMPILATION_UNIT_PARSER:
6409 case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
6410 case Token.EVAL_STATEMENT_PARSER:
6412 case Token.LAST_KEYWORD:
6413 return "<internal>";
6415 // A bit more robust.
6417 return yyNames [token];