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