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