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
31 using System.Collections;
37 public class CSharpParser : GenericParser {
38 Namespace current_namespace;
39 TypeContainer current_container;
42 // Current block is used to add statements as we find
49 // Current interface is used by the various declaration
50 // productions in the interface declaration to "add"
51 // the interfaces as we find them.
53 Interface current_interface;
56 // This is used by the unary_expression code to resolve
57 // a name against a parameter.
59 Parameters current_local_parameters;
62 // Using during property parsing to describe the implicit
63 // value parameter that is passed to the "set" and "get"accesor
64 // methods (properties and indexers).
66 string implicit_value_parameter_type;
67 Parameters indexer_parameters;
70 // Used to determine if we are parsing the get/set pair
71 // of an indexer or a property
76 // Used to record all types defined
81 // An out-of-band stack.
90 %token NONE /* This token is never returned by our lexer */
91 %token ERROR // This is used not by the parser, but by the tokenizer.
95 *These are the C# keywords
175 /* C# keywords which are not really keywords */
179 /* C# single character operators/punctuation. */
180 %token OPEN_BRACE "{"
181 %token CLOSE_BRACE "}"
182 %token OPEN_BRACKET "["
183 %token CLOSE_BRACKET "]"
184 %token OPEN_PARENS "("
185 %token CLOSE_PARENS ")"
198 %token BITWISE_AND "&"
199 %token BITWISE_OR "|"
206 /* C# multi-character operators. */
209 %token OP_SHIFT_LEFT "<<"
210 %token OP_SHIFT_RIGHT ">>"
217 %token OP_MULT_ASSIGN "*="
218 %token OP_DIV_ASSIGN "/="
219 %token OP_MOD_ASSIGN "%="
220 %token OP_ADD_ASSIGN "+="
221 %token OP_SUB_ASSIGN "-="
222 %token OP_SHIFT_LEFT_ASSIGN "<<="
223 %token OP_SHIFT_RIGHT_ASSIGN ">>="
224 %token OP_AND_ASSIGN "&="
225 %token OP_XOR_ASSIGN "^="
226 %token OP_OR_ASSIGN "|="
230 %token LITERAL_INTEGER "int literal"
231 %token LITERAL_FLOAT "float literal"
232 %token LITERAL_DOUBLE "double literal"
233 %token LITERAL_DECIMAL "decimal literal"
234 %token LITERAL_CHARACTER "character literal"
235 %token LITERAL_STRING "string literal"
239 /* Add precedence rules to solve dangling else s/r conflict */
248 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
250 %left STAR DIV PERCENT
251 %right BANG CARRET UMINUS
252 %nonassoc OP_INC OP_DEC
254 %left OPEN_BRACKET OPEN_BRACE
258 %start compilation_unit
259 /*%start namespace_declaration */
263 : opt_using_directives opt_namespace_member_declarations EOF
265 // Check that using comes only before namespace elements
271 | using_directives using_directive
275 : using_alias_directive
276 | using_namespace_directive
279 using_alias_directive
280 : USING IDENTIFIER ASSIGN
281 namespace_or_type_name SEMICOLON
283 // FIXME : Need to implement actual action.
287 using_namespace_directive
288 : USING namespace_name SEMICOLON
290 current_namespace.Using ((string) $2);
294 // namespace_declarations
295 // : namespace_declaration
296 // | namespace_declarations namespace_declaration
298 namespace_declaration
299 : NAMESPACE qualified_identifier
301 current_namespace = tree.RecordNamespace (current_namespace, (string) $2);
303 namespace_body opt_semicolon
305 current_namespace = current_namespace.Parent;
321 | qualified_identifier DOT IDENTIFIER {
322 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
327 : namespace_or_type_name
333 opt_namespace_member_declarations
344 opt_namespace_member_declarations
346 | namespace_member_declarations
349 namespace_member_declarations
350 : namespace_member_declaration
351 | namespace_member_declarations namespace_member_declaration
354 namespace_member_declaration
361 Class c = (Class) $1;
362 mod_flags = c.ModFlags;
364 } else if ($1 is Struct){
365 Struct s = (Struct) $1;
366 mod_flags = s.ModFlags;
372 // We remove this error until we can
373 //if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
374 // error (1527, "Namespace elements cant be explicitly " +
375 // "declared private or protected in `" + name + "'");
378 | namespace_declaration
384 | interface_declaration
386 | delegate_declaration
393 : /* empty */ { $$ = null; }
394 | attribute_section opt_attributes
399 attrs = (Attributes) $2;
400 attrs.AddAttribute ((AttributeSection) $1);
402 attrs = new Attributes ((AttributeSection) $1);
409 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
411 string target = null;
414 target = (string) $2;
416 $$ = new AttributeSection (target, (ArrayList) $3);
418 | OPEN_BRACKET attribute_list CLOSE_BRACKET
420 $$ = new AttributeSection (null, (ArrayList) $2);
424 attribute_target_specifier
425 : attribute_target COLON
434 CheckAttributeTarget ((string) $1);
437 | EVENT { $$ = "event"; }
438 | RETURN { $$ = "return"; }
444 ArrayList attrs = new ArrayList ();
450 | attribute_list COMMA attribute
452 ArrayList attrs = (ArrayList) $1;
464 opt_attribute_arguments
466 $$ = new Attribute ((string) $1, (ArrayList) $3, (Location) $2);
471 : type_name { /* reserved attribute name or identifier: 17.4 */ }
474 opt_attribute_arguments
475 : /* empty */ { $$ = null; }
476 | OPEN_PARENS attribute_arguments CLOSE_PARENS
484 : positional_argument_list
486 ArrayList args = new ArrayList ();
491 | positional_argument_list COMMA named_argument_list
493 ArrayList args = new ArrayList ();
499 | named_argument_list
501 ArrayList args = new ArrayList ();
510 positional_argument_list
513 ArrayList args = new ArrayList ();
514 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
518 | positional_argument_list COMMA expression
520 ArrayList args = (ArrayList) $1;
521 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
530 ArrayList args = new ArrayList ();
535 | named_argument_list COMMA named_argument
537 ArrayList args = (ArrayList) $1;
545 : IDENTIFIER ASSIGN expression
547 $$ = new DictionaryEntry (
549 new Argument ((Expression) $3, Argument.AType.Expression));
555 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
558 opt_class_member_declarations
560 | class_member_declarations
563 class_member_declarations
564 : class_member_declaration
565 | class_member_declarations
566 class_member_declaration
569 class_member_declaration
570 : constant_declaration // done
571 | field_declaration // done
572 | method_declaration // done
573 | property_declaration // done
574 | event_declaration // done
575 | indexer_declaration // done
576 | operator_declaration // done
577 | constructor_declaration // done
578 | destructor_declaration // done
588 string full_struct_name = MakeName ((string) $4);
590 new_struct = new Struct (rc, current_container, full_struct_name, (int) $2,
591 (Attributes) $1, lexer.Location);
592 current_container = new_struct;
593 current_container.Namespace = current_namespace;
594 tree.RecordStruct (full_struct_name, new_struct);
596 opt_struct_interfaces
600 Struct new_struct = (Struct) current_container;
602 current_container = current_container.Parent;
603 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
608 opt_struct_interfaces
615 | struct_interfaces struct_interface
623 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
626 opt_struct_member_declarations
628 | struct_member_declarations
631 struct_member_declarations
632 : struct_member_declaration
633 | struct_member_declarations struct_member_declaration
636 struct_member_declaration
637 : constant_declaration
640 | property_declaration
642 | indexer_declaration
643 | operator_declaration
644 | constructor_declaration
656 foreach (VariableDeclaration constant in (ArrayList) $5){
657 Constant c = new Constant (
658 (string) $4, (string) constant.identifier,
659 (Expression) constant.expression_or_array_initializer, (int) $2, (Attributes) $1,
662 CheckDef (current_container.AddConstant (c), c.Name);
668 : constant_declarator
670 ArrayList constants = new ArrayList ();
674 | constant_declarators COMMA constant_declarator
676 ArrayList constants = (ArrayList) $1;
683 : IDENTIFIER ASSIGN constant_expression
685 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
696 string type = (string) $3;
699 foreach (VariableDeclaration var in (ArrayList) $4){
700 Field field = new Field (type, mod, var.identifier,
701 var.expression_or_array_initializer, (Attributes) $1, var.Location);
703 CheckDef (current_container.AddField (field), field.Name);
710 : variable_declarator
712 ArrayList decl = new ArrayList ();
716 | variable_declarators COMMA variable_declarator
718 ArrayList decls = (ArrayList) $1;
725 : IDENTIFIER ASSIGN variable_initializer
727 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
731 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
750 Method method = (Method) $1;
751 Block b = (Block) $2;
754 if ((method.ModFlags & (Modifiers.EXTERN | Modifiers.ABSTRACT)) == 0){
756 501, lexer.Location, "`" +
757 current_container.Name + "." + method.Name + "'" +
758 "must declare a body because it is not marked abstract or extern");
762 method.Block = (Block) $2;
763 CheckDef (current_container.AddMethod (method), method.Name);
765 current_local_parameters = null;
774 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
776 Method method = new Method ((string) $3, (int) $2, (string) $4,
777 (Parameters) $6, (Attributes) $1, lexer.Location);
779 current_local_parameters = (Parameters) $6;
787 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
789 Method method = new Method ("System.Void", (int) $2, (string) $4,
790 (Parameters) $6, (Attributes) $1, lexer.Location);
792 current_local_parameters = (Parameters) $6;
799 | SEMICOLON { $$ = null; }
802 opt_formal_parameter_list
803 : /* empty */ { $$ = Parameters.GetEmptyReadOnlyParameters (); }
804 | formal_parameter_list
807 formal_parameter_list
810 ArrayList pars_list = (ArrayList) $1;
812 Parameter [] pars = new Parameter [pars_list.Count];
813 pars_list.CopyTo (pars);
815 $$ = new Parameters (pars, null);
817 | fixed_parameters COMMA parameter_array
819 ArrayList pars_list = (ArrayList) $1;
821 Parameter [] pars = new Parameter [pars_list.Count];
822 pars_list.CopyTo (pars);
824 $$ = new Parameters (pars, (Parameter) $3);
828 $$ = new Parameters (null, (Parameter) $1);
835 ArrayList pars = new ArrayList ();
840 | fixed_parameters COMMA fixed_parameter
842 ArrayList pars = (ArrayList) $1;
851 opt_parameter_modifier
855 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
859 opt_parameter_modifier
860 : /* empty */ { $$ = Parameter.Modifier.NONE; }
865 : REF { $$ = Parameter.Modifier.REF; }
866 | OUT { $$ = Parameter.Modifier.OUT; }
870 : opt_attributes PARAMS type IDENTIFIER
872 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
873 note ("type must be a single-dimension array type");
878 : IDENTIFIER { $$ = $1.ToString (); }
879 | interface_type DOT IDENTIFIER { $$ = $1.ToString () + "." + $3.ToString (); }
888 implicit_value_parameter_type = (string) $3;
890 lexer.properties = true;
894 accessor_declarations
896 lexer.properties = false;
901 DictionaryEntry pair = (DictionaryEntry) $7;
902 Block get_block = null;
903 Block set_block = null;
905 if (pair.Key != null)
906 get_block = (Block) pair.Key;
907 if (pair.Value != null)
908 set_block = (Block) pair.Value;
910 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block,
911 (Attributes) $1, (Location) $6);
913 CheckDef (current_container.AddProperty (prop), prop.Name);
914 implicit_value_parameter_type = null;
918 accessor_declarations
919 : get_accessor_declaration opt_set_accessor_declaration
921 $$ = new DictionaryEntry ($1, $2);
923 | set_accessor_declaration opt_get_accessor_declaration
925 $$ = new DictionaryEntry ($2, $1);
929 opt_get_accessor_declaration
930 : /* empty */ { $$ = null; }
931 | get_accessor_declaration
934 opt_set_accessor_declaration
935 : /* empty */ { $$ = null; }
936 | set_accessor_declaration
939 get_accessor_declaration
942 // If this is not the case, then current_local_parameters has already
943 // been set in indexer_declaration
944 if (parsing_indexer == false)
945 current_local_parameters = null;
947 current_local_parameters = indexer_parameters;
954 current_local_parameters = null;
958 set_accessor_declaration
962 Parameter implicit_value_parameter = new Parameter (
963 implicit_value_parameter_type, "value",
964 Parameter.Modifier.NONE, null);
966 if (parsing_indexer == false) {
967 args = new Parameter [1];
968 args [0] = implicit_value_parameter;
970 Parameter [] fp = indexer_parameters.FixedParameters;
971 int count = fp.Length;
973 args = new Parameter [count + 1];
976 args [count] = implicit_value_parameter;
978 current_local_parameters = new Parameters (args, null);
983 current_local_parameters = null;
989 | SEMICOLON { $$ = new Block (null); }
992 interface_declaration
997 Interface new_interface;
998 string full_interface_name = MakeName ((string) $4);
1000 new_interface = new Interface (rc, current_container, full_interface_name, (int) $2,
1001 (Attributes) $1, lexer.Location);
1002 if (current_interface != null) {
1003 Location l = lexer.Location;
1004 Report.Error (-2, l, "Internal compiler error: interface inside interface");
1006 current_interface = new_interface;
1007 tree.RecordInterface (full_interface_name, new_interface);
1012 Interface new_interface = (Interface) current_interface;
1015 new_interface.Bases = (ArrayList) $6;
1017 current_interface = null;
1018 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
1023 : /* empty */ { $$ = null; }
1028 : COLON interface_type_list { $$ = $2; }
1034 ArrayList interfaces = new ArrayList ();
1036 interfaces.Add ($1);
1039 | interface_type_list COMMA interface_type
1041 ArrayList interfaces = (ArrayList) $1;
1042 interfaces.Add ($3);
1049 opt_interface_member_declarations
1053 opt_interface_member_declarations
1055 | interface_member_declarations
1058 interface_member_declarations
1059 : interface_member_declaration
1060 | interface_member_declarations interface_member_declaration
1063 interface_member_declaration
1064 : interface_method_declaration
1066 InterfaceMethod m = (InterfaceMethod) $1;
1068 CheckDef (current_interface.AddMethod (m), m.Name);
1070 | interface_property_declaration
1072 InterfaceProperty p = (InterfaceProperty) $1;
1074 CheckDef (current_interface.AddProperty (p), p.Name);
1076 | interface_event_declaration
1078 InterfaceEvent e = (InterfaceEvent) $1;
1080 CheckDef (current_interface.AddEvent (e), e.Name);
1082 | interface_indexer_declaration
1084 InterfaceIndexer i = (InterfaceIndexer) $1;
1086 CheckDef (current_interface.AddIndexer (i), "indexer");
1091 : /* empty */ { $$ = false; }
1092 | NEW { $$ = true; }
1095 interface_method_declaration
1096 : opt_attributes opt_new type IDENTIFIER
1097 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1100 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1102 | opt_attributes opt_new VOID IDENTIFIER
1103 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1106 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1110 interface_property_declaration
1115 { lexer.properties = true; }
1117 { lexer.properties = false; }
1122 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
1123 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1);
1128 : opt_attributes GET SEMICOLON { $$ = 1; }
1129 | opt_attributes SET SEMICOLON { $$ = 2; }
1130 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
1132 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1136 interface_event_declaration
1137 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1139 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1143 interface_indexer_declaration
1144 : opt_attributes opt_new type THIS
1145 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1147 { lexer.properties = true; }
1149 { lexer.properties = false; }
1152 int a_flags = (int) $10;
1154 bool do_get = (a_flags & 1) == 1;
1155 bool do_set = (a_flags & 2) == 2;
1157 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set, (bool) $2, (Attributes) $1);
1161 operator_declaration
1162 : opt_attributes opt_modifiers operator_declarator block
1164 OperatorDeclaration decl = (OperatorDeclaration) $3;
1166 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1167 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1169 // Note again, checking is done in semantic analysis
1170 current_container.AddOperator (op);
1172 current_local_parameters = null;
1177 : type OPERATOR overloadable_operator
1178 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1180 Operator.OpType op = (Operator.OpType) $3;
1181 CheckUnaryOperator (op);
1183 if (op == Operator.OpType.Addition)
1184 op = Operator.OpType.UnaryPlus;
1186 if (op == Operator.OpType.Subtraction)
1187 op = Operator.OpType.UnaryNegation;
1189 Parameter [] pars = new Parameter [1];
1191 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1193 current_local_parameters = new Parameters (pars, null);
1195 $$ = new OperatorDeclaration (op, (string) $1, (string) $5, (string) $6,
1196 null, null, lexer.Location);
1198 | type OPERATOR overloadable_operator
1200 type IDENTIFIER COMMA
1204 CheckBinaryOperator ((Operator.OpType) $3);
1206 Parameter [] pars = new Parameter [2];
1208 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1209 pars [1] = new Parameter ((string) $8, (string) $9, Parameter.Modifier.NONE, null);
1211 current_local_parameters = new Parameters (pars, null);
1213 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1214 (string) $8, (string) $9, lexer.Location);
1216 | conversion_operator_declarator
1219 overloadable_operator
1221 : BANG { $$ = Operator.OpType.LogicalNot; }
1222 | TILDE { $$ = Operator.OpType.OnesComplement; }
1223 | OP_INC { $$ = Operator.OpType.Increment; }
1224 | OP_DEC { $$ = Operator.OpType.Decrement; }
1225 | TRUE { $$ = Operator.OpType.True; }
1226 | FALSE { $$ = Operator.OpType.False; }
1227 // Unary and binary:
1228 | PLUS { $$ = Operator.OpType.Addition; }
1229 | MINUS { $$ = Operator.OpType.Subtraction; }
1231 | STAR { $$ = Operator.OpType.Multiply; }
1232 | DIV { $$ = Operator.OpType.Division; }
1233 | PERCENT { $$ = Operator.OpType.Modulus; }
1234 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1235 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1236 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1237 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1238 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1239 | OP_EQ { $$ = Operator.OpType.Equality; }
1240 | OP_NE { $$ = Operator.OpType.Inequality; }
1241 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1242 | OP_LT { $$ = Operator.OpType.LessThan; }
1243 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1244 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1247 conversion_operator_declarator
1248 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1250 Parameter [] pars = new Parameter [1];
1252 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1254 current_local_parameters = new Parameters (pars, null);
1256 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1257 null, null, lexer.Location);
1259 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1261 Parameter [] pars = new Parameter [1];
1263 pars [0] = new Parameter ((string) $5, (string) $6, Parameter.Modifier.NONE, null);
1265 current_local_parameters = new Parameters (pars, null);
1267 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1268 null, null, lexer.Location);
1272 constructor_declaration
1275 constructor_declarator
1278 Constructor c = (Constructor) $3;
1279 c.Block = (Block) $4;
1280 c.ModFlags = (int) $2;
1281 c.OptAttributes = (Attributes) $1;
1283 if ((c.ModFlags & Modifiers.STATIC) != 0){
1284 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1285 Location l = lexer.Location;
1286 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1289 if (c.Initializer != null){
1290 Location l = lexer.Location;
1291 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1294 if (!c.Parameters.Empty){
1295 Location l = lexer.Location;
1296 Report.Error (103, l, "Static constructors should not have parameters");
1300 CheckDef (current_container.AddConstructor (c), c.Name);
1302 current_local_parameters = null;
1306 constructor_declarator
1308 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1309 opt_constructor_initializer
1311 Location l = lexer.Location;
1313 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5, l);
1315 current_local_parameters = (Parameters) $3;
1319 opt_constructor_initializer
1320 : /* empty */ { $$ = null; }
1321 | constructor_initializer
1324 constructor_initializer
1325 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1327 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1329 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1331 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1335 destructor_declaration
1336 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1339 // FIXME: The destructor needs to generate special code:
1345 // void Finalize () {
1346 // try { code } finally { base.Finalize (); }
1348 Method d = new Method ("System.Void", 0, "Finalize",
1349 new Parameters (null, null), (Attributes) $1, lexer.Location);
1351 d.Block = (Block) $6;
1352 CheckDef (current_container.AddMethod (d), d.Name);
1359 EVENT type variable_declarators SEMICOLON
1361 foreach (VariableDeclaration var in (ArrayList) $5) {
1363 // FIXME : Is this right ?
1364 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1365 (int) $2, null, null, (Attributes) $1, lexer.Location);
1367 CheckDef (current_container.AddEvent (e), e.Name);
1373 EVENT type member_name
1374 OPEN_BRACE event_accessor_declarations CLOSE_BRACE
1376 DictionaryEntry pair = (DictionaryEntry) $7;
1377 Block add_block = null;
1378 Block rem_block = null;
1380 if (pair.Key != null)
1381 add_block = (Block) pair.Key;
1382 if (pair.Value != null)
1383 rem_block = (Block) pair.Value;
1385 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
1386 (Attributes) $1, lexer.Location);
1388 CheckDef (current_container.AddEvent (e), e.Name);
1392 event_accessor_declarations
1393 : add_accessor_declaration remove_accessor_declaration
1395 $$ = new DictionaryEntry ($1, $2);
1397 | remove_accessor_declaration add_accessor_declaration
1399 $$ = new DictionaryEntry ($2, $1);
1403 add_accessor_declaration
1404 : opt_attributes ADD block
1410 remove_accessor_declaration
1411 : opt_attributes REMOVE block
1418 : opt_attributes opt_modifiers indexer_declarator
1421 IndexerDeclaration decl = (IndexerDeclaration) $3;
1423 implicit_value_parameter_type = decl.type;
1425 lexer.properties = true;
1426 parsing_indexer = true;
1428 indexer_parameters = decl.param_list;
1429 $$ = lexer.Location;
1431 accessor_declarations
1433 lexer.properties = false;
1434 parsing_indexer = false;
1438 // The signature is computed from the signature of the indexer. Look
1439 // at section 3.6 on the spec
1442 IndexerDeclaration decl = (IndexerDeclaration) $3;
1443 DictionaryEntry pair = (DictionaryEntry) $6;
1444 Block get_block = null;
1445 Block set_block = null;
1447 if (pair.Key != null)
1448 get_block = (Block) pair.Key;
1449 if (pair.Value != null)
1450 set_block = (Block) pair.Value;
1452 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1453 get_block, set_block, (Attributes) $1, (Location) $5);
1455 // Note that there is no equivalent of CheckDef for this case
1456 // We shall handle this in semantic analysis
1458 current_container.AddIndexer (indexer);
1460 current_local_parameters = null;
1461 implicit_value_parameter_type = null;
1462 indexer_parameters = null;
1467 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1469 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
1471 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1473 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
1485 string name = (string) $4;
1486 Enum e = new Enum (rc, (string) $5, (int) $2, name, (Attributes) $1, lexer.Location);
1488 foreach (VariableDeclaration ev in (ArrayList) $6){
1489 CheckDef (e.AddEnumMember (ev.identifier,
1490 (Expression) ev.expression_or_array_initializer,
1491 (Location) ev.Location),
1495 CheckDef (current_container.AddEnum (e), name);
1500 : /* empty */ { $$ = "System.Int32"; }
1501 | COLON type { $$ = $2; }
1505 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1511 opt_enum_member_declarations
1512 : /* empty */ { $$ = new ArrayList (); }
1513 | enum_member_declarations opt_comma { $$ = $1; }
1516 enum_member_declarations
1517 : enum_member_declaration
1519 ArrayList l = new ArrayList ();
1524 | enum_member_declarations COMMA enum_member_declaration
1526 ArrayList l = (ArrayList) $1;
1534 enum_member_declaration
1535 : opt_attributes IDENTIFIER
1537 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1539 | opt_attributes IDENTIFIER
1541 $$ = lexer.Location;
1545 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location);
1549 delegate_declaration
1553 IDENTIFIER OPEN_PARENS
1554 formal_parameter_list
1558 Delegate del = new Delegate (rc, (string) $4, (int) $2,
1559 MakeName ((string) $5), (Parameters) $7,
1560 (Attributes) $1, lexer.Location);
1562 CheckDef (current_container.AddDelegate (del), del.Name);
1567 IDENTIFIER OPEN_PARENS
1568 formal_parameter_list
1572 Delegate del = new Delegate (rc, "System.Void", (int) $2, (string) $5, (Parameters) $7,
1573 (Attributes) $1, lexer.Location);
1575 CheckDef (current_container.AddDelegate (del), del.Name);
1580 : namespace_or_type_name
1583 namespace_or_type_name
1584 : qualified_identifier
1588 * Before you think of adding a return_type, notice that we have been
1589 * using two rules in the places where it matters (one rule using type
1590 * and another identical one that uses VOID as the return type). This
1591 * gets rid of a shift/reduce couple
1594 : type_name { /* class_type */
1596 This does interfaces, delegates, struct_types, class_types,
1597 parent classes, and more! 4.2
1608 ArrayList types = new ArrayList ();
1613 | type_list COMMA type
1615 ArrayList types = (ArrayList) $1;
1623 * replaces all the productions for isolating the various
1624 * simple types, but we need this to reuse it easily in local_variable_type
1627 : OBJECT { $$ = "System.Object"; }
1628 | STRING { $$ = "System.String"; }
1629 | BOOL { $$ = "System.Boolean"; }
1630 | DECIMAL { $$ = "System.Decimal"; }
1631 | FLOAT { $$ = "System.Single"; }
1632 | DOUBLE { $$ = "System.Double"; }
1637 : SBYTE { $$ = "System.SByte"; }
1638 | BYTE { $$ = "System.Byte"; }
1639 | SHORT { $$ = "System.Int16"; }
1640 | USHORT { $$ = "System.UInt16"; }
1641 | INT { $$ = "System.Int32"; }
1642 | UINT { $$ = "System.UInt32"; }
1643 | LONG { $$ = "System.Int64"; }
1644 | ULONG { $$ = "System.UInt64"; }
1645 | CHAR { $$ = "System.Char"; }
1653 : type rank_specifiers
1655 $$ = (string) $1 + (string) $2;
1660 // Expressions, section 7.5
1668 | qualified_identifier
1670 string name = (string) $1;
1673 $$ = DecomposeQI (name, lexer.Location);
1675 | parenthesized_expression
1677 | invocation_expression
1681 | post_increment_expression
1682 | post_decrement_expression
1686 | checked_expression
1687 | unchecked_expression
1694 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1695 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1696 | NULL { $$ = new NullLiteral (); }
1700 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1701 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1702 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1707 object v = lexer.Value;
1710 // FIXME: Possible optimization would be to
1711 // compute the *Literal objects directly in the scanner
1714 $$ = new IntLiteral ((Int32) v);
1716 $$ = new UIntLiteral ((UInt32) v);
1718 $$ = new LongLiteral ((Int64) v);
1719 else if (v is ulong)
1720 $$ = new ULongLiteral ((UInt64) v);
1722 Console.WriteLine ("OOPS. Unexpected result from scanner");
1727 : TRUE { $$ = new BoolLiteral (true); }
1728 | FALSE { $$ = new BoolLiteral (false); }
1731 parenthesized_expression
1732 : OPEN_PARENS expression CLOSE_PARENS
1737 : primary_expression DOT IDENTIFIER
1739 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1741 | predefined_type DOT IDENTIFIER
1743 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1751 invocation_expression
1752 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1755 // if $1 is MethodGroup
1756 // $$ = new Call ($1, $3);
1758 // $$ = new DelegateCall ($1, $3);
1760 Location l = lexer.Location;
1761 Report.Error (1, l, "THIS IS CRAZY");
1763 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1769 : /* empty */ { $$ = null; }
1776 ArrayList list = new ArrayList ();
1780 | argument_list COMMA argument
1782 ArrayList list = (ArrayList) $1;
1791 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1793 | REF variable_reference
1795 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1797 | OUT variable_reference
1799 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1804 : expression { note ("section 5.4"); $$ = $1; }
1808 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1810 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1817 ArrayList list = new ArrayList ();
1821 | expression_list COMMA expression
1823 ArrayList list = (ArrayList) $1;
1832 $$ = new This (lexer.Location);
1837 : BASE DOT IDENTIFIER
1839 $$ = new BaseAccess (BaseAccess.BaseAccessType.Member, (string) $3, null);
1841 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1843 $$ = new BaseAccess (BaseAccess.BaseAccessType.Indexer, null, (ArrayList) $3);
1847 post_increment_expression
1848 : primary_expression OP_INC
1850 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
1851 (Expression) $1, lexer.Location);
1855 post_decrement_expression
1856 : primary_expression OP_DEC
1858 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
1859 (Expression) $1, lexer.Location);
1864 : object_or_delegate_creation_expression
1865 | array_creation_expression
1868 object_or_delegate_creation_expression
1869 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1871 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1875 array_creation_expression
1876 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
1878 opt_array_initializer
1880 $$ = new ArrayCreation ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7,
1883 | NEW type rank_specifiers array_initializer
1885 $$ = new ArrayCreation ((string) $2, (string) $3, (ArrayList) $4, lexer.Location);
1905 | rank_specifiers rank_specifier
1907 $$ = (string) $2 + (string) $1;
1912 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1914 $$ = "[" + (string) $2 + "]";
1934 | dim_separators COMMA
1936 $$ = (string) $1 + ",";
1940 opt_array_initializer
1952 : OPEN_BRACE CLOSE_BRACE
1954 ArrayList list = new ArrayList ();
1957 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1959 $$ = (ArrayList) $2;
1963 variable_initializer_list
1964 : variable_initializer
1966 ArrayList list = new ArrayList ();
1970 | variable_initializer_list COMMA variable_initializer
1972 ArrayList list = (ArrayList) $1;
1979 : TYPEOF OPEN_PARENS type CLOSE_PARENS
1981 $$ = new TypeOf ((string) $3);
1986 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
1987 $$ = new SizeOf ((string) $3);
1989 note ("Verify type is unmanaged");
1990 note ("if (5.8) builtin, yield constant expression");
1995 : CHECKED OPEN_PARENS expression CLOSE_PARENS
1997 $$ = new CheckedExpr ((Expression) $3);
2001 unchecked_expression
2002 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2004 $$ = new UnCheckedExpr ((Expression) $3);
2009 : primary_expression
2010 | PLUS unary_expression
2012 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2014 | MINUS unary_expression
2016 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2018 | BANG unary_expression
2020 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2022 | TILDE unary_expression
2024 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2026 | STAR unary_expression
2028 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2030 | BITWISE_AND unary_expression
2032 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2034 | OP_INC unary_expression
2036 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2037 (Expression) $2, lexer.Location);
2039 | OP_DEC unary_expression
2041 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2042 (Expression) $2, lexer.Location);
2046 we can not do cast expressions at this level,
2047 as there is an ambiguity. Check "Cast Expressions" 7.6.8
2048 for the recipe to handle this.
2052 pre_increment_expression
2053 : OP_INC unary_expression
2055 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2056 (Expression) $2, lexer.Location);
2060 pre_decrement_expression
2061 : OP_DEC unary_expression
2063 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2064 (Expression) $2, lexer.Location);
2070 * FIXME: This is actually wrong, it should be `type' but that
2071 * introduces a lot of {shift,reduce}/reduces
2073 * This is really really wrong. We need to track down
2074 * the source of problems with QIs because expressions like:
2075 * foreach (string s in (string []) object) wont be parsed.
2077 : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
2079 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
2081 | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
2083 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
2087 multiplicative_expression
2089 | multiplicative_expression STAR unary_expression
2091 $$ = new Binary (Binary.Operator.Multiply,
2092 (Expression) $1, (Expression) $3, lexer.Location);
2094 | multiplicative_expression DIV unary_expression
2096 $$ = new Binary (Binary.Operator.Division,
2097 (Expression) $1, (Expression) $3, lexer.Location);
2099 | multiplicative_expression PERCENT unary_expression
2101 $$ = new Binary (Binary.Operator.Modulus,
2102 (Expression) $1, (Expression) $3, lexer.Location);
2107 : multiplicative_expression
2108 | additive_expression PLUS multiplicative_expression
2110 $$ = new Binary (Binary.Operator.Addition,
2111 (Expression) $1, (Expression) $3, lexer.Location);
2113 | additive_expression MINUS multiplicative_expression
2115 $$ = new Binary (Binary.Operator.Subtraction,
2116 (Expression) $1, (Expression) $3, lexer.Location);
2121 : additive_expression
2122 | shift_expression OP_SHIFT_LEFT additive_expression
2124 $$ = new Binary (Binary.Operator.LeftShift,
2125 (Expression) $1, (Expression) $3, lexer.Location);
2127 | shift_expression OP_SHIFT_RIGHT additive_expression
2129 $$ = new Binary (Binary.Operator.RightShift,
2130 (Expression) $1, (Expression) $3, lexer.Location);
2134 relational_expression
2136 | relational_expression OP_LT shift_expression
2138 $$ = new Binary (Binary.Operator.LessThan,
2139 (Expression) $1, (Expression) $3, lexer.Location);
2141 | relational_expression OP_GT shift_expression
2143 $$ = new Binary (Binary.Operator.GreaterThan,
2144 (Expression) $1, (Expression) $3, lexer.Location);
2146 | relational_expression OP_LE shift_expression
2148 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2149 (Expression) $1, (Expression) $3, lexer.Location);
2151 | relational_expression OP_GE shift_expression
2153 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2154 (Expression) $1, (Expression) $3, lexer.Location);
2156 | relational_expression IS type
2158 $$ = new Probe (Probe.Operator.Is,
2159 (Expression) $1, (string) $3);
2161 | relational_expression AS type
2163 $$ = new Probe (Probe.Operator.As,
2164 (Expression) $1, (string) $3);
2169 : relational_expression
2170 | equality_expression OP_EQ relational_expression
2172 $$ = new Binary (Binary.Operator.Equality,
2173 (Expression) $1, (Expression) $3, lexer.Location);
2175 | equality_expression OP_NE relational_expression
2177 $$ = new Binary (Binary.Operator.Inequality,
2178 (Expression) $1, (Expression) $3, lexer.Location);
2183 : equality_expression
2184 | and_expression BITWISE_AND equality_expression
2186 $$ = new Binary (Binary.Operator.BitwiseAnd,
2187 (Expression) $1, (Expression) $3, lexer.Location);
2191 exclusive_or_expression
2193 | exclusive_or_expression CARRET and_expression
2195 $$ = new Binary (Binary.Operator.ExclusiveOr,
2196 (Expression) $1, (Expression) $3, lexer.Location);
2200 inclusive_or_expression
2201 : exclusive_or_expression
2202 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2204 $$ = new Binary (Binary.Operator.BitwiseOr,
2205 (Expression) $1, (Expression) $3, lexer.Location);
2209 conditional_and_expression
2210 : inclusive_or_expression
2211 | conditional_and_expression OP_AND inclusive_or_expression
2213 $$ = new Binary (Binary.Operator.LogicalAnd,
2214 (Expression) $1, (Expression) $3, lexer.Location);
2218 conditional_or_expression
2219 : conditional_and_expression
2220 | conditional_or_expression OP_OR conditional_and_expression
2222 $$ = new Binary (Binary.Operator.LogicalOr,
2223 (Expression) $1, (Expression) $3, lexer.Location);
2227 conditional_expression
2228 : conditional_or_expression
2229 | conditional_or_expression INTERR expression COLON expression
2231 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2235 assignment_expression
2236 : unary_expression ASSIGN expression
2238 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2240 | unary_expression OP_MULT_ASSIGN expression
2242 Location l = lexer.Location;
2244 $$ = new Assign ((Expression) $1,
2245 new Binary (Binary.Operator.Multiply,
2247 (Expression) $3, l), l);
2249 | unary_expression OP_DIV_ASSIGN expression
2251 Location l = lexer.Location;
2253 $$ = new Assign ((Expression) $1,
2254 new Binary (Binary.Operator.Division,
2256 (Expression) $3, l), l);
2258 | unary_expression OP_MOD_ASSIGN expression
2260 Location l = lexer.Location;
2262 $$ = new Assign ((Expression) $1,
2263 new Binary (Binary.Operator.Modulus,
2265 (Expression) $3, l), l);
2267 | unary_expression OP_ADD_ASSIGN expression
2269 Location l = lexer.Location;
2271 $$ = new Assign ((Expression) $1,
2272 new Binary (Binary.Operator.Addition,
2274 (Expression) $3, l), l);
2276 | unary_expression OP_SUB_ASSIGN expression
2278 Location l = lexer.Location;
2280 $$ = new Assign ((Expression) $1,
2281 new Binary (Binary.Operator.Subtraction,
2283 (Expression) $3, l), l);
2285 | unary_expression OP_SHIFT_LEFT_ASSIGN expression
2287 Location l = lexer.Location;
2289 $$ = new Assign ((Expression) $1,
2290 new Binary (Binary.Operator.LeftShift,
2292 (Expression) $3, l), l);
2294 | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2296 Location l = lexer.Location;
2298 $$ = new Assign ((Expression) $1,
2299 new Binary (Binary.Operator.RightShift,
2301 (Expression) $3, l), l);
2303 | unary_expression OP_AND_ASSIGN expression
2305 Location l = lexer.Location;
2307 $$ = new Assign ((Expression) $1,
2308 new Binary (Binary.Operator.BitwiseAnd,
2310 (Expression) $3, l), l);
2312 | unary_expression OP_OR_ASSIGN expression
2314 Location l = lexer.Location;
2316 $$ = new Assign ((Expression) $1,
2317 new Binary (Binary.Operator.BitwiseOr,
2319 (Expression) $3, l), l);
2321 | unary_expression OP_XOR_ASSIGN expression
2323 Location l = lexer.Location;
2325 $$ = new Assign ((Expression) $1,
2326 new Binary (Binary.Operator.ExclusiveOr,
2328 (Expression) $3, l), l);
2333 : conditional_expression
2334 | assignment_expression
2342 : expression { CheckBoolean ((Expression) $1); $$ = $1; }
2354 string full_class_name = MakeName ((string) $4);
2356 new_class = new Class (rc, current_container, full_class_name, (int) $2,
2357 (Attributes) $1, lexer.Location);
2358 current_container = new_class;
2359 current_container.Namespace = current_namespace;
2360 tree.RecordClass (full_class_name, new_class);
2366 Class new_class = (Class) current_container;
2369 new_class.Bases = (ArrayList) $6;
2371 current_container = current_container.Parent;
2372 CheckDef (current_container.AddClass (new_class), new_class.Name);
2379 : /* empty */ { $$ = (int) 0; }
2385 | modifiers modifier
2390 if ((m1 & m2) != 0) {
2391 Location l = lexer.Location;
2392 Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2394 $$ = (int) (m1 | m2);
2399 : NEW { $$ = Modifiers.NEW; }
2400 | PUBLIC { $$ = Modifiers.PUBLIC; }
2401 | PROTECTED { $$ = Modifiers.PROTECTED; }
2402 | INTERNAL { $$ = Modifiers.INTERNAL; }
2403 | PRIVATE { $$ = Modifiers.PRIVATE; }
2404 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2405 | SEALED { $$ = Modifiers.SEALED; }
2406 | STATIC { $$ = Modifiers.STATIC; }
2407 | READONLY { $$ = Modifiers.READONLY; }
2408 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2409 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2410 | EXTERN { $$ = Modifiers.EXTERN; }
2414 : /* empty */ { $$ = null; }
2415 | class_base { $$ = $1; }
2419 : COLON type_list { $$ = $2; }
2427 // A block is "contained" on the following places:
2429 // property_declaration as part of the accessor body (get/set)
2430 // operator_declaration
2431 // constructor_declaration
2432 // destructor_declaration
2433 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2438 current_block = new Block (current_block);
2440 opt_statement_list CLOSE_BRACE
2442 while (current_block.Implicit)
2443 current_block = current_block.Parent;
2445 current_block = current_block.Parent;
2456 | statement_list statement
2460 : declaration_statement
2462 if ((Block) $1 != current_block){
2463 current_block.AddStatement ((Statement) $1);
2464 current_block = (Block) $1;
2467 | embedded_statement
2469 Statement s = (Statement) $1;
2472 current_block.AddStatement ((Statement) $1);
2476 current_block.AddStatement ((Statement) $1);
2483 | expression_statement
2484 | selection_statement
2485 | iteration_statement
2489 | unchecked_statement
2497 $$ = new EmptyStatement ();
2502 : IDENTIFIER COLON statement
2504 string lab = (String) $1;
2507 block = new Block (current_block, lab);
2508 block.AddStatement ((Statement) $3);
2511 if (!current_block.AddLabel (lab, block)){
2512 Location l = lexer.Location;
2513 Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2519 declaration_statement
2520 : local_variable_declaration SEMICOLON
2522 DictionaryEntry de = (DictionaryEntry) $1;
2524 $$ = declare_local_variables ((string) de.Key, (ArrayList) de.Value);
2527 | local_constant_declaration SEMICOLON // finishme
2531 * The following is from Rhys' grammar:
2532 * > Types in local variable declarations must be recognized as
2533 * > expressions to prevent reduce/reduce errors in the grammar.
2534 * > The expressions are converted into types during semantic analysis.
2537 : primary_expression opt_rank_specifier
2539 // FIXME: Do something smart here regarding the composition of the type.
2541 // Ok, the above "primary_expression" is there to get rid of
2542 // both reduce/reduce and shift/reduces in the grammar, it should
2543 // really just be "type_name". If you use type_name, a reduce/reduce
2544 // creeps up. If you use qualified_identifier (which is all we need
2545 // really) two shift/reduces appear.
2548 // So the super-trick is that primary_expression
2549 // can only be either a SimpleName or a MemberAccess.
2550 // The MemberAccess case arises when you have a fully qualified type-name like :
2552 // SimpleName is when you have
2555 Expression expr = (Expression) $1;
2556 if (!(expr is SimpleName || expr is MemberAccess)) {
2557 Location l = lexer.Location;
2558 Report.Error (-1, l, "Invalid Type definition");
2559 $$ = "System.Object";
2563 // So we extract the string corresponding to the SimpleName
2566 $$ = GetQualifiedIdentifier (expr) + (string) $2;
2568 | builtin_types opt_rank_specifier
2570 $$ = (string) $1 + (string) $2;
2574 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2577 // // FIXME: this is a string that represents the type
2578 // // Figure out something to make this work.
2583 local_variable_declaration
2584 : local_variable_type variable_declarators
2586 $$ = new DictionaryEntry ($1, $2);
2590 local_constant_declaration
2591 : CONST type constant_declarator
2595 expression_statement
2596 : statement_expression SEMICOLON
2603 // We have to do the wrapping here and not in the case above,
2604 // because statement_expression is used for example in for_statement
2606 statement_expression
2607 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2608 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2609 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2610 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2611 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2612 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2613 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2616 object_creation_expression
2617 : object_or_delegate_creation_expression
2618 { note ("complain if this is a delegate maybe?"); }
2627 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2630 $$ = new If ((Expression) $3, (Statement) $5);
2632 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2633 embedded_statement ELSE embedded_statement
2635 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2640 : SWITCH OPEN_PARENS expression CLOSE_PARENS
2643 $$ = new Switch ((Expression) $3, (ArrayList) $5);
2657 : /* empty */ { $$ = new ArrayList (); }
2664 ArrayList sections = new ArrayList ();
2669 | switch_sections switch_section
2671 ArrayList sections = (ArrayList) $1;
2681 current_block = new Block (current_block);
2685 while (current_block.Implicit)
2686 current_block = current_block.Parent;
2687 $$ = new SwitchSection ((ArrayList) $1, current_block);
2688 current_block = current_block.Parent;
2695 ArrayList labels = new ArrayList ();
2700 | switch_labels switch_label
2702 ArrayList labels = (ArrayList) ($1);
2710 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2); }
2711 | DEFAULT COLON { $$ = new SwitchLabel (null); }
2722 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2724 $$ = new While ((Expression) $3, (Statement) $5);
2729 : DO embedded_statement
2730 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2732 $$ = new Do ((Statement) $2, (Expression) $5);
2738 opt_for_initializer SEMICOLON
2740 Block assign_block = new Block (current_block);
2741 current_block = assign_block;
2743 if ($3 is DictionaryEntry){
2744 DictionaryEntry de = (DictionaryEntry) $3;
2747 string type = (string) de.Key;
2748 ArrayList var_declarators = (ArrayList) de.Value;
2750 foreach (VariableDeclaration decl in var_declarators){
2751 if (!current_block.AddVariable (type, decl.identifier, decl.Location)){
2752 Report.Error (128, decl.Location,
2753 "A local variable `" + decl.identifier + "' is already" +
2754 "defined in this scope");
2758 foreach (VariableDeclaration decl in var_declarators){
2759 if (decl.expression_or_array_initializer is Expression){
2760 Expression expr = (Expression) decl.expression_or_array_initializer;
2761 Assign a = new Assign (
2762 new LocalVariableReference (
2763 assign_block, decl.identifier),
2764 expr, decl.Location);
2766 assign_block.AddStatement (new StatementExpression (a));
2768 Console.WriteLine ("Not handling array initializers, strange");
2774 opt_for_condition SEMICOLON
2775 opt_for_iterator CLOSE_PARENS
2778 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10);
2780 current_block.AddStatement (f);
2781 while (current_block.Implicit)
2782 current_block = current_block.Parent;
2784 current_block = current_block.Parent;
2789 : /* empty */ { $$ = new EmptyStatement (); }
2794 : local_variable_declaration
2795 | statement_expression_list
2799 : /* empty */ { $$ = new BoolLiteral (true); }
2800 | boolean_expression
2804 : /* empty */ { $$ = new EmptyStatement (); }
2809 : statement_expression_list
2812 statement_expression_list
2813 : statement_expression
2815 Block b = new Block (null, true);
2817 b.AddStatement ((Statement) $1);
2820 | statement_expression_list COMMA statement_expression
2822 Block b = (Block) $1;
2824 b.AddStatement ((Statement) $3);
2830 : FOREACH OPEN_PARENS type IDENTIFIER IN
2832 oob_stack.Push (lexer.Location);
2834 expression CLOSE_PARENS
2836 Block foreach_block = new Block (current_block, true);
2837 LocalVariableReference v;
2839 foreach_block.AddVariable ((string) $3, (string) $4, lexer.Location);
2840 v = new LocalVariableReference (foreach_block, (string) $4);
2842 current_block.AddStatement (foreach_block);
2843 current_block = foreach_block;
2845 oob_stack.Push (foreach_block);
2850 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
2851 Block foreach_block = (Block) oob_stack.Pop ();
2852 Location l = (Location) oob_stack.Pop ();
2854 $$ = new Foreach ((string) $3, v, (Expression) $7, (Statement) $10, l);
2860 | continue_statement
2869 $$ = new Break (lexer.Location);
2874 : CONTINUE SEMICOLON
2876 $$ = new Continue (lexer.Location);
2881 : GOTO IDENTIFIER SEMICOLON
2883 $$ = new Goto ((string) $2, lexer.Location);
2885 | GOTO CASE constant_expression SEMICOLON
2889 | GOTO DEFAULT SEMICOLON
2896 : RETURN opt_expression SEMICOLON
2898 $$ = new Return ((Expression) $2, lexer.Location);
2903 : THROW opt_expression SEMICOLON
2905 $$ = new Throw ((Expression) $2);
2915 : TRY block catch_clauses
2918 ArrayList s = new ArrayList ();
2920 foreach (Catch cc in (ArrayList) $3) {
2921 if (cc.Type == null)
2927 // Now s contains the list of specific catch clauses
2928 // and g contains the general one.
2930 $$ = new Try ((Block) $2, s, g, null);
2932 | TRY block opt_catch_clauses FINALLY block
2935 ArrayList s = new ArrayList ();
2937 foreach (Catch cc in (ArrayList) $3) {
2938 if (cc.Type == null)
2944 $$ = new Try ((Block) $2, s, g, (Block) $5);
2949 : /* empty */ { $$ = null; }
2956 ArrayList l = new ArrayList ();
2961 | catch_clauses catch_clause
2963 ArrayList l = (ArrayList) $1;
2971 : /* empty */ { $$ = null; }
2976 : CATCH opt_catch_args
2978 string type = null, id = null;
2981 DictionaryEntry cc = (DictionaryEntry) $2;
2982 type = (string) cc.Key;
2983 id = (string) cc.Value;
2986 ArrayList one = new ArrayList ();
2987 one.Add (new VariableDeclaration (id, null, lexer.Location));
2990 current_block = new Block (current_block);
2991 Block b = declare_local_variables (type, one);
2998 string type = null, id = null;
3001 DictionaryEntry cc = (DictionaryEntry) $2;
3002 type = (string) cc.Key;
3003 id = (string) cc.Value;
3006 while (current_block != (Block) $1)
3007 current_block = current_block.Parent;
3012 $$ = new Catch (type, id , (Block) $4);
3017 : /* empty */ { $$ = null; }
3022 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3024 $$ = new DictionaryEntry ($2, $3);
3031 $$ = new Checked ((Block) $2);
3038 $$ = new Unchecked ((Block) $2);
3043 : LOCK OPEN_PARENS expression CLOSE_PARENS
3049 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3054 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3059 $$ = new Using ($3, (Statement) $6, lexer.Location);
3063 resource_acquisition
3064 : local_variable_declaration
3066 if ((Block) $1 != current_block){
3067 current_block.AddStatement ((Statement) $1);
3068 current_block = (Block) $1;
3077 // A class used to pass around variable declarations and constants
3079 public class VariableDeclaration {
3080 public string identifier;
3081 public object expression_or_array_initializer;
3082 public Location Location;
3084 public VariableDeclaration (string id, object eoai, Location l){
3085 this.identifier = id;
3086 this.expression_or_array_initializer = eoai;
3092 // A class used to hold info about an indexer declarator
3095 public class IndexerDeclaration {
3097 public string interface_type;
3098 public Parameters param_list;
3100 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
3103 this.interface_type = interface_type;
3104 this.param_list = param_list;
3109 // A class used to hold info about an operator declarator
3112 public class OperatorDeclaration {
3113 public Operator.OpType optype;
3114 public string ret_type;
3115 public string arg1type;
3116 public string arg1name;
3117 public string arg2type;
3118 public string arg2name;
3119 public Location location;
3121 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
3122 string arg2type, string arg2name, Location location)
3125 this.ret_type = ret_type;
3126 this.arg1type = arg1type;
3127 this.arg1name = arg1name;
3128 this.arg2type = arg2type;
3129 this.arg2name = arg2name;
3130 this.location = location;
3136 // Given the @class_name name, it creates a fully qualified name
3137 // based on the containing declaration space
3140 MakeName (string class_name)
3142 string ns = current_namespace.Name;
3143 string container_name = current_container.Name;
3145 if (container_name == ""){
3147 return ns + "." + class_name;
3151 return container_name + "." + class_name;
3155 // Used to report back to the user the result of a declaration
3156 // in the current declaration space
3159 CheckDef (DeclSpace.AdditionResult result, string name)
3161 if (result == DeclSpace.AdditionResult.Success)
3164 Location l = lexer.Location;
3167 case DeclSpace.AdditionResult.NameExists:
3168 Report.Error (102, l, "The namespace `" + current_container.Name +
3169 "' already contains a definition for `"+
3174 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
3176 // case DeclSpace.AdditionResult.MethodDuplicated:
3177 // error (111, "Class `"+current_container.Name+
3178 // "' already defines a member called '" +
3179 // name + "' with the same parameter types");
3182 case DeclSpace.AdditionResult.EnclosingClash:
3183 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3186 case DeclSpace.AdditionResult.NotAConstructor:
3187 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3193 CheckDef (bool result, string name)
3197 CheckDef (DeclSpace.AdditionResult.NameExists, name);
3201 SimpleLookup (string name)
3204 // we need to check against current_block not being null
3205 // as `expression' is allowed in argument_lists, which
3206 // do not exist inside a block.
3208 if (current_block != null){
3209 if (current_block.IsVariableDefined (name))
3210 return new LocalVariableReference (current_block, name);
3213 if (current_local_parameters != null){
3215 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
3217 return new ParameterReference (current_local_parameters, idx, name);
3223 Expression DecomposeQI (string name, Location l)
3227 if (name.IndexOf ('.') == -1){
3228 o = SimpleLookup (name);
3230 return new SimpleName (name, l);
3233 int pos = name.LastIndexOf (".");
3234 string left = name.Substring (0, pos);
3235 string right = name.Substring (pos + 1);
3237 o = DecomposeQI (left, l);
3238 return new MemberAccess (o, right, l);
3243 // This method is used to get at the complete string representation of
3244 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3245 // This is necessary because local_variable_type admits primary_expression
3246 // as the type of the variable. So we do some extra checking
3248 string GetQualifiedIdentifier (Expression expr)
3250 if (expr is SimpleName)
3251 return ((SimpleName)expr).Name;
3252 else if (expr is MemberAccess)
3253 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3255 throw new Exception ("Expr has to be either SimpleName or MemberAccess !");
3259 Block declare_local_variables (string type, ArrayList variable_declarators)
3261 Block implicit_block;
3262 ArrayList inits = null;
3265 // We use the `Used' property to check whether statements
3266 // have been added to the current block. If so, we need
3267 // to create another block to contain the new declaration
3268 // otherwise, as an optimization, we use the same block to
3269 // add the declaration.
3271 // FIXME: A further optimization is to check if the statements
3272 // that were added were added as part of the initialization
3273 // below. In which case, no other statements have been executed
3274 // and we might be able to reduce the number of blocks for
3275 // situations like this:
3277 // int j = 1; int k = j + 1;
3279 if (current_block.Used)
3280 implicit_block = new Block (current_block, true);
3282 implicit_block = current_block;
3284 foreach (VariableDeclaration decl in variable_declarators){
3285 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3286 if (decl.expression_or_array_initializer != null){
3288 inits = new ArrayList ();
3292 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3293 "' is already defined in this scope");
3298 return implicit_block;
3300 foreach (VariableDeclaration decl in inits){
3301 if (decl.expression_or_array_initializer is Expression){
3302 Expression expr = (Expression) decl.expression_or_array_initializer;
3305 assign = new Assign (new LocalVariableReference (
3306 implicit_block, decl.identifier),
3307 expr, decl.Location);
3309 implicit_block.AddStatement (new StatementExpression (assign));
3311 Console.WriteLine ("Not handling Array initializers yet");
3315 return implicit_block;
3318 void CheckConstant (Expression expr)
3323 void CheckBoolean (Expression expr)
3328 void CheckAttributeTarget (string a)
3332 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3336 Location l = lexer.Location;
3337 Report.Error (658, l, "Invalid attribute target");
3343 void CheckUnaryOperator (Operator.OpType op)
3347 case Operator.OpType.LogicalNot:
3348 case Operator.OpType.OnesComplement:
3349 case Operator.OpType.Increment:
3350 case Operator.OpType.Decrement:
3351 case Operator.OpType.True:
3352 case Operator.OpType.False:
3353 case Operator.OpType.Addition:
3354 case Operator.OpType.Subtraction:
3359 Location l = lexer.Location;
3360 Report.Error (1019, l, "Overloadable unary operator expected");
3366 void CheckBinaryOperator (Operator.OpType op)
3370 case Operator.OpType.Addition:
3371 case Operator.OpType.Subtraction:
3372 case Operator.OpType.Multiply:
3373 case Operator.OpType.Division:
3374 case Operator.OpType.Modulus:
3375 case Operator.OpType.BitwiseAnd:
3376 case Operator.OpType.BitwiseOr:
3377 case Operator.OpType.ExclusiveOr:
3378 case Operator.OpType.LeftShift:
3379 case Operator.OpType.RightShift:
3380 case Operator.OpType.Equality:
3381 case Operator.OpType.Inequality:
3382 case Operator.OpType.GreaterThan:
3383 case Operator.OpType.LessThan:
3384 case Operator.OpType.GreaterThanOrEqual:
3385 case Operator.OpType.LessThanOrEqual:
3389 Location l = lexer.Location;
3390 Report.Error (1020, l, "Overloadable binary operator expected");
3396 void output (string s)
3398 Console.WriteLine (s);
3401 void note (string s)
3403 // Used to put annotations
3408 public Tokenizer Lexer {
3414 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
3416 current_namespace = new Namespace (null, "");
3418 this.tree = rc.Tree;
3421 current_container = tree.Types;
3422 current_container.Namespace = current_namespace;
3423 oob_stack = new Stack ();
3425 lexer = new Tokenizer (input, name);
3428 public override int parse ()
3430 StringBuilder value = new StringBuilder ();
3434 if (yacc_verbose_flag)
3435 yyparse (lexer, new yydebug.yyDebugSimple ());
3438 } catch (Exception e){
3439 // Console.WriteLine ("Fatal error: " + name);
3440 // Console.WriteLine (lexer.location);
3442 Console.WriteLine (lexer.location + " : Parsing error ");
3443 Console.WriteLine (e);
3447 return global_errors;