예제 #1
0
        public static IEnumerable <CommandToken> Parse(string commandLine)
        {
            CommandToken lastToken = null;

            for (int index = 0, count = commandLine.Length; index < count;)
            {
                switch (commandLine[index])
                {
                case ' ':
                {
                    lastToken = CommandSpaceToken.Parse(commandLine, ref index);
                    yield return(lastToken);

                    break;
                }

                case '"':
                case '\'':
                {
                    // "'"
                    if (lastToken is CommandQuoteToken quote && quote.Value[0] != commandLine[index])
                    {
                        goto default;
                    }

                    lastToken = CommandQuoteToken.Parse(commandLine, ref index);
                    yield return(lastToken);

                    break;
                }

                default:
                {
                    lastToken = CommandStringToken.Parse(commandLine, ref index,
                                                         lastToken is CommandQuoteToken quote ? quote : null);

                    yield return(lastToken);

                    break;
                }
                }
            }
        }
예제 #2
0
        internal static CommandStringToken Parse(string commandLine, ref int index, CommandQuoteToken quote)
        {
            int end;
            int offset = index;

            if (quote != null)
            {
                var ix = index;

                do
                {
                    end = commandLine.IndexOf(quote.Value[0], ix + 1);

                    if (end == -1)
                    {
                        throw new ArgumentException("String not closed");
                    }

                    if (IsScaped(commandLine, end - 1))
                    {
                        ix  = end;
                        end = -1;
                    }
                }while (end < 0);
            }
            else
            {
                end = commandLine.IndexOf(' ', index + 1);
            }

            if (end == -1)
            {
                end = commandLine.Length;
            }

            var ret = new CommandStringToken(offset, commandLine.Substring(index, end - index));

            index += end - index;
            return(ret);
        }