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