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