示例#1
0
文件: Lexer.cs 项目: mwh/kernan
        private UnicodeCategory validateChar()
        {
            char c = code[index];

            if (c >= 0xD800 && c <= 0xDBFF)
            {
                // Leading surrogate
                char c2 = code[index + 1];
                if (c2 >= 0xDC00 && c2 <= 0xDFFF)
                {
                    // Trailing surrogate - ignore for now
                }
                else
                {
                    reportError("L0007", "Illegal lone surrogate");
                }
            }
            else if (c >= 0xDC00 && c <= 0xDFFF)
            {
                // Trailing surrogate
                reportError("L0007", "Illegal lone surrogate");
            }
            UnicodeCategory cat = UnicodeLookup.GetUnicodeCategory(code, index);

            if (c == '\t')
            {
                reportError("L0001", "Tab characters are not permitted.");
            }
            if (cat == UnicodeCategory.ParagraphSeparator ||
                cat == UnicodeCategory.SpaceSeparator)
            {
                if (c != ' ' && c != '\u2028')
                {
                    reportError("L0002", new Dictionary <string, string>()
                    {
                        { "codepoint", formatCodepoint(Char.ConvertToUtf32(code, index)) },
                        { "name", UnicodeLookup.GetCodepointName(Char.ConvertToUtf32(code, index)) }
                    },
                                "Illegal whitespace.");
                }
            }
            else if ((cat == UnicodeCategory.Control ||
                      cat == UnicodeCategory.Format ||
                      cat == UnicodeCategory.Surrogate
                      ) &&
                     c != '\n' && c != '\r')
            {
                reportError("L0003", new Dictionary <string, string>()
                {
                    { "codepoint", formatCodepoint(Char.ConvertToUtf32(code, index)) },
                    { "name", UnicodeLookup.GetCodepointName(Char.ConvertToUtf32(code, index)) }
                },
                            "Illegal control character. ");
            }
            return(cat);
        }
示例#2
0
文件: Lexer.cs 项目: mwh/kernan
        /// <summary>
        /// Check whether a given string is an identifier.
        /// </summary>
        /// <param name="s">String to check</param>
        public static bool IsIdentifier(string s)
        {
            var first = StringInfo.GetNextTextElement(s);
            var cat   = UnicodeLookup.GetUnicodeCategory(s, 0);

            if (!isIdentifierStartCharacter(first[0], cat))
            {
                return(false);
            }
            var en = StringInfo.GetTextElementEnumerator(s);

            while (en.MoveNext())
            {
                var el = (string)en.Current;
                cat = UnicodeLookup.GetUnicodeCategory(el, 0);
                if (!isIdentifierCharacter(el[0], cat))
                {
                    return(false);
                }
            }
            return(true);
        }