Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Parser/mcs/cs-parser.jay @ 11937

Last change on this file since 11937 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 209.5 KB
Line 
1%{
2//
3// cs-parser.jay: The Parser for the C# compiler
4//
5// Authors: Miguel de Icaza (miguel@gnome.org)
6//          Ravi Pratap     (ravi@ximian.com)
7//          Marek Safar     (marek.safar@gmail.com)
8//
9// Dual Licensed under the terms of the GNU GPL and the MIT X11 license
10//
11// (C) 2001 Ximian, Inc (http://www.ximian.com)
12// (C) 2004-2011 Novell, Inc
13// Copyright 2011-2012 Xamarin Inc.
14//
15
16using System.Text;
17using System.IO;
18using System;
19using System.Collections.Generic;
20
21namespace Mono.CSharp
22{
23  /// <summary>
24  ///    The C# Parser
25  /// </summary>
26  public class CSharpParser
27  {
28    [Flags]
29    enum ParameterModifierType
30    {
31      Ref   = 1 << 1,
32      Out   = 1 << 2,
33      This  = 1 << 3,
34      Params  = 1 << 4,
35      Arglist = 1 << 5,
36      DefaultValue = 1 << 6,
37     
38      All = Ref | Out | This | Params | Arglist | DefaultValue,
39      PrimaryConstructor = Ref | Out | Params | DefaultValue
40    }
41   
42    static readonly object ModifierNone = 0;
43 
44    NamespaceContainer current_namespace;
45    TypeContainer current_container;
46    TypeDefinition current_type;
47    PropertyBase current_property;
48    EventProperty current_event;
49    EventField current_event_field;
50    FieldBase current_field;
51 
52    /// <summary>
53    ///   Current block is used to add statements as we find
54    ///   them. 
55    /// </summary>
56    Block      current_block;
57   
58    BlockVariable current_variable;
59
60    Delegate   current_delegate;
61   
62    AnonymousMethodExpression current_anonymous_method;
63
64    /// <summary>
65    ///   This is used by the unary_expression code to resolve
66    ///   a name against a parameter. 
67    /// </summary>
68   
69    // FIXME: This is very ugly and it's very hard to reset it correctly
70    // on all places, especially when some parameters are autogenerated.
71    ParametersCompiled current_local_parameters;
72
73    bool parsing_anonymous_method;
74   
75    bool async_block;
76
77    ///
78    /// An out-of-band stack.
79    ///
80    Stack<object> oob_stack;
81
82    ///
83    /// Controls the verbosity of the errors produced by the parser
84    ///
85    int yacc_verbose_flag;
86
87    ///
88    /// Used by the interactive shell, flags whether EOF was reached
89    /// and an error was produced
90    ///
91    public bool UnexpectedEOF;
92
93    ///
94    /// The current file.
95    ///
96    readonly CompilationSourceFile file;
97
98    ///
99    /// Temporary Xml documentation cache.
100    /// For enum types, we need one more temporary store.
101    ///
102    string tmpComment;
103    string enumTypeComment;
104           
105    /// Current attribute target
106    string current_attr_target;
107   
108    ParameterModifierType valid_param_mod;
109   
110    bool default_parameter_used;
111
112    /// When using the interactive parser, this holds the
113    /// resulting expression
114    public Class InteractiveResult;
115
116    //
117    // Keeps track of global data changes to undo on parser error
118    //
119    public Undo undo;
120
121    bool? interactive_async;
122   
123    Stack<Linq.QueryBlock> linq_clause_blocks;
124
125    ModuleContainer module;
126   
127    readonly CompilerContext compiler;
128    readonly LanguageVersion lang_version;
129    readonly bool doc_support;
130    readonly CompilerSettings settings;
131    readonly Report report;
132   
133    //
134    // Instead of allocating carrier array everytime we
135    // share the bucket for very common constructs which can never
136    // be recursive
137    //
138    List<Parameter> parameters_bucket;
139   
140    //
141    // Full AST support members
142    //
143    LocationsBag lbag;
144    List<Tuple<Modifiers, Location>> mod_locations;
145    Location parameterModifierLocation, savedLocation, savedEventAssignLocation;
146    Location savedAttrParenOpenLocation, savedAttrParenCloseLocation, savedOperatorLocation;
147    Stack<List<Location>> locationListStack = new Stack<List<Location>> (); // used for type parameters
148    Stack<Location> opt_intoStack = new Stack<Location> ();
149
150    bool HadAttributeParens;
151    List<Location> attributeArgumentCommas = new List<Location> ();
152    List<Location> parameterListCommas = new List<Location> ();
153    Stack<Location> location_stack;
154%}
155
156%token EOF
157%token NONE   /* This token is never returned by our lexer */
158%token ERROR    // This is used not by the parser, but by the tokenizer.
159      // do not remove.
160
161/*
162 *These are the C# keywords
163 */
164%token FIRST_KEYWORD
165%token ABSTRACT
166%token AS
167%token ADD
168%token BASE
169%token BOOL
170%token BREAK 
171%token BYTE
172%token CASE
173%token CATCH 
174%token CHAR
175%token CHECKED 
176%token CLASS 
177%token CONST 
178%token CONTINUE
179%token DECIMAL 
180%token DEFAULT 
181%token DELEGATE
182%token DO
183%token DOUBLE
184%token ELSE
185%token ENUM
186%token EVENT 
187%token EXPLICIT
188%token EXTERN
189%token FALSE 
190%token FINALLY 
191%token FIXED 
192%token FLOAT 
193%token FOR 
194%token FOREACH 
195%token GOTO
196%token IF
197%token IMPLICIT
198%token IN
199%token INT 
200%token INTERFACE
201%token INTERNAL
202%token IS
203%token LOCK
204%token LONG
205%token NAMESPACE
206%token NEW 
207%token NULL
208%token OBJECT
209%token OPERATOR
210%token OUT 
211%token OVERRIDE
212%token PARAMS
213%token PRIVATE 
214%token PROTECTED
215%token PUBLIC
216%token READONLY
217%token REF 
218%token RETURN
219%token REMOVE
220%token SBYTE 
221%token SEALED
222%token SHORT 
223%token SIZEOF
224%token STACKALLOC
225%token STATIC
226%token STRING
227%token STRUCT
228%token SWITCH
229%token THIS
230%token THROW 
231%token TRUE
232%token TRY 
233%token TYPEOF
234%token UINT
235%token ULONG 
236%token UNCHECKED
237%token UNSAFE
238%token USHORT
239%token USING 
240%token VIRTUAL 
241%token VOID
242%token VOLATILE
243%token WHERE
244%token WHILE 
245%token ARGLIST
246%token PARTIAL
247%token ARROW
248%token FROM
249%token FROM_FIRST
250%token JOIN
251%token ON
252%token EQUALS
253%token SELECT
254%token GROUP
255%token BY
256%token LET
257%token ORDERBY
258%token ASCENDING
259%token DESCENDING
260%token INTO
261%token INTERR_NULLABLE
262%token EXTERN_ALIAS
263%token REFVALUE
264%token REFTYPE
265%token MAKEREF
266%token ASYNC
267%token AWAIT
268%token INTERR_OPERATOR
269
270/* C# keywords which are not really keywords */
271%token GET
272%token SET
273
274%left LAST_KEYWORD
275
276/* C# single character operators/punctuation. */
277%token OPEN_BRACE
278%token CLOSE_BRACE
279%token OPEN_BRACKET
280%token CLOSE_BRACKET
281%token OPEN_PARENS
282%token CLOSE_PARENS
283
284%token DOT
285%token COMMA
286%token COLON
287%token SEMICOLON
288%token TILDE
289
290%token PLUS
291%token MINUS
292%token BANG
293%token ASSIGN
294%token OP_LT
295%token OP_GT
296%token BITWISE_AND
297%token BITWISE_OR
298%token STAR
299%token PERCENT
300%token DIV
301%token CARRET
302%token INTERR
303
304/* C# multi-character operators. */
305%token DOUBLE_COLON
306%token OP_INC
307%token OP_DEC
308%token OP_SHIFT_LEFT
309%token OP_SHIFT_RIGHT
310%token OP_LE
311%token OP_GE
312%token OP_EQ
313%token OP_NE
314%token OP_AND
315%token OP_OR
316%token OP_MULT_ASSIGN
317%token OP_DIV_ASSIGN
318%token OP_MOD_ASSIGN
319%token OP_ADD_ASSIGN
320%token OP_SUB_ASSIGN
321%token OP_SHIFT_LEFT_ASSIGN
322%token OP_SHIFT_RIGHT_ASSIGN
323%token OP_AND_ASSIGN
324%token OP_XOR_ASSIGN
325%token OP_OR_ASSIGN
326%token OP_PTR
327%token OP_COALESCING
328
329/* Generics <,> tokens */
330%token OP_GENERICS_LT
331%token OP_GENERICS_LT_DECL
332%token OP_GENERICS_GT
333
334%token LITERAL
335
336%token IDENTIFIER
337%token OPEN_PARENS_LAMBDA
338%token OPEN_PARENS_CAST
339%token GENERIC_DIMENSION
340%token DEFAULT_COLON
341%token OPEN_BRACKET_EXPR
342
343// Make the parser go into eval mode parsing (statements and compilation units).
344%token EVAL_STATEMENT_PARSER
345%token EVAL_COMPILATION_UNIT_PARSER
346%token EVAL_USING_DECLARATIONS_UNIT_PARSER
347
348%token DOC_SEE
349
350//
351// This token is generated to trigger the completion engine at this point
352//
353%token GENERATE_COMPLETION
354
355//
356// This token is return repeatedly after the first GENERATE_COMPLETION
357// token is produced and before the final EOF
358//
359%token COMPLETE_COMPLETION
360
361/* Add precedence rules to solve dangling else s/r conflict */
362%nonassoc IF
363%nonassoc ELSE
364
365/* Define the operator tokens and their precedences */
366%right ASSIGN
367%right OP_COALESCING
368%right INTERR
369%left OP_OR
370%left OP_AND
371%left BITWISE_OR
372%left BITWISE_AND
373%left OP_SHIFT_LEFT OP_SHIFT_RIGHT
374%left PLUS MINUS
375%left STAR DIV PERCENT
376%right BANG CARRET UMINUS
377%nonassoc OP_INC OP_DEC
378%left OPEN_PARENS
379%left OPEN_BRACKET OPEN_BRACE
380%left DOT
381
382%start compilation_unit
383%%
384
385compilation_unit
386  : outer_declaration opt_EOF
387    {
388    Lexer.check_incorrect_doc_comment ();
389    }
390  | interactive_parsing  { Lexer.CompleteOnEOF = false; } opt_EOF
391  | documentation_parsing
392  ;
393 
394outer_declaration
395  : opt_extern_alias_directives opt_using_directives
396  | opt_extern_alias_directives opt_using_directives namespace_or_type_declarations opt_attributes
397    {
398    if ($4 != null) {
399      Attributes attrs = (Attributes) $4;
400      report.Error (1730, attrs.Attrs [0].Location,
401        "Assembly and module attributes must precede all other elements except using clauses and extern alias declarations");
402
403      current_namespace.UnattachedAttributes = attrs;
404    }
405    }
406  | opt_extern_alias_directives opt_using_directives attribute_sections
407    {
408    module.AddAttributes ((Attributes) $3, current_namespace);
409    }
410  | error
411    {
412    if (yyToken == Token.EXTERN_ALIAS)
413      report.Error (439, lexer.Location, "An extern alias declaration must precede all other elements");
414    else
415      Error_SyntaxError (yyToken);
416    }
417  ;
418 
419opt_EOF
420  : /* empty */
421  | EOF
422  ;
423
424extern_alias_directives
425  : extern_alias_directive
426  | extern_alias_directives extern_alias_directive
427  ;
428
429extern_alias_directive
430  : EXTERN_ALIAS IDENTIFIER IDENTIFIER SEMICOLON
431    {
432    var lt = (LocatedToken) $2;
433    string s = lt.Value;
434    if (s != "alias") {
435      syntax_error (lt.Location, "`alias' expected");
436    } else {
437      if (lang_version == LanguageVersion.ISO_1)
438        FeatureIsNotAvailable (lt.Location, "external alias");
439
440      lt = (LocatedToken) $3;
441      if (lt.Value == QualifiedAliasMember.GlobalAlias) {
442        RootNamespace.Error_GlobalNamespaceRedefined (report, lt.Location);
443      }
444     
445      var na = new UsingExternAlias (new SimpleMemberName (lt.Value, lt.Location), GetLocation ($1));
446      current_namespace.AddUsing (na);
447     
448      lbag.AddLocation (na, GetLocation ($2), GetLocation ($4));
449    }
450    }
451  | EXTERN_ALIAS error
452    {
453    Error_SyntaxError (yyToken);
454    }
455  ;
456 
457using_directives
458  : using_directive
459  | using_directives using_directive
460  ;
461
462using_directive
463  : using_namespace
464    {
465    if (doc_support)
466      Lexer.doc_state = XmlCommentState.Allowed;
467    }
468  ;
469
470using_namespace
471  : USING namespace_or_type_expr SEMICOLON
472    {
473    var un = new UsingNamespace ((ATypeNameExpression) $2, GetLocation ($1));
474    current_namespace.AddUsing (un);
475   
476    lbag.AddLocation (un, GetLocation ($3));
477    }
478  | USING IDENTIFIER ASSIGN namespace_or_type_expr SEMICOLON
479    {
480    var lt = (LocatedToken) $2;
481    if (lang_version != LanguageVersion.ISO_1 && lt.Value == "global") {
482      report.Warning (440, 2, lt.Location,
483       "An alias named `global' will not be used when resolving `global::'. The global namespace will be used instead");
484    }
485
486    var un = new UsingAliasNamespace (new SimpleMemberName (lt.Value, lt.Location), (ATypeNameExpression) $4, GetLocation ($1));
487    current_namespace.AddUsing (un);
488    lbag.AddLocation (un, GetLocation ($3), GetLocation ($5));
489    }
490  | USING error
491   {
492    Error_SyntaxError (yyToken);
493    $$ = null;
494   }
495  ;
496
497//
498// Strictly speaking, namespaces don't have attributes but
499// we parse global attributes along with namespace declarations and then
500// detach them
501//
502namespace_declaration
503  : opt_attributes NAMESPACE namespace_name
504    {
505    Attributes attrs = (Attributes) $1;
506    var name = (MemberName) $3;
507    if (attrs != null) {
508      bool valid_global_attrs = true;
509      if ((current_namespace.DeclarationFound || current_namespace != file)) {
510        valid_global_attrs = false;
511      } else {
512        foreach (var a in attrs.Attrs) {
513          if (a.ExplicitTarget == "assembly" || a.ExplicitTarget == "module")
514            continue;
515           
516          valid_global_attrs = false;
517          break;
518        }
519      }
520     
521      if (!valid_global_attrs)
522        report.Error (1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
523    }
524 
525    module.AddAttributes (attrs, current_namespace);
526   
527    var ns = new NamespaceContainer (name, current_namespace);
528    current_namespace.AddTypeContainer (ns);
529    current_container = current_namespace = ns;
530    }
531    OPEN_BRACE
532    {
533    if (doc_support)
534      Lexer.doc_state = XmlCommentState.Allowed;
535    }
536    opt_extern_alias_directives opt_using_directives opt_namespace_or_type_declarations CLOSE_BRACE opt_semicolon_error
537    {
538    if ($11 != null)
539      lbag.AddLocation (current_container, GetLocation ($2), GetLocation ($5), GetLocation ($10), GetLocation ($11));
540    else
541      lbag.AddLocation (current_container, GetLocation ($2), GetLocation ($5), GetLocation ($10));
542   
543    current_container = current_namespace = current_namespace.Parent;
544    }
545  | opt_attributes NAMESPACE namespace_name
546    {
547    report.Error (1514, lexer.Location, "Unexpected symbol `{0}', expecting `.' or `{{'", GetSymbolName (yyToken));
548
549    var name = (MemberName) $3;   
550    var ns = new NamespaceContainer (name, current_namespace);
551    lbag.AddLocation (ns, GetLocation ($2));
552    current_namespace.AddTypeContainer (ns);
553    }
554  ;
555
556opt_semicolon_error
557  : /* empty */
558  | SEMICOLON
559  | error
560    {
561    Error_SyntaxError (yyToken);
562      $$ = null;
563    }
564  ;
565
566namespace_name
567  : IDENTIFIER
568    {
569    var lt = (LocatedToken) $1;
570    $$ = new MemberName (lt.Value, lt.Location);
571    }
572  | namespace_name DOT IDENTIFIER
573    {
574    var lt = (LocatedToken) $3;
575    $$ = new MemberName ((MemberName) $1, lt.Value, lt.Location);   
576    lbag.AddLocation ($$, GetLocation ($2));
577    }
578  | error
579    {
580    Error_SyntaxError (yyToken);
581    $$ = new MemberName ("<invalid>", lexer.Location);
582    }
583  ;
584
585opt_semicolon
586  : /* empty */
587  | SEMICOLON
588  ;
589
590opt_comma
591  : /* empty */
592  | COMMA
593  ;
594
595opt_using_directives
596  : /* empty */
597  | using_directives
598  ;
599
600opt_extern_alias_directives
601  : /* empty */
602  | extern_alias_directives
603  ;
604
605opt_namespace_or_type_declarations
606  : /* empty */
607  | namespace_or_type_declarations
608  ;
609
610namespace_or_type_declarations
611  : namespace_or_type_declaration
612  | namespace_or_type_declarations namespace_or_type_declaration
613  ;
614
615namespace_or_type_declaration
616  : type_declaration
617    {
618    if ($1 != null) {
619      TypeContainer ds = (TypeContainer)$1;
620
621      if ((ds.ModFlags & (Modifiers.PRIVATE | Modifiers.PROTECTED)) != 0){
622        report.Error (1527, ds.Location,
623        "Namespace elements cannot be explicitly declared as private, protected or protected internal");
624      }
625
626      // Here is a trick, for explicit attributes we don't know where they belong to until
627      // we parse succeeding declaration hence we parse them as normal and re-attach them
628      // when we know whether they are global (assembly:, module:) or local (type:).
629      if (ds.OptAttributes != null) {
630        ds.OptAttributes.ConvertGlobalAttributes (ds, current_namespace, !current_namespace.DeclarationFound && current_namespace == file);
631      }
632    }
633    current_namespace.DeclarationFound = true;
634    }
635  | namespace_declaration
636    {
637    current_namespace.DeclarationFound = true;
638    }
639  | attribute_sections CLOSE_BRACE {
640    current_namespace.UnattachedAttributes = (Attributes) $1;
641    report.Error (1518, lexer.Location, "Attributes must be attached to class, delegate, enum, interface or struct");
642    lexer.putback ('}');
643    }
644  ;
645
646type_declaration
647  : class_declaration   
648  | struct_declaration
649  | interface_declaration
650  | enum_declaration   
651  | delegate_declaration
652//
653// Enable this when we have handled all errors, because this acts as a generic fallback
654//
655//  | error {
656//    Console.WriteLine ("Token=" + yyToken);
657//    report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
658//    }
659  ;
660
661//
662// Attributes
663//
664
665opt_attributes
666  : /* empty */
667  | attribute_sections
668    ;
669 
670attribute_sections
671  : attribute_section
672    {
673    var sect = (List<Attribute>) $1;
674    $$ = new Attributes (sect);
675    }
676  | attribute_sections attribute_section
677    {
678    Attributes attrs = $1 as Attributes;
679    var sect = (List<Attribute>) $2;
680    if (attrs == null)
681      attrs = new Attributes (sect);
682    else if (sect != null)
683      attrs.AddAttributes (sect);
684    $$ = attrs;
685    }
686  ;
687 
688attribute_section
689  : OPEN_BRACKET
690    {
691      PushLocation (GetLocation ($1));
692    lexer.parsing_attribute_section = true;
693    }
694    attribute_section_cont
695    {
696    lexer.parsing_attribute_section = false;
697    $$ = $3;
698    }
699  ;
700 
701attribute_section_cont
702  : attribute_target COLON
703    {
704    current_attr_target = (string) $1;
705    if (current_attr_target == "assembly" || current_attr_target == "module") {
706      Lexer.check_incorrect_doc_comment ();
707    }
708    }
709    attribute_list opt_comma CLOSE_BRACKET
710    {
711    // when attribute target is invalid
712    if (current_attr_target == string.Empty)
713      $$ = new List<Attribute> (0);
714    else
715      $$ = $4;
716    lbag.InsertLocation ($$, 0, GetLocation ($2));
717    lbag.InsertLocation ($$, 0, PopLocation ());
718    lbag.InsertLocation ($$, 0, PopLocation ());
719    if ($5 != null) {
720      lbag.AddLocation ($$, GetLocation ($5), GetLocation ($6));
721    } else {
722      lbag.AddLocation ($$, GetLocation ($6));
723    }
724
725    current_attr_target = null;
726    lexer.parsing_attribute_section = false;
727    }
728  | attribute_list opt_comma CLOSE_BRACKET
729    {
730    $$ = $1;
731    lbag.InsertLocation ($$, 0, PopLocation ());
732    if ($2 != null) {
733      lbag.AddLocation ($$, GetLocation($2), GetLocation ($3));
734    } else {
735      lbag.AddLocation ($$, GetLocation($3));
736    }
737    }
738  | IDENTIFIER error
739    {
740    Error_SyntaxError (yyToken);
741
742    var lt = (LocatedToken) $1;
743    var tne = new SimpleName (lt.Value, null, lt.Location);
744
745    $$ = new List<Attribute> () {
746      new Attribute (null, tne, null, GetLocation ($1), false)
747    };
748    }
749  | error
750    {
751    CheckAttributeTarget (yyToken, GetTokenName (yyToken), GetLocation ($1));
752    $$ = null;
753    }
754  ;
755
756attribute_target
757  : IDENTIFIER
758    {
759    var lt = (LocatedToken) $1;
760    $$ = CheckAttributeTarget (yyToken, lt.Value, lt.Location);
761    PushLocation (GetLocation ($1));
762    }
763  | EVENT  { $$ = "event"; PushLocation (GetLocation ($1)); }
764  | RETURN { $$ = "return"; PushLocation (GetLocation ($1)); }
765  ;
766
767attribute_list
768  : attribute
769    {
770    $$ = new List<Attribute> (4) { (Attribute) $1 };
771    }
772  | attribute_list COMMA attribute
773    {
774    var attrs = (List<Attribute>) $1;
775    if (attrs != null) {
776      attrs.Add ((Attribute) $3);
777        lbag.AddLocation (attrs, GetLocation ($2));
778      }
779
780    $$ = attrs;
781    }
782  ;
783
784attribute
785  : attribute_name
786    {
787    ++lexer.parsing_block;
788    }
789    opt_attribute_arguments
790    {
791    --lexer.parsing_block;
792   
793    var tne = (ATypeNameExpression) $1;
794    if (tne.HasTypeArguments) {
795      report.Error (404, tne.Location, "Attributes cannot be generic");
796    }
797    Arguments [] arguments = (Arguments []) $3;
798
799    $$ = new Attribute (current_attr_target, tne, (Arguments[]) $3, GetLocation ($1), lexer.IsEscapedIdentifier (tne));
800    if (arguments != null) {
801      attributeArgumentCommas.Insert (0, savedAttrParenOpenLocation);
802      attributeArgumentCommas.Add (savedAttrParenCloseLocation);
803      lbag.AddLocation ($$, attributeArgumentCommas);
804      attributeArgumentCommas.Clear ();
805    } else if (HadAttributeParens) {
806      lbag.AddLocation ($$, savedAttrParenOpenLocation, savedAttrParenCloseLocation);
807    }
808    }
809  ;
810
811attribute_name
812  : namespace_or_type_expr
813  ;
814
815opt_attribute_arguments
816  : /* empty */   { $$ = null; HadAttributeParens = false;  }
817  | OPEN_PARENS attribute_arguments CLOSE_PARENS
818    {
819    savedAttrParenOpenLocation = GetLocation ($1);
820    savedAttrParenCloseLocation = GetLocation ($3);
821    $$ = $2;
822    HadAttributeParens = true;
823    }
824  ;
825
826
827attribute_arguments
828  : /* empty */     { $$ = null; }
829  | positional_or_named_argument
830    {
831      Arguments a = new Arguments (4);
832    a.Add ((Argument) $1);
833    $$ = new Arguments [] { a, null };
834    }
835  | named_attribute_argument
836    {
837      Arguments a = new Arguments (4);
838    a.Add ((Argument) $1); 
839    $$ = new Arguments [] { null, a };
840    }
841    | attribute_arguments COMMA positional_or_named_argument
842    {
843    Arguments[] o = (Arguments[]) $1;
844    if (o [1] != null) {
845      report.Error (1016, ((Argument) $3).Expr.Location, "Named attribute arguments must appear after the positional arguments");
846      o [0] = new Arguments (4);
847    }
848   
849    Arguments args = ((Arguments) o [0]);
850    if (args.Count > 0 && !($3 is NamedArgument) && args [args.Count - 1] is NamedArgument)
851      Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
852   
853    args.Add ((Argument) $3);
854    attributeArgumentCommas.Add (GetLocation ($2));
855    }
856    | attribute_arguments COMMA named_attribute_argument
857    {
858    Arguments[] o = (Arguments[]) $1;
859    if (o [1] == null) {
860      o [1] = new Arguments (4);
861    }
862
863    ((Arguments) o [1]).Add ((Argument) $3);
864    attributeArgumentCommas.Add (GetLocation ($2));
865    }
866    ;
867
868positional_or_named_argument
869  : expression
870    {
871      $$ = new Argument ((Expression) $1);
872    }
873  | named_argument
874  | error
875    {
876    Error_SyntaxError (yyToken);
877    $$ = null;
878    }
879  ;
880
881named_attribute_argument
882  : IDENTIFIER ASSIGN
883    {
884    ++lexer.parsing_block;
885    }
886    expression
887    {
888      --lexer.parsing_block;
889      var lt = (LocatedToken) $1;
890    $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $4);   
891    lbag.AddLocation ($$, GetLocation($2));
892    }
893  ;
894 
895named_argument
896  : identifier_inside_body COLON opt_named_modifier expression_or_error
897    {
898    if (lang_version <= LanguageVersion.V_3)
899      FeatureIsNotAvailable (GetLocation ($1), "named argument");
900     
901    // Avoid boxing in common case (no modifier)
902    var arg_mod = $3 == null ? Argument.AType.None : (Argument.AType) $3;
903     
904    var lt = (LocatedToken) $1;
905    $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $4, arg_mod);
906    lbag.AddLocation ($$, GetLocation($2));
907    }
908  ;
909 
910opt_named_modifier
911  : /* empty */   { $$ = null; }
912  | REF
913    {
914    $$ = Argument.AType.Ref;
915    }
916  | OUT
917    {
918    $$ = Argument.AType.Out;
919    }
920  ;
921     
922opt_class_member_declarations
923  : /* empty */
924  | class_member_declarations
925  ;
926
927class_member_declarations
928  : class_member_declaration
929    {
930    lexer.parsing_modifiers = true;
931    lexer.parsing_block = 0;
932    }
933  | class_member_declarations class_member_declaration
934    {
935    lexer.parsing_modifiers = true;
936    lexer.parsing_block = 0;
937    }
938  ;
939 
940class_member_declaration
941  : constant_declaration
942  | field_declaration
943  | method_declaration
944  | property_declaration
945  | event_declaration
946  | indexer_declaration
947  | operator_declaration
948  | constructor_declaration
949  | primary_constructor_body
950  | destructor_declaration
951  | type_declaration
952  | attributes_without_members
953  | incomplete_member
954  | error
955    {
956    report.Error (1519, lexer.Location, "Unexpected symbol `{0}' in class, struct, or interface member declaration",
957      GetSymbolName (yyToken));
958    $$ = null;
959    lexer.parsing_generic_declaration = false;
960    }
961  ;
962
963primary_constructor_body
964  : OPEN_BRACE
965    {
966    current_local_parameters = current_type.PrimaryConstructorParameters;
967    if (current_local_parameters == null) {
968      report.Error (9010, GetLocation ($1), "Primary constructor body is not allowed");
969      current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
970    }
971
972    ++lexer.parsing_block;
973    start_block (GetLocation ($1));
974    }
975    opt_statement_list block_end
976    {
977    current_local_parameters = null;
978    var t = current_type as ClassOrStruct;
979    if (t != null) {
980      var b = (ToplevelBlock) $4;
981      if (t.PrimaryConstructorBlock != null) {
982        report.Error (8041, b.StartLocation, "Primary constructor already has a body");
983      } else {
984        t.PrimaryConstructorBlock = b;
985      }
986    }
987    }
988  ;
989
990struct_declaration
991  : opt_attributes
992    opt_modifiers
993    opt_partial
994    STRUCT
995    {
996    }
997    type_declaration_name
998    {
999    lexer.ConstraintsParsing = true;
1000    valid_param_mod = ParameterModifierType.PrimaryConstructor;
1001    push_current_container (new Struct (current_container, (MemberName) $6, (Modifiers) $2, (Attributes) $1), $3);
1002    lbag.AddMember (current_container, GetModifierLocations (), GetLocation ($4));
1003    }
1004    opt_primary_parameters
1005    opt_class_base
1006    opt_type_parameter_constraints_clauses
1007    {
1008    valid_param_mod = 0;
1009    lexer.ConstraintsParsing = false;
1010
1011    if ($8 != null)
1012      current_type.PrimaryConstructorParameters = (ParametersCompiled) $8;
1013
1014    if ($10 != null)
1015      current_container.SetConstraints ((List<Constraints>) $10);
1016
1017    if (doc_support)
1018      current_container.PartialContainer.DocComment = Lexer.consume_doc_comment ();
1019
1020   
1021    lexer.parsing_modifiers = true;
1022    }
1023    OPEN_BRACE
1024    {
1025    if (doc_support)
1026      Lexer.doc_state = XmlCommentState.Allowed;
1027    }
1028    opt_class_member_declarations CLOSE_BRACE
1029    {
1030    --lexer.parsing_declaration;
1031    if (doc_support)
1032      Lexer.doc_state = XmlCommentState.Allowed;
1033    }
1034    opt_semicolon
1035    {
1036    if ($16 == null) {
1037      lbag.AppendToMember (current_container, GetLocation ($12), GetLocation ($15));
1038    } else {
1039      lbag.AppendToMember (current_container, GetLocation ($12), GetLocation ($15), GetLocation ($17));
1040    }
1041    $$ = pop_current_class ();
1042    }
1043  | opt_attributes opt_modifiers opt_partial STRUCT error
1044    {
1045    Error_SyntaxError (yyToken);
1046    }
1047  ;
1048 
1049constant_declaration
1050  : opt_attributes
1051    opt_modifiers
1052    CONST type IDENTIFIER
1053    {
1054    var lt = (LocatedToken) $5;
1055    var mod = (Modifiers) $2;
1056    current_field = new Const (current_type, (FullNamedExpression) $4, mod, new MemberName (lt.Value, lt.Location), (Attributes) $1);
1057    current_type.AddMember (current_field);
1058   
1059    if ((mod & Modifiers.STATIC) != 0) {
1060      report.Error (504, current_field.Location, "The constant `{0}' cannot be marked static", current_field.GetSignatureForError ());
1061    }
1062   
1063    $$ = current_field;
1064    }
1065    constant_initializer opt_constant_declarators SEMICOLON
1066    {
1067    if (doc_support) {
1068      current_field.DocComment = Lexer.consume_doc_comment ();
1069      Lexer.doc_state = XmlCommentState.Allowed;
1070    }
1071   
1072    current_field.Initializer = (ConstInitializer) $7;
1073    lbag.AddMember (current_field, GetModifierLocations (), GetLocation ($3), GetLocation ($9));
1074    current_field = null;
1075    }
1076  | opt_attributes
1077    opt_modifiers
1078    CONST type error
1079    {
1080    Error_SyntaxError (yyToken);
1081
1082    current_type.AddMember (new Const (current_type, (FullNamedExpression) $4, (Modifiers) $2, MemberName.Null, (Attributes) $1));
1083    }
1084  ;
1085 
1086opt_constant_declarators
1087  : /* empty */
1088  | constant_declarators
1089  ;
1090 
1091constant_declarators
1092  : constant_declarator
1093    {
1094    current_field.AddDeclarator ((FieldDeclarator) $1);
1095    }
1096  | constant_declarators constant_declarator
1097    {
1098    current_field.AddDeclarator ((FieldDeclarator) $2);
1099    }
1100  ;
1101 
1102constant_declarator
1103  : COMMA IDENTIFIER constant_initializer
1104    {
1105    var lt = (LocatedToken) $2;
1106      $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (ConstInitializer) $3);
1107      lbag.AddLocation ($$, GetLocation ($1));
1108    }
1109  ;   
1110
1111constant_initializer
1112  : ASSIGN
1113    {
1114    ++lexer.parsing_block;
1115    }
1116    constant_initializer_expr
1117    {
1118    --lexer.parsing_block;
1119    $$ = new ConstInitializer (current_field, (Expression) $3, GetLocation ($1));
1120      lbag.AddLocation ($$, GetLocation ($1));
1121    }
1122  | error
1123    {
1124    report.Error (145, lexer.Location, "A const field requires a value to be provided");
1125    $$ = null;
1126    }   
1127  ;
1128 
1129constant_initializer_expr
1130  : constant_expression
1131  | array_initializer
1132  ;
1133
1134field_declaration
1135  : opt_attributes
1136    opt_modifiers
1137    member_type IDENTIFIER
1138    {
1139      lexer.parsing_generic_declaration = false;
1140
1141    FullNamedExpression type = (FullNamedExpression) $3;
1142    if (type.Type != null && type.Type.Kind == MemberKind.Void)
1143      report.Error (670, GetLocation ($3), "Fields cannot have void type");
1144     
1145    var lt = (LocatedToken) $4;
1146    current_field = new Field (current_type, type, (Modifiers) $2, new MemberName (lt.Value, lt.Location), (Attributes) $1);
1147    current_type.AddField (current_field);
1148    $$ = current_field;
1149    }
1150    opt_field_initializer
1151    opt_field_declarators
1152    SEMICOLON
1153    {
1154    if (doc_support) {
1155      current_field.DocComment = Lexer.consume_doc_comment ();
1156      Lexer.doc_state = XmlCommentState.Allowed;
1157    }
1158     
1159    lbag.AddMember (current_field, GetModifierLocations (), GetLocation ($8));
1160    $$ = current_field;
1161    current_field = null;
1162    }
1163  | opt_attributes
1164    opt_modifiers
1165    FIXED simple_type IDENTIFIER
1166    {
1167    if (lang_version < LanguageVersion.ISO_2)
1168      FeatureIsNotAvailable (GetLocation ($3), "fixed size buffers");
1169
1170    var lt = (LocatedToken) $5;
1171    current_field = new FixedField (current_type, (FullNamedExpression) $4, (Modifiers) $2,
1172      new MemberName (lt.Value, lt.Location), (Attributes) $1);
1173     
1174    current_type.AddField (current_field);
1175    }
1176    fixed_field_size opt_fixed_field_declarators SEMICOLON
1177    {
1178    if (doc_support) {
1179      current_field.DocComment = Lexer.consume_doc_comment ();
1180      Lexer.doc_state = XmlCommentState.Allowed;
1181      }
1182
1183    current_field.Initializer = (ConstInitializer) $7;     
1184    lbag.AddMember (current_field, GetModifierLocations (), GetLocation ($3), GetLocation ($9));
1185    $$ = current_field;
1186      current_field = null;
1187    }
1188  | opt_attributes
1189    opt_modifiers
1190    FIXED simple_type error
1191    SEMICOLON
1192    {
1193    report.Error (1641, GetLocation ($5), "A fixed size buffer field must have the array size specifier after the field name");
1194    }
1195  ;
1196 
1197opt_field_initializer
1198  : /* empty */
1199  | ASSIGN
1200    {
1201      ++lexer.parsing_block;
1202    current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
1203      start_block (GetLocation ($1));
1204    }
1205    variable_initializer
1206    {
1207      --lexer.parsing_block;
1208    current_field.Initializer = (Expression) $3;
1209    lbag.AppendToMember (current_field, GetLocation ($1));
1210    end_block (lexer.Location);
1211    current_local_parameters = null;
1212    }
1213  ;
1214 
1215opt_field_declarators
1216  : /* empty */
1217  | field_declarators
1218  ;
1219 
1220field_declarators
1221  : field_declarator
1222    {
1223    current_field.AddDeclarator ((FieldDeclarator) $1);
1224    }
1225  | field_declarators field_declarator
1226    {
1227    current_field.AddDeclarator ((FieldDeclarator) $2);
1228    }
1229  ;
1230 
1231field_declarator
1232  : COMMA IDENTIFIER
1233    {
1234    var lt = (LocatedToken) $2;
1235      $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), null);
1236      lbag.AddLocation ($$, GetLocation ($1));
1237    }
1238  | COMMA IDENTIFIER ASSIGN
1239    {
1240    ++lexer.parsing_block;
1241    }
1242    variable_initializer
1243    {
1244    --lexer.parsing_block;
1245    var lt = (LocatedToken) $2;   
1246      $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (Expression) $5);
1247      lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
1248    }
1249  ;
1250
1251opt_fixed_field_declarators
1252  : /* empty */
1253  | fixed_field_declarators
1254  ;
1255 
1256fixed_field_declarators
1257  : fixed_field_declarator
1258    {
1259    current_field.AddDeclarator ((FieldDeclarator) $1);
1260    }
1261  | fixed_field_declarators fixed_field_declarator
1262    {
1263    current_field.AddDeclarator ((FieldDeclarator) $2);
1264    }
1265  ;
1266 
1267fixed_field_declarator
1268  : COMMA IDENTIFIER fixed_field_size
1269    {
1270    var lt = (LocatedToken) $2;   
1271    $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (ConstInitializer) $3);
1272    lbag.AddLocation ($$, GetLocation ($1));
1273    }
1274  ;
1275
1276fixed_field_size
1277  : OPEN_BRACKET
1278    {
1279    ++lexer.parsing_block;
1280    }
1281    expression CLOSE_BRACKET
1282    {
1283    --lexer.parsing_block;
1284    $$ = new ConstInitializer (current_field, (Expression) $3, GetLocation ($1));
1285    lbag.AddLocation ($$, GetLocation ($4));
1286    }
1287  | OPEN_BRACKET error
1288    {
1289    report.Error (443, lexer.Location, "Value or constant expected");
1290    $$ = null;
1291    }   
1292  ;
1293
1294variable_initializer
1295  : expression
1296  | array_initializer
1297  | error
1298    {
1299    // It has to be here for the parent to safely restore artificial block
1300      Error_SyntaxError (yyToken);
1301      $$ = null;
1302    }
1303  ;
1304
1305method_declaration
1306  : method_header
1307    {
1308    if (doc_support)
1309      Lexer.doc_state = XmlCommentState.NotAllowed;
1310
1311    // Was added earlier in the case of body being eof for full ast
1312    }
1313    method_body_expression_block
1314    {
1315    Method method = (Method) $1;
1316    method.Block = (ToplevelBlock) $3;
1317    async_block = false;
1318   
1319    if (method.Block == null) {
1320      lbag.AppendToMember (method, savedLocation); // semicolon
1321      method.ParameterInfo.CheckParameters (method);
1322
1323      if ((method.ModFlags & Modifiers.ASYNC) != 0) {
1324        report.Error (1994, method.Location, "`{0}': The async modifier can only be used with methods that have a body",
1325          method.GetSignatureForError ());
1326      }
1327    } else {
1328      if (current_container.Kind == MemberKind.Interface) {
1329        report.Error (531, method.Location, "`{0}': interface members cannot have a definition",
1330          method.GetSignatureForError ());
1331      }
1332    }
1333
1334    current_local_parameters = null;
1335
1336    if (doc_support)
1337      Lexer.doc_state = XmlCommentState.Allowed;
1338    }
1339  ;
1340
1341method_header
1342  : opt_attributes
1343    opt_modifiers
1344    member_type
1345    method_declaration_name OPEN_PARENS
1346    {
1347    valid_param_mod = ParameterModifierType.All;
1348    }
1349    opt_formal_parameter_list CLOSE_PARENS
1350    {
1351    valid_param_mod = 0;
1352    MemberName name = (MemberName) $4;
1353    current_local_parameters = (ParametersCompiled) $7;
1354
1355    var method = Method.Create (current_type, (FullNamedExpression) $3, (Modifiers) $2,
1356             name, current_local_parameters, (Attributes) $1);
1357
1358    current_type.AddMember (method);
1359
1360    async_block = (method.ModFlags & Modifiers.ASYNC) != 0;
1361
1362    if (doc_support)
1363      method.DocComment = Lexer.consume_doc_comment ();
1364
1365    lbag.AddMember (method, GetModifierLocations (), GetLocation ($5), GetLocation ($8));
1366    $$ = method;
1367
1368    lexer.ConstraintsParsing = true;
1369    }
1370    opt_type_parameter_constraints_clauses
1371    {
1372    lexer.ConstraintsParsing = false;
1373
1374    if ($10 != null) {
1375      var method = (Method) $9;
1376      method.SetConstraints ((List<Constraints>) $10);
1377    }
1378
1379    $$ = $9;
1380    }
1381  | opt_attributes
1382    opt_modifiers
1383    PARTIAL
1384    VOID
1385    {
1386    lexer.parsing_generic_declaration = true;
1387    }
1388    method_declaration_name
1389    OPEN_PARENS
1390    {
1391    lexer.parsing_generic_declaration = false;
1392      valid_param_mod = ParameterModifierType.All;
1393    }
1394    opt_formal_parameter_list CLOSE_PARENS
1395    {
1396    lexer.ConstraintsParsing = true;
1397    }
1398    opt_type_parameter_constraints_clauses
1399    {
1400    lexer.ConstraintsParsing = false;
1401    valid_param_mod = 0;
1402
1403    MemberName name = (MemberName) $6;
1404    current_local_parameters = (ParametersCompiled) $9;
1405
1406    var modifiers = (Modifiers) $2;
1407    modifiers |= Modifiers.PARTIAL;
1408
1409    var method = Method.Create (current_type, new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($4)),
1410             modifiers, name, current_local_parameters, (Attributes) $1);
1411
1412    current_type.AddMember (method);
1413
1414    async_block = (method.ModFlags & Modifiers.ASYNC) != 0;
1415
1416    if ($12 != null)
1417      method.SetConstraints ((List<Constraints>) $12);
1418
1419    if (doc_support)
1420      method.DocComment = Lexer.consume_doc_comment ();
1421
1422    StoreModifierLocation (Modifiers.PARTIAL, GetLocation ($3));
1423    lbag.AddMember (method, GetModifierLocations (), GetLocation ($7), GetLocation ($10));
1424    $$ = method;
1425    }
1426  | opt_attributes
1427    opt_modifiers
1428    member_type
1429    modifiers method_declaration_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1430    {
1431    MemberName name = (MemberName) $5;
1432    report.Error (1585, name.Location,
1433      "Member modifier `{0}' must precede the member type and name", ModifiersExtensions.Name ((Modifiers) $4));
1434
1435    var method = Method.Create (current_type, (FullNamedExpression) $3,
1436              0, name, (ParametersCompiled) $7, (Attributes) $1);
1437
1438    current_type.AddMember (method);
1439
1440    current_local_parameters = (ParametersCompiled) $7;
1441
1442    if (doc_support)
1443      method.DocComment = Lexer.consume_doc_comment ();
1444
1445    $$ = method;
1446    }
1447  | opt_attributes
1448    opt_modifiers
1449    member_type
1450    method_declaration_name error
1451    {
1452    Error_SyntaxError (yyToken);
1453    current_local_parameters = ParametersCompiled.Undefined;
1454
1455    MemberName name = (MemberName) $4;
1456    var method = Method.Create (current_type, (FullNamedExpression) $3, (Modifiers) $2,
1457                  name, current_local_parameters, (Attributes) $1);
1458
1459    current_type.AddMember (method);
1460
1461    if (doc_support)
1462      method.DocComment = Lexer.consume_doc_comment ();
1463
1464    $$ = method;
1465    }
1466  ;
1467
1468method_body_expression_block
1469  : method_body
1470  | expression_block
1471  ;
1472
1473method_body
1474  : block
1475  | SEMICOLON   { savedLocation = GetLocation ($1); $$ = null; }
1476  ;
1477
1478expression_block
1479  : ARROW
1480   {
1481    if (lang_version < LanguageVersion.V_6) {
1482      FeatureIsNotAvailable (GetLocation ($1), "expression bodied members");
1483    }
1484
1485    ++lexer.parsing_block;
1486    start_block (GetLocation ($1));
1487   }
1488   expression SEMICOLON
1489   {
1490    lexer.parsing_block = 0;
1491    current_block.AddStatement (new ContextualReturn ((Expression) $3));
1492    var b = end_block (GetLocation ($4));
1493    b.IsCompilerGenerated = true;
1494    $$ = b;
1495   }
1496  ;
1497
1498opt_formal_parameter_list
1499  : /* empty */     { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
1500  | formal_parameter_list
1501  ;
1502 
1503formal_parameter_list
1504  : fixed_parameters
1505    {
1506    var pars_list = (List<Parameter>) $1;
1507      $$ = new ParametersCompiled (pars_list.ToArray ());
1508      lbag.AddLocation ($$, parameterListCommas);
1509    }
1510  | fixed_parameters COMMA parameter_array
1511    {
1512    var pars_list = (List<Parameter>) $1;
1513    pars_list.Add ((Parameter) $3);
1514    parameterListCommas.Add (GetLocation ($2));
1515   
1516    $$ = new ParametersCompiled (pars_list.ToArray ());
1517      lbag.AddLocation ($$, parameterListCommas);
1518    }
1519  | fixed_parameters COMMA arglist_modifier
1520    {
1521    var pars_list = (List<Parameter>) $1;
1522    pars_list.Add (new ArglistParameter (GetLocation ($3)));
1523    parameterListCommas.Add (GetLocation ($2));
1524   
1525    $$ = new ParametersCompiled (pars_list.ToArray (), true);
1526      lbag.AddLocation ($$, parameterListCommas);
1527    }
1528  | parameter_array COMMA error
1529    {
1530    if ($1 != null)
1531      report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1532
1533    $$ = new ParametersCompiled (new Parameter[] { (Parameter) $1 } );     
1534      lbag.AddLocation ($$, parameterListCommas);
1535    }
1536  | fixed_parameters COMMA parameter_array COMMA error
1537    {
1538    if ($3 != null)
1539      report.Error (231, ((Parameter) $3).Location, "A params parameter must be the last parameter in a formal parameter list");
1540
1541    var pars_list = (List<Parameter>) $1;
1542    pars_list.Add (new ArglistParameter (GetLocation ($3)));
1543    parameterListCommas.Add (GetLocation ($2));
1544    parameterListCommas.Add (GetLocation ($4));
1545   
1546    $$ = new ParametersCompiled (pars_list.ToArray (), true);
1547      lbag.AddLocation ($$, parameterListCommas);
1548    }
1549  | arglist_modifier COMMA error
1550    {
1551    report.Error (257, GetLocation ($1), "An __arglist parameter must be the last parameter in a formal parameter list");
1552
1553    $$ = new ParametersCompiled (new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
1554      lbag.AddLocation ($$, parameterListCommas);
1555    }
1556  | fixed_parameters COMMA ARGLIST COMMA error
1557    {
1558    report.Error (257, GetLocation ($3), "An __arglist parameter must be the last parameter in a formal parameter list");
1559
1560    var pars_list = (List<Parameter>) $1;
1561    pars_list.Add (new ArglistParameter (GetLocation ($3)));
1562    parameterListCommas.Add (GetLocation ($2));
1563    parameterListCommas.Add (GetLocation ($4));
1564
1565    $$ = new ParametersCompiled (pars_list.ToArray (), true);
1566      lbag.AddLocation ($$, parameterListCommas);
1567    }
1568  | parameter_array
1569    {
1570    $$ = new ParametersCompiled (new Parameter[] { (Parameter) $1 } );
1571    }
1572  | arglist_modifier
1573    {
1574    $$ = new ParametersCompiled (new Parameter [] { new ArglistParameter (GetLocation ($1)) }, true);
1575    }
1576  | error
1577    {
1578    Error_SyntaxError (yyToken);
1579    $$ = ParametersCompiled.EmptyReadOnlyParameters;
1580    }
1581  ;
1582
1583fixed_parameters
1584  : fixed_parameter
1585    {
1586    parameters_bucket.Clear ();
1587    Parameter p = (Parameter) $1;
1588    parameters_bucket.Add (p);
1589    parameterListCommas.Clear ();
1590    default_parameter_used = p.HasDefaultValue;
1591    $$ = parameters_bucket;
1592    }
1593  | fixed_parameters COMMA fixed_parameter
1594    {
1595    var pars = (List<Parameter>) $1;
1596    Parameter p = (Parameter) $3;
1597    if (p != null) {
1598      if (p.HasExtensionMethodModifier)
1599        report.Error (1100, p.Location, "The parameter modifier `this' can only be used on the first parameter");
1600      else if (!p.HasDefaultValue && default_parameter_used)
1601        report.Error (1737, p.Location, "Optional parameter cannot precede required parameters");
1602
1603      default_parameter_used |= p.HasDefaultValue;
1604      pars.Add (p);
1605     
1606      parameterListCommas.Add (GetLocation ($2));
1607    }
1608   
1609    $$ = $1;
1610    }
1611  ;
1612
1613fixed_parameter
1614  : opt_attributes
1615    opt_parameter_modifier
1616    parameter_type
1617    identifier_inside_body
1618    {
1619    var lt = (LocatedToken) $4;
1620    $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1621    lbag.AddLocation ($$, parameterModifierLocation);
1622    }
1623  | opt_attributes
1624    opt_parameter_modifier
1625    parameter_type
1626    identifier_inside_body OPEN_BRACKET CLOSE_BRACKET
1627    {
1628    var lt = (LocatedToken) $4;
1629    report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1630    $$ = new Parameter ((FullNamedExpression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1631    lbag.AddLocation ($$, parameterModifierLocation);
1632    }
1633  | attribute_sections error
1634    {
1635    Error_SyntaxError (yyToken);
1636      Location l = GetLocation ($2);
1637    $$ = new Parameter (null, null, Parameter.Modifier.NONE, (Attributes) $1, l);
1638    }
1639  | opt_attributes
1640    opt_parameter_modifier
1641    parameter_type
1642    error
1643    {
1644    Error_SyntaxError (yyToken);
1645      Location l = GetLocation ($4);
1646    $$ = new Parameter ((FullNamedExpression) $3, null, (Parameter.Modifier) $2, (Attributes) $1, l);
1647    lbag.AddLocation ($$, parameterModifierLocation);
1648    }
1649  | opt_attributes
1650    opt_parameter_modifier
1651    parameter_type
1652    identifier_inside_body
1653    ASSIGN
1654    {
1655      ++lexer.parsing_block;
1656    }
1657    constant_expression
1658    {
1659      --lexer.parsing_block;
1660    if (lang_version <= LanguageVersion.V_3) {
1661      FeatureIsNotAvailable (GetLocation ($5), "optional parameter");
1662    }
1663   
1664    Parameter.Modifier mod = (Parameter.Modifier) $2;
1665    if (mod != Parameter.Modifier.NONE) {
1666      switch (mod) {
1667      case Parameter.Modifier.REF:
1668      case Parameter.Modifier.OUT:
1669        report.Error (1741, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1670          Parameter.GetModifierSignature (mod));
1671        break;
1672       
1673      case Parameter.Modifier.This:
1674        report.Error (1743, GetLocation ($2), "Cannot specify a default value for the `{0}' parameter",
1675          Parameter.GetModifierSignature (mod));
1676        break;
1677      default:
1678        throw new NotImplementedException (mod.ToString ());
1679      }
1680       
1681      mod = Parameter.Modifier.NONE;
1682    }
1683   
1684    if ((valid_param_mod & ParameterModifierType.DefaultValue) == 0)
1685      report.Error (1065, GetLocation ($5), "Optional parameter is not valid in this context");
1686   
1687    var lt = (LocatedToken) $4;
1688    $$ = new Parameter ((FullNamedExpression) $3, lt.Value, mod, (Attributes) $1, lt.Location);
1689    lbag.AddLocation ($$, parameterModifierLocation, GetLocation ($5)); // parameterModifierLocation should be ignored when mod == NONE
1690   
1691    if ($7 != null)
1692      ((Parameter) $$).DefaultValue = new DefaultParameterValueExpression ((Expression) $7);
1693    }
1694  ;
1695
1696opt_parameter_modifier
1697  : /* empty */   { $$ = Parameter.Modifier.NONE; }
1698  | parameter_modifiers
1699  ;
1700
1701parameter_modifiers
1702  : parameter_modifier
1703    {
1704    $$ = $1;
1705    }
1706  | parameter_modifiers parameter_modifier
1707    {
1708    Parameter.Modifier p2 = (Parameter.Modifier)$2;
1709      Parameter.Modifier mod = (Parameter.Modifier)$1 | p2;
1710      if (((Parameter.Modifier)$1 & p2) == p2) {
1711        Error_DuplicateParameterModifier (lexer.Location, p2);
1712      } else {
1713        switch (mod & ~Parameter.Modifier.This) {
1714          case Parameter.Modifier.REF:
1715          report.Error (1101, lexer.Location, "The parameter modifiers `this' and `ref' cannot be used altogether");
1716            break;
1717          case Parameter.Modifier.OUT:
1718          report.Error (1102, lexer.Location, "The parameter modifiers `this' and `out' cannot be used altogether");
1719            break;
1720          default:
1721          report.Error (1108, lexer.Location, "A parameter cannot have specified more than one modifier");
1722          break;
1723      }
1724      }
1725      $$ = mod;
1726    }
1727  ;
1728
1729parameter_modifier
1730  : REF
1731    {
1732      if ((valid_param_mod & ParameterModifierType.Ref) == 0)
1733        Error_ParameterModifierNotValid ("ref", GetLocation ($1));
1734    parameterModifierLocation = GetLocation ($1);
1735      $$ = Parameter.Modifier.REF;
1736    }
1737  | OUT
1738    {
1739      if ((valid_param_mod & ParameterModifierType.Out) == 0)
1740        Error_ParameterModifierNotValid ("out", GetLocation ($1));
1741    parameterModifierLocation = GetLocation ($1);
1742      $$ = Parameter.Modifier.OUT;
1743    }
1744  | THIS
1745    {
1746    if ((valid_param_mod & ParameterModifierType.This) == 0)
1747        Error_ParameterModifierNotValid ("this", GetLocation ($1));
1748
1749      if (lang_version <= LanguageVersion.ISO_2)
1750        FeatureIsNotAvailable (GetLocation ($1), "extension methods");
1751    parameterModifierLocation = GetLocation ($1);
1752    $$ = Parameter.Modifier.This;
1753    }
1754  ;
1755
1756parameter_array
1757  : opt_attributes params_modifier type IDENTIFIER
1758    {
1759    var lt = (LocatedToken) $4;
1760    $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);
1761    lbag.AddLocation ($$, savedLocation);
1762    }
1763  | opt_attributes params_modifier type IDENTIFIER ASSIGN constant_expression
1764    {
1765    report.Error (1751, GetLocation ($2), "Cannot specify a default value for a parameter array");
1766   
1767    var lt = (LocatedToken) $4;
1768    $$ = new ParamsParameter ((FullNamedExpression) $3, lt.Value, (Attributes) $1, lt.Location);   
1769    lbag.AddLocation ($$, savedLocation);
1770    }
1771  | opt_attributes params_modifier type error
1772    {
1773    Error_SyntaxError (yyToken);
1774
1775    $$ = new ParamsParameter ((FullNamedExpression) $3, null, (Attributes) $1, Location.Null);
1776    }
1777  ;
1778 
1779params_modifier
1780  : PARAMS
1781    {
1782    if ((valid_param_mod & ParameterModifierType.Params) == 0)
1783      report.Error (1670, (GetLocation ($1)), "The `params' modifier is not allowed in current context");
1784    savedLocation = GetLocation ($1);
1785    }
1786  | PARAMS parameter_modifier
1787    {
1788    Parameter.Modifier mod = (Parameter.Modifier)$2;
1789    if ((mod & Parameter.Modifier.This) != 0) {
1790      report.Error (1104, GetLocation ($1), "The parameter modifiers `this' and `params' cannot be used altogether");
1791    } else {
1792      report.Error (1611, GetLocation ($1), "The params parameter cannot be declared as ref or out");
1793    }   
1794    savedLocation = GetLocation ($1);
1795    }
1796  | PARAMS params_modifier
1797    {
1798    Error_DuplicateParameterModifier (GetLocation ($1), Parameter.Modifier.PARAMS);
1799    }
1800  ;
1801 
1802arglist_modifier
1803  : ARGLIST
1804    {
1805      if ((valid_param_mod & ParameterModifierType.Arglist) == 0)
1806        report.Error (1669, GetLocation ($1), "__arglist is not valid in this context");
1807    }
1808  ;
1809
1810property_declaration
1811  : opt_attributes
1812    opt_modifiers
1813    member_type
1814    member_declaration_name
1815    {
1816    lexer.parsing_generic_declaration = false;
1817    if (doc_support)
1818      tmpComment = Lexer.consume_doc_comment ();
1819    }
1820    OPEN_BRACE
1821    {
1822    var type = (FullNamedExpression) $3;
1823    current_property = new Property (current_type, type, (Modifiers) $2,
1824      (MemberName) $4, (Attributes) $1);
1825     
1826    if (type.Type != null && type.Type.Kind == MemberKind.Void)
1827      report.Error (547, GetLocation ($3), "`{0}': property or indexer cannot have void type", current_property.GetSignatureForError ());         
1828     
1829    current_type.AddMember (current_property);
1830    lbag.AddMember (current_property, GetModifierLocations (), GetLocation ($6));
1831   
1832    lexer.PropertyParsing = true;
1833    }
1834    accessor_declarations
1835    {
1836    lexer.PropertyParsing = false;
1837   
1838    if (doc_support)
1839      current_property.DocComment = ConsumeStoredComment ();       
1840    }
1841    CLOSE_BRACE
1842    {
1843    lbag.AppendToMember (current_property, GetLocation ($10));
1844    lexer.parsing_modifiers = true;
1845    }
1846    opt_property_initializer
1847    {
1848    current_property = null;
1849    }
1850  | opt_attributes
1851    opt_modifiers
1852    member_type
1853    member_declaration_name
1854    {
1855    lexer.parsing_generic_declaration = false;
1856    if (doc_support)
1857      tmpComment = Lexer.consume_doc_comment ();
1858    current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
1859    }
1860    expression_block
1861    {
1862    var type = (FullNamedExpression) $3;
1863    var property = new Property (current_type, type, (Modifiers) $2,
1864      (MemberName) $4, (Attributes) $1);
1865
1866    property.Get = new Property.GetMethod (property, Modifiers.COMPILER_GENERATED, null, property.Location);
1867    property.Get.Block = (ToplevelBlock) $6;
1868
1869    if (current_container.Kind == MemberKind.Interface) {
1870      report.Error (531, property.Get.Block.StartLocation,
1871        "`{0}': interface members cannot have a definition", property.GetSignatureForError ());
1872    }
1873
1874    if (type.Type != null && type.Type.Kind == MemberKind.Void)
1875      report.Error (547, GetLocation ($3), "`{0}': property or indexer cannot have void type", property.GetSignatureForError ());
1876
1877    current_type.AddMember (property);
1878
1879    current_local_parameters = null;
1880    }
1881  ;
1882
1883opt_property_initializer
1884  : /* empty */
1885  | ASSIGN
1886    {
1887    ++lexer.parsing_block;
1888    current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
1889    start_block (GetLocation ($1));
1890    }
1891    property_initializer SEMICOLON
1892    {
1893    --lexer.parsing_block;
1894    ((Property)current_property).Initializer = (Expression) $3;
1895    lbag.AppendToMember (current_property, GetLocation ($1), GetLocation ($4));
1896    end_block (GetLocation ($4));
1897    current_local_parameters = null;
1898    }
1899  ;
1900
1901property_initializer
1902  : expression
1903  | array_initializer
1904  ;
1905
1906indexer_declaration
1907  : opt_attributes opt_modifiers
1908    member_type indexer_declaration_name OPEN_BRACKET
1909    {
1910      valid_param_mod = ParameterModifierType.Params | ParameterModifierType.DefaultValue;
1911    }
1912    opt_formal_parameter_list CLOSE_BRACKET
1913    {
1914    valid_param_mod = 0;
1915    var type = (FullNamedExpression) $3;
1916    Indexer indexer = new Indexer (current_type, type, (MemberName) $4, (Modifiers) $2, (ParametersCompiled) $7, (Attributes) $1);
1917     
1918    current_property = indexer;
1919
1920      current_type.AddIndexer (indexer);
1921    lbag.AddMember (current_property, GetModifierLocations (), GetLocation ($5), GetLocation ($8));
1922     
1923    if (type.Type != null && type.Type.Kind == MemberKind.Void)
1924      report.Error (620, GetLocation ($3), "`{0}': indexer return type cannot be `void'", indexer.GetSignatureForError ());     
1925
1926    if (indexer.ParameterInfo.IsEmpty) {
1927      report.Error (1551, GetLocation ($5), "Indexers must have at least one parameter");
1928    }
1929
1930    if (doc_support) {
1931      tmpComment = Lexer.consume_doc_comment ();
1932      Lexer.doc_state = XmlCommentState.Allowed;
1933    }
1934
1935    lexer.PropertyParsing = true;
1936    current_local_parameters = (ParametersCompiled) $7;
1937    }
1938    indexer_body
1939    {
1940    lexer.PropertyParsing = false;
1941    current_local_parameters = null;
1942
1943    if (current_property.AccessorFirst != null && current_property.AccessorFirst.Block == null)
1944      ((Indexer) current_property).ParameterInfo.CheckParameters (current_property);
1945   
1946    if (doc_support)
1947      current_property.DocComment = ConsumeStoredComment ();
1948     
1949    current_property = null;   
1950    }
1951  ;
1952
1953indexer_body
1954  : OPEN_BRACE accessor_declarations CLOSE_BRACE
1955    {
1956    lbag.AppendToMember (current_property, GetLocation ($1), GetLocation ($3));
1957    }
1958  | expression_block
1959    {
1960    current_property.Get = new Indexer.GetIndexerMethod (current_property, Modifiers.COMPILER_GENERATED, current_local_parameters, null, current_property.Location);
1961    current_property.Get.Block = (ToplevelBlock) $1;
1962    }
1963  ;
1964
1965accessor_declarations
1966  : get_accessor_declaration
1967  | get_accessor_declaration accessor_declarations
1968  | set_accessor_declaration
1969  | set_accessor_declaration accessor_declarations
1970  | error
1971    {
1972      if (yyToken == Token.CLOSE_BRACE) {
1973        report.Error (548, lexer.Location, "`{0}': property or indexer must have at least one accessor", current_property.GetSignatureForError ());
1974    } else {
1975      if (yyToken == Token.SEMICOLON)
1976        report.Error (1597, lexer.Location, "Semicolon after method or accessor block is not valid");
1977      else
1978        report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1979    }
1980    }
1981  ;
1982
1983get_accessor_declaration
1984  : opt_attributes opt_modifiers GET
1985    {
1986    if ($2 != ModifierNone && lang_version == LanguageVersion.ISO_1) {
1987      FeatureIsNotAvailable (GetLocation ($2), "access modifiers on properties");
1988    }
1989   
1990    if (current_property.Get != null) {
1991      report.Error (1007, GetLocation ($3), "Property accessor already defined");
1992    }
1993   
1994    if (current_property is Indexer) {
1995      current_property.Get = new Indexer.GetIndexerMethod (current_property, (Modifiers) $2, ((Indexer)current_property).ParameterInfo.Clone (),
1996        (Attributes) $1, GetLocation ($3));
1997    } else {
1998      current_property.Get = new Property.GetMethod (current_property,
1999        (Modifiers) $2, (Attributes) $1, GetLocation ($3));
2000    }
2001   
2002    current_local_parameters = current_property.Get.ParameterInfo;   
2003    lexer.PropertyParsing = false;
2004    }
2005    accessor_body
2006    {
2007      if ($5 != null) {
2008        current_property.Get.Block = (ToplevelBlock) $5;     
2009     
2010      if (current_container.Kind == MemberKind.Interface) {
2011        report.Error (531, current_property.Get.Block.StartLocation,
2012          "`{0}': interface members cannot have a definition", current_property.Get.GetSignatureForError ());
2013      }
2014      lbag.AddMember (current_property.Get, GetModifierLocations ());
2015    } else {
2016      lbag.AddMember (current_property.Get, GetModifierLocations (), savedLocation);
2017    }
2018   
2019    current_local_parameters = null;
2020    lexer.PropertyParsing = true;
2021
2022    if (doc_support)
2023      if (Lexer.doc_state == XmlCommentState.Error)
2024        Lexer.doc_state = XmlCommentState.NotAllowed;
2025    }
2026  ;
2027
2028set_accessor_declaration
2029  : opt_attributes opt_modifiers SET
2030    {
2031    if ($2 != ModifierNone && lang_version == LanguageVersion.ISO_1) {
2032      FeatureIsNotAvailable (GetLocation ($2), "access modifiers on properties");
2033    }
2034   
2035    if (current_property.Set != null) {
2036      report.Error (1007, GetLocation ($3), "Property accessor already defined");
2037    }
2038   
2039    if (current_property is Indexer) {
2040      current_property.Set = new Indexer.SetIndexerMethod (current_property, (Modifiers) $2,
2041        ParametersCompiled.MergeGenerated (compiler,
2042        ((Indexer)current_property).ParameterInfo, true, new Parameter (
2043          current_property.TypeExpression, "value", Parameter.Modifier.NONE, null, GetLocation ($3)),
2044          null),
2045        (Attributes) $1, GetLocation ($3));
2046    } else {
2047      current_property.Set = new Property.SetMethod (current_property, (Modifiers) $2,
2048        ParametersCompiled.CreateImplicitParameter (current_property.TypeExpression, GetLocation ($3)),
2049        (Attributes) $1, GetLocation ($3));
2050    }
2051   
2052    current_local_parameters = current_property.Set.ParameterInfo; 
2053    lexer.PropertyParsing = false;
2054    }
2055    accessor_body
2056    {
2057    if ($5 != null) {   
2058      current_property.Set.Block = (ToplevelBlock) $5;
2059   
2060      if (current_container.Kind == MemberKind.Interface) {
2061        report.Error (531, current_property.Set.Block.StartLocation,
2062          "`{0}': interface members cannot have a definition", current_property.Set.GetSignatureForError ());
2063      }
2064      lbag.AddMember (current_property.Set, GetModifierLocations ());
2065    } else {
2066      lbag.AddMember (current_property.Set, GetModifierLocations (), savedLocation);
2067    }
2068   
2069    current_local_parameters = null;
2070    lexer.PropertyParsing = true;
2071
2072    if (doc_support
2073      && Lexer.doc_state == XmlCommentState.Error)
2074      Lexer.doc_state = XmlCommentState.NotAllowed;
2075    }
2076  ;
2077
2078accessor_body
2079  : block
2080  | SEMICOLON
2081    {
2082    savedLocation = GetLocation ($1);
2083      $$ = null;
2084    }
2085  | error
2086    {
2087      Error_SyntaxError (1043, yyToken, "Invalid accessor body");
2088      $$ = null;
2089    }
2090  ;
2091
2092interface_declaration
2093  : opt_attributes
2094    opt_modifiers
2095    opt_partial
2096    INTERFACE
2097    {
2098    }
2099    type_declaration_name
2100    {
2101    lexer.ConstraintsParsing = true;
2102    push_current_container (new Interface (current_container, (MemberName) $6, (Modifiers) $2, (Attributes) $1), $3);
2103    lbag.AddMember (current_container, GetModifierLocations (), GetLocation ($4));   
2104    }
2105    opt_class_base
2106    opt_type_parameter_constraints_clauses
2107    {
2108    lexer.ConstraintsParsing = false;
2109
2110    if ($9 != null)
2111      current_container.SetConstraints ((List<Constraints>) $9);
2112
2113    if (doc_support) {
2114      current_container.PartialContainer.DocComment = Lexer.consume_doc_comment ();
2115      Lexer.doc_state = XmlCommentState.Allowed;
2116    }
2117   
2118    lexer.parsing_modifiers = true;
2119    }
2120    OPEN_BRACE opt_interface_member_declarations CLOSE_BRACE
2121    {
2122    --lexer.parsing_declaration;   
2123    if (doc_support)
2124      Lexer.doc_state = XmlCommentState.Allowed;
2125    }
2126    opt_semicolon
2127    {
2128    if ($15 == null) {
2129      lbag.AppendToMember (current_container, GetLocation ($11), GetLocation ($13));
2130    } else {
2131      lbag.AppendToMember (current_container, GetLocation ($11), GetLocation ($13), GetLocation ($15));
2132    }
2133    $$ = pop_current_class ();
2134    }
2135  | opt_attributes opt_modifiers opt_partial INTERFACE error
2136    {
2137    Error_SyntaxError (yyToken);   
2138    }
2139  ;
2140
2141opt_interface_member_declarations
2142  : /* empty */
2143  | interface_member_declarations
2144  ;
2145
2146interface_member_declarations
2147  : interface_member_declaration
2148    {
2149    lexer.parsing_modifiers = true;
2150    lexer.parsing_block = 0;
2151    }
2152  | interface_member_declarations interface_member_declaration
2153    {
2154    lexer.parsing_modifiers = true;
2155    lexer.parsing_block = 0;
2156    }
2157  ;
2158
2159interface_member_declaration
2160  : constant_declaration
2161    {
2162    report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
2163    }
2164  | field_declaration
2165    {
2166    report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
2167    }
2168  | method_declaration
2169  | property_declaration
2170  | event_declaration
2171  | indexer_declaration
2172  | operator_declaration
2173    {
2174      report.Error (567, GetLocation ($1), "Interfaces cannot contain operators");
2175    }
2176  | constructor_declaration
2177    {
2178      report.Error (526, GetLocation ($1), "Interfaces cannot contain contructors");
2179    }
2180  | type_declaration
2181    {
2182      report.Error (524, GetLocation ($1), "Interfaces cannot declare classes, structs, interfaces, delegates, or enumerations");
2183    }
2184  ;
2185
2186operator_declaration
2187  : opt_attributes opt_modifiers operator_declarator
2188    {
2189    }
2190    method_body_expression_block
2191    {
2192    OperatorDeclaration decl = (OperatorDeclaration) $3;
2193    if (decl != null) {
2194      Operator op = new Operator (
2195        current_type, decl.optype, decl.ret_type, (Modifiers) $2,
2196        current_local_parameters,
2197        (ToplevelBlock) $5, (Attributes) $1, decl.location);
2198       
2199      if (op.Block == null)
2200        op.ParameterInfo.CheckParameters (op);
2201
2202      if (doc_support) {
2203        op.DocComment = tmpComment;
2204        Lexer.doc_state = XmlCommentState.Allowed;
2205      }
2206
2207      // Note again, checking is done in semantic analysis
2208      current_type.AddOperator (op);
2209
2210      lbag.AddMember (op, GetModifierLocations (), lbag.GetLocations (decl));
2211      if ($5 == null) { // Semicolon
2212        lbag.AddLocation (op, savedLocation);
2213      }
2214    }
2215   
2216    current_local_parameters = null;
2217    }
2218  ;
2219
2220operator_type
2221  : type_expression_or_array
2222  | VOID
2223    {
2224    report.Error (590, GetLocation ($1), "User-defined operators cannot return void");
2225    $$ = new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1));
2226    }
2227  ;
2228
2229operator_declarator
2230  : operator_type OPERATOR overloadable_operator OPEN_PARENS
2231    {
2232    valid_param_mod = ParameterModifierType.DefaultValue;
2233    }
2234    opt_formal_parameter_list CLOSE_PARENS
2235    {
2236    valid_param_mod = 0;
2237
2238    Location loc = GetLocation ($2);
2239    Operator.OpType op = (Operator.OpType) $3;
2240    current_local_parameters = (ParametersCompiled)$6;
2241   
2242    int p_count = current_local_parameters.Count;
2243    if (p_count == 1) {
2244      if (op == Operator.OpType.Addition)
2245        op = Operator.OpType.UnaryPlus;
2246      else if (op == Operator.OpType.Subtraction)
2247        op = Operator.OpType.UnaryNegation;
2248    }
2249   
2250    if (IsUnaryOperator (op)) {
2251      if (p_count == 2) {
2252        report.Error (1020, loc, "Overloadable binary operator expected");
2253      } else if (p_count != 1) {
2254        report.Error (1535, loc, "Overloaded unary operator `{0}' takes one parameter",
2255          Operator.GetName (op));
2256      }
2257    } else {
2258      if (p_count == 1) {
2259        report.Error (1019, loc, "Overloadable unary operator expected");
2260      } else if (p_count != 2) {
2261        report.Error (1534, loc, "Overloaded binary operator `{0}' takes two parameters",
2262          Operator.GetName (op));
2263      }
2264    }
2265   
2266    if (doc_support) {
2267      tmpComment = Lexer.consume_doc_comment ();
2268      Lexer.doc_state = XmlCommentState.NotAllowed;
2269    }
2270
2271    $$ = new OperatorDeclaration (op, (FullNamedExpression) $1, loc);
2272    lbag.AddLocation ($$, GetLocation ($2), savedOperatorLocation, GetLocation ($4), GetLocation ($7));
2273    }
2274  | conversion_operator_declarator
2275  ;
2276
2277overloadable_operator
2278// Unary operators:
2279  : BANG   { $$ = Operator.OpType.LogicalNot; savedOperatorLocation = GetLocation ($1); }
2280        | TILDE  { $$ = Operator.OpType.OnesComplement; savedOperatorLocation = GetLocation ($1); } 
2281        | OP_INC { $$ = Operator.OpType.Increment; savedOperatorLocation = GetLocation ($1); }
2282        | OP_DEC { $$ = Operator.OpType.Decrement; savedOperatorLocation = GetLocation ($1); }
2283        | TRUE   { $$ = Operator.OpType.True; savedOperatorLocation = GetLocation ($1); }
2284        | FALSE  { $$ = Operator.OpType.False; savedOperatorLocation = GetLocation ($1); }
2285// Unary and binary:
2286        | PLUS { $$ = Operator.OpType.Addition; savedOperatorLocation = GetLocation ($1); }
2287        | MINUS { $$ = Operator.OpType.Subtraction; savedOperatorLocation = GetLocation ($1); }
2288// Binary:
2289        | STAR { $$ = Operator.OpType.Multiply; savedOperatorLocation = GetLocation ($1); }
2290        | DIV {  $$ = Operator.OpType.Division; savedOperatorLocation = GetLocation ($1); }
2291        | PERCENT { $$ = Operator.OpType.Modulus; savedOperatorLocation = GetLocation ($1); }
2292        | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; savedOperatorLocation = GetLocation ($1); }
2293        | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; savedOperatorLocation = GetLocation ($1); }
2294        | CARRET { $$ = Operator.OpType.ExclusiveOr; savedOperatorLocation = GetLocation ($1); }
2295        | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; savedOperatorLocation = GetLocation ($1); }
2296        | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; savedOperatorLocation = GetLocation ($1); }
2297        | OP_EQ { $$ = Operator.OpType.Equality; savedOperatorLocation = GetLocation ($1); }
2298        | OP_NE { $$ = Operator.OpType.Inequality; savedOperatorLocation = GetLocation ($1); }
2299        | OP_GT { $$ = Operator.OpType.GreaterThan; savedOperatorLocation = GetLocation ($1); }
2300        | OP_LT { $$ = Operator.OpType.LessThan; savedOperatorLocation = GetLocation ($1); }
2301        | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; savedOperatorLocation = GetLocation ($1); }
2302        | OP_LE { $$ = Operator.OpType.LessThanOrEqual; savedOperatorLocation = GetLocation ($1); }
2303  ;
2304
2305conversion_operator_declarator
2306  : IMPLICIT OPERATOR type OPEN_PARENS
2307    {
2308    valid_param_mod = ParameterModifierType.DefaultValue;
2309    }
2310    opt_formal_parameter_list CLOSE_PARENS
2311    {
2312    valid_param_mod = 0;
2313
2314    Location loc = GetLocation ($2);
2315    current_local_parameters = (ParametersCompiled)$6; 
2316
2317    if (current_local_parameters.Count != 1) {
2318      report.Error (1535, loc, "Overloaded unary operator `implicit' takes one parameter");
2319    }
2320
2321    if (doc_support) {
2322      tmpComment = Lexer.consume_doc_comment ();
2323      Lexer.doc_state = XmlCommentState.NotAllowed;
2324    }
2325
2326    $$ = new OperatorDeclaration (Operator.OpType.Implicit, (FullNamedExpression) $3, loc);
2327    lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2), GetLocation ($4), GetLocation ($7));
2328    }
2329  | EXPLICIT OPERATOR type OPEN_PARENS
2330    {
2331    valid_param_mod = ParameterModifierType.DefaultValue;
2332    }
2333    opt_formal_parameter_list CLOSE_PARENS
2334    {
2335    valid_param_mod = 0;
2336   
2337    Location loc = GetLocation ($2);
2338    current_local_parameters = (ParametersCompiled)$6; 
2339
2340    if (current_local_parameters.Count != 1) {
2341      report.Error (1535, loc, "Overloaded unary operator `explicit' takes one parameter");
2342    }
2343
2344    if (doc_support) {
2345      tmpComment = Lexer.consume_doc_comment ();
2346      Lexer.doc_state = XmlCommentState.NotAllowed;
2347    }
2348
2349    $$ = new OperatorDeclaration (Operator.OpType.Explicit, (FullNamedExpression) $3, loc);
2350    lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2), GetLocation ($4), GetLocation ($7));
2351    }
2352  | IMPLICIT error
2353    {
2354      Error_SyntaxError (yyToken);
2355    current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2356    $$ = new OperatorDeclaration (Operator.OpType.Implicit, null, GetLocation ($1));
2357    }
2358  | EXPLICIT error
2359    {
2360      Error_SyntaxError (yyToken);
2361    current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2362      $$ = new OperatorDeclaration (Operator.OpType.Explicit, null, GetLocation ($1));
2363    }
2364  ;
2365
2366constructor_declaration
2367  : constructor_declarator
2368    constructor_body
2369    {
2370    Constructor c = (Constructor) $1;
2371    c.Block = (ToplevelBlock) $2;
2372   
2373    if (doc_support)
2374      c.DocComment = ConsumeStoredComment ();
2375
2376    current_local_parameters = null;
2377    if (doc_support)
2378      Lexer.doc_state = XmlCommentState.Allowed;
2379    }
2380  ;
2381
2382constructor_declarator
2383  : opt_attributes
2384    opt_modifiers
2385    IDENTIFIER
2386    {
2387    if (doc_support) {
2388      tmpComment = Lexer.consume_doc_comment ();
2389      Lexer.doc_state = XmlCommentState.Allowed;
2390    }
2391   
2392    valid_param_mod = ParameterModifierType.All;
2393    }
2394    OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2395    {
2396    valid_param_mod = 0;
2397    current_local_parameters = (ParametersCompiled) $6;
2398   
2399    var lt = (LocatedToken) $3;
2400    var mods = (Modifiers) $2;
2401    var c = new Constructor (current_type, lt.Value, mods, (Attributes) $1, current_local_parameters, lt.Location);
2402
2403    if (lt.Value != current_container.MemberName.Name) {
2404      report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
2405    } else if ((mods & Modifiers.STATIC) != 0) {
2406      if ((mods & Modifiers.AccessibilityMask) != 0){
2407        report.Error (515, c.Location,
2408          "`{0}': static constructor cannot have an access modifier",
2409          c.GetSignatureForError ());
2410      }
2411    }
2412
2413    current_type.AddConstructor (c);
2414    lbag.AddMember (c, GetModifierLocations (), GetLocation ($5), GetLocation ($7));
2415    $$ = c;
2416
2417    //
2418    // start block here, so possible anonymous methods inside
2419    // constructor initializer can get correct parent block
2420    //
2421      start_block (lexer.Location);
2422    }
2423    opt_constructor_initializer
2424    {
2425    if ($9 != null) {
2426      var c = (Constructor) $8;
2427      c.Initializer = (ConstructorInitializer) $9;
2428     
2429      if (c.IsStatic) {
2430        report.Error (514, c.Location,
2431          "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2432          c.GetSignatureForError ());
2433      }
2434    }
2435
2436    $$ = $8;
2437    }
2438  ;
2439
2440constructor_body
2441  : block_prepared
2442  | SEMICOLON     { current_block = null; $$ = null; }
2443  ;
2444
2445opt_constructor_initializer
2446  : /* Empty */
2447  | constructor_initializer
2448  ;
2449
2450constructor_initializer
2451  : COLON BASE OPEN_PARENS
2452    {
2453    ++lexer.parsing_block;
2454    }
2455    opt_argument_list CLOSE_PARENS
2456    {
2457      --lexer.parsing_block;
2458    $$ = new ConstructorBaseInitializer ((Arguments) $5, GetLocation ($2));
2459    lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3), GetLocation ($6));
2460    }
2461  | COLON THIS OPEN_PARENS
2462    {
2463    ++lexer.parsing_block;
2464    }
2465    opt_argument_list CLOSE_PARENS
2466    {
2467      --lexer.parsing_block;
2468    $$ = new ConstructorThisInitializer ((Arguments) $5, GetLocation ($2));
2469    lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3), GetLocation ($6));
2470    }
2471  | COLON error
2472    {
2473    Error_SyntaxError (yyToken);   
2474    $$ = new ConstructorThisInitializer (null, GetLocation ($2));
2475    lbag.AddLocation ($$, GetLocation ($1));
2476    }
2477  | error
2478    {
2479    Error_SyntaxError (yyToken);
2480    $$ = null;
2481    }
2482  ;
2483
2484destructor_declaration
2485  : opt_attributes opt_modifiers TILDE
2486    {
2487    if (doc_support) {
2488      tmpComment = Lexer.consume_doc_comment ();
2489      Lexer.doc_state = XmlCommentState.NotAllowed;
2490    }
2491   
2492    current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters;
2493    }
2494    IDENTIFIER OPEN_PARENS CLOSE_PARENS method_body
2495    {
2496    var lt = (LocatedToken) $5;
2497    if (lt.Value != current_container.MemberName.Name){
2498      report.Error (574, lt.Location, "Name of destructor must match name of class");
2499    } else if (current_container.Kind != MemberKind.Class){
2500      report.Error (575, lt.Location, "Only class types can contain destructor");
2501    }
2502   
2503    Destructor d = new Destructor (current_type, (Modifiers) $2,
2504      ParametersCompiled.EmptyReadOnlyParameters, (Attributes) $1, lt.Location);
2505    d.Identifier = lt.Value;
2506    if (doc_support)
2507      d.DocComment = ConsumeStoredComment ();
2508     
2509    d.Block = (ToplevelBlock) $8;
2510    current_type.AddMember (d);
2511    lbag.AddMember (d, GetModifierLocations (), GetLocation ($3), GetLocation ($6), GetLocation ($7));
2512
2513    current_local_parameters = null;
2514    }
2515  ;
2516
2517event_declaration
2518  : opt_attributes
2519    opt_modifiers
2520    EVENT type member_declaration_name
2521    {
2522    current_event_field = new EventField (current_type, (FullNamedExpression) $4, (Modifiers) $2, (MemberName) $5, (Attributes) $1);
2523    current_type.AddMember (current_event_field);
2524   
2525    if (current_event_field.MemberName.ExplicitInterface != null) {
2526      report.Error (71, current_event_field.Location, "`{0}': An explicit interface implementation of an event must use property syntax",
2527      current_event_field.GetSignatureForError ());
2528    }
2529   
2530    $$ = current_event_field;
2531    }
2532    opt_event_initializer
2533    opt_event_declarators
2534    SEMICOLON
2535    {
2536    if (doc_support) {
2537      current_event_field.DocComment = Lexer.consume_doc_comment ();
2538      Lexer.doc_state = XmlCommentState.Allowed;
2539    }
2540    if (current_event_field.Initializer != null) {
2541      lbag.AddMember (current_event_field, GetModifierLocations (), GetLocation ($3), savedEventAssignLocation, GetLocation ($9));
2542    } else {
2543      lbag.AddMember (current_event_field, GetModifierLocations (), GetLocation ($3), GetLocation ($9));
2544    }
2545    current_event_field = null;
2546    }
2547  | opt_attributes
2548    opt_modifiers
2549    EVENT type member_declaration_name
2550    OPEN_BRACE
2551    {
2552    current_event = new EventProperty (current_type, (FullNamedExpression) $4, (Modifiers) $2, (MemberName) $5, (Attributes) $1);
2553    current_type.AddMember (current_event);
2554    lbag.AddMember (current_event, GetModifierLocations (), GetLocation ($3), GetLocation ($6));
2555   
2556    lexer.EventParsing = true;
2557    }
2558    event_accessor_declarations
2559    {
2560    if (current_container.Kind == MemberKind.Interface)
2561      report.Error (69, GetLocation ($6), "Event in interface cannot have add or remove accessors");
2562   
2563    lexer.EventParsing = false;
2564    }
2565    CLOSE_BRACE
2566    {
2567    if (doc_support) {
2568      current_event.DocComment = Lexer.consume_doc_comment ();
2569      Lexer.doc_state = XmlCommentState.Allowed;
2570    }
2571   
2572    lbag.AppendToMember (current_event, GetLocation ($9));
2573      current_event = null;
2574    current_local_parameters = null;
2575    }
2576  | opt_attributes
2577    opt_modifiers
2578    EVENT type error
2579    {
2580    Error_SyntaxError (yyToken);
2581
2582    current_type.AddMember (new EventField (current_type, (FullNamedExpression) $4, (Modifiers) $2, MemberName.Null, (Attributes) $1));
2583    }
2584  ;
2585 
2586opt_event_initializer
2587  : /* empty */
2588  | ASSIGN
2589    {
2590      ++lexer.parsing_block;
2591    }
2592    event_variable_initializer
2593    {
2594      --lexer.parsing_block;
2595      savedEventAssignLocation = GetLocation ($1);
2596    current_event_field.Initializer = (Expression) $3;
2597    }
2598  ;
2599 
2600opt_event_declarators
2601  : /* empty */
2602  | event_declarators
2603  ;
2604 
2605event_declarators
2606  : event_declarator
2607    {
2608    current_event_field.AddDeclarator ((FieldDeclarator) $1);
2609    }
2610  | event_declarators event_declarator
2611    {
2612    current_event_field.AddDeclarator ((FieldDeclarator) $2);
2613    }
2614  ;
2615 
2616event_declarator
2617  : COMMA IDENTIFIER
2618    {
2619    var lt = (LocatedToken) $2;
2620      $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), null);
2621      lbag.AddLocation ($$, GetLocation ($1));
2622    }
2623  | COMMA IDENTIFIER ASSIGN
2624    {
2625    ++lexer.parsing_block;
2626    }
2627    event_variable_initializer
2628    {
2629    --lexer.parsing_block;
2630    var lt = (LocatedToken) $2;   
2631      $$ = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (Expression) $5);
2632      lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
2633    }
2634  ;
2635 
2636event_variable_initializer
2637  : {
2638    if (current_container.Kind == MemberKind.Interface) {
2639      report.Error (68, lexer.Location, "`{0}': event in interface cannot have an initializer",
2640        current_event_field.GetSignatureForError ());
2641    }
2642   
2643      if ((current_event_field.ModFlags & Modifiers.ABSTRACT) != 0) {
2644      report.Error (74, lexer.Location, "`{0}': abstract event cannot have an initializer",
2645        current_event_field.GetSignatureForError ());
2646      }   
2647    }
2648    variable_initializer
2649    {
2650    $$ = $2;
2651    }
2652  ;
2653 
2654event_accessor_declarations
2655  : add_accessor_declaration remove_accessor_declaration
2656  | remove_accessor_declaration add_accessor_declaration
2657  | add_accessor_declaration
2658    {
2659    report.Error (65, lexer.Location, "`{0}': event property must have both add and remove accessors",
2660      current_event.GetSignatureForError ());
2661    }
2662  | remove_accessor_declaration
2663    {
2664    report.Error (65, lexer.Location, "`{0}': event property must have both add and remove accessors",
2665      current_event.GetSignatureForError ());
2666    }
2667  | error
2668    {
2669    report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2670    $$ = null;
2671    }
2672  ;
2673
2674add_accessor_declaration
2675  : opt_attributes opt_modifiers ADD
2676    {
2677      if ($2 != ModifierNone) {
2678      report.Error (1609, GetLocation ($2), "Modifiers cannot be placed on event accessor declarations");
2679      }
2680     
2681      current_event.Add = new EventProperty.AddDelegateMethod (current_event, (Attributes) $1, GetLocation ($3));
2682    current_local_parameters = current_event.Add.ParameterInfo;
2683   
2684    lbag.AddMember (current_event.Add, GetModifierLocations ());
2685    lexer.EventParsing = false;   
2686    }
2687    event_accessor_block
2688    {
2689    lexer.EventParsing = true;
2690   
2691      current_event.Add.Block = (ToplevelBlock) $5;
2692   
2693    if (current_container.Kind == MemberKind.Interface) {
2694      report.Error (531, current_event.Add.Block.StartLocation,
2695        "`{0}': interface members cannot have a definition", current_event.Add.GetSignatureForError ());
2696    }
2697   
2698    current_local_parameters = null;
2699    }
2700  ;
2701 
2702remove_accessor_declaration
2703  : opt_attributes opt_modifiers REMOVE
2704    {
2705      if ($2 != ModifierNone) {
2706      report.Error (1609, GetLocation ($2), "Modifiers cannot be placed on event accessor declarations");
2707      }
2708     
2709      current_event.Remove = new EventProperty.RemoveDelegateMethod (current_event, (Attributes) $1, GetLocation ($3));
2710    current_local_parameters = current_event.Remove.ParameterInfo;
2711
2712    lbag.AddMember (current_event.Remove, GetModifierLocations ());
2713    lexer.EventParsing = false;   
2714    }
2715    event_accessor_block
2716    {
2717    lexer.EventParsing = true;
2718   
2719      current_event.Remove.Block = (ToplevelBlock) $5;
2720   
2721    if (current_container.Kind == MemberKind.Interface) {
2722      report.Error (531, current_event.Remove.Block.StartLocation,
2723        "`{0}': interface members cannot have a definition", current_event.Remove.GetSignatureForError ());
2724    }
2725   
2726    current_local_parameters = null;
2727    }
2728  ;
2729
2730event_accessor_block
2731  : opt_semicolon
2732    {
2733    report.Error (73, lexer.Location, "An add or remove accessor must have a body");
2734    $$ = null;
2735    }
2736  | block;
2737  ;
2738
2739attributes_without_members
2740  : attribute_sections CLOSE_BRACE
2741    {
2742    current_type.UnattachedAttributes = (Attributes) $1;
2743    report.Error (1519, GetLocation ($1), "An attribute is missing member declaration");
2744    lexer.putback ('}');
2745    }
2746  ;
2747
2748// For full ast try to recover incomplete ambiguous member
2749// declaration in form on class X { public int }
2750incomplete_member
2751  : opt_attributes opt_modifiers member_type CLOSE_BRACE
2752    {
2753    report.Error (1519, lexer.Location, "Unexpected symbol `}' in class, struct, or interface member declaration");
2754 
2755    lexer.putback ('}');
2756
2757      lexer.parsing_generic_declaration = false;
2758    FullNamedExpression type = (FullNamedExpression) $3;
2759    current_field = new Field (current_type, type, (Modifiers) $2, MemberName.Null, (Attributes) $1);
2760    current_type.AddField (current_field);
2761    lbag.AddMember (current_field, GetModifierLocations ());
2762    $$ = current_field;
2763    }
2764  ;
2765   
2766enum_declaration
2767  : opt_attributes
2768    opt_modifiers
2769    ENUM
2770    type_declaration_name
2771    opt_enum_base
2772    {
2773    if (doc_support)
2774      enumTypeComment = Lexer.consume_doc_comment ();
2775    }
2776    OPEN_BRACE
2777    {
2778    if (doc_support)
2779      Lexer.doc_state = XmlCommentState.Allowed;
2780
2781    MemberName name = (MemberName) $4;
2782    if (name.IsGeneric) {
2783      report.Error (1675, name.Location, "Enums cannot have type parameters");
2784    }
2785   
2786    push_current_container (new Enum (current_container, (FullNamedExpression) $5, (Modifiers) $2, name, (Attributes) $1), null);
2787    if ($5 != null) {
2788      lbag.AddMember (current_container, GetModifierLocations (), GetLocation ($3), savedLocation, GetLocation ($7));
2789    } else {
2790      lbag.AddMember (current_container, GetModifierLocations (), GetLocation ($3), GetLocation ($7));
2791    }
2792    }
2793    opt_enum_member_declarations
2794    {
2795    lexer.parsing_modifiers = true;
2796   
2797      // here will be evaluated after CLOSE_BLACE is consumed.
2798    if (doc_support)
2799      Lexer.doc_state = XmlCommentState.Allowed;
2800    }
2801    CLOSE_BRACE opt_semicolon
2802    {
2803    lbag.AppendToMember (current_container, GetLocation ($11));
2804    if ($12 != null) {
2805      lbag.AppendToMember (current_container, GetLocation ($12));
2806    }
2807    if (doc_support)
2808      current_container.DocComment = enumTypeComment;
2809     
2810    --lexer.parsing_declaration;
2811
2812//      if (doc_support)
2813//        em.DocComment = ev.DocComment;
2814
2815    $$ = pop_current_class ();
2816    }
2817  ;
2818
2819opt_enum_base
2820  : /* empty */
2821  | COLON type
2822   {
2823    savedLocation = GetLocation ($1);
2824    $$ = $2;
2825   }
2826  | COLON error
2827   {
2828    Error_TypeExpected (GetLocation ($1));
2829    $$ = null;
2830   }
2831  ;
2832
2833opt_enum_member_declarations
2834  : /* empty */
2835  | enum_member_declarations
2836  | enum_member_declarations COMMA
2837    {
2838    lbag.AppendToMember (current_container, GetLocation ($2));
2839    }
2840  ;
2841
2842enum_member_declarations
2843  : enum_member_declaration
2844  | enum_member_declarations COMMA enum_member_declaration
2845    {
2846    lbag.AppendToMember (current_container, GetLocation ($2));
2847      $$ = $3;
2848    }
2849  ;
2850
2851enum_member_declaration
2852  : opt_attributes IDENTIFIER
2853    {
2854    var lt = (LocatedToken) $2;
2855      var em = new EnumMember ((Enum) current_type, new MemberName (lt.Value, lt.Location), (Attributes) $1);
2856      ((Enum) current_type).AddEnumMember (em);
2857
2858    if (doc_support) {
2859      em.DocComment = Lexer.consume_doc_comment ();
2860      Lexer.doc_state = XmlCommentState.Allowed;
2861    }
2862
2863    $$ = em;
2864    }
2865  | opt_attributes IDENTIFIER
2866    {
2867      ++lexer.parsing_block;
2868    if (doc_support) {
2869      tmpComment = Lexer.consume_doc_comment ();
2870      Lexer.doc_state = XmlCommentState.NotAllowed;
2871    }
2872    }
2873    ASSIGN constant_expression
2874    {
2875    --lexer.parsing_block;
2876   
2877    var lt = (LocatedToken) $2;
2878      var em = new EnumMember ((Enum) current_type, new MemberName (lt.Value, lt.Location), (Attributes) $1);
2879      em.Initializer = new ConstInitializer (em, (Expression) $5, GetLocation ($4));
2880      ((Enum) current_type).AddEnumMember (em);
2881   
2882    if (doc_support)
2883      em.DocComment = ConsumeStoredComment ();
2884
2885    $$ = em;
2886    }
2887  | opt_attributes IDENTIFIER error
2888    {
2889    Error_SyntaxError (yyToken);
2890   
2891    var lt = (LocatedToken) $2;
2892      var em = new EnumMember ((Enum) current_type, new MemberName (lt.Value, lt.Location), (Attributes) $1);
2893      ((Enum) current_type).AddEnumMember (em);
2894
2895    if (doc_support) {
2896      em.DocComment = Lexer.consume_doc_comment ();
2897      Lexer.doc_state = XmlCommentState.Allowed;
2898    }
2899
2900    $$ = em;
2901    }
2902  | attributes_without_members
2903  ;
2904
2905delegate_declaration
2906  : opt_attributes
2907    opt_modifiers
2908    DELEGATE
2909    member_type type_declaration_name
2910    OPEN_PARENS
2911    {
2912    valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out | ParameterModifierType.Params | ParameterModifierType.DefaultValue;
2913    }
2914    opt_formal_parameter_list CLOSE_PARENS
2915    {
2916    valid_param_mod = 0;
2917
2918    ParametersCompiled p = (ParametersCompiled) $8;
2919
2920    Delegate del = new Delegate (current_container, (FullNamedExpression) $4, (Modifiers) $2, (MemberName) $5, p, (Attributes) $1);
2921
2922    p.CheckParameters (del);
2923
2924    current_container.AddTypeContainer (del);
2925
2926    current_delegate = del;
2927    lexer.ConstraintsParsing = true;
2928    }
2929    opt_type_parameter_constraints_clauses
2930    {
2931    lexer.ConstraintsParsing = false;
2932    }
2933    SEMICOLON
2934    {
2935    if (doc_support) {
2936      current_delegate.DocComment = Lexer.consume_doc_comment ();
2937      Lexer.doc_state = XmlCommentState.Allowed;
2938    }
2939   
2940    if ($11 != null)
2941      current_delegate.SetConstraints ((List<Constraints>) $11);
2942    lbag.AddMember (current_delegate, GetModifierLocations (), GetLocation ($3), GetLocation ($6), GetLocation ($9), GetLocation ($13));
2943
2944    $$ = current_delegate;
2945
2946    current_delegate = null;
2947    }
2948  ;
2949
2950opt_nullable
2951  : /* empty */
2952  | INTERR_NULLABLE
2953    {
2954    if (lang_version < LanguageVersion.ISO_2)
2955      FeatureIsNotAvailable (GetLocation ($1), "nullable types");
2956   
2957      $$ = ComposedTypeSpecifier.CreateNullable (GetLocation ($1));
2958    }
2959  ;
2960
2961namespace_or_type_expr
2962  : member_name
2963  | qualified_alias_member IDENTIFIER opt_type_argument_list
2964    {
2965    var lt1 = (LocatedToken) $1;
2966    var lt2 = (LocatedToken) $2;
2967   
2968    $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
2969    lbag.AddLocation ($$, savedLocation, GetLocation ($2));
2970    }
2971  | qualified_alias_member IDENTIFIER generic_dimension
2972    {
2973    var lt1 = (LocatedToken) $1;
2974    var lt2 = (LocatedToken) $2;
2975    var qam = new QualifiedAliasMember (lt1.Value, lt2.Value, (int) $3, lt1.Location);
2976    lbag.AddLocation (qam.TypeArguments, Lexer.GenericDimensionLocations);
2977    $$ = qam;
2978    lbag.AddLocation ($$, GetLocation ($2));
2979    }
2980  ;
2981
2982member_name
2983  : simple_name_expr
2984  | namespace_or_type_expr DOT IDENTIFIER opt_type_argument_list
2985    {
2986    var lt = (LocatedToken) $3;
2987    $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
2988    lbag.AddLocation ($$, GetLocation ($2));
2989    }
2990  | namespace_or_type_expr DOT IDENTIFIER generic_dimension
2991    {
2992    var lt = (LocatedToken) $3;
2993    var ma = new MemberAccess ((Expression) $1, lt.Value, (int) $4, lt.Location);
2994    lbag.AddLocation (ma.TypeArguments, Lexer.GenericDimensionLocations);
2995    $$ = ma;
2996    lbag.AddLocation ($$, GetLocation ($2));
2997    }
2998  ;
2999
3000simple_name_expr
3001  : IDENTIFIER opt_type_argument_list
3002    {
3003    var lt = (LocatedToken) $1;
3004    $$ = new SimpleName (lt.Value, (TypeArguments)$2, lt.Location);
3005    }
3006  | IDENTIFIER generic_dimension
3007    { 
3008    var lt = (LocatedToken) $1;
3009    var sn = new SimpleName (lt.Value, (int) $2, lt.Location);
3010    lbag.AddLocation (sn.TypeArguments, Lexer.GenericDimensionLocations);
3011    $$ = sn;
3012    }
3013  ;
3014
3015//
3016// Generics arguments  (any type, without attributes)
3017//
3018opt_type_argument_list
3019  : /* empty */
3020  | OP_GENERICS_LT type_arguments OP_GENERICS_GT
3021    {
3022    if (lang_version < LanguageVersion.ISO_2)
3023      FeatureIsNotAvailable (GetLocation ($1), "generics");
3024    var list = locationListStack.Pop ();
3025    list.Add (GetLocation ($1));
3026    list.Add (GetLocation ($2));
3027    lbag.AddLocation ($2, list);
3028 
3029    $$ = $2;;
3030    }
3031  | OP_GENERICS_LT error
3032    {
3033    Error_TypeExpected (lexer.Location);
3034    $$ = new TypeArguments ();
3035    }
3036  ;
3037
3038type_arguments
3039  : type
3040    {
3041    TypeArguments type_args = new TypeArguments ();
3042    type_args.Add ((FullNamedExpression) $1);
3043    $$ = type_args;
3044    locationListStack.Push (new List<Location> ());
3045    }
3046  | type_arguments COMMA type
3047    {
3048    TypeArguments type_args = (TypeArguments) $1;
3049    type_args.Add ((FullNamedExpression) $3);
3050    $$ = type_args;
3051    locationListStack.Peek ().Add (GetLocation ($2));
3052    }
3053  ;
3054
3055//
3056// Generics parameters (identifiers only, with attributes), used in type or method declarations
3057//
3058type_declaration_name
3059  : IDENTIFIER
3060    {
3061    lexer.parsing_generic_declaration = true;
3062    }
3063    opt_type_parameter_list
3064    {
3065    lexer.parsing_generic_declaration = false;
3066    var lt = (LocatedToken) $1;
3067    $$ = new MemberName (lt.Value, (TypeParameters)$3, lt.Location);
3068    }
3069  ;
3070
3071member_declaration_name
3072  : method_declaration_name
3073    {
3074      MemberName mn = (MemberName)$1;
3075      if (mn.TypeParameters != null)
3076        syntax_error (mn.Location, string.Format ("Member `{0}' cannot declare type arguments",
3077          mn.GetSignatureForError ()));
3078    }
3079  ;
3080
3081method_declaration_name
3082  : type_declaration_name
3083  | explicit_interface IDENTIFIER opt_type_parameter_list
3084    {
3085    lexer.parsing_generic_declaration = false;   
3086    var lt = (LocatedToken) $2;
3087    $$ = new MemberName (lt.Value, (TypeParameters) $3, (ATypeNameExpression) $1, lt.Location);
3088    }
3089  ;
3090 
3091indexer_declaration_name
3092  : THIS
3093    {
3094    lexer.parsing_generic_declaration = false;   
3095    $$ = new MemberName (TypeDefinition.DefaultIndexerName, GetLocation ($1));
3096    }
3097  | explicit_interface THIS
3098    {
3099    lexer.parsing_generic_declaration = false;
3100    $$ = new MemberName (TypeDefinition.DefaultIndexerName, null, (ATypeNameExpression) $1, GetLocation ($2));
3101    }
3102  ;
3103
3104explicit_interface
3105  : IDENTIFIER opt_type_argument_list DOT
3106    {
3107    var lt = (LocatedToken) $1;
3108    $$ = new SimpleName (lt.Value, (TypeArguments) $2, lt.Location);
3109    lbag.AddLocation ($$, GetLocation ($3));
3110    }
3111  | qualified_alias_member IDENTIFIER opt_type_argument_list DOT
3112    {
3113    var lt1 = (LocatedToken) $1;
3114    var lt2 = (LocatedToken) $2;
3115
3116    $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
3117    lbag.AddLocation ($$, savedLocation, GetLocation ($4));
3118    }
3119  | explicit_interface IDENTIFIER opt_type_argument_list DOT
3120    {
3121    var lt = (LocatedToken) $2;
3122    $$ = new MemberAccess ((ATypeNameExpression) $1, lt.Value, (TypeArguments) $3, lt.Location);
3123    lbag.AddLocation ($$, GetLocation ($4));
3124    }
3125  ;
3126 
3127opt_type_parameter_list
3128  : /* empty */
3129  | OP_GENERICS_LT_DECL type_parameters OP_GENERICS_GT
3130    {
3131    if (lang_version < LanguageVersion.ISO_2)
3132      FeatureIsNotAvailable (GetLocation ($1), "generics");
3133   
3134    $$ = $2;
3135    var list = locationListStack.Pop ();
3136    list.Add (GetLocation ($1));
3137    list.Add (GetLocation ($2));
3138    lbag.AddLocation ($2, list);
3139    }
3140  ;
3141
3142type_parameters
3143  : type_parameter
3144    {
3145    var tparams = new TypeParameters ();
3146    tparams.Add ((TypeParameter)$1);
3147    $$ = tparams;
3148    locationListStack.Push (new List<Location> ());
3149    }
3150  | type_parameters COMMA type_parameter
3151    {
3152    var tparams = (TypeParameters) $1;
3153    tparams.Add ((TypeParameter)$3);
3154    $$ = tparams;
3155    locationListStack.Peek ().Add (GetLocation ($2));
3156    }   
3157  ;
3158
3159type_parameter
3160  : opt_attributes opt_type_parameter_variance IDENTIFIER
3161    {
3162    var lt = (LocatedToken)$3;
3163    var variance = (VarianceDecl) $2;
3164    $$ = new TypeParameter (new MemberName (lt.Value, lt.Location), (Attributes)$1, variance);
3165    if (variance != null)
3166      lbag.AddLocation ($$, savedLocation);
3167      }
3168    | error
3169      {
3170        if (GetTokenName (yyToken) == "type")
3171      report.Error (81, GetLocation ($1), "Type parameter declaration must be an identifier not a type");
3172    else
3173      Error_SyntaxError (yyToken);
3174     
3175        $$ = new TypeParameter (MemberName.Null, null, null);
3176      }
3177  ;
3178
3179//
3180// All types where void is allowed
3181//
3182type_and_void
3183  : type_expression_or_array
3184  | VOID
3185    {
3186    $$ = new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1));
3187    }
3188  ;
3189 
3190member_type
3191  : type_and_void
3192    {
3193    lexer.parsing_generic_declaration = true;
3194    }
3195  ;
3196 
3197//
3198// A type which does not allow `void' to be used
3199//
3200type
3201  : type_expression_or_array
3202  | void_invalid
3203  ;
3204 
3205simple_type
3206  : type_expression
3207  | void_invalid
3208  ;
3209 
3210parameter_type
3211  : type_expression_or_array
3212  | VOID
3213    {
3214    report.Error (1536, GetLocation ($1), "Invalid parameter type `void'");
3215    $$ = new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1));
3216    }
3217  ;
3218
3219type_expression_or_array
3220  : type_expression
3221  | type_expression rank_specifiers
3222    {
3223    $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
3224    }
3225  ;
3226 
3227type_expression
3228  : namespace_or_type_expr opt_nullable
3229    {
3230    if ($2 != null) {
3231      $$ = new ComposedCast ((ATypeNameExpression) $1, (ComposedTypeSpecifier) $2);
3232    } else {
3233      var sn = $1 as SimpleName;
3234      if (sn != null && sn.Name == "var")
3235        $$ = new VarExpr (sn.Location);
3236      else
3237        $$ = $1;
3238    }
3239    }
3240  | namespace_or_type_expr pointer_stars
3241    {
3242    $$ = new ComposedCast ((ATypeNameExpression) $1, (ComposedTypeSpecifier) $2);
3243    }
3244  | builtin_type_expression
3245  ;
3246
3247void_invalid
3248  : VOID
3249    {
3250    Expression.Error_VoidInvalidInTheContext (GetLocation ($1), report);
3251    $$ = new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1));
3252    }
3253  ;
3254
3255builtin_type_expression
3256  : builtin_types opt_nullable
3257    {
3258    if ($2 != null)
3259      $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
3260    }
3261  | builtin_types pointer_stars
3262    {
3263    $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
3264    }
3265  | VOID pointer_stars
3266    {
3267    $$ = new ComposedCast (new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1)), (ComposedTypeSpecifier) $2);
3268    }
3269  ;
3270
3271type_list
3272  : base_type_name
3273    {
3274    var types = new List<FullNamedExpression> (2);
3275    types.Add ((FullNamedExpression) $1);
3276    $$ = types;
3277    }
3278  | type_list COMMA base_type_name
3279    {
3280    var types = (List<FullNamedExpression>) $1;
3281    types.Add ((FullNamedExpression) $3);
3282    lbag.AddLocation (types, GetLocation ($2));
3283    $$ = types;
3284    }
3285  ;
3286
3287base_type_name
3288  : type
3289    {
3290    if ($1 is ComposedCast) {
3291      report.Error (1521, GetLocation ($1), "Invalid base type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
3292    }
3293    $$ = $1;
3294    }
3295  ;
3296 
3297/*
3298 * replaces all the productions for isolating the various
3299 * simple types, but we need this to reuse it easily in variable_type
3300 */
3301builtin_types
3302  : OBJECT  { $$ = new TypeExpression (compiler.BuiltinTypes.Object, GetLocation ($1)); }
3303  | STRING  { $$ = new TypeExpression (compiler.BuiltinTypes.String, GetLocation ($1)); }
3304  | BOOL    { $$ = new TypeExpression (compiler.BuiltinTypes.Bool, GetLocation ($1)); }
3305  | DECIMAL { $$ = new TypeExpression (compiler.BuiltinTypes.Decimal, GetLocation ($1)); }
3306  | FLOAT   { $$ = new TypeExpression (compiler.BuiltinTypes.Float, GetLocation ($1)); }
3307  | DOUBLE  { $$ = new TypeExpression (compiler.BuiltinTypes.Double, GetLocation ($1)); }
3308  | integral_type
3309  ;
3310
3311integral_type
3312  : SBYTE   { $$ = new TypeExpression (compiler.BuiltinTypes.SByte, GetLocation ($1)); }
3313  | BYTE    { $$ = new TypeExpression (compiler.BuiltinTypes.Byte, GetLocation ($1)); }
3314  | SHORT   { $$ = new TypeExpression (compiler.BuiltinTypes.Short, GetLocation ($1)); }
3315  | USHORT  { $$ = new TypeExpression (compiler.BuiltinTypes.UShort, GetLocation ($1)); }
3316  | INT   { $$ = new TypeExpression (compiler.BuiltinTypes.Int, GetLocation ($1)); }
3317  | UINT    { $$ = new TypeExpression (compiler.BuiltinTypes.UInt, GetLocation ($1)); }
3318  | LONG    { $$ = new TypeExpression (compiler.BuiltinTypes.Long, GetLocation ($1)); }
3319  | ULONG   { $$ = new TypeExpression (compiler.BuiltinTypes.ULong, GetLocation ($1)); }
3320  | CHAR    { $$ = new TypeExpression (compiler.BuiltinTypes.Char, GetLocation ($1)); }
3321  ;
3322
3323//
3324// Expressions, section 7.5
3325//
3326
3327
3328primary_expression
3329  : primary_expression_or_type
3330  | literal
3331  | array_creation_expression
3332  | parenthesized_expression
3333  | default_value_expression
3334  | invocation_expression
3335  | element_access
3336  | this_access
3337  | base_access
3338  | post_increment_expression
3339  | post_decrement_expression
3340  | object_or_delegate_creation_expression
3341  | anonymous_type_expression
3342  | typeof_expression
3343  | sizeof_expression
3344  | checked_expression
3345  | unchecked_expression
3346  | pointer_member_access
3347  | anonymous_method_expression
3348  | undocumented_expressions
3349  ;
3350
3351primary_expression_or_type
3352  : simple_name_expr
3353  | IDENTIFIER GENERATE_COMPLETION {
3354    var lt = (LocatedToken) $1;
3355         $$ = new CompletionSimpleName (MemberName.MakeName (lt.Value, null), lt.Location);
3356    }
3357  | member_access
3358  ;
3359
3360literal
3361  : boolean_literal
3362  | LITERAL
3363  | NULL      { $$ = new NullLiteral (GetLocation ($1)); }
3364  ;
3365
3366boolean_literal
3367  : TRUE      { $$ = new BoolLiteral (compiler.BuiltinTypes, true, GetLocation ($1)); }
3368  | FALSE     { $$ = new BoolLiteral (compiler.BuiltinTypes, false, GetLocation ($1)); }
3369  ;
3370
3371
3372//
3373// Here is the trick, tokenizer may think that parens is a special but
3374// parser is interested in open parens only, so we merge them.
3375// Consider: if (a)foo ();
3376//
3377open_parens_any
3378  : OPEN_PARENS
3379  | OPEN_PARENS_CAST
3380  ;
3381
3382//
3383// Use this production to accept closing parenthesis or
3384// performing completion
3385//
3386close_parens
3387  : CLOSE_PARENS
3388  | COMPLETE_COMPLETION
3389  ;
3390
3391
3392parenthesized_expression
3393  : OPEN_PARENS expression CLOSE_PARENS
3394    {
3395    $$ = new ParenthesizedExpression ((Expression) $2, GetLocation ($1));
3396    lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
3397    }
3398  | OPEN_PARENS expression COMPLETE_COMPLETION
3399    {
3400    $$ = new ParenthesizedExpression ((Expression) $2, GetLocation ($1));
3401    }
3402  ;
3403
3404member_access
3405  : primary_expression DOT identifier_inside_body opt_type_argument_list
3406    {
3407    var lt = (LocatedToken) $3;
3408    $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3409    lbag.AddLocation ($$, GetLocation ($2));
3410    }
3411  | primary_expression DOT identifier_inside_body generic_dimension
3412    {
3413    var lt = (LocatedToken) $3;
3414    var ma = new MemberAccess ((Expression) $1, lt.Value, (int) $4, lt.Location);
3415    lbag.AddLocation (ma.TypeArguments, Lexer.GenericDimensionLocations);
3416    $$ = ma;
3417    lbag.AddLocation ($$, GetLocation ($2));
3418    }
3419  | primary_expression INTERR_OPERATOR DOT identifier_inside_body opt_type_argument_list
3420    {
3421    if (lang_version < LanguageVersion.V_6)
3422      FeatureIsNotAvailable (GetLocation ($2), "null propagating operator");
3423
3424    var lt = (LocatedToken) $4;
3425    $$ = new ConditionalMemberAccess ((Expression) $1, lt.Value, (TypeArguments) $5, lt.Location);
3426    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3));
3427    }
3428  | builtin_types DOT identifier_inside_body opt_type_argument_list
3429    {
3430    var lt = (LocatedToken) $3;
3431    $$ = new MemberAccess ((Expression) $1, lt.Value, (TypeArguments) $4, lt.Location);
3432    lbag.AddLocation ($$, GetLocation ($2));
3433    }
3434  | BASE DOT identifier_inside_body opt_type_argument_list
3435    {
3436    var lt = (LocatedToken) $3;
3437    $$ = new MemberAccess (new BaseThis (GetLocation ($1)), lt.Value, (TypeArguments) $4, lt.Location);
3438    lbag.AddLocation ($$, GetLocation ($2));
3439    }
3440  | AWAIT DOT identifier_inside_body opt_type_argument_list
3441    {
3442    var lt = (LocatedToken) $3;
3443    $$ = new MemberAccess (new SimpleName ("await", ((LocatedToken) $1).Location), lt.Value, (TypeArguments) $4, lt.Location);
3444    lbag.AddLocation ($$, GetLocation ($2));
3445    }
3446  | qualified_alias_member identifier_inside_body opt_type_argument_list
3447    {
3448    var lt1 = (LocatedToken) $1;
3449    var lt2 = (LocatedToken) $2;
3450
3451    $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, (TypeArguments) $3, lt1.Location);
3452    lbag.AddLocation ($$, savedLocation, GetLocation ($2));
3453    }
3454  | qualified_alias_member identifier_inside_body generic_dimension
3455    {
3456    var lt1 = (LocatedToken) $1;
3457    var lt2 = (LocatedToken) $2;
3458    var qam = new QualifiedAliasMember (lt1.Value, lt2.Value, (int) $3, lt1.Location);
3459    lbag.AddLocation (qam.TypeArguments, Lexer.GenericDimensionLocations);
3460    $$ = qam;
3461    lbag.AddLocation ($$, GetLocation ($2));
3462    }
3463  | primary_expression DOT GENERATE_COMPLETION {
3464    $$ = new CompletionMemberAccess ((Expression) $1, null,GetLocation ($3));
3465    }
3466  | primary_expression DOT IDENTIFIER GENERATE_COMPLETION {
3467    var lt = (LocatedToken) $3;
3468    $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3469    }
3470  | builtin_types DOT GENERATE_COMPLETION
3471    {
3472    $$ = new CompletionMemberAccess ((Expression) $1, null, lexer.Location);
3473    }
3474  | builtin_types DOT IDENTIFIER GENERATE_COMPLETION {
3475    var lt = (LocatedToken) $3;
3476    $$ = new CompletionMemberAccess ((Expression) $1, lt.Value, lt.Location);
3477    }
3478  ;
3479
3480invocation_expression
3481  : primary_expression open_parens_any opt_argument_list close_parens
3482    {
3483    $$ = new Invocation ((Expression) $1, (Arguments) $3);
3484    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3485    }
3486  | primary_expression open_parens_any argument_list error
3487    {
3488    Error_SyntaxError (yyToken);
3489
3490    $$ = new Invocation ((Expression) $1, (Arguments) $3);
3491    lbag.AddLocation ($$, GetLocation ($2));
3492    }
3493  | primary_expression open_parens_any error
3494    {
3495    Error_SyntaxError (yyToken);
3496
3497    $$ = new Invocation ((Expression) $1, null);
3498    lbag.AddLocation ($$, GetLocation ($2));
3499    }
3500  ;
3501
3502opt_object_or_collection_initializer
3503  : /* empty */   { $$ = null; }
3504  | object_or_collection_initializer
3505  ;
3506
3507object_or_collection_initializer
3508  : OPEN_BRACE opt_member_initializer_list close_brace_or_complete_completion
3509    {
3510      if ($2 == null) {
3511        $$ = new CollectionOrObjectInitializers (GetLocation ($1));
3512      } else {
3513        $$ = new CollectionOrObjectInitializers ((List<Expression>) $2, GetLocation ($1));
3514      }
3515    lbag.AddLocation ($$, GetLocation ($3));
3516    }
3517  | OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
3518    {
3519      $$ = new CollectionOrObjectInitializers ((List<Expression>) $2, GetLocation ($1));
3520      lbag.AddLocation ($$, GetLocation ($3), GetLocation ($4));
3521    }
3522  ;
3523
3524opt_member_initializer_list
3525  : /* empty */   { $$ = null; }
3526  | member_initializer_list
3527  {
3528    $$ = $1;
3529  }
3530  ;
3531
3532member_initializer_list
3533  : member_initializer
3534    {
3535      var a = new List<Expression> ();
3536      a.Add ((Expression) $1);
3537      $$ = a;
3538    }
3539  | member_initializer_list COMMA member_initializer
3540    {
3541      var a = (List<Expression>)$1;
3542      a.Add ((Expression) $3);
3543    lbag.AddLocation (a, GetLocation ($2));
3544      $$ = a;
3545    }
3546  | member_initializer_list error {
3547      Error_SyntaxError (yyToken);
3548    $$ = $1;
3549    }
3550  ;
3551
3552member_initializer
3553  : IDENTIFIER ASSIGN initializer_value
3554    {
3555      var lt = (LocatedToken) $1;
3556      $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3557      lbag.AddLocation ($$, GetLocation ($2));
3558    }
3559  | AWAIT ASSIGN initializer_value
3560    {
3561      var lt = (LocatedToken) Error_AwaitAsIdentifier ($1);
3562      $$ = new ElementInitializer (lt.Value, (Expression)$3, lt.Location);
3563      lbag.AddLocation ($$, GetLocation ($2));
3564    }
3565  | GENERATE_COMPLETION
3566    {
3567    $$ = new CompletionElementInitializer (null, GetLocation ($1));
3568    }
3569  | non_assignment_expression opt_COMPLETE_COMPLETION  {
3570    CompletionSimpleName csn = $1 as CompletionSimpleName;
3571    if (csn == null)
3572      $$ = new CollectionElementInitializer ((Expression)$1);
3573    else
3574      $$ = new CompletionElementInitializer (csn.Prefix, csn.Location);
3575    }
3576  | OPEN_BRACE expression_list CLOSE_BRACE
3577    {
3578    if ($2 == null)
3579      $$ = new CollectionElementInitializer (GetLocation ($1));
3580    else {
3581        $$ = new CollectionElementInitializer ((List<Expression>)$2, GetLocation ($1));
3582      lbag.AddLocation ($$, GetLocation ($2));
3583    }
3584    lbag.AddLocation ($$, GetLocation ($3));
3585    }
3586  | OPEN_BRACKET_EXPR expression_list CLOSE_BRACKET ASSIGN initializer_value
3587    {
3588    if (lang_version < LanguageVersion.V_6)
3589      FeatureIsNotAvailable (GetLocation ($1), "dictionary initializer");
3590
3591    $$ = new DictionaryElementInitializer ((List<Expression>)$2, (Expression) $5, GetLocation ($1));
3592    lbag.AddLocation ($$, GetLocation ($3), GetLocation ($4));
3593    }
3594  | OPEN_BRACE CLOSE_BRACE
3595    {
3596      report.Error (1920, GetLocation ($1), "An element initializer cannot be empty");
3597    $$ = new CollectionElementInitializer (GetLocation ($1));
3598    lbag.AddLocation ($$, GetLocation ($2));
3599    }
3600  ;
3601
3602initializer_value
3603  : expression
3604  | object_or_collection_initializer
3605  ;
3606
3607opt_argument_list
3608  : /* empty */   { $$ = null; }
3609  | argument_list
3610  ;
3611
3612argument_list
3613  : argument_or_named_argument
3614    {
3615    Arguments list = new Arguments (4);
3616    list.Add ((Argument) $1);
3617    $$ = list;
3618    }
3619  | argument_list COMMA argument
3620    {
3621    Arguments list = (Arguments) $1;
3622    if (list [list.Count - 1] is NamedArgument)
3623      Error_NamedArgumentExpected ((NamedArgument) list [list.Count - 1]);
3624   
3625    list.Add ((Argument) $3);
3626    lbag.AddLocation (list, GetLocation ($2));
3627    $$ = list;
3628    }
3629  | argument_list COMMA named_argument
3630    {
3631    Arguments list = (Arguments) $1;
3632    NamedArgument a = (NamedArgument) $3;
3633    for (int i = 0; i < list.Count; ++i) {
3634      NamedArgument na = list [i] as NamedArgument;
3635      if (na != null && na.Name == a.Name)
3636        report.Error (1740, na.Location, "Named argument `{0}' specified multiple times",
3637          na.Name);
3638    }
3639   
3640    list.Add (a);
3641    lbag.AddLocation (list, GetLocation ($2));
3642    $$ = list;
3643    }
3644  | argument_list COMMA error
3645    {
3646      if (lexer.putback_char == -1)
3647        lexer.putback (')'); // TODO: Wrong but what can I do
3648    Error_SyntaxError (yyToken);
3649    $$ = $1;
3650    }
3651  | COMMA error
3652    {
3653      report.Error (839, GetLocation ($1), "An argument is missing");
3654      $$ = null;
3655    }
3656  ;
3657
3658argument
3659  : expression
3660    {
3661    $$ = new Argument ((Expression) $1);
3662    }
3663  | non_simple_argument
3664  ;
3665
3666argument_or_named_argument
3667  : argument
3668  | named_argument
3669  ;
3670
3671non_simple_argument
3672  : REF variable_reference
3673    {
3674    $$ = new Argument ((Expression) $2, Argument.AType.Ref);
3675    lbag.AddLocation ($$, GetLocation ($1));
3676    }
3677  | OUT variable_reference
3678    {
3679    $$ = new Argument ((Expression) $2, Argument.AType.Out);
3680    lbag.AddLocation ($$, GetLocation ($1));
3681    }
3682  | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
3683    {
3684    $$ = new Argument (new Arglist ((Arguments) $3, GetLocation ($1)));
3685    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3686    }
3687  | ARGLIST OPEN_PARENS CLOSE_PARENS
3688    {
3689    $$ = new Argument (new Arglist (GetLocation ($1)));
3690    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3));
3691    }   
3692  ;
3693
3694variable_reference
3695  : expression
3696  ;
3697
3698element_access
3699  : primary_expression OPEN_BRACKET_EXPR expression_list_arguments CLOSE_BRACKET 
3700    {
3701    $$ = new ElementAccess ((Expression) $1, (Arguments) $3, GetLocation ($2));
3702    lbag.AddLocation ($$, GetLocation ($4));
3703    }
3704  | primary_expression INTERR_OPERATOR OPEN_BRACKET_EXPR expression_list_arguments CLOSE_BRACKET 
3705    {
3706    if (lang_version < LanguageVersion.V_6)
3707      FeatureIsNotAvailable (GetLocation ($2), "null propagating operator");
3708
3709    $$ = new ElementAccess ((Expression) $1, (Arguments) $4, GetLocation ($3)) {
3710      ConditionalAccess = true
3711    };
3712
3713    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($5));
3714    }
3715  | primary_expression OPEN_BRACKET_EXPR expression_list_arguments error
3716    {
3717    Error_SyntaxError (yyToken);
3718    $$ = new ElementAccess ((Expression) $1, (Arguments) $3, GetLocation ($2));
3719    }
3720  | primary_expression OPEN_BRACKET_EXPR error
3721    {
3722    Error_SyntaxError (yyToken);
3723    $$ = new ElementAccess ((Expression) $1, null, GetLocation ($2));
3724    }
3725  ;
3726
3727expression_list
3728  : expression_or_error
3729    {
3730    var list = new List<Expression> (4);
3731    list.Add ((Expression) $1);
3732    $$ = list;
3733    }
3734  | expression_list COMMA expression_or_error
3735    {
3736    var list = (List<Expression>) $1;
3737    list.Add ((Expression) $3);
3738    lbag.AddLocation (list, GetLocation ($2));
3739    $$ = list;
3740    }
3741  ;
3742 
3743expression_list_arguments
3744  : expression_list_argument
3745    {
3746    Arguments args = new Arguments (4);
3747    args.Add ((Argument) $1);
3748    $$ = args;
3749    }
3750  | expression_list_arguments COMMA expression_list_argument
3751    {
3752    Arguments args = (Arguments) $1;
3753    if (args [args.Count - 1] is NamedArgument && !($3 is NamedArgument))
3754      Error_NamedArgumentExpected ((NamedArgument) args [args.Count - 1]);
3755   
3756    args.Add ((Argument) $3);
3757    lbag.AddLocation (args, GetLocation ($2));
3758    $$ = args;   
3759    }
3760  ;
3761 
3762expression_list_argument
3763  : expression
3764    {
3765      $$ = new Argument ((Expression) $1);
3766    }
3767  | named_argument
3768  ;
3769
3770this_access
3771  : THIS
3772    {
3773    $$ = new This (GetLocation ($1));
3774    }
3775  ;
3776
3777base_access
3778  : BASE OPEN_BRACKET_EXPR expression_list_arguments CLOSE_BRACKET
3779    {
3780      $$ = new ElementAccess (new BaseThis (GetLocation ($1)), (Arguments) $3, GetLocation ($2));
3781    lbag.AddLocation ($$, GetLocation ($4));
3782    }
3783  | BASE OPEN_BRACKET error
3784    {
3785      Error_SyntaxError (yyToken);
3786    $$ = new ElementAccess (null, null, GetLocation ($2));
3787    }
3788  ;
3789
3790post_increment_expression
3791  : primary_expression OP_INC
3792    {
3793    $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement, (Expression) $1, GetLocation ($2));
3794    }
3795  ;
3796
3797post_decrement_expression
3798  : primary_expression OP_DEC
3799    {
3800    $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement, (Expression) $1, GetLocation ($2));
3801    }
3802  ;
3803 
3804object_or_delegate_creation_expression
3805  : NEW new_expr_type open_parens_any opt_argument_list CLOSE_PARENS opt_object_or_collection_initializer
3806    {
3807    if ($6 != null) {
3808      if (lang_version <= LanguageVersion.ISO_2)
3809        FeatureIsNotAvailable (GetLocation ($1), "object initializers");
3810       
3811      $$ = new NewInitialize ((FullNamedExpression) $2, (Arguments) $4, (CollectionOrObjectInitializers) $6, GetLocation ($1));
3812    } else {
3813      $$ = new New ((FullNamedExpression) $2, (Arguments) $4, GetLocation ($1));
3814    }
3815   
3816    lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
3817    }
3818  | NEW new_expr_type object_or_collection_initializer
3819    {
3820    if (lang_version <= LanguageVersion.ISO_2)
3821      FeatureIsNotAvailable (GetLocation ($1), "collection initializers");
3822   
3823    $$ = new NewInitialize ((FullNamedExpression) $2, null, (CollectionOrObjectInitializers) $3, GetLocation ($1));
3824    }
3825  ;
3826
3827array_creation_expression
3828  : NEW new_expr_type OPEN_BRACKET_EXPR expression_list CLOSE_BRACKET
3829    opt_rank_specifier
3830    opt_array_initializer
3831    {
3832    $$ = new ArrayCreation ((FullNamedExpression) $2, (List<Expression>) $4,
3833        new ComposedTypeSpecifier (((List<Expression>) $4).Count, GetLocation ($3)) {
3834            Next = (ComposedTypeSpecifier) $6
3835          }, (ArrayInitializer) $7, GetLocation ($1));
3836    lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
3837    }
3838  | NEW new_expr_type rank_specifiers opt_array_initializer
3839    {
3840      if ($4 == null)
3841        report.Error (1586, GetLocation ($1), "Array creation must have array size or array initializer");
3842
3843    $$ = new ArrayCreation ((FullNamedExpression) $2, (ComposedTypeSpecifier) $3, (ArrayInitializer) $4, GetLocation ($1));
3844    }
3845  | NEW rank_specifier array_initializer
3846    {
3847    if (lang_version <= LanguageVersion.ISO_2)
3848      FeatureIsNotAvailable (GetLocation ($1), "implicitly typed arrays");
3849   
3850    $$ = new ImplicitlyTypedArrayCreation ((ComposedTypeSpecifier) $2, (ArrayInitializer) $3, GetLocation ($1));
3851    }
3852  | NEW new_expr_type OPEN_BRACKET CLOSE_BRACKET OPEN_BRACKET_EXPR error CLOSE_BRACKET
3853    {
3854    report.Error (178, GetLocation ($6), "Invalid rank specifier, expecting `,' or `]'");
3855    $$ = new ArrayCreation ((FullNamedExpression) $2, null, GetLocation ($1));
3856    }
3857  | NEW new_expr_type error
3858    {
3859    Error_SyntaxError (yyToken);
3860    // It can be any of new expression, create the most common one
3861    $$ = new New ((FullNamedExpression) $2, null, GetLocation ($1));
3862    }
3863  ;
3864
3865new_expr_type
3866  : {
3867    ++lexer.parsing_type;
3868    }
3869    simple_type
3870    {
3871    --lexer.parsing_type;
3872    $$ = $2;
3873    }
3874  ;
3875
3876anonymous_type_expression
3877  : NEW OPEN_BRACE anonymous_type_parameters_opt_comma CLOSE_BRACE
3878    {
3879    if (lang_version <= LanguageVersion.ISO_2)
3880        FeatureIsNotAvailable (GetLocation ($1), "anonymous types");
3881
3882    $$ = new NewAnonymousType ((List<AnonymousTypeParameter>) $3, current_container, GetLocation ($1));
3883   
3884    // TODO: lbag comma location
3885    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
3886    }
3887  | NEW OPEN_BRACE GENERATE_COMPLETION
3888    {
3889    $$ = new EmptyCompletion ();
3890    }
3891  ;
3892
3893anonymous_type_parameters_opt_comma
3894  : anonymous_type_parameters_opt
3895  | anonymous_type_parameters COMMA
3896  ;
3897
3898anonymous_type_parameters_opt
3899  : { $$ = null; }
3900  | anonymous_type_parameters
3901  ;
3902
3903anonymous_type_parameters
3904  : anonymous_type_parameter
3905    {
3906      var a = new List<AnonymousTypeParameter> (4);
3907      a.Add ((AnonymousTypeParameter) $1);
3908      $$ = a;
3909    }
3910  | anonymous_type_parameters COMMA anonymous_type_parameter
3911    {
3912      var a = (List<AnonymousTypeParameter>) $1;
3913      a.Add ((AnonymousTypeParameter) $3);
3914      lbag.AddLocation (a, GetLocation ($2));
3915
3916      $$ = a;
3917    }
3918  | COMPLETE_COMPLETION
3919    {
3920    $$ = new EmptyCompletion ();
3921    }
3922  | anonymous_type_parameter COMPLETE_COMPLETION
3923    {
3924      $$ = $1;
3925    }
3926  ;
3927
3928anonymous_type_parameter
3929  : identifier_inside_body ASSIGN variable_initializer
3930    {
3931    var lt = (LocatedToken)$1;
3932      $$ = new AnonymousTypeParameter ((Expression)$3, lt.Value, lt.Location);
3933      lbag.AddLocation ($$, GetLocation ($2));
3934    }
3935  | identifier_inside_body
3936    {
3937    var lt = (LocatedToken)$1;
3938      $$ = new AnonymousTypeParameter (new SimpleName (lt.Value, lt.Location),
3939        lt.Value, lt.Location);
3940    }
3941  | member_access
3942    {
3943      MemberAccess ma = (MemberAccess) $1;
3944      $$ = new AnonymousTypeParameter (ma, ma.Name, ma.Location);
3945    }
3946  | error
3947    {
3948    report.Error (746, lexer.Location,
3949      "Invalid anonymous type member declarator. Anonymous type members must be a member assignment, simple name or member access expression");
3950    $$ = null;
3951    }
3952  ;
3953
3954opt_rank_specifier
3955  : /* empty */
3956  | rank_specifiers
3957  ;
3958
3959rank_specifiers
3960  : rank_specifier
3961  | rank_specifier rank_specifiers
3962    {
3963      ((ComposedTypeSpecifier) $1).Next = (ComposedTypeSpecifier) $2;
3964      $$ = $1;
3965    }
3966  ;
3967
3968rank_specifier
3969  : OPEN_BRACKET CLOSE_BRACKET
3970    {
3971    $$ = ComposedTypeSpecifier.CreateArrayDimension (1, GetLocation ($1));
3972    lbag.AddLocation ($$, GetLocation ($2));
3973    }
3974  | OPEN_BRACKET dim_separators CLOSE_BRACKET
3975    {
3976    $$ = ComposedTypeSpecifier.CreateArrayDimension ((int)$2, GetLocation ($1));
3977    lbag.AddLocation ($$, GetLocation ($3));
3978    }
3979  ;
3980
3981dim_separators
3982  : COMMA
3983    {
3984    $$ = 2;
3985    }
3986  | dim_separators COMMA
3987    {
3988    $$ = ((int) $1) + 1;
3989    }
3990  ;
3991
3992opt_array_initializer
3993  : /* empty */
3994    {
3995    $$ = null;
3996    }
3997  | array_initializer
3998    {
3999    $$ = $1;
4000    }
4001  ;
4002
4003array_initializer
4004  : OPEN_BRACE CLOSE_BRACE
4005    {
4006    var ai = new ArrayInitializer (0, GetLocation ($1));
4007    ai.VariableDeclaration = current_variable;
4008    lbag.AddLocation (ai, GetLocation ($2));
4009    $$ = ai;
4010    }
4011  | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
4012    {
4013    var ai = new ArrayInitializer ((List<Expression>) $2, GetLocation ($1));
4014    ai.VariableDeclaration = current_variable;
4015    if ($3 != null) {
4016      lbag.AddLocation (ai, GetLocation ($3), GetLocation ($4));
4017    } else {
4018      lbag.AddLocation (ai, GetLocation ($4));
4019    }
4020    $$ = ai;
4021    }
4022  ;
4023
4024variable_initializer_list
4025  : variable_initializer
4026    {
4027    var list = new List<Expression> (4);
4028    list.Add ((Expression) $1);
4029    $$ = list;
4030    }
4031  | variable_initializer_list COMMA variable_initializer
4032    {
4033    var list = (List<Expression>) $1;
4034    list.Add ((Expression) $3);
4035    lbag.AddLocation (list, GetLocation ($2));
4036    $$ = list;
4037    }
4038  ;
4039
4040typeof_expression
4041  : TYPEOF open_parens_any typeof_type_expression CLOSE_PARENS
4042    {
4043    $$ = new TypeOf ((FullNamedExpression) $3, GetLocation ($1));
4044    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4045    }
4046  ;
4047 
4048typeof_type_expression
4049  : type_and_void
4050  | error
4051   {
4052    Error_TypeExpected (lexer.Location);
4053    $$ = null;
4054   }
4055  ;
4056
4057generic_dimension
4058  : GENERIC_DIMENSION
4059    {
4060    if (lang_version < LanguageVersion.ISO_2)
4061      FeatureIsNotAvailable (GetLocation ($1), "generics");
4062
4063    $$ = $1;
4064    }
4065  ;
4066 
4067qualified_alias_member
4068  : IDENTIFIER DOUBLE_COLON
4069    {
4070    var lt = (LocatedToken) $1;
4071    if (lang_version == LanguageVersion.ISO_1)
4072      FeatureIsNotAvailable (lt.Location, "namespace alias qualifier");
4073    savedLocation = GetLocation ($2);
4074    $$ = lt;   
4075    }
4076  ;
4077
4078sizeof_expression
4079  : SIZEOF open_parens_any type CLOSE_PARENS
4080    {
4081    $$ = new SizeOf ((Expression) $3, GetLocation ($1));
4082    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4083    }
4084  | SIZEOF open_parens_any type error
4085    {
4086    Error_SyntaxError (yyToken);
4087
4088    $$ = new SizeOf ((Expression) $3, GetLocation ($1));
4089    lbag.AddLocation ($$, GetLocation ($2));
4090    }
4091  ;
4092
4093checked_expression
4094  : CHECKED open_parens_any expression CLOSE_PARENS
4095    {
4096    $$ = new CheckedExpr ((Expression) $3, GetLocation ($1));
4097    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4098    }
4099  | CHECKED error
4100    {
4101    Error_SyntaxError (yyToken);
4102
4103    $$ = new CheckedExpr (null, GetLocation ($1));
4104    }
4105  ;
4106
4107unchecked_expression
4108  : UNCHECKED open_parens_any expression CLOSE_PARENS
4109    {
4110    $$ = new UnCheckedExpr ((Expression) $3, GetLocation ($1));
4111    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4112    }
4113  | UNCHECKED error
4114    {
4115    Error_SyntaxError (yyToken);
4116
4117    $$ = new UnCheckedExpr (null, GetLocation ($1));
4118    }
4119  ;
4120
4121pointer_member_access
4122  : primary_expression OP_PTR IDENTIFIER opt_type_argument_list
4123    {
4124    var lt = (LocatedToken) $3;
4125    $$ = new MemberAccess (new Indirection ((Expression) $1, GetLocation ($2)), lt.Value, (TypeArguments) $4, lt.Location);
4126    }
4127  ;
4128
4129anonymous_method_expression
4130  : DELEGATE opt_anonymous_method_signature
4131    {
4132    start_anonymous (false, (ParametersCompiled) $2, false, GetLocation ($1));
4133    }
4134    block
4135    {
4136    $$ = end_anonymous ((ParametersBlock) $4);
4137    if ((ParametersCompiled) $2 != ParametersCompiled.Undefined) {
4138      lbag.AddLocation ($$, GetLocation ($1), PopLocation (), PopLocation ());
4139    } else {
4140      lbag.AddLocation ($$, GetLocation ($1));
4141    }
4142    }
4143  | ASYNC DELEGATE opt_anonymous_method_signature
4144    {
4145    start_anonymous (false, (ParametersCompiled) $3, true, GetLocation ($1));
4146    }
4147    block
4148    {
4149    $$ = end_anonymous ((ParametersBlock) $5);
4150   
4151    if ((ParametersCompiled) $3 != ParametersCompiled.Undefined) {
4152      lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2), PopLocation (), PopLocation ());
4153    } else {
4154      lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2));
4155    }
4156    }
4157  ;
4158
4159opt_anonymous_method_signature
4160  :
4161    {
4162    $$ = ParametersCompiled.Undefined;
4163    }
4164  | anonymous_method_signature
4165  ;
4166
4167anonymous_method_signature
4168  : OPEN_PARENS
4169    {
4170      valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
4171    }
4172    opt_formal_parameter_list CLOSE_PARENS
4173    {
4174    valid_param_mod = 0;
4175      $$ = $3;
4176      PushLocation (GetLocation ($3));
4177      PushLocation (GetLocation ($1));
4178    }
4179  ;
4180
4181default_value_expression
4182  : DEFAULT open_parens_any type CLOSE_PARENS
4183    {
4184    if (lang_version < LanguageVersion.ISO_2)
4185      FeatureIsNotAvailable (GetLocation ($1), "default value expression");
4186
4187    $$ = new DefaultValueExpression ((Expression) $3, GetLocation ($1));
4188    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4189    }
4190  ;
4191
4192unary_expression
4193  : primary_expression
4194  | BANG prefixed_unary_expression
4195    {
4196    $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, GetLocation ($1));
4197    }
4198  | TILDE prefixed_unary_expression
4199    {
4200    $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, GetLocation ($1));
4201    }
4202  | OPEN_PARENS_CAST type CLOSE_PARENS prefixed_unary_expression
4203    {
4204    $$ = new Cast ((FullNamedExpression) $2, (Expression) $4, GetLocation ($1));
4205    lbag.AddLocation ($$, GetLocation ($3));
4206    }
4207  | AWAIT prefixed_unary_expression
4208    {
4209    if (!async_block) {
4210       if (current_anonymous_method is LambdaExpression) {
4211        report.Error (4034, GetLocation ($1),
4212          "The `await' operator can only be used when its containing lambda expression is marked with the `async' modifier");
4213      } else if (current_anonymous_method != null) {
4214        report.Error (4035, GetLocation ($1),
4215          "The `await' operator can only be used when its containing anonymous method is marked with the `async' modifier");
4216      } else if (interactive_async != null) {
4217        current_block.Explicit.RegisterAsyncAwait ();
4218        interactive_async = true;
4219      } else {
4220        report.Error (4033, GetLocation ($1),
4221          "The `await' operator can only be used when its containing method is marked with the `async' modifier");
4222      }
4223    } else {
4224      current_block.Explicit.RegisterAsyncAwait ();
4225    }
4226   
4227    $$ = new Await ((Expression) $2, GetLocation ($1));
4228    }
4229  | BANG error
4230    {
4231    Error_SyntaxError (yyToken);
4232
4233    $$ = new Unary (Unary.Operator.LogicalNot, null, GetLocation ($1));
4234    }
4235  | TILDE error
4236    {
4237    Error_SyntaxError (yyToken);
4238
4239    $$ = new Unary (Unary.Operator.OnesComplement, null, GetLocation ($1));
4240    }
4241  | OPEN_PARENS_CAST type CLOSE_PARENS error
4242    {
4243    Error_SyntaxError (yyToken);
4244
4245    $$ = new Cast ((FullNamedExpression) $2, null, GetLocation ($1));
4246    lbag.AddLocation ($$, GetLocation ($3));
4247    }
4248  | AWAIT error
4249    {
4250    Error_SyntaxError (yyToken);
4251
4252    $$ = new Await (null, GetLocation ($1));
4253    }
4254  ;
4255
4256  //
4257  // The idea to split this out is from Rhys' grammar
4258  // to solve the problem with casts.
4259  //
4260prefixed_unary_expression
4261  : unary_expression
4262  | PLUS prefixed_unary_expression
4263    {
4264      $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, GetLocation ($1));
4265    }
4266  | MINUS prefixed_unary_expression
4267    {
4268    $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, GetLocation ($1));
4269    }
4270  | OP_INC prefixed_unary_expression
4271    {
4272    $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, (Expression) $2, GetLocation ($1));
4273    }
4274  | OP_DEC prefixed_unary_expression
4275    {
4276    $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, (Expression) $2, GetLocation ($1));
4277    }
4278  | STAR prefixed_unary_expression
4279    {
4280    $$ = new Indirection ((Expression) $2, GetLocation ($1));
4281    }
4282  | BITWISE_AND prefixed_unary_expression
4283    {
4284    $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, GetLocation ($1));
4285    }
4286  | PLUS error
4287    {
4288    Error_SyntaxError (yyToken);
4289
4290      $$ = new Unary (Unary.Operator.UnaryPlus, null, GetLocation ($1));
4291    }
4292  | MINUS error
4293    {
4294    Error_SyntaxError (yyToken);
4295
4296    $$ = new Unary (Unary.Operator.UnaryNegation, null, GetLocation ($1));
4297    }
4298  | OP_INC error
4299    {
4300    Error_SyntaxError (yyToken);
4301
4302    $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement, null, GetLocation ($1));
4303    }
4304  | OP_DEC error
4305    {
4306    Error_SyntaxError (yyToken);
4307
4308    $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement, null, GetLocation ($1));
4309    }
4310  | STAR error
4311    {
4312    Error_SyntaxError (yyToken);
4313
4314    $$ = new Indirection (null, GetLocation ($1));
4315    }
4316  | BITWISE_AND error
4317    {
4318    Error_SyntaxError (yyToken);
4319
4320    $$ = new Unary (Unary.Operator.AddressOf, null, GetLocation ($1));
4321    }
4322  ;
4323
4324multiplicative_expression
4325  : prefixed_unary_expression
4326  | multiplicative_expression STAR prefixed_unary_expression
4327    {
4328    $$ = new Binary (Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4329    lbag.AddLocation ($$, GetLocation ($2));
4330    }
4331  | multiplicative_expression DIV prefixed_unary_expression
4332    {
4333    $$ = new Binary (Binary.Operator.Division, (Expression) $1, (Expression) $3);
4334    lbag.AddLocation ($$, GetLocation ($2));
4335    }
4336  | multiplicative_expression PERCENT prefixed_unary_expression
4337    {
4338    $$ = new Binary (Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4339    lbag.AddLocation ($$, GetLocation ($2));
4340    }
4341  | multiplicative_expression STAR error
4342    {
4343    Error_SyntaxError (yyToken);
4344
4345    $$ = new Binary (Binary.Operator.Multiply, (Expression) $1, null);
4346    lbag.AddLocation ($$, GetLocation ($2));
4347    }
4348  | multiplicative_expression DIV error
4349    {
4350    Error_SyntaxError (yyToken);
4351
4352    $$ = new Binary (Binary.Operator.Division, (Expression) $1, null);
4353    lbag.AddLocation ($$, GetLocation ($2));
4354    }
4355  | multiplicative_expression PERCENT error
4356    {
4357    Error_SyntaxError (yyToken);
4358
4359    $$ = new Binary (Binary.Operator.Modulus, (Expression) $1, null);
4360    lbag.AddLocation ($$, GetLocation ($2));
4361    }
4362  ;
4363
4364additive_expression
4365  : multiplicative_expression
4366  | additive_expression PLUS multiplicative_expression
4367    {
4368    $$ = new Binary (Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4369    lbag.AddLocation ($$, GetLocation ($2));
4370    }
4371  | additive_expression MINUS multiplicative_expression
4372    {
4373    $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4374    lbag.AddLocation ($$, GetLocation ($2));
4375    }
4376  | additive_expression PLUS error
4377    {
4378    Error_SyntaxError (yyToken);
4379
4380    $$ = new Binary (Binary.Operator.Addition, (Expression) $1, null);
4381    lbag.AddLocation ($$, GetLocation ($2));
4382    }
4383  | additive_expression MINUS error
4384    {
4385    Error_SyntaxError (yyToken);
4386
4387    $$ = new Binary (Binary.Operator.Subtraction, (Expression) $1, null);
4388    lbag.AddLocation ($$, GetLocation ($2));
4389    }
4390  | additive_expression AS type
4391    {
4392    $$ = new As ((Expression) $1, (Expression) $3, GetLocation ($2));
4393    }
4394  | additive_expression IS is_match_expr opt_identifier
4395    {
4396    var is_expr = new Is ((Expression) $1, (Expression) $3, GetLocation ($2));
4397    if ($4 != null) {
4398      if (lang_version != LanguageVersion.Experimental)
4399        FeatureIsNotAvailable (GetLocation ($4), "type pattern matching");
4400
4401      var lt = (LocatedToken) $4;
4402      is_expr.Variable = new LocalVariable (current_block, lt.Value, lt.Location);
4403      current_block.AddLocalName (is_expr.Variable);
4404    }
4405
4406    $$ = is_expr;
4407    }
4408  | additive_expression AS error
4409    {
4410    Error_SyntaxError (yyToken);
4411
4412    $$ = new As ((Expression) $1, null, GetLocation ($2));
4413    }
4414  | additive_expression IS error
4415    {
4416    Error_SyntaxError (yyToken);
4417
4418    $$ = new Is ((Expression) $1, null, GetLocation ($2));
4419    }
4420  | AWAIT IS type
4421    {
4422    var lt = (LocatedToken) $1;
4423    $$ = new Is (new SimpleName (lt.Value, lt.Location), (Expression) $3, GetLocation ($2));
4424    }
4425  | AWAIT AS type
4426    {
4427    var lt = (LocatedToken) $1;
4428    $$ = new As (new SimpleName (lt.Value, lt.Location), (Expression) $3, GetLocation ($2));
4429    }
4430  ;
4431
4432is_match_expr
4433  : match_type
4434  | match_type rank_specifiers
4435    {
4436    if ($1 is VarExpr)
4437      $1 = new SimpleName ("var", ((VarExpr) $1).Location);
4438
4439    $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
4440    }
4441  | literal
4442  | PLUS prefixed_unary_expression
4443    {
4444    $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, GetLocation ($1));
4445    }
4446  | MINUS prefixed_unary_expression
4447    {
4448    $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, GetLocation ($1));
4449    }
4450  ;
4451
4452match_type
4453  : primary_expression_or_type opt_nullable
4454    {
4455    Expression expr = (Expression) $1;
4456    if ($2 == null) {
4457      SimpleName sn = expr as SimpleName;
4458      if (sn != null && sn.Name == "var")
4459        $$ = new VarExpr (sn.Location);
4460      else
4461        $$ = $1;
4462    } else if (expr is ATypeNameExpression) {
4463      $$ = new ComposedCast ((ATypeNameExpression)expr, (ComposedTypeSpecifier) $2);
4464    } else {
4465      Error_ExpectingTypeName (expr);
4466      $$ = null;
4467    }
4468    }
4469  | primary_expression_or_type pointer_stars
4470    {
4471    ATypeNameExpression expr = $1 as ATypeNameExpression;
4472
4473    if (expr != null) {
4474      $$ = new ComposedCast (expr, (ComposedTypeSpecifier) $2);
4475    } else {
4476      Error_ExpectingTypeName ((Expression)$1);
4477      $$ = expr;
4478    }
4479    }
4480  | builtin_type_expression
4481  | void_invalid
4482  ;
4483
4484shift_expression
4485  : additive_expression
4486  | shift_expression OP_SHIFT_LEFT additive_expression
4487    {
4488    $$ = new Binary (Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4489    lbag.AddLocation ($$, GetLocation ($2));
4490    }
4491  | shift_expression OP_SHIFT_RIGHT additive_expression
4492    {
4493    $$ = new Binary (Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4494    lbag.AddLocation ($$, GetLocation ($2));
4495    }
4496  | shift_expression OP_SHIFT_LEFT error
4497    {
4498    Error_SyntaxError (yyToken);
4499
4500    $$ = new Binary (Binary.Operator.LeftShift, (Expression) $1, null);
4501    lbag.AddLocation ($$, GetLocation ($2));
4502    }
4503  | shift_expression OP_SHIFT_RIGHT error
4504    {
4505    Error_SyntaxError (yyToken);
4506
4507    $$ = new Binary (Binary.Operator.RightShift, (Expression) $1, null);
4508    lbag.AddLocation ($$, GetLocation ($2));
4509    }
4510  ;
4511
4512relational_expression
4513  : shift_expression
4514  | relational_expression OP_LT shift_expression
4515    {
4516    $$ = new Binary (Binary.Operator.LessThan, (Expression) $1, (Expression) $3);
4517    lbag.AddLocation ($$, GetLocation ($2));
4518    }
4519  | relational_expression OP_GT shift_expression
4520    {
4521    $$ = new Binary (Binary.Operator.GreaterThan, (Expression) $1, (Expression) $3);
4522    lbag.AddLocation ($$, GetLocation ($2));
4523    }
4524  | relational_expression OP_LE shift_expression
4525    {
4526    $$ = new Binary (Binary.Operator.LessThanOrEqual, (Expression) $1, (Expression) $3);
4527    lbag.AddLocation ($$, GetLocation ($2));
4528    }
4529  | relational_expression OP_GE shift_expression
4530    {
4531    $$ = new Binary (Binary.Operator.GreaterThanOrEqual, (Expression) $1, (Expression) $3);
4532    lbag.AddLocation ($$, GetLocation ($2));
4533    }
4534  | relational_expression OP_LT error
4535    {
4536    Error_SyntaxError (yyToken);
4537
4538    $$ = new Binary (Binary.Operator.LessThan, (Expression) $1, null);
4539    lbag.AddLocation ($$, GetLocation ($2));
4540    }
4541  | relational_expression OP_GT error
4542    {
4543    Error_SyntaxError (yyToken);
4544
4545    $$ = new Binary (Binary.Operator.GreaterThan, (Expression) $1, null);
4546    lbag.AddLocation ($$, GetLocation ($2));
4547    }
4548  | relational_expression OP_LE error
4549    {
4550    Error_SyntaxError (yyToken);
4551
4552    $$ = new Binary (Binary.Operator.LessThanOrEqual, (Expression) $1, null);
4553    lbag.AddLocation ($$, GetLocation ($2));
4554    }
4555  | relational_expression OP_GE error
4556    {
4557    Error_SyntaxError (yyToken);
4558
4559    $$ = new Binary (Binary.Operator.GreaterThanOrEqual, (Expression) $1, null);
4560    lbag.AddLocation ($$, GetLocation ($2));
4561    }
4562  ;
4563
4564equality_expression
4565  : relational_expression
4566  | equality_expression OP_EQ relational_expression
4567    {
4568    $$ = new Binary (Binary.Operator.Equality, (Expression) $1, (Expression) $3);
4569    lbag.AddLocation ($$, GetLocation ($2));
4570    }
4571  | equality_expression OP_NE relational_expression
4572    {
4573    $$ = new Binary (Binary.Operator.Inequality, (Expression) $1, (Expression) $3);
4574    lbag.AddLocation ($$, GetLocation ($2));
4575    }
4576  | equality_expression OP_EQ error
4577    {
4578    Error_SyntaxError (yyToken);
4579
4580    $$ = new Binary (Binary.Operator.Equality, (Expression) $1, null);
4581    lbag.AddLocation ($$, GetLocation ($2));
4582    }
4583  | equality_expression OP_NE error
4584    {
4585    Error_SyntaxError (yyToken);
4586
4587    $$ = new Binary (Binary.Operator.Inequality, (Expression) $1, null);
4588    lbag.AddLocation ($$, GetLocation ($2));
4589    }
4590  ;
4591
4592and_expression
4593  : equality_expression
4594  | and_expression BITWISE_AND equality_expression
4595    {
4596    $$ = new Binary (Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4597    lbag.AddLocation ($$, GetLocation ($2));
4598    }
4599  | and_expression BITWISE_AND error
4600    {
4601    Error_SyntaxError (yyToken);
4602
4603    $$ = new Binary (Binary.Operator.BitwiseAnd, (Expression) $1, null);
4604    lbag.AddLocation ($$, GetLocation ($2));
4605    }
4606  ;
4607
4608exclusive_or_expression
4609  : and_expression
4610  | exclusive_or_expression CARRET and_expression
4611    {
4612    $$ = new Binary (Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4613    lbag.AddLocation ($$, GetLocation ($2));
4614    }
4615  | exclusive_or_expression CARRET error
4616    {
4617    Error_SyntaxError (yyToken);
4618
4619    $$ = new Binary (Binary.Operator.ExclusiveOr, (Expression) $1, null);
4620    lbag.AddLocation ($$, GetLocation ($2));
4621    }
4622  ;
4623
4624inclusive_or_expression
4625  : exclusive_or_expression
4626  | inclusive_or_expression BITWISE_OR exclusive_or_expression
4627    {
4628    $$ = new Binary (Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4629    lbag.AddLocation ($$, GetLocation ($2));
4630    }
4631  | inclusive_or_expression BITWISE_OR error
4632    {
4633    Error_SyntaxError (yyToken);
4634
4635    $$ = new Binary (Binary.Operator.BitwiseOr, (Expression) $1, null);
4636    lbag.AddLocation ($$, GetLocation ($2));
4637    }
4638  ;
4639
4640conditional_and_expression
4641  : inclusive_or_expression
4642  | conditional_and_expression OP_AND inclusive_or_expression
4643    {
4644    $$ = new Binary (Binary.Operator.LogicalAnd, (Expression) $1, (Expression) $3);
4645    lbag.AddLocation ($$, GetLocation ($2));
4646    }
4647  | conditional_and_expression OP_AND error
4648    {
4649    Error_SyntaxError (yyToken);
4650
4651    $$ = new Binary (Binary.Operator.LogicalAnd, (Expression) $1, null);
4652    lbag.AddLocation ($$, GetLocation ($2));
4653    }
4654  ;
4655
4656conditional_or_expression
4657  : conditional_and_expression
4658  | conditional_or_expression OP_OR conditional_and_expression
4659    {
4660    $$ = new Binary (Binary.Operator.LogicalOr, (Expression) $1, (Expression) $3);
4661    lbag.AddLocation ($$, GetLocation ($2));
4662    }
4663  | conditional_or_expression OP_OR error
4664    {
4665    Error_SyntaxError (yyToken);
4666
4667    $$ = new Binary (Binary.Operator.LogicalOr, (Expression) $1, null);
4668    lbag.AddLocation ($$, GetLocation ($2));
4669    }
4670  ;
4671 
4672null_coalescing_expression
4673  : conditional_or_expression
4674  | conditional_or_expression OP_COALESCING null_coalescing_expression
4675    {
4676    if (lang_version < LanguageVersion.ISO_2)
4677      FeatureIsNotAvailable (GetLocation ($2), "null coalescing operator");
4678     
4679    $$ = new Nullable.NullCoalescingOperator ((Expression) $1, (Expression) $3);
4680    lbag.AddLocation ($$, GetLocation ($2));
4681    }
4682  ;
4683
4684conditional_expression
4685  : null_coalescing_expression
4686  | null_coalescing_expression INTERR expression COLON expression
4687    {
4688    $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, (Expression) $5, GetLocation ($2));
4689    lbag.AddLocation ($$, GetLocation ($4));
4690    }
4691  | null_coalescing_expression INTERR expression error
4692    {
4693    Error_SyntaxError (yyToken);
4694
4695    $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, null, GetLocation ($2));
4696    }
4697  | null_coalescing_expression INTERR expression COLON error
4698    {
4699    Error_SyntaxError (yyToken);
4700
4701    $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, null, GetLocation ($2));
4702    lbag.AddLocation ($$, GetLocation ($4));
4703    }
4704  | null_coalescing_expression INTERR expression COLON CLOSE_BRACE
4705    {
4706    Error_SyntaxError (Token.CLOSE_BRACE);
4707
4708    $$ = new Conditional (new BooleanExpression ((Expression) $1), (Expression) $3, null, GetLocation ($2));
4709    lbag.AddLocation ($$, GetLocation ($4));
4710    lexer.putback ('}');
4711    }
4712  ;
4713
4714assignment_expression
4715  : prefixed_unary_expression ASSIGN expression
4716    {
4717    $$ = new SimpleAssign ((Expression) $1, (Expression) $3);
4718    lbag.AddLocation ($$, GetLocation ($2));
4719    }
4720  | prefixed_unary_expression OP_MULT_ASSIGN expression
4721    {
4722    $$ = new CompoundAssign (Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
4723    lbag.AddLocation ($$, GetLocation ($2));
4724    }
4725  | prefixed_unary_expression OP_DIV_ASSIGN expression
4726    {
4727    $$ = new CompoundAssign (Binary.Operator.Division, (Expression) $1, (Expression) $3);
4728    lbag.AddLocation ($$, GetLocation ($2));
4729    }
4730  | prefixed_unary_expression OP_MOD_ASSIGN expression
4731    {
4732    $$ = new CompoundAssign (Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
4733    lbag.AddLocation ($$, GetLocation ($2));
4734    }
4735  | prefixed_unary_expression OP_ADD_ASSIGN expression
4736    {
4737    $$ = new CompoundAssign (Binary.Operator.Addition, (Expression) $1, (Expression) $3);
4738    lbag.AddLocation ($$, GetLocation ($2));
4739    }
4740  | prefixed_unary_expression OP_SUB_ASSIGN expression
4741    {
4742    $$ = new CompoundAssign (Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
4743    lbag.AddLocation ($$, GetLocation ($2));
4744    }
4745  | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
4746    {
4747    $$ = new CompoundAssign (Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
4748    lbag.AddLocation ($$, GetLocation ($2));
4749    }
4750  | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
4751    {
4752    $$ = new CompoundAssign (Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
4753    lbag.AddLocation ($$, GetLocation ($2));
4754    }
4755  | prefixed_unary_expression OP_AND_ASSIGN expression
4756    {
4757    $$ = new CompoundAssign (Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
4758    lbag.AddLocation ($$, GetLocation ($2));
4759    }
4760  | prefixed_unary_expression OP_OR_ASSIGN expression
4761    {
4762    $$ = new CompoundAssign (Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
4763    lbag.AddLocation ($$, GetLocation ($2));
4764    }
4765  | prefixed_unary_expression OP_XOR_ASSIGN expression
4766    {
4767    $$ = new CompoundAssign (Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
4768    lbag.AddLocation ($$, GetLocation ($2));
4769    }
4770  ;
4771
4772lambda_parameter_list
4773  : lambda_parameter
4774    {
4775    var pars = new List<Parameter> (4);
4776    pars.Add ((Parameter) $1);
4777    parameterListCommas.Clear ();
4778    $$ = pars;
4779    }
4780  | lambda_parameter_list COMMA lambda_parameter
4781    {
4782    var pars = (List<Parameter>) $1;
4783    Parameter p = (Parameter)$3;
4784    if (pars[0].GetType () != p.GetType ()) {
4785      report.Error (748, p.Location, "All lambda parameters must be typed either explicitly or implicitly");
4786    }
4787   
4788    pars.Add (p);
4789    parameterListCommas.Add (GetLocation ($2));
4790
4791    $$ = pars;
4792    }
4793  ;
4794
4795lambda_parameter
4796  : parameter_modifier parameter_type identifier_inside_body
4797    {
4798    var lt = (LocatedToken) $3;
4799
4800    $$ = new Parameter ((FullNamedExpression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
4801    }
4802  | parameter_type identifier_inside_body
4803    {
4804    var lt = (LocatedToken) $2;
4805
4806    $$ = new Parameter ((FullNamedExpression) $1, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
4807    }
4808  | IDENTIFIER
4809    {
4810      var lt = (LocatedToken) $1;
4811    $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4812    }
4813  | AWAIT
4814    {
4815      var lt = (LocatedToken) Error_AwaitAsIdentifier ($1);
4816    $$ = new ImplicitLambdaParameter (lt.Value, lt.Location);
4817    }
4818  ;
4819
4820opt_lambda_parameter_list
4821  : /* empty */     { $$ = ParametersCompiled.EmptyReadOnlyParameters; }
4822  | lambda_parameter_list   {
4823    var pars_list = (List<Parameter>) $1;
4824    $$ = new ParametersCompiled (pars_list.ToArray ());
4825    lbag.AddLocation ($$, parameterListCommas);
4826    }
4827  ;
4828
4829lambda_expression_body
4830  : {
4831    start_block (Location.Null);
4832    }
4833    expression  // All expressions must handle error or current block won't be restored and breaking ast completely
4834    {
4835    Block b = end_block (Location.Null);
4836    b.IsCompilerGenerated = true;
4837    b.AddStatement (new ContextualReturn ((Expression) $2));
4838    $$ = b;
4839    }
4840  | block
4841  | error
4842    {
4843      // Handles only cases like foo = x.FirstOrDefault (l => );
4844      // where we must restore current_variable
4845    Block b = end_block (Location.Null);
4846    b.IsCompilerGenerated = true;
4847
4848    Error_SyntaxError (yyToken);
4849    $$ = null;
4850    }
4851  ;
4852
4853expression_or_error
4854  : expression
4855  | error
4856    {
4857    Error_SyntaxError (yyToken);
4858    $$ = null;
4859    }
4860  ;
4861 
4862lambda_expression
4863  : IDENTIFIER ARROW
4864    {
4865    var lt = (LocatedToken) $1;
4866    Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
4867    start_anonymous (true, new ParametersCompiled (p), false, lt.Location);
4868    }
4869    lambda_expression_body
4870    {
4871    $$ = end_anonymous ((ParametersBlock) $4);
4872    lbag.AddLocation ($$, GetLocation ($2));
4873    }
4874  | AWAIT ARROW
4875    {
4876    var lt = (LocatedToken) Error_AwaitAsIdentifier ($1);
4877    Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
4878    start_anonymous (true, new ParametersCompiled (p), false, lt.Location);
4879    }
4880    lambda_expression_body
4881    {
4882    $$ = end_anonymous ((ParametersBlock) $4);
4883    lbag.AddLocation ($$, GetLocation ($2));
4884    }
4885  | ASYNC identifier_inside_body ARROW
4886    {
4887    var lt = (LocatedToken) $2;
4888    Parameter p = new ImplicitLambdaParameter (lt.Value, lt.Location);
4889    start_anonymous (true, new ParametersCompiled (p), true, lt.Location);
4890    }
4891    lambda_expression_body
4892    {
4893    $$ = end_anonymous ((ParametersBlock) $5);
4894    lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
4895    }
4896  | OPEN_PARENS_LAMBDA
4897    {
4898      valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
4899    }
4900    opt_lambda_parameter_list CLOSE_PARENS ARROW
4901    {
4902      valid_param_mod = 0;
4903    start_anonymous (true, (ParametersCompiled) $3, false, GetLocation ($1));
4904    }
4905    lambda_expression_body
4906    {
4907    $$ = end_anonymous ((ParametersBlock) $7);
4908    lbag.AddLocation ($$, GetLocation ($1), GetLocation ($4), GetLocation ($5));
4909    }
4910  | ASYNC OPEN_PARENS_LAMBDA
4911    {
4912      valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;   
4913    }
4914    opt_lambda_parameter_list CLOSE_PARENS ARROW
4915    {
4916      valid_param_mod = 0;
4917    start_anonymous (true, (ParametersCompiled) $4, true, GetLocation ($1));
4918    }
4919    lambda_expression_body
4920    {
4921    $$ = end_anonymous ((ParametersBlock) $8);
4922    lbag.AddLocation ($$, GetLocation ($1), GetLocation ($2), GetLocation ($5), GetLocation ($6));
4923    }
4924  ;
4925
4926expression
4927  : assignment_expression
4928  | non_assignment_expression
4929  ;
4930 
4931non_assignment_expression
4932  : conditional_expression
4933  | lambda_expression
4934  | query_expression
4935  | ARGLIST
4936    {
4937    $$ = new ArglistAccess (GetLocation ($1));
4938    }
4939  ;
4940 
4941undocumented_expressions
4942  : REFVALUE OPEN_PARENS non_assignment_expression COMMA type CLOSE_PARENS
4943    {
4944    $$ = new RefValueExpr ((Expression) $3, (FullNamedExpression) $5, GetLocation ($1));
4945    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4), GetLocation ($6));
4946    }
4947  | REFTYPE open_parens_any expression CLOSE_PARENS
4948    {
4949    $$ = new RefTypeExpr ((Expression) $3, GetLocation ($1));
4950    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
4951    }
4952  | MAKEREF open_parens_any expression CLOSE_PARENS
4953    {
4954    $$ = new MakeRefExpr ((Expression) $3, GetLocation ($1));
4955    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));   
4956    }
4957  ;
4958
4959constant_expression
4960  : expression
4961  ;
4962
4963boolean_expression
4964  : expression
4965    {
4966    $$ = new BooleanExpression ((Expression) $1);
4967    }
4968  ;
4969
4970opt_primary_parameters
4971  : /* empty */
4972    {
4973      $$ = null;
4974    }
4975  | primary_parameters
4976  ;
4977
4978primary_parameters
4979  : OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
4980    {
4981    $$ = $2;
4982
4983    // Cannot use opt_formal_parameter_list because it can be shared instance for empty parameters
4984      lbag.AppendToMember (current_container, GetLocation ($1), GetLocation ($3));
4985
4986    if (lang_version < LanguageVersion.V_6)
4987      FeatureIsNotAvailable (GetLocation ($1), "primary constructor");
4988    }
4989  ;
4990
4991opt_primary_parameters_with_class_base
4992  : /* empty */
4993    {
4994      $$ = null;
4995    }
4996  | class_base
4997    {
4998    $$ = null;
4999    }
5000  | primary_parameters
5001    {
5002    $$ = $1;
5003    }
5004  | primary_parameters class_base
5005    {
5006    $$ = $1;
5007    }
5008  | primary_parameters class_base OPEN_PARENS
5009    {
5010    ++lexer.parsing_block;
5011    current_type.PrimaryConstructorBaseArgumentsStart = GetLocation ($3);
5012    }
5013    opt_argument_list CLOSE_PARENS
5014    {
5015    lbag.AppendToMember (current_container, GetLocation ($6));
5016    current_type.PrimaryConstructorBaseArguments = (Arguments) $5;
5017    --lexer.parsing_block;
5018
5019    $$ = $1;
5020    }
5021  ;
5022
5023//
5024// 10 classes
5025//
5026class_declaration
5027  : opt_attributes
5028    opt_modifiers
5029    opt_partial
5030    CLASS
5031    {
5032    }
5033    type_declaration_name
5034    {
5035    lexer.ConstraintsParsing = true;
5036
5037    Class c = new Class (current_container, (MemberName) $6, (Modifiers) $2, (Attributes) $1);
5038    if (((c.ModFlags & Modifiers.STATIC) != 0) && lang_version == LanguageVersion.ISO_1) {
5039      FeatureIsNotAvailable (c.Location, "static classes");
5040    }
5041     
5042    push_current_container (c, $3);
5043    lbag.AddMember (current_container, GetModifierLocations (), GetLocation ($4));
5044    valid_param_mod = ParameterModifierType.PrimaryConstructor;
5045    }
5046    opt_primary_parameters_with_class_base
5047    opt_type_parameter_constraints_clauses
5048    {
5049    valid_param_mod = 0;
5050    lexer.ConstraintsParsing = false;
5051
5052    if ($8 != null)
5053      current_type.PrimaryConstructorParameters = (ParametersCompiled) $8;
5054
5055    if ($9 != null)
5056      current_container.SetConstraints ((List<Constraints>) $9);
5057
5058    if (doc_support) {
5059      current_container.PartialContainer.DocComment = Lexer.consume_doc_comment ();
5060      Lexer.doc_state = XmlCommentState.Allowed;
5061    }
5062   
5063    lexer.parsing_modifiers = true;
5064    }
5065    OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
5066    {
5067    --lexer.parsing_declaration;
5068    if (doc_support)
5069      Lexer.doc_state = XmlCommentState.Allowed;
5070    }
5071    opt_semicolon
5072    {
5073    if ($15 == null) {
5074      lbag.AppendToMember (current_container, GetLocation ($11), GetLocation ($13));
5075    } else {
5076      lbag.AppendToMember (current_container, GetLocation ($11), GetLocation ($13), GetLocation ($15));
5077    }
5078    $$ = pop_current_class ();
5079    }
5080  ;
5081
5082opt_partial
5083  : /* empty */
5084    { $$ = null; }
5085  | PARTIAL
5086    { $$ = $1; StoreModifierLocation (Modifiers.PARTIAL, GetLocation ($1)); } // location
5087  ;
5088
5089opt_modifiers
5090  : /* empty */
5091    {
5092      mod_locations = null;
5093    $$ = ModifierNone;
5094    lexer.parsing_modifiers = false;
5095    }
5096  | modifiers
5097    {
5098    lexer.parsing_modifiers = false;   
5099    }
5100  ;
5101
5102modifiers
5103  : modifier
5104  | modifiers modifier
5105    {
5106    var m1 = (Modifiers) $1;
5107    var m2 = (Modifiers) $2;
5108
5109    if ((m1 & m2) != 0) {
5110      report.Error (1004, lexer.Location - ModifiersExtensions.Name (m2).Length,
5111        "Duplicate `{0}' modifier", ModifiersExtensions.Name (m2));
5112    } else if ((m2 & Modifiers.AccessibilityMask) != 0 && (m1 & Modifiers.AccessibilityMask) != 0 &&
5113      ((m2 | m1 & Modifiers.AccessibilityMask) != (Modifiers.PROTECTED | Modifiers.INTERNAL))) {
5114      report.Error (107, lexer.Location - ModifiersExtensions.Name (m2).Length,
5115        "More than one protection modifier specified");
5116    }
5117   
5118    $$ = m1 | m2;
5119    }
5120  ;
5121
5122modifier
5123  : NEW
5124    {
5125    $$ = Modifiers.NEW;
5126    StoreModifierLocation ($$, GetLocation ($1));
5127   
5128    if (current_container.Kind == MemberKind.Namespace)
5129      report.Error (1530, GetLocation ($1), "Keyword `new' is not allowed on namespace elements");
5130    }
5131  | PUBLIC
5132    {
5133    $$ = Modifiers.PUBLIC;
5134    StoreModifierLocation ($$, GetLocation ($1));
5135    }
5136  | PROTECTED
5137    {
5138    $$ = Modifiers.PROTECTED;
5139    StoreModifierLocation ($$, GetLocation ($1));
5140    }
5141  | INTERNAL
5142    {
5143    $$ = Modifiers.INTERNAL;
5144    StoreModifierLocation ($$, GetLocation ($1));
5145    }
5146  | PRIVATE
5147    {
5148    $$ = Modifiers.PRIVATE;
5149    StoreModifierLocation ($$, GetLocation ($1));
5150    }
5151  | ABSTRACT
5152    {
5153    $$ = Modifiers.ABSTRACT;
5154    StoreModifierLocation ($$, GetLocation ($1));
5155    }
5156  | SEALED
5157    {
5158    $$ = Modifiers.SEALED;
5159    StoreModifierLocation ($$, GetLocation ($1));
5160    }
5161  | STATIC
5162    {
5163    $$ = Modifiers.STATIC;
5164    StoreModifierLocation ($$, GetLocation ($1));
5165    }
5166  | READONLY
5167    {
5168    $$ = Modifiers.READONLY;
5169    StoreModifierLocation ($$, GetLocation ($1));
5170    }
5171  | VIRTUAL
5172    {
5173    $$ = Modifiers.VIRTUAL;
5174    StoreModifierLocation ($$, GetLocation ($1));
5175    }
5176  | OVERRIDE
5177    {
5178    $$ = Modifiers.OVERRIDE;
5179    StoreModifierLocation ($$, GetLocation ($1));
5180    }
5181  | EXTERN
5182    {
5183    $$ = Modifiers.EXTERN;
5184    StoreModifierLocation ($$, GetLocation ($1));
5185    }
5186  | VOLATILE
5187    {
5188    $$ = Modifiers.VOLATILE;
5189    StoreModifierLocation ($$, GetLocation ($1));
5190    }
5191  | UNSAFE
5192    {
5193    $$ = Modifiers.UNSAFE;
5194    StoreModifierLocation ($$, GetLocation ($1));
5195    if (!settings.Unsafe)
5196      Error_UnsafeCodeNotAllowed (GetLocation ($1));
5197    }
5198  | ASYNC
5199    {
5200    $$ = Modifiers.ASYNC;
5201    StoreModifierLocation ($$, GetLocation ($1));
5202    }
5203  ;
5204 
5205opt_class_base
5206  : /* empty */
5207  | class_base
5208  ;
5209
5210class_base
5211  : COLON type_list
5212   {
5213    current_type.SetBaseTypes ((List<FullNamedExpression>) $2);
5214    lbag.AppendToMember (current_type, GetLocation ($1));
5215   }
5216  | COLON type_list error
5217    {
5218    Error_SyntaxError (yyToken);
5219
5220    current_type.SetBaseTypes ((List<FullNamedExpression>) $2);
5221    }
5222  ;
5223
5224opt_type_parameter_constraints_clauses
5225  : /* empty */
5226  | type_parameter_constraints_clauses
5227    {
5228    $$ = $1;
5229    }
5230  ;
5231
5232type_parameter_constraints_clauses
5233  : type_parameter_constraints_clause
5234    {
5235    var constraints = new List<Constraints> (1);
5236    constraints.Add ((Constraints) $1);
5237    $$ = constraints;
5238    }
5239  | type_parameter_constraints_clauses type_parameter_constraints_clause
5240    {
5241    var constraints = (List<Constraints>) $1;
5242    Constraints new_constraint = (Constraints)$2;
5243
5244    foreach (Constraints c in constraints) {
5245      if (new_constraint.TypeParameter.Value == c.TypeParameter.Value) {
5246        report.Error (409, new_constraint.Location,
5247          "A constraint clause has already been specified for type parameter `{0}'",
5248          new_constraint.TypeParameter.Value);
5249      }
5250    }
5251
5252    constraints.Add (new_constraint);
5253    $$ = constraints;
5254    }
5255  ;
5256
5257type_parameter_constraints_clause
5258  : WHERE IDENTIFIER COLON type_parameter_constraints
5259    {
5260    var lt = (LocatedToken) $2;
5261    $$ = new Constraints (new SimpleMemberName (lt.Value, lt.Location), (List<FullNamedExpression>) $4, GetLocation ($1));
5262    lbag.AddLocation ($$, GetLocation ($3));
5263    }
5264  | WHERE IDENTIFIER error
5265    {
5266    Error_SyntaxError (yyToken);
5267   
5268    var lt = (LocatedToken) $2;
5269    $$ = new Constraints (new SimpleMemberName (lt.Value, lt.Location), null, GetLocation ($1));
5270    }
5271  ;
5272
5273type_parameter_constraints
5274  : type_parameter_constraint
5275    {
5276    var constraints = new List<FullNamedExpression> (1);
5277    constraints.Add ((FullNamedExpression) $1);
5278    $$ = constraints;
5279    }
5280  | type_parameter_constraints COMMA type_parameter_constraint
5281    {
5282    var constraints = (List<FullNamedExpression>) $1;
5283    var prev = constraints [constraints.Count - 1] as SpecialContraintExpr;
5284    if (prev != null && (prev.Constraint & SpecialConstraint.Constructor) != 0) {     
5285      report.Error (401, GetLocation ($2), "The `new()' constraint must be the last constraint specified");
5286    }
5287   
5288    prev = $3 as SpecialContraintExpr;
5289    if (prev != null) {
5290      if ((prev.Constraint & (SpecialConstraint.Class | SpecialConstraint.Struct)) != 0) {
5291        report.Error (449, prev.Location, "The `class' or `struct' constraint must be the first constraint specified");     
5292      } else {
5293        prev = constraints [0] as SpecialContraintExpr;
5294        if (prev != null && (prev.Constraint & SpecialConstraint.Struct) != 0) {     
5295          report.Error (451, GetLocation ($3), "The `new()' constraint cannot be used with the `struct' constraint");
5296        }
5297      }
5298    }
5299
5300    constraints.Add ((FullNamedExpression) $3);
5301    lbag.AddLocation (constraints, GetLocation ($2));
5302    $$ = constraints;
5303    }
5304  ;
5305
5306type_parameter_constraint
5307  : type
5308    {
5309    if ($1 is ComposedCast)
5310      report.Error (706, GetLocation ($1), "Invalid constraint type `{0}'", ((ComposedCast)$1).GetSignatureForError ());
5311   
5312      $$ = $1;
5313    }
5314  | NEW OPEN_PARENS CLOSE_PARENS
5315    {
5316    $$ = new SpecialContraintExpr (SpecialConstraint.Constructor, GetLocation ($1));
5317    lbag.AddLocation ($$, GetLocation ($2), GetLocation ($3));
5318    }
5319  | CLASS
5320    {
5321    $$ = new SpecialContraintExpr (SpecialConstraint.Class, GetLocation ($1));
5322    }
5323  | STRUCT
5324    {
5325    $$ = new SpecialContraintExpr (SpecialConstraint.Struct, GetLocation ($1));
5326    }
5327  ;
5328
5329opt_type_parameter_variance
5330  : /* empty */
5331    {
5332    $$ = null;
5333    }
5334  | type_parameter_variance
5335    {
5336    if (lang_version <= LanguageVersion.V_3)
5337      FeatureIsNotAvailable (lexer.Location, "generic type variance");
5338   
5339    $$ = $1;
5340    }
5341  ;
5342
5343type_parameter_variance
5344  : OUT
5345    {
5346    $$ = new VarianceDecl (Variance.Covariant, GetLocation ($1));
5347    savedLocation = GetLocation ($1);
5348    }
5349  | IN
5350    {
5351    $$ = new VarianceDecl (Variance.Contravariant, GetLocation ($1));
5352    savedLocation = GetLocation ($1);
5353    }
5354  ;
5355
5356//
5357// Statements (8.2)
5358//
5359
5360//
5361// A block is "contained" on the following places:
5362//  method_body
5363//  property_declaration as part of the accessor body (get/set)
5364//      operator_declaration
5365//  constructor_declaration
5366//  destructor_declaration
5367//  event_declaration as part of add_accessor_declaration or remove_accessor_declaration
5368//     
5369block
5370  : OPEN_BRACE 
5371    {
5372    ++lexer.parsing_block;
5373    start_block (GetLocation ($1));
5374    }
5375    opt_statement_list block_end
5376    {
5377    $$ = $4;
5378    }
5379  ;
5380
5381block_end
5382  : CLOSE_BRACE
5383    {
5384    --lexer.parsing_block;
5385    $$ = end_block (GetLocation ($1));
5386    }
5387  | COMPLETE_COMPLETION
5388    {
5389    --lexer.parsing_block;
5390    $$ = end_block (lexer.Location);
5391    }
5392  ;
5393
5394
5395block_prepared
5396  : OPEN_BRACE
5397    {
5398    ++lexer.parsing_block;
5399    current_block.StartLocation = GetLocation ($1);
5400    }
5401    opt_statement_list CLOSE_BRACE
5402    {
5403    --lexer.parsing_block;
5404    $$ = end_block (GetLocation ($4));
5405    } | CLOSE_BRACE
5406    {
5407    report.Error (1525, GetLocation ($1), "Unexpected symbol '}', expected '{'");
5408    lexer.putback ('}');
5409    $$ = end_block (GetLocation ($1));
5410    }
5411  ;
5412
5413block_prepared_strict
5414  : OPEN_BRACE
5415    {
5416    ++lexer.parsing_block;
5417    current_block.StartLocation = GetLocation ($1);
5418    }
5419    opt_statement_list CLOSE_BRACE
5420    {
5421    --lexer.parsing_block;
5422    $$ = end_block (GetLocation ($4));
5423    }
5424  ;
5425
5426opt_statement_list
5427  : /* empty */
5428  | statement_list
5429  ;
5430
5431statement_list
5432  : statement
5433  | statement_list statement
5434  ;
5435
5436statement
5437  : block_variable_declaration
5438    {
5439    current_block.AddStatement ((Statement) $1);
5440    }
5441  | valid_declaration_statement
5442    {
5443    current_block.AddStatement ((Statement) $1);
5444    }
5445  | labeled_statement
5446// WORKAROUND:Remove that rule, if it is >really< fixed.
5447  | IDENTIFIER error
5448  {
5449    Error_SyntaxError (yyToken);
5450    var lt =(LocatedToken) $1;
5451    var sn = new SimpleName (lt.Value, lt.Location);
5452    current_block.AddStatement(new StatementErrorExpression (sn));
5453    $$ = null;
5454  }
5455////////
5456  | error
5457    {
5458    Error_SyntaxError (yyToken);
5459    $$ = null;
5460    }
5461  ;
5462
5463//
5464// The interactive_statement and its derivatives are only
5465// used to provide a special version of `expression_statement'
5466// that has a side effect of assigning the expression to
5467// $retval
5468//
5469interactive_statement_list
5470  : interactive_statement
5471  | interactive_statement_list interactive_statement
5472  ;
5473
5474interactive_statement
5475  : block_variable_declaration
5476    {
5477    current_block.AddStatement ((Statement) $1);
5478    }
5479  | interactive_valid_declaration_statement
5480    {
5481    current_block.AddStatement ((Statement) $1);
5482    }
5483  | labeled_statement
5484  ;
5485
5486valid_declaration_statement
5487  : block
5488  | empty_statement
5489  | expression_statement
5490  | selection_statement
5491  | iteration_statement
5492  | jump_statement     
5493  | try_statement
5494  | checked_statement
5495  | unchecked_statement
5496  | lock_statement
5497  | using_statement
5498  | unsafe_statement
5499  | fixed_statement
5500  ;
5501
5502interactive_valid_declaration_statement
5503  : block
5504  | empty_statement
5505        | interactive_expression_statement
5506  | selection_statement
5507  | iteration_statement
5508  | jump_statement     
5509  | try_statement
5510  | checked_statement
5511  | unchecked_statement
5512  | lock_statement
5513  | using_statement
5514  | unsafe_statement
5515  | fixed_statement
5516  ;
5517
5518embedded_statement
5519  : valid_declaration_statement
5520  | block_variable_declaration
5521    {
5522      report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
5523      $$ = null;
5524    }
5525  | labeled_statement
5526    {
5527      report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
5528      $$ = null;
5529    }
5530  | error
5531    {
5532    Error_SyntaxError (yyToken);
5533    $$ = new EmptyStatement (GetLocation ($1));
5534    }
5535  ;
5536
5537empty_statement
5538  : SEMICOLON
5539    {
5540    // Uses lexer.Location because semicolon location is not kept in quick mode
5541    $$ = new EmptyStatement (lexer.Location);
5542    }
5543  ;
5544
5545labeled_statement
5546  : identifier_inside_body COLON
5547    {
5548    var lt = (LocatedToken) $1;
5549    LabeledStatement labeled = new LabeledStatement (lt.Value, current_block, lt.Location);
5550    lbag.AddLocation (labeled, GetLocation ($2));
5551    current_block.AddLabel (labeled);
5552    current_block.AddStatement (labeled);
5553    }
5554    statement
5555  ;
5556
5557variable_type
5558  : variable_type_simple
5559  | variable_type_simple rank_specifiers
5560    {
5561    if ($1 is VarExpr)
5562      $1 = new SimpleName ("var", ((VarExpr) $1).Location);
5563   
5564    $$ = new ComposedCast ((FullNamedExpression) $1, (ComposedTypeSpecifier) $2);
5565    }
5566  ;
5567
5568/*
5569 * The following is from Rhys' grammar:
5570 * > Types in local variable declarations must be recognized as
5571 * > expressions to prevent reduce/reduce errors in the grammar.
5572 * > The expressions are converted into types during semantic analysis.
5573 */
5574variable_type_simple
5575  : primary_expression_or_type opt_nullable
5576    {
5577    // Ok, the above "primary_expression" is there to get rid of
5578    // both reduce/reduce and shift/reduces in the grammar, it should
5579    // really just be "type_name".  If you use type_name, a reduce/reduce
5580    // creeps up.  If you use namespace_or_type_name (which is all we need
5581    // really) two shift/reduces appear.
5582    //
5583
5584    // So the super-trick is that primary_expression
5585    // can only be either a SimpleName or a MemberAccess.
5586    // The MemberAccess case arises when you have a fully qualified type-name like :
5587    // Foo.Bar.Blah i;
5588    // SimpleName is when you have
5589    // Blah i;
5590   
5591    Expression expr = (Expression) $1;
5592    if ($2 == null) {
5593      SimpleName sn = expr as SimpleName;
5594      if (sn != null && sn.Name == "var")
5595        $$ = new VarExpr (sn.Location);
5596      else
5597        $$ = $1;
5598    } else if (expr is ATypeNameExpression) {
5599      $$ = new ComposedCast ((ATypeNameExpression)expr, (ComposedTypeSpecifier) $2);
5600    } else {
5601      Error_ExpectingTypeName (expr);
5602      $$ = null;
5603    }
5604    }
5605  | primary_expression_or_type pointer_stars
5606    {
5607    ATypeNameExpression expr = $1 as ATypeNameExpression;
5608
5609    if (expr != null) {
5610      $$ = new ComposedCast (expr, (ComposedTypeSpecifier) $2);
5611    } else {
5612      Error_ExpectingTypeName ((Expression)$1);
5613      $$ = expr;
5614    }
5615    }
5616  | builtin_type_expression
5617  | void_invalid
5618  ;
5619 
5620pointer_stars
5621  : pointer_star
5622  | pointer_star pointer_stars
5623    {
5624      ((ComposedTypeSpecifier) $1).Next = (ComposedTypeSpecifier) $2;
5625      $$ = $1;
5626    }   
5627  ;
5628
5629pointer_star
5630  : STAR
5631    {
5632    $$ = ComposedTypeSpecifier.CreatePointer (GetLocation ($1));
5633    }
5634  ;
5635
5636identifier_inside_body
5637  : IDENTIFIER
5638  | AWAIT
5639    {
5640      $$ = Error_AwaitAsIdentifier ($1);
5641    }
5642  ;
5643
5644block_variable_declaration
5645  : variable_type identifier_inside_body
5646    {
5647    var lt = (LocatedToken) $2;
5648    var li = new LocalVariable (current_block, lt.Value, lt.Location);
5649    current_block.AddLocalName (li);
5650    current_variable = new BlockVariable ((FullNamedExpression) $1, li);
5651    }
5652    opt_local_variable_initializer opt_variable_declarators semicolon_or_handle_error_close_brace
5653    {
5654    $$ = current_variable;
5655    current_variable = null;
5656    if ($4 != null)
5657      lbag.AddLocation ($$, PopLocation (), GetLocation ($6));
5658    else
5659      lbag.AddLocation ($$, GetLocation ($6));
5660    }
5661  | CONST variable_type identifier_inside_body
5662    {
5663    var lt = (LocatedToken) $3;
5664    var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.Constant, lt.Location);
5665    current_block.AddLocalName (li);
5666    current_variable = new BlockConstant ((FullNamedExpression) $2, li);
5667    }
5668    const_variable_initializer opt_const_declarators SEMICOLON
5669    {
5670    if (current_variable.Initializer != null) {
5671      lbag.AddLocation (current_variable, GetLocation ($1), savedLocation, GetLocation ($7));
5672    } else {
5673      lbag.AddLocation (current_variable, GetLocation ($1), GetLocation ($7));
5674    }
5675    $$ = current_variable;;
5676    current_variable = null;
5677    }
5678  ;
5679
5680semicolon_or_handle_error_close_brace
5681  : SEMICOLON
5682  | CLOSE_BRACE {
5683    // Redundant, but wont regress
5684    report.Error (1525, lexer.Location, "Unexpected symbol }");
5685    lexer.putback ('}');
5686    $$ = $1;
5687    }
5688  ;
5689
5690opt_local_variable_initializer
5691  : /* empty */
5692  | ASSIGN block_variable_initializer
5693    {
5694    current_variable.Initializer = (Expression) $2;
5695    PushLocation (GetLocation ($1));
5696    $$ = current_variable;
5697    }
5698  | error
5699    {
5700      if (yyToken == Token.OPEN_BRACKET_EXPR) {
5701      report.Error (650, lexer.Location,
5702        "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type");
5703    } else {
5704      Error_SyntaxError (yyToken);
5705    }
5706    }
5707  ;
5708
5709opt_variable_declarators
5710  : /* empty */
5711  | variable_declarators
5712  ;
5713 
5714opt_using_or_fixed_variable_declarators
5715  : /* empty */
5716  | variable_declarators
5717    {
5718    foreach (var d in current_variable.Declarators) {
5719      if (d.Initializer == null)
5720        Error_MissingInitializer (d.Variable.Location);
5721    }
5722    }
5723  ;
5724 
5725variable_declarators
5726  : variable_declarator
5727  | variable_declarators variable_declarator
5728  ;
5729 
5730variable_declarator
5731  : COMMA identifier_inside_body
5732    {
5733    var lt = (LocatedToken) $2;   
5734    var li = new LocalVariable (current_variable.Variable, lt.Value, lt.Location);
5735    var d = new BlockVariableDeclarator (li, null);
5736    current_variable.AddDeclarator (d);
5737    current_block.AddLocalName (li);
5738      lbag.AddLocation (d, GetLocation ($1));
5739    }
5740  | COMMA identifier_inside_body ASSIGN block_variable_initializer
5741    {
5742    var lt = (LocatedToken) $2;   
5743    var li = new LocalVariable (current_variable.Variable, lt.Value, lt.Location);
5744    var d = new BlockVariableDeclarator (li, (Expression) $4);
5745    current_variable.AddDeclarator (d);
5746    current_block.AddLocalName (li);
5747      lbag.AddLocation (d, GetLocation ($1), GetLocation ($3));
5748    }
5749  ;
5750 
5751const_variable_initializer
5752  : /* empty */
5753    {
5754    report.Error (145, lexer.Location, "A const field requires a value to be provided");
5755    }
5756  | ASSIGN constant_initializer_expr
5757    {
5758    savedLocation = GetLocation ($1);
5759    current_variable.Initializer = (Expression) $2;
5760    }
5761  ;
5762 
5763opt_const_declarators
5764  : /* empty */
5765  | const_declarators
5766  ;
5767 
5768const_declarators
5769  : const_declarator
5770  | const_declarators const_declarator
5771  ;
5772 
5773const_declarator
5774  : COMMA identifier_inside_body ASSIGN constant_initializer_expr
5775    {
5776    var lt = (LocatedToken) $2;   
5777    var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.Constant, lt.Location);
5778    var d = new BlockVariableDeclarator (li, (Expression) $4);
5779    current_variable.AddDeclarator (d);
5780    current_block.AddLocalName (li);
5781      lbag.AddLocation (d, GetLocation ($1), GetLocation ($3));
5782    }
5783  ;
5784 
5785block_variable_initializer
5786  : variable_initializer
5787  | STACKALLOC simple_type OPEN_BRACKET_EXPR expression CLOSE_BRACKET
5788    {
5789    $$ = new StackAlloc ((Expression) $2, (Expression) $4, GetLocation ($1));
5790    lbag.AddLocation ($$, GetLocation ($3), GetLocation ($5));
5791    }
5792  | STACKALLOC simple_type
5793    {
5794    report.Error (1575, GetLocation ($1), "A stackalloc expression requires [] after type");
5795    $$ = new StackAlloc ((Expression) $2, null, GetLocation ($1));   
5796    }
5797  ;
5798
5799expression_statement
5800  : statement_expression SEMICOLON
5801    {
5802    $$ = $1;
5803    lbag.AddStatement ($$, GetLocation ($2));
5804    }
5805  | statement_expression COMPLETE_COMPLETION { $$ = $1; }
5806  | statement_expression CLOSE_BRACE
5807    {
5808    $$ = $1;
5809    report.Error (1002, GetLocation ($2), "; expected");
5810    lexer.putback ('}');
5811    }
5812  ;
5813
5814interactive_expression_statement
5815  : interactive_statement_expression SEMICOLON { $$ = $1; }
5816  | interactive_statement_expression COMPLETE_COMPLETION { $$ = $1; }
5817  ;
5818
5819  //
5820  // We have to do the wrapping here and not in the case above,
5821  // because statement_expression is used for example in for_statement
5822  //
5823statement_expression
5824  : expression
5825    {
5826    ExpressionStatement s = $1 as ExpressionStatement;
5827    if (s == null) {
5828      var expr = $1 as Expression;
5829      $$ = new StatementErrorExpression (expr);
5830    } else {
5831      $$ = new StatementExpression (s);
5832    }
5833    }
5834  ;
5835
5836interactive_statement_expression
5837  : expression
5838    {
5839    Expression expr = (Expression) $1;
5840    $$ = new StatementExpression (new OptionalAssign (expr, lexer.Location));
5841    }
5842  | error
5843    {
5844    Error_SyntaxError (yyToken);
5845    $$ = new EmptyStatement (GetLocation ($1));
5846    }
5847  ;
5848 
5849selection_statement
5850  : if_statement
5851  | switch_statement
5852  ;
5853
5854if_statement
5855  : IF open_parens_any boolean_expression CLOSE_PARENS
5856    embedded_statement
5857    {
5858    if ($5 is EmptyStatement)
5859      Warning_EmptyStatement (GetLocation ($5));
5860   
5861    $$ = new If ((BooleanExpression) $3, (Statement) $5, GetLocation ($1));
5862    lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
5863    }
5864  | IF open_parens_any boolean_expression CLOSE_PARENS
5865    embedded_statement ELSE embedded_statement
5866    {
5867    $$ = new If ((BooleanExpression) $3, (Statement) $5, (Statement) $7, GetLocation ($1));
5868    lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4), GetLocation ($6));
5869   
5870    if ($5 is EmptyStatement)
5871      Warning_EmptyStatement (GetLocation ($5));
5872    if ($7 is EmptyStatement)
5873      Warning_EmptyStatement (GetLocation ($7));
5874    }
5875  | IF open_parens_any boolean_expression error
5876    {
5877    Error_SyntaxError (yyToken);
5878   
5879    $$ = new If ((BooleanExpression) $3, null, GetLocation ($1));
5880    lbag.AddStatement ($$, GetLocation ($2));
5881    }
5882  ;
5883
5884switch_statement
5885  : SWITCH open_parens_any expression CLOSE_PARENS OPEN_BRACE
5886    {
5887    start_block (GetLocation ($5));
5888    }
5889    opt_switch_sections CLOSE_BRACE
5890    {
5891    $$ = new Switch ((Expression) $3, (ExplicitBlock) current_block.Explicit, GetLocation ($1)); 
5892    end_block (GetLocation ($8));
5893    lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4), GetLocation ($5), GetLocation ($8));
5894    }
5895  | SWITCH open_parens_any expression error
5896    {
5897    Error_SyntaxError (yyToken);
5898   
5899    $$ = new Switch ((Expression) $3, null, GetLocation ($1)); 
5900    lbag.AddStatement ($$, GetLocation ($2));
5901    }
5902  ;
5903
5904opt_switch_sections
5905  : /* empty */     
5906      {
5907    report.Warning (1522, 1, current_block.StartLocation, "Empty switch block");
5908    }
5909  | switch_sections
5910  ;
5911
5912switch_sections
5913  : switch_section
5914  | switch_sections switch_section
5915  | error
5916    {
5917    Error_SyntaxError (yyToken);
5918    }
5919  ;
5920
5921switch_section
5922  : switch_labels statement_list
5923  ;
5924
5925switch_labels
5926  : switch_label
5927    {
5928      var label = (SwitchLabel) $1;
5929      label.SectionStart = true;
5930    current_block.AddStatement (label);
5931    }
5932  | switch_labels switch_label
5933    {
5934    current_block.AddStatement ((Statement) $2);
5935    }
5936  ;
5937
5938switch_label
5939  : CASE constant_expression COLON
5940   {
5941    $$ = new SwitchLabel ((Expression) $2, GetLocation ($1));
5942    lbag.AddLocation ($$, GetLocation ($3));
5943   }
5944  | CASE constant_expression error
5945    {
5946    Error_SyntaxError (yyToken);
5947    $$ = new SwitchLabel ((Expression) $2, GetLocation ($1));
5948    }
5949  | DEFAULT_COLON
5950    {
5951    $$ = new SwitchLabel (null, GetLocation ($1));
5952    }
5953  ;
5954
5955iteration_statement
5956  : while_statement
5957  | do_statement
5958  | for_statement
5959  | foreach_statement
5960  ;
5961
5962while_statement
5963  : WHILE open_parens_any boolean_expression CLOSE_PARENS embedded_statement
5964    {
5965    if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
5966      Warning_EmptyStatement (GetLocation ($5));
5967   
5968    $$ = new While ((BooleanExpression) $3, (Statement) $5, GetLocation ($1));
5969    lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
5970    }
5971  | WHILE open_parens_any boolean_expression error
5972    {
5973    Error_SyntaxError (yyToken);
5974   
5975    $$ = new While ((BooleanExpression) $3, null, GetLocation ($1));
5976    lbag.AddStatement ($$, GetLocation ($2));
5977    }
5978  ;
5979
5980do_statement
5981  : DO embedded_statement WHILE open_parens_any boolean_expression CLOSE_PARENS SEMICOLON
5982    {
5983    $$ = new Do ((Statement) $2, (BooleanExpression) $5, GetLocation ($1), GetLocation ($3));
5984    lbag.AddStatement ($$, GetLocation ($3), GetLocation ($4), GetLocation ($6), GetLocation ($7));
5985    }
5986  | DO embedded_statement error
5987    {
5988    Error_SyntaxError (yyToken);
5989    $$ = new Do ((Statement) $2, null, GetLocation ($1), Location.Null);
5990    }
5991  | DO embedded_statement WHILE open_parens_any boolean_expression error
5992    {
5993    Error_SyntaxError (yyToken);
5994   
5995    $$ = new Do ((Statement) $2, (BooleanExpression) $5, GetLocation ($1), GetLocation ($3));
5996    lbag.AddStatement ($$, GetLocation ($3), GetLocation ($4));
5997    }
5998  ;
5999
6000for_statement
6001  : FOR open_parens_any
6002    {
6003    start_block (GetLocation ($2));
6004    current_block.IsCompilerGenerated = true;
6005    For f = new For (GetLocation ($1));
6006    current_block.AddStatement (f);
6007    lbag.AddStatement (f, current_block.StartLocation);
6008    $$ = f;
6009    }
6010    for_statement_cont
6011    {
6012    $$ = $4;
6013    }
6014  ;
6015 
6016// Has to use be extra rule to recover started block
6017for_statement_cont
6018  : opt_for_initializer SEMICOLON
6019    {
6020    For f =  (For) $0;
6021    f.Initializer = (Statement) $1;
6022    lbag.AddLocation (f, GetLocation ($2));
6023    $$ = f;
6024    }
6025    for_statement_condition
6026    {
6027    $$ = $4;
6028    }
6029  | opt_for_initializer CLOSE_PARENS {
6030    report.Error (1525, GetLocation ($2), "Unexpected symbol ')', expected ';'");
6031    For f =  (For) $0;
6032    f.Initializer = (Statement) $1;
6033    lbag.AddLocation (f, GetLocation ($2));
6034    $$ = end_block (GetLocation ($2));
6035  }
6036  ;
6037
6038for_statement_condition
6039  : opt_for_condition SEMICOLON
6040    {
6041    For f =  (For) $0;
6042    f.Condition = (BooleanExpression) $1;
6043    lbag.AddLocation (f, GetLocation ($2));
6044    $$ = f;
6045    }
6046    for_statement_end
6047    {
6048    $$ = $4;
6049    }
6050
6051  | boolean_expression CLOSE_PARENS {
6052    report.Error (1525, GetLocation ($2), "Unexpected symbol ')', expected ';'");
6053    For f =  (For) $0;
6054    f.Condition = (BooleanExpression) $1;
6055    lbag.AddLocation (f, GetLocation ($2));
6056    $$ = end_block (GetLocation ($2));
6057  }
6058  ;
6059
6060for_statement_end
6061  : opt_for_iterator CLOSE_PARENS
6062    embedded_statement
6063    {
6064    For f =  (For) $0;
6065    f.Iterator = (Statement) $1;
6066   
6067    if ($3 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6068      Warning_EmptyStatement (GetLocation ($3));
6069   
6070    f.Statement = (Statement) $3;
6071    lbag.AddLocation (f, GetLocation ($2));
6072
6073    $$ = end_block (GetLocation ($2));
6074    }
6075  | error
6076    {
6077    Error_SyntaxError (yyToken);
6078    $$ = end_block (current_block.StartLocation);
6079    }
6080  ;
6081
6082opt_for_initializer
6083  : /* empty */   { $$ = new EmptyStatement (lexer.Location); }
6084  | for_initializer
6085  ;
6086
6087for_initializer
6088  : variable_type identifier_inside_body
6089    {
6090    var lt = (LocatedToken) $2;
6091    var li = new LocalVariable (current_block, lt.Value, lt.Location);
6092    current_block.AddLocalName (li);
6093    current_variable = new BlockVariable ((FullNamedExpression) $1, li);
6094    }
6095    opt_local_variable_initializer opt_variable_declarators
6096    {
6097    $$ = current_variable;
6098    if ($4 != null)
6099      lbag.AddLocation (current_variable, PopLocation ());
6100
6101    current_variable = null;
6102    }
6103  | statement_expression_list
6104  ;
6105
6106opt_for_condition
6107  : /* empty */   { $$ = null; }
6108  | boolean_expression
6109  ;
6110
6111opt_for_iterator
6112  : /* empty */   { $$ = new EmptyStatement (lexer.Location); }
6113  | for_iterator
6114  ;
6115
6116for_iterator
6117  : statement_expression_list
6118  ;
6119
6120statement_expression_list
6121  : statement_expression
6122  | statement_expression_list COMMA statement_expression
6123    {
6124      var sl = $1 as StatementList;
6125      if (sl == null) {
6126        sl = new StatementList ((Statement) $1, (Statement) $3);
6127      lbag.AddStatement (sl, GetLocation ($2));
6128      } else {
6129        sl.Add ((Statement) $3);
6130        lbag.AddLocation (sl, GetLocation ($2));
6131       
6132      }
6133       
6134    $$ = sl;
6135    }
6136  ;
6137
6138foreach_statement
6139  : FOREACH open_parens_any type error
6140    {
6141    report.Error (230, GetLocation ($1), "Type and identifier are both required in a foreach statement");
6142
6143    start_block (GetLocation ($2));
6144    current_block.IsCompilerGenerated = true;
6145   
6146    Foreach f = new Foreach ((Expression) $3, null, null, null, null, GetLocation ($1));
6147    current_block.AddStatement (f);
6148   
6149    lbag.AddStatement (f, GetLocation ($2));
6150    $$ = end_block (GetLocation ($4));
6151    }
6152  | FOREACH open_parens_any type identifier_inside_body error
6153    {
6154    Error_SyntaxError (yyToken);
6155 
6156    start_block (GetLocation ($2));
6157    current_block.IsCompilerGenerated = true;
6158   
6159    var lt = (LocatedToken) $4;
6160    var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.ForeachVariable | LocalVariable.Flags.Used, lt.Location);
6161    current_block.AddLocalName (li);
6162   
6163    Foreach f = new Foreach ((Expression) $3, li, null, null, null, GetLocation ($1));
6164    current_block.AddStatement (f);
6165   
6166    lbag.AddStatement (f, GetLocation ($2));
6167    $$ = end_block (GetLocation ($5));
6168    }
6169  | FOREACH open_parens_any type identifier_inside_body IN expression CLOSE_PARENS
6170    {
6171    start_block (GetLocation ($2));
6172    current_block.IsCompilerGenerated = true;
6173   
6174    var lt = (LocatedToken) $4;
6175    var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.ForeachVariable | LocalVariable.Flags.Used, lt.Location);
6176    current_block.AddLocalName (li);
6177    $$ = li;
6178    }
6179    embedded_statement
6180    {
6181    if ($9 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6182      Warning_EmptyStatement (GetLocation ($9));
6183   
6184    Foreach f = new Foreach ((Expression) $3, (LocalVariable) $8, (Expression) $6, (Statement) $9, current_block, GetLocation ($1));
6185    lbag.AddStatement (f, GetLocation ($2), GetLocation ($5), GetLocation ($7));
6186    end_block (GetLocation ($7));
6187   
6188    $$ = f;
6189    }
6190  | FOREACH open_parens_any type identifier_inside_body error
6191    {
6192    start_block (GetLocation ($2));
6193    current_block.IsCompilerGenerated = true;
6194    var lt = $4 as LocatedToken;
6195    var li = lt != null ? new LocalVariable (current_block, lt.Value, LocalVariable.Flags.ForeachVariable | LocalVariable.Flags.Used, lt.Location) : null;
6196   
6197    Foreach f = new Foreach ((Expression) $3, li, null, null, null, GetLocation ($1));
6198    current_block.AddStatement (f);
6199   
6200    lbag.AddStatement (f, GetLocation ($2));
6201    $$ = end_block (GetLocation ($5));
6202    }
6203  | FOREACH open_parens_any type error
6204    {
6205    Foreach f = new Foreach ((Expression) $3, null, null, null, null, GetLocation ($1));
6206    current_block.AddStatement (f);
6207   
6208    lbag.AddStatement (f, GetLocation ($2));
6209    $$ = f;
6210    }
6211  ;
6212
6213jump_statement
6214  : break_statement
6215  | continue_statement
6216  | goto_statement
6217  | return_statement
6218  | throw_statement
6219  | yield_statement
6220  ;
6221
6222break_statement
6223  : BREAK SEMICOLON
6224    {
6225    $$ = new Break (GetLocation ($1));
6226    lbag.AddStatement ($$, GetLocation ($2));
6227    }
6228  ;
6229
6230continue_statement
6231  : CONTINUE SEMICOLON
6232    {
6233    $$ = new Continue (GetLocation ($1));
6234    lbag.AddStatement ($$, GetLocation ($2));
6235    }
6236  | CONTINUE error
6237    {
6238    Error_SyntaxError (yyToken);
6239    $$ = new Continue (GetLocation ($1));
6240    }
6241  ;
6242
6243goto_statement
6244  : GOTO identifier_inside_body SEMICOLON
6245    {
6246    var lt = (LocatedToken) $2;
6247    $$ = new Goto (lt.Value, GetLocation ($1));
6248    lbag.AddStatement ($$, GetLocation ($2), GetLocation ($3));
6249    }
6250  | GOTO CASE constant_expression SEMICOLON
6251    {
6252    $$ = new GotoCase ((Expression) $3, GetLocation ($1));
6253    lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6254    }
6255  | GOTO DEFAULT SEMICOLON
6256    {
6257    $$ = new GotoDefault (GetLocation ($1));
6258    lbag.AddStatement ($$, GetLocation ($2), GetLocation ($3));
6259    }
6260  ;
6261
6262return_statement
6263  : RETURN opt_expression SEMICOLON
6264    {
6265    $$ = new Return ((Expression) $2, GetLocation ($1));
6266    lbag.AddStatement ($$, GetLocation ($3));
6267    }
6268  | RETURN expression error
6269    {
6270    Error_SyntaxError (yyToken);
6271    $$ = new Return ((Expression) $2, GetLocation ($1));
6272    }
6273  | RETURN error
6274    {
6275    Error_SyntaxError (yyToken);
6276    $$ = new Return (null, GetLocation ($1));
6277    }
6278  ;
6279
6280throw_statement
6281  : THROW opt_expression SEMICOLON
6282    {
6283    $$ = new Throw ((Expression) $2, GetLocation ($1));
6284    lbag.AddStatement ($$, GetLocation ($3));
6285    }
6286  | THROW expression error
6287    {
6288    Error_SyntaxError (yyToken);
6289    $$ = new Throw ((Expression) $2, GetLocation ($1));
6290    }
6291  | THROW error
6292    {
6293    Error_SyntaxError (yyToken);
6294    $$ = new Throw (null, GetLocation ($1));
6295    }
6296  ;
6297
6298yield_statement
6299  : identifier_inside_body RETURN opt_expression SEMICOLON
6300    {
6301    var lt = (LocatedToken) $1;
6302    string s = lt.Value;
6303    if (s != "yield"){
6304      report.Error (1003, lt.Location, "; expected");
6305    } else if ($3 == null) {
6306      report.Error (1627, GetLocation ($4), "Expression expected after yield return");
6307    } else if (lang_version == LanguageVersion.ISO_1){
6308      FeatureIsNotAvailable (lt.Location, "iterators");
6309    }
6310   
6311    current_block.Explicit.RegisterIteratorYield ();
6312    $$ = new Yield ((Expression) $3, lt.Location);
6313    lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6314    }
6315  | identifier_inside_body RETURN expression error
6316    {
6317    Error_SyntaxError (yyToken);
6318
6319    var lt = (LocatedToken) $1;
6320    string s = lt.Value;
6321    if (s != "yield"){
6322      report.Error (1003, lt.Location, "; expected");
6323    } else if ($3 == null) {
6324      report.Error (1627, GetLocation ($4), "Expression expected after yield return");
6325    } else if (lang_version == LanguageVersion.ISO_1){
6326      FeatureIsNotAvailable (lt.Location, "iterators");
6327    }
6328   
6329    current_block.Explicit.RegisterIteratorYield ();
6330    $$ = new Yield ((Expression) $3, lt.Location);
6331    lbag.AddStatement ($$, GetLocation ($2));
6332    }
6333  | identifier_inside_body BREAK SEMICOLON
6334    {
6335    var lt = (LocatedToken) $1;
6336    string s = lt.Value;
6337    if (s != "yield"){
6338      report.Error (1003, lt.Location, "; expected");
6339    } else if (lang_version == LanguageVersion.ISO_1){
6340      FeatureIsNotAvailable (lt.Location, "iterators");
6341    }
6342   
6343    current_block.ParametersBlock.TopBlock.IsIterator = true;
6344    $$ = new YieldBreak (lt.Location);
6345    lbag.AddStatement ($$, GetLocation ($2), GetLocation ($3));
6346    }
6347  ;
6348
6349opt_expression
6350  : /* empty */
6351  | expression
6352  ;
6353
6354try_statement
6355  : TRY block catch_clauses
6356    {
6357    $$ = new TryCatch ((Block) $2, (List<Catch>) $3, GetLocation ($1), false);
6358    }
6359  | TRY block FINALLY block
6360    {
6361    $$ = new TryFinally ((Statement) $2, (ExplicitBlock) $4, GetLocation ($1));
6362    lbag.AddStatement ($$, GetLocation ($3));
6363    }
6364  | TRY block catch_clauses FINALLY block
6365    {
6366    $$ = new TryFinally (new TryCatch ((Block) $2, (List<Catch>) $3, GetLocation ($1), true), (ExplicitBlock) $5, GetLocation ($1));
6367    lbag.AddStatement ($$, GetLocation ($4));
6368    }
6369  | TRY block error
6370    {
6371    Error_SyntaxError (1524, yyToken);
6372    $$ = new TryCatch ((Block) $2, null, GetLocation ($1), false);
6373    }
6374  ;
6375
6376catch_clauses
6377  : catch_clause
6378    {
6379    var l = new List<Catch> (2);
6380
6381    l.Add ((Catch) $1);
6382    $$ = l;
6383    }
6384  | catch_clauses catch_clause
6385    {
6386    var l = (List<Catch>) $1;
6387   
6388    Catch c = (Catch) $2;
6389    var prev_catch = l [l.Count - 1];
6390    if (prev_catch.IsGeneral && prev_catch.Filter == null) {
6391      report.Error (1017, c.loc, "Try statement already has an empty catch block");
6392    }
6393   
6394    l.Add (c);
6395    $$ = l;
6396    }
6397  ;
6398
6399opt_identifier
6400  : /* empty */
6401  | identifier_inside_body
6402  ;
6403
6404catch_clause
6405  : CATCH opt_catch_filter block
6406    {
6407      var c = new Catch ((ExplicitBlock) $3, GetLocation ($1));
6408      c.Filter = (CatchFilterExpression) $2;
6409      $$ = c;
6410    }
6411  | CATCH open_parens_any type opt_identifier CLOSE_PARENS
6412    {
6413    start_block (GetLocation ($2));
6414    var c = new Catch ((ExplicitBlock) current_block, GetLocation ($1));
6415    c.TypeExpression = (FullNamedExpression) $3;
6416
6417    if ($4 != null) {
6418      var lt = (LocatedToken) $4;
6419      c.Variable = new LocalVariable (current_block, lt.Value, lt.Location);
6420      current_block.AddLocalName (c.Variable);
6421    }
6422
6423    lbag.AddLocation (c, GetLocation ($2), GetLocation ($5));
6424    $$ = c;
6425    }
6426    opt_catch_filter block_prepared
6427    {
6428      ((Catch) $6).Filter = (CatchFilterExpression) $7;
6429    $$ = $6;
6430    }
6431  | CATCH open_parens_any error
6432    {
6433      if (yyToken == Token.CLOSE_PARENS) {
6434      report.Error (1015, lexer.Location,
6435        "A type that derives from `System.Exception', `object', or `string' expected");
6436    } else {
6437      Error_SyntaxError (yyToken);
6438    }
6439   
6440    $$ = new Catch (null, GetLocation ($1));
6441    }
6442  | CATCH open_parens_any type opt_identifier CLOSE_PARENS error
6443    {
6444    Error_SyntaxError (yyToken);
6445
6446    // Required otherwise missing block could not be detected because
6447    // start_block is run early
6448    var c = new Catch (null, GetLocation ($1));
6449    c.TypeExpression = (FullNamedExpression) $3;
6450   
6451    if ($4 != null) {
6452      var lt = (LocatedToken) $4;
6453      c.Variable = new LocalVariable (current_block, lt.Value, lt.Location);
6454    }
6455
6456    if ($4 != null) {
6457      var lt = (LocatedToken) $4;
6458      c.Variable = new LocalVariable (current_block, lt.Value, lt.Location);
6459    }
6460
6461    lbag.AddLocation (c, GetLocation ($2), GetLocation ($5));
6462
6463    $$ = c;
6464    }
6465  ;
6466
6467opt_catch_filter
6468  : /* empty */
6469  | IF open_parens_any expression CLOSE_PARENS
6470    {
6471    if (lang_version <= LanguageVersion.V_5)
6472      FeatureIsNotAvailable (GetLocation ($1), "exception filter");
6473
6474      $$ = new CatchFilterExpression ((Expression) $3, GetLocation ($1));
6475      lbag.AddLocation ($$, GetLocation ($2), GetLocation ($4));
6476    }
6477  ;
6478
6479checked_statement
6480  : CHECKED block
6481    {
6482    $$ = new Checked ((Block) $2, GetLocation ($1));
6483    }
6484  ;
6485
6486unchecked_statement
6487  : UNCHECKED block
6488    {
6489    $$ = new Unchecked ((Block) $2, GetLocation ($1));
6490    }
6491  ;
6492
6493unsafe_statement
6494  : UNSAFE
6495    {
6496    if (!settings.Unsafe)
6497      Error_UnsafeCodeNotAllowed (GetLocation ($1));
6498    } block {
6499    $$ = new Unsafe ((Block) $3, GetLocation ($1));
6500    }
6501  ;
6502
6503lock_statement
6504  : LOCK open_parens_any expression CLOSE_PARENS embedded_statement
6505    {
6506    if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6507      Warning_EmptyStatement (GetLocation ($5));
6508   
6509    $$ = new Lock ((Expression) $3, (Statement) $5, GetLocation ($1));
6510    lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6511    }
6512  | LOCK open_parens_any expression error
6513    {
6514    Error_SyntaxError (yyToken);
6515
6516    $$ = new Lock ((Expression) $3, null, GetLocation ($1));
6517    lbag.AddStatement ($$, GetLocation ($2));
6518    }
6519  ;
6520
6521fixed_statement
6522  : FIXED open_parens_any variable_type identifier_inside_body
6523    {
6524      start_block (GetLocation ($2));
6525     
6526    current_block.IsCompilerGenerated = true;
6527    var lt = (LocatedToken) $4;
6528    var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.FixedVariable | LocalVariable.Flags.Used, lt.Location);
6529    current_block.AddLocalName (li);
6530    current_variable = new Fixed.VariableDeclaration ((FullNamedExpression) $3, li);
6531    }
6532    using_or_fixed_variable_initializer opt_using_or_fixed_variable_declarators CLOSE_PARENS
6533    {
6534    $$ = current_variable;
6535    current_variable = null;
6536    }
6537    embedded_statement
6538    {
6539    if ($10 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6540      Warning_EmptyStatement (GetLocation ($10));
6541   
6542    Fixed f = new Fixed ((Fixed.VariableDeclaration) $9, (Statement) $10, GetLocation ($1));
6543    current_block.AddStatement (f);
6544    lbag.AddStatement (f, GetLocation ($2), GetLocation ($8));
6545    $$ = end_block (GetLocation ($8));
6546    }
6547  ;
6548
6549using_statement
6550  : USING open_parens_any variable_type identifier_inside_body
6551    {
6552      start_block (GetLocation ($2));
6553     
6554    current_block.IsCompilerGenerated = true;
6555    var lt = (LocatedToken) $4;
6556    var li = new LocalVariable (current_block, lt.Value, LocalVariable.Flags.UsingVariable | LocalVariable.Flags.Used, lt.Location);
6557    current_block.AddLocalName (li);
6558    current_variable = new Using.VariableDeclaration ((FullNamedExpression) $3, li);
6559    }
6560    using_initialization CLOSE_PARENS
6561    {
6562    $$ = current_variable;   
6563    current_variable = null;
6564    }
6565    embedded_statement
6566    {
6567    if ($9 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6568      Warning_EmptyStatement (GetLocation ($9));
6569   
6570    Using u = new Using ((Using.VariableDeclaration) $8, (Statement) $9, GetLocation ($1));
6571    lbag.AddStatement (u, GetLocation ($2), GetLocation ($7));
6572    current_block.AddStatement (u);
6573    $$ = end_block (GetLocation ($7));
6574    }
6575  | USING open_parens_any expression CLOSE_PARENS embedded_statement
6576    {
6577    if ($5 is EmptyStatement && lexer.peek_token () == Token.OPEN_BRACE)
6578      Warning_EmptyStatement (GetLocation ($5));
6579   
6580    $$ = new Using ((Expression) $3, (Statement) $5, GetLocation ($1));
6581    lbag.AddStatement ($$, GetLocation ($2), GetLocation ($4));
6582    }
6583  | USING open_parens_any expression error
6584    {
6585    Error_SyntaxError (yyToken);
6586   
6587    $$ = new Using ((Expression) $3, null, GetLocation ($1));
6588    lbag.AddStatement ($$, GetLocation ($2));
6589    }
6590  ;
6591 
6592using_initialization
6593  : using_or_fixed_variable_initializer opt_using_or_fixed_variable_declarators
6594  | error
6595    {
6596    // It has to be here for the parent to safely restore artificial block
6597      Error_SyntaxError (yyToken);
6598    }
6599  ;
6600 
6601using_or_fixed_variable_initializer
6602  : /* empty */
6603    {
6604    Error_MissingInitializer (lexer.Location);
6605    }
6606  | ASSIGN variable_initializer
6607    {
6608    current_variable.Initializer = (Expression) $2;
6609    lbag.AddLocation (current_variable, GetLocation ($1));
6610    $$ = current_variable;
6611    }
6612  ;
6613
6614
6615// LINQ
6616
6617query_expression
6618  : first_from_clause query_body
6619    {
6620    lexer.query_parsing = false;
6621     
6622    Linq.AQueryClause from = $1 as Linq.AQueryClause;
6623     
6624    from.Tail.Next = (Linq.AQueryClause)$2;
6625    $$ = from;
6626   
6627    current_block.SetEndLocation (lexer.Location);
6628    current_block = current_block.Parent;
6629    }
6630  | nested_from_clause query_body
6631    {
6632    Linq.AQueryClause from = $1 as Linq.AQueryClause;
6633     
6634    from.Tail.Next = (Linq.AQueryClause)$2;
6635    $$ = from;
6636   
6637    current_block.SetEndLocation (lexer.Location);
6638    current_block = current_block.Parent;
6639    }
6640
6641  // Bubble up COMPLETE_COMPLETION productions
6642  | first_from_clause COMPLETE_COMPLETION {
6643          lexer.query_parsing = false;
6644    $$ = $1;
6645
6646    current_block.SetEndLocation (lexer.Location);
6647    current_block = current_block.Parent;
6648    }
6649  | nested_from_clause COMPLETE_COMPLETION {
6650          $$ = $1;
6651    current_block.SetEndLocation (lexer.Location);
6652    current_block = current_block.Parent;
6653    }
6654  ;
6655 
6656first_from_clause
6657  : FROM_FIRST identifier_inside_body IN expression
6658    {
6659    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6660   
6661    var lt = (LocatedToken) $2;
6662    var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6663    var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, rv, GetLocation ($1));
6664    lbag.AddLocation (clause, GetLocation ($3));
6665    $$ = new Linq.QueryExpression (clause);
6666    }
6667  | FROM_FIRST type identifier_inside_body IN expression
6668    {
6669    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6670   
6671    var lt = (LocatedToken) $3;
6672    var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6673    var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, rv, GetLocation ($1)) {
6674        IdentifierType = (FullNamedExpression)$2
6675    };
6676    lbag.AddLocation (clause, GetLocation ($4));
6677    $$ = new Linq.QueryExpression (clause);
6678    }
6679  ;
6680
6681nested_from_clause
6682  : FROM identifier_inside_body IN expression
6683    {
6684    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6685   
6686    var lt = (LocatedToken) $2;
6687    var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6688    var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$4, rv, GetLocation ($1));
6689    lbag.AddLocation (clause, GetLocation ($3));
6690    $$ = new Linq.QueryExpression (clause);
6691    }
6692  | FROM type identifier_inside_body IN expression
6693    {
6694    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6695   
6696    var lt = (LocatedToken) $3;
6697    var rv = new Linq.RangeVariable (lt.Value, lt.Location);
6698    var clause = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, (Expression)$5, rv, GetLocation ($1)) {
6699        IdentifierType = (FullNamedExpression)$2
6700    };
6701    lbag.AddLocation (clause, GetLocation ($4));
6702    $$ = new Linq.QueryExpression (clause);
6703    }
6704  ;
6705 
6706from_clause
6707  : FROM identifier_inside_body IN
6708    {
6709    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6710    }
6711    expression_or_error
6712    {
6713    var lt = (LocatedToken) $2;
6714    var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6715    $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$5, GetLocation ($1));
6716   
6717    current_block.SetEndLocation (lexer.Location);
6718    current_block = current_block.Parent;
6719    ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
6720    lbag.AddLocation ($$, GetLocation ($3));
6721    }   
6722  | FROM type identifier_inside_body IN
6723    {
6724    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6725    }
6726    expression_or_error
6727    {
6728    var lt = (LocatedToken) $3;
6729    var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6730
6731    $$ = new Linq.SelectMany ((Linq.QueryBlock)current_block, sn, (Expression)$6, GetLocation ($1)) {
6732      IdentifierType = (FullNamedExpression)$2
6733    };
6734   
6735    current_block.SetEndLocation (lexer.Location);
6736    current_block = current_block.Parent;
6737   
6738    ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
6739   
6740    lbag.AddLocation ($$, GetLocation ($4));
6741    }
6742  ;
6743
6744query_body
6745  : query_body_clauses select_or_group_clause opt_query_continuation
6746    {
6747      Linq.AQueryClause head = (Linq.AQueryClause)$2;
6748   
6749    if ($3 != null)
6750      head.Next = (Linq.AQueryClause)$3;
6751       
6752    if ($1 != null) {
6753      Linq.AQueryClause clause = (Linq.AQueryClause)$1;
6754      clause.Tail.Next = head;
6755      head = clause;
6756    }
6757   
6758    $$ = head;
6759    }
6760  | select_or_group_clause opt_query_continuation
6761    {
6762      Linq.AQueryClause head = (Linq.AQueryClause)$2;
6763
6764    if ($1 != null) {
6765      Linq.AQueryClause clause = (Linq.AQueryClause)$1;
6766      clause.Tail.Next = head;
6767      head = clause;
6768    }
6769   
6770    $$ = head;
6771    }
6772  | query_body_clauses COMPLETE_COMPLETION
6773  | query_body_clauses error
6774    {
6775    report.Error (742, GetLocation ($2), "Unexpected symbol `{0}'. A query body must end with select or group clause", GetSymbolName (yyToken));
6776    $$ = $1;
6777    }
6778  | error
6779    {
6780    Error_SyntaxError (yyToken);
6781    $$ = null;
6782    }
6783  ;
6784 
6785select_or_group_clause
6786  : SELECT
6787    {
6788      current_block = new Linq.QueryBlock (current_block, lexer.Location);
6789    }
6790    expression_or_error
6791    {
6792    $$ = new Linq.Select ((Linq.QueryBlock)current_block, (Expression)$3, GetLocation ($1));
6793
6794    current_block.SetEndLocation (lexer.Location);
6795    current_block = current_block.Parent;
6796    }
6797  | GROUP
6798    {
6799      if (linq_clause_blocks == null)
6800        linq_clause_blocks = new Stack<Linq.QueryBlock> ();
6801       
6802      current_block = new Linq.QueryBlock (current_block, lexer.Location);
6803      linq_clause_blocks.Push ((Linq.QueryBlock)current_block);
6804    }
6805    expression_or_error
6806    {
6807    current_block.SetEndLocation (lexer.Location);
6808    current_block = current_block.Parent;
6809   
6810    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6811    }
6812    by_expression
6813    {
6814    var obj = (object[]) $5;
6815
6816    $$ = new Linq.GroupBy ((Linq.QueryBlock)current_block, (Expression)$3, linq_clause_blocks.Pop (), (Expression)obj[0], GetLocation ($1));
6817    lbag.AddLocation ($$, (Location) obj[1]);
6818   
6819    current_block.SetEndLocation (lexer.Location);
6820    current_block = current_block.Parent;
6821    }
6822  ;
6823
6824by_expression
6825  : BY expression_or_error
6826    {
6827      $$ = new object[] { $2, GetLocation ($1) };
6828    }
6829  | error
6830    {
6831    Error_SyntaxError (yyToken);
6832    $$ = new object[2] { null, Location.Null };
6833    }
6834  ;
6835 
6836query_body_clauses
6837  : query_body_clause
6838  | query_body_clauses query_body_clause
6839    {
6840    ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$2;
6841    $$ = $1;
6842    }
6843  ;
6844 
6845query_body_clause
6846  : from_clause
6847  | let_clause
6848  | where_clause
6849  | join_clause
6850  | orderby_clause
6851  ;
6852 
6853let_clause
6854  : LET identifier_inside_body ASSIGN
6855    {
6856      current_block = new Linq.QueryBlock (current_block, lexer.Location);
6857    }
6858    expression_or_error
6859    {
6860    var lt = (LocatedToken) $2;
6861    var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6862      $$ = new Linq.Let ((Linq.QueryBlock) current_block, sn, (Expression)$5, GetLocation ($1));
6863    lbag.AddLocation ($$, GetLocation ($3));
6864     
6865    current_block.SetEndLocation (lexer.Location);
6866    current_block = current_block.Parent;
6867   
6868    ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
6869    }
6870  ;
6871
6872where_clause
6873  : WHERE
6874    {
6875      current_block = new Linq.QueryBlock (current_block, lexer.Location);
6876    }
6877    expression_or_error
6878    {
6879    $$ = new Linq.Where ((Linq.QueryBlock)current_block, (Expression)$3, GetLocation ($1));
6880
6881    current_block.SetEndLocation (lexer.Location);
6882    current_block = current_block.Parent;
6883    }
6884  ;
6885 
6886join_clause
6887  : JOIN identifier_inside_body IN
6888    {
6889    if (linq_clause_blocks == null)
6890      linq_clause_blocks = new Stack<Linq.QueryBlock> ();
6891       
6892    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6893    linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
6894    }
6895    expression_or_error ON
6896    {
6897    current_block.SetEndLocation (lexer.Location);
6898    current_block = current_block.Parent;
6899
6900    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6901    linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
6902    }
6903    expression_or_error EQUALS
6904    {
6905    current_block.AddStatement (new ContextualReturn ((Expression) $8));
6906    current_block.SetEndLocation (lexer.Location);
6907    current_block = current_block.Parent;
6908
6909    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6910    }
6911    expression_or_error opt_join_into
6912    {
6913    current_block.AddStatement (new ContextualReturn ((Expression) $11));
6914    current_block.SetEndLocation (lexer.Location);
6915   
6916    var outer_selector = linq_clause_blocks.Pop ();
6917    var block = linq_clause_blocks.Pop ();
6918
6919    var lt = (LocatedToken) $2;
6920    var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6921    Linq.RangeVariable into;
6922   
6923    if ($12 == null) {
6924      into = sn;
6925        $$ = new Linq.Join (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1));
6926      lbag.AddLocation ($$, GetLocation ($3), GetLocation ($6), GetLocation ($9));
6927    } else {
6928      //
6929      // Set equals right side parent to beginning of linq query, it is not accessible therefore cannot cause name collisions
6930      //
6931      var parent = block.Parent;
6932      while (parent is Linq.QueryBlock) {
6933        parent = parent.Parent;
6934      }
6935      current_block.Parent = parent;
6936     
6937      ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
6938   
6939      lt = (LocatedToken) $12;
6940      into = new Linq.RangeVariable (lt.Value, lt.Location);
6941
6942      $$ = new Linq.GroupJoin (block, sn, (Expression)$5, outer_selector, (Linq.QueryBlock) current_block, into, GetLocation ($1));
6943      lbag.AddLocation ($$, GetLocation ($3), GetLocation ($6), GetLocation ($9), opt_intoStack.Pop ());
6944    }
6945
6946    current_block = block.Parent;
6947    ((Linq.QueryBlock)current_block).AddRangeVariable (into);
6948    }
6949  | JOIN type identifier_inside_body IN
6950    {
6951    if (linq_clause_blocks == null)
6952      linq_clause_blocks = new Stack<Linq.QueryBlock> ();
6953       
6954    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6955    linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
6956    }
6957    expression_or_error ON
6958    {
6959    current_block.SetEndLocation (lexer.Location);
6960    current_block = current_block.Parent;
6961
6962    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6963    linq_clause_blocks.Push ((Linq.QueryBlock) current_block);
6964    }
6965    expression_or_error EQUALS
6966    {
6967    current_block.AddStatement (new ContextualReturn ((Expression) $9));
6968    current_block.SetEndLocation (lexer.Location);
6969    current_block = current_block.Parent;
6970
6971    current_block = new Linq.QueryBlock (current_block, lexer.Location);
6972    }
6973    expression_or_error opt_join_into
6974    {
6975    current_block.AddStatement (new ContextualReturn ((Expression) $12));
6976    current_block.SetEndLocation (lexer.Location);
6977   
6978    var outer_selector = linq_clause_blocks.Pop ();
6979    var block = linq_clause_blocks.Pop ();
6980   
6981    var lt = (LocatedToken) $3;
6982    var sn = new Linq.RangeVariable (lt.Value, lt.Location);
6983    Linq.RangeVariable into;
6984   
6985    if ($13 == null) {
6986      into = sn;   
6987        $$ = new Linq.Join (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, GetLocation ($1)) {
6988          IdentifierType = (FullNamedExpression)$2
6989        };
6990      lbag.AddLocation ($$, GetLocation ($3), GetLocation ($6), GetLocation ($9));
6991    } else {
6992      //
6993      // Set equals right side parent to beginning of linq query, it is not accessible therefore cannot cause name collisions
6994      //
6995      var parent = block.Parent;
6996      while (parent is Linq.QueryBlock) {
6997        parent = parent.Parent;
6998      }
6999      current_block.Parent = parent;
7000   
7001      ((Linq.QueryBlock)current_block).AddRangeVariable (sn);
7002   
7003      lt = (LocatedToken) $13;
7004      into = new Linq.RangeVariable (lt.Value, lt.Location); // TODO:
7005     
7006      $$ = new Linq.GroupJoin (block, sn, (Expression)$6, outer_selector, (Linq.QueryBlock) current_block, into, GetLocation ($1)) {
7007          IdentifierType = (FullNamedExpression)$2
7008        };     
7009      lbag.AddLocation ($$, GetLocation ($3), GetLocation ($6), GetLocation ($9), opt_intoStack.Pop ());
7010    }
7011   
7012    current_block = block.Parent;
7013    ((Linq.QueryBlock)current_block).AddRangeVariable (into);   
7014    }
7015  ;
7016 
7017opt_join_into
7018  : /* empty */
7019  | INTO identifier_inside_body
7020    {
7021    opt_intoStack.Push (GetLocation ($1));
7022    $$ = $2;
7023    }
7024  ;
7025 
7026orderby_clause
7027  : ORDERBY
7028    {
7029    current_block = new Linq.QueryBlock (current_block, lexer.Location);
7030    lbag.AddLocation (current_block, GetLocation ($1));
7031    }
7032    orderings
7033    {
7034    current_block.SetEndLocation (lexer.Location);
7035    current_block = current_block.Parent;
7036   
7037    $$ = $3;
7038    }
7039  ;
7040 
7041orderings
7042  : order_by
7043  | order_by COMMA
7044    {
7045    current_block.SetEndLocation (lexer.Location);
7046    current_block = current_block.Parent;
7047   
7048    current_block = new Linq.QueryBlock (current_block, lexer.Location);
7049    }
7050    orderings_then_by
7051    {
7052    ((Linq.AQueryClause)$1).Next = (Linq.AQueryClause)$4;
7053    $$ = $1;
7054    }
7055  ;
7056 
7057orderings_then_by
7058  : then_by
7059  | orderings_then_by COMMA
7060   {
7061    current_block.SetEndLocation (lexer.Location);
7062    current_block = current_block.Parent;
7063   
7064    current_block = new Linq.QueryBlock ((Linq.QueryBlock) current_block, lexer.Location);   
7065   }
7066   then_by
7067   {
7068    ((Linq.AQueryClause)$1).Tail.Next = (Linq.AQueryClause)$4;
7069    $$ = $1;
7070   }
7071  ;
7072 
7073order_by
7074  : expression
7075    {
7076    $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);
7077    }
7078  | expression ASCENDING
7079    {
7080    $$ = new Linq.OrderByAscending ((Linq.QueryBlock) current_block, (Expression)$1);
7081    lbag.AddLocation ($$, GetLocation ($2));
7082    }
7083  | expression DESCENDING
7084    {
7085    $$ = new Linq.OrderByDescending ((Linq.QueryBlock) current_block, (Expression)$1); 
7086    lbag.AddLocation ($$, GetLocation ($2));
7087    }
7088  ;
7089
7090then_by
7091  : expression
7092    {
7093    $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1); 
7094    }
7095  | expression ASCENDING
7096    {
7097    $$ = new Linq.ThenByAscending ((Linq.QueryBlock) current_block, (Expression)$1); 
7098    lbag.AddLocation ($$, GetLocation ($2));
7099    }
7100  | expression DESCENDING
7101    {
7102    $$ = new Linq.ThenByDescending ((Linq.QueryBlock) current_block, (Expression)$1);
7103    lbag.AddLocation ($$, GetLocation ($2));
7104    }
7105  ;
7106
7107
7108opt_query_continuation
7109  : /* empty */
7110  | INTO identifier_inside_body
7111    {
7112    // query continuation block is not linked with query block but with block
7113    // before. This means each query can use same range variable names for
7114    // different identifiers.
7115
7116    current_block.SetEndLocation (GetLocation ($1));
7117    current_block = current_block.Parent;
7118 
7119    current_block = new Linq.QueryBlock (current_block, lexer.Location);
7120   
7121    if (linq_clause_blocks == null)
7122      linq_clause_blocks = new Stack<Linq.QueryBlock> ();
7123       
7124    linq_clause_blocks.Push ((Linq.QueryBlock) current_block);   
7125    }
7126    query_body
7127    {
7128    var current_block = linq_clause_blocks.Pop ();   
7129    var lt = (LocatedToken) $2;
7130    var rv = new Linq.RangeVariable (lt.Value, lt.Location);
7131      $$ = new Linq.QueryStartClause ((Linq.QueryBlock)current_block, null, rv, GetLocation ($1)) {
7132        next = (Linq.AQueryClause)$4
7133      };
7134    }
7135  ;
7136 
7137//
7138// Support for using the compiler as an interactive parser
7139//
7140// The INTERACTIVE_PARSER token is first sent to parse our
7141// productions;  If the result is a Statement, the parsing
7142// is repeated, this time with INTERACTIVE_PARSE_WITH_BLOCK
7143// to setup the blocks in advance.
7144//
7145// This setup is here so that in the future we can add
7146// support for other constructs (type parsing, namespaces, etc)
7147// that do not require a block to be setup in advance
7148//
7149
7150interactive_parsing
7151  : EVAL_STATEMENT_PARSER EOF
7152  | EVAL_USING_DECLARATIONS_UNIT_PARSER using_directives opt_COMPLETE_COMPLETION
7153  | EVAL_STATEMENT_PARSER
7154   {
7155    current_container = current_type = new Class (current_container, new MemberName ("<InteractiveExpressionClass>"), Modifiers.PUBLIC, null);
7156
7157    // (ref object retval)
7158    Parameter [] mpar = new Parameter [1];
7159    mpar [0] = new Parameter (new TypeExpression (compiler.BuiltinTypes.Object, Location.Null), "$retval", Parameter.Modifier.REF, null, Location.Null);
7160
7161    ParametersCompiled pars = new ParametersCompiled (mpar);
7162    var mods = Modifiers.PUBLIC | Modifiers.STATIC;
7163    if (settings.Unsafe)
7164      mods |= Modifiers.UNSAFE;
7165
7166    current_local_parameters = pars;
7167    var method = new InteractiveMethod (
7168      current_type,
7169      new TypeExpression (compiler.BuiltinTypes.Void, Location.Null),
7170      mods,
7171      pars);
7172     
7173    current_type.AddMember (method);     
7174    oob_stack.Push (method);
7175
7176    interactive_async = false;
7177
7178    ++lexer.parsing_block;
7179    start_block (lexer.Location);
7180    }   
7181    interactive_statement_list opt_COMPLETE_COMPLETION
7182    {
7183    --lexer.parsing_block;
7184    var method = (InteractiveMethod) oob_stack.Pop ();
7185    method.Block = (ToplevelBlock) end_block(lexer.Location);
7186
7187    if (interactive_async == true) {
7188      method.ChangeToAsync ();
7189    }
7190
7191    InteractiveResult = (Class) pop_current_class ();
7192    current_local_parameters = null;
7193    }
7194  | EVAL_COMPILATION_UNIT_PARSER interactive_compilation_unit
7195  ;
7196
7197interactive_compilation_unit
7198  : opt_extern_alias_directives opt_using_directives
7199  | opt_extern_alias_directives opt_using_directives namespace_or_type_declarations
7200  ;
7201
7202opt_COMPLETE_COMPLETION
7203  : /* nothing */
7204  | COMPLETE_COMPLETION
7205  ;
7206
7207close_brace_or_complete_completion
7208  : CLOSE_BRACE
7209  | COMPLETE_COMPLETION
7210  ;
7211 
7212//
7213// XML documentation code references micro parser
7214//
7215documentation_parsing
7216  : DOC_SEE doc_cref
7217    {
7218    module.DocumentationBuilder.ParsedName = (MemberName) $2;
7219    }
7220  ;
7221
7222doc_cref
7223  : doc_type_declaration_name opt_doc_method_sig
7224    {
7225    module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$2;
7226    }
7227  | builtin_types opt_doc_method_sig
7228    {
7229    module.DocumentationBuilder.ParsedBuiltinType = (TypeExpression)$1;
7230    module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$2;
7231    $$ = null;
7232    }
7233  | VOID opt_doc_method_sig
7234    {
7235    module.DocumentationBuilder.ParsedBuiltinType = new TypeExpression (compiler.BuiltinTypes.Void, GetLocation ($1));
7236    module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$2;
7237    $$ = null;
7238    }
7239  | builtin_types DOT IDENTIFIER opt_doc_method_sig
7240    {
7241    module.DocumentationBuilder.ParsedBuiltinType = (TypeExpression)$1;
7242    module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$4;
7243    var lt = (LocatedToken) $3;
7244    $$ = new MemberName (lt.Value);
7245    }
7246  | doc_type_declaration_name DOT THIS
7247    {
7248    $$ = new MemberName ((MemberName) $1, MemberCache.IndexerNameAlias, Location.Null);
7249    }
7250  | doc_type_declaration_name DOT THIS OPEN_BRACKET
7251    {
7252    valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
7253    }
7254    opt_doc_parameters CLOSE_BRACKET
7255    {
7256    module.DocumentationBuilder.ParsedParameters = (List<DocumentationParameter>)$6;
7257    $$ = new MemberName ((MemberName) $1, MemberCache.IndexerNameAlias, Location.Null);
7258    }
7259  | EXPLICIT OPERATOR type opt_doc_method_sig
7260    {
7261    var p = (List<DocumentationParameter>)$4 ?? new List<DocumentationParameter> (1);
7262    p.Add (new DocumentationParameter ((FullNamedExpression) $3));
7263    module.DocumentationBuilder.ParsedParameters = p;
7264    module.DocumentationBuilder.ParsedOperator = Operator.OpType.Explicit;
7265    $$ = null;
7266    }
7267  | IMPLICIT OPERATOR type opt_doc_method_sig
7268    {
7269    var p = (List<DocumentationParameter>)$4 ?? new List<DocumentationParameter> (1);
7270    p.Add (new DocumentationParameter ((FullNamedExpression) $3));
7271    module.DocumentationBuilder.ParsedParameters = p;
7272    module.DocumentationBuilder.ParsedOperator = Operator.OpType.Implicit;
7273    $$ = null;
7274    }   
7275  | OPERATOR overloadable_operator opt_doc_method_sig
7276    {
7277    var p = (List<DocumentationParameter>)$3;
7278    module.DocumentationBuilder.ParsedParameters = p;
7279    module.DocumentationBuilder.ParsedOperator = (Operator.OpType) $2;
7280    $$ = null;
7281    }
7282  ;
7283 
7284doc_type_declaration_name
7285  : type_declaration_name
7286  | doc_type_declaration_name DOT type_declaration_name
7287    {
7288    $$ = new MemberName (((MemberName) $1), (MemberName) $3);
7289    }
7290  ;
7291 
7292opt_doc_method_sig
7293  : /* empty */
7294  | OPEN_PARENS
7295    {
7296    valid_param_mod = ParameterModifierType.Ref | ParameterModifierType.Out;
7297    }
7298    opt_doc_parameters CLOSE_PARENS
7299    {
7300    $$ = $3;
7301    }
7302  ;
7303 
7304opt_doc_parameters
7305  : /* empty */
7306    {
7307    $$ = new List<DocumentationParameter> (0);
7308    }
7309  | doc_parameters
7310  ;
7311 
7312doc_parameters
7313  : doc_parameter
7314    {
7315    var parameters = new List<DocumentationParameter> ();
7316    parameters.Add ((DocumentationParameter) $1);
7317    $$ = parameters;
7318    }
7319  | doc_parameters COMMA doc_parameter
7320    {
7321    var parameters = $1 as List<DocumentationParameter>;
7322    parameters.Add ((DocumentationParameter) $3);
7323    $$ = parameters;
7324    }
7325  ;
7326 
7327doc_parameter
7328  : opt_parameter_modifier parameter_type
7329    {
7330    if ($1 != null)
7331      $$ = new DocumentationParameter ((Parameter.Modifier) $1, (FullNamedExpression) $2);
7332    else
7333      $$ = new DocumentationParameter ((FullNamedExpression) $2);
7334    }
7335  ;
7336 
7337%%
7338
7339// <summary>
7340//  A class used to hold info about an operator declarator
7341// </summary>
7342class OperatorDeclaration {
7343  public readonly Operator.OpType optype;
7344  public readonly FullNamedExpression ret_type;
7345  public readonly Location location;
7346
7347  public OperatorDeclaration (Operator.OpType op, FullNamedExpression ret_type, Location location)
7348  {
7349    optype = op;
7350    this.ret_type = ret_type;
7351    this.location = location;
7352  }
7353}
7354
7355void Error_ExpectingTypeName (Expression expr)
7356{
7357  if (expr is Invocation){
7358    report.Error (1002, expr.Location, "Expecting `;'");
7359  } else {
7360    expr.Error_InvalidExpressionStatement (report);
7361  }
7362}
7363
7364void Error_ParameterModifierNotValid (string modifier, Location loc)
7365{
7366  report.Error (631, loc, "The parameter modifier `{0}' is not valid in this context",
7367                    modifier);
7368}
7369
7370void Error_DuplicateParameterModifier (Location loc, Parameter.Modifier mod)
7371{
7372  report.Error (1107, loc, "Duplicate parameter modifier `{0}'",
7373      Parameter.GetModifierSignature (mod));
7374}
7375
7376void Error_TypeExpected (Location loc)
7377{
7378  report.Error (1031, loc, "Type expected");
7379}
7380
7381void Error_UnsafeCodeNotAllowed (Location loc)
7382{
7383  report.Error (227, loc, "Unsafe code requires the `unsafe' command line option to be specified");
7384}
7385
7386void Warning_EmptyStatement (Location loc)
7387{
7388  report.Warning (642, 3, loc, "Possible mistaken empty statement");
7389}
7390
7391void Error_NamedArgumentExpected (NamedArgument a)
7392{
7393  report.Error (1738, a.Location, "Named arguments must appear after the positional arguments");
7394}
7395
7396void Error_MissingInitializer (Location loc)
7397{
7398  report.Error (210, loc, "You must provide an initializer in a fixed or using statement declaration");
7399}
7400
7401object Error_AwaitAsIdentifier (object token)
7402{
7403  if (async_block) {
7404    report.Error (4003, GetLocation (token), "`await' cannot be used as an identifier within an async method or lambda expression");
7405    return new LocatedToken ("await", GetLocation (token));
7406  }
7407
7408  return token;
7409}
7410
7411void push_current_container (TypeDefinition tc, object partial_token)
7412{
7413  if (module.Evaluator != null){
7414    tc.Definition.Modifiers = tc.ModFlags = (tc.ModFlags & ~Modifiers.AccessibilityMask) | Modifiers.PUBLIC;
7415    if (undo == null)
7416      undo = new Undo ();
7417
7418    undo.AddTypeContainer (current_container, tc);
7419  }
7420 
7421  if (partial_token != null)
7422    current_container.AddPartial (tc);
7423  else
7424    current_container.AddTypeContainer (tc);
7425   
7426  ++lexer.parsing_declaration;
7427  current_container = tc;
7428  current_type = tc;
7429}
7430
7431TypeContainer pop_current_class ()
7432{
7433  var retval = current_container;
7434
7435  current_container = current_container.Parent;
7436  current_type = current_type.Parent as TypeDefinition;
7437
7438  return retval;
7439}
7440
7441[System.Diagnostics.Conditional ("FULL_AST")]
7442void StoreModifierLocation (object token, Location loc)
7443{
7444  if (lbag == null)
7445    return;
7446
7447  if (mod_locations == null)
7448      mod_locations = new List<Tuple<Modifiers, Location>> ();
7449
7450  mod_locations.Add (Tuple.Create ((Modifiers) token, loc));
7451}
7452
7453List<Tuple<Modifiers, Location>> GetModifierLocations ()
7454{
7455  var result = mod_locations;
7456  mod_locations = null;
7457  return result;
7458}
7459
7460[System.Diagnostics.Conditional ("FULL_AST")]
7461void PushLocation (Location loc)
7462{
7463  if (location_stack == null)
7464    location_stack = new Stack<Location> ();
7465
7466  location_stack.Push (loc);
7467}
7468
7469Location PopLocation ()
7470{
7471  if (location_stack == null)
7472    return Location.Null;
7473
7474  return location_stack.Pop ();
7475}
7476
7477string CheckAttributeTarget (int token, string a, Location l)
7478{
7479  switch (a) {
7480  case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
7481      return a;
7482  }
7483
7484  if (!Tokenizer.IsValidIdentifier (a)) {
7485    Error_SyntaxError (token);
7486  } else {
7487    report.Warning (658, 1, l,
7488       "`{0}' is invalid attribute target. All attributes in this attribute section will be ignored", a);
7489  }
7490
7491  return string.Empty;
7492}
7493
7494static bool IsUnaryOperator (Operator.OpType op)
7495{
7496  switch (op) {
7497   
7498  case Operator.OpType.LogicalNot:
7499  case Operator.OpType.OnesComplement:
7500  case Operator.OpType.Increment:
7501  case Operator.OpType.Decrement:
7502  case Operator.OpType.True:
7503  case Operator.OpType.False:
7504  case Operator.OpType.UnaryPlus:
7505  case Operator.OpType.UnaryNegation:
7506    return true;
7507  }
7508  return false;
7509}
7510
7511void syntax_error (Location l, string msg)
7512{
7513  report.Error (1003, l, "Syntax error, " + msg);
7514}
7515
7516Tokenizer lexer;
7517
7518public Tokenizer Lexer {
7519  get {
7520    return lexer;
7521  }
7522}     
7523
7524public CSharpParser (SeekableStreamReader reader, CompilationSourceFile file, ParserSession session)
7525  : this (reader, file, file.Compiler.Report, session)
7526{
7527}
7528
7529public CSharpParser (SeekableStreamReader reader, CompilationSourceFile file, Report report, ParserSession session)
7530{
7531  this.file = file;
7532  current_container = current_namespace = file;
7533 
7534  this.module = file.Module;
7535  this.compiler = file.Compiler;
7536  this.settings = compiler.Settings;
7537  this.report = report;
7538 
7539  lang_version = settings.Version;
7540  yacc_verbose_flag = settings.VerboseParserFlag;
7541  doc_support = settings.DocumentationFile != null;
7542  lexer = new Tokenizer (reader, file, session, report);
7543  oob_stack = new Stack<object> ();
7544  lbag = session.LocationsBag;
7545  use_global_stacks = session.UseJayGlobalArrays;
7546  parameters_bucket = session.ParametersStack;
7547}
7548
7549public void parse ()
7550{
7551  eof_token = Token.EOF;
7552 
7553  try {
7554    if (yacc_verbose_flag > 1)
7555      yyparse (lexer, new yydebug.yyDebugSimple ());
7556    else
7557      yyparse (lexer);
7558     
7559    Tokenizer tokenizer = lexer as Tokenizer;
7560    tokenizer.cleanup ();   
7561  } catch (Exception e){
7562      if (e is yyParser.yyUnexpectedEof) {
7563      Error_SyntaxError (yyToken);
7564      UnexpectedEOF = true;
7565      return;
7566    }
7567     
7568    if (e is yyParser.yyException) {
7569      if (report.Errors == 0)
7570        report.Error (-25, lexer.Location, "Parsing error");
7571    } else {
7572      // Used by compiler-tester to test internal errors
7573      if (yacc_verbose_flag > 0 || e is FatalException)
7574        throw;
7575   
7576      report.Error (589, lexer.Location, "Internal compiler error during parsing" + e);
7577    }
7578  }
7579}
7580
7581void CheckToken (int error, int yyToken, string msg, Location loc)
7582{
7583  if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
7584    report.Error (error, loc, "{0}: `{1}' is a keyword", msg, GetTokenName (yyToken));
7585  else
7586    report.Error (error, loc, msg);
7587}
7588
7589string ConsumeStoredComment ()
7590{
7591  string s = tmpComment;
7592  tmpComment = null;
7593  Lexer.doc_state = XmlCommentState.Allowed;
7594  return s;
7595}
7596
7597void FeatureIsNotAvailable (Location loc, string feature)
7598{
7599  report.FeatureIsNotAvailable (compiler, loc, feature);
7600}
7601
7602Location GetLocation (object obj)
7603{
7604  var lt = obj as LocatedToken;
7605  if (lt != null)
7606    return lt.Location;
7607   
7608  var mn = obj as MemberName;
7609  if (mn != null)
7610    return mn.Location;
7611   
7612  var expr = obj as Expression;
7613  if (expr != null)
7614    return expr.Location;
7615
7616  return lexer.Location;
7617}
7618
7619void start_block (Location loc)
7620{
7621  if (current_block == null) {
7622    current_block = new ToplevelBlock (compiler, current_local_parameters, loc);
7623    parsing_anonymous_method = false;
7624  } else if (parsing_anonymous_method) {
7625    current_block = new ParametersBlock (current_block, current_local_parameters, loc);
7626    parsing_anonymous_method = false;
7627  } else {
7628    current_block = new ExplicitBlock (current_block, loc, Location.Null);
7629  }
7630}
7631
7632Block
7633end_block (Location loc)
7634{
7635  Block retval = current_block.Explicit;
7636  retval.SetEndLocation (loc);
7637  current_block = retval.Parent;
7638  return retval;
7639}
7640
7641void start_anonymous (bool isLambda, ParametersCompiled parameters, bool isAsync, Location loc)
7642{
7643  oob_stack.Push (current_anonymous_method);
7644  oob_stack.Push (current_local_parameters);
7645  oob_stack.Push (current_variable);
7646  oob_stack.Push (async_block);
7647
7648  current_local_parameters = parameters;
7649  if (isLambda) {
7650    if (lang_version <= LanguageVersion.ISO_2)
7651      FeatureIsNotAvailable (loc, "lambda expressions");
7652
7653    current_anonymous_method = new LambdaExpression (loc);
7654  } else {
7655    if (lang_version == LanguageVersion.ISO_1)
7656      FeatureIsNotAvailable (loc, "anonymous methods");
7657     
7658    current_anonymous_method = new AnonymousMethodExpression (loc);
7659  }
7660  current_anonymous_method.IsAsync = isAsync;
7661 
7662  async_block = isAsync;
7663  // Force the next block to be created as a ToplevelBlock
7664  parsing_anonymous_method = true;
7665}
7666
7667/*
7668 * Completes the anonymous method processing, if lambda_expr is null, this
7669 * means that we have a Statement instead of an Expression embedded
7670 */
7671AnonymousMethodExpression end_anonymous (ParametersBlock anon_block)
7672{
7673  AnonymousMethodExpression retval;
7674
7675  if (async_block)
7676    anon_block.IsAsync = true;
7677
7678  current_anonymous_method.Block = anon_block;
7679  retval = current_anonymous_method;
7680
7681  async_block = (bool) oob_stack.Pop ();
7682  current_variable = (BlockVariable) oob_stack.Pop ();
7683  current_local_parameters = (ParametersCompiled) oob_stack.Pop ();
7684  current_anonymous_method = (AnonymousMethodExpression) oob_stack.Pop ();
7685
7686  return retval;
7687}
7688
7689void Error_SyntaxError (int token)
7690{
7691  Error_SyntaxError (0, token);
7692}
7693
7694void Error_SyntaxError (int error_code, int token)
7695{
7696  Error_SyntaxError (error_code, token, "Unexpected symbol");
7697}
7698
7699void Error_SyntaxError (int error_code, int token, string msg)
7700{
7701  Lexer.CompleteOnEOF = false;
7702
7703  // An error message has been reported by tokenizer
7704  if (token == Token.ERROR)
7705    return;
7706 
7707  // Avoid duplicit error message after unterminated string literals
7708  if (token == Token.LITERAL && lexer.Location.Column == 0)
7709    return;
7710
7711  string symbol = GetSymbolName (token);
7712  string expecting = GetExpecting ();
7713  var loc = lexer.Location - symbol.Length;
7714 
7715  if (error_code == 0) {
7716    if (expecting == "`identifier'") {
7717      if (token > Token.FIRST_KEYWORD && token < Token.LAST_KEYWORD) {
7718        report.Error (1041, loc, "Identifier expected, `{0}' is a keyword", symbol);
7719        return;
7720      }
7721     
7722      error_code = 1001;
7723      expecting = "identifier";
7724    } else if (expecting == "`)'") {
7725      error_code = 1026;
7726    } else {
7727      error_code = 1525;
7728    }
7729  }
7730 
7731  if (string.IsNullOrEmpty (expecting))
7732    report.Error (error_code, loc, "{1} `{0}'", symbol, msg);
7733  else
7734    report.Error (error_code, loc, "{2} `{0}', expecting {1}", symbol, expecting, msg);   
7735}
7736
7737string GetExpecting ()
7738{
7739  int [] tokens = yyExpectingTokens (yyExpectingState);
7740  var names = new List<string> (tokens.Length);
7741  bool has_type = false;
7742  bool has_identifier = false;
7743  for (int i = 0; i < tokens.Length; i++){
7744    int token = tokens [i];
7745    has_identifier |= token == Token.IDENTIFIER;
7746   
7747    string name = GetTokenName (token);
7748    if (name == "<internal>")
7749      continue;
7750     
7751    has_type |= name == "type";
7752    if (names.Contains (name))
7753      continue;
7754   
7755    names.Add (name);
7756  }
7757
7758  //
7759  // Too many tokens to enumerate
7760  //
7761  if (names.Count > 8)
7762    return null;
7763
7764  if (has_type && has_identifier)
7765    names.Remove ("identifier");
7766
7767  if (names.Count == 1)
7768    return "`" + GetTokenName (tokens [0]) + "'";
7769 
7770  StringBuilder sb = new StringBuilder ();
7771  names.Sort ();
7772  int count = names.Count;
7773  for (int i = 0; i < count; i++){
7774    bool last = i + 1 == count;
7775    if (last)
7776      sb.Append ("or ");
7777    sb.Append ('`');
7778    sb.Append (names [i]);
7779    sb.Append (last ? "'" : count < 3 ? "' " : "', ");
7780  }
7781  return sb.ToString ();
7782}
7783
7784
7785string GetSymbolName (int token)
7786{
7787  switch (token){
7788  case Token.LITERAL:
7789    return ((Constant)lexer.Value).GetValue ().ToString ();
7790  case Token.IDENTIFIER:
7791    return ((LocatedToken)lexer.Value).Value;
7792
7793  case Token.BOOL:
7794    return "bool";
7795  case Token.BYTE:
7796    return "byte";
7797  case Token.CHAR:
7798    return "char";
7799  case Token.VOID:
7800    return "void";
7801  case Token.DECIMAL:
7802    return "decimal";
7803  case Token.DOUBLE:
7804    return "double";
7805  case Token.FLOAT:
7806    return "float";
7807  case Token.INT:
7808    return "int";
7809  case Token.LONG:
7810    return "long";
7811  case Token.SBYTE:
7812    return "sbyte";
7813  case Token.SHORT:
7814    return "short";
7815  case Token.STRING:
7816    return "string";
7817  case Token.UINT:
7818    return "uint";
7819  case Token.ULONG:
7820    return "ulong";
7821  case Token.USHORT:
7822    return "ushort";
7823  case Token.OBJECT:
7824    return "object";
7825   
7826  case Token.PLUS:
7827    return "+";
7828  case Token.UMINUS:
7829  case Token.MINUS:
7830    return "-";
7831  case Token.BANG:
7832    return "!";
7833  case Token.BITWISE_AND:
7834    return "&";
7835  case Token.BITWISE_OR:
7836    return "|";
7837  case Token.STAR:
7838    return "*";
7839  case Token.PERCENT:
7840    return "%";
7841  case Token.DIV:
7842    return "/";
7843  case Token.CARRET:
7844    return "^";
7845  case Token.OP_INC:
7846    return "++";
7847  case Token.OP_DEC:
7848    return "--";
7849  case Token.OP_SHIFT_LEFT:
7850    return "<<";
7851  case Token.OP_SHIFT_RIGHT:
7852    return ">>";
7853  case Token.OP_LT:
7854    return "<";
7855  case Token.OP_GT:
7856    return ">";
7857  case Token.OP_LE:
7858    return "<=";
7859  case Token.OP_GE:
7860    return ">=";
7861  case Token.OP_EQ:
7862    return "==";
7863  case Token.OP_NE:
7864    return "!=";
7865  case Token.OP_AND:
7866    return "&&";
7867  case Token.OP_OR:
7868    return "||";
7869  case Token.OP_PTR:
7870    return "->";
7871  case Token.OP_COALESCING:
7872    return "??";
7873  case Token.OP_MULT_ASSIGN:
7874    return "*=";
7875  case Token.OP_DIV_ASSIGN:
7876    return "/=";
7877  case Token.OP_MOD_ASSIGN:
7878    return "%=";
7879  case Token.OP_ADD_ASSIGN:
7880    return "+=";
7881  case Token.OP_SUB_ASSIGN:
7882    return "-=";
7883  case Token.OP_SHIFT_LEFT_ASSIGN:
7884    return "<<=";
7885  case Token.OP_SHIFT_RIGHT_ASSIGN:
7886    return ">>=";
7887  case Token.OP_AND_ASSIGN:
7888    return "&=";
7889  case Token.OP_XOR_ASSIGN:
7890    return "^=";
7891  case Token.OP_OR_ASSIGN:
7892    return "|=";
7893  }
7894
7895  return GetTokenName (token);
7896}
7897
7898static string GetTokenName (int token)
7899{
7900  switch (token){
7901  case Token.ABSTRACT:
7902    return "abstract";
7903  case Token.AS:
7904    return "as";
7905  case Token.ADD:
7906    return "add";
7907  case Token.ASYNC:
7908    return "async";
7909  case Token.BASE:
7910    return "base";
7911  case Token.BREAK:
7912    return "break";
7913  case Token.CASE:
7914    return "case";
7915  case Token.CATCH:
7916    return "catch";
7917  case Token.CHECKED:
7918    return "checked";
7919  case Token.CLASS:
7920    return "class";
7921  case Token.CONST:
7922    return "const";
7923  case Token.CONTINUE:
7924    return "continue";
7925  case Token.DEFAULT:
7926    return "default";
7927  case Token.DELEGATE:
7928    return "delegate";
7929  case Token.DO:
7930    return "do";
7931  case Token.ELSE:
7932    return "else";
7933  case Token.ENUM:
7934    return "enum";
7935  case Token.EVENT:
7936    return "event";
7937  case Token.EXPLICIT:
7938    return "explicit";
7939  case Token.EXTERN:
7940  case Token.EXTERN_ALIAS:
7941    return "extern";
7942  case Token.FALSE:
7943    return "false";
7944  case Token.FINALLY:
7945    return "finally";
7946  case Token.FIXED:
7947    return "fixed";
7948  case Token.FOR:
7949    return "for";
7950  case Token.FOREACH:
7951    return "foreach";
7952  case Token.GOTO:
7953    return "goto";
7954  case Token.IF:
7955    return "if";
7956  case Token.IMPLICIT:
7957    return "implicit";
7958  case Token.IN:
7959    return "in";
7960  case Token.INTERFACE:
7961    return "interface";
7962  case Token.INTERNAL:
7963    return "internal";
7964  case Token.IS:
7965    return "is";
7966  case Token.LOCK:
7967    return "lock";
7968  case Token.NAMESPACE:
7969    return "namespace";
7970  case Token.NEW:
7971    return "new";
7972  case Token.NULL:
7973    return "null";
7974  case Token.OPERATOR:
7975    return "operator";
7976  case Token.OUT:
7977    return "out";
7978  case Token.OVERRIDE:
7979    return "override";
7980  case Token.PARAMS:
7981    return "params";
7982  case Token.PRIVATE:
7983    return "private";
7984  case Token.PROTECTED:
7985    return "protected";
7986  case Token.PUBLIC:
7987    return "public";
7988  case Token.READONLY:
7989    return "readonly";
7990  case Token.REF:
7991    return "ref";
7992  case Token.RETURN:
7993    return "return";
7994  case Token.REMOVE:
7995    return "remove";
7996  case Token.SEALED:
7997    return "sealed";
7998  case Token.SIZEOF:
7999    return "sizeof";
8000  case Token.STACKALLOC:
8001    return "stackalloc";
8002  case Token.STATIC:
8003    return "static";
8004  case Token.STRUCT:
8005    return "struct";
8006  case Token.SWITCH:
8007    return "switch";
8008  case Token.THIS:
8009    return "this";
8010  case Token.THROW:
8011    return "throw";
8012  case Token.TRUE:
8013    return "true";
8014  case Token.TRY:
8015    return "try";
8016  case Token.TYPEOF:
8017    return "typeof";
8018  case Token.UNCHECKED:
8019    return "unchecked";
8020  case Token.UNSAFE:
8021    return "unsafe";
8022  case Token.USING:
8023    return "using";
8024  case Token.VIRTUAL:
8025    return "virtual";
8026  case Token.VOLATILE:
8027    return "volatile";
8028  case Token.WHERE:
8029    return "where";
8030  case Token.WHILE:
8031    return "while";
8032  case Token.ARGLIST:
8033    return "__arglist";
8034  case Token.REFVALUE:
8035    return "__refvalue";
8036  case Token.REFTYPE:
8037    return "__reftype";
8038  case Token.MAKEREF:
8039    return "__makeref";
8040  case Token.PARTIAL:
8041    return "partial";
8042  case Token.ARROW:
8043    return "=>";
8044  case Token.FROM:
8045  case Token.FROM_FIRST:
8046    return "from";
8047  case Token.JOIN:
8048    return "join";
8049  case Token.ON:
8050    return "on";
8051  case Token.EQUALS:
8052    return "equals";
8053  case Token.SELECT:
8054    return "select";
8055  case Token.GROUP:
8056    return "group";
8057  case Token.BY:
8058    return "by";
8059  case Token.LET:
8060    return "let";
8061  case Token.ORDERBY:
8062    return "orderby";
8063  case Token.ASCENDING:
8064    return "ascending";
8065  case Token.DESCENDING:
8066    return "descending";
8067  case Token.INTO:
8068    return "into";
8069  case Token.GET:
8070    return "get";
8071  case Token.SET:
8072    return "set";
8073  case Token.OPEN_BRACE:
8074    return "{";
8075  case Token.CLOSE_BRACE:
8076    return "}";
8077  case Token.OPEN_BRACKET:
8078  case Token.OPEN_BRACKET_EXPR:
8079    return "[";
8080  case Token.CLOSE_BRACKET:
8081    return "]";
8082  case Token.OPEN_PARENS_CAST:
8083  case Token.OPEN_PARENS_LAMBDA:
8084  case Token.OPEN_PARENS:
8085    return "(";
8086  case Token.CLOSE_PARENS:
8087    return ")";
8088  case Token.DOT:
8089    return ".";
8090  case Token.COMMA:
8091    return ",";
8092  case Token.DEFAULT_COLON:
8093    return "default:";
8094  case Token.COLON:
8095    return ":";
8096  case Token.SEMICOLON:
8097    return ";";
8098  case Token.TILDE:
8099    return "~";
8100   
8101  case Token.PLUS:
8102  case Token.UMINUS:
8103  case Token.MINUS:
8104  case Token.BANG:
8105  case Token.OP_LT:
8106  case Token.OP_GT:
8107  case Token.BITWISE_AND:
8108  case Token.BITWISE_OR:
8109  case Token.STAR:
8110  case Token.PERCENT:
8111  case Token.DIV:
8112  case Token.CARRET:
8113  case Token.OP_INC:
8114  case Token.OP_DEC:
8115  case Token.OP_SHIFT_LEFT:
8116  case Token.OP_SHIFT_RIGHT:
8117  case Token.OP_LE:
8118  case Token.OP_GE:
8119  case Token.OP_EQ:
8120  case Token.OP_NE:
8121  case Token.OP_AND:
8122  case Token.OP_OR:
8123  case Token.OP_PTR:
8124  case Token.OP_COALESCING:
8125  case Token.OP_MULT_ASSIGN:
8126  case Token.OP_DIV_ASSIGN:
8127  case Token.OP_MOD_ASSIGN:
8128  case Token.OP_ADD_ASSIGN:
8129  case Token.OP_SUB_ASSIGN:
8130  case Token.OP_SHIFT_LEFT_ASSIGN:
8131  case Token.OP_SHIFT_RIGHT_ASSIGN:
8132  case Token.OP_AND_ASSIGN:
8133  case Token.OP_XOR_ASSIGN:
8134  case Token.OP_OR_ASSIGN:
8135    return "<operator>";
8136
8137  case Token.BOOL:
8138  case Token.BYTE:
8139  case Token.CHAR:
8140  case Token.VOID:
8141  case Token.DECIMAL:
8142  case Token.DOUBLE:
8143  case Token.FLOAT:
8144  case Token.INT:
8145  case Token.LONG:
8146  case Token.SBYTE:
8147  case Token.SHORT:
8148  case Token.STRING:
8149  case Token.UINT:
8150  case Token.ULONG:
8151  case Token.USHORT:
8152  case Token.OBJECT:
8153    return "type";
8154 
8155  case Token.ASSIGN:
8156    return "=";
8157  case Token.OP_GENERICS_LT:
8158  case Token.GENERIC_DIMENSION:
8159    return "<";
8160  case Token.OP_GENERICS_GT:
8161    return ">";
8162  case Token.INTERR:
8163  case Token.INTERR_NULLABLE:
8164    return "?";
8165  case Token.DOUBLE_COLON:
8166    return "::";
8167  case Token.LITERAL:
8168    return "value";
8169  case Token.IDENTIFIER:
8170  case Token.AWAIT:
8171    return "identifier";
8172
8173  case Token.EOF:
8174    return "end-of-file";
8175
8176    // All of these are internal.
8177  case Token.NONE:
8178  case Token.ERROR:
8179  case Token.FIRST_KEYWORD:
8180  case Token.EVAL_COMPILATION_UNIT_PARSER:
8181  case Token.EVAL_USING_DECLARATIONS_UNIT_PARSER:
8182  case Token.EVAL_STATEMENT_PARSER:
8183  case Token.LAST_KEYWORD:
8184  case Token.GENERATE_COMPLETION:
8185  case Token.COMPLETE_COMPLETION:
8186    return "<internal>";
8187
8188    // A bit more robust.
8189  default:
8190    return yyNames [token];
8191        }
8192}
8193
8194/* end end end */
8195}
Note: See TracBrowser for help on using the repository browser.