2001-11-08 Ravi Pratap <ravi@ximian.com>
[mono.git] / mcs / mcs / cs-parser.jay
1 %{
2 //
3 // cs-parser.jay: The Parser for the C# compiler
4 //
5 // Author: Miguel de Icaza (miguel@gnu.org)
6 //
7 // Licensed under the terms of the GNU GPL
8 //
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
10 //
11 // TODO:
12 //   (1) Get rid of the *Collections.cs, that is an idea I took from System.CodeDOM
13 //       And come to think of it, it is not that great, it duplicates a lot of code
14 //       for something which is not really needed.  We still have piles of typecasts
15 //       anwyays (due to the nature of the stack being a collection of Objects).
16 //
17 //   (2) Figure out why error productions dont work.  `type-declaration' is a
18 //       great spot to put an `error' because you can reproduce it with this input:
19 //       "public X { }"
20 //
21 //   (3) Move Modifier checking from each object into the parser itself, that will
22 //       get rid of the global "error" symbol that we use now to report errors. 
23 //       We still need to pass a pointer to the tree.ErrorHandler, but that is a 
24 //       separate problem
25 //
26 using System.Text;
27 using CIR;
28 using System;
29
30 namespace CIR
31 {
32         using System.Collections;
33         using Mono.Languages;
34
35         /// <summary>
36         ///    The C# Parser
37         /// </summary>
38         public class CSharpParser : GenericParser {
39                 Namespace     current_namespace;
40                 TypeContainer current_container;
41         
42                 // <summary>
43                 //   Current block is used to add statements as we find
44                 //   them.  
45                 // </summary>
46
47                 Block      current_block;
48
49                 // <summary>
50                 //   Current interface is used by the various declaration
51                 //   productions in the interface declaration to "add"
52                 //   the interfaces as we find them.
53                 // </summary>
54                 Interface  current_interface;
55
56                 // <summary>
57                 //   This is used by the unary_expression code to resolve
58                 //   a name against a parameter.  
59                 // </summary>
60                 Parameters current_local_parameters;
61
62                 // <summary>
63                 //   Using during property parsing to describe the implicit
64                 //   value parameter that is passed to the "set" and "get"accesor
65                 //   methods (properties and indexers).
66                 // </summary>
67                 string     implicit_value_parameter_type;
68                 Parameters indexer_parameters;
69
70                 // <summary>
71                 //   Used to determine if we are parsing the get/set pair
72                 //   of an indexer or a property
73                 // </summmary>
74                 bool  parsing_indexer;
75
76                 // <summary>
77                 //   Used to record all types defined
78                 // </summary>
79                 Tree tree;
80
81                 RootContext rc;
82
83                 // <summary>
84                 //  Used to temporarily hold the lexer's location for storage later
85                 // </summary>
86                 Location temporary_loc;
87
88 %}
89
90 %token EOF
91 %token NONE   /* This token is never returned by our lexer */
92 %token ERROR            // This is used not by the parser, but by the tokenizer.
93                         // do not remove.
94
95 /*
96  *These are the C# keywords
97  */
98 %token ABSTRACT 
99 %token AS
100 %token ADD
101 %token BASE     
102 %token BOOL     
103 %token BREAK    
104 %token BYTE     
105 %token CASE     
106 %token CATCH    
107 %token CHAR     
108 %token CHECKED  
109 %token CLASS    
110 %token CONST    
111 %token CONTINUE 
112 %token DECIMAL  
113 %token DEFAULT  
114 %token DELEGATE 
115 %token DO       
116 %token DOUBLE   
117 %token ELSE     
118 %token ENUM     
119 %token EVENT    
120 %token EXPLICIT 
121 %token EXTERN   
122 %token FALSE    
123 %token FINALLY  
124 %token FIXED    
125 %token FLOAT    
126 %token FOR      
127 %token FOREACH  
128 %token GOTO     
129 %token IF       
130 %token IMPLICIT 
131 %token IN       
132 %token INT      
133 %token INTERFACE
134 %token INTERNAL 
135 %token IS       
136 %token LOCK     
137 %token LONG     
138 %token NAMESPACE
139 %token NEW      
140 %token NULL     
141 %token OBJECT   
142 %token OPERATOR 
143 %token OUT      
144 %token OVERRIDE 
145 %token PARAMS   
146 %token PRIVATE  
147 %token PROTECTED
148 %token PUBLIC   
149 %token READONLY 
150 %token REF      
151 %token RETURN   
152 %token REMOVE
153 %token SBYTE    
154 %token SEALED   
155 %token SHORT    
156 %token SIZEOF   
157 %token STATIC   
158 %token STRING   
159 %token STRUCT   
160 %token SWITCH   
161 %token THIS     
162 %token THROW    
163 %token TRUE     
164 %token TRY      
165 %token TYPEOF   
166 %token UINT     
167 %token ULONG    
168 %token UNCHECKED
169 %token UNSAFE   
170 %token USHORT   
171 %token USING    
172 %token VIRTUAL  
173 %token VOID     
174 %token WHILE    
175
176 /* C# keywords which are not really keywords */
177 %token GET           "get"
178 %token SET           "set"
179
180 /* C# single character operators/punctuation. */
181 %token OPEN_BRACE    "{"
182 %token CLOSE_BRACE   "}"
183 %token OPEN_BRACKET  "["
184 %token CLOSE_BRACKET "]"
185 %token OPEN_PARENS   "("
186 %token CLOSE_PARENS  ")"
187 %token DOT           "."
188 %token COMMA         ","
189 %token COLON         ":"
190 %token SEMICOLON     ";"
191 %token TILDE         "~"
192
193 %token PLUS           "+"
194 %token MINUS          "-"
195 %token BANG           "!"
196 %token ASSIGN         "="
197 %token OP_LT          "<"
198 %token OP_GT          ">"
199 %token BITWISE_AND    "&"
200 %token BITWISE_OR     "|"
201 %token STAR           "*"
202 %token PERCENT        "%"
203 %token DIV            "/"
204 %token CARRET         "^"
205 %token INTERR         "?"
206
207 /* C# multi-character operators. */
208 %token OP_INC                 "++"
209 %token OP_DEC                 "--"
210 %token OP_SHIFT_LEFT          "<<"
211 %token OP_SHIFT_RIGHT         ">>"
212 %token OP_LE                  "<="
213 %token OP_GE                  ">="
214 %token OP_EQ                  "=="
215 %token OP_NE                  "!="
216 %token OP_AND                 "&&"
217 %token OP_OR                  "||"
218 %token OP_MULT_ASSIGN         "*="
219 %token OP_DIV_ASSIGN          "/="
220 %token OP_MOD_ASSIGN          "%="
221 %token OP_ADD_ASSIGN          "+="
222 %token OP_SUB_ASSIGN          "-="
223 %token OP_SHIFT_LEFT_ASSIGN   "<<="
224 %token OP_SHIFT_RIGHT_ASSIGN  ">>="
225 %token OP_AND_ASSIGN          "&="
226 %token OP_XOR_ASSIGN          "^="
227 %token OP_OR_ASSIGN           "|="
228 %token OP_PTR                 "->"
229
230 /* Numbers */
231 %token LITERAL_INTEGER           "int literal"
232 %token LITERAL_FLOAT             "float literal"
233 %token LITERAL_DOUBLE            "double literal"
234 %token LITERAL_DECIMAL           "decimal literal"
235 %token LITERAL_CHARACTER         "character literal"
236 %token LITERAL_STRING            "string literal"
237
238 %token IDENTIFIER
239
240 /* Add precedence rules to solve dangling else s/r conflict */
241 %nonassoc LOWPREC
242 %nonassoc IF
243 %nonassoc ELSE
244 %right ASSIGN
245 %left OP_OR
246 %left OP_AND
247 %left BITWISE_OR
248 %left BITWISE_AND
249 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
250 %left PLUS MINUS
251 %left STAR DIV PERCENT
252 %right BANG CARRET UMINUS
253 %nonassoc OP_INC OP_DEC
254 %left OPEN_PARENS
255 %left OPEN_BRACKET OPEN_BRACE
256 %left DOT
257 %nonassoc HIGHPREC
258
259 %start compilation_unit
260 /*%start namespace_declaration */
261 %%
262
263 compilation_unit
264         : opt_using_directives opt_namespace_member_declarations EOF
265           {
266                 // Check that using comes only before namespace elements
267           }
268         ;
269
270 using_directives
271         : using_directive 
272         | using_directives using_directive
273         ;
274
275 using_directive
276         : using_alias_directive
277         | using_namespace_directive
278         ;
279
280 using_alias_directive
281         : USING IDENTIFIER ASSIGN 
282           namespace_or_type_name SEMICOLON
283           {
284                   // FIXME : Need to implement actual action.
285           }
286         ;
287
288 using_namespace_directive
289         : USING namespace_name SEMICOLON 
290           {
291                 current_namespace.Using ((string) $2);
292           }
293         ;
294
295 //  namespace_declarations
296 //      : namespace_declaration
297 //      | namespace_declarations namespace_declaration
298
299 namespace_declaration
300         : NAMESPACE qualified_identifier 
301           {
302                 current_namespace = tree.RecordNamespace (current_namespace, (string) $2);
303           } 
304           namespace_body opt_semicolon
305           { 
306                 current_namespace = current_namespace.Parent;
307           }
308         ;
309
310 opt_semicolon
311         : /* empty */
312         | SEMICOLON
313         ;
314
315 opt_comma
316         : /* empty */
317         | COMMA
318         ;
319
320 qualified_identifier
321         : IDENTIFIER
322         | qualified_identifier DOT IDENTIFIER { 
323             $$ = (($1).ToString ()) + "." + ($3.ToString ()); }
324         ;
325
326
327 namespace_name
328         : namespace_or_type_name
329         ;
330
331 namespace_body
332         : OPEN_BRACE
333           opt_using_directives
334           opt_namespace_member_declarations
335           CLOSE_BRACE
336           {
337           }
338         ;
339
340 opt_using_directives
341         : /* empty */
342         | using_directives
343         ;
344
345 opt_namespace_member_declarations
346         : /* empty */
347         | namespace_member_declarations
348         ;
349
350 namespace_member_declarations
351         : namespace_member_declaration
352         | namespace_member_declarations namespace_member_declaration
353         ;
354
355 namespace_member_declaration
356         : type_declaration
357           {
358                 int mod_flags = 0;
359                 string name = "";
360
361                 if ($1 is Class){
362                         Class c = (Class) $1;
363                         mod_flags = c.ModFlags;
364                         name = c.Name;
365                 } else if ($1 is Struct){
366                         Struct s = (Struct) $1;
367                         mod_flags = s.ModFlags;
368                         name = s.Name;
369                 } else
370                         break;
371
372                 //
373                 // We remove this error until we can 
374                 //if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
375                 //      error (1527, "Namespace elements cant be explicitly " +
376                 //                   "declared private or protected in `" + name + "'");
377                 //}
378           }
379         | namespace_declaration
380         ;
381
382 type_declaration
383         : class_declaration             
384         | struct_declaration            
385         | interface_declaration         
386         | enum_declaration              
387         | delegate_declaration         
388         ;
389
390 //
391 // Attributes 17.2
392 //
393 opt_attributes
394         : /* empty */ { $$ = null; }
395         | attribute_section opt_attributes
396           {
397                 Attributes attrs;
398                 
399                 if ($2 != null) {
400                         attrs = (Attributes) $2;
401                         attrs.AddAttribute ((AttributeSection) $1);
402                 } else
403                         attrs = new Attributes ((AttributeSection) $1);
404                 
405                 $$ = attrs;
406           }
407         ;
408
409 attribute_section
410         : OPEN_BRACKET attribute_target_specifier attribute_list CLOSE_BRACKET
411           {
412                 string target = null;
413                 
414                 if ($2 != null)
415                         target = (string) $2;
416                 
417                 $$ = new AttributeSection (target, (ArrayList) $3);
418           }
419         | OPEN_BRACKET attribute_list CLOSE_BRACKET
420           {
421                 $$ = new AttributeSection (null, (ArrayList) $2);
422           }
423         ;
424  
425 attribute_target_specifier
426         : attribute_target COLON
427           {
428                 $$ = $1;
429           }
430         ;
431
432 attribute_target
433         : IDENTIFIER
434           {
435                 CheckAttributeTarget ((string) $1);
436                 $$ = $1;
437           }
438         | EVENT  { $$ = "event"; }        
439         | RETURN { $$ = "return"; }
440         ;
441
442 attribute_list
443         : attribute
444           {
445                 ArrayList attrs = new ArrayList ();
446                 attrs.Add ($1);
447
448                 $$ = attrs;
449                
450           }
451         | attribute_list COMMA attribute
452           {
453                 ArrayList attrs = (ArrayList) $1;
454                 attrs.Add ($3);
455
456                 $$ = attrs;
457           }
458         ;
459
460 attribute
461         : attribute_name
462           {
463                 temporary_loc = lexer.Location;
464           }
465           opt_attribute_arguments
466           {
467                 $$ = new Attribute ((string) $1, (ArrayList) $3, temporary_loc);
468           }
469         ;
470
471 attribute_name
472         : type_name  { /* reserved attribute name or identifier: 17.4 */ }
473         ;
474
475 opt_attribute_arguments
476         : /* empty */   { $$ = null; }
477         | OPEN_PARENS attribute_arguments CLOSE_PARENS
478           {
479                 $$ = $2;
480           }
481         ;
482
483
484 attribute_arguments
485         : positional_argument_list
486           {
487                 ArrayList args = new ArrayList ();
488                 args.Add ($1);
489                 
490                 $$ = args;
491           }
492         | positional_argument_list COMMA named_argument_list
493           {
494                 ArrayList args = new ArrayList ();
495                 args.Add ($1);
496                 args.Add ($3);
497
498                 $$ = args;
499           }
500         | named_argument_list
501           {
502                 ArrayList args = new ArrayList ();
503                 args.Add (null);
504                 args.Add ($1);
505                 
506                 $$ = args;
507           }
508         ;
509
510
511 positional_argument_list
512         : expression
513           {
514                 ArrayList args = new ArrayList ();
515                 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
516
517                 $$ = args;
518           }
519         | positional_argument_list COMMA expression
520          {
521                 ArrayList args = (ArrayList) $1;
522                 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
523
524                 $$ = args;
525          }
526         ;
527
528 named_argument_list
529         : named_argument
530           {
531                 ArrayList args = new ArrayList ();
532                 args.Add ($1);
533
534                 $$ = args;
535           }
536         | named_argument_list COMMA named_argument
537           {       
538                 ArrayList args = (ArrayList) $1;
539                 args.Add ($3);
540
541                 $$ = args;
542           }
543         ;
544
545 named_argument
546         : IDENTIFIER ASSIGN expression
547           {
548                 $$ = new DictionaryEntry ((string) $1, new Argument ((Expression) $3, Argument.AType.Expression));
549           }
550         ;
551
552                   
553 class_body
554         :  OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
555         ;
556
557 opt_class_member_declarations
558         : /* empty */
559         | class_member_declarations
560         ;
561
562 class_member_declarations
563         : class_member_declaration
564         | class_member_declarations 
565           class_member_declaration
566         ;
567
568 class_member_declaration
569         : constant_declaration                  // done
570         | field_declaration                     // done
571         | method_declaration                    // done
572         | property_declaration                  // done
573         | event_declaration                     // done
574         | indexer_declaration                   // done
575         | operator_declaration                  // done
576         | constructor_declaration               // done
577         | destructor_declaration                // done
578         | type_declaration
579         ;
580
581 struct_declaration
582         : opt_attributes
583           opt_modifiers
584           STRUCT IDENTIFIER
585           { 
586                 Struct new_struct;
587                 string full_struct_name = MakeName ((string) $4);
588
589                 new_struct = new Struct (rc, current_container, full_struct_name, (int) $2, 
590                                          (Attributes) $1, lexer.Location);
591                 current_container = new_struct;
592                 current_container.Namespace = current_namespace;
593                 tree.RecordStruct (full_struct_name, new_struct);
594           }
595           opt_struct_interfaces
596           struct_body
597           opt_semicolon
598           {
599                 Struct new_struct = (Struct) current_container;
600
601                 current_container = current_container.Parent;
602                 CheckDef (current_container.AddStruct (new_struct), new_struct.Name);
603                 $$ = new_struct;
604           }
605         ;
606
607 opt_struct_interfaces
608         : /* empty */
609         | struct_interfaces
610         ;
611
612 struct_interfaces
613         : struct_interface
614         | struct_interfaces struct_interface
615         ; 
616
617 struct_interface
618         : COLON type_list
619         ;
620
621 struct_body
622         : OPEN_BRACE opt_struct_member_declarations CLOSE_BRACE
623         ;
624
625 opt_struct_member_declarations
626         : /* empty */
627         | struct_member_declarations
628         ;
629
630 struct_member_declarations
631         : struct_member_declaration
632         | struct_member_declarations struct_member_declaration
633         ;
634
635 struct_member_declaration
636         : constant_declaration
637         | field_declaration
638         | method_declaration
639         | property_declaration
640         | event_declaration
641         | indexer_declaration
642         | operator_declaration
643         | constructor_declaration
644         | type_declaration
645         ;
646
647 constant_declaration
648         : opt_attributes 
649           opt_modifiers
650           CONST
651           type
652           constant_declarators
653           SEMICOLON
654           {
655                 foreach (DictionaryEntry constant in (ArrayList) $5){
656                         Constant c = new Constant (
657                                 (string) $4, (string) constant.Key, 
658                                 (Expression) constant.Value, (int) $2, (Attributes) $1);
659
660                         CheckDef (current_container.AddConstant (c), c.Name);
661                 }
662           }
663         ;
664
665 constant_declarators
666         : constant_declarator 
667           {
668                 ArrayList constants = new ArrayList ();
669                 constants.Add ($1);
670                 $$ = constants;
671           }
672         | constant_declarators COMMA constant_declarator
673           {
674                 ArrayList constants = (ArrayList) $1;
675
676                 constants.Add ($3);
677           }
678         ;
679
680 constant_declarator
681         : IDENTIFIER ASSIGN constant_expression
682           {
683                 $$ = new DictionaryEntry ($1, $3);
684           }
685         ;
686
687 field_declaration
688         : opt_attributes
689           opt_modifiers
690           type 
691           variable_declarators
692           SEMICOLON
693         { 
694                 string type = (string) $3;
695                 int mod = (int) $2;
696
697                 foreach (VariableDeclaration var in (ArrayList) $4){
698                         Field field = new Field (type, mod, var.identifier, 
699                                                  var.expression_or_array_initializer, (Attributes) $1);
700
701                         CheckDef (current_container.AddField (field), field.Name);
702                 }
703         }
704
705         ;
706
707 variable_declarators
708         : variable_declarator 
709           {
710                 ArrayList decl = new ArrayList ();
711                 decl.Add ($1);
712                 $$ = decl;
713           }
714         | variable_declarators COMMA variable_declarator
715           {
716                 ArrayList decls = (ArrayList) $1;
717                 decls.Add ($3);
718                 $$ = $1;
719           }
720         ;
721
722 variable_declarator
723         : IDENTIFIER ASSIGN variable_initializer
724           {
725                 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
726           }
727         | IDENTIFIER
728           {
729                 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
730           }
731         ;
732
733 variable_initializer
734         : expression
735           {
736                 $$ = $1;
737           }
738         | array_initializer
739         {
740                 $$ = $1;
741         }
742         ;
743
744 method_declaration
745         : method_header
746           method_body
747           {
748                 Method method = (Method) $1;
749                 Block b = (Block) $2;
750                 
751                 if (b == null){
752                         if ((method.ModFlags & (Modifiers.EXTERN | Modifiers.ABSTRACT)) == 0){
753                                 Report.Error (
754                                         501, lexer.Location, "`" + 
755                                         current_container.Name + "." + method.Name + "'" +
756                                         "must declare a body because it is not marked abstract or extern");
757                         }
758                 }
759
760                 method.Block = (Block) $2;
761                 CheckDef (current_container.AddMethod (method), method.Name);
762
763                 current_local_parameters = null;
764           }
765         ;
766
767 method_header
768         : opt_attributes
769           opt_modifiers
770           type
771           member_name
772           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
773           {
774                 Method method = new Method ((string) $3, (int) $2, (string) $4, 
775                                             (Parameters) $6, (Attributes) $1, lexer.Location);
776
777                 current_local_parameters = (Parameters) $6;
778
779                 $$ = method;
780           }
781         | opt_attributes
782           opt_modifiers
783           VOID
784           member_name
785           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
786           {
787                 Method method = new Method ("System.Void", (int) $2, (string) $4, 
788                                             (Parameters) $6, (Attributes) $1, lexer.Location);
789
790                 current_local_parameters = (Parameters) $6;
791                 $$ = method;
792           }
793         ;
794
795 method_body
796         : block
797         | SEMICOLON             { $$ = null; }
798         ;
799
800 opt_formal_parameter_list
801         : /* empty */                   { $$ = new Parameters (null, null); }
802         | formal_parameter_list
803         ;
804
805 formal_parameter_list
806         : fixed_parameters              
807           { 
808                 ArrayList pars_list = (ArrayList) $1;
809
810                 Parameter [] pars = new Parameter [pars_list.Count];
811                 pars_list.CopyTo (pars);
812
813                 $$ = new Parameters (pars, null); 
814           } 
815         | fixed_parameters COMMA parameter_array
816           {
817                 ArrayList pars_list = (ArrayList) $1;
818
819                 Parameter [] pars = new Parameter [pars_list.Count];
820                 pars_list.CopyTo (pars);
821
822                 $$ = new Parameters (pars, (Parameter) $3); 
823           }
824         | parameter_array 
825           {
826                 $$ = new Parameters (null, (Parameter) $1);
827           }
828         ;
829
830 fixed_parameters
831         : fixed_parameter       
832           {
833                 ArrayList pars = new ArrayList ();
834
835                 pars.Add ($1);
836                 $$ = pars;
837           }
838         | fixed_parameters COMMA fixed_parameter
839           {
840                 ArrayList pars = (ArrayList) $1;
841
842                 pars.Add ($3);
843                 $$ = $1;
844           }
845         ;
846
847 fixed_parameter
848         : opt_attributes
849           opt_parameter_modifier
850           type
851           IDENTIFIER
852           {
853                 $$ = new Parameter ((string) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
854           }
855         ;
856
857 opt_parameter_modifier
858         : /* empty */           { $$ = Parameter.Modifier.NONE; }
859         | parameter_modifier
860         ;
861
862 parameter_modifier
863         : REF                   { $$ = Parameter.Modifier.REF; }
864         | OUT                   { $$ = Parameter.Modifier.OUT; }
865         ;
866
867 parameter_array
868         : opt_attributes PARAMS type IDENTIFIER
869           { 
870                 $$ = new Parameter ((string) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
871                 note ("type must be a single-dimension array type"); 
872           }
873         ;
874
875 member_name 
876         : IDENTIFIER { $$ = $1.ToString (); }
877         | interface_type DOT IDENTIFIER { $$ = $1.ToString () + "." + $3.ToString (); }
878         ;
879
880 property_declaration
881         : opt_attributes
882           opt_modifiers
883           type member_name
884           OPEN_BRACE 
885           {
886                 implicit_value_parameter_type = (string) $3;
887
888                 lexer.properties = true;
889                 temporary_loc = lexer.Location;
890           }
891           accessor_declarations 
892           {
893                 lexer.properties = false;
894           }
895           CLOSE_BRACE
896           { 
897                 Property prop;
898                 DictionaryEntry pair = (DictionaryEntry) $7;
899                 Block get_block = null;
900                 Block set_block = null;
901
902                 if (pair.Key != null)
903                         get_block = (Block) pair.Key;
904                 if (pair.Value != null)
905                         set_block = (Block) pair.Value;
906
907                 prop = new Property ((string) $3, (string) $4, (int) $2, get_block, set_block,
908                                      (Attributes) $1, temporary_loc);
909                 
910                 CheckDef (current_container.AddProperty (prop), prop.Name);
911                 implicit_value_parameter_type = null;
912           }
913         ;
914
915 accessor_declarations
916         : get_accessor_declaration opt_set_accessor_declaration
917           { 
918                 $$ = new DictionaryEntry ($1, $2);
919           }
920         | set_accessor_declaration opt_get_accessor_declaration
921           {
922                 $$ = new DictionaryEntry ($2, $1);
923           }
924         ;
925
926 opt_get_accessor_declaration
927         : /* empty */                   { $$ = null; }
928         | get_accessor_declaration
929         ;
930
931 opt_set_accessor_declaration
932         : /* empty */                   { $$ = null; }
933         | set_accessor_declaration
934         ;
935
936 get_accessor_declaration
937         : opt_attributes GET
938           {
939                 // If this is not the case, then current_local_parameters has already
940                 // been set in indexer_declaration
941                 if (parsing_indexer == false)
942                         current_local_parameters = null;
943                 else 
944                         current_local_parameters = indexer_parameters;
945
946                 
947           }
948           accessor_body
949           {
950                 $$ = $4;
951                 current_local_parameters = null;
952           }
953         ;
954
955 set_accessor_declaration
956         : opt_attributes SET 
957           {
958                 Parameter [] args;
959                 Parameter implicit_value_parameter = new Parameter (
960                         implicit_value_parameter_type, "value", 
961                         Parameter.Modifier.NONE, null);
962
963                 if (parsing_indexer == false) {
964                         args  = new Parameter [1];
965                         args [0] = implicit_value_parameter;
966                 } else {
967                         Parameter [] fp = indexer_parameters.FixedParameters;
968                         int count = fp.Length;
969
970                         args = new Parameter [count + 1];
971         
972                         fp.CopyTo (args, 0);
973                         args [count] = implicit_value_parameter;
974                 }
975                 current_local_parameters = new Parameters (args, null);
976           }
977           accessor_body
978           {
979                 $$ = $4;
980                 current_local_parameters = null;
981           }
982         ;
983
984 accessor_body
985         : block 
986         | SEMICOLON             { $$ = new Block (null); }
987         ;
988
989 interface_declaration
990         : opt_attributes
991           opt_modifiers
992           INTERFACE IDENTIFIER
993           {
994                 Interface new_interface;
995                 string full_interface_name = MakeName ((string) $4);
996
997                 new_interface = new Interface (rc, current_container, full_interface_name, (int) $2, 
998                                                (Attributes) $1, lexer.Location);
999                 if (current_interface != null) {
1000                         Location l = lexer.Location;
1001                         Report.Error (-2, l, "Internal compiler error: interface inside interface");
1002                 }
1003                 current_interface = new_interface;
1004                 tree.RecordInterface (full_interface_name, new_interface);
1005           }
1006           opt_interface_base
1007           interface_body
1008           { 
1009                 Interface new_interface = (Interface) current_interface;
1010
1011                 if ($6 != null)
1012                         new_interface.Bases = (ArrayList) $6;
1013
1014                 current_interface = null;
1015                 CheckDef (current_container.AddInterface (new_interface), new_interface.Name);
1016           }
1017         ;
1018
1019 opt_interface_base
1020         : /* empty */                     { $$ = null; }
1021         | interface_base
1022         ;
1023
1024 interface_base
1025         : COLON interface_type_list       { $$ = $2; }
1026         ;
1027
1028 interface_type_list
1029         : interface_type
1030           {
1031                 ArrayList interfaces = new ArrayList ();
1032
1033                 interfaces.Add ($1);
1034                 $$ = interfaces;
1035           }
1036         | interface_type_list COMMA interface_type
1037           {
1038                 ArrayList interfaces = (ArrayList) $1;
1039                 interfaces.Add ($3);
1040                 $$ = interfaces;
1041           }
1042         ;
1043
1044 interface_body
1045         : OPEN_BRACE
1046           opt_interface_member_declarations
1047           CLOSE_BRACE
1048         ;
1049
1050 opt_interface_member_declarations
1051         : /* empty */
1052         | interface_member_declarations
1053         ;
1054
1055 interface_member_declarations
1056         : interface_member_declaration
1057         | interface_member_declarations interface_member_declaration
1058         ;
1059
1060 interface_member_declaration
1061         : interface_method_declaration          
1062           { 
1063                 InterfaceMethod m = (InterfaceMethod) $1;
1064
1065                 CheckDef (current_interface.AddMethod (m), m.Name);
1066           }
1067         | interface_property_declaration        
1068           { 
1069                 InterfaceProperty p = (InterfaceProperty) $1;
1070
1071                 CheckDef (current_interface.AddProperty (p), p.Name);
1072           }
1073         | interface_event_declaration 
1074           { 
1075                 InterfaceEvent e = (InterfaceEvent) $1;
1076
1077                 CheckDef (current_interface.AddEvent (e), e.Name);
1078           }
1079         | interface_indexer_declaration
1080           { 
1081                 InterfaceIndexer i = (InterfaceIndexer) $1;
1082
1083                 CheckDef (current_interface.AddIndexer (i), "indexer");
1084           }
1085         ;
1086
1087 opt_new
1088         : /* empty */   { $$ = false; }
1089         | NEW           { $$ = true; }
1090         ;
1091
1092 interface_method_declaration
1093         : opt_attributes opt_new type IDENTIFIER 
1094           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1095           SEMICOLON
1096           {
1097                 $$ = new InterfaceMethod ((string) $3, (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1098           }
1099         | opt_attributes opt_new VOID IDENTIFIER 
1100           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1101           SEMICOLON
1102           {
1103                 $$ = new InterfaceMethod ("System.Void", (string) $4, (bool) $2, (Parameters) $6, (Attributes) $1);
1104           }
1105         ;
1106
1107 interface_property_declaration
1108         : opt_attributes
1109           opt_new
1110           type IDENTIFIER 
1111           OPEN_BRACE 
1112           { lexer.properties = true; }
1113           interface_accesors 
1114           { lexer.properties = false; }
1115           CLOSE_BRACE
1116           {
1117                 int gs = (int) $7;
1118
1119                 $$ = new InterfaceProperty ((string) $3, (string) $4, (bool) $2, 
1120                                             (gs & 1) == 1, (gs & 2) == 2, (Attributes) $1);
1121           }
1122         ;
1123
1124 interface_accesors
1125         : opt_attributes GET SEMICOLON          { $$ = 1; }
1126         | opt_attributes SET SEMICOLON          { $$ = 2; }
1127         | opt_attributes GET SEMICOLON opt_attributes SET SEMICOLON 
1128           { $$ = 3; }
1129         | opt_attributes SET SEMICOLON opt_attributes GET SEMICOLON
1130           { $$ = 3; }
1131         ;
1132
1133 interface_event_declaration
1134         : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1135           {
1136                 $$ = new InterfaceEvent ((string) $4, (string) $5, (bool) $2, (Attributes) $1);
1137           }
1138         ;
1139
1140 interface_indexer_declaration 
1141         : opt_attributes opt_new type THIS 
1142           OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1143           OPEN_BRACE 
1144           { lexer.properties = true; }
1145           interface_accesors 
1146           { lexer.properties = false; }
1147           CLOSE_BRACE
1148           {
1149                 int a_flags = (int) $10;
1150
1151                 bool do_get = (a_flags & 1) == 1;
1152                 bool do_set = (a_flags & 2) == 2;
1153
1154                 $$ = new InterfaceIndexer ((string) $3, (Parameters) $6, do_get, do_set, (bool) $2, (Attributes) $1);
1155           }
1156         ;
1157
1158 operator_declaration
1159         : opt_attributes opt_modifiers operator_declarator block
1160           {
1161                 OperatorDeclaration decl = (OperatorDeclaration) $3;
1162                 
1163                 Operator op = new Operator (decl.optype, decl.ret_type, (int) $2, decl.arg1type, decl.arg1name,
1164                                             decl.arg2type, decl.arg2name, (Block) $4, (Attributes) $1, decl.location);
1165
1166                 // Note again, checking is done in semantic analysis
1167                 current_container.AddOperator (op);
1168           }
1169         ;
1170
1171 operator_declarator
1172         : type OPERATOR overloadable_operator 
1173           OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1174         {
1175                 Operator.OpType op = (Operator.OpType) $3;
1176                 CheckUnaryOperator (op);
1177
1178                 if (op == Operator.OpType.Addition)
1179                         op = Operator.OpType.UnaryPlus;
1180
1181                 if (op == Operator.OpType.Subtraction)
1182                         op = Operator.OpType.UnaryNegation;
1183
1184                 $$ = new OperatorDeclaration (op, (string) $1, (string) $5, (string) $6,
1185                                               null, null, lexer.Location);
1186         }
1187         | type OPERATOR overloadable_operator
1188           OPEN_PARENS 
1189                 type IDENTIFIER COMMA
1190                 type IDENTIFIER 
1191           CLOSE_PARENS
1192         {
1193                CheckBinaryOperator ((Operator.OpType) $3);
1194                
1195                $$ = new OperatorDeclaration ((Operator.OpType) $3, (string) $1, (string) $5, (string) $6,
1196                                              (string) $8, (string) $9, lexer.Location);
1197         }
1198         | conversion_operator_declarator
1199         ;
1200
1201 overloadable_operator
1202 // Unary operators:
1203         : BANG   { $$ = Operator.OpType.LogicalNot; }
1204         | TILDE  { $$ = Operator.OpType.OnesComplement; }  
1205         | OP_INC { $$ = Operator.OpType.Increment; }
1206         | OP_DEC { $$ = Operator.OpType.Decrement; }
1207         | TRUE   { $$ = Operator.OpType.True; }
1208         | FALSE  { $$ = Operator.OpType.False; }
1209 // Unary and binary:
1210         | PLUS { $$ = Operator.OpType.Addition; }
1211         | MINUS { $$ = Operator.OpType.Subtraction; }
1212 // Binary:
1213         | STAR { $$ = Operator.OpType.Multiply; }
1214         | DIV {  $$ = Operator.OpType.Division; }
1215         | PERCENT { $$ = Operator.OpType.Modulus; }
1216         | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1217         | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1218         | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1219         | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1220         | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1221         | OP_EQ { $$ = Operator.OpType.Equality; }
1222         | OP_NE { $$ = Operator.OpType.Inequality; }
1223         | OP_GT { $$ = Operator.OpType.GreaterThan; }
1224         | OP_LT { $$ = Operator.OpType.LessThan; }
1225         | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1226         | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1227         ;
1228
1229 conversion_operator_declarator
1230         : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1231           {
1232                 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (string) $3, (string) $5, (string) $6,
1233                                               null, null, lexer.Location);
1234           }
1235         | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1236           {
1237                 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (string) $3, (string) $5, (string) $6,
1238                                               null, null, lexer.Location);
1239           }
1240         ;
1241
1242 constructor_declaration
1243         : opt_attributes
1244           opt_modifiers
1245           constructor_declarator
1246           block
1247           { 
1248                 Constructor c = (Constructor) $3;
1249                 c.Block = (Block) $4;
1250                 c.ModFlags = (int) $2;
1251                 c.OptAttributes = (Attributes) $1;
1252
1253                 if ((c.ModFlags & Modifiers.STATIC) != 0){
1254                         if ((c.ModFlags & Modifiers.Accessibility) != 0) {
1255                                 Location l = lexer.Location;
1256                                 Report.Error (515, l, "Access modifiers are not allowed on static constructors");
1257                         }
1258
1259                         if (c.Initializer != null){
1260                                 Location l = lexer.Location;
1261                                 Report.Error (514, l, "Static constructors can not have an explicit this or base constructor invocations");
1262                         }
1263
1264                         if (!c.Parameters.Empty){
1265                                 Location l = lexer.Location;
1266                                 Report.Error (103, l, "Static constructors should not have parameters");
1267                         }
1268                 } 
1269                 
1270                 CheckDef (current_container.AddConstructor (c), c.Name);
1271
1272                 current_local_parameters = null;
1273           }
1274         ;
1275
1276 constructor_declarator
1277         : IDENTIFIER 
1278           OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS 
1279           opt_constructor_initializer
1280           {
1281                 Location l = lexer.Location;
1282
1283                 $$ = new Constructor ((string) $1, (Parameters) $3, (ConstructorInitializer) $5, l);
1284         
1285                 current_local_parameters = (Parameters) $3;
1286           }
1287         ;
1288
1289 opt_constructor_initializer
1290         : /* empty */                   { $$ = null; }
1291         | constructor_initializer
1292         ;
1293
1294 constructor_initializer
1295         : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1296           {
1297                 $$ = new ConstructorBaseInitializer ((ArrayList) $4, lexer.Location);
1298           }
1299         | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1300           {
1301                 $$ = new ConstructorThisInitializer ((ArrayList) $4, lexer.Location);
1302           }
1303         ;
1304
1305 destructor_declaration
1306         : opt_attributes TILDE IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1307           {
1308                 //
1309                 // FIXME: The destructor needs to generate special code:
1310                 //
1311                 // ~X ()
1312                 // { balh (); }
1313                 //
1314                 //
1315                 // void Finalize () { 
1316                 // try { code } finally { base.Finalize (); }
1317                 //
1318                 Method d = new Method ("System.Void", 0, "Finalize", 
1319                                        new Parameters (null, null), (Attributes) $1, lexer.Location);
1320                   
1321                 d.Block = (Block) $6;
1322                 CheckDef (current_container.AddMethod (d), d.Name);
1323           }
1324         ;
1325
1326 event_declaration
1327         : opt_attributes
1328           opt_modifiers
1329           EVENT type variable_declarators SEMICOLON
1330           {
1331                 foreach (VariableDeclaration var in (ArrayList) $5) {
1332
1333                         // FIXME : Is this right ?
1334                         Event e = new Event ((string) $4, var.identifier, var.expression_or_array_initializer,
1335                                              (int) $2, null, null, (Attributes) $1, lexer.Location);
1336
1337                         CheckDef (current_container.AddEvent (e), e.Name);
1338                                        
1339                 }
1340           }
1341         | opt_attributes
1342           opt_modifiers
1343           EVENT type member_name 
1344           OPEN_BRACE event_accessor_declarations CLOSE_BRACE
1345         {
1346                 DictionaryEntry pair = (DictionaryEntry) $7;
1347                 Block add_block = null;
1348                 Block rem_block = null;
1349
1350                 if (pair.Key != null)
1351                         add_block = (Block) pair.Key;
1352                 if (pair.Value != null)
1353                         rem_block = (Block) pair.Value;
1354                 
1355                 Event e = new Event ((string) $4, (string) $5, null, (int) $2, add_block, rem_block,
1356                                      (Attributes) $1, lexer.Location);
1357                 
1358                 CheckDef (current_container.AddEvent (e), e.Name);
1359         }
1360         ;
1361
1362 event_accessor_declarations
1363         : add_accessor_declaration remove_accessor_declaration
1364         {
1365                 $$ = new DictionaryEntry ($1, $2);
1366         }
1367         | remove_accessor_declaration add_accessor_declaration
1368         {
1369                 $$ = new DictionaryEntry ($2, $1);
1370         }       
1371         ;
1372
1373 add_accessor_declaration
1374         : opt_attributes ADD block
1375           {
1376                 $$ = $3;
1377           }
1378         ;
1379
1380 remove_accessor_declaration
1381         : opt_attributes REMOVE block
1382         {
1383                 $$ = $3;
1384         }
1385         ;
1386
1387 indexer_declaration
1388         : opt_attributes opt_modifiers indexer_declarator 
1389           OPEN_BRACE 
1390           {
1391                 IndexerDeclaration decl = (IndexerDeclaration) $3;
1392
1393                 implicit_value_parameter_type = decl.type;
1394                 
1395                 lexer.properties = true;
1396                 temporary_loc = lexer.Location;
1397                 parsing_indexer  = true;
1398                 
1399                 indexer_parameters = decl.param_list;
1400           }
1401           accessor_declarations 
1402           {
1403                   lexer.properties = false;
1404                   parsing_indexer  = false;
1405           }
1406           CLOSE_BRACE
1407           { 
1408                 // The signature is computed from the signature of the indexer.  Look
1409                 // at section 3.6 on the spec
1410
1411                 Indexer indexer;
1412                 IndexerDeclaration decl = (IndexerDeclaration) $3;
1413                 DictionaryEntry pair = (DictionaryEntry) $6;
1414                 Block get_block = null;
1415                 Block set_block = null;
1416
1417                 if (pair.Key != null)
1418                         get_block = (Block) pair.Key;
1419                 if (pair.Value != null)
1420                         set_block = (Block) pair.Value;
1421
1422                 indexer = new Indexer (decl.type, decl.interface_type, (int) $2, decl.param_list,
1423                                        get_block, set_block, (Attributes) $1, temporary_loc);
1424
1425                 // Note that there is no equivalent of CheckDef for this case
1426                 // We shall handle this in semantic analysis
1427                 
1428                 current_container.AddIndexer (indexer);
1429                 
1430                 current_local_parameters = null;
1431                 implicit_value_parameter_type = null;
1432                 indexer_parameters = null;
1433           }
1434         ;
1435
1436 indexer_declarator
1437         : type THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1438           {
1439                 $$ = new IndexerDeclaration ((string) $1, null, (Parameters) $4);
1440           }
1441         | type interface_type DOT THIS OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1442           {
1443                 $$ = new IndexerDeclaration ((string) $1, (string) $2, (Parameters) $6);
1444           }
1445         ;
1446
1447 enum_declaration
1448         : opt_attributes
1449           opt_modifiers
1450           ENUM IDENTIFIER
1451           opt_enum_base
1452           enum_body
1453           opt_semicolon
1454           { 
1455                 string name = (string) $4;
1456                 Enum e = new Enum (rc, (string) $5, (int) $2, name, (Attributes) $1, lexer.Location);
1457
1458                 foreach (VariableDeclaration ev in (ArrayList) $6){
1459                         CheckDef (e.AddEnumMember (ev.identifier, 
1460                                                    (Expression) ev.expression_or_array_initializer),
1461                                   ev.identifier);
1462                 }
1463
1464                 CheckDef (current_container.AddEnum (e), name);
1465           }
1466         ;
1467
1468 opt_enum_base
1469         : /* empty */                   { $$ = "System.Int32"; }
1470         | COLON integral_type           { $$ = $2;   }
1471         ;
1472
1473 enum_body
1474         : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
1475           {
1476                 $$ = $2;
1477           }
1478         ;
1479
1480 opt_enum_member_declarations
1481         : /* empty */                   { $$ = new ArrayList (); }
1482         | enum_member_declarations opt_comma { $$ = $1; }
1483         ;
1484
1485 enum_member_declarations
1486         : enum_member_declaration 
1487           {
1488                 ArrayList l = new ArrayList ();
1489
1490                 l.Add ($1);
1491                 $$ = l;
1492           }
1493         | enum_member_declarations COMMA enum_member_declaration
1494           {
1495                 ArrayList l = (ArrayList) $1;
1496
1497                 l.Add ($3);
1498
1499                 $$ = l;
1500           }
1501         ;
1502
1503 enum_member_declaration
1504         : opt_attributes IDENTIFIER 
1505           {
1506                 $$ = new VariableDeclaration ((string) $2, null, lexer.Location);
1507           }
1508         | opt_attributes IDENTIFIER ASSIGN expression
1509           { 
1510                 $$ = new VariableDeclaration ((string) $2, $4, lexer.Location);
1511           }
1512         ;
1513
1514 delegate_declaration
1515         : opt_attributes
1516           opt_modifiers
1517           DELEGATE type   
1518           IDENTIFIER OPEN_PARENS 
1519           formal_parameter_list
1520           CLOSE_PARENS 
1521           SEMICOLON
1522           {
1523                 Delegate del = new Delegate (rc, (string) $4, (int) $2, 
1524                                              MakeName ((string) $5), (Parameters) $7, 
1525                                              (Attributes) $1, lexer.Location);
1526                   
1527                 CheckDef (current_container.AddDelegate (del), del.Name);
1528           }     
1529         | opt_attributes
1530           opt_modifiers
1531           DELEGATE VOID   
1532           IDENTIFIER OPEN_PARENS 
1533           formal_parameter_list
1534           CLOSE_PARENS 
1535           SEMICOLON
1536           {
1537                 Delegate del = new Delegate (rc, "System.Void", (int) $2, (string) $5, (Parameters) $7, 
1538                                              (Attributes) $1, lexer.Location);
1539
1540                 CheckDef (current_container.AddDelegate (del), del.Name);
1541           }
1542         ;
1543
1544 type_name
1545         : namespace_or_type_name
1546         ;
1547
1548 namespace_or_type_name
1549         : qualified_identifier
1550         ;
1551
1552 /* 
1553  * Before you think of adding a return_type, notice that we have been
1554  * using two rules in the places where it matters (one rule using type
1555  * and another identical one that uses VOID as the return type).  This
1556  * gets rid of a shift/reduce couple
1557  */
1558 type
1559         : type_name {   /* class_type */
1560                 /* 
1561                    This does interfaces, delegates, struct_types, class_types, 
1562                    parent classes, and more! 4.2 
1563                  */
1564                 $$ = $1; 
1565           }
1566         | builtin_types
1567         | array_type
1568         ;
1569
1570 type_list
1571         : type
1572           {
1573                 ArrayList types = new ArrayList ();
1574
1575                 types.Add ($1);
1576                 $$ = types;
1577           }
1578         | type_list COMMA type
1579           {
1580                 ArrayList types = (ArrayList) $1;
1581
1582                 types.Add ($3);
1583                 $$ = types;
1584           }
1585         ;
1586
1587 /*
1588  * replaces all the productions for isolating the various
1589  * simple types, but we need this to reuse it easily in local_variable_type
1590  */
1591 builtin_types
1592         : OBJECT        { $$ = "System.Object"; }
1593         | STRING        { $$ = "System.String"; }
1594         | BOOL          { $$ = "System.Boolean"; }
1595         | DECIMAL       { $$ = "System.Decimal"; }
1596         | FLOAT         { $$ = "System.Single"; }
1597         | DOUBLE        { $$ = "System.Double"; }
1598         | integral_type
1599         ;
1600
1601 integral_type
1602         : SBYTE         { $$ = "System.SByte"; }
1603         | BYTE          { $$ = "System.Byte"; }
1604         | SHORT         { $$ = "System.Int16"; }
1605         | USHORT        { $$ = "System.UInt16"; }
1606         | INT           { $$ = "System.Int32"; }
1607         | UINT          { $$ = "System.UInt32"; }
1608         | LONG          { $$ = "System.Int64"; }
1609         | ULONG         { $$ = "System.UInt64"; }
1610         | CHAR          { $$ = "System.Char"; }
1611         ;
1612
1613 interface_type
1614         : type_name
1615         ;
1616
1617 array_type
1618         : type rank_specifiers
1619           {
1620                   $$ = (string) $1 + (string) $2;
1621           }
1622         ;
1623
1624 //
1625 // Expressions, section 7.5
1626 //
1627 primary_expression
1628         : literal
1629           {
1630                 // 7.5.1: Literals
1631           }
1632  
1633         | qualified_identifier
1634           {
1635                 string name = (string) $1;
1636
1637                 $$ = null;
1638                 $$ = DecomposeQI (name, lexer.Location);
1639           }
1640         | parenthesized_expression
1641         | member_access
1642         | invocation_expression
1643         | element_access
1644         | this_access
1645         | base_access
1646         | post_increment_expression
1647         | post_decrement_expression
1648         | new_expression
1649         | typeof_expression
1650         | sizeof_expression
1651         | checked_expression
1652         | unchecked_expression
1653         ;
1654
1655 literal
1656         : boolean_literal
1657         | integer_literal
1658         | real_literal
1659         | LITERAL_CHARACTER     { $$ = new CharLiteral ((char) lexer.Value); }
1660         | LITERAL_STRING        { $$ = new StringLiteral ((string) lexer.Value); }
1661         | NULL                  { $$ = new NullLiteral (); }
1662         ;
1663
1664 real_literal
1665         : LITERAL_FLOAT         { $$ = new FloatLiteral ((float) lexer.Value); }
1666         | LITERAL_DOUBLE        { $$ = new DoubleLiteral ((double) lexer.Value); }
1667         | LITERAL_DECIMAL       { $$ = new DecimalLiteral ((decimal) lexer.Value); }
1668         ;
1669
1670 integer_literal
1671         : LITERAL_INTEGER       { 
1672                 object v = lexer.Value;
1673
1674                 // 
1675                 // FIXME: Possible optimization would be to 
1676                 // compute the *Literal objects directly in the scanner
1677                 //
1678                 if (v is int)
1679                         $$ = new IntLiteral ((Int32) v); 
1680                 else if (v is uint)
1681                         $$ = new UIntLiteral ((UInt32) v);
1682                 else if (v is long)
1683                         $$ = new LongLiteral ((Int64) v);
1684                 else if (v is ulong)
1685                         $$ = new ULongLiteral ((UInt64) v);
1686                 else
1687                         Console.WriteLine ("OOPS.  Unexpected result from scanner");
1688           }
1689         ;
1690
1691 boolean_literal
1692         : TRUE                  { $$ = new BoolLiteral (true); }
1693         | FALSE                 { $$ = new BoolLiteral (false); }
1694         ;
1695
1696 parenthesized_expression
1697         : OPEN_PARENS expression CLOSE_PARENS
1698           { $$ = $2; }
1699         ;
1700
1701 member_access
1702         : primary_expression DOT IDENTIFIER
1703           {
1704                 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
1705           }
1706         | predefined_type DOT IDENTIFIER
1707           {
1708                 $$ = new SimpleName ((string) $1 + "." + (string) $3, lexer.Location);
1709           }
1710         ;
1711
1712 predefined_type
1713         : builtin_types
1714         ;
1715
1716 invocation_expression
1717         : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
1718           {
1719                 // FIXME:
1720                 // if $1 is MethodGroup
1721                 //      $$ = new Call ($1, $3);
1722                 // else 
1723                 //      $$ = new DelegateCall ($1, $3);
1724                 if ($1 == null) {
1725                         Location l = lexer.Location;
1726                         Report.Error (1, l, "THIS IS CRAZY");
1727                 }
1728                 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
1729                 
1730           }
1731         ; 
1732
1733 opt_argument_list
1734         : /* empty */           { $$ = null; }
1735         | argument_list
1736         ;
1737
1738 argument_list
1739         : argument              
1740           { 
1741                 ArrayList list = new ArrayList ();
1742                 list.Add ($1);
1743                 $$ = list;
1744           }
1745         | argument_list COMMA argument
1746           {
1747                 ArrayList list = (ArrayList) $1;
1748                 list.Add ($3);
1749                 $$ = list;
1750           }
1751         ;
1752
1753 argument
1754         : expression
1755           {
1756                 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
1757           }
1758         | REF variable_reference 
1759           { 
1760                 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
1761           }
1762         | OUT variable_reference 
1763           { 
1764                 $$ = new Argument ((Expression) $2, Argument.AType.Out);
1765           }
1766         ;
1767
1768 variable_reference
1769         : expression { note ("section 5.4"); $$ = $1; }
1770         ;
1771
1772 element_access
1773         : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET 
1774           {
1775                 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
1776           }
1777         ;
1778
1779 expression_list
1780         : expression
1781           {
1782                 ArrayList list = new ArrayList ();
1783                 list.Add ($1);
1784                 $$ = list;
1785           }
1786         | expression_list COMMA expression
1787           {
1788                 ArrayList list = (ArrayList) $1;
1789                 list.Add ($3);
1790                 $$ = list;
1791           }
1792         ;
1793
1794 this_access
1795         : THIS
1796           {
1797                 $$ = new This (lexer.Location);
1798           }
1799         ;
1800
1801 base_access
1802         : BASE DOT IDENTIFIER
1803           {
1804                 $$ = new BaseAccess (BaseAccess.BaseAccessType.Member, (string) $3, null);
1805           }
1806         | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
1807           {
1808                 $$ = new BaseAccess (BaseAccess.BaseAccessType.Indexer, null, (ArrayList) $3);
1809           }
1810         ;
1811
1812 post_increment_expression
1813         : primary_expression OP_INC
1814           {
1815                 $$ = new Unary (Unary.Operator.PostIncrement, (Expression) $1, lexer.Location);
1816           }
1817         ;
1818
1819 post_decrement_expression
1820         : primary_expression OP_DEC
1821           {
1822                 $$ = new Unary (Unary.Operator.PostDecrement, (Expression) $1, lexer.Location);
1823           }
1824         ;
1825
1826 new_expression
1827         : object_or_delegate_creation_expression
1828         | array_creation_expression
1829         ;
1830
1831 object_or_delegate_creation_expression
1832         : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
1833           {
1834                 $$ = new New ((string) $2, (ArrayList) $4, lexer.Location);
1835           }
1836         ;
1837
1838 array_creation_expression
1839         : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET 
1840           opt_rank_specifier
1841           opt_array_initializer
1842           {
1843                 $$ = new ArrayCreation ((string) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, 
1844                                         lexer.Location);
1845           }
1846         | NEW type rank_specifiers array_initializer
1847           {
1848                 $$ = new ArrayCreation ((string) $2, (string) $3, (ArrayList) $4, lexer.Location);
1849           }
1850         ;
1851
1852 opt_rank_specifier
1853         : /* empty */
1854           {
1855                   $$ = "";
1856           }
1857         | rank_specifiers
1858           {
1859                         $$ = $1;
1860           }
1861         ;
1862
1863 rank_specifiers
1864         : rank_specifier
1865           {
1866                   $$ = $1;
1867           }
1868         | rank_specifiers rank_specifier
1869           {
1870                   $$ = (string) $2 + (string) $1;
1871           }             
1872         ;
1873
1874 rank_specifier
1875         : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
1876           {
1877                 $$ = "[" + (string) $2 + "]";
1878           }
1879         ;
1880
1881 opt_dim_separators
1882         : /* empty */
1883           {
1884                 $$ = "";
1885           }
1886         | dim_separators
1887           {
1888                   $$ = $1;
1889           }               
1890         ;
1891
1892 dim_separators
1893         : COMMA
1894           {
1895                 $$ = ",";
1896           }
1897         | dim_separators COMMA
1898           {
1899                 $$ = (string) $1 + ",";
1900           }
1901         ;
1902
1903 opt_array_initializer
1904         : /* empty */
1905           {
1906                 $$ = null;
1907           }
1908         | array_initializer
1909           {
1910                 $$ = $1;
1911           }
1912         ;
1913
1914 array_initializer
1915         : OPEN_BRACE CLOSE_BRACE
1916           {
1917                 ArrayList list = new ArrayList ();
1918                 $$ = list;
1919           }
1920         | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
1921           {
1922                 $$ = (ArrayList) $2;
1923           }
1924         ;
1925
1926 variable_initializer_list
1927         : variable_initializer
1928           {
1929                 ArrayList list = new ArrayList ();
1930                 list.Add ($1);
1931                 $$ = list;
1932           }
1933         | variable_initializer_list COMMA variable_initializer
1934           {
1935                 ArrayList list = (ArrayList) $1;
1936                 list.Add ($3);
1937                 $$ = list;
1938           }
1939         ;
1940
1941 typeof_expression
1942         : TYPEOF OPEN_PARENS type CLOSE_PARENS
1943           {
1944                 $$ = new TypeOf ((string) $3);
1945           }
1946         ;
1947
1948 sizeof_expression
1949         : SIZEOF OPEN_PARENS type CLOSE_PARENS { 
1950                 $$ = new SizeOf ((string) $3);
1951
1952                 note ("Verify type is unmanaged"); 
1953                 note ("if (5.8) builtin, yield constant expression");
1954           }
1955         ;
1956
1957 checked_expression
1958         : CHECKED OPEN_PARENS expression CLOSE_PARENS
1959           {
1960                 $$ = new CheckedExpr ((Expression) $3);
1961           }
1962         ;
1963
1964 unchecked_expression
1965         : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
1966           {
1967                 $$ = new UnCheckedExpr ((Expression) $3);
1968           }
1969         ;
1970
1971 unary_expression
1972         : primary_expression
1973         | PLUS unary_expression
1974           { 
1975                 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
1976           } 
1977         | MINUS unary_expression 
1978           { 
1979                 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
1980           }
1981         | BANG unary_expression 
1982           {
1983                 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
1984           }
1985         | TILDE unary_expression
1986           {
1987                 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
1988           }
1989         | STAR unary_expression
1990           {
1991                 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
1992           }
1993         | BITWISE_AND unary_expression
1994           {
1995                 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
1996           }
1997         | OP_INC unary_expression 
1998           {
1999                 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
2000           }
2001         | OP_DEC unary_expression 
2002           {
2003                 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
2004           }
2005         | cast_expression 
2006         /*
2007          we can not do cast expressions at this level,
2008          as there is an ambiguity.  Check "Cast Expressions" 7.6.8
2009          for the recipe to handle this.
2010          */
2011         ;
2012
2013 pre_increment_expression
2014         : OP_INC unary_expression 
2015           {
2016                 $$ = new Unary (Unary.Operator.PreIncrement, (Expression) $2, lexer.Location);
2017           }
2018         ;
2019
2020 pre_decrement_expression
2021         : OP_DEC unary_expression 
2022           {
2023                 $$ = new Unary (Unary.Operator.PreDecrement, (Expression) $2, lexer.Location);
2024           }
2025         ;
2026
2027 cast_expression
2028         /* 
2029          * FIXME: This is actually wrong, it should be `type' but that
2030          * introduces a lot of {shift,reduce}/reduces
2031          *
2032          * This is really really wrong.  We need to track down
2033          * the source of problems with QIs because expressions like:
2034          * foreach (string s in (string []) object) wont be parsed.
2035          */
2036         : OPEN_PARENS qualified_identifier CLOSE_PARENS unary_expression
2037           {
2038                 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
2039           }
2040         | OPEN_PARENS builtin_types CLOSE_PARENS unary_expression
2041           {
2042                 $$ = new Cast ((string) $2, (Expression) $4, lexer.Location);
2043           }
2044         ;
2045
2046 multiplicative_expression
2047         : unary_expression
2048         | multiplicative_expression STAR unary_expression
2049           {
2050                 $$ = new Binary (Binary.Operator.Multiply, 
2051                                  (Expression) $1, (Expression) $3, lexer.Location);
2052           }
2053         | multiplicative_expression DIV unary_expression
2054           {
2055                 $$ = new Binary (Binary.Operator.Division, 
2056                                  (Expression) $1, (Expression) $3, lexer.Location);
2057           }
2058         | multiplicative_expression PERCENT unary_expression 
2059           {
2060                 $$ = new Binary (Binary.Operator.Modulus, 
2061                                  (Expression) $1, (Expression) $3, lexer.Location);
2062           }
2063         ;
2064
2065 additive_expression
2066         : multiplicative_expression
2067         | additive_expression PLUS multiplicative_expression 
2068           {
2069                 $$ = new Binary (Binary.Operator.Addition, 
2070                                  (Expression) $1, (Expression) $3, lexer.Location);
2071           }
2072         | additive_expression MINUS multiplicative_expression
2073           {
2074                 $$ = new Binary (Binary.Operator.Subtraction, 
2075                                  (Expression) $1, (Expression) $3, lexer.Location);
2076           }
2077         ;
2078
2079 shift_expression
2080         : additive_expression
2081         | shift_expression OP_SHIFT_LEFT additive_expression
2082           {
2083                 $$ = new Binary (Binary.Operator.LeftShift, 
2084                                  (Expression) $1, (Expression) $3, lexer.Location);
2085           }
2086         | shift_expression OP_SHIFT_RIGHT additive_expression
2087           {
2088                 $$ = new Binary (Binary.Operator.RightShift, 
2089                                  (Expression) $1, (Expression) $3, lexer.Location);
2090           }
2091         ; 
2092
2093 relational_expression
2094         : shift_expression
2095         | relational_expression OP_LT shift_expression
2096           {
2097                 $$ = new Binary (Binary.Operator.LessThan, 
2098                                  (Expression) $1, (Expression) $3, lexer.Location);
2099           }
2100         | relational_expression OP_GT shift_expression
2101           {
2102                 $$ = new Binary (Binary.Operator.GreaterThan, 
2103                                  (Expression) $1, (Expression) $3, lexer.Location);
2104           }
2105         | relational_expression OP_LE shift_expression
2106           {
2107                 $$ = new Binary (Binary.Operator.LessThanOrEqual, 
2108                                  (Expression) $1, (Expression) $3, lexer.Location);
2109           }
2110         | relational_expression OP_GE shift_expression
2111           {
2112                 $$ = new Binary (Binary.Operator.GreaterThanOrEqual, 
2113                                  (Expression) $1, (Expression) $3, lexer.Location);
2114           }
2115         | relational_expression IS type
2116           {
2117                 $$ = new Probe (Probe.Operator.Is, 
2118                                  (Expression) $1, (string) $3);
2119           }
2120         | relational_expression AS type
2121           {
2122                 $$ = new Probe (Probe.Operator.As, 
2123                                  (Expression) $1, (string) $3);
2124           }
2125         ;
2126
2127 equality_expression
2128         : relational_expression
2129         | equality_expression OP_EQ relational_expression
2130           {
2131                 $$ = new Binary (Binary.Operator.Equality, 
2132                                  (Expression) $1, (Expression) $3, lexer.Location);
2133           }
2134         | equality_expression OP_NE relational_expression
2135           {
2136                 $$ = new Binary (Binary.Operator.Inequality, 
2137                                  (Expression) $1, (Expression) $3, lexer.Location);
2138           }
2139         ; 
2140
2141 and_expression
2142         : equality_expression
2143         | and_expression BITWISE_AND equality_expression
2144           {
2145                 $$ = new Binary (Binary.Operator.BitwiseAnd, 
2146                                  (Expression) $1, (Expression) $3, lexer.Location);
2147           }
2148         ;
2149
2150 exclusive_or_expression
2151         : and_expression
2152         | exclusive_or_expression CARRET and_expression
2153           {
2154                 $$ = new Binary (Binary.Operator.ExclusiveOr, 
2155                                  (Expression) $1, (Expression) $3, lexer.Location);
2156           }
2157         ;
2158
2159 inclusive_or_expression
2160         : exclusive_or_expression
2161         | inclusive_or_expression BITWISE_OR exclusive_or_expression
2162           {
2163                 $$ = new Binary (Binary.Operator.BitwiseOr, 
2164                                  (Expression) $1, (Expression) $3, lexer.Location);
2165           }
2166         ;
2167
2168 conditional_and_expression
2169         : inclusive_or_expression
2170         | conditional_and_expression OP_AND inclusive_or_expression
2171           {
2172                 $$ = new Binary (Binary.Operator.LogicalAnd, 
2173                                  (Expression) $1, (Expression) $3, lexer.Location);
2174           }
2175         ;
2176
2177 conditional_or_expression
2178         : conditional_and_expression
2179         | conditional_or_expression OP_OR conditional_and_expression
2180           {
2181                 $$ = new Binary (Binary.Operator.LogicalOr, 
2182                                  (Expression) $1, (Expression) $3, lexer.Location);
2183           }
2184         ;
2185
2186 conditional_expression
2187         : conditional_or_expression
2188         | conditional_or_expression INTERR expression COLON expression 
2189           {
2190                 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
2191           }
2192         ;
2193
2194 assignment_expression
2195         : unary_expression ASSIGN expression
2196           {
2197                 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
2198           }
2199         | unary_expression OP_MULT_ASSIGN expression
2200           {
2201                 Location l = lexer.Location;
2202
2203                 $$ = new Assign ((Expression) $1,
2204                                  new Binary (Binary.Operator.Multiply, 
2205                                              (Expression) $1,
2206                                              (Expression) $3, l), l);
2207           }
2208         | unary_expression OP_DIV_ASSIGN expression
2209           {
2210                 Location l = lexer.Location;
2211
2212                 $$ = new Assign ((Expression) $1,
2213                                  new Binary (Binary.Operator.Division, 
2214                                              (Expression) $1,
2215                                              (Expression) $3, l), l);
2216           }
2217         | unary_expression OP_MOD_ASSIGN expression
2218           {
2219                 Location l = lexer.Location;
2220
2221                 $$ = new Assign ((Expression) $1,
2222                                  new Binary (Binary.Operator.Modulus, 
2223                                              (Expression) $1,
2224                                              (Expression) $3, l), l);
2225           }
2226         | unary_expression OP_ADD_ASSIGN expression
2227           {
2228                 Location l = lexer.Location;
2229
2230                 $$ = new Assign ((Expression) $1,
2231                                  new Binary (Binary.Operator.Addition, 
2232                                              (Expression) $1,
2233                                              (Expression) $3, l), l);
2234           }
2235         | unary_expression OP_SUB_ASSIGN expression
2236           {
2237                 Location l = lexer.Location;
2238
2239                 $$ = new Assign ((Expression) $1,
2240                                  new Binary (Binary.Operator.Subtraction, 
2241                                              (Expression) $1,
2242                                              (Expression) $3, l), l);
2243           }
2244         | unary_expression OP_SHIFT_LEFT_ASSIGN expression
2245           {
2246                 Location l = lexer.Location;
2247
2248                 $$ = new Assign ((Expression) $1,
2249                                  new Binary (Binary.Operator.LeftShift, 
2250                                              (Expression) $1,
2251                                              (Expression) $3, l), l);
2252           }
2253         | unary_expression OP_SHIFT_RIGHT_ASSIGN expression
2254           {
2255                 Location l = lexer.Location;
2256
2257                 $$ = new Assign ((Expression) $1,
2258                                  new Binary (Binary.Operator.RightShift, 
2259                                              (Expression) $1,
2260                                              (Expression) $3, l), l);
2261           }
2262         | unary_expression OP_AND_ASSIGN expression
2263           {
2264                 Location l = lexer.Location;
2265
2266                 $$ = new Assign ((Expression) $1,
2267                                  new Binary (Binary.Operator.BitwiseAnd, 
2268                                              (Expression) $1,
2269                                              (Expression) $3, l), l);
2270           }
2271         | unary_expression OP_OR_ASSIGN expression
2272           {
2273                 Location l = lexer.Location;
2274
2275                 $$ = new Assign ((Expression) $1,
2276                                  new Binary (Binary.Operator.BitwiseOr, 
2277                                              (Expression) $1,
2278                                              (Expression) $3, l), l);
2279           }
2280         | unary_expression OP_XOR_ASSIGN expression
2281           {
2282                 Location l = lexer.Location;
2283
2284                 $$ = new Assign ((Expression) $1,
2285                                  new Binary (Binary.Operator.ExclusiveOr, 
2286                                              (Expression) $1,
2287                                              (Expression) $3, l), l);
2288           }
2289         ;
2290
2291 expression
2292         : conditional_expression
2293         | assignment_expression
2294         ;
2295
2296 constant_expression
2297         : expression
2298         ;
2299
2300 boolean_expression
2301         : expression    { CheckBoolean ((Expression) $1); $$ = $1; } 
2302         ;
2303
2304 //
2305 // 10 classes
2306 //
2307 class_declaration
2308         : opt_attributes
2309           opt_modifiers
2310           CLASS IDENTIFIER
2311           {
2312                 Class new_class;
2313                 string full_class_name = MakeName ((string) $4);
2314
2315                 new_class = new Class (rc, current_container, full_class_name, (int) $2, 
2316                                        (Attributes) $1, lexer.Location);
2317                 current_container = new_class;
2318                 current_container.Namespace = current_namespace;
2319                 tree.RecordClass (full_class_name, new_class);
2320           }
2321           opt_class_base
2322           class_body 
2323           opt_semicolon 
2324           {
2325                 Class new_class = (Class) current_container;
2326
2327                 if ($6 != null)
2328                         new_class.Bases = (ArrayList) $6;
2329
2330                 current_container = current_container.Parent;
2331                 CheckDef (current_container.AddClass (new_class), new_class.Name);
2332
2333                 $$ = new_class;
2334           }
2335         ;       
2336
2337 opt_modifiers
2338         : /* empty */           { $$ = (int) 0; }
2339         | modifiers
2340         ;
2341
2342 modifiers
2343         : modifier
2344         | modifiers modifier
2345           { 
2346                 int m1 = (int) $1;
2347                 int m2 = (int) $2;
2348
2349                 if ((m1 & m2) != 0) {
2350                         Location l = lexer.Location;
2351                         Report.Error (1002, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
2352                 }
2353                 $$ = (int) (m1 | m2);
2354           }
2355         ;
2356
2357 modifier
2358         : NEW                   { $$ = Modifiers.NEW; }
2359         | PUBLIC                { $$ = Modifiers.PUBLIC; }
2360         | PROTECTED             { $$ = Modifiers.PROTECTED; }
2361         | INTERNAL              { $$ = Modifiers.INTERNAL; }
2362         | PRIVATE               { $$ = Modifiers.PRIVATE; }
2363         | ABSTRACT              { $$ = Modifiers.ABSTRACT; }
2364         | SEALED                { $$ = Modifiers.SEALED; }
2365         | STATIC                { $$ = Modifiers.STATIC; }
2366         | READONLY              { $$ = Modifiers.READONLY; }
2367         | VIRTUAL               { $$ = Modifiers.VIRTUAL; }
2368         | OVERRIDE              { $$ = Modifiers.OVERRIDE; }
2369         | EXTERN                { $$ = Modifiers.EXTERN; }
2370         ;
2371
2372 opt_class_base
2373         : /* empty */           { $$ = null; }
2374         | class_base            { $$ = $1;   }
2375         ;
2376
2377 class_base
2378         : COLON type_list { $$ = $2; }
2379         ;
2380
2381 //
2382 // Statements (8.2)
2383 //
2384
2385 //
2386 // A block is "contained" on the following places:
2387 //      method_body
2388 //      property_declaration as part of the accessor body (get/set)
2389 //      operator_declaration
2390 //      constructor_declaration
2391 //      destructor_declaration
2392 //      event_declaration as part of add_accessor_declaration or remove_accessor_declaration
2393 //      
2394 block
2395         : OPEN_BRACE 
2396           {
2397                 current_block = new Block (current_block);
2398           } 
2399           opt_statement_list CLOSE_BRACE 
2400           { 
2401                 while (current_block.Implicit)
2402                         current_block = current_block.Parent;
2403                 $$ = current_block;
2404                 current_block = current_block.Parent;
2405           }
2406         ;
2407
2408 opt_statement_list
2409         : /* empty */
2410         | statement_list 
2411         ;
2412
2413 statement_list
2414         : statement
2415         | statement_list statement
2416         ;
2417
2418 statement
2419         : declaration_statement
2420           {
2421                 if ((Block) $1 != current_block){
2422                         current_block.AddStatement ((Statement) $1);
2423                         current_block = (Block) $1;
2424                 }
2425           }
2426         | embedded_statement
2427           {
2428                 current_block.AddStatement ((Statement) $1);
2429           }
2430         | labeled_statement 
2431           {
2432                 current_block.AddStatement ((Statement) $1);
2433           }
2434         ;
2435
2436 embedded_statement
2437         : block
2438         | empty_statement
2439         | expression_statement
2440         | selection_statement
2441         | iteration_statement
2442         | jump_statement                  
2443         | try_statement
2444         | checked_statement
2445         | unchecked_statement
2446         | lock_statement
2447         | using_statement
2448         ;
2449
2450 empty_statement
2451         : SEMICOLON
2452           {
2453                   $$ = new EmptyStatement ();
2454           }
2455         ;
2456
2457 labeled_statement
2458         : IDENTIFIER COLON statement
2459           {
2460                 string lab = (String) $1;
2461                 Block block;
2462
2463                 block = new Block (current_block, lab);
2464                 block.AddStatement ((Statement) $3);
2465                 $$ = block;
2466
2467                 if (!current_block.AddLabel (lab, block)){
2468                         Location l = lexer.Location;
2469                         Report.Error (140, l, "The label '" + lab + "' is a duplicate");
2470                         $$ = $3;
2471                 }       
2472           }
2473         ;
2474
2475 declaration_statement
2476         : local_variable_declaration SEMICOLON          // done
2477         | local_constant_declaration SEMICOLON          // finishme
2478         ;
2479
2480 /* 
2481  * The following is from Rhys' grammar:
2482  * > Types in local variable declarations must be recognized as 
2483  * > expressions to prevent reduce/reduce errors in the grammar.
2484  * > The expressions are converted into types during semantic analysis.
2485  */
2486 local_variable_type
2487         : primary_expression opt_rank_specifier
2488           { 
2489                 // FIXME: Do something smart here regarding the composition of the type.
2490
2491                 // Ok, the above "primary_expression" is there to get rid of
2492                 // both reduce/reduce and shift/reduces in the grammar, it should
2493                 // really just be "type_name".  If you use type_name, a reduce/reduce
2494                 // creeps up.  If you use qualified_identifier (which is all we need
2495                 // really) two shift/reduces appear.
2496                 // 
2497
2498                 // So the super-trick is that primary_expression
2499                 // can only be either a SimpleName or a MemberAccess. 
2500                 // The MemberAccess case arises when you have a fully qualified type-name like :
2501                 // Foo.Bar.Blah i;
2502                 // SimpleName is when you have
2503                 // Blah i;
2504                   
2505                 Expression expr = (Expression) $1;  
2506                 if (!(expr is SimpleName || expr is MemberAccess)) {
2507                         Location l = lexer.Location;
2508                         Report.Error (-1, l, "Invalid Type definition");
2509                         $$ = "System.Object";
2510                 }
2511                 
2512                 //
2513                 // So we extract the string corresponding to the SimpleName
2514                 // or MemberAccess
2515                 // 
2516                 $$ = GetQualifiedIdentifier (expr) + (string) $2;
2517           }
2518         | builtin_types opt_rank_specifier
2519           {
2520                   $$ = (string) $1 + (string) $2;
2521           }
2522         ;
2523
2524 // FIXME : How can the type of a local variable be void ? I don't quite see ;-)
2525 //          | VOID 
2526 //        {
2527 //              // FIXME: this is a string that represents the type
2528 //              // Figure out something to make this work.
2529 //              $$ = "void";
2530 //        }
2531 //      ;
2532
2533 local_variable_declaration
2534         : local_variable_type variable_declarators
2535           {
2536                 $$ = declare_local_variables ((string) $1, (ArrayList) $2);
2537           }
2538         ;
2539
2540 local_constant_declaration
2541         : CONST type constant_declarator
2542                 // finishme     
2543         ;
2544
2545 expression_statement
2546         : statement_expression SEMICOLON
2547           {
2548                 $$ = $1;
2549           }
2550         ;
2551
2552         //
2553         // We have to do the wrapping here and not in the case above,
2554         // because statement_expression is used for example in for_statement
2555         //
2556 statement_expression
2557         : invocation_expression         { $$ = new StatementExpression ((ExpressionStatement) $1); }
2558         | object_creation_expression    { $$ = new StatementExpression ((ExpressionStatement) $1); }
2559         | assignment_expression         { $$ = new StatementExpression ((ExpressionStatement) $1); }
2560         | post_increment_expression     { $$ = new StatementExpression ((ExpressionStatement) $1); }
2561         | post_decrement_expression     { $$ = new StatementExpression ((ExpressionStatement) $1); }
2562         | pre_increment_expression      { $$ = new StatementExpression ((ExpressionStatement) $1); }
2563         | pre_decrement_expression      { $$ = new StatementExpression ((ExpressionStatement) $1); }
2564         ;
2565
2566 object_creation_expression
2567         : object_or_delegate_creation_expression
2568           { note ("complain if this is a delegate maybe?"); } 
2569         ;
2570
2571 selection_statement
2572         : if_statement
2573         | switch_statement
2574         ; 
2575
2576 if_statement
2577         : IF OPEN_PARENS boolean_expression CLOSE_PARENS 
2578           embedded_statement
2579           { 
2580                 $$ = new If ((Expression) $3, (Statement) $5);
2581           }
2582         | IF OPEN_PARENS boolean_expression CLOSE_PARENS
2583           embedded_statement ELSE embedded_statement
2584           {
2585                 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7);
2586           }
2587         ;
2588
2589 switch_statement
2590         : SWITCH OPEN_PARENS expression CLOSE_PARENS 
2591           switch_block
2592           {
2593                 $$ = new Switch ((Expression) $3, (ArrayList) $5);
2594           }
2595         ;
2596
2597 switch_block
2598         : OPEN_BRACE
2599           opt_switch_sections
2600           CLOSE_BRACE
2601           {
2602                 $$ = $2;
2603           }
2604         ;
2605
2606 opt_switch_sections
2607         : /* empty */           { $$ = new ArrayList (); }
2608         | switch_sections
2609         ;
2610
2611 switch_sections
2612         : switch_section 
2613           {
2614                 ArrayList sections = new ArrayList ();
2615
2616                 sections.Add ($1);
2617                 $$ = sections;
2618           }
2619         | switch_sections switch_section
2620           {
2621                 ArrayList sections = (ArrayList) $1;
2622
2623                 sections.Add ($2);
2624                 $$ = sections;
2625           }
2626         ;
2627
2628 switch_section
2629         : switch_labels
2630           {
2631                 current_block = new Block (current_block);
2632           }
2633           statement_list 
2634           {
2635                 while (current_block.Implicit)
2636                         current_block = current_block.Parent;
2637                 $$ = new SwitchSection ((ArrayList) $1, current_block);
2638                 current_block = current_block.Parent;
2639           }
2640         ;
2641
2642 switch_labels
2643         : switch_label 
2644           {
2645                 ArrayList labels = new ArrayList ();
2646
2647                 labels.Add ($1);
2648                 $$ = labels;
2649           }
2650         | switch_labels switch_label 
2651           {
2652                 ArrayList labels = (ArrayList) ($1);
2653                 labels.Add ($2);
2654
2655                 $$ = labels;
2656           }
2657         ;
2658
2659 switch_label
2660         : CASE constant_expression COLON        { $$ = new SwitchLabel ((Expression) $2); }
2661         | DEFAULT COLON                         { $$ = new SwitchLabel (null); }
2662         ;
2663
2664 iteration_statement
2665         : while_statement
2666         | do_statement
2667         | for_statement
2668         | foreach_statement
2669         ;
2670
2671 while_statement
2672         : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
2673         {
2674                 $$ = new While ((Expression) $3, (Statement) $5);
2675         }
2676         ;
2677
2678 do_statement
2679         : DO embedded_statement 
2680           WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
2681           {
2682                 $$ = new Do ((Statement) $2, (Expression) $5);
2683           }
2684         ;
2685
2686 for_statement
2687         : FOR OPEN_PARENS 
2688           opt_for_initializer SEMICOLON
2689           opt_for_condition SEMICOLON
2690           opt_for_iterator CLOSE_PARENS 
2691           embedded_statement
2692           {
2693                 $$ = new For ((Statement) $3, (Expression) $5, (Statement) $7, (Statement) $9);
2694           }
2695         ;
2696
2697 opt_for_initializer
2698         : /* empty */           { $$ = new EmptyStatement (); }
2699         | for_initializer       
2700         ;
2701
2702 for_initializer
2703         : local_variable_declaration 
2704           {
2705                 if ((Block) $1 != current_block){
2706                         current_block.AddStatement ((Statement) $1);
2707                         current_block = (Block) $1;
2708                 }
2709           }
2710         | statement_expression_list
2711         ;
2712
2713 opt_for_condition
2714         : /* empty */           { $$ = new BoolLiteral (true); }
2715         | boolean_expression
2716         ;
2717
2718 opt_for_iterator
2719         : /* empty */           { $$ = new EmptyStatement (); }
2720         | for_iterator
2721         ;
2722
2723 for_iterator
2724         : statement_expression_list
2725         ;
2726
2727 statement_expression_list
2728         : statement_expression  
2729           {
2730                 Block b = new Block (null, true);
2731
2732                 b.AddStatement ((Statement) $1);
2733                 $$ = b;
2734           }
2735         | statement_expression_list COMMA statement_expression
2736           {
2737                 Block b = (Block) $1;
2738
2739                 b.AddStatement ((Statement) $3);
2740                 $$ = $1;
2741           }
2742         ;
2743
2744 foreach_statement
2745         : FOREACH OPEN_PARENS type IDENTIFIER IN 
2746           { 
2747                 $1 = lexer.Location;
2748           } 
2749           expression CLOSE_PARENS 
2750           embedded_statement
2751           {
2752                 Block foreach_block = new Block (current_block, true);
2753                 LocalVariableReference v;
2754
2755                 foreach_block.AddVariable ((string) $3, (string) $4, (Location) $1);
2756                 v = new LocalVariableReference (foreach_block, (string) $4);
2757                 foreach_block.AddStatement (new Foreach ((string) $3, v, (Expression) $7, 
2758                                             (Statement) $9, (Location) $1));
2759                 $$ = foreach_block;
2760           }
2761         ;
2762
2763 jump_statement
2764         : break_statement
2765         | continue_statement
2766         | goto_statement
2767         | return_statement
2768         | throw_statement
2769         ;
2770
2771 break_statement
2772         : BREAK SEMICOLON
2773           {
2774                 $$ = new Break (lexer.Location);
2775           }
2776         ;
2777
2778 continue_statement
2779         : CONTINUE SEMICOLON
2780           {
2781                 $$ = new Continue (lexer.Location);
2782           }
2783         ;
2784
2785 goto_statement
2786         : GOTO IDENTIFIER SEMICOLON 
2787           {
2788                 $$ = new Goto ((string) $2, lexer.Location);
2789           }
2790         | GOTO CASE constant_expression SEMICOLON
2791           {
2792                 // FIXME
2793           }
2794         | GOTO DEFAULT SEMICOLON 
2795           {
2796                 // FIXME
2797           }
2798         ; 
2799
2800 return_statement
2801         : RETURN opt_expression SEMICOLON
2802           {
2803                 $$ = new Return ((Expression) $2, lexer.Location);
2804           }
2805         ;
2806
2807 throw_statement
2808         : THROW opt_expression SEMICOLON
2809           {
2810                 $$ = new Throw ((Expression) $2);
2811           }
2812         ;
2813
2814 opt_expression
2815         : /* empty */
2816         | expression
2817         ;
2818
2819 try_statement
2820         : TRY block catch_clauses 
2821         {
2822                 Catch g = null;
2823                 ArrayList s = new ArrayList ();
2824                 
2825                 foreach (Catch cc in (ArrayList) $3) {
2826                         if (cc.Type == null)
2827                                 g = cc;
2828                         else
2829                                 s.Add (cc);
2830                 }
2831
2832                 // Now s contains the list of specific catch clauses
2833                 // and g contains the general one.
2834                 
2835                 $$ = new Try ((Block) $2, s, g, null);
2836         }
2837         | TRY block opt_catch_clauses FINALLY block
2838           {
2839                 Catch g = null;
2840                 ArrayList s = new ArrayList ();
2841                 
2842                 foreach (Catch cc in (ArrayList) $3) {
2843                         if (cc.Type == null)
2844                                 g = cc;
2845                         else
2846                                 s.Add (cc);
2847                 }
2848
2849                 $$ = new Try ((Block) $2, s, g, (Block) $5);
2850           }
2851         ;
2852
2853 opt_catch_clauses
2854         : /* empty */  { $$ = null; }
2855         | catch_clauses
2856         ;
2857
2858 catch_clauses
2859         : catch_clause 
2860           {
2861                 ArrayList l = new ArrayList ();
2862
2863                 l.Add ($1);
2864                 $$ = l;
2865           }
2866         | catch_clauses catch_clause
2867           {
2868                 ArrayList l = (ArrayList) $1;
2869
2870                 l.Add ($2);
2871                 $$ = l;
2872           }
2873         ;
2874
2875 opt_identifier
2876         : /* empty */   { $$ = null; }
2877         | IDENTIFIER
2878         ;
2879
2880 catch_clause 
2881         : CATCH opt_catch_args 
2882         {
2883                 string type = null, id = null;
2884                 
2885                 if ($2 != null) {
2886                         DictionaryEntry cc = (DictionaryEntry) $2;
2887                         type = (string) cc.Key;
2888                         id   = (string) cc.Value;
2889
2890                         if (id != null){
2891                                 ArrayList one = new ArrayList ();
2892                                 one.Add (new VariableDeclaration (id, null, lexer.Location));
2893
2894                                 $1 = current_block;
2895                                 current_block = new Block (current_block);
2896                                 Block b = declare_local_variables (type, one);
2897                                 current_block = b;
2898
2899                                 
2900                         }
2901                 }
2902         } block {
2903                 string type = null, id = null;
2904
2905                 if ($2 != null){
2906                         DictionaryEntry cc = (DictionaryEntry) $2;
2907                         type = (string) cc.Key;
2908                         id   = (string) cc.Value;
2909
2910                         if ($1 != null){
2911                                 while (current_block != (Block) $1)
2912                                         current_block = current_block.Parent;
2913                         }
2914                 }
2915
2916
2917                 $$ = new Catch (type, id , (Block) $4);
2918         }
2919         ;
2920
2921 opt_catch_args
2922         : /* empty */ { $$ = null; }
2923         | catch_args
2924         ;         
2925
2926 catch_args 
2927         : OPEN_PARENS type opt_identifier CLOSE_PARENS 
2928         {
2929                 $$ = new DictionaryEntry ($2, $3);
2930         }
2931         ;
2932
2933 checked_statement
2934         : CHECKED block
2935           {
2936                 $$ = new Checked ((Block) $2);
2937           }
2938         ;
2939
2940 unchecked_statement
2941         : UNCHECKED block
2942           {
2943                 $$ = new Unchecked ((Block) $2);
2944           }
2945         ;
2946
2947 lock_statement
2948         : LOCK OPEN_PARENS expression CLOSE_PARENS 
2949           {
2950                 $1 = lexer.Location;
2951           } 
2952           embedded_statement
2953           {
2954                 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
2955           }
2956         ;
2957
2958 using_statement
2959         : USING OPEN_PARENS resource_acquisition CLOSE_PARENS 
2960           {
2961                 $1 = lexer.Location;
2962           } 
2963           embedded_statement
2964           {
2965                 $$ = new Using ($3, (Statement) $6, (Location) $1);
2966           }
2967         ; 
2968
2969 resource_acquisition
2970         : local_variable_declaration
2971           {
2972                 if ((Block) $1 != current_block){
2973                         current_block.AddStatement ((Statement) $1);
2974                         current_block = (Block) $1;
2975                 }
2976           }
2977         | expression
2978         ;
2979
2980 %%
2981
2982 // <summary>
2983 //   A class used to pass around variable declarations and constants
2984 // </summary>
2985 public class VariableDeclaration {
2986         public string identifier;
2987         public object expression_or_array_initializer;
2988         public Location Location;
2989
2990         public VariableDeclaration (string id, object eoai, Location l){
2991                 this.identifier = id;
2992                 this.expression_or_array_initializer = eoai;
2993                 this.Location = l;
2994         }
2995 }
2996
2997 // <summary>
2998 //   A class used to hold info about an indexer declarator
2999 // </summary>
3000
3001 public class IndexerDeclaration {
3002         public string type;
3003         public string interface_type;
3004         public Parameters param_list;
3005
3006         public IndexerDeclaration (string type, string interface_type, Parameters param_list)
3007         {
3008                 this.type = type;
3009                 this.interface_type = interface_type;
3010                 this.param_list = param_list;
3011         }
3012 }
3013
3014 // <summary>
3015 //  A class used to hold info about an operator declarator
3016 // </summary>
3017
3018 public class OperatorDeclaration {
3019         public Operator.OpType optype;
3020         public string ret_type;
3021         public string arg1type;
3022         public string arg1name;
3023         public string arg2type;
3024         public string arg2name;
3025         public Location location;
3026
3027         public OperatorDeclaration (Operator.OpType op, string ret_type, string arg1type, string arg1name,
3028                                     string arg2type, string arg2name, Location location)
3029         {
3030                 optype = op;
3031                 this.ret_type = ret_type;
3032                 this.arg1type = arg1type;
3033                 this.arg1name = arg1name;
3034                 this.arg2type = arg2type;
3035                 this.arg2name = arg2name;
3036                 this.location = location;
3037         }
3038
3039 }
3040
3041 // <summary>
3042 //   Given the @class_name name, it creates a fully qualified name
3043 //   based on the containing declaration space
3044 // </summary>
3045 string 
3046 MakeName (string class_name)
3047 {
3048         string ns = current_namespace.Name;
3049         string container_name = current_container.Name;
3050
3051         if (container_name == ""){
3052                 if (ns != "")
3053                         return ns + "." + class_name;
3054                 else
3055                         return class_name;
3056         } else
3057                 return container_name + "." + class_name;
3058 }
3059
3060 // <summary>
3061 //   Used to report back to the user the result of a declaration
3062 //   in the current declaration space
3063 // </summary>
3064 void 
3065 CheckDef (DeclSpace.AdditionResult result, string name)
3066 {
3067         if (result == DeclSpace.AdditionResult.Success)
3068                 return;
3069
3070         Location l = lexer.Location;
3071         
3072         switch (result){
3073         case DeclSpace.AdditionResult.NameExists:
3074                 Report.Error (102, l, "The namespace `" + current_container.Name + 
3075                                  "' already contains a definition for `"+
3076                                  name + "'");
3077                 break;
3078
3079
3080 //      NEED TO HANDLE THIS IN SEMANTIC ANALYSIS:
3081 //
3082 //      case DeclSpace.AdditionResult.MethodDuplicated:
3083 //              error (111, "Class `"+current_container.Name+
3084 //                          "' already defines a member called '" + 
3085 //                          name + "' with the same parameter types");
3086 //              break;
3087
3088         case DeclSpace.AdditionResult.EnclosingClash:
3089                 Report.Error (542, l, "Member names cannot be the same as their enclosing type");
3090                 break;
3091                 
3092         case DeclSpace.AdditionResult.NotAConstructor:
3093                 Report.Error (1520, l, "Class, struct, or interface method must have a return type");
3094                 break;
3095         }
3096 }
3097
3098 void 
3099 CheckDef (bool result, string name)
3100 {
3101         if (result)
3102                 return;
3103         CheckDef (DeclSpace.AdditionResult.NameExists, name);
3104 }
3105
3106 Expression
3107 SimpleLookup (string name)
3108 {
3109         //
3110         // we need to check against current_block not being null
3111         // as `expression' is allowed in argument_lists, which 
3112         // do not exist inside a block.  
3113         //
3114         if (current_block != null){
3115                 if (current_block.IsVariableDefined (name))
3116                         return new LocalVariableReference (current_block, name);
3117         }
3118
3119         if (current_local_parameters != null){
3120                 int idx;
3121                 Parameter par = current_local_parameters.GetParameterByName (name, out idx);
3122                 if (par != null)
3123                         return new ParameterReference (current_local_parameters, idx, name);
3124         }
3125
3126         return null;
3127 }
3128
3129 Expression DecomposeQI (string name, Location l)
3130 {
3131         Expression o;
3132
3133         if (name.IndexOf ('.') == -1){
3134                 o = SimpleLookup (name);
3135                 if (o == null)
3136                         return new SimpleName (name, l);
3137                 return o;
3138         } else {
3139                 int pos = name.LastIndexOf (".");
3140                 string left = name.Substring (0, pos);
3141                 string right = name.Substring (pos + 1);
3142
3143                 o = DecomposeQI (left, l);
3144                 return new MemberAccess (o, right, l);
3145         }
3146 }
3147
3148 // <summary>
3149 //  This method is used to get at the complete string representation of
3150 //  a fully-qualified type name, hiding inside a MemberAccess ;-)
3151 //  This is necessary because local_variable_type admits primary_expression
3152 //  as the type of the variable. So we do some extra checking
3153 // </summary>
3154 string GetQualifiedIdentifier (Expression expr)
3155 {
3156         if (expr is SimpleName)
3157                 return ((SimpleName)expr).Name;
3158         else if (expr is MemberAccess)
3159                 return GetQualifiedIdentifier (((MemberAccess)expr).Expr) + "." + ((MemberAccess) expr).Identifier;
3160         else 
3161                 throw new Exception ("Expr has to be either SimpleName or MemberAccess !");
3162         
3163 }
3164
3165 Block declare_local_variables (string type, ArrayList variable_declarators)
3166 {
3167         Block implicit_block;
3168         ArrayList inits = null;
3169
3170         //
3171         // We use the `Used' property to check whether statements
3172         // have been added to the current block.  If so, we need
3173         // to create another block to contain the new declaration
3174         // otherwise, as an optimization, we use the same block to
3175         // add the declaration.
3176         //
3177         // FIXME: A further optimization is to check if the statements
3178         // that were added were added as part of the initialization
3179         // below.  In which case, no other statements have been executed
3180         // and we might be able to reduce the number of blocks for
3181         // situations like this:
3182         //
3183         // int j = 1;  int k = j + 1;
3184         //
3185         if (current_block.Used)
3186                 implicit_block = new Block (current_block, true);
3187         else
3188                 implicit_block = current_block;
3189
3190         foreach (VariableDeclaration decl in variable_declarators){
3191                 if (implicit_block.AddVariable (type, decl.identifier, decl.Location)){
3192                         if (decl.expression_or_array_initializer != null){
3193                                 if (inits == null)
3194                                         inits = new ArrayList ();
3195                                 inits.Add (decl);
3196                         }
3197                 } else {
3198                         Location l = lexer.Location;
3199                         Report.Error (128, l, "A local variable `" + decl.identifier +
3200                                          "' is already defined in this scope");
3201                 }
3202         }
3203
3204         if (inits == null)
3205                 return implicit_block;
3206
3207         foreach (VariableDeclaration decl in inits){
3208                 if (decl.expression_or_array_initializer is Expression){
3209                         Expression expr = (Expression) decl.expression_or_array_initializer;
3210                         Assign assign;
3211                         
3212                         assign = new Assign (new LocalVariableReference (
3213                                              implicit_block, decl.identifier), 
3214                                              expr, lexer.Location);
3215                         implicit_block.AddStatement (new StatementExpression (assign));
3216                 } else {
3217                         Console.WriteLine ("Not handling Array initializers yet");
3218                 }
3219         }
3220                         
3221         return implicit_block;
3222 }
3223
3224 void CheckConstant (Expression expr)
3225 {
3226         // finishme
3227 }
3228
3229 void CheckBoolean (Expression expr)
3230 {
3231         // finishme
3232 }
3233
3234 void CheckAttributeTarget (string a)
3235 {
3236         switch (a) {
3237
3238         case "assembly" : case "field" : case "method" : case "param" : case "property" : case "type" :
3239                 return;
3240                 
3241         default :
3242                 Location l = lexer.Location;
3243                 Report.Error (658, l, "Invalid attribute target");
3244                 break;
3245         }
3246
3247 }
3248
3249 void CheckUnaryOperator (Operator.OpType op)
3250 {
3251         switch (op) {
3252                 
3253         case Operator.OpType.LogicalNot: 
3254         case Operator.OpType.OnesComplement: 
3255         case Operator.OpType.Increment:
3256         case Operator.OpType.Decrement:
3257         case Operator.OpType.True: 
3258         case Operator.OpType.False: 
3259         case Operator.OpType.Addition: 
3260         case Operator.OpType.Subtraction:
3261                 
3262                 break;
3263                 
3264         default :
3265                 Location l = lexer.Location;
3266                 Report.Error (1019, l, "Overloadable unary operator expected"); 
3267                 break;
3268                 
3269         }
3270 }
3271
3272 void CheckBinaryOperator (Operator.OpType op)
3273 {
3274         switch (op) {
3275                 
3276         case Operator.OpType.Addition: 
3277         case Operator.OpType.Subtraction: 
3278         case Operator.OpType.Multiply:
3279         case Operator.OpType.Division:
3280         case Operator.OpType.Modulus: 
3281         case Operator.OpType.BitwiseAnd: 
3282         case Operator.OpType.BitwiseOr:
3283         case Operator.OpType.ExclusiveOr: 
3284         case Operator.OpType.LeftShift: 
3285         case Operator.OpType.RightShift:
3286         case Operator.OpType.Equality: 
3287         case Operator.OpType.Inequality:
3288         case Operator.OpType.GreaterThan: 
3289         case Operator.OpType.LessThan: 
3290         case Operator.OpType.GreaterThanOrEqual:
3291         case Operator.OpType.LessThanOrEqual:
3292                 break;
3293                 
3294         default :
3295                 Location l = lexer.Location;
3296                 Report.Error (1020, l, "Overloadable binary operator expected");
3297                 break;
3298         }
3299         
3300 }
3301
3302 void output (string s)
3303 {
3304         Console.WriteLine (s);
3305 }
3306
3307 void note (string s)
3308 {
3309         // Used to put annotations
3310 }
3311
3312 Tokenizer lexer;
3313
3314 public Tokenizer Lexer {
3315         get {
3316                 return lexer;
3317         }
3318 }                  
3319
3320 public CSharpParser(RootContext rc, string name, System.IO.Stream input)
3321 {
3322         current_namespace = new Namespace (null, "");
3323         this.rc = rc;
3324         this.tree = rc.Tree;
3325         this.name = name;
3326         this.input = input;
3327         current_container = tree.Types;
3328         current_container.Namespace = current_namespace;
3329
3330         lexer = new Tokenizer (input, name);
3331 }
3332
3333 public override int parse ()
3334 {
3335         StringBuilder value = new StringBuilder ();
3336
3337         global_errors = 0;
3338         try {
3339                 if (yacc_verbose_flag)
3340                         yyparse (lexer, new yydebug.yyDebugSimple ());
3341                 else
3342                         yyparse (lexer);
3343         } catch (Exception e){
3344                 // Console.WriteLine ("Fatal error: " + name);
3345                 // Console.WriteLine (lexer.location);
3346
3347                 Console.WriteLine (lexer.location + "  : Parsing error ");
3348                 Console.WriteLine (e);
3349                 global_errors++;
3350         }
3351         
3352         return global_errors;
3353 }
3354
3355
3356 /* end end end */
3357 }