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 // Dual Licensed under the terms of the GNU GPL and the MIT X11 license
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
38 enum ParameterModifierType
45 DefaultValue = 1 << 6,
47 All = Ref | Out | This | Params | Arglist | DefaultValue
50 NamespaceEntry current_namespace;
51 TypeContainer current_container;
52 DeclSpace current_class;
55 /// Current block is used to add statements as we find
60 Delegate current_delegate;
62 GenericMethod current_generic_method;
63 AnonymousMethodExpression current_anonymous_method;
66 /// This is used by the unary_expression code to resolve
67 /// a name against a parameter.
70 // FIXME: This is very ugly and it's very hard to reset it correctly
71 // on all places, especially when some parameters are autogenerated.
72 ParametersCompiled current_local_parameters;
75 /// Using during property parsing to describe the implicit
76 /// value parameter that is passed to the "set" and "get"accesor
77 /// methods (properties and indexers).
79 FullNamedExpression implicit_value_parameter_type;
80 ParametersCompiled indexer_parameters;
83 /// Hack to help create non-typed array initializer
85 public static FullNamedExpression current_array_type;
86 FullNamedExpression pushed_current_array_type;
89 /// Used to determine if we are parsing the get/set pair
90 /// of an indexer or a property
94 bool parsing_anonymous_method;
97 /// An out-of-band stack.
99 static Stack oob_stack;
107 /// Controls the verbosity of the errors produced by the parser
109 static public int yacc_verbose_flag;
112 /// Used by the interactive shell, flags whether EOF was reached
113 /// and an error was produced
115 public bool UnexpectedEOF;
118 /// The current file.
120 CompilationUnit file;
123 /// Temporary Xml documentation cache.
124 /// For enum types, we need one more temporary store.
127 string enumTypeComment;
129 /// Current attribute target
130 string current_attr_target;
132 /// assembly and module attribute definitions are enabled
133 bool global_attrs_enabled = true;
134 bool has_get, has_set;
136 ParameterModifierType valid_param_mod;
138 bool default_parameter_used;
140 /// When using the interactive parser, this holds the
141 /// resulting expression
142 public object InteractiveResult;
145 // Keeps track of global data changes to undo on parser error
149 // Stack<ToplevelBlock>
150 Stack linq_clause_blocks;
152 // A counter to create new class names in interactive mode
153 static int class_count;
155 CompilerContext compiler;
159 %token NONE /* This token is never returned by our lexer */
160 %token ERROR // This is used not by the parser, but by the tokenizer.
164 *These are the C# keywords
263 %token INTERR_NULLABLE
266 /* Generics <,> tokens */
267 %token OP_GENERICS_LT
268 %token OP_GENERICS_LT_DECL
269 %token OP_GENERICS_GT
271 /* C# keywords which are not really keywords */
277 /* C# single character operators/punctuation. */
305 /* C# multi-character operators. */
310 %token OP_SHIFT_RIGHT
317 %token OP_MULT_ASSIGN
322 %token OP_SHIFT_LEFT_ASSIGN
323 %token OP_SHIFT_RIGHT_ASSIGN
331 %token LITERAL_INTEGER
333 %token LITERAL_DOUBLE
334 %token LITERAL_DECIMAL
335 %token LITERAL_CHARACTER
336 %token LITERAL_STRING
339 %token OPEN_PARENS_LAMBDA
340 %token OPEN_PARENS_CAST
341 %token GENERIC_DIMENSION
344 // Make the parser go into eval mode parsing (statements and compilation units).
345 %token EVAL_STATEMENT_PARSER
346 %token EVAL_COMPILATION_UNIT_PARSER
347 %token EVAL_USING_DECLARATIONS_UNIT_PARSER
350 // This token is generated to trigger the completion engine at this point
352 %token GENERATE_COMPLETION
355 // This token is return repeatedly after the first GENERATE_COMPLETION
356 // token is produced and before the final EOF
358 %token COMPLETE_COMPLETION
360 /* Add precedence rules to solve dangling else s/r conflict */
364 /* Define the operator tokens and their precedences */
372 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
374 %left STAR DIV PERCENT
375 %right BANG CARRET UMINUS
376 %nonassoc OP_INC OP_DEC
378 %left OPEN_BRACKET OPEN_BRACE
381 %start compilation_unit
385 : outer_declarations opt_EOF
386 | outer_declarations global_attributes opt_EOF
387 | global_attributes opt_EOF
388 | opt_EOF /* allow empty files */
389 | interactive_parsing { Lexer.CompleteOnEOF = false; } opt_EOF
395 Lexer.check_incorrect_doc_comment ();
399 Lexer.check_incorrect_doc_comment ();
405 | outer_declarations outer_declaration
409 : extern_alias_directive
411 | namespace_member_declaration
414 extern_alias_directives
415 : extern_alias_directive
416 | extern_alias_directives extern_alias_directive
419 extern_alias_directive
420 : EXTERN_ALIAS IDENTIFIER IDENTIFIER SEMICOLON
422 LocatedToken lt = (LocatedToken) $2;
425 syntax_error (lt.Location, "`alias' expected");
426 } else if (RootContext.Version == LanguageVersion.ISO_1) {
427 Report.FeatureIsNotAvailable (lt.Location, "external alias");
429 lt = (LocatedToken) $3;
430 current_namespace.AddUsingExternalAlias (lt.Value, lt.Location, Report);
435 syntax_error (GetLocation ($1), "`alias' expected"); // TODO: better
441 | using_directives using_directive
445 : using_alias_directive
447 if (RootContext.Documentation != null)
448 Lexer.doc_state = XmlCommentState.Allowed;
450 | using_namespace_directive
452 if (RootContext.Documentation != null)
453 Lexer.doc_state = XmlCommentState.Allowed;
457 using_alias_directive
458 : USING IDENTIFIER ASSIGN namespace_or_type_name SEMICOLON
460 LocatedToken lt = (LocatedToken) $2;
461 current_namespace.AddUsingAlias (lt.Value, (MemberName) $4, (Location) $1);
464 CheckIdentifierToken (yyToken, GetLocation ($2));
469 using_namespace_directive
470 : USING namespace_name SEMICOLON
472 current_namespace.AddUsing ((MemberName) $2, (Location) $1);
477 // Strictly speaking, namespaces don't have attributes but
478 // we parse global attributes along with namespace declarations and then
481 namespace_declaration
482 : opt_attributes NAMESPACE qualified_identifier
484 MemberName name = (MemberName) $3;
487 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
490 current_namespace = new NamespaceEntry (
491 current_namespace, file, name.GetName ());
492 current_class = current_namespace.SlaveDeclSpace;
493 current_container = current_class.PartialContainer;
495 namespace_body opt_semicolon
497 current_namespace = current_namespace.Parent;
498 current_class = current_namespace.SlaveDeclSpace;
499 current_container = current_class.PartialContainer;
506 LocatedToken lt = (LocatedToken) $1;
507 $$ = new MemberName (lt.Value, lt.Location);
509 | qualified_identifier DOT IDENTIFIER
511 LocatedToken lt = (LocatedToken) $3;
512 $$ = new MemberName ((MemberName) $1, lt.Value, lt.Location);
516 syntax_error (lexer.Location, "`.' expected");
531 : namespace_or_type_name
533 MemberName name = (MemberName) $1;
535 if (name.TypeArguments != null)
536 syntax_error (lexer.Location, "namespace name expected");
545 if (RootContext.Documentation != null)
546 Lexer.doc_state = XmlCommentState.Allowed;
552 : opt_extern_alias_directives
554 opt_namespace_member_declarations
558 Report.Error (1518, lexer.Location, "Expected `class', `delegate', `enum', `interface', or `struct'");
561 | opt_extern_alias_directives
563 opt_namespace_member_declarations
566 Report.Error (1513, lexer.Location, "Expected `}'");
575 opt_extern_alias_directives
577 | extern_alias_directives
580 opt_namespace_member_declarations
582 | namespace_member_declarations
585 namespace_member_declarations
586 : namespace_member_declaration
587 | namespace_member_declarations namespace_member_declaration
590 namespace_member_declaration
594 DeclSpace ds = (DeclSpace)$1;
596 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
597 Report.Error (1527, ds.Location,
598 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
601 current_namespace.DeclarationFound = true;
603 | namespace_declaration {
604 current_namespace.DeclarationFound = true;
607 | field_declaration {
608 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
610 | method_declaration {
611 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
618 | interface_declaration
620 | delegate_declaration
622 // Enable this when we have handled all errors, because this acts as a generic fallback
625 // Console.WriteLine ("Token=" + yyToken);
626 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
638 Attributes attrs = (Attributes)$1;
639 if (global_attrs_enabled) {
640 CodeGen.Assembly.AddAttributes (attrs.Attrs, current_namespace);
642 foreach (Attribute a in attrs.Attrs) {
643 Report.Error (1730, a.Location, "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
654 global_attrs_enabled = false;
659 global_attrs_enabled = false;
668 if (current_attr_target != String.Empty) {
669 ArrayList sect = (ArrayList) $1;
671 if (global_attrs_enabled) {
672 if (current_attr_target == "module") {
673 current_container.Module.AddAttributes (sect);
675 } else if (current_attr_target != null && current_attr_target.Length > 0) {
676 CodeGen.Assembly.AddAttributes (sect, current_namespace);
679 $$ = new Attributes (sect);
682 if (RootContext.Documentation != null) {
683 Lexer.check_incorrect_doc_comment ();
685 XmlCommentState.Allowed;
689 $$ = new Attributes (sect);
694 current_attr_target = null;
696 | attribute_sections attribute_section
698 if (current_attr_target != String.Empty) {
699 Attributes attrs = $1 as Attributes;
700 ArrayList sect = (ArrayList) $2;
702 if (global_attrs_enabled) {
703 if (current_attr_target == "module") {
704 current_container.Module.AddAttributes (sect);
706 } else if (current_attr_target == "assembly") {
707 CodeGen.Assembly.AddAttributes (sect, current_namespace);
711 attrs = new Attributes (sect);
713 attrs.AddAttributes (sect);
717 attrs = new Attributes (sect);
719 attrs.AddAttributes (sect);
725 current_attr_target = null;
730 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
734 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
740 attribute_target_specifier
741 : attribute_target COLON
743 current_attr_target = (string)$1;
751 LocatedToken lt = (LocatedToken) $1;
752 $$ = CheckAttributeTarget (lt.Value, lt.Location);
754 | EVENT { $$ = "event"; }
755 | RETURN { $$ = "return"; }
758 string name = GetTokenName (yyToken);
759 $$ = CheckAttributeTarget (name, GetLocation ($1));
766 ArrayList attrs = new ArrayList (4);
772 | attribute_list COMMA attribute
774 ArrayList attrs = (ArrayList) $1;
784 ++lexer.parsing_block;
786 opt_attribute_arguments
788 --lexer.parsing_block;
789 MemberName mname = (MemberName) $1;
790 if (mname.IsGeneric) {
791 Report.Error (404, lexer.Location,
792 "'<' unexpected: attributes cannot be generic");
795 Arguments [] arguments = (Arguments []) $3;
796 ATypeNameExpression expr = mname.GetTypeExpression ();
798 if (current_attr_target == String.Empty)
800 else if (global_attrs_enabled && (current_attr_target == "assembly" || current_attr_target == "module"))
801 // FIXME: supply "nameEscaped" parameter here.
802 $$ = new GlobalAttribute (current_namespace, current_attr_target,
803 expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
805 $$ = new Attribute (current_attr_target, expr, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
810 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
813 opt_attribute_arguments
814 : /* empty */ { $$ = null; }
815 | OPEN_PARENS attribute_arguments CLOSE_PARENS
823 : /* empty */ { $$ = null; }
824 | positional_or_named_argument
826 Arguments a = new Arguments (4);
827 a.Add ((Argument) $1);
828 $$ = new Arguments [] { a, null };
830 | named_attribute_argument
832 Arguments a = new Arguments (4);
833 a.Add ((Argument) $1);
834 $$ = new Arguments [] { null, a };
836 | attribute_arguments COMMA positional_or_named_argument
838 Arguments[] o = (Arguments[]) $1;
840 Report.Error (1016, ((Argument) $3).Expr.Location, "Named attribute arguments must appear after the positional arguments");
841 o [0] = new Arguments (4);
844 Arguments args = ((Arguments) o [0]);
845 if (args.Count > 0 && !($3 is NamedArgument) && args [args.Count - 1] is NamedArgument)
846 Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
848 args.Add ((Argument) $3);
850 | attribute_arguments COMMA named_attribute_argument
852 Arguments[] o = (Arguments[]) $1;
854 o [1] = new Arguments (4);
857 ((Arguments) o [1]).Add ((Argument) $3);
861 positional_or_named_argument
864 $$ = new Argument ((Expression) $1);
869 named_attribute_argument
870 : IDENTIFIER ASSIGN expression
872 $$ = new NamedArgument ((LocatedToken) $1, (Expression) $3);
877 : IDENTIFIER COLON expression
879 if (RootContext.Version <= LanguageVersion.V_3)
880 Report.FeatureIsNotAvailable (GetLocation ($1), "named argument");
882 $$ = new NamedArgument ((LocatedToken) $1, (Expression) $3);
888 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
891 opt_class_member_declarations
893 | class_member_declarations
896 class_member_declarations
897 : class_member_declaration
898 | class_member_declarations
899 class_member_declaration
902 class_member_declaration
903 : constant_declaration // done
904 | field_declaration // done
905 | method_declaration // done
906 | property_declaration // done
907 | event_declaration // done
908 | indexer_declaration // done
909 | operator_declaration // done
910 | constructor_declaration // done
911 | destructor_declaration // done
915 Report.Error (1519, lexer.Location, "Unexpected symbol `{0}' in class, struct, or interface member declaration",
916 GetSymbolName (yyToken));
918 lexer.parsing_generic_declaration = false;
928 lexer.ConstraintsParsing = true;
930 type_declaration_name
932 MemberName name = MakeName ((MemberName) $6);
933 push_current_class (new Struct (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
936 opt_type_parameter_constraints_clauses
938 lexer.ConstraintsParsing = false;
940 current_class.SetParameterInfo ((ArrayList) $9);
942 if (RootContext.Documentation != null)
943 current_container.DocComment = Lexer.consume_doc_comment ();
947 --lexer.parsing_declaration;
948 if (RootContext.Documentation != null)
949 Lexer.doc_state = XmlCommentState.Allowed;
953 $$ = pop_current_class ();
955 | opt_attributes opt_modifiers opt_partial STRUCT error {
956 CheckIdentifierToken (yyToken, GetLocation ($5));
963 if (RootContext.Documentation != null)
964 Lexer.doc_state = XmlCommentState.Allowed;
966 opt_struct_member_declarations CLOSE_BRACE
969 opt_struct_member_declarations
971 | struct_member_declarations
974 struct_member_declarations
975 : struct_member_declaration
976 | struct_member_declarations struct_member_declaration
979 struct_member_declaration
980 : constant_declaration
983 | property_declaration
985 | indexer_declaration
986 | operator_declaration
987 | constructor_declaration
991 * This is only included so we can flag error 575:
992 * destructors only allowed on class types
994 | destructor_declaration
1002 constant_declarators
1005 int modflags = (int) $2;
1006 foreach (VariableDeclaration constant in (ArrayList) $5){
1007 Location l = constant.Location;
1008 if ((modflags & Modifiers.STATIC) != 0) {
1009 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
1013 Const c = new Const (
1014 current_class, (FullNamedExpression) $4, (string) constant.identifier,
1015 (Expression) constant.expression_or_array_initializer, modflags,
1016 (Attributes) $1, l);
1018 if (RootContext.Documentation != null) {
1019 c.DocComment = Lexer.consume_doc_comment ();
1020 Lexer.doc_state = XmlCommentState.Allowed;
1022 current_container.AddConstant (c);
1027 constant_declarators
1028 : constant_declarator
1030 ArrayList constants = new ArrayList (4);
1035 | constant_declarators COMMA constant_declarator
1038 ArrayList constants = (ArrayList) $1;
1047 ++lexer.parsing_block;
1049 constant_initializer
1051 --lexer.parsing_block;
1052 $$ = new VariableDeclaration ((LocatedToken) $1, $4);
1056 // A const field requires a value to be provided
1057 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
1062 constant_initializer
1063 : constant_expression
1071 variable_declarators
1074 FullNamedExpression type = (FullNamedExpression) $3;
1075 if (type == TypeManager.system_void_expr)
1076 Report.Error (670, GetLocation ($3), "Fields cannot have void type");
1080 current_array_type = null;
1082 foreach (VariableMemberDeclaration var in (ArrayList) $4){
1083 Field field = new Field (current_class, type, mod, var.MemberName, (Attributes) $1);
1085 field.Initializer = var.expression_or_array_initializer;
1087 if (RootContext.Documentation != null) {
1088 field.DocComment = Lexer.consume_doc_comment ();
1089 Lexer.doc_state = XmlCommentState.Allowed;
1091 current_container.AddField (field);
1092 $$ = field; // FIXME: might be better if it points to the top item
1099 fixed_variable_declarators
1102 FullNamedExpression type = (FullNamedExpression) $4;
1106 current_array_type = null;
1108 foreach (VariableDeclaration var in (ArrayList) $5) {
1109 FixedField field = new FixedField (current_class, type, mod, var.identifier,
1110 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
1112 if (RootContext.Documentation != null) {
1113 field.DocComment = Lexer.consume_doc_comment ();
1114 Lexer.doc_state = XmlCommentState.Allowed;
1116 current_container.AddField (field);
1117 $$ = field; // FIXME: might be better if it points to the top item
1126 Report.Error (1641, GetLocation ($4), "A fixed size buffer field must have the array size specifier after the field name");
1130 fixed_variable_declarators
1131 : fixed_variable_declarator
1133 ArrayList decl = new ArrayList (2);
1137 | fixed_variable_declarators COMMA fixed_variable_declarator
1139 ArrayList decls = (ArrayList) $1;
1145 fixed_variable_declarator
1146 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
1148 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1150 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1152 Report.Error (443, lexer.Location, "Value or constant expected");
1153 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1158 local_variable_declarators
1159 : local_variable_declarator
1161 ArrayList decl = new ArrayList (4);
1166 | local_variable_declarators COMMA local_variable_declarator
1168 ArrayList decls = (ArrayList) $1;
1174 local_variable_declarator
1175 : IDENTIFIER ASSIGN local_variable_initializer
1177 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1181 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1183 | IDENTIFIER variable_bad_array
1189 local_variable_initializer
1192 | STACKALLOC simple_type OPEN_BRACKET expression CLOSE_BRACKET
1194 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1198 $$ = new ArglistAccess ((Location) $1);
1200 | STACKALLOC simple_type
1202 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1203 $$ = new StackAlloc ((Expression) $2, null, (Location) $1);
1207 variable_declarators
1208 : variable_declarator
1210 ArrayList decl = new ArrayList (4);
1215 | variable_declarators COMMA variable_declarator
1217 ArrayList decls = (ArrayList) $1;
1224 : member_declaration_name ASSIGN
1226 ++lexer.parsing_block;
1227 lexer.parsing_generic_declaration = false;
1229 variable_initializer
1231 --lexer.parsing_block;
1232 $$ = new VariableMemberDeclaration ((MemberName) $1, $4);
1234 | member_declaration_name
1236 lexer.parsing_generic_declaration = false;
1237 $$ = new VariableMemberDeclaration ((MemberName) $1, null);
1239 | member_declaration_name variable_bad_array
1241 lexer.parsing_generic_declaration = false;
1247 : OPEN_BRACKET opt_expression CLOSE_BRACKET
1249 Report.Error (650, GetLocation ($1), "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1250 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1254 variable_initializer
1261 if (RootContext.Documentation != null)
1262 Lexer.doc_state = XmlCommentState.NotAllowed;
1266 Method method = (Method) $1;
1267 method.Block = (ToplevelBlock) $3;
1268 current_container.AddMethod (method);
1270 if (current_container.Kind == Kind.Interface && method.Block != null) {
1271 Report.Error (531, method.Location, "`{0}': interface members cannot have a definition", method.GetSignatureForError ());
1274 current_generic_method = null;
1275 current_local_parameters = null;
1277 if (RootContext.Documentation != null)
1278 Lexer.doc_state = XmlCommentState.Allowed;
1286 method_declaration_name OPEN_PARENS
1288 valid_param_mod = ParameterModifierType.All;
1290 opt_formal_parameter_list CLOSE_PARENS
1292 lexer.ConstraintsParsing = true;
1294 opt_type_parameter_constraints_clauses
1296 lexer.ConstraintsParsing = false;
1297 valid_param_mod = 0;
1298 MemberName name = (MemberName) $4;
1299 current_local_parameters = (ParametersCompiled) $7;
1301 if ($10 != null && name.TypeArguments == null)
1302 Report.Error (80, lexer.Location,
1303 "Constraints are not allowed on non-generic declarations");
1307 GenericMethod generic = null;
1308 if (name.TypeArguments != null) {
1309 generic = new GenericMethod (current_namespace, current_class, name,
1310 (FullNamedExpression) $3, current_local_parameters);
1312 generic.SetParameterInfo ((ArrayList) $10);
1315 method = new Method (current_class, generic, (FullNamedExpression) $3, (int) $2,
1316 name, current_local_parameters, (Attributes) $1);
1318 current_generic_method = generic;
1320 if (RootContext.Documentation != null)
1321 method.DocComment = Lexer.consume_doc_comment ();
1328 VOID method_declaration_name
1331 valid_param_mod = ParameterModifierType.All;
1333 opt_formal_parameter_list CLOSE_PARENS
1335 lexer.ConstraintsParsing = true;
1337 opt_type_parameter_constraints_clauses
1339 lexer.ConstraintsParsing = false;
1340 valid_param_mod = 0;
1342 MemberName name = (MemberName) $5;
1343 current_local_parameters = (ParametersCompiled) $8;
1345 if ($10 != null && name.TypeArguments == null)
1346 Report.Error (80, lexer.Location,
1347 "Constraints are not allowed on non-generic declarations");
1350 GenericMethod generic = null;
1351 if (name.TypeArguments != null) {
1352 generic = new GenericMethod (current_namespace, current_class, name,
1353 TypeManager.system_void_expr, current_local_parameters);
1355 generic.SetParameterInfo ((ArrayList) $11);
1358 int modifiers = (int) $2;
1361 const int invalid_partial_mod = Modifiers.Accessibility | Modifiers.ABSTRACT | Modifiers.EXTERN |
1362 Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
1364 if ((modifiers & invalid_partial_mod) != 0) {
1365 Report.Error (750, name.Location, "A partial method cannot define access modifier or " +
1366 "any of abstract, extern, new, override, sealed, or virtual modifiers");
1367 modifiers &= ~invalid_partial_mod;
1370 if ((current_class.ModFlags & Modifiers.PARTIAL) == 0) {
1371 Report.Error (751, name.Location, "A partial method must be declared within a " +
1372 "partial class or partial struct");
1375 modifiers |= Modifiers.PARTIAL | Modifiers.PRIVATE;
1377 method = new Method (current_class, generic, TypeManager.system_void_expr,
1378 modifiers, name, current_local_parameters, (Attributes) $1);
1380 current_generic_method = generic;
1382 if (RootContext.Documentation != null)
1383 method.DocComment = Lexer.consume_doc_comment ();
1390 modifiers method_declaration_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1392 MemberName name = (MemberName) $5;
1393 Report.Error (1585, name.Location,
1394 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1396 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1397 0, name, (ParametersCompiled) $7, (Attributes) $1);
1399 current_local_parameters = (ParametersCompiled) $7;
1401 if (RootContext.Documentation != null)
1402 method.DocComment = Lexer.consume_doc_comment ();
1410 | SEMICOLON { $$ = null; }
1413 opt_formal_parameter_list
1414 : /* empty */ { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
1415 | formal_parameter_list
1418 formal_parameter_list
1421 ArrayList pars_list = (ArrayList) $1;
1423 Parameter [] pars = new Parameter [pars_list.Count];
1424 pars_list.CopyTo (pars);
1426 $$ = new ParametersCompiled (pars);
1428 | fixed_parameters COMMA parameter_array
1430 ArrayList pars_list = (ArrayList) $1;
1433 Parameter [] pars = new Parameter [pars_list.Count];
1434 pars_list.CopyTo (pars);
1436 $$ = new ParametersCompiled (pars);
1438 | fixed_parameters COMMA arglist_modifier
1440 ArrayList pars_list = (ArrayList) $1;
1441 pars_list.Add (new ArglistParameter (GetLocation ($3)));
1443 Parameter [] pars = new Parameter [pars_list.Count];
1444 pars_list.CopyTo (pars);
1446 $$ = new ParametersCompiled (pars, true);
1448 | parameter_array COMMA error
1451 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1454 | fixed_parameters COMMA parameter_array COMMA error
1457 Report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1460 | arglist_modifier COMMA error
1462 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1465 | fixed_parameters COMMA ARGLIST COMMA error
1467 Report.Error (257, (Location) $3, "An __arglist parameter must be the last parameter in a formal parameter list");
1472 $$ = new ParametersCompiled (new Parameter[] { (Parameter) $1 } );
1476 $$ = new ParametersCompiled (new Parameter [] { new ArglistParameter ((Location) $1) }, true);
1483 ArrayList pars = new ArrayList (4);
1484 Parameter p = (Parameter) $1;
1487 default_parameter_used = p.HasDefaultValue;
1490 | fixed_parameters COMMA fixed_parameter
1492 ArrayList pars = (ArrayList) $1;
1493 Parameter p = (Parameter) $3;
1495 if (p.HasExtensionMethodModifier)
1496 Report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
1497 else if (!p.HasDefaultValue && default_parameter_used)
1498 Report.Error (1737, p.Location, "Optional parameter cannot precede required parameters");
1500 default_parameter_used |= p.HasDefaultValue;
1509 opt_parameter_modifier
1513 LocatedToken lt = (LocatedToken) $4;
1514 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1517 opt_parameter_modifier
1519 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1521 LocatedToken lt = (LocatedToken) $4;
1522 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1526 opt_parameter_modifier
1530 Location l = GetLocation ($4);
1531 CheckIdentifierToken (yyToken, l);
1532 $$ = new Parameter ((FullNamedExpression) $3, "NeedSomeGeneratorHere", (Parameter.Modifier) $2, (Attributes) $1, l);
1535 opt_parameter_modifier
1541 if (RootContext.Version <= LanguageVersion.V_3) {
1542 Report.FeatureIsNotAvailable (GetLocation ($5), "optional parameter");
1545 Parameter.Modifier mod = (Parameter.Modifier) $2;
1546 if (mod != Parameter.Modifier.NONE) {
1548 case Parameter.Modifier.REF:
1549 case Parameter.Modifier.OUT:
1550 Report.Error (1741, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1551 Parameter.GetModifierSignature (mod));
1554 case Parameter.Modifier.This:
1555 Report.Error (1743, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1556 Parameter.GetModifierSignature (mod));
1559 throw new NotImplementedException (mod.ToString ());
1562 mod = Parameter.Modifier.NONE;
1565 if ((valid_param_mod & ParameterModifierType.DefaultValue) == 0)
1566 Report.Error (1065, GetLocation ($6), "Optional parameter is not valid in this context");
1568 LocatedToken lt = (LocatedToken) $4;
1569 $$ = new Parameter ((FullNamedExpression) $3, lt.Value, mod, (Attributes) $1, lt.Location);
1571 ((Parameter) $$).DefaultValue = (Expression) $6;
1575 opt_parameter_modifier
1576 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1577 | parameter_modifiers
1581 : parameter_modifier
1585 | parameter_modifiers parameter_modifier
1587 Parameter.Modifier p2 = (Parameter.Modifier)$2;
1588 Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1589 if (((Parameter.Modifier)$1 & p2) == p2) {
1590 Error_DuplicateParameterModifier (lexer.Location, p2);
1592 switch (mod & ~Parameter.Modifier.This) {
1593 case Parameter.Modifier.REF:
1594 Report.Error (1101, lexer.Location, "The parameter modifiers `this' and `ref' cannot be used altogether");
1596 case Parameter.Modifier.OUT:
1597 Report.Error (1102, lexer.Location, "The parameter modifiers `this' and `out' cannot be used altogether");
1600 Report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1611 if ((valid_param_mod & ParameterModifierType.Ref) == 0)
1612 Error_ParameterModifierNotValid ("ref", (Location)$1);
1614 $$ = Parameter.Modifier.REF;
1618 if ((valid_param_mod & ParameterModifierType.Out) == 0)
1619 Error_ParameterModifierNotValid ("out", (Location)$1);
1621 $$ = Parameter.Modifier.OUT;
1625 if ((valid_param_mod & ParameterModifierType.This) == 0)
1626 Error_ParameterModifierNotValid ("this", (Location)$1);
1628 if (RootContext.Version <= LanguageVersion.ISO_2)
1629 Report.FeatureIsNotAvailable (GetLocation ($1), "extension methods");
1631 $$ = Parameter.Modifier.This;
1636 : opt_attributes params_modifier type IDENTIFIER
1638 LocatedToken lt = (LocatedToken) $4;
1639 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1641 | opt_attributes params_modifier type IDENTIFIER ASSIGN constant_expression
1643 Report.Error (1751, GetLocation ($2), "Cannot specify a default value for a parameter array");
1645 LocatedToken lt = (LocatedToken) $4;
1646 $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1648 | opt_attributes params_modifier type error {
1649 CheckIdentifierToken (yyToken, GetLocation ($4));
1657 if ((valid_param_mod & ParameterModifierType.Params) == 0)
1658 Report.Error (1670, ((Location) $1), "The `params' modifier is not allowed in current context");
1660 | PARAMS parameter_modifier
1662 Parameter.Modifier mod = (Parameter.Modifier)$2;
1663 if ((mod & Parameter.Modifier.This) != 0) {
1664 Report.Error (1104, (Location)$1, "The parameter modifiers `this' and `params' cannot be used altogether");
1666 Report.Error (1611, (Location)$1, "The params parameter cannot be declared as ref or out");
1669 | PARAMS params_modifier
1671 Error_DuplicateParameterModifier ((Location)$1, Parameter.Modifier.PARAMS);
1678 if ((valid_param_mod & ParameterModifierType.Arglist) == 0)
1679 Report.Error (1669, (Location) $1, "__arglist is not valid in this context");
1683 property_declaration
1687 member_declaration_name
1689 if (RootContext.Documentation != null)
1690 tmpComment = Lexer.consume_doc_comment ();
1694 implicit_value_parameter_type = (FullNamedExpression) $3;
1695 lexer.PropertyParsing = true;
1697 accessor_declarations
1699 lexer.PropertyParsing = false;
1700 has_get = has_set = false;
1705 Accessors accessors = (Accessors) $8;
1706 Accessor get_block = accessors != null ? accessors.get_or_add : null;
1707 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
1708 bool order = accessors != null ? accessors.declared_in_reverse : false;
1710 MemberName name = (MemberName) $4;
1711 FullNamedExpression ptype = (FullNamedExpression) $3;
1713 prop = new Property (current_class, ptype, (int) $2,
1714 name, (Attributes) $1, get_block, set_block, order, current_block);
1716 if (ptype == TypeManager.system_void_expr)
1717 Report.Error (547, name.Location, "`{0}': property or indexer cannot have void type", prop.GetSignatureForError ());
1719 if (accessors == null)
1720 Report.Error (548, prop.Location, "`{0}': property or indexer must have at least one accessor", prop.GetSignatureForError ());
1722 if (current_container.Kind == Kind.Interface) {
1723 if (prop.Get.Block != null)
1724 Report.Error (531, prop.Location, "`{0}.get': interface members cannot have a definition", prop.GetSignatureForError ());
1726 if (prop.Set.Block != null)
1727 Report.Error (531, prop.Location, "`{0}.set': interface members cannot have a definition", prop.GetSignatureForError ());
1730 current_container.AddProperty (prop);
1731 implicit_value_parameter_type = null;
1733 if (RootContext.Documentation != null)
1734 prop.DocComment = ConsumeStoredComment ();
1739 accessor_declarations
1740 : get_accessor_declaration
1742 $$ = new Accessors ((Accessor) $1, null);
1744 | get_accessor_declaration accessor_declarations
1746 Accessors accessors = (Accessors) $2;
1747 accessors.get_or_add = (Accessor) $1;
1750 | set_accessor_declaration
1752 $$ = new Accessors (null, (Accessor) $1);
1754 | set_accessor_declaration accessor_declarations
1756 Accessors accessors = (Accessors) $2;
1757 accessors.set_or_remove = (Accessor) $1;
1758 accessors.declared_in_reverse = true;
1763 if (yyToken == Token.CLOSE_BRACE) {
1766 if (yyToken == Token.SEMICOLON)
1767 Report.Error (1597, lexer.Location, "Semicolon after method or accessor block is not valid");
1769 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1771 $$ = new Accessors (null, null);
1776 get_accessor_declaration
1777 : opt_attributes opt_modifiers GET
1779 // If this is not the case, then current_local_parameters has already
1780 // been set in indexer_declaration
1781 if (parsing_indexer == false)
1782 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
1784 current_local_parameters = indexer_parameters;
1785 lexer.PropertyParsing = false;
1790 Report.Error (1007, GetLocation ($3), "Property accessor already defined");
1793 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, current_local_parameters, (Location) $3);
1795 current_local_parameters = null;
1796 lexer.PropertyParsing = true;
1798 if (RootContext.Documentation != null)
1799 if (Lexer.doc_state == XmlCommentState.Error)
1800 Lexer.doc_state = XmlCommentState.NotAllowed;
1806 set_accessor_declaration
1807 : opt_attributes opt_modifiers SET
1809 Parameter implicit_value_parameter = new Parameter (
1810 implicit_value_parameter_type, "value",
1811 Parameter.Modifier.NONE, null, (Location) $3);
1813 if (!parsing_indexer) {
1814 current_local_parameters = new ParametersCompiled (new Parameter [] { implicit_value_parameter });
1816 current_local_parameters = ParametersCompiled.MergeGenerated (
1817 indexer_parameters, true, implicit_value_parameter, null);
1820 lexer.PropertyParsing = false;
1825 Report.Error (1007, GetLocation ($3), "Property accessor already defined");
1828 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, current_local_parameters, (Location) $3);
1830 current_local_parameters = null;
1831 lexer.PropertyParsing = true;
1833 if (RootContext.Documentation != null
1834 && Lexer.doc_state == XmlCommentState.Error)
1835 Lexer.doc_state = XmlCommentState.NotAllowed;
1849 Error_SyntaxError (1043, yyToken);
1854 interface_declaration
1860 lexer.ConstraintsParsing = true;
1862 type_declaration_name
1864 MemberName name = MakeName ((MemberName) $6);
1865 push_current_class (new Interface (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
1868 opt_type_parameter_constraints_clauses
1870 lexer.ConstraintsParsing = false;
1872 current_class.SetParameterInfo ((ArrayList) $9);
1874 if (RootContext.Documentation != null) {
1875 current_container.DocComment = Lexer.consume_doc_comment ();
1876 Lexer.doc_state = XmlCommentState.Allowed;
1881 --lexer.parsing_declaration;
1882 if (RootContext.Documentation != null)
1883 Lexer.doc_state = XmlCommentState.Allowed;
1887 $$ = pop_current_class ();
1889 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1890 CheckIdentifierToken (yyToken, GetLocation ($5));
1896 opt_interface_member_declarations
1900 opt_interface_member_declarations
1902 | interface_member_declarations
1905 interface_member_declarations
1906 : interface_member_declaration
1907 | interface_member_declarations interface_member_declaration
1910 interface_member_declaration
1911 : constant_declaration
1913 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1917 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1919 | method_declaration
1920 | property_declaration
1922 | indexer_declaration
1923 | operator_declaration
1925 Report.Error (567, GetLocation ($1), "Interfaces cannot contain operators");
1927 | constructor_declaration
1929 Report.Error (526, GetLocation ($1), "Interfaces cannot contain contructors");
1933 Report.Error (524, GetLocation ($1), "Interfaces cannot declare classes, structs, interfaces, delegates, or enumerations");
1937 operator_declaration
1938 : opt_attributes opt_modifiers operator_declarator
1946 OperatorDeclaration decl = (OperatorDeclaration) $3;
1947 Operator op = new Operator (
1948 current_class, decl.optype, decl.ret_type, (int) $2,
1949 current_local_parameters,
1950 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1952 if (RootContext.Documentation != null) {
1953 op.DocComment = tmpComment;
1954 Lexer.doc_state = XmlCommentState.Allowed;
1957 // Note again, checking is done in semantic analysis
1958 current_container.AddOperator (op);
1960 current_local_parameters = null;
1966 | SEMICOLON { $$ = null; }
1970 : type_expression_or_array
1973 Report.Error (590, lexer.Location, "User-defined operators cannot return void");
1974 $$ = TypeManager.system_void_expr;
1979 : operator_type OPERATOR overloadable_operator OPEN_PARENS
1981 valid_param_mod = ParameterModifierType.DefaultValue;
1983 opt_formal_parameter_list CLOSE_PARENS
1985 valid_param_mod = 0;
1987 Location loc = (Location) $2;
1988 Operator.OpType op = (Operator.OpType) $3;
1989 current_local_parameters = (ParametersCompiled)$6;
1991 int p_count = current_local_parameters.Count;
1993 if (op == Operator.OpType.Addition)
1994 op = Operator.OpType.UnaryPlus;
1995 else if (op == Operator.OpType.Subtraction)
1996 op = Operator.OpType.UnaryNegation;
1999 if (IsUnaryOperator (op)) {
2001 Report.Error (1020, loc, "Overloadable binary operator expected");
2002 } else if (p_count != 1) {
2003 Report.Error (1535, loc, "Overloaded unary operator `{0}' takes one parameter",
2004 Operator.GetName (op));
2008 Report.Error (1534, loc, "Overloaded binary operator `{0}' takes two parameters",
2009 Operator.GetName (op));
2010 } else if (p_count != 2) {
2011 Report.Error (1019, loc, "Overloadable unary operator expected");
2015 if (RootContext.Documentation != null) {
2016 tmpComment = Lexer.consume_doc_comment ();
2017 Lexer.doc_state = XmlCommentState.NotAllowed;
2020 $$ = new OperatorDeclaration (op, (FullNamedExpression) $1, loc);
2022 | conversion_operator_declarator
2025 overloadable_operator
2027 : BANG { $$ = Operator.OpType.LogicalNot; }
2028 | TILDE { $$ = Operator.OpType.OnesComplement; }
2029 | OP_INC { $$ = Operator.OpType.Increment; }
2030 | OP_DEC { $$ = Operator.OpType.Decrement; }
2031 | TRUE { $$ = Operator.OpType.True; }
2032 | FALSE { $$ = Operator.OpType.False; }
2033 // Unary and binary:
2034 | PLUS { $$ = Operator.OpType.Addition; }
2035 | MINUS { $$ = Operator.OpType.Subtraction; }
2037 | STAR { $$ = Operator.OpType.Multiply; }
2038 | DIV { $$ = Operator.OpType.Division; }
2039 | PERCENT { $$ = Operator.OpType.Modulus; }
2040 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
2041 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
2042 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
2043 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
2044 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
2045 | OP_EQ { $$ = Operator.OpType.Equality; }
2046 | OP_NE { $$ = Operator.OpType.Inequality; }
2047 | OP_GT { $$ = Operator.OpType.GreaterThan; }
2048 | OP_LT { $$ = Operator.OpType.LessThan; }
2049 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
2050 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
2053 conversion_operator_declarator
2054 : IMPLICIT OPERATOR type OPEN_PARENS
2056 valid_param_mod = ParameterModifierType.DefaultValue;
2058 opt_formal_parameter_list CLOSE_PARENS
2060 valid_param_mod = 0;
2062 Location loc = (Location) $2;
2063 current_local_parameters = (ParametersCompiled)$6;
2065 if (RootContext.Documentation != null) {
2066 tmpComment = Lexer.consume_doc_comment ();
2067 Lexer.doc_state = XmlCommentState.NotAllowed;
2070 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (FullNamedExpression) $3, loc);
2072 | EXPLICIT OPERATOR type OPEN_PARENS
2074 valid_param_mod = ParameterModifierType.DefaultValue;
2076 opt_formal_parameter_list CLOSE_PARENS
2078 valid_param_mod = 0;
2080 Location loc = (Location) $2;
2081 current_local_parameters = (ParametersCompiled)$6;
2083 if (RootContext.Documentation != null) {
2084 tmpComment = Lexer.consume_doc_comment ();
2085 Lexer.doc_state = XmlCommentState.NotAllowed;
2088 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (FullNamedExpression) $3, loc);
2092 Error_SyntaxError (yyToken);
2093 $$ = new OperatorDeclaration (Operator.OpType.Implicit, null, GetLocation ($1));
2097 Error_SyntaxError (yyToken);
2098 $$ = new OperatorDeclaration (Operator.OpType.Explicit, null, GetLocation ($1));
2102 constructor_declaration
2103 : constructor_declarator
2106 Constructor c = (Constructor) $1;
2107 c.Block = (ToplevelBlock) $2;
2109 if (RootContext.Documentation != null)
2110 c.DocComment = ConsumeStoredComment ();
2112 current_container.AddConstructor (c);
2114 current_local_parameters = null;
2115 if (RootContext.Documentation != null)
2116 Lexer.doc_state = XmlCommentState.Allowed;
2120 constructor_declarator
2125 if (RootContext.Documentation != null) {
2126 tmpComment = Lexer.consume_doc_comment ();
2127 Lexer.doc_state = XmlCommentState.Allowed;
2130 valid_param_mod = ParameterModifierType.All;
2132 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2134 valid_param_mod = 0;
2135 current_local_parameters = (ParametersCompiled) $6;
2138 // start block here, so possible anonymous methods inside
2139 // constructor initializer can get correct parent block
2141 start_block (lexer.Location);
2143 opt_constructor_initializer
2145 LocatedToken lt = (LocatedToken) $3;
2146 int mods = (int) $2;
2147 ConstructorInitializer ci = (ConstructorInitializer) $9;
2149 Constructor c = new Constructor (current_class, lt.Value, mods,
2150 (Attributes) $1, current_local_parameters, ci, lt.Location);
2152 if (lt.Value != current_container.MemberName.Name) {
2153 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
2154 } else if ((mods & Modifiers.STATIC) != 0) {
2155 if ((mods & Modifiers.Accessibility) != 0){
2156 Report.Error (515, c.Location,
2157 "`{0}': static constructor cannot have an access modifier",
2158 c.GetSignatureForError ());
2161 Report.Error (514, c.Location,
2162 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2163 c.GetSignatureForError ());
2174 | SEMICOLON { current_block = null; $$ = null; }
2177 opt_constructor_initializer
2179 | constructor_initializer
2182 constructor_initializer
2183 : COLON BASE OPEN_PARENS
2185 ++lexer.parsing_block;
2187 opt_argument_list CLOSE_PARENS
2189 --lexer.parsing_block;
2190 $$ = new ConstructorBaseInitializer ((Arguments) $5, (Location) $2);
2192 | COLON THIS OPEN_PARENS
2194 ++lexer.parsing_block;
2196 opt_argument_list CLOSE_PARENS
2198 --lexer.parsing_block;
2199 $$ = new ConstructorThisInitializer ((Arguments) $5, (Location) $2);
2202 Report.Error (1018, GetLocation ($1), "Keyword `this' or `base' expected");
2207 destructor_declaration
2208 : opt_attributes opt_modifiers TILDE
2210 if (RootContext.Documentation != null) {
2211 tmpComment = Lexer.consume_doc_comment ();
2212 Lexer.doc_state = XmlCommentState.NotAllowed;
2215 current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2217 IDENTIFIER OPEN_PARENS CLOSE_PARENS method_body
2219 LocatedToken lt = (LocatedToken) $5;
2220 if (lt.Value != current_container.MemberName.Name){
2221 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2222 } else if (current_container.Kind != Kind.Class){
2223 Report.Error (575, lt.Location, "Only class types can contain destructor");
2225 Destructor d = new Destructor (current_class, (int) $2,
2226 ParametersCompiled.EmptyReadOnlyParameters, (Attributes) $1, lt.Location);
2227 if (RootContext.Documentation != null)
2228 d.DocComment = ConsumeStoredComment ();
2230 d.Block = (ToplevelBlock) $8;
2231 current_container.AddMethod (d);
2234 current_local_parameters = null;
2241 EVENT type variable_declarators SEMICOLON
2243 current_array_type = null;
2244 foreach (VariableMemberDeclaration var in (ArrayList) $5) {
2246 EventField e = new EventField (
2247 current_class, (FullNamedExpression) $4, (int) $2, var.MemberName, (Attributes) $1);
2249 if (var.expression_or_array_initializer != null) {
2250 if (current_container.Kind == Kind.Interface) {
2251 Report.Error (68, e.Location, "`{0}': event in interface cannot have initializer", e.GetSignatureForError ());
2254 e.Initializer = var.expression_or_array_initializer;
2257 if (var.MemberName.Left != null) {
2258 Report.Error (71, e.Location,
2259 "`{0}': An explicit interface implementation of an event must use property syntax",
2260 e.GetSignatureForError ());
2263 current_container.AddEvent (e);
2265 if (RootContext.Documentation != null) {
2266 e.DocComment = Lexer.consume_doc_comment ();
2267 Lexer.doc_state = XmlCommentState.Allowed;
2273 EVENT type member_declaration_name
2276 implicit_value_parameter_type = (FullNamedExpression) $4;
2277 current_local_parameters = new ParametersCompiled (
2278 new Parameter (implicit_value_parameter_type, "value",
2279 Parameter.Modifier.NONE, null, GetLocation ($3)));
2281 lexer.EventParsing = true;
2283 event_accessor_declarations
2285 lexer.EventParsing = false;
2289 MemberName name = (MemberName) $5;
2291 if (current_container.Kind == Kind.Interface) {
2292 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
2293 $8 = new Accessors (null, null);
2294 } else if ($8 == null) {
2295 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2296 current_container.GetSignatureForError (), name.GetSignatureForError ());
2297 $8 = new Accessors (null, null);
2300 Accessors accessors = (Accessors) $8;
2302 if (accessors.get_or_add == null || accessors.set_or_remove == null)
2303 // CS0073 is already reported, so no CS0065 here.
2306 Event e = new EventProperty (
2307 current_class, (FullNamedExpression) $4, (int) $2, name,
2308 (Attributes) $1, accessors.get_or_add, accessors.set_or_remove);
2309 if (RootContext.Documentation != null) {
2310 e.DocComment = Lexer.consume_doc_comment ();
2311 Lexer.doc_state = XmlCommentState.Allowed;
2314 current_container.AddEvent (e);
2315 implicit_value_parameter_type = null;
2317 current_local_parameters = null;
2319 | opt_attributes opt_modifiers EVENT type member_declaration_name error
2321 MemberName mn = (MemberName) $5;
2322 if (mn.Left != null)
2323 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2325 if (RootContext.Documentation != null)
2326 Lexer.doc_state = XmlCommentState.Allowed;
2328 Error_SyntaxError (yyToken);
2333 event_accessor_declarations
2334 : add_accessor_declaration remove_accessor_declaration
2336 $$ = new Accessors ((Accessor) $1, (Accessor) $2);
2338 | remove_accessor_declaration add_accessor_declaration
2340 Accessors accessors = new Accessors ((Accessor) $2, (Accessor) $1);
2341 accessors.declared_in_reverse = true;
2344 | add_accessor_declaration { $$ = null; }
2345 | remove_accessor_declaration { $$ = null; }
2348 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2354 add_accessor_declaration
2355 : opt_attributes ADD
2357 lexer.EventParsing = false;
2361 Accessor accessor = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, (Location) $2);
2362 lexer.EventParsing = true;
2365 | opt_attributes ADD error {
2366 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2369 | opt_attributes modifiers ADD {
2370 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2375 remove_accessor_declaration
2376 : opt_attributes REMOVE
2378 lexer.EventParsing = false;
2382 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, null, (Location) $2);
2383 lexer.EventParsing = true;
2385 | opt_attributes REMOVE error {
2386 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2389 | opt_attributes modifiers REMOVE {
2390 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2396 : opt_attributes opt_modifiers
2397 member_type indexer_declaration_name OPEN_BRACKET
2399 valid_param_mod = ParameterModifierType.Params | ParameterModifierType.DefaultValue;
2401 opt_formal_parameter_list CLOSE_BRACKET
2404 valid_param_mod = 0;
2405 implicit_value_parameter_type = (FullNamedExpression) $3;
2406 indexer_parameters = (ParametersCompiled) $7;
2408 if (indexer_parameters.IsEmpty) {
2409 Report.Error (1551, GetLocation ($5), "Indexers must have at least one parameter");
2412 if (RootContext.Documentation != null) {
2413 tmpComment = Lexer.consume_doc_comment ();
2414 Lexer.doc_state = XmlCommentState.Allowed;
2417 lexer.PropertyParsing = true;
2418 parsing_indexer = true;
2421 accessor_declarations
2423 lexer.PropertyParsing = false;
2424 has_get = has_set = false;
2425 parsing_indexer = false;
2429 Accessors accessors = (Accessors) $11;
2430 Accessor get_block = accessors != null ? accessors.get_or_add : null;
2431 Accessor set_block = accessors != null ? accessors.set_or_remove : null;
2432 bool order = accessors != null ? accessors.declared_in_reverse : false;
2434 Indexer indexer = new Indexer (current_class, (FullNamedExpression) $3,
2435 (MemberName)$4, (int) $2, (ParametersCompiled) $7, (Attributes) $1,
2436 get_block, set_block, order);
2438 if ($3 == TypeManager.system_void_expr)
2439 Report.Error (620, GetLocation ($3), "`{0}': indexer return type cannot be `void'", indexer.GetSignatureForError ());
2441 if (accessors == null)
2442 Report.Error (548, indexer.Location, "`{0}': property or indexer must have at least one accessor", indexer.GetSignatureForError ());
2444 if (current_container.Kind == Kind.Interface) {
2445 if (indexer.Get.Block != null)
2446 Report.Error (531, indexer.Location, "`{0}.get': interface members cannot have a definition", indexer.GetSignatureForError ());
2448 if (indexer.Set.Block != null)
2449 Report.Error (531, indexer.Location, "`{0}.set': interface members cannot have a definition", indexer.GetSignatureForError ());
2452 if (RootContext.Documentation != null)
2453 indexer.DocComment = ConsumeStoredComment ();
2455 current_container.AddIndexer (indexer);
2457 current_local_parameters = null;
2458 implicit_value_parameter_type = null;
2459 indexer_parameters = null;
2466 ENUM type_declaration_name
2468 if (RootContext.Documentation != null)
2469 enumTypeComment = Lexer.consume_doc_comment ();
2474 MemberName name = (MemberName) $4;
2475 if (name.IsGeneric) {
2476 Report.Error (1675, name.Location, "Enums cannot have type parameters");
2479 name = MakeName (name);
2480 Enum e = new Enum (current_namespace, current_class, (TypeExpr) $5, (int) $2,
2481 name, (Attributes) $1);
2483 if (RootContext.Documentation != null)
2484 e.DocComment = enumTypeComment;
2487 EnumMember em = null;
2488 foreach (VariableDeclaration ev in (ArrayList) $7) {
2489 em = new EnumMember (
2490 e, em, ev.identifier, (Expression) ev.expression_or_array_initializer,
2491 ev.OptAttributes, ev.Location);
2493 // if (RootContext.Documentation != null)
2494 em.DocComment = ev.DocComment;
2496 e.AddEnumMember (em);
2498 if (RootContext.EvalMode)
2499 undo.AddTypeContainer (current_container, e);
2501 current_container.AddTypeContainer (e);
2509 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2512 if ($2 != TypeManager.system_int32_expr && $2 != TypeManager.system_uint32_expr &&
2513 $2 != TypeManager.system_int64_expr && $2 != TypeManager.system_uint64_expr &&
2514 $2 != TypeManager.system_int16_expr && $2 != TypeManager.system_uint16_expr &&
2515 $2 != TypeManager.system_byte_expr && $2 != TypeManager.system_sbyte_expr)
2516 Enum.Error_1008 (GetLocation ($2), Report);
2522 Error_TypeExpected (lexer.Location);
2529 if (RootContext.Documentation != null)
2530 Lexer.doc_state = XmlCommentState.Allowed;
2532 opt_enum_member_declarations
2534 // here will be evaluated after CLOSE_BLACE is consumed.
2535 if (RootContext.Documentation != null)
2536 Lexer.doc_state = XmlCommentState.Allowed;
2544 opt_enum_member_declarations
2545 : /* empty */ { $$ = new ArrayList (0); }
2546 | enum_member_declarations opt_comma { $$ = $1; }
2549 enum_member_declarations
2550 : enum_member_declaration
2552 ArrayList l = new ArrayList (4);
2557 | enum_member_declarations COMMA enum_member_declaration
2559 ArrayList l = (ArrayList) $1;
2567 enum_member_declaration
2568 : opt_attributes IDENTIFIER
2570 VariableDeclaration vd = new VariableDeclaration (
2571 (LocatedToken) $2, null, (Attributes) $1);
2573 if (RootContext.Documentation != null) {
2574 vd.DocComment = Lexer.consume_doc_comment ();
2575 Lexer.doc_state = XmlCommentState.Allowed;
2580 | opt_attributes IDENTIFIER
2582 ++lexer.parsing_block;
2583 if (RootContext.Documentation != null) {
2584 tmpComment = Lexer.consume_doc_comment ();
2585 Lexer.doc_state = XmlCommentState.NotAllowed;
2588 ASSIGN constant_expression
2590 --lexer.parsing_block;
2591 VariableDeclaration vd = new VariableDeclaration (
2592 (LocatedToken) $2, $5, (Attributes) $1);
2594 if (RootContext.Documentation != null)
2595 vd.DocComment = ConsumeStoredComment ();
2601 delegate_declaration
2605 member_type type_declaration_name
2608 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out | ParameterModifierType.Params | ParameterModifierType.DefaultValue;
2610 opt_formal_parameter_list CLOSE_PARENS
2612 valid_param_mod = 0;
2614 MemberName name = MakeName ((MemberName) $5);
2615 ParametersCompiled p = (ParametersCompiled) $8;
2617 Delegate del = new Delegate (current_namespace, current_class, (FullNamedExpression) $4,
2618 (int) $2, name, p, (Attributes) $1);
2620 if (RootContext.Documentation != null) {
2621 del.DocComment = Lexer.consume_doc_comment ();
2622 Lexer.doc_state = XmlCommentState.Allowed;
2625 current_container.AddDelegate (del);
2626 current_delegate = del;
2627 lexer.ConstraintsParsing = true;
2629 opt_type_parameter_constraints_clauses
2631 lexer.ConstraintsParsing = false;
2635 current_delegate.SetParameterInfo ((ArrayList) $11);
2636 $$ = current_delegate;
2638 current_delegate = null;
2649 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
2650 Report.FeatureIsNotSupported (lexer.Location, "nullable types");
2651 else if (RootContext.Version < LanguageVersion.ISO_2)
2652 Report.FeatureIsNotAvailable (lexer.Location, "nullable types");
2658 namespace_or_type_name
2660 | qualified_alias_member IDENTIFIER opt_type_argument_list
2662 LocatedToken lt1 = (LocatedToken) $1;
2663 LocatedToken lt2 = (LocatedToken) $2;
2665 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2671 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list
2673 LocatedToken lt = (LocatedToken) $3;
2674 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $4, lt.Location);
2679 : IDENTIFIER opt_type_argument_list
2681 LocatedToken lt = (LocatedToken) $1;
2682 $$ = new MemberName (lt.Value, (TypeArguments)$2, lt.Location);
2687 // Generics arguments (any type, without attributes)
2689 opt_type_argument_list
2690 : /* empty */ { $$ = null; }
2691 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2693 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
2694 Report.FeatureIsNotSupported (lexer.Location, "generics");
2695 else if (RootContext.Version < LanguageVersion.ISO_2)
2696 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2700 | OP_GENERICS_LT error
2702 Error_TypeExpected (lexer.Location);
2703 $$ = new TypeArguments ();
2710 TypeArguments type_args = new TypeArguments ();
2711 type_args.Add ((FullNamedExpression) $1);
2714 | type_arguments COMMA type
2716 TypeArguments type_args = (TypeArguments) $1;
2717 type_args.Add ((FullNamedExpression) $3);
2723 // Generics parameters (identifiers only, with attributes), used in type or method declarations
2725 type_declaration_name
2728 lexer.parsing_generic_declaration = true;
2730 opt_type_parameter_list
2732 lexer.parsing_generic_declaration = false;
2733 LocatedToken lt = (LocatedToken) $1;
2734 $$ = new MemberName (lt.Value, (TypeArguments)$3, lt.Location);
2738 member_declaration_name
2739 : method_declaration_name
2741 MemberName mn = (MemberName)$1;
2742 if (mn.TypeArguments != null)
2743 syntax_error (mn.Location, string.Format ("Member `{0}' cannot declare type arguments",
2744 mn.GetSignatureForError ()));
2748 method_declaration_name
2749 : type_declaration_name
2750 | explicit_interface IDENTIFIER opt_type_parameter_list
2752 lexer.parsing_generic_declaration = false;
2753 LocatedToken lt = (LocatedToken) $2;
2754 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $3, lt.Location);
2758 indexer_declaration_name
2761 lexer.parsing_generic_declaration = false;
2762 $$ = new MemberName (TypeContainer.DefaultIndexerName, GetLocation ($1));
2764 | explicit_interface THIS
2766 lexer.parsing_generic_declaration = false;
2767 $$ = new MemberName ((MemberName) $1, TypeContainer.DefaultIndexerName, null, GetLocation ($1));
2772 : IDENTIFIER opt_type_argument_list DOT
2774 LocatedToken lt = (LocatedToken) $1;
2775 $$ = new MemberName (lt.Value, (TypeArguments) $2, lt.Location);
2777 | qualified_alias_member IDENTIFIER opt_type_argument_list DOT
2779 LocatedToken lt1 = (LocatedToken) $1;
2780 LocatedToken lt2 = (LocatedToken) $2;
2782 $$ = new MemberName (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2784 | explicit_interface IDENTIFIER opt_type_argument_list DOT
2786 LocatedToken lt = (LocatedToken) $2;
2787 $$ = new MemberName ((MemberName) $1, lt.Value, (TypeArguments) $3, lt.Location);
2791 opt_type_parameter_list
2792 : /* empty */ { $$ = null; }
2793 | OP_GENERICS_LT_DECL type_parameters OP_GENERICS_GT
2795 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
2796 Report.FeatureIsNotSupported (lexer.Location, "generics");
2797 else if (RootContext.Version < LanguageVersion.ISO_2)
2798 Report.FeatureIsNotAvailable (GetLocation ($1), "generics");
2807 TypeArguments type_args = new TypeArguments ();
2808 type_args.Add ((FullNamedExpression)$1);
2811 | type_parameters COMMA type_parameter
2813 TypeArguments type_args = (TypeArguments) $1;
2814 type_args.Add ((FullNamedExpression)$3);
2820 : opt_attributes opt_type_parameter_variance IDENTIFIER
2822 LocatedToken lt = (LocatedToken)$3;
2823 $$ = new TypeParameterName (lt.Value, (Attributes)$1, (Variance) $2, lt.Location);
2827 if (GetTokenName (yyToken) == "type")
2828 Report.Error (81, GetLocation ($1), "Type parameter declaration must be an identifier not a type");
2830 Error_SyntaxError (yyToken);
2832 $$ = new TypeParameterName ("", null, lexer.Location);
2837 // All types where void is allowed
2840 : type_expression_or_array
2843 $$ = TypeManager.system_void_expr;
2850 lexer.parsing_generic_declaration = true;
2855 // A type which does not allow `void' to be used
2858 : type_expression_or_array
2861 Expression.Error_VoidInvalidInTheContext (lexer.Location, Report);
2862 $$ = TypeManager.system_void_expr;
2870 Expression.Error_VoidInvalidInTheContext (lexer.Location, Report);
2871 $$ = TypeManager.system_void_expr;
2876 : type_expression_or_array
2879 Report.Error (1536, lexer.Location, "Invalid parameter type `void'");
2880 $$ = TypeManager.system_void_expr;
2884 type_expression_or_array
2886 | type_expression rank_specifiers
2888 string rank_specifiers = (string) $2;
2889 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank_specifiers);
2894 : namespace_or_type_name opt_nullable
2896 MemberName name = (MemberName) $1;
2899 $$ = new ComposedCast (name.GetTypeExpression (), "?", lexer.Location);
2901 if (name.Left == null && name.Name == "var")
2902 $$ = current_array_type = new VarExpr (name.Location);
2904 $$ = name.GetTypeExpression ();
2907 | builtin_types opt_nullable
2910 $$ = new ComposedCast ((FullNamedExpression) $1, "?", lexer.Location);
2912 | type_expression STAR
2915 // Note that here only unmanaged types are allowed but we
2916 // can't perform checks during this phase - we do it during
2917 // semantic analysis.
2919 $$ = new ComposedCast ((FullNamedExpression) $1, "*", Lexer.Location);
2923 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2930 ArrayList types = new ArrayList (2);
2934 | type_list COMMA base_type_name
2936 ArrayList types = (ArrayList) $1;
2945 if ($1 is ComposedCast)
2946 Report.Error (1521, GetLocation ($1), "Invalid base type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
2951 Error_TypeExpected (lexer.Location);
2956 * replaces all the productions for isolating the various
2957 * simple types, but we need this to reuse it easily in variable_type
2960 : OBJECT { $$ = TypeManager.system_object_expr; }
2961 | STRING { $$ = TypeManager.system_string_expr; }
2962 | BOOL { $$ = TypeManager.system_boolean_expr; }
2963 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2964 | FLOAT { $$ = TypeManager.system_single_expr; }
2965 | DOUBLE { $$ = TypeManager.system_double_expr; }
2970 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2971 | BYTE { $$ = TypeManager.system_byte_expr; }
2972 | SHORT { $$ = TypeManager.system_int16_expr; }
2973 | USHORT { $$ = TypeManager.system_uint16_expr; }
2974 | INT { $$ = TypeManager.system_int32_expr; }
2975 | UINT { $$ = TypeManager.system_uint32_expr; }
2976 | LONG { $$ = TypeManager.system_int64_expr; }
2977 | ULONG { $$ = TypeManager.system_uint64_expr; }
2978 | CHAR { $$ = TypeManager.system_char_expr; }
2985 $$ = TypeManager.system_void_expr;
2990 // Expressions, section 7.5
2995 : primary_expression_no_array_creation
2996 | array_creation_expression
2999 primary_expression_no_array_creation
3001 | IDENTIFIER opt_type_argument_list
3003 LocatedToken lt = (LocatedToken) $1;
3004 $$ = new SimpleName (MemberName.MakeName (lt.Value, (TypeArguments)$2), (TypeArguments)$2, lt.Location);
3006 | IDENTIFIER GENERATE_COMPLETION {
3007 LocatedToken lt = (LocatedToken) $1;
3008 $$ = new CompletionSimpleName (MemberName.MakeName (lt.Value, null), lt.Location);
3010 | parenthesized_expression
3011 | default_value_expression
3013 | invocation_expression
3017 | post_increment_expression
3018 | post_decrement_expression
3019 | object_or_delegate_creation_expression
3020 | anonymous_type_expression
3023 | checked_expression
3024 | unchecked_expression
3025 | pointer_member_access
3026 | anonymous_method_expression
3033 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
3034 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
3035 | NULL { $$ = new NullLiteral (lexer.Location); }
3039 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
3040 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
3041 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
3046 object v = lexer.Value;
3049 $$ = new IntLiteral ((int) v, lexer.Location);
3050 } else if (v is uint)
3051 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
3053 $$ = new LongLiteral ((Int64) v, lexer.Location);
3054 else if (v is ulong)
3055 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
3057 Console.WriteLine ("OOPS. Unexpected result from scanner");
3062 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
3063 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
3068 // Here is the trick, tokenizer may think that parens is a special but
3069 // parser is interested in open parens only, so we merge them.
3070 // Consider: if (a)foo ();
3075 | OPEN_PARENS_LAMBDA
3078 parenthesized_expression
3079 : OPEN_PARENS expression CLOSE_PARENS
3081 $$ = new ParenthesizedExpression ((Expression) $2);
3083 | OPEN_PARENS expression COMPLETE_COMPLETION
3085 $$ = new ParenthesizedExpression ((Expression) $2);
3090 : primary_expression DOT IDENTIFIER opt_type_argument_list
3092 LocatedToken lt = (LocatedToken) $3;
3093 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3095 | predefined_type DOT IDENTIFIER opt_type_argument_list
3097 LocatedToken lt = (LocatedToken) $3;
3098 // TODO: Location is wrong as some predefined types doesn't hold a location
3099 $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3101 | qualified_alias_member IDENTIFIER opt_type_argument_list
3103 LocatedToken lt1 = (LocatedToken) $1;
3104 LocatedToken lt2 = (LocatedToken) $2;
3106 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
3108 | primary_expression DOT GENERATE_COMPLETION {
3109 $$ = new CompletionMemberAccess ((Expression) $1, null,GetLocation ($3));
3111 | primary_expression DOT IDENTIFIER GENERATE_COMPLETION {
3112 LocatedToken lt = (LocatedToken) $3;
3113 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3115 | predefined_type DOT GENERATE_COMPLETION
3117 // TODO: Location is wrong as some predefined types doesn't hold a location
3118 $$ = new CompletionMemberAccess ((Expression) $1, null, lexer.Location);
3120 | predefined_type DOT IDENTIFIER GENERATE_COMPLETION {
3121 LocatedToken lt = (LocatedToken) $3;
3122 $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3126 invocation_expression
3127 : primary_expression open_parens_any opt_argument_list CLOSE_PARENS
3129 $$ = new Invocation ((Expression) $1, (Arguments) $3);
3133 opt_object_or_collection_initializer
3134 : /* empty */ { $$ = null; }
3135 | object_or_collection_initializer
3138 object_or_collection_initializer
3139 : OPEN_BRACE opt_member_initializer_list close_brace_or_complete_completion
3142 $$ = CollectionOrObjectInitializers.Empty;
3144 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3146 | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3148 $$ = new CollectionOrObjectInitializers ((ArrayList) $2, GetLocation ($1));
3152 opt_member_initializer_list
3153 : /* empty */ { $$ = null; }
3154 | member_initializer_list
3160 member_initializer_list
3161 : member_initializer
3163 ArrayList a = new ArrayList ();
3167 | member_initializer_list COMMA member_initializer
3169 ArrayList a = (ArrayList)$1;
3176 : IDENTIFIER ASSIGN initializer_value
3178 LocatedToken lt = $1 as LocatedToken;
3179 $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3181 | GENERATE_COMPLETION
3183 $$ = new CompletionElementInitializer (null, GetLocation ($1));
3185 | non_assignment_expression opt_COMPLETE_COMPLETION {
3186 CompletionSimpleName csn = $1 as CompletionSimpleName;
3188 $$ = new CollectionElementInitializer ((Expression)$1);
3190 $$ = new CompletionElementInitializer (csn.Prefix, csn.Location);
3192 | OPEN_BRACE expression_list CLOSE_BRACE
3194 $$ = new CollectionElementInitializer ((ArrayList)$2, GetLocation ($1));
3196 | OPEN_BRACE CLOSE_BRACE
3198 Report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3204 | object_or_collection_initializer
3208 : /* empty */ { $$ = null; }
3213 : argument_or_named_argument
3215 Arguments list = new Arguments (4);
3216 list.Add ((Argument) $1);
3219 | argument_list COMMA argument
3221 Arguments list = (Arguments) $1;
3222 if (list [list.Count - 1] is NamedArgument)
3223 Error_NamedArgumentExpected ((NamedArgument) list [list.Count - 1]);
3225 list.Add ((Argument) $3);
3228 | argument_list COMMA named_argument
3230 Arguments list = (Arguments) $1;
3231 NamedArgument a = (NamedArgument) $3;
3232 for (int i = 0; i < list.Count; ++i) {
3233 NamedArgument na = list [i] as NamedArgument;
3234 if (na != null && na.Name.Value == a.Name.Value)
3235 Report.Error (1740, na.Name.Location, "Named argument `{0}' specified multiple times",
3242 | argument_list COMMA
3244 Report.Error (839, GetLocation ($2), "An argument is missing");
3247 | COMMA argument_or_named_argument
3249 Report.Error (839, GetLocation ($1), "An argument is missing");
3257 $$ = new Argument ((Expression) $1);
3259 | non_simple_argument
3262 argument_or_named_argument
3268 : REF variable_reference
3270 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3272 | OUT variable_reference
3274 $$ = new Argument ((Expression) $2, Argument.AType.Out);
3276 | ARGLIST open_parens_any argument_list CLOSE_PARENS
3278 $$ = new Argument (new Arglist ((Arguments) $3, (Location) $1));
3280 | ARGLIST open_parens_any CLOSE_PARENS
3282 $$ = new Argument (new Arglist ((Location) $1));
3286 $$ = new Argument (new ArglistAccess ((Location) $1));
3295 : primary_expression_no_array_creation OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3297 $$ = new ElementAccess ((Expression) $1, (Arguments) $3);
3299 | array_creation_expression OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3301 // LAMESPEC: Not allowed according to specification
3302 $$ = new ElementAccess ((Expression) $1, (Arguments) $3);
3304 | primary_expression_no_array_creation rank_specifiers
3306 // So the super-trick is that primary_expression
3307 // can only be either a SimpleName or a MemberAccess.
3308 // The MemberAccess case arises when you have a fully qualified type-name like :
3310 // SimpleName is when you have
3313 Expression expr = (Expression) $1;
3314 if (expr is ComposedCast){
3315 $$ = new ComposedCast ((ComposedCast)expr, (string) $2);
3316 } else if (expr is ATypeNameExpression){
3318 // So we extract the string corresponding to the SimpleName
3321 $$ = new ComposedCast ((ATypeNameExpression)expr, (string) $2);
3323 Error_ExpectingTypeName (expr);
3324 $$ = TypeManager.system_object_expr;
3327 current_array_type = (FullNamedExpression)$$;
3334 ArrayList list = new ArrayList (4);
3338 | expression_list COMMA expression
3340 ArrayList list = (ArrayList) $1;
3346 expression_list_arguments
3347 : expression_list_argument
3349 Arguments args = new Arguments (4);
3350 args.Add ((Argument) $1);
3353 | expression_list_arguments COMMA expression_list_argument
3355 Arguments args = (Arguments) $1;
3356 args.Add ((Argument) $3);
3361 expression_list_argument
3364 $$ = new Argument ((Expression) $1);
3372 $$ = new This (current_block, (Location) $1);
3377 : BASE DOT IDENTIFIER opt_type_argument_list
3379 LocatedToken lt = (LocatedToken) $3;
3380 $$ = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3382 | BASE OPEN_BRACKET expression_list_arguments CLOSE_BRACKET
3384 $$ = new BaseIndexerAccess ((Arguments) $3, (Location) $1);
3388 Error_SyntaxError (yyToken);
3389 $$ = new BaseAccess (null, GetLocation ($2));
3393 post_increment_expression
3394 : primary_expression OP_INC
3396 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement, (Expression) $1);
3400 post_decrement_expression
3401 : primary_expression OP_DEC
3403 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement, (Expression) $1);
3407 object_or_delegate_creation_expression
3408 : new_expr_start open_parens_any opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3411 if (RootContext.Version <= LanguageVersion.ISO_2)
3412 Report.FeatureIsNotAvailable (GetLocation ($1), "object initializers");
3414 $$ = new NewInitialize ((Expression) $1, (Arguments) $3, (CollectionOrObjectInitializers) $5, GetLocation ($1));
3417 $$ = new New ((Expression) $1, (Arguments) $3, GetLocation ($1));
3419 | new_expr_start object_or_collection_initializer
3421 if (RootContext.Version <= LanguageVersion.ISO_2)
3422 Report.FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
3424 $$ = new NewInitialize ((Expression) $1, null, (CollectionOrObjectInitializers) $2, GetLocation ($1));
3428 array_creation_expression
3429 : new_expr_start OPEN_BRACKET expression_list CLOSE_BRACKET
3430 opt_rank_specifier // shift/reduce on OPEN_BRACE
3431 opt_array_initializer
3433 $$ = new ArrayCreation ((FullNamedExpression) $1, (ArrayList) $3, (string) $5, (ArrayList) $6, GetLocation ($1));
3435 | new_expr_start rank_specifiers opt_array_initializer
3438 Report.Error (1586, GetLocation ($1), "Array creation must have array size or array initializer");
3440 $$ = new ArrayCreation ((FullNamedExpression) $1, (string) $2, (ArrayList) $3, GetLocation ($1));
3442 | NEW rank_specifiers array_initializer
3444 if (RootContext.Version <= LanguageVersion.ISO_2)
3445 Report.FeatureIsNotAvailable (GetLocation ($1), "implicitly typed arrays");
3447 $$ = new ImplicitlyTypedArrayCreation ((string) $2, (ArrayList) $3, GetLocation ($1));
3449 | new_expr_start error
3451 Report.Error (1526, GetLocation ($1), "A new expression requires () or [] after type");
3459 ++lexer.parsing_type;
3463 --lexer.parsing_type;
3468 anonymous_type_expression
3469 : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
3471 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
3472 Report.FeatureIsNotSupported (lexer.Location, "anonymous types");
3473 else if (RootContext.Version <= LanguageVersion.ISO_2)
3474 Report.FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
3476 $$ = new NewAnonymousType ((ArrayList) $3, current_container, GetLocation ($1));
3480 anonymous_type_parameters_opt_comma
3481 : anonymous_type_parameters_opt
3482 | anonymous_type_parameters COMMA
3485 anonymous_type_parameters_opt
3487 | anonymous_type_parameters
3490 anonymous_type_parameters
3491 : anonymous_type_parameter
3493 ArrayList a = new ArrayList (4);
3497 | anonymous_type_parameters COMMA anonymous_type_parameter
3499 ArrayList a = (ArrayList) $1;
3505 anonymous_type_parameter
3506 : IDENTIFIER ASSIGN variable_initializer
3508 LocatedToken lt = (LocatedToken)$1;
3509 $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
3513 LocatedToken lt = (LocatedToken)$1;
3514 $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
3515 lt.Value, lt.Location);
3517 | BASE DOT IDENTIFIER opt_type_argument_list
3519 LocatedToken lt = (LocatedToken) $3;
3520 BaseAccess ba = new BaseAccess (lt.Value, (TypeArguments) $4, lt.Location);
3521 $$ = new AnonymousTypeParameter (ba, lt.Value, lt.Location);
3525 MemberAccess ma = (MemberAccess) $1;
3526 $$ = new AnonymousTypeParameter (ma, ma.Name, ma.Location);
3530 Report.Error (746, lexer.Location, "Invalid anonymous type member declarator. " +
3531 "Anonymous type members must be a member assignment, simple name or member access expression");
3546 opt_rank_specifier_or_nullable
3554 | opt_nullable rank_specifiers
3557 $$ = "?" + (string) $2;
3565 | rank_specifier rank_specifiers
3567 $$ = (string) $2 + (string) $1;
3572 : OPEN_BRACKET CLOSE_BRACKET
3576 | OPEN_BRACKET dim_separators CLOSE_BRACKET
3578 $$ = "[" + (string) $2 + "]";
3580 | OPEN_BRACKET error CLOSE_BRACKET
3582 Report.Error (178, GetLocation ($1), "Invalid rank specifier: expected `,' or `]'");
3592 | dim_separators COMMA
3594 $$ = (string) $1 + ",";
3598 opt_array_initializer
3610 : OPEN_BRACE CLOSE_BRACE
3612 ArrayList list = new ArrayList (4);
3615 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3617 $$ = (ArrayList) $2;
3621 variable_initializer_list
3622 : variable_initializer
3624 ArrayList list = new ArrayList (4);
3628 | variable_initializer_list COMMA variable_initializer
3630 ArrayList list = (ArrayList) $1;
3636 Error_SyntaxError (yyToken);
3637 $$ = new ArrayList ();
3644 pushed_current_array_type = current_array_type;
3645 lexer.TypeOfParsing = true;
3647 open_parens_any typeof_type_expression CLOSE_PARENS
3649 lexer.TypeOfParsing = false;
3650 Expression type = (Expression)$4;
3651 if (type == TypeManager.system_void_expr)
3652 $$ = new TypeOfVoid ((Location) $1);
3654 $$ = new TypeOf (type, (Location) $1);
3655 current_array_type = pushed_current_array_type;
3659 typeof_type_expression
3664 Error_TypeExpected (lexer.Location);
3670 : IDENTIFIER generic_dimension
3672 LocatedToken lt = (LocatedToken) $1;
3674 $$ = new SimpleName (MemberName.MakeName (lt.Value, (int)$2), lt.Location);
3676 | qualified_alias_member IDENTIFIER generic_dimension
3678 LocatedToken lt1 = (LocatedToken) $1;
3679 LocatedToken lt2 = (LocatedToken) $2;
3681 $$ = new QualifiedAliasMember (lt1.Value, MemberName.MakeName (lt2.Value, (int) $3), lt1.Location);
3683 | unbound_type_name DOT IDENTIFIER
3685 LocatedToken lt = (LocatedToken) $3;
3687 $$ = new MemberAccess ((Expression) $1, lt.Value, lt.Location);
3689 | unbound_type_name DOT IDENTIFIER generic_dimension
3691 LocatedToken lt = (LocatedToken) $3;
3693 $$ = new MemberAccess ((Expression) $1, MemberName.MakeName (lt.Value, (int) $4), lt.Location);
3695 | namespace_or_type_name DOT IDENTIFIER generic_dimension
3697 LocatedToken lt = (LocatedToken) $3;
3698 MemberName name = (MemberName) $1;
3700 $$ = new MemberAccess (name.GetTypeExpression (), MemberName.MakeName (lt.Value, (int) $4), lt.Location);
3707 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
3708 Report.FeatureIsNotSupported (lexer.Location, "generics");
3709 else if (RootContext.Version < LanguageVersion.ISO_2)
3710 Report.FeatureIsNotAvailable (lexer.Location, "generics");
3716 qualified_alias_member
3717 : IDENTIFIER DOUBLE_COLON
3719 LocatedToken lt = (LocatedToken) $1;
3720 if (RootContext.Version == LanguageVersion.ISO_1)
3721 Report.FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
3728 : SIZEOF open_parens_any type CLOSE_PARENS {
3729 $$ = new SizeOf ((Expression) $3, (Location) $1);
3734 : CHECKED open_parens_any expression CLOSE_PARENS
3736 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3740 unchecked_expression
3741 : UNCHECKED open_parens_any expression CLOSE_PARENS
3743 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3747 pointer_member_access
3748 : primary_expression OP_PTR IDENTIFIER
3751 LocatedToken lt = (LocatedToken) $3;
3753 deref = new Indirection ((Expression) $1, lt.Location);
3754 $$ = new MemberAccess (deref, lt.Value);
3758 anonymous_method_expression
3759 : DELEGATE opt_anonymous_method_signature
3761 start_anonymous (false, (ParametersCompiled) $2, (Location) $1);
3765 $$ = end_anonymous ((ToplevelBlock) $4);
3769 opt_anonymous_method_signature
3772 $$ = ParametersCompiled.Undefined;
3774 | anonymous_method_signature
3777 anonymous_method_signature
3780 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
3782 opt_formal_parameter_list CLOSE_PARENS
3784 valid_param_mod = 0;
3789 default_value_expression
3790 : DEFAULT open_parens_any type CLOSE_PARENS
3792 if (RootContext.Version < LanguageVersion.ISO_2)
3793 Report.FeatureIsNotAvailable (lexer.Location, "default value expression");
3795 $$ = new DefaultValueExpression ((Expression) $3, GetLocation ($1));
3800 : primary_expression
3801 | BANG prefixed_unary_expression
3803 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2);
3805 | TILDE prefixed_unary_expression
3807 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2);
3813 : OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression
3815 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
3817 | OPEN_PARENS predefined_type CLOSE_PARENS prefixed_unary_expression
3819 $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
3824 // The idea to split this out is from Rhys' grammar
3825 // to solve the problem with casts.
3827 prefixed_unary_expression
3829 | PLUS prefixed_unary_expression
3831 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2);
3833 | MINUS prefixed_unary_expression
3835 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2);
3837 | OP_INC prefixed_unary_expression
3839 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, (Expression) $2);
3841 | OP_DEC prefixed_unary_expression
3843 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, (Expression) $2);
3845 | STAR prefixed_unary_expression
3847 $$ = new Indirection ((Expression) $2, GetLocation ($1));
3849 | BITWISE_AND prefixed_unary_expression
3851 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2);
3855 multiplicative_expression
3856 : prefixed_unary_expression
3857 | multiplicative_expression STAR prefixed_unary_expression
3859 $$ = new Binary (Binary.Operator.Multiply,
3860 (Expression) $1, (Expression) $3);
3862 | multiplicative_expression DIV prefixed_unary_expression
3864 $$ = new Binary (Binary.Operator.Division,
3865 (Expression) $1, (Expression) $3);
3867 | multiplicative_expression PERCENT prefixed_unary_expression
3869 $$ = new Binary (Binary.Operator.Modulus,
3870 (Expression) $1, (Expression) $3);
3875 : multiplicative_expression
3876 | additive_expression PLUS multiplicative_expression
3878 $$ = new Binary (Binary.Operator.Addition,
3879 (Expression) $1, (Expression) $3);
3881 | additive_expression MINUS multiplicative_expression
3883 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3885 | parenthesized_expression MINUS multiplicative_expression
3887 // Shift/Reduce conflict
3888 $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3890 | additive_expression AS type
3892 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3894 | additive_expression IS type
3896 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3901 : additive_expression
3902 | shift_expression OP_SHIFT_LEFT additive_expression
3904 $$ = new Binary (Binary.Operator.LeftShift,
3905 (Expression) $1, (Expression) $3);
3907 | shift_expression OP_SHIFT_RIGHT additive_expression
3909 $$ = new Binary (Binary.Operator.RightShift,
3910 (Expression) $1, (Expression) $3);
3914 relational_expression
3916 | relational_expression OP_LT shift_expression
3918 $$ = new Binary (Binary.Operator.LessThan,
3919 (Expression) $1, (Expression) $3);
3921 | relational_expression OP_GT shift_expression
3923 $$ = new Binary (Binary.Operator.GreaterThan,
3924 (Expression) $1, (Expression) $3);
3926 | relational_expression OP_LE shift_expression
3928 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3929 (Expression) $1, (Expression) $3);
3931 | relational_expression OP_GE shift_expression
3933 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3934 (Expression) $1, (Expression) $3);
3939 : relational_expression
3940 | equality_expression OP_EQ relational_expression
3942 $$ = new Binary (Binary.Operator.Equality,
3943 (Expression) $1, (Expression) $3);
3945 | equality_expression OP_NE relational_expression
3947 $$ = new Binary (Binary.Operator.Inequality,
3948 (Expression) $1, (Expression) $3);
3953 : equality_expression
3954 | and_expression BITWISE_AND equality_expression
3956 $$ = new Binary (Binary.Operator.BitwiseAnd,
3957 (Expression) $1, (Expression) $3);
3961 exclusive_or_expression
3963 | exclusive_or_expression CARRET and_expression
3965 $$ = new Binary (Binary.Operator.ExclusiveOr,
3966 (Expression) $1, (Expression) $3);
3970 inclusive_or_expression
3971 : exclusive_or_expression
3972 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3974 $$ = new Binary (Binary.Operator.BitwiseOr,
3975 (Expression) $1, (Expression) $3);
3979 conditional_and_expression
3980 : inclusive_or_expression
3981 | conditional_and_expression OP_AND inclusive_or_expression
3983 $$ = new Binary (Binary.Operator.LogicalAnd,
3984 (Expression) $1, (Expression) $3);
3988 conditional_or_expression
3989 : conditional_and_expression
3990 | conditional_or_expression OP_OR conditional_and_expression
3992 $$ = new Binary (Binary.Operator.LogicalOr,
3993 (Expression) $1, (Expression) $3);
3997 null_coalescing_expression
3998 : conditional_or_expression
3999 | conditional_or_expression OP_COALESCING null_coalescing_expression
4001 if (RootContext.Version < LanguageVersion.ISO_2)
4002 Report.FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
4004 $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3, lexer.Location);
4008 conditional_expression
4009 : null_coalescing_expression
4010 | null_coalescing_expression INTERR expression COLON expression
4012 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
4016 assignment_expression
4017 : prefixed_unary_expression ASSIGN expression
4019 $$ = new SimpleAssign ((Expression) $1, (Expression) $3);
4021 | prefixed_unary_expression OP_MULT_ASSIGN expression
4023 $$ = new CompoundAssign (
4024 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4026 | prefixed_unary_expression OP_DIV_ASSIGN expression
4028 $$ = new CompoundAssign (
4029 Binary.Operator.Division, (Expression) $1, (Expression) $3);
4031 | prefixed_unary_expression OP_MOD_ASSIGN expression
4033 $$ = new CompoundAssign (
4034 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4036 | prefixed_unary_expression OP_ADD_ASSIGN expression
4038 $$ = new CompoundAssign (
4039 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4041 | prefixed_unary_expression OP_SUB_ASSIGN expression
4043 $$ = new CompoundAssign (
4044 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4046 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
4048 $$ = new CompoundAssign (
4049 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4051 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
4053 $$ = new CompoundAssign (
4054 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4056 | prefixed_unary_expression OP_AND_ASSIGN expression
4058 $$ = new CompoundAssign (
4059 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4061 | prefixed_unary_expression OP_OR_ASSIGN expression
4063 $$ = new CompoundAssign (
4064 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4066 | prefixed_unary_expression OP_XOR_ASSIGN expression
4068 $$ = new CompoundAssign (
4069 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4073 lambda_parameter_list
4076 ArrayList pars = new ArrayList (4);
4081 | lambda_parameter_list COMMA lambda_parameter
4083 ArrayList pars = (ArrayList) $1;
4084 Parameter p = (Parameter)$3;
4085 if (pars[0].GetType () != p.GetType ()) {
4086 Report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
4095 : parameter_modifier parameter_type IDENTIFIER
4097 LocatedToken lt = (LocatedToken) $3;
4099 $$ = new Parameter ((FullNamedExpression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
4101 | parameter_type IDENTIFIER
4103 LocatedToken lt = (LocatedToken) $2;
4105 $$ = new Parameter ((FullNamedExpression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
4109 LocatedToken lt = (LocatedToken) $1;
4110 $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4114 opt_lambda_parameter_list
4115 : /* empty */ { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
4116 | lambda_parameter_list {
4117 ArrayList pars_list = (ArrayList) $1;
4118 $$ = new ParametersCompiled ((Parameter[])pars_list.ToArray (typeof (Parameter)));
4122 lambda_expression_body
4124 start_block (lexer.Location);
4128 Block b = end_block (lexer.Location);
4129 b.AddStatement (new ContextualReturn ((Expression) $2));
4140 LocatedToken lt = (LocatedToken) $1;
4141 Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
4142 start_anonymous (true, new ParametersCompiled (p), GetLocation ($1));
4144 lambda_expression_body
4146 $$ = end_anonymous ((ToplevelBlock) $4);
4148 | OPEN_PARENS_LAMBDA
4150 valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
4152 opt_lambda_parameter_list CLOSE_PARENS ARROW
4154 valid_param_mod = 0;
4155 start_anonymous (true, (ParametersCompiled) $3, GetLocation ($1));
4157 lambda_expression_body
4159 $$ = end_anonymous ((ToplevelBlock) $7);
4164 : assignment_expression
4165 | non_assignment_expression
4168 non_assignment_expression
4169 : conditional_expression
4191 lexer.ConstraintsParsing = true;
4193 type_declaration_name
4195 MemberName name = MakeName ((MemberName) $6);
4196 push_current_class (new Class (current_namespace, current_class, name, (int) $2, (Attributes) $1), $3);
4199 opt_type_parameter_constraints_clauses
4201 lexer.ConstraintsParsing = false;
4203 current_class.SetParameterInfo ((ArrayList) $9);
4205 if (RootContext.Documentation != null) {
4206 current_container.DocComment = Lexer.consume_doc_comment ();
4207 Lexer.doc_state = XmlCommentState.Allowed;
4212 --lexer.parsing_declaration;
4213 if (RootContext.Documentation != null)
4214 Lexer.doc_state = XmlCommentState.Allowed;
4218 $$ = pop_current_class ();
4226 { $$ = $1; } // location
4230 : /* empty */ { $$ = (int) 0; }
4236 | modifiers modifier
4241 if ((m1 & m2) != 0) {
4242 Location l = lexer.Location;
4243 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
4245 $$ = (int) (m1 | m2);
4253 if (current_container == RootContext.ToplevelTypes)
4254 Report.Error (1530, lexer.Location, "Keyword `new' is not allowed on namespace elements");
4256 | PUBLIC { $$ = Modifiers.PUBLIC; }
4257 | PROTECTED { $$ = Modifiers.PROTECTED; }
4258 | INTERNAL { $$ = Modifiers.INTERNAL; }
4259 | PRIVATE { $$ = Modifiers.PRIVATE; }
4260 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
4261 | SEALED { $$ = Modifiers.SEALED; }
4262 | STATIC { $$ = Modifiers.STATIC; }
4263 | READONLY { $$ = Modifiers.READONLY; }
4264 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
4265 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
4266 | EXTERN { $$ = Modifiers.EXTERN; }
4267 | VOLATILE { $$ = Modifiers.VOLATILE; }
4268 | UNSAFE { $$ = Modifiers.UNSAFE; }
4277 : COLON type_list { current_container.AddBasesForPart (current_class, (ArrayList) $2); }
4280 opt_type_parameter_constraints_clauses
4281 : /* empty */ { $$ = null; }
4282 | type_parameter_constraints_clauses
4286 type_parameter_constraints_clauses
4287 : type_parameter_constraints_clause {
4288 ArrayList constraints = new ArrayList (1);
4289 constraints.Add ($1);
4292 | type_parameter_constraints_clauses type_parameter_constraints_clause {
4293 ArrayList constraints = (ArrayList) $1;
4294 Constraints new_constraint = (Constraints)$2;
4296 foreach (Constraints c in constraints) {
4297 if (new_constraint.TypeParameter == c.TypeParameter) {
4298 Report.Error (409, new_constraint.Location, "A constraint clause has already been specified for type parameter `{0}'",
4299 new_constraint.TypeParameter);
4303 constraints.Add (new_constraint);
4308 type_parameter_constraints_clause
4309 : WHERE IDENTIFIER COLON type_parameter_constraints {
4310 LocatedToken lt = (LocatedToken) $2;
4311 $$ = new Constraints (lt.Value, (ArrayList) $4, lt.Location);
4315 type_parameter_constraints
4316 : type_parameter_constraint {
4317 ArrayList constraints = new ArrayList (1);
4318 constraints.Add ($1);
4321 | type_parameter_constraints COMMA type_parameter_constraint {
4322 ArrayList constraints = (ArrayList) $1;
4324 constraints.Add ($3);
4329 type_parameter_constraint
4331 | NEW OPEN_PARENS CLOSE_PARENS {
4332 $$ = SpecialConstraint.Constructor;
4335 $$ = SpecialConstraint.ReferenceType;
4338 $$ = SpecialConstraint.ValueType;
4342 opt_type_parameter_variance
4347 | type_parameter_variance
4349 if (RootContext.MetadataCompatibilityVersion < MetadataVersion.v2)
4350 Report.FeatureIsNotSupported (lexer.Location, "generic type variance");
4351 else if (RootContext.Version <= LanguageVersion.V_3)
4352 Report.FeatureIsNotAvailable (lexer.Location, "generic type variance");
4358 type_parameter_variance
4361 $$ = Variance.Covariant;
4365 $$ = Variance.Contravariant;
4374 // A block is "contained" on the following places:
4376 // property_declaration as part of the accessor body (get/set)
4377 // operator_declaration
4378 // constructor_declaration
4379 // destructor_declaration
4380 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
4385 ++lexer.parsing_block;
4386 start_block ((Location) $1);
4388 opt_statement_list block_end
4397 --lexer.parsing_block;
4398 $$ = end_block ((Location) $1);
4400 | COMPLETE_COMPLETION
4402 --lexer.parsing_block;
4403 $$ = end_block (lexer.Location);
4411 ++lexer.parsing_block;
4412 current_block.StartLocation = GetLocation ($1);
4414 opt_statement_list CLOSE_BRACE
4416 --lexer.parsing_block;
4417 $$ = end_block ((Location) $4);
4428 | statement_list statement
4432 : declaration_statement
4434 if ($1 != null && (Block) $1 != current_block){
4435 current_block.AddStatement ((Statement) $1);
4436 current_block = (Block) $1;
4439 | valid_declaration_statement
4441 current_block.AddStatement ((Statement) $1);
4447 // The interactive_statement and its derivatives are only
4448 // used to provide a special version of `expression_statement'
4449 // that has a side effect of assigning the expression to
4452 interactive_statement_list
4453 : interactive_statement
4454 | interactive_statement_list interactive_statement
4457 interactive_statement
4458 : declaration_statement
4460 if ($1 != null && (Block) $1 != current_block){
4461 current_block.AddStatement ((Statement) $1);
4462 current_block = (Block) $1;
4465 | interactive_valid_declaration_statement
4467 current_block.AddStatement ((Statement) $1);
4472 valid_declaration_statement
4475 | expression_statement
4476 | selection_statement
4477 | iteration_statement
4481 | unchecked_statement
4488 interactive_valid_declaration_statement
4491 | interactive_expression_statement
4492 | selection_statement
4493 | iteration_statement
4497 | unchecked_statement
4505 : valid_declaration_statement
4506 | declaration_statement
4508 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4513 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
4521 $$ = EmptyStatement.Value;
4528 LocatedToken lt = (LocatedToken) $1;
4529 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
4531 if (current_block.AddLabel (labeled))
4532 current_block.AddStatement (labeled);
4537 declaration_statement
4538 : local_variable_declaration SEMICOLON
4540 current_array_type = null;
4542 DictionaryEntry de = (DictionaryEntry) $1;
4543 Expression e = (Expression) de.Key;
4545 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
4549 | local_constant_declaration SEMICOLON
4551 current_array_type = null;
4553 DictionaryEntry de = (DictionaryEntry) $1;
4555 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
4561 * The following is from Rhys' grammar:
4562 * > Types in local variable declarations must be recognized as
4563 * > expressions to prevent reduce/reduce errors in the grammar.
4564 * > The expressions are converted into types during semantic analysis.
4567 : primary_expression_no_array_creation opt_rank_specifier_or_nullable
4569 // FIXME: Do something smart here regarding the composition of the type.
4571 // Ok, the above "primary_expression" is there to get rid of
4572 // both reduce/reduce and shift/reduces in the grammar, it should
4573 // really just be "type_name". If you use type_name, a reduce/reduce
4574 // creeps up. If you use namespace_or_type_name (which is all we need
4575 // really) two shift/reduces appear.
4578 // So the super-trick is that primary_expression
4579 // can only be either a SimpleName or a MemberAccess.
4580 // The MemberAccess case arises when you have a fully qualified type-name like :
4582 // SimpleName is when you have
4585 Expression expr = (Expression) $1;
4586 string rank_or_nullable = (string) $2;
4588 if (expr is ComposedCast){
4589 $$ = new ComposedCast ((ComposedCast)expr, rank_or_nullable);
4590 } else if (expr is ATypeNameExpression){
4592 // So we extract the string corresponding to the SimpleName
4595 if (rank_or_nullable.Length == 0) {
4596 SimpleName sn = expr as SimpleName;
4597 if (sn != null && sn.Name == "var")
4598 $$ = current_array_type = new VarExpr (sn.Location);
4602 $$ = new ComposedCast ((ATypeNameExpression)expr, rank_or_nullable);
4605 Error_ExpectingTypeName (expr);
4606 $$ = TypeManager.system_object_expr;
4609 | builtin_types opt_rank_specifier_or_nullable
4611 if ((string) $2 == "")
4614 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, (string) $2, lexer.Location);
4616 | VOID opt_rank_specifier
4618 Expression.Error_VoidInvalidInTheContext (lexer.Location, Report);
4619 $$ = TypeManager.system_void_expr;
4623 local_variable_pointer_type
4624 : primary_expression_no_array_creation STAR
4626 ATypeNameExpression expr = $1 as ATypeNameExpression;
4629 $$ = new ComposedCast (expr, "*");
4631 Error_ExpectingTypeName ((Expression)$1);
4635 | builtin_types STAR
4637 $$ = new ComposedCast ((FullNamedExpression) $1, "*", GetLocation ($1));
4641 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
4643 | local_variable_pointer_type STAR
4645 $$ = new ComposedCast ((FullNamedExpression) $1, "*");
4651 | local_variable_pointer_type opt_rank_specifier
4654 string rank = (string)$2;
4659 $$ = current_array_type = new ComposedCast ((FullNamedExpression) $1, rank);
4666 local_variable_declaration
4667 : local_variable_type local_variable_declarators
4670 VarExpr ve = $1 as VarExpr;
4672 ve.VariableInitializer = (ArrayList)$2;
4674 $$ = new DictionaryEntry ($1, $2);
4680 local_constant_declaration
4681 : CONST variable_type constant_declarators
4684 $$ = new DictionaryEntry ($2, $3);
4690 expression_statement
4691 : statement_expression SEMICOLON { $$ = $1; }
4692 | statement_expression COMPLETE_COMPLETION { $$ = $1; }
4695 interactive_expression_statement
4696 : interactive_statement_expression SEMICOLON { $$ = $1; }
4697 | interactive_statement_expression COMPLETE_COMPLETION { $$ = $1; }
4701 // We have to do the wrapping here and not in the case above,
4702 // because statement_expression is used for example in for_statement
4704 statement_expression
4707 ExpressionStatement s = $1 as ExpressionStatement;
4709 Expression.Error_InvalidExpressionStatement (Report, GetLocation ($1));
4710 s = EmptyExpressionStatement.Instance;
4713 $$ = new StatementExpression (s);
4717 Error_SyntaxError (yyToken);
4718 $$ = EmptyStatement.Value;
4722 interactive_statement_expression
4725 Expression expr = (Expression) $1;
4726 ExpressionStatement s;
4728 s = new OptionalAssign (new SimpleName ("$retval", lexer.Location), expr, lexer.Location);
4729 $$ = new StatementExpression (s);
4733 Error_SyntaxError (yyToken);
4744 : IF open_parens_any boolean_expression CLOSE_PARENS
4747 Location l = (Location) $1;
4749 $$ = new If ((Expression) $3, (Statement) $5, l);
4751 // FIXME: location for warning should be loc property of $5.
4752 if ($5 == EmptyStatement.Value)
4753 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4756 | IF open_parens_any boolean_expression CLOSE_PARENS
4757 embedded_statement ELSE embedded_statement
4759 Location l = (Location) $1;
4761 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
4763 // FIXME: location for warning should be loc property of $5 and $7.
4764 if ($5 == EmptyStatement.Value)
4765 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4766 if ($7 == EmptyStatement.Value)
4767 Report.Warning (642, 3, l, "Possible mistaken empty statement");
4772 : SWITCH open_parens_any
4774 if (switch_stack == null)
4775 switch_stack = new Stack (2);
4776 switch_stack.Push (current_block);
4778 expression CLOSE_PARENS
4781 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
4782 current_block = (Block) switch_stack.Pop ();
4798 Report.Warning (1522, 1, lexer.Location, "Empty switch block");
4799 $$ = new ArrayList ();
4807 ArrayList sections = new ArrayList (4);
4812 | switch_sections switch_section
4814 ArrayList sections = (ArrayList) $1;
4824 current_block = current_block.CreateSwitchBlock (lexer.Location);
4828 $$ = new SwitchSection ((ArrayList) $1, current_block.Explicit);
4835 ArrayList labels = new ArrayList (4);
4840 | switch_labels switch_label
4842 ArrayList labels = (ArrayList) ($1);
4850 : CASE constant_expression COLON
4852 $$ = new SwitchLabel ((Expression) $2, (Location) $1);
4856 $$ = new SwitchLabel (null, (Location) $1);
4868 : WHILE open_parens_any boolean_expression CLOSE_PARENS embedded_statement
4870 Location l = (Location) $1;
4871 $$ = new While ((Expression) $3, (Statement) $5, l);
4876 : DO embedded_statement
4877 WHILE open_parens_any boolean_expression CLOSE_PARENS SEMICOLON
4879 Location l = (Location) $1;
4881 $$ = new Do ((Statement) $2, (Expression) $5, l);
4886 : FOR open_parens_any opt_for_initializer SEMICOLON
4888 Location l = lexer.Location;
4890 Block assign_block = current_block;
4892 if ($3 is DictionaryEntry){
4893 DictionaryEntry de = (DictionaryEntry) $3;
4895 Expression type = (Expression) de.Key;
4896 ArrayList var_declarators = (ArrayList) de.Value;
4898 foreach (VariableDeclaration decl in var_declarators){
4902 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4906 Expression expr = decl.expression_or_array_initializer;
4908 LocalVariableReference var;
4909 var = new LocalVariableReference (assign_block, decl.identifier, l);
4912 Assign a = new SimpleAssign (var, expr, decl.Location);
4914 assign_block.AddStatement (new StatementExpression (a));
4918 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4919 // This can be referred to as $5 below.
4925 opt_for_condition SEMICOLON
4926 opt_for_iterator CLOSE_PARENS
4929 Location l = (Location) $1;
4931 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4933 current_block.AddStatement (f);
4935 $$ = end_block (lexer.Location);
4940 : /* empty */ { $$ = EmptyStatement.Value; }
4945 : local_variable_declaration
4946 | statement_expression_list
4950 : /* empty */ { $$ = null; }
4951 | boolean_expression
4955 : /* empty */ { $$ = EmptyStatement.Value; }
4960 : statement_expression_list
4963 statement_expression_list
4964 : statement_expression
4966 // CHANGE: was `null'
4967 Statement s = (Statement) $1;
4968 Block b = new Block (current_block, s.loc, lexer.Location);
4973 | statement_expression_list COMMA statement_expression
4975 Block b = (Block) $1;
4977 b.AddStatement ((Statement) $3);
4983 : FOREACH open_parens_any type IN expression CLOSE_PARENS
4985 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4988 | FOREACH open_parens_any type IDENTIFIER IN
4989 expression CLOSE_PARENS
4991 start_block (lexer.Location);
4992 Block foreach_block = current_block;
4994 LocatedToken lt = (LocatedToken) $4;
4995 Location l = lt.Location;
4996 LocalInfo vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4998 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
5000 // Get a writable reference to this read-only variable.
5002 // Note that the $$ here refers to the value of _this_ code block,
5003 // not the value of the LHS non-terminal. This can be referred to as $8 below.
5004 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
5011 LocalVariableReference v = (LocalVariableReference) $8;
5012 Location l = (Location) $1;
5015 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
5016 current_block.AddStatement (f);
5019 $$ = end_block (lexer.Location);
5025 | continue_statement
5035 $$ = new Break ((Location) $1);
5040 : CONTINUE SEMICOLON
5042 $$ = new Continue ((Location) $1);
5047 : GOTO IDENTIFIER SEMICOLON
5049 LocatedToken lt = (LocatedToken) $2;
5050 $$ = new Goto (lt.Value, lt.Location);
5052 | GOTO CASE constant_expression SEMICOLON
5054 $$ = new GotoCase ((Expression) $3, (Location) $1);
5056 | GOTO DEFAULT SEMICOLON
5058 $$ = new GotoDefault ((Location) $1);
5063 : RETURN opt_expression SEMICOLON
5065 $$ = new Return ((Expression) $2, (Location) $1);
5070 : THROW opt_expression SEMICOLON
5072 $$ = new Throw ((Expression) $2, (Location) $1);
5077 : IDENTIFIER RETURN expression SEMICOLON
5079 LocatedToken lt = (LocatedToken) $1;
5080 string s = lt.Value;
5082 Report.Error (1003, lt.Location, "; expected");
5085 if (RootContext.Version == LanguageVersion.ISO_1){
5086 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
5089 current_block.Toplevel.IsIterator = true;
5090 $$ = new Yield ((Expression) $3, lt.Location);
5092 | IDENTIFIER RETURN SEMICOLON
5094 Report.Error (1627, (Location) $2, "Expression expected after yield return");
5097 | IDENTIFIER BREAK SEMICOLON
5099 LocatedToken lt = (LocatedToken) $1;
5100 string s = lt.Value;
5102 Report.Error (1003, lt.Location, "; expected");
5105 if (RootContext.Version == LanguageVersion.ISO_1){
5106 Report.FeatureIsNotAvailable (lt.Location, "yield statement");
5110 current_block.Toplevel.IsIterator = true;
5111 $$ = new YieldBreak (lt.Location);
5121 : TRY block catch_clauses
5123 $$ = new TryCatch ((Block) $2, (ArrayList) $3, (Location) $1, false);
5125 | TRY block FINALLY block
5127 $$ = new TryFinally ((Statement) $2, (Block) $4, (Location) $1);
5129 | TRY block catch_clauses FINALLY block
5131 $$ = new TryFinally (new TryCatch ((Block) $2, (ArrayList) $3, (Location) $1, true), (Block) $5, (Location) $1);
5135 Report.Error (1524, (Location) $1, "Expected catch or finally");
5143 ArrayList l = new ArrayList (4);
5148 | catch_clauses catch_clause
5150 ArrayList l = (ArrayList) $1;
5152 Catch c = (Catch) $2;
5153 if (((Catch) l [0]).IsGeneral) {
5154 Report.Error (1017, c.loc, "Try statement already has an empty catch block");
5167 : /* empty */ { $$ = null; }
5172 : CATCH opt_catch_args
5174 Expression type = null;
5177 DictionaryEntry cc = (DictionaryEntry) $2;
5178 type = (Expression) cc.Key;
5179 LocatedToken lt = (LocatedToken) cc.Value;
5182 ArrayList one = new ArrayList (4);
5184 one.Add (new VariableDeclaration (lt, null));
5186 start_block (lexer.Location);
5187 current_block = declare_local_variables (type, one, lt.Location);
5191 Expression type = null;
5193 Block var_block = null;
5196 DictionaryEntry cc = (DictionaryEntry) $2;
5197 type = (Expression) cc.Key;
5198 LocatedToken lt = (LocatedToken) cc.Value;
5202 var_block = end_block (lexer.Location);
5206 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
5211 : /* empty */ { $$ = null; }
5216 : open_parens_any type opt_identifier CLOSE_PARENS
5218 $$ = new DictionaryEntry ($2, $3);
5220 | open_parens_any CLOSE_PARENS
5222 Report.Error (1015, GetLocation ($1), "A type that derives from `System.Exception', `object', or `string' expected");
5229 $$ = new Checked ((Block) $2);
5236 $$ = new Unchecked ((Block) $2);
5243 RootContext.CheckUnsafeOption ((Location) $1, Report);
5245 $$ = new Unsafe ((Block) $3);
5250 : FIXED open_parens_any
5251 type_and_void fixed_pointer_declarators
5254 ArrayList list = (ArrayList) $4;
5255 Expression type = (Expression) $3;
5256 Location l = (Location) $1;
5257 int top = list.Count;
5259 start_block (lexer.Location);
5261 for (int i = 0; i < top; i++){
5262 Pair p = (Pair) list [i];
5265 v = current_block.AddVariable (type, (string) p.First, l);
5269 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
5277 Location l = (Location) $1;
5279 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
5281 current_block.AddStatement (f);
5283 $$ = end_block (lexer.Location);
5287 fixed_pointer_declarators
5288 : fixed_pointer_declarator {
5289 ArrayList declarators = new ArrayList (4);
5291 declarators.Add ($1);
5294 | fixed_pointer_declarators COMMA fixed_pointer_declarator
5296 ArrayList declarators = (ArrayList) $1;
5298 declarators.Add ($3);
5303 fixed_pointer_declarator
5304 : IDENTIFIER ASSIGN expression
5306 LocatedToken lt = (LocatedToken) $1;
5307 // FIXME: keep location
5308 $$ = new Pair (lt.Value, $3);
5312 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
5318 : LOCK open_parens_any expression CLOSE_PARENS
5324 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
5329 : USING open_parens_any local_variable_declaration CLOSE_PARENS
5331 start_block (lexer.Location);
5332 Block assign_block = current_block;
5334 DictionaryEntry de = (DictionaryEntry) $3;
5335 Location l = (Location) $1;
5337 Expression type = (Expression) de.Key;
5338 ArrayList var_declarators = (ArrayList) de.Value;
5340 Stack vars = new Stack ();
5342 foreach (VariableDeclaration decl in var_declarators) {
5343 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
5346 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
5348 Expression expr = decl.expression_or_array_initializer;
5350 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
5353 LocalVariableReference var;
5355 // Get a writable reference to this read-only variable.
5356 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
5358 // This is so that it is not a warning on using variables
5361 vars.Push (new DictionaryEntry (var, expr));
5363 // Assign a = new SimpleAssign (var, expr, decl.Location);
5364 // assign_block.AddStatement (new StatementExpression (a));
5367 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
5368 // It can be referred to as $5 below.
5373 Statement stmt = (Statement) $6;
5374 Stack vars = (Stack) $5;
5375 Location l = (Location) $1;
5377 while (vars.Count > 0) {
5378 DictionaryEntry de = (DictionaryEntry) vars.Pop ();
5379 stmt = new Using ((Expression) de.Key, (Expression) de.Value, stmt, l);
5381 current_block.AddStatement (stmt);
5382 $$ = end_block (lexer.Location);
5384 | USING open_parens_any expression CLOSE_PARENS
5386 start_block (lexer.Location);
5390 current_block.AddStatement (new UsingTemporary ((Expression) $3, (Statement) $6, (Location) $1));
5391 $$ = end_block (lexer.Location);
5399 : first_from_clause query_body
5401 lexer.query_parsing = false;
5403 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5405 from.Tail.Next = (Linq.AQueryClause)$2;
5408 current_block.SetEndLocation (lexer.Location);
5409 current_block = current_block.Parent;
5411 | nested_from_clause query_body
5413 Linq.AQueryClause from = $1 as Linq.AQueryClause;
5415 from.Tail.Next = (Linq.AQueryClause)$2;
5418 current_block.SetEndLocation (lexer.Location);
5419 current_block = current_block.Parent;
5424 : FROM_FIRST IDENTIFIER IN expression
5426 $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
5427 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $2, GetLocation ($1));
5429 | FROM_FIRST type IDENTIFIER IN expression
5431 $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
5432 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $3, GetLocation ($1));
5437 : FROM IDENTIFIER IN expression
5439 $$ = new Linq.QueryExpression (current_block, new Linq.QueryStartClause ((Expression)$4));
5440 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $2, GetLocation ($1));
5442 | FROM type IDENTIFIER IN expression
5444 $$ = new Linq.QueryExpression (current_block, new Linq.Cast ((FullNamedExpression)$2, (Expression)$5));
5445 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $3, GetLocation ($1));
5450 : FROM IDENTIFIER IN
5452 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5456 LocatedToken lt = (LocatedToken) $2;
5457 $$ = new Linq.SelectMany (current_block.Toplevel, lt, (Expression)$5);
5459 current_block.SetEndLocation (lexer.Location);
5460 current_block = current_block.Parent;
5462 ((Linq.QueryBlock)current_block).AddTransparentParameter (lt);
5464 | FROM type IDENTIFIER IN
5466 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5470 LocatedToken lt = (LocatedToken) $3;
5471 FullNamedExpression type = (FullNamedExpression)$2;
5473 $$ = new Linq.SelectMany (current_block.Toplevel, lt, new Linq.Cast (type, (FullNamedExpression)$6));
5475 current_block.SetEndLocation (lexer.Location);
5476 current_block = current_block.Parent;
5478 ((Linq.QueryBlock)current_block).AddTransparentParameter (lt);
5483 : opt_query_body_clauses select_or_group_clause opt_query_continuation
5485 Linq.AQueryClause head = (Linq.AQueryClause)$2;
5488 head.Next = (Linq.AQueryClause)$3;
5491 Linq.AQueryClause clause = (Linq.AQueryClause)$1;
5492 clause.Tail.Next = head;
5500 select_or_group_clause
5503 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5507 $$ = new Linq.Select (current_block.Toplevel, (Expression)$3, GetLocation ($1));
5509 current_block.SetEndLocation (lexer.Location);
5510 current_block = current_block.Parent;
5514 if (linq_clause_blocks == null)
5515 linq_clause_blocks = new Stack ();
5517 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5518 linq_clause_blocks.Push (current_block);
5522 current_block.SetEndLocation (lexer.Location);
5523 current_block = current_block.Parent;
5525 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5529 $$ = new Linq.GroupBy (current_block.Toplevel, (Expression)$3, (ToplevelBlock) linq_clause_blocks.Pop (), (Expression)$6, GetLocation ($1));
5531 current_block.SetEndLocation (lexer.Location);
5532 current_block = current_block.Parent;
5536 opt_query_body_clauses
5538 | query_body_clauses
5543 | query_body_clauses query_body_clause
5545 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
5559 : LET IDENTIFIER ASSIGN
5561 current_block = new Linq.QueryBlock (compiler, current_block, GetLocation ($1));
5565 LocatedToken lt = (LocatedToken) $2;
5566 $$ = new Linq.Let (current_block.Toplevel, current_container, lt, (Expression)$5);
5568 current_block.SetEndLocation (lexer.Location);
5569 current_block = current_block.Parent;
5571 ((Linq.QueryBlock)current_block).AddTransparentParameter (lt);
5578 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5582 $$ = new Linq.Where (current_block.Toplevel, (Expression)$3, GetLocation ($1));
5584 current_block.SetEndLocation (lexer.Location);
5585 current_block = current_block.Parent;
5590 : JOIN IDENTIFIER IN
5592 if (linq_clause_blocks == null)
5593 linq_clause_blocks = new Stack ();
5595 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5596 linq_clause_blocks.Push (current_block);
5600 current_block.SetEndLocation (lexer.Location);
5601 current_block = current_block.Parent;
5603 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5604 linq_clause_blocks.Push (current_block);
5608 current_block.AddStatement (new ContextualReturn ((Expression) $8));
5609 current_block.SetEndLocation (lexer.Location);
5610 current_block = current_block.Parent;
5612 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $2, lexer.Location);
5614 expression opt_join_into
5616 LocatedToken lt = (LocatedToken) $2;
5618 ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
5619 ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
5622 $$ = new Linq.Join (block, lt, (Expression)$5, outer_selector, current_block.Toplevel, GetLocation ($1));
5624 $$ = new Linq.GroupJoin (block, lt, (Expression)$5, outer_selector, current_block.Toplevel,
5625 (LocatedToken) $12, GetLocation ($1));
5628 current_block.AddStatement (new ContextualReturn ((Expression) $11));
5629 current_block.SetEndLocation (lexer.Location);
5630 current_block = current_block.Parent;
5633 ((Linq.QueryBlock)current_block).AddTransparentParameter (lt);
5635 ((Linq.QueryBlock)current_block).AddTransparentParameter ((LocatedToken) $12);
5637 | JOIN type IDENTIFIER IN
5639 if (linq_clause_blocks == null)
5640 linq_clause_blocks = new Stack ();
5642 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5643 linq_clause_blocks.Push (current_block);
5647 current_block.SetEndLocation (lexer.Location);
5648 current_block = current_block.Parent;
5650 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5651 linq_clause_blocks.Push (current_block);
5655 current_block.AddStatement (new ContextualReturn ((Expression) $9));
5656 current_block.SetEndLocation (lexer.Location);
5657 current_block = current_block.Parent;
5659 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $3, lexer.Location);
5661 expression opt_join_into
5663 LocatedToken lt = (LocatedToken) $3;
5664 ToplevelBlock outer_selector = (ToplevelBlock) linq_clause_blocks.Pop ();
5665 ToplevelBlock block = (ToplevelBlock) linq_clause_blocks.Pop ();
5667 Linq.Cast cast = new Linq.Cast ((FullNamedExpression)$2, (Expression)$6);
5669 $$ = new Linq.Join (block, lt, cast, outer_selector, current_block.Toplevel, GetLocation ($1));
5671 $$ = new Linq.GroupJoin (block, lt, cast, outer_selector, current_block.Toplevel,
5672 (LocatedToken) $13, GetLocation ($1));
5675 current_block.AddStatement (new ContextualReturn ((Expression) $12));
5676 current_block.SetEndLocation (lexer.Location);
5677 current_block = current_block.Parent;
5680 ((Linq.QueryBlock)current_block).AddTransparentParameter (lt);
5682 ((Linq.QueryBlock)current_block).AddTransparentParameter ((LocatedToken) $13);
5697 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5701 current_block.SetEndLocation (lexer.Location);
5702 current_block = current_block.Parent;
5712 current_block.SetEndLocation (lexer.Location);
5713 current_block = current_block.Parent;
5715 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5719 ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$4;
5726 | orderings_then_by COMMA
5728 current_block.SetEndLocation (lexer.Location);
5729 current_block = current_block.Parent;
5731 current_block = new Linq.QueryBlock (compiler, current_block, lexer.Location);
5735 ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$3;
5743 $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);
5745 | expression ASCENDING
5747 $$ = new Linq.OrderByAscending (current_block.Toplevel, (Expression)$1);
5749 | expression DESCENDING
5751 $$ = new Linq.OrderByDescending (current_block.Toplevel, (Expression)$1);
5758 $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1);
5760 | expression ASCENDING
5762 $$ = new Linq.ThenByAscending (current_block.Toplevel, (Expression)$1);
5764 | expression DESCENDING
5766 $$ = new Linq.ThenByDescending (current_block.Toplevel, (Expression)$1);
5771 opt_query_continuation
5775 // query continuation block is not linked with query block but with block
5776 // before. This means each query can use same range variable names for
5777 // different identifiers.
5779 current_block.SetEndLocation (GetLocation ($1));
5780 current_block = current_block.Parent;
5782 current_block = new Linq.QueryBlock (compiler, current_block, (LocatedToken) $2, GetLocation ($1));
5786 $$ = new Linq.QueryExpression (current_block, (Linq.AQueryClause)$4);
5791 // Support for using the compiler as an interactive parser
5793 // The INTERACTIVE_PARSER token is first sent to parse our
5794 // productions; If the result is a Statement, the parsing
5795 // is repeated, this time with INTERACTIVE_PARSE_WITH_BLOCK
5796 // to setup the blocks in advance.
5798 // This setup is here so that in the future we can add
5799 // support for other constructs (type parsing, namespaces, etc)
5800 // that do not require a block to be setup in advance
5804 : EVAL_STATEMENT_PARSER EOF
5805 | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives
5806 | EVAL_STATEMENT_PARSER {
5807 Evaluator.LoadAliases (current_namespace);
5809 push_current_class (new Class (current_namespace, current_class, new MemberName ("Class" + class_count++),
5810 Modifiers.PUBLIC, null), null);
5812 ArrayList baseclass_list = new ArrayList ();
5813 baseclass_list.Add (new TypeExpression (Evaluator.InteractiveBaseClass, lexer.Location));
5814 current_container.AddBasesForPart (current_class, baseclass_list);
5816 // (ref object retval)
5817 Parameter [] mpar = new Parameter [1];
5818 mpar [0] = new Parameter (TypeManager.system_object_expr, "$retval", Parameter.Modifier.REF, null, Location.Null);
5820 ParametersCompiled pars = new ParametersCompiled (mpar);
5821 current_local_parameters = pars;
5822 Method method = new Method (
5825 TypeManager.system_void_expr,
5826 Modifiers.PUBLIC | Modifiers.STATIC,
5827 new MemberName ("Host"),
5829 null /* attributes */);
5831 oob_stack.Push (method);
5832 ++lexer.parsing_block;
5833 start_block (lexer.Location);
5835 interactive_statement_list opt_COMPLETE_COMPLETION
5837 --lexer.parsing_block;
5838 Method method = (Method) oob_stack.Pop ();
5840 method.Block = (ToplevelBlock) end_block(lexer.Location);
5841 current_container.AddMethod (method);
5843 --lexer.parsing_declaration;
5844 InteractiveResult = pop_current_class ();
5845 current_local_parameters = null;
5847 | EVAL_COMPILATION_UNIT_PARSER {
5848 Evaluator.LoadAliases (current_namespace);
5850 interactive_compilation_unit
5853 interactive_compilation_unit
5854 : outer_declarations
5855 | outer_declarations global_attributes
5860 opt_COMPLETE_COMPLETION
5862 | COMPLETE_COMPLETION
5865 close_brace_or_complete_completion
5867 | COMPLETE_COMPLETION
5872 // A class used to pass around variable declarations and constants
5874 public class VariableDeclaration {
5875 public string identifier;
5876 public Expression expression_or_array_initializer;
5877 public Location Location;
5878 public Attributes OptAttributes;
5879 public string DocComment;
5881 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
5883 this.identifier = lt.Value;
5884 if (eoai is ArrayList) {
5885 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
5887 this.expression_or_array_initializer = (Expression)eoai;
5889 this.Location = lt.Location;
5890 this.OptAttributes = opt_attrs;
5893 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
5898 class VariableMemberDeclaration
5900 public readonly MemberName MemberName;
5901 public Expression expression_or_array_initializer;
5903 public VariableMemberDeclaration (MemberName mn, object initializer)
5907 if (initializer is ArrayList) {
5908 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)initializer, mn.Location);
5910 this.expression_or_array_initializer = (Expression)initializer;
5917 // A class used to hold info about an operator declarator
5919 struct OperatorDeclaration {
5920 public readonly Operator.OpType optype;
5921 public readonly FullNamedExpression ret_type;
5922 public readonly Location location;
5924 public OperatorDeclaration (Operator.OpType op, FullNamedExpression ret_type, Location location)
5927 this.ret_type = ret_type;
5928 this.location = location;
5932 void Error_ExpectingTypeName (Expression expr)
5934 if (expr is Invocation){
5935 Report.Error (1002, expr.Location, "Expecting `;'");
5937 Expression.Error_InvalidExpressionStatement (Report, expr.Location);
5941 void Error_ParameterModifierNotValid (string modifier, Location loc)
5943 Report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
5947 void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
5949 Report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
5950 Parameter.GetModifierSignature (mod));
5953 void Error_TypeExpected (Location loc)
5955 Report.Error (1031, loc, "Type expected");
5958 void Error_NamedArgumentExpected (NamedArgument a)
5960 Report.Error (1738, a.Name.Location, "Named arguments must appear after the positional arguments");
5963 void push_current_class (TypeContainer tc, object partial_token)
5965 if (RootContext.EvalMode){
5966 tc.ModFlags = (tc.ModFlags & ~(Modifiers.PRIVATE|Modifiers.INTERNAL)) | Modifiers.PUBLIC;
5967 undo.AddTypeContainer (current_container, tc);
5970 if (partial_token != null)
5971 current_container = current_container.AddPartial (tc);
5973 current_container = current_container.AddTypeContainer (tc);
5975 ++lexer.parsing_declaration;
5979 DeclSpace pop_current_class ()
5981 DeclSpace retval = current_class;
5983 current_class = current_class.Parent;
5984 current_container = current_class.PartialContainer;
5990 // Given the @class_name name, it creates a fully qualified name
5991 // based on the containing declaration space
5994 MakeName (MemberName class_name)
5996 Namespace ns = current_namespace.NS;
5998 if (current_container == RootContext.ToplevelTypes) {
5999 if (ns.Name.Length != 0)
6000 return new MemberName (ns.MemberName, class_name);
6004 return new MemberName (current_container.MemberName, class_name);
6008 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
6010 Block implicit_block;
6011 ArrayList inits = null;
6014 // If we are doing interactive editing, we want variable declarations
6015 // that are in the top block to be added instead to the class as
6018 if (RootContext.StatementMode){
6021 for (Block b = current_block; b != null; b = b.Parent){
6022 if (b is ExplicitBlock && !(b is ToplevelBlock)){
6023 // There has been an explicit block, we cant add to the class
6030 // We can use "current_block" since we know there are no explicit blocks
6032 foreach (VariableDeclaration decl in variable_declarators){
6033 // We can not use the super-handy f.Initializer, because
6034 // multiple lines would force code to be executed out of sync
6035 if (decl.expression_or_array_initializer != null){
6036 string id = "$" + decl.identifier;
6037 LocalInfo vi = current_block.AddVariable (type, id, decl.Location);
6039 // Avoid warning about this variable not being used.
6042 LocalVariableReference var;
6043 var = new LocalVariableReferenceWithClassSideEffect (current_container, decl.identifier, current_block, id, vi, decl.Location);
6044 Assign assign = new SimpleAssign (var, decl.expression_or_array_initializer, decl.Location);
6045 current_block.AddStatement (new StatementExpression (assign));
6046 assign = new SimpleAssign (new SimpleName (decl.identifier, decl.Location), var);
6047 current_block.AddStatement (new StatementExpression (assign));
6049 Field f = new Field (current_container, (FullNamedExpression) type, Modifiers.PUBLIC | Modifiers.STATIC,
6050 new MemberName (decl.identifier, loc), null);
6051 current_container.AddField (f);
6053 // Register the field to be visible later as a global variable
6054 Evaluator.QueueField (f);
6058 return current_block;
6063 // We use the `Used' property to check whether statements
6064 // have been added to the current block. If so, we need
6065 // to create another block to contain the new declaration
6066 // otherwise, as an optimization, we use the same block to
6067 // add the declaration.
6069 // FIXME: A further optimization is to check if the statements
6070 // that were added were added as part of the initialization
6071 // below. In which case, no other statements have been executed
6072 // and we might be able to reduce the number of blocks for
6073 // situations like this:
6075 // int j = 1; int k = j + 1;
6077 if (current_block.Used)
6078 implicit_block = new Block (current_block, loc, lexer.Location);
6080 implicit_block = current_block;
6082 foreach (VariableDeclaration decl in variable_declarators){
6084 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
6085 if (decl.expression_or_array_initializer != null){
6087 inits = new ArrayList (4);
6094 return implicit_block;
6096 foreach (VariableDeclaration decl in inits){
6098 Expression expr = decl.expression_or_array_initializer;
6100 LocalVariableReference var;
6101 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
6103 assign = new SimpleAssign (var, expr, decl.Location);
6105 implicit_block.AddStatement (new StatementExpression (assign));
6108 return implicit_block;
6111 Block declare_local_constants (Expression type, ArrayList declarators)
6113 Block implicit_block;
6115 if (current_block.Used)
6116 implicit_block = new Block (current_block);
6118 implicit_block = current_block;
6120 foreach (VariableDeclaration decl in declarators){
6121 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
6124 return implicit_block;
6127 string CheckAttributeTarget (string a, Location l)
6130 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
6134 Report.Warning (658, 1, l,
6135 "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
6136 return string.Empty;
6139 static bool IsUnaryOperator (Operator.OpType op)
6143 case Operator.OpType.LogicalNot:
6144 case Operator.OpType.OnesComplement:
6145 case Operator.OpType.Increment:
6146 case Operator.OpType.Decrement:
6147 case Operator.OpType.True:
6148 case Operator.OpType.False:
6149 case Operator.OpType.UnaryPlus:
6150 case Operator.OpType.UnaryNegation:
6156 void syntax_error (Location l, string msg)
6158 Report.Error (1003, l, "Syntax error, " + msg);
6163 public Tokenizer Lexer {
6169 static CSharpParser ()
6171 oob_stack = new Stack ();
6174 public CSharpParser (SeekableStreamReader reader, CompilationUnit file, CompilerContext ctx)
6176 if (RootContext.EvalMode)
6180 this.compiler = ctx;
6181 current_namespace = new NamespaceEntry (null, file, null);
6182 current_class = current_namespace.SlaveDeclSpace;
6183 current_container = current_class.PartialContainer; // == RootContest.ToplevelTypes
6185 lexer = new Tokenizer (reader, file, ctx);
6188 public void parse ()
6190 eof_token = Token.EOF;
6193 if (yacc_verbose_flag > 1)
6194 yyparse (lexer, new yydebug.yyDebugSimple ());
6197 } catch (Exception e){
6198 if (e is yyParser.yyUnexpectedEof)
6199 UnexpectedEOF = true;
6200 else if (yacc_verbose_flag > 0)
6201 Console.WriteLine (e);
6202 if (e is yyParser.yyException)
6203 Report.Error (-25, lexer.Location, "Parsing error");
6205 Report.Error (-32, lexer.Location, "Internal compiler error during parsing, Run with -v for details");
6207 Tokenizer tokenizer = lexer as Tokenizer;
6208 tokenizer.cleanup ();
6210 if (RootContext.ToplevelTypes.NamespaceEntry != null)
6211 throw new InternalErrorException ("who set it?");
6214 void CheckToken (int error, int yyToken, string msg, Location loc)
6216 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
6217 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, GetTokenName (yyToken));
6219 Report.Error (error, loc, msg);
6222 void CheckIdentifierToken (int yyToken, Location loc)
6224 CheckToken (1041, yyToken, "Identifier expected", loc);
6227 string ConsumeStoredComment ()
6229 string s = tmpComment;
6231 Lexer.doc_state = XmlCommentState.Allowed;
6235 Location GetLocation (object obj)
6237 if (obj is MemberCore)
6238 return ((MemberCore) obj).Location;
6239 if (obj is MemberName)
6240 return ((MemberName) obj).Location;
6241 if (obj is LocatedToken)
6242 return ((LocatedToken) obj).Location;
6243 if (obj is Location)
6244 return (Location) obj;
6245 return lexer.Location;
6249 get { return compiler.Report; }
6252 void start_block (Location loc)
6254 if (current_block == null || parsing_anonymous_method) {
6255 current_block = new ToplevelBlock (compiler, current_block, current_local_parameters, current_generic_method, loc);
6256 parsing_anonymous_method = false;
6258 current_block = new ExplicitBlock (current_block, loc, Location.Null);
6263 end_block (Location loc)
6265 Block retval = current_block.Explicit;
6266 retval.SetEndLocation (loc);
6267 current_block = retval.Parent;
6272 start_anonymous (bool lambda, ParametersCompiled parameters, Location loc)
6274 if (RootContext.Version == LanguageVersion.ISO_1){
6275 Report.FeatureIsNotAvailable (loc, "anonymous methods");
6278 oob_stack.Push (current_anonymous_method);
6279 oob_stack.Push (current_local_parameters);
6281 current_local_parameters = parameters;
6283 current_anonymous_method = lambda
6284 ? new LambdaExpression (loc)
6285 : new AnonymousMethodExpression (loc);
6287 // Force the next block to be created as a ToplevelBlock
6288 parsing_anonymous_method = true;
6292 * Completes the anonymous method processing, if lambda_expr is null, this
6293 * means that we have a Statement instead of an Expression embedded
6295 AnonymousMethodExpression end_anonymous (ToplevelBlock anon_block)
6297 AnonymousMethodExpression retval;
6299 current_anonymous_method.Block = anon_block;
6300 retval = current_anonymous_method;
6302 current_local_parameters = (ParametersCompiled) oob_stack.Pop ();
6303 current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
6308 public NamespaceEntry CurrentNamespace {
6310 return current_namespace;
6315 void Error_SyntaxError (int token)
6317 Error_SyntaxError (0, token);
6320 void Error_SyntaxError (int error_code, int token)
6322 string symbol = GetSymbolName (token);
6323 string expecting = GetExpecting ();
6325 if (error_code == 0) {
6326 if (expecting == "`)'")
6332 if (expecting != null)
6333 Report.Error (error_code, lexer.Location, "Unexpected symbol `{0}', expecting {1}",
6336 Report.Error (error_code, lexer.Location, "Unexpected symbol `{0}'", symbol);
6339 string GetExpecting ()
6341 int [] tokens = yyExpectingTokens (yyExpectingState);
6342 ArrayList names = new ArrayList (tokens.Length);
6343 bool has_type = false;
6344 bool has_identifier = false;
6345 for (int i = 0; i < tokens.Length; i++){
6346 int token = tokens [i];
6347 has_identifier |= token == Token.IDENTIFIER;
6349 string name = GetTokenName (token);
6350 if (name == "<internal>")
6353 has_type |= name == "type";
6354 if (names.Contains (name))
6361 // Too many tokens to enumerate
6363 if (names.Count > 8)
6366 if (has_type && has_identifier)
6367 names.Remove ("identifier");
6369 if (names.Count == 1)
6370 return "`" + GetTokenName (tokens [0]) + "'";
6372 StringBuilder sb = new StringBuilder ();
6374 int count = names.Count;
6375 for (int i = 0; i < count; i++){
6376 bool last = i + 1 == count;
6380 sb.Append (names [i]);
6381 sb.Append (last ? "'" : "', ");
6383 return sb.ToString ();
6387 string GetSymbolName (int token)
6390 case Token.LITERAL_FLOAT:
6391 case Token.LITERAL_INTEGER:
6392 case Token.LITERAL_DOUBLE:
6393 case Token.LITERAL_DECIMAL:
6394 case Token.LITERAL_CHARACTER:
6395 case Token.LITERAL_STRING:
6396 return lexer.Value.ToString ();
6397 case Token.IDENTIFIER:
6398 return ((LocatedToken)lexer.Value).Value;
6440 case Token.BITWISE_AND:
6442 case Token.BITWISE_OR:
6456 case Token.OP_SHIFT_LEFT:
6458 case Token.OP_SHIFT_RIGHT:
6478 case Token.OP_COALESCING:
6480 case Token.OP_MULT_ASSIGN:
6482 case Token.OP_DIV_ASSIGN:
6484 case Token.OP_MOD_ASSIGN:
6486 case Token.OP_ADD_ASSIGN:
6488 case Token.OP_SUB_ASSIGN:
6490 case Token.OP_SHIFT_LEFT_ASSIGN:
6492 case Token.OP_SHIFT_RIGHT_ASSIGN:
6494 case Token.OP_AND_ASSIGN:
6496 case Token.OP_XOR_ASSIGN:
6498 case Token.OP_OR_ASSIGN:
6502 return GetTokenName (token);
6505 static string GetTokenName (int token)
6508 case Token.ABSTRACT:
6528 case Token.CONTINUE:
6532 case Token.DELEGATE:
6542 case Token.EXPLICIT:
6560 case Token.IMPLICIT:
6564 case Token.INTERFACE:
6566 case Token.INTERNAL:
6572 case Token.NAMESPACE:
6578 case Token.OPERATOR:
6582 case Token.OVERRIDE:
6588 case Token.PROTECTED:
6592 case Token.READONLY:
6604 case Token.STACKALLOC:
6605 return "stackalloc";
6622 case Token.UNCHECKED:
6630 case Token.VOLATILE:
6643 case Token.FROM_FIRST:
6661 case Token.ASCENDING:
6663 case Token.DESCENDING:
6664 return "descending";
6671 case Token.OPEN_BRACE:
6673 case Token.CLOSE_BRACE:
6675 case Token.OPEN_BRACKET:
6677 case Token.CLOSE_BRACKET:
6679 case Token.OPEN_PARENS_CAST:
6680 case Token.OPEN_PARENS_LAMBDA:
6681 case Token.OPEN_PARENS:
6683 case Token.CLOSE_PARENS:
6689 case Token.DEFAULT_COLON:
6693 case Token.SEMICOLON:
6704 case Token.BITWISE_AND:
6705 case Token.BITWISE_OR:
6712 case Token.OP_SHIFT_LEFT:
6713 case Token.OP_SHIFT_RIGHT:
6721 case Token.OP_COALESCING:
6722 case Token.OP_MULT_ASSIGN:
6723 case Token.OP_DIV_ASSIGN:
6724 case Token.OP_MOD_ASSIGN:
6725 case Token.OP_ADD_ASSIGN:
6726 case Token.OP_SUB_ASSIGN:
6727 case Token.OP_SHIFT_LEFT_ASSIGN:
6728 case Token.OP_SHIFT_RIGHT_ASSIGN:
6729 case Token.OP_AND_ASSIGN:
6730 case Token.OP_XOR_ASSIGN:
6731 case Token.OP_OR_ASSIGN:
6732 return "<operator>";
6754 case Token.OP_GENERICS_LT:
6755 case Token.GENERIC_DIMENSION:
6757 case Token.OP_GENERICS_GT:
6760 case Token.INTERR_NULLABLE:
6762 case Token.DOUBLE_COLON:
6764 case Token.LITERAL_FLOAT:
6765 case Token.LITERAL_INTEGER:
6766 case Token.LITERAL_DOUBLE:
6767 case Token.LITERAL_DECIMAL:
6768 case Token.LITERAL_CHARACTER:
6769 case Token.LITERAL_STRING:
6771 case Token.IDENTIFIER:
6772 return "identifier";
6774 // All of these are internal.
6777 case Token.FIRST_KEYWORD:
6779 case Token.EVAL_COMPILATION_UNIT_PARSER:
6780 case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
6781 case Token.EVAL_STATEMENT_PARSER:
6782 case Token.LAST_KEYWORD:
6783 case Token.GENERATE_COMPLETION:
6784 case Token.COMPLETE_COMPLETION:
6785 return "<internal>";
6787 // A bit more robust.
6789 return yyNames [token];