Ejemplo n.º 1
0
        internal static SearchItem CreateSearchExpressionItem(string queryString, string alias = null)
        {
            var queryView = queryString.GetStringView();

            return(CreateSearchExpressionItem(new SearchExpression(
                                                  SearchExpressionType.QueryString, queryView, queryView, alias.GetStringView(),
                                                  EvaluatorManager.GetConstantEvaluatorByName("query"))));
        }
Ejemplo n.º 2
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()));
        }
Ejemplo n.º 3
0
        internal SearchExpression Apply(string functionName, params SearchExpression[] args)
        {
            var see     = EvaluatorManager.GetEvaluatorByNameDuringParsing(functionName, innerText);
            var seeArgs = new List <SearchExpression> {
                this
            };

            seeArgs.AddRange(args);
            return(new SearchExpression(SearchExpressionType.Function, StringView.nil, innerText, alias, see, seeArgs.ToArray()));
        }
Ejemplo n.º 4
0
        public static IEnumerable <SearchItem> Apply(SearchExpressionContext c)
        {
            if (c.args.Length == 0)
            {
                c.ThrowError("Invalid Arguments");
            }

            var resultStreamExpr = c.args[0];

            if (!resultStreamExpr.types.IsIterable())
            {
                c.ThrowError($"No iterators", resultStreamExpr.outerText);
            }

            var exprs = new List <SearchExpression>(c.expression.parameters.Length);

            for (var i = 1; i < c.expression.parameters.Length; ++i)
            {
                var templateExpr = c.expression.parameters[i];
                var currentStreamName = $"apply@stream#{i}";
                var currentStream = EvaluatorUtils.CreateStreamExpression(resultStreamExpr, currentStreamName);
                var args = new[] { currentStream }.Concat(templateExpr.parameters).ToArray();
                var innerText = templateExpr.innerText;
                var evaluator = templateExpr.evaluator;

                if (templateExpr.types.HasFlag(SearchExpressionType.Function))
                {
                    // Back patch arguments
                    var restOfArguments = templateExpr.innerText.Substring(templateExpr.evaluator.name.Length + 1);
                    innerText = $"{templateExpr.evaluator.name}{{{currentStreamName}, {restOfArguments}}}".GetStringView();
                }
                else if (templateExpr.types.HasFlag(SearchExpressionType.Text))
                {
                    // String literal used as an evaluator name:
                    var filterFunction = EvaluatorManager.GetEvaluatorByNameDuringEvaluation(templateExpr.innerText.ToString(), templateExpr.innerText, c);
                    innerText = $"{templateExpr.innerText}{{{currentStreamName}}}".GetStringView();
                    evaluator = filterFunction;
                }
                else
                {
                    c.ThrowError($"Bad argument type", templateExpr.outerText);
                }

                var patchedExpr = new SearchExpression(templateExpr.types, templateExpr.outerText, innerText, evaluator, args);
                exprs.Add(patchedExpr);
                resultStreamExpr = patchedExpr;
            }

            // Pull on the last element to start the evaluation chain
            return(exprs.Last().Execute(c));
        }