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 Operator.OpType op = (Operator.OpType) $3;
1099 CheckUnaryOperator (op);
1101 if (op == Operator.OpType.Addition)
1102 op = Operator.OpType.UnaryPlus;
1104 if (op == Operator.OpType.Subtraction)
1105 op = Operator.OpType.UnaryNegation;
1107 $$ = new OperatorDeclaration (op, (string) $1, (string) $5, (string) $6,
1108 null, null, lexer.Location);
1110 | type OPERATOR overloadable_operator
1112 type IDENTIFIER COMMA
1116 CheckBinaryOperator ((Operator.OpType) $3);
1118 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1119 (string) $8, (string) $9, lexer.Location);
1121 | conversion_operator_declarator
1124 overloadable_operator
1126 : BANG { $$ = Operator.OpType.LogicalNot; }
1127 | TILDE { $$ = Operator.OpType.OnesComplement; }
1128 | OP_INC { $$ = Operator.OpType.Increment; }
1129 | OP_DEC { $$ = Operator.OpType.Decrement; }
1130 | TRUE { $$ = Operator.OpType.True; }
1131 | FALSE { $$ = Operator.OpType.False; }
1132 // Unary and binary:
1133 | PLUS { $$ = Operator.OpType.Addition; }
1134 | MINUS { $$ = Operator.OpType.Subtraction; }
1136 | STAR { $$ = Operator.OpType.Multiply; }
1137 | DIV { $$ = Operator.OpType.Division; }
1138 | PERCENT { $$ = Operator.OpType.Modulus; }
1139 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1140 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1141 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1142 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1143 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1144 | OP_EQ { $$ = Operator.OpType.Equality; }
1145 | OP_NE { $$ = Operator.OpType.Inequality; }
1146 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1147 | OP_LT { $$ = Operator.OpType.LessThan; }
1148 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1149 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1152 conversion_operator_declarator
1153 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1155 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1156 null, null, lexer.Location);
1158 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1160 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1161 null, null, lexer.Location);
1165 constructor_declaration
1168 constructor_declarator
1171 Constructor c = (Constructor) $3;
1172 c.Block = (Block) $4;
1173 c.ModFlags = (int) $2;
1175 if ((c.ModFlags & Modifiers.STATIC) != 0){
1176 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1177 Location l = lexer.Location;
1178 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1181 if (c.Initializer != null){
1182 Location l = lexer.Location;
1183 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1186 if (!c.Parameters.Empty){
1187 Location l = lexer.Location;
1188 Report.Error (103, l, "Static constructors should not have parameters");
1192 CheckDef (current_container.AddConstructor (c), c.Name);
1194 current_local_parameters = null;
1198 constructor_declarator
1200 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1201 opt_constructor_initializer
1203 Location l = lexer.Location;
1205 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5, l);
1207 current_local_parameters = (Parameters) $3;
1211 opt_constructor_initializer
1212 : /* empty */ { $$ = null; }
1213 | constructor_initializer
1216 constructor_initializer
1217 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1219 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1221 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1223 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1227 destructor_declaration
1228 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1230 Method d = new Method ("System.Void", 0, "Finalize",
1231 new Parameters (null, null), (Attributes) $1, lexer.Location);
1233 d.Block = (Block) $6;
1234 CheckDef (current_container.AddMethod (d), d.Name);
1241 EVENT type variable_declarators SEMICOLON
1243 foreach (VariableDeclaration var in (ArrayList) $5) {
1245 // FIXME : Is this right ?
1246 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1247 (int) $2, null, null, (Attributes) $1);
1249 CheckDef (current_container.AddEvent (e), e.Name);
1255 EVENT type member_name
1256 OPEN_BRACE event_accessor_declarations CLOSE_BRACE
1258 DictionaryEntry pair = (DictionaryEntry) $7;
1259 Block add_block = null;
1260 Block rem_block = null;
1262 if (pair.Key != null)
1263 add_block = (Block) pair.Key;
1264 if (pair.Value != null)
1265 rem_block = (Block) pair.Value;
1267 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block, (Attributes) $1);
1269 CheckDef (current_container.AddEvent (e), e.Name);
1273 event_accessor_declarations
1274 : add_accessor_declaration remove_accessor_declaration
1276 $$ = new DictionaryEntry ($1, $2);
1278 | remove_accessor_declaration add_accessor_declaration
1280 $$ = new DictionaryEntry ($2, $1);
1284 add_accessor_declaration
1285 : opt_attributes ADD block
1291 remove_accessor_declaration
1292 : opt_attributes REMOVE block
1299 : opt_attributes opt_modifiers indexer_declarator
1302 IndexerDeclaration decl = (IndexerDeclaration) $3;
1304 lexer.properties = true;
1305 parsing_indexer = true;
1307 current_local_parameters = decl.param_list;
1309 accessor_declarations
1311 lexer.properties = false;
1312 parsing_indexer = false;
1316 // The signature is computed from the signature of the indexer. Look
1317 // at section 3.6 on the spec
1320 IndexerDeclaration decl = (IndexerDeclaration) $3;
1321 DictionaryEntry pair = (DictionaryEntry) $6;
1322 Block get_block = null;
1323 Block set_block = null;
1325 if (pair.Key != null)
1326 get_block = (Block) pair.Key;
1327 if (pair.Value != null)
1328 set_block = (Block) pair.Value;
1330 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1331 get_block, set_block, (Attributes) $1);
1333 // Note that there is no equivalent of CheckDef for this case
1334 // We shall handle this in semantic analysis
1336 current_container.AddIndexer (indexer);
1338 current_local_parameters = null;
1343 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1345 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
1347 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1349 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
1361 string name = (string) $4;
1362 Enum e = new Enum ((string) $5, (int) $2, name, (Attributes) $1, lexer.Location);
1364 foreach (VariableDeclaration ev in (ArrayList) $6){
1365 CheckDef (e.AddEnumMember (ev.identifier,
1366 (Expression) ev.expression_or_array_initializer),
1370 CheckDef (current_container.AddEnum (e), name);
1375 : /* empty */ { $$ = "System.Int32"; }
1376 | COLON integral_type { $$ = $2; }
1380 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1386 opt_enum_member_declarations
1387 : /* empty */ { $$ = new ArrayList (); }
1388 | enum_member_declarations opt_comma { $$ = $1; }
1391 enum_member_declarations
1392 : enum_member_declaration
1394 ArrayList l = new ArrayList ();
1399 | enum_member_declarations COMMA enum_member_declaration
1401 ArrayList l = (ArrayList) $1;
1409 enum_member_declaration
1410 : opt_attributes IDENTIFIER
1412 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1414 | opt_attributes IDENTIFIER ASSIGN expression
1416 $$ = new VariableDeclaration ((string) $2, $4, lexer.Location);
1420 delegate_declaration
1424 IDENTIFIER OPEN_PARENS
1425 formal_parameter_list
1429 Delegate del = new Delegate (rc, (string) $4, (int) $2,
1430 MakeName ((string) $5), (Parameters) $7,
1431 (Attributes) $1, lexer.Location);
1433 CheckDef (current_container.AddDelegate (del), del.Name);
1438 IDENTIFIER OPEN_PARENS
1439 formal_parameter_list
1443 Delegate del = new Delegate (rc, "System.Void", (int) $2, (string) $5, (Parameters) $7,
1444 (Attributes) $1, lexer.Location);
1446 CheckDef (current_container.AddDelegate (del), del.Name);
1451 : namespace_or_type_name
1454 namespace_or_type_name
1455 : qualified_identifier
1459 * Before you think of adding a return_type, notice that we have been
1460 * using two rules in the places where it matters (one rule using type
1461 * and another identical one that uses VOID as the return type). This
1462 * gets rid of a shift/reduce couple
1465 : type_name { /* class_type */
1467 This does interfaces, delegates, struct_types, class_types,
1468 parent classes, and more! 4.2
1479 ArrayList types = new ArrayList ();
1484 | type_list COMMA type
1486 ArrayList types = (ArrayList) $1;
1494 * replaces all the productions for isolating the various
1495 * simple types, but we need this to reuse it easily in local_variable_type
1498 : OBJECT { $$ = "System.Object"; }
1499 | STRING { $$ = "System.String"; }
1500 | BOOL { $$ = "System.Boolean"; }
1501 | DECIMAL { $$ = "System.Decimal"; }
1502 | FLOAT { $$ = "System.Single"; }
1503 | DOUBLE { $$ = "System.Double"; }
1508 : SBYTE { $$ = "System.SByte"; }
1509 | BYTE { $$ = "System.Byte"; }
1510 | SHORT { $$ = "System.Int16"; }
1511 | USHORT { $$ = "System.UInt16"; }
1512 | INT { $$ = "System.Int32"; }
1513 | UINT { $$ = "System.UInt32"; }
1514 | LONG { $$ = "System.Int64"; }
1515 | ULONG { $$ = "System.UInt64"; }
1516 | CHAR { $$ = "System.Char"; }
1524 : type rank_specifiers
1526 $$ = (string) $1 + (string) $2;
1531 // Expressions, section 7.5
1539 | qualified_identifier
1541 string name = (string) $1;
1544 $$ = DecomposeQI (name, lexer.Location);
1546 | parenthesized_expression
1548 | invocation_expression
1552 | post_increment_expression
1553 | post_decrement_expression
1557 | checked_expression
1558 | unchecked_expression
1565 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1566 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1567 | NULL { $$ = new NullLiteral (); }
1571 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1572 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1573 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1578 object v = lexer.Value;
1581 // FIXME: Possible optimization would be to
1582 // compute the *Literal objects directly in the scanner
1585 $$ = new IntLiteral ((Int32) v);
1587 $$ = new UIntLiteral ((UInt32) v);
1589 $$ = new LongLiteral ((Int64) v);
1590 else if (v is ulong)
1591 $$ = new ULongLiteral ((UInt64) v);
1593 Console.WriteLine ("OOPS. Unexpected result from scanner");
1598 : TRUE { $$ = new BoolLiteral (true); }
1599 | FALSE { $$ = new BoolLiteral (false); }
1602 parenthesized_expression
1603 : OPEN_PARENS expression CLOSE_PARENS
1608 : primary_expression DOT IDENTIFIER
1610 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1612 | predefined_type DOT IDENTIFIER
1614 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1622 invocation_expression
1623 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1626 // if $1 is MethodGroup
1627 // $$ = new Call ($1, $3);
1629 // $$ = new DelegateCall ($1, $3);
1631 Location l = lexer.Location;
1632 Report.Error (1, l, "THIS IS CRAZY");
1634 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1640 : /* empty */ { $$ = null; }
1647 ArrayList list = new ArrayList ();
1651 | argument_list COMMA argument
1653 ArrayList list = (ArrayList) $1;
1662 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1664 | REF variable_reference
1666 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1668 | OUT variable_reference
1670 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1675 : expression { note ("section 5.4"); $$ = $1; }
1679 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1681 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1688 ArrayList list = new ArrayList ();
1692 | expression_list COMMA expression
1694 ArrayList list = (ArrayList) $1;
1703 $$ = new This (lexer.Location);
1708 : BASE DOT IDENTIFIER
1710 $$ = new BaseAccess (BaseAccess.BaseAccessType.Member, (string) $3, null);
1712 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1714 $$ = new BaseAccess (BaseAccess.BaseAccessType.Indexer, null, (ArrayList) $3);
1718 post_increment_expression
1719 : primary_expression OP_INC
1721 $$ = new Unary (Unary.Operator.PostIncrement, (Expression) $1, lexer.Location);
1725 post_decrement_expression
1726 : primary_expression OP_DEC
1728 $$ = new Unary (Unary.Operator.PostDecrement, (Expression) $1, lexer.Location);
1733 : object_or_delegate_creation_expression
1734 | array_creation_expression
1737 object_or_delegate_creation_expression
1738 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1740 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1744 array_creation_expression
1745 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
1747 opt_array_initializer
1749 $$ = new New ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
1769 | rank_specifiers rank_specifier
1771 $$ = (string) $2 + (string) $1;
1776 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1778 $$ = "[" + (string) $2 + "]";
1798 | dim_separators COMMA
1800 $$ = (string) $1 + ",";
1804 opt_array_initializer
1816 : OPEN_BRACE CLOSE_BRACE
1818 ArrayList list = new ArrayList ();
1821 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1823 $$ = (ArrayList) $2;
1827 variable_initializer_list
1828 : variable_initializer
1830 ArrayList list = new ArrayList ();
1834 | variable_initializer_list COMMA variable_initializer
1836 ArrayList list = (ArrayList) $1;
1843 : TYPEOF OPEN_PARENS type CLOSE_PARENS
1845 $$ = new TypeOf ((string) $3);
1850 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
1851 $$ = new SizeOf ((string) $3);
1853 note ("Verify type is unmanaged");
1854 note ("if (5.8) builtin, yield constant expression");
1859 : CHECKED OPEN_PARENS expression CLOSE_PARENS
1861 $$ = new CheckedExpr ((Expression) $3);
1865 unchecked_expression
1866 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
1868 $$ = new UnCheckedExpr ((Expression) $3);
1873 : primary_expression
1874 | PLUS unary_expression
1876 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
1878 | MINUS unary_expression
1880 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
1882 | BANG unary_expression
1884 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
1886 | TILDE unary_expression
1888 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
1890 | STAR unary_expression
1892 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
1894 | BITWISE_AND unary_expression
1896 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
1898 | OP_INC unary_expression
1900 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1902 | OP_DEC unary_expression
1904 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1908 we can not do cast expressions at this level,
1909 as there is an ambiguity. Check "Cast Expressions" 7.6.8
1910 for the recipe to handle this.
1914 pre_increment_expression
1915 : OP_INC unary_expression
1917 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1921 pre_decrement_expression
1922 : OP_DEC unary_expression
1924 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1930 * FIXME: This is actually wrong, it should be `type' but that
1931 * introduces a lot of {shift,reduce}/reduces
1933 * This is really really wrong. We need to track down
1934 * the source of problems with QIs because expressions like:
1935 * foreach (string s in (string []) object) wont be parsed.
1937 : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
1939 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
1941 | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
1943 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
1947 multiplicative_expression
1949 | multiplicative_expression STAR unary_expression
1951 $$ = new Binary (Binary.Operator.Multiply,
1952 (Expression) $1, (Expression) $3, lexer.Location);
1954 | multiplicative_expression DIV unary_expression
1956 $$ = new Binary (Binary.Operator.Division,
1957 (Expression) $1, (Expression) $3, lexer.Location);
1959 | multiplicative_expression PERCENT unary_expression
1961 $$ = new Binary (Binary.Operator.Modulus,
1962 (Expression) $1, (Expression) $3, lexer.Location);
1967 : multiplicative_expression
1968 | additive_expression PLUS multiplicative_expression
1970 $$ = new Binary (Binary.Operator.Addition,
1971 (Expression) $1, (Expression) $3, lexer.Location);
1973 | additive_expression MINUS multiplicative_expression
1975 $$ = new Binary (Binary.Operator.Subtraction,
1976 (Expression) $1, (Expression) $3, lexer.Location);
1981 : additive_expression
1982 | shift_expression OP_SHIFT_LEFT additive_expression
1984 $$ = new Binary (Binary.Operator.LeftShift,
1985 (Expression) $1, (Expression) $3, lexer.Location);
1987 | shift_expression OP_SHIFT_RIGHT additive_expression
1989 $$ = new Binary (Binary.Operator.RightShift,
1990 (Expression) $1, (Expression) $3, lexer.Location);
1994 relational_expression
1996 | relational_expression OP_LT shift_expression
1998 $$ = new Binary (Binary.Operator.LessThan,
1999 (Expression) $1, (Expression) $3, lexer.Location);
2001 | relational_expression OP_GT shift_expression
2003 $$ = new Binary (Binary.Operator.GreaterThan,
2004 (Expression) $1, (Expression) $3, lexer.Location);
2006 | relational_expression OP_LE shift_expression
2008 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2009 (Expression) $1, (Expression) $3, lexer.Location);
2011 | relational_expression OP_GE shift_expression
2013 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2014 (Expression) $1, (Expression) $3, lexer.Location);
2016 | relational_expression IS type
2018 $$ = new Probe (Probe.Operator.Is,
2019 (Expression) $1, (string) $3);
2021 | relational_expression AS type
2023 $$ = new Probe (Probe.Operator.As,
2024 (Expression) $1, (string) $3);
2029 : relational_expression
2030 | equality_expression OP_EQ relational_expression
2032 $$ = new Binary (Binary.Operator.Equality,
2033 (Expression) $1, (Expression) $3, lexer.Location);
2035 | equality_expression OP_NE relational_expression
2037 $$ = new Binary (Binary.Operator.Inequality,
2038 (Expression) $1, (Expression) $3, lexer.Location);
2043 : equality_expression
2044 | and_expression BITWISE_AND equality_expression
2046 $$ = new Binary (Binary.Operator.BitwiseAnd,
2047 (Expression) $1, (Expression) $3, lexer.Location);
2051 exclusive_or_expression
2053 | exclusive_or_expression CARRET and_expression
2055 $$ = new Binary (Binary.Operator.ExclusiveOr,
2056 (Expression) $1, (Expression) $3, lexer.Location);
2060 inclusive_or_expression
2061 : exclusive_or_expression
2062 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2064 $$ = new Binary (Binary.Operator.BitwiseOr,
2065 (Expression) $1, (Expression) $3, lexer.Location);
2069 conditional_and_expression
2070 : inclusive_or_expression
2071 | conditional_and_expression OP_AND inclusive_or_expression
2073 $$ = new Binary (Binary.Operator.LogicalAnd,
2074 (Expression) $1, (Expression) $3, lexer.Location);
2078 conditional_or_expression
2079 : conditional_and_expression
2080 | conditional_or_expression OP_OR conditional_and_expression
2082 $$ = new Binary (Binary.Operator.LogicalOr,
2083 (Expression) $1, (Expression) $3, lexer.Location);
2087 conditional_expression
2088 : conditional_or_expression
2089 | conditional_or_expression INTERR expression COLON expression
2091 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2095 assignment_expression
2096 : unary_expression ASSIGN expression
2098 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2100 | unary_expression OP_MULT_ASSIGN expression
2102 Location l = lexer.Location;
2104 $$ = new Assign ((Expression) $1,
2105 new Binary (Binary.Operator.Multiply,
2107 (Expression) $3, l), l);
2109 | unary_expression OP_DIV_ASSIGN expression
2111 Location l = lexer.Location;
2113 $$ = new Assign ((Expression) $1,
2114 new Binary (Binary.Operator.Division,
2116 (Expression) $3, l), l);
2118 | unary_expression OP_MOD_ASSIGN expression
2120 Location l = lexer.Location;
2122 $$ = new Assign ((Expression) $1,
2123 new Binary (Binary.Operator.Modulus,
2125 (Expression) $3, l), l);
2127 | unary_expression OP_ADD_ASSIGN expression
2129 Location l = lexer.Location;
2131 $$ = new Assign ((Expression) $1,
2132 new Binary (Binary.Operator.Addition,
2134 (Expression) $3, l), l);
2136 | unary_expression OP_SUB_ASSIGN expression
2138 Location l = lexer.Location;
2140 $$ = new Assign ((Expression) $1,
2141 new Binary (Binary.Operator.Subtraction,
2143 (Expression) $3, l), l);
2145 | unary_expression OP_SHIFT_LEFT_ASSIGN expression
2147 Location l = lexer.Location;
2149 $$ = new Assign ((Expression) $1,
2150 new Binary (Binary.Operator.LeftShift,
2152 (Expression) $3, l), l);
2154 | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2156 Location l = lexer.Location;
2158 $$ = new Assign ((Expression) $1,
2159 new Binary (Binary.Operator.RightShift,
2161 (Expression) $3, l), l);
2163 | unary_expression OP_AND_ASSIGN expression
2165 Location l = lexer.Location;
2167 $$ = new Assign ((Expression) $1,
2168 new Binary (Binary.Operator.BitwiseAnd,
2170 (Expression) $3, l), l);
2172 | unary_expression OP_OR_ASSIGN expression
2174 Location l = lexer.Location;
2176 $$ = new Assign ((Expression) $1,
2177 new Binary (Binary.Operator.BitwiseOr,
2179 (Expression) $3, l), l);
2181 | unary_expression OP_XOR_ASSIGN expression
2183 Location l = lexer.Location;
2185 $$ = new Assign ((Expression) $1,
2186 new Binary (Binary.Operator.ExclusiveOr,
2188 (Expression) $3, l), l);
2193 : conditional_expression
2194 | assignment_expression
2202 : expression { CheckBoolean ((Expression) $1); $$ = $1; }
2214 string full_class_name = MakeName ((string) $4);
2216 new_class = new Class (rc, current_container, full_class_name, (int) $2,
2217 (Attributes) $1, lexer.Location);
2218 current_container = new_class;
2219 current_container.Namespace = current_namespace;
2220 tree.RecordClass (full_class_name, new_class);
2226 Class new_class = (Class) current_container;
2229 new_class.Bases = (ArrayList) $6;
2231 current_container = current_container.Parent;
2232 CheckDef (current_container.AddClass (new_class), new_class.Name);
2239 : /* empty */ { $$ = (int) 0; }
2245 | modifiers modifier
2250 if ((m1 & m2) != 0) {
2251 Location l = lexer.Location;
2252 Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2254 $$ = (int) (m1 | m2);
2259 : NEW { $$ = Modifiers.NEW; }
2260 | PUBLIC { $$ = Modifiers.PUBLIC; }
2261 | PROTECTED { $$ = Modifiers.PROTECTED; }
2262 | INTERNAL { $$ = Modifiers.INTERNAL; }
2263 | PRIVATE { $$ = Modifiers.PRIVATE; }
2264 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2265 | SEALED { $$ = Modifiers.SEALED; }
2266 | STATIC { $$ = Modifiers.STATIC; }
2267 | READONLY { $$ = Modifiers.READONLY; }
2268 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2269 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2270 | EXTERN { $$ = Modifiers.EXTERN; }
2274 : /* empty */ { $$ = null; }
2275 | class_base { $$ = $1; }
2279 : COLON type_list { $$ = $2; }
2287 // A block is "contained" on the following places:
2289 // property_declaration as part of the accessor body (get/set)
2290 // operator_declaration
2291 // constructor_declaration
2292 // destructor_declaration
2293 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2298 current_block = new Block (current_block);
2300 opt_statement_list CLOSE_BRACE
2302 while (current_block.Implicit)
2303 current_block = current_block.Parent;
2305 current_block = current_block.Parent;
2316 | statement_list statement
2320 : declaration_statement
2322 if ((Block) $1 != current_block){
2323 current_block.AddStatement ((Statement) $1);
2324 current_block = (Block) $1;
2327 | embedded_statement
2329 current_block.AddStatement ((Statement) $1);
2333 current_block.AddStatement ((Statement) $1);
2340 | expression_statement
2341 | selection_statement
2342 | iteration_statement
2346 | unchecked_statement
2354 $$ = new EmptyStatement ();
2359 : IDENTIFIER COLON statement
2361 string lab = (String) $1;
2364 block = new Block (current_block, lab);
2365 block.AddStatement ((Statement) $3);
2368 if (!current_block.AddLabel (lab, block)){
2369 Location l = lexer.Location;
2370 Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2376 declaration_statement
2377 : local_variable_declaration SEMICOLON // done
2378 | local_constant_declaration SEMICOLON // finishme
2382 * The following is from Rhys' grammar:
2383 * > Types in local variable declarations must be recognized as
2384 * > expressions to prevent reduce/reduce errors in the grammar.
2385 * > The expressions are converted into types during semantic analysis.
2388 : primary_expression opt_rank_specifier
2390 // FIXME: Do something smart here regarding the composition of the type.
2392 // Ok, the above "primary_expression" is there to get rid of
2393 // both reduce/reduce and shift/reduces in the grammar, it should
2394 // really just be "type_name". If you use type_name, a reduce/reduce
2395 // creeps up. If you use qualified_identifier (which is all we need
2396 // really) two shift/reduces appear.
2398 // So, instead we do a super trick: we just allow ($1) to be a
2399 // SimpleName Expression.
2401 if (((Expression) $1) is SimpleName)
2402 $$ = ((SimpleName) $1).Name;
2404 Location l = lexer.Location;
2405 Report.Error (-1, l, "Invalid Type definition");
2406 $$ = "System.Object";
2409 | builtin_types opt_rank_specifier
2411 $$ = (string) $1 + (string) $2;
2415 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2418 // // FIXME: this is a string that represents the type
2419 // // Figure out something to make this work.
2424 local_variable_declaration
2425 : local_variable_type variable_declarators
2427 $$ = declare_local_variables ((string) $1, (ArrayList) $2);
2431 local_constant_declaration
2432 : CONST type constant_declarator
2436 expression_statement
2437 : statement_expression SEMICOLON
2444 // We have to do the wrapping here and not in the case above,
2445 // because statement_expression is used for example in for_statement
2447 statement_expression
2448 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2449 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2450 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2451 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2452 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2453 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2454 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2457 object_creation_expression
2458 : object_or_delegate_creation_expression
2459 { note ("complain if this is a delegate maybe?"); }
2468 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2471 $$ = new If ((Expression) $3, (Statement) $5);
2473 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2474 embedded_statement ELSE embedded_statement
2476 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2481 : SWITCH OPEN_PARENS expression CLOSE_PARENS
2484 $$ = new Switch ((Expression) $3, (ArrayList) $5);
2498 : /* empty */ { $$ = new ArrayList (); }
2505 ArrayList sections = new ArrayList ();
2510 | switch_sections switch_section
2512 ArrayList sections = (ArrayList) $1;
2522 current_block = new Block (current_block);
2526 while (current_block.Implicit)
2527 current_block = current_block.Parent;
2528 $$ = new SwitchSection ((ArrayList) $1, current_block);
2529 current_block = current_block.Parent;
2536 ArrayList labels = new ArrayList ();
2541 | switch_labels switch_label
2543 ArrayList labels = (ArrayList) ($1);
2551 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2); }
2552 | DEFAULT COLON { $$ = new SwitchLabel (null); }
2563 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2565 $$ = new While ((Expression) $3, (Statement) $5);
2570 : DO embedded_statement
2571 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2573 $$ = new Do ((Statement) $2, (Expression) $5);
2579 opt_for_initializer SEMICOLON
2580 opt_for_condition SEMICOLON
2581 opt_for_iterator CLOSE_PARENS
2584 $$ = new For ((Statement) $3, (Expression) $5, (Statement) $7, (Statement) $9);
2589 : /* empty */ { $$ = new EmptyStatement (); }
2594 : local_variable_declaration
2596 if ((Block) $1 != current_block){
2597 current_block.AddStatement ((Statement) $1);
2598 current_block = (Block) $1;
2601 | statement_expression_list
2605 : /* empty */ { $$ = new BoolLiteral (true); }
2606 | boolean_expression
2610 : /* empty */ { $$ = new EmptyStatement (); }
2615 : statement_expression_list
2618 statement_expression_list
2619 : statement_expression
2621 Block b = new Block (null, true);
2623 b.AddStatement ((Statement) $1);
2626 | statement_expression_list COMMA statement_expression
2628 Block b = (Block) $1;
2630 b.AddStatement ((Statement) $3);
2636 : FOREACH OPEN_PARENS type IDENTIFIER IN
2638 $1 = lexer.Location;
2640 expression CLOSE_PARENS
2643 Block foreach_block = new Block (current_block, true);
2644 LocalVariableReference v;
2646 foreach_block.AddVariable ((string) $3, (string) $4, (Location) $1);
2647 v = new LocalVariableReference (foreach_block, (string) $4);
2648 foreach_block.AddStatement (new Foreach ((string) $3, v, (Expression) $7,
2649 (Statement) $9, (Location) $1));
2656 | continue_statement
2665 $$ = new Break (lexer.Location);
2670 : CONTINUE SEMICOLON
2672 $$ = new Continue (lexer.Location);
2677 : GOTO IDENTIFIER SEMICOLON
2679 $$ = new Goto ((string) $2, lexer.Location);
2681 | GOTO CASE constant_expression SEMICOLON
2685 | GOTO DEFAULT SEMICOLON
2692 : RETURN opt_expression SEMICOLON
2694 $$ = new Return ((Expression) $2, lexer.Location);
2699 : THROW opt_expression SEMICOLON
2701 $$ = new Throw ((Expression) $2);
2711 : TRY block catch_clauses
2714 ArrayList s = new ArrayList ();
2716 foreach (Catch cc in (ArrayList) $3) {
2717 if (cc.Type == null)
2723 // Now s contains the list of specific catch clauses
2724 // and g contains the general one.
2726 $$ = new Try ((Block) $2, s, g, null);
2728 | TRY block opt_catch_clauses FINALLY block
2731 ArrayList s = new ArrayList ();
2733 foreach (Catch cc in (ArrayList) $3) {
2734 if (cc.Type == null)
2740 $$ = new Try ((Block) $2, s, g, (Block) $5);
2745 : /* empty */ { $$ = null; }
2752 ArrayList l = new ArrayList ();
2757 | catch_clauses catch_clause
2759 ArrayList l = (ArrayList) $1;
2767 : /* empty */ { $$ = null; }
2772 : CATCH opt_catch_args
2774 string type = null, id = null;
2777 DictionaryEntry cc = (DictionaryEntry) $2;
2778 type = (string) cc.Key;
2779 id = (string) cc.Value;
2782 ArrayList one = new ArrayList ();
2783 one.Add (new VariableDeclaration (id, null, lexer.Location));
2786 current_block = new Block (current_block);
2787 Block b = declare_local_variables (type, one);
2794 string type = null, id = null;
2797 DictionaryEntry cc = (DictionaryEntry) $2;
2798 type = (string) cc.Key;
2799 id = (string) cc.Value;
2802 while (current_block != (Block) $1)
2803 current_block = current_block.Parent;
2808 $$ = new Catch (type, id , (Block) $4);
2813 : /* empty */ { $$ = null; }
2818 : OPEN_PARENS type opt_identifier CLOSE_PARENS
2820 $$ = new DictionaryEntry ($2, $3);
2827 $$ = new Checked ((Block) $2);
2834 $$ = new Unchecked ((Block) $2);
2839 : LOCK OPEN_PARENS expression CLOSE_PARENS embedded_statement
2841 $$ = new Lock ((Expression) $3, (Statement) $5);
2846 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS embedded_statement
2850 resource_acquisition
2851 : local_variable_declaration
2853 // FIXME: Add the current block chaining for local_variable_decl
2862 // A class used to pass around variable declarations and constants
2864 public class VariableDeclaration {
2865 public string identifier;
2866 public object expression_or_array_initializer;
2867 public Location Location;
2869 public VariableDeclaration (string id, object eoai, Location l){
2870 this.identifier = id;
2871 this.expression_or_array_initializer = eoai;
2877 // A class used to hold info about an indexer declarator
2880 public class IndexerDeclaration {
2882 public string interface_type;
2883 public Parameters param_list;
2885 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
2888 this.interface_type = interface_type;
2889 this.param_list = param_list;
2894 // A class used to hold info about an operator declarator
2897 public class OperatorDeclaration {
2898 public Operator.OpType optype;
2899 public string ret_type;
2900 public string arg1type;
2901 public string arg1name;
2902 public string arg2type;
2903 public string arg2name;
2904 public Location location;
2906 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
2907 string arg2type, string arg2name, Location location)
2910 this.ret_type = ret_type;
2911 this.arg1type = arg1type;
2912 this.arg1name = arg1name;
2913 this.arg2type = arg2type;
2914 this.arg2name = arg2name;
2915 this.location = location;
2921 // Given the @class_name name, it creates a fully qualified name
2922 // based on the containing declaration space
2925 MakeName (string class_name)
2927 string ns = current_namespace.Name;
2928 string container_name = current_container.Name;
2930 if (container_name == ""){
2932 return ns + "." + class_name;
2936 return container_name + "." + class_name;
2940 // Used to report back to the user the result of a declaration
2941 // in the current declaration space
2944 CheckDef (DeclSpace.AdditionResult result, string name)
2946 if (result == DeclSpace.AdditionResult.Success)
2949 Location l = lexer.Location;
2952 case DeclSpace.AdditionResult.NameExists:
2953 Report.Error (102, l, "The namespace `" + current_container.Name +
2954 "' already contains a definition for `"+
2959 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
2961 // case DeclSpace.AdditionResult.MethodDuplicated:
2962 // error (111, "Class `"+current_container.Name+
2963 // "' already defines a member called '" +
2964 // name + "' with the same parameter types");
2967 case DeclSpace.AdditionResult.EnclosingClash:
2968 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
2971 case DeclSpace.AdditionResult.NotAConstructor:
2972 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
2978 CheckDef (bool result, string name)
2982 CheckDef (DeclSpace.AdditionResult.NameExists, name);
2986 SimpleLookup (string name)
2989 // we need to check against current_block not being null
2990 // as `expression' is allowed in argument_lists, which
2991 // do not exist inside a block.
2993 if (current_block != null){
2994 if (current_block.IsVariableDefined (name))
2995 return new LocalVariableReference (current_block, name);
2998 if (current_local_parameters != null){
3000 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
3002 return new ParameterReference (current_local_parameters, idx, name);
3008 Expression DecomposeQI (string name, Location l)
3012 if (name.IndexOf ('.') == -1){
3013 o = SimpleLookup (name);
3015 return new SimpleName (name, l);
3018 int pos = name.LastIndexOf (".");
3019 string left = name.Substring (0, pos);
3020 string right = name.Substring (pos + 1);
3022 o = DecomposeQI (left, l);
3023 return new MemberAccess (o, right, l);
3027 Block declare_local_variables (string type, ArrayList variable_declarators)
3029 Block implicit_block;
3030 ArrayList inits = null;
3033 // We use the `Used' property to check whether statements
3034 // have been added to the current block. If so, we need
3035 // to create another block to contain the new declaration
3036 // otherwise, as an optimization, we use the same block to
3037 // add the declaration.
3039 // FIXME: A further optimization is to check if the statements
3040 // that were added were added as part of the initialization
3041 // below. In which case, no other statements have been executed
3042 // and we might be able to reduce the number of blocks for
3043 // situations like this:
3045 // int j = 1; int k = j + 1;
3047 if (current_block.Used)
3048 implicit_block = new Block (current_block, true);
3050 implicit_block = current_block;
3052 foreach (VariableDeclaration decl in variable_declarators){
3053 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3054 if (decl.expression_or_array_initializer != null){
3056 inits = new ArrayList ();
3060 Location l = lexer.Location;
3061 Report.Error (128, l, "A local variable `" + decl.identifier +
3062 "' is already defined in this scope");
3067 return implicit_block;
3069 foreach (VariableDeclaration decl in inits){
3070 if (decl.expression_or_array_initializer is Expression){
3071 Expression expr = (Expression) decl.expression_or_array_initializer;
3074 assign = new Assign (new LocalVariableReference (
3075 implicit_block, decl.identifier),
3076 expr, lexer.Location);
3077 implicit_block.AddStatement (new StatementExpression (assign));
3079 Console.WriteLine ("Not handling Array initializers yet");
3083 return implicit_block;
3086 void CheckConstant (Expression expr)
3091 void CheckBoolean (Expression expr)
3096 void CheckAttributeTarget (string a)
3100 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3104 Location l = lexer.Location;
3105 Report.Error (658, l, "Invalid attribute target");
3111 void CheckUnaryOperator (Operator.OpType op)
3115 case Operator.OpType.LogicalNot:
3116 case Operator.OpType.OnesComplement:
3117 case Operator.OpType.Increment:
3118 case Operator.OpType.Decrement:
3119 case Operator.OpType.True:
3120 case Operator.OpType.False:
3121 case Operator.OpType.Addition:
3122 case Operator.OpType.Subtraction:
3127 Location l = lexer.Location;
3128 Report.Error (1019, l, "Overloadable unary operator expected");
3134 void CheckBinaryOperator (Operator.OpType op)
3138 case Operator.OpType.Addition:
3139 case Operator.OpType.Subtraction:
3140 case Operator.OpType.Multiply:
3141 case Operator.OpType.Division:
3142 case Operator.OpType.Modulus:
3143 case Operator.OpType.BitwiseAnd:
3144 case Operator.OpType.BitwiseOr:
3145 case Operator.OpType.ExclusiveOr:
3146 case Operator.OpType.LeftShift:
3147 case Operator.OpType.RightShift:
3148 case Operator.OpType.Equality:
3149 case Operator.OpType.Inequality:
3150 case Operator.OpType.GreaterThan:
3151 case Operator.OpType.LessThan:
3152 case Operator.OpType.GreaterThanOrEqual:
3153 case Operator.OpType.LessThanOrEqual:
3157 Location l = lexer.Location;
3158 Report.Error (1020, l, "Overloadable binary operator expected");
3164 void output (string s)
3166 Console.WriteLine (s);
3169 void note (string s)
3171 // Used to put annotations
3176 public Tokenizer Lexer {
3182 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
3184 current_namespace = new Namespace (null, "");
3186 this.tree = rc.Tree;
3189 current_container = tree.Types;
3190 current_container.Namespace = current_namespace;
3192 lexer = new Tokenizer (input, name);
3195 public override int parse ()
3197 StringBuilder value = new StringBuilder ();
3201 if (yacc_verbose_flag)
3202 yyparse (lexer, new yydebug.yyDebugSimple ());
3205 } catch (Exception e){
3206 // Console.WriteLine ("Fatal error: " + name);
3207 // Console.WriteLine (lexer.location);
3209 Console.WriteLine (lexer.location + " : Parsing error ");
3210 Console.WriteLine (e);
3214 return global_errors;