You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandParser.cs 7.7 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Immutable;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Discord.Commands
  7. {
  8. internal static class CommandParser
  9. {
  10. private enum ParserPart
  11. {
  12. None,
  13. Parameter,
  14. QuotedParameter
  15. }
  16. public static async Task<ParseResult> ParseArgsAsync(CommandInfo command, ICommandContext context, bool ignoreExtraArgs, IServiceProvider services, string input, int startPos)
  17. {
  18. ParameterInfo curParam = null;
  19. StringBuilder argBuilder = new StringBuilder(input.Length);
  20. int endPos = input.Length;
  21. var curPart = ParserPart.None;
  22. int lastArgEndPos = int.MinValue;
  23. var argList = ImmutableArray.CreateBuilder<TypeReaderResult>();
  24. var paramList = ImmutableArray.CreateBuilder<TypeReaderResult>();
  25. bool isEscaping = false;
  26. char c, matchQuote = '\0';
  27. // local helper functions
  28. bool IsOpenQuote(IReadOnlyDictionary<char, char> dict, char ch)
  29. {
  30. // return if the key is contained in the dictionary if it exists
  31. if (dict != null)
  32. return dict.ContainsKey(ch);
  33. // or otherwise if it is the default double quote
  34. return c == '\"';
  35. }
  36. char GetMatch(IReadOnlyDictionary<char, char> dict, char ch)
  37. {
  38. // get the corresponding value for the key, if it exists
  39. if (dict != null)
  40. {
  41. if (dict.TryGetValue(c, out var value))
  42. {
  43. return value;
  44. }
  45. }
  46. // or get the default pair of the default double quote
  47. return '\"';
  48. }
  49. for (int curPos = startPos; curPos <= endPos; curPos++)
  50. {
  51. if (curPos < endPos)
  52. c = input[curPos];
  53. else
  54. c = '\0';
  55. //If this character is escaped, skip it
  56. if (isEscaping)
  57. {
  58. if (curPos != endPos)
  59. {
  60. argBuilder.Append(c);
  61. isEscaping = false;
  62. continue;
  63. }
  64. }
  65. //Are we escaping the next character?
  66. if (c == '\\' && (curParam == null || !curParam.IsRemainder))
  67. {
  68. isEscaping = true;
  69. continue;
  70. }
  71. //If we're processing an remainder parameter, ignore all other logic
  72. if (curParam != null && curParam.IsRemainder && curPos != endPos)
  73. {
  74. argBuilder.Append(c);
  75. continue;
  76. }
  77. //If we're not currently processing one, are we starting the next argument yet?
  78. if (curPart == ParserPart.None)
  79. {
  80. if (char.IsWhiteSpace(c) || curPos == endPos)
  81. continue; //Skip whitespace between arguments
  82. else if (curPos == lastArgEndPos)
  83. return ParseResult.FromError(CommandError.ParseFailed, "There must be at least one character of whitespace between arguments.");
  84. else
  85. {
  86. if (curParam == null)
  87. curParam = command.Parameters.Count > argList.Count ? command.Parameters[argList.Count] : null;
  88. if (curParam != null && curParam.IsRemainder)
  89. {
  90. argBuilder.Append(c);
  91. continue;
  92. }
  93. if (IsOpenQuote(command._quotationAliases, c))
  94. {
  95. curPart = ParserPart.QuotedParameter;
  96. matchQuote = GetMatch(command._quotationAliases, c);
  97. continue;
  98. }
  99. curPart = ParserPart.Parameter;
  100. }
  101. }
  102. //Has this parameter ended yet?
  103. string argString = null;
  104. if (curPart == ParserPart.Parameter)
  105. {
  106. if (curPos == endPos || char.IsWhiteSpace(c))
  107. {
  108. argString = argBuilder.ToString();
  109. lastArgEndPos = curPos;
  110. }
  111. else
  112. argBuilder.Append(c);
  113. }
  114. else if (curPart == ParserPart.QuotedParameter)
  115. {
  116. if (c == matchQuote)
  117. {
  118. argString = argBuilder.ToString(); //Remove quotes
  119. lastArgEndPos = curPos + 1;
  120. }
  121. else
  122. argBuilder.Append(c);
  123. }
  124. if (argString != null)
  125. {
  126. if (curParam == null)
  127. {
  128. if (ignoreExtraArgs)
  129. break;
  130. else
  131. return ParseResult.FromError(CommandError.BadArgCount, "The input text has too many parameters.");
  132. }
  133. var typeReaderResult = await curParam.ParseAsync(context, argString, services).ConfigureAwait(false);
  134. if (!typeReaderResult.IsSuccess && typeReaderResult.Error != CommandError.MultipleMatches)
  135. return ParseResult.FromError(typeReaderResult);
  136. if (curParam.IsMultiple)
  137. {
  138. paramList.Add(typeReaderResult);
  139. curPart = ParserPart.None;
  140. }
  141. else
  142. {
  143. argList.Add(typeReaderResult);
  144. curParam = null;
  145. curPart = ParserPart.None;
  146. }
  147. argBuilder.Clear();
  148. }
  149. }
  150. if (curParam != null && curParam.IsRemainder)
  151. {
  152. var typeReaderResult = await curParam.ParseAsync(context, argBuilder.ToString(), services).ConfigureAwait(false);
  153. if (!typeReaderResult.IsSuccess)
  154. return ParseResult.FromError(typeReaderResult);
  155. argList.Add(typeReaderResult);
  156. }
  157. if (isEscaping)
  158. return ParseResult.FromError(CommandError.ParseFailed, "Input text may not end on an incomplete escape.");
  159. if (curPart == ParserPart.QuotedParameter)
  160. return ParseResult.FromError(CommandError.ParseFailed, "A quoted parameter is incomplete");
  161. //Add missing optionals
  162. for (int i = argList.Count; i < command.Parameters.Count; i++)
  163. {
  164. var param = command.Parameters[i];
  165. if (param.IsMultiple)
  166. continue;
  167. if (!param.IsOptional)
  168. return ParseResult.FromError(CommandError.BadArgCount, "The input text has too few parameters.");
  169. argList.Add(TypeReaderResult.FromSuccess(param.DefaultValue));
  170. }
  171. return ParseResult.FromSuccess(argList.ToImmutable(), paramList.ToImmutable());
  172. }
  173. }
  174. }