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