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
79 // RootContext rc; Now on GenericParser
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 (rc, 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 $$ = DecomposeQI (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, lexer.Location);
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, lexer.Location);
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_specifiers rank_specifier
1762 $$ = (string) $2 + (string) $1;
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
1867 $$ = new Unary (Unary.Operator.Addition, (Expression) $2, lexer.Location);
1869 | MINUS unary_expression
1871 $$ = new Unary (Unary.Operator.Subtraction, (Expression) $2, lexer.Location);
1873 | BANG unary_expression
1875 $$ = new Unary (Unary.Operator.Negate, (Expression) $2, lexer.Location);
1877 | TILDE unary_expression
1879 $$ = new Unary (Unary.Operator.BitComplement, (Expression) $2, lexer.Location);
1881 | STAR unary_expression
1883 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
1885 | BITWISE_AND unary_expression
1887 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
1889 | OP_INC unary_expression
1891 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1893 | OP_DEC unary_expression
1895 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1899 we can not do cast expressions at this level,
1900 as there is an ambiguity. Check "Cast Expressions" 7.6.8
1901 for the recipe to handle this.
1905 pre_increment_expression
1906 : OP_INC unary_expression
1908 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1912 pre_decrement_expression
1913 : OP_DEC unary_expression
1915 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1921 * FIXME: This is actually wrong, it should be `type' but that
1922 * introduces a lot of {shift,reduce}/reduces
1924 * This is really really wrong. We need to track down
1925 * the source of problems with QIs because expressions like:
1926 * foreach (string s in (string []) object) wont be parsed.
1928 : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
1930 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
1932 | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
1934 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
1938 multiplicative_expression
1940 | multiplicative_expression STAR unary_expression
1942 $$ = new Binary (Binary.Operator.Multiply,
1943 (Expression) $1, (Expression) $3, lexer.Location);
1945 | multiplicative_expression DIV unary_expression
1947 $$ = new Binary (Binary.Operator.Division,
1948 (Expression) $1, (Expression) $3, lexer.Location);
1950 | multiplicative_expression PERCENT unary_expression
1952 $$ = new Binary (Binary.Operator.Modulus,
1953 (Expression) $1, (Expression) $3, lexer.Location);
1958 : multiplicative_expression
1959 | additive_expression PLUS multiplicative_expression
1961 $$ = new Binary (Binary.Operator.Addition,
1962 (Expression) $1, (Expression) $3, lexer.Location);
1964 | additive_expression MINUS multiplicative_expression
1966 $$ = new Binary (Binary.Operator.Subtraction,
1967 (Expression) $1, (Expression) $3, lexer.Location);
1972 : additive_expression
1973 | shift_expression OP_SHIFT_LEFT additive_expression
1975 $$ = new Binary (Binary.Operator.LeftShift,
1976 (Expression) $1, (Expression) $3, lexer.Location);
1978 | shift_expression OP_SHIFT_RIGHT additive_expression
1980 $$ = new Binary (Binary.Operator.RightShift,
1981 (Expression) $1, (Expression) $3, lexer.Location);
1985 relational_expression
1987 | relational_expression OP_LT shift_expression
1989 $$ = new Binary (Binary.Operator.LessThan,
1990 (Expression) $1, (Expression) $3, lexer.Location);
1992 | relational_expression OP_GT shift_expression
1994 $$ = new Binary (Binary.Operator.GreaterThan,
1995 (Expression) $1, (Expression) $3, lexer.Location);
1997 | relational_expression OP_LE shift_expression
1999 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2000 (Expression) $1, (Expression) $3, lexer.Location);
2002 | relational_expression OP_GE shift_expression
2004 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2005 (Expression) $1, (Expression) $3, lexer.Location);
2007 | relational_expression IS type
2009 $$ = new Probe (Probe.Operator.Is,
2010 (Expression) $1, (string) $3);
2012 | relational_expression AS type
2014 $$ = new Probe (Probe.Operator.As,
2015 (Expression) $1, (string) $3);
2020 : relational_expression
2021 | equality_expression OP_EQ relational_expression
2023 $$ = new Binary (Binary.Operator.Equality,
2024 (Expression) $1, (Expression) $3, lexer.Location);
2026 | equality_expression OP_NE relational_expression
2028 $$ = new Binary (Binary.Operator.Inequality,
2029 (Expression) $1, (Expression) $3, lexer.Location);
2034 : equality_expression
2035 | and_expression BITWISE_AND equality_expression
2037 $$ = new Binary (Binary.Operator.BitwiseAnd,
2038 (Expression) $1, (Expression) $3, lexer.Location);
2042 exclusive_or_expression
2044 | exclusive_or_expression CARRET and_expression
2046 $$ = new Binary (Binary.Operator.ExclusiveOr,
2047 (Expression) $1, (Expression) $3, lexer.Location);
2051 inclusive_or_expression
2052 : exclusive_or_expression
2053 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2055 $$ = new Binary (Binary.Operator.BitwiseOr,
2056 (Expression) $1, (Expression) $3, lexer.Location);
2060 conditional_and_expression
2061 : inclusive_or_expression
2062 | conditional_and_expression OP_AND inclusive_or_expression
2064 $$ = new Binary (Binary.Operator.LogicalAnd,
2065 (Expression) $1, (Expression) $3, lexer.Location);
2069 conditional_or_expression
2070 : conditional_and_expression
2071 | conditional_or_expression OP_OR conditional_and_expression
2073 $$ = new Binary (Binary.Operator.LogicalOr,
2074 (Expression) $1, (Expression) $3, lexer.Location);
2078 conditional_expression
2079 : conditional_or_expression
2080 | conditional_or_expression INTERR expression COLON expression
2082 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2086 assignment_expression
2087 : unary_expression ASSIGN expression
2089 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2091 | unary_expression OP_MULT_ASSIGN expression
2093 Location l = lexer.Location;
2095 $$ = new Assign ((Expression) $1,
2096 new Binary (Binary.Operator.Multiply,
2098 (Expression) $3, l), l);
2100 | unary_expression OP_DIV_ASSIGN expression
2102 Location l = lexer.Location;
2104 $$ = new Assign ((Expression) $1,
2105 new Binary (Binary.Operator.Division,
2107 (Expression) $3, l), l);
2109 | unary_expression OP_MOD_ASSIGN expression
2111 Location l = lexer.Location;
2113 $$ = new Assign ((Expression) $1,
2114 new Binary (Binary.Operator.Modulus,
2116 (Expression) $3, l), l);
2118 | unary_expression OP_ADD_ASSIGN expression
2120 Location l = lexer.Location;
2122 $$ = new Assign ((Expression) $1,
2123 new Binary (Binary.Operator.Addition,
2125 (Expression) $3, l), l);
2127 | unary_expression OP_SUB_ASSIGN expression
2129 Location l = lexer.Location;
2131 $$ = new Assign ((Expression) $1,
2132 new Binary (Binary.Operator.Subtraction,
2134 (Expression) $3, l), l);
2136 | unary_expression OP_SHIFT_LEFT_ASSIGN expression
2138 Location l = lexer.Location;
2140 $$ = new Assign ((Expression) $1,
2141 new Binary (Binary.Operator.LeftShift,
2143 (Expression) $3, l), l);
2145 | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2147 Location l = lexer.Location;
2149 $$ = new Assign ((Expression) $1,
2150 new Binary (Binary.Operator.RightShift,
2152 (Expression) $3, l), l);
2154 | unary_expression OP_AND_ASSIGN expression
2156 Location l = lexer.Location;
2158 $$ = new Assign ((Expression) $1,
2159 new Binary (Binary.Operator.BitwiseAnd,
2161 (Expression) $3, l), l);
2163 | unary_expression OP_OR_ASSIGN expression
2165 Location l = lexer.Location;
2167 $$ = new Assign ((Expression) $1,
2168 new Binary (Binary.Operator.BitwiseOr,
2170 (Expression) $3, l), l);
2172 | unary_expression OP_XOR_ASSIGN expression
2174 Location l = lexer.Location;
2176 $$ = new Assign ((Expression) $1,
2177 new Binary (Binary.Operator.ExclusiveOr,
2179 (Expression) $3, l), l);
2184 : conditional_expression
2185 | assignment_expression
2193 : expression { CheckBoolean ((Expression) $1); $$ = $1; }
2205 string full_class_name = MakeName ((string) $4);
2207 new_class = new Class (rc, current_container, full_class_name, (int) $2,
2208 (Attributes) $1, lexer.Location);
2209 current_container = new_class;
2210 current_container.Namespace = current_namespace;
2211 tree.RecordClass (full_class_name, new_class);
2217 Class new_class = (Class) current_container;
2220 new_class.Bases = (ArrayList) $6;
2222 current_container = current_container.Parent;
2223 CheckDef (current_container.AddClass (new_class), new_class.Name);
2230 : /* empty */ { $$ = (int) 0; }
2236 | modifiers modifier
2241 if ((m1 & m2) != 0) {
2242 Location l = lexer.Location;
2243 Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2245 $$ = (int) (m1 | m2);
2250 : NEW { $$ = Modifiers.NEW; }
2251 | PUBLIC { $$ = Modifiers.PUBLIC; }
2252 | PROTECTED { $$ = Modifiers.PROTECTED; }
2253 | INTERNAL { $$ = Modifiers.INTERNAL; }
2254 | PRIVATE { $$ = Modifiers.PRIVATE; }
2255 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2256 | SEALED { $$ = Modifiers.SEALED; }
2257 | STATIC { $$ = Modifiers.STATIC; }
2258 | READONLY { $$ = Modifiers.READONLY; }
2259 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2260 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2261 | EXTERN { $$ = Modifiers.EXTERN; }
2265 : /* empty */ { $$ = null; }
2266 | class_base { $$ = $1; }
2270 : COLON type_list { $$ = $2; }
2278 // A block is "contained" on the following places:
2280 // property_declaration as part of the accessor body (get/set)
2281 // operator_declaration
2282 // constructor_declaration
2283 // destructor_declaration
2284 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2289 current_block = new Block (current_block);
2291 opt_statement_list CLOSE_BRACE
2293 while (current_block.Implicit)
2294 current_block = current_block.Parent;
2296 current_block = current_block.Parent;
2307 | statement_list statement
2311 : declaration_statement
2313 if ((Block) $1 != current_block){
2314 current_block.AddStatement ((Statement) $1);
2315 current_block = (Block) $1;
2318 | embedded_statement
2320 current_block.AddStatement ((Statement) $1);
2324 current_block.AddStatement ((Statement) $1);
2331 | expression_statement
2332 | selection_statement
2333 | iteration_statement
2337 | unchecked_statement
2345 $$ = new EmptyStatement ();
2350 : IDENTIFIER COLON statement
2352 string lab = (String) $1;
2355 block = new Block (current_block, lab);
2356 block.AddStatement ((Statement) $3);
2359 if (!current_block.AddLabel (lab, block)){
2360 Location l = lexer.Location;
2361 Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2367 declaration_statement
2368 : local_variable_declaration SEMICOLON // done
2369 | local_constant_declaration SEMICOLON // finishme
2373 * The following is from Rhys' grammar:
2374 * > Types in local variable declarations must be recognized as
2375 * > expressions to prevent reduce/reduce errors in the grammar.
2376 * > The expressions are converted into types during semantic analysis.
2379 : primary_expression opt_rank_specifier
2381 // FIXME: Do something smart here regarding the composition of the type.
2383 // Ok, the above "primary_expression" is there to get rid of
2384 // both reduce/reduce and shift/reduces in the grammar, it should
2385 // really just be "type_name". If you use type_name, a reduce/reduce
2386 // creeps up. If you use qualified_identifier (which is all we need
2387 // really) two shift/reduces appear.
2389 // So, instead we do a super trick: we just allow ($1) to be a
2390 // SimpleName Expression.
2392 if (((Expression) $1) is SimpleName)
2393 $$ = ((SimpleName) $1).Name;
2395 Location l = lexer.Location;
2396 Report.Error (-1, l, "Invalid Type definition");
2397 $$ = "System.Object";
2400 | builtin_types opt_rank_specifier
2402 $$ = (string) $1 + (string) $2;
2406 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2409 // // FIXME: this is a string that represents the type
2410 // // Figure out something to make this work.
2415 local_variable_declaration
2416 : local_variable_type variable_declarators
2418 $$ = declare_local_variables ((string) $1, (ArrayList) $2);
2422 local_constant_declaration
2423 : CONST type constant_declarator
2427 expression_statement
2428 : statement_expression SEMICOLON
2435 // We have to do the wrapping here and not in the case above,
2436 // because statement_expression is used for example in for_statement
2438 statement_expression
2439 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2440 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2441 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2442 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2443 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2444 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2445 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2448 object_creation_expression
2449 : object_or_delegate_creation_expression
2450 { note ("complain if this is a delegate maybe?"); }
2459 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2462 $$ = new If ((Expression) $3, (Statement) $5);
2464 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2465 embedded_statement ELSE embedded_statement
2467 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2472 : SWITCH OPEN_PARENS expression CLOSE_PARENS
2475 $$ = new Switch ((Expression) $3, (ArrayList) $5);
2489 : /* empty */ { $$ = new ArrayList (); }
2496 ArrayList sections = new ArrayList ();
2501 | switch_sections switch_section
2503 ArrayList sections = (ArrayList) $1;
2513 current_block = new Block (current_block);
2517 while (current_block.Implicit)
2518 current_block = current_block.Parent;
2519 $$ = new SwitchSection ((ArrayList) $1, current_block);
2520 current_block = current_block.Parent;
2527 ArrayList labels = new ArrayList ();
2532 | switch_labels switch_label
2534 ArrayList labels = (ArrayList) ($1);
2542 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2); }
2543 | DEFAULT COLON { $$ = new SwitchLabel (null); }
2554 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2556 $$ = new While ((Expression) $3, (Statement) $5);
2561 : DO embedded_statement
2562 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2564 $$ = new Do ((Statement) $2, (Expression) $5);
2570 opt_for_initializer SEMICOLON
2571 opt_for_condition SEMICOLON
2572 opt_for_iterator CLOSE_PARENS
2575 $$ = new For ((Statement) $3, (Expression) $5, (Statement) $7, (Statement) $9);
2580 : /* empty */ { $$ = new EmptyStatement (); }
2585 : local_variable_declaration
2587 if ((Block) $1 != current_block){
2588 current_block.AddStatement ((Statement) $1);
2589 current_block = (Block) $1;
2592 | statement_expression_list
2596 : /* empty */ { $$ = new BoolLiteral (true); }
2597 | boolean_expression
2601 : /* empty */ { $$ = new EmptyStatement (); }
2606 : statement_expression_list
2609 statement_expression_list
2610 : statement_expression
2612 Block b = new Block (null, true);
2614 b.AddStatement ((Statement) $1);
2617 | statement_expression_list COMMA statement_expression
2619 Block b = (Block) $1;
2621 b.AddStatement ((Statement) $3);
2627 : FOREACH OPEN_PARENS type IDENTIFIER IN
2629 $1 = lexer.Location;
2631 expression CLOSE_PARENS
2634 Block foreach_block = new Block (current_block, true);
2635 LocalVariableReference v;
2637 foreach_block.AddVariable ((string) $3, (string) $4, (Location) $1);
2638 v = new LocalVariableReference (foreach_block, (string) $4);
2639 foreach_block.AddStatement (new Foreach ((string) $3, v, (Expression) $7,
2640 (Statement) $9, (Location) $1));
2647 | continue_statement
2661 : CONTINUE SEMICOLON
2663 $$ = new Continue ();
2668 : GOTO IDENTIFIER SEMICOLON
2670 $$ = new Goto ((string) $2);
2672 | GOTO CASE constant_expression SEMICOLON
2673 | GOTO DEFAULT SEMICOLON
2677 : RETURN opt_expression SEMICOLON
2679 $$ = new Return ((Expression) $2, lexer.Location);
2684 : THROW opt_expression SEMICOLON
2686 $$ = new Throw ((Expression) $2);
2696 : TRY block catch_clauses
2699 ArrayList s = new ArrayList ();
2701 foreach (Catch cc in (ArrayList) $3) {
2702 if (cc.Type == null)
2708 // Now s contains the list of specific catch clauses
2709 // and g contains the general one.
2711 $$ = new Try ((Block) $2, s, g, null);
2713 | TRY block opt_catch_clauses FINALLY block
2716 ArrayList s = new ArrayList ();
2718 foreach (Catch cc in (ArrayList) $3) {
2719 if (cc.Type == null)
2725 $$ = new Try ((Block) $2, s, g, (Block) $5);
2730 : /* empty */ { $$ = null; }
2737 ArrayList l = new ArrayList ();
2742 | catch_clauses catch_clause
2744 ArrayList l = (ArrayList) $1;
2752 : /* empty */ { $$ = null; }
2757 : CATCH opt_catch_args
2759 string type = null, id = null;
2762 DictionaryEntry cc = (DictionaryEntry) $2;
2763 type = (string) cc.Key;
2764 id = (string) cc.Value;
2767 ArrayList one = new ArrayList ();
2768 one.Add (new VariableDeclaration (id, null, lexer.Location));
2771 current_block = new Block (current_block);
2772 Block b = declare_local_variables (type, one);
2779 string type = null, id = null;
2782 DictionaryEntry cc = (DictionaryEntry) $2;
2783 type = (string) cc.Key;
2784 id = (string) cc.Value;
2787 while (current_block != (Block) $1)
2788 current_block = current_block.Parent;
2793 $$ = new Catch (type, id , (Block) $4);
2798 : /* empty */ { $$ = null; }
2803 : OPEN_PARENS type opt_identifier CLOSE_PARENS
2805 $$ = new DictionaryEntry ($2, $3);
2812 $$ = new Checked ((Block) $2);
2819 $$ = new Unchecked ((Block) $2);
2824 : LOCK OPEN_PARENS expression CLOSE_PARENS embedded_statement
2826 $$ = new Lock ((Expression) $3, (Statement) $5);
2831 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS embedded_statement
2835 resource_acquisition
2836 : local_variable_declaration
2838 // FIXME: Add the current block chaining for local_variable_decl
2847 // A class used to pass around variable declarations and constants
2849 public class VariableDeclaration {
2850 public string identifier;
2851 public object expression_or_array_initializer;
2852 public Location Location;
2854 public VariableDeclaration (string id, object eoai, Location l){
2855 this.identifier = id;
2856 this.expression_or_array_initializer = eoai;
2862 // A class used to hold info about an indexer declarator
2865 public class IndexerDeclaration {
2867 public string interface_type;
2868 public Parameters param_list;
2870 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
2873 this.interface_type = interface_type;
2874 this.param_list = param_list;
2879 // A class used to hold info about an operator declarator
2882 public class OperatorDeclaration {
2883 public Operator.OpType optype;
2884 public string ret_type;
2885 public string arg1type;
2886 public string arg1name;
2887 public string arg2type;
2888 public string arg2name;
2889 public Location location;
2891 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
2892 string arg2type, string arg2name, Location location)
2895 this.ret_type = ret_type;
2896 this.arg1type = arg1type;
2897 this.arg1name = arg1name;
2898 this.arg2type = arg2type;
2899 this.arg2name = arg2name;
2900 this.location = location;
2906 // Given the @class_name name, it creates a fully qualified name
2907 // based on the containing declaration space
2910 MakeName (string class_name)
2912 string ns = current_namespace.Name;
2913 string container_name = current_container.Name;
2915 if (container_name == ""){
2917 return ns + "." + class_name;
2921 return container_name + "." + class_name;
2925 // Used to report back to the user the result of a declaration
2926 // in the current declaration space
2929 CheckDef (DeclSpace.AdditionResult result, string name)
2931 if (result == DeclSpace.AdditionResult.Success)
2934 Location l = lexer.Location;
2937 case DeclSpace.AdditionResult.NameExists:
2938 Report.Error (102, l, "The namespace `" + current_container.Name +
2939 "' already contains a definition for `"+
2944 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
2946 // case DeclSpace.AdditionResult.MethodDuplicated:
2947 // error (111, "Class `"+current_container.Name+
2948 // "' already defines a member called '" +
2949 // name + "' with the same parameter types");
2952 case DeclSpace.AdditionResult.EnclosingClash:
2953 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
2956 case DeclSpace.AdditionResult.NotAConstructor:
2957 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
2963 CheckDef (bool result, string name)
2967 CheckDef (DeclSpace.AdditionResult.NameExists, name);
2971 SimpleLookup (string name)
2974 // we need to check against current_block not being null
2975 // as `expression' is allowed in argument_lists, which
2976 // do not exist inside a block.
2978 if (current_block != null){
2979 if (current_block.IsVariableDefined (name))
2980 return new LocalVariableReference (current_block, name);
2983 if (current_local_parameters != null){
2985 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
2987 return new ParameterReference (current_local_parameters, idx, name);
2993 Expression DecomposeQI (string name, Location l)
2997 if (name.IndexOf ('.') == -1){
2998 o = SimpleLookup (name);
3000 return new SimpleName (name, l);
3003 int pos = name.LastIndexOf (".");
3004 string left = name.Substring (0, pos);
3005 string right = name.Substring (pos + 1);
3007 o = DecomposeQI (left, l);
3008 return new MemberAccess (o, right, l);
3012 Block declare_local_variables (string type, ArrayList variable_declarators)
3014 Block implicit_block;
3015 ArrayList inits = null;
3018 // We use the `Used' property to check whether statements
3019 // have been added to the current block. If so, we need
3020 // to create another block to contain the new declaration
3021 // otherwise, as an optimization, we use the same block to
3022 // add the declaration.
3024 // FIXME: A further optimization is to check if the statements
3025 // that were added were added as part of the initialization
3026 // below. In which case, no other statements have been executed
3027 // and we might be able to reduce the number of blocks for
3028 // situations like this:
3030 // int j = 1; int k = j + 1;
3032 if (current_block.Used)
3033 implicit_block = new Block (current_block, true);
3035 implicit_block = current_block;
3037 foreach (VariableDeclaration decl in variable_declarators){
3038 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3039 if (decl.expression_or_array_initializer != null){
3041 inits = new ArrayList ();
3045 Location l = lexer.Location;
3046 Report.Error (128, l, "A local variable `" + decl.identifier +
3047 "' is already defined in this scope");
3052 return implicit_block;
3054 foreach (VariableDeclaration decl in inits){
3055 if (decl.expression_or_array_initializer is Expression){
3056 Expression expr = (Expression) decl.expression_or_array_initializer;
3059 assign = new Assign (new LocalVariableReference (
3060 implicit_block, decl.identifier),
3061 expr, lexer.Location);
3062 implicit_block.AddStatement (new StatementExpression (assign));
3064 Console.WriteLine ("Not handling Array initializers yet");
3068 return implicit_block;
3071 void CheckConstant (Expression expr)
3076 void CheckBoolean (Expression expr)
3081 void CheckAttributeTarget (string a)
3085 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3089 Location l = lexer.Location;
3090 Report.Error (658, l, "Invalid attribute target");
3096 void CheckUnaryOperator (Operator.OpType op)
3100 case Operator.OpType.Negate:
3101 case Operator.OpType.BitComplement:
3102 case Operator.OpType.Increment:
3103 case Operator.OpType.Decrement:
3104 case Operator.OpType.True:
3105 case Operator.OpType.False:
3106 case Operator.OpType.Addition:
3107 case Operator.OpType.Subtraction:
3112 Location l = lexer.Location;
3113 Report.Error (1019, l, "Overloadable unary operator expected");
3119 void CheckBinaryOperator (Operator.OpType op)
3123 case Operator.OpType.Addition:
3124 case Operator.OpType.Subtraction:
3125 case Operator.OpType.Multiply:
3126 case Operator.OpType.Division:
3127 case Operator.OpType.Modulus:
3128 case Operator.OpType.BitwiseAnd:
3129 case Operator.OpType.BitwiseOr:
3130 case Operator.OpType.ExclusiveOr:
3131 case Operator.OpType.LeftShift:
3132 case Operator.OpType.RightShift:
3133 case Operator.OpType.Equality:
3134 case Operator.OpType.Inequality:
3135 case Operator.OpType.GreaterThan:
3136 case Operator.OpType.LessThan:
3137 case Operator.OpType.GreaterThanOrEqual:
3138 case Operator.OpType.LessThanOrEqual:
3142 Location l = lexer.Location;
3143 Report.Error (1020, l, "Overloadable binary operator expected");
3149 void output (string s)
3151 Console.WriteLine (s);
3154 void note (string s)
3156 // Used to put annotations
3161 public Tokenizer Lexer {
3167 public override int parse ()
3169 current_namespace = new Namespace (null, "");
3170 this.tree = rc.Tree;
3171 current_container = tree.Types;
3172 current_container.Namespace = current_namespace;
3174 lexer = new Tokenizer (input, name);
3175 StringBuilder value = new StringBuilder ();
3179 if (yacc_verbose_flag)
3180 yyparse (lexer, new yydebug.yyDebugSimple ());
3183 } catch (Exception e){
3184 // Console.WriteLine ("Fatal error: " + name);
3185 // Console.WriteLine (lexer.location);
3187 Console.WriteLine (lexer.location + " : Parsing error ");
3188 Console.WriteLine (e);
3192 return global_errors;