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 ((AttributeSection) $1);
403 attrs = new Attributes ((AttributeSection) $1);
410 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
412 string target = null;
415 target = (string) $2;
417 $$ = new AttributeSection (target, (ArrayList) $3);
419 | OPEN_BRACKET attribute_list CLOSE_BRACKET
421 $$ = new AttributeSection (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;
463 temporary_loc = lexer.Location;
465 opt_attribute_arguments
467 $$ = new Attribute ((string) $1, (ArrayList) $3, temporary_loc);
472 : type_name { /* reserved attribute name or identifier: 17.4 */ }
475 opt_attribute_arguments
476 : /* empty */ { $$ = null; }
477 | OPEN_PARENS attribute_arguments CLOSE_PARENS
485 : positional_argument_list
487 ArrayList args = new ArrayList ();
492 | positional_argument_list COMMA named_argument_list
494 ArrayList args = new ArrayList ();
500 | named_argument_list
502 ArrayList args = new ArrayList ();
511 positional_argument_list
514 ArrayList args = new ArrayList ();
515 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
519 | positional_argument_list COMMA expression
521 ArrayList args = (ArrayList) $1;
522 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
531 ArrayList args = new ArrayList ();
536 | named_argument_list COMMA named_argument
538 ArrayList args = (ArrayList) $1;
546 : IDENTIFIER ASSIGN expression
548 $$ = new DictionaryEntry ((string) $1, new Argument ((Expression) $3, Argument.AType.Expression));
554 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
557 opt_class_member_declarations
559 | class_member_declarations
562 class_member_declarations
563 : class_member_declaration
564 | class_member_declarations
565 class_member_declaration
568 class_member_declaration
569 : constant_declaration // done
570 | field_declaration // done
571 | method_declaration // done
572 | property_declaration // done
573 | event_declaration // done
574 | indexer_declaration // done
575 | operator_declaration // done
576 | constructor_declaration // done
577 | destructor_declaration // done
587 string full_struct_name = MakeName ((string) $4);
589 new_struct = new Struct (rc, current_container, full_struct_name, (int) $2,
590 (Attributes) $1, lexer.Location);
591 current_container = new_struct;
592 current_container.Namespace = current_namespace;
593 tree.RecordStruct (full_struct_name, new_struct);
595 opt_struct_interfaces
599 Struct new_struct = (Struct) current_container;
601 current_container = current_container.Parent;
602 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
607 opt_struct_interfaces
614 | struct_interfaces struct_interface
622 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
625 opt_struct_member_declarations
627 | struct_member_declarations
630 struct_member_declarations
631 : struct_member_declaration
632 | struct_member_declarations struct_member_declaration
635 struct_member_declaration
636 : constant_declaration
639 | property_declaration
641 | indexer_declaration
642 | operator_declaration
643 | constructor_declaration
655 foreach (DictionaryEntry constant in (ArrayList) $5){
656 Constant c = new Constant (
657 (string) $4, (string) constant.Key,
658 (Expression) constant.Value, (int) $2, (Attributes) $1);
660 CheckDef (current_container.AddConstant (c), c.Name);
666 : constant_declarator
668 ArrayList constants = new ArrayList ();
672 | constant_declarators COMMA constant_declarator
674 ArrayList constants = (ArrayList) $1;
681 : IDENTIFIER ASSIGN constant_expression
683 $$ = new DictionaryEntry ($1, $3);
694 string type = (string) $3;
697 foreach (VariableDeclaration var in (ArrayList) $4){
698 Field field = new Field (type, mod, var.identifier,
699 var.expression_or_array_initializer, (Attributes) $1);
701 CheckDef (current_container.AddField (field), field.Name);
708 : variable_declarator
710 ArrayList decl = new ArrayList ();
714 | variable_declarators COMMA variable_declarator
716 ArrayList decls = (ArrayList) $1;
723 : IDENTIFIER ASSIGN variable_initializer
725 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
729 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
748 Method method = (Method) $1;
749 Block b = (Block) $2;
752 if ((method.ModFlags & (Modifiers.EXTERN | Modifiers.ABSTRACT)) == 0){
754 501, lexer.Location, "`" +
755 current_container.Name + "." + method.Name + "'" +
756 "must declare a body because it is not marked abstract or extern");
760 method.Block = (Block) $2;
761 CheckDef (current_container.AddMethod (method), method.Name);
763 current_local_parameters = null;
772 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
774 Method method = new Method ((string) $3, (int) $2, (string) $4,
775 (Parameters) $6, (Attributes) $1, lexer.Location);
777 current_local_parameters = (Parameters) $6;
785 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
787 Method method = new Method ("System.Void", (int) $2, (string) $4,
788 (Parameters) $6, (Attributes) $1, lexer.Location);
790 current_local_parameters = (Parameters) $6;
797 | SEMICOLON { $$ = null; }
800 opt_formal_parameter_list
801 : /* empty */ { $$ = new Parameters (null, null); }
802 | formal_parameter_list
805 formal_parameter_list
808 ArrayList pars_list = (ArrayList) $1;
810 Parameter [] pars = new Parameter [pars_list.Count];
811 pars_list.CopyTo (pars);
813 $$ = new Parameters (pars, null);
815 | fixed_parameters COMMA parameter_array
817 ArrayList pars_list = (ArrayList) $1;
819 Parameter [] pars = new Parameter [pars_list.Count];
820 pars_list.CopyTo (pars);
822 $$ = new Parameters (pars, (Parameter) $3);
826 $$ = new Parameters (null, (Parameter) $1);
833 ArrayList pars = new ArrayList ();
838 | fixed_parameters COMMA fixed_parameter
840 ArrayList pars = (ArrayList) $1;
849 opt_parameter_modifier
853 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
857 opt_parameter_modifier
858 : /* empty */ { $$ = Parameter.Modifier.NONE; }
863 : REF { $$ = Parameter.Modifier.REF; }
864 | OUT { $$ = Parameter.Modifier.OUT; }
868 : opt_attributes PARAMS type IDENTIFIER
870 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
871 note ("type must be a single-dimension array type");
876 : IDENTIFIER { $$ = $1.ToString (); }
877 | interface_type DOT IDENTIFIER { $$ = $1.ToString () + "." + $3.ToString (); }
886 implicit_value_parameter_type = (string) $3;
888 lexer.properties = true;
889 temporary_loc = lexer.Location;
891 accessor_declarations
893 lexer.properties = false;
898 DictionaryEntry pair = (DictionaryEntry) $7;
899 Block get_block = null;
900 Block set_block = null;
902 if (pair.Key != null)
903 get_block = (Block) pair.Key;
904 if (pair.Value != null)
905 set_block = (Block) pair.Value;
907 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block,
908 (Attributes) $1, temporary_loc);
910 CheckDef (current_container.AddProperty (prop), prop.Name);
911 implicit_value_parameter_type = null;
915 accessor_declarations
916 : get_accessor_declaration opt_set_accessor_declaration
918 $$ = new DictionaryEntry ($1, $2);
920 | set_accessor_declaration opt_get_accessor_declaration
922 $$ = new DictionaryEntry ($2, $1);
926 opt_get_accessor_declaration
927 : /* empty */ { $$ = null; }
928 | get_accessor_declaration
931 opt_set_accessor_declaration
932 : /* empty */ { $$ = null; }
933 | set_accessor_declaration
936 get_accessor_declaration
939 // If this is not the case, then current_local_parameters has already
940 // been set in indexer_declaration
941 if (parsing_indexer == false)
942 current_local_parameters = null;
944 current_local_parameters = indexer_parameters;
951 current_local_parameters = null;
955 set_accessor_declaration
959 Parameter implicit_value_parameter = new Parameter (
960 implicit_value_parameter_type, "value",
961 Parameter.Modifier.NONE, null);
963 if (parsing_indexer == false) {
964 args = new Parameter [1];
965 args [0] = implicit_value_parameter;
967 Parameter [] fp = indexer_parameters.FixedParameters;
968 int count = fp.Length;
970 args = new Parameter [count + 1];
973 args [count] = implicit_value_parameter;
975 current_local_parameters = new Parameters (args, null);
980 current_local_parameters = null;
986 | SEMICOLON { $$ = new Block (null); }
989 interface_declaration
994 Interface new_interface;
995 string full_interface_name = MakeName ((string) $4);
997 new_interface = new Interface (rc, current_container, full_interface_name, (int) $2,
998 (Attributes) $1, lexer.Location);
999 if (current_interface != null) {
1000 Location l = lexer.Location;
1001 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1003 current_interface = new_interface;
1004 tree.RecordInterface (full_interface_name, new_interface);
1009 Interface new_interface = (Interface) current_interface;
1012 new_interface.Bases = (ArrayList) $6;
1014 current_interface = null;
1015 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
1020 : /* empty */ { $$ = null; }
1025 : COLON interface_type_list { $$ = $2; }
1031 ArrayList interfaces = new ArrayList ();
1033 interfaces.Add ($1);
1036 | interface_type_list COMMA interface_type
1038 ArrayList interfaces = (ArrayList) $1;
1039 interfaces.Add ($3);
1046 opt_interface_member_declarations
1050 opt_interface_member_declarations
1052 | interface_member_declarations
1055 interface_member_declarations
1056 : interface_member_declaration
1057 | interface_member_declarations interface_member_declaration
1060 interface_member_declaration
1061 : interface_method_declaration
1063 InterfaceMethod m = (InterfaceMethod) $1;
1065 CheckDef (current_interface.AddMethod (m), m.Name);
1067 | interface_property_declaration
1069 InterfaceProperty p = (InterfaceProperty) $1;
1071 CheckDef (current_interface.AddProperty (p), p.Name);
1073 | interface_event_declaration
1075 InterfaceEvent e = (InterfaceEvent) $1;
1077 CheckDef (current_interface.AddEvent (e), e.Name);
1079 | interface_indexer_declaration
1081 InterfaceIndexer i = (InterfaceIndexer) $1;
1083 CheckDef (current_interface.AddIndexer (i), "indexer");
1088 : /* empty */ { $$ = false; }
1089 | NEW { $$ = true; }
1092 interface_method_declaration
1093 : opt_attributes opt_new type IDENTIFIER
1094 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1097 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1099 | opt_attributes opt_new VOID IDENTIFIER
1100 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1103 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1107 interface_property_declaration
1112 { lexer.properties = true; }
1114 { lexer.properties = false; }
1119 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
1120 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1);
1125 : opt_attributes GET SEMICOLON { $$ = 1; }
1126 | opt_attributes SET SEMICOLON { $$ = 2; }
1127 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1129 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1133 interface_event_declaration
1134 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1136 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1140 interface_indexer_declaration
1141 : opt_attributes opt_new type THIS
1142 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1144 { lexer.properties = true; }
1146 { lexer.properties = false; }
1149 int a_flags = (int) $10;
1151 bool do_get = (a_flags & 1) == 1;
1152 bool do_set = (a_flags & 2) == 2;
1154 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set, (bool) $2, (Attributes) $1);
1158 operator_declaration
1159 : opt_attributes opt_modifiers operator_declarator block
1161 OperatorDeclaration decl = (OperatorDeclaration) $3;
1163 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1164 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1166 // Note again, checking is done in semantic analysis
1167 current_container.AddOperator (op);
1172 : type OPERATOR overloadable_operator
1173 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1175 Operator.OpType op = (Operator.OpType) $3;
1176 CheckUnaryOperator (op);
1178 if (op == Operator.OpType.Addition)
1179 op = Operator.OpType.UnaryPlus;
1181 if (op == Operator.OpType.Subtraction)
1182 op = Operator.OpType.UnaryNegation;
1184 $$ = new OperatorDeclaration (op, (string) $1, (string) $5, (string) $6,
1185 null, null, lexer.Location);
1187 | type OPERATOR overloadable_operator
1189 type IDENTIFIER COMMA
1193 CheckBinaryOperator ((Operator.OpType) $3);
1195 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1196 (string) $8, (string) $9, lexer.Location);
1198 | conversion_operator_declarator
1201 overloadable_operator
1203 : BANG { $$ = Operator.OpType.LogicalNot; }
1204 | TILDE { $$ = Operator.OpType.OnesComplement; }
1205 | OP_INC { $$ = Operator.OpType.Increment; }
1206 | OP_DEC { $$ = Operator.OpType.Decrement; }
1207 | TRUE { $$ = Operator.OpType.True; }
1208 | FALSE { $$ = Operator.OpType.False; }
1209 // Unary and binary:
1210 | PLUS { $$ = Operator.OpType.Addition; }
1211 | MINUS { $$ = Operator.OpType.Subtraction; }
1213 | STAR { $$ = Operator.OpType.Multiply; }
1214 | DIV { $$ = Operator.OpType.Division; }
1215 | PERCENT { $$ = Operator.OpType.Modulus; }
1216 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1217 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1218 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1219 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1220 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1221 | OP_EQ { $$ = Operator.OpType.Equality; }
1222 | OP_NE { $$ = Operator.OpType.Inequality; }
1223 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1224 | OP_LT { $$ = Operator.OpType.LessThan; }
1225 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1226 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1229 conversion_operator_declarator
1230 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1232 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1233 null, null, lexer.Location);
1235 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1237 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1238 null, null, lexer.Location);
1242 constructor_declaration
1245 constructor_declarator
1248 Constructor c = (Constructor) $3;
1249 c.Block = (Block) $4;
1250 c.ModFlags = (int) $2;
1251 c.OptAttributes = (Attributes) $1;
1253 if ((c.ModFlags & Modifiers.STATIC) != 0){
1254 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1255 Location l = lexer.Location;
1256 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1259 if (c.Initializer != null){
1260 Location l = lexer.Location;
1261 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1264 if (!c.Parameters.Empty){
1265 Location l = lexer.Location;
1266 Report.Error (103, l, "Static constructors should not have parameters");
1270 CheckDef (current_container.AddConstructor (c), c.Name);
1272 current_local_parameters = null;
1276 constructor_declarator
1278 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1279 opt_constructor_initializer
1281 Location l = lexer.Location;
1283 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5, l);
1285 current_local_parameters = (Parameters) $3;
1289 opt_constructor_initializer
1290 : /* empty */ { $$ = null; }
1291 | constructor_initializer
1294 constructor_initializer
1295 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1297 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1299 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1301 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1305 destructor_declaration
1306 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1309 // FIXME: The destructor needs to generate special code:
1315 // void Finalize () {
1316 // try { code } finally { base.Finalize (); }
1318 Method d = new Method ("System.Void", 0, "Finalize",
1319 new Parameters (null, null), (Attributes) $1, lexer.Location);
1321 d.Block = (Block) $6;
1322 CheckDef (current_container.AddMethod (d), d.Name);
1329 EVENT type variable_declarators SEMICOLON
1331 foreach (VariableDeclaration var in (ArrayList) $5) {
1333 // FIXME : Is this right ?
1334 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1335 (int) $2, null, null, (Attributes) $1, lexer.Location);
1337 CheckDef (current_container.AddEvent (e), e.Name);
1343 EVENT type member_name
1344 OPEN_BRACE event_accessor_declarations CLOSE_BRACE
1346 DictionaryEntry pair = (DictionaryEntry) $7;
1347 Block add_block = null;
1348 Block rem_block = null;
1350 if (pair.Key != null)
1351 add_block = (Block) pair.Key;
1352 if (pair.Value != null)
1353 rem_block = (Block) pair.Value;
1355 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
1356 (Attributes) $1, lexer.Location);
1358 CheckDef (current_container.AddEvent (e), e.Name);
1362 event_accessor_declarations
1363 : add_accessor_declaration remove_accessor_declaration
1365 $$ = new DictionaryEntry ($1, $2);
1367 | remove_accessor_declaration add_accessor_declaration
1369 $$ = new DictionaryEntry ($2, $1);
1373 add_accessor_declaration
1374 : opt_attributes ADD block
1380 remove_accessor_declaration
1381 : opt_attributes REMOVE block
1388 : opt_attributes opt_modifiers indexer_declarator
1391 IndexerDeclaration decl = (IndexerDeclaration) $3;
1393 implicit_value_parameter_type = decl.type;
1395 lexer.properties = true;
1396 temporary_loc = lexer.Location;
1397 parsing_indexer = true;
1399 indexer_parameters = decl.param_list;
1401 accessor_declarations
1403 lexer.properties = false;
1404 parsing_indexer = false;
1408 // The signature is computed from the signature of the indexer. Look
1409 // at section 3.6 on the spec
1412 IndexerDeclaration decl = (IndexerDeclaration) $3;
1413 DictionaryEntry pair = (DictionaryEntry) $6;
1414 Block get_block = null;
1415 Block set_block = null;
1417 if (pair.Key != null)
1418 get_block = (Block) pair.Key;
1419 if (pair.Value != null)
1420 set_block = (Block) pair.Value;
1422 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1423 get_block, set_block, (Attributes) $1, temporary_loc);
1425 // Note that there is no equivalent of CheckDef for this case
1426 // We shall handle this in semantic analysis
1428 current_container.AddIndexer (indexer);
1430 current_local_parameters = null;
1431 implicit_value_parameter_type = null;
1432 indexer_parameters = null;
1437 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1439 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
1441 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1443 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
1455 string name = (string) $4;
1456 Enum e = new Enum ((string) $5, (int) $2, name, (Attributes) $1, lexer.Location);
1458 foreach (VariableDeclaration ev in (ArrayList) $6){
1459 CheckDef (e.AddEnumMember (ev.identifier,
1460 (Expression) ev.expression_or_array_initializer),
1464 CheckDef (current_container.AddEnum (e), name);
1469 : /* empty */ { $$ = "System.Int32"; }
1470 | COLON integral_type { $$ = $2; }
1474 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1480 opt_enum_member_declarations
1481 : /* empty */ { $$ = new ArrayList (); }
1482 | enum_member_declarations opt_comma { $$ = $1; }
1485 enum_member_declarations
1486 : enum_member_declaration
1488 ArrayList l = new ArrayList ();
1493 | enum_member_declarations COMMA enum_member_declaration
1495 ArrayList l = (ArrayList) $1;
1503 enum_member_declaration
1504 : opt_attributes IDENTIFIER
1506 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1508 | opt_attributes IDENTIFIER ASSIGN expression
1510 $$ = new VariableDeclaration ((string) $2, $4, lexer.Location);
1514 delegate_declaration
1518 IDENTIFIER OPEN_PARENS
1519 formal_parameter_list
1523 Delegate del = new Delegate (rc, (string) $4, (int) $2,
1524 MakeName ((string) $5), (Parameters) $7,
1525 (Attributes) $1, lexer.Location);
1527 CheckDef (current_container.AddDelegate (del), del.Name);
1532 IDENTIFIER OPEN_PARENS
1533 formal_parameter_list
1537 Delegate del = new Delegate (rc, "System.Void", (int) $2, (string) $5, (Parameters) $7,
1538 (Attributes) $1, lexer.Location);
1540 CheckDef (current_container.AddDelegate (del), del.Name);
1545 : namespace_or_type_name
1548 namespace_or_type_name
1549 : qualified_identifier
1553 * Before you think of adding a return_type, notice that we have been
1554 * using two rules in the places where it matters (one rule using type
1555 * and another identical one that uses VOID as the return type). This
1556 * gets rid of a shift/reduce couple
1559 : type_name { /* class_type */
1561 This does interfaces, delegates, struct_types, class_types,
1562 parent classes, and more! 4.2
1573 ArrayList types = new ArrayList ();
1578 | type_list COMMA type
1580 ArrayList types = (ArrayList) $1;
1588 * replaces all the productions for isolating the various
1589 * simple types, but we need this to reuse it easily in local_variable_type
1592 : OBJECT { $$ = "System.Object"; }
1593 | STRING { $$ = "System.String"; }
1594 | BOOL { $$ = "System.Boolean"; }
1595 | DECIMAL { $$ = "System.Decimal"; }
1596 | FLOAT { $$ = "System.Single"; }
1597 | DOUBLE { $$ = "System.Double"; }
1602 : SBYTE { $$ = "System.SByte"; }
1603 | BYTE { $$ = "System.Byte"; }
1604 | SHORT { $$ = "System.Int16"; }
1605 | USHORT { $$ = "System.UInt16"; }
1606 | INT { $$ = "System.Int32"; }
1607 | UINT { $$ = "System.UInt32"; }
1608 | LONG { $$ = "System.Int64"; }
1609 | ULONG { $$ = "System.UInt64"; }
1610 | CHAR { $$ = "System.Char"; }
1618 : type rank_specifiers
1620 $$ = (string) $1 + (string) $2;
1625 // Expressions, section 7.5
1633 | qualified_identifier
1635 string name = (string) $1;
1638 $$ = DecomposeQI (name, lexer.Location);
1640 | parenthesized_expression
1642 | invocation_expression
1646 | post_increment_expression
1647 | post_decrement_expression
1651 | checked_expression
1652 | unchecked_expression
1659 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1660 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1661 | NULL { $$ = new NullLiteral (); }
1665 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1666 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1667 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1672 object v = lexer.Value;
1675 // FIXME: Possible optimization would be to
1676 // compute the *Literal objects directly in the scanner
1679 $$ = new IntLiteral ((Int32) v);
1681 $$ = new UIntLiteral ((UInt32) v);
1683 $$ = new LongLiteral ((Int64) v);
1684 else if (v is ulong)
1685 $$ = new ULongLiteral ((UInt64) v);
1687 Console.WriteLine ("OOPS. Unexpected result from scanner");
1692 : TRUE { $$ = new BoolLiteral (true); }
1693 | FALSE { $$ = new BoolLiteral (false); }
1696 parenthesized_expression
1697 : OPEN_PARENS expression CLOSE_PARENS
1702 : primary_expression DOT IDENTIFIER
1704 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1706 | predefined_type DOT IDENTIFIER
1708 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1716 invocation_expression
1717 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1720 // if $1 is MethodGroup
1721 // $$ = new Call ($1, $3);
1723 // $$ = new DelegateCall ($1, $3);
1725 Location l = lexer.Location;
1726 Report.Error (1, l, "THIS IS CRAZY");
1728 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1734 : /* empty */ { $$ = null; }
1741 ArrayList list = new ArrayList ();
1745 | argument_list COMMA argument
1747 ArrayList list = (ArrayList) $1;
1756 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1758 | REF variable_reference
1760 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1762 | OUT variable_reference
1764 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1769 : expression { note ("section 5.4"); $$ = $1; }
1773 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1775 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1782 ArrayList list = new ArrayList ();
1786 | expression_list COMMA expression
1788 ArrayList list = (ArrayList) $1;
1797 $$ = new This (lexer.Location);
1802 : BASE DOT IDENTIFIER
1804 $$ = new BaseAccess (BaseAccess.BaseAccessType.Member, (string) $3, null);
1806 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1808 $$ = new BaseAccess (BaseAccess.BaseAccessType.Indexer, null, (ArrayList) $3);
1812 post_increment_expression
1813 : primary_expression OP_INC
1815 $$ = new Unary (Unary.Operator.PostIncrement, (Expression) $1, lexer.Location);
1819 post_decrement_expression
1820 : primary_expression OP_DEC
1822 $$ = new Unary (Unary.Operator.PostDecrement, (Expression) $1, lexer.Location);
1827 : object_or_delegate_creation_expression
1828 | array_creation_expression
1831 object_or_delegate_creation_expression
1832 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1834 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1838 array_creation_expression
1839 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
1841 opt_array_initializer
1843 $$ = new ArrayCreation ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7,
1846 | NEW type rank_specifiers array_initializer
1848 $$ = new ArrayCreation ((string) $2, (string) $3, (ArrayList) $4, lexer.Location);
1868 | rank_specifiers rank_specifier
1870 $$ = (string) $2 + (string) $1;
1875 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1877 $$ = "[" + (string) $2 + "]";
1897 | dim_separators COMMA
1899 $$ = (string) $1 + ",";
1903 opt_array_initializer
1915 : OPEN_BRACE CLOSE_BRACE
1917 ArrayList list = new ArrayList ();
1920 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1922 $$ = (ArrayList) $2;
1926 variable_initializer_list
1927 : variable_initializer
1929 ArrayList list = new ArrayList ();
1933 | variable_initializer_list COMMA variable_initializer
1935 ArrayList list = (ArrayList) $1;
1942 : TYPEOF OPEN_PARENS type CLOSE_PARENS
1944 $$ = new TypeOf ((string) $3);
1949 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
1950 $$ = new SizeOf ((string) $3);
1952 note ("Verify type is unmanaged");
1953 note ("if (5.8) builtin, yield constant expression");
1958 : CHECKED OPEN_PARENS expression CLOSE_PARENS
1960 $$ = new CheckedExpr ((Expression) $3);
1964 unchecked_expression
1965 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
1967 $$ = new UnCheckedExpr ((Expression) $3);
1972 : primary_expression
1973 | PLUS unary_expression
1975 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
1977 | MINUS unary_expression
1979 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
1981 | BANG unary_expression
1983 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
1985 | TILDE unary_expression
1987 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
1989 | STAR unary_expression
1991 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
1993 | BITWISE_AND unary_expression
1995 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
1997 | OP_INC unary_expression
1999 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
2001 | OP_DEC unary_expression
2003 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
2007 we can not do cast expressions at this level,
2008 as there is an ambiguity. Check "Cast Expressions" 7.6.8
2009 for the recipe to handle this.
2013 pre_increment_expression
2014 : OP_INC unary_expression
2016 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
2020 pre_decrement_expression
2021 : OP_DEC unary_expression
2023 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
2029 * FIXME: This is actually wrong, it should be `type' but that
2030 * introduces a lot of {shift,reduce}/reduces
2032 * This is really really wrong. We need to track down
2033 * the source of problems with QIs because expressions like:
2034 * foreach (string s in (string []) object) wont be parsed.
2036 : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
2038 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
2040 | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
2042 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
2046 multiplicative_expression
2048 | multiplicative_expression STAR unary_expression
2050 $$ = new Binary (Binary.Operator.Multiply,
2051 (Expression) $1, (Expression) $3, lexer.Location);
2053 | multiplicative_expression DIV unary_expression
2055 $$ = new Binary (Binary.Operator.Division,
2056 (Expression) $1, (Expression) $3, lexer.Location);
2058 | multiplicative_expression PERCENT unary_expression
2060 $$ = new Binary (Binary.Operator.Modulus,
2061 (Expression) $1, (Expression) $3, lexer.Location);
2066 : multiplicative_expression
2067 | additive_expression PLUS multiplicative_expression
2069 $$ = new Binary (Binary.Operator.Addition,
2070 (Expression) $1, (Expression) $3, lexer.Location);
2072 | additive_expression MINUS multiplicative_expression
2074 $$ = new Binary (Binary.Operator.Subtraction,
2075 (Expression) $1, (Expression) $3, lexer.Location);
2080 : additive_expression
2081 | shift_expression OP_SHIFT_LEFT additive_expression
2083 $$ = new Binary (Binary.Operator.LeftShift,
2084 (Expression) $1, (Expression) $3, lexer.Location);
2086 | shift_expression OP_SHIFT_RIGHT additive_expression
2088 $$ = new Binary (Binary.Operator.RightShift,
2089 (Expression) $1, (Expression) $3, lexer.Location);
2093 relational_expression
2095 | relational_expression OP_LT shift_expression
2097 $$ = new Binary (Binary.Operator.LessThan,
2098 (Expression) $1, (Expression) $3, lexer.Location);
2100 | relational_expression OP_GT shift_expression
2102 $$ = new Binary (Binary.Operator.GreaterThan,
2103 (Expression) $1, (Expression) $3, lexer.Location);
2105 | relational_expression OP_LE shift_expression
2107 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2108 (Expression) $1, (Expression) $3, lexer.Location);
2110 | relational_expression OP_GE shift_expression
2112 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2113 (Expression) $1, (Expression) $3, lexer.Location);
2115 | relational_expression IS type
2117 $$ = new Probe (Probe.Operator.Is,
2118 (Expression) $1, (string) $3);
2120 | relational_expression AS type
2122 $$ = new Probe (Probe.Operator.As,
2123 (Expression) $1, (string) $3);
2128 : relational_expression
2129 | equality_expression OP_EQ relational_expression
2131 $$ = new Binary (Binary.Operator.Equality,
2132 (Expression) $1, (Expression) $3, lexer.Location);
2134 | equality_expression OP_NE relational_expression
2136 $$ = new Binary (Binary.Operator.Inequality,
2137 (Expression) $1, (Expression) $3, lexer.Location);
2142 : equality_expression
2143 | and_expression BITWISE_AND equality_expression
2145 $$ = new Binary (Binary.Operator.BitwiseAnd,
2146 (Expression) $1, (Expression) $3, lexer.Location);
2150 exclusive_or_expression
2152 | exclusive_or_expression CARRET and_expression
2154 $$ = new Binary (Binary.Operator.ExclusiveOr,
2155 (Expression) $1, (Expression) $3, lexer.Location);
2159 inclusive_or_expression
2160 : exclusive_or_expression
2161 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2163 $$ = new Binary (Binary.Operator.BitwiseOr,
2164 (Expression) $1, (Expression) $3, lexer.Location);
2168 conditional_and_expression
2169 : inclusive_or_expression
2170 | conditional_and_expression OP_AND inclusive_or_expression
2172 $$ = new Binary (Binary.Operator.LogicalAnd,
2173 (Expression) $1, (Expression) $3, lexer.Location);
2177 conditional_or_expression
2178 : conditional_and_expression
2179 | conditional_or_expression OP_OR conditional_and_expression
2181 $$ = new Binary (Binary.Operator.LogicalOr,
2182 (Expression) $1, (Expression) $3, lexer.Location);
2186 conditional_expression
2187 : conditional_or_expression
2188 | conditional_or_expression INTERR expression COLON expression
2190 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2194 assignment_expression
2195 : unary_expression ASSIGN expression
2197 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2199 | unary_expression OP_MULT_ASSIGN expression
2201 Location l = lexer.Location;
2203 $$ = new Assign ((Expression) $1,
2204 new Binary (Binary.Operator.Multiply,
2206 (Expression) $3, l), l);
2208 | unary_expression OP_DIV_ASSIGN expression
2210 Location l = lexer.Location;
2212 $$ = new Assign ((Expression) $1,
2213 new Binary (Binary.Operator.Division,
2215 (Expression) $3, l), l);
2217 | unary_expression OP_MOD_ASSIGN expression
2219 Location l = lexer.Location;
2221 $$ = new Assign ((Expression) $1,
2222 new Binary (Binary.Operator.Modulus,
2224 (Expression) $3, l), l);
2226 | unary_expression OP_ADD_ASSIGN expression
2228 Location l = lexer.Location;
2230 $$ = new Assign ((Expression) $1,
2231 new Binary (Binary.Operator.Addition,
2233 (Expression) $3, l), l);
2235 | unary_expression OP_SUB_ASSIGN expression
2237 Location l = lexer.Location;
2239 $$ = new Assign ((Expression) $1,
2240 new Binary (Binary.Operator.Subtraction,
2242 (Expression) $3, l), l);
2244 | unary_expression OP_SHIFT_LEFT_ASSIGN expression
2246 Location l = lexer.Location;
2248 $$ = new Assign ((Expression) $1,
2249 new Binary (Binary.Operator.LeftShift,
2251 (Expression) $3, l), l);
2253 | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2255 Location l = lexer.Location;
2257 $$ = new Assign ((Expression) $1,
2258 new Binary (Binary.Operator.RightShift,
2260 (Expression) $3, l), l);
2262 | unary_expression OP_AND_ASSIGN expression
2264 Location l = lexer.Location;
2266 $$ = new Assign ((Expression) $1,
2267 new Binary (Binary.Operator.BitwiseAnd,
2269 (Expression) $3, l), l);
2271 | unary_expression OP_OR_ASSIGN expression
2273 Location l = lexer.Location;
2275 $$ = new Assign ((Expression) $1,
2276 new Binary (Binary.Operator.BitwiseOr,
2278 (Expression) $3, l), l);
2280 | unary_expression OP_XOR_ASSIGN expression
2282 Location l = lexer.Location;
2284 $$ = new Assign ((Expression) $1,
2285 new Binary (Binary.Operator.ExclusiveOr,
2287 (Expression) $3, l), l);
2292 : conditional_expression
2293 | assignment_expression
2301 : expression { CheckBoolean ((Expression) $1); $$ = $1; }
2313 string full_class_name = MakeName ((string) $4);
2315 new_class = new Class (rc, current_container, full_class_name, (int) $2,
2316 (Attributes) $1, lexer.Location);
2317 current_container = new_class;
2318 current_container.Namespace = current_namespace;
2319 tree.RecordClass (full_class_name, new_class);
2325 Class new_class = (Class) current_container;
2328 new_class.Bases = (ArrayList) $6;
2330 current_container = current_container.Parent;
2331 CheckDef (current_container.AddClass (new_class), new_class.Name);
2338 : /* empty */ { $$ = (int) 0; }
2344 | modifiers modifier
2349 if ((m1 & m2) != 0) {
2350 Location l = lexer.Location;
2351 Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2353 $$ = (int) (m1 | m2);
2358 : NEW { $$ = Modifiers.NEW; }
2359 | PUBLIC { $$ = Modifiers.PUBLIC; }
2360 | PROTECTED { $$ = Modifiers.PROTECTED; }
2361 | INTERNAL { $$ = Modifiers.INTERNAL; }
2362 | PRIVATE { $$ = Modifiers.PRIVATE; }
2363 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2364 | SEALED { $$ = Modifiers.SEALED; }
2365 | STATIC { $$ = Modifiers.STATIC; }
2366 | READONLY { $$ = Modifiers.READONLY; }
2367 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2368 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2369 | EXTERN { $$ = Modifiers.EXTERN; }
2373 : /* empty */ { $$ = null; }
2374 | class_base { $$ = $1; }
2378 : COLON type_list { $$ = $2; }
2386 // A block is "contained" on the following places:
2388 // property_declaration as part of the accessor body (get/set)
2389 // operator_declaration
2390 // constructor_declaration
2391 // destructor_declaration
2392 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2397 current_block = new Block (current_block);
2399 opt_statement_list CLOSE_BRACE
2401 while (current_block.Implicit)
2402 current_block = current_block.Parent;
2404 current_block = current_block.Parent;
2415 | statement_list statement
2419 : declaration_statement
2421 if ((Block) $1 != current_block){
2422 current_block.AddStatement ((Statement) $1);
2423 current_block = (Block) $1;
2426 | embedded_statement
2428 current_block.AddStatement ((Statement) $1);
2432 current_block.AddStatement ((Statement) $1);
2439 | expression_statement
2440 | selection_statement
2441 | iteration_statement
2445 | unchecked_statement
2453 $$ = new EmptyStatement ();
2458 : IDENTIFIER COLON statement
2460 string lab = (String) $1;
2463 block = new Block (current_block, lab);
2464 block.AddStatement ((Statement) $3);
2467 if (!current_block.AddLabel (lab, block)){
2468 Location l = lexer.Location;
2469 Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2475 declaration_statement
2476 : local_variable_declaration SEMICOLON // done
2477 | local_constant_declaration SEMICOLON // finishme
2481 * The following is from Rhys' grammar:
2482 * > Types in local variable declarations must be recognized as
2483 * > expressions to prevent reduce/reduce errors in the grammar.
2484 * > The expressions are converted into types during semantic analysis.
2487 : primary_expression opt_rank_specifier
2489 // FIXME: Do something smart here regarding the composition of the type.
2491 // Ok, the above "primary_expression" is there to get rid of
2492 // both reduce/reduce and shift/reduces in the grammar, it should
2493 // really just be "type_name". If you use type_name, a reduce/reduce
2494 // creeps up. If you use qualified_identifier (which is all we need
2495 // really) two shift/reduces appear.
2498 // So the super-trick is that primary_expression
2499 // can only be either a SimpleName or a MemberAccess.
2500 // The MemberAccess case arises when you have a fully qualified type-name like :
2502 // SimpleName is when you have
2505 Expression expr = (Expression) $1;
2506 if (!(expr is SimpleName || expr is MemberAccess)) {
2507 Location l = lexer.Location;
2508 Report.Error (-1, l, "Invalid Type definition");
2509 $$ = "System.Object";
2513 // So we extract the string corresponding to the SimpleName
2516 $$ = GetQualifiedIdentifier (expr) + (string) $2;
2518 | builtin_types opt_rank_specifier
2520 $$ = (string) $1 + (string) $2;
2524 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2527 // // FIXME: this is a string that represents the type
2528 // // Figure out something to make this work.
2533 local_variable_declaration
2534 : local_variable_type variable_declarators
2536 $$ = declare_local_variables ((string) $1, (ArrayList) $2);
2540 local_constant_declaration
2541 : CONST type constant_declarator
2545 expression_statement
2546 : statement_expression SEMICOLON
2553 // We have to do the wrapping here and not in the case above,
2554 // because statement_expression is used for example in for_statement
2556 statement_expression
2557 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2558 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2559 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2560 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2561 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2562 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2563 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2566 object_creation_expression
2567 : object_or_delegate_creation_expression
2568 { note ("complain if this is a delegate maybe?"); }
2577 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2580 $$ = new If ((Expression) $3, (Statement) $5);
2582 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2583 embedded_statement ELSE embedded_statement
2585 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2590 : SWITCH OPEN_PARENS expression CLOSE_PARENS
2593 $$ = new Switch ((Expression) $3, (ArrayList) $5);
2607 : /* empty */ { $$ = new ArrayList (); }
2614 ArrayList sections = new ArrayList ();
2619 | switch_sections switch_section
2621 ArrayList sections = (ArrayList) $1;
2631 current_block = new Block (current_block);
2635 while (current_block.Implicit)
2636 current_block = current_block.Parent;
2637 $$ = new SwitchSection ((ArrayList) $1, current_block);
2638 current_block = current_block.Parent;
2645 ArrayList labels = new ArrayList ();
2650 | switch_labels switch_label
2652 ArrayList labels = (ArrayList) ($1);
2660 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2); }
2661 | DEFAULT COLON { $$ = new SwitchLabel (null); }
2672 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2674 $$ = new While ((Expression) $3, (Statement) $5);
2679 : DO embedded_statement
2680 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2682 $$ = new Do ((Statement) $2, (Expression) $5);
2688 opt_for_initializer SEMICOLON
2689 opt_for_condition SEMICOLON
2690 opt_for_iterator CLOSE_PARENS
2693 $$ = new For ((Statement) $3, (Expression) $5, (Statement) $7, (Statement) $9);
2698 : /* empty */ { $$ = new EmptyStatement (); }
2703 : local_variable_declaration
2705 if ((Block) $1 != current_block){
2706 current_block.AddStatement ((Statement) $1);
2707 current_block = (Block) $1;
2710 | statement_expression_list
2714 : /* empty */ { $$ = new BoolLiteral (true); }
2715 | boolean_expression
2719 : /* empty */ { $$ = new EmptyStatement (); }
2724 : statement_expression_list
2727 statement_expression_list
2728 : statement_expression
2730 Block b = new Block (null, true);
2732 b.AddStatement ((Statement) $1);
2735 | statement_expression_list COMMA statement_expression
2737 Block b = (Block) $1;
2739 b.AddStatement ((Statement) $3);
2745 : FOREACH OPEN_PARENS type IDENTIFIER IN
2747 $1 = lexer.Location;
2749 expression CLOSE_PARENS
2752 Block foreach_block = new Block (current_block, true);
2753 LocalVariableReference v;
2755 foreach_block.AddVariable ((string) $3, (string) $4, (Location) $1);
2756 v = new LocalVariableReference (foreach_block, (string) $4);
2757 foreach_block.AddStatement (new Foreach ((string) $3, v, (Expression) $7,
2758 (Statement) $9, (Location) $1));
2765 | continue_statement
2774 $$ = new Break (lexer.Location);
2779 : CONTINUE SEMICOLON
2781 $$ = new Continue (lexer.Location);
2786 : GOTO IDENTIFIER SEMICOLON
2788 $$ = new Goto ((string) $2, lexer.Location);
2790 | GOTO CASE constant_expression SEMICOLON
2794 | GOTO DEFAULT SEMICOLON
2801 : RETURN opt_expression SEMICOLON
2803 $$ = new Return ((Expression) $2, lexer.Location);
2808 : THROW opt_expression SEMICOLON
2810 $$ = new Throw ((Expression) $2);
2820 : TRY block catch_clauses
2823 ArrayList s = new ArrayList ();
2825 foreach (Catch cc in (ArrayList) $3) {
2826 if (cc.Type == null)
2832 // Now s contains the list of specific catch clauses
2833 // and g contains the general one.
2835 $$ = new Try ((Block) $2, s, g, null);
2837 | TRY block opt_catch_clauses FINALLY block
2840 ArrayList s = new ArrayList ();
2842 foreach (Catch cc in (ArrayList) $3) {
2843 if (cc.Type == null)
2849 $$ = new Try ((Block) $2, s, g, (Block) $5);
2854 : /* empty */ { $$ = null; }
2861 ArrayList l = new ArrayList ();
2866 | catch_clauses catch_clause
2868 ArrayList l = (ArrayList) $1;
2876 : /* empty */ { $$ = null; }
2881 : CATCH opt_catch_args
2883 string type = null, id = null;
2886 DictionaryEntry cc = (DictionaryEntry) $2;
2887 type = (string) cc.Key;
2888 id = (string) cc.Value;
2891 ArrayList one = new ArrayList ();
2892 one.Add (new VariableDeclaration (id, null, lexer.Location));
2895 current_block = new Block (current_block);
2896 Block b = declare_local_variables (type, one);
2903 string type = null, id = null;
2906 DictionaryEntry cc = (DictionaryEntry) $2;
2907 type = (string) cc.Key;
2908 id = (string) cc.Value;
2911 while (current_block != (Block) $1)
2912 current_block = current_block.Parent;
2917 $$ = new Catch (type, id , (Block) $4);
2922 : /* empty */ { $$ = null; }
2927 : OPEN_PARENS type opt_identifier CLOSE_PARENS
2929 $$ = new DictionaryEntry ($2, $3);
2936 $$ = new Checked ((Block) $2);
2943 $$ = new Unchecked ((Block) $2);
2948 : LOCK OPEN_PARENS expression CLOSE_PARENS
2950 $1 = lexer.Location;
2954 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
2959 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
2961 $1 = lexer.Location;
2965 $$ = new Using ($3, (Statement) $6, (Location) $1);
2969 resource_acquisition
2970 : local_variable_declaration
2972 if ((Block) $1 != current_block){
2973 current_block.AddStatement ((Statement) $1);
2974 current_block = (Block) $1;
2983 // A class used to pass around variable declarations and constants
2985 public class VariableDeclaration {
2986 public string identifier;
2987 public object expression_or_array_initializer;
2988 public Location Location;
2990 public VariableDeclaration (string id, object eoai, Location l){
2991 this.identifier = id;
2992 this.expression_or_array_initializer = eoai;
2998 // A class used to hold info about an indexer declarator
3001 public class IndexerDeclaration {
3003 public string interface_type;
3004 public Parameters param_list;
3006 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
3009 this.interface_type = interface_type;
3010 this.param_list = param_list;
3015 // A class used to hold info about an operator declarator
3018 public class OperatorDeclaration {
3019 public Operator.OpType optype;
3020 public string ret_type;
3021 public string arg1type;
3022 public string arg1name;
3023 public string arg2type;
3024 public string arg2name;
3025 public Location location;
3027 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
3028 string arg2type, string arg2name, Location location)
3031 this.ret_type = ret_type;
3032 this.arg1type = arg1type;
3033 this.arg1name = arg1name;
3034 this.arg2type = arg2type;
3035 this.arg2name = arg2name;
3036 this.location = location;
3042 // Given the @class_name name, it creates a fully qualified name
3043 // based on the containing declaration space
3046 MakeName (string class_name)
3048 string ns = current_namespace.Name;
3049 string container_name = current_container.Name;
3051 if (container_name == ""){
3053 return ns + "." + class_name;
3057 return container_name + "." + class_name;
3061 // Used to report back to the user the result of a declaration
3062 // in the current declaration space
3065 CheckDef (DeclSpace.AdditionResult result, string name)
3067 if (result == DeclSpace.AdditionResult.Success)
3070 Location l = lexer.Location;
3073 case DeclSpace.AdditionResult.NameExists:
3074 Report.Error (102, l, "The namespace `" + current_container.Name +
3075 "' already contains a definition for `"+
3080 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
3082 // case DeclSpace.AdditionResult.MethodDuplicated:
3083 // error (111, "Class `"+current_container.Name+
3084 // "' already defines a member called '" +
3085 // name + "' with the same parameter types");
3088 case DeclSpace.AdditionResult.EnclosingClash:
3089 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3092 case DeclSpace.AdditionResult.NotAConstructor:
3093 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3099 CheckDef (bool result, string name)
3103 CheckDef (DeclSpace.AdditionResult.NameExists, name);
3107 SimpleLookup (string name)
3110 // we need to check against current_block not being null
3111 // as `expression' is allowed in argument_lists, which
3112 // do not exist inside a block.
3114 if (current_block != null){
3115 if (current_block.IsVariableDefined (name))
3116 return new LocalVariableReference (current_block, name);
3119 if (current_local_parameters != null){
3121 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
3123 return new ParameterReference (current_local_parameters, idx, name);
3129 Expression DecomposeQI (string name, Location l)
3133 if (name.IndexOf ('.') == -1){
3134 o = SimpleLookup (name);
3136 return new SimpleName (name, l);
3139 int pos = name.LastIndexOf (".");
3140 string left = name.Substring (0, pos);
3141 string right = name.Substring (pos + 1);
3143 o = DecomposeQI (left, l);
3144 return new MemberAccess (o, right, l);
3149 // This method is used to get at the complete string representation of
3150 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3151 // This is necessary because local_variable_type admits primary_expression
3152 // as the type of the variable. So we do some extra checking
3154 string GetQualifiedIdentifier (Expression expr)
3156 if (expr is SimpleName)
3157 return ((SimpleName)expr).Name;
3158 else if (expr is MemberAccess)
3159 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3161 throw new Exception ("Expr has to be either SimpleName or MemberAccess !");
3165 Block declare_local_variables (string type, ArrayList variable_declarators)
3167 Block implicit_block;
3168 ArrayList inits = null;
3171 // We use the `Used' property to check whether statements
3172 // have been added to the current block. If so, we need
3173 // to create another block to contain the new declaration
3174 // otherwise, as an optimization, we use the same block to
3175 // add the declaration.
3177 // FIXME: A further optimization is to check if the statements
3178 // that were added were added as part of the initialization
3179 // below. In which case, no other statements have been executed
3180 // and we might be able to reduce the number of blocks for
3181 // situations like this:
3183 // int j = 1; int k = j + 1;
3185 if (current_block.Used)
3186 implicit_block = new Block (current_block, true);
3188 implicit_block = current_block;
3190 foreach (VariableDeclaration decl in variable_declarators){
3191 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3192 if (decl.expression_or_array_initializer != null){
3194 inits = new ArrayList ();
3198 Location l = lexer.Location;
3199 Report.Error (128, l, "A local variable `" + decl.identifier +
3200 "' is already defined in this scope");
3205 return implicit_block;
3207 foreach (VariableDeclaration decl in inits){
3208 if (decl.expression_or_array_initializer is Expression){
3209 Expression expr = (Expression) decl.expression_or_array_initializer;
3212 assign = new Assign (new LocalVariableReference (
3213 implicit_block, decl.identifier),
3214 expr, lexer.Location);
3215 implicit_block.AddStatement (new StatementExpression (assign));
3217 Console.WriteLine ("Not handling Array initializers yet");
3221 return implicit_block;
3224 void CheckConstant (Expression expr)
3229 void CheckBoolean (Expression expr)
3234 void CheckAttributeTarget (string a)
3238 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3242 Location l = lexer.Location;
3243 Report.Error (658, l, "Invalid attribute target");
3249 void CheckUnaryOperator (Operator.OpType op)
3253 case Operator.OpType.LogicalNot:
3254 case Operator.OpType.OnesComplement:
3255 case Operator.OpType.Increment:
3256 case Operator.OpType.Decrement:
3257 case Operator.OpType.True:
3258 case Operator.OpType.False:
3259 case Operator.OpType.Addition:
3260 case Operator.OpType.Subtraction:
3265 Location l = lexer.Location;
3266 Report.Error (1019, l, "Overloadable unary operator expected");
3272 void CheckBinaryOperator (Operator.OpType op)
3276 case Operator.OpType.Addition:
3277 case Operator.OpType.Subtraction:
3278 case Operator.OpType.Multiply:
3279 case Operator.OpType.Division:
3280 case Operator.OpType.Modulus:
3281 case Operator.OpType.BitwiseAnd:
3282 case Operator.OpType.BitwiseOr:
3283 case Operator.OpType.ExclusiveOr:
3284 case Operator.OpType.LeftShift:
3285 case Operator.OpType.RightShift:
3286 case Operator.OpType.Equality:
3287 case Operator.OpType.Inequality:
3288 case Operator.OpType.GreaterThan:
3289 case Operator.OpType.LessThan:
3290 case Operator.OpType.GreaterThanOrEqual:
3291 case Operator.OpType.LessThanOrEqual:
3295 Location l = lexer.Location;
3296 Report.Error (1020, l, "Overloadable binary operator expected");
3302 void output (string s)
3304 Console.WriteLine (s);
3307 void note (string s)
3309 // Used to put annotations
3314 public Tokenizer Lexer {
3320 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
3322 current_namespace = new Namespace (null, "");
3324 this.tree = rc.Tree;
3327 current_container = tree.Types;
3328 current_container.Namespace = current_namespace;
3330 lexer = new Tokenizer (input, name);
3333 public override int parse ()
3335 StringBuilder value = new StringBuilder ();
3339 if (yacc_verbose_flag)
3340 yyparse (lexer, new yydebug.yyDebugSimple ());
3343 } catch (Exception e){
3344 // Console.WriteLine ("Fatal error: " + name);
3345 // Console.WriteLine (lexer.location);
3347 Console.WriteLine (lexer.location + " : Parsing error ");
3348 Console.WriteLine (e);
3352 return global_errors;