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