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