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