@jsprismarine/brigadier 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +57 -39
  3. package/dist/index.js +6 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/lib/AmbiguityConsumer.js +0 -0
  6. package/dist/lib/AmbiguityConsumer.js.map +0 -0
  7. package/dist/lib/Command.js +0 -0
  8. package/dist/lib/Command.js.map +0 -0
  9. package/dist/lib/CommandDispatcher.js +45 -28
  10. package/dist/lib/CommandDispatcher.js.map +1 -1
  11. package/dist/lib/ImmutableStringReader.js +0 -0
  12. package/dist/lib/ImmutableStringReader.js.map +0 -0
  13. package/dist/lib/LiteralMessage.js +0 -0
  14. package/dist/lib/LiteralMessage.js.map +0 -0
  15. package/dist/lib/Message.js +0 -0
  16. package/dist/lib/Message.js.map +0 -0
  17. package/dist/lib/ParseResults.js +0 -0
  18. package/dist/lib/ParseResults.js.map +0 -0
  19. package/dist/lib/Predicate.js +0 -0
  20. package/dist/lib/Predicate.js.map +0 -0
  21. package/dist/lib/RedirectModifier.js +0 -0
  22. package/dist/lib/RedirectModifier.js.map +0 -0
  23. package/dist/lib/ResultConsumer.js +0 -0
  24. package/dist/lib/ResultConsumer.js.map +0 -0
  25. package/dist/lib/SingleRedirectModifier.js +0 -0
  26. package/dist/lib/SingleRedirectModifier.js.map +0 -0
  27. package/dist/lib/StringReader.js +0 -0
  28. package/dist/lib/StringReader.js.map +0 -0
  29. package/dist/lib/arguments/ArgumentType.js +1 -1
  30. package/dist/lib/arguments/ArgumentType.js.map +1 -1
  31. package/dist/lib/arguments/BoolArgumentType.js +0 -0
  32. package/dist/lib/arguments/BoolArgumentType.js.map +0 -0
  33. package/dist/lib/arguments/FloatArgumentType.js +0 -0
  34. package/dist/lib/arguments/FloatArgumentType.js.map +0 -0
  35. package/dist/lib/arguments/IntegerArgumentType.js +0 -0
  36. package/dist/lib/arguments/IntegerArgumentType.js.map +0 -0
  37. package/dist/lib/arguments/StringArgumentType.js +0 -0
  38. package/dist/lib/arguments/StringArgumentType.js.map +0 -0
  39. package/dist/lib/builder/ArgumentBuilder.js +0 -0
  40. package/dist/lib/builder/ArgumentBuilder.js.map +0 -0
  41. package/dist/lib/builder/LiteralArgumentBuilder.js +0 -0
  42. package/dist/lib/builder/LiteralArgumentBuilder.js.map +1 -1
  43. package/dist/lib/builder/RequiredArgumentBuilder.js +0 -0
  44. package/dist/lib/builder/RequiredArgumentBuilder.js.map +1 -1
  45. package/dist/lib/context/CommandContext.js +4 -4
  46. package/dist/lib/context/CommandContext.js.map +1 -1
  47. package/dist/lib/context/CommandContextBuilder.js +0 -0
  48. package/dist/lib/context/CommandContextBuilder.js.map +0 -0
  49. package/dist/lib/context/ParsedArgument.js +0 -0
  50. package/dist/lib/context/ParsedArgument.js.map +0 -0
  51. package/dist/lib/context/ParsedCommandNode.js +0 -0
  52. package/dist/lib/context/ParsedCommandNode.js.map +0 -0
  53. package/dist/lib/context/StringRange.js +0 -0
  54. package/dist/lib/context/StringRange.js.map +0 -0
  55. package/dist/lib/context/SuggestionContext.js +0 -0
  56. package/dist/lib/context/SuggestionContext.js.map +0 -0
  57. package/dist/lib/exceptions/BuiltInExceptionProvider.js +0 -0
  58. package/dist/lib/exceptions/BuiltInExceptionProvider.js.map +0 -0
  59. package/dist/lib/exceptions/BuiltInExceptions.js +0 -0
  60. package/dist/lib/exceptions/BuiltInExceptions.js.map +0 -0
  61. package/dist/lib/exceptions/CommandExceptionType.js +0 -0
  62. package/dist/lib/exceptions/CommandExceptionType.js.map +0 -0
  63. package/dist/lib/exceptions/CommandSyntaxException.js +0 -0
  64. package/dist/lib/exceptions/CommandSyntaxException.js.map +0 -0
  65. package/dist/lib/exceptions/DynamicCommandExceptionType.js +0 -0
  66. package/dist/lib/exceptions/DynamicCommandExceptionType.js.map +0 -0
  67. package/dist/lib/exceptions/SimpleCommandExceptionType.js +0 -0
  68. package/dist/lib/exceptions/SimpleCommandExceptionType.js.map +0 -0
  69. package/dist/lib/suggestion/IntegerSuggestion.js +0 -0
  70. package/dist/lib/suggestion/IntegerSuggestion.js.map +0 -0
  71. package/dist/lib/suggestion/Suggestion.js +1 -1
  72. package/dist/lib/suggestion/Suggestion.js.map +1 -1
  73. package/dist/lib/suggestion/SuggestionProvider.js +0 -0
  74. package/dist/lib/suggestion/SuggestionProvider.js.map +0 -0
  75. package/dist/lib/suggestion/Suggestions.js +1 -1
  76. package/dist/lib/suggestion/Suggestions.js.map +1 -1
  77. package/dist/lib/suggestion/SuggestionsBuilder.js +0 -0
  78. package/dist/lib/suggestion/SuggestionsBuilder.js.map +0 -0
  79. package/dist/lib/tree/ArgumentCommandNode.js +17 -16
  80. package/dist/lib/tree/ArgumentCommandNode.js.map +1 -1
  81. package/dist/lib/tree/CommandNode.js +14 -13
  82. package/dist/lib/tree/CommandNode.js.map +1 -1
  83. package/dist/lib/tree/LiteralCommandNode.js +0 -0
  84. package/dist/lib/tree/LiteralCommandNode.js.map +0 -0
  85. package/dist/lib/tree/RootCommandNode.js +0 -0
  86. package/dist/lib/tree/RootCommandNode.js.map +0 -0
  87. package/dist/lib/util/isEqual.js +0 -0
  88. package/dist/lib/util/isEqual.js.map +0 -0
  89. package/index.js +0 -0
  90. package/package.json +55 -48
  91. package/.prettierrc +0 -10
  92. package/.renovaterc +0 -3
  93. package/CHANGELOG.md +0 -30
  94. package/dist/index.d.ts +0 -23
  95. package/dist/lib/AmbiguityConsumer.d.ts +0 -4
  96. package/dist/lib/Command.d.ts +0 -4
  97. package/dist/lib/CommandDispatcher.d.ts +0 -29
  98. package/dist/lib/ImmutableStringReader.d.ts +0 -12
  99. package/dist/lib/LiteralMessage.d.ts +0 -7
  100. package/dist/lib/Message.d.ts +0 -3
  101. package/dist/lib/ParseResults.d.ts +0 -13
  102. package/dist/lib/Predicate.d.ts +0 -3
  103. package/dist/lib/Primitive.js +0 -2
  104. package/dist/lib/RedirectModifier.d.ts +0 -4
  105. package/dist/lib/ResultConsumer.d.ts +0 -4
  106. package/dist/lib/SingleRedirectModifier.d.ts +0 -4
  107. package/dist/lib/StringReader.d.ts +0 -27
  108. package/dist/lib/arguments/ArgumentType.d.ts +0 -21
  109. package/dist/lib/arguments/BoolArgumentType.d.ts +0 -13
  110. package/dist/lib/arguments/FloatArgumentType.d.ts +0 -16
  111. package/dist/lib/arguments/IntegerArgumentType.d.ts +0 -16
  112. package/dist/lib/arguments/StringArgumentType.d.ts +0 -21
  113. package/dist/lib/builder/ArgumentBuilder.d.ts +0 -28
  114. package/dist/lib/builder/LiteralArgumentBuilder.d.ts +0 -11
  115. package/dist/lib/builder/RequiredArgumentBuilder.d.ts +0 -18
  116. package/dist/lib/context/CommandContext.d.ts +0 -33
  117. package/dist/lib/context/CommandContextBuilder.d.ts +0 -38
  118. package/dist/lib/context/ParsedArgument.d.ts +0 -9
  119. package/dist/lib/context/ParsedCommandNode.d.ts +0 -11
  120. package/dist/lib/context/StringRange.d.ts +0 -16
  121. package/dist/lib/context/SuggestionContext.d.ts +0 -6
  122. package/dist/lib/exceptions/BuiltInExceptionProvider.d.ts +0 -23
  123. package/dist/lib/exceptions/BuiltInExceptions.d.ts +0 -43
  124. package/dist/lib/exceptions/CommandExceptionType.d.ts +0 -2
  125. package/dist/lib/exceptions/CommandSyntaxException.d.ts +0 -19
  126. package/dist/lib/exceptions/Dynamic2CommandExceptionType.js +0 -19
  127. package/dist/lib/exceptions/DynamicCommandExceptionType.d.ts +0 -10
  128. package/dist/lib/exceptions/SimpleCommandExceptionType.d.ts +0 -11
  129. package/dist/lib/suggestion/IntegerSuggestion.d.ts +0 -12
  130. package/dist/lib/suggestion/Suggestion.d.ts +0 -17
  131. package/dist/lib/suggestion/SuggestionProvider.d.ts +0 -6
  132. package/dist/lib/suggestion/Suggestions.d.ts +0 -16
  133. package/dist/lib/suggestion/SuggestionsBuilder.d.ts +0 -18
  134. package/dist/lib/tree/ArgumentCommandNode.d.ts +0 -31
  135. package/dist/lib/tree/CommandNode.d.ts +0 -45
  136. package/dist/lib/tree/LiteralCommandNode.d.ts +0 -27
  137. package/dist/lib/tree/RootCommandNode.d.ts +0 -21
  138. package/dist/lib/util/isEqual.d.ts +0 -1
  139. package/example/fill.js +0 -47
  140. package/example/fill.ts +0 -50
  141. package/src/index.ts +0 -50
  142. package/src/lib/AmbiguityConsumer.ts +0 -6
  143. package/src/lib/Command.ts +0 -5
  144. package/src/lib/CommandDispatcher.ts +0 -390
  145. package/src/lib/ImmutableStringReader.ts +0 -22
  146. package/src/lib/LiteralMessage.ts +0 -17
  147. package/src/lib/Message.ts +0 -3
  148. package/src/lib/ParseResults.ts +0 -32
  149. package/src/lib/Predicate.ts +0 -3
  150. package/src/lib/RedirectModifier.ts +0 -5
  151. package/src/lib/ResultConsumer.ts +0 -6
  152. package/src/lib/SingleRedirectModifier.ts +0 -6
  153. package/src/lib/StringReader.ts +0 -210
  154. package/src/lib/arguments/ArgumentType.ts +0 -27
  155. package/src/lib/arguments/BoolArgumentType.ts +0 -41
  156. package/src/lib/arguments/FloatArgumentType.ts +0 -75
  157. package/src/lib/arguments/IntegerArgumentType.ts +0 -75
  158. package/src/lib/arguments/StringArgumentType.ts +0 -83
  159. package/src/lib/builder/ArgumentBuilder.ts +0 -94
  160. package/src/lib/builder/LiteralArgumentBuilder.ts +0 -35
  161. package/src/lib/builder/RequiredArgumentBuilder.ts +0 -54
  162. package/src/lib/context/CommandContext.ts +0 -118
  163. package/src/lib/context/CommandContextBuilder.ts +0 -155
  164. package/src/lib/context/ParsedArgument.ts +0 -27
  165. package/src/lib/context/ParsedCommandNode.ts +0 -35
  166. package/src/lib/context/StringRange.ts +0 -57
  167. package/src/lib/context/SuggestionContext.ts +0 -12
  168. package/src/lib/exceptions/BuiltInExceptionProvider.ts +0 -43
  169. package/src/lib/exceptions/BuiltInExceptions.ts +0 -121
  170. package/src/lib/exceptions/CommandExceptionType.ts +0 -2
  171. package/src/lib/exceptions/CommandSyntaxException.ts +0 -77
  172. package/src/lib/exceptions/DynamicCommandExceptionType.ts +0 -22
  173. package/src/lib/exceptions/SimpleCommandExceptionType.ts +0 -26
  174. package/src/lib/suggestion/IntegerSuggestion.ts +0 -46
  175. package/src/lib/suggestion/Suggestion.ts +0 -87
  176. package/src/lib/suggestion/SuggestionProvider.ts +0 -7
  177. package/src/lib/suggestion/Suggestions.ts +0 -85
  178. package/src/lib/suggestion/SuggestionsBuilder.ts +0 -69
  179. package/src/lib/tree/ArgumentCommandNode.ts +0 -118
  180. package/src/lib/tree/CommandNode.ts +0 -200
  181. package/src/lib/tree/LiteralCommandNode.ts +0 -111
  182. package/src/lib/tree/RootCommandNode.ts +0 -62
  183. package/src/lib/util/isEqual.ts +0 -75
  184. package/test/CommandDispatcherTest.ts +0 -427
  185. package/test/CommandDispatcherUsagesTest.ts +0 -163
  186. package/test/CommandSuggestionsTest.ts +0 -283
  187. package/test/StringReaderTest.ts +0 -391
  188. package/test/arguments/BoolArgumentTypeTest.ts +0 -18
  189. package/test/arguments/FloatArgumentTypeTest.ts +0 -61
  190. package/test/arguments/IntegerArgumentTypeTest.ts +0 -63
  191. package/test/arguments/StringArgumentTypeTest.ts +0 -61
  192. package/test/benchmarks/ExecuteBenchmark.js +0 -33
  193. package/test/benchmarks/ParsingBenchmark.js +0 -96
  194. package/test/builder/ArgumentBuilderTest.ts +0 -61
  195. package/test/builder/LiteralArgumentBuilderTest.ts +0 -37
  196. package/test/builder/RequiredArgumentBuilderTest.ts +0 -40
  197. package/test/context/CommandContextTest.ts +0 -77
  198. package/test/context/ParsedArgumentTest.ts +0 -19
  199. package/test/exceptions/DynamicCommandSyntaxExceptionTypeTest.ts +0 -18
  200. package/test/exceptions/SimpleCommandSyntaxExceptionTypeTest.ts +0 -33
  201. package/test/suggestion/SuggestionBuilderTest.ts +0 -66
  202. package/test/suggestion/SuggestionTest.ts +0 -65
  203. package/test/suggestion/SuggestionsTest.ts +0 -24
  204. package/test/tree/AbstractCommandNodeTest.ts +0 -57
  205. package/test/tree/ArgumentCommandNodeTest.ts +0 -81
  206. package/test/tree/LiteralCommandNodeTest.ts +0 -112
  207. package/test/tree/RootCommandNodeTest.ts +0 -70
  208. package/test/utils/testEquality.ts +0 -10
  209. package/tsconfig.json +0 -40
  210. package/tsconfig.tsbuildinfo +0 -1578
@@ -1,26 +0,0 @@
1
- import Message from "../Message"
2
- import ImmutableStringReader from "../ImmutableStringReader"
3
- import CommandExceptionType from "./CommandExceptionType"
4
- import CommandSyntaxException from "./CommandSyntaxException";
5
-
6
- export default class SimpleCommandExceptionType implements CommandExceptionType {
7
-
8
- private message: Message;
9
-
10
- public constructor (message: Message) {
11
- this.message = message;
12
- Error.captureStackTrace(this, SimpleCommandExceptionType)
13
- }
14
-
15
- public create(): CommandSyntaxException {
16
- return new CommandSyntaxException(this, this.message)
17
- }
18
-
19
- public createWithContext(reader: ImmutableStringReader): CommandSyntaxException {
20
- return new CommandSyntaxException(this, this.message, reader.getString(), reader.getCursor());
21
- }
22
-
23
- public toString(): string {
24
- return this.message.getString();
25
- }
26
- }
@@ -1,46 +0,0 @@
1
- import Message from "../Message"
2
- import StringRange from "../context/StringRange"
3
- import Suggestion from "./Suggestion"
4
-
5
- export default class IntegerSuggestion extends Suggestion {
6
-
7
- private value: number;
8
-
9
- public constructor (range: StringRange, value: number, tooltip: Message = null) {
10
- super(range, value.toString(), tooltip);
11
- this.value = value;
12
- }
13
-
14
- public getValue(): number {
15
- return this.value;
16
- }
17
-
18
- public equals(o: object): boolean {
19
- if (this === o) return true;
20
-
21
- if (!(o instanceof IntegerSuggestion)) return false;
22
-
23
- return this.value == o.value && super.equals(o);
24
- }
25
-
26
- public toString(): String {
27
- return "IntegerSuggestion{" +
28
- "value=" + this.value +
29
- ", range=" + this.getRange() +
30
- ", text='" + this.getText() + '\'' +
31
- ", tooltip='" + this.getTooltip() + '\'' +
32
- '}';
33
- }
34
-
35
- public compareTo(o: Suggestion): number {
36
- if (o instanceof IntegerSuggestion) {
37
- return this.value < o.value ? 1 : - 1;
38
- }
39
-
40
- return super.compareTo(o);
41
- }
42
-
43
- public compareToIgnoreCase(b: Suggestion): number {
44
- return this.compareTo(b);
45
- }
46
- }
@@ -1,87 +0,0 @@
1
- import isEqual from "../util/isEqual"
2
- import Message from "../Message"
3
- import StringRange from "../context/StringRange"
4
-
5
- export default class Suggestion {
6
-
7
- private range: StringRange;
8
- private text: string;
9
- private tooltip: Message;
10
-
11
- public constructor (range: StringRange, text: string, tooltip: Message = null) {
12
- this.range = range;
13
- this.text = text;
14
- this.tooltip = tooltip;
15
- }
16
-
17
- public getRange(): StringRange {
18
- return this.range;
19
- }
20
-
21
- public getText(): string {
22
- return this.text;
23
- }
24
-
25
- public getTooltip(): Message {
26
- return this.tooltip;
27
- }
28
-
29
- public apply(input: string): string {
30
- if (this.range.getStart() === 0 && this.range.getEnd() == input.length) {
31
- return this.text;
32
- }
33
-
34
- let result = "";
35
- if (this.range.getStart() > 0) {
36
- result += input.substring(0, this.range.getStart());
37
- }
38
-
39
- result += this.text;
40
- if (this.range.getEnd() < input.length) {
41
- result += input.substring(this.range.getEnd());
42
- }
43
-
44
- return result;
45
- }
46
-
47
- public equals(o: object): boolean {
48
- if (this === o) return true;
49
- if (!(o instanceof Suggestion)) return false;
50
-
51
- return isEqual(this.range, o.range) && (this.text === o.text) && isEqual(this.tooltip, o.tooltip);
52
- }
53
-
54
- public toString(): String {
55
- return "Suggestion{" +
56
- "range=" + this.range +
57
- ", text='" + this.text + '\'' +
58
- ", tooltip='" + this.tooltip + '\'' +
59
- '}';
60
- }
61
-
62
- public compareTo(o: Suggestion): number {
63
- return this.text < o.text ? 1 : -1;
64
- }
65
-
66
- public compareToIgnoreCase(b: Suggestion): number {
67
- return this.text.toLowerCase() < b.text.toLowerCase() ? 1 : -1;
68
- }
69
-
70
- public expand(command: string, range: StringRange): Suggestion {
71
- if (range.equals(this.range)) {
72
- return this;
73
- }
74
-
75
- let result = "";
76
- if (range.getStart() < this.range.getStart()) {
77
- result += command.substring(range.getStart(), this.range.getStart());
78
- }
79
-
80
- result += this.text;
81
- if (range.getEnd() > this.range.getEnd()) {
82
- result += command.substring(this.range.getEnd(), range.getEnd());
83
- }
84
-
85
- return new Suggestion(range, result, this.tooltip);
86
- }
87
- }
@@ -1,7 +0,0 @@
1
- import Suggestions from "./Suggestions"
2
- import SuggestionsBuilder from "./SuggestionsBuilder"
3
- import CommandContext from "../context/CommandContext"
4
-
5
- export default interface SuggestionProvider<S> {
6
- getSuggestions(context: CommandContext<S>, builder: SuggestionsBuilder): Promise<Suggestions>;
7
- }
@@ -1,85 +0,0 @@
1
- import isEqual from "../util/isEqual"
2
- import StringRange from "../context/StringRange"
3
- import Suggestion from "./Suggestion"
4
-
5
- export default class Suggestions {
6
-
7
- private static EMPTY = new Suggestions(StringRange.at(0), []);
8
-
9
- private range: StringRange;
10
-
11
- private suggestions: Array<Suggestion>;
12
-
13
- public constructor (range: StringRange, suggestions: Array<Suggestion>) {
14
- this.range = range;
15
- this.suggestions = suggestions;
16
- }
17
-
18
- public getRange(): StringRange {
19
- return this.range;
20
- }
21
-
22
- public getList(): Array<Suggestion> {
23
- return this.suggestions;
24
- }
25
-
26
- public isEmpty(): boolean {
27
- return this.suggestions.length === 0;
28
- }
29
-
30
- public equals(o: object): boolean {
31
- if (this === o) return true;
32
-
33
- if (!(o instanceof Suggestions)) return false;
34
-
35
- return this.range.equals(o.range) && isEqual(this.suggestions, o.suggestions);
36
- }
37
-
38
- public toString(): String {
39
- return "Suggestions{" +
40
- "range=" + this.range +
41
- ", suggestions=" + this.suggestions + '}';
42
- }
43
-
44
- public static empty(): Promise<Suggestions> {
45
- return Promise.resolve(this.EMPTY);
46
- }
47
-
48
- public static merge(command: string, input: Array<Suggestions>): Suggestions {
49
- if (input.length === 0) {
50
- return this.EMPTY;
51
- }
52
- else if (input.length === 1) {
53
- return input[0];
54
- }
55
-
56
- const texts = [];
57
- for (let suggestions of input) {
58
- texts.push(...suggestions.getList());
59
- }
60
-
61
- return Suggestions.create(command, texts);
62
- }
63
-
64
- public static create(command: string, suggestions: Array<Suggestion>): Suggestions {
65
- if (suggestions.length === 0) {
66
- return this.EMPTY;
67
- }
68
-
69
- let start = Infinity;
70
- let end = -Infinity;
71
- for (let suggestion of suggestions) {
72
- start = Math.min(suggestion.getRange().getStart(), start);
73
- end = Math.max(suggestion.getRange().getEnd(), end);
74
- }
75
-
76
- let range = new StringRange(start, end);
77
- const texts = [];
78
- for (let suggestion of suggestions) {
79
- texts.push(suggestion.expand(command, range));
80
- }
81
-
82
- const sorted = texts.sort((a, b) => a.compareToIgnoreCase(b));
83
- return new Suggestions(range, sorted);
84
- }
85
- }
@@ -1,69 +0,0 @@
1
- import Message from "../Message"
2
- import StringRange from "../context/StringRange"
3
- import Suggestion from "./Suggestion"
4
- import Suggestions from "./Suggestions"
5
- import IntegerSuggestion from "./IntegerSuggestion"
6
-
7
- export default class SuggestionsBuilder {
8
-
9
- private input: string;
10
-
11
- private start: number;
12
-
13
- private remaining: string;
14
-
15
- private result: Array<Suggestion> = [];
16
-
17
- public constructor (input: string, start: number) {
18
- this.input = input;
19
- this.start = start;
20
- this.remaining = input.substring(start);
21
- }
22
-
23
- public getInput(): String {
24
- return this.input;
25
- }
26
-
27
- public getStart(): number {
28
- return this.start;
29
- }
30
-
31
- public getRemaining(): string {
32
- return this.remaining;
33
- }
34
-
35
- public build(): Suggestions {
36
- return Suggestions.create(this.input, this.result);
37
- }
38
-
39
- public buildPromise(): Promise<Suggestions> {
40
- return Promise.resolve(this.build());
41
- }
42
-
43
- public suggest(text: string | number, tooltip: Message = null): SuggestionsBuilder {
44
-
45
- if(typeof text === "number") {
46
- this.result.push(new IntegerSuggestion(StringRange.between(this.start, this.input.length), text, tooltip));
47
- return this;
48
- }
49
-
50
- if (text === this.remaining)
51
- return this;
52
-
53
- this.result.push(new Suggestion(StringRange.between(this.start, this.input.length), text, tooltip));
54
- return this;
55
- }
56
-
57
- public add(other: SuggestionsBuilder): SuggestionsBuilder {
58
- this.result.push(...other.result);
59
- return this;
60
- }
61
-
62
- public createOffset(start: number): SuggestionsBuilder {
63
- return new SuggestionsBuilder(this.input, this.start);
64
- }
65
-
66
- public restart(): SuggestionsBuilder {
67
- return new SuggestionsBuilder(this.input, this.start);
68
- }
69
- }
@@ -1,118 +0,0 @@
1
- import isEqual from "../util/isEqual"
2
- import Command from "../Command"
3
- import Predicate from "../Predicate"
4
- import RedirectModifier from "../RedirectModifier"
5
- import StringReader from "../StringReader"
6
- import ArgumentType from "../arguments/ArgumentType"
7
- import RequiredArgumentBuilder from "../builder/RequiredArgumentBuilder"
8
- import CommandContext from "../context/CommandContext"
9
- import ParsedArgument from "../context/ParsedArgument"
10
- import CommandContextBuilder from "../context/CommandContextBuilder"
11
- import Suggestions from "../suggestion/Suggestions"
12
- import SuggestionProvider from "../suggestion/SuggestionProvider"
13
- import SuggestionsBuilder from "../suggestion/SuggestionsBuilder"
14
- import CommandNode from "./CommandNode"
15
-
16
- const USAGE_ARGUMENT_OPEN: string = "<";
17
- const USAGE_ARGUMENT_CLOSE: string = ">";
18
-
19
- export default class ArgumentCommandNode<S, T> extends CommandNode<S> {
20
-
21
- private name: string;
22
- private type: ArgumentType<T>;
23
- private customSuggestions: SuggestionProvider<S>;
24
-
25
- public constructor(name: string, type: ArgumentType<T>, command: Command<S>, requirement: Predicate<S>, redirect: CommandNode<S>, modifier: RedirectModifier<S>, forks: boolean, customSuggestions: SuggestionProvider<S>) {
26
- super(command, requirement, redirect, modifier, forks);
27
- this.name = name;
28
- this.type = type;
29
- this.customSuggestions = customSuggestions;
30
- }
31
-
32
- public getNodeType(): string {
33
- return "argument"
34
- }
35
-
36
- public getType(): ArgumentType<T> {
37
- return this.type;
38
- }
39
-
40
- public getName(): string {
41
- return this.name;
42
- }
43
-
44
- public getUsageText(): string {
45
- return USAGE_ARGUMENT_OPEN + this.name + USAGE_ARGUMENT_CLOSE;
46
- }
47
-
48
- public getCustomSuggestions(): SuggestionProvider<S> {
49
- return this.customSuggestions;
50
- }
51
-
52
- public parse(reader: StringReader, contextBuilder: CommandContextBuilder<S>) {
53
- let start = reader.getCursor();
54
- let result: T = this.type.parse(reader);
55
- let parsed: ParsedArgument<S, T> = new ParsedArgument(start, reader.getCursor(), result);
56
- contextBuilder.withArgument(this.name, parsed);
57
- contextBuilder.withNode(this, parsed.getRange());
58
- }
59
-
60
- public listSuggestions(context: CommandContext<S>, builder: SuggestionsBuilder): Promise<Suggestions> {
61
- if (this.customSuggestions == null) {
62
- if (typeof this.type.listSuggestions === "function")
63
- return this.type.listSuggestions(context, builder);
64
- else
65
- return Suggestions.empty();
66
- }
67
- else {
68
- return this.customSuggestions.getSuggestions(context, builder);
69
- }
70
-
71
- }
72
-
73
- public createBuilder(): RequiredArgumentBuilder<S, T> {
74
- let builder: RequiredArgumentBuilder<S, T> = RequiredArgumentBuilder.argument(this.name, this.type);
75
- builder.requires(this.getRequirement());
76
- builder.forward(this.getRedirect(), this.getRedirectModifier(), this.isFork());
77
- builder.suggests(this.customSuggestions);
78
- if (this.getCommand() != null) {
79
- builder.executes(this.getCommand());
80
- }
81
-
82
- return builder;
83
- }
84
-
85
- public isValidInput(input: string): boolean {
86
- try {
87
- let reader: StringReader = new StringReader(input);
88
- this.type.parse(reader);
89
- return !reader.canRead() || reader.peek() == ' ';
90
- }
91
- catch (ignored) {
92
- }
93
-
94
- return false;
95
- }
96
-
97
- public equals(o: object): boolean {
98
- if (this === o) return true;
99
- if (!(o instanceof ArgumentCommandNode)) return false;
100
-
101
- if (!(this.name === o.name)) return false;
102
- if (!isEqual(this.type, o.type)) return false;
103
-
104
- return super.equals(o);
105
- }
106
-
107
- public getSortedKey(): string {
108
- return this.name;
109
- }
110
-
111
- public getExamples(): Iterable<string> {
112
- return typeof this.type.getExamples === "function" ? this.type.getExamples() : [];
113
- }
114
-
115
- public toString(): string {
116
- return "<argument " + this.name + ":" + this.type + ">";
117
- }
118
- }
@@ -1,200 +0,0 @@
1
- import isEqual from "../util/isEqual"
2
- import Predicate from "../Predicate"
3
- import AmbiguityConsumer from "../AmbiguityConsumer"
4
- import Command from "../Command"
5
- import RedirectModifier from "../RedirectModifier"
6
- import StringReader from "../StringReader"
7
- import ArgumentBuilder from "../builder/ArgumentBuilder"
8
- import CommandContext from "../context/CommandContext"
9
- import CommandContextBuilder from "../context/CommandContextBuilder"
10
- import Suggestions from "../suggestion/Suggestions"
11
- import SuggestionsBuilder from "../suggestion/SuggestionsBuilder"
12
-
13
- abstract class CommandNode<S> {
14
-
15
- private children: Map<string, CommandNode<S>> = new Map();
16
-
17
- private literals: Map<string, any> = new Map();
18
-
19
- private args: Map<string, any> = new Map();
20
-
21
- private requirement: Predicate<S>;
22
-
23
- private redirect: CommandNode<S>;
24
-
25
- private modifier: RedirectModifier<S>;
26
-
27
- private forks: boolean;
28
-
29
- private command: Command<S>;
30
-
31
- public abstract getNodeType(): string;
32
-
33
- public constructor (command: Command<S>, requirement: Predicate<S>, redirect: CommandNode<S>, modifier: RedirectModifier<S>, forks: boolean) {
34
- this.command = command;
35
- this.requirement = requirement || (() => true);
36
- this.redirect = redirect;
37
- this.modifier = modifier;
38
- this.forks = forks;
39
- }
40
-
41
- public getCommand(): Command<S> {
42
- return this.command;
43
- }
44
-
45
- public getChildren(): IterableIterator<CommandNode<S>> {
46
- return this.children.values();
47
- }
48
-
49
- public getChildrenCount(): number {
50
- return this.children.size;
51
- }
52
-
53
- public getChild(name: string): CommandNode<S> {
54
- return this.children.get(name);
55
- }
56
-
57
- public getRedirect(): CommandNode<S> {
58
- return this.redirect;
59
- }
60
-
61
- public getRedirectModifier(): RedirectModifier<S> {
62
- return this.modifier;
63
- }
64
-
65
- public canUse(source: S): boolean {
66
- return this.requirement(source);
67
- }
68
-
69
- public addChild(node: CommandNode<S>) {
70
- if (node.getNodeType() === "root") {
71
- throw new Error("Cannot add a RootCommandNode as a child to any other CommandNode");
72
- }
73
-
74
- let child= this.children.get(node.getName());
75
- if (child != null) {
76
- // We've found something to merge onto
77
- if ((node.getCommand() != null)) {
78
- child.command = node.getCommand();
79
- }
80
-
81
- for (let grandchild of node.getChildren()) {
82
- child.addChild(grandchild);
83
- }
84
-
85
- }
86
- else {
87
- this.children.set(node.getName(), node);
88
- if (node.getNodeType() === "literal") {
89
- this.literals.set(node.getName(), node);
90
- }
91
- else if (node.getNodeType() === "argument") {
92
- this.args.set(node.getName(), node);
93
- }
94
-
95
- }
96
-
97
- this.children = new Map([...this.children.entries()].sort((a, b) => a[1].compareTo(b[1])))
98
- }
99
-
100
- public findAmbiguities(consumer: AmbiguityConsumer<S>) {
101
- let matches: Array<string> = [];
102
- for (let child of this.children.values()) {
103
- for (let sibling of this.children.values()) {
104
- if (child === sibling) {
105
- continue;
106
- }
107
-
108
- for (let input of child.getExamples()) {
109
- if (sibling.isValidInput(input)) {
110
- matches.push(input);
111
- }
112
- }
113
-
114
- if (matches.length > 0) {
115
- consumer.ambiguous(this, child, sibling, matches);
116
- matches = [];
117
- }
118
-
119
- }
120
-
121
- child.findAmbiguities(consumer);
122
- }
123
-
124
- }
125
-
126
- public abstract isValidInput(input: string): boolean;
127
-
128
- public equals(o: object): boolean {
129
- if (this === o) return true;
130
- if (!(o instanceof CommandNode)) return false;
131
-
132
- if (this.children.size !== o.children.size) {
133
- return false;
134
- }
135
-
136
- if (!isEqual(this.children, o.children)) return false;
137
-
138
- if (this.command != null ? !isEqual(this.command, o.command) : o.command != null)
139
- return false;
140
-
141
- return true;
142
- }
143
-
144
- public getRequirement() {
145
- return this.requirement;
146
- }
147
-
148
- public abstract getName(): string;
149
-
150
- public abstract getUsageText(): string;
151
-
152
- public abstract parse(reader: StringReader, contextBuilder: CommandContextBuilder<S>): void;
153
-
154
- public abstract listSuggestions(context: CommandContext<S>, builder: SuggestionsBuilder): Promise<Suggestions>;
155
-
156
- public abstract createBuilder(): ArgumentBuilder<S, any>;
157
-
158
- public abstract getSortedKey(): string;
159
-
160
- public getRelevantNodes(input: StringReader): Iterable<CommandNode<S>> {
161
- if (this.literals.size > 0) {
162
- let cursor: number = input.getCursor();
163
- while ((input.canRead()
164
- && (input.peek() != ' '))) {
165
- input.skip();
166
- }
167
-
168
- let text = input.getString().substring(cursor, input.getCursor());
169
- input.setCursor(cursor);
170
- let literal = this.literals.get(text);
171
- if (literal != null) {
172
- return[ literal ];
173
- }
174
- else {
175
- return this.args.values();
176
- }
177
-
178
- }
179
- else {
180
- return this.args.values();
181
- }
182
-
183
- }
184
-
185
- public compareTo(o: CommandNode<S>): number {
186
- if (this.getNodeType() === o.getNodeType()) {
187
- return this.getSortedKey() > o.getSortedKey() ? 1 : -1;
188
- }
189
-
190
- return (o.getNodeType() === "literal") ? 1 : -1;
191
- }
192
-
193
- public isFork(): boolean {
194
- return this.forks;
195
- }
196
-
197
- public abstract getExamples(): Iterable<string>;
198
- }
199
-
200
- export default CommandNode;