This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
parser.ts
171 lines (160 loc) · 5.32 KB
/
parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { sentenceCase } from "sentence-case";
export interface SimpleToken {
indices: [number, number]; // [start, end], end is exclusive
value: string;
}
/**
* Parse a command string into an ordered list of tokens. Each token has indices
* to locate * where it is in the original string and a copy of its value.
* Quoted strings are treated as a single token.
*
* @param commandString User's input
*/
export function parseToSimpleTokens(commandString: string): SimpleToken[] {
const escaped = commandString.replace(
/"(?:[^"\\]|\\.)*"/g, // Escape quoted strings into "..."
(match) => `"${Array.from({ length: match.length - 1 }).join(".")}"`
);
const splits = escaped.split(" ");
const tokens: SimpleToken[] = [];
let index = 0;
for (const split of splits) {
if (!split) {
// Empty split in between double spaces
index++;
continue;
}
tokens.push({
indices: [index, index + split.length],
value: commandString.substring(index, index + split.length),
});
index += split.length + 1;
}
return tokens;
}
export interface SpecToken extends SimpleToken, Fig.BaseSuggestion {
type: "command" | "subcommand" | "option" | "argument";
}
export class InvalidTokenError extends Error {
constructor(public token: SimpleToken, public description: string) {
super(`${token.value}: ${description}`);
}
}
/**
* Parse simple tokens into detailed tokens with extra information from Fig's spec.
*
* @param spec Fig's spec
* @param simpleTokens Simple tokens from `parseToSimpleTokens`
*/
export function parseToSpecTokens(
spec: Fig.Subcommand,
simpleTokens: SimpleToken[]
): SpecToken[] {
const specTokens: SpecToken[] = [
// First token is always the command name
{
...simpleTokens[0],
type: "subcommand",
displayName: spec.displayName,
icon: spec.icon,
description: spec.description,
},
];
// Keep track of remaining arguments to accept for the subcommand, -1 means no more arguments
let subcommandArgsIndex = -1;
if (Array.isArray(spec.args) && spec.args.length > 0) subcommandArgsIndex = 0;
else if (spec.args) subcommandArgsIndex = 0;
for (let i = 1; i < simpleTokens.length; i++) {
const token = simpleTokens[i];
if (token.value.startsWith("-")) {
// Is an option
const nestedTokens: SimpleToken[] = [];
if (token.value.startsWith("--"))
nestedTokens.push(token); // Long-form option
else {
// Short-form options
const nestedValues = token.value.substring(1).split("");
nestedTokens.push(
...nestedValues.map((value, index) => ({
indices: [
token.indices[0] + index + 1,
token.indices[0] + index + 2,
] as [number, number],
value: `-${value}`,
}))
);
}
for (const token of nestedTokens) {
const option = spec.options?.find(({ name }) =>
Array.isArray(name)
? name.includes(token.value)
: token.value === name
);
if (!option) throw new InvalidTokenError(token, `option is unknown`);
const optionToken: SpecToken = {
...token,
type: "option",
displayName: option.displayName,
icon: option.icon,
description: option.description,
};
specTokens.push(optionToken);
if (!option.args) continue;
// Iterate through all arguments of the option and merge them into a single token
// with the option (because argument spec don't currently have good description, otherwise
// we may want to treat arguments as separate tokens)
const optionArgs = Array.isArray(option.args)
? option.args
: [option.args];
let j = 0;
for (; j < optionArgs.length && i + j + 1 < simpleTokens.length; j++) {
const argToken = simpleTokens[i + j + 1];
optionToken.value += ` ${argToken.value}`;
optionToken.indices[1] = argToken.indices[1];
}
i += j;
}
} else {
const subcommand = spec.subcommands?.find(({ name }) =>
Array.isArray(name) ? name.includes(token.value) : token.value === name
);
if (subcommand) {
// Is a subcommand, recursively parse subcommand
specTokens.push(
...parseToSpecTokens(subcommand, simpleTokens.slice(i))
);
i = simpleTokens.length;
} else {
// It's an argument
if (subcommandArgsIndex != -1 && spec.args) {
const arg = Array.isArray(spec.args)
? spec.args[subcommandArgsIndex]
: spec.args;
specTokens.push({
...token,
type: "argument",
description:
arg.description ||
(arg.name ? sentenceCase(arg.name) : undefined),
});
if (!arg.isVariadic) {
if (
Array.isArray(spec.args) &&
subcommandArgsIndex + 1 < spec.args.length
) {
subcommandArgsIndex++;
} else {
subcommandArgsIndex = -1; // Stop accepting subcommand's arguments
}
}
} else {
throw new InvalidTokenError(
token,
"is neither a valid subcommand, option, or argument"
);
}
}
}
}
return specTokens;
}