Repository navigation
Expand file tree
/
Copy pathTutorialStepParser.ts
More file actions
204 lines (179 loc) · 5.66 KB
/
Copy pathTutorialStepParser.ts
File metadata and controls
204 lines (179 loc) · 5.66 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import type {
Hats,
StringRecord,
TutorialContentProvider,
TutorialId,
TutorialStepFragment,
} from "@cursorless/lib-common";
import type { CustomSpokenFormGenerator } from "@cursorless/lib-engine";
import { parseScopeType } from "@cursorless/lib-engine";
import { getScopeTypeSpokenFormStrict } from "./getScopeTypeSpokenFormStrict";
import { specialTerms } from "./specialTerms";
import { ActionComponentParser } from "./stepComponentParsers/ActionComponentParser";
import { CursorlessCommandComponentParser } from "./stepComponentParsers/CursorlessCommandComponentParser";
import { GraphemeComponentParser } from "./stepComponentParsers/GraphemeComponentParser";
import { parseSpecialComponent } from "./stepComponentParsers/parseSpecialComponent";
import { parseVisualizeComponent } from "./stepComponentParsers/parseVisualizeComponent";
import type { StepComponent } from "./types/StepComponent";
import type { TutorialStep } from "./types/tutorial.types";
/**
* This is trying to catch occurrences of things like `{command:cloneStateInk.yml}`
* or `{action:chuck}` in the tutorial script.
*/
const componentRegex = /\{(\w+):([^}]+)\}/gu;
/**
* Parses a tutorial step from a raw string. Looks for components in the form
* `{action:chuck}`, `{command:cloneStateInk.yml}`, etc and parses them into
* {@link StepComponent}s, as well as fragments of plain text in between.
*/
export class TutorialStepParser {
/**
* A map of component type to a function that parses the component.
*/
private componentParsers: StringRecord<
(arg: string) => Promise<StepComponent>
>;
constructor(
contentProvider: TutorialContentProvider,
tutorialId: TutorialId,
customSpokenFormGenerator: CustomSpokenFormGenerator,
hats: Hats,
) {
this.parseTutorialStep = this.parseTutorialStep.bind(this);
const cursorlessCommandParser = new CursorlessCommandComponentParser(
contentProvider,
tutorialId,
customSpokenFormGenerator,
hats,
);
const actionParser = new ActionComponentParser(customSpokenFormGenerator);
const graphemeParser = new GraphemeComponentParser(
customSpokenFormGenerator,
);
this.componentParsers = {
command: (arg) => cursorlessCommandParser.parse(arg),
special: (arg) => Promise.resolve(parseSpecialComponent(arg)),
action: (arg) => actionParser.parse(arg),
grapheme: (arg) => graphemeParser.parse(arg),
term: (arg) =>
Promise.resolve({
content: {
type: "term",
value: specialTerms[arg as keyof typeof specialTerms],
},
}),
scopeType: (arg) =>
Promise.resolve({
content: {
type: "term",
value: getScopeTypeSpokenFormStrict(
customSpokenFormGenerator,
parseScopeType(arg),
),
},
}),
visualize: (arg) =>
Promise.resolve(
parseVisualizeComponent(customSpokenFormGenerator, arg),
),
};
}
/**
* Given the raw content of a tutorial step, parses it into a
* {@link TutorialStep} object.
*
* For example, given `Say {command:takeCap.yml}`, this would return:
*
* ```json
* {
* "content": [
* [
* {
* "type": "string",
* "value": "Say "
* },
* {
* "type": "command",
* "value": "take cap"
* }
* ]
* ],
* "initialState": {
* "documentContents": "...",
* "selections": [ ... ],
* "marks": { ... }
* },
* "languageId": "plaintext",
* "trigger": {
* "type": "command",
* "command": { ... }
* }
* }
* ```
*
* Note that the `initialState`, `languageId`, and `trigger` fiels are optional,
* and in this case come from the `takeCap.yml` fixture.
*
* @param rawContent The raw content of the tutorial step to parse
* @returns A {@link TutorialStep} object representing the parsed step
*/
async parseTutorialStep(rawContent: string): Promise<TutorialStep> {
const ret: TutorialStep = {
content: [],
};
for (const line of rawContent.split("\n")) {
const lineContent: TutorialStepFragment[] = [];
let currentIndex = 0;
componentRegex.lastIndex = 0;
for (const {
0: { length },
1: type,
2: arg,
index,
} of line.matchAll(componentRegex)) {
if (index > currentIndex) {
lineContent.push({
type: "string",
value: line.slice(currentIndex, index),
});
}
currentIndex = index + length;
const componentParser = this.componentParsers[type];
if (componentParser == null) {
throw new Error(`Unknown component type: ${type}`);
}
const result = await componentParser(arg);
const { content, ...rest } = result;
lineContent.push(content);
updateStrict<typeof rest>(ret, rest);
}
if (currentIndex < line.length) {
lineContent.push({
type: "string",
value: line.slice(currentIndex),
});
}
ret.content.push(lineContent);
}
return ret;
}
}
/**
* Update {@link target} with the non-null values from {@link source}, throwing
* an error if a key from {@link source} already exists in {@link target} with a
* non-null value.
*
* @param target The object to update
* @param source The object to update from
*/
function updateStrict<T>(target: T, source: T) {
for (const key in source) {
if (source[key] == null) {
continue;
}
if (target[key] != null) {
throw new Error(`Duplicate key: ${key}`);
}
target[key] = source[key];
}
}