links-notation 0.27.0 → 0.28.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.
package/dist/index.js CHANGED
@@ -235,9 +235,16 @@ function unescapeReference(reference) {
235
235
  }
236
236
  return reading.value;
237
237
  }
238
+ function referenceQuote(raw) {
239
+ if (typeof raw !== "string" || !QUOTES.includes(raw[0]))
240
+ return null;
241
+ const reading = readReference(raw, 0);
242
+ return reading !== null && reading.length === raw.length ? raw[0] : null;
243
+ }
238
244
 
239
245
  // src/Link.js
240
246
  class Link {
247
+ #rawId = null;
241
248
  constructor(id = null, values = null) {
242
249
  this.id = id;
243
250
  if (values !== null && values !== undefined) {
@@ -249,6 +256,18 @@ class Link {
249
256
  this.values = [];
250
257
  }
251
258
  }
259
+ get rawId() {
260
+ return this.#rawId;
261
+ }
262
+ set rawId(raw) {
263
+ if (raw !== null && raw !== undefined && typeof raw !== "string") {
264
+ throw new TypeError("rawId must be a string or null");
265
+ }
266
+ this.#rawId = raw ?? null;
267
+ }
268
+ get quote() {
269
+ return referenceQuote(this.#rawId);
270
+ }
252
271
  toString() {
253
272
  return this.format(false);
254
273
  }
@@ -460,6 +479,197 @@ function formatLinks(links, lessParentheses = false) {
460
479
  return links.map((link) => link.format(lessParentheses)).join(`
461
480
  `);
462
481
  }
482
+ // src/comments.js
483
+ var COMMENT = "#";
484
+ var BEFORE_REFERENCE = [" ", "\t", `
485
+ `, "\r", "(", ":"];
486
+ var BEFORE_COMMENT = [" ", "\t", `
487
+ `, "\r"];
488
+ function stripComments(document) {
489
+ let blanked = null;
490
+ let position = 0;
491
+ const references = new DelimitedReferences(document);
492
+ while (position < document.length) {
493
+ 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
+ if (character === COMMENT && follows(document, position, BEFORE_COMMENT)) {
500
+ blanked = blanked ?? document.split("");
501
+ while (position < document.length && document[position] !== `
502
+ ` && document[position] !== "\r") {
503
+ blanked[position] = " ";
504
+ position++;
505
+ }
506
+ continue;
507
+ }
508
+ position++;
509
+ }
510
+ return blanked === null ? document : blanked.join("");
511
+ }
512
+ function follows(document, position, allowed) {
513
+ return position === 0 || allowed.includes(document[position - 1]);
514
+ }
515
+
516
+ // src/Cst.js
517
+ var CstKind = Object.freeze({
518
+ DOCUMENT: "document",
519
+ LINE: "line",
520
+ GROUP: "group",
521
+ WHITESPACE: "whitespace",
522
+ NEWLINE: "newline",
523
+ COMMENT: "comment",
524
+ OPEN_PAREN: "open_paren",
525
+ CLOSE_PAREN: "close_paren",
526
+ COLON: "colon",
527
+ REFERENCE: "reference"
528
+ });
529
+ var NODE_KINDS = new Set([CstKind.DOCUMENT, CstKind.LINE, CstKind.GROUP]);
530
+
531
+ class CstNode {
532
+ #text;
533
+ constructor(kind, { text = "", children = [], value = null, quote = null } = {}) {
534
+ this.kind = kind;
535
+ this.children = children;
536
+ this.value = value;
537
+ this.quote = quote;
538
+ this.#text = text;
539
+ }
540
+ get isToken() {
541
+ return !NODE_KINDS.has(this.kind);
542
+ }
543
+ get text() {
544
+ if (this.isToken)
545
+ return this.#text;
546
+ return this.children.map((child) => child.text).join("");
547
+ }
548
+ toString() {
549
+ return this.text;
550
+ }
551
+ tokens() {
552
+ if (this.isToken)
553
+ return [this];
554
+ return this.children.flatMap((child) => child.tokens());
555
+ }
556
+ toSExpression() {
557
+ if (this.isToken)
558
+ return `${this.kind}:${quoteText(this.#text)}`;
559
+ return `(${[this.kind, ...this.children.map((child) => child.toSExpression())].join(" ")})`;
560
+ }
561
+ }
562
+ var ESCAPES = {
563
+ "\\": "\\\\",
564
+ '"': "\\\"",
565
+ "\n": "\\n",
566
+ "\r": "\\r",
567
+ "\t": "\\t"
568
+ };
569
+ function quoteText(text) {
570
+ return `"${text.replace(/[\\"\n\r\t]/g, (character) => ESCAPES[character])}"`;
571
+ }
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
+ var BARE_REFERENCE_END = new Set([" ", "\t", "\r", `
590
+ `, "(", ":", ")"]);
591
+ function buildCst(input, { comments = true } = {}) {
592
+ const prepared = comments ? stripComments(input) : input;
593
+ const references = new DelimitedReferences(prepared);
594
+ const document = new CstNode(CstKind.DOCUMENT);
595
+ const containers = [{ node: document, line: null }];
596
+ const add = (token) => {
597
+ const container = containers[containers.length - 1];
598
+ if (container.line === null) {
599
+ container.line = new CstNode(CstKind.LINE);
600
+ container.node.children.push(container.line);
601
+ }
602
+ container.line.children.push(token);
603
+ };
604
+ const token = (kind, start, end, parts = {}) => new CstNode(kind, { text: input.slice(start, end), ...parts });
605
+ let position = 0;
606
+ while (position < input.length) {
607
+ const character = input[position];
608
+ let end = position + 1;
609
+ if (comments && character === "#" && prepared[position] !== "#") {
610
+ while (end < input.length && input[end] !== `
611
+ ` && input[end] !== "\r") {
612
+ end++;
613
+ }
614
+ add(token(CstKind.COMMENT, position, end));
615
+ } else if (character === "\r" || character === `
616
+ `) {
617
+ if (character === "\r" && input[end] === `
618
+ `)
619
+ end++;
620
+ add(token(CstKind.NEWLINE, position, end));
621
+ containers[containers.length - 1].line = null;
622
+ } else if (character === " " || character === "\t") {
623
+ while (end < input.length && (input[end] === " " || input[end] === "\t")) {
624
+ end++;
625
+ }
626
+ add(token(CstKind.WHITESPACE, position, end));
627
+ } else if (character === "(") {
628
+ const group = new CstNode(CstKind.GROUP, {
629
+ children: [token(CstKind.OPEN_PAREN, position, end)]
630
+ });
631
+ add(group);
632
+ containers.push({ node: group, line: null });
633
+ } else if (character === ")") {
634
+ if (containers.length === 1) {
635
+ throw new SyntaxError(`Unbalanced ")" at offset ${position}`);
636
+ }
637
+ containers.pop().node.children.push(token(CstKind.CLOSE_PAREN, position, end));
638
+ } else if (character === ":") {
639
+ add(token(CstKind.COLON, position, end));
640
+ } else {
641
+ end = referenceEnd(prepared, position, references);
642
+ const text = input.slice(position, end);
643
+ const quoted = QUOTES.includes(character) && references.readAt(position);
644
+ add(token(CstKind.REFERENCE, position, end, {
645
+ value: quoted ? quoted.value : character === "~" && isLiteral(prepared, position, end) ? decodeReferenceLiteral(text) : text,
646
+ quote: quoted ? character : null
647
+ }));
648
+ }
649
+ position = end;
650
+ }
651
+ if (containers.length !== 1) {
652
+ throw new SyntaxError('Unbalanced "(" at the end of the document');
653
+ }
654
+ return document;
655
+ }
656
+ function referenceEnd(prepared, start, references) {
657
+ const literal = literalEnd(prepared, start);
658
+ if (literal !== -1)
659
+ return literal;
660
+ if (QUOTES.includes(prepared[start])) {
661
+ const end = references.endAt(start);
662
+ if (end !== null)
663
+ return end;
664
+ }
665
+ let end = start + 1;
666
+ while (end < prepared.length && !BARE_REFERENCE_END.has(prepared[end]))
667
+ end++;
668
+ return end;
669
+ }
670
+ function isLiteral(prepared, start, end) {
671
+ return literalEnd(prepared, start) === end;
672
+ }
463
673
  // src/LinksGroup.js
464
674
  class LinksGroup {
465
675
  constructor(element, children = []) {
@@ -564,40 +774,6 @@ function windowAround(lineText, column) {
564
774
  return [quoted, target - start + shift + 1];
565
775
  }
566
776
 
567
- // src/comments.js
568
- var COMMENT = "#";
569
- var BEFORE_REFERENCE = [" ", "\t", `
570
- `, "\r", "(", ":"];
571
- var BEFORE_COMMENT = [" ", "\t", `
572
- `, "\r"];
573
- function stripComments(document) {
574
- let blanked = null;
575
- let position = 0;
576
- const references = new DelimitedReferences(document);
577
- while (position < document.length) {
578
- const character = document[position];
579
- if (QUOTES.includes(character) && follows(document, position, BEFORE_REFERENCE)) {
580
- const end = references.endAt(position);
581
- position = end === null ? position + 1 : end;
582
- continue;
583
- }
584
- if (character === COMMENT && follows(document, position, BEFORE_COMMENT)) {
585
- blanked = blanked ?? document.split("");
586
- while (position < document.length && document[position] !== `
587
- ` && document[position] !== "\r") {
588
- blanked[position] = " ";
589
- position++;
590
- }
591
- continue;
592
- }
593
- position++;
594
- }
595
- return blanked === null ? document : blanked.join("");
596
- }
597
- function follows(document, position, allowed) {
598
- return position === 0 || allowed.includes(document[position - 1]);
599
- }
600
-
601
777
  // src/parser-generated.js
602
778
  class peg$SyntaxError extends SyntaxError {
603
779
  constructor(message, expected, found, location) {
@@ -789,8 +965,8 @@ function peg$parse(input, options) {
789
965
  function peg$f11(l) {
790
966
  return l;
791
967
  }
792
- function peg$f12(i) {
793
- return { id: i };
968
+ function peg$f12(r) {
969
+ return { id: r.value, rawId: r.raw };
794
970
  }
795
971
  function peg$f13(g, rest) {
796
972
  return rest === null ? g : { values: [g].concat(rest) };
@@ -834,27 +1010,31 @@ function peg$parse(input, options) {
834
1010
  return list;
835
1011
  }
836
1012
  function peg$f26(id, v) {
837
- return { id, values: v };
1013
+ return { id: id.value, rawId: id.raw, values: v };
838
1014
  }
839
1015
  function peg$f27(v) {
840
1016
  return { values: v };
841
1017
  }
842
1018
  function peg$f28(id) {
843
- return { id, values: [] };
1019
+ return { id: id.value, rawId: id.raw, values: [] };
1020
+ }
1021
+ function peg$f29(value) {
1022
+ const { start, end } = range();
1023
+ return { value, raw: source.slice(start, end) };
844
1024
  }
845
- function peg$f29() {
1025
+ function peg$f30() {
846
1026
  return hasReferenceLiteralPrefix(input, offset());
847
1027
  }
848
- function peg$f30(literal) {
1028
+ function peg$f31(literal) {
849
1029
  return decodeReferenceLiteral(literal);
850
1030
  }
851
- function peg$f31() {
1031
+ function peg$f32() {
852
1032
  return !hasReferenceLiteralPrefix(input, offset());
853
1033
  }
854
- function peg$f32(chars) {
1034
+ function peg$f33(chars) {
855
1035
  return chars.join("");
856
1036
  }
857
- function peg$f33() {
1037
+ function peg$f34() {
858
1038
  const pos = offset();
859
1039
  const result = parseQuotedStringAt(input, pos, '"');
860
1040
  if (result) {
@@ -864,19 +1044,19 @@ function peg$parse(input, options) {
864
1044
  }
865
1045
  return false;
866
1046
  }
867
- function peg$f34(chars) {
1047
+ function peg$f35(chars) {
868
1048
  return parsedValue;
869
1049
  }
870
- function peg$f35(c, cs) {
1050
+ function peg$f36(c, cs) {
871
1051
  return [c].concat(cs).join("");
872
1052
  }
873
- function peg$f36() {
1053
+ function peg$f37() {
874
1054
  return parsedLength > 1 && (parsedLength--, true);
875
1055
  }
876
- function peg$f37(c) {
1056
+ function peg$f38(c) {
877
1057
  return c;
878
1058
  }
879
- function peg$f38() {
1059
+ function peg$f39() {
880
1060
  const pos = offset();
881
1061
  const result = parseQuotedStringAt(input, pos, "'");
882
1062
  if (result) {
@@ -886,19 +1066,19 @@ function peg$parse(input, options) {
886
1066
  }
887
1067
  return false;
888
1068
  }
889
- function peg$f39(chars) {
1069
+ function peg$f40(chars) {
890
1070
  return parsedValue;
891
1071
  }
892
- function peg$f40(c, cs) {
1072
+ function peg$f41(c, cs) {
893
1073
  return [c].concat(cs).join("");
894
1074
  }
895
- function peg$f41() {
1075
+ function peg$f42() {
896
1076
  return parsedLength > 1 && (parsedLength--, true);
897
1077
  }
898
- function peg$f42(c) {
1078
+ function peg$f43(c) {
899
1079
  return c;
900
1080
  }
901
- function peg$f43() {
1081
+ function peg$f44() {
902
1082
  const pos = offset();
903
1083
  const result = parseQuotedStringAt(input, pos, "`");
904
1084
  if (result) {
@@ -908,34 +1088,34 @@ function peg$parse(input, options) {
908
1088
  }
909
1089
  return false;
910
1090
  }
911
- function peg$f44(chars) {
1091
+ function peg$f45(chars) {
912
1092
  return parsedValue;
913
1093
  }
914
- function peg$f45(c, cs) {
1094
+ function peg$f46(c, cs) {
915
1095
  return [c].concat(cs).join("");
916
1096
  }
917
- function peg$f46() {
1097
+ function peg$f47() {
918
1098
  return parsedLength > 1 && (parsedLength--, true);
919
1099
  }
920
- function peg$f47(c) {
1100
+ function peg$f48(c) {
921
1101
  return c;
922
1102
  }
923
- function peg$f48() {
1103
+ function peg$f49() {
924
1104
  return indentationStack.slice();
925
1105
  }
926
- function peg$f49(spaces) {
1106
+ function peg$f50(spaces) {
927
1107
  setBaseIndentation(spaces);
928
1108
  }
929
- function peg$f50(spaces) {
1109
+ function peg$f51(spaces) {
930
1110
  return normalizeIndentation(spaces) > getCurrentIndentation();
931
1111
  }
932
- function peg$f51(spaces) {
1112
+ function peg$f52(spaces) {
933
1113
  pushIndentation(spaces);
934
1114
  }
935
- function peg$f52(spaces) {
1115
+ function peg$f53(spaces) {
936
1116
  return checkIndentation(spaces);
937
1117
  }
938
- function peg$f53() {
1118
+ function peg$f54() {
939
1119
  return isInsideNestedContext();
940
1120
  }
941
1121
  let peg$currPos = options.peg$currPos | 0;
@@ -1781,6 +1961,17 @@ function peg$parse(input, options) {
1781
1961
  return s0;
1782
1962
  }
1783
1963
  function peg$parsereference() {
1964
+ let s0, s1;
1965
+ s0 = peg$currPos;
1966
+ s1 = peg$parsereferenceValue();
1967
+ if (s1 !== peg$FAILED) {
1968
+ peg$savedPos = s0;
1969
+ s1 = peg$f29(s1);
1970
+ }
1971
+ s0 = s1;
1972
+ return s0;
1973
+ }
1974
+ function peg$parsereferenceValue() {
1784
1975
  let s0;
1785
1976
  s0 = peg$parsereferenceLiteral();
1786
1977
  if (s0 === peg$FAILED) {
@@ -1814,7 +2005,7 @@ function peg$parse(input, options) {
1814
2005
  }
1815
2006
  if (s1 !== peg$FAILED) {
1816
2007
  peg$savedPos = peg$currPos;
1817
- s2 = peg$f29();
2008
+ s2 = peg$f30();
1818
2009
  if (s2) {
1819
2010
  s2 = undefined;
1820
2011
  } else {
@@ -1927,7 +2118,7 @@ function peg$parse(input, options) {
1927
2118
  }
1928
2119
  if (s3 !== peg$FAILED) {
1929
2120
  peg$savedPos = s0;
1930
- s0 = peg$f30(s3);
2121
+ s0 = peg$f31(s3);
1931
2122
  } else {
1932
2123
  peg$currPos = s0;
1933
2124
  s0 = peg$FAILED;
@@ -1946,7 +2137,7 @@ function peg$parse(input, options) {
1946
2137
  let s0, s1, s2, s3;
1947
2138
  s0 = peg$currPos;
1948
2139
  peg$savedPos = peg$currPos;
1949
- s1 = peg$f31();
2140
+ s1 = peg$f32();
1950
2141
  if (s1) {
1951
2142
  s1 = undefined;
1952
2143
  } else {
@@ -1965,7 +2156,7 @@ function peg$parse(input, options) {
1965
2156
  }
1966
2157
  if (s2 !== peg$FAILED) {
1967
2158
  peg$savedPos = s0;
1968
- s0 = peg$f32(s2);
2159
+ s0 = peg$f33(s2);
1969
2160
  } else {
1970
2161
  peg$currPos = s0;
1971
2162
  s0 = peg$FAILED;
@@ -2010,7 +2201,7 @@ function peg$parse(input, options) {
2010
2201
  }
2011
2202
  if (s1 !== peg$FAILED) {
2012
2203
  peg$savedPos = peg$currPos;
2013
- s2 = peg$f33();
2204
+ s2 = peg$f34();
2014
2205
  if (s2) {
2015
2206
  s2 = undefined;
2016
2207
  } else {
@@ -2020,7 +2211,7 @@ function peg$parse(input, options) {
2020
2211
  s3 = peg$parseconsumeDouble();
2021
2212
  if (s3 !== peg$FAILED) {
2022
2213
  peg$savedPos = s0;
2023
- s0 = peg$f34(s3);
2214
+ s0 = peg$f35(s3);
2024
2215
  } else {
2025
2216
  peg$currPos = s0;
2026
2217
  s0 = peg$FAILED;
@@ -2055,7 +2246,7 @@ function peg$parse(input, options) {
2055
2246
  s3 = peg$parseconsumeDoubleMore();
2056
2247
  }
2057
2248
  peg$savedPos = s0;
2058
- s0 = peg$f35(s1, s2);
2249
+ s0 = peg$f36(s1, s2);
2059
2250
  } else {
2060
2251
  peg$currPos = s0;
2061
2252
  s0 = peg$FAILED;
@@ -2066,7 +2257,7 @@ function peg$parse(input, options) {
2066
2257
  let s0, s1, s2;
2067
2258
  s0 = peg$currPos;
2068
2259
  peg$savedPos = peg$currPos;
2069
- s1 = peg$f36();
2260
+ s1 = peg$f37();
2070
2261
  if (s1) {
2071
2262
  s1 = undefined;
2072
2263
  } else {
@@ -2084,7 +2275,7 @@ function peg$parse(input, options) {
2084
2275
  }
2085
2276
  if (s2 !== peg$FAILED) {
2086
2277
  peg$savedPos = s0;
2087
- s0 = peg$f37(s2);
2278
+ s0 = peg$f38(s2);
2088
2279
  } else {
2089
2280
  peg$currPos = s0;
2090
2281
  s0 = peg$FAILED;
@@ -2118,7 +2309,7 @@ function peg$parse(input, options) {
2118
2309
  }
2119
2310
  if (s1 !== peg$FAILED) {
2120
2311
  peg$savedPos = peg$currPos;
2121
- s2 = peg$f38();
2312
+ s2 = peg$f39();
2122
2313
  if (s2) {
2123
2314
  s2 = undefined;
2124
2315
  } else {
@@ -2128,7 +2319,7 @@ function peg$parse(input, options) {
2128
2319
  s3 = peg$parseconsumeSingle();
2129
2320
  if (s3 !== peg$FAILED) {
2130
2321
  peg$savedPos = s0;
2131
- s0 = peg$f39(s3);
2322
+ s0 = peg$f40(s3);
2132
2323
  } else {
2133
2324
  peg$currPos = s0;
2134
2325
  s0 = peg$FAILED;
@@ -2163,7 +2354,7 @@ function peg$parse(input, options) {
2163
2354
  s3 = peg$parseconsumeSingleMore();
2164
2355
  }
2165
2356
  peg$savedPos = s0;
2166
- s0 = peg$f40(s1, s2);
2357
+ s0 = peg$f41(s1, s2);
2167
2358
  } else {
2168
2359
  peg$currPos = s0;
2169
2360
  s0 = peg$FAILED;
@@ -2174,7 +2365,7 @@ function peg$parse(input, options) {
2174
2365
  let s0, s1, s2;
2175
2366
  s0 = peg$currPos;
2176
2367
  peg$savedPos = peg$currPos;
2177
- s1 = peg$f41();
2368
+ s1 = peg$f42();
2178
2369
  if (s1) {
2179
2370
  s1 = undefined;
2180
2371
  } else {
@@ -2192,7 +2383,7 @@ function peg$parse(input, options) {
2192
2383
  }
2193
2384
  if (s2 !== peg$FAILED) {
2194
2385
  peg$savedPos = s0;
2195
- s0 = peg$f42(s2);
2386
+ s0 = peg$f43(s2);
2196
2387
  } else {
2197
2388
  peg$currPos = s0;
2198
2389
  s0 = peg$FAILED;
@@ -2226,7 +2417,7 @@ function peg$parse(input, options) {
2226
2417
  }
2227
2418
  if (s1 !== peg$FAILED) {
2228
2419
  peg$savedPos = peg$currPos;
2229
- s2 = peg$f43();
2420
+ s2 = peg$f44();
2230
2421
  if (s2) {
2231
2422
  s2 = undefined;
2232
2423
  } else {
@@ -2236,7 +2427,7 @@ function peg$parse(input, options) {
2236
2427
  s3 = peg$parseconsumeBacktick();
2237
2428
  if (s3 !== peg$FAILED) {
2238
2429
  peg$savedPos = s0;
2239
- s0 = peg$f44(s3);
2430
+ s0 = peg$f45(s3);
2240
2431
  } else {
2241
2432
  peg$currPos = s0;
2242
2433
  s0 = peg$FAILED;
@@ -2271,7 +2462,7 @@ function peg$parse(input, options) {
2271
2462
  s3 = peg$parseconsumeBacktickMore();
2272
2463
  }
2273
2464
  peg$savedPos = s0;
2274
- s0 = peg$f45(s1, s2);
2465
+ s0 = peg$f46(s1, s2);
2275
2466
  } else {
2276
2467
  peg$currPos = s0;
2277
2468
  s0 = peg$FAILED;
@@ -2282,7 +2473,7 @@ function peg$parse(input, options) {
2282
2473
  let s0, s1, s2;
2283
2474
  s0 = peg$currPos;
2284
2475
  peg$savedPos = peg$currPos;
2285
- s1 = peg$f46();
2476
+ s1 = peg$f47();
2286
2477
  if (s1) {
2287
2478
  s1 = undefined;
2288
2479
  } else {
@@ -2300,7 +2491,7 @@ function peg$parse(input, options) {
2300
2491
  }
2301
2492
  if (s2 !== peg$FAILED) {
2302
2493
  peg$savedPos = s0;
2303
- s0 = peg$f47(s2);
2494
+ s0 = peg$f48(s2);
2304
2495
  } else {
2305
2496
  peg$currPos = s0;
2306
2497
  s0 = peg$FAILED;
@@ -2316,7 +2507,7 @@ function peg$parse(input, options) {
2316
2507
  s0 = peg$currPos;
2317
2508
  s1 = "";
2318
2509
  peg$savedPos = s0;
2319
- s1 = peg$f48();
2510
+ s1 = peg$f49();
2320
2511
  s0 = s1;
2321
2512
  return s0;
2322
2513
  }
@@ -2346,7 +2537,7 @@ function peg$parse(input, options) {
2346
2537
  }
2347
2538
  }
2348
2539
  peg$savedPos = s0;
2349
- s1 = peg$f49(s1);
2540
+ s1 = peg$f50(s1);
2350
2541
  s0 = s1;
2351
2542
  return s0;
2352
2543
  }
@@ -2376,7 +2567,7 @@ function peg$parse(input, options) {
2376
2567
  }
2377
2568
  }
2378
2569
  peg$savedPos = peg$currPos;
2379
- s2 = peg$f50(s1);
2570
+ s2 = peg$f51(s1);
2380
2571
  if (s2) {
2381
2572
  s2 = undefined;
2382
2573
  } else {
@@ -2384,7 +2575,7 @@ function peg$parse(input, options) {
2384
2575
  }
2385
2576
  if (s2 !== peg$FAILED) {
2386
2577
  peg$savedPos = s0;
2387
- s0 = peg$f51(s1);
2578
+ s0 = peg$f52(s1);
2388
2579
  } else {
2389
2580
  peg$currPos = s0;
2390
2581
  s0 = peg$FAILED;
@@ -2417,7 +2608,7 @@ function peg$parse(input, options) {
2417
2608
  }
2418
2609
  }
2419
2610
  peg$savedPos = peg$currPos;
2420
- s2 = peg$f52(s1);
2611
+ s2 = peg$f53(s1);
2421
2612
  if (s2) {
2422
2613
  s2 = undefined;
2423
2614
  } else {
@@ -2628,7 +2819,7 @@ function peg$parse(input, options) {
2628
2819
  let s0, s1, s2, s3;
2629
2820
  s0 = peg$currPos;
2630
2821
  peg$savedPos = peg$currPos;
2631
- s1 = peg$f53();
2822
+ s1 = peg$f54();
2632
2823
  if (s1) {
2633
2824
  s1 = undefined;
2634
2825
  } else {
@@ -2755,6 +2946,7 @@ function peg$parse(input, options) {
2755
2946
  let contextStack = [];
2756
2947
  let contextDepth = 0;
2757
2948
  const maxDepth = options.maxDepth ?? Infinity;
2949
+ const source = options.source ?? input;
2758
2950
  let unreadableLines = new Set;
2759
2951
  function resetState() {
2760
2952
  indentationStack = [0];
@@ -2870,6 +3062,7 @@ class Parser {
2870
3062
  this.maxInputSize = options.maxInputSize || 10 * 1024 * 1024;
2871
3063
  this.maxDepth = options.maxDepth ?? DEFAULT_MAX_DEPTH;
2872
3064
  this.comments = options.comments ?? true;
3065
+ this.syntax = options.syntax ?? true;
2873
3066
  }
2874
3067
  parse(input) {
2875
3068
  return this._parse(input, (raw) => this.transformResult(raw));
@@ -2877,6 +3070,10 @@ class Parser {
2877
3070
  parseGroups(input) {
2878
3071
  return this._parse(input, (raw) => raw.map((item) => this._transformGroup(item)));
2879
3072
  }
3073
+ parseCst(input) {
3074
+ this.parse(input);
3075
+ return buildCst(input, { comments: this.comments });
3076
+ }
2880
3077
  _transformGroup(item) {
2881
3078
  return new LinksGroup(this.transformLink(item), (item.children || []).map((child) => this._transformGroup(child)));
2882
3079
  }
@@ -2890,7 +3087,8 @@ class Parser {
2890
3087
  const prepared = this.comments ? stripComments(input) : input;
2891
3088
  try {
2892
3089
  const rawResult = peg$parse(prepared, {
2893
- maxDepth: this.maxDepth
3090
+ maxDepth: this.maxDepth,
3091
+ source: input
2894
3092
  });
2895
3093
  return transform(rawResult);
2896
3094
  } catch (error) {
@@ -2920,6 +3118,7 @@ class Parser {
2920
3118
  const childValues = item.children.map((child) => this.transformIndentedValue(child));
2921
3119
  const linkWithChildren = {
2922
3120
  id: item.id,
3121
+ rawId: item.rawId,
2923
3122
  values: childValues
2924
3123
  };
2925
3124
  const currentLink = this.transformLink(linkWithChildren);
@@ -2952,14 +3151,14 @@ class Parser {
2952
3151
  transformIndentedValue(item) {
2953
3152
  const children = item.children || [];
2954
3153
  if (children.length && item.id != null && !item.values?.length) {
2955
- return new Link(item.id, children.map((child) => this.transformIndentedValue(child)));
3154
+ return this._withRawId(new Link(item.id, children.map((child) => this.transformIndentedValue(child))), item);
2956
3155
  }
2957
3156
  const current = this.transformLink(item);
2958
3157
  if (children.length) {
2959
- return new Link(current.id, [
3158
+ return this._withRawId(new Link(current.id, [
2960
3159
  ...current.values,
2961
3160
  ...children.map((child) => this.transformIndentedValue(child))
2962
- ]);
3161
+ ]), item);
2963
3162
  }
2964
3163
  if (item.id == null && item.nested === undefined && current.values.length === 1) {
2965
3164
  return current.values[0];
@@ -3004,14 +3203,20 @@ class Parser {
3004
3203
  return this.transformNested(item.nested);
3005
3204
  }
3006
3205
  if (item.id !== undefined && !item.values && !item.children) {
3007
- return new Link(item.id);
3206
+ return this._withRawId(new Link(item.id), item);
3008
3207
  }
3009
3208
  if (item.values && Array.isArray(item.values)) {
3010
- const link = new Link(item.id ?? null, []);
3209
+ const link = this._withRawId(new Link(item.id ?? null, []), item);
3011
3210
  link.values = item.values.map((v) => this.transformLink(v));
3012
3211
  return link;
3013
3212
  }
3014
- return new Link(item.id ?? null, []);
3213
+ return this._withRawId(new Link(item.id ?? null, []), item);
3214
+ }
3215
+ _withRawId(link, item) {
3216
+ if (this.syntax && link.id !== null && typeof item.rawId === "string") {
3217
+ link.rawId = item.rawId;
3218
+ }
3219
+ return link;
3015
3220
  }
3016
3221
  }
3017
3222
 
@@ -3072,8 +3277,104 @@ function formatIndentedDocument(entries) {
3072
3277
  `) + `
3073
3278
  ` : "";
3074
3279
  }
3280
+ // src/Emitter.js
3281
+ class Emitter {
3282
+ #listeners = new Map;
3283
+ on(event, listener) {
3284
+ return this.#add(event, listener, false, false);
3285
+ }
3286
+ addListener(event, listener) {
3287
+ return this.on(event, listener);
3288
+ }
3289
+ prependListener(event, listener) {
3290
+ return this.#add(event, listener, false, true);
3291
+ }
3292
+ once(event, listener) {
3293
+ return this.#add(event, listener, true, false);
3294
+ }
3295
+ prependOnceListener(event, listener) {
3296
+ return this.#add(event, listener, true, true);
3297
+ }
3298
+ off(event, listener) {
3299
+ checkListener(listener);
3300
+ const entries = this.#listeners.get(event);
3301
+ if (!entries)
3302
+ return this;
3303
+ const entry = entries.findLast((entry) => entry.listener === listener);
3304
+ if (entry)
3305
+ this.#remove(event, entry);
3306
+ return this;
3307
+ }
3308
+ removeListener(event, listener) {
3309
+ return this.off(event, listener);
3310
+ }
3311
+ removeAllListeners(event) {
3312
+ if (event === undefined) {
3313
+ this.#listeners.clear();
3314
+ } else {
3315
+ this.#listeners.delete(event);
3316
+ }
3317
+ return this;
3318
+ }
3319
+ emit(event, ...arguments_) {
3320
+ const entries = this.#listeners.get(event);
3321
+ if (!entries || entries.length === 0) {
3322
+ if (event === "error") {
3323
+ const error = arguments_[0];
3324
+ if (error instanceof Error)
3325
+ throw error;
3326
+ throw new Error(`Unhandled error. (${String(error)})`);
3327
+ }
3328
+ return false;
3329
+ }
3330
+ for (const entry of [...entries]) {
3331
+ if (entry.once)
3332
+ this.#remove(event, entry);
3333
+ entry.listener.apply(this, arguments_);
3334
+ }
3335
+ return true;
3336
+ }
3337
+ listeners(event) {
3338
+ return (this.#listeners.get(event) ?? []).map((entry) => entry.listener);
3339
+ }
3340
+ listenerCount(event) {
3341
+ return this.#listeners.get(event)?.length ?? 0;
3342
+ }
3343
+ eventNames() {
3344
+ return [...this.#listeners.keys()];
3345
+ }
3346
+ #remove(event, entry) {
3347
+ const entries = this.#listeners.get(event);
3348
+ const index = entries?.indexOf(entry) ?? -1;
3349
+ if (index === -1)
3350
+ return;
3351
+ entries.splice(index, 1);
3352
+ if (entries.length === 0)
3353
+ this.#listeners.delete(event);
3354
+ }
3355
+ #add(event, listener, once, prepend) {
3356
+ checkListener(listener);
3357
+ let entries = this.#listeners.get(event);
3358
+ if (!entries) {
3359
+ entries = [];
3360
+ this.#listeners.set(event, entries);
3361
+ }
3362
+ const entry = { listener, once };
3363
+ if (prepend) {
3364
+ entries.unshift(entry);
3365
+ } else {
3366
+ entries.push(entry);
3367
+ }
3368
+ return this;
3369
+ }
3370
+ }
3371
+ function checkListener(listener) {
3372
+ if (typeof listener !== "function") {
3373
+ throw new TypeError(`The "listener" argument must be a function. Received ${typeof listener}`);
3374
+ }
3375
+ }
3376
+
3075
3377
  // src/StreamParser.js
3076
- import { EventEmitter } from "events";
3077
3378
  var DEFAULT_MAX_BUFFER_SIZE = 10 * 1024 * 1024;
3078
3379
 
3079
3380
  class StreamParseError extends Error {
@@ -3094,7 +3395,7 @@ class StreamParseError extends Error {
3094
3395
  }
3095
3396
  }
3096
3397
 
3097
- class StreamParser extends EventEmitter {
3398
+ class StreamParser extends Emitter {
3098
3399
  constructor(options = {}) {
3099
3400
  super();
3100
3401
  this.parser = options.parser ?? new Parser(options);
@@ -3889,6 +4190,8 @@ export {
3889
4190
  ArityRange,
3890
4191
  BinaryLinoCodec,
3891
4192
  BinaryLinoOptions,
4193
+ CstKind,
4194
+ CstNode,
3892
4195
  DEFAULT_MAX_DEPTH,
3893
4196
  DecodeLimits,
3894
4197
  External,
@@ -3911,6 +4214,7 @@ export {
3911
4214
  formatIndentedDocument,
3912
4215
  formatLinks,
3913
4216
  parseIndentedDocument,
4217
+ referenceQuote,
3914
4218
  stripComments,
3915
4219
  unescapeReference
3916
4220
  };