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 // Used to record all types defined
81 // An out-of-band stack.
90 %token NONE /* This token is never returned by our lexer */
91 %token ERROR // This is used not by the parser, but by the tokenizer.
95 *These are the C# keywords
175 /* C# keywords which are not really keywords */
179 /* C# single character operators/punctuation. */
180 %token OPEN_BRACE "{"
181 %token CLOSE_BRACE "}"
182 %token OPEN_BRACKET "["
183 %token CLOSE_BRACKET "]"
184 %token OPEN_PARENS "("
185 %token CLOSE_PARENS ")"
198 %token BITWISE_AND "&"
199 %token BITWISE_OR "|"
206 /* C# multi-character operators. */
209 %token OP_SHIFT_LEFT "<<"
210 %token OP_SHIFT_RIGHT ">>"
217 %token OP_MULT_ASSIGN "*="
218 %token OP_DIV_ASSIGN "/="
219 %token OP_MOD_ASSIGN "%="
220 %token OP_ADD_ASSIGN "+="
221 %token OP_SUB_ASSIGN "-="
222 %token OP_SHIFT_LEFT_ASSIGN "<<="
223 %token OP_SHIFT_RIGHT_ASSIGN ">>="
224 %token OP_AND_ASSIGN "&="
225 %token OP_XOR_ASSIGN "^="
226 %token OP_OR_ASSIGN "|="
230 %token LITERAL_INTEGER "int literal"
231 %token LITERAL_FLOAT "float literal"
232 %token LITERAL_DOUBLE "double literal"
233 %token LITERAL_DECIMAL "decimal literal"
234 %token LITERAL_CHARACTER "character literal"
235 %token LITERAL_STRING "string literal"
239 /* Add precedence rules to solve dangling else s/r conflict */
248 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
250 %left STAR DIV PERCENT
251 %right BANG CARRET UMINUS
252 %nonassoc OP_INC OP_DEC
254 %left OPEN_BRACKET OPEN_BRACE
258 %start compilation_unit
259 /*%start namespace_declaration */
263 : opt_using_directives opt_namespace_member_declarations EOF
265 // Check that using comes only before namespace elements
271 | using_directives using_directive
275 : using_alias_directive
276 | using_namespace_directive
279 using_alias_directive
280 : USING IDENTIFIER ASSIGN
281 namespace_or_type_name SEMICOLON
283 // FIXME : Need to implement actual action.
287 using_namespace_directive
288 : USING namespace_name SEMICOLON
290 current_namespace.Using ((string) $2);
294 // namespace_declarations
295 // : namespace_declaration
296 // | namespace_declarations namespace_declaration
298 namespace_declaration
299 : NAMESPACE qualified_identifier
301 current_namespace = tree.RecordNamespace (current_namespace, (string) $2);
303 namespace_body opt_semicolon
305 current_namespace = current_namespace.Parent;
321 | qualified_identifier DOT IDENTIFIER {
322 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
327 : namespace_or_type_name
333 opt_namespace_member_declarations
344 opt_namespace_member_declarations
346 | namespace_member_declarations
349 namespace_member_declarations
350 : namespace_member_declaration
351 | namespace_member_declarations namespace_member_declaration
354 namespace_member_declaration
361 Class c = (Class) $1;
362 mod_flags = c.ModFlags;
364 } else if ($1 is Struct){
365 Struct s = (Struct) $1;
366 mod_flags = s.ModFlags;
372 // We remove this error until we can
373 //if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
374 // error (1527, "Namespace elements cant be explicitly " +
375 // "declared private or protected in `" + name + "'");
378 | namespace_declaration
384 | interface_declaration
386 | delegate_declaration
393 : /* empty */ { $$ = null; }
394 | attribute_section opt_attributes
399 attrs = (Attributes) $2;
400 attrs.AddAttribute ((AttributeSection) $1);
402 attrs = new Attributes ((AttributeSection) $1);
409 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
411 string target = null;
414 target = (string) $2;
416 $$ = new AttributeSection (target, (ArrayList) $3);
418 | OPEN_BRACKET attribute_list CLOSE_BRACKET
420 $$ = new AttributeSection (null, (ArrayList) $2);
424 attribute_target_specifier
425 : attribute_target COLON
434 CheckAttributeTarget ((string) $1);
437 | EVENT { $$ = "event"; }
438 | RETURN { $$ = "return"; }
444 ArrayList attrs = new ArrayList ();
450 | attribute_list COMMA attribute
452 ArrayList attrs = (ArrayList) $1;
464 opt_attribute_arguments
466 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
471 : type_name { /* reserved attribute name or identifier: 17.4 */ }
474 opt_attribute_arguments
475 : /* empty */ { $$ = null; }
476 | OPEN_PARENS attribute_arguments CLOSE_PARENS
484 : positional_argument_list
486 ArrayList args = new ArrayList ();
491 | positional_argument_list COMMA named_argument_list
493 ArrayList args = new ArrayList ();
499 | named_argument_list
501 ArrayList args = new ArrayList ();
510 positional_argument_list
513 ArrayList args = new ArrayList ();
514 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
518 | positional_argument_list COMMA expression
520 ArrayList args = (ArrayList) $1;
521 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
530 ArrayList args = new ArrayList ();
535 | named_argument_list COMMA named_argument
537 ArrayList args = (ArrayList) $1;
545 : IDENTIFIER ASSIGN expression
547 $$ = new DictionaryEntry (
549 new Argument ((Expression) $3, Argument.AType.Expression));
555 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
558 opt_class_member_declarations
560 | class_member_declarations
563 class_member_declarations
564 : class_member_declaration
565 | class_member_declarations
566 class_member_declaration
569 class_member_declaration
570 : constant_declaration // done
571 | field_declaration // done
572 | method_declaration // done
573 | property_declaration // done
574 | event_declaration // done
575 | indexer_declaration // done
576 | operator_declaration // done
577 | constructor_declaration // done
578 | destructor_declaration // done
588 string full_struct_name = MakeName ((string) $4);
590 new_struct = new Struct (rc, current_container, full_struct_name, (int) $2,
591 (Attributes) $1, lexer.Location);
592 current_container = new_struct;
593 current_container.Namespace = current_namespace;
594 tree.RecordStruct (full_struct_name, new_struct);
596 opt_struct_interfaces
600 Struct new_struct = (Struct) current_container;
602 current_container = current_container.Parent;
603 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
608 opt_struct_interfaces
615 | struct_interfaces struct_interface
623 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
626 opt_struct_member_declarations
628 | struct_member_declarations
631 struct_member_declarations
632 : struct_member_declaration
633 | struct_member_declarations struct_member_declaration
636 struct_member_declaration
637 : constant_declaration
640 | property_declaration
642 | indexer_declaration
643 | operator_declaration
644 | constructor_declaration
648 * This is only included so we can flag error 575:
649 * destructors only allowed on class types
651 | destructor_declaration
662 foreach (VariableDeclaration constant in (ArrayList) $5){
663 Constant c = new Constant (
664 (string) $4, (string) constant.identifier,
665 (Expression) constant.expression_or_array_initializer, (int) $2, (Attributes) $1,
668 CheckDef (current_container.AddConstant (c), c.Name);
674 : constant_declarator
676 ArrayList constants = new ArrayList ();
680 | constant_declarators COMMA constant_declarator
682 ArrayList constants = (ArrayList) $1;
689 : IDENTIFIER ASSIGN constant_expression
691 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
702 string type = (string) $3;
705 foreach (VariableDeclaration var in (ArrayList) $4){
706 Field field = new Field (type, mod, var.identifier,
707 var.expression_or_array_initializer, (Attributes) $1, var.Location);
709 CheckDef (current_container.AddField (field), field.Name);
715 : variable_declarator
717 ArrayList decl = new ArrayList ();
721 | variable_declarators COMMA variable_declarator
723 ArrayList decls = (ArrayList) $1;
730 : IDENTIFIER ASSIGN variable_initializer
732 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
736 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
755 Method method = (Method) $1;
756 Block b = (Block) $2;
759 if ((method.ModFlags & (Modifiers.EXTERN | Modifiers.ABSTRACT)) == 0){
761 501, lexer.Location, "`" +
762 current_container.Name + "." + method.Name + "'" +
763 "must declare a body because it is not marked abstract or extern");
767 method.Block = (Block) $2;
768 CheckDef (current_container.AddMethod (method), method.Name);
770 current_local_parameters = null;
779 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
781 Method method = new Method ((string) $3, (int) $2, (string) $4,
782 (Parameters) $6, (Attributes) $1, lexer.Location);
784 current_local_parameters = (Parameters) $6;
792 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
794 Method method = new Method ("System.Void", (int) $2, (string) $4,
795 (Parameters) $6, (Attributes) $1, lexer.Location);
797 current_local_parameters = (Parameters) $6;
804 | SEMICOLON { $$ = null; }
807 opt_formal_parameter_list
808 : /* empty */ { $$ = Parameters.GetEmptyReadOnlyParameters (); }
809 | formal_parameter_list
812 formal_parameter_list
815 ArrayList pars_list = (ArrayList) $1;
817 Parameter [] pars = new Parameter [pars_list.Count];
818 pars_list.CopyTo (pars);
820 $$ = new Parameters (pars, null);
822 | fixed_parameters COMMA parameter_array
824 ArrayList pars_list = (ArrayList) $1;
826 Parameter [] pars = new Parameter [pars_list.Count];
827 pars_list.CopyTo (pars);
829 $$ = new Parameters (pars, (Parameter) $3);
833 $$ = new Parameters (null, (Parameter) $1);
840 ArrayList pars = new ArrayList ();
845 | fixed_parameters COMMA fixed_parameter
847 ArrayList pars = (ArrayList) $1;
856 opt_parameter_modifier
860 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
864 opt_parameter_modifier
865 : /* empty */ { $$ = Parameter.Modifier.NONE; }
870 : REF { $$ = Parameter.Modifier.REF; }
871 | OUT { $$ = Parameter.Modifier.OUT; }
875 : opt_attributes PARAMS type IDENTIFIER
877 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
878 note ("type must be a single-dimension array type");
883 : qualified_identifier
892 implicit_value_parameter_type = (string) $3;
894 lexer.properties = true;
898 accessor_declarations
900 lexer.properties = false;
905 DictionaryEntry pair = (DictionaryEntry) $7;
906 Block get_block = null;
907 Block set_block = null;
909 if (pair.Key != null)
910 get_block = (Block) pair.Key;
911 if (pair.Value != null)
912 set_block = (Block) pair.Value;
914 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block,
915 (Attributes) $1, (Location) $6);
917 CheckDef (current_container.AddProperty (prop), prop.Name);
918 implicit_value_parameter_type = null;
922 accessor_declarations
923 : get_accessor_declaration opt_set_accessor_declaration
925 $$ = new DictionaryEntry ($1, $2);
927 | set_accessor_declaration opt_get_accessor_declaration
929 $$ = new DictionaryEntry ($2, $1);
933 opt_get_accessor_declaration
934 : /* empty */ { $$ = null; }
935 | get_accessor_declaration
938 opt_set_accessor_declaration
939 : /* empty */ { $$ = null; }
940 | set_accessor_declaration
943 get_accessor_declaration
946 // If this is not the case, then current_local_parameters has already
947 // been set in indexer_declaration
948 if (parsing_indexer == false)
949 current_local_parameters = null;
951 current_local_parameters = indexer_parameters;
958 current_local_parameters = null;
962 set_accessor_declaration
966 Parameter implicit_value_parameter = new Parameter (
967 implicit_value_parameter_type, "value",
968 Parameter.Modifier.NONE, null);
970 if (parsing_indexer == false) {
971 args = new Parameter [1];
972 args [0] = implicit_value_parameter;
974 Parameter [] fp = indexer_parameters.FixedParameters;
975 int count = fp.Length;
977 args = new Parameter [count + 1];
980 args [count] = implicit_value_parameter;
982 current_local_parameters = new Parameters (args, null);
987 current_local_parameters = null;
993 | SEMICOLON { $$ = new Block (null); }
996 interface_declaration
1001 Interface new_interface;
1002 string full_interface_name = MakeName ((string) $4);
1004 new_interface = new Interface (rc, current_container, full_interface_name, (int) $2,
1005 (Attributes) $1, lexer.Location);
1006 if (current_interface != null) {
1007 Location l = lexer.Location;
1008 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1010 current_interface = new_interface;
1011 tree.RecordInterface (full_interface_name, new_interface);
1016 Interface new_interface = (Interface) current_interface;
1019 new_interface.Bases = (ArrayList) $6;
1021 current_interface = null;
1022 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
1027 : /* empty */ { $$ = null; }
1032 : COLON interface_type_list { $$ = $2; }
1038 ArrayList interfaces = new ArrayList ();
1040 interfaces.Add ($1);
1043 | interface_type_list COMMA interface_type
1045 ArrayList interfaces = (ArrayList) $1;
1046 interfaces.Add ($3);
1053 opt_interface_member_declarations
1057 opt_interface_member_declarations
1059 | interface_member_declarations
1062 interface_member_declarations
1063 : interface_member_declaration
1064 | interface_member_declarations interface_member_declaration
1067 interface_member_declaration
1068 : interface_method_declaration
1070 InterfaceMethod m = (InterfaceMethod) $1;
1072 CheckDef (current_interface.AddMethod (m), m.Name);
1074 | interface_property_declaration
1076 InterfaceProperty p = (InterfaceProperty) $1;
1078 CheckDef (current_interface.AddProperty (p), p.Name);
1080 | interface_event_declaration
1082 InterfaceEvent e = (InterfaceEvent) $1;
1084 CheckDef (current_interface.AddEvent (e), e.Name);
1086 | interface_indexer_declaration
1088 InterfaceIndexer i = (InterfaceIndexer) $1;
1090 CheckDef (current_interface.AddIndexer (i), "indexer");
1095 : /* empty */ { $$ = false; }
1096 | NEW { $$ = true; }
1099 interface_method_declaration
1100 : opt_attributes opt_new type IDENTIFIER
1101 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1104 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1106 | opt_attributes opt_new VOID IDENTIFIER
1107 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1110 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1114 interface_property_declaration
1119 { lexer.properties = true; }
1121 { lexer.properties = false; }
1126 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
1127 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1);
1132 : opt_attributes GET SEMICOLON { $$ = 1; }
1133 | opt_attributes SET SEMICOLON { $$ = 2; }
1134 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1136 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1140 interface_event_declaration
1141 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1143 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1147 interface_indexer_declaration
1148 : opt_attributes opt_new type THIS
1149 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1151 { lexer.properties = true; }
1153 { lexer.properties = false; }
1156 int a_flags = (int) $10;
1158 bool do_get = (a_flags & 1) == 1;
1159 bool do_set = (a_flags & 2) == 2;
1161 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set, (bool) $2, (Attributes) $1);
1165 operator_declaration
1166 : opt_attributes opt_modifiers operator_declarator block
1168 OperatorDeclaration decl = (OperatorDeclaration) $3;
1170 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1171 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1173 // Note again, checking is done in semantic analysis
1174 current_container.AddOperator (op);
1176 current_local_parameters = null;
1181 : type OPERATOR overloadable_operator
1182 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1184 Operator.OpType op = (Operator.OpType) $3;
1185 CheckUnaryOperator (op);
1187 if (op == Operator.OpType.Addition)
1188 op = Operator.OpType.UnaryPlus;
1190 if (op == Operator.OpType.Subtraction)
1191 op = Operator.OpType.UnaryNegation;
1193 Parameter [] pars = new Parameter [1];
1195 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1197 current_local_parameters = new Parameters (pars, null);
1199 $$ = new OperatorDeclaration (op, (string) $1, (string) $5, (string) $6,
1200 null, null, lexer.Location);
1202 | type OPERATOR overloadable_operator
1204 type IDENTIFIER COMMA
1208 CheckBinaryOperator ((Operator.OpType) $3);
1210 Parameter [] pars = new Parameter [2];
1212 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1213 pars [1] = new Parameter ((string) $8, (string) $9, Parameter.Modifier.NONE, null);
1215 current_local_parameters = new Parameters (pars, null);
1217 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1218 (string) $8, (string) $9, lexer.Location);
1220 | conversion_operator_declarator
1223 overloadable_operator
1225 : BANG { $$ = Operator.OpType.LogicalNot; }
1226 | TILDE { $$ = Operator.OpType.OnesComplement; }
1227 | OP_INC { $$ = Operator.OpType.Increment; }
1228 | OP_DEC { $$ = Operator.OpType.Decrement; }
1229 | TRUE { $$ = Operator.OpType.True; }
1230 | FALSE { $$ = Operator.OpType.False; }
1231 // Unary and binary:
1232 | PLUS { $$ = Operator.OpType.Addition; }
1233 | MINUS { $$ = Operator.OpType.Subtraction; }
1235 | STAR { $$ = Operator.OpType.Multiply; }
1236 | DIV { $$ = Operator.OpType.Division; }
1237 | PERCENT { $$ = Operator.OpType.Modulus; }
1238 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1239 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1240 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1241 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1242 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1243 | OP_EQ { $$ = Operator.OpType.Equality; }
1244 | OP_NE { $$ = Operator.OpType.Inequality; }
1245 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1246 | OP_LT { $$ = Operator.OpType.LessThan; }
1247 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1248 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1251 conversion_operator_declarator
1252 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1254 Parameter [] pars = new Parameter [1];
1256 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1258 current_local_parameters = new Parameters (pars, null);
1260 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1261 null, null, lexer.Location);
1263 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1265 Parameter [] pars = new Parameter [1];
1267 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1269 current_local_parameters = new Parameters (pars, null);
1271 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1272 null, null, lexer.Location);
1276 syntax_error (lexer.Location, "'operator' expected");
1280 syntax_error (lexer.Location, "'operator' expected");
1284 constructor_declaration
1287 constructor_declarator
1290 Constructor c = (Constructor) $3;
1291 c.Block = (Block) $4;
1292 c.ModFlags = (int) $2;
1293 c.OptAttributes = (Attributes) $1;
1295 if ((c.ModFlags & Modifiers.STATIC) != 0){
1296 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1297 Location l = lexer.Location;
1298 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1301 if (c.Initializer != null){
1302 Location l = lexer.Location;
1303 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1306 if (!c.Parameters.Empty){
1307 Location l = lexer.Location;
1308 Report.Error (103, l, "Static constructors should not have parameters");
1312 CheckDef (current_container.AddConstructor (c), c.Name);
1314 current_local_parameters = null;
1318 constructor_declarator
1320 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1321 opt_constructor_initializer
1323 Location l = lexer.Location;
1325 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5, l);
1327 current_local_parameters = (Parameters) $3;
1331 opt_constructor_initializer
1332 : /* empty */ { $$ = null; }
1333 | constructor_initializer
1336 constructor_initializer
1337 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1339 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1341 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1343 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1347 destructor_declaration
1348 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1350 if ((string) $3 != current_container.Name){
1351 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1352 } else if (!(current_container is Class)){
1353 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1355 Method d = new Method (
1356 "System.Void", 0, "Finalize",
1357 new Parameters (null, null), (Attributes) $1, lexer.Location);
1359 d.Block = (Block) $6;
1360 CheckDef (current_container.AddMethod (d), d.Name);
1368 EVENT type variable_declarators SEMICOLON
1370 foreach (VariableDeclaration var in (ArrayList) $5) {
1372 // FIXME : Is this right ?
1373 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1374 (int) $2, null, null, (Attributes) $1, lexer.Location);
1376 CheckDef (current_container.AddEvent (e), e.Name);
1382 EVENT type member_name
1383 OPEN_BRACE event_accessor_declarations CLOSE_BRACE
1385 DictionaryEntry pair = (DictionaryEntry) $7;
1386 Block add_block = null;
1387 Block rem_block = null;
1389 if (pair.Key != null)
1390 add_block = (Block) pair.Key;
1391 if (pair.Value != null)
1392 rem_block = (Block) pair.Value;
1394 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
1395 (Attributes) $1, lexer.Location);
1397 CheckDef (current_container.AddEvent (e), e.Name);
1401 event_accessor_declarations
1402 : add_accessor_declaration remove_accessor_declaration
1404 $$ = new DictionaryEntry ($1, $2);
1406 | remove_accessor_declaration add_accessor_declaration
1408 $$ = new DictionaryEntry ($2, $1);
1412 add_accessor_declaration
1413 : opt_attributes ADD block
1419 remove_accessor_declaration
1420 : opt_attributes REMOVE block
1427 : opt_attributes opt_modifiers indexer_declarator
1430 IndexerDeclaration decl = (IndexerDeclaration) $3;
1432 implicit_value_parameter_type = decl.type;
1434 lexer.properties = true;
1435 parsing_indexer = true;
1437 indexer_parameters = decl.param_list;
1438 $$ = lexer.Location;
1440 accessor_declarations
1442 lexer.properties = false;
1443 parsing_indexer = false;
1447 // The signature is computed from the signature of the indexer. Look
1448 // at section 3.6 on the spec
1451 IndexerDeclaration decl = (IndexerDeclaration) $3;
1452 DictionaryEntry pair = (DictionaryEntry) $6;
1453 Block get_block = null;
1454 Block set_block = null;
1456 if (pair.Key != null)
1457 get_block = (Block) pair.Key;
1458 if (pair.Value != null)
1459 set_block = (Block) pair.Value;
1461 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1462 get_block, set_block, (Attributes) $1, (Location) $5);
1464 // Note that there is no equivalent of CheckDef for this case
1465 // We shall handle this in semantic analysis
1467 current_container.AddIndexer (indexer);
1469 current_local_parameters = null;
1470 implicit_value_parameter_type = null;
1471 indexer_parameters = null;
1476 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1478 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
1480 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1482 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
1494 string name = (string) $4;
1495 Enum e = new Enum (rc, (string) $5, (int) $2, name, (Attributes) $1, lexer.Location);
1497 foreach (VariableDeclaration ev in (ArrayList) $6){
1498 CheckDef (e.AddEnumMember (ev.identifier,
1499 (Expression) ev.expression_or_array_initializer,
1500 (Location) ev.Location),
1504 CheckDef (current_container.AddEnum (e), name);
1509 : /* empty */ { $$ = "System.Int32"; }
1510 | COLON type { $$ = $2; }
1514 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1520 opt_enum_member_declarations
1521 : /* empty */ { $$ = new ArrayList (); }
1522 | enum_member_declarations opt_comma { $$ = $1; }
1525 enum_member_declarations
1526 : enum_member_declaration
1528 ArrayList l = new ArrayList ();
1533 | enum_member_declarations COMMA enum_member_declaration
1535 ArrayList l = (ArrayList) $1;
1543 enum_member_declaration
1544 : opt_attributes IDENTIFIER
1546 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1548 | opt_attributes IDENTIFIER
1550 $$ = lexer.Location;
1554 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location);
1558 delegate_declaration
1562 IDENTIFIER OPEN_PARENS
1563 formal_parameter_list
1567 Delegate del = new Delegate (rc, (string) $4, (int) $2,
1568 MakeName ((string) $5), (Parameters) $7,
1569 (Attributes) $1, lexer.Location);
1571 CheckDef (current_container.AddDelegate (del), del.Name);
1576 IDENTIFIER OPEN_PARENS
1577 formal_parameter_list
1581 Delegate del = new Delegate (rc, "System.Void", (int) $2, (string) $5, (Parameters) $7,
1582 (Attributes) $1, lexer.Location);
1584 CheckDef (current_container.AddDelegate (del), del.Name);
1589 : namespace_or_type_name
1592 namespace_or_type_name
1593 : qualified_identifier
1597 * Before you think of adding a return_type, notice that we have been
1598 * using two rules in the places where it matters (one rule using type
1599 * and another identical one that uses VOID as the return type). This
1600 * gets rid of a shift/reduce couple
1603 : type_name { /* class_type */
1605 This does interfaces, delegates, struct_types, class_types,
1606 parent classes, and more! 4.2
1617 $$ = new SimpleName ((string) $1, lexer.Location);
1619 | non_expression_type rank_specifier
1621 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1623 | expression rank_specifiers
1625 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1632 ArrayList types = new ArrayList ();
1637 | type_list COMMA type
1639 ArrayList types = (ArrayList) $1;
1647 * replaces all the productions for isolating the various
1648 * simple types, but we need this to reuse it easily in local_variable_type
1651 : OBJECT { $$ = "System.Object"; }
1652 | STRING { $$ = "System.String"; }
1653 | BOOL { $$ = "System.Boolean"; }
1654 | DECIMAL { $$ = "System.Decimal"; }
1655 | FLOAT { $$ = "System.Single"; }
1656 | DOUBLE { $$ = "System.Double"; }
1661 : SBYTE { $$ = "System.SByte"; }
1662 | BYTE { $$ = "System.Byte"; }
1663 | SHORT { $$ = "System.Int16"; }
1664 | USHORT { $$ = "System.UInt16"; }
1665 | INT { $$ = "System.Int32"; }
1666 | UINT { $$ = "System.UInt32"; }
1667 | LONG { $$ = "System.Int64"; }
1668 | ULONG { $$ = "System.UInt64"; }
1669 | CHAR { $$ = "System.Char"; }
1677 : type rank_specifiers
1679 $$ = (string) $1 + (string) $2;
1684 // Expressions, section 7.5
1692 | qualified_identifier
1694 string name = (string) $1;
1697 $$ = DecomposeQI (name, lexer.Location);
1699 | parenthesized_expression
1701 | invocation_expression
1705 | post_increment_expression
1706 | post_decrement_expression
1710 | checked_expression
1711 | unchecked_expression
1718 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1719 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1720 | NULL { $$ = NullLiteral.Null; }
1724 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1725 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1726 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1731 object v = lexer.Value;
1734 // FIXME: Possible optimization would be to
1735 // compute the *Literal objects directly in the scanner
1738 $$ = new IntLiteral ((Int32) v);
1740 $$ = new UIntLiteral ((UInt32) v);
1742 $$ = new LongLiteral ((Int64) v);
1743 else if (v is ulong)
1744 $$ = new ULongLiteral ((UInt64) v);
1746 Console.WriteLine ("OOPS. Unexpected result from scanner");
1751 : TRUE { $$ = new BoolLiteral (true); }
1752 | FALSE { $$ = new BoolLiteral (false); }
1755 parenthesized_expression
1756 : OPEN_PARENS expression CLOSE_PARENS
1761 : primary_expression DOT IDENTIFIER
1763 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1765 | predefined_type DOT IDENTIFIER
1767 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1775 invocation_expression
1776 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1779 Location l = lexer.Location;
1780 Report.Error (1, l, "THIS IS CRAZY");
1782 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1788 : /* empty */ { $$ = null; }
1795 ArrayList list = new ArrayList ();
1799 | argument_list COMMA argument
1801 ArrayList list = (ArrayList) $1;
1810 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1812 | REF variable_reference
1814 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1816 | OUT variable_reference
1818 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1823 : expression { note ("section 5.4"); $$ = $1; }
1827 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1829 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1836 ArrayList list = new ArrayList ();
1840 | expression_list COMMA expression
1842 ArrayList list = (ArrayList) $1;
1851 $$ = new This (lexer.Location);
1856 : BASE DOT IDENTIFIER
1858 $$ = new BaseAccess ((string) $3, lexer.Location);
1860 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1862 $$ = new BaseIndexerAccess ((ArrayList) $3);
1866 post_increment_expression
1867 : primary_expression OP_INC
1869 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
1870 (Expression) $1, lexer.Location);
1874 post_decrement_expression
1875 : primary_expression OP_DEC
1877 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
1878 (Expression) $1, lexer.Location);
1883 : object_or_delegate_creation_expression
1884 | array_creation_expression
1887 object_or_delegate_creation_expression
1888 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1890 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1894 array_creation_expression
1895 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
1897 opt_array_initializer
1899 $$ = new ArrayCreation ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7,
1902 | NEW type rank_specifiers array_initializer
1904 $$ = new ArrayCreation ((string) $2, (string) $3, (ArrayList) $4, lexer.Location);
1924 | rank_specifiers rank_specifier
1926 $$ = (string) $2 + (string) $1;
1931 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1933 $$ = "[" + (string) $2 + "]";
1953 | dim_separators COMMA
1955 $$ = (string) $1 + ",";
1959 opt_array_initializer
1971 : OPEN_BRACE CLOSE_BRACE
1973 ArrayList list = new ArrayList ();
1976 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1978 $$ = (ArrayList) $2;
1982 variable_initializer_list
1983 : variable_initializer
1985 ArrayList list = new ArrayList ();
1989 | variable_initializer_list COMMA variable_initializer
1991 ArrayList list = (ArrayList) $1;
1998 : TYPEOF OPEN_PARENS type CLOSE_PARENS
2000 $$ = new TypeOf ((string) $3);
2005 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2006 $$ = new SizeOf ((string) $3);
2008 note ("Verify type is unmanaged");
2009 note ("if (5.8) builtin, yield constant expression");
2014 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2016 $$ = new CheckedExpr ((Expression) $3);
2020 unchecked_expression
2021 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2023 $$ = new UnCheckedExpr ((Expression) $3);
2028 : primary_expression
2029 | BANG prefixed_unary_expression
2031 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2033 | TILDE prefixed_unary_expression
2035 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2037 | OPEN_PARENS expression CLOSE_PARENS unary_expression
2039 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2041 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2043 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2048 // The idea to split this out is from Rhys' grammar
2049 // to solve the problem with casts.
2051 prefixed_unary_expression
2053 | PLUS prefixed_unary_expression
2055 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2057 | MINUS prefixed_unary_expression
2059 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2061 | OP_INC prefixed_unary_expression
2063 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2064 (Expression) $2, lexer.Location);
2066 | OP_DEC prefixed_unary_expression
2068 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2069 (Expression) $2, lexer.Location);
2071 | STAR prefixed_unary_expression
2073 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2075 | BITWISE_AND prefixed_unary_expression
2077 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2081 pre_increment_expression
2082 : OP_INC prefixed_unary_expression
2084 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2085 (Expression) $2, lexer.Location);
2089 pre_decrement_expression
2090 : OP_DEC prefixed_unary_expression
2092 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2093 (Expression) $2, lexer.Location);
2097 multiplicative_expression
2098 : prefixed_unary_expression
2099 | multiplicative_expression STAR prefixed_unary_expression
2101 $$ = new Binary (Binary.Operator.Multiply,
2102 (Expression) $1, (Expression) $3, lexer.Location);
2104 | multiplicative_expression DIV prefixed_unary_expression
2106 $$ = new Binary (Binary.Operator.Division,
2107 (Expression) $1, (Expression) $3, lexer.Location);
2109 | multiplicative_expression PERCENT prefixed_unary_expression
2111 $$ = new Binary (Binary.Operator.Modulus,
2112 (Expression) $1, (Expression) $3, lexer.Location);
2117 : multiplicative_expression
2118 | additive_expression PLUS multiplicative_expression
2120 $$ = new Binary (Binary.Operator.Addition,
2121 (Expression) $1, (Expression) $3, lexer.Location);
2123 | additive_expression MINUS multiplicative_expression
2125 $$ = new Binary (Binary.Operator.Subtraction,
2126 (Expression) $1, (Expression) $3, lexer.Location);
2131 : additive_expression
2132 | shift_expression OP_SHIFT_LEFT additive_expression
2134 $$ = new Binary (Binary.Operator.LeftShift,
2135 (Expression) $1, (Expression) $3, lexer.Location);
2137 | shift_expression OP_SHIFT_RIGHT additive_expression
2139 $$ = new Binary (Binary.Operator.RightShift,
2140 (Expression) $1, (Expression) $3, lexer.Location);
2144 relational_expression
2146 | relational_expression OP_LT shift_expression
2148 $$ = new Binary (Binary.Operator.LessThan,
2149 (Expression) $1, (Expression) $3, lexer.Location);
2151 | relational_expression OP_GT shift_expression
2153 $$ = new Binary (Binary.Operator.GreaterThan,
2154 (Expression) $1, (Expression) $3, lexer.Location);
2156 | relational_expression OP_LE shift_expression
2158 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2159 (Expression) $1, (Expression) $3, lexer.Location);
2161 | relational_expression OP_GE shift_expression
2163 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2164 (Expression) $1, (Expression) $3, lexer.Location);
2166 | relational_expression IS type
2168 $$ = new Is ((Expression) $1, (string) $3);
2170 | relational_expression AS type
2172 $$ = new As ((Expression) $1, (string) $3);
2177 : relational_expression
2178 | equality_expression OP_EQ relational_expression
2180 $$ = new Binary (Binary.Operator.Equality,
2181 (Expression) $1, (Expression) $3, lexer.Location);
2183 | equality_expression OP_NE relational_expression
2185 $$ = new Binary (Binary.Operator.Inequality,
2186 (Expression) $1, (Expression) $3, lexer.Location);
2191 : equality_expression
2192 | and_expression BITWISE_AND equality_expression
2194 $$ = new Binary (Binary.Operator.BitwiseAnd,
2195 (Expression) $1, (Expression) $3, lexer.Location);
2199 exclusive_or_expression
2201 | exclusive_or_expression CARRET and_expression
2203 $$ = new Binary (Binary.Operator.ExclusiveOr,
2204 (Expression) $1, (Expression) $3, lexer.Location);
2208 inclusive_or_expression
2209 : exclusive_or_expression
2210 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2212 $$ = new Binary (Binary.Operator.BitwiseOr,
2213 (Expression) $1, (Expression) $3, lexer.Location);
2217 conditional_and_expression
2218 : inclusive_or_expression
2219 | conditional_and_expression OP_AND inclusive_or_expression
2221 $$ = new Binary (Binary.Operator.LogicalAnd,
2222 (Expression) $1, (Expression) $3, lexer.Location);
2226 conditional_or_expression
2227 : conditional_and_expression
2228 | conditional_or_expression OP_OR conditional_and_expression
2230 $$ = new Binary (Binary.Operator.LogicalOr,
2231 (Expression) $1, (Expression) $3, lexer.Location);
2235 conditional_expression
2236 : conditional_or_expression
2237 | conditional_or_expression INTERR expression COLON expression
2239 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2243 assignment_expression
2244 : prefixed_unary_expression ASSIGN expression
2246 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2248 | prefixed_unary_expression OP_MULT_ASSIGN expression
2250 Location l = lexer.Location;
2252 $$ = new Assign ((Expression) $1,
2253 new Binary (Binary.Operator.Multiply,
2255 (Expression) $3, l), l);
2257 | prefixed_unary_expression OP_DIV_ASSIGN expression
2259 Location l = lexer.Location;
2261 $$ = new Assign ((Expression) $1,
2262 new Binary (Binary.Operator.Division,
2264 (Expression) $3, l), l);
2266 | prefixed_unary_expression OP_MOD_ASSIGN expression
2268 Location l = lexer.Location;
2270 $$ = new Assign ((Expression) $1,
2271 new Binary (Binary.Operator.Modulus,
2273 (Expression) $3, l), l);
2275 | prefixed_unary_expression OP_ADD_ASSIGN expression
2277 Location l = lexer.Location;
2279 $$ = new Assign ((Expression) $1,
2280 new Binary (Binary.Operator.Addition,
2282 (Expression) $3, l), l);
2284 | prefixed_unary_expression OP_SUB_ASSIGN expression
2286 Location l = lexer.Location;
2288 $$ = new Assign ((Expression) $1,
2289 new Binary (Binary.Operator.Subtraction,
2291 (Expression) $3, l), l);
2293 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2295 Location l = lexer.Location;
2297 $$ = new Assign ((Expression) $1,
2298 new Binary (Binary.Operator.LeftShift,
2300 (Expression) $3, l), l);
2302 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2304 Location l = lexer.Location;
2306 $$ = new Assign ((Expression) $1,
2307 new Binary (Binary.Operator.RightShift,
2309 (Expression) $3, l), l);
2311 | prefixed_unary_expression OP_AND_ASSIGN expression
2313 Location l = lexer.Location;
2315 $$ = new Assign ((Expression) $1,
2316 new Binary (Binary.Operator.BitwiseAnd,
2318 (Expression) $3, l), l);
2320 | prefixed_unary_expression OP_OR_ASSIGN expression
2322 Location l = lexer.Location;
2324 $$ = new Assign ((Expression) $1,
2325 new Binary (Binary.Operator.BitwiseOr,
2327 (Expression) $3, l), l);
2329 | prefixed_unary_expression OP_XOR_ASSIGN expression
2331 Location l = lexer.Location;
2333 $$ = new Assign ((Expression) $1,
2334 new Binary (Binary.Operator.ExclusiveOr,
2336 (Expression) $3, l), l);
2341 : conditional_expression
2342 | assignment_expression
2362 string full_class_name = MakeName ((string) $4);
2364 new_class = new Class (rc, current_container, full_class_name, (int) $2,
2365 (Attributes) $1, lexer.Location);
2366 current_container = new_class;
2367 current_container.Namespace = current_namespace;
2368 tree.RecordClass (full_class_name, new_class);
2374 Class new_class = (Class) current_container;
2377 new_class.Bases = (ArrayList) $6;
2379 current_container = current_container.Parent;
2380 CheckDef (current_container.AddClass (new_class), new_class.Name);
2387 : /* empty */ { $$ = (int) 0; }
2393 | modifiers modifier
2398 if ((m1 & m2) != 0) {
2399 Location l = lexer.Location;
2400 Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2402 $$ = (int) (m1 | m2);
2407 : NEW { $$ = Modifiers.NEW; }
2408 | PUBLIC { $$ = Modifiers.PUBLIC; }
2409 | PROTECTED { $$ = Modifiers.PROTECTED; }
2410 | INTERNAL { $$ = Modifiers.INTERNAL; }
2411 | PRIVATE { $$ = Modifiers.PRIVATE; }
2412 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2413 | SEALED { $$ = Modifiers.SEALED; }
2414 | STATIC { $$ = Modifiers.STATIC; }
2415 | READONLY { $$ = Modifiers.READONLY; }
2416 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2417 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2418 | EXTERN { $$ = Modifiers.EXTERN; }
2422 : /* empty */ { $$ = null; }
2423 | class_base { $$ = $1; }
2427 : COLON type_list { $$ = $2; }
2435 // A block is "contained" on the following places:
2437 // property_declaration as part of the accessor body (get/set)
2438 // operator_declaration
2439 // constructor_declaration
2440 // destructor_declaration
2441 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2446 current_block = new Block (current_block);
2448 opt_statement_list CLOSE_BRACE
2450 while (current_block.Implicit)
2451 current_block = current_block.Parent;
2453 current_block = current_block.Parent;
2464 | statement_list statement
2468 : declaration_statement
2470 if ((Block) $1 != current_block){
2471 current_block.AddStatement ((Statement) $1);
2472 current_block = (Block) $1;
2475 | embedded_statement
2477 Statement s = (Statement) $1;
2480 current_block.AddStatement ((Statement) $1);
2484 current_block.AddStatement ((Statement) $1);
2491 | expression_statement
2492 | selection_statement
2493 | iteration_statement
2497 | unchecked_statement
2505 $$ = new EmptyStatement ();
2510 : IDENTIFIER COLON statement
2512 string lab = (String) $1;
2515 block = new Block (current_block, lab);
2516 block.AddStatement ((Statement) $3);
2519 if (!current_block.AddLabel (lab, block)){
2520 Location l = lexer.Location;
2521 Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2527 declaration_statement
2528 : local_variable_declaration SEMICOLON
2530 DictionaryEntry de = (DictionaryEntry) $1;
2532 $$ = declare_local_variables ((string) de.Key, (ArrayList) de.Value, lexer.Location);
2535 | local_constant_declaration SEMICOLON
2537 DictionaryEntry de = (DictionaryEntry) $1;
2539 $$ = declare_local_constant ((string) de.Key, (VariableDeclaration) de.Value);
2544 * The following is from Rhys' grammar:
2545 * > Types in local variable declarations must be recognized as
2546 * > expressions to prevent reduce/reduce errors in the grammar.
2547 * > The expressions are converted into types during semantic analysis.
2550 : primary_expression opt_rank_specifier
2552 // FIXME: Do something smart here regarding the composition of the type.
2554 // Ok, the above "primary_expression" is there to get rid of
2555 // both reduce/reduce and shift/reduces in the grammar, it should
2556 // really just be "type_name". If you use type_name, a reduce/reduce
2557 // creeps up. If you use qualified_identifier (which is all we need
2558 // really) two shift/reduces appear.
2561 // So the super-trick is that primary_expression
2562 // can only be either a SimpleName or a MemberAccess.
2563 // The MemberAccess case arises when you have a fully qualified type-name like :
2565 // SimpleName is when you have
2568 Expression expr = (Expression) $1;
2569 if (!(expr is SimpleName || expr is MemberAccess)) {
2570 Location l = lexer.Location;
2571 Report.Error (-1, l, "Invalid Type definition");
2572 $$ = "System.Object";
2576 // So we extract the string corresponding to the SimpleName
2579 $$ = GetQualifiedIdentifier (expr) + (string) $2;
2581 | builtin_types opt_rank_specifier
2583 $$ = (string) $1 + (string) $2;
2587 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2590 // // FIXME: this is a string that represents the type
2591 // // Figure out something to make this work.
2596 local_variable_declaration
2597 : local_variable_type variable_declarators
2599 $$ = new DictionaryEntry ($1, $2);
2603 local_constant_declaration
2604 : CONST local_variable_type constant_declarator
2606 $$ = new DictionaryEntry ($2, $3);
2610 expression_statement
2611 : statement_expression SEMICOLON
2618 // We have to do the wrapping here and not in the case above,
2619 // because statement_expression is used for example in for_statement
2621 statement_expression
2622 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2623 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2624 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2625 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2626 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2627 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2628 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2630 Report.Error (1002, lexer.Location, "Expecting `;'");
2634 object_creation_expression
2635 : object_or_delegate_creation_expression
2636 { note ("complain if this is a delegate maybe?"); }
2645 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2648 $$ = new If ((Expression) $3, (Statement) $5);
2650 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2651 embedded_statement ELSE embedded_statement
2653 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2658 : SWITCH OPEN_PARENS
2660 oob_stack.Push (lexer.Location);
2662 expression CLOSE_PARENS
2665 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2679 : /* empty */ { $$ = new ArrayList (); }
2686 ArrayList sections = new ArrayList ();
2691 | switch_sections switch_section
2693 ArrayList sections = (ArrayList) $1;
2703 current_block = new Block (current_block);
2707 while (current_block.Implicit)
2708 current_block = current_block.Parent;
2709 $$ = new SwitchSection ((ArrayList) $1, current_block);
2710 current_block = current_block.Parent;
2717 ArrayList labels = new ArrayList ();
2722 | switch_labels switch_label
2724 ArrayList labels = (ArrayList) ($1);
2732 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
2733 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
2744 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2746 $$ = new While ((Expression) $3, (Statement) $5);
2751 : DO embedded_statement
2752 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2754 $$ = new Do ((Statement) $2, (Expression) $5);
2760 opt_for_initializer SEMICOLON
2762 Block assign_block = new Block (current_block);
2763 current_block = assign_block;
2765 if ($3 is DictionaryEntry){
2766 DictionaryEntry de = (DictionaryEntry) $3;
2768 string type = (string) de.Key;
2769 ArrayList var_declarators = (ArrayList) de.Value;
2771 foreach (VariableDeclaration decl in var_declarators){
2772 if (!current_block.AddVariable (type, decl.identifier, decl.Location)){
2773 Report.Error (128, decl.Location,
2774 "A local variable `" + decl.identifier + "' is already" +
2775 "defined in this scope");
2778 Location l = lexer.Location;
2780 foreach (VariableDeclaration decl in var_declarators){
2783 if (decl.expression_or_array_initializer is Expression){
2784 expr = (Expression) decl.expression_or_array_initializer;
2788 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
2790 string base_type = type.Substring (0, type.IndexOf ("["));
2791 string rank = type.Substring (type.IndexOf ("["));
2793 expr = new ArrayCreation (base_type, rank, init, decl.Location);
2796 LocalVariableReference var;
2797 var = new LocalVariableReference (
2798 assign_block, decl.identifier, l);
2800 Assign a = new Assign (var, expr, decl.Location);
2802 assign_block.AddStatement (new StatementExpression (a));
2807 opt_for_condition SEMICOLON
2808 opt_for_iterator CLOSE_PARENS
2811 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10);
2813 current_block.AddStatement (f);
2814 while (current_block.Implicit)
2815 current_block = current_block.Parent;
2817 current_block = current_block.Parent;
2822 : /* empty */ { $$ = new EmptyStatement (); }
2827 : local_variable_declaration
2828 | statement_expression_list
2832 : /* empty */ { $$ = new BoolLiteral (true); }
2833 | boolean_expression
2837 : /* empty */ { $$ = new EmptyStatement (); }
2842 : statement_expression_list
2845 statement_expression_list
2846 : statement_expression
2848 Block b = new Block (null, true);
2850 b.AddStatement ((Statement) $1);
2853 | statement_expression_list COMMA statement_expression
2855 Block b = (Block) $1;
2857 b.AddStatement ((Statement) $3);
2863 : FOREACH OPEN_PARENS type IDENTIFIER IN
2865 oob_stack.Push (lexer.Location);
2867 expression CLOSE_PARENS
2869 Block foreach_block = new Block (current_block, true);
2870 LocalVariableReference v;
2871 Location l = lexer.Location;
2873 foreach_block.AddVariable ((string) $3, (string) $4, l);
2874 v = new LocalVariableReference (foreach_block, (string) $4, l);
2876 current_block.AddStatement (foreach_block);
2877 current_block = foreach_block;
2879 oob_stack.Push (foreach_block);
2884 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
2885 Block foreach_block = (Block) oob_stack.Pop ();
2886 Location l = (Location) oob_stack.Pop ();
2888 $$ = new Foreach ((string) $3, v, (Expression) $7, (Statement) $10, l);
2894 | continue_statement
2903 $$ = new Break (lexer.Location);
2908 : CONTINUE SEMICOLON
2910 $$ = new Continue (lexer.Location);
2915 : GOTO IDENTIFIER SEMICOLON
2917 $$ = new Goto ((string) $2, lexer.Location);
2919 | GOTO CASE constant_expression SEMICOLON
2921 $$ = new GotoCase ((Expression) $3, lexer.Location);
2923 | GOTO DEFAULT SEMICOLON
2925 $$ = new GotoDefault (lexer.Location);
2930 : RETURN opt_expression SEMICOLON
2932 $$ = new Return ((Expression) $2, lexer.Location);
2937 : THROW opt_expression SEMICOLON
2939 $$ = new Throw ((Expression) $2);
2949 : TRY block catch_clauses
2952 ArrayList s = new ArrayList ();
2954 foreach (Catch cc in (ArrayList) $3) {
2955 if (cc.Type == null)
2961 // Now s contains the list of specific catch clauses
2962 // and g contains the general one.
2964 $$ = new Try ((Block) $2, s, g, null);
2966 | TRY block opt_catch_clauses FINALLY block
2969 ArrayList s = new ArrayList ();
2971 foreach (Catch cc in (ArrayList) $3) {
2972 if (cc.Type == null)
2978 $$ = new Try ((Block) $2, s, g, (Block) $5);
2983 : /* empty */ { $$ = null; }
2990 ArrayList l = new ArrayList ();
2995 | catch_clauses catch_clause
2997 ArrayList l = (ArrayList) $1;
3005 : /* empty */ { $$ = null; }
3010 : CATCH opt_catch_args
3012 string type = null, id = null;
3015 DictionaryEntry cc = (DictionaryEntry) $2;
3016 type = (string) cc.Key;
3017 id = (string) cc.Value;
3020 ArrayList one = new ArrayList ();
3021 Location loc = lexer.Location;
3023 one.Add (new VariableDeclaration (id, null, loc));
3026 current_block = new Block (current_block);
3027 Block b = declare_local_variables (type, one, loc);
3034 string type = null, id = null;
3037 DictionaryEntry cc = (DictionaryEntry) $2;
3038 type = (string) cc.Key;
3039 id = (string) cc.Value;
3042 while (current_block != (Block) $1)
3043 current_block = current_block.Parent;
3048 $$ = new Catch (type, id , (Block) $4);
3053 : /* empty */ { $$ = null; }
3058 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3060 $$ = new DictionaryEntry ($2, $3);
3067 $$ = new Checked ((Block) $2);
3074 $$ = new Unchecked ((Block) $2);
3079 : LOCK OPEN_PARENS expression CLOSE_PARENS
3085 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3090 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3092 Block assign_block = new Block (current_block);
3093 current_block = assign_block;
3095 oob_stack.Push (lexer.Location);
3097 if ($3 is DictionaryEntry){
3098 DictionaryEntry de = (DictionaryEntry) $3;
3100 Location l = lexer.Location;
3102 string type = (string) de.Key;
3103 ArrayList var_declarators = (ArrayList) de.Value;
3105 foreach (VariableDeclaration decl in var_declarators){
3106 if (!current_block.AddVariable (type, decl.identifier, decl.Location)){
3107 Report.Error (128, decl.Location,
3108 "A local variable `" + decl.identifier + "' is already" +
3109 "defined in this scope");
3113 ArrayList vars = new ArrayList ();
3115 foreach (VariableDeclaration decl in var_declarators){
3118 if (decl.expression_or_array_initializer is Expression){
3119 expr = (Expression) decl.expression_or_array_initializer;
3123 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3125 string base_type = type.Substring (0, type.IndexOf ("["));
3126 string rank = type.Substring (type.IndexOf ("["));
3128 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3131 LocalVariableReference var;
3134 var = new LocalVariableReference (assign_block, decl.identifier, l);
3135 vi = var.VariableInfo;
3138 // This is so that it is not a warning on using variables
3141 vars.Add (new DictionaryEntry (var, expr));
3143 // Assign a = new Assign (var, expr, decl.Location);
3144 // assign_block.AddStatement (new StatementExpression (a));
3151 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
3152 current_block.AddStatement (u);
3153 while (current_block.Implicit)
3154 current_block = current_block.Parent;
3156 current_block = current_block.Parent;
3160 resource_acquisition
3161 : local_variable_declaration
3168 // A class used to pass around variable declarations and constants
3170 public class VariableDeclaration {
3171 public string identifier;
3172 public object expression_or_array_initializer;
3173 public Location Location;
3175 public VariableDeclaration (string id, object eoai, Location l){
3176 this.identifier = id;
3177 this.expression_or_array_initializer = eoai;
3183 // A class used to hold info about an indexer declarator
3186 public class IndexerDeclaration {
3188 public string interface_type;
3189 public Parameters param_list;
3191 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
3194 this.interface_type = interface_type;
3195 this.param_list = param_list;
3200 // A class used to hold info about an operator declarator
3203 public class OperatorDeclaration {
3204 public Operator.OpType optype;
3205 public string ret_type;
3206 public string arg1type;
3207 public string arg1name;
3208 public string arg2type;
3209 public string arg2name;
3210 public Location location;
3212 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
3213 string arg2type, string arg2name, Location location)
3216 this.ret_type = ret_type;
3217 this.arg1type = arg1type;
3218 this.arg1name = arg1name;
3219 this.arg2type = arg2type;
3220 this.arg2name = arg2name;
3221 this.location = location;
3227 // Given the @class_name name, it creates a fully qualified name
3228 // based on the containing declaration space
3231 MakeName (string class_name)
3233 string ns = current_namespace.Name;
3234 string container_name = current_container.Name;
3236 if (container_name == ""){
3238 return ns + "." + class_name;
3242 return container_name + "." + class_name;
3246 // Used to report back to the user the result of a declaration
3247 // in the current declaration space
3250 CheckDef (DeclSpace.AdditionResult result, string name)
3252 if (result == DeclSpace.AdditionResult.Success)
3255 Location l = lexer.Location;
3258 case DeclSpace.AdditionResult.NameExists:
3259 Report.Error (102, l, "The namespace `" + current_container.Name +
3260 "' already contains a definition for `"+
3265 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
3267 // case DeclSpace.AdditionResult.MethodDuplicated:
3268 // error (111, "Class `"+current_container.Name+
3269 // "' already defines a member called '" +
3270 // name + "' with the same parameter types");
3273 case DeclSpace.AdditionResult.EnclosingClash:
3274 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3277 case DeclSpace.AdditionResult.NotAConstructor:
3278 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3284 CheckDef (bool result, string name)
3288 CheckDef (DeclSpace.AdditionResult.NameExists, name);
3292 SimpleLookup (string name, Location loc)
3295 // we need to check against current_block not being null
3296 // as `expression' is allowed in argument_lists, which
3297 // do not exist inside a block.
3299 if (current_block != null){
3300 if (current_block.IsVariableDefined (name))
3301 return new LocalVariableReference (current_block, name, loc);
3304 if (current_local_parameters != null){
3306 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
3308 return new ParameterReference (current_local_parameters, idx, name);
3314 Expression DecomposeQI (string name, Location loc)
3318 if (name.IndexOf ('.') == -1){
3319 o = SimpleLookup (name, loc);
3321 return new SimpleName (name, loc);
3324 int pos = name.LastIndexOf (".");
3325 string left = name.Substring (0, pos);
3326 string right = name.Substring (pos + 1);
3328 o = DecomposeQI (left, loc);
3329 return new MemberAccess (o, right, loc);
3334 // This method is used to get at the complete string representation of
3335 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3336 // This is necessary because local_variable_type admits primary_expression
3337 // as the type of the variable. So we do some extra checking
3339 string GetQualifiedIdentifier (Expression expr)
3341 if (expr is SimpleName)
3342 return ((SimpleName)expr).Name;
3343 else if (expr is MemberAccess)
3344 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3346 throw new Exception ("Expr has to be either SimpleName or MemberAccess !");
3350 Block declare_local_variables (string type, ArrayList variable_declarators, Location loc)
3352 Block implicit_block;
3353 ArrayList inits = null;
3356 // We use the `Used' property to check whether statements
3357 // have been added to the current block. If so, we need
3358 // to create another block to contain the new declaration
3359 // otherwise, as an optimization, we use the same block to
3360 // add the declaration.
3362 // FIXME: A further optimization is to check if the statements
3363 // that were added were added as part of the initialization
3364 // below. In which case, no other statements have been executed
3365 // and we might be able to reduce the number of blocks for
3366 // situations like this:
3368 // int j = 1; int k = j + 1;
3370 if (current_block.Used)
3371 implicit_block = new Block (current_block, true);
3373 implicit_block = current_block;
3375 foreach (VariableDeclaration decl in variable_declarators){
3376 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3377 if (decl.expression_or_array_initializer != null){
3379 inits = new ArrayList ();
3383 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3384 "' is already defined in this scope");
3389 return implicit_block;
3391 foreach (VariableDeclaration decl in inits){
3395 if (decl.expression_or_array_initializer is Expression){
3396 expr = (Expression) decl.expression_or_array_initializer;
3399 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3401 string base_type = type.Substring (0, type.IndexOf ("["));
3402 string rank = type.Substring (type.IndexOf ("["));
3404 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3408 LocalVariableReference var;
3409 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
3411 assign = new Assign (var, expr, decl.Location);
3413 implicit_block.AddStatement (new StatementExpression (assign));
3416 return implicit_block;
3419 Block declare_local_constant (string type, VariableDeclaration decl)
3421 Block implicit_block;
3423 if (current_block.Used)
3424 implicit_block = new Block (current_block, true);
3426 implicit_block = current_block;
3428 if (!(implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
3430 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3431 "' is already defined in this scope");
3434 return implicit_block;
3437 void CheckAttributeTarget (string a)
3441 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3445 Location l = lexer.Location;
3446 Report.Error (658, l, "Invalid attribute target");
3452 void CheckUnaryOperator (Operator.OpType op)
3456 case Operator.OpType.LogicalNot:
3457 case Operator.OpType.OnesComplement:
3458 case Operator.OpType.Increment:
3459 case Operator.OpType.Decrement:
3460 case Operator.OpType.True:
3461 case Operator.OpType.False:
3462 case Operator.OpType.Addition:
3463 case Operator.OpType.Subtraction:
3468 Location l = lexer.Location;
3469 Report.Error (1019, l, "Overloadable unary operator expected");
3475 void CheckBinaryOperator (Operator.OpType op)
3479 case Operator.OpType.Addition:
3480 case Operator.OpType.Subtraction:
3481 case Operator.OpType.Multiply:
3482 case Operator.OpType.Division:
3483 case Operator.OpType.Modulus:
3484 case Operator.OpType.BitwiseAnd:
3485 case Operator.OpType.BitwiseOr:
3486 case Operator.OpType.ExclusiveOr:
3487 case Operator.OpType.LeftShift:
3488 case Operator.OpType.RightShift:
3489 case Operator.OpType.Equality:
3490 case Operator.OpType.Inequality:
3491 case Operator.OpType.GreaterThan:
3492 case Operator.OpType.LessThan:
3493 case Operator.OpType.GreaterThanOrEqual:
3494 case Operator.OpType.LessThanOrEqual:
3498 Location l = lexer.Location;
3499 Report.Error (1020, l, "Overloadable binary operator expected");
3505 void syntax_error (Location l, string msg)
3507 Report.Error (1003, l, "Syntax error, " + msg);
3510 void output (string s)
3512 Console.WriteLine (s);
3515 void note (string s)
3517 // Used to put annotations
3522 public Tokenizer Lexer {
3528 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
3530 current_namespace = new Namespace (null, "");
3532 this.tree = rc.Tree;
3535 current_container = tree.Types;
3536 current_container.Namespace = current_namespace;
3537 oob_stack = new Stack ();
3539 lexer = new Tokenizer (input, name);
3542 public override int parse ()
3544 StringBuilder value = new StringBuilder ();
3548 if (yacc_verbose_flag)
3549 yyparse (lexer, new yydebug.yyDebugSimple ());
3552 } catch (Exception e){
3553 // Console.WriteLine ("Fatal error: " + name);
3554 // Console.WriteLine (lexer.location);
3557 // Please do not remove this, it is used during debugging
3560 Console.WriteLine (lexer.location + " : Parsing error ");
3561 Console.WriteLine (e);
3565 return global_errors;