ab0c799f53916e10a64e16d45532d14b6d2330a8
[mono.git] / mcs / mcs / cs-tokenizer.cs
1 //
2 // cs-tokenizer.cs: The Tokenizer for the C# compiler
3 //                  This also implements the preprocessor
4 //
5 // Author: Miguel de Icaza (miguel@gnu.org)
6 //         Marek Safar (marek.safar@gmail.com)
7 //
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 //
10 // Copyright 2001, 2002 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004-2008 Novell, Inc
12 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
13 //
14
15 using System;
16 using System.Text;
17 using System.Collections.Generic;
18 using System.Globalization;
19 using System.Diagnostics;
20 using System.Collections;
21
22 namespace Mono.CSharp
23 {
24         //
25         // This class has to be used by parser only, it reuses token
26         // details once a file is parsed
27         //
28         public class LocatedToken
29         {
30                 public int row, column;
31                 public string value;
32                 public SourceFile file;
33
34                 public LocatedToken ()
35                 {
36                 }
37
38                 public LocatedToken (string value, Location loc)
39                 {
40                         this.value = value;
41                         file = loc.SourceFile;
42                         row = loc.Row;
43                         column = loc.Column;
44                 }
45
46                 public override string ToString ()
47                 {
48                         return string.Format ("Token '{0}' at {1},{2}", Value, row, column);
49                 }
50
51                 public Location Location
52                 {
53                         get { return new Location (file, row, column); }
54                 }
55
56                 public string Value
57                 {
58                         get { return value; }
59                 }
60         }
61
62         /// <summary>
63         ///    Tokenizer for C# source code. 
64         /// </summary>
65         public class Tokenizer : yyParser.yyInput
66         {
67                 class KeywordEntry<T>
68                 {
69                         public readonly T Token;
70                         public KeywordEntry<T> Next;
71                         public readonly char[] Value;
72
73                         public KeywordEntry (string value, T token)
74                         {
75                                 this.Value = value.ToCharArray ();
76                                 this.Token = token;
77                         }
78                 }
79
80                 sealed class IdentifiersComparer : IEqualityComparer<char[]>
81                 {
82                         readonly int length;
83
84                         public IdentifiersComparer (int length)
85                         {
86                                 this.length = length;
87                         }
88
89                         public bool Equals (char[] x, char[] y)
90                         {
91                                 for (int i = 0; i < length; ++i)
92                                         if (x [i] != y [i])
93                                                 return false;
94
95                                 return true;
96                         }
97
98                         public int GetHashCode (char[] obj)
99                         {
100                                 int h = 0;
101                                 for (int i = 0; i < length; ++i)
102                                         h = (h << 5) - h + obj [i];
103
104                                 return h;
105                         }
106                 }
107
108                 public class LocatedTokenBuffer
109                 {
110                         readonly LocatedToken[] buffer;
111                         public int pos;
112
113                         public LocatedTokenBuffer ()
114                         {
115                                 buffer = new LocatedToken[0];
116                         }
117
118                         public LocatedTokenBuffer (LocatedToken[] buffer)
119                         {
120                                 this.buffer = buffer ?? new LocatedToken[0];
121                         }
122
123                         public LocatedToken Create (SourceFile file, int row, int column)
124                         {
125                                 return Create (null, file, row, column);
126                         }
127
128                         public LocatedToken Create (string value, SourceFile file, int row, int column)
129                         {
130                                 //
131                                 // TODO: I am not very happy about the logic but it's the best
132                                 // what I could come up with for now.
133                                 // Ideally we should be using just tiny buffer (256 elements) which
134                                 // is enough to hold all details for currect stack and recycle elements
135                                 // poped from the stack but there is a trick needed to recycle
136                                 // them properly.
137                                 //
138                                 LocatedToken entry;
139                                 if (pos >= buffer.Length) {
140                                         entry = new LocatedToken ();
141                                 } else {
142                                         entry = buffer[pos];
143                                         if (entry == null) {
144                                                 entry = new LocatedToken ();
145                                                 buffer[pos] = entry;
146                                         }
147
148                                         ++pos;
149                                 }
150                                 entry.value = value;
151                                 entry.file = file;
152                                 entry.row = row;
153                                 entry.column = column;
154                                 return entry;
155                         }
156
157                         //
158                         // Used for token not required by expression evaluator
159                         //
160                         [Conditional ("FULL_AST")]
161                         public void CreateOptional (SourceFile file, int row, int col, ref object token)
162                         {
163                                 token = Create (file, row, col);
164                         }
165                 }
166
167                 public enum PreprocessorDirective
168                 {
169                         Invalid = 0,
170
171                         Region = 1,
172                         Endregion = 2,
173                         If = 3 | RequiresArgument,
174                         Endif = 4,
175                         Elif = 5 | RequiresArgument,
176                         Else = 6,
177                         Define = 7 | RequiresArgument,
178                         Undef = 8 | RequiresArgument,
179                         Error = 9,
180                         Warning = 10,
181                         Pragma = 11 | CustomArgumentsParsing,
182                         Line = 12 | CustomArgumentsParsing,
183
184                         CustomArgumentsParsing = 1 << 10,
185                         RequiresArgument = 1 << 11
186                 }
187
188                 readonly SeekableStreamReader reader;
189                 readonly CompilationSourceFile source_file;
190                 readonly CompilerContext context;
191
192                 SourceFile current_source;
193                 Location hidden_block_start;
194                 int ref_line = 1;
195                 int line = 1;
196                 int col = 0;
197                 int previous_col;
198                 int current_token;
199                 readonly int tab_size;
200                 bool handle_get_set = false;
201                 bool handle_remove_add = false;
202                 bool handle_where;
203                 bool handle_typeof = false;
204                 bool lambda_arguments_parsing;
205                 List<Location> escaped_identifiers;
206                 int parsing_generic_less_than;
207                 readonly bool doc_processing;
208                 readonly LocatedTokenBuffer ltb;
209                 
210                 //
211                 // Used mainly for parser optimizations. Some expressions for instance
212                 // can appear only in block (including initializer, base initializer)
213                 // scope only
214                 //
215                 public int parsing_block;
216                 internal bool query_parsing;
217                 
218                 // 
219                 // When parsing type only, useful for ambiguous nullable types
220                 //
221                 public int parsing_type;
222                 
223                 //
224                 // Set when parsing generic declaration (type or method header)
225                 //
226                 public bool parsing_generic_declaration;
227                 public bool parsing_generic_declaration_doc;
228                 
229                 //
230                 // The value indicates that we have not reach any declaration or
231                 // namespace yet
232                 //
233                 public int parsing_declaration;
234
235                 public bool parsing_attribute_section;
236
237                 public bool parsing_modifiers;
238
239                 //
240                 // The special characters to inject on streams to run the unit parser
241                 // in the special expression mode. Using private characters from
242                 // Plane Sixteen (U+100000 to U+10FFFD)
243                 //
244                 // This character is only tested just before the tokenizer is about to report
245                 // an error;   So on the regular operation mode, this addition will have no
246                 // impact on the tokenizer's performance.
247                 //
248                 
249                 public const int EvalStatementParserCharacter = 0x100000;
250                 public const int EvalCompilationUnitParserCharacter = 0x100001;
251                 public const int EvalUsingDeclarationsParserCharacter = 0x100002;
252                 public const int DocumentationXref = 0x100003;
253                 
254                 //
255                 // XML documentation buffer. The save point is used to divide
256                 // comments on types and comments on members.
257                 //
258                 StringBuilder xml_comment_buffer;
259
260                 //
261                 // See comment on XmlCommentState enumeration.
262                 //
263                 XmlCommentState xml_doc_state = XmlCommentState.Allowed;
264
265                 //
266                 // Whether tokens have been seen on this line
267                 //
268                 bool tokens_seen = false;
269
270                 //
271                 // Set to true once the GENERATE_COMPLETION token has bee
272                 // returned.   This helps produce one GENERATE_COMPLETION,
273                 // as many COMPLETE_COMPLETION as necessary to complete the
274                 // AST tree and one final EOF.
275                 //
276                 bool generated;
277                 
278                 //
279                 // Whether a token has been seen on the file
280                 // This is needed because `define' is not allowed to be used
281                 // after a token has been seen.
282                 //
283                 bool any_token_seen;
284
285                 //
286                 // Class variables
287                 // 
288                 static readonly KeywordEntry<int>[][] keywords;
289                 static readonly KeywordEntry<PreprocessorDirective>[][] keywords_preprocessor;
290                 static readonly HashSet<string> keyword_strings;
291                 static readonly NumberStyles styles;
292                 static readonly NumberFormatInfo csharp_format_info;
293
294                 // Pragma arguments
295                 static readonly char[] pragma_warning = "warning".ToCharArray ();
296                 static readonly char[] pragma_warning_disable = "disable".ToCharArray ();
297                 static readonly char[] pragma_warning_restore = "restore".ToCharArray ();
298                 static readonly char[] pragma_checksum = "checksum".ToCharArray ();
299                 static readonly char[] line_hidden = "hidden".ToCharArray ();
300                 static readonly char[] line_default = "default".ToCharArray ();
301
302                 static readonly char[] simple_whitespaces = new char[] { ' ', '\t' };
303
304                 public bool PropertyParsing {
305                         get { return handle_get_set; }
306                         set { handle_get_set = value; }
307                 }
308
309                 public bool EventParsing {
310                         get { return handle_remove_add; }
311                         set { handle_remove_add = value; }
312                 }
313
314                 public bool ConstraintsParsing {
315                         get { return handle_where; }
316                         set { handle_where = value; }
317                 }
318
319                 public bool TypeOfParsing {
320                         get { return handle_typeof; }
321                         set { handle_typeof = value; }
322                 }
323         
324                 public XmlCommentState doc_state {
325                         get { return xml_doc_state; }
326                         set {
327                                 if (value == XmlCommentState.Allowed) {
328                                         check_incorrect_doc_comment ();
329                                         reset_doc_comment ();
330                                 }
331                                 xml_doc_state = value;
332                         }
333                 }
334
335                 //
336                 // This is used to trigger completion generation on the parser
337                 public bool CompleteOnEOF;
338                 
339                 void AddEscapedIdentifier (Location loc)
340                 {
341                         if (escaped_identifiers == null)
342                                 escaped_identifiers = new List<Location> ();
343
344                         escaped_identifiers.Add (loc);
345                 }
346
347                 public bool IsEscapedIdentifier (ATypeNameExpression name)
348                 {
349                         return escaped_identifiers != null && escaped_identifiers.Contains (name.Location);
350                 }
351
352                 //
353                 // Values for the associated token returned
354                 //
355                 internal int putback_char;      // Used by repl only
356                 object val;
357
358                 //
359                 // Pre-processor
360                 //
361                 const int TAKING        = 1;
362                 const int ELSE_SEEN     = 4;
363                 const int PARENT_TAKING = 8;
364                 const int REGION        = 16;           
365
366                 //
367                 // pre-processor if stack state:
368                 //
369                 Stack<int> ifstack;
370
371                 public const int MaxIdentifierLength = 512;
372                 public const int MaxNumberLength = 512;
373
374                 readonly char[] id_builder;
375                 readonly Dictionary<char[], string>[] identifiers;
376                 readonly char[] number_builder;
377                 int number_pos;
378
379                 char[] value_builder = new char[64];
380
381                 public int Line {
382                         get {
383                                 return ref_line;
384                         }
385                 }
386
387                 //
388                 // This is used when the tokenizer needs to save
389                 // the current position as it needs to do some parsing
390                 // on its own to deamiguate a token in behalf of the
391                 // parser.
392                 //
393                 Stack<Position> position_stack = new Stack<Position> (2);
394
395                 class Position {
396                         public int position;
397                         public int line;
398                         public int ref_line;
399                         public int col;
400                         public Location hidden;
401                         public int putback_char;
402                         public int previous_col;
403                         public Stack<int> ifstack;
404                         public int parsing_generic_less_than;
405                         public int current_token;
406                         public object val;
407
408                         public Position (Tokenizer t)
409                         {
410                                 position = t.reader.Position;
411                                 line = t.line;
412                                 ref_line = t.ref_line;
413                                 col = t.col;
414                                 hidden = t.hidden_block_start;
415                                 putback_char = t.putback_char;
416                                 previous_col = t.previous_col;
417                                 if (t.ifstack != null && t.ifstack.Count != 0) {
418                                         // There is no simple way to clone Stack<T> all
419                                         // methods reverse the order
420                                         var clone = t.ifstack.ToArray ();
421                                         Array.Reverse (clone);
422                                         ifstack = new Stack<int> (clone);
423                                 }
424                                 parsing_generic_less_than = t.parsing_generic_less_than;
425                                 current_token = t.current_token;
426                                 val = t.val;
427                         }
428                 }
429
430                 public Tokenizer (SeekableStreamReader input, CompilationSourceFile file, ParserSession session)
431                 {
432                         this.source_file = file;
433                         this.context = file.Compiler;
434                         this.current_source = file.SourceFile;
435                         this.identifiers = session.Identifiers;
436                         this.id_builder = session.IDBuilder;
437                         this.number_builder = session.NumberBuilder;
438                         this.ltb = new LocatedTokenBuffer (session.LocatedTokens);
439
440                         reader = input;
441
442                         putback_char = -1;
443
444                         xml_comment_buffer = new StringBuilder ();
445                         doc_processing = context.Settings.DocumentationFile != null;
446
447                         tab_size = context.Settings.TabSize;
448                 }
449                 
450                 public void PushPosition ()
451                 {
452                         position_stack.Push (new Position (this));
453                 }
454
455                 public void PopPosition ()
456                 {
457                         Position p = position_stack.Pop ();
458
459                         reader.Position = p.position;
460                         ref_line = p.ref_line;
461                         line = p.line;
462                         col = p.col;
463                         hidden_block_start = p.hidden;
464                         putback_char = p.putback_char;
465                         previous_col = p.previous_col;
466                         ifstack = p.ifstack;
467                         parsing_generic_less_than = p.parsing_generic_less_than;
468                         current_token = p.current_token;
469                         val = p.val;
470                 }
471
472                 // Do not reset the position, ignore it.
473                 public void DiscardPosition ()
474                 {
475                         position_stack.Pop ();
476                 }
477                 
478                 static void AddKeyword (string kw, int token)
479                 {
480                         keyword_strings.Add (kw);
481
482                         AddKeyword (keywords, kw, token);
483                 }
484
485                 static void AddPreprocessorKeyword (string kw, PreprocessorDirective directive)
486                 {
487                         AddKeyword (keywords_preprocessor, kw, directive);
488                 }
489
490                 static void AddKeyword<T> (KeywordEntry<T>[][] keywords, string kw, T token)
491                 {
492                         int length = kw.Length;
493                         if (keywords[length] == null) {
494                                 keywords[length] = new KeywordEntry<T>['z' - '_' + 1];
495                         }
496
497                         int char_index = kw[0] - '_';
498                         var kwe = keywords[length][char_index];
499                         if (kwe == null) {
500                                 keywords[length][char_index] = new KeywordEntry<T> (kw, token);
501                                 return;
502                         }
503
504                         while (kwe.Next != null) {
505                                 kwe = kwe.Next;
506                         }
507
508                         kwe.Next = new KeywordEntry<T> (kw, token);
509                 }
510
511                 //
512                 // Class initializer
513                 // 
514                 static Tokenizer ()
515                 {
516                         keyword_strings = new HashSet<string> ();
517
518                         // 11 is the length of the longest keyword for now
519                         keywords = new KeywordEntry<int>[11][];
520
521                         AddKeyword ("__arglist", Token.ARGLIST);
522                         AddKeyword ("__makeref", Token.MAKEREF);
523                         AddKeyword ("__reftype", Token.REFTYPE);
524                         AddKeyword ("__refvalue", Token.REFVALUE);
525                         AddKeyword ("abstract", Token.ABSTRACT);
526                         AddKeyword ("as", Token.AS);
527                         AddKeyword ("add", Token.ADD);
528                         AddKeyword ("base", Token.BASE);
529                         AddKeyword ("bool", Token.BOOL);
530                         AddKeyword ("break", Token.BREAK);
531                         AddKeyword ("byte", Token.BYTE);
532                         AddKeyword ("case", Token.CASE);
533                         AddKeyword ("catch", Token.CATCH);
534                         AddKeyword ("char", Token.CHAR);
535                         AddKeyword ("checked", Token.CHECKED);
536                         AddKeyword ("class", Token.CLASS);
537                         AddKeyword ("const", Token.CONST);
538                         AddKeyword ("continue", Token.CONTINUE);
539                         AddKeyword ("decimal", Token.DECIMAL);
540                         AddKeyword ("default", Token.DEFAULT);
541                         AddKeyword ("delegate", Token.DELEGATE);
542                         AddKeyword ("do", Token.DO);
543                         AddKeyword ("double", Token.DOUBLE);
544                         AddKeyword ("else", Token.ELSE);
545                         AddKeyword ("enum", Token.ENUM);
546                         AddKeyword ("event", Token.EVENT);
547                         AddKeyword ("explicit", Token.EXPLICIT);
548                         AddKeyword ("extern", Token.EXTERN);
549                         AddKeyword ("false", Token.FALSE);
550                         AddKeyword ("finally", Token.FINALLY);
551                         AddKeyword ("fixed", Token.FIXED);
552                         AddKeyword ("float", Token.FLOAT);
553                         AddKeyword ("for", Token.FOR);
554                         AddKeyword ("foreach", Token.FOREACH);
555                         AddKeyword ("goto", Token.GOTO);
556                         AddKeyword ("get", Token.GET);
557                         AddKeyword ("if", Token.IF);
558                         AddKeyword ("implicit", Token.IMPLICIT);
559                         AddKeyword ("in", Token.IN);
560                         AddKeyword ("int", Token.INT);
561                         AddKeyword ("interface", Token.INTERFACE);
562                         AddKeyword ("internal", Token.INTERNAL);
563                         AddKeyword ("is", Token.IS);
564                         AddKeyword ("lock", Token.LOCK);
565                         AddKeyword ("long", Token.LONG);
566                         AddKeyword ("namespace", Token.NAMESPACE);
567                         AddKeyword ("new", Token.NEW);
568                         AddKeyword ("null", Token.NULL);
569                         AddKeyword ("object", Token.OBJECT);
570                         AddKeyword ("operator", Token.OPERATOR);
571                         AddKeyword ("out", Token.OUT);
572                         AddKeyword ("override", Token.OVERRIDE);
573                         AddKeyword ("params", Token.PARAMS);
574                         AddKeyword ("private", Token.PRIVATE);
575                         AddKeyword ("protected", Token.PROTECTED);
576                         AddKeyword ("public", Token.PUBLIC);
577                         AddKeyword ("readonly", Token.READONLY);
578                         AddKeyword ("ref", Token.REF);
579                         AddKeyword ("remove", Token.REMOVE);
580                         AddKeyword ("return", Token.RETURN);
581                         AddKeyword ("sbyte", Token.SBYTE);
582                         AddKeyword ("sealed", Token.SEALED);
583                         AddKeyword ("set", Token.SET);
584                         AddKeyword ("short", Token.SHORT);
585                         AddKeyword ("sizeof", Token.SIZEOF);
586                         AddKeyword ("stackalloc", Token.STACKALLOC);
587                         AddKeyword ("static", Token.STATIC);
588                         AddKeyword ("string", Token.STRING);
589                         AddKeyword ("struct", Token.STRUCT);
590                         AddKeyword ("switch", Token.SWITCH);
591                         AddKeyword ("this", Token.THIS);
592                         AddKeyword ("throw", Token.THROW);
593                         AddKeyword ("true", Token.TRUE);
594                         AddKeyword ("try", Token.TRY);
595                         AddKeyword ("typeof", Token.TYPEOF);
596                         AddKeyword ("uint", Token.UINT);
597                         AddKeyword ("ulong", Token.ULONG);
598                         AddKeyword ("unchecked", Token.UNCHECKED);
599                         AddKeyword ("unsafe", Token.UNSAFE);
600                         AddKeyword ("ushort", Token.USHORT);
601                         AddKeyword ("using", Token.USING);
602                         AddKeyword ("virtual", Token.VIRTUAL);
603                         AddKeyword ("void", Token.VOID);
604                         AddKeyword ("volatile", Token.VOLATILE);
605                         AddKeyword ("while", Token.WHILE);
606                         AddKeyword ("partial", Token.PARTIAL);
607                         AddKeyword ("where", Token.WHERE);
608
609                         // LINQ keywords
610                         AddKeyword ("from", Token.FROM);
611                         AddKeyword ("join", Token.JOIN);
612                         AddKeyword ("on", Token.ON);
613                         AddKeyword ("equals", Token.EQUALS);
614                         AddKeyword ("select", Token.SELECT);
615                         AddKeyword ("group", Token.GROUP);
616                         AddKeyword ("by", Token.BY);
617                         AddKeyword ("let", Token.LET);
618                         AddKeyword ("orderby", Token.ORDERBY);
619                         AddKeyword ("ascending", Token.ASCENDING);
620                         AddKeyword ("descending", Token.DESCENDING);
621                         AddKeyword ("into", Token.INTO);
622
623                         // Contextual async keywords
624                         AddKeyword ("async", Token.ASYNC);
625                         AddKeyword ("await", Token.AWAIT);
626
627                         keywords_preprocessor = new KeywordEntry<PreprocessorDirective>[10][];
628
629                         AddPreprocessorKeyword ("region", PreprocessorDirective.Region);
630                         AddPreprocessorKeyword ("endregion", PreprocessorDirective.Endregion);
631                         AddPreprocessorKeyword ("if", PreprocessorDirective.If);
632                         AddPreprocessorKeyword ("endif", PreprocessorDirective.Endif);
633                         AddPreprocessorKeyword ("elif", PreprocessorDirective.Elif);
634                         AddPreprocessorKeyword ("else", PreprocessorDirective.Else);
635                         AddPreprocessorKeyword ("define", PreprocessorDirective.Define);
636                         AddPreprocessorKeyword ("undef", PreprocessorDirective.Undef);
637                         AddPreprocessorKeyword ("error", PreprocessorDirective.Error);
638                         AddPreprocessorKeyword ("warning", PreprocessorDirective.Warning);
639                         AddPreprocessorKeyword ("pragma", PreprocessorDirective.Pragma);
640                         AddPreprocessorKeyword ("line", PreprocessorDirective.Line);
641
642                         csharp_format_info = NumberFormatInfo.InvariantInfo;
643                         styles = NumberStyles.Float;
644                 }
645
646                 int GetKeyword (char[] id, int id_len)
647                 {
648                         //
649                         // Keywords are stored in an array of arrays grouped by their
650                         // length and then by the first character
651                         //
652                         if (id_len >= keywords.Length || keywords [id_len] == null)
653                                 return -1;
654
655                         int first_index = id [0] - '_';
656                         if (first_index > 'z' - '_')
657                                 return -1;
658
659                         var kwe = keywords [id_len] [first_index];
660                         if (kwe == null)
661                                 return -1;
662
663                         int res;
664                         do {
665                                 res = kwe.Token;
666                                 for (int i = 1; i < id_len; ++i) {
667                                         if (id [i] != kwe.Value [i]) {
668                                                 res = 0;
669                                                 kwe = kwe.Next;
670                                                 break;
671                                         }
672                                 }
673                         } while (res == 0 && kwe != null);
674
675                         if (res == 0)
676                                 return -1;
677
678                         int next_token;
679                         switch (res) {
680                         case Token.GET:
681                         case Token.SET:
682                                 if (!handle_get_set)
683                                         res = -1;
684                                 break;
685                         case Token.REMOVE:
686                         case Token.ADD:
687                                 if (!handle_remove_add)
688                                         res = -1;
689                                 break;
690                         case Token.EXTERN:
691                                 if (parsing_declaration == 0)
692                                         res = Token.EXTERN_ALIAS;
693                                 break;
694                         case Token.DEFAULT:
695                                 if (peek_token () == Token.COLON) {
696                                         token ();
697                                         res = Token.DEFAULT_COLON;
698                                 }
699                                 break;
700                         case Token.WHERE:
701                                 if (!(handle_where && current_token != Token.COLON) && !query_parsing)
702                                         res = -1;
703                                 break;
704                         case Token.FROM:
705                                 //
706                                 // A query expression is any expression that starts with `from identifier'
707                                 // followed by any token except ; , =
708                                 // 
709                                 if (!query_parsing) {
710                                         if (lambda_arguments_parsing || parsing_block == 0) {
711                                                 res = -1;
712                                                 break;
713                                         }
714
715                                         PushPosition ();
716                                         // HACK: to disable generics micro-parser, because PushPosition does not
717                                         // store identifiers array
718                                         parsing_generic_less_than = 1;
719                                         switch (xtoken ()) {
720                                         case Token.IDENTIFIER:
721                                         case Token.INT:
722                                         case Token.BOOL:
723                                         case Token.BYTE:
724                                         case Token.CHAR:
725                                         case Token.DECIMAL:
726                                         case Token.FLOAT:
727                                         case Token.LONG:
728                                         case Token.OBJECT:
729                                         case Token.STRING:
730                                         case Token.UINT:
731                                         case Token.ULONG:
732                                                 next_token = xtoken ();
733                                                 if (next_token == Token.SEMICOLON || next_token == Token.COMMA || next_token == Token.EQUALS || next_token == Token.ASSIGN)
734                                                         goto default;
735                                                 
736                                                 res = Token.FROM_FIRST;
737                                                 query_parsing = true;
738                                                 if (context.Settings.Version <= LanguageVersion.ISO_2)
739                                                         Report.FeatureIsNotAvailable (context, Location, "query expressions");
740                                                 break;
741                                         case Token.VOID:
742                                                 Expression.Error_VoidInvalidInTheContext (Location, Report);
743                                                 break;
744                                         default:
745                                                 PopPosition ();
746                                                 // HACK: A token is not a keyword so we need to restore identifiers buffer
747                                                 // which has been overwritten before we grabbed the identifier
748                                                 id_builder [0] = 'f'; id_builder [1] = 'r'; id_builder [2] = 'o'; id_builder [3] = 'm';
749                                                 return -1;
750                                         }
751                                         PopPosition ();
752                                 }
753                                 break;
754                         case Token.JOIN:
755                         case Token.ON:
756                         case Token.EQUALS:
757                         case Token.SELECT:
758                         case Token.GROUP:
759                         case Token.BY:
760                         case Token.LET:
761                         case Token.ORDERBY:
762                         case Token.ASCENDING:
763                         case Token.DESCENDING:
764                         case Token.INTO:
765                                 if (!query_parsing)
766                                         res = -1;
767                                 break;
768                                 
769                         case Token.USING:
770                         case Token.NAMESPACE:
771                                 // TODO: some explanation needed
772                                 check_incorrect_doc_comment ();
773                                 parsing_modifiers = false;
774                                 break;
775                                 
776                         case Token.PARTIAL:
777                                 if (parsing_block > 0) {
778                                         res = -1;
779                                         break;
780                                 }
781
782                                 // Save current position and parse next token.
783                                 PushPosition ();
784
785                                 next_token = token ();
786                                 bool ok = (next_token == Token.CLASS) ||
787                                         (next_token == Token.STRUCT) ||
788                                         (next_token == Token.INTERFACE) ||
789                                         (next_token == Token.VOID);
790
791                                 PopPosition ();
792
793                                 if (ok) {
794                                         if (next_token == Token.VOID) {
795                                                 if (context.Settings.Version <= LanguageVersion.ISO_2)
796                                                         Report.FeatureIsNotAvailable (context, Location, "partial methods");
797                                         } else if (context.Settings.Version == LanguageVersion.ISO_1)
798                                                 Report.FeatureIsNotAvailable (context, Location, "partial types");
799
800                                         return res;
801                                 }
802
803                                 if (next_token < Token.LAST_KEYWORD) {
804                                         Report.Error (267, Location,
805                                                 "The `partial' modifier can be used only immediately before `class', `struct', `interface', or `void' keyword");
806                                         return token ();
807                                 }
808
809                                 // HACK: A token is not a keyword so we need to restore identifiers buffer
810                                 // which has been overwritten before we grabbed the identifier
811                                 id_builder[0] = 'p';
812                                 id_builder[1] = 'a';
813                                 id_builder[2] = 'r';
814                                 id_builder[3] = 't';
815                                 id_builder[4] = 'i';
816                                 id_builder[5] = 'a';
817                                 id_builder[6] = 'l';
818                                 res = -1;
819                                 break;
820
821                         case Token.ASYNC:
822                                 if (parsing_modifiers) {
823                                         //
824                                         // Skip attributes section or constructor called async
825                                         //
826                                         if (parsing_attribute_section || peek_token () == Token.OPEN_PARENS) {
827                                                 res = -1;
828                                         } else {
829                                                 // async is keyword
830                                         }
831                                 } else if (parsing_block > 0) {
832                                         switch (peek_token ()) {
833                                         case Token.DELEGATE:
834                                         case Token.OPEN_PARENS_LAMBDA:
835                                                 // async is keyword
836                                                 break;
837                                         case Token.IDENTIFIER:
838                                                 PushPosition ();
839                                                 xtoken ();
840                                                 if (xtoken () != Token.ARROW) {
841                                                         PopPosition ();
842                                                         goto default;
843                                                 }
844
845                                                 PopPosition ();
846                                                 break;
847                                         default:
848                                                 // peek_token could overwrite id_buffer
849                                                 id_builder [0] = 'a'; id_builder [1] = 's'; id_builder [2] = 'y'; id_builder [3] = 'n'; id_builder [4] = 'c';
850                                                 res = -1;
851                                                 break;
852                                         }
853                                 } else {
854                                         res = -1;
855                                 }
856
857                                 if (res == Token.ASYNC && context.Settings.Version <= LanguageVersion.V_4) {
858                                         Report.FeatureIsNotAvailable (context, Location, "asynchronous functions");
859                                 }
860                                 
861                                 break;
862
863                         case Token.AWAIT:
864                                 if (parsing_block == 0)
865                                         res = -1;
866
867                                 break;
868                         }
869
870
871                         return res;
872                 }
873
874                 static PreprocessorDirective GetPreprocessorDirective (char[] id, int id_len)
875                 {
876                         //
877                         // Keywords are stored in an array of arrays grouped by their
878                         // length and then by the first character
879                         //
880                         if (id_len >= keywords_preprocessor.Length || keywords_preprocessor[id_len] == null)
881                                 return PreprocessorDirective.Invalid;
882
883                         int first_index = id[0] - '_';
884                         if (first_index > 'z' - '_')
885                                 return PreprocessorDirective.Invalid;
886
887                         var kwe = keywords_preprocessor[id_len][first_index];
888                         if (kwe == null)
889                                 return PreprocessorDirective.Invalid;
890
891                         PreprocessorDirective res = PreprocessorDirective.Invalid;
892                         do {
893                                 res = kwe.Token;
894                                 for (int i = 1; i < id_len; ++i) {
895                                         if (id[i] != kwe.Value[i]) {
896                                                 res = 0;
897                                                 kwe = kwe.Next;
898                                                 break;
899                                         }
900                                 }
901                         } while (res == PreprocessorDirective.Invalid && kwe != null);
902
903                         return res;
904                 }
905
906                 public Location Location {
907                         get {
908                                 return new Location (current_source, ref_line, col);
909                         }
910                 }
911
912                 static bool is_identifier_start_character (int c)
913                 {
914                         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || Char.IsLetter ((char)c);
915                 }
916
917                 static bool is_identifier_part_character (char c)
918                 {
919                         if (c >= 'a' && c <= 'z')
920                                 return true;
921
922                         if (c >= 'A' && c <= 'Z')
923                                 return true;
924
925                         if (c == '_' || (c >= '0' && c <= '9'))
926                                 return true;
927
928                         if (c < 0x80)
929                                 return false;
930
931                         return Char.IsLetter (c) || Char.GetUnicodeCategory (c) == UnicodeCategory.ConnectorPunctuation;
932                 }
933
934                 public static bool IsKeyword (string s)
935                 {
936                         return keyword_strings.Contains (s);
937                 }
938
939                 //
940                 // Open parens micro parser. Detects both lambda and cast ambiguity.
941                 //      
942                 int TokenizeOpenParens ()
943                 {
944                         int ptoken;
945                         current_token = -1;
946
947                         int bracket_level = 0;
948                         bool is_type = false;
949                         bool can_be_type = false;
950                         
951                         while (true) {
952                                 ptoken = current_token;
953                                 token ();
954
955                                 switch (current_token) {
956                                 case Token.CLOSE_PARENS:
957                                         token ();
958                                         
959                                         //
960                                         // Expression inside parens is lambda, (int i) => 
961                                         //
962                                         if (current_token == Token.ARROW)
963                                                 return Token.OPEN_PARENS_LAMBDA;
964
965                                         //
966                                         // Expression inside parens is single type, (int[])
967                                         //
968                                         if (is_type) {
969                                                 if (current_token == Token.SEMICOLON)
970                                                         return Token.OPEN_PARENS;
971
972                                                 return Token.OPEN_PARENS_CAST;
973                                         }
974
975                                         //
976                                         // Expression is possible cast, look at next token, (T)null
977                                         //
978                                         if (can_be_type) {
979                                                 switch (current_token) {
980                                                 case Token.OPEN_PARENS:
981                                                 case Token.BANG:
982                                                 case Token.TILDE:
983                                                 case Token.IDENTIFIER:
984                                                 case Token.LITERAL:
985                                                 case Token.BASE:
986                                                 case Token.CHECKED:
987                                                 case Token.DELEGATE:
988                                                 case Token.FALSE:
989                                                 case Token.FIXED:
990                                                 case Token.NEW:
991                                                 case Token.NULL:
992                                                 case Token.SIZEOF:
993                                                 case Token.THIS:
994                                                 case Token.THROW:
995                                                 case Token.TRUE:
996                                                 case Token.TYPEOF:
997                                                 case Token.UNCHECKED:
998                                                 case Token.UNSAFE:
999                                                 case Token.DEFAULT:
1000                                                 case Token.AWAIT:
1001
1002                                                 //
1003                                                 // These can be part of a member access
1004                                                 //
1005                                                 case Token.INT:
1006                                                 case Token.UINT:
1007                                                 case Token.SHORT:
1008                                                 case Token.USHORT:
1009                                                 case Token.LONG:
1010                                                 case Token.ULONG:
1011                                                 case Token.DOUBLE:
1012                                                 case Token.FLOAT:
1013                                                 case Token.CHAR:
1014                                                 case Token.BYTE:
1015                                                 case Token.DECIMAL:
1016                                                 case Token.BOOL:
1017                                                         return Token.OPEN_PARENS_CAST;
1018                                                 }
1019                                         }
1020                                         return Token.OPEN_PARENS;
1021                                         
1022                                 case Token.DOT:
1023                                 case Token.DOUBLE_COLON:
1024                                         if (ptoken != Token.IDENTIFIER && ptoken != Token.OP_GENERICS_GT)
1025                                                 goto default;
1026
1027                                         continue;
1028
1029                                 case Token.IDENTIFIER:
1030                                 case Token.AWAIT:
1031                                         switch (ptoken) {
1032                                         case Token.DOT:
1033                                                 if (bracket_level == 0) {
1034                                                         is_type = false;
1035                                                         can_be_type = true;
1036                                                 }
1037
1038                                                 continue;
1039                                         case Token.OP_GENERICS_LT:
1040                                         case Token.COMMA:
1041                                         case Token.DOUBLE_COLON:
1042                                         case -1:
1043                                                 if (bracket_level == 0)
1044                                                         can_be_type = true;
1045                                                 continue;
1046                                         default:
1047                                                 can_be_type = is_type = false;
1048                                                 continue;
1049                                         }
1050
1051                                 case Token.OBJECT:
1052                                 case Token.STRING:
1053                                 case Token.BOOL:
1054                                 case Token.DECIMAL:
1055                                 case Token.FLOAT:
1056                                 case Token.DOUBLE:
1057                                 case Token.SBYTE:
1058                                 case Token.BYTE:
1059                                 case Token.SHORT:
1060                                 case Token.USHORT:
1061                                 case Token.INT:
1062                                 case Token.UINT:
1063                                 case Token.LONG:
1064                                 case Token.ULONG:
1065                                 case Token.CHAR:
1066                                 case Token.VOID:
1067                                         if (bracket_level == 0)
1068                                                 is_type = true;
1069                                         continue;
1070
1071                                 case Token.COMMA:
1072                                         if (bracket_level == 0) {
1073                                                 bracket_level = 100;
1074                                                 can_be_type = is_type = false;
1075                                         }
1076                                         continue;
1077
1078                                 case Token.OP_GENERICS_LT:
1079                                 case Token.OPEN_BRACKET:
1080                                         if (bracket_level++ == 0)
1081                                                 is_type = true;
1082                                         continue;
1083
1084                                 case Token.OP_GENERICS_GT:
1085                                 case Token.CLOSE_BRACKET:
1086                                         --bracket_level;
1087                                         continue;
1088
1089                                 case Token.INTERR_NULLABLE:
1090                                 case Token.STAR:
1091                                         if (bracket_level == 0)
1092                                                 is_type = true;
1093                                         continue;
1094
1095                                 case Token.REF:
1096                                 case Token.OUT:
1097                                         can_be_type = is_type = false;
1098                                         continue;
1099
1100                                 default:
1101                                         return Token.OPEN_PARENS;
1102                                 }
1103                         }
1104                 }
1105
1106                 public static bool IsValidIdentifier (string s)
1107                 {
1108                         if (s == null || s.Length == 0)
1109                                 return false;
1110
1111                         if (!is_identifier_start_character (s [0]))
1112                                 return false;
1113                         
1114                         for (int i = 1; i < s.Length; i ++)
1115                                 if (! is_identifier_part_character (s [i]))
1116                                         return false;
1117                         
1118                         return true;
1119                 }
1120
1121                 bool parse_less_than ()
1122                 {
1123                 start:
1124                         int the_token = token ();
1125                         if (the_token == Token.OPEN_BRACKET) {
1126                                 while (true) {
1127                                         the_token = token ();
1128                                         if (the_token == Token.EOF)
1129                                                 return true;
1130
1131                                         if (the_token == Token.CLOSE_BRACKET)
1132                                                 break;
1133                                 }
1134                                 the_token = token ();
1135                         } else if (the_token == Token.IN || the_token == Token.OUT) {
1136                                 the_token = token ();
1137                         }
1138                         switch (the_token) {
1139                         case Token.IDENTIFIER:
1140                         case Token.OBJECT:
1141                         case Token.STRING:
1142                         case Token.BOOL:
1143                         case Token.DECIMAL:
1144                         case Token.FLOAT:
1145                         case Token.DOUBLE:
1146                         case Token.SBYTE:
1147                         case Token.BYTE:
1148                         case Token.SHORT:
1149                         case Token.USHORT:
1150                         case Token.INT:
1151                         case Token.UINT:
1152                         case Token.LONG:
1153                         case Token.ULONG:
1154                         case Token.CHAR:
1155                         case Token.VOID:
1156                                 break;
1157                         case Token.OP_GENERICS_GT:
1158                         case Token.IN:
1159                         case Token.OUT:
1160                                 return true;
1161
1162                         default:
1163                                 return false;
1164                         }
1165                 again:
1166                         the_token = token ();
1167
1168                         if (the_token == Token.OP_GENERICS_GT)
1169                                 return true;
1170                         else if (the_token == Token.COMMA || the_token == Token.DOT || the_token == Token.DOUBLE_COLON)
1171                                 goto start;
1172                         else if (the_token == Token.INTERR_NULLABLE || the_token == Token.STAR)
1173                                 goto again;
1174                         else if (the_token == Token.OP_GENERICS_LT) {
1175                                 if (!parse_less_than ())
1176                                         return false;
1177                                 goto again;
1178                         } else if (the_token == Token.OPEN_BRACKET) {
1179                         rank_specifiers:
1180                                 the_token = token ();
1181                                 if (the_token == Token.CLOSE_BRACKET)
1182                                         goto again;
1183                                 else if (the_token == Token.COMMA)
1184                                         goto rank_specifiers;
1185                                 return false;
1186                         }
1187
1188                         return false;
1189                 }
1190
1191                 bool parse_generic_dimension (out int dimension)
1192                 {
1193                         dimension = 1;
1194
1195                 again:
1196                         int the_token = token ();
1197                         if (the_token == Token.OP_GENERICS_GT)
1198                                 return true;
1199                         else if (the_token == Token.COMMA) {
1200                                 dimension++;
1201                                 goto again;
1202                         }
1203
1204                         return false;
1205                 }
1206                 
1207                 public int peek_token ()
1208                 {
1209                         int the_token;
1210
1211                         PushPosition ();
1212                         the_token = token ();
1213                         PopPosition ();
1214                         
1215                         return the_token;
1216                 }
1217                                         
1218                 //
1219                 // Tonizes `?' using custom disambiguous rules to return one
1220                 // of following tokens: INTERR_NULLABLE, OP_COALESCING, INTERR
1221                 //
1222                 // Tricky expression look like:
1223                 //
1224                 // Foo ? a = x ? b : c;
1225                 //
1226                 int TokenizePossibleNullableType ()
1227                 {
1228                         if (parsing_block == 0 || parsing_type > 0)
1229                                 return Token.INTERR_NULLABLE;
1230
1231                         int d = peek_char ();
1232                         if (d == '?') {
1233                                 get_char ();
1234                                 return Token.OP_COALESCING;
1235                         }
1236
1237                         switch (current_token) {
1238                         case Token.CLOSE_PARENS:
1239                         case Token.TRUE:
1240                         case Token.FALSE:
1241                         case Token.NULL:
1242                         case Token.LITERAL:
1243                                 return Token.INTERR;
1244                         }
1245
1246                         if (d != ' ') {
1247                                 if (d == ',' || d == ';' || d == '>')
1248                                         return Token.INTERR_NULLABLE;
1249                                 if (d == '*' || (d >= '0' && d <= '9'))
1250                                         return Token.INTERR;
1251                         }
1252
1253                         PushPosition ();
1254                         current_token = Token.NONE;
1255                         int next_token;
1256                         switch (xtoken ()) {
1257                         case Token.LITERAL:
1258                         case Token.TRUE:
1259                         case Token.FALSE:
1260                         case Token.NULL:
1261                         case Token.THIS:
1262                         case Token.NEW:
1263                                 next_token = Token.INTERR;
1264                                 break;
1265                                 
1266                         case Token.SEMICOLON:
1267                         case Token.COMMA:
1268                         case Token.CLOSE_PARENS:
1269                         case Token.OPEN_BRACKET:
1270                         case Token.OP_GENERICS_GT:
1271                         case Token.INTERR:
1272                         case Token.OP_COALESCING:
1273                         case Token.COLON:
1274                                 next_token = Token.INTERR_NULLABLE;
1275                                 break;
1276                                 
1277                         default:
1278                                 next_token = -1;
1279                                 break;
1280                         }
1281
1282                         if (next_token == -1) {
1283                                 switch (xtoken ()) {
1284                                 case Token.COMMA:
1285                                 case Token.SEMICOLON:
1286                                 case Token.OPEN_BRACE:
1287                                 case Token.CLOSE_PARENS:
1288                                 case Token.IN:
1289                                         next_token = Token.INTERR_NULLABLE;
1290                                         break;
1291                                         
1292                                 case Token.COLON:
1293                                         next_token = Token.INTERR;
1294                                         break;                                                  
1295                                         
1296                                 default:
1297                                         int ntoken;
1298                                         int interrs = 1;
1299                                         int colons = 0;
1300                                         int braces = 0;
1301                                         //
1302                                         // All shorcuts failed, do it hard way
1303                                         //
1304                                         while ((ntoken = xtoken ()) != Token.EOF) {
1305                                                 if (ntoken == Token.OPEN_BRACE) {
1306                                                         ++braces;
1307                                                         continue;
1308                                                 }
1309
1310                                                 if (ntoken == Token.CLOSE_BRACE) {
1311                                                         --braces;
1312                                                         continue;
1313                                                 }
1314
1315                                                 if (braces != 0)
1316                                                         continue;
1317
1318                                                 if (ntoken == Token.SEMICOLON)
1319                                                         break;
1320                                                 
1321                                                 if (ntoken == Token.COLON) {
1322                                                         if (++colons == interrs)
1323                                                                 break;
1324                                                         continue;
1325                                                 }
1326                                                 
1327                                                 if (ntoken == Token.INTERR) {
1328                                                         ++interrs;
1329                                                         continue;
1330                                                 }
1331                                         }
1332                                         
1333                                         next_token = colons != interrs && braces == 0 ? Token.INTERR_NULLABLE : Token.INTERR;
1334                                         break;
1335                                 }
1336                         }
1337                         
1338                         PopPosition ();
1339                         return next_token;
1340                 }
1341
1342                 bool decimal_digits (int c)
1343                 {
1344                         int d;
1345                         bool seen_digits = false;
1346                         
1347                         if (c != -1){
1348                                 if (number_pos == MaxNumberLength)
1349                                         Error_NumericConstantTooLong ();
1350                                 number_builder [number_pos++] = (char) c;
1351                         }
1352                         
1353                         //
1354                         // We use peek_char2, because decimal_digits needs to do a 
1355                         // 2-character look-ahead (5.ToString for example).
1356                         //
1357                         while ((d = peek_char2 ()) != -1){
1358                                 if (d >= '0' && d <= '9'){
1359                                         if (number_pos == MaxNumberLength)
1360                                                 Error_NumericConstantTooLong ();
1361                                         number_builder [number_pos++] = (char) d;
1362                                         get_char ();
1363                                         seen_digits = true;
1364                                 } else
1365                                         break;
1366                         }
1367                         
1368                         return seen_digits;
1369                 }
1370
1371                 static bool is_hex (int e)
1372                 {
1373                         return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
1374                 }
1375
1376                 static TypeCode real_type_suffix (int c)
1377                 {
1378                         switch (c){
1379                         case 'F': case 'f':
1380                                 return TypeCode.Single;
1381                         case 'D': case 'd':
1382                                 return TypeCode.Double;
1383                         case 'M': case 'm':
1384                                 return TypeCode.Decimal;
1385                         default:
1386                                 return TypeCode.Empty;
1387                         }
1388                 }
1389
1390                 ILiteralConstant integer_type_suffix (ulong ul, int c, Location loc)
1391                 {
1392                         bool is_unsigned = false;
1393                         bool is_long = false;
1394
1395                         if (c != -1){
1396                                 bool scanning = true;
1397                                 do {
1398                                         switch (c){
1399                                         case 'U': case 'u':
1400                                                 if (is_unsigned)
1401                                                         scanning = false;
1402                                                 is_unsigned = true;
1403                                                 get_char ();
1404                                                 break;
1405
1406                                         case 'l':
1407                                                 if (!is_unsigned){
1408                                                         //
1409                                                         // if we have not seen anything in between
1410                                                         // report this error
1411                                                         //
1412                                                         Report.Warning (78, 4, Location, "The `l' suffix is easily confused with the digit `1' (use `L' for clarity)");
1413                                                 }
1414
1415                                                 goto case 'L';
1416
1417                                         case 'L': 
1418                                                 if (is_long)
1419                                                         scanning = false;
1420                                                 is_long = true;
1421                                                 get_char ();
1422                                                 break;
1423                                                 
1424                                         default:
1425                                                 scanning = false;
1426                                                 break;
1427                                         }
1428                                         c = peek_char ();
1429                                 } while (scanning);
1430                         }
1431
1432                         if (is_long && is_unsigned){
1433                                 return new ULongLiteral (context.BuiltinTypes, ul, loc);
1434                         }
1435                         
1436                         if (is_unsigned){
1437                                 // uint if possible, or ulong else.
1438
1439                                 if ((ul & 0xffffffff00000000) == 0)
1440                                         return new UIntLiteral (context.BuiltinTypes, (uint) ul, loc);
1441                                 else
1442                                         return new ULongLiteral (context.BuiltinTypes, ul, loc);
1443                         } else if (is_long){
1444                                 // long if possible, ulong otherwise
1445                                 if ((ul & 0x8000000000000000) != 0)
1446                                         return new ULongLiteral (context.BuiltinTypes, ul, loc);
1447                                 else
1448                                         return new LongLiteral (context.BuiltinTypes, (long) ul, loc);
1449                         } else {
1450                                 // int, uint, long or ulong in that order
1451                                 if ((ul & 0xffffffff00000000) == 0){
1452                                         uint ui = (uint) ul;
1453                                         
1454                                         if ((ui & 0x80000000) != 0)
1455                                                 return new UIntLiteral (context.BuiltinTypes, ui, loc);
1456                                         else
1457                                                 return new IntLiteral (context.BuiltinTypes, (int) ui, loc);
1458                                 } else {
1459                                         if ((ul & 0x8000000000000000) != 0)
1460                                                 return new ULongLiteral (context.BuiltinTypes, ul, loc);
1461                                         else
1462                                                 return new LongLiteral (context.BuiltinTypes, (long) ul, loc);
1463                                 }
1464                         }
1465                 }
1466                                 
1467                 //
1468                 // given `c' as the next char in the input decide whether
1469                 // we need to convert to a special type, and then choose
1470                 // the best representation for the integer
1471                 //
1472                 ILiteralConstant adjust_int (int c, Location loc)
1473                 {
1474                         try {
1475                                 if (number_pos > 9){
1476                                         ulong ul = (uint) (number_builder [0] - '0');
1477
1478                                         for (int i = 1; i < number_pos; i++){
1479                                                 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
1480                                         }
1481
1482                                         return integer_type_suffix (ul, c, loc);
1483                                 } else {
1484                                         uint ui = (uint) (number_builder [0] - '0');
1485
1486                                         for (int i = 1; i < number_pos; i++){
1487                                                 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
1488                                         }
1489
1490                                         return integer_type_suffix (ui, c, loc);
1491                                 }
1492                         } catch (OverflowException) {
1493                                 Error_NumericConstantTooLong ();
1494                                 return new IntLiteral (context.BuiltinTypes, 0, loc);
1495                         }
1496                         catch (FormatException) {
1497                                 Report.Error (1013, Location, "Invalid number");
1498                                 return new IntLiteral (context.BuiltinTypes, 0, loc);
1499                         }
1500                 }
1501                 
1502                 ILiteralConstant adjust_real (TypeCode t, Location loc)
1503                 {
1504                         string s = new string (number_builder, 0, number_pos);
1505                         const string error_details = "Floating-point constant is outside the range of type `{0}'";
1506
1507                         switch (t){
1508                         case TypeCode.Decimal:
1509                                 try {
1510                                         return new DecimalLiteral (context.BuiltinTypes, decimal.Parse (s, styles, csharp_format_info), loc);
1511                                 } catch (OverflowException) {
1512                                         Report.Error (594, Location, error_details, "decimal");
1513                                         return new DecimalLiteral (context.BuiltinTypes, 0, loc);
1514                                 }
1515                         case TypeCode.Single:
1516                                 try {
1517                                         return new FloatLiteral (context.BuiltinTypes, float.Parse (s, styles, csharp_format_info), loc);
1518                                 } catch (OverflowException) {
1519                                         Report.Error (594, Location, error_details, "float");
1520                                         return new FloatLiteral (context.BuiltinTypes, 0, loc);
1521                                 }
1522                         default:
1523                                 try {
1524                                         return new DoubleLiteral (context.BuiltinTypes, double.Parse (s, styles, csharp_format_info), loc);
1525                                 } catch (OverflowException) {
1526                                         Report.Error (594, loc, error_details, "double");
1527                                         return new DoubleLiteral (context.BuiltinTypes, 0, loc);
1528                                 }
1529                         }
1530                 }
1531
1532                 ILiteralConstant handle_hex (Location loc)
1533                 {
1534                         int d;
1535                         ulong ul;
1536                         
1537                         get_char ();
1538                         while ((d = peek_char ()) != -1){
1539                                 if (is_hex (d)){
1540                                         number_builder [number_pos++] = (char) d;
1541                                         get_char ();
1542                                 } else
1543                                         break;
1544                         }
1545                         
1546                         string s = new String (number_builder, 0, number_pos);
1547
1548                         try {
1549                                 if (number_pos <= 8)
1550                                         ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
1551                                 else
1552                                         ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
1553
1554                                 return integer_type_suffix (ul, peek_char (), loc);
1555                         } catch (OverflowException){
1556                                 Error_NumericConstantTooLong ();
1557                                 return new IntLiteral (context.BuiltinTypes, 0, loc);
1558                         }
1559                         catch (FormatException) {
1560                                 Report.Error (1013, Location, "Invalid number");
1561                                 return new IntLiteral (context.BuiltinTypes, 0, loc);
1562                         }
1563                 }
1564
1565                 //
1566                 // Invoked if we know we have .digits or digits
1567                 //
1568                 int is_number (int c, bool dotLead)
1569                 {
1570                         ILiteralConstant res;
1571
1572 #if FULL_AST
1573                         int read_start = reader.Position - 1;
1574                         if (dotLead) {
1575                                 //
1576                                 // Caller did peek_char
1577                                 //
1578                                 --read_start;
1579                         }
1580 #endif
1581                         number_pos = 0;
1582                         var loc = Location;
1583
1584                         if (!dotLead){
1585                                 if (c == '0'){
1586                                         int peek = peek_char ();
1587
1588                                         if (peek == 'x' || peek == 'X') {
1589                                                 val = res = handle_hex (loc);
1590 #if FULL_AST
1591                                                 res.ParsedValue = reader.ReadChars (read_start, reader.Position - 1);
1592 #endif
1593
1594                                                 return Token.LITERAL;
1595                                         }
1596                                 }
1597                                 decimal_digits (c);
1598                                 c = peek_char ();
1599                         }
1600
1601                         //
1602                         // We need to handle the case of
1603                         // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
1604                         //
1605                         bool is_real = false;
1606                         if (c == '.'){
1607                                 if (!dotLead)
1608                                         get_char ();
1609
1610                                 if (decimal_digits ('.')){
1611                                         is_real = true;
1612                                         c = peek_char ();
1613                                 } else {
1614                                         putback ('.');
1615                                         number_pos--;
1616                                         val = res = adjust_int (-1, loc);
1617
1618 #if FULL_AST
1619                                         res.ParsedValue = reader.ReadChars (read_start, reader.Position - 1);
1620 #endif
1621                                         return Token.LITERAL;
1622                                 }
1623                         }
1624                         
1625                         if (c == 'e' || c == 'E'){
1626                                 is_real = true;
1627                                 get_char ();
1628                                 if (number_pos == MaxNumberLength)
1629                                         Error_NumericConstantTooLong ();
1630                                 number_builder [number_pos++] = (char) c;
1631                                 c = get_char ();
1632                                 
1633                                 if (c == '+'){
1634                                         if (number_pos == MaxNumberLength)
1635                                                 Error_NumericConstantTooLong ();
1636                                         number_builder [number_pos++] = '+';
1637                                         c = -1;
1638                                 } else if (c == '-') {
1639                                         if (number_pos == MaxNumberLength)
1640                                                 Error_NumericConstantTooLong ();
1641                                         number_builder [number_pos++] = '-';
1642                                         c = -1;
1643                                 } else {
1644                                         if (number_pos == MaxNumberLength)
1645                                                 Error_NumericConstantTooLong ();
1646                                         number_builder [number_pos++] = '+';
1647                                 }
1648                                         
1649                                 decimal_digits (c);
1650                                 c = peek_char ();
1651                         }
1652
1653                         var type = real_type_suffix (c);
1654                         if (type == TypeCode.Empty && !is_real) {
1655                                 res = adjust_int (c, loc);
1656                         } else {
1657                                 is_real = true;
1658
1659                                 if (type != TypeCode.Empty) {
1660                                         get_char ();
1661                                 }
1662
1663                                 res = adjust_real (type, loc);
1664                         }
1665
1666                         val = res;
1667
1668 #if FULL_AST
1669                         var chars = reader.ReadChars (read_start, reader.Position - (type == TypeCode.Empty && c > 0 ? 1 : 0));
1670                         if (chars[chars.Length - 1] == '\r')
1671                                 Array.Resize (ref chars, chars.Length - 1);
1672                         res.ParsedValue = chars;
1673 #endif
1674
1675                         return Token.LITERAL;
1676                 }
1677
1678                 //
1679                 // Accepts exactly count (4 or 8) hex, no more no less
1680                 //
1681                 int getHex (int count, out int surrogate, out bool error)
1682                 {
1683                         int i;
1684                         int total = 0;
1685                         int c;
1686                         int top = count != -1 ? count : 4;
1687                         
1688                         get_char ();
1689                         error = false;
1690                         surrogate = 0;
1691                         for (i = 0; i < top; i++){
1692                                 c = get_char ();
1693
1694                                 if (c >= '0' && c <= '9')
1695                                         c = (int) c - (int) '0';
1696                                 else if (c >= 'A' && c <= 'F')
1697                                         c = (int) c - (int) 'A' + 10;
1698                                 else if (c >= 'a' && c <= 'f')
1699                                         c = (int) c - (int) 'a' + 10;
1700                                 else {
1701                                         error = true;
1702                                         return 0;
1703                                 }
1704                                 
1705                                 total = (total * 16) + c;
1706                                 if (count == -1){
1707                                         int p = peek_char ();
1708                                         if (p == -1)
1709                                                 break;
1710                                         if (!is_hex ((char)p))
1711                                                 break;
1712                                 }
1713                         }
1714
1715                         if (top == 8) {
1716                                 if (total > 0x0010FFFF) {
1717                                         error = true;
1718                                         return 0;
1719                                 }
1720
1721                                 if (total >= 0x00010000) {
1722                                         surrogate = ((total - 0x00010000) % 0x0400 + 0xDC00);                                   
1723                                         total = ((total - 0x00010000) / 0x0400 + 0xD800);
1724                                 }
1725                         }
1726
1727                         return total;
1728                 }
1729
1730                 int escape (int c, out int surrogate)
1731                 {
1732                         bool error;
1733                         int d;
1734                         int v;
1735
1736                         d = peek_char ();
1737                         if (c != '\\') {
1738                                 surrogate = 0;
1739                                 return c;
1740                         }
1741                         
1742                         switch (d){
1743                         case 'a':
1744                                 v = '\a'; break;
1745                         case 'b':
1746                                 v = '\b'; break;
1747                         case 'n':
1748                                 v = '\n'; break;
1749                         case 't':
1750                                 v = '\t'; break;
1751                         case 'v':
1752                                 v = '\v'; break;
1753                         case 'r':
1754                                 v = '\r'; break;
1755                         case '\\':
1756                                 v = '\\'; break;
1757                         case 'f':
1758                                 v = '\f'; break;
1759                         case '0':
1760                                 v = 0; break;
1761                         case '"':
1762                                 v = '"'; break;
1763                         case '\'':
1764                                 v = '\''; break;
1765                         case 'x':
1766                                 v = getHex (-1, out surrogate, out error);
1767                                 if (error)
1768                                         goto default;
1769                                 return v;
1770                         case 'u':
1771                         case 'U':
1772                                 return EscapeUnicode (d, out surrogate);
1773                         default:
1774                                 surrogate = 0;
1775                                 Report.Error (1009, Location, "Unrecognized escape sequence `\\{0}'", ((char)d).ToString ());
1776                                 return d;
1777                         }
1778
1779                         get_char ();
1780                         surrogate = 0;
1781                         return v;
1782                 }
1783
1784                 int EscapeUnicode (int ch, out int surrogate)
1785                 {
1786                         bool error;
1787                         if (ch == 'U') {
1788                                 ch = getHex (8, out surrogate, out error);
1789                         } else {
1790                                 ch = getHex (4, out surrogate, out error);
1791                         }
1792
1793                         if (error)
1794                                 Report.Error (1009, Location, "Unrecognized escape sequence");
1795
1796                         return ch;
1797                 }
1798
1799                 int get_char ()
1800                 {
1801                         int x;
1802                         if (putback_char != -1) {
1803                                 x = putback_char;
1804                                 putback_char = -1;
1805                         } else {
1806                                 x = reader.Read ();
1807                         }
1808                         
1809                         if (x == '\r') {
1810                                 if (peek_char () == '\n') {
1811                                         putback_char = -1;
1812                                 }
1813
1814                                 x = '\n';
1815                                 advance_line ();
1816                         } else if (x == '\n') {
1817                                 advance_line ();
1818                         } else {
1819                                 col++;
1820                         }
1821                         return x;
1822                 }
1823
1824                 void advance_line ()
1825                 {
1826                         line++;
1827                         ref_line++;
1828                         previous_col = col;
1829                         col = 0;
1830                 }
1831
1832                 int peek_char ()
1833                 {
1834                         if (putback_char == -1)
1835                                 putback_char = reader.Read ();
1836                         return putback_char;
1837                 }
1838
1839                 int peek_char2 ()
1840                 {
1841                         if (putback_char != -1)
1842                                 return putback_char;
1843                         return reader.Peek ();
1844                 }
1845                 
1846                 public void putback (int c)
1847                 {
1848                         if (putback_char != -1) {
1849                                 throw new InternalErrorException (string.Format ("Secondary putback [{0}] putting back [{1}] is not allowed", (char)putback_char, (char) c), Location);
1850                         }
1851
1852                         if (c == '\n' || col == 0) {
1853                                 // It won't happen though.
1854                                 line--;
1855                                 ref_line--;
1856                                 col = previous_col;
1857                         }
1858                         else
1859                                 col--;
1860                         putback_char = c;
1861                 }
1862
1863                 public bool advance ()
1864                 {
1865                         return peek_char () != -1 || CompleteOnEOF;
1866                 }
1867
1868                 public Object Value {
1869                         get {
1870                                 return val;
1871                         }
1872                 }
1873
1874                 public Object value ()
1875                 {
1876                         return val;
1877                 }
1878
1879                 public int token ()
1880                 {
1881                         current_token = xtoken ();
1882                         return current_token;
1883                 }
1884
1885                 int TokenizePreprocessorIdentifier (out int c)
1886                 {
1887                         // skip over white space
1888                         do {
1889                                 c = get_char ();
1890                         } while (c == ' ' || c == '\t');
1891
1892
1893                         int pos = 0;
1894                         while (c != -1 && c >= 'a' && c <= 'z') {
1895                                 id_builder[pos++] = (char) c;
1896                                 c = get_char ();
1897                                 if (c == '\\') {
1898                                         int peek = peek_char ();
1899                                         if (peek == 'U' || peek == 'u') {
1900                                                 int surrogate;
1901                                                 c = EscapeUnicode (c, out surrogate);
1902                                                 if (surrogate != 0) {
1903                                                         if (is_identifier_part_character ((char) c)) {
1904                                                                 id_builder[pos++] = (char) c;
1905                                                         }
1906                                                         c = surrogate;
1907                                                 }
1908                                         }
1909                                 }
1910                         }
1911
1912                         return pos;
1913                 }
1914
1915                 PreprocessorDirective get_cmd_arg (out string arg)
1916                 {
1917                         int c;          
1918
1919                         tokens_seen = false;
1920                         arg = "";
1921
1922                         var cmd = GetPreprocessorDirective (id_builder, TokenizePreprocessorIdentifier (out c));
1923
1924                         if ((cmd & PreprocessorDirective.CustomArgumentsParsing) != 0)
1925                                 return cmd;
1926
1927                         // skip over white space
1928                         while (c == ' ' || c == '\t')
1929                                 c = get_char ();
1930
1931                         int has_identifier_argument = (int)(cmd & PreprocessorDirective.RequiresArgument);
1932                         int pos = 0;
1933
1934                         while (c != -1 && c != '\n') {
1935                                 if (c == '\\' && has_identifier_argument >= 0) {
1936                                         if (has_identifier_argument != 0) {
1937                                                 has_identifier_argument = 1;
1938
1939                                                 int peek = peek_char ();
1940                                                 if (peek == 'U' || peek == 'u') {
1941                                                         int surrogate;
1942                                                         c = EscapeUnicode (c, out surrogate);
1943                                                         if (surrogate != 0) {
1944                                                                 if (is_identifier_part_character ((char) c)) {
1945                                                                         if (pos == value_builder.Length)
1946                                                                                 Array.Resize (ref value_builder, pos * 2);
1947
1948                                                                         value_builder[pos++] = (char) c;
1949                                                                 }
1950                                                                 c = surrogate;
1951                                                         }
1952                                                 }
1953                                         } else {
1954                                                 has_identifier_argument = -1;
1955                                         }
1956                                 } else if (c == '/' && peek_char () == '/') {
1957                                         //
1958                                         // Eat single-line comments
1959                                         //
1960                                         get_char ();
1961                                         do {
1962                                                 c = get_char ();
1963                                         } while (c != -1 && c != '\n');
1964
1965                                         break;
1966                                 }
1967
1968                                 if (pos == value_builder.Length)
1969                                         Array.Resize (ref value_builder, pos * 2);
1970
1971                                 value_builder[pos++] = (char) c;
1972                                 c = get_char ();
1973                         }
1974
1975                         if (pos != 0) {
1976                                 if (pos > MaxIdentifierLength)
1977                                         arg = new string (value_builder, 0, pos);
1978                                 else
1979                                         arg = InternIdentifier (value_builder, pos);
1980
1981                                 // Eat any trailing whitespaces
1982                                 arg = arg.Trim (simple_whitespaces);
1983                         }
1984
1985                         return cmd;
1986                 }
1987
1988                 //
1989                 // Handles the #line directive
1990                 //
1991                 bool PreProcessLine ()
1992                 {
1993                         Location loc = Location;
1994
1995                         int c;
1996
1997                         int length = TokenizePreprocessorIdentifier (out c);
1998                         if (length == line_default.Length) {
1999                                 if (!IsTokenIdentifierEqual (line_default))
2000                                         return false;
2001
2002                                 current_source = source_file.SourceFile;
2003                                 if (!hidden_block_start.IsNull) {
2004                                         current_source.RegisterHiddenScope (hidden_block_start, loc);
2005                                         hidden_block_start = Location.Null;
2006                                 }
2007
2008                                 ref_line = line;
2009                                 return true;
2010                         }
2011
2012                         if (length == line_hidden.Length) {
2013                                 if (!IsTokenIdentifierEqual (line_hidden))
2014                                         return false;
2015
2016                                 if (hidden_block_start.IsNull)
2017                                         hidden_block_start = loc;
2018
2019                                 return true;
2020                         }
2021
2022                         if (length != 0 || c < '0' || c > '9') {
2023                                 //
2024                                 // Eat any remaining characters to continue parsing on next line
2025                                 //
2026                                 while (c != -1 && c != '\n') {
2027                                         c = get_char ();
2028                                 }
2029
2030                                 return false;
2031                         }
2032
2033                         int new_line = TokenizeNumber (c);
2034                         if (new_line < 1) {
2035                                 //
2036                                 // Eat any remaining characters to continue parsing on next line
2037                                 //
2038                                 while (c != -1 && c != '\n') {
2039                                         c = get_char ();
2040                                 }
2041
2042                                 return new_line != 0;
2043                         }
2044
2045                         c = get_char ();
2046                         if (c == ' ') {
2047                                 // skip over white space
2048                                 do {
2049                                         c = get_char ();
2050                                 } while (c == ' ' || c == '\t');
2051                         } else if (c == '"') {
2052                                 c = 0;
2053                         }
2054
2055                         if (c != '\n' && c != '/' && c != '"') {
2056                                 //
2057                                 // Eat any remaining characters to continue parsing on next line
2058                                 //
2059                                 while (c != -1 && c != '\n') {
2060                                         c = get_char ();
2061                                 }
2062
2063                                 Report.Error (1578, loc, "Filename, single-line comment or end-of-line expected");
2064                                 return true;
2065                         }
2066
2067                         string new_file_name = null;
2068                         if (c == '"') {
2069                                 new_file_name = TokenizeFileName (ref c);
2070
2071                                 // skip over white space
2072                                 while (c == ' ' || c == '\t') {
2073                                         c = get_char ();
2074                                 }
2075                         }
2076
2077                         if (c == '\n') {
2078                         } else if (c == '/') {
2079                                 ReadSingleLineComment ();
2080                         } else {
2081                                 //
2082                                 // Eat any remaining characters to continue parsing on next line
2083                                 //
2084                                 while (c != -1 && c != '\n') {
2085                                         c = get_char ();
2086                                 }
2087
2088                                 Error_EndLineExpected ();
2089                                 return true;
2090                         }
2091
2092                         if (new_file_name != null) {
2093                                 current_source = context.LookupFile (source_file, new_file_name);
2094                                 source_file.AddIncludeFile (current_source);
2095                         }
2096
2097                         if (!hidden_block_start.IsNull) {
2098                                 current_source.RegisterHiddenScope (hidden_block_start, loc);
2099                                 hidden_block_start = Location.Null;
2100                         }
2101
2102                         ref_line = new_line;
2103                         return true;
2104                 }
2105
2106                 //
2107                 // Handles #define and #undef
2108                 //
2109                 void PreProcessDefinition (bool is_define, string ident, bool caller_is_taking)
2110                 {
2111                         if (ident.Length == 0 || ident == "true" || ident == "false"){
2112                                 Report.Error (1001, Location, "Missing identifier to pre-processor directive");
2113                                 return;
2114                         }
2115
2116                         if (ident.IndexOfAny (simple_whitespaces) != -1){
2117                                 Error_EndLineExpected ();
2118                                 return;
2119                         }
2120
2121                         if (!is_identifier_start_character (ident [0]))
2122                                 Report.Error (1001, Location, "Identifier expected: {0}", ident);
2123                         
2124                         foreach (char c in ident.Substring (1)){
2125                                 if (!is_identifier_part_character (c)){
2126                                         Report.Error (1001, Location, "Identifier expected: {0}",  ident);
2127                                         return;
2128                                 }
2129                         }
2130
2131                         if (!caller_is_taking)
2132                                 return;
2133
2134                         if (is_define) {
2135                                 //
2136                                 // #define ident
2137                                 //
2138                                 if (context.Settings.IsConditionalSymbolDefined (ident))
2139                                         return;
2140
2141                                 source_file.AddDefine (ident);
2142                         } else {
2143                                 //
2144                                 // #undef ident
2145                                 //
2146                                 source_file.AddUndefine (ident);
2147                         }
2148                 }
2149
2150                 byte read_hex (out bool error)
2151                 {
2152                         int total;
2153                         int c = get_char ();
2154
2155                         if ((c >= '0') && (c <= '9'))
2156                                 total = (int) c - (int) '0';
2157                         else if ((c >= 'A') && (c <= 'F'))
2158                                 total = (int) c - (int) 'A' + 10;
2159                         else if ((c >= 'a') && (c <= 'f'))
2160                                 total = (int) c - (int) 'a' + 10;
2161                         else {
2162                                 error = true;
2163                                 return 0;
2164                         }
2165
2166                         total *= 16;
2167                         c = get_char ();
2168
2169                         if ((c >= '0') && (c <= '9'))
2170                                 total += (int) c - (int) '0';
2171                         else if ((c >= 'A') && (c <= 'F'))
2172                                 total += (int) c - (int) 'A' + 10;
2173                         else if ((c >= 'a') && (c <= 'f'))
2174                                 total += (int) c - (int) 'a' + 10;
2175                         else {
2176                                 error = true;
2177                                 return 0;
2178                         }
2179
2180                         error = false;
2181                         return (byte) total;
2182                 }
2183
2184                 //
2185                 // Parses #pragma checksum
2186                 //
2187                 bool ParsePragmaChecksum ()
2188                 {
2189                         //
2190                         // The syntax is ` "foo.txt" "{guid}" "hash"'
2191                         //
2192                         // guid is predefined hash algorithm guid {406ea660-64cf-4c82-b6f0-42d48172a799} for md5
2193                         //
2194                         int c = get_char ();
2195
2196                         if (c != '"')
2197                                 return false;
2198
2199                         string file_name = TokenizeFileName (ref c);
2200
2201                         // TODO: Any white-spaces count
2202                         if (c != ' ')
2203                                 return false;
2204
2205                         SourceFile file = context.LookupFile (source_file, file_name);
2206
2207                         if (get_char () != '"' || get_char () != '{')
2208                                 return false;
2209
2210                         bool error;
2211                         byte[] guid_bytes = new byte [16];
2212                         int i = 0;
2213
2214                         for (; i < 4; i++) {
2215                                 guid_bytes [i] = read_hex (out error);
2216                                 if (error)
2217                                         return false;
2218                         }
2219
2220                         if (get_char () != '-')
2221                                 return false;
2222
2223                         for (; i < 10; i++) {
2224                                 guid_bytes [i] = read_hex (out error);
2225                                 if (error)
2226                                         return false;
2227
2228                                 guid_bytes [i++] = read_hex (out error);
2229                                 if (error)
2230                                         return false;
2231
2232                                 if (get_char () != '-')
2233                                         return false;
2234                         }
2235
2236                         for (; i < 16; i++) {
2237                                 guid_bytes [i] = read_hex (out error);
2238                                 if (error)
2239                                         return false;
2240                         }
2241
2242                         if (get_char () != '}' || get_char () != '"')
2243                                 return false;
2244
2245                         // TODO: Any white-spaces count
2246                         c = get_char ();
2247                         if (c != ' ')
2248                                 return false;
2249
2250                         if (get_char () != '"')
2251                                 return false;
2252
2253                         // Any length of checksum
2254                         List<byte> checksum_bytes = new List<byte> (16);
2255
2256                         var checksum_location = Location;
2257                         c = peek_char ();
2258                         while (c != '"' && c != -1) {
2259                                 checksum_bytes.Add (read_hex (out error));
2260                                 if (error)
2261                                         return false;
2262
2263                                 c = peek_char ();
2264                         }
2265
2266                         if (c == '/') {
2267                                 ReadSingleLineComment ();
2268                         } else if (get_char () != '"') {
2269                                 return false;
2270                         }
2271
2272                         if (context.Settings.GenerateDebugInfo) {
2273                                 var chsum = checksum_bytes.ToArray ();
2274
2275                                 if (file.HasChecksum) {
2276                                         if (!ArrayComparer.IsEqual (file.Checksum, chsum)) {
2277                                                 // TODO: Report.SymbolRelatedToPreviousError
2278                                                 Report.Warning (1697, 1, checksum_location, "Different checksum values specified for file `{0}'", file.Name);
2279                                         }
2280                                 }
2281
2282                                 file.SetChecksum (guid_bytes, chsum);
2283                                 current_source.AutoGenerated = true;
2284                         }
2285
2286                         return true;
2287                 }
2288
2289                 bool IsTokenIdentifierEqual (char[] identifier)
2290                 {
2291                         for (int i = 0; i < identifier.Length; ++i) {
2292                                 if (identifier[i] != id_builder[i])
2293                                         return false;
2294                         }
2295
2296                         return true;
2297                 }
2298
2299                 int TokenizeNumber (int value)
2300                 {
2301                         number_pos = 0;
2302
2303                         decimal_digits (value);
2304                         uint ui = (uint) (number_builder[0] - '0');
2305
2306                         try {
2307                                 for (int i = 1; i < number_pos; i++) {
2308                                         ui = checked ((ui * 10) + ((uint) (number_builder[i] - '0')));
2309                                 }
2310
2311                                 return (int) ui;
2312                         } catch (OverflowException) {
2313                                 Error_NumericConstantTooLong ();
2314                                 return -1;
2315                         }
2316                 }
2317
2318                 string TokenizeFileName (ref int c)
2319                 {
2320                         var string_builder = new StringBuilder ();
2321                         while (c != -1 && c != '\n') {
2322                                 c = get_char ();
2323                                 if (c == '"') {
2324                                         c = get_char ();
2325                                         break;
2326                                 }
2327
2328                                 string_builder.Append ((char) c);
2329                         }
2330
2331                         if (string_builder.Length == 0) {
2332                                 Report.Warning (1709, 1, Location, "Filename specified for preprocessor directive is empty");
2333                         }
2334
2335                 
2336                         return string_builder.ToString ();
2337                 }
2338
2339                 int TokenizePragmaNumber (ref int c)
2340                 {
2341                         number_pos = 0;
2342
2343                         int number;
2344
2345                         if (c >= '0' && c <= '9') {
2346                                 number = TokenizeNumber (c);
2347
2348                                 c = get_char ();
2349
2350                                 // skip over white space
2351                                 while (c == ' ' || c == '\t')
2352                                         c = get_char ();
2353
2354                                 if (c == ',') {
2355                                         c = get_char ();
2356                                 }
2357
2358                                 // skip over white space
2359                                 while (c == ' ' || c == '\t')
2360                                         c = get_char ();
2361                         } else {
2362                                 number = -1;
2363                                 if (c == '/') {
2364                                         ReadSingleLineComment ();
2365                                 } else {
2366                                         Report.Warning (1692, 1, Location, "Invalid number");
2367
2368                                         // Read everything till the end of the line or file
2369                                         do {
2370                                                 c = get_char ();
2371                                         } while (c != -1 && c != '\n');
2372                                 }
2373                         }
2374
2375                         return number;
2376                 }
2377
2378                 void ReadSingleLineComment ()
2379                 {
2380                         if (peek_char () != '/')
2381                                 Report.Warning (1696, 1, Location, "Single-line comment or end-of-line expected");
2382
2383                         // Read everything till the end of the line or file
2384                         int c;
2385                         do {
2386                                 c = get_char ();
2387                         } while (c != -1 && c != '\n');
2388                 }
2389
2390                 /// <summary>
2391                 /// Handles #pragma directive
2392                 /// </summary>
2393                 void ParsePragmaDirective (string arg)
2394                 {
2395                         int c;
2396                         int length = TokenizePreprocessorIdentifier (out c);
2397                         if (length == pragma_warning.Length && IsTokenIdentifierEqual (pragma_warning)) {
2398                                 length = TokenizePreprocessorIdentifier (out c);
2399
2400                                 //
2401                                 // #pragma warning disable
2402                                 // #pragma warning restore
2403                                 //
2404                                 if (length == pragma_warning_disable.Length) {
2405                                         bool disable = IsTokenIdentifierEqual (pragma_warning_disable);
2406                                         if (disable || IsTokenIdentifierEqual (pragma_warning_restore)) {
2407                                                 // skip over white space
2408                                                 while (c == ' ' || c == '\t')
2409                                                         c = get_char ();
2410
2411                                                 var loc = Location;
2412
2413                                                 if (c == '\n' || c == '/') {
2414                                                         if (c == '/')
2415                                                                 ReadSingleLineComment ();
2416
2417                                                         //
2418                                                         // Disable/Restore all warnings
2419                                                         //
2420                                                         if (disable) {
2421                                                                 Report.RegisterWarningRegion (loc).WarningDisable (loc.Row);
2422                                                         } else {
2423                                                                 Report.RegisterWarningRegion (loc).WarningEnable (loc.Row);
2424                                                         }
2425                                                 } else {
2426                                                         //
2427                                                         // Disable/Restore a warning or group of warnings
2428                                                         //
2429                                                         int code;
2430                                                         do {
2431                                                                 code = TokenizePragmaNumber (ref c);
2432                                                                 if (code > 0) {
2433                                                                         if (disable) {
2434                                                                                 Report.RegisterWarningRegion (loc).WarningDisable (loc, code, context.Report);
2435                                                                         } else {
2436                                                                                 Report.RegisterWarningRegion (loc).WarningEnable (loc, code, context);
2437                                                                         }
2438                                                                 }
2439                                                         } while (code >= 0 && c != '\n' && c != -1);
2440                                                 }
2441
2442                                                 return;
2443                                         }
2444                                 }
2445
2446                                 Report.Warning (1634, 1, Location, "Expected disable or restore");
2447
2448                                 // Eat any remaining characters on the line
2449                                 while (c != '\n' && c != -1)
2450                                         c = get_char ();
2451
2452                                 return;
2453                         }
2454
2455                         //
2456                         // #pragma checksum
2457                         //
2458                         if (length == pragma_checksum.Length && IsTokenIdentifierEqual (pragma_checksum)) {
2459                                 if (c != ' ' || !ParsePragmaChecksum ()) {
2460                                         Report.Warning (1695, 1, Location,
2461                                                 "Invalid #pragma checksum syntax. Expected \"filename\" \"{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}\" \"XXXX...\"");
2462                                 }
2463
2464                                 return;
2465                         }
2466
2467                         Report.Warning (1633, 1, Location, "Unrecognized #pragma directive");
2468                 }
2469
2470                 bool eval_val (string s)
2471                 {
2472                         if (s == "true")
2473                                 return true;
2474                         if (s == "false")
2475                                 return false;
2476
2477                         return source_file.IsConditionalDefined (s);
2478                 }
2479
2480                 bool pp_primary (ref string s)
2481                 {
2482                         s = s.Trim ();
2483                         int len = s.Length;
2484
2485                         if (len > 0){
2486                                 char c = s [0];
2487                                 
2488                                 if (c == '('){
2489                                         s = s.Substring (1);
2490                                         bool val = pp_expr (ref s, false);
2491                                         if (s.Length > 0 && s [0] == ')'){
2492                                                 s = s.Substring (1);
2493                                                 return val;
2494                                         }
2495                                         Error_InvalidDirective ();
2496                                         return false;
2497                                 }
2498                                 
2499                                 if (is_identifier_start_character (c)){
2500                                         int j = 1;
2501
2502                                         while (j < len){
2503                                                 c = s [j];
2504                                                 
2505                                                 if (is_identifier_part_character (c)){
2506                                                         j++;
2507                                                         continue;
2508                                                 }
2509                                                 bool v = eval_val (s.Substring (0, j));
2510                                                 s = s.Substring (j);
2511                                                 return v;
2512                                         }
2513                                         bool vv = eval_val (s);
2514                                         s = "";
2515                                         return vv;
2516                                 }
2517                         }
2518                         Error_InvalidDirective ();
2519                         return false;
2520                 }
2521                 
2522                 bool pp_unary (ref string s)
2523                 {
2524                         s = s.Trim ();
2525                         int len = s.Length;
2526
2527                         if (len > 0){
2528                                 if (s [0] == '!'){
2529                                         if (len > 1 && s [1] == '='){
2530                                                 Error_InvalidDirective ();
2531                                                 return false;
2532                                         }
2533                                         s = s.Substring (1);
2534                                         return ! pp_primary (ref s);
2535                                 } else
2536                                         return pp_primary (ref s);
2537                         } else {
2538                                 Error_InvalidDirective ();
2539                                 return false;
2540                         }
2541                 }
2542                 
2543                 bool pp_eq (ref string s)
2544                 {
2545                         bool va = pp_unary (ref s);
2546
2547                         s = s.Trim ();
2548                         int len = s.Length;
2549                         if (len > 0){
2550                                 if (s [0] == '='){
2551                                         if (len > 2 && s [1] == '='){
2552                                                 s = s.Substring (2);
2553                                                 return va == pp_unary (ref s);
2554                                         } else {
2555                                                 Error_InvalidDirective ();
2556                                                 return false;
2557                                         }
2558                                 } else if (s [0] == '!' && len > 1 && s [1] == '='){
2559                                         s = s.Substring (2);
2560
2561                                         return va != pp_unary (ref s);
2562
2563                                 } 
2564                         }
2565
2566                         return va;
2567                                 
2568                 }
2569                 
2570                 bool pp_and (ref string s)
2571                 {
2572                         bool va = pp_eq (ref s);
2573
2574                         s = s.Trim ();
2575                         int len = s.Length;
2576                         if (len > 0){
2577                                 if (s [0] == '&'){
2578                                         if (len > 2 && s [1] == '&'){
2579                                                 s = s.Substring (2);
2580                                                 return (va & pp_and (ref s));
2581                                         } else {
2582                                                 Error_InvalidDirective ();
2583                                                 return false;
2584                                         }
2585                                 } 
2586                         }
2587                         return va;
2588                 }
2589                 
2590                 //
2591                 // Evaluates an expression for `#if' or `#elif'
2592                 //
2593                 bool pp_expr (ref string s, bool isTerm)
2594                 {
2595                         bool va = pp_and (ref s);
2596                         s = s.Trim ();
2597                         int len = s.Length;
2598                         if (len > 0){
2599                                 char c = s [0];
2600                                 
2601                                 if (c == '|'){
2602                                         if (len > 2 && s [1] == '|'){
2603                                                 s = s.Substring (2);
2604                                                 return va | pp_expr (ref s, isTerm);
2605                                         } else {
2606                                                 Error_InvalidDirective ();
2607                                                 return false;
2608                                         }
2609                                 }
2610                                 if (isTerm) {
2611                                         Error_EndLineExpected ();
2612                                         return false;
2613                                 }
2614                         }
2615                         
2616                         return va;
2617                 }
2618
2619                 bool eval (string s)
2620                 {
2621                         bool v = pp_expr (ref s, true);
2622                         s = s.Trim ();
2623                         if (s.Length != 0){
2624                                 return false;
2625                         }
2626
2627                         return v;
2628                 }
2629
2630                 void Error_NumericConstantTooLong ()
2631                 {
2632                         Report.Error (1021, Location, "Integral constant is too large");                        
2633                 }
2634                 
2635                 void Error_InvalidDirective ()
2636                 {
2637                         Report.Error (1517, Location, "Invalid preprocessor directive");
2638                 }
2639
2640                 void Error_UnexpectedDirective (string extra)
2641                 {
2642                         Report.Error (
2643                                 1028, Location,
2644                                 "Unexpected processor directive ({0})", extra);
2645                 }
2646
2647                 void Error_TokensSeen ()
2648                 {
2649                         Report.Error (1032, Location,
2650                                 "Cannot define or undefine preprocessor symbols after first token in file");
2651                 }
2652
2653                 void Eror_WrongPreprocessorLocation ()
2654                 {
2655                         Report.Error (1040, Location,
2656                                 "Preprocessor directives must appear as the first non-whitespace character on a line");
2657                 }
2658
2659                 void Error_EndLineExpected ()
2660                 {
2661                         Report.Error (1025, Location, "Single-line comment or end-of-line expected");
2662                 }
2663
2664                 //
2665                 // Raises a warning when tokenizer found documentation comment
2666                 // on unexpected place
2667                 //
2668                 void WarningMisplacedComment (Location loc)
2669                 {
2670                         if (doc_state != XmlCommentState.Error) {
2671                                 doc_state = XmlCommentState.Error;
2672                                 Report.Warning (1587, 2, loc, "XML comment is not placed on a valid language element");
2673                         }
2674                 }
2675                 
2676                 //
2677                 // if true, then the code continues processing the code
2678                 // if false, the code stays in a loop until another directive is
2679                 // reached.
2680                 // When caller_is_taking is false we ignore all directives except the ones
2681                 // which can help us to identify where the #if block ends
2682                 bool ParsePreprocessingDirective (bool caller_is_taking)
2683                 {
2684                         string arg;
2685                         bool region_directive = false;
2686
2687                         var directive = get_cmd_arg (out arg);
2688
2689                         //
2690                         // The first group of pre-processing instructions is always processed
2691                         //
2692                         switch (directive) {
2693                         case PreprocessorDirective.Region:
2694                                 region_directive = true;
2695                                 arg = "true";
2696                                 goto case PreprocessorDirective.If;
2697
2698                         case PreprocessorDirective.Endregion:
2699                                 if (ifstack == null || ifstack.Count == 0){
2700                                         Error_UnexpectedDirective ("no #region for this #endregion");
2701                                         return true;
2702                                 }
2703                                 int pop = ifstack.Pop ();
2704                                         
2705                                 if ((pop & REGION) == 0)
2706                                         Report.Error (1027, Location, "Expected `#endif' directive");
2707                                         
2708                                 return caller_is_taking;
2709                                 
2710                         case PreprocessorDirective.If:
2711                                 if (ifstack == null)
2712                                         ifstack = new Stack<int> (2);
2713
2714                                 int flags = region_directive ? REGION : 0;
2715                                 if (ifstack.Count == 0){
2716                                         flags |= PARENT_TAKING;
2717                                 } else {
2718                                         int state = ifstack.Peek ();
2719                                         if ((state & TAKING) != 0) {
2720                                                 flags |= PARENT_TAKING;
2721                                         }
2722                                 }
2723
2724                                 if (eval (arg) && caller_is_taking) {
2725                                         ifstack.Push (flags | TAKING);
2726                                         return true;
2727                                 }
2728                                 ifstack.Push (flags);
2729                                 return false;
2730
2731                         case PreprocessorDirective.Endif:
2732                                 if (ifstack == null || ifstack.Count == 0){
2733                                         Error_UnexpectedDirective ("no #if for this #endif");
2734                                         return true;
2735                                 } else {
2736                                         pop = ifstack.Pop ();
2737                                         
2738                                         if ((pop & REGION) != 0)
2739                                                 Report.Error (1038, Location, "#endregion directive expected");
2740                                         
2741                                         if (arg.Length != 0) {
2742                                                 Error_EndLineExpected ();
2743                                         }
2744                                         
2745                                         if (ifstack.Count == 0)
2746                                                 return true;
2747
2748                                         int state = ifstack.Peek ();
2749                                         return (state & TAKING) != 0;
2750                                 }
2751
2752                         case PreprocessorDirective.Elif:
2753                                 if (ifstack == null || ifstack.Count == 0){
2754                                         Error_UnexpectedDirective ("no #if for this #elif");
2755                                         return true;
2756                                 } else {
2757                                         int state = ifstack.Pop ();
2758
2759                                         if ((state & REGION) != 0) {
2760                                                 Report.Error (1038, Location, "#endregion directive expected");
2761                                                 return true;
2762                                         }
2763
2764                                         if ((state & ELSE_SEEN) != 0){
2765                                                 Error_UnexpectedDirective ("#elif not valid after #else");
2766                                                 return true;
2767                                         }
2768
2769                                         if ((state & TAKING) != 0) {
2770                                                 ifstack.Push (0);
2771                                                 return false;
2772                                         }
2773
2774                                         if (eval (arg) && ((state & PARENT_TAKING) != 0)){
2775                                                 ifstack.Push (state | TAKING);
2776                                                 return true;
2777                                         }
2778
2779                                         ifstack.Push (state);
2780                                         return false;
2781                                 }
2782
2783                         case PreprocessorDirective.Else:
2784                                 if (ifstack == null || ifstack.Count == 0){
2785                                         Error_UnexpectedDirective ("no #if for this #else");
2786                                         return true;
2787                                 } else {
2788                                         int state = ifstack.Peek ();
2789
2790                                         if ((state & REGION) != 0) {
2791                                                 Report.Error (1038, Location, "#endregion directive expected");
2792                                                 return true;
2793                                         }
2794
2795                                         if ((state & ELSE_SEEN) != 0){
2796                                                 Error_UnexpectedDirective ("#else within #else");
2797                                                 return true;
2798                                         }
2799
2800                                         ifstack.Pop ();
2801
2802                                         if (arg.Length != 0) {
2803                                                 Error_EndLineExpected ();
2804                                                 return true;
2805                                         }
2806
2807                                         bool ret = false;
2808                                         if ((state & PARENT_TAKING) != 0) {
2809                                                 ret = (state & TAKING) == 0;
2810                                         
2811                                                 if (ret)
2812                                                         state |= TAKING;
2813                                                 else
2814                                                         state &= ~TAKING;
2815                                         }
2816         
2817                                         ifstack.Push (state | ELSE_SEEN);
2818                                         
2819                                         return ret;
2820                                 }
2821                         case PreprocessorDirective.Define:
2822                                 if (any_token_seen){
2823                                         if (caller_is_taking)
2824                                                 Error_TokensSeen ();
2825                                         return caller_is_taking;
2826                                 }
2827                                 PreProcessDefinition (true, arg, caller_is_taking);
2828                                 return caller_is_taking;
2829
2830                         case PreprocessorDirective.Undef:
2831                                 if (any_token_seen){
2832                                         if (caller_is_taking)
2833                                                 Error_TokensSeen ();
2834                                         return caller_is_taking;
2835                                 }
2836                                 PreProcessDefinition (false, arg, caller_is_taking);
2837                                 return caller_is_taking;
2838
2839                         case PreprocessorDirective.Invalid:
2840                                 Report.Error (1024, Location, "Wrong preprocessor directive");
2841                                 return true;
2842                         }
2843
2844                         //
2845                         // These are only processed if we are in a `taking' block
2846                         //
2847                         if (!caller_is_taking)
2848                                 return false;
2849                                         
2850                         switch (directive){
2851                         case PreprocessorDirective.Error:
2852                                 Report.Error (1029, Location, "#error: '{0}'", arg);
2853                                 return true;
2854
2855                         case PreprocessorDirective.Warning:
2856                                 Report.Warning (1030, 1, Location, "#warning: `{0}'", arg);
2857                                 return true;
2858
2859                         case PreprocessorDirective.Pragma:
2860                                 if (context.Settings.Version == LanguageVersion.ISO_1) {
2861                                         Report.FeatureIsNotAvailable (context, Location, "#pragma");
2862                                 }
2863
2864                                 ParsePragmaDirective (arg);
2865                                 return true;
2866
2867                         case PreprocessorDirective.Line:
2868                                 Location loc = Location;
2869                                 if (!PreProcessLine ())
2870                                         Report.Error (1576, loc, "The line number specified for #line directive is missing or invalid");
2871
2872                                 return caller_is_taking;
2873                         }
2874
2875                         throw new NotImplementedException (directive.ToString ());
2876                 }
2877
2878                 private int consume_string (bool quoted)
2879                 {
2880                         int c;
2881                         int pos = 0;
2882                         Location start_location = Location;
2883                         if (quoted)
2884                                 start_location = start_location - 1;
2885
2886 #if FULL_AST
2887                         int reader_pos = reader.Position;
2888 #endif
2889
2890                         while (true){
2891                                 // Cannot use get_char because of \r in quoted strings
2892                                 if (putback_char != -1) {
2893                                         c = putback_char;
2894                                         putback_char = -1;
2895                                 } else {
2896                                         c = reader.Read ();
2897                                 }
2898
2899                                 if (c == '"') {
2900                                         ++col;
2901
2902                                         if (quoted && peek_char () == '"') {
2903                                                 if (pos == value_builder.Length)
2904                                                         Array.Resize (ref value_builder, pos * 2);
2905
2906                                                 value_builder[pos++] = (char) c;
2907                                                 get_char ();
2908                                                 continue;
2909                                         }
2910
2911                                         string s;
2912                                         if (pos == 0)
2913                                                 s = string.Empty;
2914                                         else if (pos <= 4)
2915                                                 s = InternIdentifier (value_builder, pos);
2916                                         else
2917                                                 s = new string (value_builder, 0, pos);
2918
2919                                         ILiteralConstant res = new StringLiteral (context.BuiltinTypes, s, start_location);
2920                                         val = res;
2921 #if FULL_AST
2922                                         res.ParsedValue = quoted ?
2923                                                 reader.ReadChars (reader_pos - 2, reader.Position - 1) :
2924                                                 reader.ReadChars (reader_pos - 1, reader.Position);
2925 #endif
2926
2927                                         return Token.LITERAL;
2928                                 }
2929
2930                                 if (c == '\n') {
2931                                         if (!quoted) {
2932                                                 Report.Error (1010, Location, "Newline in constant");
2933
2934                                                 advance_line ();
2935
2936                                                 // Don't add \r to string literal
2937                                                 if (pos > 1 && value_builder [pos - 1] == '\r')
2938                                                         --pos;
2939
2940                                                 val = new StringLiteral (context.BuiltinTypes, new string (value_builder, 0, pos), start_location);
2941                                                 return Token.LITERAL;
2942                                         }
2943
2944                                         advance_line ();
2945                                 } else if (c == '\\' && !quoted) {
2946                                         ++col;
2947                                         int surrogate;
2948                                         c = escape (c, out surrogate);
2949                                         if (c == -1)
2950                                                 return Token.ERROR;
2951                                         if (surrogate != 0) {
2952                                                 if (pos == value_builder.Length)
2953                                                         Array.Resize (ref value_builder, pos * 2);
2954
2955                                                 value_builder[pos++] = (char) c;
2956                                                 c = surrogate;
2957                                         }
2958                                 } else if (c == -1) {
2959                                         Report.Error (1039, Location, "Unterminated string literal");
2960                                         return Token.EOF;
2961                                 } else {
2962                                         ++col;
2963                                 }
2964
2965                                 if (pos == value_builder.Length)
2966                                         Array.Resize (ref value_builder, pos * 2);
2967
2968                                 value_builder[pos++] = (char) c;
2969                         }
2970                 }
2971
2972                 private int consume_identifier (int s)
2973                 {
2974                         int res = consume_identifier (s, false);
2975
2976                         if (doc_state == XmlCommentState.Allowed)
2977                                 doc_state = XmlCommentState.NotAllowed;
2978
2979                         return res;
2980                 }
2981
2982                 int consume_identifier (int c, bool quoted) 
2983                 {
2984                         //
2985                         // This method is very performance sensitive. It accounts
2986                         // for approximately 25% of all parser time
2987                         //
2988
2989                         int pos = 0;
2990                         int column = col;
2991                         if (quoted)
2992                                 --column;
2993
2994                         if (c == '\\') {
2995                                 int surrogate;
2996                                 c = escape (c, out surrogate);
2997                                 if (surrogate != 0) {
2998                                         id_builder [pos++] = (char) c;
2999                                         c = surrogate;
3000                                 }
3001                         }
3002
3003                         id_builder [pos++] = (char) c;
3004
3005                         try {
3006                                 while (true) {
3007                                         c = reader.Read ();
3008
3009                                         if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9')) {
3010                                                 id_builder [pos++] = (char) c;
3011                                                 continue;
3012                                         }
3013
3014                                         if (c < 0x80) {
3015                                                 if (c == '\\') {
3016                                                         int surrogate;
3017                                                         c = escape (c, out surrogate);
3018                                                         if (is_identifier_part_character ((char) c))
3019                                                                 id_builder[pos++] = (char) c;
3020
3021                                                         if (surrogate != 0) {
3022                                                                 c = surrogate;
3023                                                         }
3024
3025                                                         continue;
3026                                                 }
3027                                         } else if (Char.IsLetter ((char) c) || Char.GetUnicodeCategory ((char) c) == UnicodeCategory.ConnectorPunctuation) {
3028                                                 id_builder [pos++] = (char) c;
3029                                                 continue;
3030                                         }
3031
3032                                         putback_char = c;
3033                                         break;
3034                                 }
3035                         } catch (IndexOutOfRangeException) {
3036                                 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
3037                                 --pos;
3038                                 col += pos;
3039                         }
3040
3041                         col += pos - 1;
3042
3043                         //
3044                         // Optimization: avoids doing the keyword lookup
3045                         // on uppercase letters
3046                         //
3047                         if (id_builder [0] >= '_' && !quoted) {
3048                                 int keyword = GetKeyword (id_builder, pos);
3049                                 if (keyword != -1) {
3050                                         val = ltb.Create (keyword == Token.AWAIT ? "await" : null, current_source, ref_line, column);
3051                                         return keyword;
3052                                 }
3053                         }
3054
3055                         string s = InternIdentifier (id_builder, pos);
3056                         val = ltb.Create (s, current_source, ref_line, column);
3057                         if (quoted && parsing_attribute_section)
3058                                 AddEscapedIdentifier (((LocatedToken) val).Location);
3059
3060                         return Token.IDENTIFIER;
3061                 }
3062
3063                 string InternIdentifier (char[] charBuffer, int length)
3064                 {
3065                         //
3066                         // Keep identifiers in an array of hashtables to avoid needless
3067                         // allocations
3068                         //
3069                         var identifiers_group = identifiers[length];
3070                         string s;
3071                         if (identifiers_group != null) {
3072                                 if (identifiers_group.TryGetValue (charBuffer, out s)) {
3073                                         return s;
3074                                 }
3075                         } else {
3076                                 // TODO: this should be number of files dependant
3077                                 // corlib compilation peaks at 1000 and System.Core at 150
3078                                 int capacity = length > 20 ? 10 : 100;
3079                                 identifiers_group = new Dictionary<char[], string> (capacity, new IdentifiersComparer (length));
3080                                 identifiers[length] = identifiers_group;
3081                         }
3082
3083                         char[] chars = new char[length];
3084                         Array.Copy (charBuffer, chars, length);
3085
3086                         s = new string (charBuffer, 0, length);
3087                         identifiers_group.Add (chars, s);
3088                         return s;
3089                 }
3090                 
3091                 public int xtoken ()
3092                 {
3093                         int d, c;
3094
3095                         // Whether we have seen comments on the current line
3096                         bool comments_seen = false;
3097                         while ((c = get_char ()) != -1) {
3098                                 switch (c) {
3099                                 case '\t':
3100                                         col = ((col - 1 + tab_size) / tab_size) * tab_size;
3101                                         continue;
3102
3103                                 case ' ':
3104                                 case '\f':
3105                                 case '\v':
3106                                 case 0xa0:
3107                                 case 0:
3108                                 case 0xFEFF:    // Ignore BOM anywhere in the file
3109                                         continue;
3110
3111 /*                              This is required for compatibility with .NET
3112                                 case 0xEF:
3113                                         if (peek_char () == 0xBB) {
3114                                                 PushPosition ();
3115                                                 get_char ();
3116                                                 if (get_char () == 0xBF)
3117                                                         continue;
3118                                                 PopPosition ();
3119                                         }
3120                                         break;
3121 */
3122                                 case '\\':
3123                                         tokens_seen = true;
3124                                         return consume_identifier (c);
3125
3126                                 case '{':
3127                                         val = ltb.Create (current_source, ref_line, col);
3128                                         return Token.OPEN_BRACE;
3129                                 case '}':
3130                                         val = ltb.Create (current_source, ref_line, col);
3131                                         return Token.CLOSE_BRACE;
3132                                 case '[':
3133                                         // To block doccomment inside attribute declaration.
3134                                         if (doc_state == XmlCommentState.Allowed)
3135                                                 doc_state = XmlCommentState.NotAllowed;
3136
3137                                         val = ltb.Create (current_source, ref_line, col);
3138
3139                                         if (parsing_block == 0 || lambda_arguments_parsing)
3140                                                 return Token.OPEN_BRACKET;
3141
3142                                         int next = peek_char ();
3143                                         switch (next) {
3144                                         case ']':
3145                                         case ',':
3146                                                 return Token.OPEN_BRACKET;
3147
3148                                         case ' ':
3149                                         case '\f':
3150                                         case '\v':
3151                                         case '\r':
3152                                         case '\n':
3153                                         case '/':
3154                                                 next = peek_token ();
3155                                                 if (next == Token.COMMA || next == Token.CLOSE_BRACKET)
3156                                                         return Token.OPEN_BRACKET;
3157
3158                                                 return Token.OPEN_BRACKET_EXPR;
3159                                         default:
3160                                                 return Token.OPEN_BRACKET_EXPR;
3161                                         }
3162                                 case ']':
3163                                         ltb.CreateOptional (current_source, ref_line, col, ref val);
3164                                         return Token.CLOSE_BRACKET;
3165                                 case '(':
3166                                         val = ltb.Create (current_source, ref_line, col);
3167                                         //
3168                                         // An expression versions of parens can appear in block context only
3169                                         //
3170                                         if (parsing_block != 0 && !lambda_arguments_parsing) {
3171                                                 
3172                                                 //
3173                                                 // Optmize most common case where we know that parens
3174                                                 // is not special
3175                                                 //
3176                                                 switch (current_token) {
3177                                                 case Token.IDENTIFIER:
3178                                                 case Token.IF:
3179                                                 case Token.FOR:
3180                                                 case Token.FOREACH:
3181                                                 case Token.TYPEOF:
3182                                                 case Token.WHILE:
3183                                                 case Token.SWITCH:
3184                                                 case Token.USING:
3185                                                 case Token.DEFAULT:
3186                                                 case Token.DELEGATE:
3187                                                 case Token.OP_GENERICS_GT:
3188                                                         return Token.OPEN_PARENS;
3189                                                 }
3190
3191                                                 // Optimize using peek
3192                                                 int xx = peek_char ();
3193                                                 switch (xx) {
3194                                                 case '(':
3195                                                 case '\'':
3196                                                 case '"':
3197                                                 case '0':
3198                                                 case '1':
3199                                                         return Token.OPEN_PARENS;
3200                                                 }
3201
3202                                                 lambda_arguments_parsing = true;
3203                                                 PushPosition ();
3204                                                 d = TokenizeOpenParens ();
3205                                                 PopPosition ();
3206                                                 lambda_arguments_parsing = false;
3207                                                 return d;
3208                                         }
3209
3210                                         return Token.OPEN_PARENS;
3211                                 case ')':
3212                                         ltb.CreateOptional (current_source, ref_line, col, ref val);
3213                                         return Token.CLOSE_PARENS;
3214                                 case ',':
3215                                         ltb.CreateOptional (current_source, ref_line, col, ref val);
3216                                         return Token.COMMA;
3217                                 case ';':
3218                                         ltb.CreateOptional (current_source, ref_line, col, ref val);
3219                                         return Token.SEMICOLON;
3220                                 case '~':
3221                                         val = ltb.Create (current_source, ref_line, col);
3222                                         return Token.TILDE;
3223                                 case '?':
3224                                         val = ltb.Create (current_source, ref_line, col);
3225                                         return TokenizePossibleNullableType ();
3226                                 case '<':
3227                                         val = ltb.Create (current_source, ref_line, col);
3228                                         if (parsing_generic_less_than++ > 0)
3229                                                 return Token.OP_GENERICS_LT;
3230
3231                                         return TokenizeLessThan ();
3232
3233                                 case '>':
3234                                         val = ltb.Create (current_source, ref_line, col);
3235                                         d = peek_char ();
3236
3237                                         if (d == '='){
3238                                                 get_char ();
3239                                                 return Token.OP_GE;
3240                                         }
3241
3242                                         if (parsing_generic_less_than > 1 || (parsing_generic_less_than == 1 && d != '>')) {
3243                                                 parsing_generic_less_than--;
3244                                                 return Token.OP_GENERICS_GT;
3245                                         }
3246
3247                                         if (d == '>') {
3248                                                 get_char ();
3249                                                 d = peek_char ();
3250
3251                                                 if (d == '=') {
3252                                                         get_char ();
3253                                                         return Token.OP_SHIFT_RIGHT_ASSIGN;
3254                                                 }
3255                                                 return Token.OP_SHIFT_RIGHT;
3256                                         }
3257
3258                                         return Token.OP_GT;
3259
3260                                 case '+':
3261                                         val = ltb.Create (current_source, ref_line, col);
3262                                         d = peek_char ();
3263                                         if (d == '+') {
3264                                                 d = Token.OP_INC;
3265                                         } else if (d == '=') {
3266                                                 d = Token.OP_ADD_ASSIGN;
3267                                         } else {
3268                                                 return Token.PLUS;
3269                                         }
3270                                         get_char ();
3271                                         return d;
3272
3273                                 case '-':
3274                                         val = ltb.Create (current_source, ref_line, col);
3275                                         d = peek_char ();
3276                                         if (d == '-') {
3277                                                 d = Token.OP_DEC;
3278                                         } else if (d == '=')
3279                                                 d = Token.OP_SUB_ASSIGN;
3280                                         else if (d == '>')
3281                                                 d = Token.OP_PTR;
3282                                         else {
3283                                                 return Token.MINUS;
3284                                         }
3285                                         get_char ();
3286                                         return d;
3287
3288                                 case '!':
3289                                         val = ltb.Create (current_source, ref_line, col);
3290                                         if (peek_char () == '='){
3291                                                 get_char ();
3292                                                 return Token.OP_NE;
3293                                         }
3294                                         return Token.BANG;
3295
3296                                 case '=':
3297                                         val = ltb.Create (current_source, ref_line, col);
3298                                         d = peek_char ();
3299                                         if (d == '='){
3300                                                 get_char ();
3301                                                 return Token.OP_EQ;
3302                                         }
3303                                         if (d == '>'){
3304                                                 get_char ();
3305                                                 return Token.ARROW;
3306                                         }
3307
3308                                         return Token.ASSIGN;
3309
3310                                 case '&':
3311                                         val = ltb.Create (current_source, ref_line, col);
3312                                         d = peek_char ();
3313                                         if (d == '&'){
3314                                                 get_char ();
3315                                                 return Token.OP_AND;
3316                                         }
3317                                         if (d == '='){
3318                                                 get_char ();
3319                                                 return Token.OP_AND_ASSIGN;
3320                                         }
3321                                         return Token.BITWISE_AND;
3322
3323                                 case '|':
3324                                         val = ltb.Create (current_source, ref_line, col);
3325                                         d = peek_char ();
3326                                         if (d == '|'){
3327                                                 get_char ();
3328                                                 return Token.OP_OR;
3329                                         }
3330                                         if (d == '='){
3331                                                 get_char ();
3332                                                 return Token.OP_OR_ASSIGN;
3333                                         }
3334                                         return Token.BITWISE_OR;
3335
3336                                 case '*':
3337                                         val = ltb.Create (current_source, ref_line, col);
3338                                         if (peek_char () == '='){
3339                                                 get_char ();
3340                                                 return Token.OP_MULT_ASSIGN;
3341                                         }
3342                                         return Token.STAR;
3343
3344                                 case '/':
3345                                         d = peek_char ();
3346                                         if (d == '='){
3347                                                 val = ltb.Create (current_source, ref_line, col);
3348                                                 get_char ();
3349                                                 return Token.OP_DIV_ASSIGN;
3350                                         }
3351
3352                                         // Handle double-slash comments.
3353                                         if (d == '/'){
3354                                                 get_char ();
3355                                                 if (doc_processing) {
3356                                                         if (peek_char () == '/') {
3357                                                                 get_char ();
3358                                                                 // Don't allow ////.
3359                                                                 if ((d = peek_char ()) != '/') {
3360                                                                         if (doc_state == XmlCommentState.Allowed)
3361                                                                                 handle_one_line_xml_comment ();
3362                                                                         else if (doc_state == XmlCommentState.NotAllowed)
3363                                                                                 WarningMisplacedComment (Location - 3);
3364                                                                 }
3365                                                         } else {
3366                                                                 if (xml_comment_buffer.Length > 0)
3367                                                                         doc_state = XmlCommentState.NotAllowed;
3368                                                         }
3369                                                 }
3370
3371                                                 while ((d = get_char ()) != -1 && d != '\n');
3372
3373                                                 any_token_seen |= tokens_seen;
3374                                                 tokens_seen = false;
3375                                                 comments_seen = false;
3376                                                 continue;
3377                                         } else if (d == '*'){
3378                                                 get_char ();
3379                                                 bool docAppend = false;
3380                                                 if (doc_processing && peek_char () == '*') {
3381                                                         get_char ();
3382                                                         // But when it is /**/, just do nothing.
3383                                                         if (peek_char () == '/') {
3384                                                                 get_char ();
3385                                                                 continue;
3386                                                         }
3387                                                         if (doc_state == XmlCommentState.Allowed)
3388                                                                 docAppend = true;
3389                                                         else if (doc_state == XmlCommentState.NotAllowed) {
3390                                                                 WarningMisplacedComment (Location - 2);
3391                                                         }
3392                                                 }
3393
3394                                                 int current_comment_start = 0;
3395                                                 if (docAppend) {
3396                                                         current_comment_start = xml_comment_buffer.Length;
3397                                                         xml_comment_buffer.Append (Environment.NewLine);
3398                                                 }
3399
3400                                                 while ((d = get_char ()) != -1){
3401                                                         if (d == '*' && peek_char () == '/'){
3402                                                                 get_char ();
3403                                                                 comments_seen = true;
3404                                                                 break;
3405                                                         }
3406                                                         if (docAppend)
3407                                                                 xml_comment_buffer.Append ((char) d);
3408                                                         
3409                                                         if (d == '\n'){
3410                                                                 any_token_seen |= tokens_seen;
3411                                                                 tokens_seen = false;
3412                                                                 // 
3413                                                                 // Reset 'comments_seen' just to be consistent.
3414                                                                 // It doesn't matter either way, here.
3415                                                                 //
3416                                                                 comments_seen = false;
3417                                                         }
3418                                                 }
3419                                                 if (!comments_seen)
3420                                                         Report.Error (1035, Location, "End-of-file found, '*/' expected");
3421
3422                                                 if (docAppend)
3423                                                         update_formatted_doc_comment (current_comment_start);
3424                                                 continue;
3425                                         }
3426                                         val = ltb.Create (current_source, ref_line, col);
3427                                         return Token.DIV;
3428
3429                                 case '%':
3430                                         val = ltb.Create (current_source, ref_line, col);
3431                                         if (peek_char () == '='){
3432                                                 get_char ();
3433                                                 return Token.OP_MOD_ASSIGN;
3434                                         }
3435                                         return Token.PERCENT;
3436
3437                                 case '^':
3438                                         val = ltb.Create (current_source, ref_line, col);
3439                                         if (peek_char () == '='){
3440                                                 get_char ();
3441                                                 return Token.OP_XOR_ASSIGN;
3442                                         }
3443                                         return Token.CARRET;
3444
3445                                 case ':':
3446                                         val = ltb.Create (current_source, ref_line, col);
3447                                         if (peek_char () == ':') {
3448                                                 get_char ();
3449                                                 return Token.DOUBLE_COLON;
3450                                         }
3451                                         return Token.COLON;
3452
3453                                 case '0': case '1': case '2': case '3': case '4':
3454                                 case '5': case '6': case '7': case '8': case '9':
3455                                         tokens_seen = true;
3456                                         return is_number (c, false);
3457
3458                                 case '\n': // white space
3459                                         any_token_seen |= tokens_seen;
3460                                         tokens_seen = false;
3461                                         comments_seen = false;
3462                                         continue;
3463
3464                                 case '.':
3465                                         tokens_seen = true;
3466                                         d = peek_char ();
3467                                         if (d >= '0' && d <= '9')
3468                                                 return is_number (c, true);
3469
3470                                         ltb.CreateOptional (current_source, ref_line, col, ref val);
3471                                         return Token.DOT;
3472                                 
3473                                 case '#':
3474                                         if (tokens_seen || comments_seen) {
3475                                                 Eror_WrongPreprocessorLocation ();
3476                                                 return Token.ERROR;
3477                                         }
3478                                         
3479                                         if (ParsePreprocessingDirective (true))
3480                                                 continue;
3481
3482                                         bool directive_expected = false;
3483                                         while ((c = get_char ()) != -1) {
3484                                                 if (col == 1) {
3485                                                         directive_expected = true;
3486                                                 } else if (!directive_expected) {
3487                                                         // TODO: Implement comment support for disabled code and uncomment this code
3488 //                                                      if (c == '#') {
3489 //                                                              Eror_WrongPreprocessorLocation ();
3490 //                                                              return Token.ERROR;
3491 //                                                      }
3492                                                         continue;
3493                                                 }
3494
3495                                                 if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\v' )
3496                                                         continue;
3497
3498                                                 if (c == '#') {
3499                                                         if (ParsePreprocessingDirective (false))
3500                                                                 break;
3501                                                 }
3502                                                 directive_expected = false;
3503                                         }
3504
3505                                         if (c != -1) {
3506                                                 tokens_seen = false;
3507                                                 continue;
3508                                         }
3509
3510                                         return Token.EOF;
3511                                 
3512                                 case '"':
3513                                         return consume_string (false);
3514
3515                                 case '\'':
3516                                         return TokenizeBackslash ();
3517                                 
3518                                 case '@':
3519                                         c = get_char ();
3520                                         if (c == '"') {
3521                                                 tokens_seen = true;
3522                                                 return consume_string (true);
3523                                         }
3524
3525                                         if (is_identifier_start_character (c)){
3526                                                 return consume_identifier (c, true);
3527                                         }
3528
3529                                         Report.Error (1646, Location, "Keyword, identifier, or string expected after verbatim specifier: @");
3530                                         return Token.ERROR;
3531
3532                                 case EvalStatementParserCharacter:
3533                                         return Token.EVAL_STATEMENT_PARSER;
3534                                 case EvalCompilationUnitParserCharacter:
3535                                         return Token.EVAL_COMPILATION_UNIT_PARSER;
3536                                 case EvalUsingDeclarationsParserCharacter:
3537                                         return Token.EVAL_USING_DECLARATIONS_UNIT_PARSER;
3538                                 case DocumentationXref:
3539                                         return Token.DOC_SEE;
3540                                 }
3541
3542                                 if (is_identifier_start_character (c)) {
3543                                         tokens_seen = true;
3544                                         return consume_identifier (c);
3545                                 }
3546
3547                                 if (char.IsWhiteSpace ((char) c))
3548                                         continue;
3549
3550                                 Report.Error (1056, Location, "Unexpected character `{0}'", ((char) c).ToString ());
3551                         }
3552
3553                         if (CompleteOnEOF){
3554                                 if (generated)
3555                                         return Token.COMPLETE_COMPLETION;
3556                                 
3557                                 generated = true;
3558                                 return Token.GENERATE_COMPLETION;
3559                         }
3560                         
3561
3562                         return Token.EOF;
3563                 }
3564
3565                 int TokenizeBackslash ()
3566                 {
3567 #if FULL_AST
3568                         int read_start = reader.Position;
3569 #endif
3570                         Location start_location = Location;
3571                         int c = get_char ();
3572                         tokens_seen = true;
3573                         if (c == '\'') {
3574                                 val = new CharLiteral (context.BuiltinTypes, (char) c, start_location);
3575                                 Report.Error (1011, start_location, "Empty character literal");
3576                                 return Token.LITERAL;
3577                         }
3578
3579                         if (c == '\n') {
3580                                 Report.Error (1010, start_location, "Newline in constant");
3581                                 return Token.ERROR;
3582                         }
3583
3584                         int d;
3585                         c = escape (c, out d);
3586                         if (c == -1)
3587                                 return Token.ERROR;
3588                         if (d != 0)
3589                                 throw new NotImplementedException ();
3590
3591                         ILiteralConstant res = new CharLiteral (context.BuiltinTypes, (char) c, start_location);
3592                         val = res;
3593                         c = get_char ();
3594
3595                         if (c != '\'') {
3596                                 Report.Error (1012, start_location, "Too many characters in character literal");
3597
3598                                 // Try to recover, read until newline or next "'"
3599                                 while ((c = get_char ()) != -1) {
3600                                         if (c == '\n' || c == '\'')
3601                                                 break;
3602                                 }
3603                         }
3604
3605 #if FULL_AST
3606                         res.ParsedValue = reader.ReadChars (read_start - 1, reader.Position);
3607 #endif
3608
3609                         return Token.LITERAL;
3610                 }
3611
3612                 int TokenizeLessThan ()
3613                 {
3614                         int d;
3615                         if (handle_typeof) {
3616                                 PushPosition ();
3617                                 if (parse_generic_dimension (out d)) {
3618                                         val = d;
3619                                         DiscardPosition ();
3620                                         return Token.GENERIC_DIMENSION;
3621                                 }
3622                                 PopPosition ();
3623                         }
3624
3625                         // Save current position and parse next token.
3626                         PushPosition ();
3627                         if (parse_less_than ()) {
3628                                 if (parsing_generic_declaration && (parsing_generic_declaration_doc || token () != Token.DOT)) {
3629                                         d = Token.OP_GENERICS_LT_DECL;
3630                                 } else {
3631                                         d = Token.OP_GENERICS_LT;
3632                                 }
3633                                 PopPosition ();
3634                                 return d;
3635                         }
3636
3637                         PopPosition ();
3638                         parsing_generic_less_than = 0;
3639
3640                         d = peek_char ();
3641                         if (d == '<') {
3642                                 get_char ();
3643                                 d = peek_char ();
3644
3645                                 if (d == '=') {
3646                                         get_char ();
3647                                         return Token.OP_SHIFT_LEFT_ASSIGN;
3648                                 }
3649                                 return Token.OP_SHIFT_LEFT;
3650                         }
3651
3652                         if (d == '=') {
3653                                 get_char ();
3654                                 return Token.OP_LE;
3655                         }
3656                         return Token.OP_LT;
3657                 }
3658
3659                 //
3660                 // Handles one line xml comment
3661                 //
3662                 private void handle_one_line_xml_comment ()
3663                 {
3664                         int c;
3665                         while ((c = peek_char ()) == ' ')
3666                                 get_char (); // skip heading whitespaces.
3667                         while ((c = peek_char ()) != -1 && c != '\n' && c != '\r') {
3668                                 xml_comment_buffer.Append ((char) get_char ());
3669                         }
3670                         if (c == '\r' || c == '\n')
3671                                 xml_comment_buffer.Append (Environment.NewLine);
3672                 }
3673
3674                 //
3675                 // Remove heading "*" in Javadoc-like xml documentation.
3676                 //
3677                 private void update_formatted_doc_comment (int current_comment_start)
3678                 {
3679                         int length = xml_comment_buffer.Length - current_comment_start;
3680                         string [] lines = xml_comment_buffer.ToString (
3681                                 current_comment_start,
3682                                 length).Replace ("\r", "").Split ('\n');
3683                         
3684                         // The first line starts with /**, thus it is not target
3685                         // for the format check.
3686                         for (int i = 1; i < lines.Length; i++) {
3687                                 string s = lines [i];
3688                                 int idx = s.IndexOf ('*');
3689                                 string head = null;
3690                                 if (idx < 0) {
3691                                         if (i < lines.Length - 1)
3692                                                 return;
3693                                         head = s;
3694                                 } else
3695                                         head = s.Substring (0, idx);
3696                                 foreach (char c in head)
3697                                         if (c != ' ')
3698                                                 return;
3699                                 lines [i] = s.Substring (idx + 1);
3700                         }
3701                         xml_comment_buffer.Remove (current_comment_start, length);
3702                         xml_comment_buffer.Insert (current_comment_start, String.Join (Environment.NewLine, lines));
3703                 }
3704
3705                 //
3706                 // Checks if there was incorrect doc comments and raise
3707                 // warnings.
3708                 //
3709                 public void check_incorrect_doc_comment ()
3710                 {
3711                         if (xml_comment_buffer.Length > 0)
3712                                 WarningMisplacedComment (Location);
3713                 }
3714
3715                 //
3716                 // Consumes the saved xml comment lines (if any)
3717                 // as for current target member or type.
3718                 //
3719                 public string consume_doc_comment ()
3720                 {
3721                         if (xml_comment_buffer.Length > 0) {
3722                                 string ret = xml_comment_buffer.ToString ();
3723                                 reset_doc_comment ();
3724                                 return ret;
3725                         }
3726                         return null;
3727                 }
3728
3729                 Report Report {
3730                         get { return context.Report; }
3731                 }
3732
3733                 void reset_doc_comment ()
3734                 {
3735                         xml_comment_buffer.Length = 0;
3736                 }
3737
3738                 public void cleanup ()
3739                 {
3740                         if (ifstack != null && ifstack.Count >= 1) {
3741                                 int state = ifstack.Pop ();
3742                                 if ((state & REGION) != 0)
3743                                         Report.Error (1038, Location, "#endregion directive expected");
3744                                 else 
3745                                         Report.Error (1027, Location, "Expected `#endif' directive");
3746                         }
3747                 }
3748         }
3749
3750         //
3751         // Indicates whether it accepts XML documentation or not.
3752         //
3753         public enum XmlCommentState {
3754                 // comment is allowed in this state.
3755                 Allowed,
3756                 // comment is not allowed in this state.
3757                 NotAllowed,
3758                 // once comments appeared when it is NotAllowed, then the
3759                 // state is changed to it, until the state is changed to
3760                 // .Allowed.
3761                 Error
3762         }
3763 }
3764