Ejemplo n.º 1
0
        public static ExpressionEvaluationResult RunPipeline(
            CommandEvaluationContext context,
            PipelineParseResult pipelineParseResult
            )
        {
            ExpressionEvaluationResult r = null;
            var workUnitParseResult      = pipelineParseResult;

            while (workUnitParseResult != null)
            {
                r = RunWorkUnit(context, workUnitParseResult);
                workUnitParseResult = workUnitParseResult.Next;
            }
            return(r);
        }
        public static ExpressionEvaluationResult RunWorkUnit(
            CommandEvaluationContext context,
            PipelineParseResult pipelineParseResult
            )
        {
            var syntaxParsingResult = pipelineParseResult.ParseResult.SyntaxParsingResults.First();

            try
            {
                var outputData = InvokeCommand(context, syntaxParsingResult.CommandSyntax.CommandSpecification, syntaxParsingResult.MatchingParameters);

                return(new ExpressionEvaluationResult(null, ParseResultType.Valid, outputData, (int)ReturnCode.OK, null));
            }
            catch (Exception commandInvokeError)
            {
                var commandError = commandInvokeError.InnerException ?? commandInvokeError;
                Errorln(commandError.Message);
                return(new ExpressionEvaluationResult(null, pipelineParseResult.ParseResult.ParseResultType, null, (int)ReturnCode.Error, commandError));
            }
        }
Ejemplo n.º 3
0
        public static ExpressionEvaluationResult RunWorkUnit(
            CommandEvaluationContext context,
            PipelineParseResult pipelineParseResult
            )
        {
            var syntaxParsingResult = pipelineParseResult.ParseResult.SyntaxParsingResults.First();

            try
            {
                // capture the err output
                context.Err.StartRecording();

                var r          = InvokeCommand(context, syntaxParsingResult.CommandSyntax.CommandSpecification, syntaxParsingResult.MatchingParameters);
                var res        = r as ICommandResult;
                var err_record = context.Err.StopRecording();
                var err        = res.ExecErrorText;

                // auto assign from Err stream if no error text provided
                if (string.IsNullOrEmpty(res.ExecErrorText) && !string.IsNullOrEmpty(err_record))
                {
                    err = err_record;
                }

                return
                    ((res == null)?
                     new ExpressionEvaluationResult(pipelineParseResult.Expr, null, ParseResultType.Valid, null, (int)ReturnCode.Error, null, "the command has returned a null result") :
                     new ExpressionEvaluationResult(pipelineParseResult.Expr, null, ParseResultType.Valid, res.GetOuputData(), res.ReturnCode, null, err));
            }
            catch (Exception commandInvokeError)
            {
                // error is catched at shell level
                var commandError = commandInvokeError.InnerException ?? commandInvokeError;
                context.Errorln(commandError.Message);
                return(new ExpressionEvaluationResult(pipelineParseResult.Expr, null, pipelineParseResult.ParseResult.ParseResultType, null, (int)ReturnCode.Error, commandError));
            }
            finally {
                context.Err.StopRecording();
            }
        }
Ejemplo n.º 4
0
        ExpressionEvaluationResult AnalysisPipelineParseResult(
            CommandEvaluationContext context,
            PipelineParseResult pipelineParseResult,
            string expr,
            int outputX,
            ParseResult parseResult
            )
        {
            ExpressionEvaluationResult r = null;
            var errorText = "";

            string[] t;
            int      idx;
            string   serr;

            switch (parseResult.ParseResultType)
            {
            case ParseResultType.Empty:

                r = new ExpressionEvaluationResult(expr, null, parseResult.ParseResultType, null, (int)ReturnCode.OK, null);
                break;

            case ParseResultType.NotValid:      /* command syntax not valid */

                var perComErrs = new Dictionary <string, List <CommandSyntaxParsingResult> >();
                foreach (var prs in parseResult.SyntaxParsingResults)
                {
                    if (prs.CommandSyntax != null)
                    {
                        if (perComErrs.TryGetValue(prs.CommandSyntax?.CommandSpecification?.Name, out var lst))
                        {
                            lst.Add(prs);
                        }
                        else
                        {
                            perComErrs.Add(prs.CommandSyntax.CommandSpecification.Name, new List <CommandSyntaxParsingResult> {
                                prs
                            });
                        }
                    }
                }

                var errs           = new List <string>();
                var minErrPosition = int.MaxValue;
                var errPositions   = new List <int>();
                foreach (var kvp in perComErrs)
                {
                    var comSyntax = kvp.Value.First().CommandSyntax;
                    foreach (var prs in kvp.Value)
                    {
                        foreach (var perr in prs.ParseErrors)
                        {
                            minErrPosition = Math.Min(minErrPosition, perr.Position);
                            errPositions.Add(perr.Position);
                            if (!errs.Contains(perr.Description))
                            {
                                errs.Add(perr.Description);
                            }
                        }
                        errorText += Br + Red + string.Join(Br + Red, errs);
                    }
                    errorText += $"{Br}{Red}for syntax: {comSyntax}{Br}";
                }

                errPositions.Sort();
                errPositions = errPositions.Distinct().ToList();

                t = new string[expr.Length + 2];
                for (int i = 0; i < t.Length; i++)
                {
                    t[i] = " ";
                }
                foreach (var errPos in errPositions)
                {
                    t[GetIndex(context, errPos, expr)] = Settings.ErrorPositionMarker + "";
                }
                serr = string.Join("", t);
                Error(" ".PadLeft(outputX + 1) + serr, false, false);

                Error(errorText);
                r = new ExpressionEvaluationResult(expr, errorText, parseResult.ParseResultType, null, (int)ReturnCode.NotIdentified, null);
                break;

            case ParseResultType.Ambiguous:

                errorText += $"{Red}ambiguous syntaxes:{Br}";
                foreach (var prs in parseResult.SyntaxParsingResults)
                {
                    errorText += $"{Red}{prs.CommandSyntax}{Br}";
                }
                Error(errorText);
                r = new ExpressionEvaluationResult(expr, errorText, parseResult.ParseResultType, null, (int)ReturnCode.NotIdentified, null);
                break;

            case ParseResultType.NotIdentified:

                t = new string[expr.Length + 2];
                for (int j = 0; j < t.Length; j++)
                {
                    t[j] = " ";
                }
                var err = parseResult.SyntaxParsingResults.First().ParseErrors.First();
                idx        = err.Position;
                t[idx]     = Settings.ErrorPositionMarker + "";
                errorText += Red + err.Description;
                serr       = string.Join("", t);
                context.Errorln(" ".PadLeft(outputX) + serr);
                context.Errorln(errorText);
                r = new ExpressionEvaluationResult(expr, errorText, parseResult.ParseResultType, null, (int)ReturnCode.NotIdentified, null);
                break;

            case ParseResultType.SyntaxError:

                t = new string[expr.Length + 2];
                for (int j = 0; j < t.Length; j++)
                {
                    t[j] = " ";
                }
                var err2 = parseResult.SyntaxParsingResults.First().ParseErrors.First();
                idx        = err2.Index;
                t[idx]     = Settings.ErrorPositionMarker + "";
                errorText += Red + err2.Description;
                serr       = string.Join("", t);
                context.Errorln(" ".PadLeft(outputX) + serr);
                context.Errorln(errorText);
                r = new ExpressionEvaluationResult(expr, errorText, parseResult.ParseResultType, null, (int)ReturnCode.NotIdentified, null);
                break;
            }

            return(r);
        }