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
232 %token QUERY_FIRST_TOKEN
245 %token QUERY_LAST_TOKEN
246 %token INTERR_NULLABLE
248 /* C# keywords which are not really keywords */
254 /* C# single character operators/punctuation. */
272 %token OP_GENERICS_LT
274 %token OP_GENERICS_GT
283 /* C# multi-character operators. */
288 %token OP_SHIFT_RIGHT
295 %token OP_MULT_ASSIGN
300 %token OP_SHIFT_LEFT_ASSIGN
301 %token OP_SHIFT_RIGHT_ASSIGN
309 %token LITERAL_INTEGER
311 %token LITERAL_DOUBLE
312 %token LITERAL_DECIMAL
313 %token LITERAL_CHARACTER
314 %token LITERAL_STRING
317 %token OPEN_PARENS_LAMBDA
318 %token CLOSE_PARENS_CAST
319 %token CLOSE_PARENS_NO_CAST
320 %token CLOSE_PARENS_OPEN_PARENS
321 %token CLOSE_PARENS_MINUS
322 %token DEFAULT_OPEN_PARENS
323 %token GENERIC_DIMENSION
326 // Make the parser go into eval mode parsing (statements and compilation units).
327 %token EVAL_STATEMENT_PARSER
328 %token EVAL_COMPILATION_UNIT_PARSER
329 %token EVAL_USING_DECLARATIONS_UNIT_PARSER
331 /* Add precedence rules to solve dangling else s/r conflict */
340 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
342 %left STAR DIV PERCENT
343 %right BANG CARRET UMINUS
344 %nonassoc OP_INC OP_DEC
346 %left OPEN_BRACKET OPEN_BRACE
350 %start compilation_unit
354 : outer_declarations opt_EOF
355 | outer_declarations global_attributes opt_EOF
356 | global_attributes opt_EOF
357 | opt_EOF /* allow empty files */
358 | interactive_parsing opt_EOF
364 Lexer.check_incorrect_doc_comment ();
368 Lexer.check_incorrect_doc_comment ();
374 | outer_declarations outer_declaration
378 : extern_alias_directive
380 | namespace_member_declaration
383 extern_alias_directives
384 : extern_alias_directive
385 | extern_alias_directives extern_alias_directive;
387 extern_alias_directive
388 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
390 LocatedToken lt = (LocatedToken) $2;
393 Report.Error (1003, lt.Location, "'alias' expected");
394 } else if (RootContext.Version == LanguageVersion.ISO_1) {
395 Report.FeatureIsNotAvailable (lt.Location, "external alias");
397 lt = (LocatedToken) $3;
398 current_namespace.AddUsingExternalAlias (lt.Value, lt.Location);
405 | using_directives using_directive
409 : using_alias_directive
411 if (RootContext.Documentation != null)
412 Lexer.doc_state = XmlCommentState.Allowed;
414 | using_namespace_directive
416 if (RootContext.Documentation != null)
417 Lexer.doc_state = XmlCommentState.Allowed;
421 using_alias_directive
422 : USING IDENTIFIER ASSIGN
423 namespace_or_type_name SEMICOLON
425 LocatedToken lt = (LocatedToken) $2;
426 current_namespace.AddUsingAlias (lt.Value, (MemberName) $4, (Location) $1);
429 CheckIdentifierToken (yyToken, GetLocation ($2));
434 using_namespace_directive
435 : USING namespace_name SEMICOLON
437 current_namespace.AddUsing ((MemberName) $2, (Location) $1);
442 // Strictly speaking, namespaces don't have attributes but
443 // we parse global attributes along with namespace declarations and then
446 namespace_declaration
447 : opt_attributes NAMESPACE qualified_identifier
449 MemberName name = (MemberName) $3;
452 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
455 current_namespace = new NamespaceEntry (
456 current_namespace, file, name.GetName ());
457 current_class = current_namespace.SlaveDeclSpace;
458 current_container = current_class.PartialContainer;
460 namespace_body opt_semicolon
462 current_namespace = current_namespace.Parent;
463 current_class = current_namespace.SlaveDeclSpace;
464 current_container = current_class.PartialContainer;
471 LocatedToken lt = (LocatedToken) $1;
472 $$ = new MemberName (lt.Value, lt.Location);
474 | qualified_identifier DOT IDENTIFIER
476 LocatedToken lt = (LocatedToken) $3;
477 $$ = new MemberName ((MemberName) $1, lt.Value, lt.Location);
481 syntax_error (lexer.Location, "`.' expected");
496 : namespace_or_type_name {
497 MemberName name = (MemberName) $1;
499 if (name.TypeArguments != null)
500 syntax_error (lexer.Location, "namespace name expected");
509 if (RootContext.Documentation != null)
510 Lexer.doc_state = XmlCommentState.Allowed;
516 : opt_extern_alias_directives
518 opt_namespace_member_declarations
522 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
525 | opt_extern_alias_directives
527 opt_namespace_member_declarations
530 Report.Error (1513, lexer.Location, "} expected");
539 opt_extern_alias_directives
541 | extern_alias_directives
544 opt_namespace_member_declarations
546 | namespace_member_declarations
549 namespace_member_declarations
550 : namespace_member_declaration
551 | namespace_member_declarations namespace_member_declaration
554 namespace_member_declaration
558 DeclSpace ds = (DeclSpace)$1;
560 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
561 Report.Error (1527, ds.Location,
562 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
565 current_namespace.DeclarationFound = true;
567 | namespace_declaration {
568 current_namespace.DeclarationFound = true;
571 | field_declaration {
572 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
574 | method_declaration {
575 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
582 | interface_declaration
584 | delegate_declaration
586 // Enable this when we have handled all errors, because this acts as a generic fallback
589 // Console.WriteLine ("Token=" + yyToken);
590 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
602 Attributes attrs = (Attributes)$1;
603 if (global_attrs_enabled) {
604 CodeGen.Assembly.AddAttributes (attrs.Attrs);
606 foreach (Attribute a in attrs.Attrs) {
607 Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
618 global_attrs_enabled = false;
623 global_attrs_enabled = false;
632 if (current_attr_target != String.Empty) {
633 ArrayList sect = (ArrayList) $1;
635 if (global_attrs_enabled) {
636 if (current_attr_target == "module") {
637 CodeGen.Module.AddAttributes (sect);
639 } else if (current_attr_target != null && current_attr_target.Length > 0) {
640 CodeGen.Assembly.AddAttributes (sect);
643 $$ = new Attributes (sect);
646 if (RootContext.Documentation != null) {
647 Lexer.check_incorrect_doc_comment ();
649 XmlCommentState.Allowed;
653 $$ = new Attributes (sect);
658 current_attr_target = null;
660 | attribute_sections attribute_section
662 if (current_attr_target != String.Empty) {
663 Attributes attrs = $1 as Attributes;
664 ArrayList sect = (ArrayList) $2;
666 if (global_attrs_enabled) {
667 if (current_attr_target == "module") {
668 CodeGen.Module.AddAttributes (sect);
670 } else if (current_attr_target == "assembly") {
671 CodeGen.Assembly.AddAttributes (sect);
675 attrs = new Attributes (sect);
677 attrs.AddAttributes (sect);
681 attrs = new Attributes (sect);
683 attrs.AddAttributes (sect);
689 current_attr_target = null;
694 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
698 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
704 attribute_target_specifier
705 : attribute_target COLON
707 current_attr_target = (string)$1;
715 LocatedToken lt = (LocatedToken) $1;
716 $$ = CheckAttributeTarget (lt.Value, lt.Location);
718 | EVENT { $$ = "event"; }
719 | RETURN { $$ = "return"; }
722 string name = GetTokenName (yyToken);
723 $$ = CheckAttributeTarget (name, GetLocation ($1));
730 ArrayList attrs = new ArrayList (4);
736 | attribute_list COMMA attribute
738 ArrayList attrs = (ArrayList) $1;
748 ++lexer.parsing_block;
750 opt_attribute_arguments
752 --lexer.parsing_block;
753 MemberName mname = (MemberName) $1;
754 if (mname.IsGeneric) {
755 Report.Error (404, lexer.Location,
756 "'<' unexpected: attributes cannot be generic");
759 object [] arguments = (object []) $3;
760 MemberName left = mname.Left;
761 string identifier = mname.Name;
763 Expression left_expr = left == null ? null : left.GetTypeExpression ();
765 if (current_attr_target == String.Empty)
767 else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
768 // FIXME: supply "nameEscaped" parameter here.
769 $$ = new GlobalAttribute (current_namespace, current_attr_target,
770 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
772 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
777 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
780 opt_attribute_arguments
781 : /* empty */ { $$ = null; }
782 | OPEN_PARENS attribute_arguments CLOSE_PARENS
790 : opt_positional_argument_list
795 $$ = new object [] { $1, null };
798 | positional_argument_list COMMA named_argument_list
800 $$ = new object[] { $1, $3 };
802 | named_argument_list
804 $$ = new object [] { null, $1 };
809 opt_positional_argument_list
810 : /* empty */ { $$ = null; }
811 | positional_argument_list
814 positional_argument_list
817 ArrayList args = new ArrayList (4);
818 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
822 | positional_argument_list COMMA expression
824 ArrayList args = (ArrayList) $1;
825 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
834 ArrayList args = new ArrayList (4);
839 | named_argument_list COMMA named_argument
841 ArrayList args = (ArrayList) $1;
846 | named_argument_list COMMA expression
848 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
854 : IDENTIFIER ASSIGN expression
856 // FIXME: keep location
857 $$ = new DictionaryEntry (
858 ((LocatedToken) $1).Value,
859 new Argument ((Expression) $3, Argument.AType.Expression));
865 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
868 opt_class_member_declarations
870 | class_member_declarations
873 class_member_declarations
874 : class_member_declaration
875 | class_member_declarations
876 class_member_declaration
879 class_member_declaration
880 : constant_declaration // done
881 | field_declaration // done
882 | method_declaration // done
883 | property_declaration // done
884 | event_declaration // done
885 | indexer_declaration // done
886 | operator_declaration // done
887 | constructor_declaration // done
888 | destructor_declaration // done
898 lexer.ConstraintsParsing = true;
902 MemberName name = MakeName ((MemberName) $6);
903 push_current_class (new Struct (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
906 opt_type_parameter_constraints_clauses
908 lexer.ConstraintsParsing = false;
910 current_class.SetParameterInfo ((ArrayList) $9);
912 if (RootContext.Documentation != null)
913 current_container.DocComment = Lexer.consume_doc_comment ();
917 if (RootContext.Documentation != null)
918 Lexer.doc_state = XmlCommentState.Allowed;
922 $$ = pop_current_class ();
924 | opt_attributes opt_modifiers opt_partial STRUCT error {
925 CheckIdentifierToken (yyToken, GetLocation ($5));
932 if (RootContext.Documentation != null)
933 Lexer.doc_state = XmlCommentState.Allowed;
935 opt_struct_member_declarations CLOSE_BRACE
938 opt_struct_member_declarations
940 | struct_member_declarations
943 struct_member_declarations
944 : struct_member_declaration
945 | struct_member_declarations struct_member_declaration
948 struct_member_declaration
949 : constant_declaration
952 | property_declaration
954 | indexer_declaration
955 | operator_declaration
956 | constructor_declaration
960 * This is only included so we can flag error 575:
961 * destructors only allowed on class types
963 | destructor_declaration
974 int modflags = (int) $2;
975 foreach (VariableDeclaration constant in (ArrayList) $5){
976 Location l = constant.Location;
977 if ((modflags & Modifiers.STATIC) != 0) {
978 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
982 Const c = new Const (
983 current_class, (FullNamedExpression) $4, (string) constant.identifier,
984 (Expression) constant.expression_or_array_initializer, modflags,
987 if (RootContext.Documentation != null) {
988 c.DocComment = Lexer.consume_doc_comment ();
989 Lexer.doc_state = XmlCommentState.Allowed;
991 current_container.AddConstant (c);
997 : constant_declarator
999 ArrayList constants = new ArrayList (4);
1004 | constant_declarators COMMA constant_declarator
1007 ArrayList constants = (ArrayList) $1;
1016 ++lexer.parsing_block;
1018 constant_initializer
1020 --lexer.parsing_block;
1021 $$ = new VariableDeclaration ((LocatedToken) $1, $4);
1025 // A const field requires a value to be provided
1026 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
1031 constant_initializer
1032 : constant_expression
1040 variable_declarators
1043 FullNamedExpression type = (FullNamedExpression) $3;
1044 if (type == TypeManager.system_void_expr)
1045 Report.Error (670, GetLocation ($3), "Fields cannot have void type");
1049 current_array_type = null;
1051 foreach (VariableDeclaration var in (ArrayList) $4){
1052 Field field = new Field (current_class, type, mod, var.identifier,
1053 (Attributes) $1, var.Location);
1055 field.Initializer = var.expression_or_array_initializer;
1057 if (RootContext.Documentation != null) {
1058 field.DocComment = Lexer.consume_doc_comment ();
1059 Lexer.doc_state = XmlCommentState.Allowed;
1061 current_container.AddField (field);
1062 $$ = field; // FIXME: might be better if it points to the top item
1069 fixed_variable_declarators
1072 FullNamedExpression type = (FullNamedExpression) $4;
1076 current_array_type = null;
1078 foreach (VariableDeclaration var in (ArrayList) $5) {
1079 FixedField field = new FixedField (current_class, type, mod, var.identifier,
1080 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
1082 if (RootContext.Documentation != null) {
1083 field.DocComment = Lexer.consume_doc_comment ();
1084 Lexer.doc_state = XmlCommentState.Allowed;
1086 current_container.AddField (field);
1087 $$ = field; // FIXME: might be better if it points to the top item
1096 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
1100 fixed_variable_declarators
1101 : fixed_variable_declarator
1103 ArrayList decl = new ArrayList (2);
1107 | fixed_variable_declarators COMMA fixed_variable_declarator
1109 ArrayList decls = (ArrayList) $1;
1115 fixed_variable_declarator
1116 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1118 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1120 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1122 Report.Error (443, lexer.Location, "Value or constant expected");
1123 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1127 variable_declarators
1128 : variable_declarator
1130 ArrayList decl = new ArrayList (4);
1135 | variable_declarators COMMA variable_declarator
1137 ArrayList decls = (ArrayList) $1;
1146 ++lexer.parsing_block;
1148 variable_initializer
1150 --lexer.parsing_block;
1151 $$ = new VariableDeclaration ((LocatedToken) $1, $4);
1155 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1157 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1159 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1160 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1165 variable_initializer
1174 | STACKALLOC type_expression OPEN_BRACKET expression CLOSE_BRACKET
1176 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1180 $$ = new ArglistAccess ((Location) $1);
1184 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1191 if (RootContext.Documentation != null)
1192 Lexer.doc_state = XmlCommentState.NotAllowed;
1196 Method method = (Method) $1;
1197 method.Block = (ToplevelBlock) $3;
1198 current_container.AddMethod (method);
1200 if (current_container.Kind == Kind.Interface && method.Block != null) {
1201 Report.Error (531, method.Location, "`{0}': interface members cannot have a definition", method.GetSignatureForError ());
1204 current_generic_method = null;
1205 current_local_parameters = null;
1207 if (RootContext.Documentation != null)
1208 Lexer.doc_state = XmlCommentState.Allowed;
1221 Report.Error (1585, lexer.Location,
1222 "Member modifier `{0}' must precede the member type and name",
1223 Modifiers.Name (i));
1234 type_and_void member_name
1237 arglist_allowed = true;
1239 opt_formal_parameter_list CLOSE_PARENS
1241 lexer.ConstraintsParsing = true;
1243 opt_type_parameter_constraints_clauses
1245 lexer.ConstraintsParsing = false;
1246 arglist_allowed = false;
1247 MemberName name = (MemberName) $4;
1248 current_local_parameters = (Parameters) $7;
1250 if ($10 != null && name.TypeArguments == null)
1251 Report.Error (80, lexer.Location,
1252 "Constraints are not allowed on non-generic declarations");
1256 GenericMethod generic = null;
1257 if (name.TypeArguments != null) {
1258 generic = new GenericMethod (current_namespace, current_class, name,
1259 (FullNamedExpression) $3, current_local_parameters);
1261 generic.SetParameterInfo ((ArrayList) $10);
1264 method = new Method (current_class, generic, (FullNamedExpression) $3, (int) $2,
1265 name, current_local_parameters, (Attributes) $1);
1267 current_generic_method = generic;
1269 if (RootContext.Documentation != null)
1270 method.DocComment = Lexer.consume_doc_comment ();
1278 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1280 lexer.ConstraintsParsing = true;
1282 opt_type_parameter_constraints_clauses
1284 lexer.ConstraintsParsing = false;
1286 MemberName name = (MemberName) $5;
1287 current_local_parameters = (Parameters) $7;
1289 if ($9 != null && name.TypeArguments == null)
1290 Report.Error (80, lexer.Location,
1291 "Constraints are not allowed on non-generic declarations");
1294 GenericMethod generic = null;
1295 if (name.TypeArguments != null) {
1296 generic = new GenericMethod (current_namespace, current_class, name,
1297 TypeManager.system_void_expr, current_local_parameters);
1299 generic.SetParameterInfo ((ArrayList) $10);
1302 int modifiers = (int) $2;
1305 const int invalid_partial_mod = Modifiers.Accessibility | Modifiers.ABSTRACT | Modifiers.EXTERN |
1306 Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
1308 if ((modifiers & invalid_partial_mod) != 0) {
1309 Report.Error (750, name.Location, "A partial method cannot define access modifier or " +
1310 "any of abstract, extern, new, override, sealed, or virtual modifiers");
1311 modifiers &= ~invalid_partial_mod;
1314 if ((current_class.ModFlags & Modifiers.PARTIAL) == 0) {
1315 Report.Error (751, name.Location, "A partial method must be declared within a " +
1316 "partial class or partial struct");
1319 modifiers |= Modifiers.PARTIAL | Modifiers.PRIVATE;
1321 method = new Method (current_class, generic, TypeManager.system_void_expr,
1322 modifiers, name, current_local_parameters, (Attributes) $1);
1324 current_generic_method = generic;
1326 if (RootContext.Documentation != null)
1327 method.DocComment = Lexer.consume_doc_comment ();
1334 modifiers member_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1336 MemberName name = (MemberName) $5;
1337 Report.Error (1585, name.Location,
1338 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1340 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1341 0, name, (Parameters) $7, (Attributes) $1);
1343 current_local_parameters = (Parameters) $7;
1345 if (RootContext.Documentation != null)
1346 method.DocComment = Lexer.consume_doc_comment ();
1354 | SEMICOLON { $$ = null; }
1357 opt_formal_parameter_list
1358 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1359 | formal_parameter_list
1362 opt_parameter_list_no_mod
1363 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1366 parameter_modifiers_not_allowed = true;
1368 formal_parameter_list
1370 parameter_modifiers_not_allowed = false;
1375 formal_parameter_list
1378 ArrayList pars_list = (ArrayList) $1;
1380 Parameter [] pars = new Parameter [pars_list.Count];
1381 pars_list.CopyTo (pars);
1383 $$ = new Parameters (pars);
1385 | fixed_parameters COMMA parameter_array
1387 ArrayList pars_list = (ArrayList) $1;
1390 Parameter [] pars = new Parameter [pars_list.Count];
1391 pars_list.CopyTo (pars);
1393 $$ = new Parameters (pars);
1395 | fixed_parameters COMMA arglist_modifier
1397 ArrayList pars_list = (ArrayList) $1;
1398 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1400 Parameter [] pars = new Parameter [pars_list.Count];
1401 pars_list.CopyTo (pars);
1403 $$ = new Parameters (pars, true);
1405 | parameter_array COMMA error
1408 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1411 | fixed_parameters COMMA parameter_array COMMA error
1414 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1417 | arglist_modifier COMMA error
1419 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1422 | fixed_parameters COMMA ARGLIST COMMA error
1424 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1429 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1433 $$ = new Parameters (new Parameter [] { new ArglistParameter ((Location) $1) }, true);
1440 ArrayList pars = new ArrayList (4);
1445 | fixed_parameters COMMA fixed_parameter
1447 ArrayList pars = (ArrayList) $1;
1448 Parameter p = (Parameter)$3;
1450 if (p.HasExtensionMethodModifier)
1451 Report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
1460 opt_parameter_modifier
1464 LocatedToken lt = (LocatedToken) $4;
1465 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1468 opt_parameter_modifier
1470 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1472 LocatedToken lt = (LocatedToken) $4;
1473 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1477 opt_parameter_modifier
1480 Report.Error (1001, GetLocation ($3), "Identifier expected");
1484 opt_parameter_modifier
1487 CheckIdentifierToken (yyToken, GetLocation ($4));
1491 opt_parameter_modifier
1497 LocatedToken lt = (LocatedToken) $4;
1498 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1503 opt_parameter_modifier
1504 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1505 | parameter_modifiers
1509 : parameter_modifier
1513 | parameter_modifiers parameter_modifier
1515 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1516 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1517 if (((Parameter.Modifier)$1 & p2) == p2) {
1518 Error_DuplicateParameterModifier (lexer.Location, p2);
1520 switch (mod & ~Parameter.Modifier.This) {
1521 case Parameter.Modifier.REF:
1522 Report.Error (1101, lexer.Location, "The parameter modifiers `this' and `ref' cannot be used altogether");
1524 case Parameter.Modifier.OUT:
1525 Report.Error (1102, lexer.Location, "The parameter modifiers `this' and `out' cannot be used altogether");
1528 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1539 if (parameter_modifiers_not_allowed)
1540 Error_ParameterModifierNotValid ("ref", (Location)$1);
1542 $$ = Parameter.Modifier.REF;
1546 if (parameter_modifiers_not_allowed)
1547 Error_ParameterModifierNotValid ("out", (Location)$1);
1549 $$ = Parameter.Modifier.OUT;
1553 if (parameter_modifiers_not_allowed)
1554 Error_ParameterModifierNotValid ("this", (Location)$1);
1556 if (RootContext.Version <= LanguageVersion.ISO_2)
1557 Report.FeatureIsNotAvailable (GetLocation ($1), "extension methods");
1559 $$ = Parameter.Modifier.This;
1564 : opt_attributes params_modifier type IDENTIFIER
1566 LocatedToken lt = (LocatedToken) $4;
1567 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1569 | opt_attributes params_modifier type error {
1570 CheckIdentifierToken (yyToken, GetLocation ($4));
1578 if (params_modifiers_not_allowed)
1579 Report.Error (1670, ((Location) $1), "The `params' modifier is not allowed in current context");
1581 | PARAMS parameter_modifier
1583 Parameter.Modifier mod = (Parameter.Modifier)$2;
1584 if ((mod & Parameter.Modifier.This) != 0) {
1585 Report.Error (1104, (Location)$1, "The parameter modifiers `this' and `params' cannot be used altogether");
1587 Report.Error (1611, (Location)$1, "The params parameter cannot be declared as ref or out");
1590 | PARAMS params_modifier
1592 Error_DuplicateParameterModifier ((Location)$1, Parameter.Modifier.PARAMS);
1599 if (!arglist_allowed)
1600 Report.Error (1669, (Location) $1, "__arglist is not valid in this context");
1604 property_declaration
1608 namespace_or_type_name
1610 if (RootContext.Documentation != null)
1611 tmpComment = Lexer.consume_doc_comment ();
1615 implicit_value_parameter_type = (FullNamedExpression) $3;
1616 lexer.PropertyParsing = true;
1618 accessor_declarations
1620 lexer.PropertyParsing = false;
1621 has_get = has_set = false;
1626 Accessors accessors = (Accessors) $8;
1627 Accessor get_block = accessors != null ? accessors.get_or_add : null;
1628 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
1629 bool order = accessors != null ? accessors.declared_in_reverse : false;
1631 MemberName name = (MemberName) $4;
1632 FullNamedExpression ptype = (FullNamedExpression) $3;
1634 if (name.TypeArguments != null)
1635 syntax_error (lexer.Location, "a property can't have type arguments");
1637 prop = new Property (current_class, ptype, (int) $2,
1638 name, (Attributes) $1, get_block, set_block, order, current_block);
1640 if (ptype == TypeManager.system_void_expr)
1641 Report.Error (547, name.Location, "`{0}': property or indexer cannot have void type", prop.GetSignatureForError ());
1643 if (accessors == null)
1644 Report.Error (548, prop.Location, "`{0}': property or indexer must have at least one accessor", prop.GetSignatureForError ());
1646 if (current_container.Kind == Kind.Interface) {
1647 if (prop.Get.Block != null)
1648 Report.Error (531, prop.Location, "`{0}.get': interface members cannot have a definition", prop.GetSignatureForError ());
1650 if (prop.Set.Block != null)
1651 Report.Error (531, prop.Location, "`{0}.set': interface members cannot have a definition", prop.GetSignatureForError ());
1654 current_container.AddProperty (prop);
1655 implicit_value_parameter_type = null;
1657 if (RootContext.Documentation != null)
1658 prop.DocComment = ConsumeStoredComment ();
1663 accessor_declarations
1664 : get_accessor_declaration
1666 $$ = new Accessors ((Accessor) $1, null);
1668 | get_accessor_declaration accessor_declarations
1670 Accessors accessors = (Accessors) $2;
1671 accessors.get_or_add = (Accessor) $1;
1674 | set_accessor_declaration
1676 $$ = new Accessors (null, (Accessor) $1);
1678 | set_accessor_declaration accessor_declarations
1680 Accessors accessors = (Accessors) $2;
1681 accessors.set_or_remove = (Accessor) $1;
1682 accessors.declared_in_reverse = true;
1687 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1692 get_accessor_declaration
1693 : opt_attributes opt_modifiers GET
1695 // If this is not the case, then current_local_parameters has already
1696 // been set in indexer_declaration
1697 if (parsing_indexer == false)
1698 current_local_parameters = null;
1700 current_local_parameters = indexer_parameters;
1701 lexer.PropertyParsing = false;
1706 Report.Error (1007, (Location) $3, "Property accessor already defined");
1709 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, current_local_parameters, (Location) $3);
1711 current_local_parameters = null;
1712 lexer.PropertyParsing = true;
1714 if (RootContext.Documentation != null)
1715 if (Lexer.doc_state == XmlCommentState.Error)
1716 Lexer.doc_state = XmlCommentState.NotAllowed;
1722 set_accessor_declaration
1723 : opt_attributes opt_modifiers SET
1725 Parameter implicit_value_parameter = new Parameter (
1726 implicit_value_parameter_type, "value",
1727 Parameter.Modifier.NONE, null, (Location) $3);
1729 if (!parsing_indexer) {
1730 current_local_parameters = new Parameters (new Parameter [] { implicit_value_parameter });
1732 current_local_parameters = Parameters.MergeGenerated (
1733 indexer_parameters, true, implicit_value_parameter, null);
1736 lexer.PropertyParsing = false;
1741 Report.Error (1007, ((LocatedToken) $3).Location, "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 && Lexer.doc_state == XmlCommentState.Error)
1751 Lexer.doc_state = XmlCommentState.NotAllowed;
1759 | SEMICOLON { $$ = null; }
1762 interface_declaration
1768 lexer.ConstraintsParsing = true;
1772 MemberName name = MakeName ((MemberName) $6);
1773 push_current_class (new Interface (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
1776 opt_type_parameter_constraints_clauses
1778 lexer.ConstraintsParsing = false;
1780 current_class.SetParameterInfo ((ArrayList) $9);
1782 if (RootContext.Documentation != null) {
1783 current_container.DocComment = Lexer.consume_doc_comment ();
1784 Lexer.doc_state = XmlCommentState.Allowed;
1789 if (RootContext.Documentation != null)
1790 Lexer.doc_state = XmlCommentState.Allowed;
1794 $$ = pop_current_class ();
1796 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1797 CheckIdentifierToken (yyToken, GetLocation ($5));
1803 opt_interface_member_declarations
1807 opt_interface_member_declarations
1809 | interface_member_declarations
1812 interface_member_declarations
1813 : interface_member_declaration
1814 | interface_member_declarations interface_member_declaration
1817 interface_member_declaration
1818 : constant_declaration
1820 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1824 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1826 | method_declaration
1827 | property_declaration
1829 | indexer_declaration
1830 | operator_declaration
1832 Report.Error (567, GetLocation ($1), "Interfaces cannot contain operators");
1834 | constructor_declaration
1836 Report.Error (526, GetLocation ($1), "Interfaces cannot contain contructors");
1840 Report.Error (524, GetLocation ($1), "Interfaces cannot declare classes, structs, interfaces, delegates, or enumerations");
1844 operator_declaration
1845 : opt_attributes opt_modifiers operator_declarator
1853 OperatorDeclaration decl = (OperatorDeclaration) $3;
1854 Operator op = new Operator (
1855 current_class, decl.optype, decl.ret_type, (int) $2,
1856 current_local_parameters,
1857 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1859 if (RootContext.Documentation != null) {
1860 op.DocComment = tmpComment;
1861 Lexer.doc_state = XmlCommentState.Allowed;
1864 // Note again, checking is done in semantic analysis
1865 current_container.AddOperator (op);
1867 current_local_parameters = null;
1873 | SEMICOLON { $$ = null; }
1877 : type_expression_or_array
1880 Report.Error (590, lexer.Location, "User-defined operators cannot return void");
1881 $$ = TypeManager.system_void_expr;
1886 : operator_type OPERATOR overloadable_operator OPEN_PARENS
1888 params_modifiers_not_allowed = true;
1890 opt_parameter_list_no_mod CLOSE_PARENS
1892 params_modifiers_not_allowed = false;
1894 Location loc = (Location) $2;
1895 Operator.OpType op = (Operator.OpType) $3;
1896 current_local_parameters = (Parameters)$6;
1898 int p_count = current_local_parameters.Count;
1900 if (op == Operator.OpType.Addition)
1901 op = Operator.OpType.UnaryPlus;
1902 else if (op == Operator.OpType.Subtraction)
1903 op = Operator.OpType.UnaryNegation;
1906 if (IsUnaryOperator (op)) {
1908 Report.Error (1020, loc, "Overloadable binary operator expected");
1909 } else if (p_count != 1) {
1910 Report.Error (1535, loc, "Overloaded unary operator `{0}' takes one parameter",
1911 Operator.GetName (op));
1915 Report.Error (1534, loc, "Overloaded binary operator `{0}' takes two parameters",
1916 Operator.GetName (op));
1917 } else if (p_count != 2) {
1918 Report.Error (1019, loc, "Overloadable unary operator expected");
1922 if (RootContext.Documentation != null) {
1923 tmpComment = Lexer.consume_doc_comment ();
1924 Lexer.doc_state = XmlCommentState.NotAllowed;
1927 $$ = new OperatorDeclaration (op, (FullNamedExpression) $1, loc);
1929 | conversion_operator_declarator
1932 overloadable_operator
1934 : BANG { $$ = Operator.OpType.LogicalNot; }
1935 | TILDE { $$ = Operator.OpType.OnesComplement; }
1936 | OP_INC { $$ = Operator.OpType.Increment; }
1937 | OP_DEC { $$ = Operator.OpType.Decrement; }
1938 | TRUE { $$ = Operator.OpType.True; }
1939 | FALSE { $$ = Operator.OpType.False; }
1940 // Unary and binary:
1941 | PLUS { $$ = Operator.OpType.Addition; }
1942 | MINUS { $$ = Operator.OpType.Subtraction; }
1944 | STAR { $$ = Operator.OpType.Multiply; }
1945 | DIV { $$ = Operator.OpType.Division; }
1946 | PERCENT { $$ = Operator.OpType.Modulus; }
1947 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1948 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1949 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1950 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1951 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1952 | OP_EQ { $$ = Operator.OpType.Equality; }
1953 | OP_NE { $$ = Operator.OpType.Inequality; }
1954 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1955 | OP_LT { $$ = Operator.OpType.LessThan; }
1956 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1957 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1960 conversion_operator_declarator
1961 : IMPLICIT OPERATOR type OPEN_PARENS
1963 params_modifiers_not_allowed = true;
1965 opt_parameter_list_no_mod CLOSE_PARENS
1967 params_modifiers_not_allowed = false;
1969 Location loc = (Location) $2;
1970 current_local_parameters = (Parameters)$6;
1972 if (RootContext.Documentation != null) {
1973 tmpComment = Lexer.consume_doc_comment ();
1974 Lexer.doc_state = XmlCommentState.NotAllowed;
1977 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (FullNamedExpression) $3, loc);
1979 | EXPLICIT OPERATOR type OPEN_PARENS
1981 params_modifiers_not_allowed = true;
1983 opt_parameter_list_no_mod CLOSE_PARENS
1985 params_modifiers_not_allowed = false;
1987 Location loc = (Location) $2;
1988 current_local_parameters = (Parameters)$6;
1990 if (RootContext.Documentation != null) {
1991 tmpComment = Lexer.consume_doc_comment ();
1992 Lexer.doc_state = XmlCommentState.NotAllowed;
1995 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (FullNamedExpression) $3, loc);
1999 syntax_error ((Location) $1, "'operator' expected");
2003 syntax_error ((Location) $1, "'operator' expected");
2007 constructor_declaration
2008 : constructor_declarator
2011 Constructor c = (Constructor) $1;
2012 c.Block = (ToplevelBlock) $2;
2014 if (RootContext.Documentation != null)
2015 c.DocComment = ConsumeStoredComment ();
2017 current_container.AddConstructor (c);
2019 current_local_parameters = null;
2020 if (RootContext.Documentation != null)
2021 Lexer.doc_state = XmlCommentState.Allowed;
2025 constructor_declarator
2030 if (RootContext.Documentation != null) {
2031 tmpComment = Lexer.consume_doc_comment ();
2032 Lexer.doc_state = XmlCommentState.Allowed;
2035 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2037 current_local_parameters = (Parameters) $6;
2040 // start block here, so possible anonymous methods inside
2041 // constructor initializer can get correct parent block
2043 start_block (lexer.Location);
2045 opt_constructor_initializer
2047 LocatedToken lt = (LocatedToken) $3;
2048 int mods = (int) $2;
2049 ConstructorInitializer ci = (ConstructorInitializer) $9;
2051 Constructor c = new Constructor (current_class, lt.Value, mods,
2052 (Attributes) $1, current_local_parameters, ci, lt.Location);
2054 if (lt.Value != current_container.MemberName.Name) {
2055 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
2056 } else if ((mods & Modifiers.STATIC) != 0) {
2057 if ((mods & Modifiers.Accessibility) != 0){
2058 Report.Error (515, c.Location,
2059 "`{0}': static constructor cannot have an access modifier",
2060 c.GetSignatureForError ());
2063 Report.Error (514, c.Location,
2064 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2065 c.GetSignatureForError ());
2076 | SEMICOLON { current_block = null; $$ = null; }
2079 opt_constructor_initializer
2081 | constructor_initializer
2084 constructor_initializer
2085 : COLON BASE OPEN_PARENS
2087 ++lexer.parsing_block;
2089 opt_argument_list CLOSE_PARENS
2091 --lexer.parsing_block;
2092 $$ = new ConstructorBaseInitializer ((ArrayList) $5, (Location) $2);
2094 | COLON THIS OPEN_PARENS
2096 ++lexer.parsing_block;
2098 opt_argument_list CLOSE_PARENS
2100 --lexer.parsing_block;
2101 $$ = new ConstructorThisInitializer ((ArrayList) $5, (Location) $2);
2104 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2109 destructor_declaration
2110 : opt_attributes opt_modifiers TILDE
2112 if (RootContext.Documentation != null) {
2113 tmpComment = Lexer.consume_doc_comment ();
2114 Lexer.doc_state = XmlCommentState.NotAllowed;
2117 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2119 LocatedToken lt = (LocatedToken) $5;
2120 if (lt.Value != current_container.MemberName.Name){
2121 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2122 } else if (current_container.Kind != Kind.Class){
2123 Report.Error (575, lt.Location, "Only class types can contain destructor");
2125 Method d = new Destructor (
2126 current_class, TypeManager.system_void_expr, (int) $2, "Finalize",
2127 Parameters.EmptyReadOnlyParameters, (Attributes) $1, lt.Location);
2128 if (RootContext.Documentation != null)
2129 d.DocComment = ConsumeStoredComment ();
2131 d.Block = (ToplevelBlock) $8;
2132 current_container.AddMethod (d);
2140 EVENT type variable_declarators SEMICOLON
2142 current_array_type = null;
2143 foreach (VariableDeclaration var in (ArrayList) $5) {
2145 MemberName name = new MemberName (var.identifier,
2148 EventField e = new EventField (
2149 current_class, (FullNamedExpression) $4, (int) $2, name,
2152 if (var.expression_or_array_initializer != null) {
2153 if (current_container.Kind == Kind.Interface) {
2154 Report.Error (68, e.Location, "`{0}': event in interface cannot have initializer", e.GetSignatureForError ());
2157 e.Initializer = var.expression_or_array_initializer;
2160 current_container.AddEvent (e);
2162 if (RootContext.Documentation != null) {
2163 e.DocComment = Lexer.consume_doc_comment ();
2164 Lexer.doc_state = XmlCommentState.Allowed;
2170 EVENT type namespace_or_type_name
2173 implicit_value_parameter_type = (FullNamedExpression) $4;
2174 current_local_parameters = new Parameters (
2175 new Parameter (implicit_value_parameter_type, "value",
2176 Parameter.Modifier.NONE, null, GetLocation ($3)));
2178 lexer.EventParsing = true;
2180 event_accessor_declarations
2182 lexer.EventParsing = false;
2186 MemberName name = (MemberName) $5;
2188 if (current_container.Kind == Kind.Interface) {
2189 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
2193 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2194 current_container.Name, name.GetSignatureForError ());
2197 Accessors accessors = (Accessors) $8;
2199 if (name.TypeArguments != null)
2200 syntax_error (lexer.Location, "an event can't have type arguments");
2202 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2203 // CS0073 is already reported, so no CS0065 here.
2206 Event e = new EventProperty (
2207 current_class, (FullNamedExpression) $4, (int) $2, name,
2208 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2209 if (RootContext.Documentation != null) {
2210 e.DocComment = Lexer.consume_doc_comment ();
2211 Lexer.doc_state = XmlCommentState.Allowed;
2214 current_container.AddEvent (e);
2215 implicit_value_parameter_type = null;
2218 current_local_parameters = null;
2220 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2221 MemberName mn = (MemberName) $5;
2223 if (mn.Left != null)
2224 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2226 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2228 if (RootContext.Documentation != null)
2229 Lexer.doc_state = XmlCommentState.Allowed;
2233 event_accessor_declarations
2234 : add_accessor_declaration remove_accessor_declaration
2236 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2238 | remove_accessor_declaration add_accessor_declaration
2240 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2241 accessors.declared_in_reverse = true;
2244 | add_accessor_declaration { $$ = null; }
2245 | remove_accessor_declaration { $$ = null; }
2248 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2254 add_accessor_declaration
2255 : opt_attributes ADD
2257 lexer.EventParsing = false;
2261 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, (Location) $2);
2262 lexer.EventParsing = true;
2265 | opt_attributes ADD error {
2266 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2269 | opt_attributes modifiers ADD {
2270 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2275 remove_accessor_declaration
2276 : opt_attributes REMOVE
2278 lexer.EventParsing = false;
2282 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, (Location) $2);
2283 lexer.EventParsing = true;
2285 | opt_attributes REMOVE error {
2286 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2289 | opt_attributes modifiers REMOVE {
2290 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2296 : opt_attributes opt_modifiers indexer_declarator
2299 IndexerDeclaration decl = (IndexerDeclaration) $3;
2301 implicit_value_parameter_type = decl.type;
2303 lexer.PropertyParsing = true;
2304 parsing_indexer = true;
2306 indexer_parameters = decl.param_list;
2308 accessor_declarations
2310 lexer.PropertyParsing = false;
2311 has_get = has_set = false;
2312 parsing_indexer = false;
2316 // The signature is computed from the signature of the indexer. Look
2317 // at section 3.6 on the spec
2319 IndexerDeclaration decl = (IndexerDeclaration) $3;
2320 Location loc = decl.location;
2321 Accessors accessors = (Accessors) $6;
2322 Accessor get_block = accessors != null ? accessors.get_or_add : null;
2323 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
2324 bool order = accessors != null ? accessors.declared_in_reverse : false;
2327 if (decl.interface_type != null)
2328 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2330 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2332 indexer = new Indexer (current_class, decl.type, name,
2333 (int) $2, decl.param_list, (Attributes) $1,
2334 get_block, set_block, order);
2336 if (decl.type == TypeManager.system_void_expr)
2337 Report.Error (620, loc, "`{0}': indexer return type cannot be `void'", indexer.GetSignatureForError ());
2339 if (accessors == null)
2340 Report.Error (548, indexer.Location, "`{0}': property or indexer must have at least one accessor", indexer.GetSignatureForError ());
2342 if (current_container.Kind == Kind.Interface) {
2343 if (indexer.Get.Block != null)
2344 Report.Error (531, indexer.Location, "`{0}.get': interface members cannot have a definition", indexer.GetSignatureForError ());
2346 if (indexer.Set.Block != null)
2347 Report.Error (531, indexer.Location, "`{0}.set': interface members cannot have a definition", indexer.GetSignatureForError ());
2350 if (RootContext.Documentation != null)
2351 indexer.DocComment = ConsumeStoredComment ();
2353 current_container.AddIndexer (indexer);
2355 current_local_parameters = null;
2356 implicit_value_parameter_type = null;
2357 indexer_parameters = null;
2362 : type_and_void THIS OPEN_BRACKET opt_parameter_list_no_mod CLOSE_BRACKET
2364 Parameters pars = (Parameters) $4;
2366 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2368 if (RootContext.Documentation != null) {
2369 tmpComment = Lexer.consume_doc_comment ();
2370 Lexer.doc_state = XmlCommentState.Allowed;
2373 $$ = new IndexerDeclaration ((FullNamedExpression) $1, null, pars, (Location) $2);
2375 | type_and_void namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2377 Parameters pars = (Parameters) $6;
2379 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2382 MemberName name = (MemberName) $2;
2383 $$ = new IndexerDeclaration ((FullNamedExpression) $1, name, pars, (Location) $4);
2385 if (RootContext.Documentation != null) {
2386 tmpComment = Lexer.consume_doc_comment ();
2387 Lexer.doc_state = XmlCommentState.Allowed;
2397 if (RootContext.Documentation != null)
2398 enumTypeComment = Lexer.consume_doc_comment ();
2403 MemberName name = (MemberName) $4;
2404 if (name.IsGeneric) {
2405 Report.Error (1675, name.Location, "Enums cannot have type parameters");
2408 name = MakeName (name);
2409 Enum e = new Enum (current_namespace, current_class, (TypeExpr) $5, (int) $2,
2410 name, (Attributes) $1);
2412 if (RootContext.Documentation != null)
2413 e.DocComment = enumTypeComment;
2416 EnumMember em = null;
2417 foreach (VariableDeclaration ev in (ArrayList) $7) {
2418 em = new EnumMember (
2419 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2420 ev.OptAttributes, ev.Location);
2422 // if (RootContext.Documentation != null)
2423 em.DocComment = ev.DocComment;
2425 e.AddEnumMember (em);
2427 if (RootContext.EvalMode)
2428 undo.AddTypeContainer (current_container, e);
2430 current_container.AddTypeContainer (e);
2438 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2441 if ($2 != TypeManager.system_int32_expr && $2 != TypeManager.system_uint32_expr &&
2442 $2 != TypeManager.system_int64_expr && $2 != TypeManager.system_uint64_expr &&
2443 $2 != TypeManager.system_int16_expr && $2 != TypeManager.system_uint16_expr &&
2444 $2 != TypeManager.system_byte_expr && $2 != TypeManager.system_sbyte_expr)
2445 Enum.Error_1008 (GetLocation ($2));
2451 Error_TypeExpected (lexer.Location);
2458 if (RootContext.Documentation != null)
2459 Lexer.doc_state = XmlCommentState.Allowed;
2461 opt_enum_member_declarations
2463 // here will be evaluated after CLOSE_BLACE is consumed.
2464 if (RootContext.Documentation != null)
2465 Lexer.doc_state = XmlCommentState.Allowed;
2473 opt_enum_member_declarations
2474 : /* empty */ { $$ = new ArrayList (4); }
2475 | enum_member_declarations opt_comma { $$ = $1; }
2478 enum_member_declarations
2479 : enum_member_declaration
2481 ArrayList l = new ArrayList (4);
2486 | enum_member_declarations COMMA enum_member_declaration
2488 ArrayList l = (ArrayList) $1;
2496 enum_member_declaration
2497 : opt_attributes IDENTIFIER
2499 VariableDeclaration vd = new VariableDeclaration (
2500 (LocatedToken) $2, null, (Attributes) $1);
2502 if (RootContext.Documentation != null) {
2503 vd.DocComment = Lexer.consume_doc_comment ();
2504 Lexer.doc_state = XmlCommentState.Allowed;
2509 | opt_attributes IDENTIFIER
2511 if (RootContext.Documentation != null) {
2512 tmpComment = Lexer.consume_doc_comment ();
2513 Lexer.doc_state = XmlCommentState.NotAllowed;
2516 ASSIGN constant_expression
2518 VariableDeclaration vd = new VariableDeclaration (
2519 (LocatedToken) $2, $5, (Attributes) $1);
2521 if (RootContext.Documentation != null)
2522 vd.DocComment = ConsumeStoredComment ();
2528 delegate_declaration
2532 type_and_void type_name
2533 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2535 MemberName name = MakeName ((MemberName) $5);
2536 Parameters p = (Parameters) $7;
2538 Delegate del = new Delegate (current_namespace, current_class, (FullNamedExpression) $4,
2539 (int) $2, name, p, (Attributes) $1);
2541 if (RootContext.Documentation != null) {
2542 del.DocComment = Lexer.consume_doc_comment ();
2543 Lexer.doc_state = XmlCommentState.Allowed;
2546 current_container.AddDelegate (del);
2547 current_delegate = del;
2548 lexer.ConstraintsParsing = true;
2550 opt_type_parameter_constraints_clauses
2552 lexer.ConstraintsParsing = false;
2556 current_delegate.SetParameterInfo ((ArrayList) $10);
2557 $$ = current_delegate;
2559 current_delegate = null;
2570 if (RootContext.Version < LanguageVersion.ISO_2)
2571 Report.FeatureIsNotAvailable (lexer.Location, "nullable types");
2577 namespace_or_type_name
2578 : IDENTIFIER opt_type_argument_list
2580 LocatedToken lt = (LocatedToken) $1;
2581 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2583 | IDENTIFIER DOUBLE_COLON IDENTIFIER opt_type_argument_list {
2584 LocatedToken lt1 = (LocatedToken) $1;
2585 LocatedToken lt2 = (LocatedToken) $3;
2586 if (RootContext.Version == LanguageVersion.ISO_1)
2587 Report.FeatureIsNotAvailable (lt1.Location, "namespace alias qualifier");
2589 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $4, lt1.Location);
2591 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2592 LocatedToken lt = (LocatedToken) $3;
2593 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2598 : IDENTIFIER opt_type_parameter_list
2600 LocatedToken lt = (LocatedToken) $1;
2601 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2603 | namespace_or_type_name DOT IDENTIFIER opt_type_parameter_list
2605 LocatedToken lt = (LocatedToken) $3;
2606 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2611 : IDENTIFIER opt_type_parameter_list
2613 LocatedToken lt = (LocatedToken) $1;
2614 $$ = new MemberName (lt.Value, (TypeArguments)$2, lt.Location);
2619 // Generics arguments (any type, without attributes)
2622 opt_type_argument_list
2623 : /* empty */ { $$ = null; }
2624 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2626 if (RootContext.Version < LanguageVersion.ISO_2)
2627 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2631 | OP_GENERICS_LT error
2633 Error_TypeExpected (lexer.Location);
2638 // Generics parameters (identifiers only, with attributes), used in type, method declarations
2641 opt_type_parameter_list
2642 : /* empty */ { $$ = null; }
2643 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2645 if (RootContext.Version < LanguageVersion.ISO_2)
2646 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2650 | OP_GENERICS_LT error
2652 Error_TypeExpected (lexer.Location);
2659 TypeArguments type_args = new TypeArguments (lexer.Location);
2660 type_args.Add ((Expression) $1);
2663 | type_arguments COMMA type_argument
2665 TypeArguments type_args = (TypeArguments) $1;
2666 type_args.Add ((Expression) $3);
2676 | attribute_sections type
2678 SimpleName sn = $2 as SimpleName;
2680 Error_TypeExpected (GetLocation ($2));
2682 $2 = new TypeParameterName (sn.Name, (Attributes) $1, lexer.Location);
2688 // All types where void is allowed
2691 : type_expression_or_array
2694 $$ = TypeManager.system_void_expr;
2699 // A type which does not allow `void' to be used
2702 : type_expression_or_array
2705 Expression.Error_VoidInvalidInTheContext (lexer.Location);
2706 $$ = TypeManager.system_void_expr;
2710 type_expression_or_array
2716 : namespace_or_type_name opt_nullable
2718 MemberName name = (MemberName) $1;
2721 $$ = new ComposedCast (name.GetTypeExpression (), "?", lexer.Location);
2723 if (name.Left == null && name.Name == "var" &&
2724 (RootContext.Version > LanguageVersion.ISO_2 || RootContext.Version == LanguageVersion.Default_MCS))
2725 $$ = current_array_type = new VarExpr (name.Location);
2727 $$ = name.GetTypeExpression ();
2730 | builtin_types opt_nullable
2733 $$ = new ComposedCast ((FullNamedExpression) $1, "?", lexer.Location);
2735 | type_expression STAR
2738 // Note that here only unmanaged types are allowed but we
2739 // can't perform checks during this phase - we do it during
2740 // semantic analysis.
2742 $$ = new ComposedCast ((FullNamedExpression) $1, "*", Lexer.Location);
2746 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2751 : builtin_types opt_nullable
2754 $$ = new ComposedCast ((FullNamedExpression) $1, "?", lexer.Location);
2758 $$ = TypeManager.system_void_expr;
2760 | non_expression_type rank_specifiers
2762 Location loc = GetLocation ($1);
2764 loc = lexer.Location;
2765 $$ = new ComposedCast ((FullNamedExpression) $1, (string) $2, loc);
2767 | non_expression_type STAR
2769 Location loc = GetLocation ($1);
2771 loc = lexer.Location;
2772 $$ = new ComposedCast ((FullNamedExpression) $1, "*", loc);
2775 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2776 // through this different path
2778 | multiplicative_expression STAR
2780 FullNamedExpression e = $1 as FullNamedExpression;
2782 $$ = new ComposedCast (e, "*");
2784 Error_TypeExpected (GetLocation ($1));
2791 ArrayList types = new ArrayList (2);
2795 | type_list COMMA base_type_name
2797 ArrayList types = (ArrayList) $1;
2806 if ($1 is ComposedCast)
2807 Report.Error (1521, GetLocation ($1), "Invalid base type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
2812 Error_TypeExpected (lexer.Location);
2817 * replaces all the productions for isolating the various
2818 * simple types, but we need this to reuse it easily in local_variable_type
2821 : OBJECT { $$ = TypeManager.system_object_expr; }
2822 | STRING { $$ = TypeManager.system_string_expr; }
2823 | BOOL { $$ = TypeManager.system_boolean_expr; }
2824 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2825 | FLOAT { $$ = TypeManager.system_single_expr; }
2826 | DOUBLE { $$ = TypeManager.system_double_expr; }
2831 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2832 | BYTE { $$ = TypeManager.system_byte_expr; }
2833 | SHORT { $$ = TypeManager.system_int16_expr; }
2834 | USHORT { $$ = TypeManager.system_uint16_expr; }
2835 | INT { $$ = TypeManager.system_int32_expr; }
2836 | UINT { $$ = TypeManager.system_uint32_expr; }
2837 | LONG { $$ = TypeManager.system_int64_expr; }
2838 | ULONG { $$ = TypeManager.system_uint64_expr; }
2839 | CHAR { $$ = TypeManager.system_char_expr; }
2843 : type_expression rank_specifiers
2845 string rank_specifiers = (string) $2;
2846 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank_specifiers);
2854 $$ = TypeManager.system_void_expr;
2859 // Expressions, section 7.5
2868 MemberName mn = (MemberName) $1;
2869 $$ = mn.GetTypeExpression ();
2871 | IDENTIFIER DOUBLE_COLON IDENTIFIER opt_type_argument_list
2873 LocatedToken lt1 = (LocatedToken) $1;
2874 LocatedToken lt2 = (LocatedToken) $3;
2875 if (RootContext.Version == LanguageVersion.ISO_1)
2876 Report.FeatureIsNotAvailable (lt1.Location, "namespace alias qualifier");
2878 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $4, lt1.Location);
2880 | parenthesized_expression
2881 | default_value_expression
2883 | invocation_expression
2887 | post_increment_expression
2888 | post_decrement_expression
2892 | checked_expression
2893 | unchecked_expression
2894 | pointer_member_access
2895 | anonymous_method_expression
2902 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2903 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2904 | NULL { $$ = new NullLiteral (lexer.Location); }
2908 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2909 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2910 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2915 object v = lexer.Value;
2918 $$ = new IntLiteral ((int) v, lexer.Location);
2919 } else if (v is uint)
2920 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2922 $$ = new LongLiteral ((Int64) v, lexer.Location);
2923 else if (v is ulong)
2924 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2926 Console.WriteLine ("OOPS. Unexpected result from scanner");
2931 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2932 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2935 parenthesized_expression_0
2936 : OPEN_PARENS expression CLOSE_PARENS
2939 lexer.Deambiguate_CloseParens ($$);
2940 // After this, the next token returned is one of
2941 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST (CLOSE_PARENS), CLOSE_PARENS_OPEN_PARENS
2942 // or CLOSE_PARENS_MINUS.
2944 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2947 parenthesized_expression
2948 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2952 | parenthesized_expression_0 CLOSE_PARENS
2956 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2958 // If a parenthesized expression is followed by a minus, we need to wrap
2959 // the expression inside a ParenthesizedExpression for the CS0075 check
2960 // in Binary.DoResolve().
2961 $$ = new ParenthesizedExpression ((Expression) $1);
2966 : primary_expression DOT IDENTIFIER opt_type_argument_list
2968 LocatedToken lt = (LocatedToken) $3;
2969 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
2971 | predefined_type DOT IDENTIFIER opt_type_argument_list
2973 LocatedToken lt = (LocatedToken) $3;
2974 // TODO: Location is wrong as some predefined types doesn't hold a location
2975 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
2979 invocation_expression
2980 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2983 Report.Error (1, (Location) $2, "Parse error");
2985 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2987 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2989 $$ = new Invocation ((Expression) $1, new ArrayList ());
2991 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2993 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2995 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2997 ArrayList args = new ArrayList (1);
2999 $$ = new Invocation ((Expression) $1, args);
3001 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
3003 ArrayList args = ((ArrayList) $4);
3005 $$ = new Invocation ((Expression) $1, args);
3009 opt_object_or_collection_initializer
3010 : /* empty */ { $$ = null; }
3011 | object_or_collection_initializer
3014 object_or_collection_initializer
3015 : OPEN_BRACE opt_member_initializer_list CLOSE_BRACE
3018 $$ = CollectionOrObjectInitializers.Empty;
3020 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3022 | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3024 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3028 opt_member_initializer_list
3029 : /* empty */ { $$ = null; }
3030 | member_initializer_list
3036 member_initializer_list
3037 : member_initializer
3039 ArrayList a = new ArrayList ();
3043 | member_initializer_list COMMA member_initializer
3045 ArrayList a = (ArrayList)$1;
3052 : IDENTIFIER ASSIGN initializer_value
3054 LocatedToken lt = $1 as LocatedToken;
3055 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3057 | non_assignment_expression
3059 $$ = new CollectionElementInitializer ((Expression)$1);
3061 | OPEN_BRACE expression_list CLOSE_BRACE
3063 $$ = new CollectionElementInitializer ((ArrayList)$2, GetLocation ($1));
3065 | OPEN_BRACE CLOSE_BRACE
3067 Report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3073 | object_or_collection_initializer
3077 : /* empty */ { $$ = null; }
3084 ArrayList list = new ArrayList (4);
3088 | argument_list COMMA argument
3090 ArrayList list = (ArrayList) $1;
3094 | argument_list COMMA
3096 Report.Error (839, GetLocation ($2), "An argument is missing");
3101 Report.Error (839, GetLocation ($1), "An argument is missing");
3104 | argument_list error {
3105 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
3113 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
3115 | non_simple_argument
3122 : REF variable_reference
3124 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3126 | OUT variable_reference
3128 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3130 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3132 ArrayList list = (ArrayList) $3;
3133 Argument[] args = new Argument [list.Count];
3134 list.CopyTo (args, 0);
3136 Expression expr = new Arglist (args, (Location) $1);
3137 $$ = new Argument (expr, Argument.AType.Expression);
3139 | ARGLIST OPEN_PARENS CLOSE_PARENS
3141 $$ = new Argument (new Arglist ((Location) $1), Argument.AType.Expression);
3145 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
3150 : expression { note ("section 5.4"); $$ = $1; }
3154 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3156 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
3158 | primary_expression rank_specifiers
3160 // So the super-trick is that primary_expression
3161 // can only be either a SimpleName or a MemberAccess.
3162 // The MemberAccess case arises when you have a fully qualified type-name like :
3164 // SimpleName is when you have
3167 Expression expr = (Expression) $1;
3168 if (expr is ComposedCast){
3169 $$ = new ComposedCast ((ComposedCast)expr, (string) $2);
3170 } else if (expr is ATypeNameExpression){
3172 // So we extract the string corresponding to the SimpleName
3175 $$ = new ComposedCast ((ATypeNameExpression)expr, (string) $2);
3177 Error_ExpectingTypeName (expr);
3178 $$ = TypeManager.system_object_expr;
3181 current_array_type = (FullNamedExpression)$$;
3188 ArrayList list = new ArrayList (4);
3192 | expression_list COMMA expression
3194 ArrayList list = (ArrayList) $1;
3203 $$ = new This (current_block, (Location) $1);
3208 : BASE DOT IDENTIFIER opt_type_argument_list
3210 LocatedToken lt = (LocatedToken) $3;
3211 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3213 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
3215 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
3218 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
3223 post_increment_expression
3224 : primary_expression OP_INC
3226 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
3227 (Expression) $1, (Location) $2);
3231 post_decrement_expression
3232 : primary_expression OP_DEC
3234 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
3235 (Expression) $1, (Location) $2);
3240 : object_or_delegate_creation_expression
3241 | array_creation_expression
3242 | anonymous_type_expression
3245 object_or_delegate_creation_expression
3246 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3249 if (RootContext.Version <= LanguageVersion.ISO_2)
3250 Report.FeatureIsNotAvailable (GetLocation ($1), "object initializers");
3252 $$ = new NewInitialize ((Expression) $2, (ArrayList) $4, (CollectionOrObjectInitializers) $6, (Location) $1);
3255 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
3257 | NEW type object_or_collection_initializer
3259 if (RootContext.Version <= LanguageVersion.ISO_2)
3260 Report.FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
3262 $$ = new NewInitialize ((Expression) $2, null, (CollectionOrObjectInitializers) $3, (Location) $1);
3266 array_creation_expression
3267 : NEW type_expression OPEN_BRACKET expression_list CLOSE_BRACKET
3269 opt_array_initializer
3271 $$ = new ArrayCreation ((FullNamedExpression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
3273 | NEW type_expression rank_specifiers array_initializer
3275 $$ = new ArrayCreation ((FullNamedExpression) $2, (string) $3, (ArrayList) $4, (Location) $1);
3277 | NEW rank_specifiers array_initializer
3279 $$ = new ImplicitlyTypedArrayCreation ((string) $2, (ArrayList) $3, (Location) $1);
3283 Report.Error (1031, (Location) $1, "Type expected");
3286 | NEW type_expression error
3288 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
3293 anonymous_type_expression
3294 : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
3296 if (RootContext.Version <= LanguageVersion.ISO_2)
3297 Report.FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
3299 $$ = new AnonymousTypeDeclaration ((ArrayList) $3, current_container, GetLocation ($1));
3303 anonymous_type_parameters_opt_comma
3304 : anonymous_type_parameters_opt
3305 | anonymous_type_parameters COMMA
3308 anonymous_type_parameters_opt
3310 | anonymous_type_parameters
3313 anonymous_type_parameters
3314 : anonymous_type_parameter
3316 ArrayList a = new ArrayList (4);
3320 | anonymous_type_parameters COMMA anonymous_type_parameter
3322 ArrayList a = (ArrayList) $1;
3328 anonymous_type_parameter
3329 : IDENTIFIER ASSIGN variable_initializer
3331 LocatedToken lt = (LocatedToken)$1;
3332 $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
3336 LocatedToken lt = (LocatedToken)$1;
3337 $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
3338 lt.Value, lt.Location);
3340 | BASE DOT IDENTIFIER opt_type_argument_list
3342 LocatedToken lt = (LocatedToken) $3;
3343 BaseAccess ba = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3344 $$ = new AnonymousTypeParameter (ba, lt.Value, lt.Location);
3348 MemberAccess ma = (MemberAccess) $1;
3349 $$ = new AnonymousTypeParameter (ma, ma.Name, ma.Location);
3353 Report.Error (746, lexer.Location, "Invalid anonymous type member declarator. " +
3354 "Anonymous type members must be a member assignment, simple name or member access expression");
3369 opt_rank_specifier_or_nullable
3377 | opt_nullable rank_specifiers
3380 $$ = "?" + (string) $2;
3387 : rank_specifier opt_rank_specifier
3389 $$ = (string) $2 + (string) $1;
3394 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3396 $$ = "[" + (string) $2 + "]";
3416 | dim_separators COMMA
3418 $$ = (string) $1 + ",";
3422 opt_array_initializer
3434 : OPEN_BRACE CLOSE_BRACE
3436 ArrayList list = new ArrayList (4);
3439 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3441 $$ = (ArrayList) $2;
3445 variable_initializer_list
3446 : variable_initializer
3448 ArrayList list = new ArrayList (4);
3452 | variable_initializer_list COMMA variable_initializer
3454 ArrayList list = (ArrayList) $1;
3463 pushed_current_array_type = current_array_type;
3464 lexer.TypeOfParsing = true;
3466 OPEN_PARENS typeof_type_expression CLOSE_PARENS
3468 lexer.TypeOfParsing = false;
3469 Expression type = (Expression)$4;
3470 if (type == TypeManager.system_void_expr)
3471 $$ = new TypeOfVoid ((Location) $1);
3473 $$ = new TypeOf (type, (Location) $1);
3474 current_array_type = pushed_current_array_type;
3478 typeof_type_expression
3485 $$ = new UnboundTypeExpression ((MemberName)$1, lexer.Location);
3489 Error_TypeExpected (lexer.Location);
3494 : IDENTIFIER GENERIC_DIMENSION
3496 if (RootContext.Version < LanguageVersion.ISO_2)
3497 Report.FeatureIsNotAvailable (lexer.Location, "generics");
3499 LocatedToken lt = (LocatedToken) $1;
3500 TypeArguments ta = new TypeArguments ((int)$2, lt.Location);
3502 $$ = new MemberName (lt.Value, ta, lt.Location);
3504 | IDENTIFIER DOUBLE_COLON IDENTIFIER GENERIC_DIMENSION
3506 LocatedToken lt = (LocatedToken) $1;
3507 MemberName left = new MemberName (lt.Value, lt.Location);
3508 lt = (LocatedToken) $3;
3509 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3511 if (RootContext.Version == LanguageVersion.ISO_1)
3512 Report.FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
3514 $$ = new MemberName (left, lt.Value, ta, lt.Location);
3516 | unbound_type_name DOT IDENTIFIER GENERIC_DIMENSION
3518 LocatedToken lt = (LocatedToken) $3;
3519 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3521 $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
3523 | namespace_or_type_name DOT IDENTIFIER GENERIC_DIMENSION
3525 LocatedToken lt = (LocatedToken) $3;
3526 TypeArguments ta = new TypeArguments ((int)$4, lt.Location);
3528 $$ = new MemberName ((MemberName)$1, lt.Value, ta, lt.Location);
3534 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3535 $$ = new SizeOf ((Expression) $3, (Location) $1);
3540 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3542 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3546 unchecked_expression
3547 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3549 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3553 pointer_member_access
3554 : primary_expression OP_PTR IDENTIFIER
3557 LocatedToken lt = (LocatedToken) $3;
3559 deref = new Indirection ((Expression) $1, lt.Location);
3560 $$ = new MemberAccess (deref, lt.Value);
3564 anonymous_method_expression
3565 : DELEGATE opt_anonymous_method_signature
3567 start_anonymous (false, (Parameters) $2, (Location) $1);
3571 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $1);
3575 opt_anonymous_method_signature
3576 : /* empty */ { $$ = null; }
3577 | anonymous_method_signature
3580 anonymous_method_signature
3583 params_modifiers_not_allowed = true;
3585 opt_formal_parameter_list CLOSE_PARENS
3587 params_modifiers_not_allowed = false;
3592 default_value_expression
3593 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3595 if (RootContext.Version < LanguageVersion.ISO_2)
3596 Report.FeatureIsNotAvailable (lexer.Location, "default value expression");
3598 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3603 : primary_expression
3604 | BANG prefixed_unary_expression
3606 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3608 | TILDE prefixed_unary_expression
3610 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3616 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3618 $$ = new Cast ((Expression) $1, (Expression) $3);
3620 | parenthesized_expression_0 CLOSE_PARENS_NO_CAST default_value_expression
3622 $$ = new Cast ((Expression) $1, (Expression) $3);
3624 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3626 $$ = new Cast ((Expression) $1, (Expression) $3);
3632 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3634 if ($2 == TypeManager.system_void_expr)
3635 Expression.Error_VoidInvalidInTheContext (GetLocation ($2));
3637 // TODO: wrong location
3638 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3643 // The idea to split this out is from Rhys' grammar
3644 // to solve the problem with casts.
3646 prefixed_unary_expression
3648 | PLUS prefixed_unary_expression
3650 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3652 | MINUS prefixed_unary_expression
3654 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3656 | OP_INC prefixed_unary_expression
3658 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3659 (Expression) $2, (Location) $1);
3661 | OP_DEC prefixed_unary_expression
3663 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3664 (Expression) $2, (Location) $1);
3666 | STAR prefixed_unary_expression
3668 $$ = new Indirection ((Expression) $2, (Location) $1);
3670 | BITWISE_AND prefixed_unary_expression
3672 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3676 multiplicative_expression
3677 : prefixed_unary_expression
3678 | multiplicative_expression STAR prefixed_unary_expression
3680 $$ = new Binary (Binary.Operator.Multiply,
3681 (Expression) $1, (Expression) $3);
3683 | multiplicative_expression DIV prefixed_unary_expression
3685 $$ = new Binary (Binary.Operator.Division,
3686 (Expression) $1, (Expression) $3);
3688 | multiplicative_expression PERCENT prefixed_unary_expression
3690 $$ = new Binary (Binary.Operator.Modulus,
3691 (Expression) $1, (Expression) $3);
3696 : multiplicative_expression
3697 | additive_expression PLUS multiplicative_expression
3699 $$ = new Binary (Binary.Operator.Addition,
3700 (Expression) $1, (Expression) $3);
3702 | additive_expression MINUS multiplicative_expression
3704 $$ = new Binary (Binary.Operator.Subtraction,
3705 (Expression) $1, (Expression) $3);
3707 | additive_expression AS type
3709 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3711 | additive_expression IS type
3713 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3718 : additive_expression
3719 | shift_expression OP_SHIFT_LEFT additive_expression
3721 $$ = new Binary (Binary.Operator.LeftShift,
3722 (Expression) $1, (Expression) $3);
3724 | shift_expression OP_SHIFT_RIGHT additive_expression
3726 $$ = new Binary (Binary.Operator.RightShift,
3727 (Expression) $1, (Expression) $3);
3731 relational_expression
3733 | relational_expression OP_LT shift_expression
3735 $$ = new Binary (Binary.Operator.LessThan,
3736 (Expression) $1, (Expression) $3);
3738 | relational_expression OP_GT shift_expression
3740 $$ = new Binary (Binary.Operator.GreaterThan,
3741 (Expression) $1, (Expression) $3);
3743 | relational_expression OP_LE shift_expression
3745 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3746 (Expression) $1, (Expression) $3);
3748 | relational_expression OP_GE shift_expression
3750 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3751 (Expression) $1, (Expression) $3);
3756 : relational_expression
3757 | equality_expression OP_EQ relational_expression
3759 $$ = new Binary (Binary.Operator.Equality,
3760 (Expression) $1, (Expression) $3);
3762 | equality_expression OP_NE relational_expression
3764 $$ = new Binary (Binary.Operator.Inequality,
3765 (Expression) $1, (Expression) $3);
3770 : equality_expression
3771 | and_expression BITWISE_AND equality_expression
3773 $$ = new Binary (Binary.Operator.BitwiseAnd,
3774 (Expression) $1, (Expression) $3);
3778 exclusive_or_expression
3780 | exclusive_or_expression CARRET and_expression
3782 $$ = new Binary (Binary.Operator.ExclusiveOr,
3783 (Expression) $1, (Expression) $3);
3787 inclusive_or_expression
3788 : exclusive_or_expression
3789 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3791 $$ = new Binary (Binary.Operator.BitwiseOr,
3792 (Expression) $1, (Expression) $3);
3796 conditional_and_expression
3797 : inclusive_or_expression
3798 | conditional_and_expression OP_AND inclusive_or_expression
3800 $$ = new Binary (Binary.Operator.LogicalAnd,
3801 (Expression) $1, (Expression) $3);
3805 conditional_or_expression
3806 : conditional_and_expression
3807 | conditional_or_expression OP_OR conditional_and_expression
3809 $$ = new Binary (Binary.Operator.LogicalOr,
3810 (Expression) $1, (Expression) $3);
3814 conditional_expression
3815 : conditional_or_expression
3816 | conditional_or_expression INTERR expression COLON expression
3818 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3820 | conditional_or_expression OP_COALESCING expression
3822 if (RootContext.Version < LanguageVersion.ISO_2)
3823 Report.FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
3825 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, lexer.Location);
3827 | primary_expression INTERR_NULLABLE opt_rank_specifier
3830 // TODO: Is this the best place to handle `= (foo?)a;' ?
3832 if (RootContext.Version < LanguageVersion.ISO_2)
3833 Report.FeatureIsNotAvailable (GetLocation ($1), "nullable types");
3835 $$ = new ComposedCast ((FullNamedExpression) $1, "?" + (string)$3, lexer.Location);
3839 assignment_expression
3840 : prefixed_unary_expression ASSIGN expression
3842 $$ = new SimpleAssign ((Expression) $1, (Expression) $3);
3844 | prefixed_unary_expression OP_MULT_ASSIGN expression
3846 $$ = new CompoundAssign (
3847 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3849 | prefixed_unary_expression OP_DIV_ASSIGN expression
3851 $$ = new CompoundAssign (
3852 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3854 | prefixed_unary_expression OP_MOD_ASSIGN expression
3856 $$ = new CompoundAssign (
3857 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3859 | prefixed_unary_expression OP_ADD_ASSIGN expression
3861 $$ = new CompoundAssign (
3862 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3864 | prefixed_unary_expression OP_SUB_ASSIGN expression
3866 $$ = new CompoundAssign (
3867 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3869 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3871 $$ = new CompoundAssign (
3872 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3874 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3876 $$ = new CompoundAssign (
3877 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3879 | prefixed_unary_expression OP_AND_ASSIGN expression
3881 $$ = new CompoundAssign (
3882 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3884 | prefixed_unary_expression OP_OR_ASSIGN expression
3886 $$ = new CompoundAssign (
3887 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3889 | prefixed_unary_expression OP_XOR_ASSIGN expression
3891 $$ = new CompoundAssign (
3892 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3896 lambda_parameter_list
3899 ArrayList pars = new ArrayList (4);
3904 | lambda_parameter_list COMMA lambda_parameter
3906 ArrayList pars = (ArrayList) $1;
3907 Parameter p = (Parameter)$3;
3908 if (pars[0].GetType () != p.GetType ()) {
3909 Report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
3918 : parameter_modifier type IDENTIFIER
3920 LocatedToken lt = (LocatedToken) $3;
3922 $$ = new Parameter ((FullNamedExpression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3926 LocatedToken lt = (LocatedToken) $2;
3928 $$ = new Parameter ((FullNamedExpression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
3932 LocatedToken lt = (LocatedToken) $1;
3933 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
3937 opt_lambda_parameter_list
3938 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
3939 | lambda_parameter_list {
3940 ArrayList pars_list = (ArrayList) $1;
3941 $$ = new Parameters ((Parameter[])pars_list.ToArray (typeof (Parameter)));
3945 lambda_expression_body
3947 start_block (lexer.Location);
3951 Block b = end_block (lexer.Location);
3952 b.AddStatement (new ContextualReturn ((Expression) $2));
3963 LocatedToken lt = (LocatedToken) $1;
3964 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
3965 start_anonymous (true, new Parameters (p), (Location) $2);
3967 lambda_expression_body
3969 $$ = end_anonymous ((ToplevelBlock) $4, (Location) $2);
3971 | OPEN_PARENS_LAMBDA opt_lambda_parameter_list CLOSE_PARENS ARROW
3973 start_anonymous (true, (Parameters) $2, (Location) $4);
3975 lambda_expression_body
3977 $$ = end_anonymous ((ToplevelBlock) $6, (Location) $4);
3982 : assignment_expression
3983 | non_assignment_expression
3986 non_assignment_expression
3987 : conditional_expression
4009 lexer.ConstraintsParsing = true;
4013 MemberName name = MakeName ((MemberName) $6);
4014 push_current_class (new Class (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
4017 opt_type_parameter_constraints_clauses
4019 lexer.ConstraintsParsing = false;
4021 current_class.SetParameterInfo ((ArrayList) $9);
4023 if (RootContext.Documentation != null) {
4024 current_container.DocComment = Lexer.consume_doc_comment ();
4025 Lexer.doc_state = XmlCommentState.Allowed;
4030 if (RootContext.Documentation != null)
4031 Lexer.doc_state = XmlCommentState.Allowed;
4035 $$ = pop_current_class ();
4043 { $$ = $1; } // location
4047 : /* empty */ { $$ = (int) 0; }
4053 | modifiers modifier
4058 if ((m1 & m2) != 0) {
4059 Location l = lexer.Location;
4060 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
4062 $$ = (int) (m1 | m2);
4070 if (current_container == RootContext.ToplevelTypes)
4071 Report.Error (1530, lexer.Location, "Keyword `new' is not allowed on namespace elements");
4073 | PUBLIC { $$ = Modifiers.PUBLIC; }
4074 | PROTECTED { $$ = Modifiers.PROTECTED; }
4075 | INTERNAL { $$ = Modifiers.INTERNAL; }
4076 | PRIVATE { $$ = Modifiers.PRIVATE; }
4077 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
4078 | SEALED { $$ = Modifiers.SEALED; }
4079 | STATIC { $$ = Modifiers.STATIC; }
4080 | READONLY { $$ = Modifiers.READONLY; }
4081 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
4082 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
4083 | EXTERN { $$ = Modifiers.EXTERN; }
4084 | VOLATILE { $$ = Modifiers.VOLATILE; }
4085 | UNSAFE { $$ = Modifiers.UNSAFE; }
4094 : COLON type_list { current_container.AddBasesForPart (current_class, (ArrayList) $2); }
4097 opt_type_parameter_constraints_clauses
4098 : /* empty */ { $$ = null; }
4099 | type_parameter_constraints_clauses
4103 type_parameter_constraints_clauses
4104 : type_parameter_constraints_clause {
4105 ArrayList constraints = new ArrayList (1);
4106 constraints.Add ($1);
4109 | type_parameter_constraints_clauses type_parameter_constraints_clause {
4110 ArrayList constraints = (ArrayList) $1;
4111 Constraints new_constraint = (Constraints)$2;
4113 foreach (Constraints c in constraints) {
4114 if (new_constraint.TypeParameter == c.TypeParameter) {
4115 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
4116 new_constraint.TypeParameter);
4120 constraints.Add (new_constraint);
4125 type_parameter_constraints_clause
4126 : WHERE IDENTIFIER COLON type_parameter_constraints {
4127 LocatedToken lt = (LocatedToken) $2;
4128 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
4132 type_parameter_constraints
4133 : type_parameter_constraint {
4134 ArrayList constraints = new ArrayList (1);
4135 constraints.Add ($1);
4138 | type_parameter_constraints COMMA type_parameter_constraint {
4139 ArrayList constraints = (ArrayList) $1;
4141 constraints.Add ($3);
4146 type_parameter_constraint
4148 | NEW OPEN_PARENS CLOSE_PARENS {
4149 $$ = SpecialConstraint.Constructor;
4152 $$ = SpecialConstraint.ReferenceType;
4155 $$ = SpecialConstraint.ValueType;
4164 // A block is "contained" on the following places:
4166 // property_declaration as part of the accessor body (get/set)
4167 // operator_declaration
4168 // constructor_declaration
4169 // destructor_declaration
4170 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4175 ++lexer.parsing_block;
4176 start_block ((Location) $1);
4178 opt_statement_list CLOSE_BRACE
4180 --lexer.parsing_block;
4181 $$ = end_block ((Location) $4);
4188 ++lexer.parsing_block;
4189 current_block.StartLocation = GetLocation ($1);
4191 opt_statement_list CLOSE_BRACE
4193 --lexer.parsing_block;
4194 $$ = end_block ((Location) $4);
4205 | statement_list statement
4209 : declaration_statement
4211 if ($1 != null && (Block) $1 != current_block){
4212 current_block.AddStatement ((Statement) $1);
4213 current_block = (Block) $1;
4216 | valid_declaration_statement
4218 current_block.AddStatement ((Statement) $1);
4224 // The interactive_statement and its derivatives are only
4225 // used to provide a special version of `expression_statement'
4226 // that has a side effect of assigning the expression to
4229 interactive_statement_list
4230 : interactive_statement
4231 | interactive_statement_list interactive_statement
4234 interactive_statement
4235 : declaration_statement
4237 if ($1 != null && (Block) $1 != current_block){
4238 current_block.AddStatement ((Statement) $1);
4239 current_block = (Block) $1;
4242 | interactive_valid_declaration_statement
4244 current_block.AddStatement ((Statement) $1);
4249 valid_declaration_statement
4252 | expression_statement
4253 | selection_statement
4254 | iteration_statement
4258 | unchecked_statement
4265 interactive_valid_declaration_statement
4268 | interactive_expression_statement
4269 | selection_statement
4270 | iteration_statement
4274 | unchecked_statement
4282 : valid_declaration_statement
4283 | declaration_statement
4285 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4290 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4298 $$ = EmptyStatement.Value;
4305 LocatedToken lt = (LocatedToken) $1;
4306 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4308 if (current_block.AddLabel (labeled))
4309 current_block.AddStatement (labeled);
4314 declaration_statement
4315 : local_variable_declaration SEMICOLON
4317 current_array_type = null;
4319 DictionaryEntry de = (DictionaryEntry) $1;
4320 Expression e = (Expression) de.Key;
4322 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4326 | local_constant_declaration SEMICOLON
4328 current_array_type = null;
4330 DictionaryEntry de = (DictionaryEntry) $1;
4332 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4338 * The following is from Rhys' grammar:
4339 * > Types in local variable declarations must be recognized as
4340 * > expressions to prevent reduce/reduce errors in the grammar.
4341 * > The expressions are converted into types during semantic analysis.
4344 : primary_expression opt_rank_specifier_or_nullable
4346 // FIXME: Do something smart here regarding the composition of the type.
4348 // Ok, the above "primary_expression" is there to get rid of
4349 // both reduce/reduce and shift/reduces in the grammar, it should
4350 // really just be "type_name". If you use type_name, a reduce/reduce
4351 // creeps up. If you use namespace_or_type_name (which is all we need
4352 // really) two shift/reduces appear.
4355 // So the super-trick is that primary_expression
4356 // can only be either a SimpleName or a MemberAccess.
4357 // The MemberAccess case arises when you have a fully qualified type-name like :
4359 // SimpleName is when you have
4362 Expression expr = (Expression) $1;
4363 string rank_or_nullable = (string) $2;
4365 if (expr is ComposedCast){
4366 $$ = new ComposedCast ((ComposedCast)expr, rank_or_nullable);
4367 } else if (expr is ATypeNameExpression){
4369 // So we extract the string corresponding to the SimpleName
4372 if (rank_or_nullable.Length == 0) {
4373 SimpleName sn = expr as SimpleName;
4374 if (sn != null && sn.Name == "var" &&
4375 (RootContext.Version > LanguageVersion.ISO_2 || RootContext.Version == LanguageVersion.Default_MCS))
4376 $$ = current_array_type = new VarExpr (sn.Location);
4380 $$ = new ComposedCast ((ATypeNameExpression)expr, rank_or_nullable);
4383 Error_ExpectingTypeName (expr);
4384 $$ = TypeManager.system_object_expr;
4387 | builtin_types opt_rank_specifier_or_nullable
4389 if ((string) $2 == "")
4392 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, (string) $2, lexer.Location);
4394 | VOID opt_rank_specifier
4396 Expression.Error_VoidInvalidInTheContext (lexer.Location);
4397 $$ = TypeManager.system_void_expr;
4401 local_variable_pointer_type
4402 : primary_expression STAR
4404 ATypeNameExpression expr = $1 as ATypeNameExpression;
4407 $$ = new ComposedCast (expr, "*");
4409 Error_ExpectingTypeName ((Expression)$1);
4413 | builtin_types STAR
4415 $$ = new ComposedCast ((FullNamedExpression) $1, "*", lexer.Location);
4419 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4421 | local_variable_pointer_type STAR
4423 $$ = new ComposedCast ((FullNamedExpression) $1, "*");
4427 local_variable_declaration
4428 : local_variable_type variable_declarators
4431 VarExpr ve = $1 as VarExpr;
4433 ve.VariableInitializer = (ArrayList)$2;
4435 $$ = new DictionaryEntry ($1, $2);
4439 | local_variable_pointer_type opt_rank_specifier_or_nullable variable_declarators
4444 if ((string) $2 == "")
4445 t = (Expression) $1;
4447 t = new ComposedCast ((FullNamedExpression) $1, (string) $2);
4448 $$ = new DictionaryEntry (t, $3);
4454 local_constant_declaration
4455 : CONST local_variable_type constant_declarators
4458 $$ = new DictionaryEntry ($2, $3);
4464 expression_statement
4465 : statement_expression SEMICOLON { $$ = $1; }
4468 interactive_expression_statement
4469 : interactive_statement_expression SEMICOLON { $$ = $1; }
4473 // We have to do the wrapping here and not in the case above,
4474 // because statement_expression is used for example in for_statement
4476 statement_expression
4479 Expression expr = (Expression) $1;
4480 ExpressionStatement s;
4482 s = expr as ExpressionStatement;
4485 expr.Error_InvalidExpressionStatement ();
4488 $$ = new StatementExpression (s);
4493 Report.Error (1002, GetLocation ($1), "Expecting {0}, got {1}", GetExpecting (), GetTokenName (yyToken));
4498 interactive_statement_expression
4501 Expression expr = (Expression) $1;
4502 ExpressionStatement s;
4504 s = new OptionalAssign (new SimpleName ("$retval", lexer.Location), expr, lexer.Location);
4505 $$ = new StatementExpression (s);
4509 Report.Error (1002, GetLocation ($1), "Expecting {0}, got {1}", GetExpecting (), GetTokenName (yyToken));
4520 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
4523 Location l = (Location) $1;
4525 $$ = new If ((Expression) $3, (Statement) $5, l);
4527 // FIXME: location for warning should be loc property of $5.
4528 if ($5 == EmptyStatement.Value)
4529 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4532 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
4533 embedded_statement ELSE embedded_statement
4535 Location l = (Location) $1;
4537 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4539 // FIXME: location for warning should be loc property of $5 and $7.
4540 if ($5 == EmptyStatement.Value)
4541 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4542 if ($7 == EmptyStatement.Value)
4543 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4548 : SWITCH OPEN_PARENS
4550 if (switch_stack == null)
4551 switch_stack = new Stack (2);
4552 switch_stack.Push (current_block);
4554 expression CLOSE_PARENS
4557 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4558 current_block = (Block) switch_stack.Pop ();
4574 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4575 $$ = new ArrayList ();
4583 ArrayList sections = new ArrayList (4);
4588 | switch_sections switch_section
4590 ArrayList sections = (ArrayList) $1;
4600 current_block = current_block.CreateSwitchBlock (lexer.Location);
4604 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4611 ArrayList labels = new ArrayList (4);
4616 | switch_labels switch_label
4618 ArrayList labels = (ArrayList) ($1);
4626 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
4627 | DEFAULT_COLON { $$ = new SwitchLabel (null, (Location) $1); }
4630 1523, GetLocation ($1),
4631 "The keyword case or default must precede code in switch block");
4643 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
4645 Location l = (Location) $1;
4646 $$ = new While ((Expression) $3, (Statement) $5, l);
4651 : DO embedded_statement
4652 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4654 Location l = (Location) $1;
4656 $$ = new Do ((Statement) $2, (Expression) $5, l);
4662 opt_for_initializer SEMICOLON
4664 Location l = lexer.Location;
4666 Block assign_block = current_block;
4668 if ($3 is DictionaryEntry){
4669 DictionaryEntry de = (DictionaryEntry) $3;
4671 Expression type = (Expression) de.Key;
4672 ArrayList var_declarators = (ArrayList) de.Value;
4674 foreach (VariableDeclaration decl in var_declarators){
4678 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4682 Expression expr = decl.expression_or_array_initializer;
4684 LocalVariableReference var;
4685 var = new LocalVariableReference (assign_block, decl.identifier, l);
4688 Assign a = new SimpleAssign (var, expr, decl.Location);
4690 assign_block.AddStatement (new StatementExpression (a));
4694 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4695 // This can be referred to as $5 below.
4701 opt_for_condition SEMICOLON
4702 opt_for_iterator CLOSE_PARENS
4705 Location l = (Location) $1;
4707 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4709 current_block.AddStatement (f);
4711 $$ = end_block (lexer.Location);
4716 : /* empty */ { $$ = EmptyStatement.Value; }
4721 : local_variable_declaration
4722 | statement_expression_list
4726 : /* empty */ { $$ = null; }
4727 | boolean_expression
4731 : /* empty */ { $$ = EmptyStatement.Value; }
4736 : statement_expression_list
4739 statement_expression_list
4740 : statement_expression
4742 // CHANGE: was `null'
4743 Statement s = (Statement) $1;
4744 Block b = new Block (current_block, s.loc, lexer.Location);
4749 | statement_expression_list COMMA statement_expression
4751 Block b = (Block) $1;
4753 b.AddStatement ((Statement) $3);
4759 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4761 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4764 | FOREACH OPEN_PARENS type IDENTIFIER IN
4765 expression CLOSE_PARENS
4767 start_block (lexer.Location);
4768 Block foreach_block = current_block;
4770 LocatedToken lt = (LocatedToken) $4;
4771 Location l = lt.Location;
4772 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4774 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4776 // Get a writable reference to this read-only variable.
4778 // Note that the $$ here refers to the value of _this_ code block,
4779 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4780 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4787 LocalVariableReference v = (LocalVariableReference) $8;
4788 Location l = (Location) $1;
4791 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4792 current_block.AddStatement (f);
4795 $$ = end_block (lexer.Location);
4801 | continue_statement
4811 $$ = new Break ((Location) $1);
4816 : CONTINUE SEMICOLON
4818 $$ = new Continue ((Location) $1);
4823 : GOTO IDENTIFIER SEMICOLON
4825 LocatedToken lt = (LocatedToken) $2;
4826 $$ = new Goto (lt.Value, lt.Location);
4828 | GOTO CASE constant_expression SEMICOLON
4830 $$ = new GotoCase ((Expression) $3, (Location) $1);
4832 | GOTO DEFAULT SEMICOLON
4834 $$ = new GotoDefault ((Location) $1);
4839 : RETURN opt_expression SEMICOLON
4841 $$ = new Return ((Expression) $2, (Location) $1);
4846 : THROW opt_expression SEMICOLON
4848 $$ = new Throw ((Expression) $2, (Location) $1);
4853 : IDENTIFIER RETURN expression SEMICOLON
4855 LocatedToken lt = (LocatedToken) $1;
4856 string s = lt.Value;
4858 Report.Error (1003, lt.Location, "; expected");
4861 if (RootContext.Version == LanguageVersion.ISO_1){
4862 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
4865 current_block.Toplevel.IsIterator = true;
4866 $$ = new Yield ((Expression) $3, lt.Location);
4868 | IDENTIFIER RETURN SEMICOLON
4870 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4873 | IDENTIFIER BREAK SEMICOLON
4875 LocatedToken lt = (LocatedToken) $1;
4876 string s = lt.Value;
4878 Report.Error (1003, lt.Location, "; expected");
4881 if (RootContext.Version == LanguageVersion.ISO_1){
4882 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
4886 current_block.Toplevel.IsIterator = true;
4887 $$ = new YieldBreak (lt.Location);
4897 : TRY block catch_clauses
4899 $$ = new TryCatch ((Block) $2, (ArrayList) $3, (Location) $1, false);
4901 | TRY block FINALLY block
4903 $$ = new TryFinally ((Statement) $2, (Block) $4, (Location) $1);
4905 | TRY block catch_clauses FINALLY block
4907 $$ = new TryFinally (new TryCatch ((Block) $2, (ArrayList) $3, (Location) $1, true), (Block) $5, (Location) $1);
4911 Report.Error (1524, (Location) $1, "Expected catch or finally");
4919 ArrayList l = new ArrayList (4);
4924 | catch_clauses catch_clause
4926 ArrayList l = (ArrayList) $1;
4934 : /* empty */ { $$ = null; }
4939 : CATCH opt_catch_args
4941 Expression type = null;
4944 DictionaryEntry cc = (DictionaryEntry) $2;
4945 type = (Expression) cc.Key;
4946 LocatedToken lt = (LocatedToken) cc.Value;
4949 ArrayList one = new ArrayList (4);
4951 one.Add (new VariableDeclaration (lt, null));
4953 start_block (lexer.Location);
4954 current_block = declare_local_variables (type, one, lt.Location);
4958 Expression type = null;
4960 Block var_block = null;
4963 DictionaryEntry cc = (DictionaryEntry) $2;
4964 type = (Expression) cc.Key;
4965 LocatedToken lt = (LocatedToken) cc.Value;
4969 var_block = end_block (lexer.Location);
4973 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4978 : /* empty */ { $$ = null; }
4983 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4985 $$ = new DictionaryEntry ($2, $3);
4987 | OPEN_PARENS CLOSE_PARENS
4989 Report.Error (1015, GetLocation ($1), "A type that derives from `System.Exception', `object', or `string' expected");
4996 $$ = new Checked ((Block) $2);
5003 $$ = new Unchecked ((Block) $2);
5010 RootContext.CheckUnsafeOption ((Location) $1);
5012 $$ = new Unsafe ((Block) $3);
5018 type_and_void fixed_pointer_declarators
5021 ArrayList list = (ArrayList) $4;
5022 Expression type = (Expression) $3;
5023 Location l = (Location) $1;
5024 int top = list.Count;
5026 start_block (lexer.Location);
5028 for (int i = 0; i < top; i++){
5029 Pair p = (Pair) list [i];
5032 v = current_block.AddVariable (type, (string) p.First, l);
5036 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
5044 Location l = (Location) $1;
5046 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
5048 current_block.AddStatement (f);
5050 $$ = end_block (lexer.Location);
5054 fixed_pointer_declarators
5055 : fixed_pointer_declarator {
5056 ArrayList declarators = new ArrayList (4);
5058 declarators.Add ($1);
5061 | fixed_pointer_declarators COMMA fixed_pointer_declarator
5063 ArrayList declarators = (ArrayList) $1;
5065 declarators.Add ($3);
5070 fixed_pointer_declarator
5071 : IDENTIFIER ASSIGN expression
5073 LocatedToken lt = (LocatedToken) $1;
5074 // FIXME: keep location
5075 $$ = new Pair (lt.Value, $3);
5079 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
5085 : LOCK OPEN_PARENS expression CLOSE_PARENS
5091 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
5096 : USING OPEN_PARENS local_variable_declaration CLOSE_PARENS
5098 start_block (lexer.Location);
5099 Block assign_block = current_block;
5101 DictionaryEntry de = (DictionaryEntry) $3;
5102 Location l = (Location) $1;
5104 Expression type = (Expression) de.Key;
5105 ArrayList var_declarators = (ArrayList) de.Value;
5107 Stack vars = new Stack ();
5109 foreach (VariableDeclaration decl in var_declarators) {
5110 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
5113 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
5115 Expression expr = decl.expression_or_array_initializer;
5117 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
5120 LocalVariableReference var;
5122 // Get a writable reference to this read-only variable.
5123 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
5125 // This is so that it is not a warning on using variables
5128 vars.Push (new DictionaryEntry (var, expr));
5130 // Assign a = new SimpleAssign (var, expr, decl.Location);
5131 // assign_block.AddStatement (new StatementExpression (a));
5134 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
5135 // It can be referred to as $5 below.
5140 Statement stmt = (Statement) $6;
5141 Stack vars = (Stack) $5;
5142 Location l = (Location) $1;
5144 while (vars.Count > 0) {
5145 DictionaryEntry de = (DictionaryEntry) vars.Pop ();
5146 stmt = new Using ((Expression) de.Key, (Expression) de.Value, stmt, l);
5148 current_block.AddStatement (stmt);
5149 $$ = end_block (lexer.Location);
5151 | USING OPEN_PARENS expression CLOSE_PARENS
5153 start_block (lexer.Location);
5157 current_block.AddStatement (new UsingTemporary ((Expression) $3, (Statement) $6, (Location) $1));
5158 $$ = end_block (lexer.Location);
5168 ++lexer.query_parsing;
5172 if (--lexer.query_parsing == 1)
5173 lexer.query_parsing = 0;
5175 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5177 from.Tail.Next = (Linq.AQueryClause)$3;
5180 current_block.SetEndLocation (lexer.Location);
5181 current_block = current_block.Parent;
5186 : FROM IDENTIFIER IN expression
5188 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5189 LocatedToken lt = (LocatedToken) $2;
5191 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance, lt.Value, lt.Location);
5192 $$ = new Linq.QueryExpression (lt, new Linq.QueryStartClause ((Expression)$4));
5194 | FROM type IDENTIFIER IN expression
5196 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5197 LocatedToken lt = (LocatedToken) $3;
5199 Expression type = (Expression)$2;
5200 current_block.AddVariable (type, lt.Value, lt.Location);
5201 $$ = new Linq.QueryExpression (lt, new Linq.Cast (type, (Expression)$5));
5206 : FROM IDENTIFIER IN expression
5208 LocatedToken lt = (LocatedToken) $2;
5210 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5211 lt.Value, lt.Location);
5213 $$ = new Linq.SelectMany (lt, (Expression)$4);
5215 | FROM type IDENTIFIER IN expression
5217 LocatedToken lt = (LocatedToken) $3;
5219 Expression type = (Expression)$2;
5220 current_block.AddVariable (type, lt.Value, lt.Location);
5221 $$ = new Linq.SelectMany (lt, new Linq.Cast (type, (Expression)$5));
5226 : opt_query_body_clauses select_or_group_clause opt_query_continuation
5228 Linq.AQueryClause head = (Linq.AQueryClause)$2;
5231 head.Next = (Linq.AQueryClause)$3;
5234 Linq.AQueryClause clause = (Linq.AQueryClause)$1;
5235 clause.Tail.Next = head;
5243 select_or_group_clause
5246 $$ = new Linq.Select ((Expression)$2, GetLocation ($1));
5248 | GROUP expression BY expression
5250 $$ = new Linq.GroupBy ((Expression)$2, (Expression)$4, GetLocation ($1));
5254 opt_query_body_clauses
5256 | query_body_clauses
5261 | query_body_clauses query_body_clause
5263 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
5277 : LET IDENTIFIER ASSIGN expression
5279 LocatedToken lt = (LocatedToken) $2;
5280 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5281 lt.Value, lt.Location);
5282 $$ = new Linq.Let (lt, (Expression)$4, GetLocation ($1));
5287 : WHERE boolean_expression
5289 $$ = new Linq.Where ((Expression)$2, GetLocation ($1));
5294 : JOIN IDENTIFIER IN expression ON expression EQUALS expression opt_join_into
5296 Location loc = GetLocation ($1);
5297 LocatedToken lt = (LocatedToken) $2;
5298 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5299 lt.Value, lt.Location);
5302 $$ = new Linq.Join (lt, (Expression)$4, (Expression)$6,
5303 (Expression)$8, loc);
5305 LocatedToken lt_into = (LocatedToken) $9;
5306 $$ = new Linq.GroupJoin (lt, (Expression)$4, (Expression)$6,
5307 (Expression)$8, lt_into, loc);
5310 | JOIN type IDENTIFIER IN expression ON expression EQUALS expression opt_join_into
5312 Location loc = GetLocation ($1);
5313 LocatedToken lt = (LocatedToken) $3;
5314 current_block.AddVariable ((Expression)$2, lt.Value, lt.Location);
5316 Linq.Cast cast = new Linq.Cast ((Expression)$2, (Expression)$5);
5318 $$ = new Linq.Join (lt, cast, (Expression)$7,
5319 (Expression)$9, loc);
5321 LocatedToken lt_into = (LocatedToken) $10;
5322 $$ = new Linq.GroupJoin (lt, cast, (Expression)$7,
5323 (Expression)$9, lt_into, loc);
5345 | order_by COMMA orderings_then_by
5347 ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$3;
5357 | orderings_then_by COMMA then_by
5359 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
5367 $$ = new Linq.OrderByAscending ((Expression)$1);
5369 | expression ASCENDING
5371 $$ = new Linq.OrderByAscending ((Expression)$1);
5373 | expression DESCENDING
5375 $$ = new Linq.OrderByDescending ((Expression)$1);
5382 $$ = new Linq.ThenByAscending ((Expression)$1);
5384 | expression ASCENDING
5386 $$ = new Linq.ThenByAscending ((Expression)$1);
5388 | expression DESCENDING
5390 $$ = new Linq.ThenByDescending ((Expression)$1);
5395 opt_query_continuation
5399 // query continuation block is not linked with query block but with block
5400 // before. This means each query can use same range variable names for
5401 // different identifiers.
5403 current_block.SetEndLocation (GetLocation ($1));
5404 current_block = current_block.Parent;
5405 current_block = new Linq.QueryBlock (current_block, GetLocation ($1));
5407 LocatedToken lt = (LocatedToken) $2;
5408 current_block.AddVariable (Linq.ImplicitQueryParameter.ImplicitType.Instance,
5409 lt.Value, lt.Location);
5413 $$ = new Linq.QueryExpression ((LocatedToken) $2,
5414 (Linq.AQueryClause)$4);
5419 // Support for using the compiler as an interactive parser
5421 // The INTERACTIVE_PARSER token is first sent to parse our
5422 // productions; If the result is a Statement, the parsing
5423 // is repeated, this time with INTERACTIVE_PARSE_WITH_BLOCK
5424 // to setup the blocks in advance.
5426 // This setup is here so that in the future we can add
5427 // support for other constructs (type parsing, namespaces, etc)
5428 // that do not require a block to be setup in advance
5432 : EVAL_STATEMENT_PARSER EOF
5433 | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives
5434 | EVAL_STATEMENT_PARSER {
5435 Evaluator.LoadAliases (current_namespace);
5437 push_current_class (new Class (current_namespace, current_class, new MemberName ("Class" + class_count++),
5438 Modifiers.PUBLIC, null), null);
5440 ArrayList baseclass_list = new ArrayList ();
5441 baseclass_list.Add (new TypeExpression (Evaluator.InteractiveBaseClass, lexer.Location));
5442 current_container.AddBasesForPart (current_class, baseclass_list);
5444 // (ref object retval)
5445 Parameter [] mpar = new Parameter [1];
5446 mpar [0] = new Parameter (TypeManager.system_object_expr, "$retval", Parameter.Modifier.REF, null, Location.Null);
5448 Parameters pars = new Parameters (mpar);
5449 current_local_parameters = pars;
5450 Method method = new Method (
5453 TypeManager.system_void_expr,
5454 Modifiers.PUBLIC | Modifiers.STATIC,
5455 new MemberName ("Host"),
5457 null /* attributes */);
5459 oob_stack.Push (method);
5460 ++lexer.parsing_block;
5461 start_block (lexer.Location);
5463 interactive_statement_list
5465 --lexer.parsing_block;
5466 Method method = (Method) oob_stack.Pop ();
5468 method.Block = (ToplevelBlock) end_block(lexer.Location);
5469 current_container.AddMethod (method);
5471 InteractiveResult = pop_current_class ();
5472 current_local_parameters = null;
5474 | EVAL_COMPILATION_UNIT_PARSER {
5475 Evaluator.LoadAliases (current_namespace);
5477 interactive_compilation_unit
5480 interactive_compilation_unit
5481 : outer_declarations
5482 | outer_declarations global_attributes
5489 // A class used to pass around variable declarations and constants
5491 public class VariableDeclaration {
5492 public string identifier;
5493 public Expression expression_or_array_initializer;
5494 public Location Location;
5495 public Attributes OptAttributes;
5496 public string DocComment;
5498 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
5500 this.identifier = lt.Value;
5501 if (eoai is ArrayList) {
5502 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
5504 this.expression_or_array_initializer = (Expression)eoai;
5506 this.Location = lt.Location;
5507 this.OptAttributes = opt_attrs;
5510 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
5516 // A class used to hold info about an indexer declarator
5518 public class IndexerDeclaration {
5519 public FullNamedExpression type;
5520 public MemberName interface_type;
5521 public Parameters param_list;
5522 public Location location;
5524 public IndexerDeclaration (FullNamedExpression type, MemberName interface_type,
5525 Parameters param_list, Location loc)
5528 this.interface_type = interface_type;
5529 this.param_list = param_list;
5530 this.location = loc;
5535 // A class used to hold info about an operator declarator
5537 struct OperatorDeclaration {
5538 public readonly Operator.OpType optype;
5539 public readonly FullNamedExpression ret_type;
5540 public readonly Location location;
5542 public OperatorDeclaration (Operator.OpType op, FullNamedExpression ret_type, Location location)
5545 this.ret_type = ret_type;
5546 this.location = location;
5550 void Error_ExpectingTypeName (Expression expr)
5552 if (expr is Invocation){
5553 Report.Error (1002, expr.Location, "Expecting `;'");
5555 expr.Error_InvalidExpressionStatement ();
5559 static void Error_ParameterModifierNotValid (string modifier, Location loc)
5561 Report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
5565 static void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
5567 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
5568 Parameter.GetModifierSignature (mod));
5571 static void Error_TypeExpected (Location loc)
5573 Report.Error (1031, loc, "Type expected");
5576 void push_current_class (TypeContainer tc, object partial_token)
5578 if (RootContext.EvalMode){
5579 tc.ModFlags = (tc.ModFlags & ~(Modifiers.PRIVATE|Modifiers.INTERNAL)) | Modifiers.PUBLIC;
5580 undo.AddTypeContainer (current_container, tc);
5583 if (partial_token != null)
5584 current_container = current_container.AddPartial (tc);
5586 current_container = current_container.AddTypeContainer (tc);
5592 DeclSpace pop_current_class ()
5594 DeclSpace retval = current_class;
5596 current_class = current_class.Parent;
5597 current_container = current_class.PartialContainer;
5603 // Given the @class_name name, it creates a fully qualified name
5604 // based on the containing declaration space
5607 MakeName (MemberName class_name)
5609 Namespace ns = current_namespace.NS;
5611 if (current_container == RootContext.ToplevelTypes) {
5612 if (ns.Name.Length != 0)
5613 return new MemberName (ns.MemberName, class_name);
5617 return new MemberName (current_container.MemberName, class_name);
5621 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
5623 Block implicit_block;
5624 ArrayList inits = null;
5627 // If we are doing interactive editing, we want variable declarations
5628 // that are in the top block to be added instead to the class as
5631 if (RootContext.StatementMode){
5634 for (Block b = current_block; b != null; b = b.Parent){
5635 if (b is ExplicitBlock && !(b is ToplevelBlock)){
5636 // There has been an explicit block, we cant add to the class
5643 // We can use "current_block" since we know there are no explicit blocks
5645 foreach (VariableDeclaration decl in variable_declarators){
5646 // We can not use the super-handy f.Initializer, because
5647 // multiple lines would force code to be executed out of sync
5648 if (decl.expression_or_array_initializer != null){
5649 string id = "$" + decl.identifier;
5650 current_block.AddVariable (type, id, decl.Location);
5652 LocalVariableReference var;
5653 var = new LocalVariableReferenceWithClassSideEffect (current_container, decl.identifier, current_block, id, decl.Location);
5654 Assign assign = new SimpleAssign (var, decl.expression_or_array_initializer, decl.Location);
5655 current_block.AddStatement (new StatementExpression (assign));
5656 assign = new SimpleAssign (new SimpleName (decl.identifier, decl.Location), var);
5657 current_block.AddStatement (new StatementExpression (assign));
5659 Field f = new Field (current_container, (FullNamedExpression) type, Modifiers.PUBLIC | Modifiers.STATIC, decl.identifier, null, loc);
5660 current_container.AddField (f);
5662 // Register the field to be visible later as a global variable
5663 Evaluator.QueueField (f);
5667 return current_block;
5672 // We use the `Used' property to check whether statements
5673 // have been added to the current block. If so, we need
5674 // to create another block to contain the new declaration
5675 // otherwise, as an optimization, we use the same block to
5676 // add the declaration.
5678 // FIXME: A further optimization is to check if the statements
5679 // that were added were added as part of the initialization
5680 // below. In which case, no other statements have been executed
5681 // and we might be able to reduce the number of blocks for
5682 // situations like this:
5684 // int j = 1; int k = j + 1;
5686 if (current_block.Used)
5687 implicit_block = new Block (current_block, loc, lexer.Location);
5689 implicit_block = current_block;
5691 foreach (VariableDeclaration decl in variable_declarators){
5693 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
5694 if (decl.expression_or_array_initializer != null){
5696 inits = new ArrayList (4);
5703 return implicit_block;
5705 foreach (VariableDeclaration decl in inits){
5707 Expression expr = decl.expression_or_array_initializer;
5709 LocalVariableReference var;
5710 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
5712 assign = new SimpleAssign (var, expr, decl.Location);
5714 implicit_block.AddStatement (new StatementExpression (assign));
5717 return implicit_block;
5720 Block declare_local_constants (Expression type, ArrayList declarators)
5722 Block implicit_block;
5724 if (current_block.Used)
5725 implicit_block = new Block (current_block);
5727 implicit_block = current_block;
5729 foreach (VariableDeclaration decl in declarators){
5730 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
5733 return implicit_block;
5736 string CheckAttributeTarget (string a, Location l)
5739 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
5743 Report.Warning (658, 1, l,
5744 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
5745 return string.Empty;
5748 static bool IsUnaryOperator (Operator.OpType op)
5752 case Operator.OpType.LogicalNot:
5753 case Operator.OpType.OnesComplement:
5754 case Operator.OpType.Increment:
5755 case Operator.OpType.Decrement:
5756 case Operator.OpType.True:
5757 case Operator.OpType.False:
5758 case Operator.OpType.UnaryPlus:
5759 case Operator.OpType.UnaryNegation:
5765 void syntax_error (Location l, string msg)
5767 Report.Error (1003, l, "Syntax error, " + msg);
5770 void note (string s)
5772 // Used to put annotations
5777 public Tokenizer Lexer {
5783 static CSharpParser ()
5785 oob_stack = new Stack ();
5788 public CSharpParser (SeekableStreamReader reader, CompilationUnit file)
5790 if (RootContext.EvalMode)
5794 current_namespace = new NamespaceEntry (null, file, null);
5795 current_class = current_namespace.SlaveDeclSpace;
5796 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
5798 lexer = new Tokenizer (reader, file);
5801 public void parse ()
5803 eof_token = Token.EOF;
5806 if (yacc_verbose_flag > 1)
5807 yyparse (lexer, new yydebug.yyDebugSimple ());
5810 } catch (Exception e){
5811 if (e is yyParser.yyUnexpectedEof)
5812 UnexpectedEOF = true;
5813 else if (yacc_verbose_flag > 0)
5814 Console.WriteLine (e);
5815 if (e is yyParser.yyException)
5816 Report.Error (-25, lexer.Location, "Parsing error");
5818 Report.Error (-32, lexer.Location, "Internal compiler error during parsing, Run with -v for details");
5820 Tokenizer tokenizer = lexer as Tokenizer;
5821 tokenizer.cleanup ();
5823 if (RootContext.ToplevelTypes.NamespaceEntry != null)
5824 throw new InternalErrorException ("who set it?");
5827 static void CheckToken (int error, int yyToken, string msg, Location loc)
5829 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
5830 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, GetTokenName (yyToken));
5832 Report.Error (error, loc, msg);
5835 void CheckIdentifierToken (int yyToken, Location loc)
5837 CheckToken (1041, yyToken, "Identifier expected", loc);
5840 string ConsumeStoredComment ()
5842 string s = tmpComment;
5844 Lexer.doc_state = XmlCommentState.Allowed;
5848 Location GetLocation (object obj)
5850 if (obj is MemberCore)
5851 return ((MemberCore) obj).Location;
5852 if (obj is MemberName)
5853 return ((MemberName) obj).Location;
5854 if (obj is LocatedToken)
5855 return ((LocatedToken) obj).Location;
5856 if (obj is Location)
5857 return (Location) obj;
5858 return lexer.Location;
5861 void start_block (Location loc)
5863 if (current_block == null || parsing_anonymous_method) {
5864 current_block = new ToplevelBlock (current_block, current_local_parameters, current_generic_method, loc);
5865 parsing_anonymous_method = false;
5867 current_block = new ExplicitBlock (current_block, loc, Location.Null);
5872 end_block (Location loc)
5874 Block retval = current_block.Explicit;
5875 retval.SetEndLocation (loc);
5876 current_block = retval.Parent;
5881 start_anonymous (bool lambda, Parameters parameters, Location loc)
5883 oob_stack.Push (current_anonymous_method);
5884 oob_stack.Push (current_local_parameters);
5886 current_local_parameters = parameters;
5888 current_anonymous_method = lambda
5889 ? new LambdaExpression (parameters, loc)
5890 : new AnonymousMethodExpression (parameters, loc);
5892 // Force the next block to be created as a ToplevelBlock
5893 parsing_anonymous_method = true;
5897 * Completes the anonymous method processing, if lambda_expr is null, this
5898 * means that we have a Statement instead of an Expression embedded
5900 AnonymousMethodExpression end_anonymous (ToplevelBlock anon_block, Location loc)
5902 AnonymousMethodExpression retval;
5904 if (RootContext.Version == LanguageVersion.ISO_1){
5905 Report.FeatureIsNotAvailable (loc, "anonymous methods");
5908 current_anonymous_method.Block = anon_block;
5909 retval = current_anonymous_method;
5912 current_local_parameters = (Parameters) oob_stack.Pop ();
5913 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
5918 public NamespaceEntry CurrentNamespace {
5920 return current_namespace;
5924 public string GetExpecting ()
5926 int [] tokens = yyExpectingTokens (yyExpectingState);
5927 if (tokens.Length == 1)
5928 return "`" + GetTokenName (tokens [0]) + "'";
5930 StringBuilder sb = new StringBuilder ();
5931 ArrayList names = new ArrayList ();
5932 for (int i = 0; i < tokens.Length; i++){
5933 string name = GetTokenName (tokens [i]);
5934 if (name == "<internal>")
5936 if (names.Contains (name))
5943 int count = names.Count;
5944 for (int i = 0; i < count; i++){
5945 bool last = i + 1 == count;
5949 sb.Append (names [i]);
5950 sb.Append (last ? "'" : "', ");
5952 return sb.ToString ();
5955 static public string GetTokenName (int token)
5958 case Token.ABSTRACT:
5964 case Token.ASSEMBLY:
5980 case Token.CONTINUE:
5984 case Token.DELEGATE:
5994 case Token.EXPLICIT:
6012 case Token.IMPLICIT:
6016 case Token.INTERFACE:
6018 case Token.INTERNAL:
6024 case Token.NAMESPACE:
6032 case Token.OPERATOR:
6036 case Token.OVERRIDE:
6042 case Token.PROTECTED:
6046 case Token.READONLY:
6058 case Token.STACKALLOC:
6059 return "stackalloc";
6076 case Token.UNCHECKED:
6084 case Token.VOLATILE:
6114 case Token.ASCENDING:
6116 case Token.DESCENDING:
6117 return "descending";
6124 case Token.OPEN_BRACE:
6126 case Token.CLOSE_BRACE:
6128 case Token.OPEN_BRACKET:
6130 case Token.CLOSE_BRACKET:
6132 case Token.DEFAULT_OPEN_PARENS:
6133 case Token.OPEN_PARENS_LAMBDA:
6134 case Token.OPEN_PARENS:
6136 case Token.CLOSE_PARENS_CAST:
6137 case Token.CLOSE_PARENS_NO_CAST:
6138 case Token.CLOSE_PARENS_OPEN_PARENS:
6139 case Token.CLOSE_PARENS_MINUS:
6140 case Token.CLOSE_PARENS:
6146 case Token.DEFAULT_COLON:
6149 case Token.SEMICOLON:
6160 case Token.BITWISE_AND:
6161 case Token.BITWISE_OR:
6168 case Token.OP_SHIFT_LEFT:
6169 case Token.OP_SHIFT_RIGHT:
6177 case Token.OP_COALESCING:
6178 return "<operator>";
6180 case Token.OP_MULT_ASSIGN:
6181 case Token.OP_DIV_ASSIGN:
6182 case Token.OP_MOD_ASSIGN:
6183 case Token.OP_ADD_ASSIGN:
6184 case Token.OP_SUB_ASSIGN:
6185 case Token.OP_SHIFT_LEFT_ASSIGN:
6186 case Token.OP_SHIFT_RIGHT_ASSIGN:
6187 case Token.OP_AND_ASSIGN:
6188 case Token.OP_XOR_ASSIGN:
6189 case Token.OP_OR_ASSIGN:
6190 return "<operator>";
6211 case Token.OP_GENERICS_LT:
6212 case Token.GENERIC_DIMENSION:
6214 case Token.OP_GENERICS_GT:
6217 case Token.INTERR_NULLABLE:
6219 case Token.DOUBLE_COLON:
6221 case Token.LITERAL_FLOAT:
6222 case Token.LITERAL_INTEGER:
6223 case Token.LITERAL_DOUBLE:
6224 case Token.LITERAL_DECIMAL:
6225 case Token.LITERAL_CHARACTER:
6226 case Token.LITERAL_STRING:
6228 case Token.IDENTIFIER:
6229 return "identifier";
6231 // All of these are internal.
6234 case Token.FIRST_KEYWORD:
6236 case Token.EVAL_COMPILATION_UNIT_PARSER:
6237 case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
6238 case Token.EVAL_STATEMENT_PARSER:
6240 case Token.QUERY_LAST_TOKEN:
6241 case Token.QUERY_FIRST_TOKEN:
6242 case Token.LAST_KEYWORD:
6243 return "<internal>";
6245 // A bit more robust.
6247 return yyNames [token];