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 syntax_error (lexer.Location, "'operator' expected");
1276 syntax_error (lexer.Location, "'operator' expected");
1280 constructor_declaration
1283 constructor_declarator
1286 Constructor c = (Constructor) $3;
1287 c.Block = (Block) $4;
1288 c.ModFlags = (int) $2;
1289 c.OptAttributes = (Attributes) $1;
1291 if ((c.ModFlags & Modifiers.STATIC) != 0){
1292 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1293 Location l = lexer.Location;
1294 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1297 if (c.Initializer != null){
1298 Location l = lexer.Location;
1299 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1302 if (!c.Parameters.Empty){
1303 Location l = lexer.Location;
1304 Report.Error (103, l, "Static constructors should not have parameters");
1308 CheckDef (current_container.AddConstructor (c), c.Name);
1310 current_local_parameters = null;
1314 constructor_declarator
1316 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1317 opt_constructor_initializer
1319 Location l = lexer.Location;
1321 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5, l);
1323 current_local_parameters = (Parameters) $3;
1327 opt_constructor_initializer
1328 : /* empty */ { $$ = null; }
1329 | constructor_initializer
1332 constructor_initializer
1333 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1335 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1337 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1339 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1343 destructor_declaration
1344 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1347 // FIXME: The destructor needs to generate special code:
1353 // void Finalize () {
1354 // try { code } finally { base.Finalize (); }
1356 Method d = new Method ("System.Void", 0, "Finalize",
1357 new Parameters (null, null), (Attributes) $1, lexer.Location);
1359 d.Block = (Block) $6;
1360 CheckDef (current_container.AddMethod (d), d.Name);
1367 EVENT type variable_declarators SEMICOLON
1369 foreach (VariableDeclaration var in (ArrayList) $5) {
1371 // FIXME : Is this right ?
1372 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1373 (int) $2, null, null, (Attributes) $1, lexer.Location);
1375 CheckDef (current_container.AddEvent (e), e.Name);
1381 EVENT type member_name
1382 OPEN_BRACE event_accessor_declarations CLOSE_BRACE
1384 DictionaryEntry pair = (DictionaryEntry) $7;
1385 Block add_block = null;
1386 Block rem_block = null;
1388 if (pair.Key != null)
1389 add_block = (Block) pair.Key;
1390 if (pair.Value != null)
1391 rem_block = (Block) pair.Value;
1393 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
1394 (Attributes) $1, lexer.Location);
1396 CheckDef (current_container.AddEvent (e), e.Name);
1400 event_accessor_declarations
1401 : add_accessor_declaration remove_accessor_declaration
1403 $$ = new DictionaryEntry ($1, $2);
1405 | remove_accessor_declaration add_accessor_declaration
1407 $$ = new DictionaryEntry ($2, $1);
1411 add_accessor_declaration
1412 : opt_attributes ADD block
1418 remove_accessor_declaration
1419 : opt_attributes REMOVE block
1426 : opt_attributes opt_modifiers indexer_declarator
1429 IndexerDeclaration decl = (IndexerDeclaration) $3;
1431 implicit_value_parameter_type = decl.type;
1433 lexer.properties = true;
1434 parsing_indexer = true;
1436 indexer_parameters = decl.param_list;
1437 $$ = lexer.Location;
1439 accessor_declarations
1441 lexer.properties = false;
1442 parsing_indexer = false;
1446 // The signature is computed from the signature of the indexer. Look
1447 // at section 3.6 on the spec
1450 IndexerDeclaration decl = (IndexerDeclaration) $3;
1451 DictionaryEntry pair = (DictionaryEntry) $6;
1452 Block get_block = null;
1453 Block set_block = null;
1455 if (pair.Key != null)
1456 get_block = (Block) pair.Key;
1457 if (pair.Value != null)
1458 set_block = (Block) pair.Value;
1460 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1461 get_block, set_block, (Attributes) $1, (Location) $5);
1463 // Note that there is no equivalent of CheckDef for this case
1464 // We shall handle this in semantic analysis
1466 current_container.AddIndexer (indexer);
1468 current_local_parameters = null;
1469 implicit_value_parameter_type = null;
1470 indexer_parameters = null;
1475 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1477 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
1479 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1481 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
1493 string name = (string) $4;
1494 Enum e = new Enum (rc, (string) $5, (int) $2, name, (Attributes) $1, lexer.Location);
1496 foreach (VariableDeclaration ev in (ArrayList) $6){
1497 CheckDef (e.AddEnumMember (ev.identifier,
1498 (Expression) ev.expression_or_array_initializer,
1499 (Location) ev.Location),
1503 CheckDef (current_container.AddEnum (e), name);
1508 : /* empty */ { $$ = "System.Int32"; }
1509 | COLON type { $$ = $2; }
1513 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1519 opt_enum_member_declarations
1520 : /* empty */ { $$ = new ArrayList (); }
1521 | enum_member_declarations opt_comma { $$ = $1; }
1524 enum_member_declarations
1525 : enum_member_declaration
1527 ArrayList l = new ArrayList ();
1532 | enum_member_declarations COMMA enum_member_declaration
1534 ArrayList l = (ArrayList) $1;
1542 enum_member_declaration
1543 : opt_attributes IDENTIFIER
1545 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1547 | opt_attributes IDENTIFIER
1549 $$ = lexer.Location;
1553 $$ = new VariableDeclaration ((string) $2, $5, lexer.Location);
1557 delegate_declaration
1561 IDENTIFIER OPEN_PARENS
1562 formal_parameter_list
1566 Delegate del = new Delegate (rc, (string) $4, (int) $2,
1567 MakeName ((string) $5), (Parameters) $7,
1568 (Attributes) $1, lexer.Location);
1570 CheckDef (current_container.AddDelegate (del), del.Name);
1575 IDENTIFIER OPEN_PARENS
1576 formal_parameter_list
1580 Delegate del = new Delegate (rc, "System.Void", (int) $2, (string) $5, (Parameters) $7,
1581 (Attributes) $1, lexer.Location);
1583 CheckDef (current_container.AddDelegate (del), del.Name);
1588 : namespace_or_type_name
1591 namespace_or_type_name
1592 : qualified_identifier
1596 * Before you think of adding a return_type, notice that we have been
1597 * using two rules in the places where it matters (one rule using type
1598 * and another identical one that uses VOID as the return type). This
1599 * gets rid of a shift/reduce couple
1602 : type_name { /* class_type */
1604 This does interfaces, delegates, struct_types, class_types,
1605 parent classes, and more! 4.2
1616 $$ = new SimpleName ((string) $1, lexer.Location);
1618 | non_expression_type rank_specifier
1620 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1622 | expression rank_specifiers
1624 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
1631 ArrayList types = new ArrayList ();
1636 | type_list COMMA type
1638 ArrayList types = (ArrayList) $1;
1646 * replaces all the productions for isolating the various
1647 * simple types, but we need this to reuse it easily in local_variable_type
1650 : OBJECT { $$ = "System.Object"; }
1651 | STRING { $$ = "System.String"; }
1652 | BOOL { $$ = "System.Boolean"; }
1653 | DECIMAL { $$ = "System.Decimal"; }
1654 | FLOAT { $$ = "System.Single"; }
1655 | DOUBLE { $$ = "System.Double"; }
1660 : SBYTE { $$ = "System.SByte"; }
1661 | BYTE { $$ = "System.Byte"; }
1662 | SHORT { $$ = "System.Int16"; }
1663 | USHORT { $$ = "System.UInt16"; }
1664 | INT { $$ = "System.Int32"; }
1665 | UINT { $$ = "System.UInt32"; }
1666 | LONG { $$ = "System.Int64"; }
1667 | ULONG { $$ = "System.UInt64"; }
1668 | CHAR { $$ = "System.Char"; }
1676 : type rank_specifiers
1678 $$ = (string) $1 + (string) $2;
1683 // Expressions, section 7.5
1691 | qualified_identifier
1693 string name = (string) $1;
1696 $$ = DecomposeQI (name, lexer.Location);
1698 | parenthesized_expression
1700 | invocation_expression
1704 | post_increment_expression
1705 | post_decrement_expression
1709 | checked_expression
1710 | unchecked_expression
1717 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
1718 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
1719 | NULL { $$ = new NullLiteral (); }
1723 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
1724 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
1725 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1730 object v = lexer.Value;
1733 // FIXME: Possible optimization would be to
1734 // compute the *Literal objects directly in the scanner
1737 $$ = new IntLiteral ((Int32) v);
1739 $$ = new UIntLiteral ((UInt32) v);
1741 $$ = new LongLiteral ((Int64) v);
1742 else if (v is ulong)
1743 $$ = new ULongLiteral ((UInt64) v);
1745 Console.WriteLine ("OOPS. Unexpected result from scanner");
1750 : TRUE { $$ = new BoolLiteral (true); }
1751 | FALSE { $$ = new BoolLiteral (false); }
1754 parenthesized_expression
1755 : OPEN_PARENS expression CLOSE_PARENS
1760 : primary_expression DOT IDENTIFIER
1762 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1764 | predefined_type DOT IDENTIFIER
1766 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1774 invocation_expression
1775 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1778 Location l = lexer.Location;
1779 Report.Error (1, l, "THIS IS CRAZY");
1781 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1787 : /* empty */ { $$ = null; }
1794 ArrayList list = new ArrayList ();
1798 | argument_list COMMA argument
1800 ArrayList list = (ArrayList) $1;
1809 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1811 | REF variable_reference
1813 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1815 | OUT variable_reference
1817 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1822 : expression { note ("section 5.4"); $$ = $1; }
1826 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
1828 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1835 ArrayList list = new ArrayList ();
1839 | expression_list COMMA expression
1841 ArrayList list = (ArrayList) $1;
1850 $$ = new This (lexer.Location);
1855 : BASE DOT IDENTIFIER
1857 $$ = new BaseAccess (BaseAccess.BaseAccessType.Member, (string) $3, null);
1859 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1861 $$ = new BaseAccess (BaseAccess.BaseAccessType.Indexer, null, (ArrayList) $3);
1865 post_increment_expression
1866 : primary_expression OP_INC
1868 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
1869 (Expression) $1, lexer.Location);
1873 post_decrement_expression
1874 : primary_expression OP_DEC
1876 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
1877 (Expression) $1, lexer.Location);
1882 : object_or_delegate_creation_expression
1883 | array_creation_expression
1886 object_or_delegate_creation_expression
1887 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1889 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1893 array_creation_expression
1894 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
1896 opt_array_initializer
1898 $$ = new ArrayCreation ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7,
1901 | NEW type rank_specifiers array_initializer
1903 $$ = new ArrayCreation ((string) $2, (string) $3, (ArrayList) $4, lexer.Location);
1923 | rank_specifiers rank_specifier
1925 $$ = (string) $2 + (string) $1;
1930 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1932 $$ = "[" + (string) $2 + "]";
1952 | dim_separators COMMA
1954 $$ = (string) $1 + ",";
1958 opt_array_initializer
1970 : OPEN_BRACE CLOSE_BRACE
1972 ArrayList list = new ArrayList ();
1975 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1977 $$ = (ArrayList) $2;
1981 variable_initializer_list
1982 : variable_initializer
1984 ArrayList list = new ArrayList ();
1988 | variable_initializer_list COMMA variable_initializer
1990 ArrayList list = (ArrayList) $1;
1997 : TYPEOF OPEN_PARENS type CLOSE_PARENS
1999 $$ = new TypeOf ((string) $3);
2004 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2005 $$ = new SizeOf ((string) $3);
2007 note ("Verify type is unmanaged");
2008 note ("if (5.8) builtin, yield constant expression");
2013 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2015 $$ = new CheckedExpr ((Expression) $3);
2019 unchecked_expression
2020 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2022 $$ = new UnCheckedExpr ((Expression) $3);
2027 : primary_expression
2028 | BANG prefixed_unary_expression
2030 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2032 | TILDE prefixed_unary_expression
2034 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2036 | OPEN_PARENS expression CLOSE_PARENS unary_expression
2038 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2040 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2042 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2047 // The idea to split this out is from Rhys' grammar
2048 // to solve the problem with casts.
2050 prefixed_unary_expression
2052 | PLUS prefixed_unary_expression
2054 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2056 | MINUS prefixed_unary_expression
2058 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2060 | OP_INC prefixed_unary_expression
2062 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2063 (Expression) $2, lexer.Location);
2065 | OP_DEC prefixed_unary_expression
2067 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2068 (Expression) $2, lexer.Location);
2070 | STAR prefixed_unary_expression
2072 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
2074 | BITWISE_AND prefixed_unary_expression
2076 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
2080 pre_increment_expression
2081 : OP_INC prefixed_unary_expression
2083 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2084 (Expression) $2, lexer.Location);
2088 pre_decrement_expression
2089 : OP_DEC prefixed_unary_expression
2091 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2092 (Expression) $2, lexer.Location);
2096 multiplicative_expression
2097 : prefixed_unary_expression
2098 | multiplicative_expression STAR prefixed_unary_expression
2100 $$ = new Binary (Binary.Operator.Multiply,
2101 (Expression) $1, (Expression) $3, lexer.Location);
2103 | multiplicative_expression DIV prefixed_unary_expression
2105 $$ = new Binary (Binary.Operator.Division,
2106 (Expression) $1, (Expression) $3, lexer.Location);
2108 | multiplicative_expression PERCENT prefixed_unary_expression
2110 $$ = new Binary (Binary.Operator.Modulus,
2111 (Expression) $1, (Expression) $3, lexer.Location);
2116 : multiplicative_expression
2117 | additive_expression PLUS multiplicative_expression
2119 $$ = new Binary (Binary.Operator.Addition,
2120 (Expression) $1, (Expression) $3, lexer.Location);
2122 | additive_expression MINUS multiplicative_expression
2124 $$ = new Binary (Binary.Operator.Subtraction,
2125 (Expression) $1, (Expression) $3, lexer.Location);
2130 : additive_expression
2131 | shift_expression OP_SHIFT_LEFT additive_expression
2133 $$ = new Binary (Binary.Operator.LeftShift,
2134 (Expression) $1, (Expression) $3, lexer.Location);
2136 | shift_expression OP_SHIFT_RIGHT additive_expression
2138 $$ = new Binary (Binary.Operator.RightShift,
2139 (Expression) $1, (Expression) $3, lexer.Location);
2143 relational_expression
2145 | relational_expression OP_LT shift_expression
2147 $$ = new Binary (Binary.Operator.LessThan,
2148 (Expression) $1, (Expression) $3, lexer.Location);
2150 | relational_expression OP_GT shift_expression
2152 $$ = new Binary (Binary.Operator.GreaterThan,
2153 (Expression) $1, (Expression) $3, lexer.Location);
2155 | relational_expression OP_LE shift_expression
2157 $$ = new Binary (Binary.Operator.LessThanOrEqual,
2158 (Expression) $1, (Expression) $3, lexer.Location);
2160 | relational_expression OP_GE shift_expression
2162 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
2163 (Expression) $1, (Expression) $3, lexer.Location);
2165 | relational_expression IS type
2167 $$ = new Probe (Probe.Operator.Is,
2168 (Expression) $1, (string) $3);
2170 | relational_expression AS type
2172 $$ = new Probe (Probe.Operator.As,
2173 (Expression) $1, (string) $3);
2178 : relational_expression
2179 | equality_expression OP_EQ relational_expression
2181 $$ = new Binary (Binary.Operator.Equality,
2182 (Expression) $1, (Expression) $3, lexer.Location);
2184 | equality_expression OP_NE relational_expression
2186 $$ = new Binary (Binary.Operator.Inequality,
2187 (Expression) $1, (Expression) $3, lexer.Location);
2192 : equality_expression
2193 | and_expression BITWISE_AND equality_expression
2195 $$ = new Binary (Binary.Operator.BitwiseAnd,
2196 (Expression) $1, (Expression) $3, lexer.Location);
2200 exclusive_or_expression
2202 | exclusive_or_expression CARRET and_expression
2204 $$ = new Binary (Binary.Operator.ExclusiveOr,
2205 (Expression) $1, (Expression) $3, lexer.Location);
2209 inclusive_or_expression
2210 : exclusive_or_expression
2211 | inclusive_or_expression BITWISE_OR exclusive_or_expression
2213 $$ = new Binary (Binary.Operator.BitwiseOr,
2214 (Expression) $1, (Expression) $3, lexer.Location);
2218 conditional_and_expression
2219 : inclusive_or_expression
2220 | conditional_and_expression OP_AND inclusive_or_expression
2222 $$ = new Binary (Binary.Operator.LogicalAnd,
2223 (Expression) $1, (Expression) $3, lexer.Location);
2227 conditional_or_expression
2228 : conditional_and_expression
2229 | conditional_or_expression OP_OR conditional_and_expression
2231 $$ = new Binary (Binary.Operator.LogicalOr,
2232 (Expression) $1, (Expression) $3, lexer.Location);
2236 conditional_expression
2237 : conditional_or_expression
2238 | conditional_or_expression INTERR expression COLON expression
2240 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2244 assignment_expression
2245 : prefixed_unary_expression ASSIGN expression
2247 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2249 | prefixed_unary_expression OP_MULT_ASSIGN expression
2251 Location l = lexer.Location;
2253 $$ = new Assign ((Expression) $1,
2254 new Binary (Binary.Operator.Multiply,
2256 (Expression) $3, l), l);
2258 | prefixed_unary_expression OP_DIV_ASSIGN expression
2260 Location l = lexer.Location;
2262 $$ = new Assign ((Expression) $1,
2263 new Binary (Binary.Operator.Division,
2265 (Expression) $3, l), l);
2267 | prefixed_unary_expression OP_MOD_ASSIGN expression
2269 Location l = lexer.Location;
2271 $$ = new Assign ((Expression) $1,
2272 new Binary (Binary.Operator.Modulus,
2274 (Expression) $3, l), l);
2276 | prefixed_unary_expression OP_ADD_ASSIGN expression
2278 Location l = lexer.Location;
2280 $$ = new Assign ((Expression) $1,
2281 new Binary (Binary.Operator.Addition,
2283 (Expression) $3, l), l);
2285 | prefixed_unary_expression OP_SUB_ASSIGN expression
2287 Location l = lexer.Location;
2289 $$ = new Assign ((Expression) $1,
2290 new Binary (Binary.Operator.Subtraction,
2292 (Expression) $3, l), l);
2294 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
2296 Location l = lexer.Location;
2298 $$ = new Assign ((Expression) $1,
2299 new Binary (Binary.Operator.LeftShift,
2301 (Expression) $3, l), l);
2303 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2305 Location l = lexer.Location;
2307 $$ = new Assign ((Expression) $1,
2308 new Binary (Binary.Operator.RightShift,
2310 (Expression) $3, l), l);
2312 | prefixed_unary_expression OP_AND_ASSIGN expression
2314 Location l = lexer.Location;
2316 $$ = new Assign ((Expression) $1,
2317 new Binary (Binary.Operator.BitwiseAnd,
2319 (Expression) $3, l), l);
2321 | prefixed_unary_expression OP_OR_ASSIGN expression
2323 Location l = lexer.Location;
2325 $$ = new Assign ((Expression) $1,
2326 new Binary (Binary.Operator.BitwiseOr,
2328 (Expression) $3, l), l);
2330 | prefixed_unary_expression OP_XOR_ASSIGN expression
2332 Location l = lexer.Location;
2334 $$ = new Assign ((Expression) $1,
2335 new Binary (Binary.Operator.ExclusiveOr,
2337 (Expression) $3, l), l);
2342 : conditional_expression
2343 | assignment_expression
2363 string full_class_name = MakeName ((string) $4);
2365 new_class = new Class (rc, current_container, full_class_name, (int) $2,
2366 (Attributes) $1, lexer.Location);
2367 current_container = new_class;
2368 current_container.Namespace = current_namespace;
2369 tree.RecordClass (full_class_name, new_class);
2375 Class new_class = (Class) current_container;
2378 new_class.Bases = (ArrayList) $6;
2380 current_container = current_container.Parent;
2381 CheckDef (current_container.AddClass (new_class), new_class.Name);
2388 : /* empty */ { $$ = (int) 0; }
2394 | modifiers modifier
2399 if ((m1 & m2) != 0) {
2400 Location l = lexer.Location;
2401 Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2403 $$ = (int) (m1 | m2);
2408 : NEW { $$ = Modifiers.NEW; }
2409 | PUBLIC { $$ = Modifiers.PUBLIC; }
2410 | PROTECTED { $$ = Modifiers.PROTECTED; }
2411 | INTERNAL { $$ = Modifiers.INTERNAL; }
2412 | PRIVATE { $$ = Modifiers.PRIVATE; }
2413 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
2414 | SEALED { $$ = Modifiers.SEALED; }
2415 | STATIC { $$ = Modifiers.STATIC; }
2416 | READONLY { $$ = Modifiers.READONLY; }
2417 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
2418 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
2419 | EXTERN { $$ = Modifiers.EXTERN; }
2423 : /* empty */ { $$ = null; }
2424 | class_base { $$ = $1; }
2428 : COLON type_list { $$ = $2; }
2436 // A block is "contained" on the following places:
2438 // property_declaration as part of the accessor body (get/set)
2439 // operator_declaration
2440 // constructor_declaration
2441 // destructor_declaration
2442 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2447 current_block = new Block (current_block);
2449 opt_statement_list CLOSE_BRACE
2451 while (current_block.Implicit)
2452 current_block = current_block.Parent;
2454 current_block = current_block.Parent;
2465 | statement_list statement
2469 : declaration_statement
2471 if ((Block) $1 != current_block){
2472 current_block.AddStatement ((Statement) $1);
2473 current_block = (Block) $1;
2476 | embedded_statement
2478 Statement s = (Statement) $1;
2481 current_block.AddStatement ((Statement) $1);
2485 current_block.AddStatement ((Statement) $1);
2492 | expression_statement
2493 | selection_statement
2494 | iteration_statement
2498 | unchecked_statement
2506 $$ = new EmptyStatement ();
2511 : IDENTIFIER COLON statement
2513 string lab = (String) $1;
2516 block = new Block (current_block, lab);
2517 block.AddStatement ((Statement) $3);
2520 if (!current_block.AddLabel (lab, block)){
2521 Location l = lexer.Location;
2522 Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2528 declaration_statement
2529 : local_variable_declaration SEMICOLON
2531 DictionaryEntry de = (DictionaryEntry) $1;
2533 $$ = declare_local_variables ((string) de.Key, (ArrayList) de.Value);
2536 | local_constant_declaration SEMICOLON // finishme
2540 * The following is from Rhys' grammar:
2541 * > Types in local variable declarations must be recognized as
2542 * > expressions to prevent reduce/reduce errors in the grammar.
2543 * > The expressions are converted into types during semantic analysis.
2546 : primary_expression opt_rank_specifier
2548 // FIXME: Do something smart here regarding the composition of the type.
2550 // Ok, the above "primary_expression" is there to get rid of
2551 // both reduce/reduce and shift/reduces in the grammar, it should
2552 // really just be "type_name". If you use type_name, a reduce/reduce
2553 // creeps up. If you use qualified_identifier (which is all we need
2554 // really) two shift/reduces appear.
2557 // So the super-trick is that primary_expression
2558 // can only be either a SimpleName or a MemberAccess.
2559 // The MemberAccess case arises when you have a fully qualified type-name like :
2561 // SimpleName is when you have
2564 Expression expr = (Expression) $1;
2565 if (!(expr is SimpleName || expr is MemberAccess)) {
2566 Location l = lexer.Location;
2567 Report.Error (-1, l, "Invalid Type definition");
2568 $$ = "System.Object";
2572 // So we extract the string corresponding to the SimpleName
2575 $$ = GetQualifiedIdentifier (expr) + (string) $2;
2577 | builtin_types opt_rank_specifier
2579 $$ = (string) $1 + (string) $2;
2583 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2586 // // FIXME: this is a string that represents the type
2587 // // Figure out something to make this work.
2592 local_variable_declaration
2593 : local_variable_type variable_declarators
2595 $$ = new DictionaryEntry ($1, $2);
2599 local_constant_declaration
2600 : CONST type constant_declarator
2604 expression_statement
2605 : statement_expression SEMICOLON
2612 // We have to do the wrapping here and not in the case above,
2613 // because statement_expression is used for example in for_statement
2615 statement_expression
2616 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2617 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2618 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2619 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2620 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2621 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2622 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1); }
2625 object_creation_expression
2626 : object_or_delegate_creation_expression
2627 { note ("complain if this is a delegate maybe?"); }
2636 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
2639 $$ = new If ((Expression) $3, (Statement) $5);
2641 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2642 embedded_statement ELSE embedded_statement
2644 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2649 : SWITCH OPEN_PARENS
2651 oob_stack.Push (lexer.Location);
2653 expression CLOSE_PARENS
2656 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
2670 : /* empty */ { $$ = new ArrayList (); }
2677 ArrayList sections = new ArrayList ();
2682 | switch_sections switch_section
2684 ArrayList sections = (ArrayList) $1;
2694 current_block = new Block (current_block);
2698 while (current_block.Implicit)
2699 current_block = current_block.Parent;
2700 $$ = new SwitchSection ((ArrayList) $1, current_block);
2701 current_block = current_block.Parent;
2708 ArrayList labels = new ArrayList ();
2713 | switch_labels switch_label
2715 ArrayList labels = (ArrayList) ($1);
2723 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
2724 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
2735 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2737 $$ = new While ((Expression) $3, (Statement) $5);
2742 : DO embedded_statement
2743 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2745 $$ = new Do ((Statement) $2, (Expression) $5);
2751 opt_for_initializer SEMICOLON
2753 Block assign_block = new Block (current_block);
2754 current_block = assign_block;
2756 if ($3 is DictionaryEntry){
2757 DictionaryEntry de = (DictionaryEntry) $3;
2760 string type = (string) de.Key;
2761 ArrayList var_declarators = (ArrayList) de.Value;
2763 foreach (VariableDeclaration decl in var_declarators){
2764 if (!current_block.AddVariable (type, decl.identifier, decl.Location)){
2765 Report.Error (128, decl.Location,
2766 "A local variable `" + decl.identifier + "' is already" +
2767 "defined in this scope");
2771 foreach (VariableDeclaration decl in var_declarators){
2774 if (decl.expression_or_array_initializer is Expression){
2775 expr = (Expression) decl.expression_or_array_initializer;
2779 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
2781 string base_type = type.Substring (0, type.IndexOf ("["));
2782 string rank = type.Substring (type.IndexOf ("["));
2784 expr = new ArrayCreation (base_type, rank, init, decl.Location);
2787 Assign a = new Assign (new LocalVariableReference (assign_block, decl.identifier),
2788 expr, decl.Location);
2790 assign_block.AddStatement (new StatementExpression (a));
2795 opt_for_condition SEMICOLON
2796 opt_for_iterator CLOSE_PARENS
2799 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10);
2801 current_block.AddStatement (f);
2802 while (current_block.Implicit)
2803 current_block = current_block.Parent;
2805 current_block = current_block.Parent;
2810 : /* empty */ { $$ = new EmptyStatement (); }
2815 : local_variable_declaration
2816 | statement_expression_list
2820 : /* empty */ { $$ = new BoolLiteral (true); }
2821 | boolean_expression
2825 : /* empty */ { $$ = new EmptyStatement (); }
2830 : statement_expression_list
2833 statement_expression_list
2834 : statement_expression
2836 Block b = new Block (null, true);
2838 b.AddStatement ((Statement) $1);
2841 | statement_expression_list COMMA statement_expression
2843 Block b = (Block) $1;
2845 b.AddStatement ((Statement) $3);
2851 : FOREACH OPEN_PARENS type IDENTIFIER IN
2853 oob_stack.Push (lexer.Location);
2855 expression CLOSE_PARENS
2857 Block foreach_block = new Block (current_block, true);
2858 LocalVariableReference v;
2860 foreach_block.AddVariable ((string) $3, (string) $4, lexer.Location);
2861 v = new LocalVariableReference (foreach_block, (string) $4);
2863 current_block.AddStatement (foreach_block);
2864 current_block = foreach_block;
2866 oob_stack.Push (foreach_block);
2871 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
2872 Block foreach_block = (Block) oob_stack.Pop ();
2873 Location l = (Location) oob_stack.Pop ();
2875 $$ = new Foreach ((string) $3, v, (Expression) $7, (Statement) $10, l);
2881 | continue_statement
2890 $$ = new Break (lexer.Location);
2895 : CONTINUE SEMICOLON
2897 $$ = new Continue (lexer.Location);
2902 : GOTO IDENTIFIER SEMICOLON
2904 $$ = new Goto ((string) $2, lexer.Location);
2906 | GOTO CASE constant_expression SEMICOLON
2910 | GOTO DEFAULT SEMICOLON
2917 : RETURN opt_expression SEMICOLON
2919 $$ = new Return ((Expression) $2, lexer.Location);
2924 : THROW opt_expression SEMICOLON
2926 $$ = new Throw ((Expression) $2);
2936 : TRY block catch_clauses
2939 ArrayList s = new ArrayList ();
2941 foreach (Catch cc in (ArrayList) $3) {
2942 if (cc.Type == null)
2948 // Now s contains the list of specific catch clauses
2949 // and g contains the general one.
2951 $$ = new Try ((Block) $2, s, g, null);
2953 | TRY block opt_catch_clauses FINALLY block
2956 ArrayList s = new ArrayList ();
2958 foreach (Catch cc in (ArrayList) $3) {
2959 if (cc.Type == null)
2965 $$ = new Try ((Block) $2, s, g, (Block) $5);
2970 : /* empty */ { $$ = null; }
2977 ArrayList l = new ArrayList ();
2982 | catch_clauses catch_clause
2984 ArrayList l = (ArrayList) $1;
2992 : /* empty */ { $$ = null; }
2997 : CATCH opt_catch_args
2999 string type = null, id = null;
3002 DictionaryEntry cc = (DictionaryEntry) $2;
3003 type = (string) cc.Key;
3004 id = (string) cc.Value;
3007 ArrayList one = new ArrayList ();
3008 one.Add (new VariableDeclaration (id, null, lexer.Location));
3011 current_block = new Block (current_block);
3012 Block b = declare_local_variables (type, one);
3019 string type = null, id = null;
3022 DictionaryEntry cc = (DictionaryEntry) $2;
3023 type = (string) cc.Key;
3024 id = (string) cc.Value;
3027 while (current_block != (Block) $1)
3028 current_block = current_block.Parent;
3033 $$ = new Catch (type, id , (Block) $4);
3038 : /* empty */ { $$ = null; }
3043 : OPEN_PARENS type opt_identifier CLOSE_PARENS
3045 $$ = new DictionaryEntry ($2, $3);
3052 $$ = new Checked ((Block) $2);
3059 $$ = new Unchecked ((Block) $2);
3064 : LOCK OPEN_PARENS expression CLOSE_PARENS
3070 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
3075 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
3080 $$ = new Using ($3, (Statement) $6, lexer.Location);
3084 resource_acquisition
3085 : local_variable_declaration
3087 if ((Block) $1 != current_block){
3088 current_block.AddStatement ((Statement) $1);
3089 current_block = (Block) $1;
3098 // A class used to pass around variable declarations and constants
3100 public class VariableDeclaration {
3101 public string identifier;
3102 public object expression_or_array_initializer;
3103 public Location Location;
3105 public VariableDeclaration (string id, object eoai, Location l){
3106 this.identifier = id;
3107 this.expression_or_array_initializer = eoai;
3113 // A class used to hold info about an indexer declarator
3116 public class IndexerDeclaration {
3118 public string interface_type;
3119 public Parameters param_list;
3121 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
3124 this.interface_type = interface_type;
3125 this.param_list = param_list;
3130 // A class used to hold info about an operator declarator
3133 public class OperatorDeclaration {
3134 public Operator.OpType optype;
3135 public string ret_type;
3136 public string arg1type;
3137 public string arg1name;
3138 public string arg2type;
3139 public string arg2name;
3140 public Location location;
3142 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
3143 string arg2type, string arg2name, Location location)
3146 this.ret_type = ret_type;
3147 this.arg1type = arg1type;
3148 this.arg1name = arg1name;
3149 this.arg2type = arg2type;
3150 this.arg2name = arg2name;
3151 this.location = location;
3157 // Given the @class_name name, it creates a fully qualified name
3158 // based on the containing declaration space
3161 MakeName (string class_name)
3163 string ns = current_namespace.Name;
3164 string container_name = current_container.Name;
3166 if (container_name == ""){
3168 return ns + "." + class_name;
3172 return container_name + "." + class_name;
3176 // Used to report back to the user the result of a declaration
3177 // in the current declaration space
3180 CheckDef (DeclSpace.AdditionResult result, string name)
3182 if (result == DeclSpace.AdditionResult.Success)
3185 Location l = lexer.Location;
3188 case DeclSpace.AdditionResult.NameExists:
3189 Report.Error (102, l, "The namespace `" + current_container.Name +
3190 "' already contains a definition for `"+
3195 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
3197 // case DeclSpace.AdditionResult.MethodDuplicated:
3198 // error (111, "Class `"+current_container.Name+
3199 // "' already defines a member called '" +
3200 // name + "' with the same parameter types");
3203 case DeclSpace.AdditionResult.EnclosingClash:
3204 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3207 case DeclSpace.AdditionResult.NotAConstructor:
3208 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3214 CheckDef (bool result, string name)
3218 CheckDef (DeclSpace.AdditionResult.NameExists, name);
3222 SimpleLookup (string name)
3225 // we need to check against current_block not being null
3226 // as `expression' is allowed in argument_lists, which
3227 // do not exist inside a block.
3229 if (current_block != null){
3230 if (current_block.IsVariableDefined (name))
3231 return new LocalVariableReference (current_block, name);
3234 if (current_local_parameters != null){
3236 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
3238 return new ParameterReference (current_local_parameters, idx, name);
3244 Expression DecomposeQI (string name, Location l)
3248 if (name.IndexOf ('.') == -1){
3249 o = SimpleLookup (name);
3251 return new SimpleName (name, l);
3254 int pos = name.LastIndexOf (".");
3255 string left = name.Substring (0, pos);
3256 string right = name.Substring (pos + 1);
3258 o = DecomposeQI (left, l);
3259 return new MemberAccess (o, right, l);
3264 // This method is used to get at the complete string representation of
3265 // a fully-qualified type name, hiding inside a MemberAccess ;-)
3266 // This is necessary because local_variable_type admits primary_expression
3267 // as the type of the variable. So we do some extra checking
3269 string GetQualifiedIdentifier (Expression expr)
3271 if (expr is SimpleName)
3272 return ((SimpleName)expr).Name;
3273 else if (expr is MemberAccess)
3274 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3276 throw new Exception ("Expr has to be either SimpleName or MemberAccess !");
3280 Block declare_local_variables (string type, ArrayList variable_declarators)
3282 Block implicit_block;
3283 ArrayList inits = null;
3286 // We use the `Used' property to check whether statements
3287 // have been added to the current block. If so, we need
3288 // to create another block to contain the new declaration
3289 // otherwise, as an optimization, we use the same block to
3290 // add the declaration.
3292 // FIXME: A further optimization is to check if the statements
3293 // that were added were added as part of the initialization
3294 // below. In which case, no other statements have been executed
3295 // and we might be able to reduce the number of blocks for
3296 // situations like this:
3298 // int j = 1; int k = j + 1;
3300 if (current_block.Used)
3301 implicit_block = new Block (current_block, true);
3303 implicit_block = current_block;
3305 foreach (VariableDeclaration decl in variable_declarators){
3306 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3307 if (decl.expression_or_array_initializer != null){
3309 inits = new ArrayList ();
3313 Report.Error (128, decl.Location, "A local variable `" + decl.identifier +
3314 "' is already defined in this scope");
3319 return implicit_block;
3321 foreach (VariableDeclaration decl in inits){
3325 if (decl.expression_or_array_initializer is Expression){
3326 expr = (Expression) decl.expression_or_array_initializer;
3329 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3331 string base_type = type.Substring (0, type.IndexOf ("["));
3332 string rank = type.Substring (type.IndexOf ("["));
3334 expr = new ArrayCreation (base_type, rank, init, decl.Location);
3338 assign = new Assign (new LocalVariableReference (implicit_block, decl.identifier), expr, decl.Location);
3339 implicit_block.AddStatement (new StatementExpression (assign));
3342 return implicit_block;
3345 void CheckAttributeTarget (string a)
3349 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3353 Location l = lexer.Location;
3354 Report.Error (658, l, "Invalid attribute target");
3360 void CheckUnaryOperator (Operator.OpType op)
3364 case Operator.OpType.LogicalNot:
3365 case Operator.OpType.OnesComplement:
3366 case Operator.OpType.Increment:
3367 case Operator.OpType.Decrement:
3368 case Operator.OpType.True:
3369 case Operator.OpType.False:
3370 case Operator.OpType.Addition:
3371 case Operator.OpType.Subtraction:
3376 Location l = lexer.Location;
3377 Report.Error (1019, l, "Overloadable unary operator expected");
3383 void CheckBinaryOperator (Operator.OpType op)
3387 case Operator.OpType.Addition:
3388 case Operator.OpType.Subtraction:
3389 case Operator.OpType.Multiply:
3390 case Operator.OpType.Division:
3391 case Operator.OpType.Modulus:
3392 case Operator.OpType.BitwiseAnd:
3393 case Operator.OpType.BitwiseOr:
3394 case Operator.OpType.ExclusiveOr:
3395 case Operator.OpType.LeftShift:
3396 case Operator.OpType.RightShift:
3397 case Operator.OpType.Equality:
3398 case Operator.OpType.Inequality:
3399 case Operator.OpType.GreaterThan:
3400 case Operator.OpType.LessThan:
3401 case Operator.OpType.GreaterThanOrEqual:
3402 case Operator.OpType.LessThanOrEqual:
3406 Location l = lexer.Location;
3407 Report.Error (1020, l, "Overloadable binary operator expected");
3413 void syntax_error (Location l, string msg)
3415 Report.Error (1003, l, "Syntax error, " + msg);
3418 void output (string s)
3420 Console.WriteLine (s);
3423 void note (string s)
3425 // Used to put annotations
3430 public Tokenizer Lexer {
3436 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
3438 current_namespace = new Namespace (null, "");
3440 this.tree = rc.Tree;
3443 current_container = tree.Types;
3444 current_container.Namespace = current_namespace;
3445 oob_stack = new Stack ();
3447 lexer = new Tokenizer (input, name);
3450 public override int parse ()
3452 StringBuilder value = new StringBuilder ();
3456 if (yacc_verbose_flag)
3457 yyparse (lexer, new yydebug.yyDebugSimple ());
3460 } catch (Exception e){
3461 // Console.WriteLine ("Fatal error: " + name);
3462 // Console.WriteLine (lexer.location);
3465 // Please do not remove this, it is used during debugging
3468 Console.WriteLine (lexer.location + " : Parsing error ");
3469 Console.WriteLine (e);
3473 return global_errors;