コード例 #1
0
ファイル: Identifier.cs プロジェクト: resonancellc/nquery
        private static Identifier InternalFromSource([SuppressMessage("Microsoft.Globalization", "CA1303")] string text, bool allowInvalid)
        {
            if (text == null)
            {
                throw ExceptionBuilder.ArgumentNull("text");
            }

            if (text.Length == 0 || (text[0] != '"' && text[0] != '['))
            {
                return(CreateNonVerbatim(text));
            }

            const char      EOF = '\0';
            IdentifierFlags flags;
            char            endChar;
            string          unterminatedMessage;

            if (text[0] == '[')
            {
                endChar             = ']';
                unterminatedMessage = Resources.UnterminatedParenthesizedIdentifier;
                flags = IdentifierFlags.Parenthesized;
            }
            else
            {
                endChar             = '"';
                unterminatedMessage = Resources.UnterminatedQuotedIdentifier;
                flags = IdentifierFlags.Verbatim;
            }

            StringBuilder sb  = new StringBuilder();
            int           pos = 1;

            while (true)
            {
                char c = pos < text.Length ? text[pos] : EOF;
                char l = pos < text.Length - 1 ? text[pos + 1] : EOF;

                if (c == EOF)
                {
                    if (allowInvalid)
                    {
                        break;
                    }
                    else
                    {
                        throw ExceptionBuilder.ArgumentInvalidIdentifier("text", String.Format(CultureInfo.CurrentCulture, unterminatedMessage, text));
                    }
                }
                else if (c == endChar)
                {
                    if (l == endChar)
                    {
                        pos++;
                    }
                    else
                    {
                        break;
                    }
                }

                sb.Append(c);
                pos++;
            }

            if (!allowInvalid && pos < text.Length - 1)
            {
                throw ExceptionBuilder.ArgumentInvalidIdentifier("text", String.Format(CultureInfo.CurrentCulture, Resources.InvalidIdentifier, text));
            }

            return(new Identifier(sb.ToString(), flags));
        }