@wq2/brigadier-ts 1.0.0
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/.github/workflows/publish.yml +49 -0
- package/.github/workflows/test.yml +23 -0
- package/LICENSE +21 -0
- package/README.md +131 -0
- package/dist/Command.d.ts +2 -0
- package/dist/Command.js +2 -0
- package/dist/CommandDispatcher.d.ts +20 -0
- package/dist/CommandDispatcher.js +416 -0
- package/dist/ParseResults.d.ts +10 -0
- package/dist/ParseResults.js +21 -0
- package/dist/Predicate.d.ts +1 -0
- package/dist/Predicate.js +2 -0
- package/dist/StringReader.d.ts +26 -0
- package/dist/StringReader.js +188 -0
- package/dist/arguments/ArgumentType.d.ts +5 -0
- package/dist/arguments/ArgumentType.js +13 -0
- package/dist/arguments/BoolArgumentType.d.ts +6 -0
- package/dist/arguments/BoolArgumentType.js +43 -0
- package/dist/arguments/FloatArgumentType.d.ts +7 -0
- package/dist/arguments/FloatArgumentType.js +38 -0
- package/dist/arguments/IntegerArgumentType.d.ts +7 -0
- package/dist/arguments/IntegerArgumentType.js +38 -0
- package/dist/arguments/LongArgumentType.d.ts +9 -0
- package/dist/arguments/LongArgumentType.js +40 -0
- package/dist/arguments/NumberArgumentType.d.ts +12 -0
- package/dist/arguments/NumberArgumentType.js +49 -0
- package/dist/arguments/StringArgumentType.d.ts +12 -0
- package/dist/arguments/StringArgumentType.js +57 -0
- package/dist/builder/ArgumentBuilder.d.ts +25 -0
- package/dist/builder/ArgumentBuilder.js +95 -0
- package/dist/builder/LiteralArgumentBuilder.d.ts +9 -0
- package/dist/builder/LiteralArgumentBuilder.js +47 -0
- package/dist/builder/RequiredArgumentBuilder.d.ts +11 -0
- package/dist/builder/RequiredArgumentBuilder.js +51 -0
- package/dist/context/CommandContext.d.ts +27 -0
- package/dist/context/CommandContext.js +67 -0
- package/dist/context/CommandContextBuilder.d.ts +31 -0
- package/dist/context/CommandContextBuilder.js +118 -0
- package/dist/context/ParsedArgument.d.ts +8 -0
- package/dist/context/ParsedArgument.js +18 -0
- package/dist/context/ParsedCommandNode.d.ts +8 -0
- package/dist/context/ParsedCommandNode.js +17 -0
- package/dist/context/StringRange.d.ts +11 -0
- package/dist/context/StringRange.js +31 -0
- package/dist/context/SuggestionContext.d.ts +6 -0
- package/dist/context/SuggestionContext.js +11 -0
- package/dist/exceptions/CommandErrorType.d.ts +9 -0
- package/dist/exceptions/CommandErrorType.js +27 -0
- package/dist/exceptions/CommandSyntaxError.d.ts +28 -0
- package/dist/exceptions/CommandSyntaxError.js +61 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +46 -0
- package/dist/suggestion/Suggestion.d.ts +12 -0
- package/dist/suggestion/Suggestion.js +49 -0
- package/dist/suggestion/Suggestions.d.ts +13 -0
- package/dist/suggestion/Suggestions.js +58 -0
- package/dist/suggestion/SuggestionsBuilder.d.ts +17 -0
- package/dist/suggestion/SuggestionsBuilder.js +46 -0
- package/dist/tree/ArgumentCommandNode.d.ts +11 -0
- package/dist/tree/ArgumentCommandNode.js +49 -0
- package/dist/tree/CommandNode.d.ts +25 -0
- package/dist/tree/CommandNode.js +74 -0
- package/dist/tree/LiteralCommandNode.d.ts +10 -0
- package/dist/tree/LiteralCommandNode.js +68 -0
- package/dist/tree/RootCommandNode.d.ts +8 -0
- package/dist/tree/RootCommandNode.js +77 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/jest.config.js +12 -0
- package/package.json +34 -0
- package/src/Command.ts +3 -0
- package/src/CommandDispatcher.ts +300 -0
- package/src/ParseResults.ts +30 -0
- package/src/Predicate.ts +1 -0
- package/src/StringReader.ts +197 -0
- package/src/arguments/ArgumentType.ts +14 -0
- package/src/arguments/BoolArgumentType.ts +28 -0
- package/src/arguments/FloatArgumentType.ts +20 -0
- package/src/arguments/IntegerArgumentType.ts +20 -0
- package/src/arguments/LongArgumentType.ts +23 -0
- package/src/arguments/NumberArgumentType.ts +39 -0
- package/src/arguments/StringArgumentType.ts +40 -0
- package/src/builder/ArgumentBuilder.ts +82 -0
- package/src/builder/LiteralArgumentBuilder.ts +30 -0
- package/src/builder/RequiredArgumentBuilder.ts +40 -0
- package/src/context/CommandContext.ts +94 -0
- package/src/context/CommandContextBuilder.ts +148 -0
- package/src/context/ParsedArgument.ts +19 -0
- package/src/context/ParsedCommandNode.ts +19 -0
- package/src/context/StringRange.ts +35 -0
- package/src/context/SuggestionContext.ts +11 -0
- package/src/exceptions/CommandErrorType.ts +20 -0
- package/src/exceptions/CommandSyntaxError.ts +48 -0
- package/src/index.ts +30 -0
- package/src/suggestion/Suggestion.ts +55 -0
- package/src/suggestion/Suggestions.ts +60 -0
- package/src/suggestion/SuggestionsBuilder.ts +60 -0
- package/src/tree/ArgumentCommandNode.ts +48 -0
- package/src/tree/CommandNode.ts +105 -0
- package/src/tree/LiteralCommandNode.ts +64 -0
- package/src/tree/RootCommandNode.ts +30 -0
- package/test/Arguments.test.ts +36 -0
- package/test/CommandDispatcher.test.ts +25 -0
- package/test/StringReader.test.ts +47 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- v*
|
|
7
|
+
workflow_dispatch:
|
|
8
|
+
inputs:
|
|
9
|
+
tag_name:
|
|
10
|
+
description: Version to publish
|
|
11
|
+
required: true
|
|
12
|
+
type: string
|
|
13
|
+
release_name:
|
|
14
|
+
description: GitHub Release name
|
|
15
|
+
required: true
|
|
16
|
+
type: string
|
|
17
|
+
|
|
18
|
+
jobs:
|
|
19
|
+
publish:
|
|
20
|
+
environment: a
|
|
21
|
+
runs-on: ubuntu-slim
|
|
22
|
+
# TODO: also publish to GitHub packages (because why not)
|
|
23
|
+
permissions:
|
|
24
|
+
id-token: write # Required for OIDC
|
|
25
|
+
contents: write
|
|
26
|
+
packages: write
|
|
27
|
+
|
|
28
|
+
steps:
|
|
29
|
+
- uses: actions/checkout@v6
|
|
30
|
+
- uses: actions/setup-node@v6
|
|
31
|
+
with:
|
|
32
|
+
node-version: '24'
|
|
33
|
+
|
|
34
|
+
- name: Install & Build
|
|
35
|
+
run: npm install && npm run build
|
|
36
|
+
|
|
37
|
+
- name: Publish (npm)
|
|
38
|
+
run: npm publish --registry=https://registry.npmjs.org/ --loglevel info
|
|
39
|
+
env:
|
|
40
|
+
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
|
|
41
|
+
NPM_TOKEN: ${{ env.GITHUB_TOKEN }}
|
|
42
|
+
|
|
43
|
+
- name: Create GitHub release
|
|
44
|
+
uses: softprops/action-gh-release@v2
|
|
45
|
+
with:
|
|
46
|
+
tag_name: ${{ github.event.inputs.tag_name || github.ref_name }}
|
|
47
|
+
name: ${{ github.event.inputs.release_name || github.ref_name }}
|
|
48
|
+
env:
|
|
49
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
name: Test
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- master
|
|
7
|
+
pull_request:
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- name: Checkout
|
|
14
|
+
uses: actions/checkout@v1
|
|
15
|
+
|
|
16
|
+
- name: Install
|
|
17
|
+
run: npm ci
|
|
18
|
+
|
|
19
|
+
- name: Build
|
|
20
|
+
run: npm run build
|
|
21
|
+
|
|
22
|
+
- name: Test
|
|
23
|
+
run: npm run test
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Misode
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Brigadier-ts
|
|
2
|
+
|
|
3
|
+
Fork of [brigadier-ts](https://github.com/misode/brigadier-ts) with some changes:
|
|
4
|
+
|
|
5
|
+
- (breaking change) Predicates and Commands are async
|
|
6
|
+
- Update branding
|
|
7
|
+
|
|
8
|
+
(not many, but we'll probably add more later)
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
npm install @wq2/brigadier-ts
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
#### Example using a command source
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import {
|
|
22
|
+
CommandDispatcher,
|
|
23
|
+
IntegerArgumentType,
|
|
24
|
+
literal,
|
|
25
|
+
argument
|
|
26
|
+
} from "brigadier-ts";
|
|
27
|
+
|
|
28
|
+
class CommandSource {
|
|
29
|
+
private a: number;
|
|
30
|
+
constructor(a: number) {
|
|
31
|
+
this.a = a;
|
|
32
|
+
}
|
|
33
|
+
getA(): number {
|
|
34
|
+
return this.a;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const dispatcher = new CommandDispatcher<CommandSource>();
|
|
39
|
+
dispatcher.register(literal("random")
|
|
40
|
+
.executes(async c => 4)
|
|
41
|
+
);
|
|
42
|
+
dispatcher.register(literal("double")
|
|
43
|
+
.then(argument("value", new IntegerArgumentType())
|
|
44
|
+
.executes(async c => 2 * c.get("value"))
|
|
45
|
+
)
|
|
46
|
+
);
|
|
47
|
+
dispatcher.register(literal("add")
|
|
48
|
+
.executes(async c => 4 + c.getSource().getA())
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
console.log(await dispatcher.execute("random", null)); // 4
|
|
52
|
+
console.log(await dispatcher.execute("double 3", null)); // 6
|
|
53
|
+
console.log(await dispatcher.execute("add", new CommandSource(3))); // 7
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
#### Example using redirection and forking
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import {
|
|
60
|
+
CommandDispatcher,
|
|
61
|
+
IntegerArgumentType,
|
|
62
|
+
literal,
|
|
63
|
+
argument
|
|
64
|
+
} from "brigadier-ts";
|
|
65
|
+
|
|
66
|
+
const dispatcher = new CommandDispatcher<number>();
|
|
67
|
+
const execute = dispatcher.register(literal("execute"));
|
|
68
|
+
|
|
69
|
+
dispatcher.register(literal("execute")
|
|
70
|
+
.then(literal("run")
|
|
71
|
+
.redirect(dispatcher.getRoot())
|
|
72
|
+
)
|
|
73
|
+
.then(literal("enumerate")
|
|
74
|
+
.then(argument("nums", new IntegerArgumentType())
|
|
75
|
+
.fork(async execute, c => {
|
|
76
|
+
let list: number[] = [];
|
|
77
|
+
for (let i = c.get("nums"); i > 0; i--) {
|
|
78
|
+
list.push(i);
|
|
79
|
+
}
|
|
80
|
+
return list;
|
|
81
|
+
})
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
dispatcher.register(literal("say")
|
|
87
|
+
.executes(async c => {
|
|
88
|
+
console.log(c.getSource());
|
|
89
|
+
})
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
console.log(await dispatcher.execute("execute enumerate 5 run say", 0));
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### Example using suggestions and errors
|
|
96
|
+
```ts
|
|
97
|
+
import {
|
|
98
|
+
CommandDispatcher,
|
|
99
|
+
IntegerArgumentType,
|
|
100
|
+
literal,
|
|
101
|
+
argument
|
|
102
|
+
} from "brigadier-ts";
|
|
103
|
+
|
|
104
|
+
const dispatcher = new CommandDispatcher<number>();
|
|
105
|
+
dispatcher.register(literal("double")
|
|
106
|
+
.then(argument("value", new IntegerArgumentType())
|
|
107
|
+
.executes(async c => 2 * c.get("value"))
|
|
108
|
+
)
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const getFeedback = async (command: string): Promise<string> => {
|
|
112
|
+
const parseResults = await dispatcher.parse(command, null);
|
|
113
|
+
if (parseResults.getErrors().size > 0) {
|
|
114
|
+
return command + " >> " + parseResults.getErrors().values().next().value.message;
|
|
115
|
+
}
|
|
116
|
+
const suggestions = await dispatcher.getCompletionSuggestions(parseResults);
|
|
117
|
+
if (suggestions.getList().length > 0) {
|
|
118
|
+
const s = suggestions.getList()[0];
|
|
119
|
+
return command + " >> S " + s.getText() + " (" + s.getRange().getStart() + ", " + s.getRange().getEnd() + ")";
|
|
120
|
+
}
|
|
121
|
+
const usage = await dispatcher.getAllUsage(parseResults.getContext().getRootNode(), null, false);
|
|
122
|
+
if (usage.length > 0) {
|
|
123
|
+
return command + " >> U " + usage[0];
|
|
124
|
+
}
|
|
125
|
+
return command;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
getFeedback("dou").then(f => console.log(f)); // dou >> S double
|
|
129
|
+
getFeedback("double").then(f => console.log(f)); // double >> U double <value>
|
|
130
|
+
getFeedback("double ").then(f => console.log(f)); // double >> Expected integer at position 7: double <--[HERE]
|
|
131
|
+
```
|
package/dist/Command.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { RootCommandNode, LiteralCommandNode, StringReader, LiteralArgumentBuilder, CommandNode, ParseResults, Suggestions } from ".";
|
|
2
|
+
export declare class CommandDispatcher<S> {
|
|
3
|
+
private root;
|
|
4
|
+
private static USAGE_OPTIONAL_OPEN;
|
|
5
|
+
private static USAGE_OPTIONAL_CLOSE;
|
|
6
|
+
private static USAGE_REQUIRED_OPEN;
|
|
7
|
+
private static USAGE_REQUIRED_CLOSE;
|
|
8
|
+
private static USAGE_OR;
|
|
9
|
+
constructor();
|
|
10
|
+
register(command: LiteralArgumentBuilder<S>): LiteralCommandNode<S>;
|
|
11
|
+
execute(parse: ParseResults<S> | string, source: S): Promise<number>;
|
|
12
|
+
parse(reader: StringReader | string, source: S): Promise<ParseResults<S>>;
|
|
13
|
+
private parseNodes;
|
|
14
|
+
getAllUsage(node: CommandNode<S>, source: S, restricted: boolean): Promise<string[]>;
|
|
15
|
+
private getAllUsageImpl;
|
|
16
|
+
getCompletionSuggestions(parse: ParseResults<S>, cursor?: number): Promise<Suggestions>;
|
|
17
|
+
getSmartUsage(node: CommandNode<S>, source: S): Map<CommandNode<S>, string>;
|
|
18
|
+
getSmartUsage(node: CommandNode<S>, source: S, optional: boolean, deep: boolean): string;
|
|
19
|
+
getRoot(): RootCommandNode<S>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.CommandDispatcher = void 0;
|
|
40
|
+
var _1 = require(".");
|
|
41
|
+
var CommandDispatcher = /** @class */ (function () {
|
|
42
|
+
function CommandDispatcher() {
|
|
43
|
+
this.root = new _1.RootCommandNode();
|
|
44
|
+
}
|
|
45
|
+
CommandDispatcher.prototype.register = function (command) {
|
|
46
|
+
var build = command.build();
|
|
47
|
+
this.root.addChild(build);
|
|
48
|
+
return build;
|
|
49
|
+
};
|
|
50
|
+
CommandDispatcher.prototype.execute = function (parse, source) {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
52
|
+
var result, successfulForks, forked, foundCommand, command, original, contexts, next, size, _loop_1, i;
|
|
53
|
+
return __generator(this, function (_a) {
|
|
54
|
+
switch (_a.label) {
|
|
55
|
+
case 0:
|
|
56
|
+
if (!(typeof (parse) === "string")) return [3 /*break*/, 2];
|
|
57
|
+
return [4 /*yield*/, this.parse(new _1.StringReader(parse), source)];
|
|
58
|
+
case 1:
|
|
59
|
+
parse = _a.sent();
|
|
60
|
+
_a.label = 2;
|
|
61
|
+
case 2:
|
|
62
|
+
if (parse.getReader().canRead()) {
|
|
63
|
+
if (parse.getErrors().size == 1) {
|
|
64
|
+
throw parse.getErrors().values().next();
|
|
65
|
+
}
|
|
66
|
+
else if (parse.getContext().getRange().isEmpty()) {
|
|
67
|
+
throw _1.CommandSyntaxError.DISPATCHER_UNKNOWN_COMMAND.createWithContext(parse.getReader());
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
throw _1.CommandSyntaxError.DISPATCHER_UNKNOWN_ARGUMENT.createWithContext(parse.getReader());
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
result = 0;
|
|
74
|
+
successfulForks = 0;
|
|
75
|
+
forked = false;
|
|
76
|
+
foundCommand = false;
|
|
77
|
+
command = parse.getReader().getString();
|
|
78
|
+
original = parse.getContext().build(command);
|
|
79
|
+
contexts = [original];
|
|
80
|
+
next = [];
|
|
81
|
+
_a.label = 3;
|
|
82
|
+
case 3:
|
|
83
|
+
if (!(contexts.length > 0)) return [3 /*break*/, 8];
|
|
84
|
+
size = contexts.length;
|
|
85
|
+
_loop_1 = function (i) {
|
|
86
|
+
var context, child, modifier, results, value, e_1;
|
|
87
|
+
return __generator(this, function (_b) {
|
|
88
|
+
switch (_b.label) {
|
|
89
|
+
case 0:
|
|
90
|
+
context = contexts[i];
|
|
91
|
+
child = context.getChild();
|
|
92
|
+
if (!(child !== null)) return [3 /*break*/, 1];
|
|
93
|
+
forked = forked || context.isForked();
|
|
94
|
+
if (child.hasNodes()) {
|
|
95
|
+
foundCommand = true;
|
|
96
|
+
modifier = context.getRedirectModifier();
|
|
97
|
+
if (modifier === null) {
|
|
98
|
+
next.push(child.copyFor(context.getSource()));
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
try {
|
|
102
|
+
results = modifier(context);
|
|
103
|
+
results.forEach(function (source) {
|
|
104
|
+
next.push(child.copyFor(source));
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
if (!forked)
|
|
109
|
+
throw e;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return [3 /*break*/, 5];
|
|
114
|
+
case 1:
|
|
115
|
+
if (!context.getCommand()) return [3 /*break*/, 5];
|
|
116
|
+
foundCommand = true;
|
|
117
|
+
_b.label = 2;
|
|
118
|
+
case 2:
|
|
119
|
+
_b.trys.push([2, 4, , 5]);
|
|
120
|
+
return [4 /*yield*/, context.getCommand()(context)];
|
|
121
|
+
case 3:
|
|
122
|
+
value = _b.sent();
|
|
123
|
+
result += (value || value === 0) ? value : 1;
|
|
124
|
+
successfulForks++;
|
|
125
|
+
return [3 /*break*/, 5];
|
|
126
|
+
case 4:
|
|
127
|
+
e_1 = _b.sent();
|
|
128
|
+
if (!forked)
|
|
129
|
+
throw e_1;
|
|
130
|
+
return [3 /*break*/, 5];
|
|
131
|
+
case 5: return [2 /*return*/];
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
i = 0;
|
|
136
|
+
_a.label = 4;
|
|
137
|
+
case 4:
|
|
138
|
+
if (!(i < size)) return [3 /*break*/, 7];
|
|
139
|
+
return [5 /*yield**/, _loop_1(i)];
|
|
140
|
+
case 5:
|
|
141
|
+
_a.sent();
|
|
142
|
+
_a.label = 6;
|
|
143
|
+
case 6:
|
|
144
|
+
i++;
|
|
145
|
+
return [3 /*break*/, 4];
|
|
146
|
+
case 7:
|
|
147
|
+
contexts = next;
|
|
148
|
+
next = [];
|
|
149
|
+
return [3 /*break*/, 3];
|
|
150
|
+
case 8:
|
|
151
|
+
if (!foundCommand) {
|
|
152
|
+
throw _1.CommandSyntaxError.DISPATCHER_UNKNOWN_COMMAND.createWithContext(parse.getReader());
|
|
153
|
+
}
|
|
154
|
+
return [2 /*return*/, forked ? successfulForks : result];
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
CommandDispatcher.prototype.parse = function (reader, source) {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
161
|
+
var context;
|
|
162
|
+
return __generator(this, function (_a) {
|
|
163
|
+
reader = new _1.StringReader(reader);
|
|
164
|
+
context = new _1.CommandContextBuilder(this, source, this.root, reader.getCursor());
|
|
165
|
+
return [2 /*return*/, this.parseNodes(this.root, reader, context)];
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
CommandDispatcher.prototype.parseNodes = function (node, originalReader, contextSoFar) {
|
|
170
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
171
|
+
var source, errors, potentials, cursor, _i, _a, child, context, reader, childContext, parse;
|
|
172
|
+
return __generator(this, function (_b) {
|
|
173
|
+
switch (_b.label) {
|
|
174
|
+
case 0:
|
|
175
|
+
source = contextSoFar.getSource();
|
|
176
|
+
errors = new Map();
|
|
177
|
+
potentials = [];
|
|
178
|
+
cursor = originalReader.getCursor();
|
|
179
|
+
_i = 0, _a = node.getRelevantNodes(originalReader);
|
|
180
|
+
_b.label = 1;
|
|
181
|
+
case 1:
|
|
182
|
+
if (!(_i < _a.length)) return [3 /*break*/, 8];
|
|
183
|
+
child = _a[_i];
|
|
184
|
+
return [4 /*yield*/, child.canUse(source)];
|
|
185
|
+
case 2:
|
|
186
|
+
if (!(_b.sent())) {
|
|
187
|
+
return [3 /*break*/, 7];
|
|
188
|
+
}
|
|
189
|
+
context = contextSoFar.copy();
|
|
190
|
+
reader = new _1.StringReader(originalReader);
|
|
191
|
+
try {
|
|
192
|
+
try {
|
|
193
|
+
child.parse(reader, context);
|
|
194
|
+
}
|
|
195
|
+
catch (e) {
|
|
196
|
+
if (e instanceof _1.CommandSyntaxError) {
|
|
197
|
+
throw e;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
throw _1.CommandSyntaxError.DISPATCHER_PARSE_ERROR.createWithContext(reader, e.message);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (reader.canRead() && reader.peek() !== " ") {
|
|
204
|
+
throw _1.CommandSyntaxError.DISPATCHER_EXPECTED_ARGUMENT_SEPARATOR.createWithContext(reader);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
catch (e) {
|
|
208
|
+
if (e instanceof _1.CommandSyntaxError) {
|
|
209
|
+
errors.set(child, e);
|
|
210
|
+
reader.setCursor(cursor);
|
|
211
|
+
return [3 /*break*/, 7];
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
throw e;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
context.withCommand(child.getCommand());
|
|
218
|
+
if (!reader.canRead(child.getRedirect() === null ? 2 : 1)) return [3 /*break*/, 6];
|
|
219
|
+
reader.skip();
|
|
220
|
+
if (!child.getRedirect()) return [3 /*break*/, 4];
|
|
221
|
+
childContext = new _1.CommandContextBuilder(this, source, child.getRedirect(), reader.getCursor());
|
|
222
|
+
return [4 /*yield*/, this.parseNodes(child.getRedirect(), reader, childContext)];
|
|
223
|
+
case 3:
|
|
224
|
+
parse = _b.sent();
|
|
225
|
+
context.withChild(parse.getContext());
|
|
226
|
+
return [2 /*return*/, new _1.ParseResults(context, parse.getReader(), parse.getErrors())];
|
|
227
|
+
case 4:
|
|
228
|
+
potentials.push(this.parseNodes(child, reader, context));
|
|
229
|
+
_b.label = 5;
|
|
230
|
+
case 5: return [3 /*break*/, 7];
|
|
231
|
+
case 6:
|
|
232
|
+
potentials.push(new _1.ParseResults(context, reader, new Map()));
|
|
233
|
+
_b.label = 7;
|
|
234
|
+
case 7:
|
|
235
|
+
_i++;
|
|
236
|
+
return [3 /*break*/, 1];
|
|
237
|
+
case 8:
|
|
238
|
+
if (potentials.length == 0) {
|
|
239
|
+
potentials.push(new _1.ParseResults(contextSoFar, originalReader, errors));
|
|
240
|
+
}
|
|
241
|
+
return [2 /*return*/, potentials[0]];
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
CommandDispatcher.prototype.getAllUsage = function (node, source, restricted) {
|
|
247
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
248
|
+
var result;
|
|
249
|
+
return __generator(this, function (_a) {
|
|
250
|
+
switch (_a.label) {
|
|
251
|
+
case 0:
|
|
252
|
+
result = [];
|
|
253
|
+
return [4 /*yield*/, this.getAllUsageImpl(node, source, result, "", restricted)];
|
|
254
|
+
case 1:
|
|
255
|
+
_a.sent();
|
|
256
|
+
return [2 /*return*/, result];
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
CommandDispatcher.prototype.getAllUsageImpl = function (node, source, result, prefix, restricted) {
|
|
262
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
263
|
+
var _a, redirect, _i, _b, child, newPrefix;
|
|
264
|
+
return __generator(this, function (_c) {
|
|
265
|
+
switch (_c.label) {
|
|
266
|
+
case 0:
|
|
267
|
+
_a = restricted;
|
|
268
|
+
if (!_a) return [3 /*break*/, 2];
|
|
269
|
+
return [4 /*yield*/, node.canUse(source)];
|
|
270
|
+
case 1:
|
|
271
|
+
_a = !(_c.sent());
|
|
272
|
+
_c.label = 2;
|
|
273
|
+
case 2:
|
|
274
|
+
if (_a) {
|
|
275
|
+
return [2 /*return*/];
|
|
276
|
+
}
|
|
277
|
+
if (node.getCommand() != null) {
|
|
278
|
+
result.push(prefix);
|
|
279
|
+
}
|
|
280
|
+
if (!(node.getRedirect() != null)) return [3 /*break*/, 3];
|
|
281
|
+
redirect = node.getRedirect() === this.root ? "..." : "-> " + node.getRedirect().getUsageText();
|
|
282
|
+
result.push(prefix.length === 0 ? node.getUsageText() + " " + redirect : prefix + " " + redirect);
|
|
283
|
+
return [3 /*break*/, 7];
|
|
284
|
+
case 3:
|
|
285
|
+
if (!(node.getChildren().length > 0)) return [3 /*break*/, 7];
|
|
286
|
+
_i = 0, _b = node.getChildren();
|
|
287
|
+
_c.label = 4;
|
|
288
|
+
case 4:
|
|
289
|
+
if (!(_i < _b.length)) return [3 /*break*/, 7];
|
|
290
|
+
child = _b[_i];
|
|
291
|
+
newPrefix = prefix.length === 0 ? child.getUsageText() : prefix + " " + child.getUsageText();
|
|
292
|
+
return [4 /*yield*/, this.getAllUsageImpl(child, source, result, newPrefix, restricted)];
|
|
293
|
+
case 5:
|
|
294
|
+
_c.sent();
|
|
295
|
+
_c.label = 6;
|
|
296
|
+
case 6:
|
|
297
|
+
_i++;
|
|
298
|
+
return [3 /*break*/, 4];
|
|
299
|
+
case 7: return [2 /*return*/];
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
};
|
|
304
|
+
CommandDispatcher.prototype.getCompletionSuggestions = function (parse, cursor) {
|
|
305
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
306
|
+
var context, nodeBeforeCursor, parent, start, fullInput, truncatedInput, promises, _i, _a, node, promise, suggestions;
|
|
307
|
+
return __generator(this, function (_b) {
|
|
308
|
+
switch (_b.label) {
|
|
309
|
+
case 0:
|
|
310
|
+
if (cursor === undefined) {
|
|
311
|
+
cursor = parse.getReader().getTotalLength();
|
|
312
|
+
}
|
|
313
|
+
context = parse.getContext();
|
|
314
|
+
nodeBeforeCursor = context.findSuggestionContext(cursor);
|
|
315
|
+
parent = nodeBeforeCursor.parent;
|
|
316
|
+
start = Math.min(nodeBeforeCursor.startPos, cursor);
|
|
317
|
+
fullInput = parse.getReader().getString();
|
|
318
|
+
truncatedInput = fullInput.substring(0, cursor);
|
|
319
|
+
promises = [];
|
|
320
|
+
for (_i = 0, _a = parent.getChildren(); _i < _a.length; _i++) {
|
|
321
|
+
node = _a[_i];
|
|
322
|
+
promise = _1.Suggestions.empty();
|
|
323
|
+
try {
|
|
324
|
+
promise = node.listSuggestions(context.build(truncatedInput), new _1.SuggestionsBuilder(truncatedInput, start));
|
|
325
|
+
}
|
|
326
|
+
catch (ignored) {
|
|
327
|
+
console.log("???", ignored);
|
|
328
|
+
}
|
|
329
|
+
promises.push(promise);
|
|
330
|
+
}
|
|
331
|
+
return [4 /*yield*/, Promise.all(promises)];
|
|
332
|
+
case 1:
|
|
333
|
+
suggestions = _b.sent();
|
|
334
|
+
return [2 /*return*/, _1.Suggestions.merge(fullInput, suggestions)];
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
};
|
|
339
|
+
CommandDispatcher.prototype.getSmartUsage = function (node, source, optional, deep) {
|
|
340
|
+
if (optional === undefined && deep === undefined) {
|
|
341
|
+
var result = new Map();
|
|
342
|
+
var optional_1 = node.getCommand() !== undefined && node.getCommand() !== null;
|
|
343
|
+
var children = node.getChildren();
|
|
344
|
+
for (var index in children) {
|
|
345
|
+
var child = children[index];
|
|
346
|
+
var usage = this.getSmartUsage(child, source, optional_1, false);
|
|
347
|
+
if (usage !== undefined) {
|
|
348
|
+
result.set(child, usage);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return result;
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
if (!node.canUse(source)) {
|
|
355
|
+
return undefined;
|
|
356
|
+
}
|
|
357
|
+
var self_1 = optional ? CommandDispatcher.USAGE_OPTIONAL_OPEN + node.getUsageText() + CommandDispatcher.USAGE_OPTIONAL_CLOSE : node.getUsageText();
|
|
358
|
+
var childOptional = node.getCommand() !== undefined;
|
|
359
|
+
var open_1 = childOptional ? CommandDispatcher.USAGE_OPTIONAL_OPEN : CommandDispatcher.USAGE_REQUIRED_OPEN;
|
|
360
|
+
var close_1 = childOptional ? CommandDispatcher.USAGE_OPTIONAL_CLOSE : CommandDispatcher.USAGE_REQUIRED_CLOSE;
|
|
361
|
+
if (!deep) {
|
|
362
|
+
if (node.getRedirect() !== undefined) {
|
|
363
|
+
var redirect = node.getRedirect() === this.root ? "..." : "-> " + node.getRedirect().getUsageText();
|
|
364
|
+
return self_1 + " " + redirect;
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
var children = node.getChildren().filter(function (c) { return c.canUse(source); });
|
|
368
|
+
if (children.length === 1) {
|
|
369
|
+
var usage = String(this.getSmartUsage(children[0], source, childOptional, childOptional));
|
|
370
|
+
if (usage !== undefined) {
|
|
371
|
+
return self_1 + " " + usage;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
else if (children.length > 1) {
|
|
375
|
+
var childUsage = new Set();
|
|
376
|
+
for (var index in children) {
|
|
377
|
+
var child = children[index];
|
|
378
|
+
var usage = this.getSmartUsage(child, source, childOptional, true);
|
|
379
|
+
if (usage !== undefined) {
|
|
380
|
+
childUsage.add(usage);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (childUsage.size === 1) {
|
|
384
|
+
var usage = childUsage.values().next().value;
|
|
385
|
+
return self_1 + " " + (childOptional ? CommandDispatcher.USAGE_OPTIONAL_OPEN + usage + CommandDispatcher.USAGE_OPTIONAL_CLOSE : usage);
|
|
386
|
+
}
|
|
387
|
+
else if (childUsage.size > 1) {
|
|
388
|
+
var builder = open_1;
|
|
389
|
+
for (var index = 0; index < children.length; index++) {
|
|
390
|
+
var child = children[index];
|
|
391
|
+
if (index > 0) {
|
|
392
|
+
builder += CommandDispatcher.USAGE_OR;
|
|
393
|
+
}
|
|
394
|
+
builder += child.getUsageText();
|
|
395
|
+
}
|
|
396
|
+
if (children.length > 0) {
|
|
397
|
+
builder += close_1;
|
|
398
|
+
return self_1 + " " + builder;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
CommandDispatcher.prototype.getRoot = function () {
|
|
407
|
+
return this.root;
|
|
408
|
+
};
|
|
409
|
+
CommandDispatcher.USAGE_OPTIONAL_OPEN = "[";
|
|
410
|
+
CommandDispatcher.USAGE_OPTIONAL_CLOSE = "]";
|
|
411
|
+
CommandDispatcher.USAGE_REQUIRED_OPEN = "(";
|
|
412
|
+
CommandDispatcher.USAGE_REQUIRED_CLOSE = ")";
|
|
413
|
+
CommandDispatcher.USAGE_OR = "|";
|
|
414
|
+
return CommandDispatcher;
|
|
415
|
+
}());
|
|
416
|
+
exports.CommandDispatcher = CommandDispatcher;
|