Пример #1
0
        internal static SearchExpression QueryParser(SearchExpressionParserArgs args)
        {
            var text = ParserUtils.SimplifyExpression(args.text);
            var nestedExpressions         = new List <SearchExpression>();
            var expressionsStartAndLength = ParserUtils.GetExpressionsStartAndLength(text, out _);
            var lastExpressionEndIndex    = 0;

            foreach (var expression in expressionsStartAndLength)
            {
                string exprName = string.Empty;
                if (expression[0] == '{')
                {
                    var namedText = text.Substring(lastExpressionEndIndex, expression.startIndex - text.startIndex + 1 - lastExpressionEndIndex).ToString();
                    var match     = ParserUtils.namedExpressionStartRegex.Match(namedText);
                    if (match != null && match.Success && match.Groups["name"].Value != string.Empty)
                    {
                        exprName = match.Groups["name"].Value;
                    }
                }

                var paramText        = args.With(text.Substring(expression.startIndex - text.startIndex - exprName.Length, expression.length + exprName.Length));
                var nestedExpression = ParserManager.Parse(paramText);
                nestedExpressions.Add(nestedExpression);
                lastExpressionEndIndex = expression.startIndex + expression.length;
            }

            return(new SearchExpression(SearchExpressionType.QueryString, args.text, text, QueryEvaluator, nestedExpressions.ToArray()));
        }
Пример #2
0
        internal static SearchExpression AliasParser(SearchExpressionParserArgs args)
        {
            // Make sure the expression contains at least the keyword `as`
            if (!args.text.Contains("as"))
            {
                return(null);
            }

            var m = s_AliasWithQuotesRx.Match(args.text.ToString());

            if (!m.Success)
            {
                m = s_AliasWithoutQuotesRx.Match(args.text.ToString());
            }

            if (!m.Success || m.Groups.Count != 2)
            {
                return(null);
            }

            var aliasGroup      = m.Groups["alias"];
            var alias           = args.text.Substring(aliasGroup.Index, aliasGroup.Length).Trim();
            var aliasExpression = ParserManager.Parse(args.With(alias).With(SearchExpressionParserFlags.ImplicitLiterals));

            var exprText       = args.text.Substring(0, m.Index);
            var expression     = ParserManager.Parse(args.With(exprText));
            var isDynamicAlias = !aliasExpression.types.HasFlag(SearchExpressionType.Text) || alias.Contains('@') || alias.Contains('$');

            if (isDynamicAlias && !string.Equals(expression.name, "alias", System.StringComparison.OrdinalIgnoreCase))
            {
                return(expression.Apply("alias", aliasExpression));
            }

            return(new SearchExpression(expression.types, args.text, expression.innerText, alias, expression.evaluator, expression.parameters));
        }
        internal static SearchExpression ExpressionSetParser(SearchExpressionParserArgs args)
        {
            var outerText = args.text;

            if (outerText.length < 2)
            {
                return(null);
            }
            var innerText = ParserUtils.SimplifyExpression(outerText, false);

            if (outerText.length == innerText.length)
            {
                return(null);
            }
            var text = outerText.Substring(innerText.startIndex - outerText.startIndex - 1, innerText.length + 2);

            if (text[0] != '{' || text[text.length - 1] != '}')
            {
                return(null);
            }

            var expressions = ParserUtils.GetExpressionsStartAndLength(innerText, out var rootHasParameters);

            if (!rootHasParameters)
            {
                return(null);
            }

            var parameters = ParserUtils.ExtractArguments(text)
                             .Select(paramText => ParserManager.Parse(args.With(paramText).Without(SearchExpressionParserFlags.ImplicitLiterals)))
                             .ToArray();

            return(new SearchExpression(SearchExpressionType.Set, outerText, innerText.Trim(), SetEvaluator, parameters));
        }
Пример #4
0
        internal static SearchExpression NamedParser(SearchExpressionParserArgs args)
        {
            var text = ParserUtils.SimplifyExpression(args.text);

            if (text.IsNullOrEmpty())
            {
                return(null);
            }

            var match = ParserUtils.namedExpressionStartRegex.Match(text.ToString());

            if (!match.Success || match.Index != 0 || match.Groups["name"].Length == 0)
            {
                return(null);
            }

            var expressionsStartAndLength = ParserUtils.GetExpressionsStartAndLength(text, out _);

            if (expressionsStartAndLength.Length != 1)
            {
                return(null);
            }

            var expressionName = match.Groups["name"].Value;

            if ((expressionName.Length + expressionsStartAndLength[0].length) != text.length)
            {
                return(null);
            }

            var evaluator           = EvaluatorManager.GetEvaluatorByNameDuringParsing(expressionName, text.Substring(0, expressionName.Length));
            var parametersText      = text.Substring(expressionName.Length, text.length - expressionName.Length);
            var parametersPositions = ParserUtils.ExtractArguments(parametersText, expressionName);
            var parameters          = new List <SearchExpression>();

            var argsWith    = SearchExpressionParserFlags.None;
            var argsWithout = SearchExpressionParserFlags.ImplicitLiterals;

            ApplyEvaluatorHints(evaluator.hints, ref argsWith, ref argsWithout);
            foreach (var paramText in parametersPositions)
            {
                parameters.Add(ParserManager.Parse(args.With(paramText, argsWith).Without(argsWithout)));
            }

            if (!evaluator.hints.HasFlag(SearchExpressionEvaluationHints.DoNotValidateSignature) &&
                args.HasOption(SearchExpressionParserFlags.ValidateSignature))
            {
                var signatures = EvaluatorManager.GetSignaturesByName(expressionName);
                if (signatures != null)
                {
                    SearchExpressionValidator.ValidateExpressionArguments(evaluator, parameters.ToArray(), signatures, text);
                }
            }

            var expressionText = ParserUtils.SimplifyExpression(expressionsStartAndLength[0].Substring(1, expressionsStartAndLength[0].length - 2));

            return(new SearchExpression(SearchExpressionType.Function, args.text, expressionText, evaluator, parameters.ToArray()));
        }
        internal static SearchExpression FixedSetParser(SearchExpressionParserArgs args)
        {
            var outerText = args.text;
            var text      = ParserUtils.SimplifyExpression(outerText);

            if (text.length < 2 || text[0] != '[' || text[text.length - 1] != ']')
            {
                return(null);
            }
            var expressions = ParserUtils.GetExpressionsStartAndLength(text, out _);

            if (expressions.Length != 1 || expressions[0].startIndex != text.startIndex || expressions[0].length != text.length)
            {
                return(null);
            }

            var parameters = ParserUtils.ExtractArguments(text)
                             .Select(paramText => ParserManager.Parse(args.With(paramText).With(SearchExpressionParserFlags.ImplicitLiterals)))
                             .ToArray();

            var innerText = ParserUtils.SimplifyExpression(text.Substring(1, text.length - 2));

            return(new SearchExpression(SearchExpressionType.Set, outerText, innerText, SetEvaluator, parameters));
        }
Пример #6
0
 internal static SearchExpression Parse(SearchContext context, SearchExpressionParserFlags options = SearchExpressionParserFlags.Default)
 {
     return(ParserManager.Parse(new SearchExpressionParserArgs(context.searchText.GetStringView(), context, options)));
 }
Пример #7
0
 internal static SearchExpression Parse(SearchExpressionParserArgs args)
 {
     return(ParserManager.Parse(args));
 }