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;
688 method.Block = (Block) $2;
689 CheckDef (current_container.AddMethod (method), method.Name);
691 current_local_parameters = null;
700 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
702 Method method = new Method ((string) $3, (int) $2, (string) $4,
703 (Parameters) $6, (Attributes) $1, lexer.Location);
705 current_local_parameters = (Parameters) $6;
713 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
715 Method method = new Method ("System.Void", (int) $2, (string) $4,
716 (Parameters) $6, (Attributes) $1, lexer.Location);
718 current_local_parameters = (Parameters) $6;
725 | SEMICOLON { $$ = null; }
728 opt_formal_parameter_list
729 : /* empty */ { $$ = new Parameters (null, null); }
730 | formal_parameter_list
733 formal_parameter_list
736 ArrayList pars_list = (ArrayList) $1;
738 Parameter [] pars = new Parameter [pars_list.Count];
739 pars_list.CopyTo (pars);
741 $$ = new Parameters (pars, null);
743 | fixed_parameters COMMA parameter_array
745 ArrayList pars_list = (ArrayList) $1;
747 Parameter [] pars = new Parameter [pars_list.Count];
748 pars_list.CopyTo (pars);
750 $$ = new Parameters (pars, (Parameter) $3);
754 $$ = new Parameters (null, (Parameter) $1);
761 ArrayList pars = new ArrayList ();
766 | fixed_parameters COMMA fixed_parameter
768 ArrayList pars = (ArrayList) $1;
777 opt_parameter_modifier
781 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
785 opt_parameter_modifier
786 : /* empty */ { $$ = Parameter.Modifier.NONE; }
791 : REF { $$ = Parameter.Modifier.REF; }
792 | OUT { $$ = Parameter.Modifier.OUT; }
796 : opt_attributes PARAMS type IDENTIFIER
798 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
799 note ("type must be a single-dimension array type");
804 : IDENTIFIER { $$ = $1.ToString (); }
805 | interface_type DOT IDENTIFIER { $$ = $1.ToString () + "." + $3.ToString (); }
814 Parameter implicit_value_parameter;
815 implicit_value_parameter = new Parameter ((string) $3, "value", Parameter.Modifier.NONE, null);
817 lexer.properties = true;
819 implicit_value_parameters = new Parameter [1];
820 implicit_value_parameters [0] = implicit_value_parameter;
822 accessor_declarations
824 lexer.properties = false;
829 DictionaryEntry pair = (DictionaryEntry) $7;
830 Block get_block = null;
831 Block set_block = null;
833 if (pair.Key != null)
834 get_block = (Block) pair.Key;
835 if (pair.Value != null)
836 set_block = (Block) pair.Value;
838 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block, (Attributes) $1);
840 CheckDef (current_container.AddProperty (prop), prop.Name);
841 implicit_value_parameters = null;
845 accessor_declarations
846 : get_accessor_declaration opt_set_accessor_declaration
848 $$ = new DictionaryEntry ($1, $2);
850 | set_accessor_declaration opt_get_accessor_declaration
852 $$ = new DictionaryEntry ($2, $1);
856 opt_get_accessor_declaration
857 : /* empty */ { $$ = null; }
858 | get_accessor_declaration
861 opt_set_accessor_declaration
862 : /* empty */ { $$ = null; }
863 | set_accessor_declaration
866 get_accessor_declaration
869 // If this is not the case, then current_local_parameters has already
870 // been set in indexer_declaration
871 if (parsing_indexer == false)
872 current_local_parameters = new Parameters (implicit_value_parameters, null);
879 current_local_parameters = null;
883 set_accessor_declaration
886 if (parsing_indexer == false)
887 current_local_parameters = new Parameters (implicit_value_parameters, null);
892 current_local_parameters = null;
898 | SEMICOLON { $$ = new Block (null); }
901 interface_declaration
906 Interface new_interface;
907 string full_interface_name = MakeName ((string) $4);
909 new_interface = new Interface (current_container, full_interface_name, (int) $2,
910 (Attributes) $1, lexer.Location);
911 if (current_interface != null) {
912 Location l = lexer.Location;
913 rc.Report.Error (-2, l, "Internal compiler error: interface inside interface");
915 current_interface = new_interface;
916 tree.RecordInterface (full_interface_name, new_interface);
921 Interface new_interface = (Interface) current_interface;
924 new_interface.Bases = (ArrayList) $6;
926 current_interface = null;
927 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
932 : /* empty */ { $$ = null; }
937 : COLON interface_type_list { $$ = $2; }
943 ArrayList interfaces = new ArrayList ();
948 | interface_type_list COMMA interface_type
950 ArrayList interfaces = (ArrayList) $1;
958 opt_interface_member_declarations
962 opt_interface_member_declarations
964 | interface_member_declarations
967 interface_member_declarations
968 : interface_member_declaration
969 | interface_member_declarations interface_member_declaration
972 interface_member_declaration
973 : interface_method_declaration
975 InterfaceMethod m = (InterfaceMethod) $1;
977 CheckDef (current_interface.AddMethod (m), m.Name);
979 | interface_property_declaration
981 InterfaceProperty p = (InterfaceProperty) $1;
983 CheckDef (current_interface.AddProperty (p), p.Name);
985 | interface_event_declaration
987 InterfaceEvent e = (InterfaceEvent) $1;
989 CheckDef (current_interface.AddEvent (e), e.Name);
991 | interface_indexer_declaration
993 InterfaceIndexer i = (InterfaceIndexer) $1;
995 CheckDef (current_interface.AddIndexer (i), "indexer");
1000 : /* empty */ { $$ = false; }
1001 | NEW { $$ = true; }
1004 interface_method_declaration
1005 : opt_attributes opt_new type IDENTIFIER
1006 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1009 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1011 | opt_attributes opt_new VOID IDENTIFIER
1012 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1015 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1019 interface_property_declaration
1024 { lexer.properties = true; }
1026 { lexer.properties = false; }
1031 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
1032 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1);
1037 : opt_attributes GET SEMICOLON { $$ = 1; }
1038 | opt_attributes SET SEMICOLON { $$ = 2; }
1039 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1041 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1045 interface_event_declaration
1046 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1048 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1052 interface_indexer_declaration
1053 : opt_attributes opt_new type THIS
1054 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1056 { lexer.properties = true; }
1058 { lexer.properties = false; }
1061 int a_flags = (int) $10;
1063 bool do_get = (a_flags & 1) == 1;
1064 bool do_set = (a_flags & 2) == 2;
1066 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set, (bool) $2, (Attributes) $1);
1070 operator_declaration
1071 : opt_attributes opt_modifiers operator_declarator block
1073 OperatorDeclaration decl = (OperatorDeclaration) $3;
1075 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1076 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1078 // Note again, checking is done in semantic analysis
1079 current_container.AddOperator (op);
1084 : type OPERATOR overloadable_operator
1085 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1087 CheckUnaryOperator ((Operator.OpType) $3);
1089 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1090 null, null, lexer.Location);
1092 | type OPERATOR overloadable_operator
1094 type IDENTIFIER COMMA
1098 CheckBinaryOperator ((Operator.OpType) $3);
1100 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1101 (string) $8, (string) $9, lexer.Location);
1103 | conversion_operator_declarator
1106 overloadable_operator
1108 : BANG { $$ = Operator.OpType.Negate; }
1109 | TILDE { $$ = Operator.OpType.BitComplement; }
1110 | OP_INC { $$ = Operator.OpType.Increment; }
1111 | OP_DEC { $$ = Operator.OpType.Decrement; }
1112 | TRUE { $$ = Operator.OpType.True; }
1113 | FALSE { $$ = Operator.OpType.False; }
1114 // Unary and binary:
1115 | PLUS { $$ = Operator.OpType.Addition; }
1116 | MINUS { $$ = Operator.OpType.Subtraction; }
1118 | STAR { $$ = Operator.OpType.Multiply; }
1119 | DIV { $$ = Operator.OpType.Division; }
1120 | PERCENT { $$ = Operator.OpType.Modulus; }
1121 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1122 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1123 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1124 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1125 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1126 | OP_EQ { $$ = Operator.OpType.Equality; }
1127 | OP_NE { $$ = Operator.OpType.Inequality; }
1128 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1129 | OP_LT { $$ = Operator.OpType.LessThan; }
1130 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1131 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1134 conversion_operator_declarator
1135 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1137 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1138 null, null, lexer.Location);
1140 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1142 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1143 null, null, lexer.Location);
1147 constructor_declaration
1150 constructor_declarator
1153 Constructor c = (Constructor) $3;
1154 c.Block = (Block) $4;
1155 c.ModFlags = (int) $2;
1157 if ((c.ModFlags & Modifiers.STATIC) != 0){
1158 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1159 Location l = lexer.Location;
1160 rc.Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1163 if (c.Initializer != null){
1164 Location l = lexer.Location;
1165 rc.Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1168 if (!c.Parameters.Empty){
1169 Location l = lexer.Location;
1170 rc.Report.Error (103, l, "Static constructors should not have parameters");
1174 CheckDef (current_container.AddConstructor (c), c.Name);
1176 current_local_parameters = null;
1180 constructor_declarator
1182 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1183 opt_constructor_initializer
1185 Location l = lexer.Location;
1187 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5, l);
1189 current_local_parameters = (Parameters) $3;
1193 opt_constructor_initializer
1194 : /* empty */ { $$ = null; }
1195 | constructor_initializer
1198 constructor_initializer
1199 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1201 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1203 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1205 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1209 destructor_declaration
1210 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1212 Method d = new Method ("System.Void", 0, "Finalize",
1213 new Parameters (null, null), (Attributes) $1, lexer.Location);
1215 d.Block = (Block) $6;
1216 CheckDef (current_container.AddMethod (d), d.Name);
1223 EVENT type variable_declarators SEMICOLON
1225 foreach (VariableDeclaration var in (ArrayList) $5) {
1227 // FIXME : Is this right ?
1228 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1229 (int) $2, null, null, (Attributes) $1);
1231 CheckDef (current_container.AddEvent (e), e.Name);
1237 EVENT type member_name
1238 OPEN_BRACE event_accessor_declarations CLOSE_BRACE
1240 DictionaryEntry pair = (DictionaryEntry) $7;
1241 Block add_block = null;
1242 Block rem_block = null;
1244 if (pair.Key != null)
1245 add_block = (Block) pair.Key;
1246 if (pair.Value != null)
1247 rem_block = (Block) pair.Value;
1249 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block, (Attributes) $1);
1251 CheckDef (current_container.AddEvent (e), e.Name);
1255 event_accessor_declarations
1256 : add_accessor_declaration remove_accessor_declaration
1258 $$ = new DictionaryEntry ($1, $2);
1260 | remove_accessor_declaration add_accessor_declaration
1262 $$ = new DictionaryEntry ($2, $1);
1266 add_accessor_declaration
1267 : opt_attributes ADD block
1273 remove_accessor_declaration
1274 : opt_attributes REMOVE block
1281 : opt_attributes opt_modifiers indexer_declarator
1284 IndexerDeclaration decl = (IndexerDeclaration) $3;
1286 lexer.properties = true;
1287 parsing_indexer = true;
1289 current_local_parameters = decl.param_list;
1291 accessor_declarations
1293 lexer.properties = false;
1294 parsing_indexer = false;
1298 // The signature is computed from the signature of the indexer. Look
1299 // at section 3.6 on the spec
1302 IndexerDeclaration decl = (IndexerDeclaration) $3;
1303 DictionaryEntry pair = (DictionaryEntry) $6;
1304 Block get_block = null;
1305 Block set_block = null;
1307 if (pair.Key != null)
1308 get_block = (Block) pair.Key;
1309 if (pair.Value != null)
1310 set_block = (Block) pair.Value;
1312 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1313 get_block, set_block, (Attributes) $1);
1315 // Note that there is no equivalent of CheckDef for this case
1316 // We shall handle this in semantic analysis
1318 current_container.AddIndexer (indexer);
1320 current_local_parameters = null;
1325 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1327 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
1329 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1331 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
1343 string name = (string) $4;
1344 Enum e = new Enum ((string) $5, (int) $2, name, (Attributes) $1, lexer.Location);
1346 foreach (VariableDeclaration ev in (ArrayList) $6){
1347 CheckDef (e.AddEnumMember (ev.identifier,
1348 (Expression) ev.expression_or_array_initializer),
1352 CheckDef (current_container.AddEnum (e), name);
1357 : /* empty */ { $$ = "System.Int32"; }
1358 | COLON integral_type { $$ = $2; }
1362 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1368 opt_enum_member_declarations
1369 : /* empty */ { $$ = new ArrayList (); }
1370 | enum_member_declarations opt_comma { $$ = $1; }
1373 enum_member_declarations
1374 : enum_member_declaration
1376 ArrayList l = new ArrayList ();
1381 | enum_member_declarations COMMA enum_member_declaration
1383 ArrayList l = (ArrayList) $1;
1391 enum_member_declaration
1392 : opt_attributes IDENTIFIER
1394 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1396 | opt_attributes IDENTIFIER ASSIGN expression
1398 $$ = new VariableDeclaration ((string) $2, $4, lexer.Location);
1402 delegate_declaration
1406 IDENTIFIER OPEN_PARENS
1407 formal_parameter_list
1411 Delegate del = new Delegate ((string) $4, (int) $2, (string) $5, (Parameters) $7,
1412 (Attributes) $1, lexer.Location);
1414 CheckDef (current_container.AddDelegate (del), del.Name);
1419 IDENTIFIER OPEN_PARENS
1420 formal_parameter_list
1424 Delegate del = new Delegate (null, (int) $2, (string) $5, (Parameters) $7,
1425 (Attributes) $1, lexer.Location);
1427 CheckDef (current_container.AddDelegate (del), del.Name);
1432 : namespace_or_type_name
1435 namespace_or_type_name
1436 : qualified_identifier
1440 * Before you think of adding a return_type, notice that we have been
1441 * using two rules in the places where it matters (one rule using type
1442 * and another identical one that uses VOID as the return type). This
1443 * gets rid of a shift/reduce couple
1446 : type_name { /* class_type */
1448 This does interfaces, delegates, struct_types, class_types,
1449 parent classes, and more! 4.2
1460 ArrayList types = new ArrayList ();
1465 | type_list COMMA type
1467 ArrayList types = (ArrayList) $1;
1475 * replaces all the productions for isolating the various
1476 * simple types, but we need this to reuse it easily in local_variable_type
1479 : OBJECT { $$ = "System.Object"; }
1480 | STRING { $$ = "System.String"; }
1481 | BOOL { $$ = "System.Boolean"; }
1482 | DECIMAL { $$ = "System.Decimal"; }
1483 | FLOAT { $$ = "System.Single"; }
1484 | DOUBLE { $$ = "System.Double"; }
1489 : SBYTE { $$ = "System.SByte"; }
1490 | BYTE { $$ = "System.Byte"; }
1491 | SHORT { $$ = "System.Int16"; }
1492 | USHORT { $$ = "System.UInt16"; }
1493 | INT { $$ = "System.Int32"; }
1494 | UINT { $$ = "System.UInt32"; }
1495 | LONG { $$ = "System.Int64"; }
1496 | ULONG { $$ = "System.UInt64"; }
1497 | CHAR { $$ = "System.Char"; }
1505 : type rank_specifiers
1507 $$ = (string) $1 + (string) $2;
1512 // Expressions, section 7.5
1520 | qualified_identifier
1522 string name = (string) $1;
1525 $$ = QualifiedIdentifier (name, lexer.Location);
1527 | parenthesized_expression
1529 | invocation_expression
1533 | post_increment_expression
1534 | post_decrement_expression
1538 | checked_expression
1539 | unchecked_expression
1546 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1547 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1548 | NULL { $$ = new NullLiteral (); }
1552 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1553 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1554 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1559 object v = lexer.Value;
1562 // FIXME: Possible optimization would be to
1563 // compute the *Literal objects directly in the scanner
1566 $$ = new IntLiteral ((Int32) v);
1568 $$ = new UIntLiteral ((UInt32) v);
1570 $$ = new LongLiteral ((Int64) v);
1571 else if (v is ulong)
1572 $$ = new ULongLiteral ((UInt64) v);
1574 Console.WriteLine ("OOPS. Unexpected result from scanner");
1579 : TRUE { $$ = new BoolLiteral (true); }
1580 | FALSE { $$ = new BoolLiteral (false); }
1583 parenthesized_expression
1584 : OPEN_PARENS expression CLOSE_PARENS
1589 : primary_expression DOT IDENTIFIER
1591 $$ = new MemberAccess ((Expression) $1, (string) $3);
1593 | predefined_type DOT IDENTIFIER
1595 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1603 invocation_expression
1604 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1607 // if $1 is MethodGroup
1608 // $$ = new Call ($1, $3);
1610 // $$ = new DelegateCall ($1, $3);
1612 Location l = lexer.Location;
1613 rc.Report.Error (1, l, "THIS IS CRAZY");
1615 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1621 : /* empty */ { $$ = null; }
1628 ArrayList list = new ArrayList ();
1632 | argument_list COMMA argument
1634 ArrayList list = (ArrayList) $1;
1643 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1645 | REF variable_reference
1647 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1649 | OUT variable_reference
1651 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1656 : expression { note ("section 5.4"); $$ = $1; }
1660 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1662 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
1669 ArrayList list = new ArrayList ();
1673 | expression_list COMMA expression
1675 ArrayList list = (ArrayList) $1;
1689 : BASE DOT IDENTIFIER
1691 $$ = new BaseAccess (BaseAccess.BaseAccessType.Member, (string) $3, null);
1693 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1695 $$ = new BaseAccess (BaseAccess.BaseAccessType.Indexer, null, (ArrayList) $3);
1699 post_increment_expression
1700 : primary_expression OP_INC
1702 $$ = new Unary (Unary.Operator.PostIncrement, (Expression) $1, lexer.Location);
1706 post_decrement_expression
1707 : primary_expression OP_DEC
1709 $$ = new Unary (Unary.Operator.PostDecrement, (Expression) $1, lexer.Location);
1714 : object_or_delegate_creation_expression
1715 | array_creation_expression
1718 object_or_delegate_creation_expression
1719 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1721 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1725 array_creation_expression
1726 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
1728 opt_array_initializer
1730 $$ = new New ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
1750 | rank_specifier rank_specifiers
1752 $$ = (string) $1 + (string) $2;
1757 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1759 $$ = "[" + (string) $2 + "]";
1779 | dim_separators COMMA
1781 $$ =(string) $1 + ",";
1785 opt_array_initializer
1797 : OPEN_BRACE CLOSE_BRACE
1799 ArrayList list = new ArrayList ();
1802 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1804 $$ = (ArrayList) $2;
1808 variable_initializer_list
1809 : variable_initializer
1811 ArrayList list = new ArrayList ();
1815 | variable_initializer_list COMMA variable_initializer
1817 ArrayList list = (ArrayList) $1;
1824 : TYPEOF OPEN_PARENS type CLOSE_PARENS
1826 $$ = new TypeOf ((string) $3);
1831 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
1832 $$ = new SizeOf ((string) $3);
1834 note ("Verify type is unmanaged");
1835 note ("if (5.8) builtin, yield constant expression");
1840 : CHECKED OPEN_PARENS expression CLOSE_PARENS
1842 $$ = new CheckedExpr ((Expression) $3);
1846 unchecked_expression
1847 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
1849 $$ = new UnCheckedExpr ((Expression) $3);
1854 : primary_expression
1855 | PLUS unary_expression {
1856 $$ = new Unary (Unary.Operator.Addition, (Expression) $2, lexer.Location);
1858 | MINUS unary_expression
1860 $$ = new Unary (Unary.Operator.Subtraction, (Expression) $2, lexer.Location);
1862 | BANG unary_expression
1864 $$ = new Unary (Unary.Operator.Negate, (Expression) $2, lexer.Location);
1866 | TILDE unary_expression
1868 $$ = new Unary (Unary.Operator.BitComplement, (Expression) $2, lexer.Location);
1870 | STAR unary_expression
1872 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
1874 | BITWISE_AND unary_expression
1876 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
1878 | OP_INC unary_expression
1880 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1882 | OP_DEC unary_expression
1884 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1888 we can not do cast expressions at this level,
1889 as there is an ambiguity. Check "Cast Expressions" 7.6.8
1890 for the recipe to handle this.
1894 pre_increment_expression
1895 : OP_INC unary_expression
1897 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1901 pre_decrement_expression
1902 : OP_DEC unary_expression
1904 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1910 * FIXME: This is actually wrong, it should be `type' but that
1911 * introduces a lot of {shift,reduce}/reduces
1913 * This is really really wrong. We need to track down
1914 * the source of problems with QIs because expressions like:
1915 * foreach (string s in (string []) object) wont be parsed.
1917 : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
1919 $$ = new Cast ((string) $2, (Expression) $4);
1921 | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
1923 $$ = new Cast ((string) $2, (Expression) $4);
1927 multiplicative_expression
1929 | multiplicative_expression STAR unary_expression
1931 $$ = new Binary (Binary.Operator.Multiply,
1932 (Expression) $1, (Expression) $3, lexer.Location);
1934 | multiplicative_expression DIV unary_expression
1936 $$ = new Binary (Binary.Operator.Division,
1937 (Expression) $1, (Expression) $3, lexer.Location);
1939 | multiplicative_expression PERCENT unary_expression
1941 $$ = new Binary (Binary.Operator.Modulus,
1942 (Expression) $1, (Expression) $3, lexer.Location);
1947 : multiplicative_expression
1948 | additive_expression PLUS multiplicative_expression
1950 $$ = new Binary (Binary.Operator.Addition,
1951 (Expression) $1, (Expression) $3, lexer.Location);
1953 | additive_expression MINUS multiplicative_expression
1955 $$ = new Binary (Binary.Operator.Subtraction,
1956 (Expression) $1, (Expression) $3, lexer.Location);
1961 : additive_expression
1962 | shift_expression OP_SHIFT_LEFT additive_expression
1964 $$ = new Binary (Binary.Operator.LeftShift,
1965 (Expression) $1, (Expression) $3, lexer.Location);
1967 | shift_expression OP_SHIFT_RIGHT additive_expression
1969 $$ = new Binary (Binary.Operator.RightShift,
1970 (Expression) $1, (Expression) $3, lexer.Location);
1974 relational_expression
1976 | relational_expression OP_LT shift_expression
1978 $$ = new Binary (Binary.Operator.LessThan,
1979 (Expression) $1, (Expression) $3, lexer.Location);
1981 | relational_expression OP_GT shift_expression
1983 $$ = new Binary (Binary.Operator.GreaterThan,
1984 (Expression) $1, (Expression) $3, lexer.Location);
1986 | relational_expression OP_LE shift_expression
1988 $$ = new Binary (Binary.Operator.LessThanOrEqual,
1989 (Expression) $1, (Expression) $3, lexer.Location);
1991 | relational_expression OP_GE shift_expression
1993 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
1994 (Expression) $1, (Expression) $3, lexer.Location);
1996 | relational_expression IS type
1998 $$ = new Probe (Probe.Operator.Is,
1999 (Expression) $1, (string) $3);
2001 | relational_expression AS type
2003 $$ = new Probe (Probe.Operator.As,
2004 (Expression) $1, (string) $3);
2009 : relational_expression
2010 | equality_expression OP_EQ relational_expression
2012 $$ = new Binary (Binary.Operator.Equality,
2013 (Expression) $1, (Expression) $3, lexer.Location);
2015 | equality_expression OP_NE relational_expression
2017 $$ = new Binary (Binary.Operator.Inequality,
2018 (Expression) $1, (Expression) $3, lexer.Location);
2023 : equality_expression
2024 | and_expression BITWISE_AND equality_expression
2026 $$ = new Binary (Binary.Operator.BitwiseAnd,
2027 (Expression) $1, (Expression) $3, lexer.Location);
2031 exclusive_or_expression
2033 | exclusive_or_expression CARRET and_expression
2035 $$ = new Binary (Binary.Operator.ExclusiveOr,
2036 (Expression) $1, (Expression) $3, lexer.Location);
2040 inclusive_or_expression
2041 : exclusive_or_expression
2042 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2044 $$ = new Binary (Binary.Operator.BitwiseOr,
2045 (Expression) $1, (Expression) $3, lexer.Location);
2049 conditional_and_expression
2050 : inclusive_or_expression
2051 | conditional_and_expression OP_AND inclusive_or_expression
2053 $$ = new Binary (Binary.Operator.LogicalAnd,
2054 (Expression) $1, (Expression) $3, lexer.Location);
2058 conditional_or_expression
2059 : conditional_and_expression
2060 | conditional_or_expression OP_OR conditional_and_expression
2062 $$ = new Binary (Binary.Operator.LogicalOr,
2063 (Expression) $1, (Expression) $3, lexer.Location);
2067 conditional_expression
2068 : conditional_or_expression
2069 | conditional_or_expression INTERR expression COLON expression
2071 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2075 assignment_expression
2076 : unary_expression ASSIGN expression
2078 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2080 | unary_expression OP_MULT_ASSIGN expression
2082 Location l = lexer.Location;
2084 $$ = new Assign ((Expression) $1,
2085 new Binary (Binary.Operator.Multiply,
2087 (Expression) $3, l), l);
2089 | unary_expression OP_DIV_ASSIGN expression
2091 Location l = lexer.Location;
2093 $$ = new Assign ((Expression) $1,
2094 new Binary (Binary.Operator.Division,
2096 (Expression) $3, l), l);
2098 | unary_expression OP_MOD_ASSIGN expression
2100 Location l = lexer.Location;
2102 $$ = new Assign ((Expression) $1,
2103 new Binary (Binary.Operator.Modulus,
2105 (Expression) $3, l), l);
2107 | unary_expression OP_ADD_ASSIGN expression
2109 Location l = lexer.Location;
2111 $$ = new Assign ((Expression) $1,
2112 new Binary (Binary.Operator.Addition,
2114 (Expression) $3, l), l);
2116 | unary_expression OP_SUB_ASSIGN expression
2118 Location l = lexer.Location;
2120 $$ = new Assign ((Expression) $1,
2121 new Binary (Binary.Operator.Subtraction,
2123 (Expression) $3, l), l);
2125 | unary_expression OP_SHIFT_LEFT_ASSIGN expression
2127 Location l = lexer.Location;
2129 $$ = new Assign ((Expression) $1,
2130 new Binary (Binary.Operator.LeftShift,
2132 (Expression) $3, l), l);
2134 | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2136 Location l = lexer.Location;
2138 $$ = new Assign ((Expression) $1,
2139 new Binary (Binary.Operator.RightShift,
2141 (Expression) $3, l), l);
2143 | unary_expression OP_AND_ASSIGN expression
2145 Location l = lexer.Location;
2147 $$ = new Assign ((Expression) $1,
2148 new Binary (Binary.Operator.BitwiseAnd,
2150 (Expression) $3, l), l);
2152 | unary_expression OP_OR_ASSIGN expression
2154 Location l = lexer.Location;
2156 $$ = new Assign ((Expression) $1,
2157 new Binary (Binary.Operator.BitwiseOr,
2159 (Expression) $3, l), l);
2161 | unary_expression OP_XOR_ASSIGN expression
2163 Location l = lexer.Location;
2165 $$ = new Assign ((Expression) $1,
2166 new Binary (Binary.Operator.ExclusiveOr,
2168 (Expression) $3, l), l);
2173 : conditional_expression
2174 | assignment_expression
2182 : expression { CheckBoolean ((Expression) $1); $$ = $1; }
2194 string full_class_name = MakeName ((string) $4);
2196 new_class = new Class (rc, current_container, full_class_name, (int) $2,
2197 (Attributes) $1, lexer.Location);
2198 current_container = new_class;
2199 current_container.Namespace = current_namespace;
2200 tree.RecordClass (full_class_name, new_class);
2206 Class new_class = (Class) current_container;
2209 new_class.Bases = (ArrayList) $6;
2211 current_container = current_container.Parent;
2212 CheckDef (current_container.AddClass (new_class), new_class.Name);
2219 : /* empty */ { $$ = (int) 0; }
2225 | modifiers modifier
2230 if ((m1 & m2) != 0) {
2231 Location l = lexer.Location;
2232 rc.Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2234 $$ = (int) (m1 | m2);
2239 : NEW { $$ = Modifiers.NEW; }
2240 | PUBLIC { $$ = Modifiers.PUBLIC; }
2241 | PROTECTED { $$ = Modifiers.PROTECTED; }
2242 | INTERNAL { $$ = Modifiers.INTERNAL; }
2243 | PRIVATE { $$ = Modifiers.PRIVATE; }
2244 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2245 | SEALED { $$ = Modifiers.SEALED; }
2246 | STATIC { $$ = Modifiers.STATIC; }
2247 | READONLY { $$ = Modifiers.READONLY; }
2248 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2249 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2250 | EXTERN { $$ = Modifiers.EXTERN; }
2254 : /* empty */ { $$ = null; }
2255 | class_base { $$ = $1; }
2259 : COLON type_list { $$ = $2; }
2267 // A block is "contained" on the following places:
2269 // property_declaration as part of the accessor body (get/set)
2270 // operator_declaration
2271 // constructor_declaration
2272 // destructor_declaration
2273 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2278 current_block = new Block (current_block);
2280 opt_statement_list CLOSE_BRACE
2282 while (current_block.Implicit)
2283 current_block = current_block.Parent;
2285 current_block = current_block.Parent;
2296 | statement_list statement
2300 : declaration_statement
2302 if ((Block) $1 != current_block){
2303 current_block.AddStatement ((Statement) $1);
2304 current_block = (Block) $1;
2307 | embedded_statement
2309 current_block.AddStatement ((Statement) $1);
2313 current_block.AddStatement ((Statement) $1);
2320 | expression_statement
2321 | selection_statement
2322 | iteration_statement
2326 | unchecked_statement
2334 $$ = new EmptyStatement ();
2339 : IDENTIFIER COLON statement
2341 string lab = (String) $1;
2344 block = new Block (current_block, lab);
2345 block.AddStatement ((Statement) $3);
2348 if (!current_block.AddLabel (lab, block)){
2349 Location l = lexer.Location;
2350 rc.Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2356 declaration_statement
2357 : local_variable_declaration SEMICOLON // done
2358 | local_constant_declaration SEMICOLON // finishme
2362 * The following is from Rhys' grammar:
2363 * > Types in local variable declarations must be recognized as
2364 * > expressions to prevent reduce/reduce errors in the grammar.
2365 * > The expressions are converted into types during semantic analysis.
2368 : primary_expression type_suffixes
2370 // FIXME: Do something smart here regarding the composition of the type.
2372 // Ok, the above "primary_expression" is there to get rid of
2373 // both reduce/reduce and shift/reduces in the grammar, it should
2374 // really just be "type_name". If you use type_name, a reduce/reduce
2375 // creeps up. If you use qualified_identifier (which is all we need
2376 // really) two shift/reduces appear.
2378 // So, instead we do a super trick: we just allow ($1) to be a
2379 // SimpleName Expression.
2381 if (((Expression) $1) is SimpleName)
2382 $$ = ((SimpleName) $1).Name;
2384 Location l = lexer.Location;
2385 rc.Report.Error (-1, l, "Invalid Type definition");
2386 $$ = "System.Object";
2389 | builtin_types type_suffixes
2391 $$ = (string) $1 + (string) $2;
2395 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2398 // // FIXME: this is a string that represents the type
2399 // // Figure out something to make this work.
2414 | type_suffix_list type_suffix
2416 $$ = (string) $1 + (string) $2;
2421 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2423 $$ = "[" + (string) $2 + "]";
2427 local_variable_declaration
2428 : local_variable_type variable_declarators
2430 $$ = declare_local_variables ((string) $1, (ArrayList) $2);
2434 local_constant_declaration
2435 : CONST type constant_declarator
2439 expression_statement
2440 : statement_expression SEMICOLON
2447 // We have to do the wrapping here and not in the case above,
2448 // because statement_expression is used for example in for_statement
2450 statement_expression
2451 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2452 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2453 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2454 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2455 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2456 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2457 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2460 object_creation_expression
2461 : object_or_delegate_creation_expression
2462 { note ("complain if this is a delegate maybe?"); }
2471 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2474 $$ = new If ((Expression) $3, (Statement) $5);
2476 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2477 embedded_statement ELSE embedded_statement
2479 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2484 : SWITCH OPEN_PARENS expression CLOSE_PARENS
2487 $$ = new Switch ((Expression) $3, (ArrayList) $5);
2501 : /* empty */ { $$ = new ArrayList (); }
2508 ArrayList sections = new ArrayList ();
2513 | switch_sections switch_section
2515 ArrayList sections = (ArrayList) $1;
2525 current_block = new Block (current_block);
2529 while (current_block.Implicit)
2530 current_block = current_block.Parent;
2531 $$ = new SwitchSection ((ArrayList) $1, current_block);
2532 current_block = current_block.Parent;
2539 ArrayList labels = new ArrayList ();
2544 | switch_labels switch_label
2546 ArrayList labels = (ArrayList) ($1);
2554 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2); }
2555 | DEFAULT COLON { $$ = new SwitchLabel (null); }
2566 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2568 $$ = new While ((Expression) $3, (Statement) $5);
2573 : DO embedded_statement
2574 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2576 $$ = new Do ((Statement) $2, (Expression) $5);
2582 opt_for_initializer SEMICOLON
2583 opt_for_condition SEMICOLON
2584 opt_for_iterator CLOSE_PARENS
2587 $$ = new For ((Statement) $3, (Expression) $5, (Statement) $7, (Statement) $9);
2592 : /* empty */ { $$ = new EmptyStatement (); }
2597 : local_variable_declaration
2598 | statement_expression_list
2602 : /* empty */ { $$ = new BoolLiteral (true); }
2603 | boolean_expression
2607 : /* empty */ { $$ = new EmptyStatement (); }
2612 : statement_expression_list
2615 statement_expression_list
2616 : statement_expression
2618 Block b = new Block (null, true);
2620 b.AddStatement ((Statement) $1);
2623 | statement_expression_list COMMA statement_expression
2625 Block b = (Block) $1;
2627 b.AddStatement ((Statement) $3);
2633 : FOREACH OPEN_PARENS type IDENTIFIER IN expression CLOSE_PARENS
2636 string temp_id = current_block.MakeInternalID ();
2637 ExpressionStatement assign_e;
2639 Statement getcurrent;
2640 Block foreach_block, child_block;
2641 Location l = lexer.Location;
2643 foreach_block = new Block (current_block, true);
2645 foreach_block.AddVariable ("System.IEnumerator", temp_id, l);
2646 foreach_block.AddVariable ((string) $3, (string) $4, l);
2647 assign_e = new Assign (new LocalVariableReference (foreach_block, temp_id),
2649 new MemberAccess ((Expression) $6, "GetEnumerator"),
2650 null, lexer.Location), lexer.Location);
2651 current_block.AddStatement (new StatementExpression (assign_e));
2652 ma = new MemberAccess (new LocalVariableReference (foreach_block, temp_id), "MoveNext");
2653 child_block = new Block (current_block);
2655 getcurrent = new StatementExpression (
2657 new LocalVariableReference (foreach_block, (string) $4),
2661 new LocalVariableReference (foreach_block, temp_id), "Current")),
2664 child_block.AddStatement (getcurrent);
2665 child_block.AddStatement ((Statement) $8);
2666 foreach_block.AddStatement (new While (ma, (Statement) child_block));
2674 | continue_statement
2688 : CONTINUE SEMICOLON
2690 $$ = new Continue ();
2695 : GOTO IDENTIFIER SEMICOLON
2697 $$ = new Goto ((string) $2);
2699 | GOTO CASE constant_expression SEMICOLON
2700 | GOTO DEFAULT SEMICOLON
2704 : RETURN opt_expression SEMICOLON
2706 $$ = new Return ((Expression) $2);
2711 : THROW opt_expression SEMICOLON
2713 $$ = new Throw ((Expression) $2);
2723 : TRY block catch_clauses
2726 ArrayList s = new ArrayList ();
2728 foreach (Catch cc in (ArrayList) $3) {
2729 if (cc.Type == null)
2735 // Now s contains the list of specific catch clauses
2736 // and g contains the general one.
2738 $$ = new Try ((Block) $2, s, g, null);
2740 | TRY block opt_catch_clauses FINALLY block
2743 ArrayList s = new ArrayList ();
2745 foreach (Catch cc in (ArrayList) $3) {
2746 if (cc.Type == null)
2752 $$ = new Try ((Block) $2, s, g, (Block) $5);
2757 : /* empty */ { $$ = null; }
2764 ArrayList l = new ArrayList ();
2769 | catch_clauses catch_clause
2771 ArrayList l = (ArrayList) $1;
2779 : /* empty */ { $$ = null; }
2784 : CATCH opt_catch_args block
2790 DictionaryEntry cc = (DictionaryEntry) $2;
2791 type = (string) cc.Key;
2792 id = (string) cc.Value;
2795 $$ = new Catch (type, id, (Block) $3);
2800 : /* empty */ { $$ = null; }
2805 : OPEN_PARENS type opt_identifier CLOSE_PARENS
2807 $$ = new DictionaryEntry ($2, $3);
2814 $$ = new Checked ((Block) $2);
2821 $$ = new Unchecked ((Block) $2);
2826 : LOCK OPEN_PARENS expression CLOSE_PARENS embedded_statement
2828 $$ = new Lock ((Expression) $3, (Statement) $5);
2833 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS embedded_statement
2837 resource_acquisition
2838 : local_variable_declaration
2846 // A class used to pass around variable declarations and constants
2848 public class VariableDeclaration {
2849 public string identifier;
2850 public object expression_or_array_initializer;
2851 public Location Location;
2853 public VariableDeclaration (string id, object eoai, Location l){
2854 this.identifier = id;
2855 this.expression_or_array_initializer = eoai;
2861 // A class used to hold info about an indexer declarator
2864 public class IndexerDeclaration {
2866 public string interface_type;
2867 public Parameters param_list;
2869 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
2872 this.interface_type = interface_type;
2873 this.param_list = param_list;
2878 // A class used to hold info about an operator declarator
2881 public class OperatorDeclaration {
2882 public Operator.OpType optype;
2883 public string ret_type;
2884 public string arg1type;
2885 public string arg1name;
2886 public string arg2type;
2887 public string arg2name;
2888 public Location location;
2890 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
2891 string arg2type, string arg2name, Location location)
2894 this.ret_type = ret_type;
2895 this.arg1type = arg1type;
2896 this.arg1name = arg1name;
2897 this.arg2type = arg2type;
2898 this.arg2name = arg2name;
2899 this.location = location;
2905 // Given the @class_name name, it creates a fully qualified name
2906 // based on the containing declaration space
2909 MakeName (string class_name)
2911 string ns = current_namespace.Name;
2912 string container_name = current_container.Name;
2914 if (container_name == ""){
2916 return ns + "." + class_name;
2920 return container_name + "." + class_name;
2924 // Used to report back to the user the result of a declaration
2925 // in the current declaration space
2928 CheckDef (DeclSpace.AdditionResult result, string name)
2930 if (result == DeclSpace.AdditionResult.Success)
2933 Location l = lexer.Location;
2936 case DeclSpace.AdditionResult.NameExists:
2937 rc.Report.Error (102, l, "The namespace `" + current_container.Name +
2938 "' already contains a definition for `"+
2943 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
2945 // case DeclSpace.AdditionResult.MethodDuplicated:
2946 // error (111, "Class `"+current_container.Name+
2947 // "' already defines a member called '" +
2948 // name + "' with the same parameter types");
2951 case DeclSpace.AdditionResult.EnclosingClash:
2952 rc.Report.Error (542, l, "Member names cannot be the same as their enclosing type");
2955 case DeclSpace.AdditionResult.NotAConstructor:
2956 rc.Report.Error (1520, l, "Class, struct, or interface method must have a return type");
2962 CheckDef (bool result, string name)
2966 CheckDef (DeclSpace.AdditionResult.NameExists, name);
2970 SimpleLookup (string name)
2973 // we need to check against current_block not being null
2974 // as `expression' is allowed in argument_lists, which
2975 // do not exist inside a block.
2977 if (current_block != null){
2978 if (current_block.IsVariableDefined (name))
2979 return new LocalVariableReference (current_block, name);
2982 if (current_local_parameters != null){
2984 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
2986 return new ParameterReference (current_local_parameters, idx, name);
2993 // Assumes that the name contains a `.' and tries to perform a simple lookup
2994 // and shape the result to be a MemberAccess on a Local/Parameter
2996 object CompositeLookup (string name)
2998 int pos = name.IndexOf (".");
2999 string left = name.Substring (0, pos);
3002 o = SimpleLookup (left);
3004 string right = name.Substring (pos + 1);
3005 return new MemberAccess ((Expression) o, right);
3011 object QualifiedIdentifier (string name, Location l)
3015 if (name.IndexOf ('.') == -1)
3016 o = SimpleLookup (name);
3018 o = CompositeLookup (name);
3021 o = new SimpleName (name, l);
3026 Block declare_local_variables (string type, ArrayList variable_declarators)
3028 Block implicit_block;
3029 ArrayList inits = null;
3032 // We use the `Used' property to check whether statements
3033 // have been added to the current block. If so, we need
3034 // to create another block to contain the new declaration
3035 // otherwise, as an optimization, we use the same block to
3036 // add the declaration.
3038 // FIXME: A further optimization is to check if the statements
3039 // that were added were added as part of the initialization
3040 // below. In which case, no other statements have been executed
3041 // and we might be able to reduce the number of blocks for
3042 // situations like this:
3044 // int j = 1; int k = j + 1;
3046 if (current_block.Used)
3047 implicit_block = new Block (current_block, true);
3049 implicit_block = new Block (current_block, true);
3051 foreach (VariableDeclaration decl in variable_declarators){
3052 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3053 if (decl.expression_or_array_initializer != null){
3055 inits = new ArrayList ();
3059 Location l = lexer.Location;
3060 rc.Report.Error (128, l, "A local variable `" + decl.identifier +
3061 "' is already defined in this scope");
3066 return implicit_block;
3068 foreach (VariableDeclaration decl in inits){
3069 if (decl.expression_or_array_initializer is Expression){
3070 Expression expr = (Expression) decl.expression_or_array_initializer;
3073 assign = new Assign (new LocalVariableReference (
3074 implicit_block, decl.identifier),
3075 expr, lexer.Location);
3076 implicit_block.AddStatement (new StatementExpression (assign));
3078 Console.WriteLine ("Not handling Array initializers yet");
3082 return implicit_block;
3085 void CheckConstant (Expression expr)
3090 void CheckBoolean (Expression expr)
3095 void CheckAttributeTarget (string a)
3099 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3103 Location l = lexer.Location;
3104 rc.Report.Error (658, l, "Invalid attribute target");
3110 void CheckUnaryOperator (Operator.OpType op)
3114 case Operator.OpType.Negate:
3115 case Operator.OpType.BitComplement:
3116 case Operator.OpType.Increment:
3117 case Operator.OpType.Decrement:
3118 case Operator.OpType.True:
3119 case Operator.OpType.False:
3120 case Operator.OpType.Addition:
3121 case Operator.OpType.Subtraction:
3126 Location l = lexer.Location;
3127 rc.Report.Error (1019, l, "Overloadable unary operator expected");
3133 void CheckBinaryOperator (Operator.OpType op)
3137 case Operator.OpType.Addition:
3138 case Operator.OpType.Subtraction:
3139 case Operator.OpType.Multiply:
3140 case Operator.OpType.Division:
3141 case Operator.OpType.Modulus:
3142 case Operator.OpType.BitwiseAnd:
3143 case Operator.OpType.BitwiseOr:
3144 case Operator.OpType.ExclusiveOr:
3145 case Operator.OpType.LeftShift:
3146 case Operator.OpType.RightShift:
3147 case Operator.OpType.Equality:
3148 case Operator.OpType.Inequality:
3149 case Operator.OpType.GreaterThan:
3150 case Operator.OpType.LessThan:
3151 case Operator.OpType.GreaterThanOrEqual:
3152 case Operator.OpType.LessThanOrEqual:
3156 Location l = lexer.Location;
3157 rc.Report.Error (1020, l, "Overloadable binary operator expected");
3163 void output (string s)
3165 Console.WriteLine (s);
3168 void note (string s)
3170 // Used to put annotations
3175 public Tokenizer Lexer {
3181 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
3183 current_namespace = new Namespace (null, "");
3185 this.tree = rc.Tree;
3188 current_container = tree.Types;
3189 current_container.Namespace = current_namespace;
3191 lexer = new Tokenizer (input, name);
3194 public override int parse ()
3196 StringBuilder value = new StringBuilder ();
3200 if (yacc_verbose_flag)
3201 yyparse (lexer, new yydebug.yyDebugSimple ());
3204 } catch (Exception e){
3205 // Console.WriteLine ("Fatal error: " + name);
3206 // Console.WriteLine (lexer.location);
3208 Console.WriteLine (lexer.location + " : Parsing error");
3209 Console.WriteLine (e);
3213 return global_errors;