squiffy-compiler 7.0.0-beta.4 → 7.0.0-beta.6

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 (43) hide show
  1. package/README.md +10 -2
  2. package/dist/analyse.d.ts +3 -0
  3. package/dist/analyse.js +73 -12
  4. package/dist/compiler.d.ts +12 -2
  5. package/dist/compiler.js +6 -2
  6. package/dist/emit.d.ts +6 -0
  7. package/dist/emit.js +17 -1
  8. package/dist/language-service.js +36 -5
  9. package/dist/node.d.ts +8 -1
  10. package/dist/node.js +19 -5
  11. package/dist/parse.js +18 -2
  12. package/dist/project.d.ts +56 -0
  13. package/dist/project.js +159 -17
  14. package/dist/quest/aslx.d.ts +50 -0
  15. package/dist/quest/aslx.js +181 -0
  16. package/dist/quest/convert.d.ts +20 -0
  17. package/dist/quest/convert.js +475 -0
  18. package/dist/quest/script.d.ts +92 -0
  19. package/dist/quest/script.js +453 -0
  20. package/dist/quest/scripts.d.ts +24 -0
  21. package/dist/quest/scripts.js +501 -0
  22. package/dist/quest/text.d.ts +17 -0
  23. package/dist/quest/text.js +305 -0
  24. package/dist/setters.d.ts +6 -0
  25. package/dist/setters.js +43 -0
  26. package/dist/story-graph.d.ts +3 -1
  27. package/dist/story-graph.js +12 -3
  28. package/dist/syntax.d.ts +22 -2
  29. package/dist/text.d.ts +4 -0
  30. package/dist/text.js +28 -21
  31. package/dist/twine/convert.d.ts +82 -0
  32. package/dist/twine/convert.js +744 -0
  33. package/dist/twine/expression.d.ts +68 -0
  34. package/dist/twine/expression.js +215 -0
  35. package/dist/twine/harlowe.d.ts +18 -0
  36. package/dist/twine/harlowe.js +1912 -0
  37. package/dist/twine/story.d.ts +24 -0
  38. package/dist/twine/story.js +231 -0
  39. package/dist/twine/sugarcube.d.ts +34 -0
  40. package/dist/twine/sugarcube.js +1621 -0
  41. package/dist/v5.js +2 -1
  42. package/package.json +3 -2
  43. package/syntaxes/squiffy.tmLanguage.json +1 -1
@@ -0,0 +1,68 @@
1
+ export type BinaryOp = "or" | "and" | "=" | "!=" | ">" | ">=" | "<" | "<=" | "+" | "-" | "*" | "/" | "%";
2
+ export type Expr = {
3
+ k: "num";
4
+ v: number;
5
+ } | {
6
+ k: "str";
7
+ v: string;
8
+ } | {
9
+ k: "bool";
10
+ v: boolean;
11
+ } | {
12
+ k: "null";
13
+ } | {
14
+ k: "var";
15
+ name: string;
16
+ } | {
17
+ k: "bin";
18
+ op: BinaryOp;
19
+ l: Expr;
20
+ r: Expr;
21
+ } | {
22
+ k: "not";
23
+ e: Expr;
24
+ } | {
25
+ k: "neg";
26
+ e: Expr;
27
+ } | {
28
+ k: "call";
29
+ fn: string;
30
+ args: Expr[];
31
+ } | {
32
+ k: "array";
33
+ items: Expr[];
34
+ } | {
35
+ k: "loop";
36
+ kind: "item" | "index";
37
+ up: number;
38
+ array?: string;
39
+ };
40
+ export declare class Unconvertible extends Error {
41
+ }
42
+ export interface PrintOptions {
43
+ preferSingle?: boolean;
44
+ inLink?: boolean;
45
+ }
46
+ export declare function quote(text: string, options?: PrintOptions): string;
47
+ export declare function attributeRef(name: string): string;
48
+ export declare function print(e: Expr, options?: PrintOptions): string;
49
+ export declare function isBoolean(e: Expr): boolean;
50
+ export declare function randomInteger(min: Expr, max: Expr): Expr;
51
+ export declare function display(e: Expr): string;
52
+ export declare class Tokens<T extends {
53
+ type: string;
54
+ text: string;
55
+ }> {
56
+ readonly items: T[];
57
+ pos: number;
58
+ constructor(items: T[]);
59
+ peek(offset?: number): T | undefined;
60
+ next(): T;
61
+ is(text: string, offset?: number): boolean;
62
+ accept(text: string): boolean;
63
+ expect(text: string): void;
64
+ get done(): boolean;
65
+ }
66
+ export declare function skipBalanced(s: string, start: number, open: string, close: string, quotes?: string): number;
67
+ export declare function skipString(s: string, start: number): number;
68
+ export declare function unescapeString(literal: string): string;
@@ -0,0 +1,215 @@
1
+ // Expressions shared by the Harlowe and SugarCube converters: each parses its own format's
2
+ // expressions into this tree, which is written out as a Squiffy expression, as {{#if}}, @set and
3
+ // {{set}} take (see runtime/src/infixExpressions.ts).
4
+ // Thrown when something can't be converted. The message says why, for the list of problems.
5
+ export class Unconvertible extends Error {
6
+ }
7
+ const precedence = {
8
+ "or": 1, "and": 2,
9
+ "=": 4, "!=": 4, ">": 4, ">=": 4, "<": 4, "<=": 4,
10
+ "+": 5, "-": 5,
11
+ "*": 6, "/": 6, "%": 6,
12
+ };
13
+ const notPrecedence = 3;
14
+ const atomPrecedence = 8;
15
+ function precedenceOf(e) {
16
+ if (e.k == "bin")
17
+ return precedence[e.op];
18
+ if (e.k == "not")
19
+ return notPrecedence;
20
+ if (e.k == "neg")
21
+ return 7;
22
+ return atomPrecedence;
23
+ }
24
+ // How a loop's item or position is written in Handlebars, e.g. this, ../this or @index
25
+ function loopRef(e) {
26
+ const up = "../".repeat(e.up);
27
+ return e.kind == "item" ? up + "this" : "@" + up + "index";
28
+ }
29
+ // Text as a quoted string. Handlebars and Squiffy's expression tokenizer both take a backslash
30
+ // before the quote character, but Handlebars leaves other backslashes in, so those can't be
31
+ // written exactly.
32
+ export function quote(text, options = {}) {
33
+ if (text.includes("\\"))
34
+ throw new Unconvertible("text with a backslash in it");
35
+ // }} would end the {{...}} the expression is in
36
+ text = text.replace(/\}\}/g, "}&#125;").replace(/\n/g, " ");
37
+ const hasDouble = text.includes("\"");
38
+ const hasSingle = text.includes("'");
39
+ if (hasDouble && hasSingle)
40
+ return `"${text.replace(/"/g, "&quot;")}"`;
41
+ if (hasDouble || (options.preferSingle && !hasSingle))
42
+ return `'${text}'`;
43
+ return `"${text}"`;
44
+ }
45
+ const reservedWords = new Set(["and", "or", "not", "true", "false", "null", "undefined"]);
46
+ // An attribute name in an expression: the name, or [name] if it isn't one word
47
+ export function attributeRef(name) {
48
+ if (/^[A-Za-z_]\w*$/.test(name) && !reservedWords.has(name))
49
+ return name;
50
+ return `[${name}]`;
51
+ }
52
+ export function print(e, options = {}) {
53
+ switch (e.k) {
54
+ case "num": return String(e.v);
55
+ case "str": return quote(e.v, options);
56
+ case "bool": return String(e.v);
57
+ case "null": return "null";
58
+ case "var": return attributeRef(e.name);
59
+ case "loop":
60
+ if (!options.inLink)
61
+ return loopRef(e);
62
+ // A link fills in its setters with only its own loop's item
63
+ if (e.up)
64
+ throw new Unconvertible("a link in a loop that sets a variable to an outer loop's item");
65
+ return `'{{${loopRef(e)}}}'`;
66
+ case "array": return `(array${e.items.map(i => " " + printArgument(i, options)).join("")})`;
67
+ case "call": return `(${e.fn}${e.args.map(a => " " + printArgument(a, options)).join("")})`;
68
+ case "neg": {
69
+ const inner = print(e.e, options);
70
+ return precedenceOf(e.e) < atomPrecedence ? `-(${inner})` : `-${inner}`;
71
+ }
72
+ case "not": {
73
+ const inner = print(e.e, options);
74
+ return `not ${precedenceOf(e.e) < notPrecedence ? `(${inner})` : inner}`;
75
+ }
76
+ case "bin": {
77
+ const p = precedence[e.op];
78
+ const comparison = p == 4;
79
+ const left = print(e.l, options);
80
+ const right = print(e.r, options);
81
+ const lp = precedenceOf(e.l);
82
+ const rp = precedenceOf(e.r);
83
+ const l = lp < p || (comparison && lp == p) ? `(${left})` : left;
84
+ const r = rp <= p || e.r.k == "neg" ? `(${right})` : right;
85
+ return `${l} ${e.op} ${r}`;
86
+ }
87
+ }
88
+ }
89
+ // A helper's argument: anything but a single value or call needs brackets
90
+ function printArgument(e, options) {
91
+ const text = print(e, options);
92
+ return precedenceOf(e) < atomPrecedence || e.k == "neg" ? `(${text})` : text;
93
+ }
94
+ // Whether an expression can only be true or false, for Harlowe's "$a is 1 or 2"
95
+ export function isBoolean(e) {
96
+ if (e.k == "bool" || e.k == "not")
97
+ return true;
98
+ if (e.k == "bin")
99
+ return precedence[e.op] <= 4;
100
+ if (e.k == "call")
101
+ return e.fn == "seen" || e.fn == "contains" || e.fn == "at";
102
+ return false;
103
+ }
104
+ // A random whole number from min to max, as Squiffy's {{random}} picks from an array
105
+ export function randomInteger(min, max) {
106
+ if (min.k != "num" || max.k != "num")
107
+ throw new Unconvertible("a random number with a range that isn't fixed");
108
+ const low = Math.ceil(Math.min(min.v, max.v));
109
+ const high = Math.floor(Math.max(min.v, max.v));
110
+ if (high - low > 100)
111
+ throw new Unconvertible("a random number from a range of more than 100 numbers");
112
+ const items = [];
113
+ for (let n = low; n <= high; n++)
114
+ items.push({ k: "num", v: n });
115
+ return { k: "call", fn: "random", args: [{ k: "array", items }] };
116
+ }
117
+ // Expressions shown in text: {{name}} for an attribute, or {{= expression}}
118
+ export function display(e) {
119
+ // Fixed text is just text
120
+ if (e.k == "str")
121
+ return e.v.replace(/\[/g, "&#91;").replace(/\]/g, "&#93;").replace(/\{\{/g, "&#123;{").replace(/\}\}/g, "}&#125;");
122
+ if (e.k == "num" || e.k == "bool")
123
+ return String(e.v);
124
+ if (e.k == "var") {
125
+ return /^[A-Za-z_]\w*$/.test(e.name) && !reservedWords.has(e.name) ? `{{${e.name}}}` : `{{get ${quote(e.name)}}}`;
126
+ }
127
+ if (e.k == "loop")
128
+ return `{{${loopRef(e)}}}`;
129
+ if (e.k == "call")
130
+ return `{{${print(e).slice(1, -1)}}}`;
131
+ return `{{= ${print(e)}}}`;
132
+ }
133
+ // A token stream for the expression parsers
134
+ export class Tokens {
135
+ constructor(items) {
136
+ Object.defineProperty(this, "items", {
137
+ enumerable: true,
138
+ configurable: true,
139
+ writable: true,
140
+ value: items
141
+ });
142
+ Object.defineProperty(this, "pos", {
143
+ enumerable: true,
144
+ configurable: true,
145
+ writable: true,
146
+ value: 0
147
+ });
148
+ }
149
+ peek(offset = 0) {
150
+ return this.items[this.pos + offset];
151
+ }
152
+ next() {
153
+ const token = this.items[this.pos++];
154
+ if (!token)
155
+ throw new Unconvertible("an expression that ends too soon");
156
+ return token;
157
+ }
158
+ is(text, offset = 0) {
159
+ return this.peek(offset)?.text == text;
160
+ }
161
+ accept(text) {
162
+ if (this.is(text)) {
163
+ this.pos++;
164
+ return true;
165
+ }
166
+ return false;
167
+ }
168
+ expect(text) {
169
+ if (!this.accept(text))
170
+ throw new Unconvertible(`an expression Squiffy can't read (expected "${text}")`);
171
+ }
172
+ get done() {
173
+ return this.pos >= this.items.length;
174
+ }
175
+ }
176
+ // Finds the end of a bracketed or quoted run, starting at the opening character. Returns the
177
+ // index just after the closing character, or -1.
178
+ export function skipBalanced(s, start, open, close, quotes = "\"'`") {
179
+ let depth = 0;
180
+ for (let i = start; i < s.length; i++) {
181
+ const c = s[i];
182
+ // Harlowe's possessive: $player's hp
183
+ if (c == "'" && s[i + 1] == "s" && /\s/.test(s[i + 2] ?? "") && /[\w)]/.test(s[i - 1] ?? ""))
184
+ continue;
185
+ if (quotes.includes(c)) {
186
+ const end = skipString(s, i);
187
+ if (end == -1)
188
+ return -1;
189
+ i = end - 1;
190
+ }
191
+ else if (c == open) {
192
+ depth++;
193
+ }
194
+ else if (c == close) {
195
+ depth--;
196
+ if (depth == 0)
197
+ return i + 1;
198
+ }
199
+ }
200
+ return -1;
201
+ }
202
+ // The index after a quoted string starting at start, or -1 if it isn't closed
203
+ export function skipString(s, start) {
204
+ const q = s[start];
205
+ for (let i = start + 1; i < s.length; i++) {
206
+ if (s[i] == "\\")
207
+ i++;
208
+ else if (s[i] == q)
209
+ return i + 1;
210
+ }
211
+ return -1;
212
+ }
213
+ export function unescapeString(literal) {
214
+ return literal.slice(1, -1).replace(/\\(.)/g, (_m, c) => c == "n" ? "\n" : c == "t" ? "\t" : c);
215
+ }
@@ -0,0 +1,18 @@
1
+ import type { Expr } from "./expression.js";
2
+ import type { ConvertedPassage, PassageContext } from "./convert.js";
3
+ export declare function splitArgs(args: string): string[];
4
+ export declare class HarloweExpressions {
5
+ readonly ctx: PassageContext;
6
+ constructor(ctx: PassageContext);
7
+ attribute(name: string, temp: boolean): string;
8
+ variable(name: string, temp: boolean): Expr;
9
+ parse(source: string, it?: Expr): Expr;
10
+ passageName(source: string): {
11
+ name: string;
12
+ } | {
13
+ expr: Expr;
14
+ };
15
+ macro(name: string, raw: string, it?: Expr): Expr;
16
+ visits(): Expr;
17
+ }
18
+ export declare function convertHarlowePassage(text: string, ctx: PassageContext): ConvertedPassage;