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
84 %token NONE /* This token is never returned by our lexer */
85 %token ERROR // This is used not by the parser, but by the tokenizer.
89 *These are the C# keywords
169 /* C# keywords which are not really keywords */
173 /* C# single character operators/punctuation. */
174 %token OPEN_BRACE "{"
175 %token CLOSE_BRACE "}"
176 %token OPEN_BRACKET "["
177 %token CLOSE_BRACKET "]"
178 %token OPEN_PARENS "("
179 %token CLOSE_PARENS ")"
192 %token BITWISE_AND "&"
193 %token BITWISE_OR "|"
200 /* C# multi-character operators. */
203 %token OP_SHIFT_LEFT "<<"
204 %token OP_SHIFT_RIGHT ">>"
211 %token OP_MULT_ASSIGN "*="
212 %token OP_DIV_ASSIGN "/="
213 %token OP_MOD_ASSIGN "%="
214 %token OP_ADD_ASSIGN "+="
215 %token OP_SUB_ASSIGN "-="
216 %token OP_SHIFT_LEFT_ASSIGN "<<="
217 %token OP_SHIFT_RIGHT_ASSIGN ">>="
218 %token OP_AND_ASSIGN "&="
219 %token OP_XOR_ASSIGN "^="
220 %token OP_OR_ASSIGN "|="
224 %token LITERAL_INTEGER "int literal"
225 %token LITERAL_FLOAT "float literal"
226 %token LITERAL_DOUBLE "double literal"
227 %token LITERAL_DECIMAL "decimal literal"
228 %token LITERAL_CHARACTER "character literal"
229 %token LITERAL_STRING "string literal"
233 /* Add precedence rules to solve dangling else s/r conflict */
242 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
244 %left STAR DIV PERCENT
245 %right BANG CARRET UMINUS
246 %nonassoc OP_INC OP_DEC
248 %left OPEN_BRACKET OPEN_BRACE
252 %start compilation_unit
253 /*%start namespace_declaration */
257 : opt_using_directives opt_attributes opt_namespace_member_declarations EOF
259 // Check that using comes only before namespace elements
265 | using_directives using_directive
269 : using_alias_directive
270 | using_namespace_directive
273 using_alias_directive
274 : USING IDENTIFIER ASSIGN
275 namespace_or_type_name SEMICOLON
277 // FIXME : Need to implement actual action.
281 using_namespace_directive
282 : USING namespace_name SEMICOLON
284 current_namespace.Using ((string) $2);
288 // namespace_declarations
289 // : namespace_declaration
290 // | namespace_declarations namespace_declaration
292 namespace_declaration
293 : NAMESPACE qualified_identifier
295 current_namespace = new Namespace (current_namespace, (string) $2);
296 tree.RecordNamespace ((string) $2, current_namespace);
298 namespace_body opt_semicolon
300 current_namespace = current_namespace.Parent;
316 | qualified_identifier DOT IDENTIFIER {
317 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
322 : namespace_or_type_name
328 opt_namespace_member_declarations
339 opt_namespace_member_declarations
341 | namespace_member_declarations
344 namespace_member_declarations
345 : namespace_member_declaration
346 | namespace_member_declarations namespace_member_declaration
349 namespace_member_declaration
356 Class c = (Class) $1;
357 mod_flags = c.ModFlags;
359 } else if ($1 is Struct){
360 Struct s = (Struct) $1;
361 mod_flags = s.ModFlags;
367 // We remove this error until we can
368 //if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
369 // error (1527, "Namespace elements cant be explicitly " +
370 // "declared private or protected in `" + name + "'");
373 | namespace_declaration
379 | interface_declaration
381 | delegate_declaration
388 : /* empty */ { $$ = null; }
389 | attribute_section opt_attributes
394 attrs = (Attributes) $2;
395 attrs.AddAttribute ((Attribute) $1);
397 attrs = new Attributes ((Attribute) $1);
404 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
406 string target = null;
409 target = (string) $2;
411 $$ = new Attribute (target, (ArrayList) $3);
413 | OPEN_BRACKET attribute_list CLOSE_BRACKET
415 $$ = new Attribute (null, (ArrayList) $2);
419 attribute_target_specifier
420 : attribute_target COLON
429 CheckAttributeTarget ((string) $1);
432 | EVENT { $$ = "event"; }
433 | RETURN { $$ = "return"; }
439 ArrayList attrs = new ArrayList ();
445 | attribute_list COMMA attribute
447 ArrayList attrs = (ArrayList) $1;
455 : attribute_name opt_attribute_arguments
457 $$ = new DictionaryEntry ($1, $2);
462 : type_name { /* reserved attribute name or identifier: 17.4 */ }
465 opt_attribute_arguments
466 : /* empty */ { $$ = null; }
467 | OPEN_PARENS attribute_arguments CLOSE_PARENS
476 ArrayList args = new ArrayList ();
481 | attribute_arguments COMMA expression
483 ArrayList args = (ArrayList) $1;
492 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
495 opt_class_member_declarations
497 | class_member_declarations
500 class_member_declarations
501 : class_member_declaration
502 | class_member_declarations
503 class_member_declaration
506 class_member_declaration
507 : constant_declaration // done
508 | field_declaration // done
509 | method_declaration // done
510 | property_declaration // done
511 | event_declaration // done
512 | indexer_declaration // done
513 | operator_declaration // done
514 | constructor_declaration // done
515 | destructor_declaration // done
525 string full_struct_name = MakeName ((string) $4);
527 new_struct = new Struct (rc, current_container, full_struct_name, (int) $2,
528 (Attributes) $1, lexer.Location);
529 current_container = new_struct;
530 current_container.Namespace = current_namespace;
531 tree.RecordStruct (full_struct_name, new_struct);
533 opt_struct_interfaces
537 Struct new_struct = (Struct) current_container;
539 current_container = current_container.Parent;
540 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
545 opt_struct_interfaces
552 | struct_interfaces struct_interface
560 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
563 opt_struct_member_declarations
565 | struct_member_declarations
568 struct_member_declarations
569 : struct_member_declaration
570 | struct_member_declarations struct_member_declaration
573 struct_member_declaration
574 : constant_declaration
577 | property_declaration
579 | indexer_declaration
580 | operator_declaration
581 | constructor_declaration
593 foreach (DictionaryEntry constant in (ArrayList) $5){
594 Constant c = new Constant (
595 (string) $4, (string) constant.Key,
596 (Expression) constant.Value, (int) $2, (Attributes) $1);
598 CheckDef (current_container.AddConstant (c), c.Name);
604 : constant_declarator
606 ArrayList constants = new ArrayList ();
610 | constant_declarators COMMA constant_declarator
612 ArrayList constants = (ArrayList) $1;
619 : IDENTIFIER ASSIGN constant_expression
621 $$ = new DictionaryEntry ($1, $3);
632 string type = (string) $3;
635 foreach (VariableDeclaration var in (ArrayList) $4){
636 Field field = new Field (type, mod, var.identifier,
637 var.expression_or_array_initializer, (Attributes) $1);
639 CheckDef (current_container.AddField (field), field.Name);
646 : variable_declarator
648 ArrayList decl = new ArrayList ();
652 | variable_declarators COMMA variable_declarator
654 ArrayList decls = (ArrayList) $1;
661 : IDENTIFIER ASSIGN variable_initializer
663 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
667 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
686 Method method = (Method) $1;
687 Block b = (Block) $2;
690 if ((method.ModFlags & (Modifiers.EXTERN | Modifiers.ABSTRACT)) == 0){
692 501, lexer.Location, "`" +
693 current_container.Name + "." + method.Name + "'" +
694 "must declare a body because it is not marked abstract or extern");
698 method.Block = (Block) $2;
699 CheckDef (current_container.AddMethod (method), method.Name);
701 current_local_parameters = null;
710 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
712 Method method = new Method ((string) $3, (int) $2, (string) $4,
713 (Parameters) $6, (Attributes) $1, lexer.Location);
715 current_local_parameters = (Parameters) $6;
723 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
725 Method method = new Method ("System.Void", (int) $2, (string) $4,
726 (Parameters) $6, (Attributes) $1, lexer.Location);
728 current_local_parameters = (Parameters) $6;
735 | SEMICOLON { $$ = null; }
738 opt_formal_parameter_list
739 : /* empty */ { $$ = new Parameters (null, null); }
740 | formal_parameter_list
743 formal_parameter_list
746 ArrayList pars_list = (ArrayList) $1;
748 Parameter [] pars = new Parameter [pars_list.Count];
749 pars_list.CopyTo (pars);
751 $$ = new Parameters (pars, null);
753 | fixed_parameters COMMA parameter_array
755 ArrayList pars_list = (ArrayList) $1;
757 Parameter [] pars = new Parameter [pars_list.Count];
758 pars_list.CopyTo (pars);
760 $$ = new Parameters (pars, (Parameter) $3);
764 $$ = new Parameters (null, (Parameter) $1);
771 ArrayList pars = new ArrayList ();
776 | fixed_parameters COMMA fixed_parameter
778 ArrayList pars = (ArrayList) $1;
787 opt_parameter_modifier
791 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
795 opt_parameter_modifier
796 : /* empty */ { $$ = Parameter.Modifier.NONE; }
801 : REF { $$ = Parameter.Modifier.REF; }
802 | OUT { $$ = Parameter.Modifier.OUT; }
806 : opt_attributes PARAMS type IDENTIFIER
808 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
809 note ("type must be a single-dimension array type");
814 : IDENTIFIER { $$ = $1.ToString (); }
815 | interface_type DOT IDENTIFIER { $$ = $1.ToString () + "." + $3.ToString (); }
824 Parameter implicit_value_parameter;
825 implicit_value_parameter = new Parameter ((string) $3, "value", Parameter.Modifier.NONE, null);
827 lexer.properties = true;
829 implicit_value_parameters = new Parameter [1];
830 implicit_value_parameters [0] = implicit_value_parameter;
832 accessor_declarations
834 lexer.properties = false;
839 DictionaryEntry pair = (DictionaryEntry) $7;
840 Block get_block = null;
841 Block set_block = null;
843 if (pair.Key != null)
844 get_block = (Block) pair.Key;
845 if (pair.Value != null)
846 set_block = (Block) pair.Value;
848 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block, (Attributes) $1);
850 CheckDef (current_container.AddProperty (prop), prop.Name);
851 implicit_value_parameters = null;
855 accessor_declarations
856 : get_accessor_declaration opt_set_accessor_declaration
858 $$ = new DictionaryEntry ($1, $2);
860 | set_accessor_declaration opt_get_accessor_declaration
862 $$ = new DictionaryEntry ($2, $1);
866 opt_get_accessor_declaration
867 : /* empty */ { $$ = null; }
868 | get_accessor_declaration
871 opt_set_accessor_declaration
872 : /* empty */ { $$ = null; }
873 | set_accessor_declaration
876 get_accessor_declaration
879 // If this is not the case, then current_local_parameters has already
880 // been set in indexer_declaration
881 if (parsing_indexer == false)
882 current_local_parameters = new Parameters (implicit_value_parameters, null);
889 current_local_parameters = null;
893 set_accessor_declaration
896 if (parsing_indexer == false)
897 current_local_parameters = new Parameters (implicit_value_parameters, null);
902 current_local_parameters = null;
908 | SEMICOLON { $$ = new Block (null); }
911 interface_declaration
916 Interface new_interface;
917 string full_interface_name = MakeName ((string) $4);
919 new_interface = new Interface (current_container, full_interface_name, (int) $2,
920 (Attributes) $1, lexer.Location);
921 if (current_interface != null) {
922 Location l = lexer.Location;
923 Report.Error (-2, l, "Internal compiler error: interface inside interface");
925 current_interface = new_interface;
926 tree.RecordInterface (full_interface_name, new_interface);
931 Interface new_interface = (Interface) current_interface;
934 new_interface.Bases = (ArrayList) $6;
936 current_interface = null;
937 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
942 : /* empty */ { $$ = null; }
947 : COLON interface_type_list { $$ = $2; }
953 ArrayList interfaces = new ArrayList ();
958 | interface_type_list COMMA interface_type
960 ArrayList interfaces = (ArrayList) $1;
968 opt_interface_member_declarations
972 opt_interface_member_declarations
974 | interface_member_declarations
977 interface_member_declarations
978 : interface_member_declaration
979 | interface_member_declarations interface_member_declaration
982 interface_member_declaration
983 : interface_method_declaration
985 InterfaceMethod m = (InterfaceMethod) $1;
987 CheckDef (current_interface.AddMethod (m), m.Name);
989 | interface_property_declaration
991 InterfaceProperty p = (InterfaceProperty) $1;
993 CheckDef (current_interface.AddProperty (p), p.Name);
995 | interface_event_declaration
997 InterfaceEvent e = (InterfaceEvent) $1;
999 CheckDef (current_interface.AddEvent (e), e.Name);
1001 | interface_indexer_declaration
1003 InterfaceIndexer i = (InterfaceIndexer) $1;
1005 CheckDef (current_interface.AddIndexer (i), "indexer");
1010 : /* empty */ { $$ = false; }
1011 | NEW { $$ = true; }
1014 interface_method_declaration
1015 : opt_attributes opt_new type IDENTIFIER
1016 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1019 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1021 | opt_attributes opt_new VOID IDENTIFIER
1022 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1025 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1029 interface_property_declaration
1034 { lexer.properties = true; }
1036 { lexer.properties = false; }
1041 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
1042 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1);
1047 : opt_attributes GET SEMICOLON { $$ = 1; }
1048 | opt_attributes SET SEMICOLON { $$ = 2; }
1049 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1051 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1055 interface_event_declaration
1056 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1058 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1062 interface_indexer_declaration
1063 : opt_attributes opt_new type THIS
1064 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1066 { lexer.properties = true; }
1068 { lexer.properties = false; }
1071 int a_flags = (int) $10;
1073 bool do_get = (a_flags & 1) == 1;
1074 bool do_set = (a_flags & 2) == 2;
1076 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set, (bool) $2, (Attributes) $1);
1080 operator_declaration
1081 : opt_attributes opt_modifiers operator_declarator block
1083 OperatorDeclaration decl = (OperatorDeclaration) $3;
1085 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1086 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1088 // Note again, checking is done in semantic analysis
1089 current_container.AddOperator (op);
1094 : type OPERATOR overloadable_operator
1095 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1097 CheckUnaryOperator ((Operator.OpType) $3);
1099 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1100 null, null, lexer.Location);
1102 | type OPERATOR overloadable_operator
1104 type IDENTIFIER COMMA
1108 CheckBinaryOperator ((Operator.OpType) $3);
1110 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1111 (string) $8, (string) $9, lexer.Location);
1113 | conversion_operator_declarator
1116 overloadable_operator
1118 : BANG { $$ = Operator.OpType.Negate; }
1119 | TILDE { $$ = Operator.OpType.BitComplement; }
1120 | OP_INC { $$ = Operator.OpType.Increment; }
1121 | OP_DEC { $$ = Operator.OpType.Decrement; }
1122 | TRUE { $$ = Operator.OpType.True; }
1123 | FALSE { $$ = Operator.OpType.False; }
1124 // Unary and binary:
1125 | PLUS { $$ = Operator.OpType.Addition; }
1126 | MINUS { $$ = Operator.OpType.Subtraction; }
1128 | STAR { $$ = Operator.OpType.Multiply; }
1129 | DIV { $$ = Operator.OpType.Division; }
1130 | PERCENT { $$ = Operator.OpType.Modulus; }
1131 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1132 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1133 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1134 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1135 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1136 | OP_EQ { $$ = Operator.OpType.Equality; }
1137 | OP_NE { $$ = Operator.OpType.Inequality; }
1138 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1139 | OP_LT { $$ = Operator.OpType.LessThan; }
1140 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1141 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1144 conversion_operator_declarator
1145 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1147 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1148 null, null, lexer.Location);
1150 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1152 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1153 null, null, lexer.Location);
1157 constructor_declaration
1160 constructor_declarator
1163 Constructor c = (Constructor) $3;
1164 c.Block = (Block) $4;
1165 c.ModFlags = (int) $2;
1167 if ((c.ModFlags & Modifiers.STATIC) != 0){
1168 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1169 Location l = lexer.Location;
1170 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1173 if (c.Initializer != null){
1174 Location l = lexer.Location;
1175 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1178 if (!c.Parameters.Empty){
1179 Location l = lexer.Location;
1180 Report.Error (103, l, "Static constructors should not have parameters");
1184 CheckDef (current_container.AddConstructor (c), c.Name);
1186 current_local_parameters = null;
1190 constructor_declarator
1192 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1193 opt_constructor_initializer
1195 Location l = lexer.Location;
1197 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5, l);
1199 current_local_parameters = (Parameters) $3;
1203 opt_constructor_initializer
1204 : /* empty */ { $$ = null; }
1205 | constructor_initializer
1208 constructor_initializer
1209 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1211 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1213 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1215 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1219 destructor_declaration
1220 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1222 Method d = new Method ("System.Void", 0, "Finalize",
1223 new Parameters (null, null), (Attributes) $1, lexer.Location);
1225 d.Block = (Block) $6;
1226 CheckDef (current_container.AddMethod (d), d.Name);
1233 EVENT type variable_declarators SEMICOLON
1235 foreach (VariableDeclaration var in (ArrayList) $5) {
1237 // FIXME : Is this right ?
1238 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1239 (int) $2, null, null, (Attributes) $1);
1241 CheckDef (current_container.AddEvent (e), e.Name);
1247 EVENT type member_name
1248 OPEN_BRACE event_accessor_declarations CLOSE_BRACE
1250 DictionaryEntry pair = (DictionaryEntry) $7;
1251 Block add_block = null;
1252 Block rem_block = null;
1254 if (pair.Key != null)
1255 add_block = (Block) pair.Key;
1256 if (pair.Value != null)
1257 rem_block = (Block) pair.Value;
1259 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block, (Attributes) $1);
1261 CheckDef (current_container.AddEvent (e), e.Name);
1265 event_accessor_declarations
1266 : add_accessor_declaration remove_accessor_declaration
1268 $$ = new DictionaryEntry ($1, $2);
1270 | remove_accessor_declaration add_accessor_declaration
1272 $$ = new DictionaryEntry ($2, $1);
1276 add_accessor_declaration
1277 : opt_attributes ADD block
1283 remove_accessor_declaration
1284 : opt_attributes REMOVE block
1291 : opt_attributes opt_modifiers indexer_declarator
1294 IndexerDeclaration decl = (IndexerDeclaration) $3;
1296 lexer.properties = true;
1297 parsing_indexer = true;
1299 current_local_parameters = decl.param_list;
1301 accessor_declarations
1303 lexer.properties = false;
1304 parsing_indexer = false;
1308 // The signature is computed from the signature of the indexer. Look
1309 // at section 3.6 on the spec
1312 IndexerDeclaration decl = (IndexerDeclaration) $3;
1313 DictionaryEntry pair = (DictionaryEntry) $6;
1314 Block get_block = null;
1315 Block set_block = null;
1317 if (pair.Key != null)
1318 get_block = (Block) pair.Key;
1319 if (pair.Value != null)
1320 set_block = (Block) pair.Value;
1322 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1323 get_block, set_block, (Attributes) $1);
1325 // Note that there is no equivalent of CheckDef for this case
1326 // We shall handle this in semantic analysis
1328 current_container.AddIndexer (indexer);
1330 current_local_parameters = null;
1335 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1337 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
1339 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1341 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
1353 string name = (string) $4;
1354 Enum e = new Enum ((string) $5, (int) $2, name, (Attributes) $1, lexer.Location);
1356 foreach (VariableDeclaration ev in (ArrayList) $6){
1357 CheckDef (e.AddEnumMember (ev.identifier,
1358 (Expression) ev.expression_or_array_initializer),
1362 CheckDef (current_container.AddEnum (e), name);
1367 : /* empty */ { $$ = "System.Int32"; }
1368 | COLON integral_type { $$ = $2; }
1372 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1378 opt_enum_member_declarations
1379 : /* empty */ { $$ = new ArrayList (); }
1380 | enum_member_declarations opt_comma { $$ = $1; }
1383 enum_member_declarations
1384 : enum_member_declaration
1386 ArrayList l = new ArrayList ();
1391 | enum_member_declarations COMMA enum_member_declaration
1393 ArrayList l = (ArrayList) $1;
1401 enum_member_declaration
1402 : opt_attributes IDENTIFIER
1404 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1406 | opt_attributes IDENTIFIER ASSIGN expression
1408 $$ = new VariableDeclaration ((string) $2, $4, lexer.Location);
1412 delegate_declaration
1416 IDENTIFIER OPEN_PARENS
1417 formal_parameter_list
1421 Delegate del = new Delegate ((string) $4, (int) $2, (string) $5, (Parameters) $7,
1422 (Attributes) $1, lexer.Location);
1424 CheckDef (current_container.AddDelegate (del), del.Name);
1429 IDENTIFIER OPEN_PARENS
1430 formal_parameter_list
1434 Delegate del = new Delegate (null, (int) $2, (string) $5, (Parameters) $7,
1435 (Attributes) $1, lexer.Location);
1437 CheckDef (current_container.AddDelegate (del), del.Name);
1442 : namespace_or_type_name
1445 namespace_or_type_name
1446 : qualified_identifier
1450 * Before you think of adding a return_type, notice that we have been
1451 * using two rules in the places where it matters (one rule using type
1452 * and another identical one that uses VOID as the return type). This
1453 * gets rid of a shift/reduce couple
1456 : type_name { /* class_type */
1458 This does interfaces, delegates, struct_types, class_types,
1459 parent classes, and more! 4.2
1470 ArrayList types = new ArrayList ();
1475 | type_list COMMA type
1477 ArrayList types = (ArrayList) $1;
1485 * replaces all the productions for isolating the various
1486 * simple types, but we need this to reuse it easily in local_variable_type
1489 : OBJECT { $$ = "System.Object"; }
1490 | STRING { $$ = "System.String"; }
1491 | BOOL { $$ = "System.Boolean"; }
1492 | DECIMAL { $$ = "System.Decimal"; }
1493 | FLOAT { $$ = "System.Single"; }
1494 | DOUBLE { $$ = "System.Double"; }
1499 : SBYTE { $$ = "System.SByte"; }
1500 | BYTE { $$ = "System.Byte"; }
1501 | SHORT { $$ = "System.Int16"; }
1502 | USHORT { $$ = "System.UInt16"; }
1503 | INT { $$ = "System.Int32"; }
1504 | UINT { $$ = "System.UInt32"; }
1505 | LONG { $$ = "System.Int64"; }
1506 | ULONG { $$ = "System.UInt64"; }
1507 | CHAR { $$ = "System.Char"; }
1515 : type rank_specifiers
1517 $$ = (string) $1 + (string) $2;
1522 // Expressions, section 7.5
1530 | qualified_identifier
1532 string name = (string) $1;
1535 $$ = QualifiedIdentifier (name, lexer.Location);
1537 | parenthesized_expression
1539 | invocation_expression
1543 | post_increment_expression
1544 | post_decrement_expression
1548 | checked_expression
1549 | unchecked_expression
1556 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1557 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1558 | NULL { $$ = new NullLiteral (); }
1562 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1563 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1564 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1569 object v = lexer.Value;
1572 // FIXME: Possible optimization would be to
1573 // compute the *Literal objects directly in the scanner
1576 $$ = new IntLiteral ((Int32) v);
1578 $$ = new UIntLiteral ((UInt32) v);
1580 $$ = new LongLiteral ((Int64) v);
1581 else if (v is ulong)
1582 $$ = new ULongLiteral ((UInt64) v);
1584 Console.WriteLine ("OOPS. Unexpected result from scanner");
1589 : TRUE { $$ = new BoolLiteral (true); }
1590 | FALSE { $$ = new BoolLiteral (false); }
1593 parenthesized_expression
1594 : OPEN_PARENS expression CLOSE_PARENS
1599 : primary_expression DOT IDENTIFIER
1601 $$ = new MemberAccess ((Expression) $1, (string) $3);
1603 | predefined_type DOT IDENTIFIER
1605 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1613 invocation_expression
1614 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1617 // if $1 is MethodGroup
1618 // $$ = new Call ($1, $3);
1620 // $$ = new DelegateCall ($1, $3);
1622 Location l = lexer.Location;
1623 Report.Error (1, l, "THIS IS CRAZY");
1625 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1631 : /* empty */ { $$ = null; }
1638 ArrayList list = new ArrayList ();
1642 | argument_list COMMA argument
1644 ArrayList list = (ArrayList) $1;
1653 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1655 | REF variable_reference
1657 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1659 | OUT variable_reference
1661 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1666 : expression { note ("section 5.4"); $$ = $1; }
1670 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1672 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
1679 ArrayList list = new ArrayList ();
1683 | expression_list COMMA expression
1685 ArrayList list = (ArrayList) $1;
1699 : BASE DOT IDENTIFIER
1701 $$ = new BaseAccess (BaseAccess.BaseAccessType.Member, (string) $3, null);
1703 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1705 $$ = new BaseAccess (BaseAccess.BaseAccessType.Indexer, null, (ArrayList) $3);
1709 post_increment_expression
1710 : primary_expression OP_INC
1712 $$ = new Unary (Unary.Operator.PostIncrement, (Expression) $1, lexer.Location);
1716 post_decrement_expression
1717 : primary_expression OP_DEC
1719 $$ = new Unary (Unary.Operator.PostDecrement, (Expression) $1, lexer.Location);
1724 : object_or_delegate_creation_expression
1725 | array_creation_expression
1728 object_or_delegate_creation_expression
1729 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1731 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1735 array_creation_expression
1736 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
1738 opt_array_initializer
1740 $$ = new New ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
1760 | rank_specifier rank_specifiers
1762 $$ = (string) $1 + (string) $2;
1767 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1769 $$ = "[" + (string) $2 + "]";
1789 | dim_separators COMMA
1791 $$ =(string) $1 + ",";
1795 opt_array_initializer
1807 : OPEN_BRACE CLOSE_BRACE
1809 ArrayList list = new ArrayList ();
1812 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1814 $$ = (ArrayList) $2;
1818 variable_initializer_list
1819 : variable_initializer
1821 ArrayList list = new ArrayList ();
1825 | variable_initializer_list COMMA variable_initializer
1827 ArrayList list = (ArrayList) $1;
1834 : TYPEOF OPEN_PARENS type CLOSE_PARENS
1836 $$ = new TypeOf ((string) $3);
1841 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
1842 $$ = new SizeOf ((string) $3);
1844 note ("Verify type is unmanaged");
1845 note ("if (5.8) builtin, yield constant expression");
1850 : CHECKED OPEN_PARENS expression CLOSE_PARENS
1852 $$ = new CheckedExpr ((Expression) $3);
1856 unchecked_expression
1857 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
1859 $$ = new UnCheckedExpr ((Expression) $3);
1864 : primary_expression
1865 | PLUS unary_expression {
1866 $$ = new Unary (Unary.Operator.Addition, (Expression) $2, lexer.Location);
1868 | MINUS unary_expression
1870 $$ = new Unary (Unary.Operator.Subtraction, (Expression) $2, lexer.Location);
1872 | BANG unary_expression
1874 $$ = new Unary (Unary.Operator.Negate, (Expression) $2, lexer.Location);
1876 | TILDE unary_expression
1878 $$ = new Unary (Unary.Operator.BitComplement, (Expression) $2, lexer.Location);
1880 | STAR unary_expression
1882 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
1884 | BITWISE_AND unary_expression
1886 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
1888 | OP_INC unary_expression
1890 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1892 | OP_DEC unary_expression
1894 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1898 we can not do cast expressions at this level,
1899 as there is an ambiguity. Check "Cast Expressions" 7.6.8
1900 for the recipe to handle this.
1904 pre_increment_expression
1905 : OP_INC unary_expression
1907 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1911 pre_decrement_expression
1912 : OP_DEC unary_expression
1914 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1920 * FIXME: This is actually wrong, it should be `type' but that
1921 * introduces a lot of {shift,reduce}/reduces
1923 * This is really really wrong. We need to track down
1924 * the source of problems with QIs because expressions like:
1925 * foreach (string s in (string []) object) wont be parsed.
1927 : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
1929 $$ = new Cast ((string) $2, (Expression) $4);
1931 | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
1933 $$ = new Cast ((string) $2, (Expression) $4);
1937 multiplicative_expression
1939 | multiplicative_expression STAR unary_expression
1941 $$ = new Binary (Binary.Operator.Multiply,
1942 (Expression) $1, (Expression) $3, lexer.Location);
1944 | multiplicative_expression DIV unary_expression
1946 $$ = new Binary (Binary.Operator.Division,
1947 (Expression) $1, (Expression) $3, lexer.Location);
1949 | multiplicative_expression PERCENT unary_expression
1951 $$ = new Binary (Binary.Operator.Modulus,
1952 (Expression) $1, (Expression) $3, lexer.Location);
1957 : multiplicative_expression
1958 | additive_expression PLUS multiplicative_expression
1960 $$ = new Binary (Binary.Operator.Addition,
1961 (Expression) $1, (Expression) $3, lexer.Location);
1963 | additive_expression MINUS multiplicative_expression
1965 $$ = new Binary (Binary.Operator.Subtraction,
1966 (Expression) $1, (Expression) $3, lexer.Location);
1971 : additive_expression
1972 | shift_expression OP_SHIFT_LEFT additive_expression
1974 $$ = new Binary (Binary.Operator.LeftShift,
1975 (Expression) $1, (Expression) $3, lexer.Location);
1977 | shift_expression OP_SHIFT_RIGHT additive_expression
1979 $$ = new Binary (Binary.Operator.RightShift,
1980 (Expression) $1, (Expression) $3, lexer.Location);
1984 relational_expression
1986 | relational_expression OP_LT shift_expression
1988 $$ = new Binary (Binary.Operator.LessThan,
1989 (Expression) $1, (Expression) $3, lexer.Location);
1991 | relational_expression OP_GT shift_expression
1993 $$ = new Binary (Binary.Operator.GreaterThan,
1994 (Expression) $1, (Expression) $3, lexer.Location);
1996 | relational_expression OP_LE shift_expression
1998 $$ = new Binary (Binary.Operator.LessThanOrEqual,
1999 (Expression) $1, (Expression) $3, lexer.Location);
2001 | relational_expression OP_GE shift_expression
2003 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2004 (Expression) $1, (Expression) $3, lexer.Location);
2006 | relational_expression IS type
2008 $$ = new Probe (Probe.Operator.Is,
2009 (Expression) $1, (string) $3);
2011 | relational_expression AS type
2013 $$ = new Probe (Probe.Operator.As,
2014 (Expression) $1, (string) $3);
2019 : relational_expression
2020 | equality_expression OP_EQ relational_expression
2022 $$ = new Binary (Binary.Operator.Equality,
2023 (Expression) $1, (Expression) $3, lexer.Location);
2025 | equality_expression OP_NE relational_expression
2027 $$ = new Binary (Binary.Operator.Inequality,
2028 (Expression) $1, (Expression) $3, lexer.Location);
2033 : equality_expression
2034 | and_expression BITWISE_AND equality_expression
2036 $$ = new Binary (Binary.Operator.BitwiseAnd,
2037 (Expression) $1, (Expression) $3, lexer.Location);
2041 exclusive_or_expression
2043 | exclusive_or_expression CARRET and_expression
2045 $$ = new Binary (Binary.Operator.ExclusiveOr,
2046 (Expression) $1, (Expression) $3, lexer.Location);
2050 inclusive_or_expression
2051 : exclusive_or_expression
2052 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2054 $$ = new Binary (Binary.Operator.BitwiseOr,
2055 (Expression) $1, (Expression) $3, lexer.Location);
2059 conditional_and_expression
2060 : inclusive_or_expression
2061 | conditional_and_expression OP_AND inclusive_or_expression
2063 $$ = new Binary (Binary.Operator.LogicalAnd,
2064 (Expression) $1, (Expression) $3, lexer.Location);
2068 conditional_or_expression
2069 : conditional_and_expression
2070 | conditional_or_expression OP_OR conditional_and_expression
2072 $$ = new Binary (Binary.Operator.LogicalOr,
2073 (Expression) $1, (Expression) $3, lexer.Location);
2077 conditional_expression
2078 : conditional_or_expression
2079 | conditional_or_expression INTERR expression COLON expression
2081 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2085 assignment_expression
2086 : unary_expression ASSIGN expression
2088 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2090 | unary_expression OP_MULT_ASSIGN expression
2092 Location l = lexer.Location;
2094 $$ = new Assign ((Expression) $1,
2095 new Binary (Binary.Operator.Multiply,
2097 (Expression) $3, l), l);
2099 | unary_expression OP_DIV_ASSIGN expression
2101 Location l = lexer.Location;
2103 $$ = new Assign ((Expression) $1,
2104 new Binary (Binary.Operator.Division,
2106 (Expression) $3, l), l);
2108 | unary_expression OP_MOD_ASSIGN expression
2110 Location l = lexer.Location;
2112 $$ = new Assign ((Expression) $1,
2113 new Binary (Binary.Operator.Modulus,
2115 (Expression) $3, l), l);
2117 | unary_expression OP_ADD_ASSIGN expression
2119 Location l = lexer.Location;
2121 $$ = new Assign ((Expression) $1,
2122 new Binary (Binary.Operator.Addition,
2124 (Expression) $3, l), l);
2126 | unary_expression OP_SUB_ASSIGN expression
2128 Location l = lexer.Location;
2130 $$ = new Assign ((Expression) $1,
2131 new Binary (Binary.Operator.Subtraction,
2133 (Expression) $3, l), l);
2135 | unary_expression OP_SHIFT_LEFT_ASSIGN expression
2137 Location l = lexer.Location;
2139 $$ = new Assign ((Expression) $1,
2140 new Binary (Binary.Operator.LeftShift,
2142 (Expression) $3, l), l);
2144 | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2146 Location l = lexer.Location;
2148 $$ = new Assign ((Expression) $1,
2149 new Binary (Binary.Operator.RightShift,
2151 (Expression) $3, l), l);
2153 | unary_expression OP_AND_ASSIGN expression
2155 Location l = lexer.Location;
2157 $$ = new Assign ((Expression) $1,
2158 new Binary (Binary.Operator.BitwiseAnd,
2160 (Expression) $3, l), l);
2162 | unary_expression OP_OR_ASSIGN expression
2164 Location l = lexer.Location;
2166 $$ = new Assign ((Expression) $1,
2167 new Binary (Binary.Operator.BitwiseOr,
2169 (Expression) $3, l), l);
2171 | unary_expression OP_XOR_ASSIGN expression
2173 Location l = lexer.Location;
2175 $$ = new Assign ((Expression) $1,
2176 new Binary (Binary.Operator.ExclusiveOr,
2178 (Expression) $3, l), l);
2183 : conditional_expression
2184 | assignment_expression
2192 : expression { CheckBoolean ((Expression) $1); $$ = $1; }
2204 string full_class_name = MakeName ((string) $4);
2206 new_class = new Class (rc, current_container, full_class_name, (int) $2,
2207 (Attributes) $1, lexer.Location);
2208 current_container = new_class;
2209 current_container.Namespace = current_namespace;
2210 tree.RecordClass (full_class_name, new_class);
2216 Class new_class = (Class) current_container;
2219 new_class.Bases = (ArrayList) $6;
2221 current_container = current_container.Parent;
2222 CheckDef (current_container.AddClass (new_class), new_class.Name);
2229 : /* empty */ { $$ = (int) 0; }
2235 | modifiers modifier
2240 if ((m1 & m2) != 0) {
2241 Location l = lexer.Location;
2242 Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2244 $$ = (int) (m1 | m2);
2249 : NEW { $$ = Modifiers.NEW; }
2250 | PUBLIC { $$ = Modifiers.PUBLIC; }
2251 | PROTECTED { $$ = Modifiers.PROTECTED; }
2252 | INTERNAL { $$ = Modifiers.INTERNAL; }
2253 | PRIVATE { $$ = Modifiers.PRIVATE; }
2254 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2255 | SEALED { $$ = Modifiers.SEALED; }
2256 | STATIC { $$ = Modifiers.STATIC; }
2257 | READONLY { $$ = Modifiers.READONLY; }
2258 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2259 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2260 | EXTERN { $$ = Modifiers.EXTERN; }
2264 : /* empty */ { $$ = null; }
2265 | class_base { $$ = $1; }
2269 : COLON type_list { $$ = $2; }
2277 // A block is "contained" on the following places:
2279 // property_declaration as part of the accessor body (get/set)
2280 // operator_declaration
2281 // constructor_declaration
2282 // destructor_declaration
2283 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2288 current_block = new Block (current_block);
2290 opt_statement_list CLOSE_BRACE
2292 while (current_block.Implicit)
2293 current_block = current_block.Parent;
2295 current_block = current_block.Parent;
2306 | statement_list statement
2310 : declaration_statement
2312 if ((Block) $1 != current_block){
2313 current_block.AddStatement ((Statement) $1);
2314 current_block = (Block) $1;
2317 | embedded_statement
2319 current_block.AddStatement ((Statement) $1);
2323 current_block.AddStatement ((Statement) $1);
2330 | expression_statement
2331 | selection_statement
2332 | iteration_statement
2336 | unchecked_statement
2344 $$ = new EmptyStatement ();
2349 : IDENTIFIER COLON statement
2351 string lab = (String) $1;
2354 block = new Block (current_block, lab);
2355 block.AddStatement ((Statement) $3);
2358 if (!current_block.AddLabel (lab, block)){
2359 Location l = lexer.Location;
2360 Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2366 declaration_statement
2367 : local_variable_declaration SEMICOLON // done
2368 | local_constant_declaration SEMICOLON // finishme
2372 * The following is from Rhys' grammar:
2373 * > Types in local variable declarations must be recognized as
2374 * > expressions to prevent reduce/reduce errors in the grammar.
2375 * > The expressions are converted into types during semantic analysis.
2378 : primary_expression type_suffixes
2380 // FIXME: Do something smart here regarding the composition of the type.
2382 // Ok, the above "primary_expression" is there to get rid of
2383 // both reduce/reduce and shift/reduces in the grammar, it should
2384 // really just be "type_name". If you use type_name, a reduce/reduce
2385 // creeps up. If you use qualified_identifier (which is all we need
2386 // really) two shift/reduces appear.
2388 // So, instead we do a super trick: we just allow ($1) to be a
2389 // SimpleName Expression.
2391 if (((Expression) $1) is SimpleName)
2392 $$ = ((SimpleName) $1).Name;
2394 Location l = lexer.Location;
2395 Report.Error (-1, l, "Invalid Type definition");
2396 $$ = "System.Object";
2399 | builtin_types type_suffixes
2401 $$ = (string) $1 + (string) $2;
2405 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2408 // // FIXME: this is a string that represents the type
2409 // // Figure out something to make this work.
2424 | type_suffix_list type_suffix
2426 $$ = (string) $1 + (string) $2;
2431 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2433 $$ = "[" + (string) $2 + "]";
2437 local_variable_declaration
2438 : local_variable_type variable_declarators
2440 $$ = declare_local_variables ((string) $1, (ArrayList) $2);
2444 local_constant_declaration
2445 : CONST type constant_declarator
2449 expression_statement
2450 : statement_expression SEMICOLON
2457 // We have to do the wrapping here and not in the case above,
2458 // because statement_expression is used for example in for_statement
2460 statement_expression
2461 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2462 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2463 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2464 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2465 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2466 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2467 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2470 object_creation_expression
2471 : object_or_delegate_creation_expression
2472 { note ("complain if this is a delegate maybe?"); }
2481 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2484 $$ = new If ((Expression) $3, (Statement) $5);
2486 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2487 embedded_statement ELSE embedded_statement
2489 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2494 : SWITCH OPEN_PARENS expression CLOSE_PARENS
2497 $$ = new Switch ((Expression) $3, (ArrayList) $5);
2511 : /* empty */ { $$ = new ArrayList (); }
2518 ArrayList sections = new ArrayList ();
2523 | switch_sections switch_section
2525 ArrayList sections = (ArrayList) $1;
2535 current_block = new Block (current_block);
2539 while (current_block.Implicit)
2540 current_block = current_block.Parent;
2541 $$ = new SwitchSection ((ArrayList) $1, current_block);
2542 current_block = current_block.Parent;
2549 ArrayList labels = new ArrayList ();
2554 | switch_labels switch_label
2556 ArrayList labels = (ArrayList) ($1);
2564 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2); }
2565 | DEFAULT COLON { $$ = new SwitchLabel (null); }
2576 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2578 $$ = new While ((Expression) $3, (Statement) $5);
2583 : DO embedded_statement
2584 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2586 $$ = new Do ((Statement) $2, (Expression) $5);
2592 opt_for_initializer SEMICOLON
2593 opt_for_condition SEMICOLON
2594 opt_for_iterator CLOSE_PARENS
2597 $$ = new For ((Statement) $3, (Expression) $5, (Statement) $7, (Statement) $9);
2602 : /* empty */ { $$ = new EmptyStatement (); }
2607 : local_variable_declaration
2608 | statement_expression_list
2612 : /* empty */ { $$ = new BoolLiteral (true); }
2613 | boolean_expression
2617 : /* empty */ { $$ = new EmptyStatement (); }
2622 : statement_expression_list
2625 statement_expression_list
2626 : statement_expression
2628 Block b = new Block (null, true);
2630 b.AddStatement ((Statement) $1);
2633 | statement_expression_list COMMA statement_expression
2635 Block b = (Block) $1;
2637 b.AddStatement ((Statement) $3);
2643 : FOREACH OPEN_PARENS type IDENTIFIER IN expression CLOSE_PARENS
2646 string temp_id = current_block.MakeInternalID ();
2647 ExpressionStatement assign_e;
2649 Statement getcurrent;
2650 Block foreach_block, child_block;
2651 Location l = lexer.Location;
2653 foreach_block = new Block (current_block, true);
2655 foreach_block.AddVariable ("System.IEnumerator", temp_id, l);
2656 foreach_block.AddVariable ((string) $3, (string) $4, l);
2657 assign_e = new Assign (new LocalVariableReference (foreach_block, temp_id),
2659 new MemberAccess ((Expression) $6, "GetEnumerator"),
2660 null, lexer.Location), lexer.Location);
2661 current_block.AddStatement (new StatementExpression (assign_e));
2662 ma = new MemberAccess (new LocalVariableReference (foreach_block, temp_id), "MoveNext");
2663 child_block = new Block (current_block);
2665 getcurrent = new StatementExpression (
2667 new LocalVariableReference (foreach_block, (string) $4),
2671 new LocalVariableReference (foreach_block, temp_id), "Current")),
2674 child_block.AddStatement (getcurrent);
2675 child_block.AddStatement ((Statement) $8);
2676 foreach_block.AddStatement (new While (ma, (Statement) child_block));
2684 | continue_statement
2698 : CONTINUE SEMICOLON
2700 $$ = new Continue ();
2705 : GOTO IDENTIFIER SEMICOLON
2707 $$ = new Goto ((string) $2);
2709 | GOTO CASE constant_expression SEMICOLON
2710 | GOTO DEFAULT SEMICOLON
2714 : RETURN opt_expression SEMICOLON
2716 $$ = new Return ((Expression) $2);
2721 : THROW opt_expression SEMICOLON
2723 $$ = new Throw ((Expression) $2);
2733 : TRY block catch_clauses
2736 ArrayList s = new ArrayList ();
2738 foreach (Catch cc in (ArrayList) $3) {
2739 if (cc.Type == null)
2745 // Now s contains the list of specific catch clauses
2746 // and g contains the general one.
2748 $$ = new Try ((Block) $2, s, g, null);
2750 | TRY block opt_catch_clauses FINALLY block
2753 ArrayList s = new ArrayList ();
2755 foreach (Catch cc in (ArrayList) $3) {
2756 if (cc.Type == null)
2762 $$ = new Try ((Block) $2, s, g, (Block) $5);
2767 : /* empty */ { $$ = null; }
2774 ArrayList l = new ArrayList ();
2779 | catch_clauses catch_clause
2781 ArrayList l = (ArrayList) $1;
2789 : /* empty */ { $$ = null; }
2794 : CATCH opt_catch_args block
2800 DictionaryEntry cc = (DictionaryEntry) $2;
2801 type = (string) cc.Key;
2802 id = (string) cc.Value;
2805 $$ = new Catch (type, id, (Block) $3);
2810 : /* empty */ { $$ = null; }
2815 : OPEN_PARENS type opt_identifier CLOSE_PARENS
2817 $$ = new DictionaryEntry ($2, $3);
2824 $$ = new Checked ((Block) $2);
2831 $$ = new Unchecked ((Block) $2);
2836 : LOCK OPEN_PARENS expression CLOSE_PARENS embedded_statement
2838 $$ = new Lock ((Expression) $3, (Statement) $5);
2843 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS embedded_statement
2847 resource_acquisition
2848 : local_variable_declaration
2856 // A class used to pass around variable declarations and constants
2858 public class VariableDeclaration {
2859 public string identifier;
2860 public object expression_or_array_initializer;
2861 public Location Location;
2863 public VariableDeclaration (string id, object eoai, Location l){
2864 this.identifier = id;
2865 this.expression_or_array_initializer = eoai;
2871 // A class used to hold info about an indexer declarator
2874 public class IndexerDeclaration {
2876 public string interface_type;
2877 public Parameters param_list;
2879 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
2882 this.interface_type = interface_type;
2883 this.param_list = param_list;
2888 // A class used to hold info about an operator declarator
2891 public class OperatorDeclaration {
2892 public Operator.OpType optype;
2893 public string ret_type;
2894 public string arg1type;
2895 public string arg1name;
2896 public string arg2type;
2897 public string arg2name;
2898 public Location location;
2900 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
2901 string arg2type, string arg2name, Location location)
2904 this.ret_type = ret_type;
2905 this.arg1type = arg1type;
2906 this.arg1name = arg1name;
2907 this.arg2type = arg2type;
2908 this.arg2name = arg2name;
2909 this.location = location;
2915 // Given the @class_name name, it creates a fully qualified name
2916 // based on the containing declaration space
2919 MakeName (string class_name)
2921 string ns = current_namespace.Name;
2922 string container_name = current_container.Name;
2924 if (container_name == ""){
2926 return ns + "." + class_name;
2930 return container_name + "." + class_name;
2934 // Used to report back to the user the result of a declaration
2935 // in the current declaration space
2938 CheckDef (DeclSpace.AdditionResult result, string name)
2940 if (result == DeclSpace.AdditionResult.Success)
2943 Location l = lexer.Location;
2946 case DeclSpace.AdditionResult.NameExists:
2947 Report.Error (102, l, "The namespace `" + current_container.Name +
2948 "' already contains a definition for `"+
2953 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
2955 // case DeclSpace.AdditionResult.MethodDuplicated:
2956 // error (111, "Class `"+current_container.Name+
2957 // "' already defines a member called '" +
2958 // name + "' with the same parameter types");
2961 case DeclSpace.AdditionResult.EnclosingClash:
2962 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
2965 case DeclSpace.AdditionResult.NotAConstructor:
2966 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
2972 CheckDef (bool result, string name)
2976 CheckDef (DeclSpace.AdditionResult.NameExists, name);
2980 SimpleLookup (string name)
2983 // we need to check against current_block not being null
2984 // as `expression' is allowed in argument_lists, which
2985 // do not exist inside a block.
2987 if (current_block != null){
2988 if (current_block.IsVariableDefined (name))
2989 return new LocalVariableReference (current_block, name);
2992 if (current_local_parameters != null){
2994 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
2996 return new ParameterReference (current_local_parameters, idx, name);
3003 // Assumes that the name contains a `.' and tries to perform a simple lookup
3004 // and shape the result to be a MemberAccess on a Local/Parameter
3006 object CompositeLookup (string name)
3008 int pos = name.IndexOf (".");
3009 string left = name.Substring (0, pos);
3012 o = SimpleLookup (left);
3014 string right = name.Substring (pos + 1);
3015 return new MemberAccess ((Expression) o, right);
3021 object QualifiedIdentifier (string name, Location l)
3025 if (name.IndexOf ('.') == -1)
3026 o = SimpleLookup (name);
3028 o = CompositeLookup (name);
3031 o = new SimpleName (name, l);
3036 Block declare_local_variables (string type, ArrayList variable_declarators)
3038 Block implicit_block;
3039 ArrayList inits = null;
3042 // We use the `Used' property to check whether statements
3043 // have been added to the current block. If so, we need
3044 // to create another block to contain the new declaration
3045 // otherwise, as an optimization, we use the same block to
3046 // add the declaration.
3048 // FIXME: A further optimization is to check if the statements
3049 // that were added were added as part of the initialization
3050 // below. In which case, no other statements have been executed
3051 // and we might be able to reduce the number of blocks for
3052 // situations like this:
3054 // int j = 1; int k = j + 1;
3056 if (current_block.Used)
3057 implicit_block = new Block (current_block, true);
3059 implicit_block = new Block (current_block, true);
3061 foreach (VariableDeclaration decl in variable_declarators){
3062 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3063 if (decl.expression_or_array_initializer != null){
3065 inits = new ArrayList ();
3069 Location l = lexer.Location;
3070 Report.Error (128, l, "A local variable `" + decl.identifier +
3071 "' is already defined in this scope");
3076 return implicit_block;
3078 foreach (VariableDeclaration decl in inits){
3079 if (decl.expression_or_array_initializer is Expression){
3080 Expression expr = (Expression) decl.expression_or_array_initializer;
3083 assign = new Assign (new LocalVariableReference (
3084 implicit_block, decl.identifier),
3085 expr, lexer.Location);
3086 implicit_block.AddStatement (new StatementExpression (assign));
3088 Console.WriteLine ("Not handling Array initializers yet");
3092 return implicit_block;
3095 void CheckConstant (Expression expr)
3100 void CheckBoolean (Expression expr)
3105 void CheckAttributeTarget (string a)
3109 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3113 Location l = lexer.Location;
3114 Report.Error (658, l, "Invalid attribute target");
3120 void CheckUnaryOperator (Operator.OpType op)
3124 case Operator.OpType.Negate:
3125 case Operator.OpType.BitComplement:
3126 case Operator.OpType.Increment:
3127 case Operator.OpType.Decrement:
3128 case Operator.OpType.True:
3129 case Operator.OpType.False:
3130 case Operator.OpType.Addition:
3131 case Operator.OpType.Subtraction:
3136 Location l = lexer.Location;
3137 Report.Error (1019, l, "Overloadable unary operator expected");
3143 void CheckBinaryOperator (Operator.OpType op)
3147 case Operator.OpType.Addition:
3148 case Operator.OpType.Subtraction:
3149 case Operator.OpType.Multiply:
3150 case Operator.OpType.Division:
3151 case Operator.OpType.Modulus:
3152 case Operator.OpType.BitwiseAnd:
3153 case Operator.OpType.BitwiseOr:
3154 case Operator.OpType.ExclusiveOr:
3155 case Operator.OpType.LeftShift:
3156 case Operator.OpType.RightShift:
3157 case Operator.OpType.Equality:
3158 case Operator.OpType.Inequality:
3159 case Operator.OpType.GreaterThan:
3160 case Operator.OpType.LessThan:
3161 case Operator.OpType.GreaterThanOrEqual:
3162 case Operator.OpType.LessThanOrEqual:
3166 Location l = lexer.Location;
3167 Report.Error (1020, l, "Overloadable binary operator expected");
3173 void output (string s)
3175 Console.WriteLine (s);
3178 void note (string s)
3180 // Used to put annotations
3185 public Tokenizer Lexer {
3191 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
3193 current_namespace = new Namespace (null, "");
3195 this.tree = rc.Tree;
3198 current_container = tree.Types;
3199 current_container.Namespace = current_namespace;
3201 lexer = new Tokenizer (input, name);
3204 public override int parse ()
3206 StringBuilder value = new StringBuilder ();
3210 if (yacc_verbose_flag)
3211 yyparse (lexer, new yydebug.yyDebugSimple ());
3214 } catch (Exception e){
3215 // Console.WriteLine ("Fatal error: " + name);
3216 // Console.WriteLine (lexer.location);
3218 Console.WriteLine (lexer.location + " : Parsing error");
3219 Console.WriteLine (e);
3223 return global_errors;