Ejemplo n.º 1
0
        internal static IEnumerable <SymbolResult> AllSymbolResults(this SymbolResult symbolResult)
        {
            if (symbolResult == null)
            {
                throw new ArgumentNullException(nameof(symbolResult));
            }

            yield return(symbolResult);

            foreach (var item in symbolResult
                     .Children
                     .FlattenBreadthFirst(o => o.Children))
            {
                yield return(item);
            }
        }
        internal void AddToSymbolMap(SymbolResult result)
        {
            switch (result)
            {
            case ArgumentResult argumentResult:
                _allArgumentResults.Add(argumentResult.Argument, argumentResult);
                break;

            case CommandResult commandResult:
                _allCommandResults.Add(commandResult.Command, commandResult);
                break;

            case OptionResult optionResult:
                _allOptionResults.Add(optionResult.Option, optionResult);
                break;

            default:
                throw new ArgumentException($"Unsupported {nameof(SymbolResult)} type: {result.GetType()}");
            }
        }
Ejemplo n.º 3
0
        private static void Diagram(
            this StringBuilder builder,
            SymbolResult symbolResult,
            ParseResult parseResult)
        {
            if (parseResult.Errors.Any(e => e.SymbolResult == symbolResult))
            {
                builder.Append("!");
            }

            if (symbolResult is OptionResult optionResult &&
                optionResult.IsImplicit)
            {
                builder.Append("*");
            }

            if (symbolResult is ArgumentResult argumentResult)
            {
                var includeArgumentName =
                    argumentResult.Argument is Argument argument &&
                    argument.Parents[0] is ICommand command &&
                    command.Arguments.Count() > 1;

                if (includeArgumentName)
                {
                    builder.Append("[ ");
                    builder.Append(argumentResult.Argument.Name);
                    builder.Append(" ");
                }

                switch (argumentResult.GetArgumentConversionResult())
                {
                case SuccessfulArgumentConversionResult successful:

                    switch (successful.Value)
                    {
                    case string s:
                        builder.Append($"<{s}>");
                        break;

                    case IEnumerable items:
                        builder.Append("<");
                        builder.Append(
                            string.Join("> <",
                                        items.Cast <object>().ToArray()));
                        builder.Append(">");
                        break;

                    default:
                        builder.Append("<");
                        builder.Append(successful.Value);
                        builder.Append(">");
                        break;
                    }

                    break;

                case FailedArgumentConversionResult _:

                    builder.Append("<");
                    builder.Append(string.Join("> <", symbolResult.Tokens.Select(t => t.Value)));
                    builder.Append(">");

                    break;
                }

                if (includeArgumentName)
                {
                    builder.Append(" ]");
                }
            }
            else
            {
                builder.Append("[ ");
                builder.Append(symbolResult.Token().Value);

                foreach (var child in symbolResult.Children)
                {
                    builder.Append(" ");
                    builder.Diagram(child, parseResult);
                }

                builder.Append(" ]");
            }
        }
Ejemplo n.º 4
0
 internal ArgumentResult(
     IArgument argument,
     SymbolResult parent) : base(argument, parent)
 {
     Argument = argument;
 }
Ejemplo n.º 5
0
        private static void Diagram(
            this StringBuilder builder,
            SymbolResult symbolResult,
            ParseResult parseResult)
        {
            if (parseResult.Errors.Any(e => e.SymbolResult == symbolResult))
            {
                builder.Append("!");
            }

            if (symbolResult is OptionResult optionResult &&
                optionResult.IsImplicit)
            {
                builder.Append("*");
            }

            if (symbolResult is ArgumentResult argumentResult)
            {
                var includeArgumentName =
                    argumentResult.Argument is Argument argument &&
                    argument.Parents.First() is ICommand command &&
                    command.Name != argument.Name;

                if (includeArgumentName)
                {
                    builder.Append("[ ");
                    builder.Append(argumentResult.Argument.Name);
                    builder.Append(" ");
                }

                switch (argumentResult.ArgumentConversionResult)
                {
                case SuccessfulArgumentConversionResult successful:

                    switch (successful.Value)
                    {
                    case null:
                    case IReadOnlyCollection <string> a when a.Count == 0:
                        break;

                    case IEnumerable <string> args:
                        builder.Append("<");
                        builder.Append(string.Join("> <", args));
                        builder.Append(">");
                        break;

                    default:
                        builder.Append("<");
                        builder.Append(successful.Value);
                        builder.Append(">");
                        break;
                    }

                    break;

                case FailedArgumentConversionResult _:

                    builder.Append("<");
                    builder.Append(string.Join("> <", symbolResult.Tokens.Select(t => t.Value)));
                    builder.Append(">");

                    break;
                }

                if (includeArgumentName)
                {
                    builder.Append(" ]");
                }
            }
            else
            {
                builder.Append("[ ");
                builder.Append(symbolResult.Token().Value);

                foreach (var child in symbolResult.Children)
                {
                    builder.Append(" ");
                    builder.Diagram(child, parseResult);
                }

                builder.Append(" ]");
            }
        }
        private static void Diagram(
            this StringBuilder builder,
            SymbolResult symbolResult,
            ParseResult parseResult)
        {
            if (parseResult.Errors.Any(e => e.SymbolResult == symbolResult))
            {
                builder.Append("!");
            }


            switch (symbolResult)
            {
            case ArgumentResult argumentResult:
            {
                var includeArgumentName =
                    argumentResult.Argument.FirstParent !.Symbol is Command command &&
                    command.Arguments.Count > 1;

                if (includeArgumentName)
                {
                    builder.Append("[ ");
                    builder.Append(argumentResult.Argument.Name);
                    builder.Append(" ");
                }

                if (argumentResult.Argument.Arity.MaximumNumberOfValues > 0)
                {
                    ArgumentConversionResult conversionResult = argumentResult.GetArgumentConversionResult();
                    switch (conversionResult.Result)
                    {
                    case ArgumentConversionResultType.NoArgument:
                        break;

                    case ArgumentConversionResultType.Successful:
                        switch (conversionResult.Value)
                        {
                        case string s:
                            builder.Append($"<{s}>");
                            break;

                        case IEnumerable items:
                            builder.Append("<");
                            builder.Append(
                                string.Join("> <",
                                            items.Cast <object>().ToArray()));
                            builder.Append(">");
                            break;

                        default:
                            builder.Append("<");
                            builder.Append(conversionResult.Value);
                            builder.Append(">");
                            break;
                        }

                        break;

                    default:         // failures
                        builder.Append("<");
                        builder.Append(string.Join("> <", symbolResult.Tokens.Select(t => t.Value)));
                        builder.Append(">");

                        break;
                    }
                }

                if (includeArgumentName)
                {
                    builder.Append(" ]");
                }

                break;
            }

            default:
            {
                if (symbolResult is OptionResult {
                        IsImplicit : true
                    })
                {
                    builder.Append("*");
                }

                builder.Append("[ ");
                builder.Append(symbolResult.Token().Value);

                for (var i = 0; i < symbolResult.Children.Count; i++)
                {
                    var child = symbolResult.Children[i];

                    if (child is ArgumentResult arg &&
                        (arg.Argument.ValueType == typeof(bool) ||
                         arg.Argument.Arity.MaximumNumberOfValues == 0))
                    {
                        continue;
                    }

                    builder.Append(" ");

                    builder.Diagram(child, parseResult);
                }

                builder.Append(" ]");
                break;
            }
Ejemplo n.º 7
0
 internal static Token Token(this SymbolResult symbolResult) =>
 symbolResult switch
 {