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