2004-06-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / mcs / cs-tokenizer.cs
index 7cd6268b1ca681a6d9289b8ff1064e9a5d6f2ef6..5191948f407398ec12fa615e59c95e1ead4eb4fb 100755 (executable)
@@ -1,3 +1,4 @@
+// -*- coding: dos -*-\r
 //\r
 // cs-tokenizer.cs: The Tokenizer for the C# compiler\r
 //                  This also implements the preprocessor\r
@@ -6,7 +7,7 @@
 //\r
 // Licensed under the terms of the GNU GPL\r
 //\r
-// (C) 2001 Ximian, Inc (http://www.ximian.com)\r
+// (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)\r
 //\r
 \r
 /*\r
@@ -30,8 +31,9 @@ namespace Mono.CSharp
 \r
        public class Tokenizer : yyParser.yyInput\r
        {\r
-               StreamReader reader;\r
-               public string ref_name;\r
+               SeekableStreamReader reader;\r
+               public SourceFile ref_name;\r
+               public SourceFile file_name;\r
                public int ref_line = 1;\r
                public int line = 1;\r
                public int col = 1;\r
@@ -94,8 +96,8 @@ namespace Mono.CSharp
                                if (current_token_name == null)\r
                                        current_token_name = current_token.ToString ();\r
 \r
-                               return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name,\r
-                                                                                      line,\r
+                               return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name.Name,\r
+                                                                                      ref_line,\r
                                                                                       col,\r
                                                                                       current_token_name,\r
                                                                                       det);\r
@@ -135,7 +137,7 @@ namespace Mono.CSharp
                //\r
                // Class variables\r
                // \r
-               static Hashtable keywords;\r
+               static CharArrayHashtable[] keywords;\r
                static NumberStyles styles;\r
                static NumberFormatInfo csharp_format_info;\r
                \r
@@ -154,15 +156,23 @@ namespace Mono.CSharp
                const int TAKEN_BEFORE  = 2;\r
                const int ELSE_SEEN     = 4;\r
                const int PARENT_TAKING = 8;\r
-               \r
+               const int REGION        = 16;           \r
+\r
                //\r
                // pre-processor if stack state:\r
                //\r
                Stack ifstack;\r
 \r
-               static System.Text.StringBuilder id_builder;\r
                static System.Text.StringBuilder string_builder;\r
-               static System.Text.StringBuilder number_builder;\r
+\r
+               const int max_id_size = 512;\r
+               static char [] id_builder = new char [max_id_size];\r
+\r
+               static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];\r
+\r
+               const int max_number_size = 128;\r
+               static char [] number_builder = new char [max_number_size];\r
+               static int number_pos;\r
                \r
                //\r
                // Details about the error encoutered by the tokenizer\r
@@ -177,7 +187,7 @@ namespace Mono.CSharp
                \r
                public int Line {\r
                        get {\r
-                               return line;\r
+                               return ref_line;\r
                        }\r
                }\r
 \r
@@ -186,93 +196,101 @@ namespace Mono.CSharp
                                return col;\r
                        }\r
                }\r
-               \r
+\r
+               static void AddKeyword (string kw, int token) {\r
+                       if (keywords [kw.Length] == null) {\r
+                               keywords [kw.Length] = new CharArrayHashtable (kw.Length);\r
+                       }\r
+                       keywords [kw.Length] [kw.ToCharArray ()] = token;\r
+               }\r
+\r
                static void InitTokens ()\r
                {\r
-                       keywords = new Hashtable ();\r
-\r
-                       keywords.Add ("abstract", Token.ABSTRACT);\r
-                       keywords.Add ("as", Token.AS);\r
-                       keywords.Add ("add", Token.ADD);\r
-                       keywords.Add ("assembly", Token.ASSEMBLY);\r
-                       keywords.Add ("base", Token.BASE);\r
-                       keywords.Add ("bool", Token.BOOL);\r
-                       keywords.Add ("break", Token.BREAK);\r
-                       keywords.Add ("byte", Token.BYTE);\r
-                       keywords.Add ("case", Token.CASE);\r
-                       keywords.Add ("catch", Token.CATCH);\r
-                       keywords.Add ("char", Token.CHAR);\r
-                       keywords.Add ("checked", Token.CHECKED);\r
-                       keywords.Add ("class", Token.CLASS);\r
-                       keywords.Add ("const", Token.CONST);\r
-                       keywords.Add ("continue", Token.CONTINUE);\r
-                       keywords.Add ("decimal", Token.DECIMAL);\r
-                       keywords.Add ("default", Token.DEFAULT);\r
-                       keywords.Add ("delegate", Token.DELEGATE);\r
-                       keywords.Add ("do", Token.DO);\r
-                       keywords.Add ("double", Token.DOUBLE);\r
-                       keywords.Add ("else", Token.ELSE);\r
-                       keywords.Add ("enum", Token.ENUM);\r
-                       keywords.Add ("event", Token.EVENT);\r
-                       keywords.Add ("explicit", Token.EXPLICIT);\r
-                       keywords.Add ("extern", Token.EXTERN);\r
-                       keywords.Add ("false", Token.FALSE);\r
-                       keywords.Add ("finally", Token.FINALLY);\r
-                       keywords.Add ("fixed", Token.FIXED);\r
-                       keywords.Add ("float", Token.FLOAT);\r
-                       keywords.Add ("for", Token.FOR);\r
-                       keywords.Add ("foreach", Token.FOREACH);\r
-                       keywords.Add ("goto", Token.GOTO);\r
-                       keywords.Add ("get", Token.GET);\r
-                       keywords.Add ("if", Token.IF);\r
-                       keywords.Add ("implicit", Token.IMPLICIT);\r
-                       keywords.Add ("in", Token.IN);\r
-                       keywords.Add ("int", Token.INT);\r
-                       keywords.Add ("interface", Token.INTERFACE);\r
-                       keywords.Add ("internal", Token.INTERNAL);\r
-                       keywords.Add ("is", Token.IS);\r
-                       keywords.Add ("lock", Token.LOCK);\r
-                       keywords.Add ("long", Token.LONG);\r
-                       keywords.Add ("namespace", Token.NAMESPACE);\r
-                       keywords.Add ("new", Token.NEW);\r
-                       keywords.Add ("null", Token.NULL);\r
-                       keywords.Add ("object", Token.OBJECT);\r
-                       keywords.Add ("operator", Token.OPERATOR);\r
-                       keywords.Add ("out", Token.OUT);\r
-                       keywords.Add ("override", Token.OVERRIDE);\r
-                       keywords.Add ("params", Token.PARAMS);\r
-                       keywords.Add ("private", Token.PRIVATE);\r
-                       keywords.Add ("protected", Token.PROTECTED);\r
-                       keywords.Add ("public", Token.PUBLIC);\r
-                       keywords.Add ("readonly", Token.READONLY);\r
-                       keywords.Add ("ref", Token.REF);\r
-                       keywords.Add ("remove", Token.REMOVE);\r
-                       keywords.Add ("return", Token.RETURN);\r
-                       keywords.Add ("sbyte", Token.SBYTE);\r
-                       keywords.Add ("sealed", Token.SEALED);\r
-                       keywords.Add ("set", Token.SET);\r
-                       keywords.Add ("short", Token.SHORT);\r
-                       keywords.Add ("sizeof", Token.SIZEOF);\r
-                       keywords.Add ("stackalloc", Token.STACKALLOC);\r
-                       keywords.Add ("static", Token.STATIC);\r
-                       keywords.Add ("string", Token.STRING);\r
-                       keywords.Add ("struct", Token.STRUCT);\r
-                       keywords.Add ("switch", Token.SWITCH);\r
-                       keywords.Add ("this", Token.THIS);\r
-                       keywords.Add ("throw", Token.THROW);\r
-                       keywords.Add ("true", Token.TRUE);\r
-                       keywords.Add ("try", Token.TRY);\r
-                       keywords.Add ("typeof", Token.TYPEOF);\r
-                       keywords.Add ("uint", Token.UINT);\r
-                       keywords.Add ("ulong", Token.ULONG);\r
-                       keywords.Add ("unchecked", Token.UNCHECKED);\r
-                       keywords.Add ("unsafe", Token.UNSAFE);\r
-                       keywords.Add ("ushort", Token.USHORT);\r
-                       keywords.Add ("using", Token.USING);\r
-                       keywords.Add ("virtual", Token.VIRTUAL);\r
-                       keywords.Add ("void", Token.VOID);\r
-                       keywords.Add ("volatile", Token.VOLATILE);\r
-                       keywords.Add ("while", Token.WHILE);\r
+                       keywords = new CharArrayHashtable [64];\r
+\r
+                       AddKeyword ("__arglist", Token.ARGLIST);\r
+                       AddKeyword ("abstract", Token.ABSTRACT);\r
+                       AddKeyword ("as", Token.AS);\r
+                       AddKeyword ("add", Token.ADD);\r
+                       AddKeyword ("assembly", Token.ASSEMBLY);\r
+                       AddKeyword ("base", Token.BASE);\r
+                       AddKeyword ("bool", Token.BOOL);\r
+                       AddKeyword ("break", Token.BREAK);\r
+                       AddKeyword ("byte", Token.BYTE);\r
+                       AddKeyword ("case", Token.CASE);\r
+                       AddKeyword ("catch", Token.CATCH);\r
+                       AddKeyword ("char", Token.CHAR);\r
+                       AddKeyword ("checked", Token.CHECKED);\r
+                       AddKeyword ("class", Token.CLASS);\r
+                       AddKeyword ("const", Token.CONST);\r
+                       AddKeyword ("continue", Token.CONTINUE);\r
+                       AddKeyword ("decimal", Token.DECIMAL);\r
+                       AddKeyword ("default", Token.DEFAULT);\r
+                       AddKeyword ("delegate", Token.DELEGATE);\r
+                       AddKeyword ("do", Token.DO);\r
+                       AddKeyword ("double", Token.DOUBLE);\r
+                       AddKeyword ("else", Token.ELSE);\r
+                       AddKeyword ("enum", Token.ENUM);\r
+                       AddKeyword ("event", Token.EVENT);\r
+                       AddKeyword ("explicit", Token.EXPLICIT);\r
+                       AddKeyword ("extern", Token.EXTERN);\r
+                       AddKeyword ("false", Token.FALSE);\r
+                       AddKeyword ("finally", Token.FINALLY);\r
+                       AddKeyword ("fixed", Token.FIXED);\r
+                       AddKeyword ("float", Token.FLOAT);\r
+                       AddKeyword ("for", Token.FOR);\r
+                       AddKeyword ("foreach", Token.FOREACH);\r
+                       AddKeyword ("goto", Token.GOTO);\r
+                       AddKeyword ("get", Token.GET);\r
+                       AddKeyword ("if", Token.IF);\r
+                       AddKeyword ("implicit", Token.IMPLICIT);\r
+                       AddKeyword ("in", Token.IN);\r
+                       AddKeyword ("int", Token.INT);\r
+                       AddKeyword ("interface", Token.INTERFACE);\r
+                       AddKeyword ("internal", Token.INTERNAL);\r
+                       AddKeyword ("is", Token.IS);\r
+                       AddKeyword ("lock", Token.LOCK);\r
+                       AddKeyword ("long", Token.LONG);\r
+                       AddKeyword ("namespace", Token.NAMESPACE);\r
+                       AddKeyword ("new", Token.NEW);\r
+                       AddKeyword ("null", Token.NULL);\r
+                       AddKeyword ("object", Token.OBJECT);\r
+                       AddKeyword ("operator", Token.OPERATOR);\r
+                       AddKeyword ("out", Token.OUT);\r
+                       AddKeyword ("override", Token.OVERRIDE);\r
+                       AddKeyword ("params", Token.PARAMS);\r
+                       AddKeyword ("private", Token.PRIVATE);\r
+                       AddKeyword ("protected", Token.PROTECTED);\r
+                       AddKeyword ("public", Token.PUBLIC);\r
+                       AddKeyword ("readonly", Token.READONLY);\r
+                       AddKeyword ("ref", Token.REF);\r
+                       AddKeyword ("remove", Token.REMOVE);\r
+                       AddKeyword ("return", Token.RETURN);\r
+                       AddKeyword ("sbyte", Token.SBYTE);\r
+                       AddKeyword ("sealed", Token.SEALED);\r
+                       AddKeyword ("set", Token.SET);\r
+                       AddKeyword ("short", Token.SHORT);\r
+                       AddKeyword ("sizeof", Token.SIZEOF);\r
+                       AddKeyword ("stackalloc", Token.STACKALLOC);\r
+                       AddKeyword ("static", Token.STATIC);\r
+                       AddKeyword ("string", Token.STRING);\r
+                       AddKeyword ("struct", Token.STRUCT);\r
+                       AddKeyword ("switch", Token.SWITCH);\r
+                       AddKeyword ("this", Token.THIS);\r
+                       AddKeyword ("throw", Token.THROW);\r
+                       AddKeyword ("true", Token.TRUE);\r
+                       AddKeyword ("try", Token.TRY);\r
+                       AddKeyword ("typeof", Token.TYPEOF);\r
+                       AddKeyword ("uint", Token.UINT);\r
+                       AddKeyword ("ulong", Token.ULONG);\r
+                       AddKeyword ("unchecked", Token.UNCHECKED);\r
+                       AddKeyword ("unsafe", Token.UNSAFE);\r
+                       AddKeyword ("ushort", Token.USHORT);\r
+                       AddKeyword ("using", Token.USING);\r
+                       AddKeyword ("virtual", Token.VIRTUAL);\r
+                       AddKeyword ("void", Token.VOID);\r
+                       AddKeyword ("volatile", Token.VOLATILE);\r
+                       AddKeyword ("while", Token.WHILE);\r
                }\r
 \r
                //\r
@@ -284,28 +302,33 @@ namespace Mono.CSharp
                        csharp_format_info = NumberFormatInfo.InvariantInfo;\r
                        styles = NumberStyles.Float;\r
                        \r
-                       id_builder = new System.Text.StringBuilder ();\r
                        string_builder = new System.Text.StringBuilder ();\r
-                       number_builder = new System.Text.StringBuilder ();\r
                }\r
 \r
-               bool is_keyword (string name)\r
+               int GetKeyword (char[] id, int id_len)\r
                {\r
-                       bool res;\r
+                       /*\r
+                        * Keywords are stored in an array of hashtables grouped by their\r
+                        * length.\r
+                        */\r
+\r
+                       if ((id_len >= keywords.Length) || (keywords [id_len] == null))\r
+                               return -1;\r
+                       object o = keywords [id_len] [id];\r
+\r
+                       if (o == null)\r
+                               return -1;\r
                        \r
-                       res = keywords.Contains (name);\r
-                       if (handle_get_set == false && (name == "get" || name == "set"))\r
-                               return false;\r
-                       if (handle_remove_add == false && (name == "remove" || name == "add"))\r
-                               return false;\r
-                       if (handle_assembly == false && (name == "assembly"))\r
-                               return false;\r
+                       int res = (int) o;\r
+\r
+                       if (handle_get_set == false && (res == Token.GET || res == Token.SET))\r
+                               return -1;\r
+                       if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))\r
+                               return -1;\r
+                       if (handle_assembly == false && res == Token.ASSEMBLY)\r
+                               return -1;\r
                        return res;\r
-               }\r
-\r
-               int GetKeyword (string name)\r
-               {\r
-                       return (int) (keywords [name]);\r
+                       \r
                }\r
 \r
                public Location Location {\r
@@ -324,9 +347,10 @@ namespace Mono.CSharp
                        defines [def] = true;\r
                }\r
                \r
-               public Tokenizer (StreamReader input, string fname, ArrayList defs)\r
+               public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)\r
                {\r
-                       this.ref_name = fname;\r
+                       this.ref_name = file;\r
+                       this.file_name = file;\r
                        reader = input;\r
                        \r
                        putback_char = -1;\r
@@ -341,17 +365,36 @@ namespace Mono.CSharp
                        // FIXME: This could be `Location.Push' but we have to\r
                        // find out why the MS compiler allows this\r
                        //\r
-                       Mono.CSharp.Location.Push (fname);\r
+                       Mono.CSharp.Location.Push (file);\r
                }\r
 \r
-               bool is_identifier_start_character (char c)\r
+               public static void Cleanup () {\r
+                       identifiers = null;\r
+               }\r
+\r
+               static bool is_identifier_start_character (char c)\r
                {\r
-                       return Char.IsLetter (c) || c == '_' ;\r
+                       return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);\r
                }\r
 \r
-               bool is_identifier_part_character (char c)\r
+               static bool is_identifier_part_character (char c)\r
+               {\r
+                       return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);\r
+               }\r
+               \r
+               public static bool IsValidIdentifier (string s)\r
                {\r
-                       return (Char.IsLetter (c) || Char.IsDigit (c) || c == '_');\r
+                       if (s == null || s.Length == 0)\r
+                               return false;\r
+                       \r
+                       if (!is_identifier_start_character (s [0]))\r
+                               return false;\r
+                       \r
+                       for (int i = 1; i < s.Length; i ++)\r
+                               if (! is_identifier_part_character (s [i]))\r
+                                       return false;\r
+                       \r
+                       return true;\r
                }\r
 \r
                int is_punct (char c, ref bool doread)\r
@@ -372,8 +415,28 @@ namespace Mono.CSharp
                                return Token.CLOSE_BRACKET;\r
                        case '(':\r
                                return Token.OPEN_PARENS;\r
-                       case ')':\r
-                               return Token.CLOSE_PARENS;\r
+                       case ')': {\r
+                               if (deambiguate_close_parens == 0)\r
+                                       return Token.CLOSE_PARENS;\r
+\r
+                               --deambiguate_close_parens;\r
+\r
+                               // Save current position and parse next token.\r
+                               int old = reader.Position;\r
+                               int new_token = token ();\r
+                               reader.Position = old;\r
+                               putback_char = -1;\r
+\r
+                               if (new_token == Token.OPEN_PARENS)\r
+                                       return Token.CLOSE_PARENS_OPEN_PARENS;\r
+                               else if (new_token == Token.MINUS)\r
+                                       return Token.CLOSE_PARENS_MINUS;\r
+                               else if (IsCastToken (new_token))\r
+                                       return Token.CLOSE_PARENS_CAST;\r
+                               else\r
+                                       return Token.CLOSE_PARENS_NO_CAST;\r
+                       }\r
+\r
                        case ',':\r
                                return Token.COMMA;\r
                        case ':':\r
@@ -517,17 +580,39 @@ namespace Mono.CSharp
                        return Token.ERROR;\r
                }\r
 \r
+               int deambiguate_close_parens = 0;\r
+\r
+               public void Deambiguate_CloseParens ()\r
+               {\r
+                       putback (')');\r
+                       deambiguate_close_parens++;\r
+               }\r
+\r
+               void Error_NumericConstantTooLong ()\r
+               {\r
+                       Report.Error (1021, Location, "Numeric constant too long");                     \r
+               }\r
+               \r
                bool decimal_digits (int c)\r
                {\r
                        int d;\r
                        bool seen_digits = false;\r
                        \r
-                       if (c != -1)\r
-                               number_builder.Append ((char) c);\r
+                       if (c != -1){\r
+                               if (number_pos == max_number_size)\r
+                                       Error_NumericConstantTooLong ();\r
+                               number_builder [number_pos++] = (char) c;\r
+                       }\r
                        \r
-                       while ((d = peekChar ()) != -1){\r
-                               if (Char.IsDigit ((char)d)){\r
-                                       number_builder.Append ((char) d);\r
+                       //\r
+                       // We use peekChar2, because decimal_digits needs to do a \r
+                       // 2-character look-ahead (5.ToString for example).\r
+                       //\r
+                       while ((d = peekChar2 ()) != -1){\r
+                               if (d >= '0' && d <= '9'){\r
+                                       if (number_pos == max_number_size)\r
+                                               Error_NumericConstantTooLong ();\r
+                                       number_builder [number_pos++] = (char) d;\r
                                        getChar ();\r
                                        seen_digits = true;\r
                                } else\r
@@ -537,26 +622,16 @@ namespace Mono.CSharp
                        return seen_digits;\r
                }\r
 \r
-               bool is_hex (char e)\r
+               bool is_hex (int e)\r
                {\r
-                       return Char.IsDigit (e) || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');\r
+                       return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');\r
                }\r
                \r
                void hex_digits (int c)\r
                {\r
-                       int d;\r
-\r
                        if (c != -1)\r
-                               number_builder.Append ((char) c);\r
-                       while ((d = peekChar ()) != -1){\r
-                               char e = Char.ToUpper ((char) d);\r
-                               \r
-                               if (is_hex (e)){\r
-                                       number_builder.Append ((char) e);\r
-                                       getChar ();\r
-                               } else\r
-                                       break;\r
-                       }\r
+                               number_builder [number_pos++] = (char) c;\r
+                       \r
                }\r
                \r
                int real_type_suffix (int c)\r
@@ -666,8 +741,21 @@ namespace Mono.CSharp
                int adjust_int (int c)\r
                {\r
                        try {\r
-                               ulong ul = System.UInt64.Parse (number_builder.ToString ());\r
-                               return integer_type_suffix (ul, c);\r
+                               if (number_pos > 9){\r
+                                       ulong ul = (uint) (number_builder [0] - '0');\r
+\r
+                                       for (int i = 1; i < number_pos; i++){\r
+                                               ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));\r
+                                       }\r
+                                       return integer_type_suffix (ul, c);\r
+                               } else {\r
+                                       uint ui = (uint) (number_builder [0] - '0');\r
+\r
+                                       for (int i = 1; i < number_pos; i++){\r
+                                               ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));\r
+                                       }\r
+                                       return integer_type_suffix (ui, c);\r
+                               }\r
                        } catch (OverflowException) {\r
                                error_details = "Integral constant is too large";\r
                                Report.Error (1021, Location, error_details);\r
@@ -678,7 +766,7 @@ namespace Mono.CSharp
                \r
                int adjust_real (int t)\r
                {\r
-                       string s = number_builder.ToString ();\r
+                       string s = new String (number_builder, 0, number_pos);\r
 \r
                        switch (t){\r
                        case Token.LITERAL_DECIMAL:\r
@@ -715,6 +803,36 @@ namespace Mono.CSharp
                        return t;\r
                }\r
 \r
+               int handle_hex ()\r
+               {\r
+                       int d;\r
+                       ulong ul;\r
+                       \r
+                       getChar ();\r
+                       while ((d = peekChar ()) != -1){\r
+                               if (is_hex (d)){\r
+                                       number_builder [number_pos++] = (char) d;\r
+                                       getChar ();\r
+                               } else\r
+                                       break;\r
+                       }\r
+                       \r
+                       string s = new String (number_builder, 0, number_pos);\r
+                       try {\r
+                               if (number_pos <= 8)\r
+                                       ul = System.UInt32.Parse (s, NumberStyles.HexNumber);\r
+                               else\r
+                                       ul = System.UInt64.Parse (s, NumberStyles.HexNumber);\r
+                       } catch (OverflowException){\r
+                               error_details = "Integral constant is too large";\r
+                               Report.Error (1021, Location, error_details);\r
+                               val = 0ul;\r
+                               return Token.LITERAL_INTEGER;\r
+                       }\r
+                       \r
+                       return integer_type_suffix (ul, peekChar ());\r
+               }\r
+\r
                //\r
                // Invoked if we know we have .digits or digits\r
                //\r
@@ -723,18 +841,14 @@ namespace Mono.CSharp
                        bool is_real = false;\r
                        int type;\r
 \r
-                       number_builder.Length = 0;\r
+                       number_pos = 0;\r
 \r
-                       if (Char.IsDigit ((char)c)){\r
-                               if (c == '0' && peekChar () == 'x' || peekChar () == 'X'){\r
-                                       ulong ul;\r
-                                       getChar ();\r
-                                       hex_digits (-1);\r
-\r
-                                       string s = number_builder.ToString ();\r
+                       if (c >= '0' && c <= '9'){\r
+                               if (c == '0'){\r
+                                       int peek = peekChar ();\r
 \r
-                                       ul = System.UInt64.Parse (s, NumberStyles.HexNumber);\r
-                                       return integer_type_suffix (ul, peekChar ());\r
+                                       if (peek == 'x' || peek == 'X')\r
+                                               return handle_hex ();\r
                                }\r
                                decimal_digits (c);\r
                                c = getChar ();\r
@@ -750,21 +864,35 @@ namespace Mono.CSharp
                                        c = getChar ();\r
                                } else {\r
                                        putback ('.');\r
-                                       number_builder.Length -= 1;\r
+                                       number_pos--;\r
                                        return adjust_int (-1);\r
                                }\r
                        }\r
                        \r
                        if (c == 'e' || c == 'E'){\r
                                is_real = true;\r
-                               number_builder.Append ("e");\r
+                               if (number_pos == max_number_size)\r
+                                       Error_NumericConstantTooLong ();\r
+                               number_builder [number_pos++] = 'e';\r
                                c = getChar ();\r
                                \r
-                               if (c == '+')\r
-                                       number_builder.Append ((char) c);\r
-                               else if (c == '-')\r
-                                       number_builder.Append ((char) c);\r
-                               decimal_digits (-1);\r
+                               if (c == '+'){\r
+                                       if (number_pos == max_number_size)\r
+                                               Error_NumericConstantTooLong ();\r
+                                       number_builder [number_pos++] = '+';\r
+                                       c = -1;\r
+                               } else if (c == '-') {\r
+                                       if (number_pos == max_number_size)\r
+                                               Error_NumericConstantTooLong ();\r
+                                       number_builder [number_pos++] = '-';\r
+                                       c = -1;\r
+                               } else {\r
+                                       if (number_pos == max_number_size)\r
+                                               Error_NumericConstantTooLong ();\r
+                                       number_builder [number_pos++] = '+';\r
+                               }\r
+                                       \r
+                               decimal_digits (c);\r
                                c = getChar ();\r
                        }\r
 \r
@@ -791,27 +919,27 @@ namespace Mono.CSharp
                //\r
                int getHex (int count, out bool error)\r
                {\r
-                       int [] buffer = new int [8];\r
                        int i;\r
                        int total = 0;\r
                        int c;\r
-                       char e;\r
                        int top = count != -1 ? count : 4;\r
                        \r
                        getChar ();\r
                        error = false;\r
                        for (i = 0; i < top; i++){\r
                                c = getChar ();\r
-                               e = Char.ToUpper ((char) c);\r
                                \r
-                               if (!is_hex (e)){\r
+                               if (c >= '0' && c <= '9')\r
+                                       c = (int) c - (int) '0';\r
+                               else if (c >= 'A' && c <= 'F')\r
+                                       c = (int) c - (int) 'A' + 10;\r
+                               else if (c >= 'a' && c <= 'f')\r
+                                       c = (int) c - (int) 'a' + 10;\r
+                               else {\r
                                        error = true;\r
                                        return 0;\r
                                }\r
-                               if (Char.IsDigit (e))\r
-                                       c = (int) e - (int) '0';\r
-                               else\r
-                                       c = (int) e - (int) 'A' + 10;\r
+                               \r
                                total = (total * 16) + c;\r
                                if (count == -1){\r
                                        int p = peekChar ();\r
@@ -895,13 +1023,26 @@ namespace Mono.CSharp
                {\r
                        if (putback_char != -1)\r
                                return putback_char;\r
-                       return reader.Peek ();\r
+                       putback_char = reader.Read ();\r
+                       return putback_char;\r
                }\r
 \r
-               void putback (int c)\r
+               int peekChar2 ()\r
                {\r
                        if (putback_char != -1)\r
+                               return putback_char;\r
+                       return reader.Peek ();\r
+               }\r
+               \r
+               void putback (int c)\r
+               {\r
+                       if (putback_char != -1){\r
+                               Console.WriteLine ("Col: " + col);\r
+                               Console.WriteLine ("Row: " + line);\r
+                               Console.WriteLine ("Name: " + ref_name.Name);\r
+                               Console.WriteLine ("Current [{0}] putting back [{1}]  ", putback_char, c);\r
                                throw new Exception ("This should not happen putback on putback");\r
+                       }\r
                        putback_char = c;\r
                }\r
 \r
@@ -920,13 +1061,58 @@ namespace Mono.CSharp
                {\r
                        return val;\r
                }\r
-               \r
-               public int token ()\r
+\r
+               bool IsCastToken (int token)\r
                {\r
-                       current_token = xtoken ();\r
-                       return current_token;\r
+                       switch (token) {\r
+                       case Token.BANG:\r
+                       case Token.TILDE:\r
+                       case Token.IDENTIFIER:\r
+                       case Token.LITERAL_INTEGER:\r
+                       case Token.LITERAL_FLOAT:\r
+                       case Token.LITERAL_DOUBLE:\r
+                       case Token.LITERAL_DECIMAL:\r
+                       case Token.LITERAL_CHARACTER:\r
+                       case Token.LITERAL_STRING:\r
+                       case Token.BASE:\r
+                       case Token.CHECKED:\r
+                       case Token.FALSE:\r
+                       case Token.FIXED:\r
+                       case Token.NEW:\r
+                       case Token.NULL:\r
+                       case Token.SIZEOF:\r
+                       case Token.THIS:\r
+                       case Token.THROW:\r
+                       case Token.TRUE:\r
+                       case Token.TYPEOF:\r
+                       case Token.UNCHECKED:\r
+                       case Token.UNSAFE:\r
+\r
+                               //\r
+                               // These can be part of a member access\r
+                               //\r
+                       case Token.INT:\r
+                       case Token.UINT:\r
+                       case Token.SHORT:\r
+                       case Token.USHORT:\r
+                       case Token.LONG:\r
+                       case Token.ULONG:\r
+                       case Token.DOUBLE:\r
+                       case Token.FLOAT:\r
+                       case Token.CHAR:\r
+                               return true;\r
+\r
+                       default:\r
+                               return false;\r
+                       }\r
                }\r
 \r
+               public int token ()\r
+                {\r
+                       current_token = xtoken ();\r
+                        return current_token;\r
+                }\r
+\r
                static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();\r
                \r
                void get_cmd_arg (out string cmd, out string arg)\r
@@ -936,11 +1122,20 @@ namespace Mono.CSharp
                        tokens_seen = false;\r
                        arg = "";\r
                        static_cmd_arg.Length = 0;\r
-                               \r
-                       while ((c = getChar ()) != -1 && (c != '\n') && (c != ' ') && (c != '\t')){\r
-                               if (c == '\r')\r
-                                       continue;\r
-                               static_cmd_arg.Append ((char) c);\r
+\r
+                       // skip over white space\r
+                       while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))\r
+                               ;\r
+\r
+\r
+                       while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){\r
+                               if (is_identifier_part_character ((char) c)){\r
+                                       static_cmd_arg.Append ((char) c);\r
+                                       c = getChar ();\r
+                               } else {\r
+                                       putback (c);\r
+                                       break;\r
+                               }\r
                        }\r
 \r
                        cmd = static_cmd_arg.ToString ();\r
@@ -949,31 +1144,34 @@ namespace Mono.CSharp
                                line++;\r
                                ref_line++;\r
                                return;\r
-                       }\r
+                       } else if (c == '\r')\r
+                               col = 0;\r
 \r
                        // skip over white space\r
-                       while ((c = getChar ()) != -1 && (c != '\n') && ((c == ' ') || (c == '\t')))\r
+                       while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))\r
                                ;\r
 \r
                        if (c == '\n'){\r
                                line++;\r
                                ref_line++;\r
                                return;\r
+                       } else if (c == '\r'){\r
+                               col = 0;\r
+                               return;\r
                        }\r
                        \r
                        static_cmd_arg.Length = 0;\r
                        static_cmd_arg.Append ((char) c);\r
                        \r
-                       while ((c = getChar ()) != -1 && (c != '\n')){\r
-                               if (c == '\r')\r
-                                       continue;\r
+                       while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){\r
                                static_cmd_arg.Append ((char) c);\r
                        }\r
 \r
                        if (c == '\n'){\r
                                line++;\r
                                ref_line++;\r
-                       }\r
+                       } else if (c == '\r')\r
+                               col = 0;\r
                        arg = static_cmd_arg.ToString ().Trim ();\r
                }\r
 \r
@@ -986,10 +1184,17 @@ namespace Mono.CSharp
                                return false;\r
 \r
                        if (arg == "default"){\r
-                               line = ref_line = line;\r
-                               return false;\r
+                               ref_line = line;\r
+                               ref_name = file_name;\r
+                               Location.Push (ref_name);\r
+                               return true;\r
+                       } else if (arg == "hidden"){\r
+                               //\r
+                               // We ignore #line hidden\r
+                               //\r
+                               return true;\r
                        }\r
-                       \r
+\r
                        try {\r
                                int pos;\r
 \r
@@ -999,7 +1204,11 @@ namespace Mono.CSharp
                                        \r
                                        char [] quotes = { '\"' };\r
                                        \r
-                                       ref_name = arg.Substring (pos). Trim (quotes);\r
+                                       string name = arg.Substring (pos). Trim (quotes);\r
+                                       ref_name = Location.LookupFile (name);\r
+                                       file_name.HasLineDirective = true;\r
+                                       ref_name.HasLineDirective = true;\r
+                                       Location.Push (ref_name);\r
                                } else {\r
                                        ref_line = System.Int32.Parse (arg);\r
                                }\r
@@ -1026,9 +1235,12 @@ namespace Mono.CSharp
                                return;\r
                        }\r
 \r
-                       foreach (char c in arg){\r
-                               if (!Char.IsLetter (c) && (c != '_')){\r
-                                       Report.Error (1001, Location, "Identifier expected");\r
+                       if (!is_identifier_start_character (arg [0]))\r
+                               Report.Error (1001, Location, "Identifier expected: " + arg);\r
+                       \r
+                       foreach (char c in arg.Substring (1)){\r
+                               if (!is_identifier_part_character (c)){\r
+                                       Report.Error (1001, Location, "Identifier expected: " + arg);\r
                                        return;\r
                                }\r
                        }\r
@@ -1143,7 +1355,7 @@ namespace Mono.CSharp
 \r
                                        return va != pp_unary (ref s);\r
 \r
-                               }\r
+                               } \r
                        }\r
 \r
                        return va;\r
@@ -1160,7 +1372,7 @@ namespace Mono.CSharp
                                if (s [0] == '&'){\r
                                        if (len > 2 && s [1] == '&'){\r
                                                s = s.Substring (2);\r
-                                               return va && pp_eq (ref s);\r
+                                               return (va & pp_eq (ref s));\r
                                        } else {\r
                                                Error_InvalidDirective ();\r
                                                return false;\r
@@ -1176,7 +1388,6 @@ namespace Mono.CSharp
                bool pp_expr (ref string s)\r
                {\r
                        bool va = pp_and (ref s);\r
-\r
                        s = s.Trim ();\r
                        int len = s.Length;\r
                        if (len > 0){\r
@@ -1185,14 +1396,14 @@ namespace Mono.CSharp
                                if (c == '|'){\r
                                        if (len > 2 && s [1] == '|'){\r
                                                s = s.Substring (2);\r
-                                               return va || pp_and (ref s);\r
+                                               return va | pp_expr (ref s);\r
                                        } else {\r
                                                Error_InvalidDirective ();\r
                                                return false;\r
                                        }\r
                                } \r
                        }\r
-\r
+                       \r
                        return va;\r
                }\r
 \r
@@ -1234,9 +1445,9 @@ namespace Mono.CSharp
                //\r
                bool handle_preprocessing_directive (bool caller_is_taking)\r
                {\r
-                       char [] blank = { ' ', '\t' };\r
                        string cmd, arg;\r
-                       \r
+                       bool region_directive = false;\r
+\r
                        get_cmd_arg (out cmd, out arg);\r
 \r
                        // Eat any trailing whitespaces and single-line comments\r
@@ -1248,6 +1459,11 @@ namespace Mono.CSharp
                        // The first group of pre-processing instructions is always processed\r
                        //\r
                        switch (cmd){\r
+                       case "pragma":\r
+                               if (RootContext.V2)\r
+                                       return true;\r
+                               break;\r
+                               \r
                        case "line":\r
                                if (!PreProcessLine (arg))\r
                                        Report.Error (\r
@@ -1256,10 +1472,12 @@ namespace Mono.CSharp
                                return true;\r
 \r
                        case "region":\r
+                               region_directive = true;\r
                                arg = "true";\r
                                goto case "if";\r
 \r
                        case "endregion":\r
+                               region_directive = true;\r
                                goto case "endif";\r
                                \r
                        case "if":\r
@@ -1278,12 +1496,18 @@ namespace Mono.CSharp
                                        if ((state & TAKING) != 0)\r
                                                taking = true;\r
                                }\r
-                                       \r
+\r
                                if (eval (arg) && taking){\r
-                                       ifstack.Push (TAKING | TAKEN_BEFORE | PARENT_TAKING);\r
+                                       int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;\r
+                                       if (region_directive)\r
+                                               push |= REGION;\r
+                                       ifstack.Push (push);\r
                                        return true;\r
                                } else {\r
-                                       ifstack.Push (taking ? PARENT_TAKING : 0);\r
+                                       int push = (taking ? PARENT_TAKING : 0);\r
+                                       if (region_directive)\r
+                                               push |= REGION;\r
+                                       ifstack.Push (push);\r
                                        return false;\r
                                }\r
                                \r
@@ -1292,7 +1516,13 @@ namespace Mono.CSharp
                                        Error_UnexpectedDirective ("no #if for this #endif");\r
                                        return true;\r
                                } else {\r
-                                       ifstack.Pop ();\r
+                                       int pop = (int) ifstack.Pop ();\r
+                                       \r
+                                       if (region_directive && ((pop & REGION) == 0))\r
+                                               Report.Error (1027, Location, "#endif directive expected");\r
+                                       else if (!region_directive && ((pop & REGION) != 0))\r
+                                               Report.Error (1038, Location, "#endregion directive expected");\r
+                                       \r
                                        if (ifstack.Count == 0)\r
                                                return true;\r
                                        else {\r
@@ -1312,6 +1542,11 @@ namespace Mono.CSharp
                                } else {\r
                                        int state = (int) ifstack.Peek ();\r
 \r
+                                       if ((state & REGION) != 0) {\r
+                                               Report.Error (1038, Location, "#endregion directive expected");\r
+                                               return true;\r
+                                       }\r
+\r
                                        if ((state & ELSE_SEEN) != 0){\r
                                                Error_UnexpectedDirective ("#elif not valid after #else");\r
                                                return true;\r
@@ -1337,21 +1572,32 @@ namespace Mono.CSharp
                                } else {\r
                                        int state = (int) ifstack.Peek ();\r
 \r
+                                       if ((state & REGION) != 0) {\r
+                                               Report.Error (1038, Location, "#endregion directive expected");\r
+                                               return true;\r
+                                       }\r
+\r
                                        if ((state & ELSE_SEEN) != 0){\r
                                                Error_UnexpectedDirective ("#else within #else");\r
                                                return true;\r
                                        }\r
 \r
                                        ifstack.Pop ();\r
-                                       ifstack.Push (state | ELSE_SEEN);\r
 \r
+                                       bool ret;\r
                                        if ((state & TAKEN_BEFORE) == 0){\r
-                                               if ((state & PARENT_TAKING) != 0)\r
-                                                       return true;\r
-                                               else\r
-                                                       return false;\r
-                                       }\r
-                                       return false;\r
+                                               ret = ((state & PARENT_TAKING) != 0);\r
+                                       } else\r
+                                               ret = false;\r
+                                       \r
+                                       if (ret)\r
+                                               state |= TAKING;\r
+                                       else\r
+                                               state &= ~TAKING;\r
+                                       \r
+                                       ifstack.Push (state | ELSE_SEEN);\r
+                                       \r
+                                       return ret;\r
                                }\r
                        }\r
 \r
@@ -1430,34 +1676,61 @@ namespace Mono.CSharp
                        return Token.EOF;\r
                }\r
 \r
-               private int consume_identifier (int c, bool quoted) \r
+               private int consume_identifier (int s, bool quoted) \r
                {\r
-                       id_builder.Length = 0;\r
-\r
-                       id_builder.Append ((char) c);\r
+                       int pos = 1;\r
+                       int c;\r
+                       \r
+                       id_builder [0] = (char) s;\r
                                        \r
-                       while ((c = peekChar ()) != -1) {\r
+                       while ((c = reader.Read ()) != -1) {\r
                                if (is_identifier_part_character ((char) c)){\r
-                                       id_builder.Append ((char)getChar ());\r
+                                       if (pos == max_id_size){\r
+                                               Report.Error (645, Location, "Identifier too long (limit is 512 chars)");\r
+                                               return Token.ERROR;\r
+                                       }\r
+                                       \r
+                                       id_builder [pos++] = (char) c;\r
+                                       putback_char = -1;\r
                                        col++;\r
-                               } else \r
+                               } else {\r
+                                       putback_char = c;\r
                                        break;\r
+                               }\r
                        }\r
-                                       \r
-                       string ids = id_builder.ToString ();\r
 \r
-                       if (!is_keyword (ids) || quoted) {\r
-                               val = ids;\r
-                               if (ids.Length > 512){\r
-                                       Report.Error (\r
-                                               645, Location,\r
-                                               "Identifier too long (limit is 512 chars)");\r
+                       //\r
+                       // Optimization: avoids doing the keyword lookup\r
+                       // on uppercase letters and _\r
+                       //\r
+                       if (s >= 'a' || s == '_'){\r
+                               int keyword = GetKeyword (id_builder, pos);\r
+                               if (keyword != -1 && !quoted)\r
+                                       return keyword;\r
+                       }\r
+\r
+                       //\r
+                       // Keep identifiers in an array of hashtables to avoid needless\r
+                       // allocations\r
+                       //\r
+\r
+                       if (identifiers [pos] != null) {\r
+                               val = identifiers [pos][id_builder];\r
+                               if (val != null) {\r
+                                       return Token.IDENTIFIER;\r
                                }\r
-                               return Token.IDENTIFIER;\r
                        }\r
+                       else\r
+                               identifiers [pos] = new CharArrayHashtable (pos);\r
+\r
+                       val = new String (id_builder, 0, pos);\r
 \r
-                       // true, false and null are in the hash anyway.\r
-                       return GetKeyword (ids);\r
+                       char [] chars = new char [pos];\r
+                       Array.Copy (id_builder, chars, pos);\r
+\r
+                       identifiers [pos] [chars] = val;\r
+\r
+                       return Token.IDENTIFIER;\r
                }\r
                \r
                public int xtoken ()\r
@@ -1469,21 +1742,27 @@ namespace Mono.CSharp
                        val = null;\r
                        // optimization: eliminate col and implement #directive semantic correctly.\r
                        for (;(c = getChar ()) != -1; col++) {\r
-                               if (is_identifier_start_character ((char)c)){\r
-                                       tokens_seen = true;\r
-                                       return consume_identifier (c, false);\r
-                               }\r
-\r
-                               if (c == '.'){\r
-                                       tokens_seen = true;\r
-                                       if (Char.IsDigit ((char) peekChar ()))\r
-                                               return is_number (c);\r
-                                       return Token.DOT;\r
+                               if (c == ' ')\r
+                                       continue;\r
+                               \r
+                               if (c == '\t') {\r
+                                       col = (((col + 8) / 8) * 8) - 1;\r
+                                       continue;\r
                                }\r
                                \r
-                               if (Char.IsDigit ((char) c)){\r
-                                       tokens_seen = true;\r
-                                       return is_number (c);\r
+                               if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0)\r
+                                       continue;\r
+\r
+                               if (c == '\r') {\r
+                                       if (peekChar () == '\n')\r
+                                               getChar ();\r
+\r
+                                       line++;\r
+                                       ref_line++;\r
+                                       col = 0;\r
+                                       any_token_seen |= tokens_seen;\r
+                                       tokens_seen = false;\r
+                                       continue;\r
                                }\r
 \r
                                // Handle double-slash comments.\r
@@ -1521,8 +1800,48 @@ namespace Mono.CSharp
                                                }\r
                                                continue;\r
                                        }\r
+                                       goto is_punct_label;\r
+                               }\r
+\r
+                               \r
+                               if (is_identifier_start_character ((char)c)){\r
+                                       tokens_seen = true;\r
+                                       return consume_identifier (c, false);\r
+                               }\r
+\r
+                       is_punct_label:\r
+                               if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){\r
+                                       tokens_seen = true;\r
+                                       if (doread){\r
+                                               getChar ();\r
+                                               col++;\r
+                                       }\r
+                                       return t;\r
+                               }\r
+\r
+                               // white space\r
+                               if (c == '\n'){\r
+                                       line++;\r
+                                       ref_line++;\r
+                                       col = 0;\r
+                                       any_token_seen |= tokens_seen;\r
+                                       tokens_seen = false;\r
+                                       continue;\r
                                }\r
 \r
+                               if (c >= '0' && c <= '9'){\r
+                                       tokens_seen = true;\r
+                                       return is_number (c);\r
+                               }\r
+\r
+                               if (c == '.'){\r
+                                       tokens_seen = true;\r
+                                       int peek = peekChar ();\r
+                                       if (peek >= '0' && peek <= '9')\r
+                                               return is_number (c);\r
+                                       return Token.DOT;\r
+                               }\r
+                               \r
                                /* For now, ignore pre-processor commands */\r
                                // FIXME: In C# the '#' is not limited to appear\r
                                // on the first column.\r
@@ -1546,7 +1865,7 @@ namespace Mono.CSharp
                                                        line++;\r
                                                        ref_line++;\r
                                                        skipping = false;\r
-                                               } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r')\r
+                                               } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)\r
                                                        continue;\r
                                                else if (c != '#')\r
                                                        skipping = true;\r
@@ -1560,18 +1879,8 @@ namespace Mono.CSharp
                                        continue;\r
                                }\r
                                \r
-                               if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){\r
-                                       tokens_seen = true;\r
-                                       if (doread){\r
-                                               getChar ();\r
-                                               col++;\r
-                                       }\r
-                                       return t;\r
-                               }\r
-                               \r
-                               if (c == '"') {\r
+                               if (c == '"') \r
                                        return consume_string (false);\r
-                               }\r
 \r
                                if (c == '\''){\r
                                        c = getChar ();\r
@@ -1608,22 +1917,6 @@ namespace Mono.CSharp
                                        return Token.LITERAL_CHARACTER;\r
                                }\r
                                \r
-                               // white space\r
-                               if (c == '\n'){\r
-                                       line++;\r
-                                       ref_line++;\r
-                                       col = 0;\r
-                                       any_token_seen |= tokens_seen;\r
-                                       tokens_seen = false;\r
-                                       continue;\r
-                               }\r
-\r
-                               if (c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == '\r'){\r
-                                       if (c == '\t')\r
-                                               col = (((col + 8) / 8) * 8) - 1;\r
-                                       continue;\r
-                               }\r
-\r
                                if (c == '@') {\r
                                        c = getChar ();\r
                                        if (c == '"') {\r
@@ -1636,16 +1929,34 @@ namespace Mono.CSharp
                                        }\r
                                }\r
 \r
+                               if (c == '#') {\r
+                                       error_details = "Preprocessor directives must appear as the first non-whitespace " +\r
+                                               "character on a line.";\r
+\r
+                                       Report.Error (1040, Location, error_details);\r
+\r
+                                       return Token.ERROR;\r
+                               }\r
+\r
                                error_details = ((char)c).ToString ();\r
                                \r
                                return Token.ERROR;\r
                        }\r
 \r
-                       if (ifstack != null && ifstack.Count >= 1)\r
-                               Report.Error (1027, Location, "#endif/#endregion expected");\r
-\r
                        return Token.EOF;\r
                }\r
+\r
+               public void cleanup ()\r
+               {\r
+                       if (ifstack != null && ifstack.Count >= 1) {\r
+                               int state = (int) ifstack.Pop ();\r
+                               if ((state & REGION) != 0)\r
+                                       Report.Error (1038, "#endregion directive expected");\r
+                               else \r
+                                       Report.Error (1027, "#endif directive expected");\r
+                       }\r
+                               \r
+               }\r
        }\r
 }\r
 \r