Beispiel #1
0
        private static CommandTreeToken ScanQuotedString(CommandTreeTokenizerContext context, TextBuffer reader)
        {
            var position = reader.Position;

            reader.Consume('\"');
            context.AddRemaining('\"');

            var builder = new StringBuilder();

            while (!reader.ReachedEnd)
            {
                var character = reader.Peek();
                if (character == '\"')
                {
                    break;
                }

                context.AddRemaining(character);
                builder.Append(reader.Read());
            }

            if (reader.Peek() != '\"')
            {
                var unterminatedQuote = builder.ToString();
                var token             = new CommandTreeToken(CommandTreeToken.Kind.String, position, unterminatedQuote, $"\"{unterminatedQuote}");
                throw ParseException.UnterminatedQuote(reader.Original, token);
            }

            reader.Read();
            context.AddRemaining('\"');

            var value = builder.ToString();

            return(new CommandTreeToken(CommandTreeToken.Kind.String, position, value, $"\"{value}\""));
        }
Beispiel #2
0
        private static IEnumerable <CommandTreeToken> ScanOptions(CommandTreeTokenizerContext context, TextBuffer reader)
        {
            var result = new List <CommandTreeToken>();

            var position = reader.Position;

            reader.Consume('-');
            context.AddRemaining('-');

            if (!reader.TryPeek(out var character))
            {
                var token = new CommandTreeToken(CommandTreeToken.Kind.ShortOption, position, "-", "-");
                throw ParseException.OptionHasNoName(reader.Original, token);
            }

            switch (character)
            {
            case '-':
                var option = ScanLongOption(context, reader, position);
                if (option != null)
                {
                    result.Add(option);
                }
                break;

            default:
                result.AddRange(ScanShortOptions(context, reader, position));
                break;
            }

            if (reader.TryPeek(out character))
            {
                // Encountered a separator?
                if (character == '=' || character == ':')
                {
                    reader.Read(); // Consume
                    context.AddRemaining(character);

                    if (!reader.TryPeek(out character))
                    {
                        var token = new CommandTreeToken(CommandTreeToken.Kind.String, reader.Position, "=", "=");
                        throw ParseException.OptionValueWasExpected(reader.Original, token);
                    }

                    result.Add(ScanString(context, reader));
                }
            }

            return(result);
        }
Beispiel #3
0
        private static IEnumerable <CommandTreeToken> ScanShortOptions(CommandTreeTokenizerContext context, TextBuffer reader, int position)
        {
            var result = new List <CommandTreeToken>();

            while (!reader.ReachedEnd)
            {
                var current = reader.Peek();
                if (char.IsWhiteSpace(current))
                {
                    break;
                }

                // Encountered a separator?
                if (current == '=' || current == ':')
                {
                    break;
                }

                if (char.IsLetter(current))
                {
                    context.AddRemaining(current);
                    reader.Read(); // Consume

                    var value = current.ToString(CultureInfo.InvariantCulture);
                    result.Add(result.Count == 0
                        ? new CommandTreeToken(CommandTreeToken.Kind.ShortOption, position, value, $"-{value}")
                        : new CommandTreeToken(CommandTreeToken.Kind.ShortOption, position + result.Count, value, value));
                }
                else
                {
                    // Create a token representing the short option.
                    var tokenPosition = position + 1 + result.Count;
                    var token         = new CommandTreeToken(CommandTreeToken.Kind.ShortOption, tokenPosition, current.ToString(), current.ToString());
                    throw ParseException.InvalidShortOptionName(reader.Original, token);
                }
            }

            if (result.Count > 1)
            {
                foreach (var item in result)
                {
                    item.IsGrouped = true;
                }
            }

            return(result);
        }
Beispiel #4
0
        private void ParseOption(
            CommandTreeParserContext context,
            CommandTreeTokenStream stream,
            CommandTreeToken token,
            CommandTree node,
            bool isLongOption)
        {
            // Consume the option token.
            stream.Consume(isLongOption ? CommandTreeToken.Kind.LongOption : CommandTreeToken.Kind.ShortOption);

            if (context.State == State.Normal)
            {
                // Find the option.
                var option = node.FindOption(token.Value, isLongOption);
                if (option != null)
                {
                    node.Mapped.Add(new MappedCommandParameter(
                                        option, ParseOptionValue(context, stream, token, node, option)));

                    return;
                }

                // Help?
                if (_help?.IsMatch(token.Value) == true)
                {
                    node.ShowHelp = true;
                    return;
                }
            }

            if (context.State == State.Remaining)
            {
                ParseOptionValue(context, stream, token, node, null);
                return;
            }

            if (context.ParsingMode == ParsingMode.Strict)
            {
                throw ParseException.UnknownOption(context.Arguments, token);
            }
            else
            {
                ParseOptionValue(context, stream, token, node, null);
            }
        }
Beispiel #5
0
        private static string ParseOptionValue(
            CommandTreeParserContext context,
            CommandTreeTokenStream stream,
            CommandTreeToken token,
            CommandTree current,
            CommandParameter parameter)
        {
            var value = (string)null;

            // Parse the value of the token (if any).
            var valueToken = stream.Peek();

            if (valueToken?.TokenKind == CommandTreeToken.Kind.String)
            {
                var parseValue = true;
                if (token.TokenKind == CommandTreeToken.Kind.ShortOption && token.IsGrouped)
                {
                    parseValue = false;
                }

                if (context.State == State.Normal && parseValue)
                {
                    // Is this a command?
                    if (current.Command.FindCommand(valueToken.Value) == null)
                    {
                        if (parameter != null)
                        {
                            if (parameter.ParameterKind == ParameterKind.Single)
                            {
                                value = stream.Consume(CommandTreeToken.Kind.String).Value;
                            }
                            else if (parameter.ParameterKind == ParameterKind.Flag)
                            {
                                // Flags cannot be assigned a value.
                                throw ParseException.CannotAssignValueToFlag(context.Arguments, token);
                            }
                        }
                        else
                        {
                            // Unknown parameter value.
                            value = stream.Consume(CommandTreeToken.Kind.String).Value;

                            // In relaxed parsing mode?
                            if (context.ParsingMode == ParsingMode.Relaxed)
                            {
                                context.AddRemainingArgument(token.Value, value);
                            }
                        }
                    }
                }
                else
                {
                    context.AddRemainingArgument(token.Value, parseValue ? valueToken.Value : null);
                }
            }
            else
            {
                if (context.State == State.Remaining || context.ParsingMode == ParsingMode.Relaxed)
                {
                    context.AddRemainingArgument(token.Value, null);
                }
            }

            // No value?
            if (context.State == State.Normal)
            {
                if (value == null && parameter != null)
                {
                    if (parameter.ParameterKind == ParameterKind.Flag)
                    {
                        value = "true";
                    }
                    else
                    {
                        switch (parameter)
                        {
                        case CommandOption option:
                            throw ParseException.OptionHasNoValue(context.Arguments, token, option);

                        default:
                            // This should not happen at all. If it does, it's because we've added a new
                            // option type which isn't a CommandOption for some reason.
                            throw new InvalidOperationException($"Found invalid parameter type '{parameter.GetType().FullName}'.");
                        }
                    }
                }
            }

            return(value);
        }