@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.
- package/LICENSE +0 -0
- package/README.md +57 -39
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/AmbiguityConsumer.js +0 -0
- package/dist/lib/AmbiguityConsumer.js.map +0 -0
- package/dist/lib/Command.js +0 -0
- package/dist/lib/Command.js.map +0 -0
- package/dist/lib/CommandDispatcher.js +45 -28
- package/dist/lib/CommandDispatcher.js.map +1 -1
- package/dist/lib/ImmutableStringReader.js +0 -0
- package/dist/lib/ImmutableStringReader.js.map +0 -0
- package/dist/lib/LiteralMessage.js +0 -0
- package/dist/lib/LiteralMessage.js.map +0 -0
- package/dist/lib/Message.js +0 -0
- package/dist/lib/Message.js.map +0 -0
- package/dist/lib/ParseResults.js +0 -0
- package/dist/lib/ParseResults.js.map +0 -0
- package/dist/lib/Predicate.js +0 -0
- package/dist/lib/Predicate.js.map +0 -0
- package/dist/lib/RedirectModifier.js +0 -0
- package/dist/lib/RedirectModifier.js.map +0 -0
- package/dist/lib/ResultConsumer.js +0 -0
- package/dist/lib/ResultConsumer.js.map +0 -0
- package/dist/lib/SingleRedirectModifier.js +0 -0
- package/dist/lib/SingleRedirectModifier.js.map +0 -0
- package/dist/lib/StringReader.js +0 -0
- package/dist/lib/StringReader.js.map +0 -0
- package/dist/lib/arguments/ArgumentType.js +1 -1
- package/dist/lib/arguments/ArgumentType.js.map +1 -1
- package/dist/lib/arguments/BoolArgumentType.js +0 -0
- package/dist/lib/arguments/BoolArgumentType.js.map +0 -0
- package/dist/lib/arguments/FloatArgumentType.js +0 -0
- package/dist/lib/arguments/FloatArgumentType.js.map +0 -0
- package/dist/lib/arguments/IntegerArgumentType.js +0 -0
- package/dist/lib/arguments/IntegerArgumentType.js.map +0 -0
- package/dist/lib/arguments/StringArgumentType.js +0 -0
- package/dist/lib/arguments/StringArgumentType.js.map +0 -0
- package/dist/lib/builder/ArgumentBuilder.js +0 -0
- package/dist/lib/builder/ArgumentBuilder.js.map +0 -0
- package/dist/lib/builder/LiteralArgumentBuilder.js +0 -0
- package/dist/lib/builder/LiteralArgumentBuilder.js.map +1 -1
- package/dist/lib/builder/RequiredArgumentBuilder.js +0 -0
- package/dist/lib/builder/RequiredArgumentBuilder.js.map +1 -1
- package/dist/lib/context/CommandContext.js +4 -4
- package/dist/lib/context/CommandContext.js.map +1 -1
- package/dist/lib/context/CommandContextBuilder.js +0 -0
- package/dist/lib/context/CommandContextBuilder.js.map +0 -0
- package/dist/lib/context/ParsedArgument.js +0 -0
- package/dist/lib/context/ParsedArgument.js.map +0 -0
- package/dist/lib/context/ParsedCommandNode.js +0 -0
- package/dist/lib/context/ParsedCommandNode.js.map +0 -0
- package/dist/lib/context/StringRange.js +0 -0
- package/dist/lib/context/StringRange.js.map +0 -0
- package/dist/lib/context/SuggestionContext.js +0 -0
- package/dist/lib/context/SuggestionContext.js.map +0 -0
- package/dist/lib/exceptions/BuiltInExceptionProvider.js +0 -0
- package/dist/lib/exceptions/BuiltInExceptionProvider.js.map +0 -0
- package/dist/lib/exceptions/BuiltInExceptions.js +0 -0
- package/dist/lib/exceptions/BuiltInExceptions.js.map +0 -0
- package/dist/lib/exceptions/CommandExceptionType.js +0 -0
- package/dist/lib/exceptions/CommandExceptionType.js.map +0 -0
- package/dist/lib/exceptions/CommandSyntaxException.js +0 -0
- package/dist/lib/exceptions/CommandSyntaxException.js.map +0 -0
- package/dist/lib/exceptions/DynamicCommandExceptionType.js +0 -0
- package/dist/lib/exceptions/DynamicCommandExceptionType.js.map +0 -0
- package/dist/lib/exceptions/SimpleCommandExceptionType.js +0 -0
- package/dist/lib/exceptions/SimpleCommandExceptionType.js.map +0 -0
- package/dist/lib/suggestion/IntegerSuggestion.js +0 -0
- package/dist/lib/suggestion/IntegerSuggestion.js.map +0 -0
- package/dist/lib/suggestion/Suggestion.js +1 -1
- package/dist/lib/suggestion/Suggestion.js.map +1 -1
- package/dist/lib/suggestion/SuggestionProvider.js +0 -0
- package/dist/lib/suggestion/SuggestionProvider.js.map +0 -0
- package/dist/lib/suggestion/Suggestions.js +1 -1
- package/dist/lib/suggestion/Suggestions.js.map +1 -1
- package/dist/lib/suggestion/SuggestionsBuilder.js +0 -0
- package/dist/lib/suggestion/SuggestionsBuilder.js.map +0 -0
- package/dist/lib/tree/ArgumentCommandNode.js +17 -16
- package/dist/lib/tree/ArgumentCommandNode.js.map +1 -1
- package/dist/lib/tree/CommandNode.js +14 -13
- package/dist/lib/tree/CommandNode.js.map +1 -1
- package/dist/lib/tree/LiteralCommandNode.js +0 -0
- package/dist/lib/tree/LiteralCommandNode.js.map +0 -0
- package/dist/lib/tree/RootCommandNode.js +0 -0
- package/dist/lib/tree/RootCommandNode.js.map +0 -0
- package/dist/lib/util/isEqual.js +0 -0
- package/dist/lib/util/isEqual.js.map +0 -0
- package/index.js +0 -0
- package/package.json +55 -48
- package/.prettierrc +0 -10
- package/.renovaterc +0 -3
- package/CHANGELOG.md +0 -30
- package/dist/index.d.ts +0 -23
- package/dist/lib/AmbiguityConsumer.d.ts +0 -4
- package/dist/lib/Command.d.ts +0 -4
- package/dist/lib/CommandDispatcher.d.ts +0 -29
- package/dist/lib/ImmutableStringReader.d.ts +0 -12
- package/dist/lib/LiteralMessage.d.ts +0 -7
- package/dist/lib/Message.d.ts +0 -3
- package/dist/lib/ParseResults.d.ts +0 -13
- package/dist/lib/Predicate.d.ts +0 -3
- package/dist/lib/Primitive.js +0 -2
- package/dist/lib/RedirectModifier.d.ts +0 -4
- package/dist/lib/ResultConsumer.d.ts +0 -4
- package/dist/lib/SingleRedirectModifier.d.ts +0 -4
- package/dist/lib/StringReader.d.ts +0 -27
- package/dist/lib/arguments/ArgumentType.d.ts +0 -21
- package/dist/lib/arguments/BoolArgumentType.d.ts +0 -13
- package/dist/lib/arguments/FloatArgumentType.d.ts +0 -16
- package/dist/lib/arguments/IntegerArgumentType.d.ts +0 -16
- package/dist/lib/arguments/StringArgumentType.d.ts +0 -21
- package/dist/lib/builder/ArgumentBuilder.d.ts +0 -28
- package/dist/lib/builder/LiteralArgumentBuilder.d.ts +0 -11
- package/dist/lib/builder/RequiredArgumentBuilder.d.ts +0 -18
- package/dist/lib/context/CommandContext.d.ts +0 -33
- package/dist/lib/context/CommandContextBuilder.d.ts +0 -38
- package/dist/lib/context/ParsedArgument.d.ts +0 -9
- package/dist/lib/context/ParsedCommandNode.d.ts +0 -11
- package/dist/lib/context/StringRange.d.ts +0 -16
- package/dist/lib/context/SuggestionContext.d.ts +0 -6
- package/dist/lib/exceptions/BuiltInExceptionProvider.d.ts +0 -23
- package/dist/lib/exceptions/BuiltInExceptions.d.ts +0 -43
- package/dist/lib/exceptions/CommandExceptionType.d.ts +0 -2
- package/dist/lib/exceptions/CommandSyntaxException.d.ts +0 -19
- package/dist/lib/exceptions/Dynamic2CommandExceptionType.js +0 -19
- package/dist/lib/exceptions/DynamicCommandExceptionType.d.ts +0 -10
- package/dist/lib/exceptions/SimpleCommandExceptionType.d.ts +0 -11
- package/dist/lib/suggestion/IntegerSuggestion.d.ts +0 -12
- package/dist/lib/suggestion/Suggestion.d.ts +0 -17
- package/dist/lib/suggestion/SuggestionProvider.d.ts +0 -6
- package/dist/lib/suggestion/Suggestions.d.ts +0 -16
- package/dist/lib/suggestion/SuggestionsBuilder.d.ts +0 -18
- package/dist/lib/tree/ArgumentCommandNode.d.ts +0 -31
- package/dist/lib/tree/CommandNode.d.ts +0 -45
- package/dist/lib/tree/LiteralCommandNode.d.ts +0 -27
- package/dist/lib/tree/RootCommandNode.d.ts +0 -21
- package/dist/lib/util/isEqual.d.ts +0 -1
- package/example/fill.js +0 -47
- package/example/fill.ts +0 -50
- package/src/index.ts +0 -50
- package/src/lib/AmbiguityConsumer.ts +0 -6
- package/src/lib/Command.ts +0 -5
- package/src/lib/CommandDispatcher.ts +0 -390
- package/src/lib/ImmutableStringReader.ts +0 -22
- package/src/lib/LiteralMessage.ts +0 -17
- package/src/lib/Message.ts +0 -3
- package/src/lib/ParseResults.ts +0 -32
- package/src/lib/Predicate.ts +0 -3
- package/src/lib/RedirectModifier.ts +0 -5
- package/src/lib/ResultConsumer.ts +0 -6
- package/src/lib/SingleRedirectModifier.ts +0 -6
- package/src/lib/StringReader.ts +0 -210
- package/src/lib/arguments/ArgumentType.ts +0 -27
- package/src/lib/arguments/BoolArgumentType.ts +0 -41
- package/src/lib/arguments/FloatArgumentType.ts +0 -75
- package/src/lib/arguments/IntegerArgumentType.ts +0 -75
- package/src/lib/arguments/StringArgumentType.ts +0 -83
- package/src/lib/builder/ArgumentBuilder.ts +0 -94
- package/src/lib/builder/LiteralArgumentBuilder.ts +0 -35
- package/src/lib/builder/RequiredArgumentBuilder.ts +0 -54
- package/src/lib/context/CommandContext.ts +0 -118
- package/src/lib/context/CommandContextBuilder.ts +0 -155
- package/src/lib/context/ParsedArgument.ts +0 -27
- package/src/lib/context/ParsedCommandNode.ts +0 -35
- package/src/lib/context/StringRange.ts +0 -57
- package/src/lib/context/SuggestionContext.ts +0 -12
- package/src/lib/exceptions/BuiltInExceptionProvider.ts +0 -43
- package/src/lib/exceptions/BuiltInExceptions.ts +0 -121
- package/src/lib/exceptions/CommandExceptionType.ts +0 -2
- package/src/lib/exceptions/CommandSyntaxException.ts +0 -77
- package/src/lib/exceptions/DynamicCommandExceptionType.ts +0 -22
- package/src/lib/exceptions/SimpleCommandExceptionType.ts +0 -26
- package/src/lib/suggestion/IntegerSuggestion.ts +0 -46
- package/src/lib/suggestion/Suggestion.ts +0 -87
- package/src/lib/suggestion/SuggestionProvider.ts +0 -7
- package/src/lib/suggestion/Suggestions.ts +0 -85
- package/src/lib/suggestion/SuggestionsBuilder.ts +0 -69
- package/src/lib/tree/ArgumentCommandNode.ts +0 -118
- package/src/lib/tree/CommandNode.ts +0 -200
- package/src/lib/tree/LiteralCommandNode.ts +0 -111
- package/src/lib/tree/RootCommandNode.ts +0 -62
- package/src/lib/util/isEqual.ts +0 -75
- package/test/CommandDispatcherTest.ts +0 -427
- package/test/CommandDispatcherUsagesTest.ts +0 -163
- package/test/CommandSuggestionsTest.ts +0 -283
- package/test/StringReaderTest.ts +0 -391
- package/test/arguments/BoolArgumentTypeTest.ts +0 -18
- package/test/arguments/FloatArgumentTypeTest.ts +0 -61
- package/test/arguments/IntegerArgumentTypeTest.ts +0 -63
- package/test/arguments/StringArgumentTypeTest.ts +0 -61
- package/test/benchmarks/ExecuteBenchmark.js +0 -33
- package/test/benchmarks/ParsingBenchmark.js +0 -96
- package/test/builder/ArgumentBuilderTest.ts +0 -61
- package/test/builder/LiteralArgumentBuilderTest.ts +0 -37
- package/test/builder/RequiredArgumentBuilderTest.ts +0 -40
- package/test/context/CommandContextTest.ts +0 -77
- package/test/context/ParsedArgumentTest.ts +0 -19
- package/test/exceptions/DynamicCommandSyntaxExceptionTypeTest.ts +0 -18
- package/test/exceptions/SimpleCommandSyntaxExceptionTypeTest.ts +0 -33
- package/test/suggestion/SuggestionBuilderTest.ts +0 -66
- package/test/suggestion/SuggestionTest.ts +0 -65
- package/test/suggestion/SuggestionsTest.ts +0 -24
- package/test/tree/AbstractCommandNodeTest.ts +0 -57
- package/test/tree/ArgumentCommandNodeTest.ts +0 -81
- package/test/tree/LiteralCommandNodeTest.ts +0 -112
- package/test/tree/RootCommandNodeTest.ts +0 -70
- package/test/utils/testEquality.ts +0 -10
- package/tsconfig.json +0 -40
- package/tsconfig.tsbuildinfo +0 -1578
package/example/fill.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { CommandDispatcher, literal, argument, string, ArgumentType, StringReader, CommandContext, SuggestionsBuilder, Suggestions } from "../dist"
|
|
2
|
-
|
|
3
|
-
class BlockPos implements ArgumentType<BlockPos> {
|
|
4
|
-
|
|
5
|
-
public x: number;
|
|
6
|
-
public y: number;
|
|
7
|
-
public z: number;
|
|
8
|
-
constructor(x: number = 0, y: number = 0, z: number = 0) {
|
|
9
|
-
this.x = x; this.y = y; this.z = z;
|
|
10
|
-
}
|
|
11
|
-
parse(reader: StringReader) {
|
|
12
|
-
this.x = reader.readInt();
|
|
13
|
-
reader.skip();
|
|
14
|
-
this.y = reader.readInt();
|
|
15
|
-
reader.skip();
|
|
16
|
-
this.z = reader.readInt();
|
|
17
|
-
return this;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
getExamples() {
|
|
21
|
-
return [
|
|
22
|
-
"1 2 3"
|
|
23
|
-
]
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const dispatcher = new CommandDispatcher();
|
|
28
|
-
|
|
29
|
-
dispatcher.register(
|
|
30
|
-
literal("fill").then(
|
|
31
|
-
argument("pos1", new BlockPos()).then(
|
|
32
|
-
argument("pos2", new BlockPos()).then(
|
|
33
|
-
argument("block", string()).executes(context => {
|
|
34
|
-
console.log(context.getArgument("pos1", BlockPos))
|
|
35
|
-
console.log(context.getArgument("pos2", BlockPos))
|
|
36
|
-
console.log(context.getArgument("block", String))
|
|
37
|
-
return 0;
|
|
38
|
-
}).build()
|
|
39
|
-
)
|
|
40
|
-
)
|
|
41
|
-
)
|
|
42
|
-
)
|
|
43
|
-
|
|
44
|
-
const parsedCommand = dispatcher.parse("fill 3 4 5 10 11 12 air", {})
|
|
45
|
-
|
|
46
|
-
try {
|
|
47
|
-
dispatcher.execute(parsedCommand);
|
|
48
|
-
} catch (ex) {
|
|
49
|
-
console.error(ex.getMessage());
|
|
50
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import CommandDispatcher from "./lib/CommandDispatcher"
|
|
2
|
-
import LiteralMessage from "./lib/LiteralMessage"
|
|
3
|
-
import ParseResults from "./lib/ParseResults"
|
|
4
|
-
import StringReader from "./lib/StringReader"
|
|
5
|
-
import { DefaultType } from "./lib/arguments/ArgumentType"
|
|
6
|
-
import LiteralArgumentBuilder, { literal } from "./lib/builder/LiteralArgumentBuilder"
|
|
7
|
-
import RequiredArgumentBuilder, { argument } from "./lib/builder/RequiredArgumentBuilder"
|
|
8
|
-
import CommandContext from "./lib/context/CommandContext"
|
|
9
|
-
import CommandContextBuilder from "./lib/context/CommandContextBuilder"
|
|
10
|
-
import ParsedArgument from "./lib/context/ParsedArgument"
|
|
11
|
-
import ParsedCommandNode from "./lib/context/ParsedCommandNode"
|
|
12
|
-
import StringRange from "./lib/context/StringRange"
|
|
13
|
-
import SuggestionsContext from "./lib/context/SuggestionContext"
|
|
14
|
-
import CommandSyntaxException from "./lib/exceptions/CommandSyntaxException"
|
|
15
|
-
import DynamicCommandExceptionType from "./lib/exceptions/DynamicCommandExceptionType"
|
|
16
|
-
import SimpleCommandExceptionType from "./lib/exceptions/SimpleCommandExceptionType"
|
|
17
|
-
import Suggestion from "./lib/suggestion/Suggestion"
|
|
18
|
-
import Suggestions from "./lib/suggestion/Suggestions"
|
|
19
|
-
import SuggestionsBuilder from "./lib/suggestion/SuggestionsBuilder";
|
|
20
|
-
import ArgumentCommandNode from "./lib/tree/ArgumentCommandNode"
|
|
21
|
-
import LiteralCommandNode from "./lib/tree/LiteralCommandNode"
|
|
22
|
-
import RootCommandNode from "./lib/tree/RootCommandNode"
|
|
23
|
-
|
|
24
|
-
const { word, string, greedyString, bool, integer, float} = DefaultType
|
|
25
|
-
|
|
26
|
-
export {
|
|
27
|
-
word, string, greedyString, bool, integer, float,
|
|
28
|
-
literal, argument,
|
|
29
|
-
CommandDispatcher,
|
|
30
|
-
LiteralMessage,
|
|
31
|
-
ParseResults,
|
|
32
|
-
StringReader,
|
|
33
|
-
LiteralArgumentBuilder,
|
|
34
|
-
RequiredArgumentBuilder,
|
|
35
|
-
CommandContext,
|
|
36
|
-
CommandContextBuilder,
|
|
37
|
-
ParsedArgument,
|
|
38
|
-
ParsedCommandNode,
|
|
39
|
-
StringRange,
|
|
40
|
-
SuggestionsContext,
|
|
41
|
-
CommandSyntaxException,
|
|
42
|
-
SimpleCommandExceptionType,
|
|
43
|
-
DynamicCommandExceptionType,
|
|
44
|
-
Suggestion,
|
|
45
|
-
Suggestions,
|
|
46
|
-
SuggestionsBuilder,
|
|
47
|
-
ArgumentCommandNode,
|
|
48
|
-
LiteralCommandNode,
|
|
49
|
-
RootCommandNode
|
|
50
|
-
}
|
package/src/lib/Command.ts
DELETED
|
@@ -1,390 +0,0 @@
|
|
|
1
|
-
import ParseResults from "./ParseResults"
|
|
2
|
-
import ResultConsumer from "./ResultConsumer"
|
|
3
|
-
import RedirectModifier from "./RedirectModifier"
|
|
4
|
-
import AmbiguityConsumer from "./AmbiguityConsumer"
|
|
5
|
-
import LiteralArgumentBuilder from "./builder/LiteralArgumentBuilder"
|
|
6
|
-
import CommandContext from "./context/CommandContext"
|
|
7
|
-
import CommandContextBuilder from "./context/CommandContextBuilder"
|
|
8
|
-
import SuggestionContext from "./context/SuggestionContext"
|
|
9
|
-
import CommandSyntaxException from "./exceptions/CommandSyntaxException"
|
|
10
|
-
import Suggestions from "./suggestion/Suggestions"
|
|
11
|
-
import SuggestionsBuilder from "./suggestion/SuggestionsBuilder"
|
|
12
|
-
import CommandNode from "./tree/CommandNode"
|
|
13
|
-
import LiteralCommandNode from "./tree/LiteralCommandNode"
|
|
14
|
-
import RootCommandNode from "./tree/RootCommandNode"
|
|
15
|
-
import StringReader from "./StringReader";
|
|
16
|
-
|
|
17
|
-
const ARGUMENT_SEPARATOR = " ";
|
|
18
|
-
const USAGE_OPTIONAL_OPEN = "[";
|
|
19
|
-
const USAGE_OPTIONAL_CLOSE = "]";
|
|
20
|
-
const USAGE_REQUIRED_OPEN = "(";
|
|
21
|
-
const USAGE_REQUIRED_CLOSE = ")";
|
|
22
|
-
const USAGE_OR = "|";
|
|
23
|
-
|
|
24
|
-
export default class CommandDispatcher<S> {
|
|
25
|
-
|
|
26
|
-
private root: RootCommandNode<S>;
|
|
27
|
-
|
|
28
|
-
private consumer: ResultConsumer<S> = {
|
|
29
|
-
onCommandComplete(){}
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
public constructor (root: RootCommandNode<S> = null) {
|
|
33
|
-
this.root = root || new RootCommandNode<S>();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public register(command: LiteralArgumentBuilder<S>): LiteralCommandNode<S> {
|
|
37
|
-
let build = command.build();
|
|
38
|
-
this.root.addChild(build);
|
|
39
|
-
return build;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public setConsumer(consumer: ResultConsumer<S>) {
|
|
43
|
-
this.consumer = consumer;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public execute(input: string | StringReader | ParseResults<S>, source: S = null): Promise<any>[] {
|
|
47
|
-
|
|
48
|
-
if (typeof input === "string")
|
|
49
|
-
input = new StringReader(input);
|
|
50
|
-
|
|
51
|
-
let parse: ParseResults<S>;
|
|
52
|
-
if (input instanceof StringReader) {
|
|
53
|
-
if (!(source == null))
|
|
54
|
-
parse = this.parse(input, source);
|
|
55
|
-
} else
|
|
56
|
-
parse = input;
|
|
57
|
-
|
|
58
|
-
if (parse.getReader().canRead()) {
|
|
59
|
-
if (parse.getExceptions().size === 1) {
|
|
60
|
-
throw parse.getExceptions().values().next().value;
|
|
61
|
-
}
|
|
62
|
-
else if (parse.getContext().getRange().isEmpty()) {
|
|
63
|
-
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().createWithContext(parse.getReader());
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(parse.getReader());
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
let result = [];
|
|
72
|
-
let successfulForks = 0;
|
|
73
|
-
let forked = false;
|
|
74
|
-
let foundCommand = false;
|
|
75
|
-
let command = parse.getReader().getString();
|
|
76
|
-
let original = parse.getContext().build(command);
|
|
77
|
-
let contexts: Array<CommandContext<S>> = [];
|
|
78
|
-
contexts.push(original)
|
|
79
|
-
let next: CommandContext<S>[] = null;
|
|
80
|
-
while (!(contexts == null)) {
|
|
81
|
-
for (let i = 0; i < contexts.length; i++) {
|
|
82
|
-
let context: CommandContext<S> = contexts[i];
|
|
83
|
-
let child: CommandContext<S> = context.getChild();
|
|
84
|
-
if (!(child == null)) {
|
|
85
|
-
forked = forked || context.isForked();
|
|
86
|
-
if (child.hasNodes()) {
|
|
87
|
-
foundCommand = true;
|
|
88
|
-
let modifier: RedirectModifier<S> = context.getRedirectModifier();
|
|
89
|
-
if (modifier == null) {
|
|
90
|
-
if (next == null)
|
|
91
|
-
next = [];
|
|
92
|
-
next.push(child.copyFor(context.getSource()));
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
try {
|
|
96
|
-
let results: Array<S> = modifier.apply(context);
|
|
97
|
-
if (results.length !== 0) {
|
|
98
|
-
if (next == null)
|
|
99
|
-
next = []
|
|
100
|
-
|
|
101
|
-
for (let source of results) {
|
|
102
|
-
next.push(child.copyFor(source));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
catch (ex) {
|
|
108
|
-
this.consumer.onCommandComplete(context, false, 0);
|
|
109
|
-
if (!forked)
|
|
110
|
-
throw ex;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
} else if (context.getCommand() != null) {
|
|
116
|
-
foundCommand = true;
|
|
117
|
-
try {
|
|
118
|
-
let value = context.getCommand()(context);
|
|
119
|
-
result.push(value);
|
|
120
|
-
this.consumer.onCommandComplete(context, true, value);
|
|
121
|
-
successfulForks++;
|
|
122
|
-
} catch (ex) {
|
|
123
|
-
this.consumer.onCommandComplete(context, false, ex);
|
|
124
|
-
if (!forked)
|
|
125
|
-
throw ex;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
contexts = next;
|
|
132
|
-
next = null;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (!foundCommand) {
|
|
136
|
-
this.consumer.onCommandComplete(original, false, 0);
|
|
137
|
-
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().createWithContext(parse.getReader());
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return result;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public parse(command: string | StringReader, source: S): ParseResults<S> {
|
|
144
|
-
if (typeof command === "string")
|
|
145
|
-
command = new StringReader(command)
|
|
146
|
-
|
|
147
|
-
let context: CommandContextBuilder<S> = new CommandContextBuilder(this, source, this.root, command.getCursor());
|
|
148
|
-
return this.parseNodes(this.root, command, context);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
private parseNodes(node: CommandNode<S>, originalReader: StringReader, contextSoFar: CommandContextBuilder<S>): ParseResults<S> {
|
|
152
|
-
|
|
153
|
-
let source: S = contextSoFar.getSource();
|
|
154
|
-
let errors: Map<CommandNode<S>, CommandSyntaxException> = null;
|
|
155
|
-
let potentials: ParseResults<S>[] = null;
|
|
156
|
-
let cursor = originalReader.getCursor();
|
|
157
|
-
for (let child of node.getRelevantNodes(originalReader)) {
|
|
158
|
-
if (!child.canUse(source))
|
|
159
|
-
continue;
|
|
160
|
-
|
|
161
|
-
let context: CommandContextBuilder<S> = contextSoFar.copy();
|
|
162
|
-
let reader: StringReader = new StringReader(originalReader);
|
|
163
|
-
try {
|
|
164
|
-
child.parse(reader, context);
|
|
165
|
-
|
|
166
|
-
if (reader.canRead())
|
|
167
|
-
if (reader.peek() != ARGUMENT_SEPARATOR)
|
|
168
|
-
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherExpectedArgumentSeparator().createWithContext(reader);
|
|
169
|
-
}
|
|
170
|
-
catch (ex) {
|
|
171
|
-
if (errors == null) {
|
|
172
|
-
errors = new Map();
|
|
173
|
-
}
|
|
174
|
-
errors.set(child, ex);
|
|
175
|
-
reader.setCursor(cursor);
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
context.withCommand(child.getCommand());
|
|
180
|
-
if (reader.canRead(child.getRedirect() == null ? 2 : 1)) {
|
|
181
|
-
reader.skip();
|
|
182
|
-
if (!(child.getRedirect() == null)) {
|
|
183
|
-
let childContext: CommandContextBuilder<S> = new CommandContextBuilder(this, source, child.getRedirect(), reader.getCursor());
|
|
184
|
-
let parse: ParseResults<S> = this.parseNodes(child.getRedirect(), reader, childContext);
|
|
185
|
-
context.withChild(parse.getContext());
|
|
186
|
-
return new ParseResults(context, parse.getReader(), parse.getExceptions());
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
let parse: ParseResults<S> = this.parseNodes(child, reader, context);
|
|
190
|
-
if (potentials == null) {
|
|
191
|
-
potentials = [];
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
potentials.push(parse);
|
|
195
|
-
}
|
|
196
|
-
} else {
|
|
197
|
-
if (potentials == null) {
|
|
198
|
-
potentials = [];
|
|
199
|
-
}
|
|
200
|
-
potentials.push(new ParseResults(context, reader, new Map()));
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (!(potentials == null)) {
|
|
205
|
-
if (potentials.length > 1) {
|
|
206
|
-
potentials.sort((a, b) => {
|
|
207
|
-
if (!a.getReader().canRead() && b.getReader().canRead()) {
|
|
208
|
-
return -1;
|
|
209
|
-
}
|
|
210
|
-
if (a.getReader().canRead() && !b.getReader().canRead()) {
|
|
211
|
-
return 1;
|
|
212
|
-
}
|
|
213
|
-
if (a.getExceptions().size === 0 && b.getExceptions().size !== 0) {
|
|
214
|
-
return -1;
|
|
215
|
-
}
|
|
216
|
-
if (a.getExceptions().size !== 0 && b.getExceptions().size === 0) {
|
|
217
|
-
return 1;
|
|
218
|
-
}
|
|
219
|
-
return 0;
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
return potentials[0];
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
return new ParseResults<S>(contextSoFar, originalReader, errors == null ? new Map() : errors);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
public getAllUsage(node: CommandNode<S>, source: S, restricted: boolean): string[] {
|
|
229
|
-
const result: Array<string> = [];
|
|
230
|
-
this.__getAllUsage(node, source, result, "", restricted);
|
|
231
|
-
return result;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
private __getAllUsage(node: CommandNode<S>, source: S, result: string[], prefix = "", restricted: boolean) {
|
|
235
|
-
if (restricted && !node.canUse(source)) {
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (node.getCommand() != null) {
|
|
240
|
-
result.push(prefix);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
if (node.getRedirect() != null) {
|
|
244
|
-
const redirect = node.getRedirect() === this.root ? "..." : "-> " + node.getRedirect().getUsageText();
|
|
245
|
-
result.push(prefix.length === 0 ? node.getUsageText() + ARGUMENT_SEPARATOR + redirect : prefix + ARGUMENT_SEPARATOR + redirect);
|
|
246
|
-
}
|
|
247
|
-
else if (node.getChildrenCount() > 0) {
|
|
248
|
-
for (let child of node.getChildren()) {
|
|
249
|
-
this.__getAllUsage(child, source, result, prefix.length === 0 ? child.getUsageText() : prefix + ARGUMENT_SEPARATOR + child.getUsageText(), restricted);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
public getSmartUsage(node: CommandNode<S>, source: S): Map<CommandNode<S>, string> {
|
|
257
|
-
let result = new Map<CommandNode<S>, string>();
|
|
258
|
-
|
|
259
|
-
let optional = node.getCommand() !== null;
|
|
260
|
-
for (let child of node.getChildren()) {
|
|
261
|
-
let usage = this.__getSmartUsage(child, source, optional, false);
|
|
262
|
-
if (!(usage == null)) {
|
|
263
|
-
result.set(child, usage);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
return result;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
private __getSmartUsage(node: CommandNode<S>, source: S, optional: boolean, deep: boolean): string {
|
|
271
|
-
if (!node.canUse(source)) {
|
|
272
|
-
return null;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
let self = optional ? USAGE_OPTIONAL_OPEN + node.getUsageText() + USAGE_OPTIONAL_CLOSE : node.getUsageText();
|
|
276
|
-
let childOptional = node.getCommand() != null;
|
|
277
|
-
let open = childOptional ? USAGE_OPTIONAL_OPEN : USAGE_REQUIRED_OPEN;
|
|
278
|
-
let close = childOptional ? USAGE_OPTIONAL_CLOSE : USAGE_REQUIRED_CLOSE;
|
|
279
|
-
|
|
280
|
-
if (!deep) {
|
|
281
|
-
if ((node.getRedirect() != null)) {
|
|
282
|
-
let redirect = node.getRedirect() == this.root ? "..." : "-> " + node.getRedirect().getUsageText();
|
|
283
|
-
return self + ARGUMENT_SEPARATOR + redirect;
|
|
284
|
-
}
|
|
285
|
-
else {
|
|
286
|
-
let children: CommandNode<S>[] = [...node.getChildren()].filter(c => c.canUse(source));
|
|
287
|
-
if ((children.length == 1)) {
|
|
288
|
-
let usage = this.__getSmartUsage(children[0], source, childOptional, childOptional);
|
|
289
|
-
if (!(usage == null)) {
|
|
290
|
-
return self + ARGUMENT_SEPARATOR + usage;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
else if (children.length > 1) {
|
|
294
|
-
let childUsage = new Set<string>();
|
|
295
|
-
for (let child of children) {
|
|
296
|
-
let usage = this.__getSmartUsage(child, source, childOptional, true);
|
|
297
|
-
if (!(usage == null)) {
|
|
298
|
-
childUsage.add(usage);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
if (childUsage.size === 1) {
|
|
302
|
-
let usage = childUsage.values().next().value;
|
|
303
|
-
return self + ARGUMENT_SEPARATOR + (childOptional ? USAGE_OPTIONAL_OPEN + usage + USAGE_OPTIONAL_CLOSE : usage);
|
|
304
|
-
}
|
|
305
|
-
else if (childUsage.size > 1) {
|
|
306
|
-
let builder = open;
|
|
307
|
-
let count = 0;
|
|
308
|
-
for (let child of children) {
|
|
309
|
-
if (count > 0) {
|
|
310
|
-
builder += USAGE_OR;
|
|
311
|
-
}
|
|
312
|
-
builder += child.getUsageText();
|
|
313
|
-
count++;
|
|
314
|
-
}
|
|
315
|
-
if (count > 0) {
|
|
316
|
-
builder += close;
|
|
317
|
-
return self + ARGUMENT_SEPARATOR + builder;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
return self;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
public async getCompletionSuggestions(parse: ParseResults<S>, cursor = parse.getReader().getTotalLength()): Promise<Suggestions> {
|
|
327
|
-
let context: CommandContextBuilder<S> = parse.getContext();
|
|
328
|
-
let nodeBeforeCursor: SuggestionContext<S> = context.findSuggestionContext(cursor);
|
|
329
|
-
let parent: CommandNode<S> = nodeBeforeCursor.parent;
|
|
330
|
-
let start = Math.min(nodeBeforeCursor.startPos, cursor);
|
|
331
|
-
let fullInput = parse.getReader().getString();
|
|
332
|
-
let truncatedInput = fullInput.substring(0, cursor);
|
|
333
|
-
let futures = [];
|
|
334
|
-
for (let node of parent.getChildren()) {
|
|
335
|
-
let future = await Suggestions.empty();
|
|
336
|
-
try {
|
|
337
|
-
future = await node.listSuggestions(context.build(truncatedInput), new SuggestionsBuilder(truncatedInput, start));
|
|
338
|
-
}
|
|
339
|
-
catch (ignored) {
|
|
340
|
-
}
|
|
341
|
-
futures.push(future);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
return Promise.resolve(Suggestions.merge(fullInput, futures));
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
public getRoot(): RootCommandNode<S> {
|
|
348
|
-
return this.root;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
public getPath(target: CommandNode<S>): string[] {
|
|
352
|
-
let nodes: Array<Array<CommandNode<S>>> = [];
|
|
353
|
-
this.addPaths(this.root, nodes, []);
|
|
354
|
-
for (let list of nodes) {
|
|
355
|
-
if (list[list.length - 1] === target) {
|
|
356
|
-
let result = [];
|
|
357
|
-
for (let node of list) {
|
|
358
|
-
if (node !== this.root) {
|
|
359
|
-
result.push(node.getName());
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
return result;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
return [];
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
public findNode(path: string[]): CommandNode<S> {
|
|
369
|
-
let node: CommandNode<S> = this.root;
|
|
370
|
-
for (let name of path) {
|
|
371
|
-
node = node.getChild(name);
|
|
372
|
-
if (node == null)
|
|
373
|
-
return null;
|
|
374
|
-
}
|
|
375
|
-
return node;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
public findAmbiguities(consumer: AmbiguityConsumer<S>) {
|
|
379
|
-
this.root.findAmbiguities(consumer);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
private addPaths(node: CommandNode<S>, result: Array<Array<CommandNode<S>>>, parents: Array<CommandNode<S>>): void {
|
|
383
|
-
let current: Array<CommandNode<S>> = [];
|
|
384
|
-
current.push(...parents);
|
|
385
|
-
current.push(node);
|
|
386
|
-
result.push(current);
|
|
387
|
-
for (let child of node.getChildren())
|
|
388
|
-
this.addPaths(child, result, current);
|
|
389
|
-
}
|
|
390
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export default interface ImmutableStringReader {
|
|
2
|
-
|
|
3
|
-
getString(): string;
|
|
4
|
-
|
|
5
|
-
getRemainingLength(): number;
|
|
6
|
-
|
|
7
|
-
getTotalLength(): number;
|
|
8
|
-
|
|
9
|
-
getCursor(): number;
|
|
10
|
-
|
|
11
|
-
getRead(): string;
|
|
12
|
-
|
|
13
|
-
getRemaining(): string;
|
|
14
|
-
|
|
15
|
-
canRead(length: number): boolean;
|
|
16
|
-
|
|
17
|
-
canRead(): boolean;
|
|
18
|
-
|
|
19
|
-
peek(): string;
|
|
20
|
-
|
|
21
|
-
peek(offset: number): string;
|
|
22
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import Message from "./Message"
|
|
2
|
-
|
|
3
|
-
export default class LiteralMessage implements Message {
|
|
4
|
-
|
|
5
|
-
str: string;
|
|
6
|
-
public constructor (str: string) {
|
|
7
|
-
this.str = str;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
public getString(): string {
|
|
11
|
-
return this.str;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public toString(): string {
|
|
15
|
-
return this.str;
|
|
16
|
-
}
|
|
17
|
-
}
|
package/src/lib/Message.ts
DELETED
package/src/lib/ParseResults.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import CommandContextBuilder from "./context/CommandContextBuilder"
|
|
2
|
-
import CommandSyntaxException from "./exceptions/CommandSyntaxException"
|
|
3
|
-
import CommandNode from "./tree/CommandNode"
|
|
4
|
-
import ImmutableStringReader from "./ImmutableStringReader"
|
|
5
|
-
import StringReader from "./StringReader"
|
|
6
|
-
|
|
7
|
-
export default class ParseResults<S> {
|
|
8
|
-
|
|
9
|
-
private context: CommandContextBuilder<S>;
|
|
10
|
-
|
|
11
|
-
private exceptions: Map<CommandNode<S>, CommandSyntaxException>;
|
|
12
|
-
|
|
13
|
-
private reader: ImmutableStringReader;
|
|
14
|
-
|
|
15
|
-
public constructor (context: CommandContextBuilder<S>, reader: ImmutableStringReader, exceptions: Map<CommandNode<S>, CommandSyntaxException>) {
|
|
16
|
-
this.context = context;
|
|
17
|
-
this.reader = reader || new StringReader("");
|
|
18
|
-
this.exceptions = exceptions || new Map<CommandNode<S>, CommandSyntaxException>();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public getContext(): CommandContextBuilder<S> {
|
|
22
|
-
return this.context;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public getReader(): ImmutableStringReader {
|
|
26
|
-
return this.reader;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public getExceptions(): Map<CommandNode<S>, CommandSyntaxException> {
|
|
30
|
-
return this.exceptions;
|
|
31
|
-
}
|
|
32
|
-
}
|
package/src/lib/Predicate.ts
DELETED