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