Ejemplo n.º 1
0
 public bool IsSameAsCommand(FormattedTextCommand command)
 {
     return(command.CommandName == CommandName);
 }
Ejemplo n.º 2
0
        public static FormattedTextCommand[] ParseCommands(string parsableString)
        {
            var result           = new List <FormattedTextCommand>();
            var charIndex        = 0;
            var pendingToken     = new StringBuilder();
            var pendingCommand   = new StringBuilder();
            var pendingArguments = new StringBuilder();
            var currentState     = ParseState.ConsumingPlainText;

            while (charIndex < parsableString.Length)
            {
                var currentChar = parsableString[charIndex++];

                if (currentState == ParseState.ConsumingPlainText)
                {
                    if (currentChar == '[' && parsableString[charIndex] == '#')
                    {
                        result.Add(FormattedTextCommand.PlainText.WithArguments(pendingToken.ToString()));
                        pendingToken.Clear();
                        charIndex++;
                        currentState = ParseState.ConsumingCommand;
                    }
                    else
                    {
                        pendingToken.Append(currentChar);
                    }
                }
                else if (currentState == ParseState.ConsumingCommand)
                {
                    if (currentChar == ':')
                    {
                        currentState = ParseState.ConsumingArguments;
                    }
                    else
                    {
                        pendingCommand.Append(currentChar);
                    }
                }
                else if (currentState == ParseState.ConsumingArguments)
                {
                    if (currentChar == ']')
                    {
                        var command  = pendingCommand.ToString();
                        var argument = pendingArguments.ToString();

                        pendingCommand.Clear();
                        pendingArguments.Clear();
                        currentState = ParseState.ConsumingPlainText;

                        result.Add(FormattedTextCommand.FromName(command).WithArguments(argument));
                    }
                    else
                    {
                        pendingArguments.Append(currentChar);
                    }
                }
            }

            result.Add(FormattedTextCommand.PlainText.WithArguments(pendingToken.ToString()));
            return(result.ToArray());
        }