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