Ejemplo n.º 1
0
        public void SemicolonOnlyAllowedInParensInExpand()
        {
            SelectExpandParser parser = new SelectExpandParser("one;two", 3);
            Action             parse  = () => parser.ParseExpand();

            parse.ShouldThrow <ODataException>().WithMessage(ODataErrorStrings.ExpressionLexer_InvalidCharacter(";", 3, "one;two"));
        }
Ejemplo n.º 2
0
        public void PeekingShouldThrowWhenIncorrectCharacterAtStart()
        {
            ExpressionLexer lexer = new ExpressionLexer("#$*@#", false, false);
            Action          peek  = () => lexer.PeekNextToken();

            peek.ShouldThrow <ODataException>().WithMessage(ODataErrorStrings.ExpressionLexer_InvalidCharacter("#", "0", "#$*@#"));
        }
Ejemplo n.º 3
0
        public void ParsePath_AliasInFunctionImport_InvalidAliasName()
        {
            Action parse = () => ParseUriAndVerify(
                new Uri("http://gobbledygook/GetPet4(id=@p!1)?@p!1=1.01M"),
                (oDataPath, filterClause, orderByClause, selectExpandClause, aliasNodes) =>
            {
            });

            parse.ShouldThrow <ODataException>().WithMessage(ODataErrorStrings.ExpressionLexer_InvalidCharacter("!", 5, "id=@p!1"));
        }
Ejemplo n.º 4
0
        public void ShouldErrorWhenIncorrectCharacterAtStart()
        {
            ExpressionLexer lexer = new ExpressionLexer("#$*@#", false, false);
            ExpressionToken resultToken;
            Exception       error  = null;
            bool            result = lexer.TryPeekNextToken(out resultToken, out error);

            result.Should().BeFalse();
            error.Should().NotBeNull();
            error.Message.Should().Be(ODataErrorStrings.ExpressionLexer_InvalidCharacter("#", "0", "#$*@#"));
        }
Ejemplo n.º 5
0
        protected virtual ExpressionToken NextTokenImplementation(out Exception error)
        {
            error = null;

            if (this.ignoreWhitespace)
            {
                this.ParseWhitespace();
            }

            ExpressionTokenKind t;
            int tokenPos = this.textPos;

            switch (this.ch)
            {
            case '(':
                this.NextChar();
                t = ExpressionTokenKind.OpenParen;
                break;

            case ')':
                this.NextChar();
                t = ExpressionTokenKind.CloseParen;
                break;

            case ',':
                this.NextChar();
                t = ExpressionTokenKind.Comma;
                break;

            case '-':
                bool hasNext = this.textPos + 1 < this.TextLen;
                if (hasNext && Char.IsDigit(this.Text[this.textPos + 1]))
                {
                    // don't separate '-' and its following digits : -2147483648 is valid int.MinValue, but 2147483648 is long.
                    t = this.ParseFromDigit();
                    if (ExpressionLexerUtils.IsNumeric(t))
                    {
                        break;
                    }

                    // If it looked like a numeric but wasn't (because it was a binary 0x... value for example),
                    // we'll rewind and fall through to a simple '-' token.
                    this.SetTextPos(tokenPos);
                }
                else if (hasNext && this.Text[tokenPos + 1] == ExpressionConstants.InfinityLiteral[0])
                {
                    this.NextChar();
                    this.ParseIdentifier();
                    string currentIdentifier = this.Text.Substring(tokenPos + 1, this.textPos - tokenPos - 1);

                    if (ExpressionLexerUtils.IsInfinityLiteralDouble(currentIdentifier))
                    {
                        t = ExpressionTokenKind.DoubleLiteral;
                        break;
                    }
                    else if (ExpressionLexerUtils.IsInfinityLiteralSingle(currentIdentifier))
                    {
                        t = ExpressionTokenKind.SingleLiteral;
                        break;
                    }

                    // If it looked like '-INF' but wasn't we'll rewind and fall through to a simple '-' token.
                    this.SetTextPos(tokenPos);
                }

                this.NextChar();
                t = ExpressionTokenKind.Minus;
                break;

            case '=':
                this.NextChar();
                t = ExpressionTokenKind.Equal;
                break;

            case '/':
                this.NextChar();
                t = ExpressionTokenKind.Slash;
                break;

            case '?':
                this.NextChar();
                t = ExpressionTokenKind.Question;
                break;

            case '.':
                this.NextChar();
                t = ExpressionTokenKind.Dot;
                break;

            case '\'':
                char quote = this.ch.Value;
                do
                {
                    this.AdvanceToNextOccuranceOf(quote);

                    if (this.textPos == this.TextLen)
                    {
                        error = ParseError(ODataErrorStrings.ExpressionLexer_UnterminatedStringLiteral(this.textPos, this.Text));
                    }

                    this.NextChar();
                }while (this.ch.HasValue && (this.ch.Value == quote));
                t = ExpressionTokenKind.StringLiteral;
                break;

            case '*':
                this.NextChar();
                t = ExpressionTokenKind.Star;
                break;

            case ':':
                this.NextChar();
                t = ExpressionTokenKind.Colon;
                break;

            case '{':
                this.NextChar();
                this.AdvanceThroughBalancedExpression('{', '}');
                t = ExpressionTokenKind.BracketedExpression;
                break;

            case '[':
                this.NextChar();
                this.AdvanceThroughBalancedExpression('[', ']');
                t = ExpressionTokenKind.BracketedExpression;
                break;

            default:
                if (this.IsValidWhiteSpace)
                {
                    Debug.Assert(!this.ignoreWhitespace, "should not hit ws while ignoring it");
                    this.ParseWhitespace();
                    t = ExpressionTokenKind.Unknown;
                    break;
                }

                if (this.IsValidStartingCharForIdentifier)
                {
                    this.ParseIdentifier();

                    // Guids will have '-' in them
                    // guidValue = 8HEXDIG "-" 4HEXDIG "-" 4HEXDIG "-" 4HEXDIG "-" 12HEXDIG
                    if (this.ch == '-' &&
                        this.TryParseGuid(tokenPos))
                    {
                        t = ExpressionTokenKind.GuidLiteral;
                        break;
                    }

                    t = ExpressionTokenKind.Identifier;
                    break;
                }

                if (this.IsValidDigit)
                {
                    t = this.ParseFromDigit();
                    break;
                }

                if (this.textPos == this.TextLen)
                {
                    t = ExpressionTokenKind.End;
                    break;
                }

                if (this.useSemicolonDelimeter && this.ch == ';')
                {
                    this.NextChar();
                    t = ExpressionTokenKind.SemiColon;
                    break;
                }

                if (this.parsingFunctionParameters && this.ch == '@')
                {
                    this.NextChar();

                    if (this.textPos == this.TextLen)
                    {
                        error = ParseError(ODataErrorStrings.ExpressionLexer_SyntaxError(this.textPos, this.Text));
                        t     = ExpressionTokenKind.Unknown;
                        break;
                    }

                    if (!this.IsValidStartingCharForIdentifier)
                    {
                        error = ParseError(ODataErrorStrings.ExpressionLexer_InvalidCharacter(this.ch, this.textPos, this.Text));
                        t     = ExpressionTokenKind.Unknown;
                        break;
                    }

                    this.ParseIdentifier();
                    t = ExpressionTokenKind.ParameterAlias;
                    break;
                }

                error = ParseError(ODataErrorStrings.ExpressionLexer_InvalidCharacter(this.ch, this.textPos, this.Text));
                t     = ExpressionTokenKind.Unknown;
                break;
            }

            this.token.Kind     = t;
            this.token.Text     = this.Text.Substring(tokenPos, this.textPos - tokenPos);
            this.token.Position = tokenPos;

            this.HandleTypePrefixedLiterals();

            return(this.token);
        }
Ejemplo n.º 6
0
 public void OverClosedBracketsThrow()
 {
     ValidateLexerException <ODataException>("{stuff: morestuff}}", ODataErrorStrings.ExpressionLexer_InvalidCharacter("}", "18", "{stuff: morestuff}}"));
 }
Ejemplo n.º 7
0
        public void ExpressionLexerShouldExpectIdentifierStartAfterAtSymbol()
        {
            Action lex = () => new ExpressionLexer("@1", moveToFirstToken: true, useSemicolonDelimeter: false, parsingFunctionParameters: true);

            lex.ShouldThrow <ODataException>().WithMessage(ODataErrorStrings.ExpressionLexer_InvalidCharacter("1", 1, "@1"));
        }
Ejemplo n.º 8
0
        public void ExpressionLexerShouldFailByDefaultForAtSymbol()
        {
            Action lex = () => new ExpressionLexer("@", moveToFirstToken: true, useSemicolonDelimeter: false);

            lex.ShouldThrow <ODataException>().WithMessage(ODataErrorStrings.ExpressionLexer_InvalidCharacter("@", 0, "@"));
        }