示例#1
0
        public async Task <ParseResult> Parse(IUserMessage context, SearchResult searchResult, PreconditionResult?preconditionResult = null)
        {
            if (!searchResult.IsSuccess)
            {
                return(ParseResult.FromError(searchResult));
            }
            if (preconditionResult != null && !preconditionResult.Value.IsSuccess)
            {
                return(ParseResult.FromError(preconditionResult.Value));
            }

            string input           = searchResult.Text;
            var    matchingAliases = Aliases.Where(alias => input.StartsWith(alias));

            string matchingAlias = "";

            foreach (string alias in matchingAliases)
            {
                if (alias.Length > matchingAlias.Length)
                {
                    matchingAlias = alias;
                }
            }

            input = input.Substring(matchingAlias.Length);

            return(await CommandParser.ParseArgs(this, context, input, 0).ConfigureAwait(false));
        }
示例#2
0
        public async Task <ParseResult> ParseAsync(ICommandContext context, int startIndex, SearchResult searchResult, PreconditionResult?preconditionResult = null)
        {
            if (!searchResult.IsSuccess)
            {
                return(ParseResult.FromError(searchResult));
            }
            if (preconditionResult != null && !preconditionResult.Value.IsSuccess)
            {
                return(ParseResult.FromError(preconditionResult.Value));
            }

            string input = searchResult.Text.Substring(startIndex);

            return(await CommandParser.ParseArgs(this, context, input, 0).ConfigureAwait(false));
        }
示例#3
0
        public async Task <ParseResult> ParseAsync(ICommandContext context, int startIndex, SearchResult searchResult, PreconditionResult preconditionResult = null, IServiceProvider services = null)
        {
            services = services ?? EmptyServiceProvider.Instance;

            if (!searchResult.IsSuccess)
            {
                return(ParseResult.FromError(searchResult));
            }
            if (preconditionResult != null && !preconditionResult.IsSuccess)
            {
                return(ParseResult.FromError(preconditionResult));
            }

            string input = searchResult.Text.Substring(startIndex);

            return(await CommandParser.ParseArgsAsync(this, context, _commandService._ignoreExtraArgs, services, input, 0).ConfigureAwait(false));
        }
示例#4
0
        public static async Task <ParseResult> ParseArgsAsync(CommandInfo command, ICommandContext context, bool ignoreExtraArgs, IServiceProvider services, string input, int startPos, IReadOnlyDictionary <char, char> aliasMap)
        {
            ParameterInfo curParam = null;
            StringBuilder argBuilder = new StringBuilder(input.Length);
            int           endPos = input.Length;
            var           curPart = ParserPart.None;
            int           lastArgEndPos = int.MinValue;
            var           argList = ImmutableArray.CreateBuilder <TypeReaderResult>();
            var           paramList = ImmutableArray.CreateBuilder <TypeReaderResult>();
            bool          isEscaping = false;
            char          c, matchQuote = '\0';

            // local helper functions
            bool IsOpenQuote(IReadOnlyDictionary <char, char> dict, char ch)
            {
                // return if the key is contained in the dictionary if it is populated
                if (dict.Count != 0)
                {
                    return(dict.ContainsKey(ch));
                }
                // or otherwise if it is the default double quote
                return(c == '\"');
            }

            char GetMatch(IReadOnlyDictionary <char, char> dict, char ch)
            {
                // get the corresponding value for the key, if it exists
                // and if the dictionary is populated
                if (dict.Count != 0 && dict.TryGetValue(c, out var value))
                {
                    return(value);
                }
                // or get the default pair of the default double quote
                return('\"');
            }

            for (int curPos = startPos; curPos <= endPos; curPos++)
            {
                if (curPos < endPos)
                {
                    c = input[curPos];
                }
                else
                {
                    c = '\0';
                }

                //If this character is escaped, skip it
                if (isEscaping)
                {
                    if (curPos != endPos)
                    {
                        argBuilder.Append(c);
                        isEscaping = false;
                        continue;
                    }
                }
                //Are we escaping the next character?
                if (c == '\\' && (curParam == null || !curParam.IsRemainder))
                {
                    isEscaping = true;
                    continue;
                }

                //If we're processing an remainder parameter, ignore all other logic
                if (curParam != null && curParam.IsRemainder && curPos != endPos)
                {
                    argBuilder.Append(c);
                    continue;
                }

                //If we're not currently processing one, are we starting the next argument yet?
                if (curPart == ParserPart.None)
                {
                    if (char.IsWhiteSpace(c) || curPos == endPos)
                    {
                        continue; //Skip whitespace between arguments
                    }
                    else if (curPos == lastArgEndPos)
                    {
                        return(ParseResult.FromError(CommandError.ParseFailed, "There must be at least one character of whitespace between arguments."));
                    }
                    else
                    {
                        if (curParam == null)
                        {
                            curParam = command.Parameters.Count > argList.Count ? command.Parameters[argList.Count] : null;
                        }

                        if (curParam != null && curParam.IsRemainder)
                        {
                            argBuilder.Append(c);
                            continue;
                        }

                        if (IsOpenQuote(aliasMap, c))
                        {
                            curPart    = ParserPart.QuotedParameter;
                            matchQuote = GetMatch(aliasMap, c);
                            continue;
                        }
                        curPart = ParserPart.Parameter;
                    }
                }

                //Has this parameter ended yet?
                string argString = null;
                if (curPart == ParserPart.Parameter)
                {
                    if (curPos == endPos || char.IsWhiteSpace(c))
                    {
                        argString     = argBuilder.ToString();
                        lastArgEndPos = curPos;
                    }
                    else
                    {
                        argBuilder.Append(c);
                    }
                }
                else if (curPart == ParserPart.QuotedParameter)
                {
                    if (c == matchQuote)
                    {
                        argString     = argBuilder.ToString(); //Remove quotes
                        lastArgEndPos = curPos + 1;
                    }
                    else
                    {
                        argBuilder.Append(c);
                    }
                }

                if (argString != null)
                {
                    if (curParam == null)
                    {
                        if (command.IgnoreExtraArgs)
                        {
                            break;
                        }
                        else
                        {
                            return(ParseResult.FromError(CommandError.BadArgCount, "The input text has too many parameters."));
                        }
                    }

                    var typeReaderResult = await curParam.ParseAsync(context, argString, services).ConfigureAwait(false);

                    if (!typeReaderResult.IsSuccess && typeReaderResult.Error != CommandError.MultipleMatches)
                    {
                        return(ParseResult.FromError(typeReaderResult));
                    }

                    if (curParam.IsMultiple)
                    {
                        paramList.Add(typeReaderResult);

                        curPart = ParserPart.None;
                    }
                    else
                    {
                        argList.Add(typeReaderResult);

                        curParam = null;
                        curPart  = ParserPart.None;
                    }
                    argBuilder.Clear();
                }
            }

            if (curParam != null && curParam.IsRemainder)
            {
                var typeReaderResult = await curParam.ParseAsync(context, argBuilder.ToString(), services).ConfigureAwait(false);

                if (!typeReaderResult.IsSuccess)
                {
                    return(ParseResult.FromError(typeReaderResult));
                }
                argList.Add(typeReaderResult);
            }

            if (isEscaping)
            {
                return(ParseResult.FromError(CommandError.ParseFailed, "Input text may not end on an incomplete escape."));
            }
            if (curPart == ParserPart.QuotedParameter)
            {
                return(ParseResult.FromError(CommandError.ParseFailed, "A quoted parameter is incomplete"));
            }

            //Add missing optionals
            for (int i = argList.Count; i < command.Parameters.Count; i++)
            {
                var param = command.Parameters[i];
                if (param.IsMultiple)
                {
                    continue;
                }
                if (!param.IsOptional)
                {
                    return(ParseResult.FromError(CommandError.BadArgCount, "The input text has too few parameters."));
                }
                argList.Add(TypeReaderResult.FromSuccess(param.DefaultValue));
            }

            return(ParseResult.FromSuccess(argList.ToImmutable(), paramList.ToImmutable()));
        }
示例#5
0
        public static async Task <ParseResult> ParseArgsAsync(CommandInfo command, ICommandContext context, IServiceProvider services, string input, int startPos)
        {
            ParameterInfo curParam      = null;
            StringBuilder argBuilder    = new StringBuilder(input.Length);
            int           endPos        = input.Length;
            var           curPart       = ParserPart.None;
            int           lastArgEndPos = int.MinValue;
            var           argList       = ImmutableArray.CreateBuilder <TypeReaderResult>();
            var           paramList     = ImmutableArray.CreateBuilder <TypeReaderResult>();
            bool          isEscaping    = false;
            char          c;

            for (int curPos = startPos; curPos <= endPos; curPos++)
            {
                if (curPos < endPos)
                {
                    c = input[curPos];
                }
                else
                {
                    c = '\0';
                }

                //If this character is escaped, skip it
                if (isEscaping)
                {
                    if (curPos != endPos)
                    {
                        argBuilder.Append(c);
                        isEscaping = false;
                        continue;
                    }
                }
                //Are we escaping the next character?
                if (c == '\\' && (curParam == null || !curParam.IsRemainder))
                {
                    isEscaping = true;
                    continue;
                }

                //If we're processing an remainder parameter, ignore all other logic
                if (curParam != null && curParam.IsRemainder && curPos != endPos)
                {
                    argBuilder.Append(c);
                    continue;
                }

                //If we're not currently processing one, are we starting the next argument yet?
                if (curPart == ParserPart.None)
                {
                    if (char.IsWhiteSpace(c) || curPos == endPos)
                    {
                        continue; //Skip whitespace between arguments
                    }
                    else if (curPos == lastArgEndPos)
                    {
                        return(ParseResult.FromError(CommandError.ParseFailed, "There must be at least one character of whitespace between arguments."));
                    }
                    else
                    {
                        if (curParam == null)
                        {
                            curParam = command.Parameters.Count > argList.Count ? command.Parameters[argList.Count] : null;
                        }

                        if (curParam != null && curParam.IsRemainder)
                        {
                            argBuilder.Append(c);
                            continue;
                        }
                        if (c == '\"')
                        {
                            curPart = ParserPart.QuotedParameter;
                            continue;
                        }
                        curPart = ParserPart.Parameter;
                    }
                }

                //Has this parameter ended yet?
                string argString = null;
                if (curPart == ParserPart.Parameter)
                {
                    if (curPos == endPos || char.IsWhiteSpace(c))
                    {
                        argString     = argBuilder.ToString();
                        lastArgEndPos = curPos;
                    }
                    else
                    {
                        argBuilder.Append(c);
                    }
                }
                else if (curPart == ParserPart.QuotedParameter)
                {
                    if (c == '\"')
                    {
                        argString     = argBuilder.ToString(); //Remove quotes
                        lastArgEndPos = curPos + 1;
                    }
                    else
                    {
                        argBuilder.Append(c);
                    }
                }

                if (argString != null)
                {
                    if (curParam == null)
                    {
                        return(ParseResult.FromError(CommandError.BadArgCount, "The input text has too many parameters."));
                    }

                    var typeReaderResult = await curParam.ParseAsync(context, argString, services).ConfigureAwait(false);

                    if (!typeReaderResult.IsSuccess && typeReaderResult.Error != CommandError.MultipleMatches)
                    {
                        return(ParseResult.FromError(typeReaderResult));
                    }

                    if (curParam.IsMultiple)
                    {
                        paramList.Add(typeReaderResult);

                        curPart = ParserPart.None;
                    }
                    else
                    {
                        argList.Add(typeReaderResult);

                        curParam = null;
                        curPart  = ParserPart.None;
                    }
                    argBuilder.Clear();
                }
            }

            if (curParam != null && curParam.IsRemainder)
            {
                var typeReaderResult = await curParam.ParseAsync(context, argBuilder.ToString(), services).ConfigureAwait(false);

                if (!typeReaderResult.IsSuccess)
                {
                    return(ParseResult.FromError(typeReaderResult));
                }
                argList.Add(typeReaderResult);
            }

            if (isEscaping)
            {
                return(ParseResult.FromError(CommandError.ParseFailed, "Input text may not end on an incomplete escape."));
            }
            if (curPart == ParserPart.QuotedParameter)
            {
                return(ParseResult.FromError(CommandError.ParseFailed, "A quoted parameter is incomplete"));
            }

            //Add missing optionals
            for (int i = argList.Count; i < command.Parameters.Count; i++)
            {
                var param = command.Parameters[i];
                if (param.IsMultiple)
                {
                    continue;
                }
                if (!param.IsOptional)
                {
                    return(ParseResult.FromError(CommandError.BadArgCount, "The input text has too few parameters."));
                }
                argList.Add(TypeReaderResult.FromSuccess(param.DefaultValue));
            }

            return(ParseResult.FromSuccess(argList.ToImmutable(), paramList.ToImmutable()));
        }