Fixes for compiling mPhoto:
[mono.git] / mcs / mcs / cs-tokenizer.cs
index f359d2b7426fb5743512e0cf8cdb3f7945b0b714..351edc585cff13156417ea6524866a325199227d 100755 (executable)
@@ -1,39 +1,18 @@
 //\r
 // cs-tokenizer.cs: The Tokenizer for the C# compiler\r
+//                  This also implements the preprocessor\r
 //\r
 // Author: Miguel de Icaza (miguel@gnu.org)\r
 //\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
-  Todo:\r
-\r
-  Do something with the integer and float suffixes, pass full datatype?\r
-  Make sure we accept the proper Unicode ranges, per the spec.\r
-\r
-  * Error reporting.\r
-\r
-          I was returning Token.ERROR on errors and setting an\r
-          internal error string with the details, but it might make sense\r
-         to just use exceptions.\r
-\r
-         Change of mind: I think I want to keep returning errors *UNLESS* the\r
-         parser is catching errors from the tokenizer (at that point, there is\r
-         not really any reason to use exceptions) so that I can continue the\r
-         parsing \r
-\r
-  * IDEA\r
-\r
-          I think I have solved the problem.  The idea is to not even *bother*\r
-         about handling data types a lot here (except for fitting data into\r
-         the proper places), but let the upper layer handle it.\r
-\r
-         Ie, treat LITERAL_CHARACTER, LITERAL_INTEGER, LITERAL_FLOAT, LITERAL_DOUBLE, and\r
-         return then as `LITERAL_LITERAL' with maybe subdetail information\r
-\r
+ * TODO:\r
+ *   Make sure we accept the proper Unicode ranges, per the spec.\r
+ *   Report error 1032\r
 */\r
 \r
 using System;\r
@@ -41,8 +20,9 @@ using System.Text;
 using System.Collections;\r
 using System.IO;\r
 using System.Globalization;\r
+using System.Reflection;\r
 \r
-namespace CIR\r
+namespace Mono.CSharp\r
 {\r
        /// <summary>\r
        ///    Tokenizer for C# source code. \r
@@ -51,13 +31,51 @@ namespace CIR
        public class Tokenizer : yyParser.yyInput\r
        {\r
                StreamReader reader;\r
-               public string ref_name;\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
                public int current_token;\r
                bool handle_get_set = false;\r
+               bool handle_remove_add = false;\r
+               bool handle_assembly = false;\r
 \r
+               //\r
+               // Whether tokens have been seen on this line\r
+               //\r
+               bool tokens_seen = false;\r
+\r
+               //\r
+               // Whether a token has been seen on the file\r
+               // This is needed because `define' is not allowed to be used\r
+               // after a token has been seen.\r
+               //\r
+               bool any_token_seen = false;\r
+               static Hashtable tokenValues;\r
+               \r
+               private static Hashtable TokenValueName\r
+               {\r
+                       get {\r
+                               if (tokenValues == null)\r
+                                       tokenValues = GetTokenValueNameHash ();\r
+\r
+                               return tokenValues;\r
+                       }\r
+               }\r
+\r
+               private static Hashtable GetTokenValueNameHash ()\r
+               {\r
+                       Type t = typeof (Token);\r
+                       FieldInfo [] fields = t.GetFields ();\r
+                       Hashtable hash = new Hashtable ();\r
+                       foreach (FieldInfo field in fields) {\r
+                               if (field.IsLiteral && field.IsStatic && field.FieldType == typeof (int))\r
+                                       hash.Add (field.GetValue (null), field.Name);\r
+                       }\r
+                       return hash;\r
+               }\r
+               \r
                //\r
                // Returns a verbose representation of the current location\r
                //\r
@@ -73,12 +91,19 @@ namespace CIR
                                // return "Line:     "+line+" Col: "+col + "\n" +\r
                                //       "VirtLine: "+ref_line +\r
                                //       " Token: "+current_token + " " + det;\r
-\r
-                               return ref_name + " " + "(" + line + "," + col + "), Token:" + current_token + " " + det;\r
+                               string current_token_name = TokenValueName [current_token] as string;\r
+                               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.Name,\r
+                                                                                      ref_line,\r
+                                                                                      col,\r
+                                                                                      current_token_name,\r
+                                                                                      det);\r
                        }\r
                }\r
 \r
-               public bool properties {\r
+               public bool PropertyParsing {\r
                        get {\r
                                return handle_get_set;\r
                        }\r
@@ -87,6 +112,26 @@ namespace CIR
                                handle_get_set = value;\r
                        }\r
                 }\r
+\r
+               public bool AssemblyTargetParsing {\r
+                       get {\r
+                               return handle_assembly;\r
+                       }\r
+\r
+                       set {\r
+                               handle_assembly = value;\r
+                       }\r
+               }\r
+\r
+               public bool EventParsing {\r
+                       get {\r
+                               return handle_remove_add;\r
+                       }\r
+\r
+                       set {\r
+                               handle_remove_add = value;\r
+                       }\r
+               }\r
                \r
                //\r
                // Class variables\r
@@ -98,9 +143,33 @@ namespace CIR
                //\r
                // Values for the associated token returned\r
                //\r
-               System.Text.StringBuilder number;\r
                int putback_char;\r
                Object val;\r
+\r
+               //\r
+               // Pre-processor\r
+               //\r
+               Hashtable defines;\r
+\r
+               const int TAKING        = 1;\r
+               const int TAKEN_BEFORE  = 2;\r
+               const int ELSE_SEEN     = 4;\r
+               const int PARENT_TAKING = 8;\r
+               const int REGION        = 16;           \r
+\r
+               //\r
+               // pre-processor if stack state:\r
+               //\r
+               Stack ifstack;\r
+\r
+               static System.Text.StringBuilder string_builder;\r
+\r
+               const int max_id_size = 512;\r
+               static char [] id_builder = new char [max_id_size];\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
@@ -115,7 +184,7 @@ namespace CIR
                \r
                public int Line {\r
                        get {\r
-                               return line;\r
+                               return ref_line;\r
                        }\r
                }\r
 \r
@@ -124,14 +193,15 @@ namespace CIR
                                return col;\r
                        }\r
                }\r
-               \r
-               static void initTokens ()\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
@@ -190,6 +260,7 @@ namespace CIR
                        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
@@ -207,7 +278,13 @@ namespace CIR
                        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
+\r
+                       if (RootContext.V2){\r
+                               keywords.Add ("__yield", Token.YIELD);\r
+                               keywords.Add ("yield", Token.YIELD);\r
+                       }\r
                }\r
 \r
                //\r
@@ -215,25 +292,30 @@ namespace CIR
                // \r
                static Tokenizer ()\r
                {\r
-                       initTokens ();\r
-                       csharp_format_info = new NumberFormatInfo ();\r
-                       csharp_format_info.CurrencyDecimalSeparator = ".";\r
-                       styles = NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint;\r
+                       InitTokens ();\r
+                       csharp_format_info = NumberFormatInfo.InvariantInfo;\r
+                       styles = NumberStyles.Float;\r
+                       \r
+                       string_builder = new System.Text.StringBuilder ();\r
                }\r
 \r
-               bool is_keyword (string name)\r
+               int GetKeyword (string name)\r
                {\r
-                       bool res;\r
+                       object o = keywords [name];\r
+\r
+                       if (o == null)\r
+                               return -1;\r
                        \r
-                       res = keywords.Contains (name);\r
-                       if ((name == "get" || name == "set") && handle_get_set == false)\r
-                               return false;\r
-                       return res;\r
-               }\r
+                       int res = (int) o;\r
 \r
-               int getKeyword (string name)\r
-               {\r
-                       return (int) (keywords [name]);\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
 \r
                public Location Location {\r
@@ -241,29 +323,50 @@ namespace CIR
                                return new Location (ref_line);\r
                        }\r
                }\r
+\r
+               void define (string def)\r
+               {\r
+                       if (!RootContext.AllDefines.Contains (def)){\r
+                               RootContext.AllDefines [def] = true;\r
+                       }\r
+                       if (defines.Contains (def))\r
+                               return;\r
+                       defines [def] = true;\r
+               }\r
                \r
-               public Tokenizer (System.IO.Stream input, string fname)\r
+               public Tokenizer (StreamReader input, SourceFile file, ArrayList defs)\r
                {\r
-                       this.ref_name = fname;\r
-                       reader = new System.IO.StreamReader (input);\r
+                       this.ref_name = file;\r
+                       this.file_name = file;\r
+                       reader = input;\r
+                       \r
                        putback_char = -1;\r
 \r
-                       Location.Push (fname);\r
+                       if (defs != null){\r
+                               defines = new Hashtable ();\r
+                               foreach (string def in defs)\r
+                                       define (def);\r
+                       }\r
+\r
+                       //\r
+                       // 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 (file);\r
                }\r
 \r
                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
                {\r
-                       return (Char.IsLetter (c) || Char.IsDigit (c) || c == '_');\r
+                       return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);\r
                }\r
 \r
                int is_punct (char c, ref bool doread)\r
                {\r
-                       int idx = "{}[](),:;~+-*/%&|^!=<>?".IndexOf (c);\r
                        int d;\r
                        int t;\r
 \r
@@ -312,7 +415,7 @@ namespace CIR
                                else if (d == '=')\r
                                        t = Token.OP_SUB_ASSIGN;\r
                                else if (d == '>')\r
-                                       return Token.OP_PTR;\r
+                                       t = Token.OP_PTR;\r
                                else\r
                                        return Token.MINUS;\r
                                doread = true;\r
@@ -425,47 +528,56 @@ namespace CIR
                        return Token.ERROR;\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.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.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
                                        break;\r
                        }\r
+                       \r
                        return seen_digits;\r
                }\r
 \r
+               bool is_hex (int e)\r
+               {\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.Append ((char) c);\r
-                       while ((d = peekChar ()) != -1){\r
-                               char e = Char.ToUpper ((char) d);\r
-                               \r
-                               if (Char.IsDigit (e) ||\r
-                                   (e >= 'A' && e <= 'F')){\r
-                                       number.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
                {\r
                        int t;\r
-                       \r
+\r
                        switch (c){\r
                        case 'F': case 'f':\r
                                t =  Token.LITERAL_FLOAT;\r
@@ -479,90 +591,200 @@ namespace CIR
                        default:\r
                                return Token.NONE;\r
                        }\r
-                       //getChar ();\r
                        return t;\r
                }\r
 \r
-               int integer_type_suffix (int c)\r
+               int integer_type_suffix (ulong ul, int c)\r
                {\r
-                       // FIXME: Handle U and L suffixes.\r
-                       // We also need to see in which kind of\r
-                       // Int the thing fits better according to the spec.\r
+                       bool is_unsigned = false;\r
+                       bool is_long = false;\r
+\r
+                       if (c != -1){\r
+                               bool scanning = true;\r
+                               do {\r
+                                       switch (c){\r
+                                       case 'U': case 'u':\r
+                                               if (is_unsigned)\r
+                                                       scanning = false;\r
+                                               is_unsigned = true;\r
+                                               getChar ();\r
+                                               break;\r
+\r
+                                       case 'l':\r
+                                               if (!is_unsigned){\r
+                                                       //\r
+                                                       // if we have not seen anything in between\r
+                                                       // report this error\r
+                                                       //\r
+                                                       Report.Warning (\r
+                                                               78, Location,\r
+                                                       "the 'l' suffix is easily confused with digit `1'," +\r
+                                                       " use 'L' for clarity");\r
+                                               }\r
+                                               goto case 'L';\r
+                                               \r
+                                       case 'L': \r
+                                               if (is_long)\r
+                                                       scanning = false;\r
+                                               is_long = true;\r
+                                               getChar ();\r
+                                               break;\r
+                                               \r
+                                       default:\r
+                                               scanning = false;\r
+                                               break;\r
+                                       }\r
+                                       c = peekChar ();\r
+                               } while (scanning);\r
+                       }\r
+\r
+                       if (is_long && is_unsigned){\r
+                               val = ul;\r
+                               return Token.LITERAL_INTEGER;\r
+                       } else if (is_unsigned){\r
+                               // uint if possible, or ulong else.\r
+\r
+                               if ((ul & 0xffffffff00000000) == 0)\r
+                                       val = (uint) ul;\r
+                               else\r
+                                       val = ul;\r
+                       } else if (is_long){\r
+                               // long if possible, ulong otherwise\r
+                               if ((ul & 0x8000000000000000) != 0)\r
+                                       val = ul;\r
+                               else\r
+                                       val = (long) ul;\r
+                       } else {\r
+                               // int, uint, long or ulong in that order\r
+                               if ((ul & 0xffffffff00000000) == 0){\r
+                                       uint ui = (uint) ul;\r
+                                       \r
+                                       if ((ui & 0x80000000) != 0)\r
+                                               val = ui;\r
+                                       else\r
+                                               val = (int) ui;\r
+                               } else {\r
+                                       if ((ul & 0x8000000000000000) != 0)\r
+                                               val = ul;\r
+                                       else\r
+                                               val = (long) ul;\r
+                               }\r
+                       }\r
                        return Token.LITERAL_INTEGER;\r
                }\r
-               \r
-               void adjust_int (int t)\r
+                               \r
+               //\r
+               // given `c' as the next char in the input decide whether\r
+               // we need to convert to a special type, and then choose\r
+               // the best representation for the integer\r
+               //\r
+               int adjust_int (int c)\r
                {\r
-                       val = new System.Int32();\r
-                       val = System.Int32.Parse (number.ToString (), 0);\r
-               }\r
+                       try {\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
+                               val = 0ul;\r
+                               return Token.LITERAL_INTEGER;\r
+                       }\r
+               }\r
+               \r
                int adjust_real (int t)\r
                {\r
-                       string s = number.ToString ();\r
+                       string s = new String (number_builder, 0, number_pos);\r
 \r
                        switch (t){\r
                        case Token.LITERAL_DECIMAL:\r
-                               val = new System.Decimal ();\r
-                               val = System.Decimal.Parse (\r
-                                       s, styles, csharp_format_info);\r
-                               break;\r
-                       case Token.LITERAL_DOUBLE:\r
-                               val = new System.Double ();\r
-                               val = System.Double.Parse (\r
-                                       s, styles, csharp_format_info);\r
+                               try {\r
+                                       val = System.Decimal.Parse (s, styles, csharp_format_info);\r
+                               } catch (OverflowException) {\r
+                                       val = 0m;     \r
+                                       error_details = "Floating-point constant is outside the range of the type 'decimal'";\r
+                                       Report.Error (594, Location, error_details);\r
+                               }\r
                                break;\r
                        case Token.LITERAL_FLOAT:\r
-                               val = new System.Double ();\r
-                               val = (float) System.Double.Parse (\r
-                                       s, styles, csharp_format_info);\r
+                               try {\r
+                                       val = (float) System.Double.Parse (s, styles, csharp_format_info);\r
+                               } catch (OverflowException) {\r
+                                       val = 0.0f;     \r
+                                       error_details = "Floating-point constant is outside the range of the type 'float'";\r
+                                       Report.Error (594, Location, error_details);\r
+                               }\r
                                break;\r
-\r
+                               \r
+                       case Token.LITERAL_DOUBLE:\r
                        case Token.NONE:\r
-                               val = new System.Double ();\r
-                               val = System.Double.Parse (\r
-                                       s, styles, csharp_format_info);\r
                                t = Token.LITERAL_DOUBLE;\r
+                               try {\r
+                                       val = System.Double.Parse (s, styles, csharp_format_info);\r
+                               } catch (OverflowException) {\r
+                                       val = 0.0;     \r
+                                       error_details = "Floating-point constant is outside the range of the type 'double'";\r
+                                       Report.Error (594, Location, error_details);\r
+                               }\r
                                break;\r
                        }\r
                        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
+                                       if (number_pos == 16){\r
+                                               Report.Error (1021, Location, "Integral constant too large");\r
+                                               return Token.ERROR;\r
+                                       }\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
+                       if (number_pos <= 8)\r
+                               ul = System.UInt32.Parse (s, NumberStyles.HexNumber);\r
+                       else\r
+                               ul = System.UInt64.Parse (s, NumberStyles.HexNumber);\r
+                       return integer_type_suffix (ul, peekChar ());\r
+               }\r
+\r
                //\r
                // Invoked if we know we have .digits or digits\r
                //\r
                int is_number (int c)\r
                {\r
                        bool is_real = false;\r
-                       number = new System.Text.StringBuilder ();\r
                        int type;\r
 \r
-                       number.Length = 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
+                       number_pos = 0;\r
 \r
-                                       string s = number.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
-                                       if ((ul & 0xffffffff00000000) == 0){\r
-                                               uint ui = (uint) ul;\r
-                                               \r
-                                               if ((ui & 0x80000000) != 0)\r
-                                                       val = ui;\r
-                                               else\r
-                                                       val = (int) ui;\r
-                                       } else {\r
-                                               if ((ul & 0x8000000000000000) != 0)\r
-                                                       val = ul;\r
-                                               else\r
-                                                       val = (long) ul;\r
-                                       }\r
-\r
-                                       return integer_type_suffix (peekChar ());\r
+                                       if (peek == 'x' || peek == 'X')\r
+                                               return handle_hex ();\r
                                }\r
                                decimal_digits (c);\r
                                c = getChar ();\r
@@ -575,52 +797,101 @@ namespace CIR
                        if (c == '.'){\r
                                if (decimal_digits ('.')){\r
                                        is_real = true;\r
-                                       c = peekChar ();\r
+                                       c = getChar ();\r
                                } else {\r
                                        putback ('.');\r
-                                       number.Length -= 1;\r
-                                       adjust_int (Token.LITERAL_INTEGER);\r
-                                       return Token.LITERAL_INTEGER;\r
+                                       number_pos--;\r
+                                       return adjust_int (-1);\r
                                }\r
                        }\r
                        \r
                        if (c == 'e' || c == 'E'){\r
                                is_real = true;\r
-                               number.Append ("e");\r
-                               getChar ();\r
+                               if (number_pos == max_number_size)\r
+                                       Error_NumericConstantTooLong ();\r
+                               number_builder [number_pos++] = 'e';\r
+                               c = getChar ();\r
                                \r
-                               c = peekChar ();\r
                                if (c == '+'){\r
-                                       number.Append ((char) c);\r
-                                       getChar ();\r
-                                       c = peekChar ();\r
-                               } else if (c == '-'){\r
-                                       number.Append ((char) c);\r
-                                       getChar ();\r
-                                       c = peekChar ();\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
-                               decimal_digits (-1);\r
-                               c = peekChar ();\r
+                                       \r
+                               decimal_digits (c);\r
+                               c = getChar ();\r
                        }\r
 \r
                        type = real_type_suffix (c);\r
                        if (type == Token.NONE && !is_real){\r
-                               type = integer_type_suffix (c);\r
-                               adjust_int (type);\r
                                putback (c);\r
-                               return type;\r
-                       } else\r
+                               return adjust_int (c);\r
+                       } else \r
                                is_real = true;\r
 \r
+                       if (type == Token.NONE){\r
+                               putback (c);\r
+                       }\r
+                       \r
                        if (is_real)\r
                                return adjust_real (type);\r
 \r
                        Console.WriteLine ("This should not be reached");\r
                        throw new Exception ("Is Number should never reach this point");\r
                }\r
+\r
+               //\r
+               // Accepts exactly count (4 or 8) hex, no more no less\r
+               //\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
+                       int top = count != -1 ? count : 4;\r
                        \r
+                       getChar ();\r
+                       error = false;\r
+                       for (i = 0; i < top; i++){\r
+                               c = getChar ();\r
+                               \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
+                               \r
+                               total = (total * 16) + c;\r
+                               if (count == -1){\r
+                                       int p = peekChar ();\r
+                                       if (p == -1)\r
+                                               break;\r
+                                       if (!is_hex ((char)p))\r
+                                               break;\r
+                               }\r
+                       }\r
+                       return total;\r
+               }\r
+\r
                int escape (int c)\r
                {\r
+                       bool error;\r
                        int d;\r
                        int v;\r
 \r
@@ -640,7 +911,7 @@ namespace CIR
                        case 'v':\r
                                v = '\v'; break;\r
                        case 'r':\r
-                               v = 'c'; break;\r
+                               v = '\r'; break;\r
                        case '\\':\r
                                v = '\\'; break;\r
                        case 'f':\r
@@ -651,9 +922,24 @@ namespace CIR
                                v = '"'; break;\r
                        case '\'':\r
                                v = '\''; break;\r
+                       case 'x':\r
+                               v = getHex (-1, out error);\r
+                               if (error)\r
+                                       goto default;\r
+                               return v;\r
+                       case 'u':\r
+                               v = getHex (4, out error);\r
+                               if (error)\r
+                                       goto default;\r
+                               return v;\r
+                       case 'U':\r
+                               v = getHex (8, out error);\r
+                               if (error)\r
+                                       goto default;\r
+                               return v;\r
                        default:\r
-                               error_details = "cs1009: Unrecognized escape sequence " + (char)d;\r
-                               return -1;\r
+                               Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);\r
+                               return d;\r
                        }\r
                        getChar ();\r
                        return v;\r
@@ -674,13 +960,26 @@ namespace CIR
                {\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
@@ -705,51 +1004,597 @@ namespace CIR
                        current_token = xtoken ();\r
                        return current_token;\r
                }\r
+\r
+               static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();\r
                \r
-               public int xtoken ()\r
+               void get_cmd_arg (out string cmd, out string arg)\r
                {\r
-                       int t;\r
-                       bool allow_keyword_as_ident = false;\r
-                       bool doread = false;\r
                        int c;\r
+                       \r
+                       tokens_seen = false;\r
+                       arg = "";\r
+                       static_cmd_arg.Length = 0;\r
+                               \r
+                       while ((c = getChar ()) != -1 && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){\r
+                               static_cmd_arg.Append ((char) c);\r
+                       }\r
 \r
-                       val = null;\r
-                       for (;(c = getChar ()) != -1; col++) {\r
+                       cmd = static_cmd_arg.ToString ();\r
+\r
+                       if (c == '\n'){\r
+                               line++;\r
+                               ref_line++;\r
+                               return;\r
+                       } else if (c == '\r')\r
+                               col = 0;\r
+\r
+                       // skip over white space\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
-                               if (is_identifier_start_character ((char) c)){\r
-                                       System.Text.StringBuilder id = new System.Text.StringBuilder ();\r
-                                       string ids;\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
+                       } else if (c == '\r')\r
+                               col = 0;\r
+                       arg = static_cmd_arg.ToString ().Trim ();\r
+               }\r
+\r
+               //\r
+               // Handles the #line directive\r
+               //\r
+               bool PreProcessLine (string arg)\r
+               {\r
+                       if (arg == "")\r
+                               return false;\r
+\r
+                       if (arg == "default"){\r
+                               ref_line = line;\r
+                               ref_name = file_name;\r
+                               Location.Push (ref_name);\r
+                               return true;\r
+                       }\r
+                       \r
+                       try {\r
+                               int pos;\r
+\r
+                               if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){\r
+                                       ref_line = System.Int32.Parse (arg.Substring (0, pos));\r
+                                       pos++;\r
                                        \r
-                                       id.Append ((char) c);\r
+                                       char [] quotes = { '\"' };\r
                                        \r
-                                       while ((c = peekChar ()) != -1) {\r
-                                               if (is_identifier_part_character ((char) c)){\r
-                                                       id.Append ((char)getChar ());\r
-                                                       col++;\r
-                                               } else \r
-                                                       break;\r
+                                       string name = arg.Substring (pos). Trim (quotes);\r
+                                       ref_name = Location.LookupFile (name);\r
+                                       Location.Push (ref_name);\r
+                               } else {\r
+                                       ref_line = System.Int32.Parse (arg);\r
+                               }\r
+                       } catch {\r
+                               return false;\r
+                       }\r
+                       \r
+                       return true;\r
+               }\r
+\r
+               //\r
+               // Handles #define and #undef\r
+               //\r
+               void PreProcessDefinition (bool is_define, string arg)\r
+               {\r
+                       if (arg == "" || arg == "true" || arg == "false"){\r
+                               Report.Error (1001, Location, "Missing identifer to pre-processor directive");\r
+                               return;\r
+                       }\r
+\r
+                       char[] whitespace = { ' ', '\t' };\r
+                       if (arg.IndexOfAny (whitespace) != -1){\r
+                               Report.Error (1025, Location, "Single-line comment or end-of-line expected");\r
+                               return;\r
+                       }\r
+\r
+                       foreach (char c in arg){\r
+                               if (!Char.IsLetter (c) && (c != '_')){\r
+                                       Report.Error (1001, Location, "Identifier expected");\r
+                                       return;\r
+                               }\r
+                       }\r
+\r
+                       if (is_define){\r
+                               if (defines == null)\r
+                                       defines = new Hashtable ();\r
+                               define (arg);\r
+                       } else {\r
+                               if (defines == null)\r
+                                       return;\r
+                               if (defines.Contains (arg))\r
+                                       defines.Remove (arg);\r
+                       }\r
+               }\r
+\r
+               bool eval_val (string s)\r
+               {\r
+                       if (s == "true")\r
+                               return true;\r
+                       if (s == "false")\r
+                               return false;\r
+                       \r
+                       if (defines == null)\r
+                               return false;\r
+                       if (defines.Contains (s))\r
+                               return true;\r
+\r
+                       return false;\r
+               }\r
+\r
+               bool pp_primary (ref string s)\r
+               {\r
+                       s = s.Trim ();\r
+                       int len = s.Length;\r
+\r
+                       if (len > 0){\r
+                               char c = s [0];\r
+                               \r
+                               if (c == '('){\r
+                                       s = s.Substring (1);\r
+                                       bool val = pp_expr (ref s);\r
+                                       if (s.Length > 0 && s [0] == ')'){\r
+                                               s = s.Substring (1);\r
+                                               return val;\r
                                        }\r
-                                       \r
-                                       ids = id.ToString ();\r
+                                       Error_InvalidDirective ();\r
+                                       return false;\r
+                               }\r
+                               \r
+                               if (is_identifier_start_character (c)){\r
+                                       int j = 1;\r
 \r
-                                       if (!is_keyword (ids) || allow_keyword_as_ident) {\r
-                                               val = ids;\r
-                                               return Token.IDENTIFIER;\r
+                                       while (j < len){\r
+                                               c = s [j];\r
+                                               \r
+                                               if (is_identifier_part_character (c)){\r
+                                                       j++;\r
+                                                       continue;\r
+                                               }\r
+                                               bool v = eval_val (s.Substring (0, j));\r
+                                               s = s.Substring (j);\r
+                                               return v;\r
+                                       }\r
+                                       bool vv = eval_val (s);\r
+                                       s = "";\r
+                                       return vv;\r
+                               }\r
+                       }\r
+                       Error_InvalidDirective ();\r
+                       return false;\r
+               }\r
+               \r
+               bool pp_unary (ref string s)\r
+               {\r
+                       s = s.Trim ();\r
+                       int len = s.Length;\r
+\r
+                       if (len > 0){\r
+                               if (s [0] == '!'){\r
+                                       if (len > 1 && s [1] == '='){\r
+                                               Error_InvalidDirective ();\r
+                                               return false;\r
+                                       }\r
+                                       s = s.Substring (1);\r
+                                       return ! pp_primary (ref s);\r
+                               } else\r
+                                       return pp_primary (ref s);\r
+                       } else {\r
+                               Error_InvalidDirective ();\r
+                               return false;\r
+                       }\r
+               }\r
+               \r
+               bool pp_eq (ref string s)\r
+               {\r
+                       bool va = pp_unary (ref s);\r
+\r
+                       s = s.Trim ();\r
+                       int len = s.Length;\r
+                       if (len > 0){\r
+                               if (s [0] == '='){\r
+                                       if (len > 2 && s [1] == '='){\r
+                                               s = s.Substring (2);\r
+                                               return va == pp_unary (ref s);\r
+                                       } else {\r
+                                               Error_InvalidDirective ();\r
+                                               return false;\r
+                                       }\r
+                               } else if (s [0] == '!' && len > 1 && s [1] == '='){\r
+                                       s = s.Substring (2);\r
+\r
+                                       return va != pp_unary (ref s);\r
+\r
+                               } \r
+                       }\r
+\r
+                       return va;\r
+                               \r
+               }\r
+               \r
+               bool pp_and (ref string s)\r
+               {\r
+                       bool va = pp_eq (ref s);\r
+\r
+                       s = s.Trim ();\r
+                       int len = s.Length;\r
+                       if (len > 0){\r
+                               if (s [0] == '&'){\r
+                                       if (len > 2 && s [1] == '&'){\r
+                                               s = s.Substring (2);\r
+                                               return (va & pp_eq (ref s));\r
+                                       } else {\r
+                                               Error_InvalidDirective ();\r
+                                               return false;\r
+                                       }\r
+                               } \r
+                       }\r
+                       return va;\r
+               }\r
+               \r
+               //\r
+               // Evaluates an expression for `#if' or `#elif'\r
+               //\r
+               bool pp_expr (ref string s)\r
+               {\r
+                       bool va = pp_and (ref s);\r
+                       s = s.Trim ();\r
+                       int len = s.Length;\r
+                       if (len > 0){\r
+                               char c = s [0];\r
+                               \r
+                               if (c == '|'){\r
+                                       if (len > 2 && s [1] == '|'){\r
+                                               s = s.Substring (2);\r
+                                               return va | pp_expr (ref s);\r
+                                       } else {\r
+                                               Error_InvalidDirective ();\r
+                                               return false;\r
                                        }\r
+                               } \r
+                       }\r
+                       \r
+                       return va;\r
+               }\r
 \r
-                                       // true, false and null are in the hash anyway.\r
-                                       return getKeyword (ids);\r
+               bool eval (string s)\r
+               {\r
+                       bool v = pp_expr (ref s);\r
+                       s = s.Trim ();\r
+                       if (s.Length != 0){\r
+                               Error_InvalidDirective ();\r
+                               return false;\r
+                       }\r
+\r
+                       return v;\r
+               }\r
+               \r
+               void Error_InvalidDirective ()\r
+               {\r
+                       Report.Error (1517, Location, "Invalid pre-processor directive");\r
+               }\r
 \r
+               void Error_UnexpectedDirective (string extra)\r
+               {\r
+                       Report.Error (\r
+                               1028, Location,\r
+                               "Unexpected processor directive (" + extra + ")");\r
+               }\r
+\r
+               void Error_TokensSeen ()\r
+               {\r
+                       Report.Error (\r
+                               1032, Location,\r
+                               "Cannot define or undefine pre-processor symbols after a token in the file");\r
+               }\r
+               \r
+               //\r
+               // if true, then the code continues processing the code\r
+               // if false, the code stays in a loop until another directive is\r
+               // reached.\r
+               //\r
+               bool handle_preprocessing_directive (bool caller_is_taking)\r
+               {\r
+                       char [] blank = { ' ', '\t' };\r
+                       string cmd, arg;\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
+                       if (arg.IndexOf ("//") != -1)\r
+                               arg = arg.Substring (0, arg.IndexOf ("//"));\r
+                       arg = arg.TrimEnd (' ', '\t');\r
+\r
+                       //\r
+                       // The first group of pre-processing instructions is always processed\r
+                       //\r
+                       switch (cmd){\r
+                       case "line":\r
+                               if (!PreProcessLine (arg))\r
+                                       Report.Error (\r
+                                               1576, Location,\r
+                                               "Argument to #line directive is missing or invalid");\r
+                               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
+                               if (arg == ""){\r
+                                       Error_InvalidDirective ();\r
+                                       return true;\r
                                }\r
+                               bool taking = false;\r
+                               if (ifstack == null)\r
+                                       ifstack = new Stack ();\r
 \r
-                               if (c == '.'){\r
-                                       if (Char.IsDigit ((char) peekChar ()))\r
-                                               return is_number (c);\r
-                                       return Token.DOT;\r
+                               if (ifstack.Count == 0){\r
+                                       taking = true;\r
+                               } else {\r
+                                       int state = (int) ifstack.Peek ();\r
+                                       if ((state & TAKING) != 0)\r
+                                               taking = true;\r
+                               }\r
+\r
+                               if (eval (arg) && 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
+                                       int push = (taking ? PARENT_TAKING : 0);\r
+                                       if (region_directive)\r
+                                               push |= REGION;\r
+                                       ifstack.Push (push);\r
+                                       return false;\r
                                }\r
                                \r
-                               if (Char.IsDigit ((char) c))\r
-                                       return is_number (c);\r
+                       case "endif":\r
+                               if (ifstack == null || ifstack.Count == 0){\r
+                                       Error_UnexpectedDirective ("no #if for this #endif");\r
+                                       return true;\r
+                               } else {\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
+                                               int state = (int) ifstack.Peek ();\r
+\r
+                                               if ((state & TAKING) != 0)\r
+                                                       return true;\r
+                                               else\r
+                                                       return false;\r
+                                       }\r
+                               }\r
+\r
+                       case "elif":\r
+                               if (ifstack == null || ifstack.Count == 0){\r
+                                       Error_UnexpectedDirective ("no #if for this #elif");\r
+                                       return true;\r
+                               } 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
+                                       }\r
+\r
+                                       if ((state & (TAKEN_BEFORE | TAKING)) != 0)\r
+                                               return false;\r
+\r
+                                       if (eval (arg) && ((state & PARENT_TAKING) != 0)){\r
+                                               state = (int) ifstack.Pop ();\r
+                                               ifstack.Push (state | TAKING | TAKEN_BEFORE);\r
+                                               return true;\r
+                                       } else \r
+                                               return false;\r
+                               }\r
+\r
+                       case "else":\r
+                               if (ifstack == null || ifstack.Count == 0){\r
+                                       Report.Error (\r
+                                               1028, Location,\r
+                                               "Unexpected processor directive (no #if for this #else)");\r
+                                       return true;\r
+                               } 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
+\r
+                                       bool ret;\r
+                                       if ((state & TAKEN_BEFORE) == 0){\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
+                       //\r
+                       // These are only processed if we are in a `taking' block\r
+                       //\r
+                       if (!caller_is_taking)\r
+                               return false;\r
+                                       \r
+                       switch (cmd){\r
+                       case "define":\r
+                               if (any_token_seen){\r
+                                       Error_TokensSeen ();\r
+                                       return true;\r
+                               }\r
+                               PreProcessDefinition (true, arg);\r
+                               return true;\r
+\r
+                       case "undef":\r
+                               if (any_token_seen){\r
+                                       Error_TokensSeen ();\r
+                                       return true;\r
+                               }\r
+                               PreProcessDefinition (false, arg);\r
+                               return true;\r
+\r
+                       case "error":\r
+                               Report.Error (1029, Location, "#error: '" + arg + "'");\r
+                               return true;\r
+\r
+                       case "warning":\r
+                               Report.Warning (1030, Location, "#warning: '" + arg + "'");\r
+                               return true;\r
+                       }\r
+\r
+                       Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");\r
+                       return true;\r
+\r
+               }\r
+\r
+               private int consume_string (bool quoted) \r
+               {\r
+                       int c;\r
+                       string_builder.Length = 0;\r
+                                                               \r
+                       while ((c = getChar ()) != -1){\r
+                               if (c == '"'){\r
+                                       if (quoted && peekChar () == '"'){\r
+                                               string_builder.Append ((char) c);\r
+                                               getChar ();\r
+                                               continue;\r
+                                       } else {\r
+                                               val = string_builder.ToString ();\r
+                                               return Token.LITERAL_STRING;\r
+                                       }\r
+                               }\r
+\r
+                               if (c == '\n'){\r
+                                       if (!quoted)\r
+                                               Report.Error (1010, Location, "Newline in constant");\r
+                                       line++;\r
+                                       ref_line++;\r
+                                       col = 0;\r
+                               } else\r
+                                       col++;\r
+\r
+                               if (!quoted){\r
+                                       c = escape (c);\r
+                                       if (c == -1)\r
+                                               return Token.ERROR;\r
+                               }\r
+                               string_builder.Append ((char) c);\r
+                       }\r
+\r
+                       Report.Error (1039, Location, "Unterminated string literal");\r
+                       return Token.EOF;\r
+               }\r
+\r
+               private int consume_identifier (int s, bool quoted) \r
+               {\r
+                       int pos = 1;\r
+                       int c;\r
+                       \r
+                       id_builder [0] = (char) s;\r
+                                       \r
+                       while ((c = reader.Read ()) != -1) {\r
+                               if (is_identifier_part_character ((char) c)){\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
+                                       putback_char = c;\r
+                                       break;\r
+                               }\r
+                       }\r
+\r
+                       string ids = new String (id_builder, 0, pos);\r
+\r
+                       if (s >= 'a'){\r
+                               int keyword = GetKeyword (ids);\r
+                               if (keyword == -1 || quoted){\r
+                                       val = ids;\r
+                                       return Token.IDENTIFIER;\r
+                               }\r
+                               return keyword;\r
+                       }\r
+                       val = ids;\r
+                       return Token.IDENTIFIER;\r
+               }\r
+               \r
+               public int xtoken ()\r
+               {\r
+                       int t;\r
+                       bool doread = false;\r
+                       int c;\r
+\r
+                       val = null;\r
+                       // optimization: eliminate col and implement #directive semantic correctly.\r
+                       for (;(c = getChar ()) != -1; col++) {\r
+                               if (c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == '\r' || c == 0xa0){\r
+                                       \r
+                                       if (c == '\t')\r
+                                               col = (((col + 8) / 8) * 8) - 1;\r
+                                       continue;\r
+                               }\r
 \r
                                // Handle double-slash comments.\r
                                if (c == '/'){\r
@@ -757,10 +1602,15 @@ namespace CIR
                                \r
                                        if (d == '/'){\r
                                                getChar ();\r
-                                               while ((d = getChar ()) != -1 && (d != '\n'))\r
+                                               while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')\r
                                                        col++;\r
-                                               line++;\r
-                                               ref_line++;\r
+                                               if (d == '\n'){\r
+                                                       line++;\r
+                                                       ref_line++;\r
+                                                       col = 0;\r
+                                               }\r
+                                               any_token_seen |= tokens_seen;\r
+                                               tokens_seen = false;\r
                                                continue;\r
                                        } else if (d == '*'){\r
                                                getChar ();\r
@@ -774,69 +1624,101 @@ namespace CIR
                                                        if (d == '\n'){\r
                                                                line++;\r
                                                                ref_line++;\r
+                                                               col = 0;\r
+                                                               any_token_seen |= tokens_seen;\r
+                                                               tokens_seen = false;\r
                                                        }\r
-                                                       col++;\r
                                                }\r
                                                continue;\r
                                        }\r
+                                       goto is_punct_label;\r
                                }\r
 \r
-                               /* For now, ignore pre-processor commands */\r
-                               if (col == 1 && c == '#'){\r
-                                       System.Text.StringBuilder s = new System.Text.StringBuilder ();\r
-                                       \r
-                                       while ((c = getChar ()) != -1 && (c != '\n')){\r
-                                               s.Append ((char) c);\r
-                                       }\r
-                                       if (String.Compare (s.ToString (), 0, "line", 0, 4) == 0){\r
-                                               string arg = s.ToString ().Substring (5);\r
-                                               int pos;\r
-\r
-                                               if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){\r
-                                                       ref_line = System.Int32.Parse (arg.Substring (0, pos));\r
-                                                       pos++;\r
-\r
-                                                       char [] quotes = { '\"' };\r
-\r
-                                                       ref_name = arg.Substring (pos);\r
-                                                       ref_name.TrimStart (quotes);\r
-                                                       ref_name.TrimEnd (quotes);\r
-                                               } else\r
-                                                       ref_line = System.Int32.Parse (arg);\r
-                                       }\r
-                                       line++;\r
-                                       ref_line++;\r
-                                       continue;\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
-                               if (c == '"'){\r
-                                       System.Text.StringBuilder s = new System.Text.StringBuilder ();\r
 \r
-                                       while ((c = getChar ()) != -1){\r
-                                               if (c == '"'){\r
-                                                       val = s.ToString ();\r
-                                                       return Token.LITERAL_STRING;\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
-                                               c = escape (c);\r
-                                               if (c == -1)\r
-                                                       return Token.ERROR;\r
-                                               s.Append ((char) c);\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
+                               if (c == '#' && !tokens_seen){\r
+                                       bool cont = true;\r
+                                       \r
+                               start_again:\r
+                                       \r
+                                       cont = handle_preprocessing_directive (cont);\r
+\r
+                                       if (cont){\r
+                                               col = 0;\r
+                                               continue;\r
+                                       }\r
+                                       col = 1;\r
+\r
+                                       bool skipping = false;\r
+                                       for (;(c = getChar ()) != -1; col++){\r
+                                               if (c == '\n'){\r
+                                                       col = 0;\r
+                                                       line++;\r
+                                                       ref_line++;\r
+                                                       skipping = false;\r
+                                               } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)\r
+                                                       continue;\r
+                                               else if (c != '#')\r
+                                                       skipping = true;\r
+                                               if (c == '#' && !skipping)\r
+                                                       goto start_again;\r
                                        }\r
+                                       any_token_seen |= tokens_seen;\r
+                                       tokens_seen = false;\r
+                                       if (c == -1)\r
+                                               Report.Error (1027, Location, "#endif/#endregion expected");\r
+                                       continue;\r
                                }\r
+                               \r
+                               if (c == '"') \r
+                                       return consume_string (false);\r
 \r
                                if (c == '\''){\r
                                        c = getChar ();\r
+                                       tokens_seen = true;\r
                                        if (c == '\''){\r
-                                               error_details = "CS1011: Empty character literal";\r
+                                               error_details = "Empty character literal";\r
+                                               Report.Error (1011, Location, error_details);\r
                                                return Token.ERROR;\r
                                        }\r
                                        c = escape (c);\r
@@ -845,12 +1727,20 @@ namespace CIR
                                        val = new System.Char ();\r
                                        val = (char) c;\r
                                        c = getChar ();\r
+\r
                                        if (c != '\''){\r
-                                               error_details = "CS1012: Too many characters in character literal";\r
+                                               error_details = "Too many characters in character literal";\r
+                                               Report.Error (1012, Location, error_details);\r
+\r
                                                // Try to recover, read until newline or next "'"\r
                                                while ((c = getChar ()) != -1){\r
-                                                       if (c == '\n' || c == '\'')\r
+                                                       if (c == '\n' || c == '\''){\r
+                                                               line++;\r
+                                                               ref_line++;\r
+                                                               col = 0;\r
                                                                break;\r
+                                                       } else\r
+                                                               col++;\r
                                                        \r
                                                }\r
                                                return Token.ERROR;\r
@@ -858,23 +1748,16 @@ namespace CIR
                                        return Token.LITERAL_CHARACTER;\r
                                }\r
                                \r
-                               // white space\r
-                               if (c == '\n'){\r
-                                       line++;\r
-                                       ref_line++;\r
-                                       col = 0;\r
-                                       continue;\r
-                               }\r
-                               if (c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == '\r'){\r
-                                       if (c == '\t')\r
-                                               col = (((col + 8) / 8) * 8) - 1;\r
-                                       \r
-                                       continue;\r
-                               }\r
-\r
-                               if (c == '@'){\r
-                                       allow_keyword_as_ident = true;\r
-                                       continue;\r
+                               if (c == '@') {\r
+                                       c = getChar ();\r
+                                       if (c == '"') {\r
+                                               tokens_seen = true;\r
+                                               return consume_string (true);\r
+                                       } else if (is_identifier_start_character ((char) c)){\r
+                                               return consume_identifier (c, true);\r
+                                       } else {\r
+                                               Report.Error (1033, Location, "'@' must be followed by string constant or identifier");\r
+                                       }\r
                                }\r
 \r
                                error_details = ((char)c).ToString ();\r
@@ -884,6 +1767,19 @@ namespace CIR
 \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
 \r