squiffy-compiler 7.0.0-beta.5 → 7.0.0-beta.7

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.
Files changed (47) hide show
  1. package/README.md +10 -2
  2. package/dist/analyse.d.ts +6 -0
  3. package/dist/analyse.js +101 -11
  4. package/dist/compiler.d.ts +9 -2
  5. package/dist/compiler.js +5 -2
  6. package/dist/css-urls.d.ts +4 -0
  7. package/dist/css-urls.js +59 -0
  8. package/dist/emit.d.ts +13 -0
  9. package/dist/emit.js +45 -1
  10. package/dist/language-service.d.ts +1 -0
  11. package/dist/language-service.js +39 -13
  12. package/dist/node.d.ts +7 -1
  13. package/dist/node.js +16 -5
  14. package/dist/parse.d.ts +3 -0
  15. package/dist/parse.js +25 -2
  16. package/dist/project.d.ts +56 -0
  17. package/dist/project.js +148 -15
  18. package/dist/quest/aslx.d.ts +50 -0
  19. package/dist/quest/aslx.js +181 -0
  20. package/dist/quest/convert.d.ts +20 -0
  21. package/dist/quest/convert.js +504 -0
  22. package/dist/quest/script.d.ts +92 -0
  23. package/dist/quest/script.js +453 -0
  24. package/dist/quest/scripts.d.ts +24 -0
  25. package/dist/quest/scripts.js +505 -0
  26. package/dist/quest/text.d.ts +17 -0
  27. package/dist/quest/text.js +305 -0
  28. package/dist/setters.d.ts +4 -0
  29. package/dist/setters.js +10 -5
  30. package/dist/story-graph.d.ts +2 -1
  31. package/dist/story-graph.js +2 -0
  32. package/dist/syntax.d.ts +27 -2
  33. package/dist/text.d.ts +2 -0
  34. package/dist/text.js +24 -15
  35. package/dist/twine/audio.d.ts +21 -0
  36. package/dist/twine/audio.js +80 -0
  37. package/dist/twine/convert.d.ts +15 -1
  38. package/dist/twine/convert.js +217 -21
  39. package/dist/twine/expression.d.ts +26 -1
  40. package/dist/twine/expression.js +111 -6
  41. package/dist/twine/harlowe.d.ts +10 -0
  42. package/dist/twine/harlowe.js +417 -58
  43. package/dist/twine/sugarcube.d.ts +4 -0
  44. package/dist/twine/sugarcube.js +388 -86
  45. package/dist/v5.js +2 -1
  46. package/package.json +3 -2
  47. package/syntaxes/squiffy.tmLanguage.json +9 -4
@@ -0,0 +1,181 @@
1
+ // Reads a Quest 5 game file (.aslx), and the parts of a gamebook in it that the converter needs.
2
+ // A saved game has the whole of Quest's core library in it, as well as the author's pages, so this
3
+ // only reads the game, its pages, and the library's defaults for the game's settings.
4
+ export class QuestParseError extends Error {
5
+ }
6
+ // A small XML reader: ASLX is plain XML, without namespaces or a DTD
7
+ export function parseXml(source) {
8
+ let pos = 0;
9
+ const root = { name: "", attrs: {}, children: [], text: "" };
10
+ const stack = [root];
11
+ const top = () => stack[stack.length - 1];
12
+ while (pos < source.length) {
13
+ const lt = source.indexOf("<", pos);
14
+ if (lt == -1) {
15
+ top().text += decodeEntities(source.substring(pos));
16
+ break;
17
+ }
18
+ if (lt > pos)
19
+ top().text += decodeEntities(source.substring(pos, lt));
20
+ if (source.startsWith("<!--", lt)) {
21
+ pos = endOf(source, "-->", lt);
22
+ }
23
+ else if (source.startsWith("<![CDATA[", lt)) {
24
+ const end = source.indexOf("]]>", lt);
25
+ if (end == -1)
26
+ throw new QuestParseError("The file ends inside a CDATA section.");
27
+ top().text += source.substring(lt + 9, end);
28
+ pos = end + 3;
29
+ }
30
+ else if (source.startsWith("<?", lt)) {
31
+ pos = endOf(source, "?>", lt);
32
+ }
33
+ else if (source.startsWith("<!", lt)) {
34
+ pos = endOf(source, ">", lt);
35
+ }
36
+ else if (source.startsWith("</", lt)) {
37
+ const end = source.indexOf(">", lt);
38
+ if (end == -1)
39
+ throw new QuestParseError("The file ends inside a tag.");
40
+ const name = source.substring(lt + 2, end).trim();
41
+ // Close up to the matching element, so a stray closing tag doesn't lose the rest
42
+ const index = stack.map(e => e.name).lastIndexOf(name);
43
+ if (index > 0)
44
+ stack.length = index;
45
+ pos = end + 1;
46
+ }
47
+ else {
48
+ const tag = /^<([A-Za-z_][\w.:-]*)((?:\s+[\w.:-]+\s*=\s*(?:"[^"]*"|'[^']*'))*)\s*(\/?)>/.exec(source.substring(lt, lt + 4096));
49
+ if (!tag) {
50
+ // Not a tag: a < in text
51
+ top().text += "<";
52
+ pos = lt + 1;
53
+ continue;
54
+ }
55
+ const attrs = {};
56
+ for (const a of tag[2].matchAll(/([\w.:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g)) {
57
+ attrs[a[1]] = decodeEntities(a[2] ?? a[3]);
58
+ }
59
+ const element = { name: tag[1], attrs, children: [], text: "" };
60
+ top().children.push(element);
61
+ if (tag[3] != "/")
62
+ stack.push(element);
63
+ pos = lt + tag[0].length;
64
+ }
65
+ }
66
+ return root;
67
+ }
68
+ function endOf(source, terminator, from) {
69
+ const end = source.indexOf(terminator, from);
70
+ if (end == -1)
71
+ throw new QuestParseError("The file ends too soon.");
72
+ return end + terminator.length;
73
+ }
74
+ const namedEntities = { lt: "<", gt: ">", amp: "&", quot: "\"", apos: "'" };
75
+ function decodeEntities(text) {
76
+ return text.replace(/&(#x[0-9a-fA-F]+|#\d+|\w+);/g, (match, entity) => {
77
+ if (entity.startsWith("#x"))
78
+ return String.fromCodePoint(parseInt(entity.substring(2), 16));
79
+ if (entity.startsWith("#"))
80
+ return String.fromCodePoint(parseInt(entity.substring(1), 10));
81
+ return namedEntities[entity] ?? match;
82
+ });
83
+ }
84
+ // Reads an object's attribute from its element: <name type="...">value</name>, or
85
+ // <attr name="name" type="...">. An element with no type and nothing in it is true.
86
+ export function readValue(element) {
87
+ const type = element.attrs.type;
88
+ const text = element.text;
89
+ switch (type) {
90
+ case undefined:
91
+ case "":
92
+ if (element.children.length == 0 && text.trim() == "")
93
+ return { type: "boolean", value: true };
94
+ return { type: "string", value: text };
95
+ case "string":
96
+ return { type: "string", value: text };
97
+ case "boolean":
98
+ return { type: "boolean", value: text.trim().toLowerCase() != "false" };
99
+ case "int":
100
+ case "double":
101
+ return { type: "number", value: Number(text.trim()) || 0 };
102
+ case "script":
103
+ return { type: "script", value: text };
104
+ case "stringdictionary":
105
+ return {
106
+ type: "stringdictionary",
107
+ value: element.children.filter(c => c.name == "item").map(item => [
108
+ item.children.find(c => c.name == "key")?.text ?? item.attrs.key ?? "",
109
+ item.children.find(c => c.name == "value")?.text ?? "",
110
+ ]),
111
+ };
112
+ case "simplestringlist":
113
+ return { type: "stringlist", value: text.split(/[;\n]/).map(s => s.trim()).filter(s => s) };
114
+ case "stringlist":
115
+ case "list":
116
+ return { type: "stringlist", value: element.children.filter(c => c.name == "value").map(c => c.text) };
117
+ default:
118
+ return { type: "other", typeName: type, value: text };
119
+ }
120
+ }
121
+ function readObject(element) {
122
+ const attributes = new Map();
123
+ const inherits = [];
124
+ const children = [];
125
+ for (const child of element.children) {
126
+ if (child.name == "inherit") {
127
+ if (child.attrs.name)
128
+ inherits.push(child.attrs.name);
129
+ }
130
+ else if (child.name == "object") {
131
+ children.push(readObject(child));
132
+ }
133
+ else if (child.name == "attr") {
134
+ if (child.attrs.name)
135
+ attributes.set(child.attrs.name, readValue(child));
136
+ }
137
+ else {
138
+ attributes.set(child.name, readValue(child));
139
+ }
140
+ }
141
+ return { name: element.attrs.name ?? "", inherits, attributes, children };
142
+ }
143
+ export function parseQuestGame(source) {
144
+ const root = parseXml(source.replace(/^\uFEFF/, ""));
145
+ const asl = root.children.find(e => e.name == "asl");
146
+ if (!asl)
147
+ throw new QuestParseError("This isn't a Quest game: there's no <asl> element in it.");
148
+ const gameElement = asl.children.find(e => e.name == "game");
149
+ if (!gameElement)
150
+ throw new QuestParseError("This isn't a Quest game: there's no <game> element in it.");
151
+ const game = readObject(gameElement);
152
+ const gameDefaults = new Map();
153
+ const defaultGame = asl.children.find(e => e.name == "type" && e.attrs.name == "defaultgame");
154
+ if (defaultGame) {
155
+ for (const [name, value] of readObject(defaultGame).attributes)
156
+ gameDefaults.set(name, value);
157
+ }
158
+ const style = game.attributes.get("_editorstyle") ?? gameDefaults.get("_editorstyle");
159
+ const gamebook = (style?.type == "string" && style.value.trim() == "gamebook")
160
+ || asl.children.some(e => e.name == "include" && /^GamebookCore\.aslx$/i.test(e.attrs.ref ?? ""));
161
+ const pages = [];
162
+ let start;
163
+ const isPlayer = (o) => o.name == "player" || o.inherits.includes("defaultplayer");
164
+ const collect = (objects) => {
165
+ for (const o of objects) {
166
+ if (isPlayer(o))
167
+ continue;
168
+ pages.push(o);
169
+ if (o.children.some(isPlayer))
170
+ start ?? (start = o.name);
171
+ collect(o.children);
172
+ }
173
+ };
174
+ collect(asl.children.filter(e => e.name == "object").map(readObject));
175
+ const javascript = asl.children.filter(e => e.name == "javascript" && e.attrs.src).map(e => e.attrs.src);
176
+ return { aslVersion: asl.attrs.version ?? "", gamebook, game, gameDefaults, pages, start, javascript };
177
+ }
178
+ // Whether some text is a Quest game file
179
+ export function isQuestSource(source) {
180
+ return /<asl\s+version\s*=/.test(source.substring(0, 2000));
181
+ }
@@ -0,0 +1,20 @@
1
+ export interface QuestConversionProblem {
2
+ page?: string;
3
+ text: string;
4
+ reason: string;
5
+ }
6
+ export interface QuestConversionResult {
7
+ title: string;
8
+ format: string;
9
+ files: Record<string, string>;
10
+ main: string;
11
+ pages: number;
12
+ assets: string[];
13
+ problems: QuestConversionProblem[];
14
+ }
15
+ export interface QuestConversionOptions {
16
+ name?: string;
17
+ files?: string[];
18
+ }
19
+ export declare function convertQuestGamebook(source: string, options?: QuestConversionOptions): QuestConversionResult;
20
+ export declare function formatQuestProblems(result: QuestConversionResult, sourceName?: string): string;