@rohal12/spindle 0.64.14 → 0.64.15

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.
@@ -13617,41 +13617,6 @@ function lastEnd(body) {
13617
13617
  */
13618
13618
  var NON_STRING_QUOTE_PREFIX = /[\p{L}\p{N}_\\]/u;
13619
13619
  /**
13620
- * Skip the literal opening at `i`: a '…' or "…" string, which must close on
13621
- * its line, or a `…` template, whose `${…}` parts are read as braces.
13622
- * Returns the index just past its closing quote or backtick, or -1.
13623
- */
13624
- function skipLiteral(input, i) {
13625
- const quote = input[i];
13626
- const template = quote === "`";
13627
- let ranOut = false;
13628
- for (let j = i + 1; j < input.length;) {
13629
- const c = input[j];
13630
- if (c === "\\") j += 2;
13631
- else if (c === quote) return {
13632
- end: j + 1,
13633
- ranOut
13634
- };
13635
- else if (!template && c === "\n") return {
13636
- end: -1,
13637
- ranOut
13638
- };
13639
- else if (template && c === "$" && input[j + 1] === "{") {
13640
- const close = lenientRead(input, j + 2);
13641
- ranOut ||= close.ranOut;
13642
- if (close.end === -1) return {
13643
- end: -1,
13644
- ranOut
13645
- };
13646
- j = close.end + 1;
13647
- } else j++;
13648
- }
13649
- return {
13650
- end: -1,
13651
- ranOut: ranOut || template
13652
- };
13653
- }
13654
- /**
13655
13620
  * The closing `}` of braces read leniently from `i` (just past a `{`): braces
13656
13621
  * count except inside string and template literals, and a quote that can't
13657
13622
  * start a string (an apostrophe, or one not closed on its line) is text, as
@@ -13677,31 +13642,71 @@ function lenientRead(input, i) {
13677
13642
  }
13678
13643
  var memoInput = "";
13679
13644
  var memo = /* @__PURE__ */ new Map();
13680
- function scanLenient(input, i) {
13681
- let depth = 0;
13682
- let ranOut = false;
13683
- while (i < input.length) {
13684
- const c = input[i];
13685
- if (c === "{") {
13686
- depth++;
13687
- i++;
13688
- } else if (c === "}") {
13689
- if (depth === 0) return {
13690
- end: i,
13691
- ranOut
13692
- };
13693
- depth--;
13694
- i++;
13695
- } else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) {
13696
- const close = skipLiteral(input, i);
13697
- ranOut ||= close.ranOut;
13698
- i = close.end === -1 ? i + 1 : close.end;
13699
- } else i++;
13700
- }
13701
- return {
13645
+ function scanLenient(input, start) {
13646
+ const codeFrame = (start) => ({
13647
+ kind: "code",
13648
+ start,
13649
+ i: start,
13650
+ depth: 0,
13651
+ ranOut: false
13652
+ });
13653
+ const frames = [codeFrame(start)];
13654
+ let result = {
13702
13655
  end: -1,
13703
13656
  ranOut: true
13704
13657
  };
13658
+ const finish = (end, ranOut) => {
13659
+ const frame = frames.pop();
13660
+ result = {
13661
+ end,
13662
+ ranOut
13663
+ };
13664
+ if (frame.kind === "code") memo.set(frame.start, result);
13665
+ const parent = frames[frames.length - 1];
13666
+ if (!parent) return;
13667
+ parent.ranOut ||= ranOut;
13668
+ if (parent.kind === "code") parent.i = end === -1 ? parent.i + 1 : end;
13669
+ else if (end === -1) parent.i = input.length;
13670
+ else parent.i = end + 1;
13671
+ };
13672
+ while (frames.length) {
13673
+ const frame = frames[frames.length - 1];
13674
+ const { i, ranOut } = frame;
13675
+ if (i >= input.length) {
13676
+ finish(-1, ranOut || frame.kind === "code" || frame.quote === "`");
13677
+ continue;
13678
+ }
13679
+ const c = input[i];
13680
+ if (frame.kind === "literal") {
13681
+ if (c === "\\") frame.i += 2;
13682
+ else if (c === frame.quote) finish(i + 1, ranOut);
13683
+ else if (frame.quote !== "`" && c === "\n") finish(-1, ranOut);
13684
+ else if (frame.quote === "`" && c === "$" && input[i + 1] === "{") {
13685
+ const cached = memo.get(i + 2);
13686
+ if (cached) {
13687
+ frame.ranOut ||= cached.ranOut;
13688
+ frame.i = cached.end === -1 ? input.length : cached.end + 1;
13689
+ } else frames.push(codeFrame(i + 2));
13690
+ } else frame.i++;
13691
+ } else if (c === "{") {
13692
+ frame.depth++;
13693
+ frame.i++;
13694
+ } else if (c === "}") {
13695
+ if (frame.depth === 0) finish(i, ranOut);
13696
+ else {
13697
+ frame.depth--;
13698
+ frame.i++;
13699
+ }
13700
+ } else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) frames.push({
13701
+ kind: "literal",
13702
+ start: i,
13703
+ i: i + 1,
13704
+ quote: c,
13705
+ ranOut: false
13706
+ });
13707
+ else frame.i++;
13708
+ }
13709
+ return result;
13705
13710
  }
13706
13711
  /**
13707
13712
  * The JavaScript reader Spindle uses: js-lexer, then a lenient scan. With
@@ -24755,6 +24760,7 @@ function useDetachedBody() {
24755
24760
  mounted.current.splice(at, 1);
24756
24761
  render(null, container);
24757
24762
  } : null;
24763
+ mounted.current.push(container);
24758
24764
  render(/* @__PURE__ */ jsx(FrozenStateContext.Provider, {
24759
24765
  value: frozenAlways,
24760
24766
  children: /* @__PURE__ */ jsx(LocalsUpdateContext.Provider, {
@@ -24800,7 +24806,6 @@ function useDetachedBody() {
24800
24806
  })
24801
24807
  })
24802
24808
  }), container);
24803
- mounted.current.push(container);
24804
24809
  };
24805
24810
  return (children) => runWithCommittedMutations(() => run(children));
24806
24811
  }
@@ -10183,41 +10183,6 @@ function completionIdentifier(source, cursor, goal = "expression") {
10183
10183
  */
10184
10184
  var NON_STRING_QUOTE_PREFIX = /[\p{L}\p{N}_\\]/u;
10185
10185
  /**
10186
- * Skip the literal opening at `i`: a '…' or "…" string, which must close on
10187
- * its line, or a `…` template, whose `${…}` parts are read as braces.
10188
- * Returns the index just past its closing quote or backtick, or -1.
10189
- */
10190
- function skipLiteral(input, i) {
10191
- const quote = input[i];
10192
- const template = quote === "`";
10193
- let ranOut = false;
10194
- for (let j = i + 1; j < input.length;) {
10195
- const c = input[j];
10196
- if (c === "\\") j += 2;
10197
- else if (c === quote) return {
10198
- end: j + 1,
10199
- ranOut
10200
- };
10201
- else if (!template && c === "\n") return {
10202
- end: -1,
10203
- ranOut
10204
- };
10205
- else if (template && c === "$" && input[j + 1] === "{") {
10206
- const close = lenientRead(input, j + 2);
10207
- ranOut || (ranOut = close.ranOut);
10208
- if (close.end === -1) return {
10209
- end: -1,
10210
- ranOut
10211
- };
10212
- j = close.end + 1;
10213
- } else j++;
10214
- }
10215
- return {
10216
- end: -1,
10217
- ranOut: ranOut || template
10218
- };
10219
- }
10220
- /**
10221
10186
  * The closing `}` of braces read leniently from `i` (just past a `{`): braces
10222
10187
  * count except inside string and template literals, and a quote that can't
10223
10188
  * start a string (an apostrophe, or one not closed on its line) is text, as
@@ -10243,31 +10208,71 @@ function lenientRead(input, i) {
10243
10208
  }
10244
10209
  var memoInput = "";
10245
10210
  var memo = /* @__PURE__ */ new Map();
10246
- function scanLenient(input, i) {
10247
- let depth = 0;
10248
- let ranOut = false;
10249
- while (i < input.length) {
10250
- const c = input[i];
10251
- if (c === "{") {
10252
- depth++;
10253
- i++;
10254
- } else if (c === "}") {
10255
- if (depth === 0) return {
10256
- end: i,
10257
- ranOut
10258
- };
10259
- depth--;
10260
- i++;
10261
- } else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) {
10262
- const close = skipLiteral(input, i);
10263
- ranOut || (ranOut = close.ranOut);
10264
- i = close.end === -1 ? i + 1 : close.end;
10265
- } else i++;
10266
- }
10267
- return {
10211
+ function scanLenient(input, start) {
10212
+ const codeFrame = (start) => ({
10213
+ kind: "code",
10214
+ start,
10215
+ i: start,
10216
+ depth: 0,
10217
+ ranOut: false
10218
+ });
10219
+ const frames = [codeFrame(start)];
10220
+ let result = {
10268
10221
  end: -1,
10269
10222
  ranOut: true
10270
10223
  };
10224
+ const finish = (end, ranOut) => {
10225
+ const frame = frames.pop();
10226
+ result = {
10227
+ end,
10228
+ ranOut
10229
+ };
10230
+ if (frame.kind === "code") memo.set(frame.start, result);
10231
+ const parent = frames[frames.length - 1];
10232
+ if (!parent) return;
10233
+ parent.ranOut || (parent.ranOut = ranOut);
10234
+ if (parent.kind === "code") parent.i = end === -1 ? parent.i + 1 : end;
10235
+ else if (end === -1) parent.i = input.length;
10236
+ else parent.i = end + 1;
10237
+ };
10238
+ while (frames.length) {
10239
+ const frame = frames[frames.length - 1];
10240
+ const { i, ranOut } = frame;
10241
+ if (i >= input.length) {
10242
+ finish(-1, ranOut || frame.kind === "code" || frame.quote === "`");
10243
+ continue;
10244
+ }
10245
+ const c = input[i];
10246
+ if (frame.kind === "literal") {
10247
+ if (c === "\\") frame.i += 2;
10248
+ else if (c === frame.quote) finish(i + 1, ranOut);
10249
+ else if (frame.quote !== "`" && c === "\n") finish(-1, ranOut);
10250
+ else if (frame.quote === "`" && c === "$" && input[i + 1] === "{") {
10251
+ const cached = memo.get(i + 2);
10252
+ if (cached) {
10253
+ frame.ranOut || (frame.ranOut = cached.ranOut);
10254
+ frame.i = cached.end === -1 ? input.length : cached.end + 1;
10255
+ } else frames.push(codeFrame(i + 2));
10256
+ } else frame.i++;
10257
+ } else if (c === "{") {
10258
+ frame.depth++;
10259
+ frame.i++;
10260
+ } else if (c === "}") {
10261
+ if (frame.depth === 0) finish(i, ranOut);
10262
+ else {
10263
+ frame.depth--;
10264
+ frame.i++;
10265
+ }
10266
+ } else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) frames.push({
10267
+ kind: "literal",
10268
+ start: i,
10269
+ i: i + 1,
10270
+ quote: c,
10271
+ ranOut: false
10272
+ });
10273
+ else frame.i++;
10274
+ }
10275
+ return result;
10271
10276
  }
10272
10277
  /**
10273
10278
  * The JavaScript reader Spindle uses: js-lexer, then a lenient scan. With
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.64.14",
3
+ "version": "0.64.15",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -149,6 +149,9 @@ export function useDetachedBody(): (children: ASTNode[]) => void {
149
149
  render(null, container);
150
150
  }
151
151
  : null;
152
+ // Replacement can happen synchronously inside render (for example {do}).
153
+ // Register first so that cancellation also sees the body doing it.
154
+ mounted.current.push(container);
152
155
  render(
153
156
  <FrozenStateContext.Provider value={frozenAlways}>
154
157
  <LocalsUpdateContext.Provider value={updater}>
@@ -185,7 +188,6 @@ export function useDetachedBody(): (children: ASTNode[]) => void {
185
188
  </FrozenStateContext.Provider>,
186
189
  container,
187
190
  );
188
- mounted.current.push(container);
189
191
  };
190
192
  return (children) => runWithCommittedMutations(() => run(children));
191
193
  }
@@ -35,30 +35,6 @@ interface Lenient {
35
35
  ranOut: boolean;
36
36
  }
37
37
 
38
- /**
39
- * Skip the literal opening at `i`: a '…' or "…" string, which must close on
40
- * its line, or a `…` template, whose `${…}` parts are read as braces.
41
- * Returns the index just past its closing quote or backtick, or -1.
42
- */
43
- function skipLiteral(input: string, i: number): Lenient {
44
- const quote = input[i];
45
- const template = quote === '`';
46
- let ranOut = false;
47
- for (let j = i + 1; j < input.length;) {
48
- const c = input[j];
49
- if (c === '\\') j += 2;
50
- else if (c === quote) return { end: j + 1, ranOut };
51
- else if (!template && c === '\n') return { end: -1, ranOut };
52
- else if (template && c === '$' && input[j + 1] === '{') {
53
- const close = lenientRead(input, j + 2);
54
- ranOut ||= close.ranOut;
55
- if (close.end === -1) return { end: -1, ranOut };
56
- j = close.end + 1;
57
- } else j++;
58
- }
59
- return { end: -1, ranOut: ranOut || template };
60
- }
61
-
62
38
  /**
63
39
  * The closing `}` of braces read leniently from `i` (just past a `{`): braces
64
40
  * count except inside string and template literals, and a quote that can't
@@ -94,31 +70,83 @@ function lenientRead(input: string, i: number): Lenient {
94
70
  let memoInput = '';
95
71
  const memo = new Map<number, Lenient>();
96
72
 
97
- function scanLenient(input: string, i: number): Lenient {
98
- let depth = 0;
99
- let ranOut = false;
100
- while (i < input.length) {
73
+ /** A suspended code or literal scan; nested templates use heap frames. */
74
+ type ScanFrame = {
75
+ start: number;
76
+ i: number;
77
+ ranOut: boolean;
78
+ } & ({ kind: 'code'; depth: number } | { kind: 'literal'; quote: string });
79
+
80
+ function scanLenient(input: string, start: number): Lenient {
81
+ const codeFrame = (start: number): ScanFrame => ({
82
+ kind: 'code',
83
+ start,
84
+ i: start,
85
+ depth: 0,
86
+ ranOut: false,
87
+ });
88
+ const frames: ScanFrame[] = [codeFrame(start)];
89
+ let result: Lenient = { end: -1, ranOut: true };
90
+ const finish = (end: number, ranOut: boolean) => {
91
+ const frame = frames.pop()!;
92
+ result = { end, ranOut };
93
+ if (frame.kind === 'code') memo.set(frame.start, result);
94
+ const parent = frames[frames.length - 1];
95
+ if (!parent) return;
96
+ parent.ranOut ||= ranOut;
97
+ if (parent.kind === 'code') {
98
+ // An unclosed quote/backtick is plain text in the lenient reader.
99
+ parent.i = end === -1 ? parent.i + 1 : end;
100
+ } else if (end === -1) {
101
+ // The interpolation did not close, so neither can its template.
102
+ parent.i = input.length;
103
+ } else {
104
+ parent.i = end + 1;
105
+ }
106
+ };
107
+ while (frames.length) {
108
+ const frame = frames[frames.length - 1]!;
109
+ const { i, ranOut } = frame;
110
+ if (i >= input.length) {
111
+ finish(-1, ranOut || frame.kind === 'code' || frame.quote === '`');
112
+ continue;
113
+ }
101
114
  const c = input[i]!;
102
- if (c === '{') {
103
- depth++;
104
- i++;
115
+ if (frame.kind === 'literal') {
116
+ if (c === '\\') frame.i += 2;
117
+ else if (c === frame.quote) finish(i + 1, ranOut);
118
+ else if (frame.quote !== '`' && c === '\n') finish(-1, ranOut);
119
+ else if (frame.quote === '`' && c === '$' && input[i + 1] === '{') {
120
+ const cached = memo.get(i + 2);
121
+ if (cached) {
122
+ frame.ranOut ||= cached.ranOut;
123
+ frame.i = cached.end === -1 ? input.length : cached.end + 1;
124
+ } else frames.push(codeFrame(i + 2));
125
+ } else frame.i++;
126
+ } else if (c === '{') {
127
+ frame.depth++;
128
+ frame.i++;
105
129
  } else if (c === '}') {
106
- if (depth === 0) return { end: i, ranOut };
107
- depth--;
108
- i++;
130
+ if (frame.depth === 0) finish(i, ranOut);
131
+ else {
132
+ frame.depth--;
133
+ frame.i++;
134
+ }
109
135
  } else if (
110
136
  c === '`' ||
111
137
  ((c === '"' || c === "'") &&
112
138
  !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]!)))
113
139
  ) {
114
- const close = skipLiteral(input, i);
115
- ranOut ||= close.ranOut;
116
- i = close.end === -1 ? i + 1 : close.end;
117
- } else {
118
- i++;
119
- }
140
+ frames.push({
141
+ kind: 'literal',
142
+ start: i,
143
+ i: i + 1,
144
+ quote: c,
145
+ ranOut: false,
146
+ });
147
+ } else frame.i++;
120
148
  }
121
- return { end: -1, ranOut: true };
149
+ return result;
122
150
  }
123
151
 
124
152
  /**
@@ -17,8 +17,10 @@ export interface ModeState {
17
17
  * from its start, ending in a line break; `''` when nothing is open.
18
18
  */
19
19
  tail: string;
20
- /** The spans left to emit on the current line, in window offsets. */
20
+ /** The current line’s spans, in window offsets; immutable during emission. */
21
21
  spans: StyledSpan[];
22
+ /** Index of the next span to emit. */
23
+ spanIndex: number;
22
24
  /** Where the current line starts in the window its spans came from. */
23
25
  lineStart: number;
24
26
  /**
@@ -89,6 +91,7 @@ function startLine(stream: StringStream, state: ModeState): void {
89
91
  end: Math.min(span.end, head.length),
90
92
  style: span.style,
91
93
  }));
94
+ state.spanIndex = 0;
92
95
  state.lineStart = lineStart;
93
96
  state.capped = capped;
94
97
  state.context = line.context;
@@ -111,17 +114,18 @@ export function mode(): Mode<ModeState> {
111
114
  context: '',
112
115
  tail: '',
113
116
  spans: [],
117
+ spanIndex: 0,
114
118
  lineStart: 0,
115
119
  capped: false,
116
120
  }),
117
- copyState: (state) => ({ ...state, spans: [...state.spans] }),
121
+ copyState: (state) => ({ ...state }),
118
122
  blankLine: (state) => {
119
123
  if (state.tail) state.tail += '\n';
120
124
  },
121
125
  token: (stream, state) => {
122
126
  try {
123
- if (stream.sol() && state.spans.length === 0) startLine(stream, state);
124
- const span = state.spans[0];
127
+ if (stream.sol()) startLine(stream, state);
128
+ const span = state.spans[state.spanIndex];
125
129
  if (!span) {
126
130
  stream.skipToEnd();
127
131
  return null;
@@ -131,12 +135,13 @@ export function mode(): Mode<ModeState> {
131
135
  stream.pos = span.start - state.lineStart;
132
136
  return null;
133
137
  }
134
- state.spans.shift();
138
+ state.spanIndex++;
135
139
  stream.pos = span.end - state.lineStart;
136
140
  return span.style;
137
141
  } catch {
138
142
  stream.skipToEnd();
139
143
  state.spans = [];
144
+ state.spanIndex = 0;
140
145
  state.tail = '';
141
146
  state.context = '';
142
147
  state.capped = false;