@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.
- package/README.md +1 -0
- package/dist/Command.d.ts +1 -1
- package/dist/CommandDispatcher.d.ts +1 -1
- package/dist/CommandDispatcher.js +231 -357
- package/dist/ParseResults.d.ts +1 -1
- package/dist/ParseResults.js +9 -10
- package/dist/StringReader.d.ts +1 -1
- package/dist/StringReader.js +75 -77
- package/dist/arguments/ArgumentType.d.ts +1 -1
- package/dist/arguments/ArgumentType.js +9 -8
- package/dist/arguments/BoolArgumentType.d.ts +5 -1
- package/dist/arguments/BoolArgumentType.js +9 -27
- package/dist/arguments/FloatArgumentType.d.ts +1 -1
- package/dist/arguments/FloatArgumentType.js +11 -30
- package/dist/arguments/IntegerArgumentType.d.ts +1 -1
- package/dist/arguments/IntegerArgumentType.js +11 -30
- package/dist/arguments/LongArgumentType.d.ts +3 -3
- package/dist/arguments/LongArgumentType.js +13 -32
- package/dist/arguments/NumberArgumentType.d.ts +2 -2
- package/dist/arguments/NumberArgumentType.js +15 -33
- package/dist/arguments/StringArgumentType.d.ts +1 -1
- package/dist/arguments/StringArgumentType.js +11 -29
- package/dist/builder/ArgumentBuilder.d.ts +1 -1
- package/dist/builder/ArgumentBuilder.js +31 -71
- package/dist/builder/LiteralArgumentBuilder.js +14 -33
- package/dist/builder/RequiredArgumentBuilder.d.ts +1 -1
- package/dist/builder/RequiredArgumentBuilder.js +18 -37
- package/dist/context/CommandContext.d.ts +1 -1
- package/dist/context/CommandContext.js +31 -32
- package/dist/context/CommandContextBuilder.d.ts +1 -1
- package/dist/context/CommandContextBuilder.js +47 -50
- package/dist/context/ParsedArgument.js +8 -9
- package/dist/context/ParsedCommandNode.d.ts +1 -1
- package/dist/context/ParsedCommandNode.js +7 -8
- package/dist/context/StringRange.js +17 -18
- package/dist/context/SuggestionContext.d.ts +1 -1
- package/dist/context/SuggestionContext.js +3 -4
- package/dist/exceptions/CommandErrorType.d.ts +3 -3
- package/dist/exceptions/CommandErrorType.js +10 -19
- package/dist/exceptions/CommandSyntaxError.js +36 -54
- package/dist/suggestion/Suggestion.d.ts +1 -1
- package/dist/suggestion/Suggestion.js +16 -17
- package/dist/suggestion/Suggestions.d.ts +1 -1
- package/dist/suggestion/Suggestions.js +28 -30
- package/dist/suggestion/SuggestionsBuilder.d.ts +1 -1
- package/dist/suggestion/SuggestionsBuilder.js +22 -23
- package/dist/tree/ArgumentCommandNode.d.ts +1 -1
- package/dist/tree/ArgumentCommandNode.js +22 -40
- package/dist/tree/CommandNode.d.ts +1 -1
- package/dist/tree/CommandNode.js +28 -29
- package/dist/tree/LiteralCommandNode.d.ts +1 -1
- package/dist/tree/LiteralCommandNode.js +28 -41
- package/dist/tree/RootCommandNode.d.ts +1 -1
- package/dist/tree/RootCommandNode.js +17 -69
- package/jest.config.js +5 -5
- package/package.json +32 -32
- package/src/Command.ts +2 -1
- package/src/CommandDispatcher.ts +397 -295
- package/src/ParseResults.ts +26 -22
- package/src/StringReader.ts +212 -193
- package/src/arguments/ArgumentType.ts +13 -8
- package/src/arguments/BoolArgumentType.ts +22 -21
- package/src/arguments/FloatArgumentType.ts +13 -14
- package/src/arguments/IntegerArgumentType.ts +13 -14
- package/src/arguments/LongArgumentType.ts +16 -17
- package/src/arguments/NumberArgumentType.ts +48 -38
- package/src/arguments/StringArgumentType.ts +26 -26
- package/src/builder/ArgumentBuilder.ts +80 -75
- package/src/builder/LiteralArgumentBuilder.ts +31 -21
- package/src/builder/RequiredArgumentBuilder.ts +42 -32
- package/src/context/CommandContext.ts +99 -76
- package/src/context/CommandContextBuilder.ts +169 -143
- package/src/context/ParsedArgument.ts +13 -13
- package/src/context/ParsedCommandNode.ts +14 -14
- package/src/context/StringRange.ts +26 -26
- package/src/context/SuggestionContext.ts +7 -7
- package/src/exceptions/CommandErrorType.ts +20 -13
- package/src/exceptions/CommandSyntaxError.ts +78 -37
- package/src/index.ts +30 -30
- package/src/suggestion/Suggestion.ts +46 -46
- package/src/suggestion/Suggestions.ts +59 -57
- package/src/suggestion/SuggestionsBuilder.ts +59 -57
- package/src/tree/ArgumentCommandNode.ts +51 -40
- package/src/tree/CommandNode.ts +96 -87
- package/src/tree/LiteralCommandNode.ts +78 -57
- package/src/tree/RootCommandNode.ts +33 -23
- package/test/Arguments.test.ts +47 -33
- package/test/CommandDispatcher.test.ts +18 -22
- package/test/StringReader.test.ts +47 -47
- package/tsconfig.json +9 -14
|
@@ -1,349 +1,212 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.CommandDispatcher = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
4
|
+
const _1 = require(".");
|
|
5
|
+
class CommandDispatcher {
|
|
6
|
+
constructor() {
|
|
43
7
|
this.root = new _1.RootCommandNode();
|
|
44
8
|
}
|
|
45
|
-
|
|
46
|
-
|
|
9
|
+
register(command) {
|
|
10
|
+
const build = command.build();
|
|
47
11
|
this.root.addChild(build);
|
|
48
12
|
return build;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (
|
|
84
|
-
|
|
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];
|
|
13
|
+
}
|
|
14
|
+
async execute(parse, source) {
|
|
15
|
+
if (typeof parse === "string") {
|
|
16
|
+
parse = await this.parse(new _1.StringReader(parse), source);
|
|
17
|
+
}
|
|
18
|
+
if (parse.getReader().canRead()) {
|
|
19
|
+
if (parse.getErrors().size === 1) {
|
|
20
|
+
throw parse.getErrors().values().next();
|
|
21
|
+
}
|
|
22
|
+
else if (parse.getContext().getRange().isEmpty()) {
|
|
23
|
+
throw _1.CommandSyntaxError.DISPATCHER_UNKNOWN_COMMAND.createWithContext(parse.getReader());
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
throw _1.CommandSyntaxError.DISPATCHER_UNKNOWN_ARGUMENT.createWithContext(parse.getReader());
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
let result = 0;
|
|
30
|
+
let successfulForks = 0;
|
|
31
|
+
let forked = false;
|
|
32
|
+
let foundCommand = false;
|
|
33
|
+
const command = parse.getReader().getString();
|
|
34
|
+
const original = parse.getContext().build(command);
|
|
35
|
+
let contexts = [original];
|
|
36
|
+
let next = [];
|
|
37
|
+
while (contexts.length > 0) {
|
|
38
|
+
const size = contexts.length;
|
|
39
|
+
for (let i = 0; i < size; i++) {
|
|
40
|
+
const context = contexts[i];
|
|
41
|
+
const child = context.getChild();
|
|
42
|
+
if (child !== null) {
|
|
43
|
+
forked = forked || context.isForked();
|
|
44
|
+
if (child.hasNodes()) {
|
|
45
|
+
foundCommand = true;
|
|
46
|
+
const modifier = context.getRedirectModifier();
|
|
47
|
+
if (modifier === null) {
|
|
48
|
+
next.push(child.copyFor(context.getSource()));
|
|
188
49
|
}
|
|
189
|
-
|
|
190
|
-
reader = new _1.StringReader(originalReader);
|
|
191
|
-
try {
|
|
50
|
+
else {
|
|
192
51
|
try {
|
|
193
|
-
|
|
52
|
+
const results = modifier(context);
|
|
53
|
+
results.forEach((source) => {
|
|
54
|
+
next.push(child.copyFor(source));
|
|
55
|
+
});
|
|
194
56
|
}
|
|
195
57
|
catch (e) {
|
|
196
|
-
if (
|
|
58
|
+
if (!forked)
|
|
197
59
|
throw e;
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
throw _1.CommandSyntaxError.DISPATCHER_PARSE_ERROR.createWithContext(reader, e.message);
|
|
201
|
-
}
|
|
202
60
|
}
|
|
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
61
|
}
|
|
241
|
-
|
|
62
|
+
}
|
|
242
63
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
case 1:
|
|
255
|
-
_a.sent();
|
|
256
|
-
return [2 /*return*/, result];
|
|
64
|
+
else if (context.getCommand()) {
|
|
65
|
+
foundCommand = true;
|
|
66
|
+
try {
|
|
67
|
+
const value = await context.getCommand()(context);
|
|
68
|
+
result += value || value === 0 ? value : 1;
|
|
69
|
+
successfulForks++;
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
if (!forked)
|
|
73
|
+
throw e;
|
|
74
|
+
}
|
|
257
75
|
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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*/];
|
|
76
|
+
}
|
|
77
|
+
contexts = next;
|
|
78
|
+
next = [];
|
|
79
|
+
}
|
|
80
|
+
if (!foundCommand) {
|
|
81
|
+
throw _1.CommandSyntaxError.DISPATCHER_UNKNOWN_COMMAND.createWithContext(parse.getReader());
|
|
82
|
+
}
|
|
83
|
+
return forked ? successfulForks : result;
|
|
84
|
+
}
|
|
85
|
+
async parse(reader, source) {
|
|
86
|
+
reader = new _1.StringReader(reader);
|
|
87
|
+
const context = new _1.CommandContextBuilder(this, source, this.root, reader.getCursor());
|
|
88
|
+
return this.parseNodes(this.root, reader, context);
|
|
89
|
+
}
|
|
90
|
+
async parseNodes(node, originalReader, contextSoFar) {
|
|
91
|
+
const source = contextSoFar.getSource();
|
|
92
|
+
const errors = new Map();
|
|
93
|
+
const potentials = [];
|
|
94
|
+
const cursor = originalReader.getCursor();
|
|
95
|
+
for (const child of node.getRelevantNodes(originalReader)) {
|
|
96
|
+
if (!(await child.canUse(source))) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const context = contextSoFar.copy();
|
|
100
|
+
const reader = new _1.StringReader(originalReader);
|
|
101
|
+
try {
|
|
102
|
+
try {
|
|
103
|
+
child.parse(reader, context);
|
|
300
104
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
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)];
|
|
105
|
+
catch (e) {
|
|
106
|
+
if (e instanceof _1.CommandSyntaxError) {
|
|
107
|
+
throw e;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
throw _1.CommandSyntaxError.DISPATCHER_PARSE_ERROR.createWithContext(reader, e.message);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (reader.canRead() && reader.peek() !== " ") {
|
|
114
|
+
throw _1.CommandSyntaxError.DISPATCHER_EXPECTED_ARGUMENT_SEPARATOR.createWithContext(reader);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
if (e instanceof _1.CommandSyntaxError) {
|
|
119
|
+
errors.set(child, e);
|
|
120
|
+
reader.setCursor(cursor);
|
|
121
|
+
continue;
|
|
335
122
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
123
|
+
else {
|
|
124
|
+
throw e;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
context.withCommand(child.getCommand());
|
|
128
|
+
if (reader.canRead(child.getRedirect() === null ? 2 : 1)) {
|
|
129
|
+
reader.skip();
|
|
130
|
+
if (child.getRedirect()) {
|
|
131
|
+
const childContext = new _1.CommandContextBuilder(this, source, child.getRedirect(), reader.getCursor());
|
|
132
|
+
const parse = await this.parseNodes(child.getRedirect(), reader, childContext);
|
|
133
|
+
context.withChild(parse.getContext());
|
|
134
|
+
return new _1.ParseResults(context, parse.getReader(), parse.getErrors());
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
potentials.push(this.parseNodes(child, reader, context));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
potentials.push(new _1.ParseResults(context, reader, new Map()));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (potentials.length === 0) {
|
|
145
|
+
potentials.push(new _1.ParseResults(contextSoFar, originalReader, errors));
|
|
146
|
+
}
|
|
147
|
+
return potentials[0];
|
|
148
|
+
}
|
|
149
|
+
async getAllUsage(node, source, restricted) {
|
|
150
|
+
const result = [];
|
|
151
|
+
await this.getAllUsageImpl(node, source, result, "", restricted);
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
async getAllUsageImpl(node, source, result, prefix, restricted) {
|
|
155
|
+
if (restricted && !(await node.canUse(source))) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (node.getCommand() != null) {
|
|
159
|
+
result.push(prefix);
|
|
160
|
+
}
|
|
161
|
+
if (node.getRedirect() != null) {
|
|
162
|
+
const redirect = node.getRedirect() === this.root
|
|
163
|
+
? "..."
|
|
164
|
+
: `-> ${node.getRedirect().getUsageText()}`;
|
|
165
|
+
result.push(prefix.length === 0
|
|
166
|
+
? `${node.getUsageText()} ${redirect}`
|
|
167
|
+
: `${prefix} ${redirect}`);
|
|
168
|
+
}
|
|
169
|
+
else if (node.getChildren().length > 0) {
|
|
170
|
+
for (const child of node.getChildren()) {
|
|
171
|
+
const newPrefix = prefix.length === 0
|
|
172
|
+
? child.getUsageText()
|
|
173
|
+
: `${prefix} ${child.getUsageText()}`;
|
|
174
|
+
await this.getAllUsageImpl(child, source, result, newPrefix, restricted);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async getCompletionSuggestions(parse, cursor) {
|
|
179
|
+
if (cursor === undefined) {
|
|
180
|
+
cursor = parse.getReader().getTotalLength();
|
|
181
|
+
}
|
|
182
|
+
const context = parse.getContext();
|
|
183
|
+
const nodeBeforeCursor = context.findSuggestionContext(cursor);
|
|
184
|
+
const parent = nodeBeforeCursor.parent;
|
|
185
|
+
const start = Math.min(nodeBeforeCursor.startPos, cursor);
|
|
186
|
+
const fullInput = parse.getReader().getString();
|
|
187
|
+
const truncatedInput = fullInput.substring(0, cursor);
|
|
188
|
+
const promises = [];
|
|
189
|
+
for (const node of parent.getChildren()) {
|
|
190
|
+
let promise = _1.Suggestions.empty();
|
|
191
|
+
try {
|
|
192
|
+
promise = node.listSuggestions(context.build(truncatedInput), new _1.SuggestionsBuilder(truncatedInput, start));
|
|
193
|
+
}
|
|
194
|
+
catch (ignored) {
|
|
195
|
+
console.log("???", ignored);
|
|
196
|
+
}
|
|
197
|
+
promises.push(promise);
|
|
198
|
+
}
|
|
199
|
+
const suggestions = await Promise.all(promises);
|
|
200
|
+
return _1.Suggestions.merge(fullInput, suggestions);
|
|
201
|
+
}
|
|
202
|
+
getSmartUsage(node, source, optional, deep) {
|
|
340
203
|
if (optional === undefined && deep === undefined) {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
for (
|
|
345
|
-
|
|
346
|
-
|
|
204
|
+
const result = new Map();
|
|
205
|
+
const optional = node.getCommand() !== undefined && node.getCommand() !== null;
|
|
206
|
+
const children = node.getChildren();
|
|
207
|
+
for (const index in children) {
|
|
208
|
+
const child = children[index];
|
|
209
|
+
const usage = this.getSmartUsage(child, source, optional, false);
|
|
347
210
|
if (usage !== undefined) {
|
|
348
211
|
result.set(child, usage);
|
|
349
212
|
}
|
|
@@ -354,63 +217,74 @@ var CommandDispatcher = /** @class */ (function () {
|
|
|
354
217
|
if (!node.canUse(source)) {
|
|
355
218
|
return undefined;
|
|
356
219
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
220
|
+
const self = optional
|
|
221
|
+
? CommandDispatcher.USAGE_OPTIONAL_OPEN +
|
|
222
|
+
node.getUsageText() +
|
|
223
|
+
CommandDispatcher.USAGE_OPTIONAL_CLOSE
|
|
224
|
+
: node.getUsageText();
|
|
225
|
+
const childOptional = node.getCommand() !== undefined;
|
|
226
|
+
const open = childOptional
|
|
227
|
+
? CommandDispatcher.USAGE_OPTIONAL_OPEN
|
|
228
|
+
: CommandDispatcher.USAGE_REQUIRED_OPEN;
|
|
229
|
+
const close = childOptional
|
|
230
|
+
? CommandDispatcher.USAGE_OPTIONAL_CLOSE
|
|
231
|
+
: CommandDispatcher.USAGE_REQUIRED_CLOSE;
|
|
361
232
|
if (!deep) {
|
|
362
233
|
if (node.getRedirect() !== undefined) {
|
|
363
|
-
|
|
364
|
-
|
|
234
|
+
const redirect = node.getRedirect() === this.root
|
|
235
|
+
? "..."
|
|
236
|
+
: `-> ${node.getRedirect().getUsageText()}`;
|
|
237
|
+
return `${self} ${redirect}`;
|
|
365
238
|
}
|
|
366
239
|
else {
|
|
367
|
-
|
|
240
|
+
const children = node
|
|
241
|
+
.getChildren()
|
|
242
|
+
.filter((c) => c.canUse(source));
|
|
368
243
|
if (children.length === 1) {
|
|
369
|
-
|
|
244
|
+
const usage = String(this.getSmartUsage(children[0], source, childOptional, childOptional));
|
|
370
245
|
if (usage !== undefined) {
|
|
371
|
-
return
|
|
246
|
+
return `${self} ${usage}`;
|
|
372
247
|
}
|
|
373
248
|
}
|
|
374
249
|
else if (children.length > 1) {
|
|
375
|
-
|
|
376
|
-
for (
|
|
377
|
-
|
|
378
|
-
|
|
250
|
+
const childUsage = new Set();
|
|
251
|
+
for (const index in children) {
|
|
252
|
+
const child = children[index];
|
|
253
|
+
const usage = this.getSmartUsage(child, source, childOptional, true);
|
|
379
254
|
if (usage !== undefined) {
|
|
380
255
|
childUsage.add(usage);
|
|
381
256
|
}
|
|
382
257
|
}
|
|
383
258
|
if (childUsage.size === 1) {
|
|
384
|
-
|
|
385
|
-
return
|
|
259
|
+
const usage = childUsage.values().next().value;
|
|
260
|
+
return `${self} ${childOptional ? CommandDispatcher.USAGE_OPTIONAL_OPEN + usage + CommandDispatcher.USAGE_OPTIONAL_CLOSE : usage}`;
|
|
386
261
|
}
|
|
387
262
|
else if (childUsage.size > 1) {
|
|
388
|
-
|
|
389
|
-
for (
|
|
390
|
-
|
|
263
|
+
let builder = open;
|
|
264
|
+
for (let index = 0; index < children.length; index++) {
|
|
265
|
+
const child = children[index];
|
|
391
266
|
if (index > 0) {
|
|
392
267
|
builder += CommandDispatcher.USAGE_OR;
|
|
393
268
|
}
|
|
394
269
|
builder += child.getUsageText();
|
|
395
270
|
}
|
|
396
271
|
if (children.length > 0) {
|
|
397
|
-
builder +=
|
|
398
|
-
return
|
|
272
|
+
builder += close;
|
|
273
|
+
return `${self} ${builder}`;
|
|
399
274
|
}
|
|
400
275
|
}
|
|
401
276
|
}
|
|
402
277
|
}
|
|
403
278
|
}
|
|
404
279
|
}
|
|
405
|
-
}
|
|
406
|
-
|
|
280
|
+
}
|
|
281
|
+
getRoot() {
|
|
407
282
|
return this.root;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
CommandDispatcher.USAGE_OPTIONAL_CLOSE = "]";
|
|
411
|
-
CommandDispatcher.USAGE_REQUIRED_OPEN = "(";
|
|
412
|
-
CommandDispatcher.USAGE_REQUIRED_CLOSE = ")";
|
|
413
|
-
CommandDispatcher.USAGE_OR = "|";
|
|
414
|
-
return CommandDispatcher;
|
|
415
|
-
}());
|
|
283
|
+
}
|
|
284
|
+
}
|
|
416
285
|
exports.CommandDispatcher = CommandDispatcher;
|
|
286
|
+
CommandDispatcher.USAGE_OPTIONAL_OPEN = "[";
|
|
287
|
+
CommandDispatcher.USAGE_OPTIONAL_CLOSE = "]";
|
|
288
|
+
CommandDispatcher.USAGE_REQUIRED_OPEN = "(";
|
|
289
|
+
CommandDispatcher.USAGE_REQUIRED_CLOSE = ")";
|
|
290
|
+
CommandDispatcher.USAGE_OR = "|";
|
package/dist/ParseResults.d.ts
CHANGED