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