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,
1423 MakeName ((string) $5), (Parameters) $7,
1424 (Attributes) $1, lexer.Location);
1426 CheckDef (current_container.AddDelegate (del), del.Name);
1431 IDENTIFIER OPEN_PARENS
1432 formal_parameter_list
1436 Delegate del = new Delegate (rc, "System.Void", (int) $2, (string) $5, (Parameters) $7,
1437 (Attributes) $1, lexer.Location);
1439 CheckDef (current_container.AddDelegate (del), del.Name);
1444 : namespace_or_type_name
1447 namespace_or_type_name
1448 : qualified_identifier
1452 * Before you think of adding a return_type, notice that we have been
1453 * using two rules in the places where it matters (one rule using type
1454 * and another identical one that uses VOID as the return type). This
1455 * gets rid of a shift/reduce couple
1458 : type_name { /* class_type */
1460 This does interfaces, delegates, struct_types, class_types,
1461 parent classes, and more! 4.2
1472 ArrayList types = new ArrayList ();
1477 | type_list COMMA type
1479 ArrayList types = (ArrayList) $1;
1487 * replaces all the productions for isolating the various
1488 * simple types, but we need this to reuse it easily in local_variable_type
1491 : OBJECT { $$ = "System.Object"; }
1492 | STRING { $$ = "System.String"; }
1493 | BOOL { $$ = "System.Boolean"; }
1494 | DECIMAL { $$ = "System.Decimal"; }
1495 | FLOAT { $$ = "System.Single"; }
1496 | DOUBLE { $$ = "System.Double"; }
1501 : SBYTE { $$ = "System.SByte"; }
1502 | BYTE { $$ = "System.Byte"; }
1503 | SHORT { $$ = "System.Int16"; }
1504 | USHORT { $$ = "System.UInt16"; }
1505 | INT { $$ = "System.Int32"; }
1506 | UINT { $$ = "System.UInt32"; }
1507 | LONG { $$ = "System.Int64"; }
1508 | ULONG { $$ = "System.UInt64"; }
1509 | CHAR { $$ = "System.Char"; }
1517 : type rank_specifiers
1519 $$ = (string) $1 + (string) $2;
1524 // Expressions, section 7.5
1532 | qualified_identifier
1534 string name = (string) $1;
1537 $$ = DecomposeQI (name, lexer.Location);
1539 | parenthesized_expression
1541 | invocation_expression
1545 | post_increment_expression
1546 | post_decrement_expression
1550 | checked_expression
1551 | unchecked_expression
1558 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1559 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1560 | NULL { $$ = new NullLiteral (); }
1564 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1565 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1566 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1571 object v = lexer.Value;
1574 // FIXME: Possible optimization would be to
1575 // compute the *Literal objects directly in the scanner
1578 $$ = new IntLiteral ((Int32) v);
1580 $$ = new UIntLiteral ((UInt32) v);
1582 $$ = new LongLiteral ((Int64) v);
1583 else if (v is ulong)
1584 $$ = new ULongLiteral ((UInt64) v);
1586 Console.WriteLine ("OOPS. Unexpected result from scanner");
1591 : TRUE { $$ = new BoolLiteral (true); }
1592 | FALSE { $$ = new BoolLiteral (false); }
1595 parenthesized_expression
1596 : OPEN_PARENS expression CLOSE_PARENS
1601 : primary_expression DOT IDENTIFIER
1603 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1605 | predefined_type DOT IDENTIFIER
1607 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1615 invocation_expression
1616 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1619 // if $1 is MethodGroup
1620 // $$ = new Call ($1, $3);
1622 // $$ = new DelegateCall ($1, $3);
1624 Location l = lexer.Location;
1625 Report.Error (1, l, "THIS IS CRAZY");
1627 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1633 : /* empty */ { $$ = null; }
1640 ArrayList list = new ArrayList ();
1644 | argument_list COMMA argument
1646 ArrayList list = (ArrayList) $1;
1655 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1657 | REF variable_reference
1659 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1661 | OUT variable_reference
1663 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1668 : expression { note ("section 5.4"); $$ = $1; }
1672 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1674 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1681 ArrayList list = new ArrayList ();
1685 | expression_list COMMA expression
1687 ArrayList list = (ArrayList) $1;
1696 $$ = new This (lexer.Location);
1701 : BASE DOT IDENTIFIER
1703 $$ = new BaseAccess (BaseAccess.BaseAccessType.Member, (string) $3, null);
1705 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1707 $$ = new BaseAccess (BaseAccess.BaseAccessType.Indexer, null, (ArrayList) $3);
1711 post_increment_expression
1712 : primary_expression OP_INC
1714 $$ = new Unary (Unary.Operator.PostIncrement, (Expression) $1, lexer.Location);
1718 post_decrement_expression
1719 : primary_expression OP_DEC
1721 $$ = new Unary (Unary.Operator.PostDecrement, (Expression) $1, lexer.Location);
1726 : object_or_delegate_creation_expression
1727 | array_creation_expression
1730 object_or_delegate_creation_expression
1731 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1733 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1737 array_creation_expression
1738 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
1740 opt_array_initializer
1742 $$ = new New ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
1762 | rank_specifiers rank_specifier
1764 $$ = (string) $2 + (string) $1;
1769 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1771 $$ = "[" + (string) $2 + "]";
1791 | dim_separators COMMA
1793 $$ = (string) $1 + ",";
1797 opt_array_initializer
1809 : OPEN_BRACE CLOSE_BRACE
1811 ArrayList list = new ArrayList ();
1814 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1816 $$ = (ArrayList) $2;
1820 variable_initializer_list
1821 : variable_initializer
1823 ArrayList list = new ArrayList ();
1827 | variable_initializer_list COMMA variable_initializer
1829 ArrayList list = (ArrayList) $1;
1836 : TYPEOF OPEN_PARENS type CLOSE_PARENS
1838 $$ = new TypeOf ((string) $3);
1843 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
1844 $$ = new SizeOf ((string) $3);
1846 note ("Verify type is unmanaged");
1847 note ("if (5.8) builtin, yield constant expression");
1852 : CHECKED OPEN_PARENS expression CLOSE_PARENS
1854 $$ = new CheckedExpr ((Expression) $3);
1858 unchecked_expression
1859 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
1861 $$ = new UnCheckedExpr ((Expression) $3);
1866 : primary_expression
1867 | PLUS unary_expression
1869 $$ = new Unary (Unary.Operator.Addition, (Expression) $2, lexer.Location);
1871 | MINUS unary_expression
1873 $$ = new Unary (Unary.Operator.Subtraction, (Expression) $2, lexer.Location);
1875 | BANG unary_expression
1877 $$ = new Unary (Unary.Operator.Negate, (Expression) $2, lexer.Location);
1879 | TILDE unary_expression
1881 $$ = new Unary (Unary.Operator.BitComplement, (Expression) $2, lexer.Location);
1883 | STAR unary_expression
1885 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
1887 | BITWISE_AND unary_expression
1889 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
1891 | OP_INC unary_expression
1893 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1895 | OP_DEC unary_expression
1897 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1901 we can not do cast expressions at this level,
1902 as there is an ambiguity. Check "Cast Expressions" 7.6.8
1903 for the recipe to handle this.
1907 pre_increment_expression
1908 : OP_INC unary_expression
1910 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1914 pre_decrement_expression
1915 : OP_DEC unary_expression
1917 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1923 * FIXME: This is actually wrong, it should be `type' but that
1924 * introduces a lot of {shift,reduce}/reduces
1926 * This is really really wrong. We need to track down
1927 * the source of problems with QIs because expressions like:
1928 * foreach (string s in (string []) object) wont be parsed.
1930 : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
1932 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
1934 | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
1936 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
1940 multiplicative_expression
1942 | multiplicative_expression STAR unary_expression
1944 $$ = new Binary (Binary.Operator.Multiply,
1945 (Expression) $1, (Expression) $3, lexer.Location);
1947 | multiplicative_expression DIV unary_expression
1949 $$ = new Binary (Binary.Operator.Division,
1950 (Expression) $1, (Expression) $3, lexer.Location);
1952 | multiplicative_expression PERCENT unary_expression
1954 $$ = new Binary (Binary.Operator.Modulus,
1955 (Expression) $1, (Expression) $3, lexer.Location);
1960 : multiplicative_expression
1961 | additive_expression PLUS multiplicative_expression
1963 $$ = new Binary (Binary.Operator.Addition,
1964 (Expression) $1, (Expression) $3, lexer.Location);
1966 | additive_expression MINUS multiplicative_expression
1968 $$ = new Binary (Binary.Operator.Subtraction,
1969 (Expression) $1, (Expression) $3, lexer.Location);
1974 : additive_expression
1975 | shift_expression OP_SHIFT_LEFT additive_expression
1977 $$ = new Binary (Binary.Operator.LeftShift,
1978 (Expression) $1, (Expression) $3, lexer.Location);
1980 | shift_expression OP_SHIFT_RIGHT additive_expression
1982 $$ = new Binary (Binary.Operator.RightShift,
1983 (Expression) $1, (Expression) $3, lexer.Location);
1987 relational_expression
1989 | relational_expression OP_LT shift_expression
1991 $$ = new Binary (Binary.Operator.LessThan,
1992 (Expression) $1, (Expression) $3, lexer.Location);
1994 | relational_expression OP_GT shift_expression
1996 $$ = new Binary (Binary.Operator.GreaterThan,
1997 (Expression) $1, (Expression) $3, lexer.Location);
1999 | relational_expression OP_LE shift_expression
2001 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2002 (Expression) $1, (Expression) $3, lexer.Location);
2004 | relational_expression OP_GE shift_expression
2006 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2007 (Expression) $1, (Expression) $3, lexer.Location);
2009 | relational_expression IS type
2011 $$ = new Probe (Probe.Operator.Is,
2012 (Expression) $1, (string) $3);
2014 | relational_expression AS type
2016 $$ = new Probe (Probe.Operator.As,
2017 (Expression) $1, (string) $3);
2022 : relational_expression
2023 | equality_expression OP_EQ relational_expression
2025 $$ = new Binary (Binary.Operator.Equality,
2026 (Expression) $1, (Expression) $3, lexer.Location);
2028 | equality_expression OP_NE relational_expression
2030 $$ = new Binary (Binary.Operator.Inequality,
2031 (Expression) $1, (Expression) $3, lexer.Location);
2036 : equality_expression
2037 | and_expression BITWISE_AND equality_expression
2039 $$ = new Binary (Binary.Operator.BitwiseAnd,
2040 (Expression) $1, (Expression) $3, lexer.Location);
2044 exclusive_or_expression
2046 | exclusive_or_expression CARRET and_expression
2048 $$ = new Binary (Binary.Operator.ExclusiveOr,
2049 (Expression) $1, (Expression) $3, lexer.Location);
2053 inclusive_or_expression
2054 : exclusive_or_expression
2055 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2057 $$ = new Binary (Binary.Operator.BitwiseOr,
2058 (Expression) $1, (Expression) $3, lexer.Location);
2062 conditional_and_expression
2063 : inclusive_or_expression
2064 | conditional_and_expression OP_AND inclusive_or_expression
2066 $$ = new Binary (Binary.Operator.LogicalAnd,
2067 (Expression) $1, (Expression) $3, lexer.Location);
2071 conditional_or_expression
2072 : conditional_and_expression
2073 | conditional_or_expression OP_OR conditional_and_expression
2075 $$ = new Binary (Binary.Operator.LogicalOr,
2076 (Expression) $1, (Expression) $3, lexer.Location);
2080 conditional_expression
2081 : conditional_or_expression
2082 | conditional_or_expression INTERR expression COLON expression
2084 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2088 assignment_expression
2089 : unary_expression ASSIGN expression
2091 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2093 | unary_expression OP_MULT_ASSIGN expression
2095 Location l = lexer.Location;
2097 $$ = new Assign ((Expression) $1,
2098 new Binary (Binary.Operator.Multiply,
2100 (Expression) $3, l), l);
2102 | unary_expression OP_DIV_ASSIGN expression
2104 Location l = lexer.Location;
2106 $$ = new Assign ((Expression) $1,
2107 new Binary (Binary.Operator.Division,
2109 (Expression) $3, l), l);
2111 | unary_expression OP_MOD_ASSIGN expression
2113 Location l = lexer.Location;
2115 $$ = new Assign ((Expression) $1,
2116 new Binary (Binary.Operator.Modulus,
2118 (Expression) $3, l), l);
2120 | unary_expression OP_ADD_ASSIGN expression
2122 Location l = lexer.Location;
2124 $$ = new Assign ((Expression) $1,
2125 new Binary (Binary.Operator.Addition,
2127 (Expression) $3, l), l);
2129 | unary_expression OP_SUB_ASSIGN expression
2131 Location l = lexer.Location;
2133 $$ = new Assign ((Expression) $1,
2134 new Binary (Binary.Operator.Subtraction,
2136 (Expression) $3, l), l);
2138 | unary_expression OP_SHIFT_LEFT_ASSIGN expression
2140 Location l = lexer.Location;
2142 $$ = new Assign ((Expression) $1,
2143 new Binary (Binary.Operator.LeftShift,
2145 (Expression) $3, l), l);
2147 | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2149 Location l = lexer.Location;
2151 $$ = new Assign ((Expression) $1,
2152 new Binary (Binary.Operator.RightShift,
2154 (Expression) $3, l), l);
2156 | unary_expression OP_AND_ASSIGN expression
2158 Location l = lexer.Location;
2160 $$ = new Assign ((Expression) $1,
2161 new Binary (Binary.Operator.BitwiseAnd,
2163 (Expression) $3, l), l);
2165 | unary_expression OP_OR_ASSIGN expression
2167 Location l = lexer.Location;
2169 $$ = new Assign ((Expression) $1,
2170 new Binary (Binary.Operator.BitwiseOr,
2172 (Expression) $3, l), l);
2174 | unary_expression OP_XOR_ASSIGN expression
2176 Location l = lexer.Location;
2178 $$ = new Assign ((Expression) $1,
2179 new Binary (Binary.Operator.ExclusiveOr,
2181 (Expression) $3, l), l);
2186 : conditional_expression
2187 | assignment_expression
2195 : expression { CheckBoolean ((Expression) $1); $$ = $1; }
2207 string full_class_name = MakeName ((string) $4);
2209 new_class = new Class (rc, current_container, full_class_name, (int) $2,
2210 (Attributes) $1, lexer.Location);
2211 current_container = new_class;
2212 current_container.Namespace = current_namespace;
2213 tree.RecordClass (full_class_name, new_class);
2219 Class new_class = (Class) current_container;
2222 new_class.Bases = (ArrayList) $6;
2224 current_container = current_container.Parent;
2225 CheckDef (current_container.AddClass (new_class), new_class.Name);
2232 : /* empty */ { $$ = (int) 0; }
2238 | modifiers modifier
2243 if ((m1 & m2) != 0) {
2244 Location l = lexer.Location;
2245 Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2247 $$ = (int) (m1 | m2);
2252 : NEW { $$ = Modifiers.NEW; }
2253 | PUBLIC { $$ = Modifiers.PUBLIC; }
2254 | PROTECTED { $$ = Modifiers.PROTECTED; }
2255 | INTERNAL { $$ = Modifiers.INTERNAL; }
2256 | PRIVATE { $$ = Modifiers.PRIVATE; }
2257 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2258 | SEALED { $$ = Modifiers.SEALED; }
2259 | STATIC { $$ = Modifiers.STATIC; }
2260 | READONLY { $$ = Modifiers.READONLY; }
2261 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2262 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2263 | EXTERN { $$ = Modifiers.EXTERN; }
2267 : /* empty */ { $$ = null; }
2268 | class_base { $$ = $1; }
2272 : COLON type_list { $$ = $2; }
2280 // A block is "contained" on the following places:
2282 // property_declaration as part of the accessor body (get/set)
2283 // operator_declaration
2284 // constructor_declaration
2285 // destructor_declaration
2286 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2291 current_block = new Block (current_block);
2293 opt_statement_list CLOSE_BRACE
2295 while (current_block.Implicit)
2296 current_block = current_block.Parent;
2298 current_block = current_block.Parent;
2309 | statement_list statement
2313 : declaration_statement
2315 if ((Block) $1 != current_block){
2316 current_block.AddStatement ((Statement) $1);
2317 current_block = (Block) $1;
2320 | embedded_statement
2322 current_block.AddStatement ((Statement) $1);
2326 current_block.AddStatement ((Statement) $1);
2333 | expression_statement
2334 | selection_statement
2335 | iteration_statement
2339 | unchecked_statement
2347 $$ = new EmptyStatement ();
2352 : IDENTIFIER COLON statement
2354 string lab = (String) $1;
2357 block = new Block (current_block, lab);
2358 block.AddStatement ((Statement) $3);
2361 if (!current_block.AddLabel (lab, block)){
2362 Location l = lexer.Location;
2363 Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2369 declaration_statement
2370 : local_variable_declaration SEMICOLON // done
2371 | local_constant_declaration SEMICOLON // finishme
2375 * The following is from Rhys' grammar:
2376 * > Types in local variable declarations must be recognized as
2377 * > expressions to prevent reduce/reduce errors in the grammar.
2378 * > The expressions are converted into types during semantic analysis.
2381 : primary_expression opt_rank_specifier
2383 // FIXME: Do something smart here regarding the composition of the type.
2385 // Ok, the above "primary_expression" is there to get rid of
2386 // both reduce/reduce and shift/reduces in the grammar, it should
2387 // really just be "type_name". If you use type_name, a reduce/reduce
2388 // creeps up. If you use qualified_identifier (which is all we need
2389 // really) two shift/reduces appear.
2391 // So, instead we do a super trick: we just allow ($1) to be a
2392 // SimpleName Expression.
2394 if (((Expression) $1) is SimpleName)
2395 $$ = ((SimpleName) $1).Name;
2397 Location l = lexer.Location;
2398 Report.Error (-1, l, "Invalid Type definition");
2399 $$ = "System.Object";
2402 | builtin_types opt_rank_specifier
2404 $$ = (string) $1 + (string) $2;
2408 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2411 // // FIXME: this is a string that represents the type
2412 // // Figure out something to make this work.
2417 local_variable_declaration
2418 : local_variable_type variable_declarators
2420 $$ = declare_local_variables ((string) $1, (ArrayList) $2);
2424 local_constant_declaration
2425 : CONST type constant_declarator
2429 expression_statement
2430 : statement_expression SEMICOLON
2437 // We have to do the wrapping here and not in the case above,
2438 // because statement_expression is used for example in for_statement
2440 statement_expression
2441 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2442 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2443 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2444 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2445 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2446 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2447 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2450 object_creation_expression
2451 : object_or_delegate_creation_expression
2452 { note ("complain if this is a delegate maybe?"); }
2461 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2464 $$ = new If ((Expression) $3, (Statement) $5);
2466 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2467 embedded_statement ELSE embedded_statement
2469 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2474 : SWITCH OPEN_PARENS expression CLOSE_PARENS
2477 $$ = new Switch ((Expression) $3, (ArrayList) $5);
2491 : /* empty */ { $$ = new ArrayList (); }
2498 ArrayList sections = new ArrayList ();
2503 | switch_sections switch_section
2505 ArrayList sections = (ArrayList) $1;
2515 current_block = new Block (current_block);
2519 while (current_block.Implicit)
2520 current_block = current_block.Parent;
2521 $$ = new SwitchSection ((ArrayList) $1, current_block);
2522 current_block = current_block.Parent;
2529 ArrayList labels = new ArrayList ();
2534 | switch_labels switch_label
2536 ArrayList labels = (ArrayList) ($1);
2544 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2); }
2545 | DEFAULT COLON { $$ = new SwitchLabel (null); }
2556 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2558 $$ = new While ((Expression) $3, (Statement) $5);
2563 : DO embedded_statement
2564 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2566 $$ = new Do ((Statement) $2, (Expression) $5);
2572 opt_for_initializer SEMICOLON
2573 opt_for_condition SEMICOLON
2574 opt_for_iterator CLOSE_PARENS
2577 $$ = new For ((Statement) $3, (Expression) $5, (Statement) $7, (Statement) $9);
2582 : /* empty */ { $$ = new EmptyStatement (); }
2587 : local_variable_declaration
2589 if ((Block) $1 != current_block){
2590 current_block.AddStatement ((Statement) $1);
2591 current_block = (Block) $1;
2594 | statement_expression_list
2598 : /* empty */ { $$ = new BoolLiteral (true); }
2599 | boolean_expression
2603 : /* empty */ { $$ = new EmptyStatement (); }
2608 : statement_expression_list
2611 statement_expression_list
2612 : statement_expression
2614 Block b = new Block (null, true);
2616 b.AddStatement ((Statement) $1);
2619 | statement_expression_list COMMA statement_expression
2621 Block b = (Block) $1;
2623 b.AddStatement ((Statement) $3);
2629 : FOREACH OPEN_PARENS type IDENTIFIER IN
2631 $1 = lexer.Location;
2633 expression CLOSE_PARENS
2636 Block foreach_block = new Block (current_block, true);
2637 LocalVariableReference v;
2639 foreach_block.AddVariable ((string) $3, (string) $4, (Location) $1);
2640 v = new LocalVariableReference (foreach_block, (string) $4);
2641 foreach_block.AddStatement (new Foreach ((string) $3, v, (Expression) $7,
2642 (Statement) $9, (Location) $1));
2649 | continue_statement
2658 $$ = new Break (lexer.Location);
2663 : CONTINUE SEMICOLON
2665 $$ = new Continue (lexer.Location);
2670 : GOTO IDENTIFIER SEMICOLON
2672 $$ = new Goto ((string) $2, lexer.Location);
2674 | GOTO CASE constant_expression SEMICOLON
2678 | GOTO DEFAULT SEMICOLON
2685 : RETURN opt_expression SEMICOLON
2687 $$ = new Return ((Expression) $2, lexer.Location);
2692 : THROW opt_expression SEMICOLON
2694 $$ = new Throw ((Expression) $2);
2704 : TRY block catch_clauses
2707 ArrayList s = new ArrayList ();
2709 foreach (Catch cc in (ArrayList) $3) {
2710 if (cc.Type == null)
2716 // Now s contains the list of specific catch clauses
2717 // and g contains the general one.
2719 $$ = new Try ((Block) $2, s, g, null);
2721 | TRY block opt_catch_clauses FINALLY block
2724 ArrayList s = new ArrayList ();
2726 foreach (Catch cc in (ArrayList) $3) {
2727 if (cc.Type == null)
2733 $$ = new Try ((Block) $2, s, g, (Block) $5);
2738 : /* empty */ { $$ = null; }
2745 ArrayList l = new ArrayList ();
2750 | catch_clauses catch_clause
2752 ArrayList l = (ArrayList) $1;
2760 : /* empty */ { $$ = null; }
2765 : CATCH opt_catch_args
2767 string type = null, id = null;
2770 DictionaryEntry cc = (DictionaryEntry) $2;
2771 type = (string) cc.Key;
2772 id = (string) cc.Value;
2775 ArrayList one = new ArrayList ();
2776 one.Add (new VariableDeclaration (id, null, lexer.Location));
2779 current_block = new Block (current_block);
2780 Block b = declare_local_variables (type, one);
2787 string type = null, id = null;
2790 DictionaryEntry cc = (DictionaryEntry) $2;
2791 type = (string) cc.Key;
2792 id = (string) cc.Value;
2795 while (current_block != (Block) $1)
2796 current_block = current_block.Parent;
2801 $$ = new Catch (type, id , (Block) $4);
2806 : /* empty */ { $$ = null; }
2811 : OPEN_PARENS type opt_identifier CLOSE_PARENS
2813 $$ = new DictionaryEntry ($2, $3);
2820 $$ = new Checked ((Block) $2);
2827 $$ = new Unchecked ((Block) $2);
2832 : LOCK OPEN_PARENS expression CLOSE_PARENS embedded_statement
2834 $$ = new Lock ((Expression) $3, (Statement) $5);
2839 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS embedded_statement
2843 resource_acquisition
2844 : local_variable_declaration
2846 // FIXME: Add the current block chaining for local_variable_decl
2855 // A class used to pass around variable declarations and constants
2857 public class VariableDeclaration {
2858 public string identifier;
2859 public object expression_or_array_initializer;
2860 public Location Location;
2862 public VariableDeclaration (string id, object eoai, Location l){
2863 this.identifier = id;
2864 this.expression_or_array_initializer = eoai;
2870 // A class used to hold info about an indexer declarator
2873 public class IndexerDeclaration {
2875 public string interface_type;
2876 public Parameters param_list;
2878 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
2881 this.interface_type = interface_type;
2882 this.param_list = param_list;
2887 // A class used to hold info about an operator declarator
2890 public class OperatorDeclaration {
2891 public Operator.OpType optype;
2892 public string ret_type;
2893 public string arg1type;
2894 public string arg1name;
2895 public string arg2type;
2896 public string arg2name;
2897 public Location location;
2899 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
2900 string arg2type, string arg2name, Location location)
2903 this.ret_type = ret_type;
2904 this.arg1type = arg1type;
2905 this.arg1name = arg1name;
2906 this.arg2type = arg2type;
2907 this.arg2name = arg2name;
2908 this.location = location;
2914 // Given the @class_name name, it creates a fully qualified name
2915 // based on the containing declaration space
2918 MakeName (string class_name)
2920 string ns = current_namespace.Name;
2921 string container_name = current_container.Name;
2923 if (container_name == ""){
2925 return ns + "." + class_name;
2929 return container_name + "." + class_name;
2933 // Used to report back to the user the result of a declaration
2934 // in the current declaration space
2937 CheckDef (DeclSpace.AdditionResult result, string name)
2939 if (result == DeclSpace.AdditionResult.Success)
2942 Location l = lexer.Location;
2945 case DeclSpace.AdditionResult.NameExists:
2946 Report.Error (102, l, "The namespace `" + current_container.Name +
2947 "' already contains a definition for `"+
2952 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
2954 // case DeclSpace.AdditionResult.MethodDuplicated:
2955 // error (111, "Class `"+current_container.Name+
2956 // "' already defines a member called '" +
2957 // name + "' with the same parameter types");
2960 case DeclSpace.AdditionResult.EnclosingClash:
2961 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
2964 case DeclSpace.AdditionResult.NotAConstructor:
2965 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
2971 CheckDef (bool result, string name)
2975 CheckDef (DeclSpace.AdditionResult.NameExists, name);
2979 SimpleLookup (string name)
2982 // we need to check against current_block not being null
2983 // as `expression' is allowed in argument_lists, which
2984 // do not exist inside a block.
2986 if (current_block != null){
2987 if (current_block.IsVariableDefined (name))
2988 return new LocalVariableReference (current_block, name);
2991 if (current_local_parameters != null){
2993 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
2995 return new ParameterReference (current_local_parameters, idx, name);
3001 Expression DecomposeQI (string name, Location l)
3005 if (name.IndexOf ('.') == -1){
3006 o = SimpleLookup (name);
3008 return new SimpleName (name, l);
3011 int pos = name.LastIndexOf (".");
3012 string left = name.Substring (0, pos);
3013 string right = name.Substring (pos + 1);
3015 o = DecomposeQI (left, l);
3016 return new MemberAccess (o, right, l);
3020 Block declare_local_variables (string type, ArrayList variable_declarators)
3022 Block implicit_block;
3023 ArrayList inits = null;
3026 // We use the `Used' property to check whether statements
3027 // have been added to the current block. If so, we need
3028 // to create another block to contain the new declaration
3029 // otherwise, as an optimization, we use the same block to
3030 // add the declaration.
3032 // FIXME: A further optimization is to check if the statements
3033 // that were added were added as part of the initialization
3034 // below. In which case, no other statements have been executed
3035 // and we might be able to reduce the number of blocks for
3036 // situations like this:
3038 // int j = 1; int k = j + 1;
3040 if (current_block.Used)
3041 implicit_block = new Block (current_block, true);
3043 implicit_block = current_block;
3045 foreach (VariableDeclaration decl in variable_declarators){
3046 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3047 if (decl.expression_or_array_initializer != null){
3049 inits = new ArrayList ();
3053 Location l = lexer.Location;
3054 Report.Error (128, l, "A local variable `" + decl.identifier +
3055 "' is already defined in this scope");
3060 return implicit_block;
3062 foreach (VariableDeclaration decl in inits){
3063 if (decl.expression_or_array_initializer is Expression){
3064 Expression expr = (Expression) decl.expression_or_array_initializer;
3067 assign = new Assign (new LocalVariableReference (
3068 implicit_block, decl.identifier),
3069 expr, lexer.Location);
3070 implicit_block.AddStatement (new StatementExpression (assign));
3072 Console.WriteLine ("Not handling Array initializers yet");
3076 return implicit_block;
3079 void CheckConstant (Expression expr)
3084 void CheckBoolean (Expression expr)
3089 void CheckAttributeTarget (string a)
3093 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3097 Location l = lexer.Location;
3098 Report.Error (658, l, "Invalid attribute target");
3104 void CheckUnaryOperator (Operator.OpType op)
3108 case Operator.OpType.Negate:
3109 case Operator.OpType.BitComplement:
3110 case Operator.OpType.Increment:
3111 case Operator.OpType.Decrement:
3112 case Operator.OpType.True:
3113 case Operator.OpType.False:
3114 case Operator.OpType.Addition:
3115 case Operator.OpType.Subtraction:
3120 Location l = lexer.Location;
3121 Report.Error (1019, l, "Overloadable unary operator expected");
3127 void CheckBinaryOperator (Operator.OpType op)
3131 case Operator.OpType.Addition:
3132 case Operator.OpType.Subtraction:
3133 case Operator.OpType.Multiply:
3134 case Operator.OpType.Division:
3135 case Operator.OpType.Modulus:
3136 case Operator.OpType.BitwiseAnd:
3137 case Operator.OpType.BitwiseOr:
3138 case Operator.OpType.ExclusiveOr:
3139 case Operator.OpType.LeftShift:
3140 case Operator.OpType.RightShift:
3141 case Operator.OpType.Equality:
3142 case Operator.OpType.Inequality:
3143 case Operator.OpType.GreaterThan:
3144 case Operator.OpType.LessThan:
3145 case Operator.OpType.GreaterThanOrEqual:
3146 case Operator.OpType.LessThanOrEqual:
3150 Location l = lexer.Location;
3151 Report.Error (1020, l, "Overloadable binary operator expected");
3157 void output (string s)
3159 Console.WriteLine (s);
3162 void note (string s)
3164 // Used to put annotations
3169 public Tokenizer Lexer {
3175 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
3177 current_namespace = new Namespace (null, "");
3179 this.tree = rc.Tree;
3182 current_container = tree.Types;
3183 current_container.Namespace = current_namespace;
3185 lexer = new Tokenizer (input, name);
3188 public override int parse ()
3190 StringBuilder value = new StringBuilder ();
3194 if (yacc_verbose_flag)
3195 yyparse (lexer, new yydebug.yyDebugSimple ());
3198 } catch (Exception e){
3199 // Console.WriteLine ("Fatal error: " + name);
3200 // Console.WriteLine (lexer.location);
3202 Console.WriteLine (lexer.location + " : Parsing error ");
3203 Console.WriteLine (e);
3207 return global_errors;