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

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 (46) hide show
  1. package/dist/analyse.d.ts +12 -1
  2. package/dist/analyse.js +156 -9
  3. package/dist/compiler.d.ts +5 -3
  4. package/dist/compiler.js +3 -2
  5. package/dist/css-urls.d.ts +4 -0
  6. package/dist/css-urls.js +59 -0
  7. package/dist/emit.d.ts +18 -1
  8. package/dist/emit.js +46 -0
  9. package/dist/infix-expressions.d.ts +19 -0
  10. package/dist/infix-expressions.js +713 -0
  11. package/dist/language-service.d.ts +1 -0
  12. package/dist/language-service.js +34 -15
  13. package/dist/node.d.ts +2 -1
  14. package/dist/node.js +1 -0
  15. package/dist/parse.d.ts +4 -0
  16. package/dist/parse.js +29 -0
  17. package/dist/project.d.ts +31 -0
  18. package/dist/project.js +98 -0
  19. package/dist/quest/convert.js +67 -11
  20. package/dist/quest/scripts.js +13 -9
  21. package/dist/quest/text.js +17 -8
  22. package/dist/section-labels.d.ts +4 -0
  23. package/dist/section-labels.js +64 -0
  24. package/dist/setters.d.ts +4 -0
  25. package/dist/setters.js +11 -9
  26. package/dist/story-graph.d.ts +2 -0
  27. package/dist/story-graph.js +61 -16
  28. package/dist/syntax.d.ts +24 -3
  29. package/dist/templates.d.ts +6 -0
  30. package/dist/templates.js +128 -0
  31. package/dist/text.d.ts +3 -1
  32. package/dist/text.js +45 -11
  33. package/dist/twine/audio.d.ts +21 -0
  34. package/dist/twine/audio.js +80 -0
  35. package/dist/twine/convert.d.ts +45 -3
  36. package/dist/twine/convert.js +305 -37
  37. package/dist/twine/expression.d.ts +21 -1
  38. package/dist/twine/expression.js +105 -8
  39. package/dist/twine/harlowe.d.ts +9 -0
  40. package/dist/twine/harlowe.js +461 -86
  41. package/dist/twine/settings.d.ts +9 -0
  42. package/dist/twine/settings.js +322 -0
  43. package/dist/twine/sugarcube.d.ts +3 -0
  44. package/dist/twine/sugarcube.js +438 -114
  45. package/package.json +9 -2
  46. package/syntaxes/squiffy.tmLanguage.json +8 -3
@@ -1,18 +1,26 @@
1
1
  // Expressions shared by the Harlowe and SugarCube converters: each parses its own format's
2
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).
3
+ // {{set}} take (see ../infix-expressions.ts).
4
+ // A delay for after=, start= or {{#every}} (#451): a number of milliseconds, or an expression,
5
+ // whose value can be milliseconds or a time like "3s", in parentheses if it's a calculation
6
+ export function delayValue(e) {
7
+ const text = print(e);
8
+ return e.k == "bin" || e.k == "cond" || e.k == "not" || e.k == "neg" ? `(${text})` : text;
9
+ }
4
10
  // Thrown when something can't be converted. The message says why, for the list of problems.
5
11
  export class Unconvertible extends Error {
6
12
  }
7
13
  const precedence = {
8
14
  "or": 1, "and": 2,
9
- "=": 4, "!=": 4, ">": 4, ">=": 4, "<": 4, "<=": 4,
15
+ "=": 4, "!=": 4, ">": 4, ">=": 4, "<": 4, "<=": 4, "contains": 4,
10
16
  "+": 5, "-": 5,
11
17
  "*": 6, "/": 6, "%": 6,
12
18
  };
13
19
  const notPrecedence = 3;
14
20
  const atomPrecedence = 8;
15
21
  function precedenceOf(e) {
22
+ if (e.k == "cond")
23
+ return 0;
16
24
  if (e.k == "bin")
17
25
  return precedence[e.op];
18
26
  if (e.k == "not")
@@ -43,12 +51,42 @@ export function quote(text, options = {}) {
43
51
  return `"${text}"`;
44
52
  }
45
53
  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
54
+ const word = /^[A-Za-z_]\w*$/;
55
+ // Whether an attribute name is a path to a value inside an object, written with dots, such as
56
+ // hero.hp (#442). The converters only make names of one word, so a dot can only be a path.
57
+ function isPath(name) {
58
+ const parts = name.split(".");
59
+ return parts.length > 1 && parts.every(part => word.test(part)) && !reservedWords.has(parts[0]);
60
+ }
61
+ // An attribute name in an expression: the name, or [name] if it isn't one word. A path to a
62
+ // value inside an object, hero.hp or inventory[i].name, is written as it is.
47
63
  export function attributeRef(name) {
48
- if (/^[A-Za-z_]\w*$/.test(name) && !reservedWords.has(name))
64
+ if ((word.test(name) && !reservedWords.has(name)) || isPath(name) || isDynamicTarget(name))
49
65
  return name;
50
66
  return `[${name}]`;
51
67
  }
68
+ // Whether a statement's target is a value inside an object found as the story runs, such as
69
+ // inventory[i].name, which only a setter can set: {{set}} and the like take a fixed name
70
+ export function isDynamicTarget(name) {
71
+ return /^[A-Za-z_]\w*[.[]/.test(name) && name.includes("[");
72
+ }
73
+ // A value inside an object or list: hero.hp for a fixed word inside an attribute, otherwise
74
+ // inventory[i] (#442)
75
+ export function member(e, key) {
76
+ if (e.k == "var" && key.k == "str" && word.test(key.v) && (word.test(e.name) || isPath(e.name)) && !reservedWords.has(e.name)) {
77
+ return { k: "var", name: `${e.name}.${key.v}` };
78
+ }
79
+ return { k: "member", e, key };
80
+ }
81
+ // The text of a setter's target, e.g. hero.hp or inventory[i].name, or null if it isn't
82
+ // something that can be set
83
+ export function targetName(e) {
84
+ if (e.k == "var")
85
+ return e.name;
86
+ if (e.k == "member" && targetName(e.e) !== null)
87
+ return print(e);
88
+ return null;
89
+ }
52
90
  export function print(e, options = {}) {
53
91
  switch (e.k) {
54
92
  case "num": return String(e.v);
@@ -62,9 +100,28 @@ export function print(e, options = {}) {
62
100
  // A link fills in its setters with only its own loop's item
63
101
  if (e.up)
64
102
  throw new Unconvertible("a link in a loop that sets a variable to an outer loop's item");
65
- return `'{{${loopRef(e)}}}'`;
103
+ // Filled in as text, so quoted, unless it's a number
104
+ return e.kind == "index" || e.numbers ? `{{${loopRef(e)}}}` : `'{{${loopRef(e)}}}'`;
66
105
  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("")})`;
106
+ // Always a space before "}", so nested objects never have the "}}" that would end a tag
107
+ case "object":
108
+ if (!e.entries.length)
109
+ return "{ }";
110
+ return `{ ${e.entries.map(([key, value]) => `${word.test(key) ? key : quote(key, options)}: ${print(value, options)}`).join(", ")} }`;
111
+ case "member": {
112
+ if (e.e.k == "loop" && options.inLink)
113
+ throw new Unconvertible("a link in a loop that sets a variable to a value inside the loop's item");
114
+ const base = printArgument(e.e, options);
115
+ if (e.key.k == "str" && word.test(e.key.v) && (e.e.k == "member" || (e.e.k == "loop" && e.e.kind == "item"))) {
116
+ return `${base}.${e.key.v}`;
117
+ }
118
+ return `${base}[${print(e.key, options)}]`;
119
+ }
120
+ case "call":
121
+ // .indexOf() that isn't checking whether something is there (see sugarcube.ts) (#447)
122
+ if (e.fn == "__indexof")
123
+ return print({ ...e, fn: "indexof" }, options);
124
+ return `(${e.fn}${e.args.map(a => " " + printArgument(a, options)).join("")})`;
68
125
  case "neg": {
69
126
  const inner = print(e.e, options);
70
127
  return precedenceOf(e.e) < atomPrecedence ? `-(${inner})` : `-${inner}`;
@@ -73,6 +130,12 @@ export function print(e, options = {}) {
73
130
  const inner = print(e.e, options);
74
131
  return `not ${precedenceOf(e.e) < notPrecedence ? `(${inner})` : inner}`;
75
132
  }
133
+ case "cond": {
134
+ // Right-associative, so only a condition in the test or the first value needs brackets
135
+ const test = print(e.test, options);
136
+ const then = print(e.then, options);
137
+ return `${e.test.k == "cond" ? `(${test})` : test} ? ${e.then.k == "cond" ? `(${then})` : then} : ${print(e.otherwise, options)}`;
138
+ }
76
139
  case "bin": {
77
140
  const p = precedence[e.op];
78
141
  const comparison = p == 4;
@@ -91,6 +154,11 @@ function printArgument(e, options) {
91
154
  const text = print(e, options);
92
155
  return precedenceOf(e) < atomPrecedence || e.k == "neg" ? `(${text})` : text;
93
156
  }
157
+ // Whether an expression is a call to a helper that makes a new list (#447), which {{#each}} can
158
+ // go through as it can an attribute
159
+ export function isListCall(e) {
160
+ return e.k == "call" && ["range", "sort", "shuffle", "slice"].includes(e.fn);
161
+ }
94
162
  // Whether an expression can only be true or false, for Harlowe's "$a is 1 or 2"
95
163
  export function isBoolean(e) {
96
164
  if (e.k == "bool" || e.k == "not")
@@ -101,6 +169,21 @@ export function isBoolean(e) {
101
169
  return e.fn == "seen" || e.fn == "contains" || e.fn == "at";
102
170
  return false;
103
171
  }
172
+ // The length of a list or text, which Handlebars gives as its length property: name.length (#446)
173
+ export function lengthOf(e) {
174
+ if (e.k == "str")
175
+ return { k: "num", v: e.v.length };
176
+ if (e.k == "array")
177
+ return { k: "num", v: e.items.length };
178
+ return member(e, { k: "str", v: "length" });
179
+ }
180
+ // Harlowe's (cond: test, a, test2, b, otherwise), as a ? b : c
181
+ export function conditional(values, what) {
182
+ if (values.length < 3 || values.length % 2 == 0)
183
+ throw new Unconvertible(`${what} without a condition and two values for each`);
184
+ const [test, then, ...rest] = values;
185
+ return { k: "cond", test, then, otherwise: rest.length == 1 ? rest[0] : conditional(rest, what) };
186
+ }
104
187
  // A random whole number from min to max, as Squiffy's {{random}} picks from an array
105
188
  export function randomInteger(min, max) {
106
189
  if (min.k != "num" || max.k != "num")
@@ -122,14 +205,28 @@ export function display(e) {
122
205
  if (e.k == "num" || e.k == "bool")
123
206
  return String(e.v);
124
207
  if (e.k == "var") {
125
- return /^[A-Za-z_]\w*$/.test(e.name) && !reservedWords.has(e.name) ? `{{${e.name}}}` : `{{get ${quote(e.name)}}}`;
208
+ return (word.test(e.name) && !reservedWords.has(e.name)) || isPath(e.name) ? `{{${e.name}}}` : `{{get ${quote(e.name)}}}`;
126
209
  }
127
210
  if (e.k == "loop")
128
211
  return `{{${loopRef(e)}}}`;
129
- if (e.k == "call")
212
+ // A helper call is written as it is, unless an argument needs an expression, such as
213
+ // (abs (-hp)), which only {{= }} rewrites
214
+ if (e.k == "call" && e.args.every(isPlain))
130
215
  return `{{${print(e).slice(1, -1)}}}`;
131
216
  return `{{= ${print(e)}}}`;
132
217
  }
218
+ // Whether a value is written the same in Handlebars as in an expression
219
+ function isPlain(e) {
220
+ if (e.k == "num" || e.k == "str" || e.k == "bool" || e.k == "null" || e.k == "loop")
221
+ return true;
222
+ if (e.k == "var")
223
+ return !isDynamicTarget(e.name);
224
+ if (e.k == "call")
225
+ return e.args.every(isPlain);
226
+ if (e.k == "array")
227
+ return e.items.every(isPlain);
228
+ return false;
229
+ }
133
230
  // A token stream for the expression parsers
134
231
  export class Tokens {
135
232
  constructor(items) {
@@ -1,4 +1,5 @@
1
1
  import type { Expr } from "./expression.js";
2
+ import type { StoryAudio } from "./audio.js";
2
3
  import type { ConvertedPassage, PassageContext } from "./convert.js";
3
4
  export declare function splitArgs(args: string): string[];
4
5
  export declare class HarloweExpressions {
@@ -15,4 +16,12 @@ export declare class HarloweExpressions {
15
16
  macro(name: string, raw: string, it?: Expr): Expr;
16
17
  visits(): Expr;
17
18
  }
19
+ export declare const harloweAudioPassages: Set<string>;
20
+ export declare function harloweAudio(story: PassageContext["story"]): StoryAudio;
21
+ export declare function harloweMacros(story: PassageContext["story"]): Map<string, {
22
+ section: string;
23
+ params: string[];
24
+ body: string;
25
+ defined: boolean;
26
+ }>;
18
27
  export declare function convertHarlowePassage(text: string, ctx: PassageContext): ConvertedPassage;