Exemple #1
0
        private QueryExpression ParseExpression(bool scan)
        {
            QueryExpression     rootExpression   = null;
            CompositeExpression parentExpression = null;

            while (_currentIndex < _expression.Length)
            {
                var    left  = ParseSide(scan);
                object right = null;

                QueryOperator op;
                if (_expression[_currentIndex] == ')' ||
                    _expression[_currentIndex] == '|' ||
                    _expression[_currentIndex] == '&')
                {
                    op = QueryOperator.Exists;
                }
                else
                {
                    op = ParseOperator();

                    right = ParseSide(scan);
                }

                var booleanExpression = new BooleanQueryExpression
                {
                    Left     = left,
                    Operator = op,
                    Right    = right
                };

                if (_expression[_currentIndex] == ')')
                {
                    if (parentExpression != null)
                    {
                        parentExpression.Expressions.Add(booleanExpression);
                        return(rootExpression);
                    }

                    return(booleanExpression);
                }
                if (_expression[_currentIndex] == '&')
                {
                    if (!Match("&&"))
                    {
                        throw CreateUnexpectedCharacterException();
                    }

                    if (parentExpression == null || parentExpression.Operator != QueryOperator.And)
                    {
                        var andExpression = new CompositeExpression {
                            Operator = QueryOperator.And
                        };

                        parentExpression?.Expressions.Add(andExpression);

                        parentExpression = andExpression;

                        if (rootExpression == null)
                        {
                            rootExpression = parentExpression;
                        }
                    }

                    parentExpression.Expressions.Add(booleanExpression);
                }
                if (_expression[_currentIndex] == '|')
                {
                    if (!Match("||"))
                    {
                        throw CreateUnexpectedCharacterException();
                    }

                    if (parentExpression == null || parentExpression.Operator != QueryOperator.Or)
                    {
                        var orExpression = new CompositeExpression {
                            Operator = QueryOperator.Or
                        };

                        parentExpression?.Expressions.Add(orExpression);

                        parentExpression = orExpression;

                        if (rootExpression == null)
                        {
                            rootExpression = parentExpression;
                        }
                    }

                    parentExpression.Expressions.Add(booleanExpression);
                }
            }

            throw new JsonException("Path ended with open query.");
        }
Exemple #2
0
        private QueryExpression ParseExpression()
        {
            QueryExpression     rootExpression   = null;
            CompositeExpression parentExpression = null;

            while (_currentIndex < _expression.Length)
            {
                EatWhitespace();
                if (_expression[_currentIndex] != '@')
                {
                    throw new JsonException("Unexpected character while parsing path query: " + _expression[_currentIndex]);
                }
                _currentIndex++;
                var expressionPath = new List <PathFilter>();
                if (ParsePath(expressionPath, _currentIndex, true))
                {
                    throw new JsonException("Path ended with open query.");
                }
                EatWhitespace();
                EnsureLength("Path ended with open query.");

                QueryOperator op;
                object        value = null;
                if (_expression[_currentIndex] == ')' || _expression[_currentIndex] == '|' || _expression[_currentIndex] == '&')
                {
                    op = QueryOperator.Exists;
                }
                else
                {
                    op = ParseOperator();
                    EatWhitespace();
                    EnsureLength("Path ended with open query.");
                    value = ParseValue();
                    EatWhitespace();
                    EnsureLength("Path ended with open query.");
                }

                var booleanExpression = new BooleanQueryExpression {
                    Path = expressionPath, Operator = op, Value = (op != QueryOperator.Exists) ? new JValue(value) : null
                };

                if (_expression[_currentIndex] == ')')
                {
                    if (parentExpression == null)
                    {
                        return(booleanExpression);
                    }
                    parentExpression.Expressions.Add(booleanExpression);
                    return(rootExpression);
                }
                if (_expression[_currentIndex] == '&' && Match("&&"))
                {
                    if (parentExpression == null || parentExpression.Operator != QueryOperator.And)
                    {
                        var andExpression = new CompositeExpression {
                            Operator = QueryOperator.And
                        };

                        if (parentExpression != null)
                        {
                            parentExpression.Expressions.Add(andExpression);
                        }

                        parentExpression = andExpression;

                        if (rootExpression == null)
                        {
                            rootExpression = parentExpression;
                        }
                    }

                    parentExpression.Expressions.Add(booleanExpression);
                }
                if (_expression[_currentIndex] != '|' || !Match("||"))
                {
                    continue;
                }
                if (parentExpression == null || parentExpression.Operator != QueryOperator.Or)
                {
                    var orExpression = new CompositeExpression {
                        Operator = QueryOperator.Or
                    };

                    if (parentExpression != null)
                    {
                        parentExpression.Expressions.Add(orExpression);
                    }

                    parentExpression = orExpression;

                    if (rootExpression == null)
                    {
                        rootExpression = parentExpression;
                    }
                }

                parentExpression.Expressions.Add(booleanExpression);
            }

            throw new JsonException("Path ended with open query.");
        }
Exemple #3
0
        private QueryExpression ParseExpression()
        {
            QueryExpression rootExpression = null;
            CompositeExpression parentExpression = null;

            while (_currentIndex < _expression.Length)
            {
                EatWhitespace();
                if (_expression[_currentIndex] != '@') throw new JsonException("Unexpected character while parsing path query: " + _expression[_currentIndex]);
                _currentIndex++;
                var expressionPath = new List<PathFilter>();
                if (ParsePath(expressionPath, _currentIndex, true)) throw new JsonException("Path ended with open query.");
                EatWhitespace();
                EnsureLength("Path ended with open query.");

                QueryOperator op;
                object value = null;
                if (_expression[_currentIndex] == ')' || _expression[_currentIndex] == '|' || _expression[_currentIndex] == '&')
                    op = QueryOperator.Exists;
                else
                {
                    op = ParseOperator();
                    EatWhitespace();
                    EnsureLength("Path ended with open query.");
                    value = ParseValue();
                    EatWhitespace();
                    EnsureLength("Path ended with open query.");
                }

                var booleanExpression = new BooleanQueryExpression {Path = expressionPath, Operator = op, Value = (op != QueryOperator.Exists) ? new JValue(value) : null};

                if (_expression[_currentIndex] == ')')
                {
                    if (parentExpression == null) return booleanExpression;
                    parentExpression.Expressions.Add(booleanExpression);
                    return rootExpression;
                }
                if (_expression[_currentIndex] == '&' && Match("&&"))
                {
                    if (parentExpression == null || parentExpression.Operator != QueryOperator.And)
                    {
                        var andExpression = new CompositeExpression {Operator = QueryOperator.And};

                        if (parentExpression != null)
                            parentExpression.Expressions.Add(andExpression);

                        parentExpression = andExpression;

                        if (rootExpression == null)
                            rootExpression = parentExpression;
                    }

                    parentExpression.Expressions.Add(booleanExpression);
                }
                if (_expression[_currentIndex] != '|' || !Match("||")) continue;
                if (parentExpression == null || parentExpression.Operator != QueryOperator.Or)
                {
                    var orExpression = new CompositeExpression {Operator = QueryOperator.Or};

                    if (parentExpression != null)
                        parentExpression.Expressions.Add(orExpression);

                    parentExpression = orExpression;

                    if (rootExpression == null)
                        rootExpression = parentExpression;
                }

                parentExpression.Expressions.Add(booleanExpression);
            }

            throw new JsonException("Path ended with open query.");
        }