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