2008-10-13 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / cs-tokenizer.cs
index bef0ea27b436e1689c755ca44908117b5576de70..b68ec2bb70bbd39a52353e506df663b74559441f 100644 (file)
@@ -5,10 +5,10 @@
 // Author: Miguel de Icaza (miguel@gnu.org)
 //         Marek Safar (marek.safar@seznam.cz)
 //
-// Licensed under the terms of the GNU GPL
+// Dual licensed under the terms of the MIT X11 or GNU GPL
 //
-// (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
-// (C) 2004 Novell, Inc
+// Copyright 2001, 2002 Ximian, Inc (http://www.ximian.com)
+// Copyright 2004-2008 Novell, Inc
 //
 //
 
@@ -29,7 +29,8 @@ namespace Mono.CSharp
        {
                SeekableStreamReader reader;
                SourceFile ref_name;
-               SourceFile file_name;
+               CompilationUnit file_name;
+               bool hidden = false;
                int ref_line = 1;
                int line = 1;
                int col = 0;
@@ -37,14 +38,46 @@ namespace Mono.CSharp
                int current_token;
                bool handle_get_set = false;
                bool handle_remove_add = false;
-               bool handle_assembly = false;
-               bool handle_constraints = false;
+               bool handle_where = false;
                bool handle_typeof = false;
-               bool linq;
+               bool lambda_arguments_parsing;
                Location current_location;
                Location current_comment_location = Location.Null;
-               ArrayList escapedIdentifiers = new ArrayList ();
+               ArrayList escaped_identifiers = new ArrayList ();
+               
+               //
+               // Used mainly for parser optimizations. Some expressions for instance
+               // can appear only in block (including initializer, base initializer)
+               // scope only
+               //
+               public int parsing_block;
+               internal int query_parsing;
+               
+               //
+               // Set when parsing generic declaration (type or method header)
+               //
+               public bool parsing_generic_declaration;
+               
+               //
+               // The value indicates that we have not reach any declaration or
+               // namespace yet
+               //
+               public int parsing_declaration;
 
+               //
+               // 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).
+               //
+               // 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
+               
                //
                // XML documentation buffer. The save point is used to divide
                // comments on types and comments on members.
@@ -54,7 +87,7 @@ namespace Mono.CSharp
                //
                // See comment on XmlCommentState enumeration.
                //
-               XmlCommentState xmlDocState = XmlCommentState.Allowed;
+               XmlCommentState xml_doc_state = XmlCommentState.Allowed;
 
                //
                // Whether tokens have been seen on this line
@@ -68,16 +101,16 @@ namespace Mono.CSharp
                //
                bool any_token_seen = false;
 
-               static Hashtable tokenValues;
+               static Hashtable token_values;
                static readonly char[] simple_whitespaces = new char[] { ' ', '\t' };
 
                private static Hashtable TokenValueName
                {
                        get {
-                               if (tokenValues == null)
-                                       tokenValues = GetTokenValueNameHash ();
+                               if (token_values == null)
+                                       token_values = GetTokenValueNameHash ();
 
-                               return tokenValues;
+                               return token_values;
                        }
                }
 
@@ -121,69 +154,39 @@ namespace Mono.CSharp
                }
 
                public bool PropertyParsing {
-                       get {
-                               return handle_get_set;
-                       }
-
-                       set {
-                               handle_get_set = value;
-                       }
+                       get { return handle_get_set; }
+                       set { handle_get_set = value; }
                 }
 
-               public bool AssemblyTargetParsing {
-                       get {
-                               return handle_assembly;
-                       }
-
-                       set {
-                               handle_assembly = value;
-                       }
-               }
-
                public bool EventParsing {
-                       get {
-                               return handle_remove_add;
-                       }
-
-                       set {
-                               handle_remove_add = value;
-                       }
+                       get { return handle_remove_add; }
+                       set { handle_remove_add = value; }
                }
 
                public bool ConstraintsParsing {
-                       get {
-                               return handle_constraints;
-                       }
-
-                       set {
-                               handle_constraints = value;
-                       }
+                       get { return handle_where; }
+                       set { handle_where = value; }
                }
 
                public bool TypeOfParsing {
-                       get {
-                               return handle_typeof;
-                       }
-
-                       set {
-                               handle_typeof = value;
-                       }
+                       get { return handle_typeof; }
+                       set { handle_typeof = value; }
                }
-
+               
                public XmlCommentState doc_state {
-                       get { return xmlDocState; }
+                       get { return xml_doc_state; }
                        set {
                                if (value == XmlCommentState.Allowed) {
                                        check_incorrect_doc_comment ();
                                        reset_doc_comment ();
                                }
-                               xmlDocState = value;
+                               xml_doc_state = value;
                        }
                }
 
                public bool IsEscapedIdentifier (Location loc)
                {
-                       foreach (LocatedToken lt in escapedIdentifiers)
+                       foreach (LocatedToken lt in escaped_identifiers)
                                if (lt.Location.Equals (loc))
                                        return true;
                        return false;
@@ -193,21 +196,19 @@ namespace Mono.CSharp
                // Class variables
                // 
                static CharArrayHashtable[] keywords;
-               static Hashtable keywordStrings = new Hashtable ();
+               static Hashtable keyword_strings;
                static NumberStyles styles;
                static NumberFormatInfo csharp_format_info;
                
                //
                // Values for the associated token returned
                //
-               int putback_char;
+               internal int putback_char;      // Used by repl only
                Object val;
 
                //
                // Pre-processor
                //
-               Hashtable defines;
-
                const int TAKING        = 1;
                const int ELSE_SEEN     = 4;
                const int PARENT_TAKING = 8;
@@ -261,8 +262,10 @@ namespace Mono.CSharp
                Stack position_stack = new Stack (2);
                class Position {
                        public int position;
+                       public int line;
                        public int ref_line;
                        public int col;
+                       public bool hidden;
                        public int putback_char;
                        public int previous_col;
                        public Stack ifstack;
@@ -272,8 +275,10 @@ namespace Mono.CSharp
                        public Position (Tokenizer t)
                        {
                                position = t.reader.Position;
+                               line = t.line;
                                ref_line = t.ref_line;
                                col = t.col;
+                               hidden = t.hidden;
                                putback_char = t.putback_char;
                                previous_col = t.previous_col;
                                if (t.ifstack != null && t.ifstack.Count != 0)
@@ -294,7 +299,9 @@ namespace Mono.CSharp
 
                        reader.Position = p.position;
                        ref_line = p.ref_line;
+                       line = p.line;
                        col = p.col;
+                       hidden = p.hidden;
                        putback_char = p.putback_char;
                        previous_col = p.previous_col;
                        ifstack = p.ifstack;
@@ -308,8 +315,9 @@ namespace Mono.CSharp
                        position_stack.Pop ();
                }
                
-               static void AddKeyword (string kw, int token) {
-                       keywordStrings.Add (kw, kw);
+               static void AddKeyword (string kw, int token)
+               {
+                       keyword_strings.Add (kw, kw);
                        if (keywords [kw.Length] == null) {
                                keywords [kw.Length] = new CharArrayHashtable (kw.Length);
                        }
@@ -318,13 +326,13 @@ namespace Mono.CSharp
 
                static void InitTokens ()
                {
+                       keyword_strings = new Hashtable ();
                        keywords = new CharArrayHashtable [64];
 
                        AddKeyword ("__arglist", Token.ARGLIST);
                        AddKeyword ("abstract", Token.ABSTRACT);
                        AddKeyword ("as", Token.AS);
                        AddKeyword ("add", Token.ADD);
-                       AddKeyword ("assembly", Token.ASSEMBLY);
                        AddKeyword ("base", Token.BASE);
                        AddKeyword ("bool", Token.BOOL);
                        AddKeyword ("break", Token.BREAK);
@@ -404,20 +412,37 @@ namespace Mono.CSharp
                        AddKeyword ("volatile", Token.VOLATILE);
                        AddKeyword ("while", Token.WHILE);
                        AddKeyword ("partial", Token.PARTIAL);
-#if GMCS_SOURCE
                        AddKeyword ("where", Token.WHERE);
-#endif
+
+                       // LINQ keywords
+                       AddKeyword ("from", Token.FROM);
+                       AddKeyword ("join", Token.JOIN);
+                       AddKeyword ("on", Token.ON);
+                       AddKeyword ("equals", Token.EQUALS);
+                       AddKeyword ("select", Token.SELECT);
+                       AddKeyword ("group", Token.GROUP);
+                       AddKeyword ("by", Token.BY);
+                       AddKeyword ("let", Token.LET);
+                       AddKeyword ("orderby", Token.ORDERBY);
+                       AddKeyword ("ascending", Token.ASCENDING);
+                       AddKeyword ("descending", Token.DESCENDING);
+                       AddKeyword ("into", Token.INTO);
                }
 
                //
                // Class initializer
                // 
                static Tokenizer ()
+               {
+                       Reset ();
+               }
+
+               public static void Reset ()
                {
                        InitTokens ();
                        csharp_format_info = NumberFormatInfo.InvariantInfo;
                        styles = NumberStyles.Float;
-                       
+
                        string_builder = new System.Text.StringBuilder ();
                }
 
@@ -437,56 +462,87 @@ namespace Mono.CSharp
                        
                        int res = (int) o;
 
-                       if (handle_get_set == false && (res == Token.GET || res == Token.SET))
-                               return -1;
-                       if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
+                       if (!handle_get_set && (res == Token.GET || res == Token.SET))
                                return -1;
-                       if (handle_assembly == false && res == Token.ASSEMBLY)
+                       if (!handle_remove_add && (res == Token.REMOVE || res == Token.ADD))
                                return -1;
-#if GMCS_SOURCE
-                       if (handle_constraints == false && res == Token.WHERE)
+                       if (parsing_declaration == 0 && res == Token.EXTERN)
+                               return Token.EXTERN_ALIAS;
+
+                       //
+                       // A query expression is any expression that starts with `from identifier'
+                       // followed by any token except ; , =
+                       // 
+                       if (query_parsing == 0) {
+                               if (res == Token.FROM && !lambda_arguments_parsing) {
+                                       PushPosition ();
+                                       // HACK: to disable generics micro-parser, because PushPosition does not
+                                       // store identifiers array
+                                       parsing_generic_less_than = 1;
+                                       switch (xtoken ()) {
+                                       case Token.IDENTIFIER:
+                                       case Token.INT:
+                                       case Token.BOOL:
+                                       case Token.BYTE:
+                                       case Token.CHAR:
+                                       case Token.DECIMAL:
+                                       case Token.FLOAT:
+                                       case Token.LONG:
+                                       case Token.OBJECT:
+                                       case Token.STRING:
+                                       case Token.UINT:
+                                       case Token.ULONG:
+                                               int next_token = xtoken ();
+                                               if (next_token == Token.SEMICOLON || next_token == Token.COMMA || next_token == Token.EQUALS)
+                                                       goto default;
+                                               
+                                               ++query_parsing;
+                                               if (RootContext.Version <= LanguageVersion.ISO_2)
+                                                       Report.FeatureIsNotAvailable (Location, "query expressions");
+                                               break;
+                                       case Token.VOID:
+                                               Expression.Error_VoidInvalidInTheContext (Location);
+                                               break;
+                                       default:
+                                               PopPosition ();
+                                               // HACK: A token is not a keyword so we need to restore identifiers buffer
+                                               // which has been overwritten before we grabbed the identifier
+                                               id_builder [0] = 'f'; id_builder [1] = 'r'; id_builder [2] = 'o'; id_builder [3] = 'm';
+                                               return -1;
+                                       }
+                                       PopPosition ();
+                                       return res;
+                               }
+
+                               if (res > Token.QUERY_FIRST_TOKEN && res < Token.QUERY_LAST_TOKEN)
+                                       return -1;
+                       }
+
+                       if (res == Token.WHERE && !handle_where && query_parsing == 0)
                                return -1;
-#endif
-                       return res;
                        
+                       return res;
                }
 
                public Location Location {
                        get { return current_location; }
                }
 
-               void define (string def)
-               {
-                       if (!RootContext.AllDefines.Contains (def)){
-                               RootContext.AllDefines [def] = true;
-                       }
-                       if (defines.Contains (def))
-                               return;
-                       defines [def] = true;
-               }
-               
-               public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
+               public Tokenizer (SeekableStreamReader input, CompilationUnit file)
                {
                        this.ref_name = file;
                        this.file_name = file;
-                       linq = RootContext.Version == LanguageVersion.LINQ;
                        reader = input;
                        
                        putback_char = -1;
 
-                       if (defs != null){
-                               defines = new Hashtable ();
-                               foreach (string def in defs)
-                                       define (def);
-                       }
-
                        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);
+                       Mono.CSharp.Location.Push (file, file);
                }
 
                static bool is_identifier_start_character (char c)
@@ -502,7 +558,161 @@ namespace Mono.CSharp
 
                public static bool IsKeyword (string s)
                {
-                       return keywordStrings [s] != null;
+                       return keyword_strings [s] != null;
+               }
+
+               //
+               // Open parens micro parser. Detects both lambda and cast ambiguity.
+               //
+               
+               int TokenizeOpenParens ()
+               {
+                       int ntoken = -1;
+                       int ptoken;
+                       bool cast_posible = false;
+                       bool cast_not = false;
+                       bool has_star = false;
+                       
+                       while (true) {
+                               ptoken = ntoken;
+                               ntoken = xtoken ();
+
+                               switch (ntoken) {
+                               case Token.CLOSE_PARENS:
+                                       ntoken = xtoken ();
+                                       
+                                       //
+                                       // Token is a lambda
+                                       //
+                                       if (ntoken == Token.ARROW) {
+                                               if (RootContext.Version <= LanguageVersion.ISO_2)
+                                                       Report.FeatureIsNotAvailable (Location, "lambda expressions");
+
+                                               return Token.OPEN_PARENS_LAMBDA;
+                                       }
+
+                                       //
+                                       // Token is possible cast, parser will decide later
+                                       //
+                                       if (cast_posible && !cast_not) {
+                                               switch (ntoken) {
+                                               //
+                                               // Indirection is a special
+                                               // cast: (int*)&a;
+                                               // binary: (C)&a;
+                                               //
+                                               case Token.BITWISE_AND:
+                                               case Token.OP_INC:
+                                               case Token.OP_DEC:
+                                               case Token.PLUS:
+                                               case Token.MINUS:
+                                                       if (!has_star)
+                                                               break;
+                                                       return Token.OPEN_PARENS_CAST;
+
+                                               case Token.OPEN_PARENS:
+                                               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.BASE:
+                                               case Token.CHECKED:
+                                               case Token.DELEGATE:
+                                               case Token.FALSE:
+                                               case Token.FIXED:
+                                               case Token.NEW:
+                                               case Token.NULL:
+                                               case Token.SIZEOF:
+                                               case Token.THIS:
+                                               case Token.THROW:
+                                               case Token.TRUE:
+                                               case Token.TYPEOF:
+                                               case Token.UNCHECKED:
+                                               case Token.UNSAFE:
+                                               case Token.DEFAULT:
+
+                                               //
+                                               // These can be part of a member access
+                                               //
+                                               case Token.INT:
+                                               case Token.UINT:
+                                               case Token.SHORT:
+                                               case Token.USHORT:
+                                               case Token.LONG:
+                                               case Token.ULONG:
+                                               case Token.DOUBLE:
+                                               case Token.FLOAT:
+                                               case Token.CHAR:
+                                               case Token.BYTE:
+                                               case Token.DECIMAL:     
+                                                       return Token.OPEN_PARENS_CAST;
+                                               }
+                                       }
+                                       return Token.OPEN_PARENS;
+                                       
+                               case Token.DOT:
+                                       if (ptoken != Token.IDENTIFIER && ptoken != Token.OP_GENERICS_GT)
+                                               goto default;
+                                       continue;
+
+                               case Token.IDENTIFIER:
+                                       switch (ptoken) {
+                                       case Token.DOT:
+                                       case Token.OP_GENERICS_LT:
+                                       case Token.COMMA:
+                                       case Token.DOUBLE_COLON:
+                                       case -1:
+                                               cast_posible = true;
+                                               continue;
+                                       default:
+                                               cast_not = true;
+                                               continue;
+                                       }
+
+                               case Token.STAR:
+                                       has_star = true;
+                                       continue;
+
+                               case Token.OBJECT:
+                               case Token.STRING:
+                               case Token.BOOL:
+                               case Token.DECIMAL:
+                               case Token.FLOAT:
+                               case Token.DOUBLE:
+                               case Token.SBYTE:
+                               case Token.BYTE:
+                               case Token.SHORT:
+                               case Token.USHORT:
+                               case Token.INT:
+                               case Token.UINT:
+                               case Token.LONG:
+                               case Token.ULONG:
+                               case Token.CHAR:
+                               case Token.VOID:
+                               case Token.OP_GENERICS_GT:
+                               case Token.OP_GENERICS_LT:
+                               case Token.INTERR_NULLABLE:
+                               case Token.OPEN_BRACKET:
+                               case Token.CLOSE_BRACKET:
+                               case Token.COMMA:
+                               case Token.DOUBLE_COLON:
+                                       cast_posible = true;
+                                       continue;
+
+                               case Token.REF:
+                               case Token.OUT:
+                                       cast_not = true;
+                                       continue;
+                                       
+                               default:
+                                       return Token.OPEN_PARENS;
+                               }
+                       }
                }
 
                public static bool IsValidIdentifier (string s)
@@ -520,22 +730,6 @@ namespace Mono.CSharp
                        return true;
                }
 
-               bool parse_generic_dimension (out int dimension)
-               {
-                       dimension = 1;
-
-               again:
-                       int the_token = token ();
-                       if (the_token == Token.OP_GENERICS_GT)
-                               return true;
-                       else if (the_token == Token.COMMA) {
-                               dimension++;
-                               goto again;
-                       }
-
-                       return false;
-               }
-
                bool parse_less_than ()
                {
                start:
@@ -566,6 +760,9 @@ namespace Mono.CSharp
                        case Token.VOID:
                                break;
 
+                       case Token.OP_GENERICS_GT:
+                               return true;
+
                        default:
                                return false;
                        }
@@ -576,7 +773,7 @@ namespace Mono.CSharp
                                return true;
                        else if (the_token == Token.COMMA || the_token == Token.DOT || the_token == Token.DOUBLE_COLON)
                                goto start;
-                       else if (the_token == Token.INTERR || the_token == Token.STAR)
+                       else if (the_token == Token.INTERR_NULLABLE || the_token == Token.STAR)
                                goto again;
                        else if (the_token == Token.OP_GENERICS_LT) {
                                if (!parse_less_than ())
@@ -595,35 +792,21 @@ namespace Mono.CSharp
                        return false;
                }
 
-#if GMCS_SOURCE
-               public void PutbackNullable ()
-               {
-                       if (nullable_pos < 0)
-                               throw new Exception ();
-
-                       current_token = -1;
-                       val = null;
-                       reader.Position = nullable_pos;
-
-                       putback_char = '?';
-               }
-
-               public void PutbackCloseParens ()
+               bool parse_generic_dimension (out int dimension)
                {
-                       putback_char = ')';
-               }
-
+                       dimension = 1;
 
-               int nullable_pos = -1;
+               again:
+                       int the_token = token ();
+                       if (the_token == Token.OP_GENERICS_GT)
+                               return true;
+                       else if (the_token == Token.COMMA) {
+                               dimension++;
+                               goto again;
+                       }
 
-               public void CheckNullable (bool is_nullable)
-               {
-                       if (is_nullable)
-                               nullable_pos = reader.Position;
-                       else
-                               nullable_pos = -1;
+                       return false;
                }
-#endif
                
                public int peek_token ()
                {
@@ -635,130 +818,7 @@ namespace Mono.CSharp
                        
                        return the_token;
                }
-               
-               bool parse_namespace_or_typename (int next)
-               {
-                       if (next == -1)
-                               next = peek_token ();
-                       while (next == Token.IDENTIFIER){
-                               token ();
-                         again:
-                               next = peek_token ();
-                               if (next == Token.DOT || next == Token.DOUBLE_COLON){
-                                       token ();
-                                       next = peek_token ();
-                                       continue;
-                               }
-                               if (next == Token.OP_GENERICS_LT){
-                                       token ();
-                                       if (!parse_less_than ())
-                                               throw new InternalErrorException ("cannot happen");
-                                       goto again;
-                               }
-                               return true;
-                       }
-
-                       return false;
-               }
-
-               bool is_simple_type (int token)
-               {
-                       return  (token == Token.BOOL ||
-                                token == Token.DECIMAL ||
-                                token == Token.SBYTE ||
-                                token == Token.BYTE ||
-                                token == Token.SHORT ||
-                                token == Token.USHORT ||
-                                token == Token.INT ||
-                                token == Token.UINT ||
-                                token == Token.LONG ||
-                                token == Token.ULONG ||
-                                token == Token.CHAR ||
-                                token == Token.FLOAT ||
-                                token == Token.DOUBLE);
-               }
-
-               bool is_builtin_reference_type (int token)
-               {
-                       return (token == Token.OBJECT || token == Token.STRING);
-               }
-
-               bool parse_opt_rank (int next)
-               {
-                       while (true){
-                               if (next != Token.OPEN_BRACKET)
-                                       return true;
-
-                               token ();
-                               while (true){
-                                       next = token ();
-                                       if (next == Token.CLOSE_BRACKET){
-                                               next = peek_token ();
-                                               break;
-                                       }
-                                       if (next == Token.COMMA)
-                                               continue;
                                        
-                                       return false;
-                               }
-                       }
-               }
-                       
-               bool parse_type ()
-               {
-                       int next = peek_token ();
-                       
-                       if (is_simple_type (next)){
-                               token ();
-                               next = peek_token ();
-                               if (next == Token.INTERR)
-                                       token ();
-                               return parse_opt_rank (peek_token ());
-                       }
-                       if (parse_namespace_or_typename (next)){
-                               next = peek_token ();
-                               if (next == Token.INTERR)
-                                       token ();
-                               return parse_opt_rank (peek_token ());
-                       } else if (is_builtin_reference_type (next)){
-                               token ();
-                               return parse_opt_rank (peek_token ());
-                       }
-                       
-                       return false;
-               }
-               
-               //
-               // Invoked after '(' has been seen and tries to parse
-               // a type expression followed by an identifier, if this
-               // is the case, instead of returning an OPEN_PARENS token
-               // we return a special token that triggers lambda parsing.
-               //
-               // This is needed because we can not introduce the
-               // explicitly_typed_lambda_parameter_list after a '(' in the
-               // grammar without introducing reduce/reduce conflicts.
-               //
-               // We need to parse a type and if it is followed by an
-               // identifier, we know it has to be parsed as a lambda
-               // expression.  
-               //
-               // the type expression can be prefixed with `ref' or `out'
-               //
-               public bool parse_type_and_parameter ()
-               {
-                       int next = peek_token ();
-
-                       if (next == Token.REF || next == Token.OUT)
-                               token ();
-                                                
-                       if (parse_type ()){
-                               next = peek_token ();
-                               if (next == Token.IDENTIFIER)
-                                       return true;
-                       }
-                       return false;
-               }
-
                int parsing_generic_less_than = 0;
                
                int is_punct (char c, ref bool doread)
@@ -783,39 +843,22 @@ namespace Mono.CSharp
                        case ']':
                                return Token.CLOSE_BRACKET;
                        case '(':
-                               if (linq){
+                               val = Location;
+                               //
+                               // An expression versions of parens can appear in block context only
+                               //
+                               if (parsing_block != 0 && !lambda_arguments_parsing) {
+                                       lambda_arguments_parsing = true;
                                        PushPosition ();
-                                       bool is_type_and_parameter = parse_type_and_parameter ();
+                                       t = TokenizeOpenParens ();
                                        PopPosition ();
-                                       
-                                       if (is_type_and_parameter)
-                                               return Token.OPEN_PARENS_LAMBDA;
-                                       else
-                                               return Token.OPEN_PARENS;
-                               } else
-                                       return Token.OPEN_PARENS;
-                       case ')': {
-                               if (deambiguate_close_parens == 0)
-                                       return Token.CLOSE_PARENS;
-
-                               --deambiguate_close_parens;
-
-                               PushPosition ();
-
-                               int new_token = xtoken ();
-
-                               PopPosition ();
-
-                               if (new_token == Token.OPEN_PARENS)
-                                       return Token.CLOSE_PARENS_OPEN_PARENS;
-                               else if (new_token == Token.MINUS)
-                                       return Token.CLOSE_PARENS_MINUS;
-                               else if (IsCastToken (new_token))
-                                       return Token.CLOSE_PARENS_CAST;
-                               else
-                                       return Token.CLOSE_PARENS_NO_CAST;
-                       }
+                                       lambda_arguments_parsing = false;
+                                       return t;
+                               }
 
+                               return Token.OPEN_PARENS;
+                       case ')':
+                               return Token.CLOSE_PARENS;
                        case ',':
                                return Token.COMMA;
                        case ';':
@@ -825,9 +868,9 @@ namespace Mono.CSharp
                                val = Location;
                                return Token.TILDE;
                        case '?':
-                               return Token.INTERR;
+                               return TokenizePossibleNullableType ();
                        }
-#if GMCS_SOURCE
+
                        if (c == '<') {
                                if (parsing_generic_less_than++ > 0)
                                        return Token.OP_GENERICS_LT;
@@ -846,17 +889,24 @@ namespace Mono.CSharp
                                // Save current position and parse next token.
                                PushPosition ();
                                bool is_generic_lt = parse_less_than ();
-                               PopPosition ();
-
                                if (is_generic_lt) {
-                                       return Token.OP_GENERICS_LT;
-                               } else
-                                       parsing_generic_less_than = 0;
+                                       int rt;
+                                       if (parsing_generic_declaration && token () != Token.DOT) {
+                                               rt = Token.OP_GENERICS_LT_DECL;
+                                       } else {
+                                               rt = Token.OP_GENERICS_LT;
+                                       }
+                                       PopPosition ();
+                                       return rt;
+                               }
+                               
+                               PopPosition ();
+                               parsing_generic_less_than = 0;
 
-                               d = peekChar ();
+                               d = peek_char ();
                                if (d == '<'){
-                                       getChar ();
-                                       d = peekChar ();
+                                       get_char ();
+                                       d = peek_char ();
 
                                        if (d == '='){
                                                doread = true;
@@ -869,29 +919,33 @@ namespace Mono.CSharp
                                }
                                return Token.OP_LT;
                        } else if (c == '>') {
-                               if (parsing_generic_less_than > 0) {
+                               d = peek_char ();
+
+                               if (d == '='){
+                                       doread = true;
+                                       return Token.OP_GE;
+                               }
+
+                               if (parsing_generic_less_than > 1 || (parsing_generic_less_than == 1 && d != '>')) {
                                        parsing_generic_less_than--;
                                        return Token.OP_GENERICS_GT;
                                }
 
-                               d = peekChar ();
-                               if (d == '>'){
-                                       getChar ();
-                                       d = peekChar ();
+                               if (d == '>') {
+                                       get_char ();
+                                       d = peek_char ();
 
-                                       if (d == '='){
+                                       if (d == '=') {
                                                doread = true;
                                                return Token.OP_SHIFT_RIGHT_ASSIGN;
                                        }
                                        return Token.OP_SHIFT_RIGHT;
-                               } else if (d == '='){
-                                       doread = true;
-                                       return Token.OP_GE;
                                }
+
                                return Token.OP_GT;
                        }
-#endif
-                       d = peekChar ();
+                       
+                       d = peek_char ();
                        if (c == '+'){
                                
                                if (d == '+') {
@@ -943,6 +997,7 @@ namespace Mono.CSharp
                                        val = Location;
                                        return Token.ARROW;
                                }
+
                                return Token.ASSIGN;
                        }
 
@@ -997,46 +1052,11 @@ namespace Mono.CSharp
                        if (c == '^'){
                                if (d == '='){
                                        doread = true;
-                                       return Token.OP_XOR_ASSIGN;
-                               }
-                               return Token.CARRET;
-                       }
-
-#if !GMCS_SOURCE
-                       if (c == '<'){
-                               if (d == '<'){
-                                       getChar ();
-                                       d = peekChar ();
-
-                                       if (d == '='){
-                                               doread = true;
-                                               return Token.OP_SHIFT_LEFT_ASSIGN;
-                                       }
-                                       return Token.OP_SHIFT_LEFT;
-                               } else if (d == '='){
-                                       doread = true;
-                                       return Token.OP_LE;
-                               }
-                               return Token.OP_LT;
-                       }
-
-                       if (c == '>'){
-                               if (d == '>'){
-                                       getChar ();
-                                       d = peekChar ();
-
-                                       if (d == '='){
-                                               doread = true;
-                                               return Token.OP_SHIFT_RIGHT_ASSIGN;
-                                       }
-                                       return Token.OP_SHIFT_RIGHT;
-                               } else if (d == '='){
-                                       doread = true;
-                                       return Token.OP_GE;
+                                       return Token.OP_XOR_ASSIGN;
                                }
-                               return Token.OP_GT;
+                               return Token.CARRET;
                        }
-#endif
+
                        if (c == ':'){
                                if (d == ':'){
                                        doread = true;
@@ -1049,17 +1069,115 @@ namespace Mono.CSharp
                        return Token.ERROR;
                }
 
-               int deambiguate_close_parens = 0;
-
-               public void Deambiguate_CloseParens (object expression)
+               //
+               // Tonizes `?' using custom disambiguous rules to return one
+               // of following tokens: INTERR_NULLABLE, OP_COALESCING, INTERR
+               //
+               // Tricky expression look like:
+               //
+               // Foo ? a = x ? b : c;
+               //
+               int TokenizePossibleNullableType ()
                {
-                       putback (')');
+                       if (parsing_block == 0)
+                               return Token.INTERR_NULLABLE;
 
-                       // When any binary operation is used we are sure it is not a cast
-                       if (expression is Binary)
-                               return;
+                       int d = peek_char ();
+                       if (d == '?') {
+                               get_char ();
+                               return Token.OP_COALESCING;
+                       }
+
+                       switch (current_token) {
+                       case Token.CLOSE_PARENS:
+                       case Token.TRUE:
+                       case Token.FALSE:
+                       case Token.NULL:
+                       case Token.LITERAL_INTEGER:
+                       case Token.LITERAL_STRING:
+                               return Token.INTERR;
+                       }
+
+                       if (d != ' ') {
+                               if (d == ',' || d == ';' || d == '>')
+                                       return Token.INTERR_NULLABLE;
+                               if (d == '*' || (d >= '0' && d <= '9'))
+                                       return Token.INTERR;
+                       }
+
+                       PushPosition ();
+                       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.TRUE:
+                       case Token.FALSE:
+                       case Token.NULL:
+                       case Token.THIS:
+                               next_token = Token.INTERR;
+                               break;
+                               
+                       case Token.SEMICOLON:
+                       case Token.COMMA:
+                       case Token.CLOSE_PARENS:
+                       case Token.OPEN_BRACKET:
+                       case Token.OP_GENERICS_GT:
+                               next_token = Token.INTERR_NULLABLE;
+                               break;
+                               
+                       default:
+                               next_token = -1;
+                               break;
+                       }
 
-                       deambiguate_close_parens++;
+                       if (next_token == -1) {
+                               switch (xtoken ()) {
+                               case Token.COMMA:
+                               case Token.SEMICOLON:
+                               case Token.OPEN_BRACE:
+                               case Token.CLOSE_PARENS:
+                               case Token.IN:
+                                       next_token = Token.INTERR_NULLABLE;
+                                       break;
+                                       
+                               case Token.COLON:
+                                       next_token = Token.INTERR;
+                                       break;                                                  
+                                       
+                               default:
+                                       int ntoken;
+                                       int interrs = 1;
+                                       int colons = 0;
+                                       //
+                                       // All shorcuts failed, do it hard way
+                                       //
+                                       while ((ntoken = xtoken ()) != Token.EOF) {
+                                               if (ntoken == Token.SEMICOLON)
+                                                       break;
+                                               
+                                               if (ntoken == Token.COLON) {
+                                                       if (++colons == interrs)
+                                                               break;
+                                                       continue;
+                                               }
+                                               
+                                               if (ntoken == Token.INTERR) {
+                                                       ++interrs;
+                                                       continue;
+                                               }
+                                       }
+                                       
+                                       next_token = colons != interrs ? Token.INTERR_NULLABLE : Token.INTERR;
+                                       break;
+                               }
+                       }
+                       
+                       PopPosition ();
+                       return next_token;
                }
 
                bool decimal_digits (int c)
@@ -1074,15 +1192,15 @@ namespace Mono.CSharp
                        }
                        
                        //
-                       // We use peekChar2, because decimal_digits needs to do a 
+                       // We use peek_char2, because decimal_digits needs to do a 
                        // 2-character look-ahead (5.ToString for example).
                        //
-                       while ((d = peekChar2 ()) != -1){
+                       while ((d = peek_char2 ()) != -1){
                                if (d >= '0' && d <= '9'){
                                        if (number_pos == max_number_size)
                                                Error_NumericConstantTooLong ();
                                        number_builder [number_pos++] = (char) d;
-                                       getChar ();
+                                       get_char ();
                                        seen_digits = true;
                                } else
                                        break;
@@ -1095,7 +1213,7 @@ namespace Mono.CSharp
                {
                        return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
                }
-                               
+
                static int real_type_suffix (int c)
                {
                        int t;
@@ -1129,11 +1247,11 @@ namespace Mono.CSharp
                                                if (is_unsigned)
                                                        scanning = false;
                                                is_unsigned = true;
-                                               getChar ();
+                                               get_char ();
                                                break;
 
                                        case 'l':
-                                               if (!is_unsigned && (RootContext.WarningLevel >= 4)){
+                                               if (!is_unsigned){
                                                        //
                                                        // if we have not seen anything in between
                                                        // report this error
@@ -1148,21 +1266,21 @@ namespace Mono.CSharp
                                                if (is_long)
                                                        scanning = false;
                                                is_long = true;
-                                               getChar ();
+                                               get_char ();
                                                break;
 
                                        case 'L': 
                                                if (is_long)
                                                        scanning = false;
                                                is_long = true;
-                                               getChar ();
+                                               get_char ();
                                                break;
                                                
                                        default:
                                                scanning = false;
                                                break;
                                        }
-                                       c = peekChar ();
+                                       c = peek_char ();
                                } while (scanning);
                        }
 
@@ -1279,11 +1397,11 @@ namespace Mono.CSharp
                        int d;
                        ulong ul;
                        
-                       getChar ();
-                       while ((d = peekChar ()) != -1){
+                       get_char ();
+                       while ((d = peek_char ()) != -1){
                                if (is_hex (d)){
                                        number_builder [number_pos++] = (char) d;
-                                       getChar ();
+                                       get_char ();
                                } else
                                        break;
                        }
@@ -1306,7 +1424,7 @@ namespace Mono.CSharp
                                return Token.LITERAL_INTEGER;
                        }
                        
-                       return integer_type_suffix (ul, peekChar ());
+                       return integer_type_suffix (ul, peek_char ());
                }
 
                //
@@ -1321,13 +1439,13 @@ namespace Mono.CSharp
 
                        if (c >= '0' && c <= '9'){
                                if (c == '0'){
-                                       int peek = peekChar ();
+                                       int peek = peek_char ();
 
                                        if (peek == 'x' || peek == 'X')
                                                return handle_hex ();
                                }
                                decimal_digits (c);
-                               c = getChar ();
+                               c = get_char ();
                        }
 
                        //
@@ -1337,7 +1455,7 @@ namespace Mono.CSharp
                        if (c == '.'){
                                if (decimal_digits ('.')){
                                        is_real = true;
-                                       c = getChar ();
+                                       c = get_char ();
                                } else {
                                        putback ('.');
                                        number_pos--;
@@ -1350,7 +1468,7 @@ namespace Mono.CSharp
                                if (number_pos == max_number_size)
                                        Error_NumericConstantTooLong ();
                                number_builder [number_pos++] = 'e';
-                               c = getChar ();
+                               c = get_char ();
                                
                                if (c == '+'){
                                        if (number_pos == max_number_size)
@@ -1369,7 +1487,7 @@ namespace Mono.CSharp
                                }
                                        
                                decimal_digits (c);
-                               c = getChar ();
+                               c = get_char ();
                        }
 
                        type = real_type_suffix (c);
@@ -1393,18 +1511,19 @@ namespace Mono.CSharp
                //
                // Accepts exactly count (4 or 8) hex, no more no less
                //
-               int getHex (int count, out bool error)
+               int getHex (int count, out int surrogate, out bool error)
                {
                        int i;
                        int total = 0;
                        int c;
                        int top = count != -1 ? count : 4;
                        
-                       getChar ();
+                       get_char ();
                        error = false;
+                       surrogate = 0;
                        for (i = 0; i < top; i++){
-                               c = getChar ();
-                               
+                               c = get_char ();
+
                                if (c >= '0' && c <= '9')
                                        c = (int) c - (int) '0';
                                else if (c >= 'A' && c <= 'F')
@@ -1418,25 +1537,40 @@ namespace Mono.CSharp
                                
                                total = (total * 16) + c;
                                if (count == -1){
-                                       int p = peekChar ();
+                                       int p = peek_char ();
                                        if (p == -1)
                                                break;
                                        if (!is_hex ((char)p))
                                                break;
                                }
                        }
+
+                       if (top == 8) {
+                               if (total > 0x0010FFFF) {
+                                       error = true;
+                                       return 0;
+                               }
+
+                               if (total >= 0x00010000) {
+                                       surrogate = ((total - 0x00010000) % 0x0400 + 0xDC00);                                   
+                                       total = ((total - 0x00010000) / 0x0400 + 0xD800);
+                               }
+                       }
+
                        return total;
                }
 
-               int escape (int c)
+               int escape (int c, out int surrogate)
                {
                        bool error;
                        int d;
                        int v;
 
-                       d = peekChar ();
-                       if (c != '\\')
+                       d = peek_char ();
+                       if (c != '\\') {
+                               surrogate = 0;
                                return c;
+                       }
                        
                        switch (d){
                        case 'a':
@@ -1462,29 +1596,40 @@ namespace Mono.CSharp
                        case '\'':
                                v = '\''; break;
                        case 'x':
-                               v = getHex (-1, out error);
+                               v = getHex (-1, out surrogate, out error);
                                if (error)
                                        goto default;
                                return v;
                        case 'u':
-                               v = getHex (4, out error);
-                               if (error)
-                                       goto default;
-                               return v;
                        case 'U':
-                               v = getHex (8, out error);
-                               if (error)
-                                       goto default;
-                               return v;
+                               return EscapeUnicode (d, out surrogate);
                        default:
+                               surrogate = 0;
                                Report.Error (1009, Location, "Unrecognized escape sequence `\\{0}'", ((char)d).ToString ());
                                return d;
                        }
-                       getChar ();
+
+                       get_char ();
+                       surrogate = 0;
                        return v;
                }
 
-               int getChar ()
+               int EscapeUnicode (int ch, out int surrogate)
+               {
+                       bool error;
+                       if (ch == 'U') {
+                               ch = getHex (8, out surrogate, out error);
+                       } else {
+                               ch = getHex (4, out surrogate, out error);
+                       }
+
+                       if (error)
+                               Report.Error (1009, Location, "Unrecognized escape sequence");
+
+                       return ch;
+               }
+
+               int get_char ()
                {
                        int x;
                        if (putback_char != -1) {
@@ -1493,17 +1638,22 @@ namespace Mono.CSharp
                        } else
                                x = reader.Read ();
                        if (x == '\n') {
-                               line++;
-                               ref_line++;
-                               previous_col = col;
-                               col = 0;
-                       }
-                       else
+                               advance_line ();
+                       } else {
                                col++;
+                       }
                        return x;
                }
 
-               int peekChar ()
+               void advance_line ()
+               {
+                       line++;
+                       ref_line++;
+                       previous_col = col;
+                       col = 0;
+               }
+
+               int peek_char ()
                {
                        if (putback_char != -1)
                                return putback_char;
@@ -1511,7 +1661,7 @@ namespace Mono.CSharp
                        return putback_char;
                }
 
-               int peekChar2 ()
+               int peek_char2 ()
                {
                        if (putback_char != -1)
                                return putback_char;
@@ -1540,7 +1690,7 @@ namespace Mono.CSharp
 
                public bool advance ()
                {
-                       return peekChar () != -1;
+                       return peek_char () != -1;
                }
 
                public Object Value {
@@ -1554,60 +1704,10 @@ namespace Mono.CSharp
                        return val;
                }
 
-               static bool IsCastToken (int token)
-               {
-                       switch (token) {
-                       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.BASE:
-                       case Token.CHECKED:
-                       case Token.DELEGATE:
-                       case Token.FALSE:
-                       case Token.FIXED:
-                       case Token.NEW:
-                       case Token.NULL:
-                       case Token.SIZEOF:
-                       case Token.THIS:
-                       case Token.THROW:
-                       case Token.TRUE:
-                       case Token.TYPEOF:
-                       case Token.UNCHECKED:
-                       case Token.UNSAFE:
-#if GMCS_SOURCE
-                       case Token.DEFAULT:
-#endif
-
-                               //
-                               // These can be part of a member access
-                               //
-                       case Token.INT:
-                       case Token.UINT:
-                       case Token.SHORT:
-                       case Token.USHORT:
-                       case Token.LONG:
-                       case Token.ULONG:
-                       case Token.DOUBLE:
-                       case Token.FLOAT:
-                       case Token.CHAR:
-                               return true;
-
-                       default:
-                               return false;
-                       }
-               }
-
                public int token ()
                {
                        current_token = xtoken ();
 
-#if GMCS_SOURCE
                        if (current_token != Token.DEFAULT)
                                return current_token;
 
@@ -1615,67 +1715,72 @@ namespace Mono.CSharp
                        int c = xtoken();
                        if (c == -1)
                                current_token = Token.ERROR;
-                       else if (c == Token.OPEN_PARENS)
-                               current_token = Token.DEFAULT_OPEN_PARENS;
                        else if (c == Token.COLON)
                                current_token = Token.DEFAULT_COLON;
                        else
                                PopPosition();
-#endif
+                       
                        return current_token;
                }
 
                static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
-               
+
                void get_cmd_arg (out string cmd, out string arg)
                {
                        int c;
                        
                        tokens_seen = false;
                        arg = "";
-                       static_cmd_arg.Length = 0;
 
                        // skip over white space
-                       while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
-                               ;
-                               
-                       while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
-                               if (is_identifier_part_character ((char) c)){
-                                       static_cmd_arg.Append ((char) c);
-                                       c = getChar ();
-                               } else {
-                                       putback (c);
-                                       break;
+                       do {
+                               c = get_char ();
+                       } while (c == '\r' || c == ' ' || c == '\t');
+
+                       static_cmd_arg.Length = 0;
+                       while (c != -1 && is_identifier_part_character ((char)c)) {
+                               static_cmd_arg.Append ((char)c);
+                               c = get_char ();
+                               if (c == '\\') {
+                                       int peek = peek_char ();
+                                       if (peek == 'U' || peek == 'u') {
+                                               int surrogate;
+                                               c = EscapeUnicode (c, out surrogate);
+                                               if (surrogate != 0) {
+                                                       if (is_identifier_part_character ((char) c))
+                                                               static_cmd_arg.Append ((char) c);
+                                                       c = surrogate;
+                                               }
+                                       }
                                }
                        }
 
                        cmd = static_cmd_arg.ToString ();
 
-                       if (c == '\n' || c == '\r'){
-                               return;
-                       }
-
                        // skip over white space
-                       while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
-                               ;
+                       while (c == '\r' || c == ' ' || c == '\t')
+                               c = get_char ();
 
-                       if (c == '\n'){
-                               return;
-                       } else if (c == '\r'){
-                               return;
-                       } else if (c == -1){
-                               arg = "";
-                               return;
-                       }
-                       
                        static_cmd_arg.Length = 0;
-                       static_cmd_arg.Append ((char) c);
-                       
-                       while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
+                       while (c != -1 && c != '\n' && c != '\r') {
+                               if (c == '\\') {
+                                       int peek = peek_char ();
+                                       if (peek == 'U' || peek == 'u') {
+                                               int surrogate;
+                                               c = EscapeUnicode (c, out surrogate);
+                                               if (surrogate != 0) {
+                                                       if (is_identifier_part_character ((char) c))
+                                                               static_cmd_arg.Append ((char) c);
+                                                       c = surrogate;
+                                               }
+                                       }
+                               }
                                static_cmd_arg.Append ((char) c);
+                               c = get_char ();
                        }
 
-                       arg = static_cmd_arg.ToString ();
+                       if (static_cmd_arg.Length != 0)
+                               arg = static_cmd_arg.ToString ();
                }
 
                //
@@ -1689,12 +1794,11 @@ namespace Mono.CSharp
                        if (arg == "default"){
                                ref_line = line;
                                ref_name = file_name;
-                               Location.Push (ref_name);
+                               hidden = false;
+                               Location.Push (file_name, ref_name);
                                return true;
                        } else if (arg == "hidden"){
-                               //
-                               // We ignore #line hidden
-                               //
+                               hidden = true;
                                return true;
                        }
                        
@@ -1708,12 +1812,13 @@ namespace Mono.CSharp
                                        char [] quotes = { '\"' };
                                        
                                        string name = arg.Substring (pos). Trim (quotes);
-                                       ref_name = Location.LookupFile (name);
-                                       file_name.HasLineDirective = true;
-                                       ref_name.HasLineDirective = true;
-                                       Location.Push (ref_name);
+                                       ref_name = Location.LookupFile (file_name, name);
+                                       file_name.AddFile (ref_name);
+                                       hidden = false;
+                                       Location.Push (file_name, ref_name);
                                } else {
                                        ref_line = System.Int32.Parse (arg);
+                                       hidden = false;
                                }
                        } catch {
                                return false;
@@ -1725,24 +1830,24 @@ namespace Mono.CSharp
                //
                // Handles #define and #undef
                //
-               void PreProcessDefinition (bool is_define, string arg, bool caller_is_taking)
+               void PreProcessDefinition (bool is_define, string ident, bool caller_is_taking)
                {
-                       if (arg.Length == 0 || arg == "true" || arg == "false"){
-                               Report.Error (1001, Location, "Missing identifer to pre-processor directive");
+                       if (ident.Length == 0 || ident == "true" || ident == "false"){
+                               Report.Error (1001, Location, "Missing identifier to pre-processor directive");
                                return;
                        }
 
-                       if (arg.IndexOfAny (simple_whitespaces) != -1){
+                       if (ident.IndexOfAny (simple_whitespaces) != -1){
                                Error_EndLineExpected ();
                                return;
                        }
 
-                       if (!is_identifier_start_character (arg [0]))
-                               Report.Error (1001, Location, "Identifier expected: " + arg);
+                       if (!is_identifier_start_character (ident [0]))
+                               Report.Error (1001, Location, "Identifier expected: {0}", ident);
                        
-                       foreach (char c in arg.Substring (1)){
+                       foreach (char c in ident.Substring (1)){
                                if (!is_identifier_part_character (c)){
-                                       Report.Error (1001, Location, "Identifier expected: " + arg);
+                                       Report.Error (1001, Location, "Identifier expected: {0}",  ident);
                                        return;
                                }
                        }
@@ -1750,16 +1855,140 @@ namespace Mono.CSharp
                        if (!caller_is_taking)
                                return;
 
-                       if (is_define){
-                               if (defines == null)
-                                       defines = new Hashtable ();
-                               define (arg);
-                       } else {
-                               if (defines == null)
+                       if (is_define) {
+                               //
+                               // #define ident
+                               //
+                               if (RootContext.IsConditionalDefined (ident))
                                        return;
-                               if (defines.Contains (arg))
-                                       defines.Remove (arg);
+
+                               file_name.AddDefine (ident);
+                       } else {
+                               //
+                               // #undef ident
+                               //
+                               file_name.AddUndefine (ident);
+                       }
+               }
+
+               static byte read_hex (string arg, int pos, out bool error)
+               {
+                       error = false;
+
+                       int total;
+                       char c = arg [pos];
+
+                       if ((c >= '0') && (c <= '9'))
+                               total = (int) c - (int) '0';
+                       else if ((c >= 'A') && (c <= 'F'))
+                               total = (int) c - (int) 'A' + 10;
+                       else if ((c >= 'a') && (c <= 'f'))
+                               total = (int) c - (int) 'a' + 10;
+                       else {
+                               error = true;
+                               return 0;
+                       }
+
+                       total *= 16;
+                       c = arg [pos+1];
+
+                       if ((c >= '0') && (c <= '9'))
+                               total += (int) c - (int) '0';
+                       else if ((c >= 'A') && (c <= 'F'))
+                               total += (int) c - (int) 'A' + 10;
+                       else if ((c >= 'a') && (c <= 'f'))
+                               total += (int) c - (int) 'a' + 10;
+                       else {
+                               error = true;
+                               return 0;
+                       }
+
+                       return (byte) total;
+               }
+
+               /// <summary>
+               /// Handles #pragma checksum
+               /// </summary>
+               bool PreProcessPragmaChecksum (string arg)
+               {
+                       if ((arg [0] != ' ') && (arg [0] != '\t'))
+                               return false;
+
+                       arg = arg.Trim (simple_whitespaces);
+                       if ((arg.Length < 2) || (arg [0] != '"'))
+                               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++];
+                               }
+
+                               file_sb.Append (ch);
+                       }
+
+                       if ((pos+2 >= arg.Length) || ((arg [pos] != ' ') && (arg [pos] != '\t')))
+                               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] != '"'))
+                               return false;
+
+                       bool error;
+                       byte[] guid_bytes = new byte [16];
+
+                       for (int i = 0; i < 4; i++) {
+                               guid_bytes [i] = read_hex (arg, 2+2*i, 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 (error)
+                                       return false;
+                               guid_bytes [i+6] = read_hex (arg, 16+2*i, out error);
+                               if (error)
+                                       return false;
+                               guid_bytes [i+8] = read_hex (arg, 21+2*i, out error);
+                               if (error)
+                                       return false;
+                       }
+
+                       for (int i = 0; i < 6; i++) {
+                               guid_bytes [i+10] = read_hex (arg, 26+2*i, out error);
+                               if (error)
+                                       return false;
                        }
+
+                       arg = arg.Substring (40).Trim (simple_whitespaces);
+                       if ((arg.Length < 34) || (arg [0] != '"') || (arg [33] != '"'))
+                               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);
+                               if (error)
+                                       return false;
+                       }
+
+                       arg = arg.Substring (34).Trim (simple_whitespaces);
+                       if (arg.Length > 0)
+                               return false;
+
+                       SourceFile file = Location.LookupFile (file_name, file_sb.ToString ());
+                       file.SetChecksum (guid_bytes, checksum_bytes);
+                       ref_name.AutoGenerated = true;
+                       return true;
                }
 
                /// <summary>
@@ -1770,14 +1999,15 @@ namespace Mono.CSharp
                        const string warning = "warning";
                        const string w_disable = "warning disable";
                        const string w_restore = "warning restore";
+                       const string checksum = "checksum";
 
                        if (arg == w_disable) {
-                               Report.RegisterWarningRegion (Location).WarningDisable (line);
+                               Report.RegisterWarningRegion (Location).WarningDisable (Location.Row);
                                return;
                        }
 
                        if (arg == w_restore) {
-                               Report.RegisterWarningRegion (Location).WarningEnable (line);
+                               Report.RegisterWarningRegion (Location).WarningEnable (Location.Row);
                                return;
                        }
 
@@ -1806,6 +2036,12 @@ namespace Mono.CSharp
                                return;
                        }
 
+                       if (arg.StartsWith (checksum)) {
+                               if (!PreProcessPragmaChecksum (arg.Substring (checksum.Length)))
+                                       Warning_InvalidPragmaChecksum ();
+                               return;
+                       }
+
                        Report.Warning (1633, 1, Location, "Unrecognized #pragma directive");
                }
 
@@ -1831,13 +2067,8 @@ namespace Mono.CSharp
                                return true;
                        if (s == "false")
                                return false;
-                       
-                       if (defines == null)
-                               return false;
-                       if (defines.Contains (s))
-                               return true;
 
-                       return false;
+                       return file_name.IsConditionalDefined (s);
                }
 
                bool pp_primary (ref string s)
@@ -2004,7 +2235,7 @@ namespace Mono.CSharp
                {
                        Report.Error (
                                1028, Location,
-                               "Unexpected processor directive (" + extra + ")");
+                               "Unexpected processor directive ({0})", extra);
                }
 
                void Error_TokensSeen ()
@@ -2024,6 +2255,13 @@ namespace Mono.CSharp
                        Report.Error (1025, Location, "Single-line comment or end-of-line expected");
                }
                
+               void Warning_InvalidPragmaChecksum ()
+               {
+                       Report.Warning (1695, 1, Location,
+                                       "Invalid #pragma checksum syntax; should be " +
+                                       "#pragma checksum \"filename\" " +
+                                       "\"{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}\" \"XXXX...\"");
+               }
                //
                // if true, then the code continues processing the code
                // if false, the code stays in a loop until another directive is
@@ -2199,7 +2437,7 @@ namespace Mono.CSharp
                                        
                        switch (cmd){
                        case "error":
-                               Report.Error (1029, Location, "#error: '" + arg + "'");
+                               Report.Error (1029, Location, "#error: '{0}'", arg);
                                return true;
 
                        case "warning":
@@ -2208,7 +2446,7 @@ namespace Mono.CSharp
 
                        case "pragma":
                                if (RootContext.Version == LanguageVersion.ISO_1) {
-                                       Report.FeatureIsNotISO1 (Location, "#pragma");
+                                       Report.FeatureIsNotAvailable (Location, "#pragma");
                                        return true;
                                }
 
@@ -2232,12 +2470,18 @@ namespace Mono.CSharp
                {
                        int c;
                        string_builder.Length = 0;
-                                                               
-                       while ((c = getChar ()) != -1){
+
+                       //
+                       // No need to parse full string when parsing lambda arguments
+                       //
+                       if (lambda_arguments_parsing)
+                               return Token.LITERAL_STRING;                    
+                       
+                       while ((c = get_char ()) != -1){
                                if (c == '"'){
-                                       if (quoted && peekChar () == '"'){
+                                       if (quoted && peek_char () == '"'){
                                                string_builder.Append ((char) c);
-                                               getChar ();
+                                               get_char ();
                                                continue;
                                        } else {
                                                val = string_builder.ToString ();
@@ -2251,9 +2495,14 @@ namespace Mono.CSharp
                                }
 
                                if (!quoted){
-                                       c = escape (c);
+                                       int surrogate;
+                                       c = escape (c, out surrogate);
                                        if (c == -1)
                                                return Token.ERROR;
+                                       if (surrogate != 0) {
+                                               string_builder.Append ((char) c);
+                                               c = surrogate;
+                                       }
                                }
                                string_builder.Append ((char) c);
                        }
@@ -2276,21 +2525,38 @@ namespace Mono.CSharp
                        }
 
                        if (res == Token.PARTIAL) {
+                               if (parsing_block > 0) {
+                                       val = new LocatedToken (Location, "partial");
+                                       return Token.IDENTIFIER;
+                               }
+
                                // Save current position and parse next token.
                                PushPosition ();
 
                                int next_token = token ();
                                bool ok = (next_token == Token.CLASS) ||
                                        (next_token == Token.STRUCT) ||
-                                       (next_token == Token.INTERFACE);
+                                       (next_token == Token.INTERFACE) ||
+                                       (next_token == Token.VOID);
 
                                PopPosition ();
 
-                               if (ok)
+                               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");
+
                                        return res;
+                               }
 
-                               if (next_token < Token.LAST_KEYWORD)
-                                       Report.Error (267, Location, "The `partial' modifier can be used only immediately before keyword `class', `struct', or `interface'");
+                               if (next_token < Token.LAST_KEYWORD) {
+                                       Report.Error (267, Location,
+                                               "The `partial' modifier can be used only immediately before `class', `struct', `interface', or `void' keyword");
+                                       return token ();
+                               }                                       
 
                                val = new LocatedToken (Location, "partial");
                                return Token.IDENTIFIER;
@@ -2299,16 +2565,23 @@ namespace Mono.CSharp
                        return res;
                }
 
-               private int consume_identifier (int s, bool quoted) 
+               private int consume_identifier (int c, bool quoted) 
                {
-                       int pos = 1;
-                       int c = -1;
-                       
-                       id_builder [0] = (char) s;
+                       int pos = 0;
 
-                       current_location = new Location (ref_line, Col);
+                       if (c == '\\') {
+                               int surrogate;
+                               c = escape (c, out surrogate);
+                               if (surrogate != 0) {
+                                       id_builder [pos++] = (char) c;
+                                       c = surrogate;
+                               }
+                       }
+
+                       id_builder [pos++] = (char) c;
+                       current_location = new Location (ref_line, hidden ? -1 : Col);
 
-                       while ((c = getChar ()) != -1) {
+                       while ((c = get_char ()) != -1) {
                        loop:
                                if (is_identifier_part_character ((char) c)){
                                        if (pos == max_id_size){
@@ -2319,7 +2592,13 @@ namespace Mono.CSharp
                                        id_builder [pos++] = (char) c;
 //                                     putback_char = -1;
                                } else if (c == '\\') {
-                                       c = escape (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;
+                                       }
                                        goto loop;
                                } else {
 //                                     putback_char = c;
@@ -2330,9 +2609,9 @@ namespace Mono.CSharp
 
                        //
                        // Optimization: avoids doing the keyword lookup
-                       // on uppercase letters and _
+                       // on uppercase letters
                        //
-                       if (!quoted && (s >= 'a' || s == '_')){
+                       if (id_builder [0] >= '_' && !quoted) {
                                int keyword = GetKeyword (id_builder, pos);
                                if (keyword != -1) {
                                        val = Location;
@@ -2350,7 +2629,7 @@ namespace Mono.CSharp
                                if (val != null) {
                                        val = new LocatedToken (Location, (string) val);
                                        if (quoted)
-                                               escapedIdentifiers.Add (val);
+                                               escaped_identifiers.Add (val);
                                        return Token.IDENTIFIER;
                                }
                        }
@@ -2375,7 +2654,7 @@ namespace Mono.CSharp
 
                        val = new LocatedToken (Location, (string) val);
                        if (quoted)
-                               escapedIdentifiers.Add (val);
+                               escaped_identifiers.Add (val);
                        return Token.IDENTIFIER;
                }
                
@@ -2387,15 +2666,21 @@ namespace Mono.CSharp
 
                        // Whether we have seen comments on the current line
                        bool comments_seen = false;
-                       
                        val = null;
-                       for (;(c = getChar ()) != -1;) {
-                               if (c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == 0xa0)
+                       for (;(c = get_char ()) != -1;) {
+                               if (c == '\t'){
+                                       col = ((col + 8) / 8) * 8;
+                                       continue;
+                               }
+                               
+                               if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0 || c == 0)
                                        continue;
 
                                if (c == '\r') {
-                                       if (peekChar () == '\n')
-                                               getChar ();
+                                       if (peek_char () != '\n')
+                                               advance_line ();
+                                       else
+                                               get_char ();
 
                                        any_token_seen |= tokens_seen;
                                        tokens_seen = false;
@@ -2405,14 +2690,14 @@ namespace Mono.CSharp
 
                                // Handle double-slash comments.
                                if (c == '/'){
-                                       int d = peekChar ();
+                                       int d = peek_char ();
                                
                                        if (d == '/'){
-                                               getChar ();
-                                               if (RootContext.Documentation != null && peekChar () == '/') {
-                                                       getChar ();
+                                               get_char ();
+                                               if (RootContext.Documentation != null && peek_char () == '/') {
+                                                       get_char ();
                                                        // Don't allow ////.
-                                                       if ((d = peekChar ()) != '/') {
+                                                       if ((d = peek_char ()) != '/') {
                                                                update_comment_location ();
                                                                if (doc_state == XmlCommentState.Allowed)
                                                                        handle_one_line_xml_comment ();
@@ -2420,22 +2705,21 @@ namespace Mono.CSharp
                                                                        warn_incorrect_doc_comment ();
                                                        }
                                                }
-                                               while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
-                                                       if (d == '\n'){
-                                                       }
+                                               while ((d = get_char ()) != -1 && (d != '\n') && d != '\r');
+
                                                any_token_seen |= tokens_seen;
                                                tokens_seen = false;
                                                comments_seen = false;
                                                continue;
                                        } else if (d == '*'){
-                                               getChar ();
+                                               get_char ();
                                                bool docAppend = false;
-                                               if (RootContext.Documentation != null && peekChar () == '*') {
-                                                       getChar ();
+                                               if (RootContext.Documentation != null && peek_char () == '*') {
+                                                       get_char ();
                                                        update_comment_location ();
                                                        // But when it is /**/, just do nothing.
-                                                       if (peekChar () == '/') {
-                                                               getChar ();
+                                                       if (peek_char () == '/') {
+                                                               get_char ();
                                                                continue;
                                                        }
                                                        if (doc_state == XmlCommentState.Allowed)
@@ -2452,9 +2736,9 @@ namespace Mono.CSharp
 
                                                Location start_location = Location;
 
-                                               while ((d = getChar ()) != -1){
-                                                       if (d == '*' && peekChar () == '/'){
-                                                               getChar ();
+                                               while ((d = get_char ()) != -1){
+                                                       if (d == '*' && peek_char () == '/'){
+                                                               get_char ();
                                                                comments_seen = true;
                                                                break;
                                                        }
@@ -2488,11 +2772,11 @@ namespace Mono.CSharp
                                }
 
                        is_punct_label:
-                               current_location = new Location (ref_line, Col);
+                               current_location = new Location (ref_line, hidden ? -1 : Col);
                                if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
                                        tokens_seen = true;
                                        if (doread){
-                                               getChar ();
+                                               get_char ();
                                        }
                                        return t;
                                }
@@ -2512,7 +2796,7 @@ namespace Mono.CSharp
 
                                if (c == '.'){
                                        tokens_seen = true;
-                                       int peek = peekChar ();
+                                       int peek = peek_char ();
                                        if (peek >= '0' && peek <= '9')
                                                return is_number (c);
                                        return Token.DOT;
@@ -2528,7 +2812,7 @@ namespace Mono.CSharp
                                                continue;
 
                                        bool directive_expected = false;
-                                       while ((c = getChar ()) != -1) {
+                                       while ((c = get_char ()) != -1) {
                                                if (col == 1) {
                                                        directive_expected = true;
                                                } else if (!directive_expected) {
@@ -2562,7 +2846,7 @@ namespace Mono.CSharp
                                        return consume_string (false);
 
                                if (c == '\''){
-                                       c = getChar ();
+                                       c = get_char ();
                                        tokens_seen = true;
                                        if (c == '\''){
                                                error_details = "Empty character literal";
@@ -2573,19 +2857,23 @@ namespace Mono.CSharp
                                                Report.Error (1010, Location, "Newline in constant");
                                                return Token.ERROR;
                                        }
-                                       c = escape (c);
+
+                                       int surrogate;
+                                       c = escape (c, out surrogate);
                                        if (c == -1)
                                                return Token.ERROR;
-                                       val = new System.Char ();
+                                       if (surrogate != 0)
+                                               throw new NotImplementedException ();
+
                                        val = (char) c;
-                                       c = getChar ();
+                                       c = get_char ();
 
                                        if (c != '\''){
                                                error_details = "Too many characters in character literal";
                                                Report.Error (1012, Location, error_details);
 
                                                // Try to recover, read until newline or next "'"
-                                               while ((c = getChar ()) != -1){
+                                               while ((c = get_char ()) != -1){
                                                        if (c == '\n'){
                                                                break;
                                                        }
@@ -2598,7 +2886,7 @@ namespace Mono.CSharp
                                }
                                
                                if (c == '@') {
-                                       c = getChar ();
+                                       c = get_char ();
                                        if (c == '"') {
                                                tokens_seen = true;
                                                return consume_string (true);
@@ -2609,6 +2897,13 @@ namespace Mono.CSharp
                                        }
                                }
 
+                               if (c == EvalStatementParserCharacter)
+                                       return Token.EVAL_STATEMENT_PARSER;
+                               if (c == EvalCompilationUnitParserCharacter)
+                                       return Token.EVAL_COMPILATION_UNIT_PARSER;
+                               if (c == EvalUsingDeclarationsParserCharacter)
+                                       return Token.EVAL_USING_DECLARATIONS_UNIT_PARSER;
+                               
                                error_details = ((char)c).ToString ();
                                
                                return Token.ERROR;
@@ -2623,10 +2918,10 @@ namespace Mono.CSharp
                private void handle_one_line_xml_comment ()
                {
                        int c;
-                       while ((c = peekChar ()) == ' ')
-                               getChar (); // skip heading whitespaces.
-                       while ((c = peekChar ()) != -1 && c != '\n' && c != '\r') {
-                               xml_comment_buffer.Append ((char) getChar ());
+                       while ((c = peek_char ()) == ' ')
+                               get_char (); // skip heading whitespaces.
+                       while ((c = peek_char ()) != -1 && c != '\n' && c != '\r') {
+                               xml_comment_buffer.Append ((char) get_char ());
                        }
                        if (c == '\r' || c == '\n')
                                xml_comment_buffer.Append (Environment.NewLine);
@@ -2671,7 +2966,7 @@ namespace Mono.CSharp
                        if (current_comment_location.IsNull) {
                                // "-2" is for heading "//" or "/*"
                                current_comment_location =
-                                       new Location (ref_line, col - 2);
+                                       new Location (ref_line, hidden ? -1 : col - 2);
                        }
                }
 
@@ -2723,7 +3018,7 @@ namespace Mono.CSharp
                public void cleanup ()
                {
                        if (ifstack != null && ifstack.Count >= 1) {
-                               current_location = new Location (ref_line, Col);
+                               current_location = new Location (ref_line, hidden ? -1 : Col);
                                int state = (int) ifstack.Pop ();
                                if ((state & REGION) != 0)
                                        Report.Error (1038, Location, "#endregion directive expected");