3 // cs-parser.jay: The Parser for the C# compiler
\r
5 // Author: Miguel de Icaza (miguel@gnu.org)
\r
7 // Licensed under the terms of the GNU GPL
\r
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
\r
12 // (1) Get rid of the *Collections.cs, that is an idea I took from System.CodeDOM
\r
13 // And come to think of it, it is not that great, it duplicates a lot of code
\r
14 // for something which is not really needed. We still have piles of typecasts
\r
15 // anwyays (due to the nature of the stack being a collection of Objects).
\r
17 // (2) Figure out why error productions dont work. `type-declaration' is a
\r
18 // great spot to put an `error' because you can reproduce it with this input:
\r
21 // (3) Move Modifier checking from each object into the parser itself, that will
\r
22 // get rid of the global "error" symbol that we use now to report errors.
\r
23 // We still need to pass a pointer to the tree.ErrorHandler, but that is a
\r
32 using System.Collections;
\r
33 using Mono.Languages;
\r
38 public class CSharpParser : GenericParser {
\r
39 Namespace current_namespace;
\r
40 TypeContainer current_container;
\r
43 // Current block is used to add statements as we find
\r
47 Block current_block;
\r
50 // Current interface is used by the various declaration
\r
51 // productions in the interface declaration to "add"
\r
52 // the interfaces as we find them.
\r
54 Interface current_interface;
\r
57 // This is used by the unary_expression code to resolve
\r
58 // a name against a parameter.
\r
60 Parameters current_local_parameters;
\r
63 // Using during property parsing to describe the implicit
\r
64 // value parameter that is passed to the "set" accesor
\r
67 Parameter [] implicit_value_parameters;
\r
70 // Used to determine if we are parsing the get/set pair
\r
71 // of an indexer or a property
\r
73 bool parsing_indexer;
\r
76 // Used to record all types defined
\r
84 %token NONE /* This token is never returned by our lexer */
\r
85 %token ERROR // This is used not by the parser, but by the tokenizer.
\r
89 *These are the C# keywords
\r
169 /* C# keywords which are not really keywords */
\r
173 /* C# single character operators/punctuation. */
\r
174 %token OPEN_BRACE "{"
\r
175 %token CLOSE_BRACE "}"
\r
176 %token OPEN_BRACKET "["
\r
177 %token CLOSE_BRACKET "]"
\r
178 %token OPEN_PARENS "("
\r
179 %token CLOSE_PARENS ")"
\r
183 %token SEMICOLON ";"
\r
192 %token BITWISE_AND "&"
\r
193 %token BITWISE_OR "|"
\r
200 /* C# multi-character operators. */
\r
203 %token OP_SHIFT_LEFT "<<"
\r
204 %token OP_SHIFT_RIGHT ">>"
\r
211 %token OP_MULT_ASSIGN "*="
\r
212 %token OP_DIV_ASSIGN "/="
\r
213 %token OP_MOD_ASSIGN "%="
\r
214 %token OP_ADD_ASSIGN "+="
\r
215 %token OP_SUB_ASSIGN "-="
\r
216 %token OP_SHIFT_LEFT_ASSIGN "<<="
\r
217 %token OP_SHIFT_RIGHT_ASSIGN ">>="
\r
218 %token OP_AND_ASSIGN "&="
\r
219 %token OP_XOR_ASSIGN "^="
\r
220 %token OP_OR_ASSIGN "|="
\r
224 %token LITERAL_INTEGER "int literal"
\r
225 %token LITERAL_FLOAT "float literal"
\r
226 %token LITERAL_DOUBLE "double literal"
\r
227 %token LITERAL_DECIMAL "decimal literal"
\r
228 %token LITERAL_CHARACTER "character literal"
\r
229 %token LITERAL_STRING "string literal"
\r
233 /* Add precedence rules to solve dangling else s/r conflict */
\r
242 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
\r
244 %left STAR DIV PERCENT
\r
245 %right BANG CARRET UMINUS
\r
246 %nonassoc OP_INC OP_DEC
\r
248 %left OPEN_BRACKET OPEN_BRACE
\r
252 %start compilation_unit
\r
253 /*%start namespace_declaration */
\r
257 : opt_using_directives opt_attributes opt_namespace_member_declarations EOF
\r
259 // Check that using comes only before namespace elements
\r
265 | using_directives using_directive
\r
269 : using_alias_directive
\r
270 | using_namespace_directive
\r
273 using_alias_directive
\r
274 : USING IDENTIFIER ASSIGN
\r
275 namespace_or_type_name SEMICOLON
\r
277 // FIXME : Need to implement actual action.
\r
281 using_namespace_directive
\r
282 : USING namespace_name SEMICOLON
\r
284 current_namespace.Using ((string) $2);
\r
288 // namespace_declarations
\r
289 // : namespace_declaration
\r
290 // | namespace_declarations namespace_declaration
\r
292 namespace_declaration
\r
293 : NAMESPACE qualified_identifier
\r
295 current_namespace = new Namespace (current_namespace, (string) $2);
\r
296 tree.RecordNamespace ((string) $2, current_namespace);
\r
298 namespace_body opt_semicolon
\r
300 current_namespace = current_namespace.Parent;
\r
314 qualified_identifier
\r
316 | qualified_identifier DOT IDENTIFIER {
\r
317 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
\r
322 : namespace_or_type_name
\r
327 opt_using_directives
\r
328 opt_namespace_member_declarations
\r
334 opt_using_directives
\r
339 opt_namespace_member_declarations
\r
341 | namespace_member_declarations
\r
344 namespace_member_declarations
\r
345 : namespace_member_declaration
\r
346 | namespace_member_declarations namespace_member_declaration
\r
349 namespace_member_declaration
\r
356 Class c = (Class) $1;
\r
357 mod_flags = c.ModFlags;
\r
359 } else if ($1 is Struct){
\r
360 Struct s = (Struct) $1;
\r
361 mod_flags = s.ModFlags;
\r
367 // We remove this error until we can
\r
368 //if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
\r
369 // error (1527, "Namespace elements cant be explicitly " +
\r
370 // "declared private or protected in `" + name + "'");
\r
373 | namespace_declaration
\r
377 : class_declaration
\r
378 | struct_declaration
\r
379 | interface_declaration
\r
380 | enum_declaration
\r
381 | delegate_declaration
\r
388 : /* empty */ { $$ = null; }
\r
389 | attribute_section opt_attributes
\r
394 attrs = (Attributes) $2;
\r
395 attrs.AddAttribute ((Attribute) $1);
\r
397 attrs = new Attributes ((Attribute) $1);
\r
404 : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
\r
406 string target = null;
\r
409 target = (string) $2;
\r
411 $$ = new Attribute (target, (ArrayList) $3);
\r
413 | OPEN_BRACKET attribute_list CLOSE_BRACKET
\r
415 $$ = new Attribute (null, (ArrayList) $2);
\r
419 attribute_target_specifier
\r
420 : attribute_target COLON
\r
429 CheckAttributeTarget ((string) $1);
\r
432 | EVENT { $$ = "event"; }
\r
433 | RETURN { $$ = "return"; }
\r
439 ArrayList attrs = new ArrayList ();
\r
445 | attribute_list COMMA attribute
\r
447 ArrayList attrs = (ArrayList) $1;
\r
455 : attribute_name opt_attribute_arguments
\r
457 $$ = new DictionaryEntry ($1, $2);
\r
462 : type_name { /* reserved attribute name or identifier: 17.4 */ }
\r
465 opt_attribute_arguments
\r
466 : /* empty */ { $$ = null; }
\r
467 | OPEN_PARENS attribute_arguments CLOSE_PARENS
\r
473 attribute_arguments
\r
476 ArrayList args = new ArrayList ();
\r
481 | attribute_arguments COMMA expression
\r
483 ArrayList args = (ArrayList) $1;
\r
492 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
\r
495 opt_class_member_declarations
\r
497 | class_member_declarations
\r
500 class_member_declarations
\r
501 : class_member_declaration
\r
502 | class_member_declarations
\r
503 class_member_declaration
\r
506 class_member_declaration
\r
507 : constant_declaration // done
\r
508 | field_declaration // done
\r
509 | method_declaration // done
\r
510 | property_declaration // done
\r
511 | event_declaration // done
\r
512 | indexer_declaration // done
\r
513 | operator_declaration // done
\r
514 | constructor_declaration // done
\r
515 | destructor_declaration // done
\r
525 string full_struct_name = MakeName ((string) $4);
\r
527 new_struct = new Struct (rc, current_container, full_struct_name, (int) $2, (Attributes) $1);
\r
528 current_container = new_struct;
\r
529 current_container.Namespace = current_namespace;
\r
530 tree.RecordStruct (full_struct_name, new_struct);
\r
532 opt_struct_interfaces
\r
536 Struct new_struct = (Struct) current_container;
\r
538 current_container = current_container.Parent;
\r
539 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
\r
544 opt_struct_interfaces
\r
546 | struct_interfaces
\r
551 | struct_interfaces struct_interface
\r
559 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
\r
562 opt_struct_member_declarations
\r
564 | struct_member_declarations
\r
567 struct_member_declarations
\r
568 : struct_member_declaration
\r
569 | struct_member_declarations struct_member_declaration
\r
572 struct_member_declaration
\r
573 : constant_declaration
\r
574 | field_declaration
\r
575 | method_declaration
\r
576 | property_declaration
\r
577 | event_declaration
\r
578 | indexer_declaration
\r
579 | operator_declaration
\r
580 | constructor_declaration
\r
584 constant_declaration
\r
589 constant_declarators
\r
592 foreach (DictionaryEntry constant in (ArrayList) $5){
\r
593 Constant c = new Constant (
\r
594 (string) $4, (string) constant.Key,
\r
595 (Expression) constant.Value, (int) $2, (Attributes) $1);
\r
597 CheckDef (current_container.AddConstant (c), c.Name);
\r
602 constant_declarators
\r
603 : constant_declarator
\r
605 ArrayList constants = new ArrayList ();
\r
606 constants.Add ($1);
\r
609 | constant_declarators COMMA constant_declarator
\r
611 ArrayList constants = (ArrayList) $1;
\r
613 constants.Add ($3);
\r
617 constant_declarator
\r
618 : IDENTIFIER ASSIGN constant_expression
\r
620 $$ = new DictionaryEntry ($1, $3);
\r
628 variable_declarators
\r
631 string type = (string) $3;
\r
632 int mod = (int) $2;
\r
634 foreach (VariableDeclaration var in (ArrayList) $4){
\r
635 Field field = new Field (type, mod, var.identifier,
\r
636 var.expression_or_array_initializer, (Attributes) $1);
\r
638 CheckDef (current_container.AddField (field), field.Name);
\r
644 variable_declarators
\r
645 : variable_declarator
\r
647 ArrayList decl = new ArrayList ();
\r
651 | variable_declarators COMMA variable_declarator
\r
653 ArrayList decls = (ArrayList) $1;
\r
659 variable_declarator
\r
660 : IDENTIFIER ASSIGN variable_initializer
\r
662 $$ = new VariableDeclaration ((string) $1, $3);
\r
666 $$ = new VariableDeclaration ((string) $1, null);
\r
670 variable_initializer
\r
675 | array_initializer
\r
685 Method method = (Method) $1;
\r
687 method.Block = (Block) $2;
\r
688 CheckDef (current_container.AddMethod (method), method.Name);
\r
690 current_local_parameters = null;
\r
699 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
\r
701 Method method = new Method ((string) $3, (int) $2, (string) $4, (Parameters) $6, (Attributes) $1);
\r
703 current_local_parameters = (Parameters) $6;
\r
711 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
\r
713 Method method = new Method ("System.Void", (int) $2, (string) $4, (Parameters) $6, (Attributes) $1);
\r
715 current_local_parameters = (Parameters) $6;
\r
722 | SEMICOLON { $$ = null; }
\r
725 opt_formal_parameter_list
\r
726 : /* empty */ { $$ = new Parameters (null, null); }
\r
727 | formal_parameter_list
\r
730 formal_parameter_list
\r
731 : fixed_parameters
\r
733 ArrayList pars_list = (ArrayList) $1;
\r
735 Parameter [] pars = new Parameter [pars_list.Count];
\r
736 pars_list.CopyTo (pars);
\r
738 $$ = new Parameters (pars, null);
\r
740 | fixed_parameters COMMA parameter_array
\r
742 ArrayList pars_list = (ArrayList) $1;
\r
744 Parameter [] pars = new Parameter [pars_list.Count];
\r
745 pars_list.CopyTo (pars);
\r
747 $$ = new Parameters (pars, (Parameter) $3);
\r
751 $$ = new Parameters (null, (Parameter) $1);
\r
758 ArrayList pars = new ArrayList ();
\r
763 | fixed_parameters COMMA fixed_parameter
\r
765 ArrayList pars = (ArrayList) $1;
\r
774 opt_parameter_modifier
\r
778 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
\r
782 opt_parameter_modifier
\r
783 : /* empty */ { $$ = Parameter.Modifier.NONE; }
\r
784 | parameter_modifier
\r
788 : REF { $$ = Parameter.Modifier.REF; }
\r
789 | OUT { $$ = Parameter.Modifier.OUT; }
\r
793 : opt_attributes PARAMS type IDENTIFIER
\r
795 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
\r
796 note ("type must be a single-dimension array type");
\r
801 : IDENTIFIER { $$ = $1.ToString (); }
\r
802 | interface_type DOT IDENTIFIER { $$ = $1.ToString () + "." + $3.ToString (); }
\r
805 property_declaration
\r
811 Parameter implicit_value_parameter;
\r
812 implicit_value_parameter = new Parameter ((string) $3, "value", Parameter.Modifier.NONE, null);
\r
814 lexer.properties = true;
\r
816 implicit_value_parameters = new Parameter [1];
\r
817 implicit_value_parameters [0] = implicit_value_parameter;
\r
819 accessor_declarations
\r
821 lexer.properties = false;
\r
826 DictionaryEntry pair = (DictionaryEntry) $7;
\r
827 Block get_block = null;
\r
828 Block set_block = null;
\r
830 if (pair.Key != null)
\r
831 get_block = (Block) pair.Key;
\r
832 if (pair.Value != null)
\r
833 set_block = (Block) pair.Value;
\r
835 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block, (Attributes) $1);
\r
837 CheckDef (current_container.AddProperty (prop), prop.Name);
\r
838 implicit_value_parameters = null;
\r
842 accessor_declarations
\r
843 : get_accessor_declaration opt_set_accessor_declaration
\r
845 $$ = new DictionaryEntry ($1, $2);
\r
847 | set_accessor_declaration opt_get_accessor_declaration
\r
849 $$ = new DictionaryEntry ($2, $1);
\r
853 opt_get_accessor_declaration
\r
854 : /* empty */ { $$ = null; }
\r
855 | get_accessor_declaration
\r
858 opt_set_accessor_declaration
\r
859 : /* empty */ { $$ = null; }
\r
860 | set_accessor_declaration
\r
863 get_accessor_declaration
\r
864 : opt_attributes GET
\r
866 // If this is not the case, then current_local_parameters has already
\r
867 // been set in indexer_declaration
\r
868 if (parsing_indexer == false)
\r
869 current_local_parameters = new Parameters (implicit_value_parameters, null);
\r
876 current_local_parameters = null;
\r
880 set_accessor_declaration
\r
881 : opt_attributes SET
\r
883 if (parsing_indexer == false)
\r
884 current_local_parameters = new Parameters (implicit_value_parameters, null);
\r
889 current_local_parameters = null;
\r
895 | SEMICOLON { $$ = new Block (null); }
\r
898 interface_declaration
\r
901 INTERFACE IDENTIFIER
\r
903 Interface new_interface;
\r
904 string full_interface_name = MakeName ((string) $4);
\r
906 new_interface = new Interface (current_container, full_interface_name, (int) $2, (Attributes) $1);
\r
907 if (current_interface != null) {
\r
908 Location l = lexer.Location;
\r
909 rc.Report.Error (-2, l, "Internal compiler error: interface inside interface");
\r
911 current_interface = new_interface;
\r
912 tree.RecordInterface (full_interface_name, new_interface);
\r
917 Interface new_interface = (Interface) current_interface;
\r
920 new_interface.Bases = (ArrayList) $6;
\r
922 current_interface = null;
\r
923 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
\r
928 : /* empty */ { $$ = null; }
\r
933 : COLON interface_type_list { $$ = $2; }
\r
936 interface_type_list
\r
939 ArrayList interfaces = new ArrayList ();
\r
941 interfaces.Add ($1);
\r
944 | interface_type_list COMMA interface_type
\r
946 ArrayList interfaces = (ArrayList) $1;
\r
947 interfaces.Add ($3);
\r
954 opt_interface_member_declarations
\r
958 opt_interface_member_declarations
\r
960 | interface_member_declarations
\r
963 interface_member_declarations
\r
964 : interface_member_declaration
\r
965 | interface_member_declarations interface_member_declaration
\r
968 interface_member_declaration
\r
969 : interface_method_declaration
\r
971 InterfaceMethod m = (InterfaceMethod) $1;
\r
973 CheckDef (current_interface.AddMethod (m), m.Name);
\r
975 | interface_property_declaration
\r
977 InterfaceProperty p = (InterfaceProperty) $1;
\r
979 CheckDef (current_interface.AddProperty (p), p.Name);
\r
981 | interface_event_declaration
\r
983 InterfaceEvent e = (InterfaceEvent) $1;
\r
985 CheckDef (current_interface.AddEvent (e), e.Name);
\r
987 | interface_indexer_declaration
\r
989 InterfaceIndexer i = (InterfaceIndexer) $1;
\r
991 CheckDef (current_interface.AddIndexer (i), "indexer");
\r
996 : /* empty */ { $$ = false; }
\r
997 | NEW { $$ = true; }
\r
1000 interface_method_declaration
\r
1001 : opt_attributes opt_new type IDENTIFIER
\r
1002 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
\r
1005 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
\r
1007 | opt_attributes opt_new VOID IDENTIFIER
\r
1008 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
\r
1011 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
\r
1015 interface_property_declaration
\r
1020 { lexer.properties = true; }
\r
1021 interface_accesors
\r
1022 { lexer.properties = false; }
\r
1025 int gs = (int) $7;
\r
1027 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2,
\r
1028 (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1);
\r
1032 interface_accesors
\r
1033 : opt_attributes GET SEMICOLON { $$ = 1; }
\r
1034 | opt_attributes SET SEMICOLON { $$ = 2; }
\r
1035 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
\r
1037 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
\r
1041 interface_event_declaration
\r
1042 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
\r
1044 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
\r
1048 interface_indexer_declaration
\r
1049 : opt_attributes opt_new type THIS
\r
1050 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
\r
1052 { lexer.properties = true; }
\r
1053 interface_accesors
\r
1054 { lexer.properties = false; }
\r
1057 int a_flags = (int) $10;
\r
1059 bool do_get = (a_flags & 1) == 1;
\r
1060 bool do_set = (a_flags & 2) == 2;
\r
1062 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set, (bool) $2, (Attributes) $1);
\r
1066 operator_declaration
\r
1067 : opt_attributes opt_modifiers operator_declarator block
\r
1069 OperatorDeclaration decl = (OperatorDeclaration) $3;
\r
1071 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
\r
1072 decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1);
\r
1074 // Note again, checking is done in semantic analysis
\r
1075 current_container.AddOperator (op);
\r
1079 operator_declarator
\r
1080 : type OPERATOR overloadable_operator
\r
1081 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
\r
1083 CheckUnaryOperator ((Operator.OpType) $3);
\r
1085 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6, null, null);
\r
1087 | type OPERATOR overloadable_operator
\r
1089 type IDENTIFIER COMMA
\r
1093 CheckBinaryOperator ((Operator.OpType) $3);
\r
1095 $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
\r
1096 (string) $8, (string) $9);
\r
1098 | conversion_operator_declarator
\r
1101 overloadable_operator
\r
1102 // Unary operators:
\r
1103 : BANG { $$ = Operator.OpType.Negate; }
\r
1104 | TILDE { $$ = Operator.OpType.BitComplement; }
\r
1105 | OP_INC { $$ = Operator.OpType.Increment; }
\r
1106 | OP_DEC { $$ = Operator.OpType.Decrement; }
\r
1107 | TRUE { $$ = Operator.OpType.True; }
\r
1108 | FALSE { $$ = Operator.OpType.False; }
\r
1109 // Unary and binary:
\r
1110 | PLUS { $$ = Operator.OpType.Add; }
\r
1111 | MINUS { $$ = Operator.OpType.Subtract; }
\r
1113 | STAR { $$ = Operator.OpType.Multiply; }
\r
1114 | DIV { $$ = Operator.OpType.Divide; }
\r
1115 | PERCENT { $$ = Operator.OpType.Modulo; }
\r
1116 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
\r
1117 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
\r
1118 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
\r
1119 | OP_SHIFT_LEFT { $$ = Operator.OpType.ShiftLeft; }
\r
1120 | OP_SHIFT_RIGHT { $$ = Operator.OpType.ShiftRight; }
\r
1121 | OP_EQ { $$ = Operator.OpType.Equal; }
\r
1122 | OP_NE { $$ = Operator.OpType.NotEqual; }
\r
1123 | OP_GT { $$ = Operator.OpType.GreaterThan; }
\r
1124 | OP_LT { $$ = Operator.OpType.LessThan; }
\r
1125 | OP_GE { $$ = Operator.OpType.GreaterOrEqual; }
\r
1126 | OP_LE { $$ = Operator.OpType.LesserOrEqual; }
\r
1129 conversion_operator_declarator
\r
1130 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
\r
1132 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
\r
1135 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
\r
1137 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
\r
1142 constructor_declaration
\r
1145 constructor_declarator
\r
1148 Constructor c = (Constructor) $3;
\r
1149 c.Block = (Block) $4;
\r
1150 c.ModFlags = (int) $2;
\r
1152 if ((c.ModFlags & Modifiers.STATIC) != 0){
\r
1153 if ((c.ModFlags & Modifiers.Accessibility) != 0) {
\r
1154 Location l = lexer.Location;
\r
1155 rc.Report.Error (515, l, "Access modifiers are not allowed on static constructors");
\r
1158 if (c.Initializer != null){
\r
1159 Location l = lexer.Location;
\r
1160 rc.Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
\r
1163 if (!c.Parameters.Empty){
\r
1164 Location l = lexer.Location;
\r
1165 rc.Report.Error (103, l, "Static constructors should not have parameters");
\r
1168 if (c.Initializer == null)
\r
1169 c.Initializer = new ConstructorBaseInitializer (null, lexer.Location);
\r
1171 CheckDef (current_container.AddConstructor (c), c.Name);
\r
1173 current_local_parameters = null;
\r
1177 constructor_declarator
\r
1179 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
\r
1180 opt_constructor_initializer
\r
1182 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5);
\r
1184 current_local_parameters = (Parameters) $3;
\r
1188 opt_constructor_initializer
\r
1189 : /* empty */ { $$ = null; }
\r
1190 | constructor_initializer
\r
1193 constructor_initializer
\r
1194 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
\r
1196 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
\r
1198 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
\r
1200 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
\r
1204 destructor_declaration
\r
1205 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
\r
1207 Method d = new Method ("System.Void", 0, "Finalize", new Parameters (null, null), (Attributes) $1);
\r
1209 d.Block = (Block) $6;
\r
1210 CheckDef (current_container.AddMethod (d), d.Name);
\r
1217 EVENT type variable_declarators SEMICOLON
\r
1219 foreach (VariableDeclaration var in (ArrayList) $5) {
\r
1221 // FIXME : Is this right ?
\r
1222 Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
\r
1223 (int) $2, null, null, (Attributes) $1);
\r
1225 CheckDef (current_container.AddEvent (e), e.Name);
\r
1231 EVENT type member_name
\r
1232 OPEN_BRACE event_accessor_declarations CLOSE_BRACE
\r
1234 DictionaryEntry pair = (DictionaryEntry) $7;
\r
1235 Block add_block = null;
\r
1236 Block rem_block = null;
\r
1238 if (pair.Key != null)
\r
1239 add_block = (Block) pair.Key;
\r
1240 if (pair.Value != null)
\r
1241 rem_block = (Block) pair.Value;
\r
1243 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block, (Attributes) $1);
\r
1245 CheckDef (current_container.AddEvent (e), e.Name);
\r
1249 event_accessor_declarations
\r
1250 : add_accessor_declaration remove_accessor_declaration
\r
1252 $$ = new DictionaryEntry ($1, $2);
\r
1254 | remove_accessor_declaration add_accessor_declaration
\r
1256 $$ = new DictionaryEntry ($2, $1);
\r
1260 add_accessor_declaration
\r
1261 : opt_attributes ADD block
\r
1267 remove_accessor_declaration
\r
1268 : opt_attributes REMOVE block
\r
1274 indexer_declaration
\r
1275 : opt_attributes opt_modifiers indexer_declarator
\r
1278 IndexerDeclaration decl = (IndexerDeclaration) $3;
\r
1280 lexer.properties = true;
\r
1281 parsing_indexer = true;
\r
1283 current_local_parameters = decl.param_list;
\r
1285 accessor_declarations
\r
1287 lexer.properties = false;
\r
1288 parsing_indexer = false;
\r
1292 // The signature is computed from the signature of the indexer. Look
\r
1293 // at section 3.6 on the spec
\r
1296 IndexerDeclaration decl = (IndexerDeclaration) $3;
\r
1297 DictionaryEntry pair = (DictionaryEntry) $6;
\r
1298 Block get_block = null;
\r
1299 Block set_block = null;
\r
1301 if (pair.Key != null)
\r
1302 get_block = (Block) pair.Key;
\r
1303 if (pair.Value != null)
\r
1304 set_block = (Block) pair.Value;
\r
1306 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
\r
1307 get_block, set_block, (Attributes) $1);
\r
1309 // Note that there is no equivalent of CheckDef for this case
\r
1310 // We shall handle this in semantic analysis
\r
1312 current_container.AddIndexer (indexer);
\r
1314 current_local_parameters = null;
\r
1318 indexer_declarator
\r
1319 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
\r
1321 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
\r
1323 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
\r
1325 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
\r
1337 string name = (string) $4;
\r
1338 Enum e = new Enum ((string) $5, (int) $2, name, (Attributes) $1);
\r
1340 foreach (VariableDeclaration ev in (ArrayList) $6){
\r
1341 CheckDef (e.AddEnumMember (ev.identifier,
\r
1342 (Expression) ev.expression_or_array_initializer),
\r
1346 CheckDef (current_container.AddEnum (e), name);
\r
1351 : /* empty */ { $$ = "System.Int32"; }
\r
1352 | COLON integral_type { $$ = $2; }
\r
1356 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
\r
1362 opt_enum_member_declarations
\r
1363 : /* empty */ { $$ = new ArrayList (); }
\r
1364 | enum_member_declarations opt_comma { $$ = $1; }
\r
1367 enum_member_declarations
\r
1368 : enum_member_declaration
\r
1370 ArrayList l = new ArrayList ();
\r
1375 | enum_member_declarations COMMA enum_member_declaration
\r
1377 ArrayList l = (ArrayList) $1;
\r
1385 enum_member_declaration
\r
1386 : opt_attributes IDENTIFIER
\r
1388 $$ = new VariableDeclaration ((string) $2, null);
\r
1390 | opt_attributes IDENTIFIER ASSIGN expression
\r
1392 $$ = new VariableDeclaration ((string) $2, $4);
\r
1396 delegate_declaration
\r
1400 IDENTIFIER OPEN_PARENS
\r
1401 formal_parameter_list
\r
1405 Delegate del = new Delegate ((string) $4, (int) $2, (string) $5, (Parameters) $7, (Attributes) $1);
\r
1407 CheckDef (current_container.AddDelegate (del), del.Name);
\r
1412 IDENTIFIER OPEN_PARENS
\r
1413 formal_parameter_list
\r
1417 Delegate del = new Delegate (null, (int) $2, (string) $5, (Parameters) $7, (Attributes) $1);
\r
1419 CheckDef (current_container.AddDelegate (del), del.Name);
\r
1424 : namespace_or_type_name
\r
1427 namespace_or_type_name
\r
1428 : qualified_identifier
\r
1432 * Before you think of adding a return_type, notice that we have been
\r
1433 * using two rules in the places where it matters (one rule using type
\r
1434 * and another identical one that uses VOID as the return type). This
\r
1435 * gets rid of a shift/reduce couple
\r
1438 : type_name { /* class_type */
\r
1440 This does interfaces, delegates, struct_types, class_types,
\r
1441 parent classes, and more! 4.2
\r
1452 ArrayList types = new ArrayList ();
\r
1457 | type_list COMMA type
\r
1459 ArrayList types = (ArrayList) $1;
\r
1467 * replaces all the productions for isolating the various
\r
1468 * simple types, but we need this to reuse it easily in local_variable_type
\r
1471 : OBJECT { $$ = "System.Object"; }
\r
1472 | STRING { $$ = "System.String"; }
\r
1473 | BOOL { $$ = "System.Boolean"; }
\r
1474 | DECIMAL { $$ = "System.Decimal"; }
\r
1475 | FLOAT { $$ = "System.Single"; }
\r
1476 | DOUBLE { $$ = "System.Double"; }
\r
1481 : SBYTE { $$ = "System.SByte"; }
\r
1482 | BYTE { $$ = "System.Byte"; }
\r
1483 | SHORT { $$ = "System.Int16"; }
\r
1484 | USHORT { $$ = "System.UInt16"; }
\r
1485 | INT { $$ = "System.Int32"; }
\r
1486 | UINT { $$ = "System.UInt32"; }
\r
1487 | LONG { $$ = "System.Int64"; }
\r
1488 | ULONG { $$ = "System.UInt64"; }
\r
1489 | CHAR { $$ = "System.Char"; }
\r
1497 : type rank_specifiers
\r
1499 $$ = (string) $1 + (string) $2;
\r
1504 // Expressions, section 7.5
\r
1506 primary_expression
\r
1509 // 7.5.1: Literals
\r
1512 | qualified_identifier
\r
1514 string name = (string) $1;
\r
1517 $$ = QualifiedIdentifier (name, lexer.Location);
\r
1519 | parenthesized_expression
\r
1521 | invocation_expression
\r
1525 | post_increment_expression
\r
1526 | post_decrement_expression
\r
1528 | typeof_expression
\r
1529 | sizeof_expression
\r
1530 | checked_expression
\r
1531 | unchecked_expression
\r
1538 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
\r
1539 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
\r
1540 | NULL { $$ = new NullLiteral (); }
\r
1544 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
\r
1545 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
\r
1546 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
\r
1550 : LITERAL_INTEGER { $$ = new IntLiteral ((Int32) lexer.Value); }
\r
1554 : TRUE { $$ = new BoolLiteral (true); }
\r
1555 | FALSE { $$ = new BoolLiteral (false); }
\r
1558 parenthesized_expression
\r
1559 : OPEN_PARENS expression CLOSE_PARENS
\r
1564 : primary_expression DOT IDENTIFIER
\r
1566 $$ = new MemberAccess ((Expression) $1, (string) $3);
\r
1568 | predefined_type DOT IDENTIFIER
\r
1570 $$ = new BuiltinTypeAccess ((string) $1, (string) $3);
\r
1578 invocation_expression
\r
1579 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
\r
1582 // if $1 is MethodGroup
\r
1583 // $$ = new Call ($1, $3);
\r
1585 // $$ = new DelegateCall ($1, $3);
\r
1587 Location l = lexer.Location;
\r
1588 rc.Report.Error (1, l, "THIS IS CRAZY");
\r
1590 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
\r
1596 : /* empty */ { $$ = null; }
\r
1603 ArrayList list = new ArrayList ();
\r
1607 | argument_list COMMA argument
\r
1609 ArrayList list = (ArrayList) $1;
\r
1618 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
\r
1620 | REF variable_reference
\r
1622 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
\r
1624 | OUT variable_reference
\r
1626 $$ = new Argument ((Expression) $2, Argument.AType.Out);
\r
1630 variable_reference
\r
1631 : expression { note ("section 5.4"); $$ = $1; }
\r
1635 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
\r
1637 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
\r
1644 ArrayList list = new ArrayList ();
\r
1648 | expression_list COMMA expression
\r
1650 ArrayList list = (ArrayList) $1;
\r
1664 : BASE DOT IDENTIFIER
\r
1666 $$ = new BaseAccess (BaseAccess.BaseAccessType.Member, (string) $3, null);
\r
1668 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
\r
1670 $$ = new BaseAccess (BaseAccess.BaseAccessType.Indexer, null, (ArrayList) $3);
\r
1674 post_increment_expression
\r
1675 : primary_expression OP_INC
\r
1677 $$ = new Unary (Unary.Operator.PostIncrement, (Expression) $1, lexer.Location);
\r
1681 post_decrement_expression
\r
1682 : primary_expression OP_DEC
\r
1684 $$ = new Unary (Unary.Operator.PostDecrement, (Expression) $1, lexer.Location);
\r
1689 : object_or_delegate_creation_expression
\r
1690 | array_creation_expression
\r
1693 object_or_delegate_creation_expression
\r
1694 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
\r
1696 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
\r
1700 array_creation_expression
\r
1701 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
\r
1702 opt_rank_specifier
\r
1703 opt_array_initializer
\r
1705 $$ = new New ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
\r
1709 opt_rank_specifier
\r
1725 | rank_specifier rank_specifiers
\r
1727 $$ = (string) $1 + (string) $2;
\r
1732 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
\r
1734 $$ = "[" + (string) $2 + "]";
\r
1738 opt_dim_separators
\r
1754 | dim_separators COMMA
\r
1756 $$ =(string) $1 + ",";
\r
1760 opt_array_initializer
\r
1765 | array_initializer
\r
1772 : OPEN_BRACE CLOSE_BRACE
\r
1774 ArrayList list = new ArrayList ();
\r
1777 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
\r
1779 $$ = (ArrayList) $2;
\r
1783 variable_initializer_list
\r
1784 : variable_initializer
\r
1786 ArrayList list = new ArrayList ();
\r
1790 | variable_initializer_list COMMA variable_initializer
\r
1792 ArrayList list = (ArrayList) $1;
\r
1799 : TYPEOF OPEN_PARENS type CLOSE_PARENS
\r
1801 $$ = new TypeOf ((string) $3);
\r
1806 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
\r
1807 $$ = new SizeOf ((string) $3);
\r
1809 note ("Verify type is unmanaged");
\r
1810 note ("if (5.8) builtin, yield constant expression");
\r
1814 checked_expression
\r
1815 : CHECKED OPEN_PARENS expression CLOSE_PARENS
\r
1817 $$ = new CheckedExpr ((Expression) $3);
\r
1821 unchecked_expression
\r
1822 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
\r
1824 $$ = new UnCheckedExpr ((Expression) $3);
\r
1829 : primary_expression
\r
1830 | PLUS unary_expression {
\r
1831 $$ = new Unary (Unary.Operator.Add, (Expression) $2, lexer.Location);
\r
1833 | MINUS unary_expression
\r
1835 $$ = new Unary (Unary.Operator.Subtract, (Expression) $2, lexer.Location);
\r
1837 | BANG unary_expression
\r
1839 $$ = new Unary (Unary.Operator.Negate, (Expression) $2, lexer.Location);
\r
1841 | TILDE unary_expression
\r
1843 $$ = new Unary (Unary.Operator.BitComplement, (Expression) $2, lexer.Location);
\r
1845 | STAR unary_expression
\r
1847 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
\r
1849 | BITWISE_AND unary_expression
\r
1851 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
\r
1853 | OP_INC unary_expression
\r
1855 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
\r
1857 | OP_DEC unary_expression
\r
1859 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
\r
1861 | cast_expression
\r
1863 we can not do cast expressions at this level,
\r
1864 as there is an ambiguity. Check "Cast Expressions" 7.6.8
\r
1865 for the recipe to handle this.
\r
1869 pre_increment_expression
\r
1870 : OP_INC unary_expression
\r
1872 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
\r
1876 pre_decrement_expression
\r
1877 : OP_DEC unary_expression
\r
1879 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
\r
1885 * FIXME: This is actually wrong, it should be `type' but that
\r
1886 * introduces a lot of {shift,reduce}/reduces
\r
1888 * This is really really wrong. We need to track down
\r
1889 * the source of problems with QIs because expressions like:
\r
1890 * foreach (string s in (string []) object) wont be parsed.
\r
1892 : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
\r
1894 $$ = new Cast ((string) $2, (Expression) $4);
\r
1896 | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
\r
1898 $$ = new Cast ((string) $2, (Expression) $4);
\r
1902 multiplicative_expression
\r
1903 : unary_expression
\r
1904 | multiplicative_expression STAR unary_expression
\r
1906 $$ = new Binary (Binary.Operator.Multiply,
\r
1907 (Expression) $1, (Expression) $3, lexer.Location);
\r
1909 | multiplicative_expression DIV unary_expression
\r
1911 $$ = new Binary (Binary.Operator.Divide,
\r
1912 (Expression) $1, (Expression) $3, lexer.Location);
\r
1914 | multiplicative_expression PERCENT unary_expression
\r
1916 $$ = new Binary (Binary.Operator.Modulo,
\r
1917 (Expression) $1, (Expression) $3, lexer.Location);
\r
1921 additive_expression
\r
1922 : multiplicative_expression
\r
1923 | additive_expression PLUS multiplicative_expression
\r
1925 $$ = new Binary (Binary.Operator.Add,
\r
1926 (Expression) $1, (Expression) $3, lexer.Location);
\r
1928 | additive_expression MINUS multiplicative_expression
\r
1930 $$ = new Binary (Binary.Operator.Subtract,
\r
1931 (Expression) $1, (Expression) $3, lexer.Location);
\r
1936 : additive_expression
\r
1937 | shift_expression OP_SHIFT_LEFT additive_expression
\r
1939 $$ = new Binary (Binary.Operator.ShiftLeft,
\r
1940 (Expression) $1, (Expression) $3, lexer.Location);
\r
1942 | shift_expression OP_SHIFT_RIGHT additive_expression
\r
1944 $$ = new Binary (Binary.Operator.ShiftRight,
\r
1945 (Expression) $1, (Expression) $3, lexer.Location);
\r
1949 relational_expression
\r
1950 : shift_expression
\r
1951 | relational_expression OP_LT shift_expression
\r
1953 $$ = new Binary (Binary.Operator.LessThan,
\r
1954 (Expression) $1, (Expression) $3, lexer.Location);
\r
1956 | relational_expression OP_GT shift_expression
\r
1958 $$ = new Binary (Binary.Operator.GreaterThan,
\r
1959 (Expression) $1, (Expression) $3, lexer.Location);
\r
1961 | relational_expression OP_LE shift_expression
\r
1963 $$ = new Binary (Binary.Operator.LessOrEqual,
\r
1964 (Expression) $1, (Expression) $3, lexer.Location);
\r
1966 | relational_expression OP_GE shift_expression
\r
1968 $$ = new Binary (Binary.Operator.GreaterOrEqual,
\r
1969 (Expression) $1, (Expression) $3, lexer.Location);
\r
1971 | relational_expression IS type
\r
1973 $$ = new Probe (Probe.Operator.Is,
\r
1974 (Expression) $1, (string) $3);
\r
1976 | relational_expression AS type
\r
1978 $$ = new Probe (Probe.Operator.As,
\r
1979 (Expression) $1, (string) $3);
\r
1983 equality_expression
\r
1984 : relational_expression
\r
1985 | equality_expression OP_EQ relational_expression
\r
1987 $$ = new Binary (Binary.Operator.Equal,
\r
1988 (Expression) $1, (Expression) $3, lexer.Location);
\r
1990 | equality_expression OP_NE relational_expression
\r
1992 $$ = new Binary (Binary.Operator.NotEqual,
\r
1993 (Expression) $1, (Expression) $3, lexer.Location);
\r
1998 : equality_expression
\r
1999 | and_expression BITWISE_AND equality_expression
\r
2001 $$ = new Binary (Binary.Operator.BitwiseAnd,
\r
2002 (Expression) $1, (Expression) $3, lexer.Location);
\r
2006 exclusive_or_expression
\r
2008 | exclusive_or_expression CARRET and_expression
\r
2010 $$ = new Binary (Binary.Operator.ExclusiveOr,
\r
2011 (Expression) $1, (Expression) $3, lexer.Location);
\r
2015 inclusive_or_expression
\r
2016 : exclusive_or_expression
\r
2017 | inclusive_or_expression BITWISE_OR exclusive_or_expression
\r
2019 $$ = new Binary (Binary.Operator.BitwiseOr,
\r
2020 (Expression) $1, (Expression) $3, lexer.Location);
\r
2024 conditional_and_expression
\r
2025 : inclusive_or_expression
\r
2026 | conditional_and_expression OP_AND inclusive_or_expression
\r
2028 $$ = new Binary (Binary.Operator.LogicalAnd,
\r
2029 (Expression) $1, (Expression) $3, lexer.Location);
\r
2033 conditional_or_expression
\r
2034 : conditional_and_expression
\r
2035 | conditional_or_expression OP_OR conditional_and_expression
\r
2037 $$ = new Binary (Binary.Operator.LogicalOr,
\r
2038 (Expression) $1, (Expression) $3, lexer.Location);
\r
2042 conditional_expression
\r
2043 : conditional_or_expression
\r
2044 | conditional_or_expression INTERR expression COLON expression
\r
2046 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
\r
2050 assignment_expression
\r
2051 : unary_expression ASSIGN expression
\r
2053 $$ = new Assign ((Expression) $1, (Expression) $3);
\r
2055 | unary_expression OP_MULT_ASSIGN expression
\r
2057 $$ = new Assign ((Expression) $1,
\r
2058 new Binary (Binary.Operator.Multiply,
\r
2060 (Expression) $3, lexer.Location));
\r
2062 | unary_expression OP_DIV_ASSIGN expression
\r
2064 $$ = new Assign ((Expression) $1,
\r
2065 new Binary (Binary.Operator.Divide,
\r
2067 (Expression) $3, lexer.Location));
\r
2069 | unary_expression OP_MOD_ASSIGN expression
\r
2071 $$ = new Assign ((Expression) $1,
\r
2072 new Binary (Binary.Operator.Modulo,
\r
2074 (Expression) $3, lexer.Location));
\r
2076 | unary_expression OP_ADD_ASSIGN expression
\r
2078 $$ = new Assign ((Expression) $1,
\r
2079 new Binary (Binary.Operator.Add,
\r
2081 (Expression) $3, lexer.Location));
\r
2083 | unary_expression OP_SUB_ASSIGN expression
\r
2085 $$ = new Assign ((Expression) $1,
\r
2086 new Binary (Binary.Operator.Subtract,
\r
2088 (Expression) $3, lexer.Location));
\r
2090 | unary_expression OP_SHIFT_LEFT_ASSIGN expression
\r
2092 $$ = new Assign ((Expression) $1,
\r
2093 new Binary (Binary.Operator.ShiftLeft,
\r
2095 (Expression) $3, lexer.Location));
\r
2097 | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
\r
2099 $$ = new Assign ((Expression) $1,
\r
2100 new Binary (Binary.Operator.ShiftRight,
\r
2102 (Expression) $3, lexer.Location));
\r
2104 | unary_expression OP_AND_ASSIGN expression
\r
2106 $$ = new Assign ((Expression) $1,
\r
2107 new Binary (Binary.Operator.BitwiseAnd,
\r
2109 (Expression) $3, lexer.Location));
\r
2111 | unary_expression OP_OR_ASSIGN expression
\r
2113 $$ = new Assign ((Expression) $1,
\r
2114 new Binary (Binary.Operator.BitwiseOr,
\r
2116 (Expression) $3, lexer.Location));
\r
2118 | unary_expression OP_XOR_ASSIGN expression
\r
2120 $$ = new Assign ((Expression) $1,
\r
2121 new Binary (Binary.Operator.ExclusiveOr,
\r
2123 (Expression) $3, lexer.Location));
\r
2128 : conditional_expression
\r
2129 | assignment_expression
\r
2132 constant_expression
\r
2136 boolean_expression
\r
2137 : expression { CheckBoolean ((Expression) $1); $$ = $1; }
\r
2149 string full_class_name = MakeName ((string) $4);
\r
2151 new_class = new Class (rc, current_container, full_class_name, (int) $2, (Attributes) $1);
\r
2152 current_container = new_class;
\r
2153 current_container.Namespace = current_namespace;
\r
2154 tree.RecordClass (full_class_name, new_class);
\r
2160 Class new_class = (Class) current_container;
\r
2163 new_class.Bases = (ArrayList) $6;
\r
2165 current_container = current_container.Parent;
\r
2166 CheckDef (current_container.AddClass (new_class), new_class.Name);
\r
2173 : /* empty */ { $$ = (int) 0; }
\r
2179 | modifiers modifier
\r
2181 int m1 = (int) $1;
\r
2182 int m2 = (int) $2;
\r
2184 if ((m1 & m2) != 0) {
\r
2185 Location l = lexer.Location;
\r
2186 rc.Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
\r
2188 $$ = (int) (m1 | m2);
\r
2193 : NEW { $$ = Modifiers.NEW; }
\r
2194 | PUBLIC { $$ = Modifiers.PUBLIC; }
\r
2195 | PROTECTED { $$ = Modifiers.PROTECTED; }
\r
2196 | INTERNAL { $$ = Modifiers.INTERNAL; }
\r
2197 | PRIVATE { $$ = Modifiers.PRIVATE; }
\r
2198 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
\r
2199 | SEALED { $$ = Modifiers.SEALED; }
\r
2200 | STATIC { $$ = Modifiers.STATIC; }
\r
2201 | READONLY { $$ = Modifiers.READONLY; }
\r
2202 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
\r
2203 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
\r
2204 | EXTERN { $$ = Modifiers.EXTERN; }
\r
2208 : /* empty */ { $$ = null; }
\r
2209 | class_base { $$ = $1; }
\r
2213 : COLON type_list { $$ = $2; }
\r
2217 // Statements (8.2)
\r
2221 // A block is "contained" on the following places:
\r
2223 // property_declaration as part of the accessor body (get/set)
\r
2224 // operator_declaration
\r
2225 // constructor_declaration
\r
2226 // destructor_declaration
\r
2227 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
\r
2232 current_block = new Block (current_block);
\r
2234 opt_statement_list CLOSE_BRACE
\r
2236 while (current_block.Implicit)
\r
2237 current_block = current_block.Parent;
\r
2238 $$ = current_block;
\r
2239 current_block = current_block.Parent;
\r
2243 opt_statement_list
\r
2250 | statement_list statement
\r
2254 : declaration_statement
\r
2256 if ((Block) $1 != current_block){
\r
2257 current_block.AddStatement ((Statement) $1);
\r
2258 current_block = (Block) $1;
\r
2261 | embedded_statement
\r
2263 current_block.AddStatement ((Statement) $1);
\r
2265 | labeled_statement
\r
2267 current_block.AddStatement ((Statement) $1);
\r
2271 embedded_statement
\r
2274 | expression_statement
\r
2275 | selection_statement
\r
2276 | iteration_statement
\r
2279 | checked_statement
\r
2280 | unchecked_statement
\r
2288 $$ = new EmptyStatement ();
\r
2293 : IDENTIFIER COLON statement
\r
2295 string lab = (String) $1;
\r
2298 block = new Block (current_block, lab);
\r
2299 block.AddStatement ((Statement) $3);
\r
2302 if (!current_block.AddLabel (lab, block)){
\r
2303 Location l = lexer.Location;
\r
2304 rc.Report.Error (140, l, "The label '" + lab + "' is a duplicate");
\r
2310 declaration_statement
\r
2311 : local_variable_declaration SEMICOLON // done
\r
2312 | local_constant_declaration SEMICOLON // finishme
\r
2316 * The following is from Rhys' grammar:
\r
2317 * > Types in local variable declarations must be recognized as
\r
2318 * > expressions to prevent reduce/reduce errors in the grammar.
\r
2319 * > The expressions are converted into types during semantic analysis.
\r
2321 local_variable_type
\r
2322 : primary_expression type_suffixes
\r
2324 // FIXME: Do something smart here regarding the composition of the type.
\r
2326 // Ok, the above "primary_expression" is there to get rid of
\r
2327 // both reduce/reduce and shift/reduces in the grammar, it should
\r
2328 // really just be "type_name". If you use type_name, a reduce/reduce
\r
2329 // creeps up. If you use qualified_identifier (which is all we need
\r
2330 // really) two shift/reduces appear.
\r
2332 // So, instead we do a super trick: we just allow ($1) to be a
\r
2333 // SimpleName Expression.
\r
2335 if (((Expression) $1) is SimpleName)
\r
2336 $$ = ((SimpleName) $1).Name;
\r
2338 Location l = lexer.Location;
\r
2339 rc.Report.Error (-1, l, "Invalid Type definition");
\r
2340 $$ = "System.Object";
\r
2343 | builtin_types type_suffixes
\r
2345 $$ = (string) $1 + (string) $2;
\r
2349 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
\r
2352 // // FIXME: this is a string that represents the type
\r
2353 // // Figure out something to make this work.
\r
2363 | type_suffix_list
\r
2368 | type_suffix_list type_suffix
\r
2370 $$ = (string) $1 + (string) $2;
\r
2375 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
\r
2377 $$ = "[" + (string) $2 + "]";
\r
2381 local_variable_declaration
\r
2382 : local_variable_type variable_declarators
\r
2384 $$ = declare_local_variables ((string) $1, (ArrayList) $2);
\r
2388 local_constant_declaration
\r
2389 : CONST type constant_declarator
\r
2393 expression_statement
\r
2394 : statement_expression SEMICOLON
\r
2401 // We have to do the wrapping here and not in the case above,
\r
2402 // because statement_expression is used for example in for_statement
\r
2404 statement_expression
\r
2405 : invocation_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2406 | object_creation_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2407 | assignment_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2408 | post_increment_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2409 | post_decrement_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2410 | pre_increment_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2411 | pre_decrement_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2414 object_creation_expression
\r
2415 : object_or_delegate_creation_expression
\r
2416 { note ("complain if this is a delegate maybe?"); }
\r
2419 selection_statement
\r
2421 | switch_statement
\r
2425 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
\r
2426 embedded_statement
\r
2428 $$ = new If ((Expression) $3, (Statement) $5);
\r
2430 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
\r
2431 embedded_statement ELSE embedded_statement
\r
2433 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
\r
2438 : SWITCH OPEN_PARENS expression CLOSE_PARENS
\r
2441 $$ = new Switch ((Expression) $3, (ArrayList) $5);
\r
2447 opt_switch_sections
\r
2454 opt_switch_sections
\r
2455 : /* empty */ { $$ = new ArrayList (); }
\r
2462 ArrayList sections = new ArrayList ();
\r
2464 sections.Add ($1);
\r
2467 | switch_sections switch_section
\r
2469 ArrayList sections = (ArrayList) $1;
\r
2471 sections.Add ($2);
\r
2479 current_block = new Block (current_block);
\r
2483 while (current_block.Implicit)
\r
2484 current_block = current_block.Parent;
\r
2485 $$ = new SwitchSection ((ArrayList) $1, current_block);
\r
2486 current_block = current_block.Parent;
\r
2493 ArrayList labels = new ArrayList ();
\r
2498 | switch_labels switch_label
\r
2500 ArrayList labels = (ArrayList) ($1);
\r
2508 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2); }
\r
2509 | DEFAULT COLON { $$ = new SwitchLabel (null); }
\r
2512 iteration_statement
\r
2516 | foreach_statement
\r
2520 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
\r
2522 $$ = new While ((Expression) $3, (Statement) $5);
\r
2527 : DO embedded_statement
\r
2528 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
\r
2530 $$ = new Do ((Statement) $2, (Expression) $5);
\r
2535 : FOR OPEN_PARENS
\r
2536 opt_for_initializer SEMICOLON
\r
2537 opt_for_condition SEMICOLON
\r
2538 opt_for_iterator CLOSE_PARENS
\r
2539 embedded_statement
\r
2541 $$ = new For ((Statement) $3, (Expression) $5, (Statement) $7, (Statement) $9);
\r
2545 opt_for_initializer
\r
2546 : /* empty */ { $$ = new EmptyStatement (); }
\r
2547 | for_initializer
\r
2551 : local_variable_declaration
\r
2552 | statement_expression_list
\r
2556 : /* empty */ { $$ = new BoolLiteral (true); }
\r
2557 | boolean_expression
\r
2561 : /* empty */ { $$ = new EmptyStatement (); }
\r
2566 : statement_expression_list
\r
2569 statement_expression_list
\r
2570 : statement_expression
\r
2572 Block b = new Block (null, true);
\r
2574 b.AddStatement ((Statement) $1);
\r
2577 | statement_expression_list COMMA statement_expression
\r
2579 Block b = (Block) $1;
\r
2581 b.AddStatement ((Statement) $3);
\r
2587 : FOREACH OPEN_PARENS type IDENTIFIER IN expression CLOSE_PARENS
\r
2588 embedded_statement
\r
2590 string temp_id = current_block.MakeInternalID ();
\r
2591 Expression assign_e, ma;
\r
2592 Statement getcurrent;
\r
2593 Block foreach_block, child_block;
\r
2595 foreach_block = new Block (current_block, true);
\r
2597 foreach_block.AddVariable ("System.IEnumerator", temp_id);
\r
2598 foreach_block.AddVariable ((string) $3, (string) $4);
\r
2599 assign_e = new Assign (new LocalVariableReference (foreach_block, temp_id),
\r
2601 new MemberAccess ((Expression) $6, "GetEnumerator"),
\r
2602 null, lexer.Location));
\r
2603 current_block.AddStatement (new StatementExpression (assign_e));
\r
2604 ma = new MemberAccess (new LocalVariableReference (foreach_block, temp_id), "MoveNext");
\r
2605 child_block = new Block (current_block);
\r
2607 getcurrent = new StatementExpression (
\r
2609 new LocalVariableReference (foreach_block, (string) $4),
\r
2612 new MemberAccess (
\r
2613 new LocalVariableReference (foreach_block, temp_id), "Current"))));
\r
2615 child_block.AddStatement (getcurrent);
\r
2616 child_block.AddStatement ((Statement) $8);
\r
2617 foreach_block.AddStatement (new While (ma, (Statement) child_block));
\r
2619 $$ = foreach_block;
\r
2625 | continue_statement
\r
2627 | return_statement
\r
2634 $$ = new Break ();
\r
2638 continue_statement
\r
2639 : CONTINUE SEMICOLON
\r
2641 $$ = new Continue ();
\r
2646 : GOTO IDENTIFIER SEMICOLON
\r
2648 $$ = new Goto ((string) $2);
\r
2650 | GOTO CASE constant_expression SEMICOLON
\r
2651 | GOTO DEFAULT SEMICOLON
\r
2655 : RETURN opt_expression SEMICOLON
\r
2657 $$ = new Return ((Expression) $2);
\r
2662 : THROW opt_expression SEMICOLON
\r
2664 $$ = new Throw ((Expression) $2);
\r
2674 : TRY block catch_clauses
\r
2677 ArrayList s = new ArrayList ();
\r
2679 foreach (Catch cc in (ArrayList) $3) {
\r
2680 if (cc.Type == null)
\r
2686 // Now s contains the list of specific catch clauses
\r
2687 // and g contains the general one.
\r
2689 $$ = new Try ((Block) $2, s, g, null);
\r
2691 | TRY block opt_catch_clauses FINALLY block
\r
2694 ArrayList s = new ArrayList ();
\r
2696 foreach (Catch cc in (ArrayList) $3) {
\r
2697 if (cc.Type == null)
\r
2703 $$ = new Try ((Block) $2, s, g, (Block) $5);
\r
2708 : /* empty */ { $$ = null; }
\r
2715 ArrayList l = new ArrayList ();
\r
2720 | catch_clauses catch_clause
\r
2722 ArrayList l = (ArrayList) $1;
\r
2730 : /* empty */ { $$ = null; }
\r
2735 : CATCH opt_catch_args block
\r
2737 string type = null;
\r
2741 DictionaryEntry cc = (DictionaryEntry) $2;
\r
2742 type = (string) cc.Key;
\r
2743 id = (string) cc.Value;
\r
2746 $$ = new Catch (type, id, (Block) $3);
\r
2751 : /* empty */ { $$ = null; }
\r
2756 : OPEN_PARENS type opt_identifier CLOSE_PARENS
\r
2758 $$ = new DictionaryEntry ($2, $3);
\r
2765 $$ = new Checked ((Block) $2);
\r
2769 unchecked_statement
\r
2772 $$ = new Unchecked ((Block) $2);
\r
2777 : LOCK OPEN_PARENS expression CLOSE_PARENS embedded_statement
\r
2779 $$ = new Lock ((Expression) $3, (Statement) $5);
\r
2784 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS embedded_statement
\r
2788 resource_acquisition
\r
2789 : local_variable_declaration
\r
2797 // A class used to pass around variable declarations and constants
\r
2799 public class VariableDeclaration {
\r
2800 public string identifier;
\r
2801 public object expression_or_array_initializer;
\r
2803 public VariableDeclaration (string id, object eoai){
\r
2804 this.identifier = id;
\r
2805 this.expression_or_array_initializer = eoai;
\r
2810 // A class used to hold info about an indexer declarator
\r
2813 public class IndexerDeclaration {
\r
2814 public string type;
\r
2815 public string interface_type;
\r
2816 public Parameters param_list;
\r
2818 public IndexerDeclaration (string type, string interface_type, Parameters param_list)
\r
2821 this.interface_type = interface_type;
\r
2822 this.param_list = param_list;
\r
2827 // A class used to hold info about an operator declarator
\r
2830 public class OperatorDeclaration {
\r
2831 public Operator.OpType optype;
\r
2832 public string ret_type;
\r
2833 public string arg1type;
\r
2834 public string arg1name;
\r
2835 public string arg2type;
\r
2836 public string arg2name;
\r
2838 public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
\r
2839 string arg2type, string arg2name)
\r
2842 this.ret_type = ret_type;
\r
2843 this.arg1type = arg1type;
\r
2844 this.arg1name = arg1name;
\r
2845 this.arg2type = arg2type;
\r
2846 this.arg2name = arg2name;
\r
2852 // Given the @class_name name, it creates a fully qualified name
\r
2853 // based on the containing declaration space
\r
2856 MakeName (string class_name)
\r
2858 string ns = current_namespace.Name;
\r
2859 string container_name = current_container.Name;
\r
2861 if (container_name == ""){
\r
2863 return ns + "." + class_name;
\r
2865 return class_name;
\r
2867 return container_name + "." + class_name;
\r
2871 // Used to report back to the user the result of a declaration
\r
2872 // in the current declaration space
\r
2875 CheckDef (DeclSpace.AdditionResult result, string name)
\r
2877 if (result == DeclSpace.AdditionResult.Success)
\r
2880 Location l = lexer.Location;
\r
2883 case DeclSpace.AdditionResult.NameExists:
\r
2884 rc.Report.Error (102, l, "The namespace `" + current_container.Name +
\r
2885 "' already contains a definition for `"+
\r
2890 // NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
\r
2892 // case DeclSpace.AdditionResult.MethodDuplicated:
\r
2893 // error (111, "Class `"+current_container.Name+
\r
2894 // "' already defines a member called '" +
\r
2895 // name + "' with the same parameter types");
\r
2898 case DeclSpace.AdditionResult.EnclosingClash:
\r
2899 rc.Report.Error (542, l, "Member names cannot be the same as their enclosing type");
\r
2902 case DeclSpace.AdditionResult.NotAConstructor:
\r
2903 rc.Report.Error (1520, l, "Class, struct, or interface method must have a return type");
\r
2909 CheckDef (bool result, string name)
\r
2913 CheckDef (DeclSpace.AdditionResult.NameExists, name);
\r
2917 SimpleLookup (string name)
\r
2920 // we need to check against current_block not being null
\r
2921 // as `expression' is allowed in argument_lists, which
\r
2922 // do not exist inside a block.
\r
2924 if (current_block != null){
\r
2925 if (current_block.IsVariableDefined (name))
\r
2926 return new LocalVariableReference (current_block, name);
\r
2929 if (current_local_parameters != null){
\r
2931 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
\r
2933 return new ParameterReference (current_local_parameters, idx, name);
\r
2940 // Assumes that the name contains a `.' and tries to perform a simple lookup
\r
2941 // and shape the result to be a MemberAccess on a Local/Parameter
\r
2943 object CompositeLookup (string name)
\r
2945 int pos = name.IndexOf (".");
\r
2946 string left = name.Substring (0, pos);
\r
2949 o = SimpleLookup (left);
\r
2951 string right = name.Substring (pos + 1);
\r
2952 return new MemberAccess ((Expression) o, right);
\r
2958 object QualifiedIdentifier (string name, Location l)
\r
2962 if (name.IndexOf ('.') == -1)
\r
2963 o = SimpleLookup (name);
\r
2965 o = CompositeLookup (name);
\r
2968 o = new SimpleName (name, l);
\r
2973 Block declare_local_variables (string type, ArrayList variable_declarators)
\r
2975 Block implicit_block;
\r
2976 ArrayList inits = null;
\r
2979 // We use the `Used' property to check whether statements
\r
2980 // have been added to the current block. If so, we need
\r
2981 // to create another block to contain the new declaration
\r
2982 // otherwise, as an optimization, we use the same block to
\r
2983 // add the declaration.
\r
2985 // FIXME: A further optimization is to check if the statements
\r
2986 // that were added were added as part of the initialization
\r
2987 // below. In which case, no other statements have been executed
\r
2988 // and we might be able to reduce the number of blocks for
\r
2989 // situations like this:
\r
2991 // int j = 1; int k = j + 1;
\r
2993 if (current_block.Used)
\r
2994 implicit_block = new Block (current_block, true);
\r
2996 implicit_block = new Block (current_block, true);
\r
2998 foreach (VariableDeclaration decl in variable_declarators){
\r
2999 if (implicit_block.AddVariable (type, decl.identifier)){
\r
3000 if (decl.expression_or_array_initializer != null){
\r
3001 if (inits == null)
\r
3002 inits = new ArrayList ();
\r
3006 Location l = lexer.Location;
\r
3007 rc.Report.Error (128, l, "A local variable `" + decl.identifier +
\r
3008 "' is already defined in this scope");
\r
3012 if (inits == null)
\r
3013 return implicit_block;
\r
3015 foreach (VariableDeclaration decl in inits){
\r
3016 if (decl.expression_or_array_initializer is Expression){
\r
3017 Expression expr = (Expression) decl.expression_or_array_initializer;
\r
3020 assign = new Assign (new LocalVariableReference (implicit_block, decl.identifier), expr);
\r
3021 implicit_block.AddStatement (new StatementExpression (assign));
\r
3023 Console.WriteLine ("Not handling Array initializers yet");
\r
3027 return implicit_block;
\r
3030 void CheckConstant (Expression expr)
\r
3035 void CheckBoolean (Expression expr)
\r
3040 void CheckAttributeTarget (string a)
\r
3044 case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
\r
3048 Location l = lexer.Location;
\r
3049 rc.Report.Error (658, l, "Invalid attribute target");
\r
3055 void CheckUnaryOperator (Operator.OpType op)
\r
3059 case Operator.OpType.Negate : case Operator.OpType.BitComplement : case Operator.OpType.Increment :
\r
3060 case Operator.OpType.Decrement :
\r
3061 case Operator.OpType.True : case Operator.OpType.False : case Operator.OpType.Add : case Operator.OpType.Subtract :
\r
3066 Location l = lexer.Location;
\r
3067 rc.Report.Error (1019, l, "Overloadable unary operator expected");
\r
3073 void CheckBinaryOperator (Operator.OpType op)
\r
3077 case Operator.OpType.Add : case Operator.OpType.Subtract : case Operator.OpType.Multiply :
\r
3078 case Operator.OpType.Divide :
\r
3079 case Operator.OpType.Modulo : case Operator.OpType.BitwiseAnd : case Operator.OpType.BitwiseOr :
\r
3080 case Operator.OpType.ExclusiveOr : case Operator.OpType.ShiftLeft : case Operator.OpType.ShiftRight :
\r
3081 case Operator.OpType.Equal : case Operator.OpType.NotEqual :
\r
3082 case Operator.OpType.GreaterThan : case Operator.OpType.LessThan : case Operator.OpType.GreaterOrEqual :
\r
3083 case Operator.OpType.LesserOrEqual :
\r
3088 Location l = lexer.Location;
\r
3089 rc.Report.Error (1020, l, "Overloadable binary operator expected");
\r
3095 void output (string s)
\r
3097 Console.WriteLine (s);
\r
3100 void note (string s)
\r
3102 // Used to put annotations
\r
3107 public Tokenizer Lexer {
\r
3113 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
\r
3115 current_namespace = new Namespace (null, "");
\r
3117 this.tree = rc.Tree;
\r
3119 this.input = input;
\r
3120 current_container = tree.Types;
\r
3121 current_container.Namespace = current_namespace;
\r
3123 lexer = new Tokenizer (input, name);
\r
3126 public override int parse ()
\r
3128 StringBuilder value = new StringBuilder ();
\r
3130 global_errors = 0;
\r
3132 if (yacc_verbose_flag)
\r
3133 yyparse (lexer, new yydebug.yyDebugSimple ());
\r
3136 } catch (Exception e){
\r
3137 // Console.WriteLine ("Fatal error: " + name);
\r
3138 // Console.WriteLine (lexer.location);
\r
3140 Console.WriteLine (lexer.location + " : Parsing error");
\r
3141 Console.WriteLine (e);
\r
3145 return global_errors;
\r