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