@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,148 +1,174 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
type CommandNode,
|
|
3
|
+
type CommandDispatcher,
|
|
4
|
+
type Command,
|
|
5
|
+
CommandContext,
|
|
6
|
+
StringRange,
|
|
7
|
+
ParsedCommandNode,
|
|
8
|
+
type ParsedArgument,
|
|
9
|
+
type RedirectModifier,
|
|
10
|
+
SuggestionContext,
|
|
11
11
|
} from "..";
|
|
12
12
|
|
|
13
13
|
export class CommandContextBuilder<S> {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
14
|
+
private source: S;
|
|
15
|
+
private arguments: Map<string, ParsedArgument<any>>;
|
|
16
|
+
private rootNode: CommandNode<S>;
|
|
17
|
+
private dispatcher: CommandDispatcher<S>;
|
|
18
|
+
private command: Command<S>;
|
|
19
|
+
private child: CommandContextBuilder<S>;
|
|
20
|
+
private range: StringRange;
|
|
21
|
+
private nodes: ParsedCommandNode<S>[];
|
|
22
|
+
private modifier: RedirectModifier<S>;
|
|
23
|
+
private forks: boolean;
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
dispatcher: CommandDispatcher<S>,
|
|
27
|
+
source: S,
|
|
28
|
+
rootNode: CommandNode<S>,
|
|
29
|
+
start: number,
|
|
30
|
+
) {
|
|
31
|
+
this.dispatcher = dispatcher;
|
|
32
|
+
this.source = source;
|
|
33
|
+
this.rootNode = rootNode;
|
|
34
|
+
this.range = StringRange.at(start);
|
|
35
|
+
this.nodes = [];
|
|
36
|
+
this.arguments = new Map();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
withSource(source: S): CommandContextBuilder<S> {
|
|
40
|
+
this.source = source;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getSource(): S {
|
|
45
|
+
return this.source;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
getRootNode(): CommandNode<S> {
|
|
49
|
+
return this.rootNode;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
withArgument(
|
|
53
|
+
name: string,
|
|
54
|
+
argument: ParsedArgument<any>,
|
|
55
|
+
): CommandContextBuilder<S> {
|
|
56
|
+
this.arguments.set(name, argument);
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
getArguments(): Map<string, ParsedArgument<any>> {
|
|
61
|
+
return this.arguments;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
withChild(child: CommandContextBuilder<S>): CommandContextBuilder<S> {
|
|
65
|
+
this.child = child;
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
getChild(): CommandContextBuilder<S> {
|
|
70
|
+
return this.child;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
getLastChild(): CommandContextBuilder<S> {
|
|
74
|
+
let result: CommandContextBuilder<S> = this;
|
|
75
|
+
while (result.getChild() != null) {
|
|
76
|
+
result = result.getChild();
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
withCommand(command: Command<S>): CommandContextBuilder<S> {
|
|
82
|
+
this.command = command;
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
getCommand(): Command<S> {
|
|
87
|
+
return this.command;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
withNode(node: CommandNode<S>, range: StringRange): CommandContextBuilder<S> {
|
|
91
|
+
this.nodes.push(new ParsedCommandNode<S>(node, range));
|
|
92
|
+
this.range = StringRange.encompassing(this.range, range);
|
|
93
|
+
this.modifier = node.getRedirectModifier();
|
|
94
|
+
this.forks = node.isFork();
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
getNodes(): ParsedCommandNode<S>[] {
|
|
99
|
+
return this.nodes;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
copy(): CommandContextBuilder<S> {
|
|
103
|
+
const copy = new CommandContextBuilder<S>(
|
|
104
|
+
this.dispatcher,
|
|
105
|
+
this.source,
|
|
106
|
+
this.rootNode,
|
|
107
|
+
this.range.getStart(),
|
|
108
|
+
);
|
|
109
|
+
copy.command = this.command;
|
|
110
|
+
copy.child = this.child;
|
|
111
|
+
copy.range = this.range;
|
|
112
|
+
copy.nodes.push(...this.nodes);
|
|
113
|
+
this.arguments.forEach((v, k) => {
|
|
114
|
+
copy.arguments.set(k, v);
|
|
115
|
+
});
|
|
116
|
+
return copy;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
build(input: string): CommandContext<S> {
|
|
120
|
+
const child = this.child == null ? null : this.child.build(input);
|
|
121
|
+
return new CommandContext(
|
|
122
|
+
this.source,
|
|
123
|
+
input,
|
|
124
|
+
this.arguments,
|
|
125
|
+
this.command,
|
|
126
|
+
this.rootNode,
|
|
127
|
+
this.nodes,
|
|
128
|
+
this.range,
|
|
129
|
+
child,
|
|
130
|
+
this.modifier,
|
|
131
|
+
this.forks,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
getDispatcher(): CommandDispatcher<S> {
|
|
136
|
+
return this.dispatcher;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
getRange(): StringRange {
|
|
140
|
+
return this.range;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
findSuggestionContext(cursor: number): SuggestionContext<S> {
|
|
144
|
+
if (this.range.getStart() <= cursor) {
|
|
145
|
+
if (this.range.getEnd() < cursor) {
|
|
146
|
+
if (this.child != null) {
|
|
147
|
+
return this.child.findSuggestionContext(cursor);
|
|
148
|
+
} else if (this.nodes.length > 0) {
|
|
149
|
+
const last = this.nodes[this.nodes.length - 1];
|
|
150
|
+
return new SuggestionContext(
|
|
151
|
+
last.getNode(),
|
|
152
|
+
last.getRange().getEnd() + 1,
|
|
153
|
+
);
|
|
154
|
+
} else {
|
|
155
|
+
return new SuggestionContext(this.rootNode, this.range.getStart());
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
let prev = this.rootNode;
|
|
159
|
+
for (const node of this.nodes) {
|
|
160
|
+
const nodeRange = node.getRange();
|
|
161
|
+
if (nodeRange.getStart() <= cursor && cursor <= nodeRange.getEnd()) {
|
|
162
|
+
return new SuggestionContext(prev, nodeRange.getStart());
|
|
163
|
+
}
|
|
164
|
+
prev = node.getNode();
|
|
165
|
+
}
|
|
166
|
+
if (prev === null) {
|
|
167
|
+
throw new Error("Can't find node before cursor");
|
|
168
|
+
}
|
|
169
|
+
return new SuggestionContext(prev, this.range.getStart());
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
throw new Error("Can't find node before cursor");
|
|
173
|
+
}
|
|
148
174
|
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { StringRange } from "..";
|
|
2
2
|
|
|
3
3
|
export class ParsedArgument<T> {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
constructor(start: number, end: number, result: T) {
|
|
8
|
-
this.range = new StringRange(start, end);
|
|
9
|
-
this.result = result;
|
|
10
|
-
}
|
|
4
|
+
private range: StringRange;
|
|
5
|
+
private result: T;
|
|
11
6
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
constructor(start: number, end: number, result: T) {
|
|
8
|
+
this.range = new StringRange(start, end);
|
|
9
|
+
this.result = result;
|
|
10
|
+
}
|
|
15
11
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
getRange(): StringRange {
|
|
13
|
+
return this.range;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getResult(): T {
|
|
17
|
+
return this.result;
|
|
18
|
+
}
|
|
19
19
|
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { CommandNode, StringRange } from "..";
|
|
1
|
+
import type { CommandNode, StringRange } from "..";
|
|
2
2
|
|
|
3
3
|
export class ParsedCommandNode<S> {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
constructor(node: CommandNode<S>, range: StringRange) {
|
|
8
|
-
this.node = node;
|
|
9
|
-
this.range = range;
|
|
10
|
-
}
|
|
4
|
+
private node: CommandNode<S>;
|
|
5
|
+
private range: StringRange;
|
|
11
6
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
constructor(node: CommandNode<S>, range: StringRange) {
|
|
8
|
+
this.node = node;
|
|
9
|
+
this.range = range;
|
|
10
|
+
}
|
|
15
11
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
getNode(): CommandNode<S> {
|
|
13
|
+
return this.node;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getRange(): StringRange {
|
|
17
|
+
return this.range;
|
|
18
|
+
}
|
|
19
19
|
}
|
|
@@ -1,35 +1,35 @@
|
|
|
1
1
|
export class StringRange {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
private start: number;
|
|
3
|
+
private end: number;
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
constructor(start: number, end: number) {
|
|
6
|
+
this.start = start;
|
|
7
|
+
this.end = end;
|
|
8
|
+
}
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
static at(pos: number): StringRange {
|
|
11
|
+
return new StringRange(pos, pos);
|
|
12
|
+
}
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
static encompassing(a: StringRange, b: StringRange): StringRange {
|
|
15
|
+
const start = Math.min(a.getStart(), b.getStart());
|
|
16
|
+
const end = Math.max(a.getEnd(), b.getEnd());
|
|
17
|
+
return new StringRange(start, end);
|
|
18
|
+
}
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
getStart(): number {
|
|
21
|
+
return this.start;
|
|
22
|
+
}
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
getEnd(): number {
|
|
25
|
+
return this.end;
|
|
26
|
+
}
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
isEmpty(): boolean {
|
|
29
|
+
return this.start === this.end;
|
|
30
|
+
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
getLength(): number {
|
|
33
|
+
return this.end - this.start;
|
|
34
|
+
}
|
|
35
35
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { CommandNode } from "..";
|
|
1
|
+
import type { CommandNode } from "..";
|
|
2
2
|
|
|
3
3
|
export class SuggestionContext<S> {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
parent: CommandNode<S>;
|
|
5
|
+
startPos: number;
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
constructor(parent: CommandNode<S>, startPos: number) {
|
|
8
|
+
this.parent = parent;
|
|
9
|
+
this.startPos = startPos;
|
|
10
|
+
}
|
|
11
11
|
}
|
|
@@ -1,20 +1,27 @@
|
|
|
1
|
-
import { CommandSyntaxError, StringReader } from "..";
|
|
1
|
+
import { CommandSyntaxError, type StringReader } from "..";
|
|
2
2
|
|
|
3
3
|
type CommandErrorFunction = (...args: any[]) => string;
|
|
4
4
|
|
|
5
5
|
export class CommandErrorType {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
private func: CommandErrorFunction;
|
|
7
|
+
constructor(func: CommandErrorFunction) {
|
|
8
|
+
this.func = func;
|
|
9
|
+
}
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
create(...args: unknown[]): CommandSyntaxError {
|
|
12
|
+
const message = this.func(...args);
|
|
13
|
+
return new CommandSyntaxError(message);
|
|
14
|
+
}
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
createWithContext(
|
|
17
|
+
reader: StringReader,
|
|
18
|
+
...args: unknown[]
|
|
19
|
+
): CommandSyntaxError {
|
|
20
|
+
const message = this.func(...args);
|
|
21
|
+
return new CommandSyntaxError(
|
|
22
|
+
message,
|
|
23
|
+
reader.getString(),
|
|
24
|
+
reader.getCursor(),
|
|
25
|
+
);
|
|
26
|
+
}
|
|
20
27
|
}
|
|
@@ -3,46 +3,87 @@ import { CommandErrorType } from "..";
|
|
|
3
3
|
const CONTEXT_AMOUNT = 10;
|
|
4
4
|
|
|
5
5
|
export class CommandSyntaxError extends Error {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
private input: string;
|
|
7
|
+
private cursor: number;
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
constructor(message: string, input?: string, cursor?: number) {
|
|
10
|
+
super(message);
|
|
11
|
+
Object.setPrototypeOf(this, CommandSyntaxError.prototype);
|
|
12
|
+
this.input = input;
|
|
13
|
+
this.cursor = cursor;
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
if (input && cursor >= 0) {
|
|
16
|
+
this.message += ` at position ${cursor}: `;
|
|
17
|
+
const cursor2 = Math.min(this.input.length, this.cursor);
|
|
18
|
+
this.message += cursor > CONTEXT_AMOUNT ? "..." : "";
|
|
19
|
+
this.message += this.input.substring(
|
|
20
|
+
Math.max(0, cursor2 - CONTEXT_AMOUNT),
|
|
21
|
+
cursor2,
|
|
22
|
+
);
|
|
23
|
+
this.message += "<--[HERE]";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
static DOUBLE_TOO_SMALL = new CommandErrorType(
|
|
28
|
+
(found, min) => `Double must not be less than ${min}, found ${found}`,
|
|
29
|
+
);
|
|
30
|
+
static DOUBLE_TOO_BIG = new CommandErrorType(
|
|
31
|
+
(found, max) => `Double must not be more than ${max}, found ${found}`,
|
|
32
|
+
);
|
|
33
|
+
static FLOAT_TOO_SMALL = new CommandErrorType(
|
|
34
|
+
(found, min) => `Float must not be less than ${min}, found ${found}`,
|
|
35
|
+
);
|
|
36
|
+
static FLOAT_TOO_BIG = new CommandErrorType(
|
|
37
|
+
(found, max) => `Float must not be more than ${max}, found ${found}`,
|
|
38
|
+
);
|
|
39
|
+
static INTEGER_TOO_SMALL = new CommandErrorType(
|
|
40
|
+
(found, min) => `Integer must not be less than ${min}, found ${found}`,
|
|
41
|
+
);
|
|
42
|
+
static INTEGER_TOO_BIG = new CommandErrorType(
|
|
43
|
+
(found, max) => `Integer must not be more than ${max}, found ${found}`,
|
|
44
|
+
);
|
|
45
|
+
static LONG_TOO_SMALL = new CommandErrorType(
|
|
46
|
+
(found, min) => `Long must not be less than ${min}, found ${found}`,
|
|
47
|
+
);
|
|
48
|
+
static LONG_TOO_BIG = new CommandErrorType(
|
|
49
|
+
(found, max) => `Long must not be more than ${max}, found ${found}`,
|
|
50
|
+
);
|
|
51
|
+
static LITERAL_INCORRECT = new CommandErrorType(
|
|
52
|
+
(expected) => `Expected literal ${expected}`,
|
|
53
|
+
);
|
|
33
54
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
55
|
+
static READER_EXPECTED_START_OF_QUOTE = new CommandErrorType(
|
|
56
|
+
() => `Expected quote to start a string`,
|
|
57
|
+
);
|
|
58
|
+
static READER_EXPECTED_END_OF_QUOTE = new CommandErrorType(
|
|
59
|
+
() => `Unclosed quoted string`,
|
|
60
|
+
);
|
|
61
|
+
static READER_INVALID_ESCAPE = new CommandErrorType(
|
|
62
|
+
(character) => `Invalid escape sequence '${character}' in quoted string`,
|
|
63
|
+
);
|
|
64
|
+
static READER_INVALID_BOOL = new CommandErrorType(
|
|
65
|
+
(value) => `Invalid bool, expected true or false but found '${value}'`,
|
|
66
|
+
);
|
|
67
|
+
static READER_EXPECTED_BOOL = new CommandErrorType(() => `Expected bool`);
|
|
68
|
+
static READER_INVALID_INT = new CommandErrorType(
|
|
69
|
+
(value) => `Invalid integer '${value}'`,
|
|
70
|
+
);
|
|
71
|
+
static READER_EXPECTED_INT = new CommandErrorType(() => `Expected integer`);
|
|
72
|
+
static READER_INVALID_FLOAT = new CommandErrorType(
|
|
73
|
+
(value) => `Invalid float '${value}'`,
|
|
74
|
+
);
|
|
75
|
+
static READER_EXPECTED_FLOAT = new CommandErrorType(() => `Expected float`);
|
|
43
76
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
77
|
+
static DISPATCHER_UNKNOWN_COMMAND = new CommandErrorType(
|
|
78
|
+
() => `Unknown Command`,
|
|
79
|
+
);
|
|
80
|
+
static DISPATCHER_UNKNOWN_ARGUMENT = new CommandErrorType(
|
|
81
|
+
() => `Incorrect argument for command`,
|
|
82
|
+
);
|
|
83
|
+
static DISPATCHER_EXPECTED_ARGUMENT_SEPARATOR = new CommandErrorType(
|
|
84
|
+
() => `Expected whitespace to end one argument, but found trailing data`,
|
|
85
|
+
);
|
|
86
|
+
static DISPATCHER_PARSE_ERROR = new CommandErrorType(
|
|
87
|
+
(message) => `Could not parse command: ${message}`,
|
|
88
|
+
);
|
|
48
89
|
}
|