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