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