@wq2/brigadier-ts 1.0.0 → 1.0.2

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 (90) hide show
  1. package/README.md +1 -0
  2. package/dist/Command.d.ts +1 -1
  3. package/dist/CommandDispatcher.d.ts +1 -1
  4. package/dist/CommandDispatcher.js +231 -357
  5. package/dist/ParseResults.d.ts +1 -1
  6. package/dist/ParseResults.js +9 -10
  7. package/dist/StringReader.d.ts +1 -1
  8. package/dist/StringReader.js +75 -77
  9. package/dist/arguments/ArgumentType.d.ts +1 -1
  10. package/dist/arguments/ArgumentType.js +9 -8
  11. package/dist/arguments/BoolArgumentType.d.ts +5 -1
  12. package/dist/arguments/BoolArgumentType.js +9 -27
  13. package/dist/arguments/FloatArgumentType.d.ts +1 -1
  14. package/dist/arguments/FloatArgumentType.js +11 -30
  15. package/dist/arguments/IntegerArgumentType.d.ts +1 -1
  16. package/dist/arguments/IntegerArgumentType.js +11 -30
  17. package/dist/arguments/LongArgumentType.d.ts +3 -3
  18. package/dist/arguments/LongArgumentType.js +13 -32
  19. package/dist/arguments/NumberArgumentType.d.ts +2 -2
  20. package/dist/arguments/NumberArgumentType.js +15 -33
  21. package/dist/arguments/StringArgumentType.d.ts +1 -1
  22. package/dist/arguments/StringArgumentType.js +11 -29
  23. package/dist/builder/ArgumentBuilder.d.ts +1 -1
  24. package/dist/builder/ArgumentBuilder.js +31 -71
  25. package/dist/builder/LiteralArgumentBuilder.js +14 -33
  26. package/dist/builder/RequiredArgumentBuilder.d.ts +1 -1
  27. package/dist/builder/RequiredArgumentBuilder.js +18 -37
  28. package/dist/context/CommandContext.d.ts +1 -1
  29. package/dist/context/CommandContext.js +31 -32
  30. package/dist/context/CommandContextBuilder.d.ts +1 -1
  31. package/dist/context/CommandContextBuilder.js +47 -50
  32. package/dist/context/ParsedArgument.js +8 -9
  33. package/dist/context/ParsedCommandNode.d.ts +1 -1
  34. package/dist/context/ParsedCommandNode.js +7 -8
  35. package/dist/context/StringRange.js +17 -18
  36. package/dist/context/SuggestionContext.d.ts +1 -1
  37. package/dist/context/SuggestionContext.js +3 -4
  38. package/dist/exceptions/CommandErrorType.d.ts +3 -3
  39. package/dist/exceptions/CommandErrorType.js +10 -19
  40. package/dist/exceptions/CommandSyntaxError.js +36 -54
  41. package/dist/suggestion/Suggestion.d.ts +1 -1
  42. package/dist/suggestion/Suggestion.js +16 -17
  43. package/dist/suggestion/Suggestions.d.ts +1 -1
  44. package/dist/suggestion/Suggestions.js +28 -30
  45. package/dist/suggestion/SuggestionsBuilder.d.ts +1 -1
  46. package/dist/suggestion/SuggestionsBuilder.js +22 -23
  47. package/dist/tree/ArgumentCommandNode.d.ts +1 -1
  48. package/dist/tree/ArgumentCommandNode.js +22 -40
  49. package/dist/tree/CommandNode.d.ts +1 -1
  50. package/dist/tree/CommandNode.js +28 -29
  51. package/dist/tree/LiteralCommandNode.d.ts +1 -1
  52. package/dist/tree/LiteralCommandNode.js +28 -41
  53. package/dist/tree/RootCommandNode.d.ts +1 -1
  54. package/dist/tree/RootCommandNode.js +17 -69
  55. package/jest.config.js +5 -5
  56. package/package.json +32 -32
  57. package/src/Command.ts +2 -1
  58. package/src/CommandDispatcher.ts +397 -295
  59. package/src/ParseResults.ts +26 -22
  60. package/src/StringReader.ts +212 -193
  61. package/src/arguments/ArgumentType.ts +13 -8
  62. package/src/arguments/BoolArgumentType.ts +22 -21
  63. package/src/arguments/FloatArgumentType.ts +13 -14
  64. package/src/arguments/IntegerArgumentType.ts +13 -14
  65. package/src/arguments/LongArgumentType.ts +16 -17
  66. package/src/arguments/NumberArgumentType.ts +48 -38
  67. package/src/arguments/StringArgumentType.ts +26 -26
  68. package/src/builder/ArgumentBuilder.ts +80 -75
  69. package/src/builder/LiteralArgumentBuilder.ts +31 -21
  70. package/src/builder/RequiredArgumentBuilder.ts +42 -32
  71. package/src/context/CommandContext.ts +99 -76
  72. package/src/context/CommandContextBuilder.ts +169 -143
  73. package/src/context/ParsedArgument.ts +13 -13
  74. package/src/context/ParsedCommandNode.ts +14 -14
  75. package/src/context/StringRange.ts +26 -26
  76. package/src/context/SuggestionContext.ts +7 -7
  77. package/src/exceptions/CommandErrorType.ts +20 -13
  78. package/src/exceptions/CommandSyntaxError.ts +78 -37
  79. package/src/index.ts +30 -30
  80. package/src/suggestion/Suggestion.ts +46 -46
  81. package/src/suggestion/Suggestions.ts +59 -57
  82. package/src/suggestion/SuggestionsBuilder.ts +59 -57
  83. package/src/tree/ArgumentCommandNode.ts +51 -40
  84. package/src/tree/CommandNode.ts +96 -87
  85. package/src/tree/LiteralCommandNode.ts +78 -57
  86. package/src/tree/RootCommandNode.ts +33 -23
  87. package/test/Arguments.test.ts +47 -33
  88. package/test/CommandDispatcher.test.ts +18 -22
  89. package/test/StringReader.test.ts +47 -47
  90. package/tsconfig.json +9 -14
package/src/index.ts CHANGED
@@ -1,30 +1,30 @@
1
- export * from "./Command"
2
- export * from "./Predicate"
3
- export * from "./context/StringRange"
4
- export * from "./exceptions/CommandErrorType"
5
- export * from "./exceptions/CommandSyntaxError"
6
- export * from "./StringReader"
7
- export * from "./suggestion/Suggestion"
8
- export * from "./suggestion/Suggestions"
9
- export * from "./suggestion/SuggestionsBuilder"
10
- export * from "./tree/CommandNode"
11
- export * from "./tree/LiteralCommandNode"
12
- export * from "./tree/ArgumentCommandNode"
13
- export * from "./tree/RootCommandNode"
14
- export * from "./arguments/ArgumentType"
15
- export * from "./arguments/NumberArgumentType"
16
- export * from "./arguments/FloatArgumentType"
17
- export * from "./arguments/IntegerArgumentType"
18
- export * from "./arguments/LongArgumentType"
19
- export * from "./arguments/BoolArgumentType"
20
- export * from "./arguments/StringArgumentType"
21
- export * from "./builder/ArgumentBuilder"
22
- export * from "./builder/LiteralArgumentBuilder"
23
- export * from "./builder/RequiredArgumentBuilder"
24
- export * from "./context/ParsedArgument"
25
- export * from "./context/ParsedCommandNode"
26
- export * from "./context/CommandContext"
27
- export * from "./context/CommandContextBuilder"
28
- export * from "./context/SuggestionContext"
29
- export * from "./ParseResults"
30
- export * from "./CommandDispatcher"
1
+ export * from "./Command";
2
+ export * from "./Predicate";
3
+ export * from "./context/StringRange";
4
+ export * from "./exceptions/CommandErrorType";
5
+ export * from "./exceptions/CommandSyntaxError";
6
+ export * from "./StringReader";
7
+ export * from "./suggestion/Suggestion";
8
+ export * from "./suggestion/Suggestions";
9
+ export * from "./suggestion/SuggestionsBuilder";
10
+ export * from "./tree/CommandNode";
11
+ export * from "./tree/LiteralCommandNode";
12
+ export * from "./tree/ArgumentCommandNode";
13
+ export * from "./tree/RootCommandNode";
14
+ export * from "./arguments/ArgumentType";
15
+ export * from "./arguments/NumberArgumentType";
16
+ export * from "./arguments/FloatArgumentType";
17
+ export * from "./arguments/IntegerArgumentType";
18
+ export * from "./arguments/LongArgumentType";
19
+ export * from "./arguments/BoolArgumentType";
20
+ export * from "./arguments/StringArgumentType";
21
+ export * from "./builder/ArgumentBuilder";
22
+ export * from "./builder/LiteralArgumentBuilder";
23
+ export * from "./builder/RequiredArgumentBuilder";
24
+ export * from "./context/ParsedArgument";
25
+ export * from "./context/ParsedCommandNode";
26
+ export * from "./context/CommandContext";
27
+ export * from "./context/CommandContextBuilder";
28
+ export * from "./context/SuggestionContext";
29
+ export * from "./ParseResults";
30
+ export * from "./CommandDispatcher";
@@ -1,55 +1,55 @@
1
- import { StringRange } from "..";
1
+ import type { StringRange } from "..";
2
2
 
3
3
  export class Suggestion {
4
- private range: StringRange;
5
- private text: string;
6
- private tooltip: string;
4
+ private range: StringRange;
5
+ private text: string;
6
+ private tooltip: string;
7
7
 
8
- constructor(range: StringRange, text: string, tooltip?: string) {
9
- this.range = range;
10
- this.text = text;
11
- this.tooltip = tooltip;
12
- }
8
+ constructor(range: StringRange, text: string, tooltip?: string) {
9
+ this.range = range;
10
+ this.text = text;
11
+ this.tooltip = tooltip;
12
+ }
13
13
 
14
- getRange() {
15
- return this.range;
16
- }
14
+ getRange() {
15
+ return this.range;
16
+ }
17
17
 
18
- getText() {
19
- return this.text;
20
- }
18
+ getText() {
19
+ return this.text;
20
+ }
21
21
 
22
- getTooltip() {
23
- return this.tooltip;
24
- }
22
+ getTooltip() {
23
+ return this.tooltip;
24
+ }
25
25
 
26
- apply(input: string): string {
27
- if (this.range.getStart() == 0 && this.range.getEnd() === input.length) {
28
- return this.text;
29
- }
30
- let result = "";
31
- if (this.range.getStart() > 0) {
32
- result += input.substring(0, this.range.getStart());
33
- }
34
- result += this.text;
35
- if (this.range.getEnd() < input.length) {
36
- result += input.substring(this.range.getEnd());
37
- }
38
- return result;
39
- }
26
+ apply(input: string): string {
27
+ if (this.range.getStart() === 0 && this.range.getEnd() === input.length) {
28
+ return this.text;
29
+ }
30
+ let result = "";
31
+ if (this.range.getStart() > 0) {
32
+ result += input.substring(0, this.range.getStart());
33
+ }
34
+ result += this.text;
35
+ if (this.range.getEnd() < input.length) {
36
+ result += input.substring(this.range.getEnd());
37
+ }
38
+ return result;
39
+ }
40
40
 
41
- expand(command: string, range: StringRange): Suggestion {
42
- if (range === this.range) {
43
- return this;
44
- }
45
- let result = "";
46
- if (range.getStart() < this.range.getStart()) {
47
- result += command.substring(range.getStart(), this.range.getStart());
48
- }
49
- result += this.text;
50
- if (range.getEnd() > this.range.getEnd()) {
51
- result += command.substring(this.range.getEnd(), range.getEnd());
52
- }
53
- return new Suggestion(range, result, this.tooltip);
54
- }
41
+ expand(command: string, range: StringRange): Suggestion {
42
+ if (range === this.range) {
43
+ return this;
44
+ }
45
+ let result = "";
46
+ if (range.getStart() < this.range.getStart()) {
47
+ result += command.substring(range.getStart(), this.range.getStart());
48
+ }
49
+ result += this.text;
50
+ if (range.getEnd() > this.range.getEnd()) {
51
+ result += command.substring(this.range.getEnd(), range.getEnd());
52
+ }
53
+ return new Suggestion(range, result, this.tooltip);
54
+ }
55
55
  }
@@ -1,60 +1,62 @@
1
- import { StringRange, Suggestion } from "..";
1
+ import { StringRange, type Suggestion } from "..";
2
2
 
3
3
  export class Suggestions {
4
- static EMPTY = new Suggestions(StringRange.at(0), []);
5
-
6
- private range: StringRange;
7
- private suggestions: Suggestion[];
8
-
9
- constructor(range: StringRange, suggestions: Suggestion[]) {
10
- this.range = range;
11
- this.suggestions = suggestions;
12
- }
13
-
14
- getRange(): StringRange {
15
- return this.range;
16
- }
17
-
18
- getList(): Suggestion[] {
19
- return this.suggestions;
20
- }
21
-
22
- isEmpty(): boolean {
23
- return this.suggestions.length === 0;
24
- }
25
-
26
- static empty(): Promise<Suggestions> {
27
- return Promise.resolve(Suggestions.EMPTY);
28
- }
29
-
30
- static merge(command: string, input: Suggestions[]): Suggestions {
31
- if (input.length === 0) {
32
- return Suggestions.EMPTY;
33
- } else if (input.length === 1) {
34
- return input[0];
35
- }
36
- const texts = new Set<Suggestion>();
37
- for (const suggestions of input) {
38
- suggestions.getList().forEach(s => texts.add(s))
39
- }
40
- return Suggestions.create(command, Array.from(texts));
41
- }
42
-
43
- static create(command: string, suggestions: Suggestion[]): Suggestions {
44
- if (suggestions.length === 0) {
45
- return Suggestions.EMPTY;
46
- }
47
- let start = Infinity;
48
- let end = -Infinity;
49
- for (const suggestion of suggestions) {
50
- start = Math.min(suggestion.getRange().getStart(), start);
51
- end = Math.max(suggestion.getRange().getEnd(), end);
52
- }
53
- const range = new StringRange(start, end);
54
- const texts = [];
55
- for (const suggestion of suggestions) {
56
- texts.push(suggestion.expand(command, range));
57
- }
58
- return new Suggestions(range, texts.sort());
59
- }
4
+ static EMPTY = new Suggestions(StringRange.at(0), []);
5
+
6
+ private range: StringRange;
7
+ private suggestions: Suggestion[];
8
+
9
+ constructor(range: StringRange, suggestions: Suggestion[]) {
10
+ this.range = range;
11
+ this.suggestions = suggestions;
12
+ }
13
+
14
+ getRange(): StringRange {
15
+ return this.range;
16
+ }
17
+
18
+ getList(): Suggestion[] {
19
+ return this.suggestions;
20
+ }
21
+
22
+ isEmpty(): boolean {
23
+ return this.suggestions.length === 0;
24
+ }
25
+
26
+ static empty(): Promise<Suggestions> {
27
+ return Promise.resolve(Suggestions.EMPTY);
28
+ }
29
+
30
+ static merge(command: string, input: Suggestions[]): Suggestions {
31
+ if (input.length === 0) {
32
+ return Suggestions.EMPTY;
33
+ } else if (input.length === 1) {
34
+ return input[0];
35
+ }
36
+ const texts = new Set<Suggestion>();
37
+ for (const suggestions of input) {
38
+ suggestions.getList().forEach((s) => {
39
+ texts.add(s);
40
+ });
41
+ }
42
+ return Suggestions.create(command, Array.from(texts));
43
+ }
44
+
45
+ static create(command: string, suggestions: Suggestion[]): Suggestions {
46
+ if (suggestions.length === 0) {
47
+ return Suggestions.EMPTY;
48
+ }
49
+ let start = Infinity;
50
+ let end = -Infinity;
51
+ for (const suggestion of suggestions) {
52
+ start = Math.min(suggestion.getRange().getStart(), start);
53
+ end = Math.max(suggestion.getRange().getEnd(), end);
54
+ }
55
+ const range = new StringRange(start, end);
56
+ const texts = [];
57
+ for (const suggestion of suggestions) {
58
+ texts.push(suggestion.expand(command, range));
59
+ }
60
+ return new Suggestions(range, texts.sort());
61
+ }
60
62
  }
@@ -1,60 +1,62 @@
1
- import {
2
- Suggestion,
3
- Suggestions,
4
- StringRange
5
- } from "..";
1
+ import { Suggestion, Suggestions, StringRange } from "..";
6
2
 
7
3
  export class SuggestionsBuilder {
8
- private input: string;
9
- private start: number;
10
- private remaining: string;
11
- private result: Suggestion[];
12
-
13
- constructor(input: string, start: number) {
14
- this.input = input;
15
- this.start = start;
16
- this.remaining = input.substring(start);
17
- this.result = [];
18
- }
19
-
20
- getInput(): string {
21
- return this.input;
22
- }
23
-
24
- getStart(): number {
25
- return this.start;
26
- }
27
-
28
- getRemaining(): string {
29
- return this.remaining;
30
- }
31
-
32
- build(): Suggestions {
33
- return Suggestions.create(this.input, this.result);
34
- }
35
-
36
- buildPromise(): Promise<Suggestions> {
37
- return Promise.resolve(this.build());
38
- }
39
-
40
- suggest(text: string, tooltip?: string): SuggestionsBuilder {
41
- if (text === this.remaining) {
42
- return this;
43
- }
44
- this.result.push(new Suggestion(new StringRange(this.start, this.input.length), text, tooltip))
45
- return this;
46
- }
47
-
48
- add(other: SuggestionsBuilder): SuggestionsBuilder {
49
- this.result.concat(other.result);
50
- return this;
51
- }
52
-
53
- createOffset(start: number): SuggestionsBuilder {
54
- return new SuggestionsBuilder(this.input, start);
55
- }
56
-
57
- restart(start: number): SuggestionsBuilder {
58
- return new SuggestionsBuilder(this.input, this.start);
59
- }
4
+ private input: string;
5
+ private start: number;
6
+ private remaining: string;
7
+ private result: Suggestion[];
8
+
9
+ constructor(input: string, start: number) {
10
+ this.input = input;
11
+ this.start = start;
12
+ this.remaining = input.substring(start);
13
+ this.result = [];
14
+ }
15
+
16
+ getInput(): string {
17
+ return this.input;
18
+ }
19
+
20
+ getStart(): number {
21
+ return this.start;
22
+ }
23
+
24
+ getRemaining(): string {
25
+ return this.remaining;
26
+ }
27
+
28
+ build(): Suggestions {
29
+ return Suggestions.create(this.input, this.result);
30
+ }
31
+
32
+ buildPromise(): Promise<Suggestions> {
33
+ return Promise.resolve(this.build());
34
+ }
35
+
36
+ suggest(text: string, tooltip?: string): SuggestionsBuilder {
37
+ if (text === this.remaining) {
38
+ return this;
39
+ }
40
+ this.result.push(
41
+ new Suggestion(
42
+ new StringRange(this.start, this.input.length),
43
+ text,
44
+ tooltip,
45
+ ),
46
+ );
47
+ return this;
48
+ }
49
+
50
+ add(other: SuggestionsBuilder): SuggestionsBuilder {
51
+ this.result.concat(other.result);
52
+ return this;
53
+ }
54
+
55
+ createOffset(start: number): SuggestionsBuilder {
56
+ return new SuggestionsBuilder(this.input, start);
57
+ }
58
+
59
+ restart(): SuggestionsBuilder {
60
+ return new SuggestionsBuilder(this.input, this.start);
61
+ }
60
62
  }
@@ -1,48 +1,59 @@
1
1
  import {
2
- ArgumentType,
3
- CommandNode,
4
- StringReader,
5
- Command,
6
- CommandContext,
7
- CommandContextBuilder,
8
- Predicate,
9
- RedirectModifier,
10
- ParsedArgument,
11
- Suggestions,
12
- SuggestionsBuilder
13
- } from '..';
2
+ type ArgumentType,
3
+ CommandNode,
4
+ type StringReader,
5
+ type Command,
6
+ type CommandContext,
7
+ type CommandContextBuilder,
8
+ type Predicate,
9
+ type RedirectModifier,
10
+ ParsedArgument,
11
+ type Suggestions,
12
+ type SuggestionsBuilder,
13
+ } from "..";
14
14
 
15
15
  export class ArgumentCommandNode<S, T> extends CommandNode<S> {
16
- name: string;
17
- type: ArgumentType<T>;
16
+ name: string;
17
+ type: ArgumentType<T>;
18
18
 
19
- constructor(name: string, type: ArgumentType<T>, command: Command<S>, requirement: Predicate<S>, redirect: CommandNode<S>, modifier: RedirectModifier<S>, forks: boolean) {
20
- super(command, requirement, redirect, modifier, forks);
21
- this.name = name;
22
- this.type = type;
23
- }
19
+ constructor(
20
+ name: string,
21
+ type: ArgumentType<T>,
22
+ command: Command<S>,
23
+ requirement: Predicate<S>,
24
+ redirect: CommandNode<S>,
25
+ modifier: RedirectModifier<S>,
26
+ forks: boolean,
27
+ ) {
28
+ super(command, requirement, redirect, modifier, forks);
29
+ this.name = name;
30
+ this.type = type;
31
+ }
24
32
 
25
- getType(): ArgumentType<T> {
26
- return this.type;
27
- }
33
+ getType(): ArgumentType<T> {
34
+ return this.type;
35
+ }
28
36
 
29
- parse(reader: StringReader, contextBuilder: CommandContextBuilder<S>): void {
30
- const start = reader.getCursor();
31
- const result = this.type.parse(reader);
32
- const parsed = new ParsedArgument<T>(start, reader.getCursor(), result);
33
- contextBuilder.withArgument(this.name, parsed);
34
- contextBuilder.withNode(this, parsed.getRange());
35
- }
36
-
37
- getName(): string {
38
- return this.name;
39
- }
37
+ parse(reader: StringReader, contextBuilder: CommandContextBuilder<S>): void {
38
+ const start = reader.getCursor();
39
+ const result = this.type.parse(reader);
40
+ const parsed = new ParsedArgument<T>(start, reader.getCursor(), result);
41
+ contextBuilder.withArgument(this.name, parsed);
42
+ contextBuilder.withNode(this, parsed.getRange());
43
+ }
40
44
 
41
- getUsageText(): string {
42
- return "<" + this.name + ">";
43
- }
44
-
45
- listSuggestions(context: CommandContext<S>, builder: SuggestionsBuilder): Promise<Suggestions> {
46
- return Suggestions.empty();
47
- }
45
+ getName(): string {
46
+ return this.name;
47
+ }
48
+
49
+ getUsageText(): string {
50
+ return `<${this.name}>`;
51
+ }
52
+
53
+ listSuggestions(
54
+ context: CommandContext<S>,
55
+ builder: SuggestionsBuilder,
56
+ ): Promise<Suggestions> {
57
+ return this.type.listSuggestions(context, builder);
58
+ }
48
59
  }