-// -*- coding: dos -*-\r
-//\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, 2002 Ximian, Inc (http://www.ximian.com)\r
-//\r
-\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
-using System.Text;\r
-using System.Collections;\r
-using System.IO;\r
-using System.Globalization;\r
-using System.Reflection;\r
-\r
-namespace Mono.CSharp\r
-{\r
- /// <summary>\r
- /// Tokenizer for C# source code. \r
- /// </summary>\r
-\r
- public class Tokenizer : yyParser.yyInput\r
- {\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
- public int current_token;\r
- bool handle_get_set = false;\r
- bool handle_remove_add = false;\r
- bool handle_assembly = false;\r
- bool handle_constraints = false;\r
- bool handle_typeof = 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
- public string location {\r
- get {\r
- string det;\r
-\r
- if (current_token == Token.ERROR)\r
- det = "detail: " + error_details;\r
- else\r
- det = "";\r
- \r
- // return "Line: "+line+" Col: "+col + "\n" +\r
- // "VirtLine: "+ref_line +\r
- // " 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 PropertyParsing {\r
- get {\r
- return handle_get_set;\r
- }\r
-\r
- set {\r
- 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
- public bool ConstraintsParsing {\r
- get {\r
- return handle_constraints;\r
- }\r
-\r
- set {\r
- handle_constraints = value;\r
- }\r
- }\r
-\r
- public bool TypeOfParsing {\r
- get {\r
- return handle_typeof;\r
- }\r
-\r
- set {\r
- handle_typeof = value;\r
- }\r
- }\r
- \r
- //\r
- // Class variables\r
- // \r
- static CharArrayHashtable[] keywords;\r
- static NumberStyles styles;\r
- static NumberFormatInfo csharp_format_info;\r
- \r
- //\r
- // Values for the associated token returned\r
- //\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
- 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
- //\r
- string error_details;\r
- \r
- public string error {\r
- get {\r
- return error_details;\r
- }\r
- }\r
- \r
- public int Line {\r
- get {\r
- return ref_line;\r
- }\r
- }\r
-\r
- public int Col {\r
- get {\r
- return col;\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 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 ("where", Token.WHERE);\r
- AddKeyword ("while", Token.WHILE);\r
- AddKeyword ("partial", Token.PARTIAL);\r
- }\r
-\r
- //\r
- // Class initializer\r
- // \r
- static Tokenizer ()\r
- {\r
- InitTokens ();\r
- csharp_format_info = NumberFormatInfo.InvariantInfo;\r
- styles = NumberStyles.Float;\r
- \r
- string_builder = new System.Text.StringBuilder ();\r
- }\r
-\r
- int GetKeyword (char[] id, int id_len)\r
- {\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
- 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
- if (handle_constraints == false && res == Token.WHERE)\r
- return -1;\r
-\r
- return res;\r
- \r
- }\r
-\r
- public Location Location {\r
- get {\r
- 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 (SeekableStreamReader input, SourceFile file, ArrayList defs)\r
- {\r
- this.ref_name = file;\r
- this.file_name = file;\r
- reader = input;\r
- \r
- putback_char = -1;\r
-\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
- public static void Cleanup () {\r
- identifiers = null;\r
- }\r
-\r
- static bool is_identifier_start_character (char c)\r
- {\r
- return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);\r
- }\r
-\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
- 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
- bool parse_generic_dimension (out int dimension)\r
- {\r
- dimension = 1;\r
-\r
- again:\r
- int the_token = token ();\r
- if (the_token == Token.OP_GENERICS_GT)\r
- return true;\r
- else if (the_token == Token.COMMA) {\r
- dimension++;\r
- goto again;\r
- }\r
-\r
- return false;\r
- }\r
-\r
- bool parse_less_than ()\r
- {\r
- start:\r
- int the_token = token ();\r
- switch (the_token) {\r
- case Token.IDENTIFIER:\r
- case Token.OBJECT:\r
- case Token.STRING:\r
- case Token.BOOL:\r
- case Token.DECIMAL:\r
- case Token.FLOAT:\r
- case Token.DOUBLE:\r
- case Token.SBYTE:\r
- case Token.BYTE:\r
- case Token.SHORT:\r
- case Token.USHORT:\r
- case Token.INT:\r
- case Token.UINT:\r
- case Token.LONG:\r
- case Token.ULONG:\r
- case Token.CHAR:\r
- case Token.VOID:\r
- break;\r
-\r
- default:\r
- return false;\r
- }\r
- again:\r
- the_token = token ();\r
-\r
- if (the_token == Token.OP_GENERICS_GT)\r
- return true;\r
- else if ((the_token == Token.COMMA) || (the_token == Token.DOT))\r
- goto start;\r
- else if (the_token == Token.OP_GENERICS_LT) {\r
- if (!parse_less_than ())\r
- return false;\r
- goto again;\r
- } else if (the_token == Token.OPEN_BRACKET) {\r
- rank_specifiers:\r
- the_token = token ();\r
- if (the_token == Token.CLOSE_BRACKET)\r
- goto again;\r
- else if (the_token == Token.COMMA)\r
- goto rank_specifiers;\r
- return false;\r
- }\r
-\r
- return false;\r
- }\r
-\r
- bool parsing_less_than = false;\r
- int parsing_generic_less_than = 0;\r
-\r
- int is_punct (char c, ref bool doread)\r
- {\r
- int d;\r
- int t;\r
-\r
- doread = false;\r
-\r
- switch (c){\r
- case '{':\r
- return Token.OPEN_BRACE;\r
- case '}':\r
- return Token.CLOSE_BRACE;\r
- case '[':\r
- return Token.OPEN_BRACKET;\r
- case ']':\r
- return Token.CLOSE_BRACKET;\r
- case '(':\r
- return Token.OPEN_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
- return Token.COLON;\r
- case ';':\r
- return Token.SEMICOLON;\r
- case '~':\r
- return Token.TILDE;\r
- case '?':\r
- return Token.INTERR;\r
- }\r
-\r
- if (c == '<') {\r
- if (parsing_generic_less_than++ > 0)\r
- return Token.OP_GENERICS_LT;\r
-\r
- int old = reader.Position;\r
- if (handle_typeof) {\r
- int dimension;\r
- if (parse_generic_dimension (out dimension)) {\r
- val = dimension;\r
- return Token.GENERIC_DIMENSION;\r
- }\r
- reader.Position = old;\r
- putback_char = -1;\r
- }\r
-\r
- // Save current position and parse next token.\r
- old = reader.Position;\r
- bool is_generic_lt = parse_less_than ();\r
- reader.Position = old;\r
- putback_char = -1;\r
-\r
- if (is_generic_lt) {\r
- parsing_generic_less_than++;\r
- return Token.OP_GENERICS_LT;\r
- } else\r
- parsing_generic_less_than = 0;\r
-\r
- d = peekChar ();\r
- if (d == '<'){\r
- getChar ();\r
- d = peekChar ();\r
-\r
- if (d == '='){\r
- doread = true;\r
- return Token.OP_SHIFT_LEFT_ASSIGN;\r
- }\r
- return Token.OP_SHIFT_LEFT;\r
- } else if (d == '='){\r
- doread = true;\r
- return Token.OP_LE;\r
- }\r
- return Token.OP_LT;\r
- } else if (c == '>') {\r
- if (parsing_generic_less_than > 0) {\r
- parsing_generic_less_than--;\r
- return Token.OP_GENERICS_GT;\r
- }\r
-\r
- d = peekChar ();\r
- if (d == '>'){\r
- getChar ();\r
- d = peekChar ();\r
-\r
- if (d == '='){\r
- doread = true;\r
- return Token.OP_SHIFT_RIGHT_ASSIGN;\r
- }\r
- return Token.OP_SHIFT_RIGHT;\r
- } else if (d == '='){\r
- doread = true;\r
- return Token.OP_GE;\r
- }\r
- return Token.OP_GT;\r
- }\r
-\r
- d = peekChar ();\r
- if (c == '+'){\r
- \r
- if (d == '+')\r
- t = Token.OP_INC;\r
- else if (d == '=')\r
- t = Token.OP_ADD_ASSIGN;\r
- else\r
- return Token.PLUS;\r
- doread = true;\r
- return t;\r
- }\r
- if (c == '-'){\r
- if (d == '-')\r
- t = Token.OP_DEC;\r
- else if (d == '=')\r
- t = Token.OP_SUB_ASSIGN;\r
- else if (d == '>')\r
- t = Token.OP_PTR;\r
- else\r
- return Token.MINUS;\r
- doread = true;\r
- return t;\r
- }\r
-\r
- if (c == '!'){\r
- if (d == '='){\r
- doread = true;\r
- return Token.OP_NE;\r
- }\r
- return Token.BANG;\r
- }\r
-\r
- if (c == '='){\r
- if (d == '='){\r
- doread = true;\r
- return Token.OP_EQ;\r
- }\r
- return Token.ASSIGN;\r
- }\r
-\r
- if (c == '&'){\r
- if (d == '&'){\r
- doread = true;\r
- return Token.OP_AND;\r
- } else if (d == '='){\r
- doread = true;\r
- return Token.OP_AND_ASSIGN;\r
- }\r
- return Token.BITWISE_AND;\r
- }\r
-\r
- if (c == '|'){\r
- if (d == '|'){\r
- doread = true;\r
- return Token.OP_OR;\r
- } else if (d == '='){\r
- doread = true;\r
- return Token.OP_OR_ASSIGN;\r
- }\r
- return Token.BITWISE_OR;\r
- }\r
-\r
- if (c == '*'){\r
- if (d == '='){\r
- doread = true;\r
- return Token.OP_MULT_ASSIGN;\r
- }\r
- return Token.STAR;\r
- }\r
-\r
- if (c == '/'){\r
- if (d == '='){\r
- doread = true;\r
- return Token.OP_DIV_ASSIGN;\r
- }\r
- return Token.DIV;\r
- }\r
-\r
- if (c == '%'){\r
- if (d == '='){\r
- doread = true;\r
- return Token.OP_MOD_ASSIGN;\r
- }\r
- return Token.PERCENT;\r
- }\r
-\r
- if (c == '^'){\r
- if (d == '='){\r
- doread = true;\r
- return Token.OP_XOR_ASSIGN;\r
- }\r
- return Token.CARRET;\r
- }\r
-\r
- 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
- if (number_pos == max_number_size)\r
- Error_NumericConstantTooLong ();\r
- number_builder [number_pos++] = (char) c;\r
- }\r
- \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
- if (c != -1)\r
- number_builder [number_pos++] = (char) c;\r
- \r
- }\r
- \r
- int real_type_suffix (int c)\r
- {\r
- int t;\r
-\r
- switch (c){\r
- case 'F': case 'f':\r
- t = Token.LITERAL_FLOAT;\r
- break;\r
- case 'D': case 'd':\r
- t = Token.LITERAL_DOUBLE;\r
- break;\r
- case 'M': case 'm':\r
- t= Token.LITERAL_DECIMAL;\r
- break;\r
- default:\r
- return Token.NONE;\r
- }\r
- return t;\r
- }\r
-\r
- int integer_type_suffix (ulong ul, int c)\r
- {\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
- //\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
- 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 = new String (number_builder, 0, number_pos);\r
-\r
- switch (t){\r
- case Token.LITERAL_DECIMAL:\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
- 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
- case Token.LITERAL_DOUBLE:\r
- case Token.NONE:\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
- 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
- int is_number (int c)\r
- {\r
- bool is_real = false;\r
- int type;\r
-\r
- number_pos = 0;\r
-\r
- if (c >= '0' && c <= '9'){\r
- if (c == '0'){\r
- int peek = peekChar ();\r
-\r
- if (peek == 'x' || peek == 'X')\r
- return handle_hex ();\r
- }\r
- decimal_digits (c);\r
- c = getChar ();\r
- }\r
-\r
- //\r
- // We need to handle the case of\r
- // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)\r
- //\r
- if (c == '.'){\r
- if (decimal_digits ('.')){\r
- is_real = true;\r
- c = getChar ();\r
- } else {\r
- putback ('.');\r
- number_pos--;\r
- return adjust_int (-1);\r
- }\r
- }\r
- \r
- if (c == 'e' || c == 'E'){\r
- is_real = true;\r
- if (number_pos == max_number_size)\r
- Error_NumericConstantTooLong ();\r
- number_builder [number_pos++] = 'e';\r
- c = getChar ();\r
- \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
- type = real_type_suffix (c);\r
- if (type == Token.NONE && !is_real){\r
- putback (c);\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 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
- d = peekChar ();\r
- if (c != '\\')\r
- return c;\r
- \r
- switch (d){\r
- case 'a':\r
- v = '\a'; break;\r
- case 'b':\r
- v = '\b'; break;\r
- case 'n':\r
- v = '\n'; break;\r
- case 't':\r
- v = '\t'; break;\r
- case 'v':\r
- v = '\v'; break;\r
- case 'r':\r
- v = '\r'; break;\r
- case '\\':\r
- v = '\\'; break;\r
- case 'f':\r
- v = '\f'; break;\r
- case '0':\r
- v = 0; break;\r
- case '"':\r
- 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
- Report.Error (1009, Location, "Unrecognized escape sequence in " + (char)d);\r
- return d;\r
- }\r
- getChar ();\r
- return v;\r
- }\r
-\r
- int getChar ()\r
- {\r
- if (putback_char != -1){\r
- int x = putback_char;\r
- putback_char = -1;\r
-\r
- return x;\r
- }\r
- return reader.Read ();\r
- }\r
-\r
- int peekChar ()\r
- {\r
- if (putback_char != -1)\r
- return putback_char;\r
- putback_char = reader.Read ();\r
- return putback_char;\r
- }\r
-\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
- public bool advance ()\r
- {\r
- return peekChar () != -1;\r
- }\r
-\r
- public Object Value {\r
- get {\r
- return val;\r
- }\r
- }\r
-\r
- public Object value ()\r
- {\r
- return val;\r
- }\r
-\r
- bool IsCastToken (int token)\r
- {\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
-\r
- if (current_token != Token.DEFAULT)\r
- return current_token;\r
-\r
- int c = consume_whitespace ();\r
- if (c == -1)\r
- current_token = Token.ERROR;\r
- else if (c == '(')\r
- current_token = Token.DEFAULT_OPEN_PARENS;\r
- else\r
- putback (c);\r
-\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
- {\r
- int c;\r
- \r
- tokens_seen = false;\r
- arg = "";\r
- static_cmd_arg.Length = 0;\r
-\r
- // skip over white space\r
- while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))\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
-\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
- 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
- } else if (arg == "hidden"){\r
- //\r
- // We ignore #line hidden\r
- //\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
- char [] quotes = { '\"' };\r
- \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
- } 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
- 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
-\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
- Error_InvalidDirective ();\r
- return false;\r
- }\r
- \r
- if (is_identifier_start_character (c)){\r
- int j = 1;\r
-\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
- 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
- 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 "pragma":\r
- if (RootContext.V2)\r
- return caller_is_taking;\r
- break;\r
- \r
- case "line":\r
- if (!PreProcessLine (arg))\r
- Report.Error (\r
- 1576, Location,\r
- "Argument to #line directive is missing or invalid");\r
- return caller_is_taking;\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 (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
- 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)\r
- {\r
- int res = consume_identifier (s, false);\r
-\r
- if (res == Token.PARTIAL) {\r
- // Save current position and parse next token.\r
- int old = reader.Position;\r
- int old_putback = putback_char;\r
-\r
- putback_char = -1;\r
-\r
- int next_token = token ();\r
- bool ok = (next_token == Token.CLASS) ||\r
- (next_token == Token.STRUCT) ||\r
- (next_token == Token.INTERFACE);\r
-\r
- reader.Position = old;\r
- putback_char = old_putback;\r
-\r
- if (ok)\r
- return res;\r
- else {\r
- val = "partial";\r
- return Token.IDENTIFIER;\r
- }\r
- }\r
-\r
- return res;\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
- //\r
- // Optimization: avoids doing the keyword lookup\r
- // on uppercase letters and _\r
- //\r
- if (!quoted && (s >= 'a' || s == '_')){\r
- int keyword = GetKeyword (id_builder, pos);\r
- if (keyword != -1)\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
- }\r
- else\r
- identifiers [pos] = new CharArrayHashtable (pos);\r
-\r
- val = new String (id_builder, 0, pos);\r
-\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
- int consume_whitespace ()\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 == ' ')\r
- continue;\r
- \r
- if (c == '\t') {\r
- col = (((col + 8) / 8) * 8) - 1;\r
- continue;\r
- }\r
- \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
- if (c == '/'){\r
- int d = peekChar ();\r
- \r
- if (d == '/'){\r
- getChar ();\r
- while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')\r
- col++;\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
-\r
- while ((d = getChar ()) != -1){\r
- if (d == '*' && peekChar () == '/'){\r
- getChar ();\r
- col++;\r
- break;\r
- }\r
- if (d == '\n'){\r
- line++;\r
- ref_line++;\r
- col = 0;\r
- any_token_seen |= tokens_seen;\r
- tokens_seen = false;\r
- }\r
- }\r
- continue;\r
- }\r
- goto is_punct_label;\r
- }\r
-\r
- is_punct_label:\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
- /* 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
- return c;\r
- }\r
-\r
- return -1;\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
-\r
- c = consume_whitespace ();\r
- if (c == -1)\r
- return Token.EOF;\r
-\r
- if (is_identifier_start_character ((char)c)){\r
- tokens_seen = true;\r
- return consume_identifier (c);\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 >= '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
- if (c == '"') \r
- return consume_string (false);\r
-\r
- if (c == '\''){\r
- c = getChar ();\r
- tokens_seen = true;\r
- if (c == '\''){\r
- error_details = "Empty character literal";\r
- Report.Error (1011, Location, error_details);\r
- return Token.ERROR;\r
- }\r
- c = escape (c);\r
- if (c == -1)\r
- return Token.ERROR;\r
- val = new System.Char ();\r
- val = (char) c;\r
- c = getChar ();\r
-\r
- if (c != '\''){\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
- line++;\r
- ref_line++;\r
- col = 0;\r
- break;\r
- } else\r
- col++;\r
- }\r
- return Token.ERROR;\r
- }\r
- return Token.LITERAL_CHARACTER;\r
- }\r
- \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
- 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
- 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
+//
+// cs-tokenizer.cs: The Tokenizer for the C# compiler
+// This also implements the preprocessor
+//
+// Author: Miguel de Icaza (miguel@gnu.org)
+//
+// Licensed under the terms of the GNU GPL
+//
+// (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
+// (C) 2004 Novell, Inc
+//
+
+/*
+ * TODO:
+ * Make sure we accept the proper Unicode ranges, per the spec.
+ * Report error 1032
+*/
+
+using System;
+using System.Text;
+using System.Collections;
+using System.IO;
+using System.Globalization;
+using System.Reflection;
+
+namespace Mono.CSharp
+{
+ /// <summary>
+ /// Tokenizer for C# source code.
+ /// </summary>
+
+ public class Tokenizer : yyParser.yyInput
+ {
+ SeekableStreamReader reader;
+ SourceFile ref_name;
+ SourceFile file_name;
+ int ref_line = 1;
+ int line = 1;
+ int col = 0;
+ int previous_col;
+ int current_token;
+ bool handle_get_set = false;
+ bool handle_remove_add = false;
+ bool handle_assembly = false;
+ bool handle_constraints = false;
+ bool handle_typeof = false;
+ Location current_location;
+ Location current_comment_location = Location.Null;
+ ArrayList escapedIdentifiers = new ArrayList ();
+
+ //
+ // XML documentation buffer. The save point is used to divide
+ // comments on types and comments on members.
+ //
+ StringBuilder xml_comment_buffer;
+
+ //
+ // See comment on XmlCommentState enumeration.
+ //
+ XmlCommentState xmlDocState = XmlCommentState.Allowed;
+
+ //
+ // Whether tokens have been seen on this line
+ //
+ bool tokens_seen = false;
+
+ //
+ // Whether a token has been seen on the file
+ // This is needed because `define' is not allowed to be used
+ // after a token has been seen.
+ //
+ bool any_token_seen = false;
+
+ static Hashtable tokenValues;
+
+ private static Hashtable TokenValueName
+ {
+ get {
+ if (tokenValues == null)
+ tokenValues = GetTokenValueNameHash ();
+
+ return tokenValues;
+ }
+ }
+
+ private static Hashtable GetTokenValueNameHash ()
+ {
+ Type t = typeof (Token);
+ FieldInfo [] fields = t.GetFields ();
+ Hashtable hash = new Hashtable ();
+ foreach (FieldInfo field in fields) {
+ if (field.IsLiteral && field.IsStatic && field.FieldType == typeof (int))
+ hash.Add (field.GetValue (null), field.Name);
+ }
+ return hash;
+ }
+
+ //
+ // Returns a verbose representation of the current location
+ //
+ public string location {
+ get {
+ string det;
+
+ if (current_token == Token.ERROR)
+ det = "detail: " + error_details;
+ else
+ det = "";
+
+ // return "Line: "+line+" Col: "+col + "\n" +
+ // "VirtLine: "+ref_line +
+ // " Token: "+current_token + " " + det;
+ string current_token_name = TokenValueName [current_token] as string;
+ if (current_token_name == null)
+ current_token_name = current_token.ToString ();
+
+ return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name.Name,
+ ref_line,
+ col,
+ current_token_name,
+ det);
+ }
+ }
+
+ public bool PropertyParsing {
+ get {
+ return handle_get_set;
+ }
+
+ set {
+ handle_get_set = value;
+ }
+ }
+
+ public bool AssemblyTargetParsing {
+ get {
+ return handle_assembly;
+ }
+
+ set {
+ handle_assembly = value;
+ }
+ }
+
+ public bool EventParsing {
+ get {
+ return handle_remove_add;
+ }
+
+ set {
+ handle_remove_add = value;
+ }
+ }
+
+ public bool ConstraintsParsing {
+ get {
+ return handle_constraints;
+ }
+
+ set {
+ handle_constraints = value;
+ }
+ }
+
+ public bool TypeOfParsing {
+ get {
+ return handle_typeof;
+ }
+
+ set {
+ handle_typeof = value;
+ }
+ }
+
+ public XmlCommentState doc_state {
+ get { return xmlDocState; }
+ set {
+ if (value == XmlCommentState.Allowed) {
+ check_incorrect_doc_comment ();
+ reset_doc_comment ();
+ }
+ xmlDocState = value;
+ }
+ }
+
+ public bool IsEscapedIdentifier (Location loc)
+ {
+ foreach (LocatedToken lt in escapedIdentifiers)
+ if (lt.Location.Equals (loc))
+ return true;
+ return false;
+ }
+
+ //
+ // Class variables
+ //
+ static CharArrayHashtable[] keywords;
+ static Hashtable keywordStrings = new Hashtable ();
+ static NumberStyles styles;
+ static NumberFormatInfo csharp_format_info;
+
+ //
+ // Values for the associated token returned
+ //
+ int putback_char;
+ Object val;
+
+ //
+ // Pre-processor
+ //
+ Hashtable defines;
+
+ const int TAKING = 1;
+ const int TAKEN_BEFORE = 2;
+ const int ELSE_SEEN = 4;
+ const int PARENT_TAKING = 8;
+ const int REGION = 16;
+
+ //
+ // pre-processor if stack state:
+ //
+ Stack ifstack;
+
+ static System.Text.StringBuilder string_builder;
+
+ const int max_id_size = 512;
+ static char [] id_builder = new char [max_id_size];
+
+ static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
+
+ const int max_number_size = 512;
+ static char [] number_builder = new char [max_number_size];
+ static int number_pos;
+
+ //
+ // Details about the error encoutered by the tokenizer
+ //
+ string error_details;
+
+ public string error {
+ get {
+ return error_details;
+ }
+ }
+
+ public int Line {
+ get {
+ return ref_line;
+ }
+ }
+
+ public int Col {
+ get {
+ return col;
+ }
+ }
+
+ //
+ // This is used when the tokenizer needs to save
+ // the current position as it needs to do some parsing
+ // on its own to deamiguate a token in behalf of the
+ // parser.
+ //
+ Stack position_stack = new Stack ();
+ class Position {
+ public int position;
+ public int ref_line;
+ public int col;
+ public int putback_char;
+ public int previous_col;
+ public int parsing_generic_less_than;
+
+ public Position (Tokenizer t)
+ {
+ position = t.reader.Position;
+ ref_line = t.ref_line;
+ col = t.col;
+ putback_char = t.putback_char;
+ previous_col = t.previous_col;
+ parsing_generic_less_than = t.parsing_generic_less_than;
+ }
+ }
+
+ public void PushPosition ()
+ {
+ position_stack.Push (new Position (this));
+ }
+
+ public void PopPosition ()
+ {
+ Position p = (Position) position_stack.Pop ();
+
+ reader.Position = p.position;
+ ref_line = p.ref_line;
+ col = p.col;
+ putback_char = p.putback_char;
+ previous_col = p.previous_col;
+
+ }
+
+ // Do not reset the position, ignore it.
+ public void DiscardPosition ()
+ {
+ position_stack.Pop ();
+ }
+
+ static void AddKeyword (string kw, int token) {
+ keywordStrings.Add (kw, kw);
+ if (keywords [kw.Length] == null) {
+ keywords [kw.Length] = new CharArrayHashtable (kw.Length);
+ }
+ keywords [kw.Length] [kw.ToCharArray ()] = token;
+ }
+
+ static void InitTokens ()
+ {
+ keywords = new CharArrayHashtable [64];
+
+ AddKeyword ("__arglist", Token.ARGLIST);
+ AddKeyword ("abstract", Token.ABSTRACT);
+ AddKeyword ("as", Token.AS);
+ AddKeyword ("add", Token.ADD);
+ AddKeyword ("assembly", Token.ASSEMBLY);
+ AddKeyword ("base", Token.BASE);
+ AddKeyword ("bool", Token.BOOL);
+ AddKeyword ("break", Token.BREAK);
+ AddKeyword ("byte", Token.BYTE);
+ AddKeyword ("case", Token.CASE);
+ AddKeyword ("catch", Token.CATCH);
+ AddKeyword ("char", Token.CHAR);
+ AddKeyword ("checked", Token.CHECKED);
+ AddKeyword ("class", Token.CLASS);
+ AddKeyword ("const", Token.CONST);
+ AddKeyword ("continue", Token.CONTINUE);
+ AddKeyword ("decimal", Token.DECIMAL);
+ AddKeyword ("default", Token.DEFAULT);
+ AddKeyword ("delegate", Token.DELEGATE);
+ AddKeyword ("do", Token.DO);
+ AddKeyword ("double", Token.DOUBLE);
+ AddKeyword ("else", Token.ELSE);
+ AddKeyword ("enum", Token.ENUM);
+ AddKeyword ("event", Token.EVENT);
+ AddKeyword ("explicit", Token.EXPLICIT);
+ AddKeyword ("extern", Token.EXTERN);
+ AddKeyword ("false", Token.FALSE);
+ AddKeyword ("finally", Token.FINALLY);
+ AddKeyword ("fixed", Token.FIXED);
+ AddKeyword ("float", Token.FLOAT);
+ AddKeyword ("for", Token.FOR);
+ AddKeyword ("foreach", Token.FOREACH);
+ AddKeyword ("goto", Token.GOTO);
+ AddKeyword ("get", Token.GET);
+ AddKeyword ("if", Token.IF);
+ AddKeyword ("implicit", Token.IMPLICIT);
+ AddKeyword ("in", Token.IN);
+ AddKeyword ("int", Token.INT);
+ AddKeyword ("interface", Token.INTERFACE);
+ AddKeyword ("internal", Token.INTERNAL);
+ AddKeyword ("is", Token.IS);
+ AddKeyword ("lock", Token.LOCK);
+ AddKeyword ("long", Token.LONG);
+ AddKeyword ("namespace", Token.NAMESPACE);
+ AddKeyword ("new", Token.NEW);
+ AddKeyword ("null", Token.NULL);
+ AddKeyword ("object", Token.OBJECT);
+ AddKeyword ("operator", Token.OPERATOR);
+ AddKeyword ("out", Token.OUT);
+ AddKeyword ("override", Token.OVERRIDE);
+ AddKeyword ("params", Token.PARAMS);
+ AddKeyword ("private", Token.PRIVATE);
+ AddKeyword ("protected", Token.PROTECTED);
+ AddKeyword ("public", Token.PUBLIC);
+ AddKeyword ("readonly", Token.READONLY);
+ AddKeyword ("ref", Token.REF);
+ AddKeyword ("remove", Token.REMOVE);
+ AddKeyword ("return", Token.RETURN);
+ AddKeyword ("sbyte", Token.SBYTE);
+ AddKeyword ("sealed", Token.SEALED);
+ AddKeyword ("set", Token.SET);
+ AddKeyword ("short", Token.SHORT);
+ AddKeyword ("sizeof", Token.SIZEOF);
+ AddKeyword ("stackalloc", Token.STACKALLOC);
+ AddKeyword ("static", Token.STATIC);
+ AddKeyword ("string", Token.STRING);
+ AddKeyword ("struct", Token.STRUCT);
+ AddKeyword ("switch", Token.SWITCH);
+ AddKeyword ("this", Token.THIS);
+ AddKeyword ("throw", Token.THROW);
+ AddKeyword ("true", Token.TRUE);
+ AddKeyword ("try", Token.TRY);
+ AddKeyword ("typeof", Token.TYPEOF);
+ AddKeyword ("uint", Token.UINT);
+ AddKeyword ("ulong", Token.ULONG);
+ AddKeyword ("unchecked", Token.UNCHECKED);
+ AddKeyword ("unsafe", Token.UNSAFE);
+ AddKeyword ("ushort", Token.USHORT);
+ AddKeyword ("using", Token.USING);
+ AddKeyword ("virtual", Token.VIRTUAL);
+ AddKeyword ("void", Token.VOID);
+ AddKeyword ("volatile", Token.VOLATILE);
+ AddKeyword ("where", Token.WHERE);
+ AddKeyword ("while", Token.WHILE);
+ AddKeyword ("partial", Token.PARTIAL);
+ }
+
+ //
+ // Class initializer
+ //
+ static Tokenizer ()
+ {
+ InitTokens ();
+ csharp_format_info = NumberFormatInfo.InvariantInfo;
+ styles = NumberStyles.Float;
+
+ string_builder = new System.Text.StringBuilder ();
+ }
+
+ int GetKeyword (char[] id, int id_len)
+ {
+ /*
+ * Keywords are stored in an array of hashtables grouped by their
+ * length.
+ */
+
+ if ((id_len >= keywords.Length) || (keywords [id_len] == null))
+ return -1;
+ object o = keywords [id_len] [id];
+
+ if (o == null)
+ return -1;
+
+ int res = (int) o;
+
+ if (handle_get_set == false && (res == Token.GET || res == Token.SET))
+ return -1;
+ if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
+ return -1;
+ if (handle_assembly == false && res == Token.ASSEMBLY)
+ return -1;
+ if (handle_constraints == false && res == Token.WHERE)
+ return -1;
+
+ return res;
+
+ }
+
+ public Location Location {
+ get { return current_location; }
+ }
+
+ void define (string def)
+ {
+ if (!RootContext.AllDefines.Contains (def)){
+ RootContext.AllDefines [def] = true;
+ }
+ if (defines.Contains (def))
+ return;
+ defines [def] = true;
+ }
+
+ public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
+ {
+ this.ref_name = file;
+ this.file_name = file;
+ reader = input;
+
+ putback_char = -1;
+
+ if (defs != null){
+ defines = new Hashtable ();
+ foreach (string def in defs)
+ define (def);
+ }
+
+ xml_comment_buffer = new StringBuilder ();
+
+ //
+ // FIXME: This could be `Location.Push' but we have to
+ // find out why the MS compiler allows this
+ //
+ Mono.CSharp.Location.Push (file);
+ }
+
+ static bool is_identifier_start_character (char c)
+ {
+ return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
+ }
+
+ static bool is_identifier_part_character (char c)
+ {
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
+ }
+
+ public static bool IsKeyword (string s)
+ {
+ return keywordStrings [s] != null;
+ }
+
+ public static bool IsValidIdentifier (string s)
+ {
+ if (s == null || s.Length == 0)
+ return false;
+
+ if (!is_identifier_start_character (s [0]))
+ return false;
+
+ for (int i = 1; i < s.Length; i ++)
+ if (! is_identifier_part_character (s [i]))
+ return false;
+
+ return true;
+ }
+
+ bool parse_generic_dimension (out int dimension)
+ {
+ dimension = 1;
+
+ again:
+ int the_token = token ();
+ if (the_token == Token.OP_GENERICS_GT)
+ return true;
+ else if (the_token == Token.COMMA) {
+ dimension++;
+ goto again;
+ }
+
+ return false;
+ }
+
+ bool parse_less_than ()
+ {
+ start:
+ int the_token = token ();
+ if (the_token == Token.OPEN_BRACKET) {
+ do {
+ the_token = token ();
+ } while (the_token != Token.CLOSE_BRACKET);
+ the_token = token ();
+ }
+ switch (the_token) {
+ case Token.IDENTIFIER:
+ case Token.OBJECT:
+ case Token.STRING:
+ case Token.BOOL:
+ case Token.DECIMAL:
+ case Token.FLOAT:
+ case Token.DOUBLE:
+ case Token.SBYTE:
+ case Token.BYTE:
+ case Token.SHORT:
+ case Token.USHORT:
+ case Token.INT:
+ case Token.UINT:
+ case Token.LONG:
+ case Token.ULONG:
+ case Token.CHAR:
+ case Token.VOID:
+ break;
+
+ default:
+ return false;
+ }
+ again:
+ the_token = token ();
+
+ if (the_token == Token.OP_GENERICS_GT)
+ return true;
+ else if ((the_token == Token.COMMA) || (the_token == Token.DOT))
+ goto start;
+ else if (the_token == Token.INTERR || the_token == Token.STAR)
+ goto again;
+ else if (the_token == Token.OP_GENERICS_LT) {
+ if (!parse_less_than ())
+ return false;
+ goto again;
+ } else if (the_token == Token.OPEN_BRACKET) {
+ rank_specifiers:
+ the_token = token ();
+ if (the_token == Token.CLOSE_BRACKET)
+ goto again;
+ else if (the_token == Token.COMMA)
+ goto rank_specifiers;
+ return false;
+ }
+
+ return false;
+ }
+
+ int parsing_generic_less_than = 0;
+
+ int is_punct (char c, ref bool doread)
+ {
+ int d;
+ int t;
+
+ doread = false;
+
+ switch (c){
+ case '{':
+ val = Location;
+ return Token.OPEN_BRACE;
+ case '}':
+ val = Location;
+ return Token.CLOSE_BRACE;
+ case '[':
+ // To block doccomment inside attribute declaration.
+ if (doc_state == XmlCommentState.Allowed)
+ doc_state = XmlCommentState.NotAllowed;
+ return Token.OPEN_BRACKET;
+ case ']':
+ return Token.CLOSE_BRACKET;
+ case '(':
+ return Token.OPEN_PARENS;
+ case ')': {
+ if (deambiguate_close_parens == 0)
+ return Token.CLOSE_PARENS;
+
+ --deambiguate_close_parens;
+
+ PushPosition ();
+ int new_token = token ();
+ PopPosition ();
+
+ if (new_token == Token.OPEN_PARENS)
+ return Token.CLOSE_PARENS_OPEN_PARENS;
+ else if (new_token == Token.MINUS)
+ return Token.CLOSE_PARENS_MINUS;
+ else if (IsCastToken (new_token))
+ return Token.CLOSE_PARENS_CAST;
+ else
+ return Token.CLOSE_PARENS_NO_CAST;
+ }
+
+ case ',':
+ return Token.COMMA;
+ case ';':
+ val = Location;
+ return Token.SEMICOLON;
+ case '~':
+ val = Location;
+ return Token.TILDE;
+ case '?':
+ return Token.INTERR;
+ }
+
+ if (c == '<') {
+ if (parsing_generic_less_than++ > 0)
+ return Token.OP_GENERICS_LT;
+
+ if (handle_typeof) {
+ int dimension;
+ PushPosition ();
+ if (parse_generic_dimension (out dimension)) {
+ val = dimension;
+ DiscardPosition ();
+ return Token.GENERIC_DIMENSION;
+ }
+ PopPosition ();
+ }
+
+ // Save current position and parse next token.
+ PushPosition ();
+ bool is_generic_lt = parse_less_than ();
+ PopPosition ();
+
+ if (is_generic_lt) {
+ parsing_generic_less_than++;
+ return Token.OP_GENERICS_LT;
+ } else
+ parsing_generic_less_than = 0;
+
+ d = peekChar ();
+ if (d == '<'){
+ getChar ();
+ d = peekChar ();
+
+ if (d == '='){
+ doread = true;
+ return Token.OP_SHIFT_LEFT_ASSIGN;
+ }
+ return Token.OP_SHIFT_LEFT;
+ } else if (d == '='){
+ doread = true;
+ return Token.OP_LE;
+ }
+ return Token.OP_LT;
+ } else if (c == '>') {
+ if (parsing_generic_less_than > 0) {
+ parsing_generic_less_than--;
+ return Token.OP_GENERICS_GT;
+ }
+
+ d = peekChar ();
+ if (d == '>'){
+ getChar ();
+ d = peekChar ();
+
+ if (d == '='){
+ doread = true;
+ return Token.OP_SHIFT_RIGHT_ASSIGN;
+ }
+ return Token.OP_SHIFT_RIGHT;
+ } else if (d == '='){
+ doread = true;
+ return Token.OP_GE;
+ }
+ return Token.OP_GT;
+ }
+
+ d = peekChar ();
+ if (c == '+'){
+
+ if (d == '+') {
+ val = Location;
+ t = Token.OP_INC;
+ }
+ else if (d == '=')
+ t = Token.OP_ADD_ASSIGN;
+ else {
+ val = Location;
+ return Token.PLUS;
+ }
+ doread = true;
+ return t;
+ }
+ if (c == '-'){
+ if (d == '-') {
+ val = Location;
+ t = Token.OP_DEC;
+ }
+ else if (d == '=')
+ t = Token.OP_SUB_ASSIGN;
+ else if (d == '>')
+ t = Token.OP_PTR;
+ else {
+ val = Location;
+ return Token.MINUS;
+ }
+ doread = true;
+ return t;
+ }
+
+ if (c == '!'){
+ if (d == '='){
+ doread = true;
+ return Token.OP_NE;
+ }
+ val = Location;
+ return Token.BANG;
+ }
+
+ if (c == '='){
+ if (d == '='){
+ doread = true;
+ return Token.OP_EQ;
+ }
+ return Token.ASSIGN;
+ }
+
+ if (c == '&'){
+ if (d == '&'){
+ doread = true;
+ return Token.OP_AND;
+ } else if (d == '='){
+ doread = true;
+ return Token.OP_AND_ASSIGN;
+ }
+ val = Location;
+ return Token.BITWISE_AND;
+ }
+
+ if (c == '|'){
+ if (d == '|'){
+ doread = true;
+ return Token.OP_OR;
+ } else if (d == '='){
+ doread = true;
+ return Token.OP_OR_ASSIGN;
+ }
+ return Token.BITWISE_OR;
+ }
+
+ if (c == '*'){
+ if (d == '='){
+ doread = true;
+ return Token.OP_MULT_ASSIGN;
+ }
+ val = Location;
+ return Token.STAR;
+ }
+
+ if (c == '/'){
+ if (d == '='){
+ doread = true;
+ return Token.OP_DIV_ASSIGN;
+ }
+ return Token.DIV;
+ }
+
+ if (c == '%'){
+ if (d == '='){
+ doread = true;
+ return Token.OP_MOD_ASSIGN;
+ }
+ return Token.PERCENT;
+ }
+
+ if (c == '^'){
+ if (d == '='){
+ doread = true;
+ return Token.OP_XOR_ASSIGN;
+ }
+ return Token.CARRET;
+ }
+
+ if (c == ':'){
+ if (d == ':'){
+ doread = true;
+ return Token.DOUBLE_COLON;
+ }
+ val = Location;
+ return Token.COLON;
+ }
+
+ return Token.ERROR;
+ }
+
+ int deambiguate_close_parens = 0;
+
+ public void Deambiguate_CloseParens ()
+ {
+ putback (')');
+ deambiguate_close_parens++;
+ }
+
+ public void PutbackNullable ()
+ {
+ if (nullable_pos < 0)
+ throw new Exception ();
+
+ current_token = -1;
+ val = null;
+ reader.Position = nullable_pos;
+
+ putback_char = '?';
+ }
+
+ public void PutbackCloseParens ()
+ {
+ putback_char = ')';
+ }
+
+ void Error_NumericConstantTooLong ()
+ {
+ Report.Error (1021, Location, "Numeric constant too long");
+ }
+
+ int nullable_pos = -1;
+
+ public void CheckNullable (bool is_nullable)
+ {
+ if (is_nullable)
+ nullable_pos = reader.Position;
+ else
+ nullable_pos = -1;
+ }
+
+ bool decimal_digits (int c)
+ {
+ int d;
+ bool seen_digits = false;
+
+ if (c != -1){
+ if (number_pos == max_number_size)
+ Error_NumericConstantTooLong ();
+ number_builder [number_pos++] = (char) c;
+ }
+
+ //
+ // We use peekChar2, because decimal_digits needs to do a
+ // 2-character look-ahead (5.ToString for example).
+ //
+ while ((d = peekChar2 ()) != -1){
+ if (d >= '0' && d <= '9'){
+ if (number_pos == max_number_size)
+ Error_NumericConstantTooLong ();
+ number_builder [number_pos++] = (char) d;
+ getChar ();
+ seen_digits = true;
+ } else
+ break;
+ }
+
+ return seen_digits;
+ }
+
+ static bool is_hex (int e)
+ {
+ return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
+ }
+
+ static int real_type_suffix (int c)
+ {
+ int t;
+
+ switch (c){
+ case 'F': case 'f':
+ t = Token.LITERAL_FLOAT;
+ break;
+ case 'D': case 'd':
+ t = Token.LITERAL_DOUBLE;
+ break;
+ case 'M': case 'm':
+ t= Token.LITERAL_DECIMAL;
+ break;
+ default:
+ return Token.NONE;
+ }
+ return t;
+ }
+
+ int integer_type_suffix (ulong ul, int c)
+ {
+ bool is_unsigned = false;
+ bool is_long = false;
+
+ if (c != -1){
+ bool scanning = true;
+ do {
+ switch (c){
+ case 'U': case 'u':
+ if (is_unsigned)
+ scanning = false;
+ is_unsigned = true;
+ getChar ();
+ break;
+
+ case 'l':
+ if (!is_unsigned && (RootContext.WarningLevel >= 4)){
+ //
+ // if we have not seen anything in between
+ // report this error
+ //
+ Report.Warning (78, 4, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
+ }
+ //
+ // This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
+ // work around that.
+ //
+ //goto case 'L';
+ if (is_long)
+ scanning = false;
+ is_long = true;
+ getChar ();
+ break;
+
+ case 'L':
+ if (is_long)
+ scanning = false;
+ is_long = true;
+ getChar ();
+ break;
+
+ default:
+ scanning = false;
+ break;
+ }
+ c = peekChar ();
+ } while (scanning);
+ }
+
+ if (is_long && is_unsigned){
+ val = ul;
+ return Token.LITERAL_INTEGER;
+ } else if (is_unsigned){
+ // uint if possible, or ulong else.
+
+ if ((ul & 0xffffffff00000000) == 0)
+ val = (uint) ul;
+ else
+ val = ul;
+ } else if (is_long){
+ // long if possible, ulong otherwise
+ if ((ul & 0x8000000000000000) != 0)
+ val = ul;
+ else
+ val = (long) ul;
+ } else {
+ // int, uint, long or ulong in that order
+ if ((ul & 0xffffffff00000000) == 0){
+ uint ui = (uint) ul;
+
+ if ((ui & 0x80000000) != 0)
+ val = ui;
+ else
+ val = (int) ui;
+ } else {
+ if ((ul & 0x8000000000000000) != 0)
+ val = ul;
+ else
+ val = (long) ul;
+ }
+ }
+ return Token.LITERAL_INTEGER;
+ }
+
+ //
+ // given `c' as the next char in the input decide whether
+ // we need to convert to a special type, and then choose
+ // the best representation for the integer
+ //
+ int adjust_int (int c)
+ {
+ try {
+ if (number_pos > 9){
+ ulong ul = (uint) (number_builder [0] - '0');
+
+ for (int i = 1; i < number_pos; i++){
+ ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
+ }
+ return integer_type_suffix (ul, c);
+ } else {
+ uint ui = (uint) (number_builder [0] - '0');
+
+ for (int i = 1; i < number_pos; i++){
+ ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
+ }
+ return integer_type_suffix (ui, c);
+ }
+ } catch (OverflowException) {
+ error_details = "Integral constant is too large";
+ Report.Error (1021, Location, error_details);
+ val = 0ul;
+ return Token.LITERAL_INTEGER;
+ }
+ catch (FormatException) {
+ Report.Error (1013, Location, "Invalid number");
+ val = 0ul;
+ return Token.LITERAL_INTEGER;
+ }
+ }
+
+ int adjust_real (int t)
+ {
+ string s = new String (number_builder, 0, number_pos);
+ const string error_details = "Floating-point constant is outside the range of type `{0}'";
+
+ switch (t){
+ case Token.LITERAL_DECIMAL:
+ try {
+ val = System.Decimal.Parse (s, styles, csharp_format_info);
+ } catch (OverflowException) {
+ val = 0m;
+ Report.Error (594, Location, error_details, "decimal");
+ }
+ break;
+ case Token.LITERAL_FLOAT:
+ try {
+ val = float.Parse (s, styles, csharp_format_info);
+ } catch (OverflowException) {
+ val = 0.0f;
+ Report.Error (594, Location, error_details, "float");
+ }
+ break;
+
+ case Token.LITERAL_DOUBLE:
+ case Token.NONE:
+ t = Token.LITERAL_DOUBLE;
+ try {
+ val = System.Double.Parse (s, styles, csharp_format_info);
+ } catch (OverflowException) {
+ val = 0.0;
+ Report.Error (594, Location, error_details, "double");
+ }
+ break;
+ }
+ return t;
+ }
+
+ int handle_hex ()
+ {
+ int d;
+ ulong ul;
+
+ getChar ();
+ while ((d = peekChar ()) != -1){
+ if (is_hex (d)){
+ number_builder [number_pos++] = (char) d;
+ getChar ();
+ } else
+ break;
+ }
+
+ string s = new String (number_builder, 0, number_pos);
+ try {
+ if (number_pos <= 8)
+ ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
+ else
+ ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
+ } catch (OverflowException){
+ error_details = "Integral constant is too large";
+ Report.Error (1021, Location, error_details);
+ val = 0ul;
+ return Token.LITERAL_INTEGER;
+ }
+ catch (FormatException) {
+ Report.Error (1013, Location, "Invalid number");
+ val = 0ul;
+ return Token.LITERAL_INTEGER;
+ }
+
+ return integer_type_suffix (ul, peekChar ());
+ }
+
+ //
+ // Invoked if we know we have .digits or digits
+ //
+ int is_number (int c)
+ {
+ bool is_real = false;
+ int type;
+
+ number_pos = 0;
+
+ if (c >= '0' && c <= '9'){
+ if (c == '0'){
+ int peek = peekChar ();
+
+ if (peek == 'x' || peek == 'X')
+ return handle_hex ();
+ }
+ decimal_digits (c);
+ c = getChar ();
+ }
+
+ //
+ // We need to handle the case of
+ // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
+ //
+ if (c == '.'){
+ if (decimal_digits ('.')){
+ is_real = true;
+ c = getChar ();
+ } else {
+ putback ('.');
+ number_pos--;
+ return adjust_int (-1);
+ }
+ }
+
+ if (c == 'e' || c == 'E'){
+ is_real = true;
+ if (number_pos == max_number_size)
+ Error_NumericConstantTooLong ();
+ number_builder [number_pos++] = 'e';
+ c = getChar ();
+
+ if (c == '+'){
+ if (number_pos == max_number_size)
+ Error_NumericConstantTooLong ();
+ number_builder [number_pos++] = '+';
+ c = -1;
+ } else if (c == '-') {
+ if (number_pos == max_number_size)
+ Error_NumericConstantTooLong ();
+ number_builder [number_pos++] = '-';
+ c = -1;
+ } else {
+ if (number_pos == max_number_size)
+ Error_NumericConstantTooLong ();
+ number_builder [number_pos++] = '+';
+ }
+
+ decimal_digits (c);
+ c = getChar ();
+ }
+
+ type = real_type_suffix (c);
+ if (type == Token.NONE && !is_real){
+ putback (c);
+ return adjust_int (c);
+ } else
+ is_real = true;
+
+ if (type == Token.NONE){
+ putback (c);
+ }
+
+ if (is_real)
+ return adjust_real (type);
+
+ Console.WriteLine ("This should not be reached");
+ throw new Exception ("Is Number should never reach this point");
+ }
+
+ //
+ // Accepts exactly count (4 or 8) hex, no more no less
+ //
+ int getHex (int count, out bool error)
+ {
+ int i;
+ int total = 0;
+ int c;
+ int top = count != -1 ? count : 4;
+
+ getChar ();
+ error = false;
+ for (i = 0; i < top; i++){
+ c = getChar ();
+
+ if (c >= '0' && c <= '9')
+ c = (int) c - (int) '0';
+ else if (c >= 'A' && c <= 'F')
+ c = (int) c - (int) 'A' + 10;
+ else if (c >= 'a' && c <= 'f')
+ c = (int) c - (int) 'a' + 10;
+ else {
+ error = true;
+ return 0;
+ }
+
+ total = (total * 16) + c;
+ if (count == -1){
+ int p = peekChar ();
+ if (p == -1)
+ break;
+ if (!is_hex ((char)p))
+ break;
+ }
+ }
+ return total;
+ }
+
+ int escape (int c)
+ {
+ bool error;
+ int d;
+ int v;
+
+ d = peekChar ();
+ if (c != '\\')
+ return c;
+
+ switch (d){
+ case 'a':
+ v = '\a'; break;
+ case 'b':
+ v = '\b'; break;
+ case 'n':
+ v = '\n'; break;
+ case 't':
+ v = '\t'; break;
+ case 'v':
+ v = '\v'; break;
+ case 'r':
+ v = '\r'; break;
+ case '\\':
+ v = '\\'; break;
+ case 'f':
+ v = '\f'; break;
+ case '0':
+ v = 0; break;
+ case '"':
+ v = '"'; break;
+ case '\'':
+ v = '\''; break;
+ case 'x':
+ v = getHex (-1, out error);
+ if (error)
+ goto default;
+ return v;
+ case 'u':
+ v = getHex (4, out error);
+ if (error)
+ goto default;
+ return v;
+ case 'U':
+ v = getHex (8, out error);
+ if (error)
+ goto default;
+ return v;
+ default:
+ Report.Error (1009, Location, "Unrecognized escape sequence `\\{0}'", ((char)d).ToString ());
+ return d;
+ }
+ getChar ();
+ return v;
+ }
+
+ int getChar ()
+ {
+ int x;
+ if (putback_char != -1) {
+ x = putback_char;
+ putback_char = -1;
+ } else
+ x = reader.Read ();
+ if (x == '\n') {
+ line++;
+ ref_line++;
+ previous_col = col;
+ col = 0;
+ }
+ else
+ col++;
+ return x;
+ }
+
+ int peekChar ()
+ {
+ if (putback_char != -1)
+ return putback_char;
+ putback_char = reader.Read ();
+ return putback_char;
+ }
+
+ int peekChar2 ()
+ {
+ if (putback_char != -1)
+ return putback_char;
+ return reader.Peek ();
+ }
+
+ void putback (int c)
+ {
+ if (putback_char != -1){
+ Console.WriteLine ("Col: " + col);
+ Console.WriteLine ("Row: " + line);
+ Console.WriteLine ("Name: " + ref_name.Name);
+ Console.WriteLine ("Current [{0}] putting back [{1}] ", putback_char, c);
+ throw new Exception ("This should not happen putback on putback");
+ }
+ if (c == '\n' || col == 0) {
+ // It won't happen though.
+ line--;
+ ref_line--;
+ col = previous_col;
+ }
+ else
+ col--;
+ putback_char = c;
+ }
+
+ public bool advance ()
+ {
+ return peekChar () != -1;
+ }
+
+ public Object Value {
+ get {
+ return val;
+ }
+ }
+
+ public Object value ()
+ {
+ return val;
+ }
+
+ static bool IsCastToken (int token)
+ {
+ switch (token) {
+ case Token.BANG:
+ case Token.TILDE:
+ case Token.IDENTIFIER:
+ case Token.LITERAL_INTEGER:
+ case Token.LITERAL_FLOAT:
+ case Token.LITERAL_DOUBLE:
+ case Token.LITERAL_DECIMAL:
+ case Token.LITERAL_CHARACTER:
+ case Token.LITERAL_STRING:
+ case Token.BASE:
+ case Token.CHECKED:
+ case Token.FALSE:
+ case Token.FIXED:
+ case Token.NEW:
+ case Token.NULL:
+ case Token.SIZEOF:
+ case Token.THIS:
+ case Token.THROW:
+ case Token.TRUE:
+ case Token.TYPEOF:
+ case Token.UNCHECKED:
+ case Token.UNSAFE:
+
+ //
+ // These can be part of a member access
+ //
+ case Token.INT:
+ case Token.UINT:
+ case Token.SHORT:
+ case Token.USHORT:
+ case Token.LONG:
+ case Token.ULONG:
+ case Token.DOUBLE:
+ case Token.FLOAT:
+ case Token.CHAR:
+ return true;
+
+ default:
+ return false;
+ }
+ }
+
+ public int token ()
+ {
+ current_token = xtoken ();
+
+ if (current_token != Token.DEFAULT)
+ return current_token;
+
+ int c = consume_whitespace ();
+ if (c == -1)
+ current_token = Token.ERROR;
+ else if (c == '(')
+ current_token = Token.DEFAULT_OPEN_PARENS;
+ else
+ putback (c);
+
+ return current_token;
+ }
+
+ static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
+
+ void get_cmd_arg (out string cmd, out string arg)
+ {
+ int c;
+
+ tokens_seen = false;
+ arg = "";
+ static_cmd_arg.Length = 0;
+
+ // skip over white space
+ while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
+ ;
+
+ while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
+ if (is_identifier_part_character ((char) c)){
+ static_cmd_arg.Append ((char) c);
+ c = getChar ();
+ } else {
+ putback (c);
+ break;
+ }
+ }
+
+ cmd = static_cmd_arg.ToString ();
+
+ if (c == '\n'){
+ return;
+ }
+
+ // skip over white space
+ while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
+ ;
+
+ if (c == '\n'){
+ return;
+ } else if (c == '\r'){
+ return;
+ } else if (c == -1){
+ arg = "";
+ return;
+ }
+
+ static_cmd_arg.Length = 0;
+ static_cmd_arg.Append ((char) c);
+
+ while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
+ static_cmd_arg.Append ((char) c);
+ }
+
+ arg = static_cmd_arg.ToString ().Trim ();
+ }
+
+ //
+ // Handles the #line directive
+ //
+ bool PreProcessLine (string arg)
+ {
+ if (arg.Length == 0)
+ return false;
+
+ if (arg == "default"){
+ ref_line = line;
+ ref_name = file_name;
+ Location.Push (ref_name);
+ return true;
+ } else if (arg == "hidden"){
+ //
+ // We ignore #line hidden
+ //
+ return true;
+ }
+
+ try {
+ int pos;
+
+ if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
+ ref_line = System.Int32.Parse (arg.Substring (0, pos));
+ pos++;
+
+ char [] quotes = { '\"' };
+
+ string name = arg.Substring (pos). Trim (quotes);
+ ref_name = Location.LookupFile (name);
+ file_name.HasLineDirective = true;
+ ref_name.HasLineDirective = true;
+ Location.Push (ref_name);
+ } else {
+ ref_line = System.Int32.Parse (arg);
+ }
+ } catch {
+ return false;
+ }
+
+ return true;
+ }
+
+ //
+ // Handles #define and #undef
+ //
+ void PreProcessDefinition (bool is_define, string arg)
+ {
+ if (arg.Length == 0 || arg == "true" || arg == "false"){
+ Report.Error (1001, Location, "Missing identifer to pre-processor directive");
+ return;
+ }
+
+ char[] whitespace = { ' ', '\t' };
+ if (arg.IndexOfAny (whitespace) != -1){
+ Report.Error (1025, Location, "Single-line comment or end-of-line expected");
+ return;
+ }
+
+ if (!is_identifier_start_character (arg [0]))
+ Report.Error (1001, Location, "Identifier expected: " + arg);
+
+ foreach (char c in arg.Substring (1)){
+ if (!is_identifier_part_character (c)){
+ Report.Error (1001, Location, "Identifier expected: " + arg);
+ return;
+ }
+ }
+
+ if (is_define){
+ if (defines == null)
+ defines = new Hashtable ();
+ define (arg);
+ } else {
+ if (defines == null)
+ return;
+ if (defines.Contains (arg))
+ defines.Remove (arg);
+ }
+ }
+
+ /// <summary>
+ /// Handles #pragma directive
+ /// </summary>
+ void PreProcessPragma (string arg)
+ {
+ const string warning = "warning";
+ const string w_disable = "warning disable";
+ const string w_restore = "warning restore";
+
+ if (arg == w_disable) {
+ Report.RegisterWarningRegion (Location).WarningDisable (line);
+ return;
+ }
+
+ if (arg == w_restore) {
+ Report.RegisterWarningRegion (Location).WarningEnable (line);
+ return;
+ }
+
+ if (arg.StartsWith (w_disable)) {
+ int[] codes = ParseNumbers (arg.Substring (w_disable.Length));
+ foreach (int code in codes) {
+ if (code != 0)
+ Report.RegisterWarningRegion (Location).WarningDisable (Location, code);
+ }
+ return;
+ }
+
+ if (arg.StartsWith (w_restore)) {
+ int[] codes = ParseNumbers (arg.Substring (w_restore.Length));
+ Hashtable w_table = Report.warning_ignore_table;
+ foreach (int code in codes) {
+ if (w_table != null && w_table.Contains (code))
+ Report.Warning (1635, 1, Location, String.Format ("Cannot restore warning `CS{0:0000}' because it was disabled globally", code));
+ Report.RegisterWarningRegion (Location).WarningEnable (Location, code);
+ }
+ return;
+ }
+
+ if (arg.StartsWith (warning)) {
+ Report.Warning (1634, 1, Location, "Expected disable or restore");
+ return;
+ }
+
+ Report.Warning (1633, 1, Location, "Unrecognized #pragma directive");
+ }
+
+ int[] ParseNumbers (string text)
+ {
+ string[] string_array = text.Split (',');
+ int[] values = new int [string_array.Length];
+ int index = 0;
+ foreach (string string_code in string_array) {
+ try {
+ values[index++] = int.Parse (string_code, System.Globalization.CultureInfo.InvariantCulture);
+ }
+ catch (FormatException) {
+ Report.Warning (1692, 1, Location, "Invalid number");
+ }
+ }
+ return values;
+ }
+
+ bool eval_val (string s)
+ {
+ if (s == "true")
+ return true;
+ if (s == "false")
+ return false;
+
+ if (defines == null)
+ return false;
+ if (defines.Contains (s))
+ return true;
+
+ return false;
+ }
+
+ bool pp_primary (ref string s)
+ {
+ s = s.Trim ();
+ int len = s.Length;
+
+ if (len > 0){
+ char c = s [0];
+
+ if (c == '('){
+ s = s.Substring (1);
+ bool val = pp_expr (ref s);
+ if (s.Length > 0 && s [0] == ')'){
+ s = s.Substring (1);
+ return val;
+ }
+ Error_InvalidDirective ();
+ return false;
+ }
+
+ if (is_identifier_start_character (c)){
+ int j = 1;
+
+ while (j < len){
+ c = s [j];
+
+ if (is_identifier_part_character (c)){
+ j++;
+ continue;
+ }
+ bool v = eval_val (s.Substring (0, j));
+ s = s.Substring (j);
+ return v;
+ }
+ bool vv = eval_val (s);
+ s = "";
+ return vv;
+ }
+ }
+ Error_InvalidDirective ();
+ return false;
+ }
+
+ bool pp_unary (ref string s)
+ {
+ s = s.Trim ();
+ int len = s.Length;
+
+ if (len > 0){
+ if (s [0] == '!'){
+ if (len > 1 && s [1] == '='){
+ Error_InvalidDirective ();
+ return false;
+ }
+ s = s.Substring (1);
+ return ! pp_primary (ref s);
+ } else
+ return pp_primary (ref s);
+ } else {
+ Error_InvalidDirective ();
+ return false;
+ }
+ }
+
+ bool pp_eq (ref string s)
+ {
+ bool va = pp_unary (ref s);
+
+ s = s.Trim ();
+ int len = s.Length;
+ if (len > 0){
+ if (s [0] == '='){
+ if (len > 2 && s [1] == '='){
+ s = s.Substring (2);
+ return va == pp_unary (ref s);
+ } else {
+ Error_InvalidDirective ();
+ return false;
+ }
+ } else if (s [0] == '!' && len > 1 && s [1] == '='){
+ s = s.Substring (2);
+
+ return va != pp_unary (ref s);
+
+ }
+ }
+
+ return va;
+
+ }
+
+ bool pp_and (ref string s)
+ {
+ bool va = pp_eq (ref s);
+
+ s = s.Trim ();
+ int len = s.Length;
+ if (len > 0){
+ if (s [0] == '&'){
+ if (len > 2 && s [1] == '&'){
+ s = s.Substring (2);
+ return (va & pp_and (ref s));
+ } else {
+ Error_InvalidDirective ();
+ return false;
+ }
+ }
+ }
+ return va;
+ }
+
+ //
+ // Evaluates an expression for `#if' or `#elif'
+ //
+ bool pp_expr (ref string s)
+ {
+ bool va = pp_and (ref s);
+ s = s.Trim ();
+ int len = s.Length;
+ if (len > 0){
+ char c = s [0];
+
+ if (c == '|'){
+ if (len > 2 && s [1] == '|'){
+ s = s.Substring (2);
+ return va | pp_expr (ref s);
+ } else {
+ Error_InvalidDirective ();
+ return false;
+ }
+ }
+ }
+
+ return va;
+ }
+
+ bool eval (string s)
+ {
+ bool v = pp_expr (ref s);
+ s = s.Trim ();
+ if (s.Length != 0){
+ Error_InvalidDirective ();
+ return false;
+ }
+
+ return v;
+ }
+
+ void Error_InvalidDirective ()
+ {
+ Report.Error (1517, Location, "Invalid preprocessor directive");
+ }
+
+ void Error_UnexpectedDirective (string extra)
+ {
+ Report.Error (
+ 1028, Location,
+ "Unexpected processor directive (" + extra + ")");
+ }
+
+ void Error_TokensSeen ()
+ {
+ Report.Error (1032, Location,
+ "Cannot define or undefine preprocessor symbols after first token in file");
+ }
+
+ //
+ // if true, then the code continues processing the code
+ // if false, the code stays in a loop until another directive is
+ // reached.
+ //
+ bool handle_preprocessing_directive (bool caller_is_taking)
+ {
+ string cmd, arg;
+ bool region_directive = false;
+
+ current_location = new Location (ref_line, Col);
+
+ get_cmd_arg (out cmd, out arg);
+
+ // Eat any trailing whitespaces and single-line comments
+ if (arg.IndexOf ("//") != -1)
+ arg = arg.Substring (0, arg.IndexOf ("//"));
+ arg = arg.TrimEnd (' ', '\t');
+
+ //
+ // The first group of pre-processing instructions is always processed
+ //
+ switch (cmd){
+ case "pragma":
+ if (RootContext.Version == LanguageVersion.ISO_1) {
+ Report.FeatureIsNotStandardized (Location, "#pragma");
+ return caller_is_taking;
+ }
+
+ PreProcessPragma (arg);
+ return caller_is_taking;
+
+ case "line":
+ if (!PreProcessLine (arg))
+ Report.Error (
+ 1576, Location,
+ "The line number specified for #line directive is missing or invalid");
+ return caller_is_taking;
+
+ case "region":
+ region_directive = true;
+ arg = "true";
+ goto case "if";
+
+ case "endregion":
+ region_directive = true;
+ goto case "endif";
+
+ case "if":
+ if (arg.Length == 0){
+ Error_InvalidDirective ();
+ return true;
+ }
+ bool taking = false;
+ if (ifstack == null)
+ ifstack = new Stack (2);
+
+ if (ifstack.Count == 0){
+ taking = true;
+ } else {
+ int state = (int) ifstack.Peek ();
+ if ((state & TAKING) != 0)
+ taking = true;
+ }
+
+ if (eval (arg) && taking){
+ int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
+ if (region_directive)
+ push |= REGION;
+ ifstack.Push (push);
+ return true;
+ } else {
+ int push = (taking ? PARENT_TAKING : 0);
+ if (region_directive)
+ push |= REGION;
+ ifstack.Push (push);
+ return false;
+ }
+
+ case "endif":
+ if (ifstack == null || ifstack.Count == 0){
+ Error_UnexpectedDirective ("no #if for this #endif");
+ return true;
+ } else {
+ int pop = (int) ifstack.Pop ();
+
+ if (region_directive && ((pop & REGION) == 0))
+ Report.Error (1027, Location, "Expected `#endif' directive");
+ else if (!region_directive && ((pop & REGION) != 0))
+ Report.Error (1038, Location, "#endregion directive expected");
+
+ if (!region_directive && arg.Length != 0) {
+ Report.Error (1025, Location, "Single-line comment or end-of-line expected");
+ }
+
+ if (ifstack.Count == 0)
+ return true;
+ else {
+ int state = (int) ifstack.Peek ();
+
+ if ((state & TAKING) != 0)
+ return true;
+ else
+ return false;
+ }
+ }
+
+ case "elif":
+ if (ifstack == null || ifstack.Count == 0){
+ Error_UnexpectedDirective ("no #if for this #elif");
+ return true;
+ } else {
+ int state = (int) ifstack.Peek ();
+
+ if ((state & REGION) != 0) {
+ Report.Error (1038, Location, "#endregion directive expected");
+ return true;
+ }
+
+ if ((state & ELSE_SEEN) != 0){
+ Error_UnexpectedDirective ("#elif not valid after #else");
+ return true;
+ }
+
+ if ((state & (TAKEN_BEFORE | TAKING)) != 0)
+ return false;
+
+ if (eval (arg) && ((state & PARENT_TAKING) != 0)){
+ state = (int) ifstack.Pop ();
+ ifstack.Push (state | TAKING | TAKEN_BEFORE);
+ return true;
+ } else
+ return false;
+ }
+
+ case "else":
+ if (ifstack == null || ifstack.Count == 0){
+ Error_UnexpectedDirective ("no #if for this #else");
+ return true;
+ } else {
+ int state = (int) ifstack.Peek ();
+
+ if ((state & REGION) != 0) {
+ Report.Error (1038, Location, "#endregion directive expected");
+ return true;
+ }
+
+ if ((state & ELSE_SEEN) != 0){
+ Error_UnexpectedDirective ("#else within #else");
+ return true;
+ }
+
+ ifstack.Pop ();
+
+ bool ret;
+ if ((state & TAKEN_BEFORE) == 0){
+ ret = ((state & PARENT_TAKING) != 0);
+ } else
+ ret = false;
+
+ if (ret)
+ state |= TAKING;
+ else
+ state &= ~TAKING;
+
+ ifstack.Push (state | ELSE_SEEN);
+
+ return ret;
+ }
+ }
+
+ //
+ // These are only processed if we are in a `taking' block
+ //
+ if (!caller_is_taking)
+ return false;
+
+ switch (cmd){
+ case "define":
+ if (any_token_seen){
+ Error_TokensSeen ();
+ return true;
+ }
+ PreProcessDefinition (true, arg);
+ return true;
+
+ case "undef":
+ if (any_token_seen){
+ Error_TokensSeen ();
+ return true;
+ }
+ PreProcessDefinition (false, arg);
+ return true;
+
+ case "error":
+ Report.Error (1029, Location, "#error: '" + arg + "'");
+ return true;
+
+ case "warning":
+ Report.Warning (1030, 1, Location, "#warning: `{0}'", arg);
+ return true;
+ }
+
+ Report.Error (1024, Location, "Wrong preprocessor directive");
+ return true;
+
+ }
+
+ private int consume_string (bool quoted)
+ {
+ int c;
+ string_builder.Length = 0;
+
+ while ((c = getChar ()) != -1){
+ if (c == '"'){
+ if (quoted && peekChar () == '"'){
+ string_builder.Append ((char) c);
+ getChar ();
+ continue;
+ } else {
+ val = string_builder.ToString ();
+ return Token.LITERAL_STRING;
+ }
+ }
+
+ if (c == '\n'){
+ if (!quoted)
+ Report.Error (1010, Location, "Newline in constant");
+ }
+
+ if (!quoted){
+ c = escape (c);
+ if (c == -1)
+ return Token.ERROR;
+ }
+ string_builder.Append ((char) c);
+ }
+
+ Report.Error (1039, Location, "Unterminated string literal");
+ return Token.EOF;
+ }
+
+ private int consume_identifier (int s)
+ {
+ int res = consume_identifier (s, false);
+
+ if (doc_state == XmlCommentState.Allowed)
+ doc_state = XmlCommentState.NotAllowed;
+ switch (res) {
+ case Token.USING:
+ case Token.NAMESPACE:
+ check_incorrect_doc_comment ();
+ break;
+ }
+
+ if (res == Token.PARTIAL) {
+ // Save current position and parse next token.
+ PushPosition ();
+
+ int next_token = token ();
+ bool ok = (next_token == Token.CLASS) ||
+ (next_token == Token.STRUCT) ||
+ (next_token == Token.INTERFACE) ||
+ (next_token == Token.ENUM); // "partial" is a keyword in 'partial enum', even though it's not valid
+
+ PopPosition ();
+
+ if (ok)
+ return res;
+ else {
+ val = new LocatedToken (Location, "partial");
+ return Token.IDENTIFIER;
+ }
+ }
+
+ return res;
+ }
+
+ private int consume_identifier (int s, bool quoted)
+ {
+ int pos = 1;
+ int c = -1;
+
+ id_builder [0] = (char) s;
+
+ current_location = new Location (ref_line, Col);
+
+ while ((c = getChar ()) != -1) {
+ loop:
+ if (is_identifier_part_character ((char) c)){
+ if (pos == max_id_size){
+ Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
+ return Token.ERROR;
+ }
+
+ id_builder [pos++] = (char) c;
+// putback_char = -1;
+ } else if (c == '\\') {
+ c = escape (c);
+ goto loop;
+ } else {
+// putback_char = c;
+ putback (c);
+ break;
+ }
+ }
+
+ //
+ // Optimization: avoids doing the keyword lookup
+ // on uppercase letters and _
+ //
+ if (!quoted && (s >= 'a' || s == '_')){
+ int keyword = GetKeyword (id_builder, pos);
+ if (keyword != -1) {
+ val = Location;
+ return keyword;
+ }
+ }
+
+ //
+ // Keep identifiers in an array of hashtables to avoid needless
+ // allocations
+ //
+
+ if (identifiers [pos] != null) {
+ val = identifiers [pos][id_builder];
+ if (val != null) {
+ val = new LocatedToken (Location, (string) val);
+ if (quoted)
+ escapedIdentifiers.Add (val);
+ return Token.IDENTIFIER;
+ }
+ }
+ else
+ identifiers [pos] = new CharArrayHashtable (pos);
+
+ val = new String (id_builder, 0, pos);
+ if (RootContext.Version == LanguageVersion.ISO_1) {
+ for (int i = 1; i < id_builder.Length; i += 3) {
+ if (id_builder [i] == '_' && (id_builder [i - 1] == '_' || id_builder [i + 1] == '_')) {
+ Report.Error (1638, Location,
+ "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val.ToString ());
+ break;
+ }
+ }
+ }
+
+ char [] chars = new char [pos];
+ Array.Copy (id_builder, chars, pos);
+
+ identifiers [pos] [chars] = val;
+
+ val = new LocatedToken (Location, (string) val);
+ if (quoted)
+ escapedIdentifiers.Add (val);
+ return Token.IDENTIFIER;
+ }
+
+ int consume_whitespace ()
+ {
+ int c;
+
+ // Whether we have seen comments on the current line
+ bool comments_seen = false;
+
+ val = null;
+ // optimization: eliminate col and implement #directive semantic correctly.
+ for (;(c = getChar ()) != -1;) {
+ if (c == ' ')
+ continue;
+
+ if (c == '\t') {
+ continue;
+ }
+
+ if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0)
+ continue;
+
+ if (c == '\r') {
+ if (peekChar () == '\n')
+ getChar ();
+
+ any_token_seen |= tokens_seen;
+ tokens_seen = false;
+ comments_seen = false;
+ continue;
+ }
+
+ // Handle double-slash comments.
+ if (c == '/'){
+ int d = peekChar ();
+
+ if (d == '/'){
+ getChar ();
+ if (RootContext.Documentation != null && peekChar () == '/') {
+ getChar ();
+ // Don't allow ////.
+ if ((d = peekChar ()) != '/') {
+ update_comment_location ();
+ if (doc_state == XmlCommentState.Allowed)
+ handle_one_line_xml_comment ();
+ else if (doc_state == XmlCommentState.NotAllowed)
+ warn_incorrect_doc_comment ();
+ }
+ }
+ while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
+ if (d == '\n'){
+ }
+ any_token_seen |= tokens_seen;
+ tokens_seen = false;
+ comments_seen = false;
+ continue;
+ } else if (d == '*'){
+ getChar ();
+ bool docAppend = false;
+ if (RootContext.Documentation != null && peekChar () == '*') {
+ getChar ();
+ update_comment_location ();
+ // But when it is /**/, just do nothing.
+ if (peekChar () == '/') {
+ getChar ();
+ continue;
+ }
+ if (doc_state == XmlCommentState.Allowed)
+ docAppend = true;
+ else if (doc_state == XmlCommentState.NotAllowed)
+ warn_incorrect_doc_comment ();
+ }
+
+ int current_comment_start = 0;
+ if (docAppend) {
+ current_comment_start = xml_comment_buffer.Length;
+ xml_comment_buffer.Append (Environment.NewLine);
+ }
+
+ Location start_location = Location;
+
+ while ((d = getChar ()) != -1){
+ if (d == '*' && peekChar () == '/'){
+ getChar ();
+ comments_seen = true;
+ break;
+ }
+ if (docAppend)
+ xml_comment_buffer.Append ((char) d);
+
+ if (d == '\n'){
+ any_token_seen |= tokens_seen;
+ tokens_seen = false;
+ //
+ // Reset 'comments_seen' just to be consistent.
+ // It doesn't matter either way, here.
+ //
+ comments_seen = false;
+ }
+ }
+ if (!comments_seen)
+ Report.Error (1035, start_location, "End-of-file found, '*/' expected");
+
+ if (docAppend)
+ update_formatted_doc_comment (current_comment_start);
+ continue;
+ }
+ goto is_punct_label;
+ }
+
+ is_punct_label:
+ // white space
+ if (c == '\n'){
+ any_token_seen |= tokens_seen;
+ tokens_seen = false;
+ comments_seen = false;
+ continue;
+ }
+
+ /* For now, ignore pre-processor commands */
+ // FIXME: In C# the '#' is not limited to appear
+ // on the first column.
+ if (c == '#') {
+ bool cont = true;
+
+ if (tokens_seen || comments_seen) {
+ error_details = "Preprocessor directives must appear as the first non-whitespace " +
+ "character on a line.";
+
+ Report.Error (1040, Location, error_details);
+
+ return Token.ERROR;
+ }
+
+ start_again:
+
+ cont = handle_preprocessing_directive (cont);
+
+ if (cont){
+ continue;
+ }
+
+ bool skipping = false;
+ for (;(c = getChar ()) != -1;){
+ if (c == '\n'){
+ skipping = false;
+ } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
+ continue;
+ else if (c != '#')
+ skipping = true;
+ if (c == '#' && !skipping)
+ goto start_again;
+ }
+ any_token_seen |= tokens_seen;
+ tokens_seen = false;
+ if (c == -1)
+ Report.Error (1027, Location, "Expected `#endif' directive");
+ continue;
+ }
+
+ return c;
+ }
+
+ return -1;
+ }
+
+ public int xtoken ()
+ {
+ int t;
+ bool doread = false;
+ int c;
+
+ val = null;
+ // optimization: eliminate col and implement #directive semantic correctly.
+
+ c = consume_whitespace ();
+ if (c == -1)
+ return Token.EOF;
+
+ if (c == '\\' || is_identifier_start_character ((char)c)){
+ tokens_seen = true;
+ return consume_identifier (c);
+ }
+
+ current_location = new Location (ref_line, Col);
+ if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
+ tokens_seen = true;
+ if (doread){
+ getChar ();
+ col++;
+ }
+ return t;
+ }
+
+ if (c >= '0' && c <= '9'){
+ tokens_seen = true;
+ return is_number (c);
+ }
+
+ if (c == '.'){
+ tokens_seen = true;
+ int peek = peekChar ();
+ if (peek >= '0' && peek <= '9')
+ return is_number (c);
+ return Token.DOT;
+ }
+
+ if (c == '"')
+ return consume_string (false);
+
+ if (c == '\''){
+ c = getChar ();
+ tokens_seen = true;
+ if (c == '\''){
+ error_details = "Empty character literal";
+ Report.Error (1011, Location, error_details);
+ return Token.ERROR;
+ }
+ if (c == '\r' || c == '\n') {
+ Report.Error (1010, Location, "Newline in constant");
+ return Token.ERROR;
+ }
+ c = escape (c);
+ if (c == -1)
+ return Token.ERROR;
+ val = new System.Char ();
+ val = (char) c;
+ c = getChar ();
+
+ if (c != '\''){
+ error_details = "Too many characters in character literal";
+ Report.Error (1012, Location, error_details);
+
+ // Try to recover, read until newline or next "'"
+ while ((c = getChar ()) != -1){
+ if (c == '\n'){
+ break;
+ }
+ else if (c == '\'')
+ break;
+ }
+ return Token.ERROR;
+ }
+ return Token.LITERAL_CHARACTER;
+ }
+
+ if (c == '@') {
+ c = getChar ();
+ if (c == '"') {
+ tokens_seen = true;
+ return consume_string (true);
+ } else if (is_identifier_start_character ((char) c)){
+ return consume_identifier (c, true);
+ } else {
+ Report.Error (1646, Location, "Keyword, identifier, or string expected after verbatim specifier: @");
+ }
+ }
+
+ if (c == '#') {
+ error_details = "Preprocessor directives must appear as the first non-whitespace " +
+ "character on a line.";
+
+ Report.Error (1040, Location, error_details);
+
+ return Token.ERROR;
+ }
+
+ error_details = ((char)c).ToString ();
+
+ return Token.ERROR;
+ }
+
+ //
+ // Handles one line xml comment
+ //
+ private void handle_one_line_xml_comment ()
+ {
+ int c;
+ while ((c = peekChar ()) == ' ')
+ getChar (); // skip heading whitespaces.
+ while ((c = peekChar ()) != -1 && c != '\n' && c != '\r') {
+ xml_comment_buffer.Append ((char) getChar ());
+ }
+ if (c == '\r' || c == '\n')
+ xml_comment_buffer.Append (Environment.NewLine);
+ }
+
+ //
+ // Remove heading "*" in Javadoc-like xml documentation.
+ //
+ private void update_formatted_doc_comment (int current_comment_start)
+ {
+ int length = xml_comment_buffer.Length - current_comment_start;
+ string [] lines = xml_comment_buffer.ToString (
+ current_comment_start,
+ length).Replace ("\r", "").Split ('\n');
+
+ // The first line starts with /**, thus it is not target
+ // for the format check.
+ for (int i = 1; i < lines.Length; i++) {
+ string s = lines [i];
+ int idx = s.IndexOf ('*');
+ string head = null;
+ if (idx < 0) {
+ if (i < lines.Length - 1)
+ return;
+ head = s;
+ } else
+ head = s.Substring (0, idx);
+ foreach (char c in head)
+ if (c != ' ')
+ return;
+ lines [i] = s.Substring (idx + 1);
+ }
+ xml_comment_buffer.Remove (current_comment_start, length);
+ xml_comment_buffer.Insert (current_comment_start, String.Join (Environment.NewLine, lines));
+ }
+
+ //
+ // Updates current comment location.
+ //
+ private void update_comment_location ()
+ {
+ if (current_comment_location.IsNull) {
+ // "-2" is for heading "//" or "/*"
+ current_comment_location =
+ new Location (ref_line, col - 2);
+ }
+ }
+
+ //
+ // Checks if there was incorrect doc comments and raise
+ // warnings.
+ //
+ public void check_incorrect_doc_comment ()
+ {
+ if (xml_comment_buffer.Length > 0)
+ warn_incorrect_doc_comment ();
+ }
+
+ //
+ // Raises a warning when tokenizer found incorrect doccomment
+ // markup.
+ //
+ private void warn_incorrect_doc_comment ()
+ {
+ if (doc_state != XmlCommentState.Error) {
+ doc_state = XmlCommentState.Error;
+ // in csc, it is 'XML comment is not placed on
+ // a valid language element'. But that does not
+ // make sense.
+ Report.Warning (1587, 2, Location, "XML comment is not placed on a valid language element");
+ }
+ }
+
+ //
+ // Consumes the saved xml comment lines (if any)
+ // as for current target member or type.
+ //
+ public string consume_doc_comment ()
+ {
+ if (xml_comment_buffer.Length > 0) {
+ string ret = xml_comment_buffer.ToString ();
+ reset_doc_comment ();
+ return ret;
+ }
+ return null;
+ }
+
+ void reset_doc_comment ()
+ {
+ xml_comment_buffer.Length = 0;
+ current_comment_location = Location.Null;
+ }
+
+ public void cleanup ()
+ {
+ if (ifstack != null && ifstack.Count >= 1) {
+ int state = (int) ifstack.Pop ();
+ if ((state & REGION) != 0)
+ Report.Error (1038, Location, "#endregion directive expected");
+ else
+ Report.Error (1027, Location, "Expected `#endif' directive");
+ }
+
+ }
+ }
+
+ //
+ // Indicates whether it accepts XML documentation or not.
+ //
+ public enum XmlCommentState {
+ // comment is allowed in this state.
+ Allowed,
+ // comment is not allowed in this state.
+ NotAllowed,
+ // once comments appeared when it is NotAllowed, then the
+ // state is changed to it, until the state is changed to
+ // .Allowed.
+ Error
+ }
+}
+