3 // cs-parser.jay: The Parser for the C# compiler
5 // Author: Miguel de Icaza (miguel@gnu.org)
7 // Licensed under the terms of the GNU GPL
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (1) Get rid of the *Collections.cs, that is an idea I took from System.CodeDOM
13 // And come to think of it, it is not that great, it duplicates a lot of code
14 // for something which is not really needed. We still have piles of typecasts
15 // anwyays (due to the nature of the stack being a collection of Objects).
17 // (2) Figure out why error productions dont work. `type-declaration' is a
18 // great spot to put an `error' because you can reproduce it with this input:
21 // (3) Move Modifier checking from each object into the parser itself, that will
22 // get rid of the global "error" symbol that we use now to report errors.
23 // We still need to pass a pointer to the tree.ErrorHandler, but that is a
31 using System.Collections;
37 public class CSharpParser : GenericParser {
38 Namespace current_namespace;
39 TypeContainer current_container;
42 // Current block is used to add statements as we find
49 // Current interface is used by the various declaration
50 // productions in the interface declaration to "add"
51 // the interfaces as we find them.
53 Interface current_interface;
56 // This is used by the unary_expression code to resolve
57 // a name against a parameter.
59 Parameters current_local_parameters;
62 // Using during property parsing to describe the implicit
63 // value parameter that is passed to the "set" and "get"accesor
64 // methods (properties and indexers).
66 string implicit_value_parameter_type;
67 Parameters indexer_parameters;
70 // Used to determine if we are parsing the get/set pair
71 // of an indexer or a property
76 // An out-of-band stack.
82 %token NONE /* This token is never returned by our lexer */
83 %token ERROR // This is used not by the parser, but by the tokenizer.
87 *These are the C# keywords
167 /* C# keywords which are not really keywords */
171 /* C# single character operators/punctuation. */
172 %token OPEN_BRACE "{"
173 %token CLOSE_BRACE "}"
174 %token OPEN_BRACKET "["
175 %token CLOSE_BRACKET "]"
176 %token OPEN_PARENS "("
177 %token CLOSE_PARENS ")"
190 %token BITWISE_AND "&"
191 %token BITWISE_OR "|"
198 /* C# multi-character operators. */
201 %token OP_SHIFT_LEFT "<<"
202 %token OP_SHIFT_RIGHT ">>"
209 %token OP_MULT_ASSIGN "*="
210 %token OP_DIV_ASSIGN "/="
211 %token OP_MOD_ASSIGN "%="
212 %token OP_ADD_ASSIGN "+="
213 %token OP_SUB_ASSIGN "-="
214 %token OP_SHIFT_LEFT_ASSIGN "<<="
215 %token OP_SHIFT_RIGHT_ASSIGN ">>="
216 %token OP_AND_ASSIGN "&="
217 %token OP_XOR_ASSIGN "^="
218 %token OP_OR_ASSIGN "|="
222 %token LITERAL_INTEGER "int literal"
223 %token LITERAL_FLOAT "float literal"
224 %token LITERAL_DOUBLE "double literal"
225 %token LITERAL_DECIMAL "decimal literal"
226 %token LITERAL_CHARACTER "character literal"
227 %token LITERAL_STRING "string literal"
231 /* Add precedence rules to solve dangling else s/r conflict */
240 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
242 %left STAR DIV PERCENT
243 %right BANG CARRET UMINUS
244 %nonassoc OP_INC OP_DEC
246 %left OPEN_BRACKET OPEN_BRACE
250 %start compilation_unit
251 /*%start namespace_declaration */
255 : opt_using_directives opt_namespace_member_declarations EOF
257 // Check that using comes only before namespace elements
263 | using_directives using_directive
267 : using_alias_directive
268 | using_namespace_directive
271 using_alias_directive
272 : USING IDENTIFIER ASSIGN
273 namespace_or_type_name SEMICOLON
275 current_namespace.UsingAlias ((string) $2, (string) $4);
279 using_namespace_directive
280 : USING namespace_name SEMICOLON
282 current_namespace.Using ((string) $2);
286 // namespace_declarations
287 // : namespace_declaration
288 // | namespace_declarations namespace_declaration
290 namespace_declaration
291 : NAMESPACE qualified_identifier
293 current_namespace = RootContext.Tree.RecordNamespace (current_namespace, (string) $2);
295 namespace_body opt_semicolon
297 current_namespace = current_namespace.Parent;
313 | qualified_identifier DOT IDENTIFIER {
314 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
319 : namespace_or_type_name
325 opt_namespace_member_declarations
336 opt_namespace_member_declarations
338 | namespace_member_declarations
341 namespace_member_declarations
342 : namespace_member_declaration
343 | namespace_member_declarations namespace_member_declaration
346 namespace_member_declaration
353 Class c = (Class) $1;
354 mod_flags = c.ModFlags;
356 } else if ($1 is Struct){
357 Struct s = (Struct) $1;
358 mod_flags = s.ModFlags;
364 // We remove this error until we can
365 //if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
366 // error (1527, "Namespace elements cant be explicitly " +
367 // "declared private or protected in `" + name + "'");
370 | namespace_declaration
376 | interface_declaration
378 | delegate_declaration
385 : /* empty */ { $$ = null; }
386 | attribute_section opt_attributes
391 attrs = (Attributes) $2;
392 attrs.AddAttribute ((AttributeSection) $1);
394 attrs = new Attributes ((AttributeSection) $1);
401 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
403 string target = null;
406 target = (string) $2;
408 $$ = new AttributeSection (target, (ArrayList) $3);
410 | OPEN_BRACKET attribute_list CLOSE_BRACKET
412 $$ = new AttributeSection (null, (ArrayList) $2);
416 attribute_target_specifier
417 : attribute_target COLON
426 CheckAttributeTarget ((string) $1);
429 | EVENT { $$ = "event"; }
430 | RETURN { $$ = "return"; }
436 ArrayList attrs = new ArrayList ();
442 | attribute_list COMMA attribute
444 ArrayList attrs = (ArrayList) $1;
456 opt_attribute_arguments
458 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
463 : type_name { /* reserved attribute name or identifier: 17.4 */ }
466 opt_attribute_arguments
467 : /* empty */ { $$ = null; }
468 | OPEN_PARENS attribute_arguments CLOSE_PARENS
476 : positional_argument_list
478 ArrayList args = new ArrayList ();
483 | positional_argument_list COMMA named_argument_list
485 ArrayList args = new ArrayList ();
491 | named_argument_list
493 ArrayList args = new ArrayList ();
502 positional_argument_list
505 ArrayList args = new ArrayList ();
506 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
510 | positional_argument_list COMMA expression
512 ArrayList args = (ArrayList) $1;
513 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
522 ArrayList args = new ArrayList ();
527 | named_argument_list COMMA named_argument
529 ArrayList args = (ArrayList) $1;
537 : IDENTIFIER ASSIGN expression
539 $$ = new DictionaryEntry (
541 new Argument ((Expression) $3, Argument.AType.Expression));
547 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
550 opt_class_member_declarations
552 | class_member_declarations
555 class_member_declarations
556 : class_member_declaration
557 | class_member_declarations
558 class_member_declaration
561 class_member_declaration
562 : constant_declaration // done
563 | field_declaration // done
564 | method_declaration // done
565 | property_declaration // done
566 | event_declaration // done
567 | indexer_declaration // done
568 | operator_declaration // done
569 | constructor_declaration // done
570 | destructor_declaration // done
580 string full_struct_name = MakeName ((string) $4);
582 new_struct = new Struct (current_container, full_struct_name, (int) $2,
583 (Attributes) $1, lexer.Location);
584 current_container = new_struct;
585 current_container.Namespace = current_namespace;
586 RootContext.Tree.RecordStruct (full_struct_name, new_struct);
588 opt_struct_interfaces
592 Struct new_struct = (Struct) current_container;
594 current_container = current_container.Parent;
595 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
600 opt_struct_interfaces
607 | struct_interfaces struct_interface
615 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
618 opt_struct_member_declarations
620 | struct_member_declarations
623 struct_member_declarations
624 : struct_member_declaration
625 | struct_member_declarations struct_member_declaration
628 struct_member_declaration
629 : constant_declaration
632 | property_declaration
634 | indexer_declaration
635 | operator_declaration
636 | constructor_declaration
640 * This is only included so we can flag error 575:
641 * destructors only allowed on class types
643 | destructor_declaration
654 foreach (VariableDeclaration constant in (ArrayList) $5){
655 Const c = new Const (
656 (string) $4, (string) constant.identifier,
657 (Expression) constant.expression_or_array_initializer, (int) $2, (Attributes) $1,
660 CheckDef (current_container.AddConstant (c), c.Name);
666 : constant_declarator
668 ArrayList constants = new ArrayList ();
672 | constant_declarators COMMA constant_declarator
674 ArrayList constants = (ArrayList) $1;
681 : IDENTIFIER ASSIGN constant_expression
683 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
694 string type = (string) $3;
697 foreach (VariableDeclaration var in (ArrayList) $4){
698 Field field = new Field (type, mod, var.identifier,
699 var.expression_or_array_initializer, (Attributes) $1, var.Location);
701 CheckDef (current_container.AddField (field), field.Name);
707 : variable_declarator
709 ArrayList decl = new ArrayList ();
713 | variable_declarators COMMA variable_declarator
715 ArrayList decls = (ArrayList) $1;
722 : IDENTIFIER ASSIGN variable_initializer
724 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
728 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
747 Method method = (Method) $1;
748 Block b = (Block) $2;
751 if ((method.ModFlags & (Modifiers.EXTERN | Modifiers.ABSTRACT)) == 0){
753 501, lexer.Location, "`" +
754 current_container.Name + "." + method.Name + "'" +
755 "must declare a body because it is not marked abstract or extern");
759 method.Block = (Block) $2;
760 CheckDef (current_container.AddMethod (method), method.Name);
762 current_local_parameters = null;
771 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
773 Method method = new Method ((string) $3, (int) $2, (string) $4,
774 (Parameters) $6, (Attributes) $1, lexer.Location);
776 current_local_parameters = (Parameters) $6;
784 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
786 Method method = new Method ("System.Void", (int) $2, (string) $4,
787 (Parameters) $6, (Attributes) $1, lexer.Location);
789 current_local_parameters = (Parameters) $6;
796 | SEMICOLON { $$ = null; }
799 opt_formal_parameter_list
800 : /* empty */ { $$ = Parameters.GetEmptyReadOnlyParameters (); }
801 | formal_parameter_list
804 formal_parameter_list
807 ArrayList pars_list = (ArrayList) $1;
809 Parameter [] pars = new Parameter [pars_list.Count];
810 pars_list.CopyTo (pars);
812 $$ = new Parameters (pars, null);
814 | fixed_parameters COMMA parameter_array
816 ArrayList pars_list = (ArrayList) $1;
818 Parameter [] pars = new Parameter [pars_list.Count];
819 pars_list.CopyTo (pars);
821 $$ = new Parameters (pars, (Parameter) $3);
825 $$ = new Parameters (null, (Parameter) $1);
832 ArrayList pars = new ArrayList ();
837 | fixed_parameters COMMA fixed_parameter
839 ArrayList pars = (ArrayList) $1;
848 opt_parameter_modifier
852 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
856 opt_parameter_modifier
857 : /* empty */ { $$ = Parameter.Modifier.NONE; }
862 : REF { $$ = Parameter.Modifier.REF; }
863 | OUT { $$ = Parameter.Modifier.OUT; }
867 : opt_attributes PARAMS type IDENTIFIER
869 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
870 note ("type must be a single-dimension array type");
875 : qualified_identifier
884 implicit_value_parameter_type = (string) $3;
886 lexer.properties = true;
890 accessor_declarations
892 lexer.properties = false;
897 DictionaryEntry pair = (DictionaryEntry) $7;
898 Block get_block = null;
899 Block set_block = null;
901 if (pair.Key != null)
902 get_block = (Block) pair.Key;
903 if (pair.Value != null)
904 set_block = (Block) pair.Value;
906 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block,
907 (Attributes) $1, (Location) $6);
909 CheckDef (current_container.AddProperty (prop), prop.Name);
910 implicit_value_parameter_type = null;
914 accessor_declarations
915 : get_accessor_declaration opt_set_accessor_declaration
917 $$ = new DictionaryEntry ($1, $2);
919 | set_accessor_declaration opt_get_accessor_declaration
921 $$ = new DictionaryEntry ($2, $1);
925 opt_get_accessor_declaration
926 : /* empty */ { $$ = null; }
927 | get_accessor_declaration
930 opt_set_accessor_declaration
931 : /* empty */ { $$ = null; }
932 | set_accessor_declaration
935 get_accessor_declaration
938 // If this is not the case, then current_local_parameters has already
939 // been set in indexer_declaration
940 if (parsing_indexer == false)
941 current_local_parameters = null;
943 current_local_parameters = indexer_parameters;
950 current_local_parameters = null;
954 set_accessor_declaration
958 Parameter implicit_value_parameter = new Parameter (
959 implicit_value_parameter_type, "value",
960 Parameter.Modifier.NONE, null);
962 if (parsing_indexer == false) {
963 args = new Parameter [1];
964 args [0] = implicit_value_parameter;
966 Parameter [] fp = indexer_parameters.FixedParameters;
967 int count = fp.Length;
969 args = new Parameter [count + 1];
972 args [count] = implicit_value_parameter;
974 current_local_parameters = new Parameters (args, null);
979 current_local_parameters = null;
985 | SEMICOLON { $$ = new Block (null); }
988 interface_declaration
993 Interface new_interface;
994 string full_interface_name = MakeName ((string) $4);
996 new_interface = new Interface (current_container, full_interface_name, (int) $2,
997 (Attributes) $1, lexer.Location);
998 if (current_interface != null) {
999 Location l = lexer.Location;
1000 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1002 current_interface = new_interface;
1003 new_interface.Namespace = current_namespace;
1004 RootContext.Tree.RecordInterface (full_interface_name, new_interface);
1009 Interface new_interface = (Interface) current_interface;
1012 new_interface.Bases = (ArrayList) $6;
1014 current_interface = null;
1015 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
1020 : /* empty */ { $$ = null; }
1025 : COLON interface_type_list { $$ = $2; }
1031 ArrayList interfaces = new ArrayList ();
1033 interfaces.Add ($1);
1036 | interface_type_list COMMA interface_type
1038 ArrayList interfaces = (ArrayList) $1;
1039 interfaces.Add ($3);
1046 opt_interface_member_declarations
1050 opt_interface_member_declarations
1052 | interface_member_declarations
1055 interface_member_declarations
1056 : interface_member_declaration
1057 | interface_member_declarations interface_member_declaration
1060 interface_member_declaration
1061 : interface_method_declaration
1063 InterfaceMethod m = (InterfaceMethod) $1;
1065 CheckDef (current_interface.AddMethod (m), m.Name);
1067 | interface_property_declaration
1069 InterfaceProperty p = (InterfaceProperty) $1;
1071 CheckDef (current_interface.AddProperty (p), p.Name);
1073 | interface_event_declaration
1075 InterfaceEvent e = (InterfaceEvent) $1;
1077 CheckDef (current_interface.AddEvent (e), e.Name);
1079 | interface_indexer_declaration
1081 InterfaceIndexer i = (InterfaceIndexer) $1;
1083 CheckDef (current_interface.AddIndexer (i), "indexer");
1088 : /* empty */ { $$ = false; }
1089 | NEW { $$ = true; }
1092 interface_method_declaration
1093 : opt_attributes opt_new type IDENTIFIER
1094 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1097 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1099 | opt_attributes opt_new VOID IDENTIFIER
1100 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1103 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1107 interface_property_declaration
1112 { lexer.properties = true; }
1114 { lexer.properties = false; }
1119 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
1120 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1);
1125 : opt_attributes GET SEMICOLON { $$ = 1; }
1126 | opt_attributes SET SEMICOLON { $$ = 2; }
1127 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1129 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1133 interface_event_declaration
1134 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1136 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1140 interface_indexer_declaration
1141 : opt_attributes opt_new type THIS
1142 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1144 { lexer.properties = true; }
1146 { lexer.properties = false; }
1149 int a_flags = (int) $10;
1151 bool do_get = (a_flags & 1) == 1;
1152 bool do_set = (a_flags & 2) == 2;
1154 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set, (bool) $2, (Attributes) $1);
1158 operator_declaration
1159 : opt_attributes opt_modifiers operator_declarator block
1161 OperatorDeclaration decl = (OperatorDeclaration) $3;
1163 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1164 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1166 // Note again, checking is done in semantic analysis
1167 current_container.AddOperator (op);
1169 current_local_parameters = null;
1174 : type OPERATOR overloadable_operator
1175 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1177 Operator.OpType op = (Operator.OpType) $3;
1178 CheckUnaryOperator (op);
1180 if (op == Operator.OpType.Addition)
1181 op = Operator.OpType.UnaryPlus;
1183 if (op == Operator.OpType.Subtraction)
1184 op = Operator.OpType.UnaryNegation;
1186 Parameter [] pars = new Parameter [1];
1188 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1190 current_local_parameters = new Parameters (pars, null);
1192 $$ = new OperatorDeclaration (op, (string) $1, (string) $5, (string) $6,
1193 null, null, lexer.Location);
1195 | type OPERATOR overloadable_operator
1197 type IDENTIFIER COMMA
1201 CheckBinaryOperator ((Operator.OpType) $3);
1203 Parameter [] pars = new Parameter [2];
1205 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1206 pars [1] = new Parameter ((string) $8, (string) $9, Parameter.Modifier.NONE, null);
1208 current_local_parameters = new Parameters (pars, null);
1210 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1211 (string) $8, (string) $9, lexer.Location);
1213 | conversion_operator_declarator
1216 overloadable_operator
1218 : BANG { $$ = Operator.OpType.LogicalNot; }
1219 | TILDE { $$ = Operator.OpType.OnesComplement; }
1220 | OP_INC { $$ = Operator.OpType.Increment; }
1221 | OP_DEC { $$ = Operator.OpType.Decrement; }
1222 | TRUE { $$ = Operator.OpType.True; }
1223 | FALSE { $$ = Operator.OpType.False; }
1224 // Unary and binary:
1225 | PLUS { $$ = Operator.OpType.Addition; }
1226 | MINUS { $$ = Operator.OpType.Subtraction; }
1228 | STAR { $$ = Operator.OpType.Multiply; }
1229 | DIV { $$ = Operator.OpType.Division; }
1230 | PERCENT { $$ = Operator.OpType.Modulus; }
1231 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1232 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1233 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1234 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1235 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1236 | OP_EQ { $$ = Operator.OpType.Equality; }
1237 | OP_NE { $$ = Operator.OpType.Inequality; }
1238 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1239 | OP_LT { $$ = Operator.OpType.LessThan; }
1240 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1241 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1244 conversion_operator_declarator
1245 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1247 Parameter [] pars = new Parameter [1];
1249 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1251 current_local_parameters = new Parameters (pars, null);
1253 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1254 null, null, lexer.Location);
1256 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1258 Parameter [] pars = new Parameter [1];
1260 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1262 current_local_parameters = new Parameters (pars, null);
1264 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1265 null, null, lexer.Location);
1269 syntax_error (lexer.Location, "'operator' expected");
1273 syntax_error (lexer.Location, "'operator' expected");
1277 constructor_declaration
1280 constructor_declarator
1283 Constructor c = (Constructor) $3;
1284 c.Block = (Block) $4;
1285 c.ModFlags = (int) $2;
1286 c.OptAttributes = (Attributes) $1;
1288 if ((c.ModFlags & Modifiers.STATIC) != 0){
1289 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1290 Location l = lexer.Location;
1291 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1294 if (c.Initializer != null){
1295 Location l = lexer.Location;
1296 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1299 if (!c.Parameters.Empty){
1300 Location l = lexer.Location;
1301 Report.Error (103, l, "Static constructors should not have parameters");
1305 CheckDef (current_container.AddConstructor (c), c.Name);
1307 current_local_parameters = null;
1311 constructor_declarator
1313 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1314 opt_constructor_initializer
1316 Location l = lexer.Location;
1318 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5, l);
1320 current_local_parameters = (Parameters) $3;
1324 opt_constructor_initializer
1325 : /* empty */ { $$ = null; }
1326 | constructor_initializer
1329 constructor_initializer
1330 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1332 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1334 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1336 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1340 destructor_declaration
1341 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1343 if ((string) $3 != current_container.Basename){
1344 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1345 } else if (!(current_container is Class)){
1346 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1348 Method d = new Method (
1349 "System.Void", 0, "Finalize",
1350 new Parameters (null, null), (Attributes) $1, lexer.Location);
1352 d.Block = (Block) $6;
1353 CheckDef (current_container.AddMethod (d), d.Name);
1361 EVENT type variable_declarators SEMICOLON
1363 foreach (VariableDeclaration var in (ArrayList) $5) {
1365 // FIXME : Is this right ?
1366 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1367 (int) $2, null, null, (Attributes) $1, lexer.Location);
1369 CheckDef (current_container.AddEvent (e), e.Name);
1375 EVENT type member_name
1376 OPEN_BRACE event_accessor_declarations CLOSE_BRACE
1378 DictionaryEntry pair = (DictionaryEntry) $7;
1379 Block add_block = null;
1380 Block rem_block = null;
1382 if (pair.Key != null)
1383 add_block = (Block) pair.Key;
1384 if (pair.Value != null)
1385 rem_block = (Block) pair.Value;
1387 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
1388 (Attributes) $1, lexer.Location);
1390 CheckDef (current_container.AddEvent (e), e.Name);
1394 event_accessor_declarations
1395 : add_accessor_declaration remove_accessor_declaration
1397 $$ = new DictionaryEntry ($1, $2);
1399 | remove_accessor_declaration add_accessor_declaration
1401 $$ = new DictionaryEntry ($2, $1);
1405 add_accessor_declaration
1406 : opt_attributes ADD block
1412 remove_accessor_declaration
1413 : opt_attributes REMOVE block
1420 : opt_attributes opt_modifiers indexer_declarator
1423 IndexerDeclaration decl = (IndexerDeclaration) $3;
1425 implicit_value_parameter_type = decl.type;
1427 lexer.properties = true;
1428 parsing_indexer = true;
1430 indexer_parameters = decl.param_list;
1431 $$ = lexer.Location;
1433 accessor_declarations
1435 lexer.properties = false;
1436 parsing_indexer = false;
1440 // The signature is computed from the signature of the indexer. Look
1441 // at section 3.6 on the spec
1444 IndexerDeclaration decl = (IndexerDeclaration) $3;
1445 DictionaryEntry pair = (DictionaryEntry) $6;
1446 Block get_block = null;
1447 Block set_block = null;
1449 if (pair.Key != null)
1450 get_block = (Block) pair.Key;
1451 if (pair.Value != null)
1452 set_block = (Block) pair.Value;
1454 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1455 get_block, set_block, (Attributes) $1, (Location) $5);
1457 // Note that there is no equivalent of CheckDef for this case
1458 // We shall handle this in semantic analysis
1460 current_container.AddIndexer (indexer);
1462 current_local_parameters = null;
1463 implicit_value_parameter_type = null;
1464 indexer_parameters = null;
1469 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1471 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
1473 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1475 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
1487 string name = (string) $4;
1488 Enum e = new Enum ((string) $5, (int) $2, name, (Attributes) $1, lexer.Location);
1490 foreach (VariableDeclaration ev in (ArrayList) $6){
1491 CheckDef (e.AddEnumMember (ev.identifier,
1492 (Expression) ev.expression_or_array_initializer,
1493 (Location) ev.Location),
1497 CheckDef (current_container.AddEnum (e), name);
1502 : /* empty */ { $$ = "System.Int32"; }
1503 | COLON type { $$ = $2; }
1507 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1513 opt_enum_member_declarations
1514 : /* empty */ { $$ = new ArrayList (); }
1515 | enum_member_declarations opt_comma { $$ = $1; }
1518 enum_member_declarations
1519 : enum_member_declaration
1521 ArrayList l = new ArrayList ();
1526 | enum_member_declarations COMMA enum_member_declaration
1528 ArrayList l = (ArrayList) $1;
1536 enum_member_declaration
1537 : opt_attributes IDENTIFIER
1539 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1541 | opt_attributes IDENTIFIER
1543 $$ = lexer.Location;
1547 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location);
1551 delegate_declaration
1555 IDENTIFIER OPEN_PARENS
1556 formal_parameter_list
1560 Delegate del = new Delegate ((string) $4, (int) $2,
1561 MakeName ((string) $5), (Parameters) $7,
1562 (Attributes) $1, lexer.Location);
1564 CheckDef (current_container.AddDelegate (del), del.Name);
1569 IDENTIFIER OPEN_PARENS
1570 formal_parameter_list
1574 Delegate del = new Delegate ("System.Void", (int) $2, (string) $5, (Parameters) $7,
1575 (Attributes) $1, lexer.Location);
1577 CheckDef (current_container.AddDelegate (del), del.Name);
1582 : namespace_or_type_name
1585 namespace_or_type_name
1586 : qualified_identifier
1590 * Before you think of adding a return_type, notice that we have been
1591 * using two rules in the places where it matters (one rule using type
1592 * and another identical one that uses VOID as the return type). This
1593 * gets rid of a shift/reduce couple
1596 : type_name { /* class_type */
1598 This does interfaces, delegates, struct_types, class_types,
1599 parent classes, and more! 4.2
1610 $$ = new SimpleName ((string) $1, lexer.Location);
1612 | non_expression_type rank_specifier
1614 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1616 | expression rank_specifiers
1618 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1625 ArrayList types = new ArrayList ();
1630 | type_list COMMA type
1632 ArrayList types = (ArrayList) $1;
1640 * replaces all the productions for isolating the various
1641 * simple types, but we need this to reuse it easily in local_variable_type
1644 : OBJECT { $$ = "System.Object"; }
1645 | STRING { $$ = "System.String"; }
1646 | BOOL { $$ = "System.Boolean"; }
1647 | DECIMAL { $$ = "System.Decimal"; }
1648 | FLOAT { $$ = "System.Single"; }
1649 | DOUBLE { $$ = "System.Double"; }
1654 : SBYTE { $$ = "System.SByte"; }
1655 | BYTE { $$ = "System.Byte"; }
1656 | SHORT { $$ = "System.Int16"; }
1657 | USHORT { $$ = "System.UInt16"; }
1658 | INT { $$ = "System.Int32"; }
1659 | UINT { $$ = "System.UInt32"; }
1660 | LONG { $$ = "System.Int64"; }
1661 | ULONG { $$ = "System.UInt64"; }
1662 | CHAR { $$ = "System.Char"; }
1670 : type rank_specifiers
1672 $$ = (string) $1 + (string) $2;
1677 // Expressions, section 7.5
1685 | qualified_identifier
1687 string name = (string) $1;
1690 $$ = DecomposeQI (name, lexer.Location);
1692 | parenthesized_expression
1694 | invocation_expression
1698 | post_increment_expression
1699 | post_decrement_expression
1703 | checked_expression
1704 | unchecked_expression
1711 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1712 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1713 | NULL { $$ = NullLiteral.Null; }
1717 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1718 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1719 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1724 object v = lexer.Value;
1727 // FIXME: Possible optimization would be to
1728 // compute the *Literal objects directly in the scanner
1731 $$ = new IntLiteral ((Int32) v);
1733 $$ = new UIntLiteral ((UInt32) v);
1735 $$ = new LongLiteral ((Int64) v);
1736 else if (v is ulong)
1737 $$ = new ULongLiteral ((UInt64) v);
1739 Console.WriteLine ("OOPS. Unexpected result from scanner");
1744 : TRUE { $$ = new BoolLiteral (true); }
1745 | FALSE { $$ = new BoolLiteral (false); }
1748 parenthesized_expression
1749 : OPEN_PARENS expression CLOSE_PARENS
1754 : primary_expression DOT IDENTIFIER
1756 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1758 | predefined_type DOT IDENTIFIER
1760 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1768 invocation_expression
1769 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1772 Location l = lexer.Location;
1773 Report.Error (1, l, "THIS IS CRAZY");
1775 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1781 : /* empty */ { $$ = null; }
1788 ArrayList list = new ArrayList ();
1792 | argument_list COMMA argument
1794 ArrayList list = (ArrayList) $1;
1803 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1805 | REF variable_reference
1807 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1809 | OUT variable_reference
1811 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1816 : expression { note ("section 5.4"); $$ = $1; }
1820 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1822 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1824 | primary_expression rank_specifiers
1826 // So the super-trick is that primary_expression
1827 // can only be either a SimpleName or a MemberAccess.
1828 // The MemberAccess case arises when you have a fully qualified type-name like :
1830 // SimpleName is when you have
1833 Expression expr = (Expression) $1;
1834 if (!(expr is SimpleName || expr is MemberAccess)) {
1835 Location l = lexer.Location;
1836 Report.Error (-1, l, "Invalid Type definition");
1837 $$ = "System.Object";
1841 // So we extract the string corresponding to the SimpleName
1844 $$ = new SimpleName (GetQualifiedIdentifier (expr) + (string) $2, lexer.Location);
1851 ArrayList list = new ArrayList ();
1855 | expression_list COMMA expression
1857 ArrayList list = (ArrayList) $1;
1866 $$ = new This (lexer.Location);
1871 : BASE DOT IDENTIFIER
1873 $$ = new BaseAccess ((string) $3, lexer.Location);
1875 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1877 $$ = new BaseIndexerAccess ((ArrayList) $3);
1881 post_increment_expression
1882 : primary_expression OP_INC
1884 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
1885 (Expression) $1, lexer.Location);
1889 post_decrement_expression
1890 : primary_expression OP_DEC
1892 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
1893 (Expression) $1, lexer.Location);
1898 : object_or_delegate_creation_expression
1899 | array_creation_expression
1902 object_or_delegate_creation_expression
1903 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1905 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1909 array_creation_expression
1910 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
1912 opt_array_initializer
1914 $$ = new ArrayCreation ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7,
1917 | NEW type rank_specifiers array_initializer
1919 $$ = new ArrayCreation ((string) $2, (string) $3, (ArrayList) $4, lexer.Location);
1939 | rank_specifiers rank_specifier
1941 $$ = (string) $2 + (string) $1;
1946 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1948 $$ = "[" + (string) $2 + "]";
1968 | dim_separators COMMA
1970 $$ = (string) $1 + ",";
1974 opt_array_initializer
1986 : OPEN_BRACE CLOSE_BRACE
1988 ArrayList list = new ArrayList ();
1991 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1993 $$ = (ArrayList) $2;
1997 variable_initializer_list
1998 : variable_initializer
2000 ArrayList list = new ArrayList ();
2004 | variable_initializer_list COMMA variable_initializer
2006 ArrayList list = (ArrayList) $1;
2013 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2015 $$ = new TypeOf ((string) $3);
2020 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2021 $$ = new SizeOf ((string) $3);
2023 note ("Verify type is unmanaged");
2024 note ("if (5.8) builtin, yield constant expression");
2029 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2031 $$ = new CheckedExpr ((Expression) $3);
2035 unchecked_expression
2036 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2038 $$ = new UnCheckedExpr ((Expression) $3);
2043 : primary_expression
2044 | BANG prefixed_unary_expression
2046 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2048 | TILDE prefixed_unary_expression
2050 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2052 | OPEN_PARENS expression CLOSE_PARENS unary_expression
2054 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2056 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2058 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2063 // The idea to split this out is from Rhys' grammar
2064 // to solve the problem with casts.
2066 prefixed_unary_expression
2068 | PLUS prefixed_unary_expression
2070 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2072 | MINUS prefixed_unary_expression
2074 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2076 | OP_INC prefixed_unary_expression
2078 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2079 (Expression) $2, lexer.Location);
2081 | OP_DEC prefixed_unary_expression
2083 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2084 (Expression) $2, lexer.Location);
2086 | STAR prefixed_unary_expression
2088 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2090 | BITWISE_AND prefixed_unary_expression
2092 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2096 pre_increment_expression
2097 : OP_INC prefixed_unary_expression
2099 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2100 (Expression) $2, lexer.Location);
2104 pre_decrement_expression
2105 : OP_DEC prefixed_unary_expression
2107 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2108 (Expression) $2, lexer.Location);
2112 multiplicative_expression
2113 : prefixed_unary_expression
2114 | multiplicative_expression STAR prefixed_unary_expression
2116 $$ = new Binary (Binary.Operator.Multiply,
2117 (Expression) $1, (Expression) $3, lexer.Location);
2119 | multiplicative_expression DIV prefixed_unary_expression
2121 $$ = new Binary (Binary.Operator.Division,
2122 (Expression) $1, (Expression) $3, lexer.Location);
2124 | multiplicative_expression PERCENT prefixed_unary_expression
2126 $$ = new Binary (Binary.Operator.Modulus,
2127 (Expression) $1, (Expression) $3, lexer.Location);
2132 : multiplicative_expression
2133 | additive_expression PLUS multiplicative_expression
2135 $$ = new Binary (Binary.Operator.Addition,
2136 (Expression) $1, (Expression) $3, lexer.Location);
2138 | additive_expression MINUS multiplicative_expression
2140 $$ = new Binary (Binary.Operator.Subtraction,
2141 (Expression) $1, (Expression) $3, lexer.Location);
2146 : additive_expression
2147 | shift_expression OP_SHIFT_LEFT additive_expression
2149 $$ = new Binary (Binary.Operator.LeftShift,
2150 (Expression) $1, (Expression) $3, lexer.Location);
2152 | shift_expression OP_SHIFT_RIGHT additive_expression
2154 $$ = new Binary (Binary.Operator.RightShift,
2155 (Expression) $1, (Expression) $3, lexer.Location);
2159 relational_expression
2161 | relational_expression OP_LT shift_expression
2163 $$ = new Binary (Binary.Operator.LessThan,
2164 (Expression) $1, (Expression) $3, lexer.Location);
2166 | relational_expression OP_GT shift_expression
2168 $$ = new Binary (Binary.Operator.GreaterThan,
2169 (Expression) $1, (Expression) $3, lexer.Location);
2171 | relational_expression OP_LE shift_expression
2173 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2174 (Expression) $1, (Expression) $3, lexer.Location);
2176 | relational_expression OP_GE shift_expression
2178 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2179 (Expression) $1, (Expression) $3, lexer.Location);
2181 | relational_expression IS type
2183 $$ = new Is ((Expression) $1, (string) $3);
2185 | relational_expression AS type
2187 $$ = new As ((Expression) $1, (string) $3);
2192 : relational_expression
2193 | equality_expression OP_EQ relational_expression
2195 $$ = new Binary (Binary.Operator.Equality,
2196 (Expression) $1, (Expression) $3, lexer.Location);
2198 | equality_expression OP_NE relational_expression
2200 $$ = new Binary (Binary.Operator.Inequality,
2201 (Expression) $1, (Expression) $3, lexer.Location);
2206 : equality_expression
2207 | and_expression BITWISE_AND equality_expression
2209 $$ = new Binary (Binary.Operator.BitwiseAnd,
2210 (Expression) $1, (Expression) $3, lexer.Location);
2214 exclusive_or_expression
2216 | exclusive_or_expression CARRET and_expression
2218 $$ = new Binary (Binary.Operator.ExclusiveOr,
2219 (Expression) $1, (Expression) $3, lexer.Location);
2223 inclusive_or_expression
2224 : exclusive_or_expression
2225 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2227 $$ = new Binary (Binary.Operator.BitwiseOr,
2228 (Expression) $1, (Expression) $3, lexer.Location);
2232 conditional_and_expression
2233 : inclusive_or_expression
2234 | conditional_and_expression OP_AND inclusive_or_expression
2236 $$ = new Binary (Binary.Operator.LogicalAnd,
2237 (Expression) $1, (Expression) $3, lexer.Location);
2241 conditional_or_expression
2242 : conditional_and_expression
2243 | conditional_or_expression OP_OR conditional_and_expression
2245 $$ = new Binary (Binary.Operator.LogicalOr,
2246 (Expression) $1, (Expression) $3, lexer.Location);
2250 conditional_expression
2251 : conditional_or_expression
2252 | conditional_or_expression INTERR expression COLON expression
2254 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2258 assignment_expression
2259 : prefixed_unary_expression ASSIGN expression
2261 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2263 | prefixed_unary_expression OP_MULT_ASSIGN expression
2265 Location l = lexer.Location;
2267 $$ = new Assign ((Expression) $1,
2268 new Binary (Binary.Operator.Multiply,
2270 (Expression) $3, l), l);
2272 | prefixed_unary_expression OP_DIV_ASSIGN expression
2274 Location l = lexer.Location;
2276 $$ = new Assign ((Expression) $1,
2277 new Binary (Binary.Operator.Division,
2279 (Expression) $3, l), l);
2281 | prefixed_unary_expression OP_MOD_ASSIGN expression
2283 Location l = lexer.Location;
2285 $$ = new Assign ((Expression) $1,
2286 new Binary (Binary.Operator.Modulus,
2288 (Expression) $3, l), l);
2290 | prefixed_unary_expression OP_ADD_ASSIGN expression
2292 Location l = lexer.Location;
2294 $$ = new Assign ((Expression) $1,
2295 new Binary (Binary.Operator.Addition,
2297 (Expression) $3, l), l);
2299 | prefixed_unary_expression OP_SUB_ASSIGN expression
2301 Location l = lexer.Location;
2303 $$ = new Assign ((Expression) $1,
2304 new Binary (Binary.Operator.Subtraction,
2306 (Expression) $3, l), l);
2308 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2310 Location l = lexer.Location;
2312 $$ = new Assign ((Expression) $1,
2313 new Binary (Binary.Operator.LeftShift,
2315 (Expression) $3, l), l);
2317 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2319 Location l = lexer.Location;
2321 $$ = new Assign ((Expression) $1,
2322 new Binary (Binary.Operator.RightShift,
2324 (Expression) $3, l), l);
2326 | prefixed_unary_expression OP_AND_ASSIGN expression
2328 Location l = lexer.Location;
2330 $$ = new Assign ((Expression) $1,
2331 new Binary (Binary.Operator.BitwiseAnd,
2333 (Expression) $3, l), l);
2335 | prefixed_unary_expression OP_OR_ASSIGN expression
2337 Location l = lexer.Location;
2339 $$ = new Assign ((Expression) $1,
2340 new Binary (Binary.Operator.BitwiseOr,
2342 (Expression) $3, l), l);
2344 | prefixed_unary_expression OP_XOR_ASSIGN expression
2346 Location l = lexer.Location;
2348 $$ = new Assign ((Expression) $1,
2349 new Binary (Binary.Operator.ExclusiveOr,
2351 (Expression) $3, l), l);
2356 : conditional_expression
2357 | assignment_expression
2377 string full_class_name = MakeName ((string) $4);
2379 new_class = new Class (current_container, full_class_name, (int) $2,
2380 (Attributes) $1, lexer.Location);
2381 current_container = new_class;
2382 current_container.Namespace = current_namespace;
2383 RootContext.Tree.RecordClass (full_class_name, new_class);
2389 Class new_class = (Class) current_container;
2392 new_class.Bases = (ArrayList) $6;
2394 current_container = current_container.Parent;
2395 CheckDef (current_container.AddClass (new_class), new_class.Name);
2402 : /* empty */ { $$ = (int) 0; }
2408 | modifiers modifier
2413 if ((m1 & m2) != 0) {
2414 Location l = lexer.Location;
2415 Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2417 $$ = (int) (m1 | m2);
2422 : NEW { $$ = Modifiers.NEW; }
2423 | PUBLIC { $$ = Modifiers.PUBLIC; }
2424 | PROTECTED { $$ = Modifiers.PROTECTED; }
2425 | INTERNAL { $$ = Modifiers.INTERNAL; }
2426 | PRIVATE { $$ = Modifiers.PRIVATE; }
2427 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2428 | SEALED { $$ = Modifiers.SEALED; }
2429 | STATIC { $$ = Modifiers.STATIC; }
2430 | READONLY { $$ = Modifiers.READONLY; }
2431 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2432 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2433 | EXTERN { $$ = Modifiers.EXTERN; }
2437 : /* empty */ { $$ = null; }
2438 | class_base { $$ = $1; }
2442 : COLON type_list { $$ = $2; }
2450 // A block is "contained" on the following places:
2452 // property_declaration as part of the accessor body (get/set)
2453 // operator_declaration
2454 // constructor_declaration
2455 // destructor_declaration
2456 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2461 current_block = new Block (current_block);
2463 opt_statement_list CLOSE_BRACE
2465 while (current_block.Implicit)
2466 current_block = current_block.Parent;
2468 current_block = current_block.Parent;
2479 | statement_list statement
2483 : declaration_statement
2485 if ((Block) $1 != current_block){
2486 current_block.AddStatement ((Statement) $1);
2487 current_block = (Block) $1;
2490 | embedded_statement
2492 Statement s = (Statement) $1;
2495 current_block.AddStatement ((Statement) $1);
2499 current_block.AddStatement ((Statement) $1);
2506 | expression_statement
2507 | selection_statement
2508 | iteration_statement
2512 | unchecked_statement
2520 $$ = new EmptyStatement ();
2525 : IDENTIFIER COLON statement
2527 string lab = (String) $1;
2530 block = new Block (current_block, lab);
2531 block.AddStatement ((Statement) $3);
2534 if (!current_block.AddLabel (lab, block)){
2535 Location l = lexer.Location;
2536 Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2542 declaration_statement
2543 : local_variable_declaration SEMICOLON
2545 DictionaryEntry de = (DictionaryEntry) $1;
2547 $$ = declare_local_variables ((string) de.Key, (ArrayList) de.Value, lexer.Location);
2550 | local_constant_declaration SEMICOLON
2552 DictionaryEntry de = (DictionaryEntry) $1;
2554 $$ = declare_local_constant ((string) de.Key, (VariableDeclaration) de.Value);
2559 * The following is from Rhys' grammar:
2560 * > Types in local variable declarations must be recognized as
2561 * > expressions to prevent reduce/reduce errors in the grammar.
2562 * > The expressions are converted into types during semantic analysis.
2565 : primary_expression opt_rank_specifier
2567 // FIXME: Do something smart here regarding the composition of the type.
2569 // Ok, the above "primary_expression" is there to get rid of
2570 // both reduce/reduce and shift/reduces in the grammar, it should
2571 // really just be "type_name". If you use type_name, a reduce/reduce
2572 // creeps up. If you use qualified_identifier (which is all we need
2573 // really) two shift/reduces appear.
2576 // So the super-trick is that primary_expression
2577 // can only be either a SimpleName or a MemberAccess.
2578 // The MemberAccess case arises when you have a fully qualified type-name like :
2580 // SimpleName is when you have
2583 Expression expr = (Expression) $1;
2584 if (!(expr is SimpleName || expr is MemberAccess)) {
2585 Location l = lexer.Location;
2586 Report.Error (-1, l, "Invalid Type definition");
2587 $$ = "System.Object";
2591 // So we extract the string corresponding to the SimpleName
2594 $$ = GetQualifiedIdentifier (expr) + (string) $2;
2596 | builtin_types opt_rank_specifier
2598 $$ = (string) $1 + (string) $2;
2602 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2605 // // FIXME: this is a string that represents the type
2606 // // Figure out something to make this work.
2611 local_variable_declaration
2612 : local_variable_type variable_declarators
2614 $$ = new DictionaryEntry ($1, $2);
2618 local_constant_declaration
2619 : CONST local_variable_type constant_declarator
2621 $$ = new DictionaryEntry ($2, $3);
2625 expression_statement
2626 : statement_expression SEMICOLON
2633 // We have to do the wrapping here and not in the case above,
2634 // because statement_expression is used for example in for_statement
2636 statement_expression
2637 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2638 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2639 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2640 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2641 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2642 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2643 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2645 Report.Error (1002, lexer.Location, "Expecting `;'");
2649 object_creation_expression
2650 : object_or_delegate_creation_expression
2651 { note ("complain if this is a delegate maybe?"); }
2660 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2663 $$ = new If ((Expression) $3, (Statement) $5);
2665 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2666 embedded_statement ELSE embedded_statement
2668 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2673 : SWITCH OPEN_PARENS
2675 oob_stack.Push (lexer.Location);
2677 expression CLOSE_PARENS
2680 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2694 : /* empty */ { $$ = new ArrayList (); }
2701 ArrayList sections = new ArrayList ();
2706 | switch_sections switch_section
2708 ArrayList sections = (ArrayList) $1;
2718 current_block = new Block (current_block);
2722 while (current_block.Implicit)
2723 current_block = current_block.Parent;
2724 $$ = new SwitchSection ((ArrayList) $1, current_block);
2725 current_block = current_block.Parent;
2732 ArrayList labels = new ArrayList ();
2737 | switch_labels switch_label
2739 ArrayList labels = (ArrayList) ($1);
2747 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
2748 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
2759 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2761 $$ = new While ((Expression) $3, (Statement) $5);
2766 : DO embedded_statement
2767 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2769 $$ = new Do ((Statement) $2, (Expression) $5);
2775 opt_for_initializer SEMICOLON
2777 Block assign_block = new Block (current_block);
2778 current_block = assign_block;
2780 if ($3 is DictionaryEntry){
2781 DictionaryEntry de = (DictionaryEntry) $3;
2783 string type = (string) de.Key;
2784 ArrayList var_declarators = (ArrayList) de.Value;
2786 foreach (VariableDeclaration decl in var_declarators){
2787 if (!current_block.AddVariable (type, decl.identifier, decl.Location)){
2788 Report.Error (128, decl.Location,
2789 "A local variable `" + decl.identifier + "' is already" +
2790 "defined in this scope");
2793 Location l = lexer.Location;
2795 foreach (VariableDeclaration decl in var_declarators){
2798 if (decl.expression_or_array_initializer is Expression){
2799 expr = (Expression) decl.expression_or_array_initializer;
2803 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
2805 string base_type = type.Substring (0, type.IndexOf ("["));
2806 string rank = type.Substring (type.IndexOf ("["));
2808 expr = new ArrayCreation (base_type, rank, init, decl.Location);
2811 LocalVariableReference var;
2812 var = new LocalVariableReference (
2813 assign_block, decl.identifier, l);
2815 Assign a = new Assign (var, expr, decl.Location);
2817 assign_block.AddStatement (new StatementExpression (a));
2822 opt_for_condition SEMICOLON
2823 opt_for_iterator CLOSE_PARENS
2826 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10);
2828 current_block.AddStatement (f);
2829 while (current_block.Implicit)
2830 current_block = current_block.Parent;
2832 current_block = current_block.Parent;
2837 : /* empty */ { $$ = new EmptyStatement (); }
2842 : local_variable_declaration
2843 | statement_expression_list
2847 : /* empty */ { $$ = new BoolLiteral (true); }
2848 | boolean_expression
2852 : /* empty */ { $$ = new EmptyStatement (); }
2857 : statement_expression_list
2860 statement_expression_list
2861 : statement_expression
2863 Block b = new Block (null, true);
2865 b.AddStatement ((Statement) $1);
2868 | statement_expression_list COMMA statement_expression
2870 Block b = (Block) $1;
2872 b.AddStatement ((Statement) $3);
2878 : FOREACH OPEN_PARENS type IDENTIFIER IN
2880 oob_stack.Push (lexer.Location);
2882 expression CLOSE_PARENS
2884 Block foreach_block = new Block (current_block, true);
2885 LocalVariableReference v;
2886 Location l = lexer.Location;
2888 foreach_block.AddVariable ((string) $3, (string) $4, l);
2889 v = new LocalVariableReference (foreach_block, (string) $4, l);
2891 current_block.AddStatement (foreach_block);
2892 current_block = foreach_block;
2894 oob_stack.Push (foreach_block);
2899 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
2900 Block foreach_block = (Block) oob_stack.Pop ();
2901 Location l = (Location) oob_stack.Pop ();
2903 $$ = new Foreach ((string) $3, v, (Expression) $7, (Statement) $10, l);
2909 | continue_statement
2918 $$ = new Break (lexer.Location);
2923 : CONTINUE SEMICOLON
2925 $$ = new Continue (lexer.Location);
2930 : GOTO IDENTIFIER SEMICOLON
2932 $$ = new Goto ((string) $2, lexer.Location);
2934 | GOTO CASE constant_expression SEMICOLON
2936 $$ = new GotoCase ((Expression) $3, lexer.Location);
2938 | GOTO DEFAULT SEMICOLON
2940 $$ = new GotoDefault (lexer.Location);
2945 : RETURN opt_expression SEMICOLON
2947 $$ = new Return ((Expression) $2, lexer.Location);
2952 : THROW opt_expression SEMICOLON
2954 $$ = new Throw ((Expression) $2);
2964 : TRY block catch_clauses
2967 ArrayList s = new ArrayList ();
2969 foreach (Catch cc in (ArrayList) $3) {
2970 if (cc.Type == null)
2976 // Now s contains the list of specific catch clauses
2977 // and g contains the general one.
2979 $$ = new Try ((Block) $2, s, g, null);
2981 | TRY block opt_catch_clauses FINALLY block
2984 ArrayList s = new ArrayList ();
2986 foreach (Catch cc in (ArrayList) $3) {
2987 if (cc.Type == null)
2993 $$ = new Try ((Block) $2, s, g, (Block) $5);
2998 : /* empty */ { $$ = null; }
3005 ArrayList l = new ArrayList ();
3010 | catch_clauses catch_clause
3012 ArrayList l = (ArrayList) $1;
3020 : /* empty */ { $$ = null; }
3025 : CATCH opt_catch_args
3027 string type = null, id = null;
3030 DictionaryEntry cc = (DictionaryEntry) $2;
3031 type = (string) cc.Key;
3032 id = (string) cc.Value;
3035 ArrayList one = new ArrayList ();
3036 Location loc = lexer.Location;
3038 one.Add (new VariableDeclaration (id, null, loc));
3041 current_block = new Block (current_block);
3042 Block b = declare_local_variables (type, one, loc);
3049 string type = null, id = null;
3052 DictionaryEntry cc = (DictionaryEntry) $2;
3053 type = (string) cc.Key;
3054 id = (string) cc.Value;
3057 while (current_block != (Block) $1)
3058 current_block = current_block.Parent;
3063 $$ = new Catch (type, id , (Block) $4);
3068 : /* empty */ { $$ = null; }
3073 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3075 $$ = new DictionaryEntry ($2, $3);
3082 $$ = new Checked ((Block) $2);
3089 $$ = new Unchecked ((Block) $2);
3094 : LOCK OPEN_PARENS expression CLOSE_PARENS
3100 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3105 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3107 Block assign_block = new Block (current_block);
3108 current_block = assign_block;
3110 oob_stack.Push (lexer.Location);
3112 if ($3 is DictionaryEntry){
3113 DictionaryEntry de = (DictionaryEntry) $3;
3114 Location l = lexer.Location;
3116 string type = (string) de.Key;
3117 ArrayList var_declarators = (ArrayList) de.Value;
3119 foreach (VariableDeclaration decl in var_declarators){
3120 if (!current_block.AddVariable (type, decl.identifier, decl.Location)){
3121 Report.Error (128, decl.Location,
3122 "A local variable `" + decl.identifier + "' is already" +
3123 "defined in this scope");
3127 ArrayList vars = new ArrayList ();
3129 foreach (VariableDeclaration decl in var_declarators){
3132 if (decl.expression_or_array_initializer is Expression){
3133 expr = (Expression) decl.expression_or_array_initializer;
3137 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3139 string base_type = type.Substring (0, type.IndexOf ("["));
3140 string rank = type.Substring (type.IndexOf ("["));
3142 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3145 LocalVariableReference var;
3148 var = new LocalVariableReference (assign_block, decl.identifier, l);
3149 vi = var.VariableInfo;
3152 // This is so that it is not a warning on using variables
3155 vars.Add (new DictionaryEntry (var, expr));
3157 // Assign a = new Assign (var, expr, decl.Location);
3158 // assign_block.AddStatement (new StatementExpression (a));
3160 $3 = new DictionaryEntry (type, vars);
3165 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3166 current_block.AddStatement (u);
3167 while (current_block.Implicit)
3168 current_block = current_block.Parent;
3170 current_block = current_block.Parent;
3174 resource_acquisition
3175 : local_variable_declaration
3182 // A class used to pass around variable declarations and constants
3184 public class VariableDeclaration {
3185 public string identifier;
3186 public object expression_or_array_initializer;
3187 public Location Location;
3189 public VariableDeclaration (string id, object eoai, Location l){
3190 this.identifier = id;
3191 this.expression_or_array_initializer = eoai;
3197 // A class used to hold info about an indexer declarator
3200 public class IndexerDeclaration {
3202 public string interface_type;
3203 public Parameters param_list;
3205 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
3208 this.interface_type = interface_type;
3209 this.param_list = param_list;
3214 // A class used to hold info about an operator declarator
3217 public class OperatorDeclaration {
3218 public Operator.OpType optype;
3219 public string ret_type;
3220 public string arg1type;
3221 public string arg1name;
3222 public string arg2type;
3223 public string arg2name;
3224 public Location location;
3226 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
3227 string arg2type, string arg2name, Location location)
3230 this.ret_type = ret_type;
3231 this.arg1type = arg1type;
3232 this.arg1name = arg1name;
3233 this.arg2type = arg2type;
3234 this.arg2name = arg2name;
3235 this.location = location;
3241 // Given the @class_name name, it creates a fully qualified name
3242 // based on the containing declaration space
3245 MakeName (string class_name)
3247 string ns = current_namespace.Name;
3248 string container_name = current_container.Name;
3250 if (container_name == ""){
3252 return ns + "." + class_name;
3256 return container_name + "." + class_name;
3260 // Used to report back to the user the result of a declaration
3261 // in the current declaration space
3264 CheckDef (DeclSpace.AdditionResult result, string name)
3266 if (result == DeclSpace.AdditionResult.Success)
3269 Location l = lexer.Location;
3272 case DeclSpace.AdditionResult.NameExists:
3273 Report.Error (102, l, "The namespace `" + current_container.Name +
3274 "' already contains a definition for `"+
3279 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
3281 // case DeclSpace.AdditionResult.MethodDuplicated:
3282 // error (111, "Class `"+current_container.Name+
3283 // "' already defines a member called '" +
3284 // name + "' with the same parameter types");
3287 case DeclSpace.AdditionResult.EnclosingClash:
3288 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3291 case DeclSpace.AdditionResult.NotAConstructor:
3292 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3298 CheckDef (bool result, string name)
3302 CheckDef (DeclSpace.AdditionResult.NameExists, name);
3306 SimpleLookup (string name, Location loc)
3309 // we need to check against current_block not being null
3310 // as `expression' is allowed in argument_lists, which
3311 // do not exist inside a block.
3313 if (current_block != null){
3314 if (current_block.IsVariableDefined (name))
3315 return new LocalVariableReference (current_block, name, loc);
3318 if (current_local_parameters != null){
3320 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
3322 return new ParameterReference (current_local_parameters, idx, name);
3328 Expression DecomposeQI (string name, Location loc)
3332 if (name.IndexOf ('.') == -1){
3333 o = SimpleLookup (name, loc);
3335 return new SimpleName (name, loc);
3338 int pos = name.LastIndexOf (".");
3339 string left = name.Substring (0, pos);
3340 string right = name.Substring (pos + 1);
3342 o = DecomposeQI (left, loc);
3343 return new MemberAccess (o, right, loc);
3348 // This method is used to get at the complete string representation of
3349 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3350 // This is necessary because local_variable_type admits primary_expression
3351 // as the type of the variable. So we do some extra checking
3353 string GetQualifiedIdentifier (Expression expr)
3355 if (expr is SimpleName)
3356 return ((SimpleName)expr).Name;
3357 else if (expr is MemberAccess)
3358 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3360 throw new Exception ("Expr has to be either SimpleName or MemberAccess! (" + expr + ")");
3364 Block declare_local_variables (string type, ArrayList variable_declarators, Location loc)
3366 Block implicit_block;
3367 ArrayList inits = null;
3370 // We use the `Used' property to check whether statements
3371 // have been added to the current block. If so, we need
3372 // to create another block to contain the new declaration
3373 // otherwise, as an optimization, we use the same block to
3374 // add the declaration.
3376 // FIXME: A further optimization is to check if the statements
3377 // that were added were added as part of the initialization
3378 // below. In which case, no other statements have been executed
3379 // and we might be able to reduce the number of blocks for
3380 // situations like this:
3382 // int j = 1; int k = j + 1;
3384 if (current_block.Used)
3385 implicit_block = new Block (current_block, true);
3387 implicit_block = current_block;
3389 foreach (VariableDeclaration decl in variable_declarators){
3390 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3391 if (decl.expression_or_array_initializer != null){
3393 inits = new ArrayList ();
3397 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3398 "' is already defined in this scope");
3403 return implicit_block;
3405 foreach (VariableDeclaration decl in inits){
3409 if (decl.expression_or_array_initializer is Expression){
3410 expr = (Expression) decl.expression_or_array_initializer;
3413 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3415 string base_type = type.Substring (0, type.IndexOf ("["));
3416 string rank = type.Substring (type.IndexOf ("["));
3418 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3422 LocalVariableReference var;
3423 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3425 assign = new Assign (var, expr, decl.Location);
3427 implicit_block.AddStatement (new StatementExpression (assign));
3430 return implicit_block;
3433 Block declare_local_constant (string type, VariableDeclaration decl)
3435 Block implicit_block;
3437 if (current_block.Used)
3438 implicit_block = new Block (current_block, true);
3440 implicit_block = current_block;
3442 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3444 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3445 "' is already defined in this scope");
3448 return implicit_block;
3451 void CheckAttributeTarget (string a)
3455 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3459 Location l = lexer.Location;
3460 Report.Error (658, l, "Invalid attribute target");
3466 void CheckUnaryOperator (Operator.OpType op)
3470 case Operator.OpType.LogicalNot:
3471 case Operator.OpType.OnesComplement:
3472 case Operator.OpType.Increment:
3473 case Operator.OpType.Decrement:
3474 case Operator.OpType.True:
3475 case Operator.OpType.False:
3476 case Operator.OpType.Addition:
3477 case Operator.OpType.Subtraction:
3482 Location l = lexer.Location;
3483 Report.Error (1019, l, "Overloadable unary operator expected");
3489 void CheckBinaryOperator (Operator.OpType op)
3493 case Operator.OpType.Addition:
3494 case Operator.OpType.Subtraction:
3495 case Operator.OpType.Multiply:
3496 case Operator.OpType.Division:
3497 case Operator.OpType.Modulus:
3498 case Operator.OpType.BitwiseAnd:
3499 case Operator.OpType.BitwiseOr:
3500 case Operator.OpType.ExclusiveOr:
3501 case Operator.OpType.LeftShift:
3502 case Operator.OpType.RightShift:
3503 case Operator.OpType.Equality:
3504 case Operator.OpType.Inequality:
3505 case Operator.OpType.GreaterThan:
3506 case Operator.OpType.LessThan:
3507 case Operator.OpType.GreaterThanOrEqual:
3508 case Operator.OpType.LessThanOrEqual:
3512 Location l = lexer.Location;
3513 Report.Error (1020, l, "Overloadable binary operator expected");
3519 void syntax_error (Location l, string msg)
3521 Report.Error (1003, l, "Syntax error, " + msg);
3524 void output (string s)
3526 Console.WriteLine (s);
3529 void note (string s)
3531 // Used to put annotations
3536 public Tokenizer Lexer {
3542 public CSharpParser (string name, System.IO.Stream input)
3544 current_namespace = new Namespace (null, "");
3547 current_container = RootContext.Tree.Types;
3548 current_container.Namespace = current_namespace;
3549 oob_stack = new Stack ();
3551 lexer = new Tokenizer (input, name);
3554 public override int parse ()
3556 StringBuilder value = new StringBuilder ();
3560 if (yacc_verbose_flag)
3561 yyparse (lexer, new yydebug.yyDebugSimple ());
3564 } catch (Exception e){
3565 // Console.WriteLine ("Fatal error: " + name);
3566 // Console.WriteLine (lexer.location);
3569 // Please do not remove this, it is used during debugging
3572 Console.WriteLine (lexer.location + " : Parsing error ");
3573 Console.WriteLine (e);
3577 return global_errors;