Mark tests as not working under TARGET_JVM
[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 // Licensed under the terms of the GNU GPL
9 //
10 // (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
12 //
13 //
14
15 using System;
16 using System.Text;
17 using System.Collections;
18 using System.IO;
19 using System.Globalization;
20 using System.Reflection;
21
22 namespace Mono.CSharp
23 {
24         /// <summary>
25         ///    Tokenizer for C# source code. 
26         /// </summary>
27
28         public class Tokenizer : yyParser.yyInput
29         {
30                 SeekableStreamReader reader;
31                 SourceFile ref_name;
32                 SourceFile file_name;
33                 int ref_line = 1;
34                 int line = 1;
35                 int col = 0;
36                 int previous_col;
37                 int current_token;
38                 bool handle_get_set = false;
39                 bool handle_remove_add = false;
40                 bool handle_assembly = false;
41                 bool handle_constraints = false;
42                 bool handle_typeof = false;
43                 bool query_parsing;
44                 Location current_location;
45                 Location current_comment_location = Location.Null;
46                 ArrayList escapedIdentifiers = new ArrayList ();
47
48                 static bool linq;
49
50                 //
51                 // XML documentation buffer. The save point is used to divide
52                 // comments on types and comments on members.
53                 //
54                 StringBuilder xml_comment_buffer;
55
56                 //
57                 // See comment on XmlCommentState enumeration.
58                 //
59                 XmlCommentState xmlDocState = XmlCommentState.Allowed;
60
61                 //
62                 // Whether tokens have been seen on this line
63                 //
64                 bool tokens_seen = false;
65
66                 //
67                 // Whether a token has been seen on the file
68                 // This is needed because `define' is not allowed to be used
69                 // after a token has been seen.
70                 //
71                 bool any_token_seen = false;
72
73                 static Hashtable tokenValues;
74                 static readonly char[] simple_whitespaces = new char[] { ' ', '\t' };
75
76                 private static Hashtable TokenValueName
77                 {
78                         get {
79                                 if (tokenValues == null)
80                                         tokenValues = GetTokenValueNameHash ();
81
82                                 return tokenValues;
83                         }
84                 }
85
86                 private static Hashtable GetTokenValueNameHash ()
87                 {
88                         Type t = typeof (Token);
89                         FieldInfo [] fields = t.GetFields ();
90                         Hashtable hash = new Hashtable ();
91                         foreach (FieldInfo field in fields) {
92                                 if (field.IsLiteral && field.IsStatic && field.FieldType == typeof (int))
93                                         hash.Add (field.GetValue (null), field.Name);
94                         }
95                         return hash;
96                 }
97                 
98                 //
99                 // Returns a verbose representation of the current location
100                 //
101                 public string location {
102                         get {
103                                 string det;
104
105                                 if (current_token == Token.ERROR)
106                                         det = "detail: " + error_details;
107                                 else
108                                         det = "";
109                                 
110                                 // return "Line:     "+line+" Col: "+col + "\n" +
111                                 //       "VirtLine: "+ref_line +
112                                 //       " Token: "+current_token + " " + det;
113                                 string current_token_name = TokenValueName [current_token] as string;
114                                 if (current_token_name == null)
115                                         current_token_name = current_token.ToString ();
116
117                                 return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name.Name,
118                                                                                        ref_line,
119                                                                                        col,
120                                                                                        current_token_name,
121                                                                                        det);
122                         }
123                 }
124
125                 public bool PropertyParsing {
126                         get {
127                                 return handle_get_set;
128                         }
129
130                         set {
131                                 handle_get_set = value;
132                         }
133                 }
134
135                 public bool AssemblyTargetParsing {
136                         get {
137                                 return handle_assembly;
138                         }
139
140                         set {
141                                 handle_assembly = value;
142                         }
143                 }
144
145                 public bool EventParsing {
146                         get {
147                                 return handle_remove_add;
148                         }
149
150                         set {
151                                 handle_remove_add = value;
152                         }
153                 }
154
155                 public bool ConstraintsParsing {
156                         get {
157                                 return handle_constraints;
158                         }
159
160                         set {
161                                 handle_constraints = value;
162                         }
163                 }
164
165                 public static bool LinqEnabled {
166                         set {
167                                 linq = value;
168                         }
169                 }
170
171                 public bool TypeOfParsing {
172                         get {
173                                 return handle_typeof;
174                         }
175
176                         set {
177                                 handle_typeof = value;
178                         }
179                 }
180
181                 public bool QueryParsing {
182                         set {
183                                 query_parsing = value;
184                         }
185                 }
186
187                 public XmlCommentState doc_state {
188                         get { return xmlDocState; }
189                         set {
190                                 if (value == XmlCommentState.Allowed) {
191                                         check_incorrect_doc_comment ();
192                                         reset_doc_comment ();
193                                 }
194                                 xmlDocState = value;
195                         }
196                 }
197
198                 public bool IsEscapedIdentifier (Location loc)
199                 {
200                         foreach (LocatedToken lt in escapedIdentifiers)
201                                 if (lt.Location.Equals (loc))
202                                         return true;
203                         return false;
204                 }
205
206                 //
207                 // Class variables
208                 // 
209                 static CharArrayHashtable[] keywords;
210                 static Hashtable keywordStrings;
211                 static NumberStyles styles;
212                 static NumberFormatInfo csharp_format_info;
213                 
214                 //
215                 // Values for the associated token returned
216                 //
217                 int putback_char;
218                 Object val;
219
220                 //
221                 // Pre-processor
222                 //
223                 Hashtable defines;
224
225                 const int TAKING        = 1;
226                 const int ELSE_SEEN     = 4;
227                 const int PARENT_TAKING = 8;
228                 const int REGION        = 16;           
229
230                 //
231                 // pre-processor if stack state:
232                 //
233                 Stack ifstack;
234
235                 static System.Text.StringBuilder string_builder;
236
237                 const int max_id_size = 512;
238                 static char [] id_builder = new char [max_id_size];
239
240                 static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
241
242                 const int max_number_size = 512;
243                 static char [] number_builder = new char [max_number_size];
244                 static int number_pos;
245                 
246                 //
247                 // Details about the error encoutered by the tokenizer
248                 //
249                 string error_details;
250                 
251                 public string error {
252                         get {
253                                 return error_details;
254                         }
255                 }
256                 
257                 public int Line {
258                         get {
259                                 return ref_line;
260                         }
261                 }
262
263                 public int Col {
264                         get {
265                                 return col;
266                         }
267                 }
268
269                 //
270                 // This is used when the tokenizer needs to save
271                 // the current position as it needs to do some parsing
272                 // on its own to deamiguate a token in behalf of the
273                 // parser.
274                 //
275                 Stack position_stack = new Stack (2);
276                 class Position {
277                         public int position;
278                         public int ref_line;
279                         public int col;
280                         public int putback_char;
281                         public int previous_col;
282                         public Stack ifstack;
283                         public int parsing_generic_less_than;
284                         public int current_token;
285
286                         public Position (Tokenizer t)
287                         {
288                                 position = t.reader.Position;
289                                 ref_line = t.ref_line;
290                                 col = t.col;
291                                 putback_char = t.putback_char;
292                                 previous_col = t.previous_col;
293                                 if (t.ifstack != null && t.ifstack.Count != 0)
294                                         ifstack = (Stack)t.ifstack.Clone ();
295                                 parsing_generic_less_than = t.parsing_generic_less_than;
296                                 current_token = t.current_token;
297                         }
298                 }
299                 
300                 public void PushPosition ()
301                 {
302                         position_stack.Push (new Position (this));
303                 }
304
305                 public void PopPosition ()
306                 {
307                         Position p = (Position) position_stack.Pop ();
308
309                         reader.Position = p.position;
310                         ref_line = p.ref_line;
311                         col = p.col;
312                         putback_char = p.putback_char;
313                         previous_col = p.previous_col;
314                         ifstack = p.ifstack;
315                         parsing_generic_less_than = p.parsing_generic_less_than;
316                         current_token = p.current_token;
317                 }
318
319                 // Do not reset the position, ignore it.
320                 public void DiscardPosition ()
321                 {
322                         position_stack.Pop ();
323                 }
324                 
325                 static void AddKeyword (string kw, int token)
326                 {
327                         keywordStrings.Add (kw, kw);
328                         if (keywords [kw.Length] == null) {
329                                 keywords [kw.Length] = new CharArrayHashtable (kw.Length);
330                         }
331                         keywords [kw.Length] [kw.ToCharArray ()] = token;
332                 }
333
334                 static void InitTokens ()
335                 {
336                         keywordStrings = new Hashtable ();
337                         keywords = new CharArrayHashtable [64];
338
339                         AddKeyword ("__arglist", Token.ARGLIST);
340                         AddKeyword ("abstract", Token.ABSTRACT);
341                         AddKeyword ("as", Token.AS);
342                         AddKeyword ("add", Token.ADD);
343                         AddKeyword ("assembly", Token.ASSEMBLY);
344                         AddKeyword ("base", Token.BASE);
345                         AddKeyword ("bool", Token.BOOL);
346                         AddKeyword ("break", Token.BREAK);
347                         AddKeyword ("byte", Token.BYTE);
348                         AddKeyword ("case", Token.CASE);
349                         AddKeyword ("catch", Token.CATCH);
350                         AddKeyword ("char", Token.CHAR);
351                         AddKeyword ("checked", Token.CHECKED);
352                         AddKeyword ("class", Token.CLASS);
353                         AddKeyword ("const", Token.CONST);
354                         AddKeyword ("continue", Token.CONTINUE);
355                         AddKeyword ("decimal", Token.DECIMAL);
356                         AddKeyword ("default", Token.DEFAULT);
357                         AddKeyword ("delegate", Token.DELEGATE);
358                         AddKeyword ("do", Token.DO);
359                         AddKeyword ("double", Token.DOUBLE);
360                         AddKeyword ("else", Token.ELSE);
361                         AddKeyword ("enum", Token.ENUM);
362                         AddKeyword ("event", Token.EVENT);
363                         AddKeyword ("explicit", Token.EXPLICIT);
364                         AddKeyword ("extern", Token.EXTERN);
365                         AddKeyword ("false", Token.FALSE);
366                         AddKeyword ("finally", Token.FINALLY);
367                         AddKeyword ("fixed", Token.FIXED);
368                         AddKeyword ("float", Token.FLOAT);
369                         AddKeyword ("for", Token.FOR);
370                         AddKeyword ("foreach", Token.FOREACH);
371                         AddKeyword ("goto", Token.GOTO);
372                         AddKeyword ("get", Token.GET);
373                         AddKeyword ("if", Token.IF);
374                         AddKeyword ("implicit", Token.IMPLICIT);
375                         AddKeyword ("in", Token.IN);
376                         AddKeyword ("int", Token.INT);
377                         AddKeyword ("interface", Token.INTERFACE);
378                         AddKeyword ("internal", Token.INTERNAL);
379                         AddKeyword ("is", Token.IS);
380                         AddKeyword ("lock", Token.LOCK);
381                         AddKeyword ("long", Token.LONG);
382                         AddKeyword ("namespace", Token.NAMESPACE);
383                         AddKeyword ("new", Token.NEW);
384                         AddKeyword ("null", Token.NULL);
385                         AddKeyword ("object", Token.OBJECT);
386                         AddKeyword ("operator", Token.OPERATOR);
387                         AddKeyword ("out", Token.OUT);
388                         AddKeyword ("override", Token.OVERRIDE);
389                         AddKeyword ("params", Token.PARAMS);
390                         AddKeyword ("private", Token.PRIVATE);
391                         AddKeyword ("protected", Token.PROTECTED);
392                         AddKeyword ("public", Token.PUBLIC);
393                         AddKeyword ("readonly", Token.READONLY);
394                         AddKeyword ("ref", Token.REF);
395                         AddKeyword ("remove", Token.REMOVE);
396                         AddKeyword ("return", Token.RETURN);
397                         AddKeyword ("sbyte", Token.SBYTE);
398                         AddKeyword ("sealed", Token.SEALED);
399                         AddKeyword ("set", Token.SET);
400                         AddKeyword ("short", Token.SHORT);
401                         AddKeyword ("sizeof", Token.SIZEOF);
402                         AddKeyword ("stackalloc", Token.STACKALLOC);
403                         AddKeyword ("static", Token.STATIC);
404                         AddKeyword ("string", Token.STRING);
405                         AddKeyword ("struct", Token.STRUCT);
406                         AddKeyword ("switch", Token.SWITCH);
407                         AddKeyword ("this", Token.THIS);
408                         AddKeyword ("throw", Token.THROW);
409                         AddKeyword ("true", Token.TRUE);
410                         AddKeyword ("try", Token.TRY);
411                         AddKeyword ("typeof", Token.TYPEOF);
412                         AddKeyword ("uint", Token.UINT);
413                         AddKeyword ("ulong", Token.ULONG);
414                         AddKeyword ("unchecked", Token.UNCHECKED);
415                         AddKeyword ("unsafe", Token.UNSAFE);
416                         AddKeyword ("ushort", Token.USHORT);
417                         AddKeyword ("using", Token.USING);
418                         AddKeyword ("virtual", Token.VIRTUAL);
419                         AddKeyword ("void", Token.VOID);
420                         AddKeyword ("volatile", Token.VOLATILE);
421                         AddKeyword ("while", Token.WHILE);
422                         AddKeyword ("partial", Token.PARTIAL);
423 #if GMCS_SOURCE
424                         AddKeyword ("where", Token.WHERE);
425
426                         if (linq) {
427                                 AddKeyword ("from", Token.FROM);
428                                 AddKeyword ("join", Token.JOIN);
429                                 AddKeyword ("on", Token.ON);
430                                 AddKeyword ("equals", Token.EQUALS);
431                                 AddKeyword ("select", Token.SELECT);
432                                 AddKeyword ("group", Token.GROUP);
433                                 AddKeyword ("by", Token.BY);
434                                 AddKeyword ("let", Token.LET);
435                                 AddKeyword ("orderby", Token.ORDERBY);
436                                 AddKeyword ("ascending", Token.ASCENDING);
437                                 AddKeyword ("descending", Token.DESCENDING);
438                                 AddKeyword ("into", Token.INTO);
439                         }
440 #endif
441                 }
442
443                 //
444                 // Class initializer
445                 // 
446                 static Tokenizer ()
447                 {
448                         Reset ();
449                 }
450
451                 public static void Reset ()
452                 {
453                         InitTokens ();
454                         csharp_format_info = NumberFormatInfo.InvariantInfo;
455                         styles = NumberStyles.Float;
456
457                         string_builder = new System.Text.StringBuilder ();
458                 }
459
460                 int GetKeyword (char[] id, int id_len)
461                 {
462                         /*
463                          * Keywords are stored in an array of hashtables grouped by their
464                          * length.
465                          */
466
467                         if ((id_len >= keywords.Length) || (keywords [id_len] == null))
468                                 return -1;
469                         object o = keywords [id_len] [id];
470
471                         if (o == null)
472                                 return -1;
473                         
474                         int res = (int) o;
475
476                         if (handle_get_set == false && (res == Token.GET || res == Token.SET))
477                                 return -1;
478                         if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
479                                 return -1;
480                         if (handle_assembly == false && res == Token.ASSEMBLY)
481                                 return -1;
482 #if GMCS_SOURCE
483                         if (linq) {
484                                 if (res == Token.FROM &&
485                                         (current_token == Token.ASSIGN || current_token == Token.OPEN_BRACKET ||
486                                          current_token == Token.RETURN || current_token == Token.IN)) {
487                                         query_parsing = true;
488                                         return res;
489                                 }
490
491                                 if (!query_parsing && res > Token.QUERY_FIRST_TOKEN && res < Token.QUERY_LAST_TOKEN)
492                                         return -1;
493
494                                 return res;
495                         }
496
497                         if (!handle_constraints && res == Token.WHERE)
498                                 return -1;
499 #endif
500                         return res;
501                         
502                 }
503
504                 public Location Location {
505                         get { return current_location; }
506                 }
507
508                 void define (string def)
509                 {
510                         if (!RootContext.AllDefines.Contains (def)){
511                                 RootContext.AllDefines [def] = true;
512                         }
513                         if (defines.Contains (def))
514                                 return;
515                         defines [def] = true;
516                 }
517                 
518                 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
519                 {
520                         this.ref_name = file;
521                         this.file_name = file;
522                         reader = input;
523                         
524                         putback_char = -1;
525
526                         if (defs != null){
527                                 defines = new Hashtable ();
528                                 foreach (string def in defs)
529                                         define (def);
530                         }
531
532                         xml_comment_buffer = new StringBuilder ();
533
534                         //
535                         // FIXME: This could be `Location.Push' but we have to
536                         // find out why the MS compiler allows this
537                         //
538                         Mono.CSharp.Location.Push (file);
539                 }
540
541                 static bool is_identifier_start_character (char c)
542                 {
543                         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || Char.IsLetter (c);
544                 }
545
546                 static bool is_identifier_part_character (char c)
547                 {
548                         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') ||
549                                 Char.IsLetter (c) || Char.GetUnicodeCategory (c) == UnicodeCategory.ConnectorPunctuation;
550                 }
551
552                 public static bool IsKeyword (string s)
553                 {
554                         return keywordStrings [s] != null;
555                 }
556
557                 public static bool IsValidIdentifier (string s)
558                 {
559                         if (s == null || s.Length == 0)
560                                 return false;
561
562                         if (!is_identifier_start_character (s [0]))
563                                 return false;
564                         
565                         for (int i = 1; i < s.Length; i ++)
566                                 if (! is_identifier_part_character (s [i]))
567                                         return false;
568                         
569                         return true;
570                 }
571
572                 bool parse_generic_dimension (out int dimension)
573                 {
574                         dimension = 1;
575
576                 again:
577                         int the_token = token ();
578                         if (the_token == Token.OP_GENERICS_GT)
579                                 return true;
580                         else if (the_token == Token.COMMA) {
581                                 dimension++;
582                                 goto again;
583                         }
584
585                         return false;
586                 }
587
588                 bool parse_less_than ()
589                 {
590                 start:
591                         int the_token = token ();
592                         if (the_token == Token.OPEN_BRACKET) {
593                                 do {
594                                         the_token = token ();
595                                 } while (the_token != Token.CLOSE_BRACKET);
596                                 the_token = token ();
597                         }
598                         switch (the_token) {
599                         case Token.IDENTIFIER:
600                         case Token.OBJECT:
601                         case Token.STRING:
602                         case Token.BOOL:
603                         case Token.DECIMAL:
604                         case Token.FLOAT:
605                         case Token.DOUBLE:
606                         case Token.SBYTE:
607                         case Token.BYTE:
608                         case Token.SHORT:
609                         case Token.USHORT:
610                         case Token.INT:
611                         case Token.UINT:
612                         case Token.LONG:
613                         case Token.ULONG:
614                         case Token.CHAR:
615                         case Token.VOID:
616                                 break;
617
618                         default:
619                                 return false;
620                         }
621                 again:
622                         the_token = token ();
623
624                         if (the_token == Token.OP_GENERICS_GT)
625                                 return true;
626                         else if (the_token == Token.COMMA || the_token == Token.DOT || the_token == Token.DOUBLE_COLON)
627                                 goto start;
628                         else if (the_token == Token.INTERR || the_token == Token.STAR)
629                                 goto again;
630                         else if (the_token == Token.OP_GENERICS_LT) {
631                                 if (!parse_less_than ())
632                                         return false;
633                                 goto again;
634                         } else if (the_token == Token.OPEN_BRACKET) {
635                         rank_specifiers:
636                                 the_token = token ();
637                                 if (the_token == Token.CLOSE_BRACKET)
638                                         goto again;
639                                 else if (the_token == Token.COMMA)
640                                         goto rank_specifiers;
641                                 return false;
642                         }
643
644                         return false;
645                 }
646
647 #if GMCS_SOURCE
648                 public void PutbackNullable ()
649                 {
650                         if (nullable_pos < 0)
651                                 throw new Exception ();
652
653                         current_token = -1;
654                         val = null;
655                         reader.Position = nullable_pos;
656
657                         putback_char = '?';
658                 }
659
660                 public void PutbackCloseParens ()
661                 {
662                         putback_char = ')';
663                 }
664
665
666                 int nullable_pos = -1;
667
668                 public void CheckNullable (bool is_nullable)
669                 {
670                         if (is_nullable)
671                                 nullable_pos = reader.Position;
672                         else
673                                 nullable_pos = -1;
674                 }
675 #endif
676                 
677                 public int peek_token ()
678                 {
679                         int the_token;
680
681                         PushPosition ();
682                         the_token = token ();
683                         PopPosition ();
684                         
685                         return the_token;
686                 }
687                 
688                 bool parse_namespace_or_typename (int next)
689                 {
690                         if (next == -1)
691                                 next = peek_token ();
692                         while (next == Token.IDENTIFIER){
693                                 token ();
694                           again:
695                                 next = peek_token ();
696                                 if (next == Token.DOT || next == Token.DOUBLE_COLON){
697                                         token ();
698                                         next = peek_token ();
699                                         continue;
700                                 }
701                                 if (next == Token.OP_GENERICS_LT){
702                                         token ();
703                                         if (!parse_less_than ())
704                                                 return false;
705                                         goto again;
706                                 }
707                                 return true;
708                         }
709
710                         return false;
711                 }
712
713                 bool is_simple_type (int token)
714                 {
715                         return  (token == Token.BOOL ||
716                                  token == Token.DECIMAL ||
717                                  token == Token.SBYTE ||
718                                  token == Token.BYTE ||
719                                  token == Token.SHORT ||
720                                  token == Token.USHORT ||
721                                  token == Token.INT ||
722                                  token == Token.UINT ||
723                                  token == Token.LONG ||
724                                  token == Token.ULONG ||
725                                  token == Token.CHAR ||
726                                  token == Token.FLOAT ||
727                                  token == Token.DOUBLE);
728                 }
729
730                 bool is_builtin_reference_type (int token)
731                 {
732                         return (token == Token.OBJECT || token == Token.STRING);
733                 }
734
735                 bool parse_opt_rank (int next)
736                 {
737                         while (true){
738                                 if (next != Token.OPEN_BRACKET)
739                                         return true;
740
741                                 token ();
742                                 while (true){
743                                         next = token ();
744                                         if (next == Token.CLOSE_BRACKET){
745                                                 next = peek_token ();
746                                                 break;
747                                         }
748                                         if (next == Token.COMMA)
749                                                 continue;
750                                         
751                                         return false;
752                                 }
753                         }
754                 }
755                         
756                 bool parse_type ()
757                 {
758                         int next = peek_token ();
759                         
760                         if (is_simple_type (next)){
761                                 token ();
762                                 next = peek_token ();
763                                 if (next == Token.INTERR)
764                                         token ();
765                                 return parse_opt_rank (peek_token ());
766                         }
767                         if (parse_namespace_or_typename (next)){
768                                 next = peek_token ();
769                                 if (next == Token.INTERR)
770                                         token ();
771                                 return parse_opt_rank (peek_token ());
772                         } else if (is_builtin_reference_type (next)){
773                                 token ();
774                                 return parse_opt_rank (peek_token ());
775                         }
776                         
777                         return false;
778                 }
779                 
780                 //
781                 // Invoked after '(' has been seen and tries to parse:
782                 // type identifier [, type identifier]*
783                 //
784                 // if this is the case, instead of returning an
785                 // OPEN_PARENS token we return a special token that
786                 // triggers lambda parsing.
787                 //
788                 // This is needed because we can not introduce the
789                 // explicitly_typed_lambda_parameter_list after a '(' in the
790                 // grammar without introducing reduce/reduce conflicts.
791                 //
792                 // We need to parse a type and if it is followed by an
793                 // identifier, we know it has to be parsed as a lambda
794                 // expression.  
795                 //
796                 // the type expression can be prefixed with `ref' or `out'
797                 //
798                 public bool parse_lambda_parameters ()
799                 {
800                         while (true){
801                                 int next = peek_token ();
802
803                                 if (next == Token.REF || next == Token.OUT)
804                                         token ();
805                                                  
806                                 if (parse_type ()){
807                                         next = peek_token ();
808                                         if (next == Token.IDENTIFIER){
809                                                 token ();
810                                                 next = peek_token ();
811                                                 if (next == Token.COMMA){
812                                                         token ();
813                                                         continue;
814                                                 }
815                                                 if (next == Token.CLOSE_PARENS)
816                                                         return true;
817                                         }
818                                 }
819                                 return false;
820                         }
821                 }
822
823                 int parsing_generic_less_than = 0;
824                 
825                 int is_punct (char c, ref bool doread)
826                 {
827                         int d;
828                         int t;
829
830                         doread = false;
831
832                         switch (c){
833                         case '{':
834                                 val = Location;
835                                 return Token.OPEN_BRACE;
836                         case '}':
837                                 val = Location;
838                                 return Token.CLOSE_BRACE;
839                         case '[':
840                                 // To block doccomment inside attribute declaration.
841                                 if (doc_state == XmlCommentState.Allowed)
842                                         doc_state = XmlCommentState.NotAllowed;
843                                 return Token.OPEN_BRACKET;
844                         case ']':
845                                 return Token.CLOSE_BRACKET;
846                         case '(':
847                                 if (linq){
848                                         PushPosition ();
849                                         bool have_lambda_parameter = parse_lambda_parameters ();
850                                         PopPosition ();
851                                         
852                                         if (have_lambda_parameter)
853                                                 return Token.OPEN_PARENS_LAMBDA;
854                                         else
855                                                 return Token.OPEN_PARENS;
856                                 } else
857                                         return Token.OPEN_PARENS;
858                         case ')': {
859                                 if (deambiguate_close_parens == 0)
860                                         return Token.CLOSE_PARENS;
861
862                                 --deambiguate_close_parens;
863
864                                 PushPosition ();
865
866                                 int new_token = xtoken ();
867
868                                 PopPosition ();
869
870                                 if (new_token == Token.OPEN_PARENS)
871                                         return Token.CLOSE_PARENS_OPEN_PARENS;
872                                 else if (new_token == Token.MINUS)
873                                         return Token.CLOSE_PARENS_MINUS;
874                                 else if (IsCastToken (new_token))
875                                         return Token.CLOSE_PARENS_CAST;
876                                 else
877                                         return Token.CLOSE_PARENS_NO_CAST;
878                         }
879
880                         case ',':
881                                 return Token.COMMA;
882                         case ';':
883                                 val = Location;
884                                 return Token.SEMICOLON;
885                         case '~':
886                                 val = Location;
887                                 return Token.TILDE;
888                         case '?':
889                                 return Token.INTERR;
890                         }
891 #if GMCS_SOURCE
892                         if (c == '<') {
893                                 if (parsing_generic_less_than++ > 0)
894                                         return Token.OP_GENERICS_LT;
895
896                                 if (handle_typeof) {
897                                         int dimension;
898                                         PushPosition ();
899                                         if (parse_generic_dimension (out dimension)) {
900                                                 val = dimension;
901                                                 DiscardPosition ();
902                                                 return Token.GENERIC_DIMENSION;
903                                         }
904                                         PopPosition ();
905                                 }
906
907                                 // Save current position and parse next token.
908                                 PushPosition ();
909                                 bool is_generic_lt = parse_less_than ();
910                                 PopPosition ();
911
912                                 if (is_generic_lt) {
913                                         return Token.OP_GENERICS_LT;
914                                 } else
915                                         parsing_generic_less_than = 0;
916
917                                 d = peekChar ();
918                                 if (d == '<'){
919                                         getChar ();
920                                         d = peekChar ();
921
922                                         if (d == '='){
923                                                 doread = true;
924                                                 return Token.OP_SHIFT_LEFT_ASSIGN;
925                                         }
926                                         return Token.OP_SHIFT_LEFT;
927                                 } else if (d == '='){
928                                         doread = true;
929                                         return Token.OP_LE;
930                                 }
931                                 return Token.OP_LT;
932                         } else if (c == '>') {
933                                 if (parsing_generic_less_than > 0) {
934                                         parsing_generic_less_than--;
935                                         return Token.OP_GENERICS_GT;
936                                 }
937
938                                 d = peekChar ();
939                                 if (d == '>'){
940                                         getChar ();
941                                         d = peekChar ();
942
943                                         if (d == '='){
944                                                 doread = true;
945                                                 return Token.OP_SHIFT_RIGHT_ASSIGN;
946                                         }
947                                         return Token.OP_SHIFT_RIGHT;
948                                 } else if (d == '='){
949                                         doread = true;
950                                         return Token.OP_GE;
951                                 }
952                                 return Token.OP_GT;
953                         }
954 #endif
955                         d = peekChar ();
956                         if (c == '+'){
957                                 
958                                 if (d == '+') {
959                                         val = Location;
960                                         t = Token.OP_INC;
961                                 }
962                                 else if (d == '=')
963                                         t = Token.OP_ADD_ASSIGN;
964                                 else {
965                                         val = Location;
966                                         return Token.PLUS;
967                                 }
968                                 doread = true;
969                                 return t;
970                         }
971                         if (c == '-'){
972                                 if (d == '-') {
973                                         val = Location;
974                                         t = Token.OP_DEC;
975                                 }
976                                 else if (d == '=')
977                                         t = Token.OP_SUB_ASSIGN;
978                                 else if (d == '>')
979                                         t = Token.OP_PTR;
980                                 else {
981                                         val = Location;
982                                         return Token.MINUS;
983                                 }
984                                 doread = true;
985                                 return t;
986                         }
987
988                         if (c == '!'){
989                                 if (d == '='){
990                                         doread = true;
991                                         return Token.OP_NE;
992                                 }
993                                 val = Location;
994                                 return Token.BANG;
995                         }
996
997                         if (c == '='){
998                                 if (d == '='){
999                                         doread = true;
1000                                         return Token.OP_EQ;
1001                                 }
1002                                 if (d == '>'){
1003                                         doread = true;
1004                                         val = Location;
1005                                         return Token.ARROW;
1006                                 }
1007                                 return Token.ASSIGN;
1008                         }
1009
1010                         if (c == '&'){
1011                                 if (d == '&'){
1012                                         doread = true;
1013                                         return Token.OP_AND;
1014                                 } else if (d == '='){
1015                                         doread = true;
1016                                         return Token.OP_AND_ASSIGN;
1017                                 }
1018                                 val = Location;
1019                                 return Token.BITWISE_AND;
1020                         }
1021
1022                         if (c == '|'){
1023                                 if (d == '|'){
1024                                         doread = true;
1025                                         return Token.OP_OR;
1026                                 } else if (d == '='){
1027                                         doread = true;
1028                                         return Token.OP_OR_ASSIGN;
1029                                 }
1030                                 return Token.BITWISE_OR;
1031                         }
1032
1033                         if (c == '*'){
1034                                 if (d == '='){
1035                                         doread = true;
1036                                         return Token.OP_MULT_ASSIGN;
1037                                 }
1038                                 val = Location;
1039                                 return Token.STAR;
1040                         }
1041
1042                         if (c == '/'){
1043                                 if (d == '='){
1044                                         doread = true;
1045                                         return Token.OP_DIV_ASSIGN;
1046                                 }
1047                                 return Token.DIV;
1048                         }
1049
1050                         if (c == '%'){
1051                                 if (d == '='){
1052                                         doread = true;
1053                                         return Token.OP_MOD_ASSIGN;
1054                                 }
1055                                 return Token.PERCENT;
1056                         }
1057
1058                         if (c == '^'){
1059                                 if (d == '='){
1060                                         doread = true;
1061                                         return Token.OP_XOR_ASSIGN;
1062                                 }
1063                                 return Token.CARRET;
1064                         }
1065
1066 #if !GMCS_SOURCE
1067                         if (c == '<'){
1068                                 if (d == '<'){
1069                                         getChar ();
1070                                         d = peekChar ();
1071
1072                                         if (d == '='){
1073                                                 doread = true;
1074                                                 return Token.OP_SHIFT_LEFT_ASSIGN;
1075                                         }
1076                                         return Token.OP_SHIFT_LEFT;
1077                                 } else if (d == '='){
1078                                         doread = true;
1079                                         return Token.OP_LE;
1080                                 }
1081                                 return Token.OP_LT;
1082                         }
1083
1084                         if (c == '>'){
1085                                 if (d == '>'){
1086                                         getChar ();
1087                                         d = peekChar ();
1088
1089                                         if (d == '='){
1090                                                 doread = true;
1091                                                 return Token.OP_SHIFT_RIGHT_ASSIGN;
1092                                         }
1093                                         return Token.OP_SHIFT_RIGHT;
1094                                 } else if (d == '='){
1095                                         doread = true;
1096                                         return Token.OP_GE;
1097                                 }
1098                                 return Token.OP_GT;
1099                         }
1100 #endif
1101                         if (c == ':'){
1102                                 if (d == ':'){
1103                                         doread = true;
1104                                         return Token.DOUBLE_COLON;
1105                                 }
1106                                 val = Location;
1107                                 return Token.COLON;
1108                         }
1109
1110                         return Token.ERROR;
1111                 }
1112
1113                 int deambiguate_close_parens = 0;
1114
1115                 public void Deambiguate_CloseParens (object expression)
1116                 {
1117                         putback (')');
1118
1119                         // When any binary operation is used we are sure it is not a cast
1120                         if (expression is Binary)
1121                                 return;
1122
1123                         deambiguate_close_parens++;
1124                 }
1125
1126                 bool decimal_digits (int c)
1127                 {
1128                         int d;
1129                         bool seen_digits = false;
1130                         
1131                         if (c != -1){
1132                                 if (number_pos == max_number_size)
1133                                         Error_NumericConstantTooLong ();
1134                                 number_builder [number_pos++] = (char) c;
1135                         }
1136                         
1137                         //
1138                         // We use peekChar2, because decimal_digits needs to do a 
1139                         // 2-character look-ahead (5.ToString for example).
1140                         //
1141                         while ((d = peekChar2 ()) != -1){
1142                                 if (d >= '0' && d <= '9'){
1143                                         if (number_pos == max_number_size)
1144                                                 Error_NumericConstantTooLong ();
1145                                         number_builder [number_pos++] = (char) d;
1146                                         getChar ();
1147                                         seen_digits = true;
1148                                 } else
1149                                         break;
1150                         }
1151                         
1152                         return seen_digits;
1153                 }
1154
1155                 static bool is_hex (int e)
1156                 {
1157                         return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
1158                 }
1159                                 
1160                 static int real_type_suffix (int c)
1161                 {
1162                         int t;
1163
1164                         switch (c){
1165                         case 'F': case 'f':
1166                                 t =  Token.LITERAL_FLOAT;
1167                                 break;
1168                         case 'D': case 'd':
1169                                 t = Token.LITERAL_DOUBLE;
1170                                 break;
1171                         case 'M': case 'm':
1172                                  t= Token.LITERAL_DECIMAL;
1173                                 break;
1174                         default:
1175                                 return Token.NONE;
1176                         }
1177                         return t;
1178                 }
1179
1180                 int integer_type_suffix (ulong ul, int c)
1181                 {
1182                         bool is_unsigned = false;
1183                         bool is_long = false;
1184
1185                         if (c != -1){
1186                                 bool scanning = true;
1187                                 do {
1188                                         switch (c){
1189                                         case 'U': case 'u':
1190                                                 if (is_unsigned)
1191                                                         scanning = false;
1192                                                 is_unsigned = true;
1193                                                 getChar ();
1194                                                 break;
1195
1196                                         case 'l':
1197                                                 if (!is_unsigned && (RootContext.WarningLevel >= 4)){
1198                                                         //
1199                                                         // if we have not seen anything in between
1200                                                         // report this error
1201                                                         //
1202                                                         Report.Warning (78, 4, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
1203                                                 }
1204                                                 //
1205                                                 // This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
1206                                                 // work around that.
1207                                                 //
1208                                                 //goto case 'L';
1209                                                 if (is_long)
1210                                                         scanning = false;
1211                                                 is_long = true;
1212                                                 getChar ();
1213                                                 break;
1214
1215                                         case 'L': 
1216                                                 if (is_long)
1217                                                         scanning = false;
1218                                                 is_long = true;
1219                                                 getChar ();
1220                                                 break;
1221                                                 
1222                                         default:
1223                                                 scanning = false;
1224                                                 break;
1225                                         }
1226                                         c = peekChar ();
1227                                 } while (scanning);
1228                         }
1229
1230                         if (is_long && is_unsigned){
1231                                 val = ul;
1232                                 return Token.LITERAL_INTEGER;
1233                         } else if (is_unsigned){
1234                                 // uint if possible, or ulong else.
1235
1236                                 if ((ul & 0xffffffff00000000) == 0)
1237                                         val = (uint) ul;
1238                                 else
1239                                         val = ul;
1240                         } else if (is_long){
1241                                 // long if possible, ulong otherwise
1242                                 if ((ul & 0x8000000000000000) != 0)
1243                                         val = ul;
1244                                 else
1245                                         val = (long) ul;
1246                         } else {
1247                                 // int, uint, long or ulong in that order
1248                                 if ((ul & 0xffffffff00000000) == 0){
1249                                         uint ui = (uint) ul;
1250                                         
1251                                         if ((ui & 0x80000000) != 0)
1252                                                 val = ui;
1253                                         else
1254                                                 val = (int) ui;
1255                                 } else {
1256                                         if ((ul & 0x8000000000000000) != 0)
1257                                                 val = ul;
1258                                         else
1259                                                 val = (long) ul;
1260                                 }
1261                         }
1262                         return Token.LITERAL_INTEGER;
1263                 }
1264                                 
1265                 //
1266                 // given `c' as the next char in the input decide whether
1267                 // we need to convert to a special type, and then choose
1268                 // the best representation for the integer
1269                 //
1270                 int adjust_int (int c)
1271                 {
1272                         try {
1273                                 if (number_pos > 9){
1274                                         ulong ul = (uint) (number_builder [0] - '0');
1275
1276                                         for (int i = 1; i < number_pos; i++){
1277                                                 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
1278                                         }
1279                                         return integer_type_suffix (ul, c);
1280                                 } else {
1281                                         uint ui = (uint) (number_builder [0] - '0');
1282
1283                                         for (int i = 1; i < number_pos; i++){
1284                                                 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
1285                                         }
1286                                         return integer_type_suffix (ui, c);
1287                                 }
1288                         } catch (OverflowException) {
1289                                 error_details = "Integral constant is too large";
1290                                 Report.Error (1021, Location, error_details);
1291                                 val = 0ul;
1292                                 return Token.LITERAL_INTEGER;
1293                         }
1294                         catch (FormatException) {
1295                                 Report.Error (1013, Location, "Invalid number");
1296                                 val = 0ul;
1297                                 return Token.LITERAL_INTEGER;
1298                         }
1299                 }
1300                 
1301                 int adjust_real (int t)
1302                 {
1303                         string s = new String (number_builder, 0, number_pos);
1304                         const string error_details = "Floating-point constant is outside the range of type `{0}'";
1305
1306                         switch (t){
1307                         case Token.LITERAL_DECIMAL:
1308                                 try {
1309                                         val = System.Decimal.Parse (s, styles, csharp_format_info);
1310                                 } catch (OverflowException) {
1311                                         val = 0m;     
1312                                         Report.Error (594, Location, error_details, "decimal");
1313                                 }
1314                                 break;
1315                         case Token.LITERAL_FLOAT:
1316                                 try {
1317                                         val = float.Parse (s, styles, csharp_format_info);
1318                                 } catch (OverflowException) {
1319                                         val = 0.0f;     
1320                                         Report.Error (594, Location, error_details, "float");
1321                                 }
1322                                 break;
1323                                 
1324                         case Token.LITERAL_DOUBLE:
1325                         case Token.NONE:
1326                                 t = Token.LITERAL_DOUBLE;
1327                                 try {
1328                                         val = System.Double.Parse (s, styles, csharp_format_info);
1329                                 } catch (OverflowException) {
1330                                         val = 0.0;     
1331                                         Report.Error (594, Location, error_details, "double");
1332                                 }
1333                                 break;
1334                         }
1335                         return t;
1336                 }
1337
1338                 int handle_hex ()
1339                 {
1340                         int d;
1341                         ulong ul;
1342                         
1343                         getChar ();
1344                         while ((d = peekChar ()) != -1){
1345                                 if (is_hex (d)){
1346                                         number_builder [number_pos++] = (char) d;
1347                                         getChar ();
1348                                 } else
1349                                         break;
1350                         }
1351                         
1352                         string s = new String (number_builder, 0, number_pos);
1353                         try {
1354                                 if (number_pos <= 8)
1355                                         ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
1356                                 else
1357                                         ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
1358                         } catch (OverflowException){
1359                                 error_details = "Integral constant is too large";
1360                                 Report.Error (1021, Location, error_details);
1361                                 val = 0ul;
1362                                 return Token.LITERAL_INTEGER;
1363                         }
1364                         catch (FormatException) {
1365                                 Report.Error (1013, Location, "Invalid number");
1366                                 val = 0ul;
1367                                 return Token.LITERAL_INTEGER;
1368                         }
1369                         
1370                         return integer_type_suffix (ul, peekChar ());
1371                 }
1372
1373                 //
1374                 // Invoked if we know we have .digits or digits
1375                 //
1376                 int is_number (int c)
1377                 {
1378                         bool is_real = false;
1379                         int type;
1380
1381                         number_pos = 0;
1382
1383                         if (c >= '0' && c <= '9'){
1384                                 if (c == '0'){
1385                                         int peek = peekChar ();
1386
1387                                         if (peek == 'x' || peek == 'X')
1388                                                 return handle_hex ();
1389                                 }
1390                                 decimal_digits (c);
1391                                 c = getChar ();
1392                         }
1393
1394                         //
1395                         // We need to handle the case of
1396                         // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
1397                         //
1398                         if (c == '.'){
1399                                 if (decimal_digits ('.')){
1400                                         is_real = true;
1401                                         c = getChar ();
1402                                 } else {
1403                                         putback ('.');
1404                                         number_pos--;
1405                                         return adjust_int (-1);
1406                                 }
1407                         }
1408                         
1409                         if (c == 'e' || c == 'E'){
1410                                 is_real = true;
1411                                 if (number_pos == max_number_size)
1412                                         Error_NumericConstantTooLong ();
1413                                 number_builder [number_pos++] = 'e';
1414                                 c = getChar ();
1415                                 
1416                                 if (c == '+'){
1417                                         if (number_pos == max_number_size)
1418                                                 Error_NumericConstantTooLong ();
1419                                         number_builder [number_pos++] = '+';
1420                                         c = -1;
1421                                 } else if (c == '-') {
1422                                         if (number_pos == max_number_size)
1423                                                 Error_NumericConstantTooLong ();
1424                                         number_builder [number_pos++] = '-';
1425                                         c = -1;
1426                                 } else {
1427                                         if (number_pos == max_number_size)
1428                                                 Error_NumericConstantTooLong ();
1429                                         number_builder [number_pos++] = '+';
1430                                 }
1431                                         
1432                                 decimal_digits (c);
1433                                 c = getChar ();
1434                         }
1435
1436                         type = real_type_suffix (c);
1437                         if (type == Token.NONE && !is_real){
1438                                 putback (c);
1439                                 return adjust_int (c);
1440                         } else 
1441                                 is_real = true;
1442
1443                         if (type == Token.NONE){
1444                                 putback (c);
1445                         }
1446                         
1447                         if (is_real)
1448                                 return adjust_real (type);
1449
1450                         Console.WriteLine ("This should not be reached");
1451                         throw new Exception ("Is Number should never reach this point");
1452                 }
1453
1454                 //
1455                 // Accepts exactly count (4 or 8) hex, no more no less
1456                 //
1457                 int getHex (int count, out bool error)
1458                 {
1459                         int i;
1460                         int total = 0;
1461                         int c;
1462                         int top = count != -1 ? count : 4;
1463                         
1464                         getChar ();
1465                         error = false;
1466                         for (i = 0; i < top; i++){
1467                                 c = getChar ();
1468                                 
1469                                 if (c >= '0' && c <= '9')
1470                                         c = (int) c - (int) '0';
1471                                 else if (c >= 'A' && c <= 'F')
1472                                         c = (int) c - (int) 'A' + 10;
1473                                 else if (c >= 'a' && c <= 'f')
1474                                         c = (int) c - (int) 'a' + 10;
1475                                 else {
1476                                         error = true;
1477                                         return 0;
1478                                 }
1479                                 
1480                                 total = (total * 16) + c;
1481                                 if (count == -1){
1482                                         int p = peekChar ();
1483                                         if (p == -1)
1484                                                 break;
1485                                         if (!is_hex ((char)p))
1486                                                 break;
1487                                 }
1488                         }
1489                         return total;
1490                 }
1491
1492                 int escape (int c)
1493                 {
1494                         bool error;
1495                         int d;
1496                         int v;
1497
1498                         d = peekChar ();
1499                         if (c != '\\')
1500                                 return c;
1501                         
1502                         switch (d){
1503                         case 'a':
1504                                 v = '\a'; break;
1505                         case 'b':
1506                                 v = '\b'; break;
1507                         case 'n':
1508                                 v = '\n'; break;
1509                         case 't':
1510                                 v = '\t'; break;
1511                         case 'v':
1512                                 v = '\v'; break;
1513                         case 'r':
1514                                 v = '\r'; break;
1515                         case '\\':
1516                                 v = '\\'; break;
1517                         case 'f':
1518                                 v = '\f'; break;
1519                         case '0':
1520                                 v = 0; break;
1521                         case '"':
1522                                 v = '"'; break;
1523                         case '\'':
1524                                 v = '\''; break;
1525                         case 'x':
1526                                 v = getHex (-1, out error);
1527                                 if (error)
1528                                         goto default;
1529                                 return v;
1530                         case 'u':
1531                                 v = getHex (4, out error);
1532                                 if (error)
1533                                         goto default;
1534                                 return v;
1535                         case 'U':
1536                                 v = getHex (8, out error);
1537                                 if (error)
1538                                         goto default;
1539                                 return v;
1540                         default:
1541                                 Report.Error (1009, Location, "Unrecognized escape sequence `\\{0}'", ((char)d).ToString ());
1542                                 return d;
1543                         }
1544                         getChar ();
1545                         return v;
1546                 }
1547
1548                 int getChar ()
1549                 {
1550                         int x;
1551                         if (putback_char != -1) {
1552                                 x = putback_char;
1553                                 putback_char = -1;
1554                         } else
1555                                 x = reader.Read ();
1556                         if (x == '\n') {
1557                                 line++;
1558                                 ref_line++;
1559                                 previous_col = col;
1560                                 col = 0;
1561                         }
1562                         else
1563                                 col++;
1564                         return x;
1565                 }
1566
1567                 int peekChar ()
1568                 {
1569                         if (putback_char != -1)
1570                                 return putback_char;
1571                         putback_char = reader.Read ();
1572                         return putback_char;
1573                 }
1574
1575                 int peekChar2 ()
1576                 {
1577                         if (putback_char != -1)
1578                                 return putback_char;
1579                         return reader.Peek ();
1580                 }
1581                 
1582                 void putback (int c)
1583                 {
1584                         if (putback_char != -1){
1585                                 Console.WriteLine ("Col: " + col);
1586                                 Console.WriteLine ("Row: " + line);
1587                                 Console.WriteLine ("Name: " + ref_name.Name);
1588                                 Console.WriteLine ("Current [{0}] putting back [{1}]  ", putback_char, c);
1589                                 throw new Exception ("This should not happen putback on putback");
1590                         }
1591                         if (c == '\n' || col == 0) {
1592                                 // It won't happen though.
1593                                 line--;
1594                                 ref_line--;
1595                                 col = previous_col;
1596                         }
1597                         else
1598                                 col--;
1599                         putback_char = c;
1600                 }
1601
1602                 public bool advance ()
1603                 {
1604                         return peekChar () != -1;
1605                 }
1606
1607                 public Object Value {
1608                         get {
1609                                 return val;
1610                         }
1611                 }
1612
1613                 public Object value ()
1614                 {
1615                         return val;
1616                 }
1617
1618                 static bool IsCastToken (int token)
1619                 {
1620                         switch (token) {
1621                         case Token.BANG:
1622                         case Token.TILDE:
1623                         case Token.IDENTIFIER:
1624                         case Token.LITERAL_INTEGER:
1625                         case Token.LITERAL_FLOAT:
1626                         case Token.LITERAL_DOUBLE:
1627                         case Token.LITERAL_DECIMAL:
1628                         case Token.LITERAL_CHARACTER:
1629                         case Token.LITERAL_STRING:
1630                         case Token.BASE:
1631                         case Token.CHECKED:
1632                         case Token.DELEGATE:
1633                         case Token.FALSE:
1634                         case Token.FIXED:
1635                         case Token.NEW:
1636                         case Token.NULL:
1637                         case Token.SIZEOF:
1638                         case Token.THIS:
1639                         case Token.THROW:
1640                         case Token.TRUE:
1641                         case Token.TYPEOF:
1642                         case Token.UNCHECKED:
1643                         case Token.UNSAFE:
1644 #if GMCS_SOURCE
1645                         case Token.DEFAULT:
1646 #endif
1647
1648                                 //
1649                                 // These can be part of a member access
1650                                 //
1651                         case Token.INT:
1652                         case Token.UINT:
1653                         case Token.SHORT:
1654                         case Token.USHORT:
1655                         case Token.LONG:
1656                         case Token.ULONG:
1657                         case Token.DOUBLE:
1658                         case Token.FLOAT:
1659                         case Token.CHAR:
1660                                 return true;
1661
1662                         default:
1663                                 return false;
1664                         }
1665                 }
1666
1667                 public int token ()
1668                 {
1669                         current_token = xtoken ();
1670
1671 #if GMCS_SOURCE
1672                         if (current_token != Token.DEFAULT)
1673                                 return current_token;
1674
1675                         PushPosition();
1676                         int c = xtoken();
1677                         if (c == -1)
1678                                 current_token = Token.ERROR;
1679                         else if (c == Token.OPEN_PARENS)
1680                                 current_token = Token.DEFAULT_OPEN_PARENS;
1681                         else if (c == Token.COLON)
1682                                 current_token = Token.DEFAULT_COLON;
1683                         else
1684                                 PopPosition();
1685 #endif
1686                         return current_token;
1687                 }
1688
1689                 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
1690                 
1691                 void get_cmd_arg (out string cmd, out string arg)
1692                 {
1693                         int c;
1694                         
1695                         tokens_seen = false;
1696                         arg = "";
1697                         static_cmd_arg.Length = 0;
1698
1699                         // skip over white space
1700                         while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1701                                 ;
1702                                 
1703                         while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
1704                                 if (is_identifier_part_character ((char) c)){
1705                                         static_cmd_arg.Append ((char) c);
1706                                         c = getChar ();
1707                                 } else {
1708                                         putback (c);
1709                                         break;
1710                                 }
1711                         }
1712
1713                         cmd = static_cmd_arg.ToString ();
1714
1715                         if (c == '\n' || c == '\r'){
1716                                 return;
1717                         }
1718
1719                         // skip over white space
1720                         while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1721                                 ;
1722
1723                         if (c == '\n'){
1724                                 return;
1725                         } else if (c == '\r'){
1726                                 return;
1727                         } else if (c == -1){
1728                                 arg = "";
1729                                 return;
1730                         }
1731                         
1732                         static_cmd_arg.Length = 0;
1733                         static_cmd_arg.Append ((char) c);
1734                         
1735                         while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
1736                                 static_cmd_arg.Append ((char) c);
1737                         }
1738
1739                         arg = static_cmd_arg.ToString ();
1740                 }
1741
1742                 //
1743                 // Handles the #line directive
1744                 //
1745                 bool PreProcessLine (string arg)
1746                 {
1747                         if (arg.Length == 0)
1748                                 return false;
1749
1750                         if (arg == "default"){
1751                                 ref_line = line;
1752                                 ref_name = file_name;
1753                                 Location.Push (ref_name);
1754                                 return true;
1755                         } else if (arg == "hidden"){
1756                                 //
1757                                 // We ignore #line hidden
1758                                 //
1759                                 return true;
1760                         }
1761                         
1762                         try {
1763                                 int pos;
1764
1765                                 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
1766                                         ref_line = System.Int32.Parse (arg.Substring (0, pos));
1767                                         pos++;
1768                                         
1769                                         char [] quotes = { '\"' };
1770                                         
1771                                         string name = arg.Substring (pos). Trim (quotes);
1772                                         ref_name = Location.LookupFile (name);
1773                                         file_name.HasLineDirective = true;
1774                                         ref_name.HasLineDirective = true;
1775                                         Location.Push (ref_name);
1776                                 } else {
1777                                         ref_line = System.Int32.Parse (arg);
1778                                 }
1779                         } catch {
1780                                 return false;
1781                         }
1782                         
1783                         return true;
1784                 }
1785
1786                 //
1787                 // Handles #define and #undef
1788                 //
1789                 void PreProcessDefinition (bool is_define, string arg, bool caller_is_taking)
1790                 {
1791                         if (arg.Length == 0 || arg == "true" || arg == "false"){
1792                                 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
1793                                 return;
1794                         }
1795
1796                         if (arg.IndexOfAny (simple_whitespaces) != -1){
1797                                 Error_EndLineExpected ();
1798                                 return;
1799                         }
1800
1801                         if (!is_identifier_start_character (arg [0]))
1802                                 Report.Error (1001, Location, "Identifier expected: " + arg);
1803                         
1804                         foreach (char c in arg.Substring (1)){
1805                                 if (!is_identifier_part_character (c)){
1806                                         Report.Error (1001, Location, "Identifier expected: " + arg);
1807                                         return;
1808                                 }
1809                         }
1810
1811                         if (!caller_is_taking)
1812                                 return;
1813
1814                         if (is_define){
1815                                 if (defines == null)
1816                                         defines = new Hashtable ();
1817                                 define (arg);
1818                         } else {
1819                                 if (defines == null)
1820                                         return;
1821                                 if (defines.Contains (arg))
1822                                         defines.Remove (arg);
1823                         }
1824                 }
1825
1826                 /// <summary>
1827                 /// Handles #pragma directive
1828                 /// </summary>
1829                 void PreProcessPragma (string arg)
1830                 {
1831                         const string warning = "warning";
1832                         const string w_disable = "warning disable";
1833                         const string w_restore = "warning restore";
1834
1835                         if (arg == w_disable) {
1836                                 Report.RegisterWarningRegion (Location).WarningDisable (line);
1837                                 return;
1838                         }
1839
1840                         if (arg == w_restore) {
1841                                 Report.RegisterWarningRegion (Location).WarningEnable (line);
1842                                 return;
1843                         }
1844
1845                         if (arg.StartsWith (w_disable)) {
1846                                 int[] codes = ParseNumbers (arg.Substring (w_disable.Length));
1847                                 foreach (int code in codes) {
1848                                         if (code != 0)
1849                                                 Report.RegisterWarningRegion (Location).WarningDisable (Location, code);
1850                                 }
1851                                 return;
1852                         }
1853
1854                         if (arg.StartsWith (w_restore)) {
1855                                 int[] codes = ParseNumbers (arg.Substring (w_restore.Length));
1856                                 Hashtable w_table = Report.warning_ignore_table;
1857                                 foreach (int code in codes) {
1858                                         if (w_table != null && w_table.Contains (code))
1859                                                 Report.Warning (1635, 1, Location, String.Format ("Cannot restore warning `CS{0:0000}' because it was disabled globally", code));
1860                                         Report.RegisterWarningRegion (Location).WarningEnable (Location, code);
1861                                 }
1862                                 return;
1863                         }
1864
1865                         if (arg.StartsWith (warning)) {
1866                                 Report.Warning (1634, 1, Location, "Expected disable or restore");
1867                                 return;
1868                         }
1869
1870                         Report.Warning (1633, 1, Location, "Unrecognized #pragma directive");
1871                 }
1872
1873                 int[] ParseNumbers (string text)
1874                 {
1875                         string[] string_array = text.Split (',');
1876                         int[] values = new int [string_array.Length];
1877                         int index = 0;
1878                         foreach (string string_code in string_array) {
1879                                 try {
1880                                         values[index++] = int.Parse (string_code, System.Globalization.CultureInfo.InvariantCulture);
1881                                 }
1882                                 catch (FormatException) {
1883                                         Report.Warning (1692, 1, Location, "Invalid number");
1884                                 }
1885                         }
1886                         return values;
1887                 }
1888
1889                 bool eval_val (string s)
1890                 {
1891                         if (s == "true")
1892                                 return true;
1893                         if (s == "false")
1894                                 return false;
1895                         
1896                         if (defines == null)
1897                                 return false;
1898                         if (defines.Contains (s))
1899                                 return true;
1900
1901                         return false;
1902                 }
1903
1904                 bool pp_primary (ref string s)
1905                 {
1906                         s = s.Trim ();
1907                         int len = s.Length;
1908
1909                         if (len > 0){
1910                                 char c = s [0];
1911                                 
1912                                 if (c == '('){
1913                                         s = s.Substring (1);
1914                                         bool val = pp_expr (ref s, false);
1915                                         if (s.Length > 0 && s [0] == ')'){
1916                                                 s = s.Substring (1);
1917                                                 return val;
1918                                         }
1919                                         Error_InvalidDirective ();
1920                                         return false;
1921                                 }
1922                                 
1923                                 if (is_identifier_start_character (c)){
1924                                         int j = 1;
1925
1926                                         while (j < len){
1927                                                 c = s [j];
1928                                                 
1929                                                 if (is_identifier_part_character (c)){
1930                                                         j++;
1931                                                         continue;
1932                                                 }
1933                                                 bool v = eval_val (s.Substring (0, j));
1934                                                 s = s.Substring (j);
1935                                                 return v;
1936                                         }
1937                                         bool vv = eval_val (s);
1938                                         s = "";
1939                                         return vv;
1940                                 }
1941                         }
1942                         Error_InvalidDirective ();
1943                         return false;
1944                 }
1945                 
1946                 bool pp_unary (ref string s)
1947                 {
1948                         s = s.Trim ();
1949                         int len = s.Length;
1950
1951                         if (len > 0){
1952                                 if (s [0] == '!'){
1953                                         if (len > 1 && s [1] == '='){
1954                                                 Error_InvalidDirective ();
1955                                                 return false;
1956                                         }
1957                                         s = s.Substring (1);
1958                                         return ! pp_primary (ref s);
1959                                 } else
1960                                         return pp_primary (ref s);
1961                         } else {
1962                                 Error_InvalidDirective ();
1963                                 return false;
1964                         }
1965                 }
1966                 
1967                 bool pp_eq (ref string s)
1968                 {
1969                         bool va = pp_unary (ref s);
1970
1971                         s = s.Trim ();
1972                         int len = s.Length;
1973                         if (len > 0){
1974                                 if (s [0] == '='){
1975                                         if (len > 2 && s [1] == '='){
1976                                                 s = s.Substring (2);
1977                                                 return va == pp_unary (ref s);
1978                                         } else {
1979                                                 Error_InvalidDirective ();
1980                                                 return false;
1981                                         }
1982                                 } else if (s [0] == '!' && len > 1 && s [1] == '='){
1983                                         s = s.Substring (2);
1984
1985                                         return va != pp_unary (ref s);
1986
1987                                 } 
1988                         }
1989
1990                         return va;
1991                                 
1992                 }
1993                 
1994                 bool pp_and (ref string s)
1995                 {
1996                         bool va = pp_eq (ref s);
1997
1998                         s = s.Trim ();
1999                         int len = s.Length;
2000                         if (len > 0){
2001                                 if (s [0] == '&'){
2002                                         if (len > 2 && s [1] == '&'){
2003                                                 s = s.Substring (2);
2004                                                 return (va & pp_and (ref s));
2005                                         } else {
2006                                                 Error_InvalidDirective ();
2007                                                 return false;
2008                                         }
2009                                 } 
2010                         }
2011                         return va;
2012                 }
2013                 
2014                 //
2015                 // Evaluates an expression for `#if' or `#elif'
2016                 //
2017                 bool pp_expr (ref string s, bool isTerm)
2018                 {
2019                         bool va = pp_and (ref s);
2020                         s = s.Trim ();
2021                         int len = s.Length;
2022                         if (len > 0){
2023                                 char c = s [0];
2024                                 
2025                                 if (c == '|'){
2026                                         if (len > 2 && s [1] == '|'){
2027                                                 s = s.Substring (2);
2028                                                 return va | pp_expr (ref s, isTerm);
2029                                         } else {
2030                                                 Error_InvalidDirective ();
2031                                                 return false;
2032                                         }
2033                                 }
2034                                 if (isTerm) {
2035                                         Error_EndLineExpected ();
2036                                         return false;
2037                                 }
2038                         }
2039                         
2040                         return va;
2041                 }
2042
2043                 bool eval (string s)
2044                 {
2045                         bool v = pp_expr (ref s, true);
2046                         s = s.Trim ();
2047                         if (s.Length != 0){
2048                                 return false;
2049                         }
2050
2051                         return v;
2052                 }
2053
2054                 void Error_NumericConstantTooLong ()
2055                 {
2056                         Report.Error (1021, Location, "Numeric constant too long");                     
2057                 }
2058                 
2059                 void Error_InvalidDirective ()
2060                 {
2061                         Report.Error (1517, Location, "Invalid preprocessor directive");
2062                 }
2063
2064                 void Error_UnexpectedDirective (string extra)
2065                 {
2066                         Report.Error (
2067                                 1028, Location,
2068                                 "Unexpected processor directive (" + extra + ")");
2069                 }
2070
2071                 void Error_TokensSeen ()
2072                 {
2073                         Report.Error (1032, Location,
2074                                 "Cannot define or undefine preprocessor symbols after first token in file");
2075                 }
2076
2077                 void Eror_WrongPreprocessorLocation ()
2078                 {
2079                         Report.Error (1040, Location,
2080                                 "Preprocessor directives must appear as the first non-whitespace character on a line");
2081                 }
2082
2083                 void Error_EndLineExpected ()
2084                 {
2085                         Report.Error (1025, Location, "Single-line comment or end-of-line expected");
2086                 }
2087                 
2088                 //
2089                 // if true, then the code continues processing the code
2090                 // if false, the code stays in a loop until another directive is
2091                 // reached.
2092                 // When caller_is_taking is false we ignore all directives except the ones
2093                 // which can help us to identify where the #if block ends
2094                 bool handle_preprocessing_directive (bool caller_is_taking)
2095                 {
2096                         string cmd, arg;
2097                         bool region_directive = false;
2098
2099                         get_cmd_arg (out cmd, out arg);
2100
2101                         // Eat any trailing whitespaces and single-line comments
2102                         if (arg.IndexOf ("//") != -1)
2103                                 arg = arg.Substring (0, arg.IndexOf ("//"));
2104                         arg = arg.Trim (simple_whitespaces);
2105
2106                         //
2107                         // The first group of pre-processing instructions is always processed
2108                         //
2109                         switch (cmd){
2110                         case "region":
2111                                 region_directive = true;
2112                                 arg = "true";
2113                                 goto case "if";
2114
2115                         case "endregion":
2116                                 if (ifstack == null || ifstack.Count == 0){
2117                                         Error_UnexpectedDirective ("no #region for this #endregion");
2118                                         return true;
2119                                 }
2120                                 int pop = (int) ifstack.Pop ();
2121                                         
2122                                 if ((pop & REGION) == 0)
2123                                         Report.Error (1027, Location, "Expected `#endif' directive");
2124                                         
2125                                 return caller_is_taking;
2126                                 
2127                         case "if":
2128                                 if (ifstack == null)
2129                                         ifstack = new Stack (2);
2130
2131                                 int flags = region_directive ? REGION : 0;
2132                                 if (ifstack.Count == 0){
2133                                         flags |= PARENT_TAKING;
2134                                 } else {
2135                                         int state = (int) ifstack.Peek ();
2136                                         if ((state & TAKING) != 0) {
2137                                                 flags |= PARENT_TAKING;
2138                                         }
2139                                 }
2140
2141                                 if (caller_is_taking && eval (arg)) {
2142                                         ifstack.Push (flags | TAKING);
2143                                         return true;
2144                                 }
2145                                 ifstack.Push (flags);
2146                                 return false;
2147                                 
2148                         case "endif":
2149                                 if (ifstack == null || ifstack.Count == 0){
2150                                         Error_UnexpectedDirective ("no #if for this #endif");
2151                                         return true;
2152                                 } else {
2153                                         pop = (int) ifstack.Pop ();
2154                                         
2155                                         if ((pop & REGION) != 0)
2156                                                 Report.Error (1038, Location, "#endregion directive expected");
2157                                         
2158                                         if (arg.Length != 0) {
2159                                                 Error_EndLineExpected ();
2160                                         }
2161                                         
2162                                         if (ifstack.Count == 0)
2163                                                 return true;
2164
2165                                         int state = (int) ifstack.Peek ();
2166                                         return (state & TAKING) != 0;
2167                                 }
2168
2169                         case "elif":
2170                                 if (ifstack == null || ifstack.Count == 0){
2171                                         Error_UnexpectedDirective ("no #if for this #elif");
2172                                         return true;
2173                                 } else {
2174                                         int state = (int) ifstack.Pop ();
2175
2176                                         if ((state & REGION) != 0) {
2177                                                 Report.Error (1038, Location, "#endregion directive expected");
2178                                                 return true;
2179                                         }
2180
2181                                         if ((state & ELSE_SEEN) != 0){
2182                                                 Error_UnexpectedDirective ("#elif not valid after #else");
2183                                                 return true;
2184                                         }
2185
2186                                         if ((state & TAKING) != 0) {
2187                                                 ifstack.Push (0);
2188                                                 return false;
2189                                         }
2190
2191                                         if (eval (arg) && ((state & PARENT_TAKING) != 0)){
2192                                                 ifstack.Push (state | TAKING);
2193                                                 return true;
2194                                         }
2195
2196                                         ifstack.Push (state);
2197                                         return false;
2198                                 }
2199
2200                         case "else":
2201                                 if (ifstack == null || ifstack.Count == 0){
2202                                         Error_UnexpectedDirective ("no #if for this #else");
2203                                         return true;
2204                                 } else {
2205                                         int state = (int) ifstack.Peek ();
2206
2207                                         if ((state & REGION) != 0) {
2208                                                 Report.Error (1038, Location, "#endregion directive expected");
2209                                                 return true;
2210                                         }
2211
2212                                         if ((state & ELSE_SEEN) != 0){
2213                                                 Error_UnexpectedDirective ("#else within #else");
2214                                                 return true;
2215                                         }
2216
2217                                         ifstack.Pop ();
2218
2219                                         if (arg.Length != 0) {
2220                                                 Error_EndLineExpected ();
2221                                                 return true;
2222                                         }
2223
2224                                         bool ret = false;
2225                                         if ((state & PARENT_TAKING) != 0) {
2226                                                 ret = (state & TAKING) == 0;
2227                                         
2228                                                 if (ret)
2229                                                         state |= TAKING;
2230                                                 else
2231                                                         state &= ~TAKING;
2232                                         }
2233         
2234                                         ifstack.Push (state | ELSE_SEEN);
2235                                         
2236                                         return ret;
2237                                 }
2238                                 case "define":
2239                                         if (any_token_seen){
2240                                                 Error_TokensSeen ();
2241                                                 return caller_is_taking;
2242                                         }
2243                                         PreProcessDefinition (true, arg, caller_is_taking);
2244                                         return caller_is_taking;
2245
2246                                 case "undef":
2247                                         if (any_token_seen){
2248                                                 Error_TokensSeen ();
2249                                                 return caller_is_taking;
2250                                         }
2251                                         PreProcessDefinition (false, arg, caller_is_taking);
2252                                         return caller_is_taking;
2253                         }
2254
2255                         //
2256                         // These are only processed if we are in a `taking' block
2257                         //
2258                         if (!caller_is_taking)
2259                                 return false;
2260                                         
2261                         switch (cmd){
2262                         case "error":
2263                                 Report.Error (1029, Location, "#error: '" + arg + "'");
2264                                 return true;
2265
2266                         case "warning":
2267                                 Report.Warning (1030, 1, Location, "#warning: `{0}'", arg);
2268                                 return true;
2269
2270                         case "pragma":
2271                                 if (RootContext.Version == LanguageVersion.ISO_1) {
2272                                         Report.FeatureIsNotISO1 (Location, "#pragma");
2273                                         return true;
2274                                 }
2275
2276                                 PreProcessPragma (arg);
2277                                 return true;
2278
2279                         case "line":
2280                                 if (!PreProcessLine (arg))
2281                                         Report.Error (
2282                                                 1576, Location,
2283                                                 "The line number specified for #line directive is missing or invalid");
2284                                 return caller_is_taking;
2285                         }
2286
2287                         Report.Error (1024, Location, "Wrong preprocessor directive");
2288                         return true;
2289
2290                 }
2291
2292                 private int consume_string (bool quoted)
2293                 {
2294                         int c;
2295                         string_builder.Length = 0;
2296                                                                 
2297                         while ((c = getChar ()) != -1){
2298                                 if (c == '"'){
2299                                         if (quoted && peekChar () == '"'){
2300                                                 string_builder.Append ((char) c);
2301                                                 getChar ();
2302                                                 continue;
2303                                         } else {
2304                                                 val = string_builder.ToString ();
2305                                                 return Token.LITERAL_STRING;
2306                                         }
2307                                 }
2308
2309                                 if (c == '\n'){
2310                                         if (!quoted)
2311                                                 Report.Error (1010, Location, "Newline in constant");
2312                                 }
2313
2314                                 if (!quoted){
2315                                         c = escape (c);
2316                                         if (c == -1)
2317                                                 return Token.ERROR;
2318                                 }
2319                                 string_builder.Append ((char) c);
2320                         }
2321
2322                         Report.Error (1039, Location, "Unterminated string literal");
2323                         return Token.EOF;
2324                 }
2325
2326                 private int consume_identifier (int s)
2327                 {
2328                         int res = consume_identifier (s, false);
2329
2330                         if (doc_state == XmlCommentState.Allowed)
2331                                 doc_state = XmlCommentState.NotAllowed;
2332                         switch (res) {
2333                         case Token.USING:
2334                         case Token.NAMESPACE:
2335                                 check_incorrect_doc_comment ();
2336                                 break;
2337                         }
2338
2339                         if (res == Token.PARTIAL) {
2340                                 // Save current position and parse next token.
2341                                 PushPosition ();
2342
2343                                 int next_token = token ();
2344                                 bool ok = (next_token == Token.CLASS) ||
2345                                         (next_token == Token.STRUCT) ||
2346                                         (next_token == Token.INTERFACE);
2347
2348                                 PopPosition ();
2349
2350                                 if (ok)
2351                                         return res;
2352
2353                                 if (next_token < Token.LAST_KEYWORD)
2354                                         Report.Error (267, Location, "The `partial' modifier can be used only immediately before keyword `class', `struct', or `interface'");
2355
2356                                 val = new LocatedToken (Location, "partial");
2357                                 return Token.IDENTIFIER;
2358                         }
2359
2360                         return res;
2361                 }
2362
2363                 private int consume_identifier (int s, bool quoted) 
2364                 {
2365                         int pos = 1;
2366                         int c = -1;
2367                         
2368                         id_builder [0] = (char) s;
2369
2370                         current_location = new Location (ref_line, Col);
2371
2372                         while ((c = getChar ()) != -1) {
2373                         loop:
2374                                 if (is_identifier_part_character ((char) c)){
2375                                         if (pos == max_id_size){
2376                                                 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
2377                                                 return Token.ERROR;
2378                                         }
2379                                         
2380                                         id_builder [pos++] = (char) c;
2381 //                                      putback_char = -1;
2382                                 } else if (c == '\\') {
2383                                         c = escape (c);
2384                                         goto loop;
2385                                 } else {
2386 //                                      putback_char = c;
2387                                         putback (c);
2388                                         break;
2389                                 }
2390                         }
2391
2392                         //
2393                         // Optimization: avoids doing the keyword lookup
2394                         // on uppercase letters and _
2395                         //
2396                         if (!quoted && (s >= 'a' || s == '_')){
2397                                 int keyword = GetKeyword (id_builder, pos);
2398                                 if (keyword != -1) {
2399                                         val = Location;
2400                                         return keyword;
2401                                 }
2402                         }
2403
2404                         //
2405                         // Keep identifiers in an array of hashtables to avoid needless
2406                         // allocations
2407                         //
2408
2409                         if (identifiers [pos] != null) {
2410                                 val = identifiers [pos][id_builder];
2411                                 if (val != null) {
2412                                         val = new LocatedToken (Location, (string) val);
2413                                         if (quoted)
2414                                                 escapedIdentifiers.Add (val);
2415                                         return Token.IDENTIFIER;
2416                                 }
2417                         }
2418                         else
2419                                 identifiers [pos] = new CharArrayHashtable (pos);
2420
2421                         val = new String (id_builder, 0, pos);
2422                         if (RootContext.Version == LanguageVersion.ISO_1) {
2423                                 for (int i = 1; i < id_builder.Length; i += 3) {
2424                                         if (id_builder [i] == '_' && (id_builder [i - 1] == '_' || id_builder [i + 1] == '_')) {
2425                                                 Report.Error (1638, Location, 
2426                                                         "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val.ToString ());
2427                                                 break;
2428                                         }
2429                                 }
2430                         }
2431
2432                         char [] chars = new char [pos];
2433                         Array.Copy (id_builder, chars, pos);
2434
2435                         identifiers [pos] [chars] = val;
2436
2437                         val = new LocatedToken (Location, (string) val);
2438                         if (quoted)
2439                                 escapedIdentifiers.Add (val);
2440                         return Token.IDENTIFIER;
2441                 }
2442                 
2443                 public int xtoken ()
2444                 {
2445                         int t;
2446                         bool doread = false;
2447                         int c;
2448
2449                         // Whether we have seen comments on the current line
2450                         bool comments_seen = false;
2451                         val = null;
2452                         for (;(c = getChar ()) != -1;) {
2453                                 if (c == '\t'){
2454                                         col = ((col + 8) / 8) * 8;
2455                                         continue;
2456                                 }
2457                                 
2458                                 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0 || c == 0)
2459                                         continue;
2460
2461                                 if (c == '\r') {
2462                                         if (peekChar () == '\n')
2463                                                 getChar ();
2464
2465                                         any_token_seen |= tokens_seen;
2466                                         tokens_seen = false;
2467                                         comments_seen = false;
2468                                         continue;
2469                                 }
2470
2471                                 // Handle double-slash comments.
2472                                 if (c == '/'){
2473                                         int d = peekChar ();
2474                                 
2475                                         if (d == '/'){
2476                                                 getChar ();
2477                                                 if (RootContext.Documentation != null && peekChar () == '/') {
2478                                                         getChar ();
2479                                                         // Don't allow ////.
2480                                                         if ((d = peekChar ()) != '/') {
2481                                                                 update_comment_location ();
2482                                                                 if (doc_state == XmlCommentState.Allowed)
2483                                                                         handle_one_line_xml_comment ();
2484                                                                 else if (doc_state == XmlCommentState.NotAllowed)
2485                                                                         warn_incorrect_doc_comment ();
2486                                                         }
2487                                                 }
2488                                                 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
2489                                                         if (d == '\n'){
2490                                                         }
2491                                                 any_token_seen |= tokens_seen;
2492                                                 tokens_seen = false;
2493                                                 comments_seen = false;
2494                                                 continue;
2495                                         } else if (d == '*'){
2496                                                 getChar ();
2497                                                 bool docAppend = false;
2498                                                 if (RootContext.Documentation != null && peekChar () == '*') {
2499                                                         getChar ();
2500                                                         update_comment_location ();
2501                                                         // But when it is /**/, just do nothing.
2502                                                         if (peekChar () == '/') {
2503                                                                 getChar ();
2504                                                                 continue;
2505                                                         }
2506                                                         if (doc_state == XmlCommentState.Allowed)
2507                                                                 docAppend = true;
2508                                                         else if (doc_state == XmlCommentState.NotAllowed)
2509                                                                 warn_incorrect_doc_comment ();
2510                                                 }
2511
2512                                                 int current_comment_start = 0;
2513                                                 if (docAppend) {
2514                                                         current_comment_start = xml_comment_buffer.Length;
2515                                                         xml_comment_buffer.Append (Environment.NewLine);
2516                                                 }
2517
2518                                                 Location start_location = Location;
2519
2520                                                 while ((d = getChar ()) != -1){
2521                                                         if (d == '*' && peekChar () == '/'){
2522                                                                 getChar ();
2523                                                                 comments_seen = true;
2524                                                                 break;
2525                                                         }
2526                                                         if (docAppend)
2527                                                                 xml_comment_buffer.Append ((char) d);
2528                                                         
2529                                                         if (d == '\n'){
2530                                                                 any_token_seen |= tokens_seen;
2531                                                                 tokens_seen = false;
2532                                                                 // 
2533                                                                 // Reset 'comments_seen' just to be consistent.
2534                                                                 // It doesn't matter either way, here.
2535                                                                 //
2536                                                                 comments_seen = false;
2537                                                         }
2538                                                 }
2539                                                 if (!comments_seen)
2540                                                         Report.Error (1035, start_location, "End-of-file found, '*/' expected");
2541
2542                                                 if (docAppend)
2543                                                         update_formatted_doc_comment (current_comment_start);
2544                                                 continue;
2545                                         }
2546                                         goto is_punct_label;
2547                                 }
2548
2549                                 
2550                                 if (c == '\\' || is_identifier_start_character ((char)c)){
2551                                         tokens_seen = true;
2552                                         return consume_identifier (c);
2553                                 }
2554
2555                         is_punct_label:
2556                                 current_location = new Location (ref_line, Col);
2557                                 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
2558                                         tokens_seen = true;
2559                                         if (doread){
2560                                                 getChar ();
2561                                         }
2562                                         return t;
2563                                 }
2564
2565                                 // white space
2566                                 if (c == '\n'){
2567                                         any_token_seen |= tokens_seen;
2568                                         tokens_seen = false;
2569                                         comments_seen = false;
2570                                         continue;
2571                                 }
2572
2573                                 if (c >= '0' && c <= '9'){
2574                                         tokens_seen = true;
2575                                         return is_number (c);
2576                                 }
2577
2578                                 if (c == '.'){
2579                                         tokens_seen = true;
2580                                         int peek = peekChar ();
2581                                         if (peek >= '0' && peek <= '9')
2582                                                 return is_number (c);
2583                                         return Token.DOT;
2584                                 }
2585                                 
2586                                 if (c == '#') {
2587                                         if (tokens_seen || comments_seen) {
2588                                                 Eror_WrongPreprocessorLocation ();
2589                                                 return Token.ERROR;
2590                                         }
2591                                         
2592                                         if (handle_preprocessing_directive (true))
2593                                                 continue;
2594
2595                                         bool directive_expected = false;
2596                                         while ((c = getChar ()) != -1) {
2597                                                 if (col == 1) {
2598                                                         directive_expected = true;
2599                                                 } else if (!directive_expected) {
2600                                                         // TODO: Implement comment support for disabled code and uncomment this code
2601 //                                                      if (c == '#') {
2602 //                                                              Eror_WrongPreprocessorLocation ();
2603 //                                                              return Token.ERROR;
2604 //                                                      }
2605                                                         continue;
2606                                                 }
2607
2608                                                 if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v' )
2609                                                         continue;
2610
2611                                                 if (c == '#') {
2612                                                         if (handle_preprocessing_directive (false))
2613                                                                 break;
2614                                                 }
2615                                                 directive_expected = false;
2616                                         }
2617
2618                                         if (c != -1) {
2619                                                 tokens_seen = false;
2620                                                 continue;
2621                                         }
2622
2623                                         return Token.EOF;
2624                                 }
2625                                 
2626                                 if (c == '"') 
2627                                         return consume_string (false);
2628
2629                                 if (c == '\''){
2630                                         c = getChar ();
2631                                         tokens_seen = true;
2632                                         if (c == '\''){
2633                                                 error_details = "Empty character literal";
2634                                                 Report.Error (1011, Location, error_details);
2635                                                 return Token.ERROR;
2636                                         }
2637                                         if (c == '\r' || c == '\n') {
2638                                                 Report.Error (1010, Location, "Newline in constant");
2639                                                 return Token.ERROR;
2640                                         }
2641                                         c = escape (c);
2642                                         if (c == -1)
2643                                                 return Token.ERROR;
2644                                         val = new System.Char ();
2645                                         val = (char) c;
2646                                         c = getChar ();
2647
2648                                         if (c != '\''){
2649                                                 error_details = "Too many characters in character literal";
2650                                                 Report.Error (1012, Location, error_details);
2651
2652                                                 // Try to recover, read until newline or next "'"
2653                                                 while ((c = getChar ()) != -1){
2654                                                         if (c == '\n'){
2655                                                                 break;
2656                                                         }
2657                                                         else if (c == '\'')
2658                                                                 break;
2659                                                 }
2660                                                 return Token.ERROR;
2661                                         }
2662                                         return Token.LITERAL_CHARACTER;
2663                                 }
2664                                 
2665                                 if (c == '@') {
2666                                         c = getChar ();
2667                                         if (c == '"') {
2668                                                 tokens_seen = true;
2669                                                 return consume_string (true);
2670                                         } else if (is_identifier_start_character ((char) c)){
2671                                                 return consume_identifier (c, true);
2672                                         } else {
2673                                                 Report.Error (1646, Location, "Keyword, identifier, or string expected after verbatim specifier: @");
2674                                         }
2675                                 }
2676
2677                                 error_details = ((char)c).ToString ();
2678                                 
2679                                 return Token.ERROR;
2680                         }
2681
2682                         return Token.EOF;
2683                 }
2684
2685                 //
2686                 // Handles one line xml comment
2687                 //
2688                 private void handle_one_line_xml_comment ()
2689                 {
2690                         int c;
2691                         while ((c = peekChar ()) == ' ')
2692                                 getChar (); // skip heading whitespaces.
2693                         while ((c = peekChar ()) != -1 && c != '\n' && c != '\r') {
2694                                 xml_comment_buffer.Append ((char) getChar ());
2695                         }
2696                         if (c == '\r' || c == '\n')
2697                                 xml_comment_buffer.Append (Environment.NewLine);
2698                 }
2699
2700                 //
2701                 // Remove heading "*" in Javadoc-like xml documentation.
2702                 //
2703                 private void update_formatted_doc_comment (int current_comment_start)
2704                 {
2705                         int length = xml_comment_buffer.Length - current_comment_start;
2706                         string [] lines = xml_comment_buffer.ToString (
2707                                 current_comment_start,
2708                                 length).Replace ("\r", "").Split ('\n');
2709                         
2710                         // The first line starts with /**, thus it is not target
2711                         // for the format check.
2712                         for (int i = 1; i < lines.Length; i++) {
2713                                 string s = lines [i];
2714                                 int idx = s.IndexOf ('*');
2715                                 string head = null;
2716                                 if (idx < 0) {
2717                                         if (i < lines.Length - 1)
2718                                                 return;
2719                                         head = s;
2720                                 } else
2721                                         head = s.Substring (0, idx);
2722                                 foreach (char c in head)
2723                                         if (c != ' ')
2724                                                 return;
2725                                 lines [i] = s.Substring (idx + 1);
2726                         }
2727                         xml_comment_buffer.Remove (current_comment_start, length);
2728                         xml_comment_buffer.Insert (current_comment_start, String.Join (Environment.NewLine, lines));
2729                 }
2730
2731                 //
2732                 // Updates current comment location.
2733                 //
2734                 private void update_comment_location ()
2735                 {
2736                         if (current_comment_location.IsNull) {
2737                                 // "-2" is for heading "//" or "/*"
2738                                 current_comment_location =
2739                                         new Location (ref_line, col - 2);
2740                         }
2741                 }
2742
2743                 //
2744                 // Checks if there was incorrect doc comments and raise
2745                 // warnings.
2746                 //
2747                 public void check_incorrect_doc_comment ()
2748                 {
2749                         if (xml_comment_buffer.Length > 0)
2750                                 warn_incorrect_doc_comment ();
2751                 }
2752
2753                 //
2754                 // Raises a warning when tokenizer found incorrect doccomment
2755                 // markup.
2756                 //
2757                 private void warn_incorrect_doc_comment ()
2758                 {
2759                         if (doc_state != XmlCommentState.Error) {
2760                                 doc_state = XmlCommentState.Error;
2761                                 // in csc, it is 'XML comment is not placed on 
2762                                 // a valid language element'. But that does not
2763                                 // make sense.
2764                                 Report.Warning (1587, 2, Location, "XML comment is not placed on a valid language element");
2765                         }
2766                 }
2767
2768                 //
2769                 // Consumes the saved xml comment lines (if any)
2770                 // as for current target member or type.
2771                 //
2772                 public string consume_doc_comment ()
2773                 {
2774                         if (xml_comment_buffer.Length > 0) {
2775                                 string ret = xml_comment_buffer.ToString ();
2776                                 reset_doc_comment ();
2777                                 return ret;
2778                         }
2779                         return null;
2780                 }
2781
2782                 void reset_doc_comment ()
2783                 {
2784                         xml_comment_buffer.Length = 0;
2785                         current_comment_location = Location.Null;
2786                 }
2787
2788                 public void cleanup ()
2789                 {
2790                         if (ifstack != null && ifstack.Count >= 1) {
2791                                 current_location = new Location (ref_line, Col);
2792                                 int state = (int) ifstack.Pop ();
2793                                 if ((state & REGION) != 0)
2794                                         Report.Error (1038, Location, "#endregion directive expected");
2795                                 else 
2796                                         Report.Error (1027, Location, "Expected `#endif' directive");
2797                         }
2798                 }
2799         }
2800
2801         //
2802         // Indicates whether it accepts XML documentation or not.
2803         //
2804         public enum XmlCommentState {
2805                 // comment is allowed in this state.
2806                 Allowed,
2807                 // comment is not allowed in this state.
2808                 NotAllowed,
2809                 // once comments appeared when it is NotAllowed, then the
2810                 // state is changed to it, until the state is changed to
2811                 // .Allowed.
2812                 Error
2813         }
2814 }
2815