Don't allocate intermediate MemberName for member access expressions
[mono.git] / mcs / mcs / cs-tokenizer.cs
index addf9aa98c78fe893ad595b5dc5d1aed0a44479e..2dccc46414da9f88d981f042d186a30fb6433351 100644 (file)
@@ -3,21 +3,20 @@
 //                  This also implements the preprocessor
 //
 // Author: Miguel de Icaza (miguel@gnu.org)
-//         Marek Safar (marek.safar@seznam.cz)
+//         Marek Safar (marek.safar@gmail.com)
 //
 // Dual licensed under the terms of the MIT X11 or GNU GPL
 //
 // Copyright 2001, 2002 Ximian, Inc (http://www.ximian.com)
 // Copyright 2004-2008 Novell, Inc
-//
+// Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
 //
 
 using System;
 using System.Text;
-using System.Collections;
-using System.IO;
+using System.Collections.Generic;
 using System.Globalization;
-using System.Reflection;
+using System.Diagnostics;
 
 namespace Mono.CSharp
 {
@@ -27,23 +26,166 @@ namespace Mono.CSharp
 
        public class Tokenizer : yyParser.yyInput
        {
+               class KeywordEntry<T>
+               {
+                       public readonly T Token;
+                       public KeywordEntry<T> Next;
+                       public readonly char[] Value;
+
+                       public KeywordEntry (string value, T token)
+                       {
+                               this.Value = value.ToCharArray ();
+                               this.Token = token;
+                       }
+               }
+
+               sealed class IdentifiersComparer : IEqualityComparer<char[]>
+               {
+                       readonly int length;
+
+                       public IdentifiersComparer (int length)
+                       {
+                               this.length = length;
+                       }
+
+                       public bool Equals (char[] x, char[] y)
+                       {
+                               for (int i = 0; i < length; ++i)
+                                       if (x [i] != y [i])
+                                               return false;
+
+                               return true;
+                       }
+
+                       public int GetHashCode (char[] obj)
+                       {
+                               int h = 0;
+                               for (int i = 0; i < length; ++i)
+                                       h = (h << 5) - h + obj [i];
+
+                               return h;
+                       }
+               }
+
+               //
+               // This class has to be used in the parser only, it reuses token
+               // details after each parse
+               //
+               public class LocatedToken
+               {
+                       int row, column;
+                       string value;
+
+                       static LocatedToken[] buffer;
+                       static int pos;
+
+                       private LocatedToken ()
+                       {
+                       }
+
+                       public static LocatedToken Create (int row, int column)
+                       {
+                               return Create (null, row, column);
+                       }
+
+                       public static LocatedToken Create (string value, Location loc)
+                       {
+                               return Create (value, loc.Row, loc.Column);
+                       }
+                       
+                       public static LocatedToken Create (string value, int row, int column)
+                       {
+                               //
+                               // TODO: I am not very happy about the logic but it's the best
+                               // what I could come up with for now.
+                               // Ideally we should be using just tiny buffer (256 elements) which
+                               // is enough to hold all details for currect stack and recycle elements
+                               // poped from the stack but there is a trick needed to recycle
+                               // them properly.
+                               //
+                               LocatedToken entry;
+                               if (pos >= buffer.Length) {
+                                       entry = new LocatedToken ();
+                               } else {
+                                       entry = buffer [pos];
+                                       if (entry == null) {
+                                               entry = new LocatedToken ();
+                                               buffer [pos] = entry;
+                                       }
+
+                                       ++pos;
+                               }
+                               entry.value = value;
+                               entry.row = row;
+                               entry.column = column;
+                               return entry;
+                       }
+
+                       //
+                       // Used for token not required by expression evaluator
+                       //
+                       [Conditional ("FULL_AST")]
+                       public static void CreateOptional (int row, int col, ref object token)
+                       {
+                               token = Create (row, col);
+                       }
+                       
+                       public static void Initialize ()
+                       {
+                               if (buffer == null)
+                                       buffer = new LocatedToken [10000];
+                               pos = 0;
+                       }
+
+                       public Location Location {
+                               get { return new Location (row, column); }
+                       }
+
+                       public string Value {
+                               get { return value; }
+                       }
+               }
+
+               public enum PreprocessorDirective
+               {
+                       Invalid = 0,
+
+                       Region = 1,
+                       Endregion = 2,
+                       If = 3 | RequiresArgument,
+                       Endif = 4,
+                       Elif = 5 | RequiresArgument,
+                       Else = 6,
+                       Define = 7 | RequiresArgument,
+                       Undef = 8 | RequiresArgument,
+                       Error = 9,
+                       Warning = 10,
+                       Pragma = 11 | CustomArgumentsParsing,
+                       Line = 12,
+
+                       CustomArgumentsParsing = 1 << 10,
+                       RequiresArgument = 1 << 11
+               }
+
                SeekableStreamReader reader;
                SourceFile ref_name;
-               CompilationUnit file_name;
+               CompilationSourceFile file_name;
+               CompilerContext context;
                bool hidden = false;
                int ref_line = 1;
                int line = 1;
                int col = 0;
                int previous_col;
                int current_token;
+               int tab_size;
                bool handle_get_set = false;
                bool handle_remove_add = false;
                bool handle_where = false;
                bool handle_typeof = false;
                bool lambda_arguments_parsing;
-               Location current_comment_location = Location.Null;
-               ArrayList escaped_identifiers;
+               List<Location> escaped_identifiers;
                int parsing_generic_less_than;
+               readonly bool doc_processing;
                
                //
                // Used mainly for parser optimizations. Some expressions for instance
@@ -62,6 +204,7 @@ namespace Mono.CSharp
                // Set when parsing generic declaration (type or method header)
                //
                public bool parsing_generic_declaration;
+               public bool parsing_generic_declaration_doc;
                
                //
                // The value indicates that we have not reach any declaration or
@@ -69,19 +212,24 @@ namespace Mono.CSharp
                //
                public int parsing_declaration;
 
+               public bool parsing_attribute_section;
+
+               public bool parsing_modifiers;
+
                //
-               // The special character to inject on streams to trigger the EXPRESSION_PARSE
-               // token to be returned.   It just happens to be a Unicode character that
-               // would never be part of a program (can not be an identifier).
+               // The special characters to inject on streams to run the unit parser
+               // in the special expression mode. Using private characters from
+               // Plane Sixteen (U+100000 to U+10FFFD)
                //
                // This character is only tested just before the tokenizer is about to report
                // an error;   So on the regular operation mode, this addition will have no
                // impact on the tokenizer's performance.
                //
                
-               public const int EvalStatementParserCharacter = 0x2190;   // Unicode Left Arrow
-               public const int EvalCompilationUnitParserCharacter = 0x2191;  // Unicode Arrow
-               public const int EvalUsingDeclarationsParserCharacter = 0x2192;  // Unicode Arrow
+               public const int EvalStatementParserCharacter = 0x100000;
+               public const int EvalCompilationUnitParserCharacter = 0x100001;
+               public const int EvalUsingDeclarationsParserCharacter = 0x100002;
+               public const int DocumentationXref = 0x100003;
                
                //
                // XML documentation buffer. The save point is used to divide
@@ -99,12 +247,35 @@ namespace Mono.CSharp
                //
                bool tokens_seen = false;
 
+               //
+               // Set to true once the GENERATE_COMPLETION token has bee
+               // returned.   This helps produce one GENERATE_COMPLETION,
+               // as many COMPLETE_COMPLETION as necessary to complete the
+               // AST tree and one final EOF.
+               //
+               bool generated;
+               
                //
                // Whether a token has been seen on the file
                // This is needed because `define' is not allowed to be used
                // after a token has been seen.
                //
-               bool any_token_seen = false;
+               bool any_token_seen;
+
+               //
+               // Class variables
+               // 
+               static readonly KeywordEntry<int>[][] keywords;
+               static readonly KeywordEntry<PreprocessorDirective>[][] keywords_preprocessor;
+               static readonly Dictionary<string, object> keyword_strings;             // TODO: HashSet
+               static readonly NumberStyles styles;
+               static readonly NumberFormatInfo csharp_format_info;
+
+               // Pragma arguments
+               static readonly char[] pragma_warning = "warning".ToCharArray ();
+               static readonly char[] pragma_warning_disable = "disable".ToCharArray ();
+               static readonly char[] pragma_warning_restore = "restore".ToCharArray ();
+               static readonly char[] pragma_checksum = "checksum".ToCharArray ();
 
                static readonly char[] simple_whitespaces = new char[] { ' ', '\t' };
 
@@ -127,6 +298,11 @@ namespace Mono.CSharp
                        get { return handle_typeof; }
                        set { handle_typeof = value; }
                }
+
+               public int TabSize {
+                       get { return tab_size; }
+                       set { tab_size = value; }
+               }
                
                public XmlCommentState doc_state {
                        get { return xml_doc_state; }
@@ -139,38 +315,28 @@ namespace Mono.CSharp
                        }
                }
 
-               void AddEscapedIdentifier (LocatedToken lt)
+               //
+               // This is used to trigger completion generation on the parser
+               public bool CompleteOnEOF;
+               
+               void AddEscapedIdentifier (Location loc)
                {
                        if (escaped_identifiers == null)
-                               escaped_identifiers = new ArrayList ();
+                               escaped_identifiers = new List<Location> ();
 
-                       escaped_identifiers.Add (lt);
+                       escaped_identifiers.Add (loc);
                }
 
-               public bool IsEscapedIdentifier (Location loc)
+               public bool IsEscapedIdentifier (ATypeNameExpression name)
                {
-                       if (escaped_identifiers != null) {
-                               foreach (LocatedToken lt in escaped_identifiers)
-                                       if (lt.Location.Equals (loc))
-                                               return true;
-                       }
-
-                       return false;
+                       return escaped_identifiers != null && escaped_identifiers.Contains (name.Location);
                }
 
-               //
-               // Class variables
-               // 
-               static CharArrayHashtable[] keywords;
-               static Hashtable keyword_strings;
-               static NumberStyles styles;
-               static NumberFormatInfo csharp_format_info;
-               
                //
                // Values for the associated token returned
                //
                internal int putback_char;      // Used by repl only
-               Object val;
+               object val;
 
                //
                // Pre-processor
@@ -183,30 +349,21 @@ namespace Mono.CSharp
                //
                // pre-processor if stack state:
                //
-               Stack ifstack;
+               Stack<int> ifstack;
 
                static System.Text.StringBuilder string_builder;
 
                const int max_id_size = 512;
-               static char [] id_builder = new char [max_id_size];
+               static readonly char [] id_builder = new char [max_id_size];
 
-               static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
+               public static Dictionary<char[], string>[] identifiers = new Dictionary<char[], string>[max_id_size + 1];
 
                const int max_number_size = 512;
                static char [] number_builder = new char [max_number_size];
                static int number_pos;
-               
-               //
-               // Details about the error encoutered by the tokenizer
-               //
-               string error_details;
-               
-               public string error {
-                       get {
-                               return error_details;
-                       }
-               }
-               
+
+               static char[] value_builder = new char[256];
+
                public int Line {
                        get {
                                return ref_line;
@@ -219,7 +376,8 @@ namespace Mono.CSharp
                // on its own to deamiguate a token in behalf of the
                // parser.
                //
-               Stack position_stack = new Stack (2);
+               Stack<Position> position_stack = new Stack<Position> (2);
+
                class Position {
                        public int position;
                        public int line;
@@ -228,9 +386,10 @@ namespace Mono.CSharp
                        public bool hidden;
                        public int putback_char;
                        public int previous_col;
-                       public Stack ifstack;
+                       public Stack<int> ifstack;
                        public int parsing_generic_less_than;
                        public int current_token;
+                       public object val;
 
                        public Position (Tokenizer t)
                        {
@@ -241,12 +400,38 @@ namespace Mono.CSharp
                                hidden = t.hidden;
                                putback_char = t.putback_char;
                                previous_col = t.previous_col;
-                               if (t.ifstack != null && t.ifstack.Count != 0)
-                                       ifstack = (Stack)t.ifstack.Clone ();
+                               if (t.ifstack != null && t.ifstack.Count != 0) {
+                                       // There is no simple way to clone Stack<T> all
+                                       // methods reverse the order
+                                       var clone = t.ifstack.ToArray ();
+                                       Array.Reverse (clone);
+                                       ifstack = new Stack<int> (clone);
+                               }
                                parsing_generic_less_than = t.parsing_generic_less_than;
                                current_token = t.current_token;
+                               val = t.val;
                        }
                }
+
+               public Tokenizer (SeekableStreamReader input, CompilationSourceFile file, CompilerContext ctx)
+               {
+                       this.ref_name = file;
+                       this.file_name = file;
+                       this.context = ctx;
+                       reader = input;
+
+                       putback_char = -1;
+
+                       xml_comment_buffer = new StringBuilder ();
+                       doc_processing = ctx.Settings.DocumentationFile != null;
+
+                       if (Environment.OSVersion.Platform == PlatformID.Win32NT)
+                               tab_size = 4;
+                       else
+                               tab_size = 8;
+
+                       Mono.CSharp.Location.Push (file, file);
+               }
                
                public void PushPosition ()
                {
@@ -255,7 +440,7 @@ namespace Mono.CSharp
 
                public void PopPosition ()
                {
-                       Position p = (Position) position_stack.Pop ();
+                       Position p = position_stack.Pop ();
 
                        reader.Position = p.position;
                        ref_line = p.ref_line;
@@ -267,6 +452,7 @@ namespace Mono.CSharp
                        ifstack = p.ifstack;
                        parsing_generic_less_than = p.parsing_generic_less_than;
                        current_token = p.current_token;
+                       val = p.val;
                }
 
                // Do not reset the position, ignore it.
@@ -277,19 +463,51 @@ namespace Mono.CSharp
                
                static void AddKeyword (string kw, int token)
                {
-                       keyword_strings.Add (kw, kw);
-                       if (keywords [kw.Length] == null) {
-                               keywords [kw.Length] = new CharArrayHashtable (kw.Length);
+                       keyword_strings.Add (kw, null);
+
+                       AddKeyword (keywords, kw, token);
+               }
+
+               static void AddPreprocessorKeyword (string kw, PreprocessorDirective directive)
+               {
+                       AddKeyword (keywords_preprocessor, kw, directive);
+               }
+
+               static void AddKeyword<T> (KeywordEntry<T>[][] keywords, string kw, T token)
+               {
+                       int length = kw.Length;
+                       if (keywords[length] == null) {
+                               keywords[length] = new KeywordEntry<T>['z' - '_' + 1];
                        }
-                       keywords [kw.Length] [kw.ToCharArray ()] = token;
+
+                       int char_index = kw[0] - '_';
+                       var kwe = keywords[length][char_index];
+                       if (kwe == null) {
+                               keywords[length][char_index] = new KeywordEntry<T> (kw, token);
+                               return;
+                       }
+
+                       while (kwe.Next != null) {
+                               kwe = kwe.Next;
+                       }
+
+                       kwe.Next = new KeywordEntry<T> (kw, token);
                }
 
-               static void InitTokens ()
+               //
+               // Class initializer
+               // 
+               static Tokenizer ()
                {
-                       keyword_strings = new Hashtable ();
-                       keywords = new CharArrayHashtable [64];
+                       keyword_strings = new Dictionary<string, object> ();
+
+                       // 11 is the length of the longest keyword for now
+                       keywords = new KeywordEntry<int>[11][];
 
                        AddKeyword ("__arglist", Token.ARGLIST);
+                       AddKeyword ("__makeref", Token.MAKEREF);
+                       AddKeyword ("__reftype", Token.REFTYPE);
+                       AddKeyword ("__refvalue", Token.REFVALUE);
                        AddKeyword ("abstract", Token.ABSTRACT);
                        AddKeyword ("as", Token.AS);
                        AddKeyword ("add", Token.ADD);
@@ -387,19 +605,26 @@ namespace Mono.CSharp
                        AddKeyword ("ascending", Token.ASCENDING);
                        AddKeyword ("descending", Token.DESCENDING);
                        AddKeyword ("into", Token.INTO);
-               }
 
-               //
-               // Class initializer
-               // 
-               static Tokenizer ()
-               {
-                       Reset ();
-               }
+                       // Contextual async keywords
+                       AddKeyword ("async", Token.ASYNC);
+                       AddKeyword ("await", Token.AWAIT);
+
+                       keywords_preprocessor = new KeywordEntry<PreprocessorDirective>[10][];
+
+                       AddPreprocessorKeyword ("region", PreprocessorDirective.Region);
+                       AddPreprocessorKeyword ("endregion", PreprocessorDirective.Endregion);
+                       AddPreprocessorKeyword ("if", PreprocessorDirective.If);
+                       AddPreprocessorKeyword ("endif", PreprocessorDirective.Endif);
+                       AddPreprocessorKeyword ("elif", PreprocessorDirective.Elif);
+                       AddPreprocessorKeyword ("else", PreprocessorDirective.Else);
+                       AddPreprocessorKeyword ("define", PreprocessorDirective.Define);
+                       AddPreprocessorKeyword ("undef", PreprocessorDirective.Undef);
+                       AddPreprocessorKeyword ("error", PreprocessorDirective.Error);
+                       AddPreprocessorKeyword ("warning", PreprocessorDirective.Warning);
+                       AddPreprocessorKeyword ("pragma", PreprocessorDirective.Pragma);
+                       AddPreprocessorKeyword ("line", PreprocessorDirective.Line);
 
-               public static void Reset ()
-               {
-                       InitTokens ();
                        csharp_format_info = NumberFormatInfo.InvariantInfo;
                        styles = NumberStyles.Float;
 
@@ -408,20 +633,37 @@ namespace Mono.CSharp
 
                int GetKeyword (char[] id, int id_len)
                {
-                       /*
-                        * Keywords are stored in an array of hashtables grouped by their
-                        * length.
-                        */
+                       //
+                       // Keywords are stored in an array of arrays grouped by their
+                       // length and then by the first character
+                       //
+                       if (id_len >= keywords.Length || keywords [id_len] == null)
+                               return -1;
 
-                       if ((id_len >= keywords.Length) || (keywords [id_len] == null))
+                       int first_index = id [0] - '_';
+                       if (first_index > 'z' - '_')
                                return -1;
-                       object o = keywords [id_len] [id];
 
-                       if (o == null)
+                       var kwe = keywords [id_len] [first_index];
+                       if (kwe == null)
+                               return -1;
+
+                       int res;
+                       do {
+                               res = kwe.Token;
+                               for (int i = 1; i < id_len; ++i) {
+                                       if (id [i] != kwe.Value [i]) {
+                                               res = 0;
+                                               kwe = kwe.Next;
+                                               break;
+                                       }
+                               }
+                       } while (res == 0 && kwe != null);
+
+                       if (res == 0)
                                return -1;
 
                        int next_token;
-                       int res = (int) o;
                        switch (res) {
                        case Token.GET:
                        case Token.SET:
@@ -481,11 +723,11 @@ namespace Mono.CSharp
                                                
                                                res = Token.FROM_FIRST;
                                                query_parsing = true;
-                                               if (RootContext.Version <= LanguageVersion.ISO_2)
-                                                       Report.FeatureIsNotAvailable (Location, "query expressions");
+                                               if (context.Settings.Version <= LanguageVersion.ISO_2)
+                                                       Report.FeatureIsNotAvailable (context, Location, "query expressions");
                                                break;
                                        case Token.VOID:
-                                               Expression.Error_VoidInvalidInTheContext (Location);
+                                               Expression.Error_VoidInvalidInTheContext (Location, Report);
                                                break;
                                        default:
                                                PopPosition ();
@@ -537,11 +779,10 @@ namespace Mono.CSharp
 
                                if (ok) {
                                        if (next_token == Token.VOID) {
-                                               if (RootContext.Version == LanguageVersion.ISO_1 ||
-                                                   RootContext.Version == LanguageVersion.ISO_2)
-                                                       Report.FeatureIsNotAvailable (Location, "partial methods");
-                                       } else if (RootContext.Version == LanguageVersion.ISO_1)
-                                               Report.FeatureIsNotAvailable (Location, "partial types");
+                                               if (context.Settings.Version <= LanguageVersion.ISO_2)
+                                                       Report.FeatureIsNotAvailable (context, Location, "partial methods");
+                                       } else if (context.Settings.Version == LanguageVersion.ISO_1)
+                                               Report.FeatureIsNotAvailable (context, Location, "partial types");
 
                                        return res;
                                }
@@ -554,8 +795,85 @@ namespace Mono.CSharp
 
                                res = -1;
                                break;
+
+                       case Token.ASYNC:
+                               if (parsing_modifiers) {
+                                       //
+                                       // Skip attributes section or constructor called async
+                                       //
+                                       if (parsing_attribute_section || peek_token () == Token.OPEN_PARENS) {
+                                               res = -1;
+                                       } else {
+                                               // async is keyword
+                                       }
+                               } else if (parsing_block > 0) {
+                                       switch (peek_token ()) {
+                                       case Token.DELEGATE:
+                                       case Token.OPEN_PARENS_LAMBDA:
+                                               // async is keyword
+                                               break;
+                                       case Token.IDENTIFIER:
+                                               PushPosition ();
+                                               xtoken ();
+                                               if (xtoken () != Token.ARROW)
+                                                       res = -1;
+
+                                               PopPosition ();
+                                               break;
+                                       default:
+                                               res = -1;
+                                               break;
+                                       }
+                               } else {
+                                       res = -1;
+                               }
+
+                               if (res == Token.ASYNC && context.Settings.Version <= LanguageVersion.V_4) {
+                                       Report.FeatureIsNotAvailable (context, Location, "asynchronous functions");
+                               }
+                               
+                               break;
+
+                       case Token.AWAIT:
+                               if (parsing_block == 0)
+                                       res = -1;
+
+                               break;
                        }
 
+
+                       return res;
+               }
+
+               static PreprocessorDirective GetPreprocessorDirective (char[] id, int id_len)
+               {
+                       //
+                       // Keywords are stored in an array of arrays grouped by their
+                       // length and then by the first character
+                       //
+                       if (id_len >= keywords_preprocessor.Length || keywords_preprocessor[id_len] == null)
+                               return PreprocessorDirective.Invalid;
+
+                       int first_index = id[0] - '_';
+                       if (first_index > 'z' - '_')
+                               return PreprocessorDirective.Invalid;
+
+                       var kwe = keywords_preprocessor[id_len][first_index];
+                       if (kwe == null)
+                               return PreprocessorDirective.Invalid;
+
+                       PreprocessorDirective res = PreprocessorDirective.Invalid;
+                       do {
+                               res = kwe.Token;
+                               for (int i = 1; i < id_len; ++i) {
+                                       if (id[i] != kwe.Value[i]) {
+                                               res = 0;
+                                               kwe = kwe.Next;
+                                               break;
+                                       }
+                               }
+                       } while (res == PreprocessorDirective.Invalid && kwe != null);
+
                        return res;
                }
 
@@ -565,23 +883,6 @@ namespace Mono.CSharp
                        }
                }
 
-               public Tokenizer (SeekableStreamReader input, CompilationUnit file)
-               {
-                       this.ref_name = file;
-                       this.file_name = file;
-                       reader = input;
-                       
-                       putback_char = -1;
-
-                       xml_comment_buffer = new StringBuilder ();
-
-                       //
-                       // FIXME: This could be `Location.Push' but we have to
-                       // find out why the MS compiler allows this
-                       //
-                       Mono.CSharp.Location.Push (file, file);
-               }
-
                static bool is_identifier_start_character (int c)
                {
                        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || Char.IsLetter ((char)c);
@@ -589,19 +890,29 @@ namespace Mono.CSharp
 
                static bool is_identifier_part_character (char c)
                {
-                       return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') ||
-                               Char.IsLetter (c) || Char.GetUnicodeCategory (c) == UnicodeCategory.ConnectorPunctuation;
+                       if (c >= 'a' && c <= 'z')
+                               return true;
+
+                       if (c >= 'A' && c <= 'Z')
+                               return true;
+
+                       if (c == '_' || (c >= '0' && c <= '9'))
+                               return true;
+
+                       if (c < 0x80)
+                               return false;
+
+                       return Char.IsLetter (c) || Char.GetUnicodeCategory (c) == UnicodeCategory.ConnectorPunctuation;
                }
 
                public static bool IsKeyword (string s)
                {
-                       return keyword_strings [s] != null;
+                       return keyword_strings.ContainsKey (s);
                }
 
                //
                // Open parens micro parser. Detects both lambda and cast ambiguity.
-               //
-               
+               //      
                int TokenizeOpenParens ()
                {
                        int ptoken;
@@ -622,12 +933,8 @@ namespace Mono.CSharp
                                        //
                                        // Expression inside parens is lambda, (int i) => 
                                        //
-                                       if (current_token == Token.ARROW) {
-                                               if (RootContext.Version <= LanguageVersion.ISO_2)
-                                                       Report.FeatureIsNotAvailable (Location, "lambda expressions");
-
+                                       if (current_token == Token.ARROW)
                                                return Token.OPEN_PARENS_LAMBDA;
-                                       }
 
                                        //
                                        // Expression inside parens is single type, (int[])
@@ -644,12 +951,7 @@ namespace Mono.CSharp
                                                case Token.BANG:
                                                case Token.TILDE:
                                                case Token.IDENTIFIER:
-                                               case Token.LITERAL_INTEGER:
-                                               case Token.LITERAL_FLOAT:
-                                               case Token.LITERAL_DOUBLE:
-                                               case Token.LITERAL_DECIMAL:
-                                               case Token.LITERAL_CHARACTER:
-                                               case Token.LITERAL_STRING:
+                                               case Token.LITERAL:
                                                case Token.BASE:
                                                case Token.CHECKED:
                                                case Token.DELEGATE:
@@ -696,6 +998,12 @@ namespace Mono.CSharp
                                case Token.IDENTIFIER:
                                        switch (ptoken) {
                                        case Token.DOT:
+                                               if (bracket_level == 0) {
+                                                       is_type = false;
+                                                       can_be_type = true;
+                                               }
+
+                                               continue;
                                        case Token.OP_GENERICS_LT:
                                        case Token.COMMA:
                                        case Token.DOUBLE_COLON:
@@ -787,6 +1095,8 @@ namespace Mono.CSharp
                                        the_token = token ();
                                } while (the_token != Token.CLOSE_BRACKET);
                                the_token = token ();
+                       } else if (the_token == Token.IN || the_token == Token.OUT) {
+                               the_token = token ();
                        }
                        switch (the_token) {
                        case Token.IDENTIFIER:
@@ -807,8 +1117,9 @@ namespace Mono.CSharp
                        case Token.CHAR:
                        case Token.VOID:
                                break;
-
                        case Token.OP_GENERICS_GT:
+                       case Token.IN:
+                       case Token.OUT:
                                return true;
 
                        default:
@@ -891,8 +1202,7 @@ namespace Mono.CSharp
                        case Token.TRUE:
                        case Token.FALSE:
                        case Token.NULL:
-                       case Token.LITERAL_INTEGER:
-                       case Token.LITERAL_STRING:
+                       case Token.LITERAL:
                                return Token.INTERR;
                        }
 
@@ -907,12 +1217,7 @@ namespace Mono.CSharp
                        current_token = Token.NONE;
                        int next_token;
                        switch (xtoken ()) {
-                       case Token.LITERAL_INTEGER:
-                       case Token.LITERAL_STRING:
-                       case Token.LITERAL_CHARACTER:
-                       case Token.LITERAL_DECIMAL:
-                       case Token.LITERAL_DOUBLE:
-                       case Token.LITERAL_FLOAT:
+                       case Token.LITERAL:
                        case Token.TRUE:
                        case Token.FALSE:
                        case Token.NULL:
@@ -926,6 +1231,7 @@ namespace Mono.CSharp
                        case Token.CLOSE_PARENS:
                        case Token.OPEN_BRACKET:
                        case Token.OP_GENERICS_GT:
+                       case Token.INTERR:
                                next_token = Token.INTERR_NULLABLE;
                                break;
                                
@@ -1014,27 +1320,21 @@ namespace Mono.CSharp
                        return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
                }
 
-               static int real_type_suffix (int c)
+               static TypeCode real_type_suffix (int c)
                {
-                       int t;
-
                        switch (c){
                        case 'F': case 'f':
-                               t =  Token.LITERAL_FLOAT;
-                               break;
+                               return TypeCode.Single;
                        case 'D': case 'd':
-                               t = Token.LITERAL_DOUBLE;
-                               break;
+                               return TypeCode.Double;
                        case 'M': case 'm':
-                                t= Token.LITERAL_DECIMAL;
-                               break;
+                               return TypeCode.Decimal;
                        default:
-                               return Token.NONE;
+                               return TypeCode.Empty;
                        }
-                       return t;
                }
 
-               int integer_type_suffix (ulong ul, int c)
+               ILiteralConstant integer_type_suffix (ulong ul, int c, Location loc)
                {
                        bool is_unsigned = false;
                        bool is_long = false;
@@ -1058,16 +1358,8 @@ namespace Mono.CSharp
                                                        //
                                                        Report.Warning (78, 4, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
                                                }
-                                               //
-                                               // This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
-                                               // work around that.
-                                               //
-                                               //goto case 'L';
-                                               if (is_long)
-                                                       scanning = false;
-                                               is_long = true;
-                                               get_char ();
-                                               break;
+
+                                               goto case 'L';
 
                                        case 'L': 
                                                if (is_long)
@@ -1085,38 +1377,38 @@ namespace Mono.CSharp
                        }
 
                        if (is_long && is_unsigned){
-                               val = ul;
-                               return Token.LITERAL_INTEGER;
-                       } else if (is_unsigned){
+                               return new ULongLiteral (context.BuiltinTypes, ul, loc);
+                       }
+                       
+                       if (is_unsigned){
                                // uint if possible, or ulong else.
 
                                if ((ul & 0xffffffff00000000) == 0)
-                                       val = (uint) ul;
+                                       return new UIntLiteral (context.BuiltinTypes, (uint) ul, loc);
                                else
-                                       val = ul;
+                                       return new ULongLiteral (context.BuiltinTypes, ul, loc);
                        } else if (is_long){
                                // long if possible, ulong otherwise
                                if ((ul & 0x8000000000000000) != 0)
-                                       val = ul;
+                                       return new ULongLiteral (context.BuiltinTypes, ul, loc);
                                else
-                                       val = (long) ul;
+                                       return new LongLiteral (context.BuiltinTypes, (long) ul, loc);
                        } else {
                                // int, uint, long or ulong in that order
                                if ((ul & 0xffffffff00000000) == 0){
                                        uint ui = (uint) ul;
                                        
                                        if ((ui & 0x80000000) != 0)
-                                               val = ui;
+                                               return new UIntLiteral (context.BuiltinTypes, ui, loc);
                                        else
-                                               val = (int) ui;
+                                               return new IntLiteral (context.BuiltinTypes, (int) ui, loc);
                                } else {
                                        if ((ul & 0x8000000000000000) != 0)
-                                               val = ul;
+                                               return new ULongLiteral (context.BuiltinTypes, ul, loc);
                                        else
-                                               val = (long) ul;
+                                               return new LongLiteral (context.BuiltinTypes, (long) ul, loc);
                                }
                        }
-                       return Token.LITERAL_INTEGER;
                }
                                
                //
@@ -1124,7 +1416,7 @@ namespace Mono.CSharp
                // we need to convert to a special type, and then choose
                // the best representation for the integer
                //
-               int adjust_int (int c)
+               ILiteralConstant adjust_int (int c, Location loc)
                {
                        try {
                                if (number_pos > 9){
@@ -1133,66 +1425,58 @@ namespace Mono.CSharp
                                        for (int i = 1; i < number_pos; i++){
                                                ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
                                        }
-                                       return integer_type_suffix (ul, c);
+
+                                       return integer_type_suffix (ul, c, loc);
                                } else {
                                        uint ui = (uint) (number_builder [0] - '0');
 
                                        for (int i = 1; i < number_pos; i++){
                                                ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
                                        }
-                                       return integer_type_suffix (ui, c);
+
+                                       return integer_type_suffix (ui, c, loc);
                                }
                        } catch (OverflowException) {
-                               error_details = "Integral constant is too large";
-                               Report.Error (1021, Location, error_details);
-                               val = 0ul;
-                               return Token.LITERAL_INTEGER;
+                               Error_NumericConstantTooLong ();
+                               return new IntLiteral (context.BuiltinTypes, 0, loc);
                        }
                        catch (FormatException) {
                                Report.Error (1013, Location, "Invalid number");
-                               val = 0ul;
-                               return Token.LITERAL_INTEGER;
+                               return new IntLiteral (context.BuiltinTypes, 0, loc);
                        }
                }
                
-               int adjust_real (int t)
+               ILiteralConstant adjust_real (TypeCode t, Location loc)
                {
-                       string s = new String (number_builder, 0, number_pos);
+                       string s = new string (number_builder, 0, number_pos);
                        const string error_details = "Floating-point constant is outside the range of type `{0}'";
 
                        switch (t){
-                       case Token.LITERAL_DECIMAL:
+                       case TypeCode.Decimal:
                                try {
-                                       val = System.Decimal.Parse (s, styles, csharp_format_info);
+                                       return new DecimalLiteral (context.BuiltinTypes, decimal.Parse (s, styles, csharp_format_info), loc);
                                } catch (OverflowException) {
-                                       val = 0m;     
                                        Report.Error (594, Location, error_details, "decimal");
+                                       return new DecimalLiteral (context.BuiltinTypes, 0, loc);
                                }
-                               break;
-                       case Token.LITERAL_FLOAT:
+                       case TypeCode.Single:
                                try {
-                                       val = float.Parse (s, styles, csharp_format_info);
+                                       return new FloatLiteral (context.BuiltinTypes, float.Parse (s, styles, csharp_format_info), loc);
                                } catch (OverflowException) {
-                                       val = 0.0f;     
                                        Report.Error (594, Location, error_details, "float");
+                                       return new FloatLiteral (context.BuiltinTypes, 0, loc);
                                }
-                               break;
-                               
-                       case Token.LITERAL_DOUBLE:
-                       case Token.NONE:
-                               t = Token.LITERAL_DOUBLE;
+                       default:
                                try {
-                                       val = System.Double.Parse (s, styles, csharp_format_info);
+                                       return new DoubleLiteral (context.BuiltinTypes, double.Parse (s, styles, csharp_format_info), loc);
                                } catch (OverflowException) {
-                                       val = 0.0;     
-                                       Report.Error (594, Location, error_details, "double");
+                                       Report.Error (594, loc, error_details, "double");
+                                       return new DoubleLiteral (context.BuiltinTypes, 0, loc);
                                }
-                               break;
                        }
-                       return t;
                }
 
-               int handle_hex ()
+               ILiteralConstant handle_hex (Location loc)
                {
                        int d;
                        ulong ul;
@@ -1207,24 +1491,22 @@ namespace Mono.CSharp
                        }
                        
                        string s = new String (number_builder, 0, number_pos);
+
                        try {
                                if (number_pos <= 8)
                                        ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
                                else
                                        ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
+
+                               return integer_type_suffix (ul, peek_char (), loc);
                        } catch (OverflowException){
-                               error_details = "Integral constant is too large";
-                               Report.Error (1021, Location, error_details);
-                               val = 0ul;
-                               return Token.LITERAL_INTEGER;
+                               Error_NumericConstantTooLong ();
+                               return new IntLiteral (context.BuiltinTypes, 0, loc);
                        }
                        catch (FormatException) {
                                Report.Error (1013, Location, "Invalid number");
-                               val = 0ul;
-                               return Token.LITERAL_INTEGER;
+                               return new IntLiteral (context.BuiltinTypes, 0, loc);
                        }
-                       
-                       return integer_type_suffix (ul, peek_char ());
                }
 
                //
@@ -1232,17 +1514,26 @@ namespace Mono.CSharp
                //
                int is_number (int c)
                {
-                       bool is_real = false;
-                       int type;
+                       ILiteralConstant res;
 
+#if FULL_AST
+                       int read_start = reader.Position - 1;
+#endif
                        number_pos = 0;
+                       var loc = Location;
 
                        if (c >= '0' && c <= '9'){
                                if (c == '0'){
                                        int peek = peek_char ();
 
-                                       if (peek == 'x' || peek == 'X')
-                                               return handle_hex ();
+                                       if (peek == 'x' || peek == 'X') {
+                                               val = res = handle_hex (loc);
+#if FULL_AST
+                                               res.ParsedValue = reader.ReadChars (read_start, reader.Position - 1);
+#endif
+
+                                               return Token.LITERAL;
+                                       }
                                }
                                decimal_digits (c);
                                c = get_char ();
@@ -1252,6 +1543,7 @@ namespace Mono.CSharp
                        // We need to handle the case of
                        // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
                        //
+                       bool is_real = false;
                        if (c == '.'){
                                if (decimal_digits ('.')){
                                        is_real = true;
@@ -1259,7 +1551,12 @@ namespace Mono.CSharp
                                } else {
                                        putback ('.');
                                        number_pos--;
-                                       return adjust_int (-1);
+                                       val = res = adjust_int (-1, loc);
+
+#if FULL_AST
+                                       res.ParsedValue = reader.ReadChars (read_start, reader.Position - 1);
+#endif
+                                       return Token.LITERAL;
                                }
                        }
                        
@@ -1267,7 +1564,7 @@ namespace Mono.CSharp
                                is_real = true;
                                if (number_pos == max_number_size)
                                        Error_NumericConstantTooLong ();
-                               number_builder [number_pos++] = 'e';
+                               number_builder [number_pos++] = (char) c;
                                c = get_char ();
                                
                                if (c == '+'){
@@ -1290,22 +1587,27 @@ namespace Mono.CSharp
                                c = get_char ();
                        }
 
-                       type = real_type_suffix (c);
-                       if (type == Token.NONE && !is_real){
+                       var type = real_type_suffix (c);
+                       if (type == TypeCode.Empty && !is_real) {
                                putback (c);
-                               return adjust_int (c);
-                       } else 
+                               res = adjust_int (c, loc);
+                       } else {
                                is_real = true;
 
-                       if (type == Token.NONE){
-                               putback (c);
+                               if (type == TypeCode.Empty) {
+                                       putback (c);
+                               }
+
+                               res = adjust_real (type, loc);
                        }
-                       
-                       if (is_real)
-                               return adjust_real (type);
 
-                       Console.WriteLine ("This should not be reached");
-                       throw new Exception ("Is Number should never reach this point");
+                       val = res;
+
+#if FULL_AST
+                       res.ParsedValue = reader.ReadChars (read_start, reader.Position - (type == TypeCode.Empty ? 1 : 0));
+#endif
+
+                       return Token.LITERAL;
                }
 
                //
@@ -1435,9 +1737,18 @@ namespace Mono.CSharp
                        if (putback_char != -1) {
                                x = putback_char;
                                putback_char = -1;
-                       } else
+                       } else {
                                x = reader.Read ();
-                       if (x == '\n') {
+                       }
+                       
+                       if (x == '\r') {
+                               if (peek_char () == '\n') {
+                                       putback_char = -1;
+                               }
+
+                               x = '\n';
+                               advance_line ();
+                       } else if (x == '\n') {
                                advance_line ();
                        } else {
                                col++;
@@ -1489,7 +1800,7 @@ namespace Mono.CSharp
 
                public bool advance ()
                {
-                       return peek_char () != -1;
+                       return peek_char () != -1 || CompleteOnEOF;
                }
 
                public Object Value {
@@ -1509,23 +1820,17 @@ namespace Mono.CSharp
                        return current_token;
                }
 
-               static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
-
-               void get_cmd_arg (out string cmd, out string arg)
+               int TokenizePreprocessorIdentifier (out int c)
                {
-                       int c;
-                       
-                       tokens_seen = false;
-                       arg = "";
-
                        // skip over white space
                        do {
                                c = get_char ();
-                       } while (c == '\r' || c == ' ' || c == '\t');
+                       } while (c == ' ' || c == '\t');
+
 
-                       static_cmd_arg.Length = 0;
-                       while (c != -1 && is_identifier_part_character ((char)c)) {
-                               static_cmd_arg.Append ((char)c);
+                       int pos = 0;
+                       while (c != -1 && c >= 'a' && c <= 'z') {
+                               id_builder[pos++] = (char) c;
                                c = get_char ();
                                if (c == '\\') {
                                        int peek = peek_char ();
@@ -1533,26 +1838,40 @@ namespace Mono.CSharp
                                                int surrogate;
                                                c = EscapeUnicode (c, out surrogate);
                                                if (surrogate != 0) {
-                                                       if (is_identifier_part_character ((char) c))
-                                                               static_cmd_arg.Append ((char) c);
+                                                       if (is_identifier_part_character ((char) c)) {
+                                                               id_builder[pos++] = (char) c;
+                                                       }
                                                        c = surrogate;
                                                }
                                        }
                                }
                        }
 
-                       cmd = static_cmd_arg.ToString ();
+                       return pos;
+               }
+
+               PreprocessorDirective get_cmd_arg (out string arg)
+               {
+                       int c;          
+
+                       tokens_seen = false;
+                       arg = "";
+
+                       var cmd = GetPreprocessorDirective (id_builder, TokenizePreprocessorIdentifier (out c));
+
+                       if ((cmd & PreprocessorDirective.CustomArgumentsParsing) != 0)
+                               return cmd;
 
                        // skip over white space
-                       while (c == '\r' || c == ' ' || c == '\t')
+                       while (c == ' ' || c == '\t')
                                c = get_char ();
 
-                       static_cmd_arg.Length = 0;
-                       int has_identifier_argument = 0;
+                       int has_identifier_argument = (int)(cmd & PreprocessorDirective.RequiresArgument);
+                       int pos = 0;
 
-                       while (c != -1 && c != '\n' && c != '\r') {
+                       while (c != -1 && c != '\n') {
                                if (c == '\\' && has_identifier_argument >= 0) {
-                                       if (has_identifier_argument != 0 || (cmd == "define" || cmd == "if" || cmd == "elif" || cmd == "undef")) {
+                                       if (has_identifier_argument != 0) {
                                                has_identifier_argument = 1;
 
                                                int peek = peek_char ();
@@ -1560,21 +1879,48 @@ namespace Mono.CSharp
                                                        int surrogate;
                                                        c = EscapeUnicode (c, out surrogate);
                                                        if (surrogate != 0) {
-                                                               if (is_identifier_part_character ((char) c))
-                                                                       static_cmd_arg.Append ((char) c);
+                                                               if (is_identifier_part_character ((char) c)) {
+                                                                       if (pos == value_builder.Length)
+                                                                               Array.Resize (ref value_builder, pos * 2);
+
+                                                                       value_builder[pos++] = (char) c;
+                                                               }
                                                                c = surrogate;
                                                        }
                                                }
                                        } else {
                                                has_identifier_argument = -1;
                                        }
+                               } else if (c == '/' && peek_char () == '/') {
+                                       //
+                                       // Eat single-line comments
+                                       //
+                                       get_char ();
+                                       do {
+                                               c = get_char ();
+                                       } while (c != -1 && c != '\n');
+
+                                       break;
                                }
-                               static_cmd_arg.Append ((char) c);
+
+                               if (pos == value_builder.Length)
+                                       Array.Resize (ref value_builder, pos * 2);
+
+                               value_builder[pos++] = (char) c;
                                c = get_char ();
                        }
 
-                       if (static_cmd_arg.Length != 0)
-                               arg = static_cmd_arg.ToString ();
+                       if (pos != 0) {
+                               if (pos > max_id_size)
+                                       arg = new string (value_builder, 0, pos);
+                               else
+                                       arg = InternIdentifier (value_builder, pos);
+
+                               // Eat any trailing whitespaces
+                               arg = arg.Trim (simple_whitespaces);
+                       }
+
+                       return cmd;
                }
 
                //
@@ -1606,8 +1952,8 @@ namespace Mono.CSharp
                                        char [] quotes = { '\"' };
                                        
                                        string name = arg.Substring (pos). Trim (quotes);
-                                       ref_name = Location.LookupFile (file_name, name);
-                                       file_name.AddFile (ref_name);
+                                       ref_name = context.LookupFile (file_name, name);
+                                       file_name.AddIncludeFile (ref_name);
                                        hidden = false;
                                        Location.Push (file_name, ref_name);
                                } else {
@@ -1653,7 +1999,7 @@ namespace Mono.CSharp
                                //
                                // #define ident
                                //
-                               if (RootContext.IsConditionalDefined (ident))
+                               if (context.Settings.IsConditionalSymbolDefined (ident))
                                        return;
 
                                file_name.AddDefine (ident);
@@ -1665,12 +2011,10 @@ namespace Mono.CSharp
                        }
                }
 
-               static byte read_hex (string arg, int pos, out bool error)
+               byte read_hex (out bool error)
                {
-                       error = false;
-
                        int total;
-                       char c = arg [pos];
+                       int c = get_char ();
 
                        if ((c >= '0') && (c <= '9'))
                                total = (int) c - (int) '0';
@@ -1684,7 +2028,7 @@ namespace Mono.CSharp
                        }
 
                        total *= 16;
-                       c = arg [pos+1];
+                       c = get_char ();
 
                        if ((c >= '0') && (c <= '9'))
                                total += (int) c - (int) '0';
@@ -1697,164 +2041,262 @@ namespace Mono.CSharp
                                return 0;
                        }
 
+                       error = false;
                        return (byte) total;
                }
 
-               /// <summary>
-               /// Handles #pragma checksum
-               /// </summary>
-               bool PreProcessPragmaChecksum (string arg)
+               //
+               // Parses #pragma checksum
+               //
+               bool ParsePragmaChecksum ()
                {
-                       if ((arg [0] != ' ') && (arg [0] != '\t'))
-                               return false;
+                       //
+                       // The syntax is ` "foo.txt" "{guid}" "hash"'
+                       //
+                       int c = get_char ();
 
-                       arg = arg.Trim (simple_whitespaces);
-                       if ((arg.Length < 2) || (arg [0] != '"'))
+                       if (c != '"')
                                return false;
 
-                       StringBuilder file_sb = new StringBuilder ();
-
-                       int pos = 1;
-                       char ch;
-                       while ((ch = arg [pos++]) != '"') {
-                               if (pos >= arg.Length)
-                                       return false;
-
-                               if (ch == '\\') {
-                                       if (pos+1 >= arg.Length)
-                                               return false;
-                                       ch = arg [pos++];
+                       string_builder.Length = 0;
+                       while (c != -1 && c != '\n') {
+                               c = get_char ();
+                               if (c == '"') {
+                                       c = get_char ();
+                                       break;
                                }
 
-                               file_sb.Append (ch);
+                               string_builder.Append ((char) c);
+                       }
+
+                       if (string_builder.Length == 0) {
+                               Report.Warning (1709, 1, Location, "Filename specified for preprocessor directive is empty");
                        }
 
-                       if ((pos+2 >= arg.Length) || ((arg [pos] != ' ') && (arg [pos] != '\t')))
+                       // TODO: Any white-spaces count
+                       if (c != ' ')
                                return false;
 
-                       arg = arg.Substring (pos).Trim (simple_whitespaces);
-                       if ((arg.Length < 42) || (arg [0] != '"') || (arg [1] != '{') ||
-                           (arg [10] != '-') || (arg [15] != '-') || (arg [20] != '-') ||
-                           (arg [25] != '-') || (arg [38] != '}') || (arg [39] != '"'))
+                       SourceFile file = context.LookupFile (file_name, string_builder.ToString ());
+
+                       if (get_char () != '"' || get_char () != '{')
                                return false;
 
                        bool error;
                        byte[] guid_bytes = new byte [16];
+                       int i = 0;
 
-                       for (int i = 0; i < 4; i++) {
-                               guid_bytes [i] = read_hex (arg, 2+2*i, out error);
+                       for (; i < 4; i++) {
+                               guid_bytes [i] = read_hex (out error);
                                if (error)
                                        return false;
                        }
-                       for (int i = 0; i < 2; i++) {
-                               guid_bytes [i+4] = read_hex (arg, 11+2*i, out error);
+
+                       if (get_char () != '-')
+                               return false;
+
+                       for (; i < 10; i++) {
+                               guid_bytes [i] = read_hex (out error);
                                if (error)
                                        return false;
-                               guid_bytes [i+6] = read_hex (arg, 16+2*i, out error);
+
+                               guid_bytes [i++] = read_hex (out error);
                                if (error)
                                        return false;
-                               guid_bytes [i+8] = read_hex (arg, 21+2*i, out error);
-                               if (error)
+
+                               if (get_char () != '-')
                                        return false;
                        }
 
-                       for (int i = 0; i < 6; i++) {
-                               guid_bytes [i+10] = read_hex (arg, 26+2*i, out error);
+                       for (; i < 16; i++) {
+                               guid_bytes [i] = read_hex (out error);
                                if (error)
                                        return false;
                        }
 
-                       arg = arg.Substring (40).Trim (simple_whitespaces);
-                       if ((arg.Length < 34) || (arg [0] != '"') || (arg [33] != '"'))
+                       if (get_char () != '}' || get_char () != '"')
+                               return false;
+
+                       // TODO: Any white-spaces count
+                       c = get_char ();
+                       if (c != ' ')
+                               return false;
+
+                       if (get_char () != '"')
                                return false;
 
-                       byte[] checksum_bytes = new byte [16];
-                       for (int i = 0; i < 16; i++) {
-                               checksum_bytes [i] = read_hex (arg, 1+2*i, out error);
+                       // Any length of checksum
+                       List<byte> checksum_bytes = new List<byte> (16);
+
+                       c = peek_char ();
+                       while (c != '"' && c != -1) {
+                               checksum_bytes.Add (read_hex (out error));
                                if (error)
                                        return false;
+
+                               c = peek_char ();
                        }
 
-                       arg = arg.Substring (34).Trim (simple_whitespaces);
-                       if (arg.Length > 0)
+                       if (c == '/') {
+                               ReadSingleLineComment ();
+                       } else if (get_char () != '"') {
                                return false;
+                       }
 
-                       SourceFile file = Location.LookupFile (file_name, file_sb.ToString ());
-                       file.SetChecksum (guid_bytes, checksum_bytes);
+                       file.SetChecksum (guid_bytes, checksum_bytes.ToArray ());
                        ref_name.AutoGenerated = true;
                        return true;
                }
 
+               bool IsTokenIdentifierEqual (char[] identifier)
+               {
+                       for (int i = 0; i < identifier.Length; ++i) {
+                               if (identifier[i] != id_builder[i])
+                                       return false;
+                       }
+
+                       return true;
+               }
+
+               int TokenizePragmaNumber (ref int c)
+               {
+                       number_pos = 0;
+
+                       int number;
+
+                       if (c >= '0' && c <= '9') {
+                               decimal_digits (c);
+                               uint ui = (uint) (number_builder[0] - '0');
+
+                               try {
+                                       for (int i = 1; i < number_pos; i++) {
+                                               ui = checked ((ui * 10) + ((uint) (number_builder[i] - '0')));
+                                       }
+
+                                       number = (int) ui;
+                               } catch (OverflowException) {
+                                       Error_NumericConstantTooLong ();
+                                       number = -1;
+                               }
+
+
+                               c = get_char ();
+
+                               // skip over white space
+                               while (c == ' ' || c == '\t')
+                                       c = get_char ();
+
+                               if (c == ',') {
+                                       c = get_char ();
+                               }
+
+                               // skip over white space
+                               while (c == ' ' || c == '\t')
+                                       c = get_char ();
+                       } else {
+                               number = -1;
+                               if (c == '/') {
+                                       ReadSingleLineComment ();
+                               } else {
+                                       Report.Warning (1692, 1, Location, "Invalid number");
+
+                                       // Read everything till the end of the line or file
+                                       do {
+                                               c = get_char ();
+                                       } while (c != -1 && c != '\n');
+                               }
+                       }
+
+                       return number;
+               }
+
+               void ReadSingleLineComment ()
+               {
+                       if (peek_char () != '/')
+                               Report.Warning (1696, 1, Location, "Single-line comment or end-of-line expected");
+
+                       // Read everything till the end of the line or file
+                       int c;
+                       do {
+                               c = get_char ();
+                       } while (c != -1 && c != '\n');
+               }
+
                /// <summary>
                /// Handles #pragma directive
                /// </summary>
-               void PreProcessPragma (string arg)
+               void ParsePragmaDirective (string arg)
                {
-                       const string warning = "warning";
-                       const string w_disable = "warning disable";
-                       const string w_restore = "warning restore";
-                       const string checksum = "checksum";
+                       int c;
+                       int length = TokenizePreprocessorIdentifier (out c);
+                       if (length == pragma_warning.Length && IsTokenIdentifierEqual (pragma_warning)) {
+                               length = TokenizePreprocessorIdentifier (out c);
 
-                       if (arg == w_disable) {
-                               Report.RegisterWarningRegion (Location).WarningDisable (Location.Row);
-                               return;
-                       }
+                               //
+                               // #pragma warning disable
+                               // #pragma warning restore
+                               //
+                               if (length == pragma_warning_disable.Length) {
+                                       bool disable = IsTokenIdentifierEqual (pragma_warning_disable);
+                                       if (disable || IsTokenIdentifierEqual (pragma_warning_restore)) {
+                                               // skip over white space
+                                               while (c == ' ' || c == '\t')
+                                                       c = get_char ();
 
-                       if (arg == w_restore) {
-                               Report.RegisterWarningRegion (Location).WarningEnable (Location.Row);
-                               return;
-                       }
+                                               var loc = Location;
 
-                       if (arg.StartsWith (w_disable)) {
-                               int[] codes = ParseNumbers (arg.Substring (w_disable.Length));
-                               foreach (int code in codes) {
-                                       if (code != 0)
-                                               Report.RegisterWarningRegion (Location).WarningDisable (Location, code);
-                               }
-                               return;
-                       }
+                                               if (c == '\n' || c == '/') {
+                                                       if (c == '/')
+                                                               ReadSingleLineComment ();
 
-                       if (arg.StartsWith (w_restore)) {
-                               int[] codes = ParseNumbers (arg.Substring (w_restore.Length));
-                               Hashtable w_table = Report.warning_ignore_table;
-                               foreach (int code in codes) {
-                                       if (w_table != null && w_table.Contains (code))
-                                               Report.Warning (1635, 1, Location, String.Format ("Cannot restore warning `CS{0:0000}' because it was disabled globally", code));
-                                       Report.RegisterWarningRegion (Location).WarningEnable (Location, code);
+                                                       //
+                                                       // Disable/Restore all warnings
+                                                       //
+                                                       if (disable) {
+                                                               Report.RegisterWarningRegion (loc).WarningDisable (loc.Row);
+                                                       } else {
+                                                               Report.RegisterWarningRegion (loc).WarningEnable (loc.Row);
+                                                       }
+                                               } else {
+                                                       //
+                                                       // Disable/Restore a warning or group of warnings
+                                                       //
+                                                       int code;
+                                                       do {
+                                                               code = TokenizePragmaNumber (ref c);
+                                                               if (code > 0) {
+                                                                       if (disable) {
+                                                                               Report.RegisterWarningRegion (loc).WarningDisable (loc, code, Report);
+                                                                       } else {
+                                                                               Report.RegisterWarningRegion (loc).WarningEnable (loc, code, Report);
+                                                                       }
+                                                               }
+                                                       } while (code >= 0 && c != '\n' && c != -1);
+                                               }
+
+                                               return;
+                                       }
                                }
-                               return;
-                       }
 
-                       if (arg.StartsWith (warning)) {
                                Report.Warning (1634, 1, Location, "Expected disable or restore");
                                return;
                        }
 
-                       if (arg.StartsWith (checksum)) {
-                               if (!PreProcessPragmaChecksum (arg.Substring (checksum.Length)))
-                                       Warning_InvalidPragmaChecksum ();
+                       //
+                       // #pragma checksum
+                       //
+                       if (length == pragma_checksum.Length && IsTokenIdentifierEqual (pragma_checksum)) {
+                               if (c != ' ' || !ParsePragmaChecksum ()) {
+                                       Report.Warning (1695, 1, Location,
+                                               "Invalid #pragma checksum syntax. Expected \"filename\" \"{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}\" \"XXXX...\"");
+                               }
+
                                return;
                        }
 
                        Report.Warning (1633, 1, Location, "Unrecognized #pragma directive");
                }
 
-               int[] ParseNumbers (string text)
-               {
-                       string[] string_array = text.Split (',');
-                       int[] values = new int [string_array.Length];
-                       int index = 0;
-                       foreach (string string_code in string_array) {
-                               try {
-                                       values[index++] = int.Parse (string_code, System.Globalization.CultureInfo.InvariantCulture);
-                               }
-                               catch (FormatException) {
-                                       Report.Warning (1692, 1, Location, "Invalid number");
-                               }
-                       }
-                       return values;
-               }
-
                bool eval_val (string s)
                {
                        if (s == "true")
@@ -1862,7 +2304,7 @@ namespace Mono.CSharp
                        if (s == "false")
                                return false;
 
-                       return file_name.IsConditionalDefined (s);
+                       return file_name.IsConditionalDefined (context, s);
                }
 
                bool pp_primary (ref string s)
@@ -2017,7 +2459,7 @@ namespace Mono.CSharp
 
                void Error_NumericConstantTooLong ()
                {
-                       Report.Error (1021, Location, "Numeric constant too long");                     
+                       Report.Error (1021, Location, "Integral constant is too large");                        
                }
                
                void Error_InvalidDirective ()
@@ -2048,80 +2490,80 @@ namespace Mono.CSharp
                {
                        Report.Error (1025, Location, "Single-line comment or end-of-line expected");
                }
-               
-               void Warning_InvalidPragmaChecksum ()
+
+               //
+               // Raises a warning when tokenizer found documentation comment
+               // on unexpected place
+               //
+               void WarningMisplacedComment (Location loc)
                {
-                       Report.Warning (1695, 1, Location,
-                                       "Invalid #pragma checksum syntax; should be " +
-                                       "#pragma checksum \"filename\" " +
-                                       "\"{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}\" \"XXXX...\"");
+                       if (doc_state != XmlCommentState.Error) {
+                               doc_state = XmlCommentState.Error;
+                               Report.Warning (1587, 2, loc, "XML comment is not placed on a valid language element");
+                       }
                }
+               
                //
                // if true, then the code continues processing the code
                // if false, the code stays in a loop until another directive is
                // reached.
                // When caller_is_taking is false we ignore all directives except the ones
                // which can help us to identify where the #if block ends
-               bool handle_preprocessing_directive (bool caller_is_taking)
+               bool ParsePreprocessingDirective (bool caller_is_taking)
                {
-                       string cmd, arg;
+                       string arg;
                        bool region_directive = false;
 
-                       get_cmd_arg (out cmd, out arg);
-
-                       // Eat any trailing whitespaces and single-line comments
-                       if (arg.IndexOf ("//") != -1)
-                               arg = arg.Substring (0, arg.IndexOf ("//"));
-                       arg = arg.Trim (simple_whitespaces);
+                       var directive = get_cmd_arg (out arg);
 
                        //
                        // The first group of pre-processing instructions is always processed
                        //
-                       switch (cmd){
-                       case "region":
+                       switch (directive) {
+                       case PreprocessorDirective.Region:
                                region_directive = true;
                                arg = "true";
-                               goto case "if";
+                               goto case PreprocessorDirective.If;
 
-                       case "endregion":
+                       case PreprocessorDirective.Endregion:
                                if (ifstack == null || ifstack.Count == 0){
                                        Error_UnexpectedDirective ("no #region for this #endregion");
                                        return true;
                                }
-                               int pop = (int) ifstack.Pop ();
+                               int pop = ifstack.Pop ();
                                        
                                if ((pop & REGION) == 0)
                                        Report.Error (1027, Location, "Expected `#endif' directive");
                                        
                                return caller_is_taking;
                                
-                       case "if":
+                       case PreprocessorDirective.If:
                                if (ifstack == null)
-                                       ifstack = new Stack (2);
+                                       ifstack = new Stack<int> (2);
 
                                int flags = region_directive ? REGION : 0;
                                if (ifstack.Count == 0){
                                        flags |= PARENT_TAKING;
                                } else {
-                                       int state = (int) ifstack.Peek ();
+                                       int state = ifstack.Peek ();
                                        if ((state & TAKING) != 0) {
                                                flags |= PARENT_TAKING;
                                        }
                                }
 
-                               if (caller_is_taking && eval (arg)) {
+                               if (eval (arg) && caller_is_taking) {
                                        ifstack.Push (flags | TAKING);
                                        return true;
                                }
                                ifstack.Push (flags);
                                return false;
-                               
-                       case "endif":
+
+                       case PreprocessorDirective.Endif:
                                if (ifstack == null || ifstack.Count == 0){
                                        Error_UnexpectedDirective ("no #if for this #endif");
                                        return true;
                                } else {
-                                       pop = (int) ifstack.Pop ();
+                                       pop = ifstack.Pop ();
                                        
                                        if ((pop & REGION) != 0)
                                                Report.Error (1038, Location, "#endregion directive expected");
@@ -2133,16 +2575,16 @@ namespace Mono.CSharp
                                        if (ifstack.Count == 0)
                                                return true;
 
-                                       int state = (int) ifstack.Peek ();
+                                       int state = ifstack.Peek ();
                                        return (state & TAKING) != 0;
                                }
 
-                       case "elif":
+                       case PreprocessorDirective.Elif:
                                if (ifstack == null || ifstack.Count == 0){
                                        Error_UnexpectedDirective ("no #if for this #elif");
                                        return true;
                                } else {
-                                       int state = (int) ifstack.Pop ();
+                                       int state = ifstack.Pop ();
 
                                        if ((state & REGION) != 0) {
                                                Report.Error (1038, Location, "#endregion directive expected");
@@ -2168,12 +2610,12 @@ namespace Mono.CSharp
                                        return false;
                                }
 
-                       case "else":
+                       case PreprocessorDirective.Else:
                                if (ifstack == null || ifstack.Count == 0){
                                        Error_UnexpectedDirective ("no #if for this #else");
                                        return true;
                                } else {
-                                       int state = (int) ifstack.Peek ();
+                                       int state = ifstack.Peek ();
 
                                        if ((state & REGION) != 0) {
                                                Report.Error (1038, Location, "#endregion directive expected");
@@ -2206,7 +2648,7 @@ namespace Mono.CSharp
                                        
                                        return ret;
                                }
-                       case "define":
+                       case PreprocessorDirective.Define:
                                if (any_token_seen){
                                        Error_TokensSeen ();
                                        return caller_is_taking;
@@ -2214,13 +2656,17 @@ namespace Mono.CSharp
                                PreProcessDefinition (true, arg, caller_is_taking);
                                return caller_is_taking;
 
-                       case "undef":
+                       case PreprocessorDirective.Undef:
                                if (any_token_seen){
                                        Error_TokensSeen ();
                                        return caller_is_taking;
                                }
                                PreProcessDefinition (false, arg, caller_is_taking);
                                return caller_is_taking;
+
+                       case PreprocessorDirective.Invalid:
+                               Report.Error (1024, Location, "Wrong preprocessor directive");
+                               return true;
                        }
 
                        //
@@ -2229,25 +2675,24 @@ namespace Mono.CSharp
                        if (!caller_is_taking)
                                return false;
                                        
-                       switch (cmd){
-                       case "error":
+                       switch (directive){
+                       case PreprocessorDirective.Error:
                                Report.Error (1029, Location, "#error: '{0}'", arg);
                                return true;
 
-                       case "warning":
+                       case PreprocessorDirective.Warning:
                                Report.Warning (1030, 1, Location, "#warning: `{0}'", arg);
                                return true;
 
-                       case "pragma":
-                               if (RootContext.Version == LanguageVersion.ISO_1) {
-                                       Report.FeatureIsNotAvailable (Location, "#pragma");
-                                       return true;
+                       case PreprocessorDirective.Pragma:
+                               if (context.Settings.Version == LanguageVersion.ISO_1) {
+                                       Report.FeatureIsNotAvailable (context, Location, "#pragma");
                                }
 
-                               PreProcessPragma (arg);
+                               ParsePragmaDirective (arg);
                                return true;
 
-                       case "line":
+                       case PreprocessorDirective.Line:
                                if (!PreProcessLine (arg))
                                        Report.Error (
                                                1576, Location,
@@ -2255,48 +2700,80 @@ namespace Mono.CSharp
                                return caller_is_taking;
                        }
 
-                       Report.Error (1024, Location, "Wrong preprocessor directive");
-                       return true;
-
+                       throw new NotImplementedException (directive.ToString ());
                }
 
                private int consume_string (bool quoted)
                {
                        int c;
-                       string_builder.Length = 0;
+                       int pos = 0;
+                       Location start_location = Location;
+                       if (quoted)
+                               start_location = start_location - 1;
+
+#if FULL_AST
+                       int reader_pos = reader.Position;
+#endif
+
+                       while (true){
+                               c = get_char ();
+                               if (c == '"') {
+                                       if (quoted && peek_char () == '"') {
+                                               if (pos == value_builder.Length)
+                                                       Array.Resize (ref value_builder, pos * 2);
 
-                       while ((c = get_char ()) != -1){
-                               if (c == '"'){
-                                       if (quoted && peek_char () == '"'){
-                                               string_builder.Append ((char) c);
+                                               value_builder[pos++] = (char) c;
                                                get_char ();
                                                continue;
-                                       } else {
-                                               val = string_builder.ToString ();
-                                               return Token.LITERAL_STRING;
                                        }
-                               }
 
-                               if (c == '\n'){
-                                       if (!quoted)
-                                               Report.Error (1010, Location, "Newline in constant");
+                                       string s;
+                                       if (pos == 0)
+                                               s = string.Empty;
+                                       else if (pos <= 4)
+                                               s = InternIdentifier (value_builder, pos);
+                                       else
+                                               s = new string (value_builder, 0, pos);
+
+                                       ILiteralConstant res = new StringLiteral (context.BuiltinTypes, s, start_location);
+                                       val = res;
+#if FULL_AST
+                                       res.ParsedValue = quoted ?
+                                               reader.ReadChars (reader_pos - 2, reader.Position - 1) :
+                                               reader.ReadChars (reader_pos - 1, reader.Position);
+#endif
+
+                                       return Token.LITERAL;
                                }
 
-                               if (!quoted){
+                               if (c == '\n') {
+                                       if (!quoted) {
+                                               Report.Error (1010, Location, "Newline in constant");
+                                               val = new StringLiteral (context.BuiltinTypes, new string (value_builder, 0, pos), start_location);
+                                               return Token.LITERAL;
+                                       }
+                               } else if (c == '\\' && !quoted) {
                                        int surrogate;
                                        c = escape (c, out surrogate);
                                        if (c == -1)
                                                return Token.ERROR;
                                        if (surrogate != 0) {
-                                               string_builder.Append ((char) c);
+                                               if (pos == value_builder.Length)
+                                                       Array.Resize (ref value_builder, pos * 2);
+
+                                               value_builder[pos++] = (char) c;
                                                c = surrogate;
                                        }
+                               } else if (c == -1) {
+                                       Report.Error (1039, Location, "Unterminated string literal");
+                                       return Token.EOF;
                                }
-                               string_builder.Append ((char) c);
-                       }
 
-                       Report.Error (1039, Location, "Unterminated string literal");
-                       return Token.EOF;
+                               if (pos == value_builder.Length)
+                                       Array.Resize (ref value_builder, pos * 2);
+
+                               value_builder[pos++] = (char) c;
+                       }
                }
 
                private int consume_identifier (int s)
@@ -2309,9 +2786,17 @@ namespace Mono.CSharp
                        return res;
                }
 
-               private int consume_identifier (int c, bool quoted) 
+               int consume_identifier (int c, bool quoted) 
                {
+                       //
+                       // This method is very performance sensitive. It accounts
+                       // for approximately 25% of all parser time
+                       //
+
                        int pos = 0;
+                       int column = col;
+                       if (quoted)
+                               --column;
 
                        if (c == '\\') {
                                int surrogate;
@@ -2323,32 +2808,45 @@ namespace Mono.CSharp
                        }
 
                        id_builder [pos++] = (char) c;
-                       Location loc = Location;
 
-                       while ((c = get_char ()) != -1) {
-                       loop:
-                               if (is_identifier_part_character ((char) c)){
-                                       if (pos == max_id_size){
-                                               Report.Error (645, loc, "Identifier too long (limit is 512 chars)");
-                                               return Token.ERROR;
+                       try {
+                               while (true) {
+                                       c = reader.Read ();
+
+                                       if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9')) {
+                                               id_builder [pos++] = (char) c;
+                                               continue;
                                        }
-                                       
-                                       id_builder [pos++] = (char) c;
-                               } else if (c == '\\') {
-                                       int surrogate;
-                                       c = escape (c, out surrogate);
-                                       if (surrogate != 0) {
-                                               if (is_identifier_part_character ((char) c))
-                                                       id_builder [pos++] = (char) c;
-                                               c = surrogate;
+
+                                       if (c < 0x80) {
+                                               if (c == '\\') {
+                                                       int surrogate;
+                                                       c = escape (c, out surrogate);
+                                                       if (is_identifier_part_character ((char) c))
+                                                               id_builder[pos++] = (char) c;
+
+                                                       if (surrogate != 0) {
+                                                               c = surrogate;
+                                                       }
+
+                                                       continue;
+                                               }
+                                       } else if (Char.IsLetter ((char) c) || Char.GetUnicodeCategory ((char) c) == UnicodeCategory.ConnectorPunctuation) {
+                                               id_builder [pos++] = (char) c;
+                                               continue;
                                        }
-                                       goto loop;
-                               } else {
-                                       putback (c);
+
+                                       putback_char = c;
                                        break;
                                }
+                       } catch (IndexOutOfRangeException) {
+                               Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
+                               --pos;
+                               col += pos;
                        }
 
+                       col += pos - 1;
+
                        //
                        // Optimization: avoids doing the keyword lookup
                        // on uppercase letters
@@ -2356,49 +2854,45 @@ namespace Mono.CSharp
                        if (id_builder [0] >= '_' && !quoted) {
                                int keyword = GetKeyword (id_builder, pos);
                                if (keyword != -1) {
-                                       // TODO: No need to store location for keyword, required location cleanup
-                                       val = loc;
+                                       val = LocatedToken.Create (keyword == Token.AWAIT ? "await" : null, ref_line, column);
                                        return keyword;
                                }
                        }
 
+                       string s = InternIdentifier (id_builder, pos);
+                       val = LocatedToken.Create (s, ref_line, column);
+                       if (quoted && parsing_attribute_section)
+                               AddEscapedIdentifier (((LocatedToken) val).Location);
+
+                       return Token.IDENTIFIER;
+               }
+
+               static string InternIdentifier (char[] charBuffer, int length)
+               {
                        //
                        // Keep identifiers in an array of hashtables to avoid needless
                        // allocations
                        //
-                       CharArrayHashtable identifiers_group = identifiers [pos];
+                       var identifiers_group = identifiers[length];
+                       string s;
                        if (identifiers_group != null) {
-                               val = identifiers_group [id_builder];
-                               if (val != null) {
-                                       val = new LocatedToken (loc, (string) val);
-                                       if (quoted)
-                                               AddEscapedIdentifier ((LocatedToken) val);
-                                       return Token.IDENTIFIER;
+                               if (identifiers_group.TryGetValue (charBuffer, out s)) {
+                                       return s;
                                }
                        } else {
-                               identifiers_group = new CharArrayHashtable (pos);
-                               identifiers [pos] = identifiers_group;
+                               // TODO: this should be number of files dependant
+                               // corlib compilation peaks at 1000 and System.Core at 150
+                               int capacity = length > 20 ? 10 : 100;
+                               identifiers_group = new Dictionary<char[], string> (capacity, new IdentifiersComparer (length));
+                               identifiers[length] = identifiers_group;
                        }
 
-                       char [] chars = new char [pos];
-                       Array.Copy (id_builder, chars, pos);
-
-                       val = new String (id_builder, 0, pos);
-                       identifiers_group.Add (chars, val);
+                       char[] chars = new char[length];
+                       Array.Copy (charBuffer, chars, length);
 
-                       if (RootContext.Version == LanguageVersion.ISO_1) {
-                               for (int i = 1; i < chars.Length; i += 3) {
-                                       if (chars [i] == '_' && (chars [i - 1] == '_' || chars [i + 1] == '_')) {
-                                               Report.Error (1638, loc,
-                                                       "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val.ToString ());
-                                       }
-                               }
-                       }
-
-                       val = new LocatedToken (loc, (string) val);
-                       if (quoted)
-                               AddEscapedIdentifier ((LocatedToken) val);
-                       return Token.IDENTIFIER;
+                       s = new string (charBuffer, 0, length);
+                       identifiers_group.Add (chars, s);
+                       return s;
                }
                
                public int xtoken ()
@@ -2410,7 +2904,7 @@ namespace Mono.CSharp
                        while ((c = get_char ()) != -1) {
                                switch (c) {
                                case '\t':
-                                       col = ((col + 8) / 8) * 8;
+                                       col = ((col - 1 + tab_size) / tab_size) * tab_size;
                                        continue;
 
                                case ' ':
@@ -2432,36 +2926,51 @@ namespace Mono.CSharp
                                        }
                                        break;
 */
-                               case '\r':
-                                       if (peek_char () != '\n')
-                                               advance_line ();
-                                       else
-                                               get_char ();
-
-                                       any_token_seen |= tokens_seen;
-                                       tokens_seen = false;
-                                       comments_seen = false;
-                                       continue;
-
                                case '\\':
                                        tokens_seen = true;
                                        return consume_identifier (c);
 
                                case '{':
-                                       val = Location;
+                                       val = LocatedToken.Create (ref_line, col);
                                        return Token.OPEN_BRACE;
                                case '}':
-                                       val = Location;
+                                       val = LocatedToken.Create (ref_line, col);
                                        return Token.CLOSE_BRACE;
                                case '[':
                                        // To block doccomment inside attribute declaration.
                                        if (doc_state == XmlCommentState.Allowed)
                                                doc_state = XmlCommentState.NotAllowed;
-                                       return Token.OPEN_BRACKET;
+
+                                       val = LocatedToken.Create (ref_line, col);
+
+                                       if (parsing_block == 0 || lambda_arguments_parsing)
+                                               return Token.OPEN_BRACKET;
+
+                                       int next = peek_char ();
+                                       switch (next) {
+                                       case ']':
+                                       case ',':
+                                               return Token.OPEN_BRACKET;
+
+                                       case ' ':
+                                       case '\f':
+                                       case '\v':
+                                       case '\r':
+                                       case '\n':
+                                       case '/':
+                                               next = peek_token ();
+                                               if (next == Token.COMMA || next == Token.CLOSE_BRACKET)
+                                                       return Token.OPEN_BRACKET;
+
+                                               return Token.OPEN_BRACKET_EXPR;
+                                       default:
+                                               return Token.OPEN_BRACKET_EXPR;
+                                       }
                                case ']':
+                                       LocatedToken.CreateOptional (ref_line, col, ref val);
                                        return Token.CLOSE_BRACKET;
                                case '(':
-                                       val = Location;
+                                       val = LocatedToken.Create (ref_line, col);
                                        //
                                        // An expression versions of parens can appear in block context only
                                        //
@@ -2506,22 +3015,29 @@ namespace Mono.CSharp
 
                                        return Token.OPEN_PARENS;
                                case ')':
+                                       LocatedToken.CreateOptional (ref_line, col, ref val);
                                        return Token.CLOSE_PARENS;
                                case ',':
+                                       LocatedToken.CreateOptional (ref_line, col, ref val);
                                        return Token.COMMA;
                                case ';':
+                                       LocatedToken.CreateOptional (ref_line, col, ref val);
                                        return Token.SEMICOLON;
                                case '~':
+                                       val = LocatedToken.Create (ref_line, col);
                                        return Token.TILDE;
                                case '?':
+                                       val = LocatedToken.Create (ref_line, col);
                                        return TokenizePossibleNullableType ();
                                case '<':
+                                       val = LocatedToken.Create (ref_line, col);
                                        if (parsing_generic_less_than++ > 0)
                                                return Token.OP_GENERICS_LT;
 
                                        return TokenizeLessThan ();
 
                                case '>':
+                                       val = LocatedToken.Create (ref_line, col);
                                        d = peek_char ();
 
                                        if (d == '='){
@@ -2546,8 +3062,9 @@ namespace Mono.CSharp
                                        }
 
                                        return Token.OP_GT;
-                               
+
                                case '+':
+                                       val = LocatedToken.Create (ref_line, col);
                                        d = peek_char ();
                                        if (d == '+') {
                                                d = Token.OP_INC;
@@ -2560,6 +3077,7 @@ namespace Mono.CSharp
                                        return d;
 
                                case '-':
+                                       val = LocatedToken.Create (ref_line, col);
                                        d = peek_char ();
                                        if (d == '-') {
                                                d = Token.OP_DEC;
@@ -2574,6 +3092,7 @@ namespace Mono.CSharp
                                        return d;
 
                                case '!':
+                                       val = LocatedToken.Create (ref_line, col);
                                        if (peek_char () == '='){
                                                get_char ();
                                                return Token.OP_NE;
@@ -2581,6 +3100,7 @@ namespace Mono.CSharp
                                        return Token.BANG;
 
                                case '=':
+                                       val = LocatedToken.Create (ref_line, col);
                                        d = peek_char ();
                                        if (d == '='){
                                                get_char ();
@@ -2594,6 +3114,7 @@ namespace Mono.CSharp
                                        return Token.ASSIGN;
 
                                case '&':
+                                       val = LocatedToken.Create (ref_line, col);
                                        d = peek_char ();
                                        if (d == '&'){
                                                get_char ();
@@ -2606,6 +3127,7 @@ namespace Mono.CSharp
                                        return Token.BITWISE_AND;
 
                                case '|':
+                                       val = LocatedToken.Create (ref_line, col);
                                        d = peek_char ();
                                        if (d == '|'){
                                                get_char ();
@@ -2618,16 +3140,17 @@ namespace Mono.CSharp
                                        return Token.BITWISE_OR;
 
                                case '*':
+                                       val = LocatedToken.Create (ref_line, col);
                                        if (peek_char () == '='){
                                                get_char ();
                                                return Token.OP_MULT_ASSIGN;
                                        }
-                                       val = Location;
                                        return Token.STAR;
 
                                case '/':
                                        d = peek_char ();
                                        if (d == '='){
+                                               val = LocatedToken.Create (ref_line, col);
                                                get_char ();
                                                return Token.OP_DIV_ASSIGN;
                                        }
@@ -2635,18 +3158,23 @@ namespace Mono.CSharp
                                        // Handle double-slash comments.
                                        if (d == '/'){
                                                get_char ();
-                                               if (RootContext.Documentation != null && peek_char () == '/') {
-                                                       get_char ();
-                                                       // Don't allow ////.
-                                                       if ((d = peek_char ()) != '/') {
-                                                               update_comment_location ();
-                                                               if (doc_state == XmlCommentState.Allowed)
-                                                                       handle_one_line_xml_comment ();
-                                                               else if (doc_state == XmlCommentState.NotAllowed)
-                                                                       warn_incorrect_doc_comment ();
+                                               if (doc_processing) {
+                                                       if (peek_char () == '/') {
+                                                               get_char ();
+                                                               // Don't allow ////.
+                                                               if ((d = peek_char ()) != '/') {
+                                                                       if (doc_state == XmlCommentState.Allowed)
+                                                                               handle_one_line_xml_comment ();
+                                                                       else if (doc_state == XmlCommentState.NotAllowed)
+                                                                               WarningMisplacedComment (Location - 3);
+                                                               }
+                                                       } else {
+                                                               if (xml_comment_buffer.Length > 0)
+                                                                       doc_state = XmlCommentState.NotAllowed;
                                                        }
                                                }
-                                               while ((d = get_char ()) != -1 && (d != '\n') && d != '\r');
+
+                                               while ((d = get_char ()) != -1 && d != '\n');
 
                                                any_token_seen |= tokens_seen;
                                                tokens_seen = false;
@@ -2655,9 +3183,8 @@ namespace Mono.CSharp
                                        } else if (d == '*'){
                                                get_char ();
                                                bool docAppend = false;
-                                               if (RootContext.Documentation != null && peek_char () == '*') {
+                                               if (doc_processing && peek_char () == '*') {
                                                        get_char ();
-                                                       update_comment_location ();
                                                        // But when it is /**/, just do nothing.
                                                        if (peek_char () == '/') {
                                                                get_char ();
@@ -2665,8 +3192,9 @@ namespace Mono.CSharp
                                                        }
                                                        if (doc_state == XmlCommentState.Allowed)
                                                                docAppend = true;
-                                                       else if (doc_state == XmlCommentState.NotAllowed)
-                                                               warn_incorrect_doc_comment ();
+                                                       else if (doc_state == XmlCommentState.NotAllowed) {
+                                                               WarningMisplacedComment (Location - 2);
+                                                       }
                                                }
 
                                                int current_comment_start = 0;
@@ -2701,9 +3229,11 @@ namespace Mono.CSharp
                                                        update_formatted_doc_comment (current_comment_start);
                                                continue;
                                        }
+                                       val = LocatedToken.Create (ref_line, col);
                                        return Token.DIV;
 
                                case '%':
+                                       val = LocatedToken.Create (ref_line, col);
                                        if (peek_char () == '='){
                                                get_char ();
                                                return Token.OP_MOD_ASSIGN;
@@ -2711,6 +3241,7 @@ namespace Mono.CSharp
                                        return Token.PERCENT;
 
                                case '^':
+                                       val = LocatedToken.Create (ref_line, col);
                                        if (peek_char () == '='){
                                                get_char ();
                                                return Token.OP_XOR_ASSIGN;
@@ -2718,6 +3249,7 @@ namespace Mono.CSharp
                                        return Token.CARRET;
 
                                case ':':
+                                       val = LocatedToken.Create (ref_line, col);
                                        if (peek_char () == ':') {
                                                get_char ();
                                                return Token.DOUBLE_COLON;
@@ -2740,6 +3272,8 @@ namespace Mono.CSharp
                                        d = peek_char ();
                                        if (d >= '0' && d <= '9')
                                                return is_number (c);
+
+                                       LocatedToken.CreateOptional (ref_line, col, ref val);
                                        return Token.DOT;
                                
                                case '#':
@@ -2748,7 +3282,7 @@ namespace Mono.CSharp
                                                return Token.ERROR;
                                        }
                                        
-                                       if (handle_preprocessing_directive (true))
+                                       if (ParsePreprocessingDirective (true))
                                                continue;
 
                                        bool directive_expected = false;
@@ -2764,11 +3298,11 @@ namespace Mono.CSharp
                                                        continue;
                                                }
 
-                                               if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v' )
+                                               if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\v' )
                                                        continue;
 
                                                if (c == '#') {
-                                                       if (handle_preprocessing_directive (false))
+                                                       if (ParsePreprocessingDirective (false))
                                                                break;
                                                }
                                                directive_expected = false;
@@ -2807,6 +3341,8 @@ namespace Mono.CSharp
                                        return Token.EVAL_COMPILATION_UNIT_PARSER;
                                case EvalUsingDeclarationsParserCharacter:
                                        return Token.EVAL_USING_DECLARATIONS_UNIT_PARSER;
+                               case DocumentationXref:
+                                       return Token.DOC_SEE;
                                }
 
                                if (is_identifier_start_character (c)) {
@@ -2814,24 +3350,40 @@ namespace Mono.CSharp
                                        return consume_identifier (c);
                                }
 
-                               error_details = ((char)c).ToString ();
-                               return Token.ERROR;
+                               if (char.IsWhiteSpace ((char) c))
+                                       continue;
+
+                               Report.Error (1056, Location, "Unexpected character `{0}'", ((char) c).ToString ());
+                       }
+
+                       if (CompleteOnEOF){
+                               if (generated)
+                                       return Token.COMPLETE_COMPLETION;
+                               
+                               generated = true;
+                               return Token.GENERATE_COMPLETION;
                        }
                        
+
                        return Token.EOF;
                }
 
                int TokenizeBackslash ()
                {
+#if FULL_AST
+                       int read_start = reader.Position;
+#endif
+                       Location start_location = Location;
                        int c = get_char ();
                        tokens_seen = true;
                        if (c == '\'') {
-                               error_details = "Empty character literal";
-                               Report.Error (1011, Location, error_details);
-                               return Token.ERROR;
+                               val = new CharLiteral (context.BuiltinTypes, (char) c, start_location);
+                               Report.Error (1011, start_location, "Empty character literal");
+                               return Token.LITERAL;
                        }
-                       if (c == '\r' || c == '\n') {
-                               Report.Error (1010, Location, "Newline in constant");
+
+                       if (c == '\n') {
+                               Report.Error (1010, start_location, "Newline in constant");
                                return Token.ERROR;
                        }
 
@@ -2842,21 +3394,25 @@ namespace Mono.CSharp
                        if (d != 0)
                                throw new NotImplementedException ();
 
-                       val = (char) c;
+                       ILiteralConstant res = new CharLiteral (context.BuiltinTypes, (char) c, start_location);
+                       val = res;
                        c = get_char ();
 
                        if (c != '\'') {
-                               Report.Error (1012, Location, "Too many characters in character literal");
+                               Report.Error (1012, start_location, "Too many characters in character literal");
 
                                // Try to recover, read until newline or next "'"
                                while ((c = get_char ()) != -1) {
                                        if (c == '\n' || c == '\'')
                                                break;
                                }
-                               return Token.ERROR;
                        }
 
-                       return Token.LITERAL_CHARACTER;
+#if FULL_AST
+                       res.ParsedValue = reader.ReadChars (read_start - 1, reader.Position);
+#endif
+
+                       return Token.LITERAL;
                }
 
                int TokenizeLessThan ()
@@ -2875,7 +3431,7 @@ namespace Mono.CSharp
                        // Save current position and parse next token.
                        PushPosition ();
                        if (parse_less_than ()) {
-                               if (parsing_generic_declaration && token () != Token.DOT) {
+                               if (parsing_generic_declaration && (parsing_generic_declaration_doc || token () != Token.DOT)) {
                                        d = Token.OP_GENERICS_LT_DECL;
                                } else {
                                        d = Token.OP_GENERICS_LT;
@@ -2952,18 +3508,6 @@ namespace Mono.CSharp
                        xml_comment_buffer.Insert (current_comment_start, String.Join (Environment.NewLine, lines));
                }
 
-               //
-               // Updates current comment location.
-               //
-               private void update_comment_location ()
-               {
-                       if (current_comment_location.IsNull) {
-                               // "-2" is for heading "//" or "/*"
-                               current_comment_location =
-                                       new Location (ref_line, hidden ? -1 : col - 2);
-                       }
-               }
-
                //
                // Checks if there was incorrect doc comments and raise
                // warnings.
@@ -2971,22 +3515,7 @@ namespace Mono.CSharp
                public void check_incorrect_doc_comment ()
                {
                        if (xml_comment_buffer.Length > 0)
-                               warn_incorrect_doc_comment ();
-               }
-
-               //
-               // Raises a warning when tokenizer found incorrect doccomment
-               // markup.
-               //
-               private void warn_incorrect_doc_comment ()
-               {
-                       if (doc_state != XmlCommentState.Error) {
-                               doc_state = XmlCommentState.Error;
-                               // in csc, it is 'XML comment is not placed on 
-                               // a valid language element'. But that does not
-                               // make sense.
-                               Report.Warning (1587, 2, Location, "XML comment is not placed on a valid language element");
-                       }
+                               WarningMisplacedComment (Location);
                }
 
                //
@@ -3003,16 +3532,19 @@ namespace Mono.CSharp
                        return null;
                }
 
+               Report Report {
+                       get { return context.Report; }
+               }
+
                void reset_doc_comment ()
                {
                        xml_comment_buffer.Length = 0;
-                       current_comment_location = Location.Null;
                }
 
                public void cleanup ()
                {
                        if (ifstack != null && ifstack.Count >= 1) {
-                               int state = (int) ifstack.Pop ();
+                               int state = ifstack.Pop ();
                                if ((state & REGION) != 0)
                                        Report.Error (1038, Location, "#endregion directive expected");
                                else