squiffy-compiler 6.0.0 → 7.0.0-beta.1
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/dist/analyse.d.ts +77 -0
- package/dist/analyse.js +520 -0
- package/dist/compiler.d.ts +12 -40
- package/dist/compiler.js +18 -680
- package/dist/emit.d.ts +43 -0
- package/dist/emit.js +114 -0
- package/dist/language-service.d.ts +77 -0
- package/dist/language-service.js +669 -0
- package/dist/parse.d.ts +17 -0
- package/dist/parse.js +258 -0
- package/dist/syntax.d.ts +146 -0
- package/dist/syntax.js +8 -0
- package/dist/text.d.ts +37 -0
- package/dist/text.js +288 -0
- package/package.json +16 -5
package/dist/text.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
// Converts the links in a section's or passage's text into Handlebars helpers, and records
|
|
2
|
+
// where each link and embed was in the original text.
|
|
3
|
+
//
|
|
4
|
+
// The conversion is a series of regex replacements, each running on the output of the one
|
|
5
|
+
// before, and the exact order matters (for example, [[a]](b) has to be converted before [a](b)
|
|
6
|
+
// would match inside it). So rather than parse links some other way, this runs exactly those
|
|
7
|
+
// replacements, and keeps a map from each position in the current string back to the original
|
|
8
|
+
// text, so that what each regex matched can be traced to where it was written.
|
|
9
|
+
class MappedString {
|
|
10
|
+
constructor(original) {
|
|
11
|
+
Object.defineProperty(this, "original", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: original
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "value", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: void 0
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "segments", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: void 0
|
|
28
|
+
});
|
|
29
|
+
this.value = original;
|
|
30
|
+
this.segments = original.length
|
|
31
|
+
? [{ start: 0, end: original.length, origStart: 0, origEnd: original.length, copied: true }]
|
|
32
|
+
: [];
|
|
33
|
+
}
|
|
34
|
+
// Like String.replace with a global regex, except the callback gets the match's offset.
|
|
35
|
+
replace(regex, replacer) {
|
|
36
|
+
const replacements = [];
|
|
37
|
+
this.value = this.value.replace(regex, (...args) => {
|
|
38
|
+
// The callback's arguments are the match, the groups, the offset and the whole string.
|
|
39
|
+
// None of these regexes have named groups, so there's no groups object after those.
|
|
40
|
+
const offset = args[args.length - 2];
|
|
41
|
+
const match = args.slice(0, args.length - 2);
|
|
42
|
+
const text = replacer(match, offset);
|
|
43
|
+
replacements.push({ start: offset, end: offset + match[0].length, text });
|
|
44
|
+
return text;
|
|
45
|
+
});
|
|
46
|
+
if (replacements.length) {
|
|
47
|
+
this.applyToSegments(replacements);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
applyToSegments(replacements) {
|
|
51
|
+
const segments = [];
|
|
52
|
+
let newPos = 0;
|
|
53
|
+
const copyRange = (from, to) => {
|
|
54
|
+
for (const s of this.segments) {
|
|
55
|
+
const start = Math.max(s.start, from);
|
|
56
|
+
const end = Math.min(s.end, to);
|
|
57
|
+
if (start >= end)
|
|
58
|
+
continue;
|
|
59
|
+
const length = end - start;
|
|
60
|
+
segments.push(s.copied
|
|
61
|
+
? { start: newPos, end: newPos + length, origStart: s.origStart + start - s.start, origEnd: s.origStart + end - s.start, copied: true }
|
|
62
|
+
: { start: newPos, end: newPos + length, origStart: s.origStart, origEnd: s.origEnd, copied: false });
|
|
63
|
+
newPos += length;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
let oldPos = 0;
|
|
67
|
+
for (const r of replacements) {
|
|
68
|
+
copyRange(oldPos, r.start);
|
|
69
|
+
const origStart = this.mapStart(r.start);
|
|
70
|
+
const origEnd = this.mapEnd(r.end);
|
|
71
|
+
if (r.text.length) {
|
|
72
|
+
segments.push({
|
|
73
|
+
start: newPos,
|
|
74
|
+
end: newPos + r.text.length,
|
|
75
|
+
origStart,
|
|
76
|
+
origEnd,
|
|
77
|
+
copied: r.text === this.original.substring(origStart, origEnd),
|
|
78
|
+
});
|
|
79
|
+
newPos += r.text.length;
|
|
80
|
+
}
|
|
81
|
+
oldPos = r.end;
|
|
82
|
+
}
|
|
83
|
+
copyRange(oldPos, Infinity);
|
|
84
|
+
this.segments = segments;
|
|
85
|
+
}
|
|
86
|
+
// Maps the start of a range in the current string to the original string.
|
|
87
|
+
mapStart(pos) {
|
|
88
|
+
const s = this.find(seg => seg.end > pos);
|
|
89
|
+
if (!s)
|
|
90
|
+
return this.original.length;
|
|
91
|
+
if (pos < s.start)
|
|
92
|
+
return s.origStart;
|
|
93
|
+
return s.copied ? s.origStart + pos - s.start : s.origStart;
|
|
94
|
+
}
|
|
95
|
+
// Maps the (exclusive) end of a range in the current string to the original string.
|
|
96
|
+
mapEnd(pos) {
|
|
97
|
+
const s = this.find(seg => seg.end >= pos);
|
|
98
|
+
if (!s)
|
|
99
|
+
return this.original.length;
|
|
100
|
+
if (pos <= s.start)
|
|
101
|
+
return s.origStart;
|
|
102
|
+
return s.copied ? s.origStart + pos - s.start : s.origEnd;
|
|
103
|
+
}
|
|
104
|
+
// Binary search for the first segment matching a predicate that's monotonic in segment order.
|
|
105
|
+
find(predicate) {
|
|
106
|
+
let low = 0;
|
|
107
|
+
let high = this.segments.length;
|
|
108
|
+
while (low < high) {
|
|
109
|
+
const mid = (low + high) >> 1;
|
|
110
|
+
if (predicate(this.segments[mid]))
|
|
111
|
+
high = mid;
|
|
112
|
+
else
|
|
113
|
+
low = mid + 1;
|
|
114
|
+
}
|
|
115
|
+
return this.segments[low];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function extractLinkFunctions(link) {
|
|
119
|
+
const fragments = link.split(",");
|
|
120
|
+
return {
|
|
121
|
+
target: fragments[0].trim(),
|
|
122
|
+
setters: fragments.slice(1).join(", ")
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function getAdditionalLinkParameters(link) {
|
|
126
|
+
const functions = extractLinkFunctions(link);
|
|
127
|
+
let additionalParameters = "";
|
|
128
|
+
if (functions.setters.length > 0) {
|
|
129
|
+
additionalParameters += ` set="${functions.setters}"`;
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
target: functions.target,
|
|
133
|
+
additionalParameters
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function escapeForHandlebarsString(s) {
|
|
137
|
+
return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
138
|
+
}
|
|
139
|
+
// getNextSectionName returns the section after this one, for [[text>]] links, or null if
|
|
140
|
+
// there isn't one.
|
|
141
|
+
export function transformText(text, getNextSectionName) {
|
|
142
|
+
const input = new MappedString(text);
|
|
143
|
+
const links = [];
|
|
144
|
+
const embeds = [];
|
|
145
|
+
const noNextSection = [];
|
|
146
|
+
// Records a link, given the offset of its match and of its target group in the current string.
|
|
147
|
+
const addLink = (link, matchStart, matchLength, targetStart, targetLength) => {
|
|
148
|
+
links.push({
|
|
149
|
+
...link,
|
|
150
|
+
start: input.mapStart(matchStart),
|
|
151
|
+
end: input.mapEnd(matchStart + matchLength),
|
|
152
|
+
targetStart: input.mapStart(targetStart),
|
|
153
|
+
targetEnd: input.mapEnd(targetStart + targetLength),
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
// Handlebars expressions can contain square brackets that aren't links, such as
|
|
157
|
+
// {{someArray.[1]}} or {{someHelper "[x]"}}, so swap each one for a placeholder while
|
|
158
|
+
// converting links. Text between block helpers like {{#if x}}...{{/if}} is still converted.
|
|
159
|
+
const mustaches = [];
|
|
160
|
+
input.replace(/\{\{\{[\s\S]*?\}\}\}|\{\{[\s\S]*?\}\}/g, ([match]) => {
|
|
161
|
+
mustaches.push(match);
|
|
162
|
+
return `${mustaches.length - 1}`;
|
|
163
|
+
});
|
|
164
|
+
const restore = (s) => s.replace(/(\d+)/g, (_match, i) => mustaches[Number(i)]);
|
|
165
|
+
// nextSectionLinkRegex matches [[text>]] - a link to the next section
|
|
166
|
+
const nextSectionLinkRegex = /\[\[([^\]]*?)>\]\]/g;
|
|
167
|
+
input.replace(nextSectionLinkRegex, ([match, rawText], offset) => {
|
|
168
|
+
const text = restore(rawText);
|
|
169
|
+
const nextSectionName = getNextSectionName();
|
|
170
|
+
if (!nextSectionName) {
|
|
171
|
+
noNextSection.push({ display: `[[${text}>]]`, start: input.mapStart(offset), end: input.mapEnd(offset + match.length) });
|
|
172
|
+
return `[[${text}]]`; // fallback
|
|
173
|
+
}
|
|
174
|
+
addLink({ linkType: "section", form: "next", text, target: "" }, offset, match.length, offset + 2 + rawText.length, 0);
|
|
175
|
+
return `{{section "${escapeForHandlebarsString(nextSectionName)}" text="${escapeForHandlebarsString(text)}"}}`;
|
|
176
|
+
});
|
|
177
|
+
// namedNextSectionLinkRegex matches [[text]](>) or [[text]](>, setter=value)
|
|
178
|
+
// - a named link where target starts with >
|
|
179
|
+
const namedNextSectionLinkRegex = /\[\[([^\]]*?)\]\]\(>(.*?)\)/g;
|
|
180
|
+
input.replace(namedNextSectionLinkRegex, ([match, rawText, rawRest], offset) => {
|
|
181
|
+
const text = restore(rawText);
|
|
182
|
+
const rest = restore(rawRest);
|
|
183
|
+
const nextSectionName = getNextSectionName();
|
|
184
|
+
if (!nextSectionName) {
|
|
185
|
+
noNextSection.push({ display: `[[${text}]](>)`, start: input.mapStart(offset), end: input.mapEnd(offset + match.length) });
|
|
186
|
+
return `[[${text}]]`; // fallback
|
|
187
|
+
}
|
|
188
|
+
addLink({ linkType: "section", form: "next", text, target: rest }, offset, match.length, offset + 2 + rawText.length + 4, rawRest.length);
|
|
189
|
+
// rest could be empty or ", setter=value"
|
|
190
|
+
const parsedName = getAdditionalLinkParameters(nextSectionName + rest);
|
|
191
|
+
return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
|
|
192
|
+
});
|
|
193
|
+
// namedSectionLinkRegex matches:
|
|
194
|
+
// open [[
|
|
195
|
+
// any text - the link text
|
|
196
|
+
// closing ]]
|
|
197
|
+
// open bracket
|
|
198
|
+
// any text - the name of the section
|
|
199
|
+
// closing bracket
|
|
200
|
+
const namedSectionLinkRegex = /\[\[([^\]]*?)\]\]\((.*?)\)/g;
|
|
201
|
+
input.replace(namedSectionLinkRegex, ([match, rawText, rawName], offset) => {
|
|
202
|
+
const text = restore(rawText);
|
|
203
|
+
const name = restore(rawName);
|
|
204
|
+
addLink({ linkType: "section", form: "named", text, target: name }, offset, match.length, offset + 2 + rawText.length + 3, rawName.length);
|
|
205
|
+
const parsedName = getAdditionalLinkParameters(name);
|
|
206
|
+
return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
|
|
207
|
+
});
|
|
208
|
+
// namedPassageLinkRegex matches:
|
|
209
|
+
// not preceded by ! (to avoid matching image links like )
|
|
210
|
+
// open [
|
|
211
|
+
// any text - the link text
|
|
212
|
+
// closing ]
|
|
213
|
+
// open bracket, but not http(s):// after it
|
|
214
|
+
// any text - the name of the passage
|
|
215
|
+
// closing bracket
|
|
216
|
+
const namedPassageLinkRegex = /(?<!!)\[([^\]]*?)\]\(((?!https?:\/\/).*?)\)/g;
|
|
217
|
+
input.replace(namedPassageLinkRegex, ([match, rawText, rawName], offset) => {
|
|
218
|
+
const text = restore(rawText);
|
|
219
|
+
const name = restore(rawName);
|
|
220
|
+
addLink({ linkType: "passage", form: "named", text, target: name }, offset, match.length, offset + 1 + rawText.length + 2, rawName.length);
|
|
221
|
+
const parsedName = getAdditionalLinkParameters(name);
|
|
222
|
+
return `{{passage "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
|
|
223
|
+
});
|
|
224
|
+
// unnamedSectionLinkRegex matches:
|
|
225
|
+
// open [[
|
|
226
|
+
// any text - the link text
|
|
227
|
+
// closing ]]
|
|
228
|
+
const unnamedSectionLinkRegex = /\[\[(.*?)\]\]/g;
|
|
229
|
+
input.replace(unnamedSectionLinkRegex, ([match, rawName], offset) => {
|
|
230
|
+
const name = restore(rawName);
|
|
231
|
+
addLink({ linkType: "section", form: "unnamed", target: name }, offset, match.length, offset + 2, rawName.length);
|
|
232
|
+
return `{{section "${escapeForHandlebarsString(name)}"}}`;
|
|
233
|
+
});
|
|
234
|
+
// unnamedPassageLinkRegex matches:
|
|
235
|
+
// not preceded by ! (to avoid matching image links like ![alt])
|
|
236
|
+
// open [
|
|
237
|
+
// any text - the link text
|
|
238
|
+
// closing ]
|
|
239
|
+
// no bracket after
|
|
240
|
+
const unnamedPassageLinkRegex = /(?<!!)\[(.*?)\]([^(]|$)/g;
|
|
241
|
+
input.replace(unnamedPassageLinkRegex, ([match, rawName, suffix], offset) => {
|
|
242
|
+
const name = restore(rawName);
|
|
243
|
+
addLink({ linkType: "passage", form: "unnamed", target: name }, offset, match.length - suffix.length, offset + 1, rawName.length);
|
|
244
|
+
return `{{passage "${escapeForHandlebarsString(name)}"}}${suffix}`;
|
|
245
|
+
});
|
|
246
|
+
input.replace(/(\d+)/g, ([, i]) => mustaches[Number(i)]);
|
|
247
|
+
// embedRegex matches {{embed "name"}} or {{embed 'name'}} with a literal name.
|
|
248
|
+
const embedRegex = /\{\{~?\s*embed\s+(["'])(.*?)\1/g;
|
|
249
|
+
let match;
|
|
250
|
+
while ((match = embedRegex.exec(input.value))) {
|
|
251
|
+
const start = match.index;
|
|
252
|
+
const end = start + match[0].length;
|
|
253
|
+
const nameEnd = end - 1;
|
|
254
|
+
const nameStart = nameEnd - match[2].length;
|
|
255
|
+
embeds.push({
|
|
256
|
+
name: match[2],
|
|
257
|
+
start: input.mapStart(start),
|
|
258
|
+
end: input.mapEnd(end),
|
|
259
|
+
nameStart: input.mapStart(nameStart),
|
|
260
|
+
nameEnd: input.mapEnd(nameEnd),
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
return { output: input.value, links, embeds, noNextSection };
|
|
264
|
+
}
|
|
265
|
+
// Returns a function that converts an offset into lines.join("\n") to a position in the source file.
|
|
266
|
+
export function positionMapper(lines, lineLengths) {
|
|
267
|
+
const starts = [];
|
|
268
|
+
let pos = 0;
|
|
269
|
+
for (const length of lineLengths) {
|
|
270
|
+
starts.push(pos);
|
|
271
|
+
pos += length + 1;
|
|
272
|
+
}
|
|
273
|
+
return (offset) => {
|
|
274
|
+
let low = 0;
|
|
275
|
+
let high = starts.length - 1;
|
|
276
|
+
while (low < high) {
|
|
277
|
+
const mid = (low + high + 1) >> 1;
|
|
278
|
+
if (starts[mid] <= offset)
|
|
279
|
+
low = mid;
|
|
280
|
+
else
|
|
281
|
+
high = mid - 1;
|
|
282
|
+
}
|
|
283
|
+
const source = lines[low];
|
|
284
|
+
if (!source)
|
|
285
|
+
return { line: 0, character: 0 };
|
|
286
|
+
return { line: source.line, character: Math.min(offset - starts[low], source.length) };
|
|
287
|
+
};
|
|
288
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "squiffy-compiler",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-beta.1",
|
|
4
4
|
"description": "A tool for creating multiple-choice interactive stories",
|
|
5
5
|
"author": "Alex Warren",
|
|
6
6
|
"contributors": [
|
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"preferGlobal": true,
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@types/node": "^26.
|
|
20
|
-
"glob": "^
|
|
19
|
+
"@types/node": "^26.6.6",
|
|
20
|
+
"glob": "^14.0.2",
|
|
21
21
|
"typescript": "^6.0.3",
|
|
22
|
-
"vitest": "^5.0.
|
|
22
|
+
"vitest": "^5.0.5"
|
|
23
23
|
},
|
|
24
24
|
"scripts": {
|
|
25
25
|
"test": "vitest --run",
|
|
@@ -28,9 +28,20 @@
|
|
|
28
28
|
},
|
|
29
29
|
"main": "dist/compiler.js",
|
|
30
30
|
"types": "dist/compiler.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/compiler.d.ts",
|
|
34
|
+
"default": "./dist/compiler.js"
|
|
35
|
+
},
|
|
36
|
+
"./language-service": {
|
|
37
|
+
"types": "./dist/language-service.d.ts",
|
|
38
|
+
"default": "./dist/language-service.js"
|
|
39
|
+
},
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
},
|
|
31
42
|
"files": [
|
|
32
43
|
"dist"
|
|
33
44
|
],
|
|
34
45
|
"type": "module",
|
|
35
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "558b8c3c0a4ce8d7a2e4eb0e9a439ecfc4c53a62"
|
|
36
47
|
}
|