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