links-notation 0.28.0 → 0.30.1

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.
package/dist/index.js CHANGED
@@ -66,10 +66,10 @@ function readReference(document, start) {
66
66
  return reading(document, start, quote, count, end);
67
67
  }
68
68
  function reading(document, start, quote, count, end) {
69
- const emptyReference = count % 2 === 0 ? { value: "", length: count } : null;
70
69
  if (end === null) {
71
- return emptyReference;
70
+ return count % 2 === 0 ? { value: "", length: count, closed: false } : null;
72
71
  }
72
+ const emptyReference = count % 2 === 0 ? { value: "", length: count, closed: true } : null;
73
73
  const parts = [];
74
74
  let position = start + count;
75
75
  let run = document.indexOf(quote, position);
@@ -86,7 +86,7 @@ function reading(document, start, quote, count, end) {
86
86
  if (emptyReference !== null && !isSubstantiveBody(value)) {
87
87
  return emptyReference;
88
88
  }
89
- return { value, length: end - start };
89
+ return { value, length: end - start, closed: true };
90
90
  }
91
91
  function runLength(text, start, quote) {
92
92
  let end = start;
@@ -158,6 +158,23 @@ function isSubstantiveBody(content) {
158
158
  function hasReferenceLiteralPrefix(text, start = 0) {
159
159
  return /^~[0-9]+\{/.test(text.slice(start));
160
160
  }
161
+ var LITERAL_PAYLOAD_END = new Set(["}", " ", "\t", "\r", `
162
+ `, "(", ")"]);
163
+ function referenceLiteralEnd(text, start) {
164
+ if (text[start] !== "~")
165
+ return -1;
166
+ let index = start + 1;
167
+ while (index < text.length && text[index] >= "0" && text[index] <= "9") {
168
+ index++;
169
+ }
170
+ if (index === start + 1 || text[index] !== "{")
171
+ return -1;
172
+ index++;
173
+ while (index < text.length && !LITERAL_PAYLOAD_END.has(text[index])) {
174
+ index++;
175
+ }
176
+ return text[index] === "}" ? index + 1 : -1;
177
+ }
161
178
  function encodeReferenceLiteral(text) {
162
179
  if (typeof text !== "string" || !text.isWellFormed()) {
163
180
  throw new TypeError("Reference must be well-formed Unicode text");
@@ -331,7 +348,7 @@ class Link {
331
348
  return this._formatWithOptions(lessParentheses, isCompoundValue);
332
349
  }
333
350
  if (this.id === null && (!this.values || this.values.length === 0)) {
334
- return lessParentheses ? "" : "()";
351
+ return "()";
335
352
  }
336
353
  if (!this.values || this.values.length === 0) {
337
354
  const escapedId = Link.escapeReference(this.id);
@@ -342,7 +359,7 @@ class Link {
342
359
  }
343
360
  const valuesStr = this.values.map((v) => this.formatValue(v)).join(" ");
344
361
  if (this.id === null) {
345
- if (lessParentheses) {
362
+ if (lessParentheses && !this._hasLoneNestedValue()) {
346
363
  const allSimple = this.values.every((v) => !v.values || v.values.length === 0);
347
364
  if (allSimple) {
348
365
  const simpleValuesStr = this.values.map((v) => Link.escapeReference(v.id)).join(" ");
@@ -372,9 +389,12 @@ class Link {
372
389
  needsParentheses(str) {
373
390
  return str && (str.includes(" ") || str.includes(":") || str.includes("(") || str.includes(")"));
374
391
  }
392
+ _hasLoneNestedValue() {
393
+ return this.values.length === 1 && this.values[0].values?.length > 0;
394
+ }
375
395
  _formatWithOptions(options, isCompoundValue = false) {
376
396
  if (this.id === null && (!this.values || this.values.length === 0)) {
377
- return options.lessParentheses ? "" : "()";
397
+ return "()";
378
398
  }
379
399
  if (!this.values || this.values.length === 0) {
380
400
  const escapedId = Link.escapeReference(this.id);
@@ -393,7 +413,7 @@ class Link {
393
413
  const idStr = Link.escapeReference(this.id);
394
414
  testLine = options.lessParentheses ? `${idStr}: ${valuesStr}` : `(${idStr}: ${valuesStr})`;
395
415
  } else {
396
- testLine = options.lessParentheses ? valuesStr : `(${valuesStr})`;
416
+ testLine = options.lessParentheses && !this._hasLoneNestedValue() ? valuesStr : `(${valuesStr})`;
397
417
  }
398
418
  if (options.shouldIndentByLength(testLine)) {
399
419
  shouldIndent = true;
@@ -404,7 +424,7 @@ class Link {
404
424
  }
405
425
  const valuesStr = this.values.map((v) => this.formatValue(v)).join(" ");
406
426
  if (this.id === null) {
407
- if (options.lessParentheses) {
427
+ if (options.lessParentheses && !this._hasLoneNestedValue()) {
408
428
  const allSimple = this.values.every((v) => !v.values || v.values.length === 0);
409
429
  if (allSimple) {
410
430
  return this.values.map((v) => Link.escapeReference(v.id)).join(" ");
@@ -481,21 +501,17 @@ function formatLinks(links, lessParentheses = false) {
481
501
  }
482
502
  // src/comments.js
483
503
  var COMMENT = "#";
484
- var BEFORE_REFERENCE = [" ", "\t", `
485
- `, "\r", "(", ":"];
504
+ var SEPARATORS = [" ", "\t", `
505
+ `, "\r", "(", ":", ")"];
486
506
  var BEFORE_COMMENT = [" ", "\t", `
487
507
  `, "\r"];
488
508
  function stripComments(document) {
489
509
  let blanked = null;
490
510
  let position = 0;
511
+ let tokenStart = true;
491
512
  const references = new DelimitedReferences(document);
492
513
  while (position < document.length) {
493
514
  const character = document[position];
494
- if (QUOTES.includes(character) && follows(document, position, BEFORE_REFERENCE)) {
495
- const end = references.endAt(position);
496
- position = end === null ? position + 1 : end;
497
- continue;
498
- }
499
515
  if (character === COMMENT && follows(document, position, BEFORE_COMMENT)) {
500
516
  blanked = blanked ?? document.split("");
501
517
  while (position < document.length && document[position] !== `
@@ -505,10 +521,30 @@ function stripComments(document) {
505
521
  }
506
522
  continue;
507
523
  }
524
+ if (SEPARATORS.includes(character)) {
525
+ tokenStart = true;
526
+ position++;
527
+ continue;
528
+ }
529
+ if (tokenStart) {
530
+ const end = delimitedTokenEnd(document, position, references);
531
+ if (end !== null) {
532
+ position = end;
533
+ continue;
534
+ }
535
+ }
536
+ tokenStart = false;
508
537
  position++;
509
538
  }
510
539
  return blanked === null ? document : blanked.join("");
511
540
  }
541
+ function delimitedTokenEnd(document, start, references) {
542
+ const literal = referenceLiteralEnd(document, start);
543
+ if (literal !== -1) {
544
+ return literal;
545
+ }
546
+ return QUOTES.includes(document[start]) ? references.endAt(start) : null;
547
+ }
512
548
  function follows(document, position, allowed) {
513
549
  return position === 0 || allowed.includes(document[position - 1]);
514
550
  }
@@ -569,28 +605,11 @@ var ESCAPES = {
569
605
  function quoteText(text) {
570
606
  return `"${text.replace(/[\\"\n\r\t]/g, (character) => ESCAPES[character])}"`;
571
607
  }
572
- var LITERAL_PAYLOAD_END = new Set(["}", " ", "\t", "\r", `
573
- `, "(", ")"]);
574
- function literalEnd(text, start) {
575
- if (text[start] !== "~")
576
- return -1;
577
- let index = start + 1;
578
- while (index < text.length && text[index] >= "0" && text[index] <= "9") {
579
- index++;
580
- }
581
- if (index === start + 1 || text[index] !== "{")
582
- return -1;
583
- index++;
584
- while (index < text.length && !LITERAL_PAYLOAD_END.has(text[index])) {
585
- index++;
586
- }
587
- return text[index] === "}" ? index + 1 : -1;
588
- }
589
608
  var BARE_REFERENCE_END = new Set([" ", "\t", "\r", `
590
609
  `, "(", ":", ")"]);
591
610
  function buildCst(input, { comments = true } = {}) {
592
611
  const prepared = comments ? stripComments(input) : input;
593
- const references = new DelimitedReferences(prepared);
612
+ const references = new DelimitedReferences(input);
594
613
  const document = new CstNode(CstKind.DOCUMENT);
595
614
  const containers = [{ node: document, line: null }];
596
615
  const add = (token) => {
@@ -654,7 +673,7 @@ function buildCst(input, { comments = true } = {}) {
654
673
  return document;
655
674
  }
656
675
  function referenceEnd(prepared, start, references) {
657
- const literal = literalEnd(prepared, start);
676
+ const literal = referenceLiteralEnd(prepared, start);
658
677
  if (literal !== -1)
659
678
  return literal;
660
679
  if (QUOTES.includes(prepared[start])) {
@@ -668,7 +687,7 @@ function referenceEnd(prepared, start, references) {
668
687
  return end;
669
688
  }
670
689
  function isLiteral(prepared, start, end) {
671
- return literalEnd(prepared, start) === end;
690
+ return referenceLiteralEnd(prepared, start) === end;
672
691
  }
673
692
  // src/LinksGroup.js
674
693
  class LinksGroup {
@@ -823,11 +842,14 @@ class peg$SyntaxError extends SyntaxError {
823
842
  }
824
843
  return s;
825
844
  }
845
+ function escape(s, special) {
846
+ return unicodeEscape(s.replace(special, "\\$&").replace(/\0/g, "\\0").replace(/\t/g, "\\t").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/[\x00-\x0F]/g, (ch) => "\\x0" + hex(ch)).replace(/[\x10-\x1F\x7F-\x9F]/g, (ch) => "\\x" + hex(ch)));
847
+ }
826
848
  function literalEscape(s) {
827
- return unicodeEscape(s.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\0/g, "\\0").replace(/\t/g, "\\t").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/[\x00-\x0F]/g, (ch) => "\\x0" + hex(ch)).replace(/[\x10-\x1F\x7F-\x9F]/g, (ch) => "\\x" + hex(ch)));
849
+ return escape(s, /[\\"]/g);
828
850
  }
829
851
  function classEscape(s) {
830
- return unicodeEscape(s.replace(/\\/g, "\\\\").replace(/\]/g, "\\]").replace(/\^/g, "\\^").replace(/-/g, "\\-").replace(/\0/g, "\\0").replace(/\t/g, "\\t").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/[\x00-\x0F]/g, (ch) => "\\x0" + hex(ch)).replace(/[\x10-\x1F\x7F-\x9F]/g, (ch) => "\\x" + hex(ch)));
852
+ return escape(s, /[\\\]^-]/g);
831
853
  }
832
854
  const DESCRIBE_EXPECTATION_FNS = {
833
855
  literal(expectation) {
@@ -896,12 +918,6 @@ function peg$parse(input, options) {
896
918
  const peg$c7 = "'";
897
919
  const peg$c8 = "`";
898
920
  const peg$c9 = " ";
899
- const peg$r0 = /^[ \t]/;
900
- const peg$r1 = /^[\r\n]/;
901
- const peg$r2 = /^[0-9]/;
902
- const peg$r3 = /^[^} \t\r\n()]/;
903
- const peg$r4 = /^[ \t\n\r]/;
904
- const peg$r5 = /^[^ \t\n\r(:)]/;
905
921
  const peg$e0 = peg$classExpectation([" ", "\t"], false, false, false);
906
922
  const peg$e1 = peg$classExpectation(["\r", `
907
923
  `], false, false, false);
@@ -1125,6 +1141,7 @@ function peg$parse(input, options) {
1125
1141
  let peg$maxFailExpected = options.peg$maxFailExpected || [];
1126
1142
  let peg$silentFails = options.peg$silentFails | 0;
1127
1143
  let peg$result;
1144
+ let peg$charCode;
1128
1145
  if (options.startRule) {
1129
1146
  if (!(options.startRule in peg$startRuleFunctions)) {
1130
1147
  throw new Error(`Can't start parsing from rule "` + options.startRule + '".');
@@ -1305,62 +1322,12 @@ function peg$parse(input, options) {
1305
1322
  function peg$parseskipEmptyLines() {
1306
1323
  let s0, s1, s2, s3;
1307
1324
  s0 = [];
1308
- s1 = peg$currPos;
1309
- s2 = [];
1310
- s3 = input.charAt(peg$currPos);
1311
- if (peg$r0.test(s3)) {
1312
- peg$currPos++;
1313
- } else {
1314
- s3 = peg$FAILED;
1315
- if (peg$silentFails === 0) {
1316
- peg$fail(peg$e0);
1317
- }
1318
- }
1319
- while (s3 !== peg$FAILED) {
1320
- s2.push(s3);
1321
- s3 = input.charAt(peg$currPos);
1322
- if (peg$r0.test(s3)) {
1323
- peg$currPos++;
1324
- } else {
1325
- s3 = peg$FAILED;
1326
- if (peg$silentFails === 0) {
1327
- peg$fail(peg$e0);
1328
- }
1329
- }
1330
- }
1331
- s3 = input.charAt(peg$currPos);
1332
- if (peg$r1.test(s3)) {
1333
- peg$currPos++;
1334
- } else {
1335
- s3 = peg$FAILED;
1336
- if (peg$silentFails === 0) {
1337
- peg$fail(peg$e1);
1338
- }
1339
- }
1340
- if (s3 !== peg$FAILED) {
1341
- s2 = [s2, s3];
1342
- s1 = s2;
1343
- } else {
1344
- peg$currPos = s1;
1345
- s1 = peg$FAILED;
1346
- }
1347
- while (s1 !== peg$FAILED) {
1348
- s0.push(s1);
1325
+ for (;; ) {
1349
1326
  s1 = peg$currPos;
1350
1327
  s2 = [];
1351
- s3 = input.charAt(peg$currPos);
1352
- if (peg$r0.test(s3)) {
1353
- peg$currPos++;
1354
- } else {
1355
- s3 = peg$FAILED;
1356
- if (peg$silentFails === 0) {
1357
- peg$fail(peg$e0);
1358
- }
1359
- }
1360
- while (s3 !== peg$FAILED) {
1361
- s2.push(s3);
1328
+ for (;; ) {
1362
1329
  s3 = input.charAt(peg$currPos);
1363
- if (peg$r0.test(s3)) {
1330
+ if (peg$charCode = s3.charCodeAt(0), peg$charCode === 32 || peg$charCode === 9) {
1364
1331
  peg$currPos++;
1365
1332
  } else {
1366
1333
  s3 = peg$FAILED;
@@ -1368,9 +1335,13 @@ function peg$parse(input, options) {
1368
1335
  peg$fail(peg$e0);
1369
1336
  }
1370
1337
  }
1338
+ if (s3 === peg$FAILED) {
1339
+ break;
1340
+ }
1341
+ s2.push(s3);
1371
1342
  }
1372
1343
  s3 = input.charAt(peg$currPos);
1373
- if (peg$r1.test(s3)) {
1344
+ if (peg$charCode = s3.charCodeAt(0), peg$charCode === 13 || peg$charCode === 10) {
1374
1345
  peg$currPos++;
1375
1346
  } else {
1376
1347
  s3 = peg$FAILED;
@@ -1385,6 +1356,10 @@ function peg$parse(input, options) {
1385
1356
  peg$currPos = s1;
1386
1357
  s1 = peg$FAILED;
1387
1358
  }
1359
+ if (s1 === peg$FAILED) {
1360
+ break;
1361
+ }
1362
+ s0.push(s1);
1388
1363
  }
1389
1364
  return s0;
1390
1365
  }
@@ -1394,10 +1369,12 @@ function peg$parse(input, options) {
1394
1369
  s1 = peg$parsefirstLine();
1395
1370
  if (s1 !== peg$FAILED) {
1396
1371
  s2 = [];
1397
- s3 = peg$parseline();
1398
- while (s3 !== peg$FAILED) {
1399
- s2.push(s3);
1372
+ for (;; ) {
1400
1373
  s3 = peg$parseline();
1374
+ if (s3 === peg$FAILED) {
1375
+ break;
1376
+ }
1377
+ s2.push(s3);
1401
1378
  }
1402
1379
  peg$savedPos = s0;
1403
1380
  s0 = peg$f4(s1, s2);
@@ -1649,10 +1626,12 @@ function peg$parse(input, options) {
1649
1626
  if (s0 === peg$FAILED) {
1650
1627
  s0 = peg$currPos;
1651
1628
  s1 = [];
1652
- s2 = peg$parsesingleLineValueAndWhitespace();
1653
- while (s2 !== peg$FAILED) {
1654
- s1.push(s2);
1629
+ for (;; ) {
1655
1630
  s2 = peg$parsesingleLineValueAndWhitespace();
1631
+ if (s2 === peg$FAILED) {
1632
+ break;
1633
+ }
1634
+ s1.push(s2);
1656
1635
  }
1657
1636
  s2 = peg$parseeol();
1658
1637
  if (s2 !== peg$FAILED) {
@@ -1864,13 +1843,14 @@ function peg$parse(input, options) {
1864
1843
  let s0, s1, s2;
1865
1844
  s0 = peg$currPos;
1866
1845
  s1 = [];
1867
- s2 = peg$parsesingleLineValueAndWhitespace();
1868
- if (s2 !== peg$FAILED) {
1869
- while (s2 !== peg$FAILED) {
1870
- s1.push(s2);
1871
- s2 = peg$parsesingleLineValueAndWhitespace();
1846
+ for (;; ) {
1847
+ s2 = peg$parsesingleLineValueAndWhitespace();
1848
+ if (s2 === peg$FAILED) {
1849
+ break;
1872
1850
  }
1873
- } else {
1851
+ s1.push(s2);
1852
+ }
1853
+ if (s1.length < 1) {
1874
1854
  s1 = peg$FAILED;
1875
1855
  }
1876
1856
  if (s1 !== peg$FAILED) {
@@ -2025,29 +2005,22 @@ function peg$parse(input, options) {
2025
2005
  }
2026
2006
  if (s5 !== peg$FAILED) {
2027
2007
  s6 = [];
2028
- s7 = input.charAt(peg$currPos);
2029
- if (peg$r2.test(s7)) {
2030
- peg$currPos++;
2031
- } else {
2032
- s7 = peg$FAILED;
2033
- if (peg$silentFails === 0) {
2034
- peg$fail(peg$e7);
2035
- }
2036
- }
2037
- if (s7 !== peg$FAILED) {
2038
- while (s7 !== peg$FAILED) {
2039
- s6.push(s7);
2040
- s7 = input.charAt(peg$currPos);
2041
- if (peg$r2.test(s7)) {
2042
- peg$currPos++;
2043
- } else {
2044
- s7 = peg$FAILED;
2045
- if (peg$silentFails === 0) {
2046
- peg$fail(peg$e7);
2047
- }
2008
+ for (;; ) {
2009
+ s7 = input.charAt(peg$currPos);
2010
+ if (peg$charCode = s7.charCodeAt(0), peg$charCode >= 48 && peg$charCode <= 57) {
2011
+ peg$currPos++;
2012
+ } else {
2013
+ s7 = peg$FAILED;
2014
+ if (peg$silentFails === 0) {
2015
+ peg$fail(peg$e7);
2048
2016
  }
2049
2017
  }
2050
- } else {
2018
+ if (s7 === peg$FAILED) {
2019
+ break;
2020
+ }
2021
+ s6.push(s7);
2022
+ }
2023
+ if (s6.length < 1) {
2051
2024
  s6 = peg$FAILED;
2052
2025
  }
2053
2026
  if (s6 !== peg$FAILED) {
@@ -2062,19 +2035,9 @@ function peg$parse(input, options) {
2062
2035
  }
2063
2036
  if (s7 !== peg$FAILED) {
2064
2037
  s8 = [];
2065
- s9 = input.charAt(peg$currPos);
2066
- if (peg$r3.test(s9)) {
2067
- peg$currPos++;
2068
- } else {
2069
- s9 = peg$FAILED;
2070
- if (peg$silentFails === 0) {
2071
- peg$fail(peg$e9);
2072
- }
2073
- }
2074
- while (s9 !== peg$FAILED) {
2075
- s8.push(s9);
2038
+ for (;; ) {
2076
2039
  s9 = input.charAt(peg$currPos);
2077
- if (peg$r3.test(s9)) {
2040
+ if (s9 !== "" && !(peg$charCode = s9.charCodeAt(0), peg$charCode === 125 || peg$charCode === 32 || peg$charCode === 9 || peg$charCode === 13 || peg$charCode === 10 || peg$charCode === 40 || peg$charCode === 41)) {
2078
2041
  peg$currPos++;
2079
2042
  } else {
2080
2043
  s9 = peg$FAILED;
@@ -2082,6 +2045,10 @@ function peg$parse(input, options) {
2082
2045
  peg$fail(peg$e9);
2083
2046
  }
2084
2047
  }
2048
+ if (s9 === peg$FAILED) {
2049
+ break;
2050
+ }
2051
+ s8.push(s9);
2085
2052
  }
2086
2053
  if (input.charCodeAt(peg$currPos) === 125) {
2087
2054
  s9 = peg$c5;
@@ -2145,13 +2112,14 @@ function peg$parse(input, options) {
2145
2112
  }
2146
2113
  if (s1 !== peg$FAILED) {
2147
2114
  s2 = [];
2148
- s3 = peg$parsereferenceSymbol();
2149
- if (s3 !== peg$FAILED) {
2150
- while (s3 !== peg$FAILED) {
2151
- s2.push(s3);
2152
- s3 = peg$parsereferenceSymbol();
2115
+ for (;; ) {
2116
+ s3 = peg$parsereferenceSymbol();
2117
+ if (s3 === peg$FAILED) {
2118
+ break;
2153
2119
  }
2154
- } else {
2120
+ s2.push(s3);
2121
+ }
2122
+ if (s2.length < 1) {
2155
2123
  s2 = peg$FAILED;
2156
2124
  }
2157
2125
  if (s2 !== peg$FAILED) {
@@ -2240,10 +2208,12 @@ function peg$parse(input, options) {
2240
2208
  }
2241
2209
  if (s1 !== peg$FAILED) {
2242
2210
  s2 = [];
2243
- s3 = peg$parseconsumeDoubleMore();
2244
- while (s3 !== peg$FAILED) {
2245
- s2.push(s3);
2211
+ for (;; ) {
2246
2212
  s3 = peg$parseconsumeDoubleMore();
2213
+ if (s3 === peg$FAILED) {
2214
+ break;
2215
+ }
2216
+ s2.push(s3);
2247
2217
  }
2248
2218
  peg$savedPos = s0;
2249
2219
  s0 = peg$f36(s1, s2);
@@ -2348,10 +2318,12 @@ function peg$parse(input, options) {
2348
2318
  }
2349
2319
  if (s1 !== peg$FAILED) {
2350
2320
  s2 = [];
2351
- s3 = peg$parseconsumeSingleMore();
2352
- while (s3 !== peg$FAILED) {
2353
- s2.push(s3);
2321
+ for (;; ) {
2354
2322
  s3 = peg$parseconsumeSingleMore();
2323
+ if (s3 === peg$FAILED) {
2324
+ break;
2325
+ }
2326
+ s2.push(s3);
2355
2327
  }
2356
2328
  peg$savedPos = s0;
2357
2329
  s0 = peg$f41(s1, s2);
@@ -2456,10 +2428,12 @@ function peg$parse(input, options) {
2456
2428
  }
2457
2429
  if (s1 !== peg$FAILED) {
2458
2430
  s2 = [];
2459
- s3 = peg$parseconsumeBacktickMore();
2460
- while (s3 !== peg$FAILED) {
2461
- s2.push(s3);
2431
+ for (;; ) {
2462
2432
  s3 = peg$parseconsumeBacktickMore();
2433
+ if (s3 === peg$FAILED) {
2434
+ break;
2435
+ }
2436
+ s2.push(s3);
2463
2437
  }
2464
2438
  peg$savedPos = s0;
2465
2439
  s0 = peg$f46(s1, s2);
@@ -2515,17 +2489,7 @@ function peg$parse(input, options) {
2515
2489
  let s0, s1, s2;
2516
2490
  s0 = peg$currPos;
2517
2491
  s1 = [];
2518
- if (input.charCodeAt(peg$currPos) === 32) {
2519
- s2 = peg$c9;
2520
- peg$currPos++;
2521
- } else {
2522
- s2 = peg$FAILED;
2523
- if (peg$silentFails === 0) {
2524
- peg$fail(peg$e14);
2525
- }
2526
- }
2527
- while (s2 !== peg$FAILED) {
2528
- s1.push(s2);
2492
+ for (;; ) {
2529
2493
  if (input.charCodeAt(peg$currPos) === 32) {
2530
2494
  s2 = peg$c9;
2531
2495
  peg$currPos++;
@@ -2535,6 +2499,10 @@ function peg$parse(input, options) {
2535
2499
  peg$fail(peg$e14);
2536
2500
  }
2537
2501
  }
2502
+ if (s2 === peg$FAILED) {
2503
+ break;
2504
+ }
2505
+ s1.push(s2);
2538
2506
  }
2539
2507
  peg$savedPos = s0;
2540
2508
  s1 = peg$f50(s1);
@@ -2545,17 +2513,7 @@ function peg$parse(input, options) {
2545
2513
  let s0, s1, s2;
2546
2514
  s0 = peg$currPos;
2547
2515
  s1 = [];
2548
- if (input.charCodeAt(peg$currPos) === 32) {
2549
- s2 = peg$c9;
2550
- peg$currPos++;
2551
- } else {
2552
- s2 = peg$FAILED;
2553
- if (peg$silentFails === 0) {
2554
- peg$fail(peg$e14);
2555
- }
2556
- }
2557
- while (s2 !== peg$FAILED) {
2558
- s1.push(s2);
2516
+ for (;; ) {
2559
2517
  if (input.charCodeAt(peg$currPos) === 32) {
2560
2518
  s2 = peg$c9;
2561
2519
  peg$currPos++;
@@ -2565,6 +2523,10 @@ function peg$parse(input, options) {
2565
2523
  peg$fail(peg$e14);
2566
2524
  }
2567
2525
  }
2526
+ if (s2 === peg$FAILED) {
2527
+ break;
2528
+ }
2529
+ s1.push(s2);
2568
2530
  }
2569
2531
  peg$savedPos = peg$currPos;
2570
2532
  s2 = peg$f51(s1);
@@ -2586,17 +2548,7 @@ function peg$parse(input, options) {
2586
2548
  let s0, s1, s2;
2587
2549
  s0 = peg$currPos;
2588
2550
  s1 = [];
2589
- if (input.charCodeAt(peg$currPos) === 32) {
2590
- s2 = peg$c9;
2591
- peg$currPos++;
2592
- } else {
2593
- s2 = peg$FAILED;
2594
- if (peg$silentFails === 0) {
2595
- peg$fail(peg$e14);
2596
- }
2597
- }
2598
- while (s2 !== peg$FAILED) {
2599
- s1.push(s2);
2551
+ for (;; ) {
2600
2552
  if (input.charCodeAt(peg$currPos) === 32) {
2601
2553
  s2 = peg$c9;
2602
2554
  peg$currPos++;
@@ -2606,6 +2558,10 @@ function peg$parse(input, options) {
2606
2558
  peg$fail(peg$e14);
2607
2559
  }
2608
2560
  }
2561
+ if (s2 === peg$FAILED) {
2562
+ break;
2563
+ }
2564
+ s1.push(s2);
2609
2565
  }
2610
2566
  peg$savedPos = peg$currPos;
2611
2567
  s2 = peg$f53(s1);
@@ -2647,49 +2603,32 @@ function peg$parse(input, options) {
2647
2603
  let s0, s1, s2, s3, s4, s5, s6;
2648
2604
  s0 = peg$currPos;
2649
2605
  s1 = [];
2650
- s2 = input.charAt(peg$currPos);
2651
- if (peg$r1.test(s2)) {
2652
- peg$currPos++;
2653
- } else {
2654
- s2 = peg$FAILED;
2655
- if (peg$silentFails === 0) {
2656
- peg$fail(peg$e1);
2657
- }
2658
- }
2659
- if (s2 !== peg$FAILED) {
2660
- while (s2 !== peg$FAILED) {
2661
- s1.push(s2);
2662
- s2 = input.charAt(peg$currPos);
2663
- if (peg$r1.test(s2)) {
2664
- peg$currPos++;
2665
- } else {
2666
- s2 = peg$FAILED;
2667
- if (peg$silentFails === 0) {
2668
- peg$fail(peg$e1);
2669
- }
2606
+ for (;; ) {
2607
+ s2 = input.charAt(peg$currPos);
2608
+ if (peg$charCode = s2.charCodeAt(0), peg$charCode === 13 || peg$charCode === 10) {
2609
+ peg$currPos++;
2610
+ } else {
2611
+ s2 = peg$FAILED;
2612
+ if (peg$silentFails === 0) {
2613
+ peg$fail(peg$e1);
2670
2614
  }
2671
2615
  }
2672
- } else {
2616
+ if (s2 === peg$FAILED) {
2617
+ break;
2618
+ }
2619
+ s1.push(s2);
2620
+ }
2621
+ if (s1.length < 1) {
2673
2622
  s1 = peg$FAILED;
2674
2623
  }
2675
2624
  if (s1 !== peg$FAILED) {
2676
2625
  s2 = [];
2677
- s3 = peg$currPos;
2678
- s4 = [];
2679
- s5 = input.charAt(peg$currPos);
2680
- if (peg$r0.test(s5)) {
2681
- peg$currPos++;
2682
- } else {
2683
- s5 = peg$FAILED;
2684
- if (peg$silentFails === 0) {
2685
- peg$fail(peg$e0);
2686
- }
2687
- }
2688
- if (s5 !== peg$FAILED) {
2689
- while (s5 !== peg$FAILED) {
2690
- s4.push(s5);
2626
+ for (;; ) {
2627
+ s3 = peg$currPos;
2628
+ s4 = [];
2629
+ for (;; ) {
2691
2630
  s5 = input.charAt(peg$currPos);
2692
- if (peg$r0.test(s5)) {
2631
+ if (peg$charCode = s5.charCodeAt(0), peg$charCode === 32 || peg$charCode === 9) {
2693
2632
  peg$currPos++;
2694
2633
  } else {
2695
2634
  s5 = peg$FAILED;
@@ -2697,26 +2636,19 @@ function peg$parse(input, options) {
2697
2636
  peg$fail(peg$e0);
2698
2637
  }
2699
2638
  }
2700
- }
2701
- } else {
2702
- s4 = peg$FAILED;
2703
- }
2704
- if (s4 !== peg$FAILED) {
2705
- s5 = [];
2706
- s6 = input.charAt(peg$currPos);
2707
- if (peg$r1.test(s6)) {
2708
- peg$currPos++;
2709
- } else {
2710
- s6 = peg$FAILED;
2711
- if (peg$silentFails === 0) {
2712
- peg$fail(peg$e1);
2639
+ if (s5 === peg$FAILED) {
2640
+ break;
2713
2641
  }
2642
+ s4.push(s5);
2714
2643
  }
2715
- if (s6 !== peg$FAILED) {
2716
- while (s6 !== peg$FAILED) {
2717
- s5.push(s6);
2644
+ if (s4.length < 1) {
2645
+ s4 = peg$FAILED;
2646
+ }
2647
+ if (s4 !== peg$FAILED) {
2648
+ s5 = [];
2649
+ for (;; ) {
2718
2650
  s6 = input.charAt(peg$currPos);
2719
- if (peg$r1.test(s6)) {
2651
+ if (peg$charCode = s6.charCodeAt(0), peg$charCode === 13 || peg$charCode === 10) {
2720
2652
  peg$currPos++;
2721
2653
  } else {
2722
2654
  s6 = peg$FAILED;
@@ -2724,75 +2656,12 @@ function peg$parse(input, options) {
2724
2656
  peg$fail(peg$e1);
2725
2657
  }
2726
2658
  }
2727
- }
2728
- } else {
2729
- s5 = peg$FAILED;
2730
- }
2731
- if (s5 !== peg$FAILED) {
2732
- s4 = [s4, s5];
2733
- s3 = s4;
2734
- } else {
2735
- peg$currPos = s3;
2736
- s3 = peg$FAILED;
2737
- }
2738
- } else {
2739
- peg$currPos = s3;
2740
- s3 = peg$FAILED;
2741
- }
2742
- while (s3 !== peg$FAILED) {
2743
- s2.push(s3);
2744
- s3 = peg$currPos;
2745
- s4 = [];
2746
- s5 = input.charAt(peg$currPos);
2747
- if (peg$r0.test(s5)) {
2748
- peg$currPos++;
2749
- } else {
2750
- s5 = peg$FAILED;
2751
- if (peg$silentFails === 0) {
2752
- peg$fail(peg$e0);
2753
- }
2754
- }
2755
- if (s5 !== peg$FAILED) {
2756
- while (s5 !== peg$FAILED) {
2757
- s4.push(s5);
2758
- s5 = input.charAt(peg$currPos);
2759
- if (peg$r0.test(s5)) {
2760
- peg$currPos++;
2761
- } else {
2762
- s5 = peg$FAILED;
2763
- if (peg$silentFails === 0) {
2764
- peg$fail(peg$e0);
2765
- }
2766
- }
2767
- }
2768
- } else {
2769
- s4 = peg$FAILED;
2770
- }
2771
- if (s4 !== peg$FAILED) {
2772
- s5 = [];
2773
- s6 = input.charAt(peg$currPos);
2774
- if (peg$r1.test(s6)) {
2775
- peg$currPos++;
2776
- } else {
2777
- s6 = peg$FAILED;
2778
- if (peg$silentFails === 0) {
2779
- peg$fail(peg$e1);
2659
+ if (s6 === peg$FAILED) {
2660
+ break;
2780
2661
  }
2662
+ s5.push(s6);
2781
2663
  }
2782
- if (s6 !== peg$FAILED) {
2783
- while (s6 !== peg$FAILED) {
2784
- s5.push(s6);
2785
- s6 = input.charAt(peg$currPos);
2786
- if (peg$r1.test(s6)) {
2787
- peg$currPos++;
2788
- } else {
2789
- s6 = peg$FAILED;
2790
- if (peg$silentFails === 0) {
2791
- peg$fail(peg$e1);
2792
- }
2793
- }
2794
- }
2795
- } else {
2664
+ if (s5.length < 1) {
2796
2665
  s5 = peg$FAILED;
2797
2666
  }
2798
2667
  if (s5 !== peg$FAILED) {
@@ -2806,6 +2675,10 @@ function peg$parse(input, options) {
2806
2675
  peg$currPos = s3;
2807
2676
  s3 = peg$FAILED;
2808
2677
  }
2678
+ if (s3 === peg$FAILED) {
2679
+ break;
2680
+ }
2681
+ s2.push(s3);
2809
2682
  }
2810
2683
  s1 = [s1, s2];
2811
2684
  s0 = s1;
@@ -2882,19 +2755,9 @@ function peg$parse(input, options) {
2882
2755
  function peg$parse__() {
2883
2756
  let s0, s1;
2884
2757
  s0 = [];
2885
- s1 = input.charAt(peg$currPos);
2886
- if (peg$r0.test(s1)) {
2887
- peg$currPos++;
2888
- } else {
2889
- s1 = peg$FAILED;
2890
- if (peg$silentFails === 0) {
2891
- peg$fail(peg$e0);
2892
- }
2893
- }
2894
- while (s1 !== peg$FAILED) {
2895
- s0.push(s1);
2758
+ for (;; ) {
2896
2759
  s1 = input.charAt(peg$currPos);
2897
- if (peg$r0.test(s1)) {
2760
+ if (peg$charCode = s1.charCodeAt(0), peg$charCode === 32 || peg$charCode === 9) {
2898
2761
  peg$currPos++;
2899
2762
  } else {
2900
2763
  s1 = peg$FAILED;
@@ -2902,23 +2765,29 @@ function peg$parse(input, options) {
2902
2765
  peg$fail(peg$e0);
2903
2766
  }
2904
2767
  }
2768
+ if (s1 === peg$FAILED) {
2769
+ break;
2770
+ }
2771
+ s0.push(s1);
2905
2772
  }
2906
2773
  return s0;
2907
2774
  }
2908
2775
  function peg$parse_() {
2909
2776
  let s0, s1;
2910
2777
  s0 = [];
2911
- s1 = peg$parsewhiteSpaceSymbol();
2912
- while (s1 !== peg$FAILED) {
2913
- s0.push(s1);
2778
+ for (;; ) {
2914
2779
  s1 = peg$parsewhiteSpaceSymbol();
2780
+ if (s1 === peg$FAILED) {
2781
+ break;
2782
+ }
2783
+ s0.push(s1);
2915
2784
  }
2916
2785
  return s0;
2917
2786
  }
2918
2787
  function peg$parsewhiteSpaceSymbol() {
2919
2788
  let s0;
2920
2789
  s0 = input.charAt(peg$currPos);
2921
- if (peg$r4.test(s0)) {
2790
+ if (peg$charCode = s0.charCodeAt(0), peg$charCode === 32 || peg$charCode === 9 || peg$charCode === 10 || peg$charCode === 13) {
2922
2791
  peg$currPos++;
2923
2792
  } else {
2924
2793
  s0 = peg$FAILED;
@@ -2931,7 +2800,7 @@ function peg$parse(input, options) {
2931
2800
  function peg$parsereferenceSymbol() {
2932
2801
  let s0;
2933
2802
  s0 = input.charAt(peg$currPos);
2934
- if (peg$r5.test(s0)) {
2803
+ if (s0 !== "" && !(peg$charCode = s0.charCodeAt(0), peg$charCode === 32 || peg$charCode === 9 || peg$charCode === 10 || peg$charCode === 13 || peg$charCode === 40 || peg$charCode === 58 || peg$charCode === 41)) {
2935
2804
  peg$currPos++;
2936
2805
  } else {
2937
2806
  s0 = peg$FAILED;
@@ -3019,7 +2888,7 @@ function peg$parse(input, options) {
3019
2888
  function getCurrentIndentation() {
3020
2889
  return indentationStack[indentationStack.length - 1];
3021
2890
  }
3022
- const delimitedReferences = new DelimitedReferences(input);
2891
+ const delimitedReferences = new DelimitedReferences(source);
3023
2892
  function parseQuotedStringAt(inputStr, startPos, quoteChar) {
3024
2893
  if (startPos >= inputStr.length || inputStr[startPos] !== quoteChar) {
3025
2894
  return null;
@@ -3559,34 +3428,48 @@ function countNewlines(value) {
3559
3428
  }
3560
3429
  function structurallyComplete(document, comments) {
3561
3430
  const quotes = ['"', "'", "`"];
3562
- const beforeReference = [" ", "\t", `
3563
- `, "\r", "(", ":"];
3431
+ const separators = [" ", "\t", `
3432
+ `, "\r", "(", ":", ")"];
3564
3433
  const beforeComment = [" ", "\t", `
3565
3434
  `, "\r"];
3566
3435
  const references = new DelimitedReferences(document);
3567
3436
  let depth = 0;
3437
+ let tokenStart = true;
3568
3438
  for (let position = 0;position < document.length; position++) {
3569
3439
  const character = document[position];
3570
3440
  const previous = position === 0 ? null : document[position - 1];
3571
- if (quotes.includes(character) && (previous === null || beforeReference.includes(previous))) {
3572
- const end = references.endAt(position);
3573
- if (end === null)
3574
- return false;
3575
- position = end - 1;
3576
- continue;
3577
- }
3578
3441
  if (comments && character === "#" && (previous === null || beforeComment.includes(previous))) {
3579
3442
  const newline = document.indexOf(`
3580
3443
  `, position);
3581
3444
  if (newline === -1)
3582
3445
  return true;
3583
3446
  position = newline;
3447
+ tokenStart = true;
3448
+ continue;
3449
+ }
3450
+ if (separators.includes(character)) {
3451
+ if (character === "(")
3452
+ depth += 1;
3453
+ if (character === ")")
3454
+ depth -= 1;
3455
+ tokenStart = true;
3584
3456
  continue;
3585
3457
  }
3586
- if (character === "(")
3587
- depth += 1;
3588
- if (character === ")")
3589
- depth -= 1;
3458
+ if (tokenStart && quotes.includes(character)) {
3459
+ const reading = references.readAt(position);
3460
+ if (reading === null || !reading.closed)
3461
+ return false;
3462
+ position += reading.length - 1;
3463
+ continue;
3464
+ }
3465
+ if (tokenStart) {
3466
+ const end = referenceLiteralEnd(document, position);
3467
+ if (end !== -1) {
3468
+ position = end - 1;
3469
+ continue;
3470
+ }
3471
+ }
3472
+ tokenStart = false;
3590
3473
  }
3591
3474
  return depth === 0;
3592
3475
  }