Exemple #1
0
        // <summary>
        // Initializes identifier.
        // </summary>
        internal Identifier(string name, bool isEscaped, string query, int inputPos)
            : base(query, inputPos)
        {
            // name may be empty in the case of "byte[]".
            // "byte" and "[]" come in as two identifiers where second one is escaped and empty.

            Debug.Assert(isEscaped || name[0] != '[', "isEscaped || name[0] != '['");

            if (!isEscaped)
            {
                var isIdentifierASCII = true;
                if (!CqlLexer.IsLetterOrDigitOrUnderscore(name, out isIdentifierASCII))
                {
                    if (isIdentifierASCII)
                    {
                        var errCtx  = ErrCtx;
                        var message = Strings.InvalidSimpleIdentifier(name);
                        throw EntitySqlException.Create(errCtx, message, null);
                    }
                    else
                    {
                        var errCtx  = ErrCtx;
                        var message = Strings.InvalidSimpleIdentifierNonASCII(name);
                        throw EntitySqlException.Create(errCtx, message, null);
                    }
                }
            }

            _name      = name;
            _isEscaped = isEscaped;
        }
Exemple #2
0
        /// <summary>
        /// Parse the specified CQL to get all statements that are inside it.
        /// </summary>
        /// <param name="cql">
        /// The CQL contains multiple statements separated with semi-colon.
        /// </param>
        /// <returns>
        /// List of statements.
        /// </returns>
        /// <exception cref="CqlException">
        /// <paramref name="cql"/> contain invalid CQL.
        /// </exception>
        /// <remarks>
        /// No semi-colon in each result statement.
        /// </remarks>
        public static IEnumerable <string> ParseStatements(string cql)
        {
            var input  = new AntlrInputStream(cql);
            var lexer  = new CqlLexer(new CaseChangingCharStream(input, true));
            var tokens = new CommonTokenStream(lexer);
            var parser = new Parsing.CqlParser(tokens)
            {
                ErrorHandler = new BailErrorStrategy()
            };
            var walker    = new ParseTreeWalker();
            var extractor = new StatementExtractor();

            try
            {
                walker.Walk(extractor, parser.root());
            }
            catch (ParseCanceledException ex)
            {
                var token = (ex.InnerException as RecognitionException)?.OffendingToken;

                if (token == null)
                {
                    throw;
                }

                throw new CqlException(token.Line, token.Column, token.Text, ex);
            }

            return(extractor.Statements);
        }
Exemple #3
0
        /// <summary>
        /// Initializes identifier.
        /// </summary>
        internal Identifier(string name, bool isEscaped, string query, int inputPos) : base(query, inputPos)
        {
            // name may be empty in the case of "byte[]".
            // "byte" and "[]" come in as two identifiers where second one is escaped and empty.

            Debug.Assert(isEscaped || name[0] != '[', "isEscaped || name[0] != '['");

            if (!isEscaped)
            {
                bool isIdentifierASCII = true;
                if (!CqlLexer.IsLetterOrDigitOrUnderscore(name, out isIdentifierASCII))
                {
                    if (isIdentifierASCII)
                    {
                        throw EntityUtil.EntitySqlError(this.ErrCtx, System.Data.Entity.Strings.InvalidSimpleIdentifier(name));
                    }
                    else
                    {
                        throw EntityUtil.EntitySqlError(this.ErrCtx, System.Data.Entity.Strings.InvalidSimpleIdentifierNonASCII(name));
                    }
                }
            }

            _name      = name;
            _isEscaped = isEscaped;
        }
        internal static string FormatErrorContext(
            string commandText,
            int errorPosition,
            string errorContextInfo,
            bool loadErrorContextInfoFromResource,
            out int lineNumber,
            out int columnNumber)
        {
            if (loadErrorContextInfoFromResource)
            {
                errorContextInfo = !string.IsNullOrEmpty(errorContextInfo) ? EntityRes.GetString(errorContextInfo) : string.Empty;
            }
            StringBuilder stringBuilder1 = new StringBuilder(commandText.Length);

            for (int index = 0; index < commandText.Length; ++index)
            {
                char c = commandText[index];
                if (CqlLexer.IsNewLine(c))
                {
                    c = '\n';
                }
                else if ((char.IsControl(c) || char.IsWhiteSpace(c)) && '\r' != c)
                {
                    c = ' ';
                }
                stringBuilder1.Append(c);
            }
            commandText = stringBuilder1.ToString().TrimEnd('\n');
            string[] strArray = commandText.Split(new char[1]
            {
                '\n'
            }, StringSplitOptions.None);
            lineNumber   = 0;
            columnNumber = errorPosition;
            while (lineNumber < strArray.Length && columnNumber > strArray[lineNumber].Length)
            {
                columnNumber -= strArray[lineNumber].Length + 1;
                ++lineNumber;
            }
            ++lineNumber;
            ++columnNumber;
            StringBuilder stringBuilder2 = new StringBuilder();

            if (!string.IsNullOrEmpty(errorContextInfo))
            {
                stringBuilder2.AppendFormat((IFormatProvider)CultureInfo.CurrentCulture, "{0}, ", (object)errorContextInfo);
            }
            if (errorPosition >= 0)
            {
                stringBuilder2.AppendFormat((IFormatProvider)CultureInfo.CurrentCulture, "{0} {1}, {2} {3}", (object)Strings.LocalizedLine, (object)lineNumber, (object)Strings.LocalizedColumn, (object)columnNumber);
            }
            return(stringBuilder2.ToString());
        }
Exemple #5
0
        private IEnumerable <TokenChecker> GetTokens(string source)
        {
            var lexer  = new CqlLexer(source);
            var tokens = new List <TokenChecker>();

            do
            {
                //tokens.Add(new TokenChecker { Token = lexer.CurrentToken, Value = lexer.StringValue });
                tokens.Add(new TokenChecker {
                    Token = lexer.CurrentToken, Value = lexer.StringValue
                });
            }while (lexer.NextToken());

            return(tokens);
        }
Exemple #6
0
 internal Identifier(string name, bool isEscaped, string query, int inputPos)
     : base(query, inputPos)
 {
     if (!isEscaped)
     {
         bool isIdentifierASCII = true;
         if (!CqlLexer.IsLetterOrDigitOrUnderscore(name, out isIdentifierASCII))
         {
             if (isIdentifierASCII)
             {
                 throw EntitySqlException.Create(this.ErrCtx, Strings.InvalidSimpleIdentifier((object)name), (Exception)null);
             }
             throw EntitySqlException.Create(this.ErrCtx, Strings.InvalidSimpleIdentifierNonASCII((object)name), (Exception)null);
         }
     }
     this._name      = name;
     this._isEscaped = isEscaped;
 }
Exemple #7
0
        public void SnQuery_Lexer_CharTypeDoesNotThrow()
        {
            var s        = new String(Enumerable.Range(1, 256 - 32).Select(i => (char)i).ToArray());
            var lexer    = new CqlLexer(s);
            var lexerAcc = new ObjectAccessor(lexer);
            var thrown   = false;

            try
            {
                while ((bool)lexerAcc.Invoke("NextChar"))
                {
                    ;
                }
            }
            catch (Exception) // not logged, not rethrown
            {
                thrown = true;
            }
            Assert.IsFalse(thrown);
        }
Exemple #8
0
 internal static bool IsReservedKeyword(string name)
 {
     return(CqlLexer.IsReservedKeyword(name));
 }
        /// <summary>
        /// Returns error context in the format [[errorContextInfo, ]line ddd, column ddd].
        /// Returns empty string if errorPosition is less than 0 and errorContextInfo is not specified.
        /// </summary>
        internal static string FormatErrorContext(
            string commandText,
            int errorPosition,
            string errorContextInfo,
            bool loadErrorContextInfoFromResource,
            out int lineNumber,
            out int columnNumber)
        {
            Debug.Assert(errorPosition > -1, "position in input stream cannot be < 0");
            Debug.Assert(errorPosition <= commandText.Length, "position in input stream cannot be greater than query text size");

            if (loadErrorContextInfoFromResource)
            {
                errorContextInfo = !String.IsNullOrEmpty(errorContextInfo) ? EntityRes.GetString(errorContextInfo) : String.Empty;
            }

            //
            // Replace control chars and newLines for single representation characters
            //
            StringBuilder sb = new StringBuilder(commandText.Length);

            for (int i = 0; i < commandText.Length; i++)
            {
                Char c = commandText[i];
                if (CqlLexer.IsNewLine(c))
                {
                    c = '\n';
                }
                else if ((Char.IsControl(c) || Char.IsWhiteSpace(c)) && ('\r' != c))
                {
                    c = ' ';
                }
                sb.Append(c);
            }
            commandText = sb.ToString().TrimEnd(new char[] { '\n' });

            //
            // Compute line and column
            //
            string[] queryLines = commandText.Split(new char[] { '\n' }, StringSplitOptions.None);
            for (lineNumber = 0, columnNumber = errorPosition;
                 lineNumber < queryLines.Length && columnNumber > queryLines[lineNumber].Length;
                 columnNumber -= (queryLines[lineNumber].Length + 1), ++lineNumber)
            {
                ;
            }

            ++lineNumber; // switch lineNum and colNum to 1-based indexes
            ++columnNumber;

            //
            // Error context format: "[errorContextInfo,] line ddd, column ddd"
            //
            sb = new Text.StringBuilder();
            if (!String.IsNullOrEmpty(errorContextInfo))
            {
                sb.AppendFormat(CultureInfo.CurrentCulture, "{0}, ", errorContextInfo);
            }

            if (errorPosition >= 0)
            {
                sb.AppendFormat(CultureInfo.CurrentCulture,
                                "{0} {1}, {2} {3}",
                                System.Data.Entity.Strings.LocalizedLine,
                                lineNumber,
                                System.Data.Entity.Strings.LocalizedColumn,
                                columnNumber);
            }

            return(sb.ToString());
        }