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
32 using System.Collections;
38 public class CSharpParser : GenericParser {
39 Namespace current_namespace;
40 TypeContainer current_container;
43 // Current block is used to add statements as we find
50 // Current interface is used by the various declaration
51 // productions in the interface declaration to "add"
52 // the interfaces as we find them.
54 Interface current_interface;
57 // This is used by the unary_expression code to resolve
58 // a name against a parameter.
60 Parameters current_local_parameters;
63 // Using during property parsing to describe the implicit
64 // value parameter that is passed to the "set" accesor
67 Parameter [] implicit_value_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
85 %token NONE /* This token is never returned by our lexer */
86 %token ERROR // This is used not by the parser, but by the tokenizer.
90 *These are the C# keywords
170 /* C# keywords which are not really keywords */
174 /* C# single character operators/punctuation. */
175 %token OPEN_BRACE "{"
176 %token CLOSE_BRACE "}"
177 %token OPEN_BRACKET "["
178 %token CLOSE_BRACKET "]"
179 %token OPEN_PARENS "("
180 %token CLOSE_PARENS ")"
193 %token BITWISE_AND "&"
194 %token BITWISE_OR "|"
201 /* C# multi-character operators. */
204 %token OP_SHIFT_LEFT "<<"
205 %token OP_SHIFT_RIGHT ">>"
212 %token OP_MULT_ASSIGN "*="
213 %token OP_DIV_ASSIGN "/="
214 %token OP_MOD_ASSIGN "%="
215 %token OP_ADD_ASSIGN "+="
216 %token OP_SUB_ASSIGN "-="
217 %token OP_SHIFT_LEFT_ASSIGN "<<="
218 %token OP_SHIFT_RIGHT_ASSIGN ">>="
219 %token OP_AND_ASSIGN "&="
220 %token OP_XOR_ASSIGN "^="
221 %token OP_OR_ASSIGN "|="
225 %token LITERAL_INTEGER "int literal"
226 %token LITERAL_FLOAT "float literal"
227 %token LITERAL_DOUBLE "double literal"
228 %token LITERAL_DECIMAL "decimal literal"
229 %token LITERAL_CHARACTER "character literal"
230 %token LITERAL_STRING "string literal"
234 /* Add precedence rules to solve dangling else s/r conflict */
243 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
245 %left STAR DIV PERCENT
246 %right BANG CARRET UMINUS
247 %nonassoc OP_INC OP_DEC
249 %left OPEN_BRACKET OPEN_BRACE
253 %start compilation_unit
254 /*%start namespace_declaration */
258 : opt_using_directives opt_attributes opt_namespace_member_declarations EOF
260 // Check that using comes only before namespace elements
266 | using_directives using_directive
270 : using_alias_directive
271 | using_namespace_directive
274 using_alias_directive
275 : USING IDENTIFIER ASSIGN
276 namespace_or_type_name SEMICOLON
278 // FIXME : Need to implement actual action.
282 using_namespace_directive
283 : USING namespace_name SEMICOLON
285 current_namespace.Using ((string) $2);
289 // namespace_declarations
290 // : namespace_declaration
291 // | namespace_declarations namespace_declaration
293 namespace_declaration
294 : NAMESPACE qualified_identifier
296 current_namespace = new Namespace (current_namespace, (string) $2);
297 tree.RecordNamespace ((string) $2, current_namespace);
299 namespace_body opt_semicolon
301 current_namespace = current_namespace.Parent;
317 | qualified_identifier DOT IDENTIFIER {
318 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
323 : namespace_or_type_name
329 opt_namespace_member_declarations
340 opt_namespace_member_declarations
342 | namespace_member_declarations
345 namespace_member_declarations
346 : namespace_member_declaration
347 | namespace_member_declarations namespace_member_declaration
350 namespace_member_declaration
357 Class c = (Class) $1;
358 mod_flags = c.ModFlags;
360 } else if ($1 is Struct){
361 Struct s = (Struct) $1;
362 mod_flags = s.ModFlags;
368 // We remove this error until we can
369 //if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
370 // error (1527, "Namespace elements cant be explicitly " +
371 // "declared private or protected in `" + name + "'");
374 | namespace_declaration
380 | interface_declaration
382 | delegate_declaration
389 : /* empty */ { $$ = null; }
390 | attribute_section opt_attributes
395 attrs = (Attributes) $2;
396 attrs.AddAttribute ((Attribute) $1);
398 attrs = new Attributes ((Attribute) $1);
405 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
407 string target = null;
410 target = (string) $2;
412 $$ = new Attribute (target, (ArrayList) $3);
414 | OPEN_BRACKET attribute_list CLOSE_BRACKET
416 $$ = new Attribute (null, (ArrayList) $2);
420 attribute_target_specifier
421 : attribute_target COLON
430 CheckAttributeTarget ((string) $1);
433 | EVENT { $$ = "event"; }
434 | RETURN { $$ = "return"; }
440 ArrayList attrs = new ArrayList ();
446 | attribute_list COMMA attribute
448 ArrayList attrs = (ArrayList) $1;
456 : attribute_name opt_attribute_arguments
458 $$ = new DictionaryEntry ($1, $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
477 ArrayList args = new ArrayList ();
482 | attribute_arguments COMMA expression
484 ArrayList args = (ArrayList) $1;
493 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
496 opt_class_member_declarations
498 | class_member_declarations
501 class_member_declarations
502 : class_member_declaration
503 | class_member_declarations
504 class_member_declaration
507 class_member_declaration
508 : constant_declaration // done
509 | field_declaration // done
510 | method_declaration // done
511 | property_declaration // done
512 | event_declaration // done
513 | indexer_declaration // done
514 | operator_declaration // done
515 | constructor_declaration // done
516 | destructor_declaration // done
526 string full_struct_name = MakeName ((string) $4);
528 new_struct = new Struct (rc, current_container, full_struct_name, (int) $2,
529 (Attributes) $1, lexer.Location);
530 current_container = new_struct;
531 current_container.Namespace = current_namespace;
532 tree.RecordStruct (full_struct_name, new_struct);
534 opt_struct_interfaces
538 Struct new_struct = (Struct) current_container;
540 current_container = current_container.Parent;
541 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
546 opt_struct_interfaces
553 | struct_interfaces struct_interface
561 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
564 opt_struct_member_declarations
566 | struct_member_declarations
569 struct_member_declarations
570 : struct_member_declaration
571 | struct_member_declarations struct_member_declaration
574 struct_member_declaration
575 : constant_declaration
578 | property_declaration
580 | indexer_declaration
581 | operator_declaration
582 | constructor_declaration
594 foreach (DictionaryEntry constant in (ArrayList) $5){
595 Constant c = new Constant (
596 (string) $4, (string) constant.Key,
597 (Expression) constant.Value, (int) $2, (Attributes) $1);
599 CheckDef (current_container.AddConstant (c), c.Name);
605 : constant_declarator
607 ArrayList constants = new ArrayList ();
611 | constant_declarators COMMA constant_declarator
613 ArrayList constants = (ArrayList) $1;
620 : IDENTIFIER ASSIGN constant_expression
622 $$ = new DictionaryEntry ($1, $3);
633 string type = (string) $3;
636 foreach (VariableDeclaration var in (ArrayList) $4){
637 Field field = new Field (type, mod, var.identifier,
638 var.expression_or_array_initializer, (Attributes) $1);
640 CheckDef (current_container.AddField (field), field.Name);
647 : variable_declarator
649 ArrayList decl = new ArrayList ();
653 | variable_declarators COMMA variable_declarator
655 ArrayList decls = (ArrayList) $1;
662 : IDENTIFIER ASSIGN variable_initializer
664 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
668 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
687 Method method = (Method) $1;
688 Block b = (Block) $2;
691 if ((method.ModFlags & (Modifiers.EXTERN | Modifiers.ABSTRACT)) == 0){
693 501, lexer.Location, "`" +
694 current_container.Name + "." + method.Name + "'" +
695 "must declare a body because it is not marked abstract or extern");
699 method.Block = (Block) $2;
700 CheckDef (current_container.AddMethod (method), method.Name);
702 current_local_parameters = null;
711 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
713 Method method = new Method ((string) $3, (int) $2, (string) $4,
714 (Parameters) $6, (Attributes) $1, lexer.Location);
716 current_local_parameters = (Parameters) $6;
724 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
726 Method method = new Method ("System.Void", (int) $2, (string) $4,
727 (Parameters) $6, (Attributes) $1, lexer.Location);
729 current_local_parameters = (Parameters) $6;
736 | SEMICOLON { $$ = null; }
739 opt_formal_parameter_list
740 : /* empty */ { $$ = new Parameters (null, null); }
741 | formal_parameter_list
744 formal_parameter_list
747 ArrayList pars_list = (ArrayList) $1;
749 Parameter [] pars = new Parameter [pars_list.Count];
750 pars_list.CopyTo (pars);
752 $$ = new Parameters (pars, null);
754 | fixed_parameters COMMA parameter_array
756 ArrayList pars_list = (ArrayList) $1;
758 Parameter [] pars = new Parameter [pars_list.Count];
759 pars_list.CopyTo (pars);
761 $$ = new Parameters (pars, (Parameter) $3);
765 $$ = new Parameters (null, (Parameter) $1);
772 ArrayList pars = new ArrayList ();
777 | fixed_parameters COMMA fixed_parameter
779 ArrayList pars = (ArrayList) $1;
788 opt_parameter_modifier
792 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
796 opt_parameter_modifier
797 : /* empty */ { $$ = Parameter.Modifier.NONE; }
802 : REF { $$ = Parameter.Modifier.REF; }
803 | OUT { $$ = Parameter.Modifier.OUT; }
807 : opt_attributes PARAMS type IDENTIFIER
809 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
810 note ("type must be a single-dimension array type");
815 : IDENTIFIER { $$ = $1.ToString (); }
816 | interface_type DOT IDENTIFIER { $$ = $1.ToString () + "." + $3.ToString (); }
825 Parameter implicit_value_parameter;
826 implicit_value_parameter = new Parameter ((string) $3, "value", Parameter.Modifier.NONE, null);
828 lexer.properties = true;
830 implicit_value_parameters = new Parameter [1];
831 implicit_value_parameters [0] = implicit_value_parameter;
833 accessor_declarations
835 lexer.properties = false;
840 DictionaryEntry pair = (DictionaryEntry) $7;
841 Block get_block = null;
842 Block set_block = null;
844 if (pair.Key != null)
845 get_block = (Block) pair.Key;
846 if (pair.Value != null)
847 set_block = (Block) pair.Value;
849 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block, (Attributes) $1);
851 CheckDef (current_container.AddProperty (prop), prop.Name);
852 implicit_value_parameters = null;
856 accessor_declarations
857 : get_accessor_declaration opt_set_accessor_declaration
859 $$ = new DictionaryEntry ($1, $2);
861 | set_accessor_declaration opt_get_accessor_declaration
863 $$ = new DictionaryEntry ($2, $1);
867 opt_get_accessor_declaration
868 : /* empty */ { $$ = null; }
869 | get_accessor_declaration
872 opt_set_accessor_declaration
873 : /* empty */ { $$ = null; }
874 | set_accessor_declaration
877 get_accessor_declaration
880 // If this is not the case, then current_local_parameters has already
881 // been set in indexer_declaration
882 if (parsing_indexer == false)
883 current_local_parameters = new Parameters (implicit_value_parameters, null);
890 current_local_parameters = null;
894 set_accessor_declaration
897 if (parsing_indexer == false)
898 current_local_parameters = new Parameters (implicit_value_parameters, null);
903 current_local_parameters = null;
909 | SEMICOLON { $$ = new Block (null); }
912 interface_declaration
917 Interface new_interface;
918 string full_interface_name = MakeName ((string) $4);
920 new_interface = new Interface (rc, current_container, full_interface_name, (int) $2,
921 (Attributes) $1, lexer.Location);
922 if (current_interface != null) {
923 Location l = lexer.Location;
924 Report.Error (-2, l, "Internal compiler error: interface inside interface");
926 current_interface = new_interface;
927 tree.RecordInterface (full_interface_name, new_interface);
932 Interface new_interface = (Interface) current_interface;
935 new_interface.Bases = (ArrayList) $6;
937 current_interface = null;
938 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
943 : /* empty */ { $$ = null; }
948 : COLON interface_type_list { $$ = $2; }
954 ArrayList interfaces = new ArrayList ();
959 | interface_type_list COMMA interface_type
961 ArrayList interfaces = (ArrayList) $1;
969 opt_interface_member_declarations
973 opt_interface_member_declarations
975 | interface_member_declarations
978 interface_member_declarations
979 : interface_member_declaration
980 | interface_member_declarations interface_member_declaration
983 interface_member_declaration
984 : interface_method_declaration
986 InterfaceMethod m = (InterfaceMethod) $1;
988 CheckDef (current_interface.AddMethod (m), m.Name);
990 | interface_property_declaration
992 InterfaceProperty p = (InterfaceProperty) $1;
994 CheckDef (current_interface.AddProperty (p), p.Name);
996 | interface_event_declaration
998 InterfaceEvent e = (InterfaceEvent) $1;
1000 CheckDef (current_interface.AddEvent (e), e.Name);
1002 | interface_indexer_declaration
1004 InterfaceIndexer i = (InterfaceIndexer) $1;
1006 CheckDef (current_interface.AddIndexer (i), "indexer");
1011 : /* empty */ { $$ = false; }
1012 | NEW { $$ = true; }
1015 interface_method_declaration
1016 : opt_attributes opt_new type IDENTIFIER
1017 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1020 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1022 | opt_attributes opt_new VOID IDENTIFIER
1023 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1026 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1030 interface_property_declaration
1035 { lexer.properties = true; }
1037 { lexer.properties = false; }
1042 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
1043 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1);
1048 : opt_attributes GET SEMICOLON { $$ = 1; }
1049 | opt_attributes SET SEMICOLON { $$ = 2; }
1050 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1052 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1056 interface_event_declaration
1057 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1059 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1063 interface_indexer_declaration
1064 : opt_attributes opt_new type THIS
1065 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1067 { lexer.properties = true; }
1069 { lexer.properties = false; }
1072 int a_flags = (int) $10;
1074 bool do_get = (a_flags & 1) == 1;
1075 bool do_set = (a_flags & 2) == 2;
1077 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set, (bool) $2, (Attributes) $1);
1081 operator_declaration
1082 : opt_attributes opt_modifiers operator_declarator block
1084 OperatorDeclaration decl = (OperatorDeclaration) $3;
1086 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1087 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1089 // Note again, checking is done in semantic analysis
1090 current_container.AddOperator (op);
1095 : type OPERATOR overloadable_operator
1096 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1098 CheckUnaryOperator ((Operator.OpType) $3);
1100 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1101 null, null, lexer.Location);
1103 | type OPERATOR overloadable_operator
1105 type IDENTIFIER COMMA
1109 CheckBinaryOperator ((Operator.OpType) $3);
1111 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1112 (string) $8, (string) $9, lexer.Location);
1114 | conversion_operator_declarator
1117 overloadable_operator
1119 : BANG { $$ = Operator.OpType.Negate; }
1120 | TILDE { $$ = Operator.OpType.BitComplement; }
1121 | OP_INC { $$ = Operator.OpType.Increment; }
1122 | OP_DEC { $$ = Operator.OpType.Decrement; }
1123 | TRUE { $$ = Operator.OpType.True; }
1124 | FALSE { $$ = Operator.OpType.False; }
1125 // Unary and binary:
1126 | PLUS { $$ = Operator.OpType.Addition; }
1127 | MINUS { $$ = Operator.OpType.Subtraction; }
1129 | STAR { $$ = Operator.OpType.Multiply; }
1130 | DIV { $$ = Operator.OpType.Division; }
1131 | PERCENT { $$ = Operator.OpType.Modulus; }
1132 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1133 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1134 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1135 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1136 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1137 | OP_EQ { $$ = Operator.OpType.Equality; }
1138 | OP_NE { $$ = Operator.OpType.Inequality; }
1139 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1140 | OP_LT { $$ = Operator.OpType.LessThan; }
1141 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1142 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1145 conversion_operator_declarator
1146 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1148 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1149 null, null, lexer.Location);
1151 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1153 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1154 null, null, lexer.Location);
1158 constructor_declaration
1161 constructor_declarator
1164 Constructor c = (Constructor) $3;
1165 c.Block = (Block) $4;
1166 c.ModFlags = (int) $2;
1168 if ((c.ModFlags & Modifiers.STATIC) != 0){
1169 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1170 Location l = lexer.Location;
1171 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1174 if (c.Initializer != null){
1175 Location l = lexer.Location;
1176 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1179 if (!c.Parameters.Empty){
1180 Location l = lexer.Location;
1181 Report.Error (103, l, "Static constructors should not have parameters");
1185 CheckDef (current_container.AddConstructor (c), c.Name);
1187 current_local_parameters = null;
1191 constructor_declarator
1193 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1194 opt_constructor_initializer
1196 Location l = lexer.Location;
1198 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5, l);
1200 current_local_parameters = (Parameters) $3;
1204 opt_constructor_initializer
1205 : /* empty */ { $$ = null; }
1206 | constructor_initializer
1209 constructor_initializer
1210 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1212 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1214 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1216 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1220 destructor_declaration
1221 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1223 Method d = new Method ("System.Void", 0, "Finalize",
1224 new Parameters (null, null), (Attributes) $1, lexer.Location);
1226 d.Block = (Block) $6;
1227 CheckDef (current_container.AddMethod (d), d.Name);
1234 EVENT type variable_declarators SEMICOLON
1236 foreach (VariableDeclaration var in (ArrayList) $5) {
1238 // FIXME : Is this right ?
1239 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1240 (int) $2, null, null, (Attributes) $1);
1242 CheckDef (current_container.AddEvent (e), e.Name);
1248 EVENT type member_name
1249 OPEN_BRACE event_accessor_declarations CLOSE_BRACE
1251 DictionaryEntry pair = (DictionaryEntry) $7;
1252 Block add_block = null;
1253 Block rem_block = null;
1255 if (pair.Key != null)
1256 add_block = (Block) pair.Key;
1257 if (pair.Value != null)
1258 rem_block = (Block) pair.Value;
1260 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block, (Attributes) $1);
1262 CheckDef (current_container.AddEvent (e), e.Name);
1266 event_accessor_declarations
1267 : add_accessor_declaration remove_accessor_declaration
1269 $$ = new DictionaryEntry ($1, $2);
1271 | remove_accessor_declaration add_accessor_declaration
1273 $$ = new DictionaryEntry ($2, $1);
1277 add_accessor_declaration
1278 : opt_attributes ADD block
1284 remove_accessor_declaration
1285 : opt_attributes REMOVE block
1292 : opt_attributes opt_modifiers indexer_declarator
1295 IndexerDeclaration decl = (IndexerDeclaration) $3;
1297 lexer.properties = true;
1298 parsing_indexer = true;
1300 current_local_parameters = decl.param_list;
1302 accessor_declarations
1304 lexer.properties = false;
1305 parsing_indexer = false;
1309 // The signature is computed from the signature of the indexer. Look
1310 // at section 3.6 on the spec
1313 IndexerDeclaration decl = (IndexerDeclaration) $3;
1314 DictionaryEntry pair = (DictionaryEntry) $6;
1315 Block get_block = null;
1316 Block set_block = null;
1318 if (pair.Key != null)
1319 get_block = (Block) pair.Key;
1320 if (pair.Value != null)
1321 set_block = (Block) pair.Value;
1323 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1324 get_block, set_block, (Attributes) $1);
1326 // Note that there is no equivalent of CheckDef for this case
1327 // We shall handle this in semantic analysis
1329 current_container.AddIndexer (indexer);
1331 current_local_parameters = null;
1336 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1338 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
1340 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1342 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
1354 string name = (string) $4;
1355 Enum e = new Enum ((string) $5, (int) $2, name, (Attributes) $1, lexer.Location);
1357 foreach (VariableDeclaration ev in (ArrayList) $6){
1358 CheckDef (e.AddEnumMember (ev.identifier,
1359 (Expression) ev.expression_or_array_initializer),
1363 CheckDef (current_container.AddEnum (e), name);
1368 : /* empty */ { $$ = "System.Int32"; }
1369 | COLON integral_type { $$ = $2; }
1373 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1379 opt_enum_member_declarations
1380 : /* empty */ { $$ = new ArrayList (); }
1381 | enum_member_declarations opt_comma { $$ = $1; }
1384 enum_member_declarations
1385 : enum_member_declaration
1387 ArrayList l = new ArrayList ();
1392 | enum_member_declarations COMMA enum_member_declaration
1394 ArrayList l = (ArrayList) $1;
1402 enum_member_declaration
1403 : opt_attributes IDENTIFIER
1405 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1407 | opt_attributes IDENTIFIER ASSIGN expression
1409 $$ = new VariableDeclaration ((string) $2, $4, lexer.Location);
1413 delegate_declaration
1417 IDENTIFIER OPEN_PARENS
1418 formal_parameter_list
1422 Delegate del = new Delegate (rc, (string) $4, (int) $2, MakeName ((string) $5), (Parameters) $7,
1423 (Attributes) $1, lexer.Location);
1425 CheckDef (current_container.AddDelegate (del), del.Name);
1430 IDENTIFIER OPEN_PARENS
1431 formal_parameter_list
1435 Delegate del = new Delegate (rc, "System.Void", (int) $2, (string) $5, (Parameters) $7,
1436 (Attributes) $1, lexer.Location);
1438 CheckDef (current_container.AddDelegate (del), del.Name);
1443 : namespace_or_type_name
1446 namespace_or_type_name
1447 : qualified_identifier
1451 * Before you think of adding a return_type, notice that we have been
1452 * using two rules in the places where it matters (one rule using type
1453 * and another identical one that uses VOID as the return type). This
1454 * gets rid of a shift/reduce couple
1457 : type_name { /* class_type */
1459 This does interfaces, delegates, struct_types, class_types,
1460 parent classes, and more! 4.2
1471 ArrayList types = new ArrayList ();
1476 | type_list COMMA type
1478 ArrayList types = (ArrayList) $1;
1486 * replaces all the productions for isolating the various
1487 * simple types, but we need this to reuse it easily in local_variable_type
1490 : OBJECT { $$ = "System.Object"; }
1491 | STRING { $$ = "System.String"; }
1492 | BOOL { $$ = "System.Boolean"; }
1493 | DECIMAL { $$ = "System.Decimal"; }
1494 | FLOAT { $$ = "System.Single"; }
1495 | DOUBLE { $$ = "System.Double"; }
1500 : SBYTE { $$ = "System.SByte"; }
1501 | BYTE { $$ = "System.Byte"; }
1502 | SHORT { $$ = "System.Int16"; }
1503 | USHORT { $$ = "System.UInt16"; }
1504 | INT { $$ = "System.Int32"; }
1505 | UINT { $$ = "System.UInt32"; }
1506 | LONG { $$ = "System.Int64"; }
1507 | ULONG { $$ = "System.UInt64"; }
1508 | CHAR { $$ = "System.Char"; }
1516 : type rank_specifiers
1518 $$ = (string) $1 + (string) $2;
1523 // Expressions, section 7.5
1531 | qualified_identifier
1533 string name = (string) $1;
1536 $$ = DecomposeQI (name, lexer.Location);
1538 | parenthesized_expression
1540 | invocation_expression
1544 | post_increment_expression
1545 | post_decrement_expression
1549 | checked_expression
1550 | unchecked_expression
1557 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1558 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1559 | NULL { $$ = new NullLiteral (); }
1563 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1564 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1565 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1570 object v = lexer.Value;
1573 // FIXME: Possible optimization would be to
1574 // compute the *Literal objects directly in the scanner
1577 $$ = new IntLiteral ((Int32) v);
1579 $$ = new UIntLiteral ((UInt32) v);
1581 $$ = new LongLiteral ((Int64) v);
1582 else if (v is ulong)
1583 $$ = new ULongLiteral ((UInt64) v);
1585 Console.WriteLine ("OOPS. Unexpected result from scanner");
1590 : TRUE { $$ = new BoolLiteral (true); }
1591 | FALSE { $$ = new BoolLiteral (false); }
1594 parenthesized_expression
1595 : OPEN_PARENS expression CLOSE_PARENS
1600 : primary_expression DOT IDENTIFIER
1602 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1604 | predefined_type DOT IDENTIFIER
1606 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1614 invocation_expression
1615 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1618 // if $1 is MethodGroup
1619 // $$ = new Call ($1, $3);
1621 // $$ = new DelegateCall ($1, $3);
1623 Location l = lexer.Location;
1624 Report.Error (1, l, "THIS IS CRAZY");
1626 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1632 : /* empty */ { $$ = null; }
1639 ArrayList list = new ArrayList ();
1643 | argument_list COMMA argument
1645 ArrayList list = (ArrayList) $1;
1654 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1656 | REF variable_reference
1658 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1660 | OUT variable_reference
1662 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1667 : expression { note ("section 5.4"); $$ = $1; }
1671 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1673 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1680 ArrayList list = new ArrayList ();
1684 | expression_list COMMA expression
1686 ArrayList list = (ArrayList) $1;
1695 $$ = new This (lexer.Location);
1700 : BASE DOT IDENTIFIER
1702 $$ = new BaseAccess (BaseAccess.BaseAccessType.Member, (string) $3, null);
1704 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1706 $$ = new BaseAccess (BaseAccess.BaseAccessType.Indexer, null, (ArrayList) $3);
1710 post_increment_expression
1711 : primary_expression OP_INC
1713 $$ = new Unary (Unary.Operator.PostIncrement, (Expression) $1, lexer.Location);
1717 post_decrement_expression
1718 : primary_expression OP_DEC
1720 $$ = new Unary (Unary.Operator.PostDecrement, (Expression) $1, lexer.Location);
1725 : object_or_delegate_creation_expression
1726 | array_creation_expression
1729 object_or_delegate_creation_expression
1730 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1732 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1736 array_creation_expression
1737 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
1739 opt_array_initializer
1741 $$ = new New ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
1761 | rank_specifiers rank_specifier
1763 $$ = (string) $2 + (string) $1;
1768 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1770 $$ = "[" + (string) $2 + "]";
1790 | dim_separators COMMA
1792 $$ = (string) $1 + ",";
1796 opt_array_initializer
1808 : OPEN_BRACE CLOSE_BRACE
1810 ArrayList list = new ArrayList ();
1813 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1815 $$ = (ArrayList) $2;
1819 variable_initializer_list
1820 : variable_initializer
1822 ArrayList list = new ArrayList ();
1826 | variable_initializer_list COMMA variable_initializer
1828 ArrayList list = (ArrayList) $1;
1835 : TYPEOF OPEN_PARENS type CLOSE_PARENS
1837 $$ = new TypeOf ((string) $3);
1842 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
1843 $$ = new SizeOf ((string) $3);
1845 note ("Verify type is unmanaged");
1846 note ("if (5.8) builtin, yield constant expression");
1851 : CHECKED OPEN_PARENS expression CLOSE_PARENS
1853 $$ = new CheckedExpr ((Expression) $3);
1857 unchecked_expression
1858 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
1860 $$ = new UnCheckedExpr ((Expression) $3);
1865 : primary_expression
1866 | PLUS unary_expression
1868 $$ = new Unary (Unary.Operator.Addition, (Expression) $2, lexer.Location);
1870 | MINUS unary_expression
1872 $$ = new Unary (Unary.Operator.Subtraction, (Expression) $2, lexer.Location);
1874 | BANG unary_expression
1876 $$ = new Unary (Unary.Operator.Negate, (Expression) $2, lexer.Location);
1878 | TILDE unary_expression
1880 $$ = new Unary (Unary.Operator.BitComplement, (Expression) $2, lexer.Location);
1882 | STAR unary_expression
1884 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
1886 | BITWISE_AND unary_expression
1888 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
1890 | OP_INC unary_expression
1892 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1894 | OP_DEC unary_expression
1896 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1900 we can not do cast expressions at this level,
1901 as there is an ambiguity. Check "Cast Expressions" 7.6.8
1902 for the recipe to handle this.
1906 pre_increment_expression
1907 : OP_INC unary_expression
1909 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1913 pre_decrement_expression
1914 : OP_DEC unary_expression
1916 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1922 * FIXME: This is actually wrong, it should be `type' but that
1923 * introduces a lot of {shift,reduce}/reduces
1925 * This is really really wrong. We need to track down
1926 * the source of problems with QIs because expressions like:
1927 * foreach (string s in (string []) object) wont be parsed.
1929 : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
1931 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
1933 | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
1935 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
1939 multiplicative_expression
1941 | multiplicative_expression STAR unary_expression
1943 $$ = new Binary (Binary.Operator.Multiply,
1944 (Expression) $1, (Expression) $3, lexer.Location);
1946 | multiplicative_expression DIV unary_expression
1948 $$ = new Binary (Binary.Operator.Division,
1949 (Expression) $1, (Expression) $3, lexer.Location);
1951 | multiplicative_expression PERCENT unary_expression
1953 $$ = new Binary (Binary.Operator.Modulus,
1954 (Expression) $1, (Expression) $3, lexer.Location);
1959 : multiplicative_expression
1960 | additive_expression PLUS multiplicative_expression
1962 $$ = new Binary (Binary.Operator.Addition,
1963 (Expression) $1, (Expression) $3, lexer.Location);
1965 | additive_expression MINUS multiplicative_expression
1967 $$ = new Binary (Binary.Operator.Subtraction,
1968 (Expression) $1, (Expression) $3, lexer.Location);
1973 : additive_expression
1974 | shift_expression OP_SHIFT_LEFT additive_expression
1976 $$ = new Binary (Binary.Operator.LeftShift,
1977 (Expression) $1, (Expression) $3, lexer.Location);
1979 | shift_expression OP_SHIFT_RIGHT additive_expression
1981 $$ = new Binary (Binary.Operator.RightShift,
1982 (Expression) $1, (Expression) $3, lexer.Location);
1986 relational_expression
1988 | relational_expression OP_LT shift_expression
1990 $$ = new Binary (Binary.Operator.LessThan,
1991 (Expression) $1, (Expression) $3, lexer.Location);
1993 | relational_expression OP_GT shift_expression
1995 $$ = new Binary (Binary.Operator.GreaterThan,
1996 (Expression) $1, (Expression) $3, lexer.Location);
1998 | relational_expression OP_LE shift_expression
2000 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2001 (Expression) $1, (Expression) $3, lexer.Location);
2003 | relational_expression OP_GE shift_expression
2005 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2006 (Expression) $1, (Expression) $3, lexer.Location);
2008 | relational_expression IS type
2010 $$ = new Probe (Probe.Operator.Is,
2011 (Expression) $1, (string) $3);
2013 | relational_expression AS type
2015 $$ = new Probe (Probe.Operator.As,
2016 (Expression) $1, (string) $3);
2021 : relational_expression
2022 | equality_expression OP_EQ relational_expression
2024 $$ = new Binary (Binary.Operator.Equality,
2025 (Expression) $1, (Expression) $3, lexer.Location);
2027 | equality_expression OP_NE relational_expression
2029 $$ = new Binary (Binary.Operator.Inequality,
2030 (Expression) $1, (Expression) $3, lexer.Location);
2035 : equality_expression
2036 | and_expression BITWISE_AND equality_expression
2038 $$ = new Binary (Binary.Operator.BitwiseAnd,
2039 (Expression) $1, (Expression) $3, lexer.Location);
2043 exclusive_or_expression
2045 | exclusive_or_expression CARRET and_expression
2047 $$ = new Binary (Binary.Operator.ExclusiveOr,
2048 (Expression) $1, (Expression) $3, lexer.Location);
2052 inclusive_or_expression
2053 : exclusive_or_expression
2054 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2056 $$ = new Binary (Binary.Operator.BitwiseOr,
2057 (Expression) $1, (Expression) $3, lexer.Location);
2061 conditional_and_expression
2062 : inclusive_or_expression
2063 | conditional_and_expression OP_AND inclusive_or_expression
2065 $$ = new Binary (Binary.Operator.LogicalAnd,
2066 (Expression) $1, (Expression) $3, lexer.Location);
2070 conditional_or_expression
2071 : conditional_and_expression
2072 | conditional_or_expression OP_OR conditional_and_expression
2074 $$ = new Binary (Binary.Operator.LogicalOr,
2075 (Expression) $1, (Expression) $3, lexer.Location);
2079 conditional_expression
2080 : conditional_or_expression
2081 | conditional_or_expression INTERR expression COLON expression
2083 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2087 assignment_expression
2088 : unary_expression ASSIGN expression
2090 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2092 | unary_expression OP_MULT_ASSIGN expression
2094 Location l = lexer.Location;
2096 $$ = new Assign ((Expression) $1,
2097 new Binary (Binary.Operator.Multiply,
2099 (Expression) $3, l), l);
2101 | unary_expression OP_DIV_ASSIGN expression
2103 Location l = lexer.Location;
2105 $$ = new Assign ((Expression) $1,
2106 new Binary (Binary.Operator.Division,
2108 (Expression) $3, l), l);
2110 | unary_expression OP_MOD_ASSIGN expression
2112 Location l = lexer.Location;
2114 $$ = new Assign ((Expression) $1,
2115 new Binary (Binary.Operator.Modulus,
2117 (Expression) $3, l), l);
2119 | unary_expression OP_ADD_ASSIGN expression
2121 Location l = lexer.Location;
2123 $$ = new Assign ((Expression) $1,
2124 new Binary (Binary.Operator.Addition,
2126 (Expression) $3, l), l);
2128 | unary_expression OP_SUB_ASSIGN expression
2130 Location l = lexer.Location;
2132 $$ = new Assign ((Expression) $1,
2133 new Binary (Binary.Operator.Subtraction,
2135 (Expression) $3, l), l);
2137 | unary_expression OP_SHIFT_LEFT_ASSIGN expression
2139 Location l = lexer.Location;
2141 $$ = new Assign ((Expression) $1,
2142 new Binary (Binary.Operator.LeftShift,
2144 (Expression) $3, l), l);
2146 | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2148 Location l = lexer.Location;
2150 $$ = new Assign ((Expression) $1,
2151 new Binary (Binary.Operator.RightShift,
2153 (Expression) $3, l), l);
2155 | unary_expression OP_AND_ASSIGN expression
2157 Location l = lexer.Location;
2159 $$ = new Assign ((Expression) $1,
2160 new Binary (Binary.Operator.BitwiseAnd,
2162 (Expression) $3, l), l);
2164 | unary_expression OP_OR_ASSIGN expression
2166 Location l = lexer.Location;
2168 $$ = new Assign ((Expression) $1,
2169 new Binary (Binary.Operator.BitwiseOr,
2171 (Expression) $3, l), l);
2173 | unary_expression OP_XOR_ASSIGN expression
2175 Location l = lexer.Location;
2177 $$ = new Assign ((Expression) $1,
2178 new Binary (Binary.Operator.ExclusiveOr,
2180 (Expression) $3, l), l);
2185 : conditional_expression
2186 | assignment_expression
2194 : expression { CheckBoolean ((Expression) $1); $$ = $1; }
2206 string full_class_name = MakeName ((string) $4);
2208 new_class = new Class (rc, current_container, full_class_name, (int) $2,
2209 (Attributes) $1, lexer.Location);
2210 current_container = new_class;
2211 current_container.Namespace = current_namespace;
2212 tree.RecordClass (full_class_name, new_class);
2218 Class new_class = (Class) current_container;
2221 new_class.Bases = (ArrayList) $6;
2223 current_container = current_container.Parent;
2224 CheckDef (current_container.AddClass (new_class), new_class.Name);
2231 : /* empty */ { $$ = (int) 0; }
2237 | modifiers modifier
2242 if ((m1 & m2) != 0) {
2243 Location l = lexer.Location;
2244 Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2246 $$ = (int) (m1 | m2);
2251 : NEW { $$ = Modifiers.NEW; }
2252 | PUBLIC { $$ = Modifiers.PUBLIC; }
2253 | PROTECTED { $$ = Modifiers.PROTECTED; }
2254 | INTERNAL { $$ = Modifiers.INTERNAL; }
2255 | PRIVATE { $$ = Modifiers.PRIVATE; }
2256 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2257 | SEALED { $$ = Modifiers.SEALED; }
2258 | STATIC { $$ = Modifiers.STATIC; }
2259 | READONLY { $$ = Modifiers.READONLY; }
2260 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2261 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2262 | EXTERN { $$ = Modifiers.EXTERN; }
2266 : /* empty */ { $$ = null; }
2267 | class_base { $$ = $1; }
2271 : COLON type_list { $$ = $2; }
2279 // A block is "contained" on the following places:
2281 // property_declaration as part of the accessor body (get/set)
2282 // operator_declaration
2283 // constructor_declaration
2284 // destructor_declaration
2285 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2290 current_block = new Block (current_block);
2292 opt_statement_list CLOSE_BRACE
2294 while (current_block.Implicit)
2295 current_block = current_block.Parent;
2297 current_block = current_block.Parent;
2308 | statement_list statement
2312 : declaration_statement
2314 if ((Block) $1 != current_block){
2315 current_block.AddStatement ((Statement) $1);
2316 current_block = (Block) $1;
2319 | embedded_statement
2321 current_block.AddStatement ((Statement) $1);
2325 current_block.AddStatement ((Statement) $1);
2332 | expression_statement
2333 | selection_statement
2334 | iteration_statement
2338 | unchecked_statement
2346 $$ = new EmptyStatement ();
2351 : IDENTIFIER COLON statement
2353 string lab = (String) $1;
2356 block = new Block (current_block, lab);
2357 block.AddStatement ((Statement) $3);
2360 if (!current_block.AddLabel (lab, block)){
2361 Location l = lexer.Location;
2362 Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2368 declaration_statement
2369 : local_variable_declaration SEMICOLON // done
2370 | local_constant_declaration SEMICOLON // finishme
2374 * The following is from Rhys' grammar:
2375 * > Types in local variable declarations must be recognized as
2376 * > expressions to prevent reduce/reduce errors in the grammar.
2377 * > The expressions are converted into types during semantic analysis.
2380 : primary_expression opt_rank_specifier
2382 // FIXME: Do something smart here regarding the composition of the type.
2384 // Ok, the above "primary_expression" is there to get rid of
2385 // both reduce/reduce and shift/reduces in the grammar, it should
2386 // really just be "type_name". If you use type_name, a reduce/reduce
2387 // creeps up. If you use qualified_identifier (which is all we need
2388 // really) two shift/reduces appear.
2390 // So, instead we do a super trick: we just allow ($1) to be a
2391 // SimpleName Expression.
2393 if (((Expression) $1) is SimpleName)
2394 $$ = ((SimpleName) $1).Name;
2396 Location l = lexer.Location;
2397 Report.Error (-1, l, "Invalid Type definition");
2398 $$ = "System.Object";
2401 | builtin_types opt_rank_specifier
2403 $$ = (string) $1 + (string) $2;
2407 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2410 // // FIXME: this is a string that represents the type
2411 // // Figure out something to make this work.
2416 local_variable_declaration
2417 : local_variable_type variable_declarators
2419 $$ = declare_local_variables ((string) $1, (ArrayList) $2);
2423 local_constant_declaration
2424 : CONST type constant_declarator
2428 expression_statement
2429 : statement_expression SEMICOLON
2436 // We have to do the wrapping here and not in the case above,
2437 // because statement_expression is used for example in for_statement
2439 statement_expression
2440 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2441 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2442 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2443 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2444 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2445 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2446 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2449 object_creation_expression
2450 : object_or_delegate_creation_expression
2451 { note ("complain if this is a delegate maybe?"); }
2460 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2463 $$ = new If ((Expression) $3, (Statement) $5);
2465 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2466 embedded_statement ELSE embedded_statement
2468 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2473 : SWITCH OPEN_PARENS expression CLOSE_PARENS
2476 $$ = new Switch ((Expression) $3, (ArrayList) $5);
2490 : /* empty */ { $$ = new ArrayList (); }
2497 ArrayList sections = new ArrayList ();
2502 | switch_sections switch_section
2504 ArrayList sections = (ArrayList) $1;
2514 current_block = new Block (current_block);
2518 while (current_block.Implicit)
2519 current_block = current_block.Parent;
2520 $$ = new SwitchSection ((ArrayList) $1, current_block);
2521 current_block = current_block.Parent;
2528 ArrayList labels = new ArrayList ();
2533 | switch_labels switch_label
2535 ArrayList labels = (ArrayList) ($1);
2543 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2); }
2544 | DEFAULT COLON { $$ = new SwitchLabel (null); }
2555 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2557 $$ = new While ((Expression) $3, (Statement) $5);
2562 : DO embedded_statement
2563 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2565 $$ = new Do ((Statement) $2, (Expression) $5);
2571 opt_for_initializer SEMICOLON
2572 opt_for_condition SEMICOLON
2573 opt_for_iterator CLOSE_PARENS
2576 $$ = new For ((Statement) $3, (Expression) $5, (Statement) $7, (Statement) $9);
2581 : /* empty */ { $$ = new EmptyStatement (); }
2586 : local_variable_declaration
2588 if ((Block) $1 != current_block){
2589 current_block.AddStatement ((Statement) $1);
2590 current_block = (Block) $1;
2593 | statement_expression_list
2597 : /* empty */ { $$ = new BoolLiteral (true); }
2598 | boolean_expression
2602 : /* empty */ { $$ = new EmptyStatement (); }
2607 : statement_expression_list
2610 statement_expression_list
2611 : statement_expression
2613 Block b = new Block (null, true);
2615 b.AddStatement ((Statement) $1);
2618 | statement_expression_list COMMA statement_expression
2620 Block b = (Block) $1;
2622 b.AddStatement ((Statement) $3);
2628 : FOREACH OPEN_PARENS type IDENTIFIER IN
2630 $1 = lexer.Location;
2632 expression CLOSE_PARENS
2635 Block foreach_block = new Block (current_block, true);
2636 LocalVariableReference v;
2638 foreach_block.AddVariable ((string) $3, (string) $4, (Location) $1);
2639 v = new LocalVariableReference (foreach_block, (string) $4);
2640 foreach_block.AddStatement (new Foreach ((string) $3, v, (Expression) $7,
2641 (Statement) $9, (Location) $1));
2648 | continue_statement
2662 : CONTINUE SEMICOLON
2664 $$ = new Continue ();
2669 : GOTO IDENTIFIER SEMICOLON
2671 $$ = new Goto ((string) $2);
2673 | GOTO CASE constant_expression SEMICOLON
2674 | GOTO DEFAULT SEMICOLON
2678 : RETURN opt_expression SEMICOLON
2680 $$ = new Return ((Expression) $2, lexer.Location);
2685 : THROW opt_expression SEMICOLON
2687 $$ = new Throw ((Expression) $2);
2697 : TRY block catch_clauses
2700 ArrayList s = new ArrayList ();
2702 foreach (Catch cc in (ArrayList) $3) {
2703 if (cc.Type == null)
2709 // Now s contains the list of specific catch clauses
2710 // and g contains the general one.
2712 $$ = new Try ((Block) $2, s, g, null);
2714 | TRY block opt_catch_clauses FINALLY block
2717 ArrayList s = new ArrayList ();
2719 foreach (Catch cc in (ArrayList) $3) {
2720 if (cc.Type == null)
2726 $$ = new Try ((Block) $2, s, g, (Block) $5);
2731 : /* empty */ { $$ = null; }
2738 ArrayList l = new ArrayList ();
2743 | catch_clauses catch_clause
2745 ArrayList l = (ArrayList) $1;
2753 : /* empty */ { $$ = null; }
2758 : CATCH opt_catch_args
2760 string type = null, id = null;
2763 DictionaryEntry cc = (DictionaryEntry) $2;
2764 type = (string) cc.Key;
2765 id = (string) cc.Value;
2768 ArrayList one = new ArrayList ();
2769 one.Add (new VariableDeclaration (id, null, lexer.Location));
2772 current_block = new Block (current_block);
2773 Block b = declare_local_variables (type, one);
2780 string type = null, id = null;
2783 DictionaryEntry cc = (DictionaryEntry) $2;
2784 type = (string) cc.Key;
2785 id = (string) cc.Value;
2788 while (current_block != (Block) $1)
2789 current_block = current_block.Parent;
2794 $$ = new Catch (type, id , (Block) $4);
2799 : /* empty */ { $$ = null; }
2804 : OPEN_PARENS type opt_identifier CLOSE_PARENS
2806 $$ = new DictionaryEntry ($2, $3);
2813 $$ = new Checked ((Block) $2);
2820 $$ = new Unchecked ((Block) $2);
2825 : LOCK OPEN_PARENS expression CLOSE_PARENS embedded_statement
2827 $$ = new Lock ((Expression) $3, (Statement) $5);
2832 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS embedded_statement
2836 resource_acquisition
2837 : local_variable_declaration
2839 // FIXME: Add the current block chaining for local_variable_decl
2848 // A class used to pass around variable declarations and constants
2850 public class VariableDeclaration {
2851 public string identifier;
2852 public object expression_or_array_initializer;
2853 public Location Location;
2855 public VariableDeclaration (string id, object eoai, Location l){
2856 this.identifier = id;
2857 this.expression_or_array_initializer = eoai;
2863 // A class used to hold info about an indexer declarator
2866 public class IndexerDeclaration {
2868 public string interface_type;
2869 public Parameters param_list;
2871 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
2874 this.interface_type = interface_type;
2875 this.param_list = param_list;
2880 // A class used to hold info about an operator declarator
2883 public class OperatorDeclaration {
2884 public Operator.OpType optype;
2885 public string ret_type;
2886 public string arg1type;
2887 public string arg1name;
2888 public string arg2type;
2889 public string arg2name;
2890 public Location location;
2892 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
2893 string arg2type, string arg2name, Location location)
2896 this.ret_type = ret_type;
2897 this.arg1type = arg1type;
2898 this.arg1name = arg1name;
2899 this.arg2type = arg2type;
2900 this.arg2name = arg2name;
2901 this.location = location;
2907 // Given the @class_name name, it creates a fully qualified name
2908 // based on the containing declaration space
2911 MakeName (string class_name)
2913 string ns = current_namespace.Name;
2914 string container_name = current_container.Name;
2916 if (container_name == ""){
2918 return ns + "." + class_name;
2922 return container_name + "." + class_name;
2926 // Used to report back to the user the result of a declaration
2927 // in the current declaration space
2930 CheckDef (DeclSpace.AdditionResult result, string name)
2932 if (result == DeclSpace.AdditionResult.Success)
2935 Location l = lexer.Location;
2938 case DeclSpace.AdditionResult.NameExists:
2939 Report.Error (102, l, "The namespace `" + current_container.Name +
2940 "' already contains a definition for `"+
2945 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
2947 // case DeclSpace.AdditionResult.MethodDuplicated:
2948 // error (111, "Class `"+current_container.Name+
2949 // "' already defines a member called '" +
2950 // name + "' with the same parameter types");
2953 case DeclSpace.AdditionResult.EnclosingClash:
2954 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
2957 case DeclSpace.AdditionResult.NotAConstructor:
2958 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
2964 CheckDef (bool result, string name)
2968 CheckDef (DeclSpace.AdditionResult.NameExists, name);
2972 SimpleLookup (string name)
2975 // we need to check against current_block not being null
2976 // as `expression' is allowed in argument_lists, which
2977 // do not exist inside a block.
2979 if (current_block != null){
2980 if (current_block.IsVariableDefined (name))
2981 return new LocalVariableReference (current_block, name);
2984 if (current_local_parameters != null){
2986 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
2988 return new ParameterReference (current_local_parameters, idx, name);
2994 Expression DecomposeQI (string name, Location l)
2998 if (name.IndexOf ('.') == -1){
2999 o = SimpleLookup (name);
3001 return new SimpleName (name, l);
3004 int pos = name.LastIndexOf (".");
3005 string left = name.Substring (0, pos);
3006 string right = name.Substring (pos + 1);
3008 o = DecomposeQI (left, l);
3009 return new MemberAccess (o, right, l);
3013 Block declare_local_variables (string type, ArrayList variable_declarators)
3015 Block implicit_block;
3016 ArrayList inits = null;
3019 // We use the `Used' property to check whether statements
3020 // have been added to the current block. If so, we need
3021 // to create another block to contain the new declaration
3022 // otherwise, as an optimization, we use the same block to
3023 // add the declaration.
3025 // FIXME: A further optimization is to check if the statements
3026 // that were added were added as part of the initialization
3027 // below. In which case, no other statements have been executed
3028 // and we might be able to reduce the number of blocks for
3029 // situations like this:
3031 // int j = 1; int k = j + 1;
3033 if (current_block.Used)
3034 implicit_block = new Block (current_block, true);
3036 implicit_block = current_block;
3038 foreach (VariableDeclaration decl in variable_declarators){
3039 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3040 if (decl.expression_or_array_initializer != null){
3042 inits = new ArrayList ();
3046 Location l = lexer.Location;
3047 Report.Error (128, l, "A local variable `" + decl.identifier +
3048 "' is already defined in this scope");
3053 return implicit_block;
3055 foreach (VariableDeclaration decl in inits){
3056 if (decl.expression_or_array_initializer is Expression){
3057 Expression expr = (Expression) decl.expression_or_array_initializer;
3060 assign = new Assign (new LocalVariableReference (
3061 implicit_block, decl.identifier),
3062 expr, lexer.Location);
3063 implicit_block.AddStatement (new StatementExpression (assign));
3065 Console.WriteLine ("Not handling Array initializers yet");
3069 return implicit_block;
3072 void CheckConstant (Expression expr)
3077 void CheckBoolean (Expression expr)
3082 void CheckAttributeTarget (string a)
3086 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3090 Location l = lexer.Location;
3091 Report.Error (658, l, "Invalid attribute target");
3097 void CheckUnaryOperator (Operator.OpType op)
3101 case Operator.OpType.Negate:
3102 case Operator.OpType.BitComplement:
3103 case Operator.OpType.Increment:
3104 case Operator.OpType.Decrement:
3105 case Operator.OpType.True:
3106 case Operator.OpType.False:
3107 case Operator.OpType.Addition:
3108 case Operator.OpType.Subtraction:
3113 Location l = lexer.Location;
3114 Report.Error (1019, l, "Overloadable unary operator expected");
3120 void CheckBinaryOperator (Operator.OpType op)
3124 case Operator.OpType.Addition:
3125 case Operator.OpType.Subtraction:
3126 case Operator.OpType.Multiply:
3127 case Operator.OpType.Division:
3128 case Operator.OpType.Modulus:
3129 case Operator.OpType.BitwiseAnd:
3130 case Operator.OpType.BitwiseOr:
3131 case Operator.OpType.ExclusiveOr:
3132 case Operator.OpType.LeftShift:
3133 case Operator.OpType.RightShift:
3134 case Operator.OpType.Equality:
3135 case Operator.OpType.Inequality:
3136 case Operator.OpType.GreaterThan:
3137 case Operator.OpType.LessThan:
3138 case Operator.OpType.GreaterThanOrEqual:
3139 case Operator.OpType.LessThanOrEqual:
3143 Location l = lexer.Location;
3144 Report.Error (1020, l, "Overloadable binary operator expected");
3150 void output (string s)
3152 Console.WriteLine (s);
3155 void note (string s)
3157 // Used to put annotations
3162 public Tokenizer Lexer {
3168 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
3170 current_namespace = new Namespace (null, "");
3172 this.tree = rc.Tree;
3175 current_container = tree.Types;
3176 current_container.Namespace = current_namespace;
3178 lexer = new Tokenizer (input, name);
3181 public override int parse ()
3183 StringBuilder value = new StringBuilder ();
3187 if (yacc_verbose_flag)
3188 yyparse (lexer, new yydebug.yyDebugSimple ());
3191 } catch (Exception e){
3192 // Console.WriteLine ("Fatal error: " + name);
3193 // Console.WriteLine (lexer.location);
3195 Console.WriteLine (lexer.location + " : Parsing error ");
3196 Console.WriteLine (e);
3200 return global_errors;