@rohal12/spindle 0.59.33 → 0.60.0

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.
@@ -9443,8 +9443,8 @@ var WORDS_BEFORE_STRING = /* @__PURE__ */ new Set([
9443
9443
  * string may span lines, as quoted macro labels may: the parse reports
9444
9444
  * what in it is no JavaScript.
9445
9445
  */
9446
- function findCodeEnd(src, start, { goal = "expression", stop } = {}) {
9447
- const end = scanCodeEnd(src, start, goal, stop);
9446
+ function findCodeEnd(src, start, { goal = "expression", stop, onRunOut } = {}) {
9447
+ const end = scanCodeEnd(src, start, goal, stop, onRunOut);
9448
9448
  return end < 0 && !stop ? parsedCodeEnd(src, start, goal) : end;
9449
9449
  }
9450
9450
  /**
@@ -9470,7 +9470,12 @@ function parsedCodeEnd(src, start, goal) {
9470
9470
  return -1;
9471
9471
  }
9472
9472
  /** `findCodeEnd` by the tokens: see there. */
9473
- function scanCodeEnd(src, start, goal, stop) {
9473
+ function scanCodeEnd(src, start, goal, stop, onRunOut) {
9474
+ /** No end: the code runs to the end of `src`. */
9475
+ const ranOut = () => {
9476
+ onRunOut?.();
9477
+ return -1;
9478
+ };
9474
9479
  const stack = [];
9475
9480
  let prevEnd = -1;
9476
9481
  let prevWord = false;
@@ -9486,11 +9491,12 @@ function scanCodeEnd(src, start, goal, stop) {
9486
9491
  if (!/^Unterminated/.test(message) && !inTemplate) {
9487
9492
  resumeAt = Math.max(tok.pos, from) + 1;
9488
9493
  afterOperand = false;
9489
- if (resumeAt >= src.length) return -1;
9494
+ if (resumeAt >= src.length) return ranOut();
9490
9495
  break;
9491
9496
  }
9492
9497
  const k = stack.lastIndexOf("${");
9493
- if (!/^Unterminated template/.test(message) || k < 0 || templateSlip || stop) return -1;
9498
+ if (!/^Unterminated template/.test(message) || k < 0 || templateSlip || stop) return /^Unterminated/.test(message) && !/^Unterminated regular/.test(message) && !(/^Unterminated string/.test(message) && prevWord && tok.pos === prevEnd) ? ranOut() : -1;
9499
+ onRunOut?.();
9494
9500
  templateSlip = true;
9495
9501
  stack.length = k;
9496
9502
  resumeAt = tok.pos;
@@ -9499,7 +9505,7 @@ function scanCodeEnd(src, start, goal, stop) {
9499
9505
  }
9500
9506
  if (tok.restarted && stack.includes("${")) return -1;
9501
9507
  const t = tok.type;
9502
- if (t === types$1.eof) return -1;
9508
+ if (t === types$1.eof) return ranOut();
9503
9509
  if (t === types$1.string && prevWord && tok.start === prevEnd) return -1;
9504
9510
  prevWord = t === types$1.name && !WORDS_BEFORE_STRING.has(tok.value) || t === types$1.num;
9505
9511
  prevEnd = tok.end;
@@ -9771,80 +9777,124 @@ var NON_STRING_QUOTE_PREFIX = /[\p{L}\p{N}_\\]/u;
9771
9777
  function skipLiteral(input, i) {
9772
9778
  const quote = input[i];
9773
9779
  const template = quote === "`";
9780
+ let ranOut = false;
9774
9781
  for (let j = i + 1; j < input.length;) {
9775
9782
  const c = input[j];
9776
9783
  if (c === "\\") j += 2;
9777
- else if (c === quote) return j + 1;
9778
- else if (!template && c === "\n") return -1;
9784
+ else if (c === quote) return {
9785
+ end: j + 1,
9786
+ ranOut
9787
+ };
9788
+ else if (!template && c === "\n") return {
9789
+ end: -1,
9790
+ ranOut
9791
+ };
9779
9792
  else if (template && c === "$" && input[j + 1] === "{") {
9780
- const close = lenientClose(input, j + 2);
9781
- if (close === -1) return -1;
9782
- j = close + 1;
9793
+ const close = lenientRead(input, j + 2);
9794
+ ranOut ||= close.ranOut;
9795
+ if (close.end === -1) return {
9796
+ end: -1,
9797
+ ranOut
9798
+ };
9799
+ j = close.end + 1;
9783
9800
  } else j++;
9784
9801
  }
9785
- return -1;
9802
+ return {
9803
+ end: -1,
9804
+ ranOut: ranOut || template
9805
+ };
9786
9806
  }
9787
9807
  /**
9788
9808
  * The closing `}` of braces read leniently from `i` (just past a `{`): braces
9789
9809
  * count except inside string and template literals, and a quote that can't
9790
9810
  * start a string (an apostrophe, or one not closed on its line) is text, as
9791
- * is a backtick without a closing one.
9811
+ * is a backtick without a closing one. `onRunOut` is called when a line
9812
+ * appended to `input` could change the result (see `Lenient`).
9792
9813
  */
9793
- function lenientClose(input, i) {
9814
+ function lenientClose(input, i, onRunOut) {
9815
+ const { end, ranOut } = lenientRead(input, i);
9816
+ if (ranOut) onRunOut?.();
9817
+ return end;
9818
+ }
9819
+ function lenientRead(input, i) {
9794
9820
  if (memoInput !== input) {
9795
9821
  memoInput = input;
9796
9822
  memo.clear();
9797
9823
  }
9798
- let end = memo.get(i);
9799
- if (end === void 0) {
9800
- end = scanLenient(input, i);
9801
- memo.set(i, end);
9824
+ let read = memo.get(i);
9825
+ if (read === void 0) {
9826
+ read = scanLenient(input, i);
9827
+ memo.set(i, read);
9802
9828
  }
9803
- return end;
9829
+ return read;
9804
9830
  }
9805
9831
  var memoInput = "";
9806
9832
  var memo = /* @__PURE__ */ new Map();
9807
9833
  function scanLenient(input, i) {
9808
9834
  let depth = 0;
9835
+ let ranOut = false;
9809
9836
  while (i < input.length) {
9810
9837
  const c = input[i];
9811
9838
  if (c === "{") {
9812
9839
  depth++;
9813
9840
  i++;
9814
9841
  } else if (c === "}") {
9815
- if (depth === 0) return i;
9842
+ if (depth === 0) return {
9843
+ end: i,
9844
+ ranOut
9845
+ };
9816
9846
  depth--;
9817
9847
  i++;
9818
9848
  } else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) {
9819
9849
  const close = skipLiteral(input, i);
9820
- i = close === -1 ? i + 1 : close;
9850
+ ranOut ||= close.ranOut;
9851
+ i = close.end === -1 ? i + 1 : close.end;
9821
9852
  } else i++;
9822
9853
  }
9823
- return -1;
9854
+ return {
9855
+ end: -1,
9856
+ ranOut: true
9857
+ };
9858
+ }
9859
+ /**
9860
+ * The JavaScript reader Spindle uses: js-lexer, then a lenient scan. With
9861
+ * `onOpenEnd`, it also reports code whose end depends on what follows the
9862
+ * input (see `OnOpenCodeEnd`); the ends it finds are the same.
9863
+ */
9864
+ function jsCodeEnd(onOpenEnd) {
9865
+ return {
9866
+ closeBrace(input, codeStart, lenientStart) {
9867
+ let ranOut = false;
9868
+ const onRunOut = () => ranOut = true;
9869
+ const strict = findCodeEnd(input, codeStart, { onRunOut });
9870
+ const end = strict !== -1 ? strict : lenientClose(input, lenientStart, onRunOut);
9871
+ if (ranOut) onOpenEnd?.(input, codeStart);
9872
+ return end;
9873
+ },
9874
+ rawBodyEnd(input, bodyStart, name) {
9875
+ const closer = `\\{/${name}\\s*\\}`;
9876
+ const first = new RegExp(closer, "gi");
9877
+ first.lastIndex = bodyStart;
9878
+ const firstAt = first.exec(input)?.index ?? -1;
9879
+ if (firstAt === -1) return -1;
9880
+ const at = new RegExp(closer, "iy");
9881
+ let ranOut = false;
9882
+ const end = findCodeEnd(input, bodyStart, {
9883
+ goal: "statements",
9884
+ stop: (k) => {
9885
+ at.lastIndex = k;
9886
+ return at.test(input);
9887
+ },
9888
+ onRunOut: () => ranOut = true
9889
+ });
9890
+ if (end !== -1) return end;
9891
+ if (onOpenEnd && (ranOut || new RegExp(`\\{/${name}\\s*$`, "i").test(input))) onOpenEnd(input, bodyStart);
9892
+ return firstAt;
9893
+ }
9894
+ };
9824
9895
  }
9825
9896
  /** The JavaScript reader Spindle uses today: js-lexer, then a lenient scan. */
9826
- var defaultCodeEnd = {
9827
- closeBrace(input, codeStart, lenientStart) {
9828
- const end = findCodeEnd(input, codeStart);
9829
- return end !== -1 ? end : lenientClose(input, lenientStart);
9830
- },
9831
- rawBodyEnd(input, bodyStart, name) {
9832
- const closer = `\\{/${name}\\s*\\}`;
9833
- const first = new RegExp(closer, "gi");
9834
- first.lastIndex = bodyStart;
9835
- const firstAt = first.exec(input)?.index ?? -1;
9836
- if (firstAt === -1) return -1;
9837
- const at = new RegExp(closer, "iy");
9838
- const end = findCodeEnd(input, bodyStart, {
9839
- goal: "statements",
9840
- stop: (k) => {
9841
- at.lastIndex = k;
9842
- return at.test(input);
9843
- }
9844
- });
9845
- return end === -1 ? firstAt : end;
9846
- }
9847
- };
9897
+ var defaultCodeEnd = jsCodeEnd();
9848
9898
  //#endregion
9849
9899
  //#region src/markup/code-attributes.ts
9850
9900
  /**
@@ -9947,7 +9997,7 @@ function run$2(source, startRule, options) {
9947
9997
  * so it has no errors of its own.
9948
9998
  */
9949
9999
  function analyzeMarkup(source, options = {}) {
9950
- const { tokens, errors, damage } = scan(source, options);
10000
+ const { tokens, errors, damage } = scanMarkupTolerant(source, options);
9951
10001
  const hooks = {
9952
10002
  ...defaultHooks,
9953
10003
  ...options.hooks
@@ -10004,7 +10054,7 @@ function tokenizeMarkup(source, options = {}) {
10004
10054
  * markup the tokens are those of `tokenizeMarkup` and there are no errors.
10005
10055
  */
10006
10056
  function tokenizeMarkupTolerant(source, options = {}) {
10007
- const { tokens, errors } = scan(source, options);
10057
+ const { tokens, errors } = scanMarkupTolerant(source, options);
10008
10058
  return {
10009
10059
  tokens,
10010
10060
  errors
@@ -10012,10 +10062,10 @@ function tokenizeMarkupTolerant(source, options = {}) {
10012
10062
  }
10013
10063
  /**
10014
10064
  * The tokens of tolerant tokenizing, the errors, and where the damage of
10015
- * each error starts: the tag that is malformed, which an error inside it
10016
- * (an attribute value) is after.
10065
+ * each error starts (`damage[i]` for `errors[i]`): the tag that is
10066
+ * malformed, which an error inside it (an attribute value) is after.
10017
10067
  */
10018
- function scan(source, options) {
10068
+ function scanMarkupTolerant(source, options = {}) {
10019
10069
  const tokens = [];
10020
10070
  const errors = [];
10021
10071
  const damage = [];