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" and "get"accesor
65 // methods (properties and indexers).
67 string implicit_value_parameter_type;
68 Parameters indexer_parameters;
71 // Used to determine if we are parsing the get/set pair
72 // of an indexer or a property
77 // Used to record all types defined
84 // Used to temporarily hold the lexer's location for storage later
86 Location temporary_loc;
91 %token NONE /* This token is never returned by our lexer */
92 %token ERROR // This is used not by the parser, but by the tokenizer.
96 *These are the C# keywords
176 /* C# keywords which are not really keywords */
180 /* C# single character operators/punctuation. */
181 %token OPEN_BRACE "{"
182 %token CLOSE_BRACE "}"
183 %token OPEN_BRACKET "["
184 %token CLOSE_BRACKET "]"
185 %token OPEN_PARENS "("
186 %token CLOSE_PARENS ")"
199 %token BITWISE_AND "&"
200 %token BITWISE_OR "|"
207 /* C# multi-character operators. */
210 %token OP_SHIFT_LEFT "<<"
211 %token OP_SHIFT_RIGHT ">>"
218 %token OP_MULT_ASSIGN "*="
219 %token OP_DIV_ASSIGN "/="
220 %token OP_MOD_ASSIGN "%="
221 %token OP_ADD_ASSIGN "+="
222 %token OP_SUB_ASSIGN "-="
223 %token OP_SHIFT_LEFT_ASSIGN "<<="
224 %token OP_SHIFT_RIGHT_ASSIGN ">>="
225 %token OP_AND_ASSIGN "&="
226 %token OP_XOR_ASSIGN "^="
227 %token OP_OR_ASSIGN "|="
231 %token LITERAL_INTEGER "int literal"
232 %token LITERAL_FLOAT "float literal"
233 %token LITERAL_DOUBLE "double literal"
234 %token LITERAL_DECIMAL "decimal literal"
235 %token LITERAL_CHARACTER "character literal"
236 %token LITERAL_STRING "string literal"
240 /* Add precedence rules to solve dangling else s/r conflict */
249 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
251 %left STAR DIV PERCENT
252 %right BANG CARRET UMINUS
253 %nonassoc OP_INC OP_DEC
255 %left OPEN_BRACKET OPEN_BRACE
259 %start compilation_unit
260 /*%start namespace_declaration */
264 : opt_using_directives opt_attributes opt_namespace_member_declarations EOF
266 // Check that using comes only before namespace elements
272 | using_directives using_directive
276 : using_alias_directive
277 | using_namespace_directive
280 using_alias_directive
281 : USING IDENTIFIER ASSIGN
282 namespace_or_type_name SEMICOLON
284 // FIXME : Need to implement actual action.
288 using_namespace_directive
289 : USING namespace_name SEMICOLON
291 current_namespace.Using ((string) $2);
295 // namespace_declarations
296 // : namespace_declaration
297 // | namespace_declarations namespace_declaration
299 namespace_declaration
300 : NAMESPACE qualified_identifier
302 current_namespace = tree.RecordNamespace (current_namespace, (string) $2);
304 namespace_body opt_semicolon
306 current_namespace = current_namespace.Parent;
322 | qualified_identifier DOT IDENTIFIER {
323 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
328 : namespace_or_type_name
334 opt_namespace_member_declarations
345 opt_namespace_member_declarations
347 | namespace_member_declarations
350 namespace_member_declarations
351 : namespace_member_declaration
352 | namespace_member_declarations namespace_member_declaration
355 namespace_member_declaration
362 Class c = (Class) $1;
363 mod_flags = c.ModFlags;
365 } else if ($1 is Struct){
366 Struct s = (Struct) $1;
367 mod_flags = s.ModFlags;
373 // We remove this error until we can
374 //if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
375 // error (1527, "Namespace elements cant be explicitly " +
376 // "declared private or protected in `" + name + "'");
379 | namespace_declaration
385 | interface_declaration
387 | delegate_declaration
394 : /* empty */ { $$ = null; }
395 | attribute_section opt_attributes
400 attrs = (Attributes) $2;
401 attrs.AddAttribute ((Attribute) $1);
403 attrs = new Attributes ((Attribute) $1);
410 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
412 string target = null;
415 target = (string) $2;
417 $$ = new Attribute (target, (ArrayList) $3);
419 | OPEN_BRACKET attribute_list CLOSE_BRACKET
421 $$ = new Attribute (null, (ArrayList) $2);
425 attribute_target_specifier
426 : attribute_target COLON
435 CheckAttributeTarget ((string) $1);
438 | EVENT { $$ = "event"; }
439 | RETURN { $$ = "return"; }
445 ArrayList attrs = new ArrayList ();
451 | attribute_list COMMA attribute
453 ArrayList attrs = (ArrayList) $1;
461 : attribute_name opt_attribute_arguments
463 $$ = new DictionaryEntry ($1, $2);
468 : type_name { /* reserved attribute name or identifier: 17.4 */ }
471 opt_attribute_arguments
472 : /* empty */ { $$ = null; }
473 | OPEN_PARENS attribute_arguments CLOSE_PARENS
482 ArrayList args = new ArrayList ();
487 | attribute_arguments COMMA expression
489 ArrayList args = (ArrayList) $1;
498 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
501 opt_class_member_declarations
503 | class_member_declarations
506 class_member_declarations
507 : class_member_declaration
508 | class_member_declarations
509 class_member_declaration
512 class_member_declaration
513 : constant_declaration // done
514 | field_declaration // done
515 | method_declaration // done
516 | property_declaration // done
517 | event_declaration // done
518 | indexer_declaration // done
519 | operator_declaration // done
520 | constructor_declaration // done
521 | destructor_declaration // done
531 string full_struct_name = MakeName ((string) $4);
533 new_struct = new Struct (rc, current_container, full_struct_name, (int) $2,
534 (Attributes) $1, lexer.Location);
535 current_container = new_struct;
536 current_container.Namespace = current_namespace;
537 tree.RecordStruct (full_struct_name, new_struct);
539 opt_struct_interfaces
543 Struct new_struct = (Struct) current_container;
545 current_container = current_container.Parent;
546 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
551 opt_struct_interfaces
558 | struct_interfaces struct_interface
566 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
569 opt_struct_member_declarations
571 | struct_member_declarations
574 struct_member_declarations
575 : struct_member_declaration
576 | struct_member_declarations struct_member_declaration
579 struct_member_declaration
580 : constant_declaration
583 | property_declaration
585 | indexer_declaration
586 | operator_declaration
587 | constructor_declaration
599 foreach (DictionaryEntry constant in (ArrayList) $5){
600 Constant c = new Constant (
601 (string) $4, (string) constant.Key,
602 (Expression) constant.Value, (int) $2, (Attributes) $1);
604 CheckDef (current_container.AddConstant (c), c.Name);
610 : constant_declarator
612 ArrayList constants = new ArrayList ();
616 | constant_declarators COMMA constant_declarator
618 ArrayList constants = (ArrayList) $1;
625 : IDENTIFIER ASSIGN constant_expression
627 $$ = new DictionaryEntry ($1, $3);
638 string type = (string) $3;
641 foreach (VariableDeclaration var in (ArrayList) $4){
642 Field field = new Field (type, mod, var.identifier,
643 var.expression_or_array_initializer, (Attributes) $1);
645 CheckDef (current_container.AddField (field), field.Name);
652 : variable_declarator
654 ArrayList decl = new ArrayList ();
658 | variable_declarators COMMA variable_declarator
660 ArrayList decls = (ArrayList) $1;
667 : IDENTIFIER ASSIGN variable_initializer
669 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
673 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
692 Method method = (Method) $1;
693 Block b = (Block) $2;
696 if ((method.ModFlags & (Modifiers.EXTERN | Modifiers.ABSTRACT)) == 0){
698 501, lexer.Location, "`" +
699 current_container.Name + "." + method.Name + "'" +
700 "must declare a body because it is not marked abstract or extern");
704 method.Block = (Block) $2;
705 CheckDef (current_container.AddMethod (method), method.Name);
707 current_local_parameters = null;
716 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
718 Method method = new Method ((string) $3, (int) $2, (string) $4,
719 (Parameters) $6, (Attributes) $1, lexer.Location);
721 current_local_parameters = (Parameters) $6;
729 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
731 Method method = new Method ("System.Void", (int) $2, (string) $4,
732 (Parameters) $6, (Attributes) $1, lexer.Location);
734 current_local_parameters = (Parameters) $6;
741 | SEMICOLON { $$ = null; }
744 opt_formal_parameter_list
745 : /* empty */ { $$ = new Parameters (null, null); }
746 | formal_parameter_list
749 formal_parameter_list
752 ArrayList pars_list = (ArrayList) $1;
754 Parameter [] pars = new Parameter [pars_list.Count];
755 pars_list.CopyTo (pars);
757 $$ = new Parameters (pars, null);
759 | fixed_parameters COMMA parameter_array
761 ArrayList pars_list = (ArrayList) $1;
763 Parameter [] pars = new Parameter [pars_list.Count];
764 pars_list.CopyTo (pars);
766 $$ = new Parameters (pars, (Parameter) $3);
770 $$ = new Parameters (null, (Parameter) $1);
777 ArrayList pars = new ArrayList ();
782 | fixed_parameters COMMA fixed_parameter
784 ArrayList pars = (ArrayList) $1;
793 opt_parameter_modifier
797 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
801 opt_parameter_modifier
802 : /* empty */ { $$ = Parameter.Modifier.NONE; }
807 : REF { $$ = Parameter.Modifier.REF; }
808 | OUT { $$ = Parameter.Modifier.OUT; }
812 : opt_attributes PARAMS type IDENTIFIER
814 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
815 note ("type must be a single-dimension array type");
820 : IDENTIFIER { $$ = $1.ToString (); }
821 | interface_type DOT IDENTIFIER { $$ = $1.ToString () + "." + $3.ToString (); }
830 implicit_value_parameter_type = (string) $3;
832 lexer.properties = true;
833 temporary_loc = lexer.Location;
835 accessor_declarations
837 lexer.properties = false;
842 DictionaryEntry pair = (DictionaryEntry) $7;
843 Block get_block = null;
844 Block set_block = null;
846 if (pair.Key != null)
847 get_block = (Block) pair.Key;
848 if (pair.Value != null)
849 set_block = (Block) pair.Value;
851 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block,
852 (Attributes) $1, temporary_loc);
854 CheckDef (current_container.AddProperty (prop), prop.Name);
855 implicit_value_parameter_type = null;
859 accessor_declarations
860 : get_accessor_declaration opt_set_accessor_declaration
862 $$ = new DictionaryEntry ($1, $2);
864 | set_accessor_declaration opt_get_accessor_declaration
866 $$ = new DictionaryEntry ($2, $1);
870 opt_get_accessor_declaration
871 : /* empty */ { $$ = null; }
872 | get_accessor_declaration
875 opt_set_accessor_declaration
876 : /* empty */ { $$ = null; }
877 | set_accessor_declaration
880 get_accessor_declaration
883 // If this is not the case, then current_local_parameters has already
884 // been set in indexer_declaration
885 if (parsing_indexer == false)
886 current_local_parameters = null;
888 current_local_parameters = indexer_parameters;
895 current_local_parameters = null;
899 set_accessor_declaration
903 Parameter implicit_value_parameter = new Parameter (
904 implicit_value_parameter_type, "value",
905 Parameter.Modifier.NONE, null);
907 if (parsing_indexer == false) {
908 args = new Parameter [1];
909 args [0] = implicit_value_parameter;
911 Parameter [] fp = indexer_parameters.FixedParameters;
912 int count = fp.Length;
914 args = new Parameter [count + 1];
917 args [count] = implicit_value_parameter;
919 current_local_parameters = new Parameters (args, null);
924 current_local_parameters = null;
930 | SEMICOLON { $$ = new Block (null); }
933 interface_declaration
938 Interface new_interface;
939 string full_interface_name = MakeName ((string) $4);
941 new_interface = new Interface (rc, current_container, full_interface_name, (int) $2,
942 (Attributes) $1, lexer.Location);
943 if (current_interface != null) {
944 Location l = lexer.Location;
945 Report.Error (-2, l, "Internal compiler error: interface inside interface");
947 current_interface = new_interface;
948 tree.RecordInterface (full_interface_name, new_interface);
953 Interface new_interface = (Interface) current_interface;
956 new_interface.Bases = (ArrayList) $6;
958 current_interface = null;
959 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
964 : /* empty */ { $$ = null; }
969 : COLON interface_type_list { $$ = $2; }
975 ArrayList interfaces = new ArrayList ();
980 | interface_type_list COMMA interface_type
982 ArrayList interfaces = (ArrayList) $1;
990 opt_interface_member_declarations
994 opt_interface_member_declarations
996 | interface_member_declarations
999 interface_member_declarations
1000 : interface_member_declaration
1001 | interface_member_declarations interface_member_declaration
1004 interface_member_declaration
1005 : interface_method_declaration
1007 InterfaceMethod m = (InterfaceMethod) $1;
1009 CheckDef (current_interface.AddMethod (m), m.Name);
1011 | interface_property_declaration
1013 InterfaceProperty p = (InterfaceProperty) $1;
1015 CheckDef (current_interface.AddProperty (p), p.Name);
1017 | interface_event_declaration
1019 InterfaceEvent e = (InterfaceEvent) $1;
1021 CheckDef (current_interface.AddEvent (e), e.Name);
1023 | interface_indexer_declaration
1025 InterfaceIndexer i = (InterfaceIndexer) $1;
1027 CheckDef (current_interface.AddIndexer (i), "indexer");
1032 : /* empty */ { $$ = false; }
1033 | NEW { $$ = true; }
1036 interface_method_declaration
1037 : opt_attributes opt_new type IDENTIFIER
1038 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1041 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1043 | opt_attributes opt_new VOID IDENTIFIER
1044 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1047 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1051 interface_property_declaration
1056 { lexer.properties = true; }
1058 { lexer.properties = false; }
1063 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
1064 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1);
1069 : opt_attributes GET SEMICOLON { $$ = 1; }
1070 | opt_attributes SET SEMICOLON { $$ = 2; }
1071 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1073 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1077 interface_event_declaration
1078 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1080 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1084 interface_indexer_declaration
1085 : opt_attributes opt_new type THIS
1086 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1088 { lexer.properties = true; }
1090 { lexer.properties = false; }
1093 int a_flags = (int) $10;
1095 bool do_get = (a_flags & 1) == 1;
1096 bool do_set = (a_flags & 2) == 2;
1098 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set, (bool) $2, (Attributes) $1);
1102 operator_declaration
1103 : opt_attributes opt_modifiers operator_declarator block
1105 OperatorDeclaration decl = (OperatorDeclaration) $3;
1107 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1108 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1110 // Note again, checking is done in semantic analysis
1111 current_container.AddOperator (op);
1116 : type OPERATOR overloadable_operator
1117 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1119 Operator.OpType op = (Operator.OpType) $3;
1120 CheckUnaryOperator (op);
1122 if (op == Operator.OpType.Addition)
1123 op = Operator.OpType.UnaryPlus;
1125 if (op == Operator.OpType.Subtraction)
1126 op = Operator.OpType.UnaryNegation;
1128 $$ = new OperatorDeclaration (op, (string) $1, (string) $5, (string) $6,
1129 null, null, lexer.Location);
1131 | type OPERATOR overloadable_operator
1133 type IDENTIFIER COMMA
1137 CheckBinaryOperator ((Operator.OpType) $3);
1139 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1140 (string) $8, (string) $9, lexer.Location);
1142 | conversion_operator_declarator
1145 overloadable_operator
1147 : BANG { $$ = Operator.OpType.LogicalNot; }
1148 | TILDE { $$ = Operator.OpType.OnesComplement; }
1149 | OP_INC { $$ = Operator.OpType.Increment; }
1150 | OP_DEC { $$ = Operator.OpType.Decrement; }
1151 | TRUE { $$ = Operator.OpType.True; }
1152 | FALSE { $$ = Operator.OpType.False; }
1153 // Unary and binary:
1154 | PLUS { $$ = Operator.OpType.Addition; }
1155 | MINUS { $$ = Operator.OpType.Subtraction; }
1157 | STAR { $$ = Operator.OpType.Multiply; }
1158 | DIV { $$ = Operator.OpType.Division; }
1159 | PERCENT { $$ = Operator.OpType.Modulus; }
1160 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1161 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1162 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1163 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1164 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1165 | OP_EQ { $$ = Operator.OpType.Equality; }
1166 | OP_NE { $$ = Operator.OpType.Inequality; }
1167 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1168 | OP_LT { $$ = Operator.OpType.LessThan; }
1169 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1170 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1173 conversion_operator_declarator
1174 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1176 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1177 null, null, lexer.Location);
1179 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1181 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1182 null, null, lexer.Location);
1186 constructor_declaration
1189 constructor_declarator
1192 Constructor c = (Constructor) $3;
1193 c.Block = (Block) $4;
1194 c.ModFlags = (int) $2;
1196 if ((c.ModFlags & Modifiers.STATIC) != 0){
1197 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1198 Location l = lexer.Location;
1199 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1202 if (c.Initializer != null){
1203 Location l = lexer.Location;
1204 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1207 if (!c.Parameters.Empty){
1208 Location l = lexer.Location;
1209 Report.Error (103, l, "Static constructors should not have parameters");
1213 CheckDef (current_container.AddConstructor (c), c.Name);
1215 current_local_parameters = null;
1219 constructor_declarator
1221 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1222 opt_constructor_initializer
1224 Location l = lexer.Location;
1226 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5, l);
1228 current_local_parameters = (Parameters) $3;
1232 opt_constructor_initializer
1233 : /* empty */ { $$ = null; }
1234 | constructor_initializer
1237 constructor_initializer
1238 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1240 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1242 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1244 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1248 destructor_declaration
1249 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1252 // FIXME: The destructor needs to generate special code:
1258 // void Finalize () {
1259 // try { code } finally { base.Finalize (); }
1261 Method d = new Method ("System.Void", 0, "Finalize",
1262 new Parameters (null, null), (Attributes) $1, lexer.Location);
1264 d.Block = (Block) $6;
1265 CheckDef (current_container.AddMethod (d), d.Name);
1272 EVENT type variable_declarators SEMICOLON
1274 foreach (VariableDeclaration var in (ArrayList) $5) {
1276 // FIXME : Is this right ?
1277 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1278 (int) $2, null, null, (Attributes) $1, lexer.Location);
1280 CheckDef (current_container.AddEvent (e), e.Name);
1286 EVENT type member_name
1287 OPEN_BRACE event_accessor_declarations CLOSE_BRACE
1289 DictionaryEntry pair = (DictionaryEntry) $7;
1290 Block add_block = null;
1291 Block rem_block = null;
1293 if (pair.Key != null)
1294 add_block = (Block) pair.Key;
1295 if (pair.Value != null)
1296 rem_block = (Block) pair.Value;
1298 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
1299 (Attributes) $1, lexer.Location);
1301 CheckDef (current_container.AddEvent (e), e.Name);
1305 event_accessor_declarations
1306 : add_accessor_declaration remove_accessor_declaration
1308 $$ = new DictionaryEntry ($1, $2);
1310 | remove_accessor_declaration add_accessor_declaration
1312 $$ = new DictionaryEntry ($2, $1);
1316 add_accessor_declaration
1317 : opt_attributes ADD block
1323 remove_accessor_declaration
1324 : opt_attributes REMOVE block
1331 : opt_attributes opt_modifiers indexer_declarator
1334 IndexerDeclaration decl = (IndexerDeclaration) $3;
1336 implicit_value_parameter_type = decl.type;
1338 lexer.properties = true;
1339 temporary_loc = lexer.Location;
1340 parsing_indexer = true;
1342 indexer_parameters = decl.param_list;
1344 accessor_declarations
1346 lexer.properties = false;
1347 parsing_indexer = false;
1351 // The signature is computed from the signature of the indexer. Look
1352 // at section 3.6 on the spec
1355 IndexerDeclaration decl = (IndexerDeclaration) $3;
1356 DictionaryEntry pair = (DictionaryEntry) $6;
1357 Block get_block = null;
1358 Block set_block = null;
1360 if (pair.Key != null)
1361 get_block = (Block) pair.Key;
1362 if (pair.Value != null)
1363 set_block = (Block) pair.Value;
1365 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1366 get_block, set_block, (Attributes) $1, temporary_loc);
1368 // Note that there is no equivalent of CheckDef for this case
1369 // We shall handle this in semantic analysis
1371 current_container.AddIndexer (indexer);
1373 current_local_parameters = null;
1374 implicit_value_parameter_type = null;
1375 indexer_parameters = null;
1380 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1382 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
1384 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1386 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
1398 string name = (string) $4;
1399 Enum e = new Enum ((string) $5, (int) $2, name, (Attributes) $1, lexer.Location);
1401 foreach (VariableDeclaration ev in (ArrayList) $6){
1402 CheckDef (e.AddEnumMember (ev.identifier,
1403 (Expression) ev.expression_or_array_initializer),
1407 CheckDef (current_container.AddEnum (e), name);
1412 : /* empty */ { $$ = "System.Int32"; }
1413 | COLON integral_type { $$ = $2; }
1417 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1423 opt_enum_member_declarations
1424 : /* empty */ { $$ = new ArrayList (); }
1425 | enum_member_declarations opt_comma { $$ = $1; }
1428 enum_member_declarations
1429 : enum_member_declaration
1431 ArrayList l = new ArrayList ();
1436 | enum_member_declarations COMMA enum_member_declaration
1438 ArrayList l = (ArrayList) $1;
1446 enum_member_declaration
1447 : opt_attributes IDENTIFIER
1449 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1451 | opt_attributes IDENTIFIER ASSIGN expression
1453 $$ = new VariableDeclaration ((string) $2, $4, lexer.Location);
1457 delegate_declaration
1461 IDENTIFIER OPEN_PARENS
1462 formal_parameter_list
1466 Delegate del = new Delegate (rc, (string) $4, (int) $2,
1467 MakeName ((string) $5), (Parameters) $7,
1468 (Attributes) $1, lexer.Location);
1470 CheckDef (current_container.AddDelegate (del), del.Name);
1475 IDENTIFIER OPEN_PARENS
1476 formal_parameter_list
1480 Delegate del = new Delegate (rc, "System.Void", (int) $2, (string) $5, (Parameters) $7,
1481 (Attributes) $1, lexer.Location);
1483 CheckDef (current_container.AddDelegate (del), del.Name);
1488 : namespace_or_type_name
1491 namespace_or_type_name
1492 : qualified_identifier
1496 * Before you think of adding a return_type, notice that we have been
1497 * using two rules in the places where it matters (one rule using type
1498 * and another identical one that uses VOID as the return type). This
1499 * gets rid of a shift/reduce couple
1502 : type_name { /* class_type */
1504 This does interfaces, delegates, struct_types, class_types,
1505 parent classes, and more! 4.2
1516 ArrayList types = new ArrayList ();
1521 | type_list COMMA type
1523 ArrayList types = (ArrayList) $1;
1531 * replaces all the productions for isolating the various
1532 * simple types, but we need this to reuse it easily in local_variable_type
1535 : OBJECT { $$ = "System.Object"; }
1536 | STRING { $$ = "System.String"; }
1537 | BOOL { $$ = "System.Boolean"; }
1538 | DECIMAL { $$ = "System.Decimal"; }
1539 | FLOAT { $$ = "System.Single"; }
1540 | DOUBLE { $$ = "System.Double"; }
1545 : SBYTE { $$ = "System.SByte"; }
1546 | BYTE { $$ = "System.Byte"; }
1547 | SHORT { $$ = "System.Int16"; }
1548 | USHORT { $$ = "System.UInt16"; }
1549 | INT { $$ = "System.Int32"; }
1550 | UINT { $$ = "System.UInt32"; }
1551 | LONG { $$ = "System.Int64"; }
1552 | ULONG { $$ = "System.UInt64"; }
1553 | CHAR { $$ = "System.Char"; }
1561 : type rank_specifiers
1563 $$ = (string) $1 + (string) $2;
1568 // Expressions, section 7.5
1576 | qualified_identifier
1578 string name = (string) $1;
1581 $$ = DecomposeQI (name, lexer.Location);
1583 | parenthesized_expression
1585 | invocation_expression
1589 | post_increment_expression
1590 | post_decrement_expression
1594 | checked_expression
1595 | unchecked_expression
1602 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1603 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1604 | NULL { $$ = new NullLiteral (); }
1608 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1609 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1610 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1615 object v = lexer.Value;
1618 // FIXME: Possible optimization would be to
1619 // compute the *Literal objects directly in the scanner
1622 $$ = new IntLiteral ((Int32) v);
1624 $$ = new UIntLiteral ((UInt32) v);
1626 $$ = new LongLiteral ((Int64) v);
1627 else if (v is ulong)
1628 $$ = new ULongLiteral ((UInt64) v);
1630 Console.WriteLine ("OOPS. Unexpected result from scanner");
1635 : TRUE { $$ = new BoolLiteral (true); }
1636 | FALSE { $$ = new BoolLiteral (false); }
1639 parenthesized_expression
1640 : OPEN_PARENS expression CLOSE_PARENS
1645 : primary_expression DOT IDENTIFIER
1647 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1649 | predefined_type DOT IDENTIFIER
1651 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1659 invocation_expression
1660 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1663 // if $1 is MethodGroup
1664 // $$ = new Call ($1, $3);
1666 // $$ = new DelegateCall ($1, $3);
1668 Location l = lexer.Location;
1669 Report.Error (1, l, "THIS IS CRAZY");
1671 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1677 : /* empty */ { $$ = null; }
1684 ArrayList list = new ArrayList ();
1688 | argument_list COMMA argument
1690 ArrayList list = (ArrayList) $1;
1699 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1701 | REF variable_reference
1703 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1705 | OUT variable_reference
1707 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1712 : expression { note ("section 5.4"); $$ = $1; }
1716 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1718 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1725 ArrayList list = new ArrayList ();
1729 | expression_list COMMA expression
1731 ArrayList list = (ArrayList) $1;
1740 $$ = new This (lexer.Location);
1745 : BASE DOT IDENTIFIER
1747 $$ = new BaseAccess (BaseAccess.BaseAccessType.Member, (string) $3, null);
1749 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1751 $$ = new BaseAccess (BaseAccess.BaseAccessType.Indexer, null, (ArrayList) $3);
1755 post_increment_expression
1756 : primary_expression OP_INC
1758 $$ = new Unary (Unary.Operator.PostIncrement, (Expression) $1, lexer.Location);
1762 post_decrement_expression
1763 : primary_expression OP_DEC
1765 $$ = new Unary (Unary.Operator.PostDecrement, (Expression) $1, lexer.Location);
1770 : object_or_delegate_creation_expression
1771 | array_creation_expression
1774 object_or_delegate_creation_expression
1775 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1777 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1781 array_creation_expression
1782 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
1784 opt_array_initializer
1786 $$ = new ArrayCreation ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7,
1789 | NEW type rank_specifiers array_initializer
1791 $$ = new ArrayCreation ((string) $2, (string) $3, (ArrayList) $4, lexer.Location);
1811 | rank_specifiers rank_specifier
1813 $$ = (string) $2 + (string) $1;
1818 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1820 $$ = "[" + (string) $2 + "]";
1840 | dim_separators COMMA
1842 $$ = (string) $1 + ",";
1846 opt_array_initializer
1858 : OPEN_BRACE CLOSE_BRACE
1860 ArrayList list = new ArrayList ();
1863 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1865 $$ = (ArrayList) $2;
1869 variable_initializer_list
1870 : variable_initializer
1872 ArrayList list = new ArrayList ();
1876 | variable_initializer_list COMMA variable_initializer
1878 ArrayList list = (ArrayList) $1;
1885 : TYPEOF OPEN_PARENS type CLOSE_PARENS
1887 $$ = new TypeOf ((string) $3);
1892 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
1893 $$ = new SizeOf ((string) $3);
1895 note ("Verify type is unmanaged");
1896 note ("if (5.8) builtin, yield constant expression");
1901 : CHECKED OPEN_PARENS expression CLOSE_PARENS
1903 $$ = new CheckedExpr ((Expression) $3);
1907 unchecked_expression
1908 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
1910 $$ = new UnCheckedExpr ((Expression) $3);
1915 : primary_expression
1916 | PLUS unary_expression
1918 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
1920 | MINUS unary_expression
1922 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
1924 | BANG unary_expression
1926 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
1928 | TILDE unary_expression
1930 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
1932 | STAR unary_expression
1934 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
1936 | BITWISE_AND unary_expression
1938 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
1940 | OP_INC unary_expression
1942 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1944 | OP_DEC unary_expression
1946 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1950 we can not do cast expressions at this level,
1951 as there is an ambiguity. Check "Cast Expressions" 7.6.8
1952 for the recipe to handle this.
1956 pre_increment_expression
1957 : OP_INC unary_expression
1959 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
1963 pre_decrement_expression
1964 : OP_DEC unary_expression
1966 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
1972 * FIXME: This is actually wrong, it should be `type' but that
1973 * introduces a lot of {shift,reduce}/reduces
1975 * This is really really wrong. We need to track down
1976 * the source of problems with QIs because expressions like:
1977 * foreach (string s in (string []) object) wont be parsed.
1979 : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
1981 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
1983 | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
1985 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
1989 multiplicative_expression
1991 | multiplicative_expression STAR unary_expression
1993 $$ = new Binary (Binary.Operator.Multiply,
1994 (Expression) $1, (Expression) $3, lexer.Location);
1996 | multiplicative_expression DIV unary_expression
1998 $$ = new Binary (Binary.Operator.Division,
1999 (Expression) $1, (Expression) $3, lexer.Location);
2001 | multiplicative_expression PERCENT unary_expression
2003 $$ = new Binary (Binary.Operator.Modulus,
2004 (Expression) $1, (Expression) $3, lexer.Location);
2009 : multiplicative_expression
2010 | additive_expression PLUS multiplicative_expression
2012 $$ = new Binary (Binary.Operator.Addition,
2013 (Expression) $1, (Expression) $3, lexer.Location);
2015 | additive_expression MINUS multiplicative_expression
2017 $$ = new Binary (Binary.Operator.Subtraction,
2018 (Expression) $1, (Expression) $3, lexer.Location);
2023 : additive_expression
2024 | shift_expression OP_SHIFT_LEFT additive_expression
2026 $$ = new Binary (Binary.Operator.LeftShift,
2027 (Expression) $1, (Expression) $3, lexer.Location);
2029 | shift_expression OP_SHIFT_RIGHT additive_expression
2031 $$ = new Binary (Binary.Operator.RightShift,
2032 (Expression) $1, (Expression) $3, lexer.Location);
2036 relational_expression
2038 | relational_expression OP_LT shift_expression
2040 $$ = new Binary (Binary.Operator.LessThan,
2041 (Expression) $1, (Expression) $3, lexer.Location);
2043 | relational_expression OP_GT shift_expression
2045 $$ = new Binary (Binary.Operator.GreaterThan,
2046 (Expression) $1, (Expression) $3, lexer.Location);
2048 | relational_expression OP_LE shift_expression
2050 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2051 (Expression) $1, (Expression) $3, lexer.Location);
2053 | relational_expression OP_GE shift_expression
2055 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2056 (Expression) $1, (Expression) $3, lexer.Location);
2058 | relational_expression IS type
2060 $$ = new Probe (Probe.Operator.Is,
2061 (Expression) $1, (string) $3);
2063 | relational_expression AS type
2065 $$ = new Probe (Probe.Operator.As,
2066 (Expression) $1, (string) $3);
2071 : relational_expression
2072 | equality_expression OP_EQ relational_expression
2074 $$ = new Binary (Binary.Operator.Equality,
2075 (Expression) $1, (Expression) $3, lexer.Location);
2077 | equality_expression OP_NE relational_expression
2079 $$ = new Binary (Binary.Operator.Inequality,
2080 (Expression) $1, (Expression) $3, lexer.Location);
2085 : equality_expression
2086 | and_expression BITWISE_AND equality_expression
2088 $$ = new Binary (Binary.Operator.BitwiseAnd,
2089 (Expression) $1, (Expression) $3, lexer.Location);
2093 exclusive_or_expression
2095 | exclusive_or_expression CARRET and_expression
2097 $$ = new Binary (Binary.Operator.ExclusiveOr,
2098 (Expression) $1, (Expression) $3, lexer.Location);
2102 inclusive_or_expression
2103 : exclusive_or_expression
2104 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2106 $$ = new Binary (Binary.Operator.BitwiseOr,
2107 (Expression) $1, (Expression) $3, lexer.Location);
2111 conditional_and_expression
2112 : inclusive_or_expression
2113 | conditional_and_expression OP_AND inclusive_or_expression
2115 $$ = new Binary (Binary.Operator.LogicalAnd,
2116 (Expression) $1, (Expression) $3, lexer.Location);
2120 conditional_or_expression
2121 : conditional_and_expression
2122 | conditional_or_expression OP_OR conditional_and_expression
2124 $$ = new Binary (Binary.Operator.LogicalOr,
2125 (Expression) $1, (Expression) $3, lexer.Location);
2129 conditional_expression
2130 : conditional_or_expression
2131 | conditional_or_expression INTERR expression COLON expression
2133 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2137 assignment_expression
2138 : unary_expression ASSIGN expression
2140 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2142 | unary_expression OP_MULT_ASSIGN expression
2144 Location l = lexer.Location;
2146 $$ = new Assign ((Expression) $1,
2147 new Binary (Binary.Operator.Multiply,
2149 (Expression) $3, l), l);
2151 | unary_expression OP_DIV_ASSIGN expression
2153 Location l = lexer.Location;
2155 $$ = new Assign ((Expression) $1,
2156 new Binary (Binary.Operator.Division,
2158 (Expression) $3, l), l);
2160 | unary_expression OP_MOD_ASSIGN expression
2162 Location l = lexer.Location;
2164 $$ = new Assign ((Expression) $1,
2165 new Binary (Binary.Operator.Modulus,
2167 (Expression) $3, l), l);
2169 | unary_expression OP_ADD_ASSIGN expression
2171 Location l = lexer.Location;
2173 $$ = new Assign ((Expression) $1,
2174 new Binary (Binary.Operator.Addition,
2176 (Expression) $3, l), l);
2178 | unary_expression OP_SUB_ASSIGN expression
2180 Location l = lexer.Location;
2182 $$ = new Assign ((Expression) $1,
2183 new Binary (Binary.Operator.Subtraction,
2185 (Expression) $3, l), l);
2187 | unary_expression OP_SHIFT_LEFT_ASSIGN expression
2189 Location l = lexer.Location;
2191 $$ = new Assign ((Expression) $1,
2192 new Binary (Binary.Operator.LeftShift,
2194 (Expression) $3, l), l);
2196 | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2198 Location l = lexer.Location;
2200 $$ = new Assign ((Expression) $1,
2201 new Binary (Binary.Operator.RightShift,
2203 (Expression) $3, l), l);
2205 | unary_expression OP_AND_ASSIGN expression
2207 Location l = lexer.Location;
2209 $$ = new Assign ((Expression) $1,
2210 new Binary (Binary.Operator.BitwiseAnd,
2212 (Expression) $3, l), l);
2214 | unary_expression OP_OR_ASSIGN expression
2216 Location l = lexer.Location;
2218 $$ = new Assign ((Expression) $1,
2219 new Binary (Binary.Operator.BitwiseOr,
2221 (Expression) $3, l), l);
2223 | unary_expression OP_XOR_ASSIGN expression
2225 Location l = lexer.Location;
2227 $$ = new Assign ((Expression) $1,
2228 new Binary (Binary.Operator.ExclusiveOr,
2230 (Expression) $3, l), l);
2235 : conditional_expression
2236 | assignment_expression
2244 : expression { CheckBoolean ((Expression) $1); $$ = $1; }
2256 string full_class_name = MakeName ((string) $4);
2258 new_class = new Class (rc, current_container, full_class_name, (int) $2,
2259 (Attributes) $1, lexer.Location);
2260 current_container = new_class;
2261 current_container.Namespace = current_namespace;
2262 tree.RecordClass (full_class_name, new_class);
2268 Class new_class = (Class) current_container;
2271 new_class.Bases = (ArrayList) $6;
2273 current_container = current_container.Parent;
2274 CheckDef (current_container.AddClass (new_class), new_class.Name);
2281 : /* empty */ { $$ = (int) 0; }
2287 | modifiers modifier
2292 if ((m1 & m2) != 0) {
2293 Location l = lexer.Location;
2294 Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2296 $$ = (int) (m1 | m2);
2301 : NEW { $$ = Modifiers.NEW; }
2302 | PUBLIC { $$ = Modifiers.PUBLIC; }
2303 | PROTECTED { $$ = Modifiers.PROTECTED; }
2304 | INTERNAL { $$ = Modifiers.INTERNAL; }
2305 | PRIVATE { $$ = Modifiers.PRIVATE; }
2306 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2307 | SEALED { $$ = Modifiers.SEALED; }
2308 | STATIC { $$ = Modifiers.STATIC; }
2309 | READONLY { $$ = Modifiers.READONLY; }
2310 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2311 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2312 | EXTERN { $$ = Modifiers.EXTERN; }
2316 : /* empty */ { $$ = null; }
2317 | class_base { $$ = $1; }
2321 : COLON type_list { $$ = $2; }
2329 // A block is "contained" on the following places:
2331 // property_declaration as part of the accessor body (get/set)
2332 // operator_declaration
2333 // constructor_declaration
2334 // destructor_declaration
2335 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2340 current_block = new Block (current_block);
2342 opt_statement_list CLOSE_BRACE
2344 while (current_block.Implicit)
2345 current_block = current_block.Parent;
2347 current_block = current_block.Parent;
2358 | statement_list statement
2362 : declaration_statement
2364 if ((Block) $1 != current_block){
2365 current_block.AddStatement ((Statement) $1);
2366 current_block = (Block) $1;
2369 | embedded_statement
2371 current_block.AddStatement ((Statement) $1);
2375 current_block.AddStatement ((Statement) $1);
2382 | expression_statement
2383 | selection_statement
2384 | iteration_statement
2388 | unchecked_statement
2396 $$ = new EmptyStatement ();
2401 : IDENTIFIER COLON statement
2403 string lab = (String) $1;
2406 block = new Block (current_block, lab);
2407 block.AddStatement ((Statement) $3);
2410 if (!current_block.AddLabel (lab, block)){
2411 Location l = lexer.Location;
2412 Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2418 declaration_statement
2419 : local_variable_declaration SEMICOLON // done
2420 | local_constant_declaration SEMICOLON // finishme
2424 * The following is from Rhys' grammar:
2425 * > Types in local variable declarations must be recognized as
2426 * > expressions to prevent reduce/reduce errors in the grammar.
2427 * > The expressions are converted into types during semantic analysis.
2430 : primary_expression opt_rank_specifier
2432 // FIXME: Do something smart here regarding the composition of the type.
2434 // Ok, the above "primary_expression" is there to get rid of
2435 // both reduce/reduce and shift/reduces in the grammar, it should
2436 // really just be "type_name". If you use type_name, a reduce/reduce
2437 // creeps up. If you use qualified_identifier (which is all we need
2438 // really) two shift/reduces appear.
2441 // So we be a little smart :-)
2442 Expression expr = (Expression) $1;
2443 if (!(expr is SimpleName) && !(expr is MemberAccess)) {
2444 Location l = lexer.Location;
2445 Console.WriteLine (((MemberAccess) $1).Identifier);
2446 Report.Error (-1, l, "Invalid Type definition");
2447 $$ = "System.Object";
2449 $$ = GetQualifiedIdentifier (expr) + (string) $2;
2451 | builtin_types opt_rank_specifier
2453 $$ = (string) $1 + (string) $2;
2457 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2460 // // FIXME: this is a string that represents the type
2461 // // Figure out something to make this work.
2466 local_variable_declaration
2467 : local_variable_type variable_declarators
2469 $$ = declare_local_variables ((string) $1, (ArrayList) $2);
2473 local_constant_declaration
2474 : CONST type constant_declarator
2478 expression_statement
2479 : statement_expression SEMICOLON
2486 // We have to do the wrapping here and not in the case above,
2487 // because statement_expression is used for example in for_statement
2489 statement_expression
2490 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2491 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2492 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2493 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2494 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2495 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2496 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2499 object_creation_expression
2500 : object_or_delegate_creation_expression
2501 { note ("complain if this is a delegate maybe?"); }
2510 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2513 $$ = new If ((Expression) $3, (Statement) $5);
2515 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2516 embedded_statement ELSE embedded_statement
2518 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2523 : SWITCH OPEN_PARENS expression CLOSE_PARENS
2526 $$ = new Switch ((Expression) $3, (ArrayList) $5);
2540 : /* empty */ { $$ = new ArrayList (); }
2547 ArrayList sections = new ArrayList ();
2552 | switch_sections switch_section
2554 ArrayList sections = (ArrayList) $1;
2564 current_block = new Block (current_block);
2568 while (current_block.Implicit)
2569 current_block = current_block.Parent;
2570 $$ = new SwitchSection ((ArrayList) $1, current_block);
2571 current_block = current_block.Parent;
2578 ArrayList labels = new ArrayList ();
2583 | switch_labels switch_label
2585 ArrayList labels = (ArrayList) ($1);
2593 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2); }
2594 | DEFAULT COLON { $$ = new SwitchLabel (null); }
2605 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2607 $$ = new While ((Expression) $3, (Statement) $5);
2612 : DO embedded_statement
2613 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2615 $$ = new Do ((Statement) $2, (Expression) $5);
2621 opt_for_initializer SEMICOLON
2622 opt_for_condition SEMICOLON
2623 opt_for_iterator CLOSE_PARENS
2626 $$ = new For ((Statement) $3, (Expression) $5, (Statement) $7, (Statement) $9);
2631 : /* empty */ { $$ = new EmptyStatement (); }
2636 : local_variable_declaration
2638 if ((Block) $1 != current_block){
2639 current_block.AddStatement ((Statement) $1);
2640 current_block = (Block) $1;
2643 | statement_expression_list
2647 : /* empty */ { $$ = new BoolLiteral (true); }
2648 | boolean_expression
2652 : /* empty */ { $$ = new EmptyStatement (); }
2657 : statement_expression_list
2660 statement_expression_list
2661 : statement_expression
2663 Block b = new Block (null, true);
2665 b.AddStatement ((Statement) $1);
2668 | statement_expression_list COMMA statement_expression
2670 Block b = (Block) $1;
2672 b.AddStatement ((Statement) $3);
2678 : FOREACH OPEN_PARENS type IDENTIFIER IN
2680 $1 = lexer.Location;
2682 expression CLOSE_PARENS
2685 Block foreach_block = new Block (current_block, true);
2686 LocalVariableReference v;
2688 foreach_block.AddVariable ((string) $3, (string) $4, (Location) $1);
2689 v = new LocalVariableReference (foreach_block, (string) $4);
2690 foreach_block.AddStatement (new Foreach ((string) $3, v, (Expression) $7,
2691 (Statement) $9, (Location) $1));
2698 | continue_statement
2707 $$ = new Break (lexer.Location);
2712 : CONTINUE SEMICOLON
2714 $$ = new Continue (lexer.Location);
2719 : GOTO IDENTIFIER SEMICOLON
2721 $$ = new Goto ((string) $2, lexer.Location);
2723 | GOTO CASE constant_expression SEMICOLON
2727 | GOTO DEFAULT SEMICOLON
2734 : RETURN opt_expression SEMICOLON
2736 $$ = new Return ((Expression) $2, lexer.Location);
2741 : THROW opt_expression SEMICOLON
2743 $$ = new Throw ((Expression) $2);
2753 : TRY block catch_clauses
2756 ArrayList s = new ArrayList ();
2758 foreach (Catch cc in (ArrayList) $3) {
2759 if (cc.Type == null)
2765 // Now s contains the list of specific catch clauses
2766 // and g contains the general one.
2768 $$ = new Try ((Block) $2, s, g, null);
2770 | TRY block opt_catch_clauses FINALLY block
2773 ArrayList s = new ArrayList ();
2775 foreach (Catch cc in (ArrayList) $3) {
2776 if (cc.Type == null)
2782 $$ = new Try ((Block) $2, s, g, (Block) $5);
2787 : /* empty */ { $$ = null; }
2794 ArrayList l = new ArrayList ();
2799 | catch_clauses catch_clause
2801 ArrayList l = (ArrayList) $1;
2809 : /* empty */ { $$ = null; }
2814 : CATCH opt_catch_args
2816 string type = null, id = null;
2819 DictionaryEntry cc = (DictionaryEntry) $2;
2820 type = (string) cc.Key;
2821 id = (string) cc.Value;
2824 ArrayList one = new ArrayList ();
2825 one.Add (new VariableDeclaration (id, null, lexer.Location));
2828 current_block = new Block (current_block);
2829 Block b = declare_local_variables (type, one);
2836 string type = null, id = null;
2839 DictionaryEntry cc = (DictionaryEntry) $2;
2840 type = (string) cc.Key;
2841 id = (string) cc.Value;
2844 while (current_block != (Block) $1)
2845 current_block = current_block.Parent;
2850 $$ = new Catch (type, id , (Block) $4);
2855 : /* empty */ { $$ = null; }
2860 : OPEN_PARENS type opt_identifier CLOSE_PARENS
2862 $$ = new DictionaryEntry ($2, $3);
2869 $$ = new Checked ((Block) $2);
2876 $$ = new Unchecked ((Block) $2);
2881 : LOCK OPEN_PARENS expression CLOSE_PARENS
2883 $1 = lexer.Location;
2887 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
2892 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
2894 $1 = lexer.Location;
2898 $$ = new Using ($3, (Statement) $6, (Location) $1);
2902 resource_acquisition
2903 : local_variable_declaration
2905 if ((Block) $1 != current_block){
2906 current_block.AddStatement ((Statement) $1);
2907 current_block = (Block) $1;
2916 // A class used to pass around variable declarations and constants
2918 public class VariableDeclaration {
2919 public string identifier;
2920 public object expression_or_array_initializer;
2921 public Location Location;
2923 public VariableDeclaration (string id, object eoai, Location l){
2924 this.identifier = id;
2925 this.expression_or_array_initializer = eoai;
2931 // A class used to hold info about an indexer declarator
2934 public class IndexerDeclaration {
2936 public string interface_type;
2937 public Parameters param_list;
2939 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
2942 this.interface_type = interface_type;
2943 this.param_list = param_list;
2948 // A class used to hold info about an operator declarator
2951 public class OperatorDeclaration {
2952 public Operator.OpType optype;
2953 public string ret_type;
2954 public string arg1type;
2955 public string arg1name;
2956 public string arg2type;
2957 public string arg2name;
2958 public Location location;
2960 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
2961 string arg2type, string arg2name, Location location)
2964 this.ret_type = ret_type;
2965 this.arg1type = arg1type;
2966 this.arg1name = arg1name;
2967 this.arg2type = arg2type;
2968 this.arg2name = arg2name;
2969 this.location = location;
2975 // Given the @class_name name, it creates a fully qualified name
2976 // based on the containing declaration space
2979 MakeName (string class_name)
2981 string ns = current_namespace.Name;
2982 string container_name = current_container.Name;
2984 if (container_name == ""){
2986 return ns + "." + class_name;
2990 return container_name + "." + class_name;
2994 // Used to report back to the user the result of a declaration
2995 // in the current declaration space
2998 CheckDef (DeclSpace.AdditionResult result, string name)
3000 if (result == DeclSpace.AdditionResult.Success)
3003 Location l = lexer.Location;
3006 case DeclSpace.AdditionResult.NameExists:
3007 Report.Error (102, l, "The namespace `" + current_container.Name +
3008 "' already contains a definition for `"+
3013 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
3015 // case DeclSpace.AdditionResult.MethodDuplicated:
3016 // error (111, "Class `"+current_container.Name+
3017 // "' already defines a member called '" +
3018 // name + "' with the same parameter types");
3021 case DeclSpace.AdditionResult.EnclosingClash:
3022 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3025 case DeclSpace.AdditionResult.NotAConstructor:
3026 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3032 CheckDef (bool result, string name)
3036 CheckDef (DeclSpace.AdditionResult.NameExists, name);
3040 SimpleLookup (string name)
3043 // we need to check against current_block not being null
3044 // as `expression' is allowed in argument_lists, which
3045 // do not exist inside a block.
3047 if (current_block != null){
3048 if (current_block.IsVariableDefined (name))
3049 return new LocalVariableReference (current_block, name);
3052 if (current_local_parameters != null){
3054 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
3056 return new ParameterReference (current_local_parameters, idx, name);
3062 Expression DecomposeQI (string name, Location l)
3066 if (name.IndexOf ('.') == -1){
3067 o = SimpleLookup (name);
3069 return new SimpleName (name, l);
3072 int pos = name.LastIndexOf (".");
3073 string left = name.Substring (0, pos);
3074 string right = name.Substring (pos + 1);
3076 o = DecomposeQI (left, l);
3077 return new MemberAccess (o, right, l);
3082 // This method is used to get at the complete string representation of
3083 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3084 // This is necessary because local_variable_type admits primary_expression
3085 // as the type of the variable. So we do some extra checking
3087 string GetQualifiedIdentifier (Expression expr)
3089 if (expr is SimpleName)
3090 return ((SimpleName)expr).Name;
3091 else if (expr is MemberAccess)
3092 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3094 throw new Exception ("Expr has to be either SimpleName or MemberAccess !");
3098 Block declare_local_variables (string type, ArrayList variable_declarators)
3100 Block implicit_block;
3101 ArrayList inits = null;
3104 // We use the `Used' property to check whether statements
3105 // have been added to the current block. If so, we need
3106 // to create another block to contain the new declaration
3107 // otherwise, as an optimization, we use the same block to
3108 // add the declaration.
3110 // FIXME: A further optimization is to check if the statements
3111 // that were added were added as part of the initialization
3112 // below. In which case, no other statements have been executed
3113 // and we might be able to reduce the number of blocks for
3114 // situations like this:
3116 // int j = 1; int k = j + 1;
3118 if (current_block.Used)
3119 implicit_block = new Block (current_block, true);
3121 implicit_block = current_block;
3123 foreach (VariableDeclaration decl in variable_declarators){
3124 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3125 if (decl.expression_or_array_initializer != null){
3127 inits = new ArrayList ();
3131 Location l = lexer.Location;
3132 Report.Error (128, l, "A local variable `" + decl.identifier +
3133 "' is already defined in this scope");
3138 return implicit_block;
3140 foreach (VariableDeclaration decl in inits){
3141 if (decl.expression_or_array_initializer is Expression){
3142 Expression expr = (Expression) decl.expression_or_array_initializer;
3145 assign = new Assign (new LocalVariableReference (
3146 implicit_block, decl.identifier),
3147 expr, lexer.Location);
3148 implicit_block.AddStatement (new StatementExpression (assign));
3150 Console.WriteLine ("Not handling Array initializers yet");
3154 return implicit_block;
3157 void CheckConstant (Expression expr)
3162 void CheckBoolean (Expression expr)
3167 void CheckAttributeTarget (string a)
3171 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3175 Location l = lexer.Location;
3176 Report.Error (658, l, "Invalid attribute target");
3182 void CheckUnaryOperator (Operator.OpType op)
3186 case Operator.OpType.LogicalNot:
3187 case Operator.OpType.OnesComplement:
3188 case Operator.OpType.Increment:
3189 case Operator.OpType.Decrement:
3190 case Operator.OpType.True:
3191 case Operator.OpType.False:
3192 case Operator.OpType.Addition:
3193 case Operator.OpType.Subtraction:
3198 Location l = lexer.Location;
3199 Report.Error (1019, l, "Overloadable unary operator expected");
3205 void CheckBinaryOperator (Operator.OpType op)
3209 case Operator.OpType.Addition:
3210 case Operator.OpType.Subtraction:
3211 case Operator.OpType.Multiply:
3212 case Operator.OpType.Division:
3213 case Operator.OpType.Modulus:
3214 case Operator.OpType.BitwiseAnd:
3215 case Operator.OpType.BitwiseOr:
3216 case Operator.OpType.ExclusiveOr:
3217 case Operator.OpType.LeftShift:
3218 case Operator.OpType.RightShift:
3219 case Operator.OpType.Equality:
3220 case Operator.OpType.Inequality:
3221 case Operator.OpType.GreaterThan:
3222 case Operator.OpType.LessThan:
3223 case Operator.OpType.GreaterThanOrEqual:
3224 case Operator.OpType.LessThanOrEqual:
3228 Location l = lexer.Location;
3229 Report.Error (1020, l, "Overloadable binary operator expected");
3235 void output (string s)
3237 Console.WriteLine (s);
3240 void note (string s)
3242 // Used to put annotations
3247 public Tokenizer Lexer {
3253 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
3255 current_namespace = new Namespace (null, "");
3257 this.tree = rc.Tree;
3260 current_container = tree.Types;
3261 current_container.Namespace = current_namespace;
3263 lexer = new Tokenizer (input, name);
3266 public override int parse ()
3268 StringBuilder value = new StringBuilder ();
3272 if (yacc_verbose_flag)
3273 yyparse (lexer, new yydebug.yyDebugSimple ());
3276 } catch (Exception e){
3277 // Console.WriteLine ("Fatal error: " + name);
3278 // Console.WriteLine (lexer.location);
3280 Console.WriteLine (lexer.location + " : Parsing error ");
3281 Console.WriteLine (e);
3285 return global_errors;