Exemplo n.º 1
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);
        }
        public static (CommandTreeTokenStream, IReadOnlyList <string>) Tokenize(IEnumerable <string> args)
        {
            var tokens         = new List <CommandTreeToken>();
            var position       = 0;
            var previousReader = default(TextBuffer);
            var context        = new CommandTreeTokenizerContext();

            foreach (var arg in args)
            {
                var start  = position;
                var reader = new TextBuffer(previousReader, arg);

                // Parse the token.
                position = ParseToken(context, reader, position, start, tokens);
                context.FlushRemaining();

                previousReader = reader;
            }

            return(new CommandTreeTokenStream(tokens), context.Remaining);
        }
Exemplo n.º 3
0
        private static CommandTreeToken ScanLongOption(CommandTreeTokenizerContext context, TextBuffer reader, int position)
        {
            reader.Consume('-');
            context.AddRemaining('-');

            if (reader.ReachedEnd)
            {
                // Rest of the arguments are remaining ones.
                context.Mode = Mode.Remaining;
                return(new CommandTreeToken(CommandTreeToken.Kind.Remaining, position, "--", "--"));
            }

            var name = ScanString(context, reader, new[] { '=', ':' });

            // Perform validation of the name.
            if (name.Value.Length == 0)
            {
                throw ParseException.LongOptionNameIsMissing(reader, position);
            }
            if (name.Value.Length == 1)
            {
                throw ParseException.LongOptionNameIsOneCharacter(reader, position, name.Value);
            }
            if (char.IsDigit(name.Value[0]))
            {
                throw ParseException.LongOptionNameStartWithDigit(reader, position, name.Value);
            }
            for (var index = 0; index < name.Value.Length; index++)
            {
                if (!char.IsLetterOrDigit(name.Value[index]) && name.Value[index] != '-' && name.Value[index] != '_')
                {
                    throw ParseException.LongOptionNameContainSymbol(reader, position + 2 + index, name.Value[index]);
                }
            }

            return(new CommandTreeToken(CommandTreeToken.Kind.LongOption, position, name.Value, $"--{name.Value}"));
        }