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
39 public class CSharpParser {
\r
40 static int global_errors;
\r
42 Namespace current_namespace;
\r
43 TypeContainer current_container;
\r
46 // Current block is used to add statements as we find
\r
50 Block current_block;
\r
53 // Current interface is used by the various declaration
\r
54 // productions in the interface declaration to "add"
\r
55 // the interfaces as we find them.
\r
57 Interface current_interface;
\r
60 // This is used by the unary_expression code to resolve
\r
61 // a name against a parameter.
\r
63 Parameters current_local_parameters;
\r
66 // Using during property parsing to describe the implicit
\r
67 // value parameter that is passed to the "set" accesor
\r
70 ParameterCollection implicit_value_parameters;
\r
73 // Here we keep track of type references.
\r
75 TypeRefManager type_references;
\r
78 // Used to record all types defined
\r
82 // Name of the file we are parsing
\r
85 // Input stream to parse from.
\r
86 public System.IO.Stream input;
\r
91 %token NONE /* This token is never returned by our lexer */
\r
92 %token ERROR // This is used not by the parser, but by the tokenizer.
\r
96 *These are the C# keywords
\r
176 /* C# keywords which are not really keywords */
\r
180 /* C# single character operators/punctuation. */
\r
181 %token OPEN_BRACE "{"
\r
182 %token CLOSE_BRACE "}"
\r
183 %token OPEN_BRACKET "["
\r
184 %token CLOSE_BRACKET "]"
\r
185 %token OPEN_PARENS "("
\r
186 %token CLOSE_PARENS ")"
\r
190 %token SEMICOLON ";"
\r
199 %token BITWISE_AND "&"
\r
200 %token BITWISE_OR "|"
\r
207 /* C# multi-character operators. */
\r
210 %token OP_SHIFT_LEFT "<<"
\r
211 %token OP_SHIFT_RIGHT ">>"
\r
218 %token OP_MULT_ASSIGN "*="
\r
219 %token OP_DIV_ASSIGN "/="
\r
220 %token OP_MOD_ASSIGN "%="
\r
221 %token OP_ADD_ASSIGN "+="
\r
222 %token OP_SUB_ASSIGN "-="
\r
223 %token OP_SHIFT_LEFT_ASSIGN "<<="
\r
224 %token OP_SHIFT_RIGHT_ASSIGN ">>="
\r
225 %token OP_AND_ASSIGN "&="
\r
226 %token OP_XOR_ASSIGN "^="
\r
227 %token OP_OR_ASSIGN "|="
\r
231 %token LITERAL_INTEGER "int literal"
\r
232 %token LITERAL_FLOAT "float literal"
\r
233 %token LITERAL_DOUBLE "double literal"
\r
234 %token LITERAL_DECIMAL "decimal literal"
\r
235 %token LITERAL_CHARACTER "character literal"
\r
236 %token LITERAL_STRING "string literal"
\r
240 /* Add precedence rules to solve dangling else s/r conflict */
\r
249 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
\r
251 %left STAR DIV PERCENT
\r
252 %right BANG CARRET UMINUS
\r
253 %nonassoc OP_INC OP_DEC
\r
255 %left OPEN_BRACKET OPEN_BRACE
\r
259 %start compilation_unit
\r
260 /*%start namespace_declaration */
\r
264 : opt_using_directives opt_attributes opt_namespace_member_declarations
\r
267 // At some point check that using only comes *before* any namespaces
\r
273 | using_directives using_directive
\r
277 : using_alias_directive
\r
278 | using_namespace_directive
\r
281 using_alias_directive
\r
282 : USING IDENTIFIER ASSIGN
\r
283 namespace_or_type_name SEMICOLON {
\r
287 using_namespace_directive
\r
288 : USING namespace_name SEMICOLON
\r
290 current_namespace.Using ((string) $2);
\r
294 namespace_declarations
\r
295 : namespace_declaration
\r
296 | namespace_declarations namespace_declaration
\r
298 namespace_declaration
\r
299 : NAMESPACE qualified_identifier
\r
301 current_namespace = new Namespace (current_namespace, (string) $2);
\r
303 namespace_body opt_semicolon
\r
305 current_namespace = current_namespace.Parent;
\r
319 qualified_identifier
\r
321 | qualified_identifier DOT IDENTIFIER {
\r
322 $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
\r
327 : namespace_or_type_name
\r
332 opt_using_directives
\r
333 opt_namespace_member_declarations
\r
338 opt_using_directives
\r
343 opt_namespace_member_declarations
\r
345 | namespace_member_declarations
\r
348 namespace_member_declarations
\r
349 : namespace_member_declaration
\r
350 | namespace_member_declarations namespace_member_declaration
\r
353 namespace_member_declaration
\r
360 Class c = (Class) $1;
\r
361 mod_flags = c.ModFlags;
\r
363 } else if ($1 is Struct){
\r
364 Struct s = (Struct) $1;
\r
365 mod_flags = s.ModFlags;
\r
371 // We remove this error until we can
\r
372 //if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
\r
373 // error (1527, "Namespace elements cant be explicitly " +
\r
374 // "declared private or protected in `" + name + "'");
\r
377 | namespace_declaration
\r
381 : class_declaration
\r
382 | struct_declaration
\r
383 | interface_declaration
\r
384 | enum_declaration
\r
385 | delegate_declaration // finishme
\r
393 | attribute_section opt_attributes
\r
398 opt_attribute_target_specifier attribute_list
\r
399 /* FIXME: opt_comma */
\r
403 opt_attribute_target_specifier
\r
405 | attribute_target_specifier
\r
408 attribute_target_specifier
\r
409 : attribute_target COLON
\r
414 // if (Collection.Contains ($$))... FIXME
\r
415 note ("Allows: assembly, field, method, module, param, property, type");
\r
423 | attribute_list COMMA attribute
\r
428 | opt_attribute_arguments
\r
432 : type_name { /* reserved attribute name or identifier: 17.4 */ }
\r
435 opt_attribute_arguments
\r
437 | OPEN_PARENS attribute_arguments CLOSE_PARENS
\r
440 attribute_arguments
\r
442 | attribute_arguments COMMA expression
\r
446 opt_dimension_separators
\r
448 | dimension_separators
\r
451 dimension_separators
\r
453 | dimension_separators COMMA
\r
457 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
\r
460 opt_class_member_declarations
\r
462 | class_member_declarations
\r
465 class_member_declarations
\r
466 : class_member_declaration
\r
467 | class_member_declarations
\r
468 class_member_declaration
\r
471 class_member_declaration
\r
472 : constant_declaration // done
\r
473 | field_declaration // done
\r
474 | method_declaration // done
\r
475 | property_declaration // done
\r
476 | event_declaration // finishme
\r
477 | indexer_declaration // finishme
\r
478 | operator_declaration // finishme
\r
479 | constructor_declaration // done
\r
480 | destructor_declaration // done
\r
490 string full_struct_name = MakeName ((string) $4);
\r
492 new_struct = new Struct (current_container, full_struct_name, (int) $2);
\r
493 current_container = new_struct;
\r
494 current_container.Namespace = current_namespace;
\r
495 tree.RecordStruct (full_struct_name, new_struct);
\r
497 opt_struct_interfaces
\r
501 Struct new_struct = (Struct) current_container;
\r
503 current_container = current_container.Parent;
\r
504 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
\r
509 opt_struct_interfaces
\r
511 | struct_interfaces
\r
516 | struct_interfaces struct_interface
\r
524 : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
\r
527 opt_struct_member_declarations
\r
529 | struct_member_declarations
\r
532 struct_member_declarations
\r
533 : struct_member_declaration
\r
534 | struct_member_declarations struct_member_declaration
\r
537 struct_member_declaration
\r
538 : constant_declaration
\r
539 | field_declaration
\r
540 | method_declaration
\r
541 | property_declaration
\r
542 | event_declaration
\r
543 | indexer_declaration
\r
544 | operator_declaration
\r
545 | constructor_declaration
\r
549 constant_declaration
\r
554 constant_declarators
\r
557 Modifiers.Check (Constant.AllowedModifiers, (int) $2, Modifiers.PRIVATE);
\r
559 foreach (DictionaryEntry constant in (ArrayList) $5){
\r
560 Constant c = new Constant (
\r
561 (TypeRef) $4, (string) constant.Key,
\r
562 (Expression) constant.Value);
\r
564 CheckDef (current_container.AddConstant (c), c.Name);
\r
569 constant_declarators
\r
570 : constant_declarator
\r
572 ArrayList constants = new ArrayList ();
\r
573 constants.Add ($1);
\r
576 | constant_declarators COMMA constant_declarator
\r
578 ArrayList constants = (ArrayList) $1;
\r
580 constants.Add ($3);
\r
584 constant_declarator
\r
585 : IDENTIFIER ASSIGN constant_expression {
\r
586 $$ = new DictionaryEntry ($1, $3);
\r
594 variable_declarators
\r
597 TypeRef typeref = (TypeRef) $3;
\r
598 int mod = (int) $2;
\r
600 foreach (VariableDeclaration var in (ArrayList) $4){
\r
601 Field field = new Field (typeref, mod, var.identifier,
\r
602 var.expression_or_array_initializer);
\r
604 CheckDef (current_container.AddField (field), field.Name);
\r
610 variable_declarators
\r
611 : variable_declarator
\r
613 ArrayList decl = new ArrayList ();
\r
617 | variable_declarators COMMA variable_declarator
\r
619 ArrayList decls = (ArrayList) $1;
\r
625 variable_declarator
\r
626 : IDENTIFIER ASSIGN variable_initializer
\r
628 $$ = new VariableDeclaration ((string) $1, $3);
\r
632 $$ = new VariableDeclaration ((string) $1, null);
\r
636 variable_initializer
\r
638 | array_initializer
\r
645 Method method = (Method) $1;
\r
647 method.Block = (Block) $2;
\r
648 CheckDef (current_container.AddMethod (method), method.Name);
\r
650 current_local_parameters = null;
\r
659 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
\r
661 Method method = new Method ((TypeRef) $3, (int) $2, (string) $4, (Parameters) $6);
\r
663 current_local_parameters = (Parameters) $6;
\r
671 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
\r
673 Method method = new Method (type ("void"), (int) $2, (string) $4, (Parameters) $6);
\r
675 current_local_parameters = (Parameters) $6;
\r
682 | SEMICOLON { $$ = null; }
\r
685 opt_formal_parameter_list
\r
686 : /* empty */ { $$ = new Parameters (null, null); }
\r
687 | formal_parameter_list
\r
690 formal_parameter_list
\r
691 : fixed_parameters
\r
693 $$ = new Parameters ((ParameterCollection) $1, null);
\r
695 | fixed_parameters COMMA parameter_array
\r
697 $$ = new Parameters ((ParameterCollection) $1, (Parameter) $3);
\r
701 $$ = new Parameters (null, (Parameter) $1);
\r
708 ParameterCollection pars = new ParameterCollection ();
\r
709 pars.Add ((Parameter) $1);
\r
712 | fixed_parameters COMMA fixed_parameter
\r
714 ParameterCollection pars = (ParameterCollection) $1;
\r
715 pars.Add ((Parameter) $3);
\r
722 opt_parameter_modifier
\r
726 $$ = new Parameter ((TypeRef) $3, (string) $4, (Parameter.Modifier) $2);
\r
730 opt_parameter_modifier
\r
731 : /* empty */ { $$ = Parameter.Modifier.NONE; }
\r
732 | parameter_modifier
\r
736 : REF { $$ = Parameter.Modifier.REF; }
\r
737 | OUT { $$ = Parameter.Modifier.OUT; }
\r
741 : opt_attributes PARAMS type IDENTIFIER
\r
743 $$ = new Parameter ((TypeRef) $3, (string) $4, Parameter.Modifier.PARAMS);
\r
744 note ("type must be a single-dimension array type");
\r
749 : IDENTIFIER { $$ = $1.ToString (); }
\r
750 | interface_type DOT IDENTIFIER { $$ = $1.ToString () + "." + $3.ToString (); }
\r
753 property_declaration
\r
759 Parameter implicit_value_parameter;
\r
760 implicit_value_parameter = new Parameter ((TypeRef) $3, "value", Parameter.Modifier.NONE);
\r
762 lexer.properties = true;
\r
764 implicit_value_parameters = new ParameterCollection ();
\r
765 implicit_value_parameters.Add (implicit_value_parameter);
\r
767 accessor_declarations
\r
769 lexer.properties = false;
\r
774 DictionaryEntry pair = (DictionaryEntry) $7;
\r
775 Block get_block = null;
\r
776 Block set_block = null;
\r
778 if (pair.Key != null)
\r
779 get_block = (Block) pair.Key;
\r
780 if (pair.Value != null)
\r
781 set_block = (Block) pair.Value;
\r
783 prop = new Property ((TypeRef) $3, (string) $4, (int) $2, get_block, set_block);
\r
785 CheckDef (current_container.AddProperty (prop), prop.Name);
\r
786 implicit_value_parameters = null;
\r
790 accessor_declarations
\r
791 : get_accessor_declaration opt_set_accessor_declaration
\r
793 $$ = new DictionaryEntry ($1, $2);
\r
795 | set_accessor_declaration opt_get_accessor_declaration
\r
797 $$ = new DictionaryEntry ($2, $1);
\r
801 opt_get_accessor_declaration
\r
802 : /* empty */ { $$ = null; }
\r
803 | get_accessor_declaration
\r
806 opt_set_accessor_declaration
\r
807 : /* empty */ { $$ = null; }
\r
808 | set_accessor_declaration
\r
811 get_accessor_declaration
\r
812 : opt_attributes GET accessor_body
\r
818 set_accessor_declaration
\r
819 : opt_attributes SET
\r
821 current_local_parameters = new Parameters (implicit_value_parameters, null);
\r
826 current_local_parameters = null;
\r
832 | SEMICOLON { $$ = new Block (null); }
\r
835 interface_declaration
\r
838 INTERFACE IDENTIFIER
\r
840 Interface new_interface;
\r
841 string full_interface_name = MakeName ((string) $4);
\r
843 new_interface = new Interface (current_container, full_interface_name, (int) $2);
\r
844 if (current_interface != null)
\r
845 error (-2, "Internal compiler error: interface inside interface");
\r
846 current_interface = new_interface;
\r
847 tree.RecordInterface (full_interface_name, new_interface);
\r
852 Interface new_interface = (Interface) current_interface;
\r
855 new_interface.Bases = (ArrayList) $6;
\r
857 current_interface = null;
\r
858 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
\r
863 : /* empty */ { $$ = null; }
\r
868 : COLON interface_type_list { $$ = $2; }
\r
871 interface_type_list
\r
874 ArrayList interfaces = new ArrayList ();
\r
876 interfaces.Add ($1);
\r
879 | interface_type_list COMMA interface_type
\r
881 ArrayList interfaces = (ArrayList) $1;
\r
882 interfaces.Add ($3);
\r
889 opt_interface_member_declarations
\r
893 opt_interface_member_declarations
\r
895 | interface_member_declarations
\r
898 interface_member_declarations
\r
899 : interface_member_declaration
\r
900 | interface_member_declarations interface_member_declaration
\r
903 interface_member_declaration
\r
904 : interface_method_declaration
\r
906 InterfaceMethod m = (InterfaceMethod) $1;
\r
908 CheckDef (current_interface.AddMethod (m), m.Name);
\r
910 | interface_property_declaration
\r
912 InterfaceProperty p = (InterfaceProperty) $1;
\r
914 CheckDef (current_interface.AddProperty (p), p.Name);
\r
916 | interface_event_declaration
\r
918 InterfaceEvent e = (InterfaceEvent) $1;
\r
920 CheckDef (current_interface.AddEvent (e), e.Name);
\r
922 | interface_indexer_declaration
\r
924 InterfaceIndexer i = (InterfaceIndexer) $1;
\r
926 CheckDef (current_interface.AddIndexer (i), "indexer");
\r
931 : /* empty */ { $$ = false; }
\r
932 | NEW { $$ = true; }
\r
935 interface_method_declaration
\r
936 : opt_attributes opt_new type IDENTIFIER
\r
937 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
\r
940 $$ = new InterfaceMethod ((TypeRef) $3, (string) $4, (bool) $2, (Parameters) $6);
\r
942 | opt_attributes opt_new VOID IDENTIFIER
\r
943 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
\r
946 $$ = new InterfaceMethod (type ("void"), (string) $4, (bool) $2, (Parameters) $6);
\r
950 interface_property_declaration
\r
955 { lexer.properties = true; }
\r
956 interface_accesors
\r
957 { lexer.properties = false; }
\r
962 $$ = new InterfaceProperty ((TypeRef) $3, (string) $4, (bool) $2,
\r
963 (gs & 1) == 1, (gs & 2) == 2);
\r
968 : opt_attributes GET SEMICOLON { $$ = 1; }
\r
969 | opt_attributes SET SEMICOLON { $$ = 2; }
\r
970 | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON
\r
972 | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
\r
976 interface_event_declaration
\r
977 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
\r
979 $$ = new InterfaceEvent ((TypeRef) $4, (string) $5, (bool) $2);
\r
983 interface_indexer_declaration
\r
984 : opt_attributes opt_new type THIS
\r
985 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
\r
987 { lexer.properties = true; }
\r
988 interface_accesors
\r
989 { lexer.properties = false; }
\r
992 int a_flags = (int) $10;
\r
994 bool do_get = (a_flags & 1) == 1;
\r
995 bool do_set = (a_flags & 2) == 2;
\r
997 $$ = new InterfaceIndexer ((TypeRef) $3, (Parameters) $6, do_get, do_set, (bool) $2);
\r
1001 operator_declaration
\r
1002 : opt_attributes opt_modifiers operator_declarator block
\r
1004 // FIXME: validate that opt_modifiers is exactly: PUBLIC and STATIC
\r
1008 operator_declarator
\r
1009 : type OPERATOR overloadable_operator
\r
1010 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
\r
1012 // FIXME: since reduce/reduce on this
\r
1013 // rule, validate overloadable_operator is unary
\r
1015 | type OPERATOR overloadable_operator
\r
1017 type IDENTIFIER COMMA
\r
1021 // FIXME: because of the reduce/reduce on PLUS and MINUS
\r
1022 // validate overloadable_operator is binary
\r
1024 | conversion_operator_declarator
\r
1027 overloadable_operator
\r
1028 // Unary operators:
\r
1035 // Unary and binary:
\r
1055 conversion_operator_declarator
\r
1056 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
\r
1057 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
\r
1060 constructor_declaration
\r
1063 constructor_declarator
\r
1066 Constructor c = (Constructor) $3;
\r
1067 c.Block = (Block) $4;
\r
1068 c.ModFlags = (int) $2;
\r
1070 if ((c.ModFlags & Modifiers.STATIC) != 0){
\r
1071 if ((c.ModFlags & Modifiers.Accessibility) != 0){
\r
1072 error (515, "Access modifiers are not allowed on static constructors");
\r
1075 CheckDef (current_container.AddConstructor (c), c.Name);
\r
1077 current_local_parameters = null;
\r
1081 constructor_declarator
\r
1083 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
\r
1084 opt_constructor_initializer
\r
1086 ConstructorInitializer i = null;
\r
1089 i = (ConstructorInitializer) $5;
\r
1091 $$ = new Constructor ((string) $1, (Parameters) $3, i);
\r
1093 current_local_parameters = (Parameters) $3;
\r
1097 opt_constructor_initializer
\r
1098 : /* empty */ { $$ = null; }
\r
1099 | constructor_initializer
\r
1102 constructor_initializer
\r
1103 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
\r
1105 $$ = new ConstructorBaseInitializer ((ArrayList) $4);
\r
1107 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
\r
1109 $$ = new ConstructorThisInitializer ((ArrayList) $4);
\r
1113 destructor_declaration
\r
1114 : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
\r
1116 Method d = new Method (type ("void"), 0, "Finalize", new Parameters (null, null));
\r
1118 d.Block = (Block) $6;
\r
1119 CheckDef (current_container.AddMethod (d), d.Name);
\r
1126 EVENT type variable_declarators SEMICOLON
\r
1127 { note ("validate that the flags only contain new public protected internal private static virtual sealed override abstract"); }
\r
1130 EVENT type member_name
\r
1131 OPEN_BRACE event_accesor_declarations CLOSE_BRACE SEMICOLON
\r
1132 { note ("validate that the flags only contain new public protected internal private static virtual sealed override abstract"); }
\r
1135 event_accesor_declarations
\r
1136 : add_accessor_declaration remove_accessor_declaration
\r
1137 | remove_accessor_declaration add_accessor_declaration
\r
1140 add_accessor_declaration
\r
1141 : opt_attributes ADD block
\r
1144 remove_accessor_declaration
\r
1145 : opt_attributes REMOVE block
\r
1148 indexer_declaration
\r
1149 : opt_attributes opt_modifiers indexer_declarator
\r
1151 { lexer.properties = true; }
\r
1152 accessor_declarations
\r
1153 { lexer.properties = false; }
\r
1156 // The signature is computed from the signature of the indexer. Look
\r
1157 // at section 3.6 on the spec
\r
1158 note ("verify modifiers are NEW PUBLIC PROTECTED INTERNAL PRIVATE VIRTUAL SEALED OVERRIDE ABSTRACT");
\r
1162 indexer_declarator
\r
1163 : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
\r
1164 | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
\r
1175 string name = (string) $4;
\r
1176 Enum e = new Enum ((TypeRef) $5, (int) $2, name);
\r
1178 foreach (VariableDeclaration ev in (ArrayList) $6){
\r
1182 (Expression) ev.expression_or_array_initializer),
\r
1186 CheckDef (current_container.AddEnum (e), name);
\r
1191 : /* empty */ { $$ = type ("System.Int32"); }
\r
1192 | COLON integral_type { $$ = $2; }
\r
1196 : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
\r
1202 opt_enum_member_declarations
\r
1203 : /* empty */ { $$ = new ArrayList (); }
\r
1204 | enum_member_declarations opt_comma { $$ = $1; }
\r
1207 enum_member_declarations
\r
1208 : enum_member_declaration
\r
1210 ArrayList l = new ArrayList ();
\r
1215 | enum_member_declarations COMMA enum_member_declaration
\r
1217 ArrayList l = (ArrayList) $1;
\r
1225 enum_member_declaration
\r
1226 : opt_attributes IDENTIFIER
\r
1228 $$ = new VariableDeclaration ((string) $2, null);
\r
1230 | opt_attributes IDENTIFIER ASSIGN expression
\r
1232 $$ = new VariableDeclaration ((string) $2, $4);
\r
1236 delegate_declaration
\r
1240 IDENTIFIER OPEN_PARENS
\r
1241 formal_parameter_list
\r
1244 { note ("validate that modifiers only contains NEW PUBLIC PROTECTED INTERNAL PRIVATE"); }
\r
1249 IDENTIFIER OPEN_PARENS
\r
1250 formal_parameter_list
\r
1253 { note ("validate that modifiers only contains NEW PUBLIC PROTECTED INTERNAL PRIVATE"); }
\r
1257 : namespace_or_type_name
\r
1260 namespace_or_type_name
\r
1261 : qualified_identifier
\r
1265 * Before you think of adding a return_type, notice that we have been
\r
1266 * using two rules in the places where it matters (one rule using type
\r
1267 * and another identical one that uses VOID as the return type). This
\r
1268 * gets rid of a shift/reduce couple
\r
1271 : type_name { /* class_type */
\r
1273 This does interfaces, delegates, struct_types, class_types,
\r
1274 parent classes, and more! 4.2
\r
1276 $$ = type ((string) $1);
\r
1285 ArrayList types = new ArrayList ();
\r
1290 | type_list COMMA type
\r
1292 ArrayList types = new ArrayList ();
\r
1299 * replaces all the productions for isolating the various
\r
1300 * simple types, but we need this to reuse it easily in local_variable_type
\r
1303 : OBJECT { $$ = type ("System.Object"); }
\r
1304 | STRING { $$ = type ("System.String"); }
\r
1305 | BOOL { $$ = type ("System.Boolean"); }
\r
1306 | DECIMAL { $$ = type ("System.Decimal"); }
\r
1307 | FLOAT { $$ = type ("System.Single"); }
\r
1308 | DOUBLE { $$ = type ("System.Double"); }
\r
1313 : SBYTE { $$ = type ("System.SByte"); }
\r
1314 | BYTE { $$ = type ("System.Byte"); }
\r
1315 | SHORT { $$ = type ("System.Int16"); }
\r
1316 | USHORT { $$ = type ("System.UInt16"); }
\r
1317 | INT { $$ = type ("System.Int32"); }
\r
1318 | UINT { $$ = type ("System.UInt32"); }
\r
1319 | LONG { $$ = type ("System.Int64"); }
\r
1320 | ULONG { $$ = type ("System.UInt64"); }
\r
1321 | CHAR { $$ = type ("System.Char"); }
\r
1329 : type rank_specifiers
\r
1332 // FIXME: We need to create a type for the nested thing.
\r
1337 // Expressions, section 7.5
\r
1339 primary_expression
\r
1342 // 7.5.1: Literals
\r
1346 | qualified_identifier
\r
1348 string name = (string) $1;
\r
1351 if (name.IndexOf ('.') == -1){
\r
1353 // we need to check against current_block not being null
\r
1354 // as `expression' is allowed in argument_lists, which
\r
1355 // do not exist inside a block.
\r
1357 if (current_block != null){
\r
1358 if (current_block.IsVariableDefined (name))
\r
1359 $$ = new LocalVariableReference (current_block, name);
\r
1361 if (($$ == null) && (current_local_parameters != null)){
\r
1362 Parameter par = current_local_parameters.GetParameterByName (name);
\r
1364 $$ = new ParameterReference (current_local_parameters, name);
\r
1368 $$ = new SimpleName (name);
\r
1370 | parenthesized_expression
\r
1372 | invocation_expression
\r
1376 | post_increment_expression
\r
1377 | post_decrement_expression
\r
1379 | typeof_expression
\r
1380 | sizeof_expression
\r
1381 | checked_expression
\r
1382 | unchecked_expression
\r
1389 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
\r
1390 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
\r
1391 | NULL { $$ = new NullLiteral (); }
\r
1395 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
\r
1396 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
\r
1397 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
\r
1401 : LITERAL_INTEGER { $$ = new IntLiteral ((Int32) lexer.Value); }
\r
1405 : TRUE { $$ = new BoolLiteral (true); }
\r
1406 | FALSE { $$ = new BoolLiteral (false); }
\r
1409 parenthesized_expression
\r
1410 : OPEN_PARENS expression CLOSE_PARENS
\r
1415 : primary_expression DOT IDENTIFIER
\r
1417 $$ = new MemberAccess ((Expression) $1, (string) $3);
\r
1419 | predefined_type DOT IDENTIFIER
\r
1421 $$ = new BuiltinTypeAccess ((TypeRef) $1, (string) $3);
\r
1429 invocation_expression
\r
1430 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
\r
1433 // if $1 is MethodGroup
\r
1434 // $$ = new Call ($1, $3);
\r
1436 // $$ = new DelegateCall ($1, $3);
\r
1438 error (1, "THIS IS CRAZY");
\r
1440 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
\r
1445 : /* empty */ { $$ = new ArrayList (); }
\r
1452 ArrayList list = new ArrayList ();
\r
1456 | argument_list COMMA argument
\r
1458 ArrayList list = (ArrayList) $1;
\r
1467 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
\r
1469 | REF variable_reference
\r
1471 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
\r
1473 | OUT variable_reference
\r
1475 $$ = new Argument ((Expression) $2, Argument.AType.Out);
\r
1479 variable_reference
\r
1480 : expression { note ("section 5.4"); $$ = $1; }
\r
1484 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
\r
1490 | expression_list COMMA expression
\r
1501 : BASE DOT IDENTIFIER
\r
1503 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
\r
1507 post_increment_expression
\r
1508 : primary_expression OP_INC
\r
1510 $$ = new Unary (Unary.Operator.PostIncrement, (Expression) $1);
\r
1514 post_decrement_expression
\r
1515 : primary_expression OP_DEC
\r
1517 $$ = new Unary (Unary.Operator.PostDecrement, (Expression) $1);
\r
1522 : object_or_delegate_creation_expression
\r
1523 | array_creation_expression
\r
1526 object_or_delegate_creation_expression
\r
1527 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
\r
1529 $$ = new New ((TypeRef) $2, (ArrayList) $4);
\r
1533 array_creation_expression
\r
1534 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
\r
1535 opt_rank_specifier
\r
1536 opt_array_initializer
\r
1540 opt_rank_specifier
\r
1547 | rank_specifier rank_specifiers
\r
1552 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
\r
1556 opt_dim_separators
\r
1564 | dim_separators COMMA
\r
1568 opt_array_initializer
\r
1570 | array_initializer
\r
1574 : OPEN_BRACE CLOSE_BRACE
\r
1575 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
\r
1579 variable_initializer_list
\r
1580 : variable_initializer
\r
1581 | variable_initializer_list COMMA variable_initializer
\r
1586 : TYPEOF OPEN_PARENS type CLOSE_PARENS
\r
1588 $$ = new TypeOf ((TypeRef) $3);
\r
1593 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
\r
1594 $$ = new SizeOf ((TypeRef) $3);
\r
1596 note ("Verify type is unmanaged");
\r
1597 note ("if (5.8) builtin, yield constant expression");
\r
1601 checked_expression
\r
1602 : CHECKED OPEN_PARENS expression CLOSE_PARENS
\r
1605 unchecked_expression
\r
1606 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
\r
1610 : primary_expression
\r
1611 | PLUS unary_expression {
\r
1612 $$ = new Unary (Unary.Operator.Plus, (Expression) $2);
\r
1614 | MINUS unary_expression
\r
1616 $$ = new Unary (Unary.Operator.Minus, (Expression) $2);
\r
1618 | BANG unary_expression
\r
1620 $$ = new Unary (Unary.Operator.Negate, (Expression) $2);
\r
1622 | TILDE unary_expression
\r
1624 $$ = new Unary (Unary.Operator.BitComplement, (Expression) $2);
\r
1626 | STAR unary_expression
\r
1628 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2);
\r
1630 | BITWISE_AND unary_expression
\r
1632 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2);
\r
1634 | OP_INC unary_expression
\r
1636 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2);
\r
1638 | OP_DEC unary_expression
\r
1640 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2);
\r
1642 | cast_expression
\r
1644 we can not do cast expressions at this level,
\r
1645 as there is an ambiguity. Check "Cast Expressions" 7.6.8
\r
1646 for the recipe to handle this.
\r
1650 pre_increment_expression
\r
1651 : OP_INC unary_expression
\r
1653 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2);
\r
1657 pre_decrement_expression
\r
1658 : OP_DEC unary_expression
\r
1660 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2);
\r
1666 * FIXME: This is actually wrong, it should be `type' but that
\r
1667 * introduces a lot of {shift,reduce}/reduces
\r
1669 * This is really really wrong. We need to track down
\r
1670 * the source of problems with QIs because expressions like:
\r
1671 * foreach (string s in (string []) object) wont be parsed.
\r
1673 : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
\r
1675 $$ = new Cast (type ((string) $2), (Expression) $4);
\r
1677 | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
\r
1679 $$ = new Cast ((TypeRef) $2, (Expression) $4);
\r
1683 multiplicative_expression
\r
1684 : unary_expression
\r
1685 | multiplicative_expression STAR unary_expression
\r
1687 $$ = new Binary (Binary.Operator.Multiply,
\r
1688 (Expression) $1, (Expression) $3);
\r
1690 | multiplicative_expression DIV unary_expression
\r
1692 $$ = new Binary (Binary.Operator.Divide,
\r
1693 (Expression) $1, (Expression) $3);
\r
1695 | multiplicative_expression PERCENT unary_expression
\r
1697 $$ = new Binary (Binary.Operator.Modulo,
\r
1698 (Expression) $1, (Expression) $3);
\r
1702 additive_expression
\r
1703 : multiplicative_expression
\r
1704 | additive_expression PLUS multiplicative_expression
\r
1706 $$ = new Binary (Binary.Operator.Add,
\r
1707 (Expression) $1, (Expression) $3);
\r
1709 | additive_expression MINUS multiplicative_expression
\r
1711 $$ = new Binary (Binary.Operator.Substract,
\r
1712 (Expression) $1, (Expression) $3);
\r
1717 : additive_expression
\r
1718 | shift_expression OP_SHIFT_LEFT additive_expression
\r
1720 $$ = new Binary (Binary.Operator.ShiftLeft,
\r
1721 (Expression) $1, (Expression) $3);
\r
1723 | shift_expression OP_SHIFT_RIGHT additive_expression
\r
1725 $$ = new Binary (Binary.Operator.ShiftRight,
\r
1726 (Expression) $1, (Expression) $3);
\r
1730 relational_expression
\r
1731 : shift_expression
\r
1732 | relational_expression OP_LT shift_expression
\r
1734 $$ = new Binary (Binary.Operator.LessThan,
\r
1735 (Expression) $1, (Expression) $3);
\r
1737 | relational_expression OP_GT shift_expression
\r
1739 $$ = new Binary (Binary.Operator.GreatherThan,
\r
1740 (Expression) $1, (Expression) $3);
\r
1742 | relational_expression OP_LE shift_expression
\r
1744 $$ = new Binary (Binary.Operator.LessOrEqual,
\r
1745 (Expression) $1, (Expression) $3);
\r
1747 | relational_expression OP_GE shift_expression
\r
1749 $$ = new Binary (Binary.Operator.GreatherOrEqual,
\r
1750 (Expression) $1, (Expression) $3);
\r
1752 | relational_expression IS type
\r
1754 $$ = new Probe (Probe.Operator.Is,
\r
1755 (Expression) $1, (TypeRef) $3);
\r
1757 | relational_expression AS type
\r
1759 $$ = new Probe (Probe.Operator.As,
\r
1760 (Expression) $1, (TypeRef) $3);
\r
1764 equality_expression
\r
1765 : relational_expression
\r
1766 | equality_expression OP_EQ relational_expression
\r
1768 $$ = new Binary (Binary.Operator.Equal,
\r
1769 (Expression) $1, (Expression) $3);
\r
1771 | equality_expression OP_NE relational_expression
\r
1773 $$ = new Binary (Binary.Operator.NotEqual,
\r
1774 (Expression) $1, (Expression) $3);
\r
1779 : equality_expression
\r
1780 | and_expression BITWISE_AND equality_expression
\r
1782 $$ = new Binary (Binary.Operator.BitwiseAnd,
\r
1783 (Expression) $1, (Expression) $3);
\r
1787 exclusive_or_expression
\r
1789 | exclusive_or_expression CARRET and_expression
\r
1791 $$ = new Binary (Binary.Operator.ExclusiveOr,
\r
1792 (Expression) $1, (Expression) $3);
\r
1796 inclusive_or_expression
\r
1797 : exclusive_or_expression
\r
1798 | inclusive_or_expression BITWISE_OR exclusive_or_expression
\r
1800 $$ = new Binary (Binary.Operator.BitwiseOr,
\r
1801 (Expression) $1, (Expression) $3);
\r
1805 conditional_and_expression
\r
1806 : inclusive_or_expression
\r
1807 | conditional_and_expression OP_AND inclusive_or_expression
\r
1809 $$ = new Binary (Binary.Operator.LogicalAnd,
\r
1810 (Expression) $1, (Expression) $3);
\r
1814 conditional_or_expression
\r
1815 : conditional_and_expression
\r
1816 | conditional_or_expression OP_OR conditional_and_expression
\r
1818 $$ = new Binary (Binary.Operator.LogicalOr,
\r
1819 (Expression) $1, (Expression) $3);
\r
1823 conditional_expression
\r
1824 : conditional_or_expression
\r
1825 | conditional_or_expression INTERR expression COLON expression
\r
1827 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
\r
1831 assignment_expression
\r
1832 : unary_expression ASSIGN expression
\r
1834 $$ = new Assign ((Expression) $1, (Expression) $3);
\r
1836 | unary_expression OP_MULT_ASSIGN expression
\r
1838 $$ = new Assign ((Expression) $1,
\r
1839 new Binary (Binary.Operator.Multiply,
\r
1841 (Expression) $3));
\r
1843 | unary_expression OP_DIV_ASSIGN expression
\r
1845 $$ = new Assign ((Expression) $1,
\r
1846 new Binary (Binary.Operator.Divide,
\r
1848 (Expression) $3));
\r
1850 | unary_expression OP_MOD_ASSIGN expression
\r
1852 $$ = new Assign ((Expression) $1,
\r
1853 new Binary (Binary.Operator.Modulo,
\r
1855 (Expression) $3));
\r
1857 | unary_expression OP_ADD_ASSIGN expression
\r
1859 $$ = new Assign ((Expression) $1,
\r
1860 new Binary (Binary.Operator.Add,
\r
1862 (Expression) $3));
\r
1864 | unary_expression OP_SUB_ASSIGN expression
\r
1866 $$ = new Assign ((Expression) $1,
\r
1867 new Binary (Binary.Operator.Substract,
\r
1869 (Expression) $3));
\r
1871 | unary_expression OP_SHIFT_LEFT_ASSIGN expression
\r
1873 $$ = new Assign ((Expression) $1,
\r
1874 new Binary (Binary.Operator.ShiftLeft,
\r
1876 (Expression) $3));
\r
1878 | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
\r
1880 $$ = new Assign ((Expression) $1,
\r
1881 new Binary (Binary.Operator.ShiftRight,
\r
1883 (Expression) $3));
\r
1885 | unary_expression OP_AND_ASSIGN expression
\r
1887 $$ = new Assign ((Expression) $1,
\r
1888 new Binary (Binary.Operator.BitwiseAnd,
\r
1890 (Expression) $3));
\r
1892 | unary_expression OP_OR_ASSIGN expression
\r
1894 $$ = new Assign ((Expression) $1,
\r
1895 new Binary (Binary.Operator.BitwiseOr,
\r
1897 (Expression) $3));
\r
1899 | unary_expression OP_XOR_ASSIGN expression
\r
1901 $$ = new Assign ((Expression) $1,
\r
1902 new Binary (Binary.Operator.ExclusiveOr,
\r
1904 (Expression) $3));
\r
1909 : conditional_expression
\r
1910 | assignment_expression
\r
1913 constant_expression
\r
1917 boolean_expression
\r
1918 : expression { CheckBoolean ((Expression) $1); $$ = $1; }
\r
1930 string full_class_name = MakeName ((string) $4);
\r
1932 new_class = new Class (current_container, full_class_name, (int) $2);
\r
1933 current_container = new_class;
\r
1934 current_container.Namespace = current_namespace;
\r
1935 tree.RecordClass (full_class_name, new_class);
\r
1941 Class new_class = (Class) current_container;
\r
1944 new_class.Bases = (ArrayList) $6;
\r
1946 current_container = current_container.Parent;
\r
1947 CheckDef (current_container.AddClass (new_class), new_class.Name);
\r
1954 : /* empty */ { $$ = (int) 0; }
\r
1960 | modifiers modifier {
\r
1961 int m1 = (int) $1;
\r
1962 int m2 = (int) $2;
\r
1964 if ((m1 & m2) != 0)
\r
1965 error (1002, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
\r
1967 $$ = (int) (m1 | m2);
\r
1972 : NEW { $$ = Modifiers.NEW; }
\r
1973 | PUBLIC { $$ = Modifiers.PUBLIC; }
\r
1974 | PROTECTED { $$ = Modifiers.PROTECTED; }
\r
1975 | INTERNAL { $$ = Modifiers.INTERNAL; }
\r
1976 | PRIVATE { $$ = Modifiers.PRIVATE; }
\r
1977 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
\r
1978 | SEALED { $$ = Modifiers.SEALED; }
\r
1979 | STATIC { $$ = Modifiers.STATIC; }
\r
1980 | READONLY { $$ = Modifiers.READONLY; }
\r
1981 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
\r
1982 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
\r
1983 | EXTERN { $$ = Modifiers.EXTERN; }
\r
1987 : /* empty */ { $$ = null; }
\r
1988 | class_base { $$ = $1; }
\r
1992 : COLON type_list { $$ = $2; }
\r
1996 // Statements (8.2)
\r
2000 // A block is "contained" on the following places:
\r
2002 // property_declaration as part of the accessor body (get/set)
\r
2003 // operator_declaration
\r
2004 // constructor_declaration
\r
2005 // destructor_declaration
\r
2006 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
\r
2011 current_block = new Block (current_block);
\r
2013 opt_statement_list CLOSE_BRACE
\r
2015 while (current_block.Implicit)
\r
2016 current_block = current_block.Parent;
\r
2017 $$ = current_block;
\r
2018 current_block = current_block.Parent;
\r
2022 opt_statement_list
\r
2029 | statement_list statement
\r
2033 : declaration_statement
\r
2035 if ((Block) $1 != current_block){
\r
2036 current_block.AddStatement ((Statement) $1);
\r
2037 current_block = (Block) $1;
\r
2040 | embedded_statement
\r
2042 current_block.AddStatement ((Statement) $1);
\r
2044 | labeled_statement
\r
2046 current_block.AddStatement ((Statement) $1);
\r
2050 embedded_statement
\r
2053 | expression_statement
\r
2054 | selection_statement
\r
2055 | iteration_statement
\r
2058 | checked_statement
\r
2059 | unchecked_statement
\r
2067 $$ = new EmptyStatement ();
\r
2072 : IDENTIFIER COLON statement
\r
2074 string lab = (String) $1;
\r
2077 block = new Block (current_block, lab);
\r
2078 block.AddStatement ((Statement) $3);
\r
2081 if (!current_block.AddLabel (lab, block)){
\r
2082 error (140, "The label '" + lab + "' is a duplicate");
\r
2088 declaration_statement
\r
2089 : local_variable_declaration SEMICOLON // done
\r
2090 | local_constant_declaration SEMICOLON // finishme
\r
2094 * The following is from Rhys' grammar:
\r
2095 * > Types in local variable declarations must be recognized as
\r
2096 * > expressions to prevent reduce/reduce errors in the grammar.
\r
2097 * > The expressions are converted into types during semantic analysis.
\r
2099 local_variable_type
\r
2100 : primary_expression type_suffixes
\r
2102 // FIXME: Do something smart here regarding the composition of the type.
\r
2105 // Ok, the above "primary_expression" is there to get rid of
\r
2106 // both reduce/reduce and shift/reduces in the grammar, it should
\r
2107 // really just be "type_name". If you use type_name, a reduce/reduce
\r
2108 // creeps up. If you use qualified_identifier (which is all we need
\r
2109 // really) two shift/reduces appear.
\r
2111 // So, instead we do a super trick: we just allow ($1) to be a
\r
2112 // SimpleName Expression.
\r
2114 if (((Expression) $1) is SimpleName)
\r
2115 $$ = type (((SimpleName) $1).Name);
\r
2117 error (-1, "Invalid Type definition");
\r
2118 $$ = type ("object");
\r
2121 | builtin_types type_suffixes
\r
2123 // FIXME: Do something smart with the type here.
\r
2126 | VOID type_suffixes
\r
2128 $$ = type ("VOID SOMETHING TYPE");
\r
2134 | type_suffix_list
\r
2139 | type_suffix_list type_suffix
\r
2143 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
\r
2144 /* FIXME: | MULTIPLY */
\r
2147 local_variable_declaration
\r
2148 : local_variable_type variable_declarators
\r
2150 $$ = declare_local_variables ((TypeRef) $1, (ArrayList) $2);
\r
2154 local_constant_declaration
\r
2155 : CONST type constant_declarator
\r
2159 expression_statement
\r
2160 : statement_expression SEMICOLON
\r
2167 // We have to do the wrapping here and not in the case above,
\r
2168 // because statement_expression is used for example in for_statement
\r
2170 statement_expression
\r
2171 : invocation_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2172 | object_creation_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2173 | assignment_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2174 | post_increment_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2175 | post_decrement_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2176 | pre_increment_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2177 | pre_decrement_expression { $$ = new StatementExpression ((Expression) $1); }
\r
2180 object_creation_expression
\r
2181 : object_or_delegate_creation_expression
\r
2182 { note ("complain if this is a delegate maybe?"); }
\r
2185 selection_statement
\r
2187 | switch_statement
\r
2191 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
\r
2192 embedded_statement
\r
2194 $$ = new If ((Expression) $3, (Statement) $5);
\r
2196 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
\r
2197 embedded_statement ELSE embedded_statement
\r
2199 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
\r
2204 : SWITCH OPEN_PARENS expression CLOSE_PARENS
\r
2207 $$ = new Switch ((Expression) $3, (ArrayList) $5);
\r
2213 opt_switch_sections
\r
2220 opt_switch_sections
\r
2221 : /* empty */ { $$ = new ArrayList (); }
\r
2228 ArrayList sections = new ArrayList ();
\r
2230 sections.Add ($1);
\r
2233 | switch_sections switch_section
\r
2235 ArrayList sections = (ArrayList) $1;
\r
2237 sections.Add ($2);
\r
2245 current_block = new Block (current_block);
\r
2249 while (current_block.Implicit)
\r
2250 current_block = current_block.Parent;
\r
2251 $$ = new SwitchSection ((ArrayList) $1, current_block);
\r
2252 current_block = current_block.Parent;
\r
2259 ArrayList labels = new ArrayList ();
\r
2264 | switch_labels switch_label
\r
2266 ArrayList labels = (ArrayList) ($1);
\r
2274 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2); }
\r
2275 | DEFAULT COLON { $$ = new SwitchLabel (null); }
\r
2278 iteration_statement
\r
2282 | foreach_statement
\r
2286 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
\r
2288 $$ = new While ((Expression) $3, (Statement) $5);
\r
2293 : DO embedded_statement
\r
2294 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
\r
2296 $$ = new Do ((Statement) $2, (Expression) $5);
\r
2301 : FOR OPEN_PARENS
\r
2302 opt_for_initializer SEMICOLON
\r
2303 opt_for_condition SEMICOLON
\r
2304 opt_for_iterator CLOSE_PARENS
\r
2305 embedded_statement
\r
2307 $$ = new For ((Statement) $3, (Expression) $5, (Statement) $7, (Statement) $9);
\r
2311 opt_for_initializer
\r
2312 : /* empty */ { $$ = new EmptyStatement (); }
\r
2313 | for_initializer
\r
2317 : local_variable_declaration
\r
2318 | statement_expression_list
\r
2322 : /* empty */ { $$ = new BoolLiteral (true); }
\r
2323 | boolean_expression
\r
2327 : /* empty */ { $$ = new EmptyStatement (); }
\r
2332 : statement_expression_list
\r
2335 statement_expression_list
\r
2336 : statement_expression
\r
2338 Block b = new Block (null, true);
\r
2340 b.AddStatement ((Statement) $1);
\r
2343 | statement_expression_list COMMA statement_expression
\r
2345 Block b = (Block) $1;
\r
2347 b.AddStatement ((Statement) $3);
\r
2353 : FOREACH OPEN_PARENS type IDENTIFIER IN expression CLOSE_PARENS
\r
2354 embedded_statement
\r
2356 string temp_id = current_block.MakeInternalID ();
\r
2357 Expression assign_e, ma;
\r
2358 Statement getcurrent;
\r
2359 Block foreach_block, child_block;
\r
2361 foreach_block = new Block (current_block, true);
\r
2363 foreach_block.AddVariable (type ("IEnumerator"), temp_id);
\r
2364 foreach_block.AddVariable ((TypeRef) $3, (string) $4);
\r
2365 assign_e = new Assign (new LocalVariableReference (foreach_block, temp_id),
\r
2367 new MemberAccess ((Expression) $6, "GetEnumerator"), null));
\r
2368 current_block.AddStatement (new StatementExpression (assign_e));
\r
2369 ma = new MemberAccess (new LocalVariableReference (foreach_block, temp_id), "MoveNext");
\r
2370 child_block = new Block (current_block);
\r
2372 getcurrent = new StatementExpression (
\r
2374 new LocalVariableReference (foreach_block, (string) $4),
\r
2377 new MemberAccess (
\r
2378 new LocalVariableReference (foreach_block, temp_id), "Current"))));
\r
2380 child_block.AddStatement (getcurrent);
\r
2381 child_block.AddStatement ((Statement) $8);
\r
2382 foreach_block.AddStatement (new While (ma, (Statement) child_block));
\r
2384 $$ = foreach_block;
\r
2390 | continue_statement
\r
2392 | return_statement
\r
2399 $$ = new Break ();
\r
2403 continue_statement
\r
2404 : CONTINUE SEMICOLON
\r
2406 $$ = new Continue ();
\r
2411 : GOTO IDENTIFIER SEMICOLON
\r
2413 $$ = new Goto ((string) $2);
\r
2415 | GOTO CASE constant_expression SEMICOLON
\r
2416 | GOTO DEFAULT SEMICOLON
\r
2420 : RETURN opt_expression SEMICOLON
\r
2422 $$ = new Return ((Expression) $2);
\r
2427 : THROW opt_expression SEMICOLON
\r
2429 $$ = new Throw ((Expression) $2);
\r
2439 : TRY block catch_clauses
\r
2441 DictionaryEntry cc = (DictionaryEntry) $3;
\r
2442 ArrayList s = null;
\r
2444 if (cc.Key != null)
\r
2445 s = (ArrayList) cc.Key;
\r
2447 $$ = new Try ((Block) $2, s, (Catch) cc.Value, null);
\r
2449 | TRY block FINALLY block
\r
2451 $$ = new Try ((Block) $2, null, null, (Block) $4);
\r
2453 | TRY block catch_clauses FINALLY block
\r
2455 DictionaryEntry cc = (DictionaryEntry) $3;
\r
2456 ArrayList s = null;
\r
2458 if (cc.Key != null)
\r
2459 s = (ArrayList) cc.Key;
\r
2461 $$ = new Try ((Block) $2, s, (Catch) cc.Value, (Block) $5);
\r
2466 : specific_catch_clauses opt_general_catch_clause
\r
2468 DictionaryEntry pair = new DictionaryEntry ();
\r
2475 | opt_specific_catch_clauses general_catch_clause
\r
2477 DictionaryEntry pair = new DictionaryEntry ();
\r
2485 opt_general_catch_clause
\r
2486 : /* empty */ { $$ = null; }
\r
2487 | general_catch_clause
\r
2490 opt_specific_catch_clauses
\r
2491 : /* emtpy */ { $$ = null; }
\r
2492 | specific_catch_clauses
\r
2495 specific_catch_clauses
\r
2496 : specific_catch_clause
\r
2498 ArrayList l = new ArrayList ();
\r
2503 | specific_catch_clauses specific_catch_clause
\r
2505 ArrayList l = (ArrayList) $1;
\r
2512 specific_catch_clause
\r
2513 : CATCH OPEN_PARENS type opt_identifier CLOSE_PARENS block
\r
2520 $$ = new Catch ((TypeRef) $3, id, (Block) $6);
\r
2525 : /* empty */ { $$ = null; }
\r
2529 general_catch_clause
\r
2532 $$ = new Catch (null, null, (Block) $2);
\r
2539 $$ = new Checked ((Block) $2);
\r
2543 unchecked_statement
\r
2546 $$ = new Unchecked ((Block) $2);
\r
2551 : LOCK OPEN_PARENS expression CLOSE_PARENS embedded_statement
\r
2553 $$ = new Lock ((Expression) $3, (Statement) $5);
\r
2558 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS embedded_statement
\r
2562 resource_acquisition
\r
2563 : local_variable_declaration
\r
2571 // A class used to pass around variable declarations and constants
\r
2573 public class VariableDeclaration {
\r
2574 public string identifier;
\r
2575 public object expression_or_array_initializer;
\r
2577 public VariableDeclaration (string id, object eoai){
\r
2578 this.identifier = id;
\r
2579 this.expression_or_array_initializer = eoai;
\r
2584 // Given the @class_name name, it creates a fully qualified name
\r
2585 // based on the containing declaration space
\r
2588 MakeName (string class_name)
\r
2590 string ns = current_namespace.Name;
\r
2591 string container_name = current_container.Name;
\r
2593 if (container_name == ""){
\r
2595 return ns + "." + class_name;
\r
2597 return class_name;
\r
2599 return container_name + "." + class_name;
\r
2603 // Used to report back to the user the result of a declaration
\r
2604 // in the current declaration space
\r
2607 CheckDef (DeclSpace.AdditionResult result, string name)
\r
2609 if (result == DeclSpace.AdditionResult.Success)
\r
2613 case DeclSpace.AdditionResult.NameExists:
\r
2614 error (102, "The namespace `" + current_container.Name +
\r
2615 "' already contains a definition for `"+
\r
2620 NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
\r
2622 case DeclSpace.AdditionResult.MethodDuplicated:
\r
2623 error (111, "Class `"+current_container.Name+
\r
2624 "' already defines a member called '" +
\r
2625 name + "' with the same parameter types");
\r
2628 case DeclSpace.AdditionResult.EnclosingClash:
\r
2629 error (542, "Member names cannot be the same as their enclosing type");
\r
2632 case DeclSpace.AdditionResult.NotAConstructor:
\r
2633 error (1520, "Class, struct, or interface method must have a return type");
\r
2639 CheckDef (bool result, string name)
\r
2643 CheckDef (DeclSpace.AdditionResult.NameExists, name);
\r
2646 Block declare_local_variables (TypeRef typeref, ArrayList variable_declarators)
\r
2648 Block implicit_block;
\r
2649 ArrayList inits = null;
\r
2652 // We use the `Used' property to check whether statements
\r
2653 // have been added to the current block. If so, we need
\r
2654 // to create another block to contain the new declaration
\r
2655 // otherwise, as an optimization, we use the same block to
\r
2656 // add the declaration.
\r
2658 // FIXME: A further optimization is to check if the statements
\r
2659 // that were added were added as part of the initialization
\r
2660 // below. In which case, no other statements have been executed
\r
2661 // and we might be able to reduce the number of blocks for
\r
2662 // situations like this:
\r
2664 // int j = 1; int k = j + 1;
\r
2666 if (current_block.Used)
\r
2667 implicit_block = new Block (current_block, true);
\r
2669 implicit_block = new Block (current_block, true);
\r
2671 foreach (VariableDeclaration decl in variable_declarators){
\r
2672 if (implicit_block.AddVariable (typeref, decl.identifier)){
\r
2673 if (decl.expression_or_array_initializer != null){
\r
2674 if (inits == null)
\r
2675 inits = new ArrayList ();
\r
2679 error (128, "A local variable `" + decl.identifier +
\r
2680 "' is already defined in this scope");
\r
2684 if (inits == null)
\r
2685 return implicit_block;
\r
2687 foreach (VariableDeclaration decl in inits){
\r
2688 if (decl.expression_or_array_initializer is Expression){
\r
2689 Expression expr = (Expression) decl.expression_or_array_initializer;
\r
2692 assign = new Assign (new LocalVariableReference (implicit_block, decl.identifier), expr);
\r
2693 implicit_block.AddStatement (new StatementExpression (assign));
\r
2698 return implicit_block;
\r
2701 void CheckConstant (Expression expr)
\r
2706 void CheckBoolean (Expression expr)
\r
2711 static public void error (int code, string desc)
\r
2713 Console.WriteLine ("Error CS"+code+": "+desc);
\r
2717 void output (string s)
\r
2719 Console.WriteLine (s);
\r
2722 void note (string s)
\r
2724 // Used to put annotations
\r
2727 TypeRef type (string type_name)
\r
2729 return type_references.GetTypeRef (current_container, type_name);
\r
2734 public CSharpParser(CIR.Tree tree, string name, System.IO.Stream input)
\r
2736 current_namespace = new Namespace (null, "");
\r
2739 this.input = input;
\r
2740 current_container = tree.Types;
\r
2741 current_container.Namespace = current_namespace;
\r
2743 lexer = new Tokenizer (input, name);
\r
2744 type_references = new TypeRefManager ();
\r
2747 public int parse ()
\r
2749 StringBuilder value = new StringBuilder ();
\r
2751 global_errors = 0;
\r
2753 if (yacc_verbose_flag)
\r
2754 yyparse (lexer, new yydebug.yyDebugSimple ());
\r
2757 } catch (Exception e){
\r
2758 Console.WriteLine ("Fatal error: "+name);
\r
2759 Console.WriteLine (e);
\r
2760 Console.WriteLine (lexer.location);
\r
2764 return global_errors;
\r
2767 bool yacc_verbose_flag = false;
\r
2769 public bool yacc_verbose {
\r
2771 yacc_verbose_flag = value;
\r
2775 return yacc_verbose_flag;
\r