Add support for parsing multiple types of quotation marks in commands, Fix #942 (#943)

* Add ability to support different types of quotation marks

* Added normal quotation mark to list of aliases, removed single quote mark

* clean up leftover changes from testing

* change quotation mark parsing to use a map of matching pairs

* remove commented out code

* Fix conventions of the command parser utility functions

* change storage type of alias dictionary to be IReadOnlyDictionary

* revert type of CommandServiceConfig QuotationMarkAliasMap to Dictionary

* minor formatting changes to CommandParser

* remove unnecessary whitespace

* Move aliases outside of CommandInfo class

* copy IReadOnlyDictionary to ImmutableDictionary

* minor syntax changes in CommandServiceConfig

* add newline before namespace for consistency

* newline formatting tweak

* simplification of GetMatch method for CommandParser

* add more quote unicode punctuation pairs

* add check for null value when building ImmutableDictionary

* Move default alias map into a separate source file

* Ensure that the collection passed into command service is not null
This commit is contained in:
Chris Johnston
2018-05-24 17:07:37 -07:00
committed by Christopher F
parent b52af7ae7c
commit cee71ef35a
5 changed files with 134 additions and 10 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using System.Threading.Tasks;
@@ -13,8 +14,7 @@ namespace Discord.Commands
Parameter,
QuotedParameter
}
public static async Task<ParseResult> ParseArgsAsync(CommandInfo command, ICommandContext context, IServiceProvider services, string input, int startPos)
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);
@@ -24,7 +24,27 @@ namespace Discord.Commands
var argList = ImmutableArray.CreateBuilder<TypeReaderResult>();
var paramList = ImmutableArray.CreateBuilder<TypeReaderResult>();
bool isEscaping = false;
char c;
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++)
{
@@ -74,9 +94,11 @@ namespace Discord.Commands
argBuilder.Append(c);
continue;
}
if (c == '\"')
if (IsOpenQuote(aliasMap, c))
{
curPart = ParserPart.QuotedParameter;
matchQuote = GetMatch(aliasMap, c);
continue;
}
curPart = ParserPart.Parameter;
@@ -97,7 +119,7 @@ namespace Discord.Commands
}
else if (curPart == ParserPart.QuotedParameter)
{
if (c == '\"')
if (c == matchQuote)
{
argString = argBuilder.ToString(); //Remove quotes
lastArgEndPos = curPos + 1;