links-notation 0.8.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 ADDED
@@ -0,0 +1,1462 @@
1
+ // src/Link.js
2
+ class Link {
3
+ constructor(id = null, values = null) {
4
+ this.id = id;
5
+ this.values = values || [];
6
+ }
7
+ toString() {
8
+ return this.format(false);
9
+ }
10
+ getValuesString() {
11
+ return !this.values || this.values.length === 0 ? "" : this.values.map((v) => Link.getValueString(v)).join(" ");
12
+ }
13
+ simplify() {
14
+ if (!this.values || this.values.length === 0) {
15
+ return this;
16
+ } else if (this.values.length === 1) {
17
+ return this.values[0];
18
+ } else {
19
+ const newValues = this.values.map((v) => v.simplify());
20
+ return new Link(this.id, newValues);
21
+ }
22
+ }
23
+ combine(other) {
24
+ return new Link(null, [this, other]);
25
+ }
26
+ static getValueString(value) {
27
+ return value.toLinkOrIdString();
28
+ }
29
+ static escapeReference(reference) {
30
+ if (!reference || reference.trim() === "") {
31
+ return "";
32
+ }
33
+ const needsSingleQuotes = reference.includes(":") || reference.includes("(") || reference.includes(")") || reference.includes(" ") || reference.includes("\t") || reference.includes(`
34
+ `) || reference.includes("\r") || reference.includes('"');
35
+ if (needsSingleQuotes) {
36
+ return `'${reference}'`;
37
+ } else if (reference.includes("'")) {
38
+ return `"${reference}"`;
39
+ } else {
40
+ return reference;
41
+ }
42
+ }
43
+ toLinkOrIdString() {
44
+ if (!this.values || this.values.length === 0) {
45
+ return this.id === null ? "" : Link.escapeReference(this.id);
46
+ }
47
+ return this.toString();
48
+ }
49
+ equals(other) {
50
+ if (!(other instanceof Link))
51
+ return false;
52
+ if (this.id !== other.id)
53
+ return false;
54
+ if (this.values.length !== other.values.length)
55
+ return false;
56
+ for (let i = 0;i < this.values.length; i++) {
57
+ if (!this.values[i].equals(other.values[i])) {
58
+ return false;
59
+ }
60
+ }
61
+ return true;
62
+ }
63
+ format(lessParentheses = false, isCompoundValue = false) {
64
+ if (this.id === null && (!this.values || this.values.length === 0)) {
65
+ return lessParentheses ? "" : "()";
66
+ }
67
+ if (!this.values || this.values.length === 0) {
68
+ const escapedId = Link.escapeReference(this.id);
69
+ if (isCompoundValue) {
70
+ return `(${escapedId})`;
71
+ }
72
+ return lessParentheses && !this.needsParentheses(this.id) ? escapedId : `(${escapedId})`;
73
+ }
74
+ const valuesStr = this.values.map((v) => this.formatValue(v)).join(" ");
75
+ if (this.id === null) {
76
+ if (lessParentheses) {
77
+ const allSimple = this.values.every((v) => !v.values || v.values.length === 0);
78
+ if (allSimple) {
79
+ const simpleValuesStr = this.values.map((v) => Link.escapeReference(v.id)).join(" ");
80
+ return simpleValuesStr;
81
+ }
82
+ return valuesStr;
83
+ }
84
+ return `(${valuesStr})`;
85
+ }
86
+ const idStr = Link.escapeReference(this.id);
87
+ const withColon = `${idStr}: ${valuesStr}`;
88
+ return lessParentheses && !this.needsParentheses(this.id) ? withColon : `(${withColon})`;
89
+ }
90
+ formatValue(value) {
91
+ if (!value.format) {
92
+ return Link.escapeReference(value.id || "");
93
+ }
94
+ const isCompoundFromPaths = this._isFromPathCombination === true;
95
+ if (isCompoundFromPaths) {
96
+ return value.format(false, true);
97
+ }
98
+ if (!value.values || value.values.length === 0) {
99
+ return Link.escapeReference(value.id);
100
+ }
101
+ return value.format(false, false);
102
+ }
103
+ needsParentheses(str) {
104
+ return str && (str.includes(" ") || str.includes(":") || str.includes("(") || str.includes(")"));
105
+ }
106
+ }
107
+ function formatLinks(links, lessParentheses = false) {
108
+ if (!links || links.length === 0)
109
+ return "";
110
+ return links.map((link) => link.format(lessParentheses)).join(`
111
+ `);
112
+ }
113
+ // src/LinksGroup.js
114
+ class LinksGroup {
115
+ constructor(element, children = []) {
116
+ this.element = element;
117
+ this.children = children;
118
+ }
119
+ toList() {
120
+ const result = [];
121
+ this._appendToList(result);
122
+ return result;
123
+ }
124
+ _appendToList(list) {
125
+ list.push(this.element);
126
+ if (this.children && this.children.length > 0) {
127
+ for (const child of this.children) {
128
+ if (child instanceof LinksGroup) {
129
+ child._appendToList(list);
130
+ } else {
131
+ list.push(child);
132
+ }
133
+ }
134
+ }
135
+ }
136
+ toString() {
137
+ const list = this.toList();
138
+ return list.map((item) => `(${item.id || item})`).join(" ");
139
+ }
140
+ }
141
+ // src/parser-generated.js
142
+ class peg$SyntaxError extends SyntaxError {
143
+ constructor(message, expected, found, location) {
144
+ super(message);
145
+ this.expected = expected;
146
+ this.found = found;
147
+ this.location = location;
148
+ this.name = "SyntaxError";
149
+ }
150
+ format(sources) {
151
+ let str = "Error: " + this.message;
152
+ if (this.location) {
153
+ let src = null;
154
+ const st = sources.find((s2) => s2.source === this.location.source);
155
+ if (st) {
156
+ src = st.text.split(/\r\n|\n|\r/g);
157
+ }
158
+ const s = this.location.start;
159
+ const offset_s = this.location.source && typeof this.location.source.offset === "function" ? this.location.source.offset(s) : s;
160
+ const loc = this.location.source + ":" + offset_s.line + ":" + offset_s.column;
161
+ if (src) {
162
+ const e = this.location.end;
163
+ const filler = "".padEnd(offset_s.line.toString().length, " ");
164
+ const line = src[s.line - 1];
165
+ const last = s.line === e.line ? e.column : line.length + 1;
166
+ const hatLen = last - s.column || 1;
167
+ str += `
168
+ --> ` + loc + `
169
+ ` + filler + ` |
170
+ ` + offset_s.line + " | " + line + `
171
+ ` + filler + " | " + "".padEnd(s.column - 1, " ") + "".padEnd(hatLen, "^");
172
+ } else {
173
+ str += `
174
+ at ` + loc;
175
+ }
176
+ }
177
+ return str;
178
+ }
179
+ static buildMessage(expected, found) {
180
+ function hex(ch) {
181
+ return ch.codePointAt(0).toString(16).toUpperCase();
182
+ }
183
+ const nonPrintable = Object.prototype.hasOwnProperty.call(RegExp.prototype, "unicode") ? new RegExp("[\\p{C}\\p{Mn}\\p{Mc}]", "gu") : null;
184
+ function unicodeEscape(s) {
185
+ if (nonPrintable) {
186
+ return s.replace(nonPrintable, (ch) => "\\u{" + hex(ch) + "}");
187
+ }
188
+ return s;
189
+ }
190
+ function literalEscape(s) {
191
+ 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)));
192
+ }
193
+ function classEscape(s) {
194
+ 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)));
195
+ }
196
+ const DESCRIBE_EXPECTATION_FNS = {
197
+ literal(expectation) {
198
+ return '"' + literalEscape(expectation.text) + '"';
199
+ },
200
+ class(expectation) {
201
+ const escapedParts = expectation.parts.map((part) => Array.isArray(part) ? classEscape(part[0]) + "-" + classEscape(part[1]) : classEscape(part));
202
+ return "[" + (expectation.inverted ? "^" : "") + escapedParts.join("") + "]" + (expectation.unicode ? "u" : "");
203
+ },
204
+ any() {
205
+ return "any character";
206
+ },
207
+ end() {
208
+ return "end of input";
209
+ },
210
+ other(expectation) {
211
+ return expectation.description;
212
+ }
213
+ };
214
+ function describeExpectation(expectation) {
215
+ return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
216
+ }
217
+ function describeExpected(expected2) {
218
+ const descriptions = expected2.map(describeExpectation);
219
+ descriptions.sort();
220
+ if (descriptions.length > 0) {
221
+ let j = 1;
222
+ for (let i = 1;i < descriptions.length; i++) {
223
+ if (descriptions[i - 1] !== descriptions[i]) {
224
+ descriptions[j] = descriptions[i];
225
+ j++;
226
+ }
227
+ }
228
+ descriptions.length = j;
229
+ }
230
+ switch (descriptions.length) {
231
+ case 1:
232
+ return descriptions[0];
233
+ case 2:
234
+ return descriptions[0] + " or " + descriptions[1];
235
+ default:
236
+ return descriptions.slice(0, -1).join(", ") + ", or " + descriptions[descriptions.length - 1];
237
+ }
238
+ }
239
+ function describeFound(found2) {
240
+ return found2 ? '"' + literalEscape(found2) + '"' : "end of input";
241
+ }
242
+ return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found.";
243
+ }
244
+ }
245
+ function peg$parse(input, options) {
246
+ options = options !== undefined ? options : {};
247
+ const peg$FAILED = {};
248
+ const peg$source = options.grammarSource;
249
+ const peg$startRuleFunctions = {
250
+ document: peg$parsedocument
251
+ };
252
+ let peg$startRuleFunction = peg$parsedocument;
253
+ const peg$c0 = ":";
254
+ const peg$c1 = "(";
255
+ const peg$c2 = ")";
256
+ const peg$c3 = '"';
257
+ const peg$c4 = "'";
258
+ const peg$c5 = " ";
259
+ const peg$r0 = /^[^"]/;
260
+ const peg$r1 = /^[^']/;
261
+ const peg$r2 = /^[\r\n]/;
262
+ const peg$r3 = /^[ \t]/;
263
+ const peg$r4 = /^[ \t\n\r]/;
264
+ const peg$r5 = /^[^ \t\n\r(:)]/;
265
+ const peg$e0 = peg$literalExpectation(":", false);
266
+ const peg$e1 = peg$literalExpectation("(", false);
267
+ const peg$e2 = peg$literalExpectation(")", false);
268
+ const peg$e3 = peg$literalExpectation('"', false);
269
+ const peg$e4 = peg$classExpectation(['"'], true, false, false);
270
+ const peg$e5 = peg$literalExpectation("'", false);
271
+ const peg$e6 = peg$classExpectation(["'"], true, false, false);
272
+ const peg$e7 = peg$literalExpectation(" ", false);
273
+ const peg$e8 = peg$classExpectation(["\r", `
274
+ `], false, false, false);
275
+ const peg$e9 = peg$anyExpectation();
276
+ const peg$e10 = peg$classExpectation([" ", "\t"], false, false, false);
277
+ const peg$e11 = peg$classExpectation([" ", "\t", `
278
+ `, "\r"], false, false, false);
279
+ const peg$e12 = peg$classExpectation([" ", "\t", `
280
+ `, "\r", "(", ":", ")"], true, false, false);
281
+ function peg$f0(links) {
282
+ return links;
283
+ }
284
+ function peg$f1() {
285
+ return [];
286
+ }
287
+ function peg$f2(fl, list) {
288
+ popIndentation();
289
+ return [fl].concat(list || []);
290
+ }
291
+ function peg$f3(l) {
292
+ return l;
293
+ }
294
+ function peg$f4(l) {
295
+ return l;
296
+ }
297
+ function peg$f5(e, l) {
298
+ return { id: e.id, values: e.values, children: l };
299
+ }
300
+ function peg$f6(e) {
301
+ return e;
302
+ }
303
+ function peg$f7(l) {
304
+ return l;
305
+ }
306
+ function peg$f8(i) {
307
+ return { id: i };
308
+ }
309
+ function peg$f9(ml) {
310
+ return ml;
311
+ }
312
+ function peg$f10(il) {
313
+ return il;
314
+ }
315
+ function peg$f11(sl) {
316
+ return sl;
317
+ }
318
+ function peg$f12(fl) {
319
+ return fl;
320
+ }
321
+ function peg$f13(vl) {
322
+ return vl;
323
+ }
324
+ function peg$f14(value) {
325
+ return value;
326
+ }
327
+ function peg$f15(list) {
328
+ return list;
329
+ }
330
+ function peg$f16(value) {
331
+ return value;
332
+ }
333
+ function peg$f17(list) {
334
+ return list;
335
+ }
336
+ function peg$f18(id, v) {
337
+ return { id, values: v };
338
+ }
339
+ function peg$f19(id, v) {
340
+ return { id, values: v };
341
+ }
342
+ function peg$f20(v) {
343
+ return { values: v };
344
+ }
345
+ function peg$f21(v) {
346
+ return { values: v };
347
+ }
348
+ function peg$f22(id) {
349
+ return { id, values: [] };
350
+ }
351
+ function peg$f23(chars) {
352
+ return chars.join("");
353
+ }
354
+ function peg$f24(r) {
355
+ return r.join("");
356
+ }
357
+ function peg$f25(r) {
358
+ return r.join("");
359
+ }
360
+ function peg$f26(spaces) {
361
+ return spaces.length > getCurrentIndentation();
362
+ }
363
+ function peg$f27(spaces) {
364
+ pushIndentation(spaces);
365
+ }
366
+ function peg$f28(spaces) {
367
+ return checkIndentation(spaces);
368
+ }
369
+ let peg$currPos = options.peg$currPos | 0;
370
+ let peg$savedPos = peg$currPos;
371
+ const peg$posDetailsCache = [{ line: 1, column: 1 }];
372
+ let peg$maxFailPos = peg$currPos;
373
+ let peg$maxFailExpected = options.peg$maxFailExpected || [];
374
+ let peg$silentFails = options.peg$silentFails | 0;
375
+ let peg$result;
376
+ if (options.startRule) {
377
+ if (!(options.startRule in peg$startRuleFunctions)) {
378
+ throw new Error(`Can't start parsing from rule "` + options.startRule + '".');
379
+ }
380
+ peg$startRuleFunction = peg$startRuleFunctions[options.startRule];
381
+ }
382
+ function text() {
383
+ return input.substring(peg$savedPos, peg$currPos);
384
+ }
385
+ function offset() {
386
+ return peg$savedPos;
387
+ }
388
+ function range() {
389
+ return {
390
+ source: peg$source,
391
+ start: peg$savedPos,
392
+ end: peg$currPos
393
+ };
394
+ }
395
+ function location() {
396
+ return peg$computeLocation(peg$savedPos, peg$currPos);
397
+ }
398
+ function expected(description, location2) {
399
+ location2 = location2 !== undefined ? location2 : peg$computeLocation(peg$savedPos, peg$currPos);
400
+ throw peg$buildStructuredError([peg$otherExpectation(description)], input.substring(peg$savedPos, peg$currPos), location2);
401
+ }
402
+ function error(message, location2) {
403
+ location2 = location2 !== undefined ? location2 : peg$computeLocation(peg$savedPos, peg$currPos);
404
+ throw peg$buildSimpleError(message, location2);
405
+ }
406
+ function peg$getUnicode(pos = peg$currPos) {
407
+ const cp = input.codePointAt(pos);
408
+ if (cp === undefined) {
409
+ return "";
410
+ }
411
+ return String.fromCodePoint(cp);
412
+ }
413
+ function peg$literalExpectation(text2, ignoreCase) {
414
+ return { type: "literal", text: text2, ignoreCase };
415
+ }
416
+ function peg$classExpectation(parts, inverted, ignoreCase, unicode) {
417
+ return { type: "class", parts, inverted, ignoreCase, unicode };
418
+ }
419
+ function peg$anyExpectation() {
420
+ return { type: "any" };
421
+ }
422
+ function peg$endExpectation() {
423
+ return { type: "end" };
424
+ }
425
+ function peg$otherExpectation(description) {
426
+ return { type: "other", description };
427
+ }
428
+ function peg$computePosDetails(pos) {
429
+ let details = peg$posDetailsCache[pos];
430
+ let p;
431
+ if (details) {
432
+ return details;
433
+ } else {
434
+ if (pos >= peg$posDetailsCache.length) {
435
+ p = peg$posDetailsCache.length - 1;
436
+ } else {
437
+ p = pos;
438
+ while (!peg$posDetailsCache[--p]) {}
439
+ }
440
+ details = peg$posDetailsCache[p];
441
+ details = {
442
+ line: details.line,
443
+ column: details.column
444
+ };
445
+ while (p < pos) {
446
+ if (input.charCodeAt(p) === 10) {
447
+ details.line++;
448
+ details.column = 1;
449
+ } else {
450
+ details.column++;
451
+ }
452
+ p++;
453
+ }
454
+ peg$posDetailsCache[pos] = details;
455
+ return details;
456
+ }
457
+ }
458
+ function peg$computeLocation(startPos, endPos, offset2) {
459
+ const startPosDetails = peg$computePosDetails(startPos);
460
+ const endPosDetails = peg$computePosDetails(endPos);
461
+ const res = {
462
+ source: peg$source,
463
+ start: {
464
+ offset: startPos,
465
+ line: startPosDetails.line,
466
+ column: startPosDetails.column
467
+ },
468
+ end: {
469
+ offset: endPos,
470
+ line: endPosDetails.line,
471
+ column: endPosDetails.column
472
+ }
473
+ };
474
+ if (offset2 && peg$source && typeof peg$source.offset === "function") {
475
+ res.start = peg$source.offset(res.start);
476
+ res.end = peg$source.offset(res.end);
477
+ }
478
+ return res;
479
+ }
480
+ function peg$fail(expected2) {
481
+ if (peg$currPos < peg$maxFailPos) {
482
+ return;
483
+ }
484
+ if (peg$currPos > peg$maxFailPos) {
485
+ peg$maxFailPos = peg$currPos;
486
+ peg$maxFailExpected = [];
487
+ }
488
+ peg$maxFailExpected.push(expected2);
489
+ }
490
+ function peg$buildSimpleError(message, location2) {
491
+ return new peg$SyntaxError(message, null, null, location2);
492
+ }
493
+ function peg$buildStructuredError(expected2, found, location2) {
494
+ return new peg$SyntaxError(peg$SyntaxError.buildMessage(expected2, found), expected2, found, location2);
495
+ }
496
+ function peg$parsedocument() {
497
+ let s0, s1, s2, s3;
498
+ s0 = peg$currPos;
499
+ s1 = peg$parse_();
500
+ s2 = peg$parselinks();
501
+ if (s2 !== peg$FAILED) {
502
+ s3 = peg$parseeof();
503
+ if (s3 !== peg$FAILED) {
504
+ peg$savedPos = s0;
505
+ s0 = peg$f0(s2);
506
+ } else {
507
+ peg$currPos = s0;
508
+ s0 = peg$FAILED;
509
+ }
510
+ } else {
511
+ peg$currPos = s0;
512
+ s0 = peg$FAILED;
513
+ }
514
+ if (s0 === peg$FAILED) {
515
+ s0 = peg$currPos;
516
+ s1 = peg$parse_();
517
+ s2 = peg$parseeof();
518
+ if (s2 !== peg$FAILED) {
519
+ peg$savedPos = s0;
520
+ s0 = peg$f1();
521
+ } else {
522
+ peg$currPos = s0;
523
+ s0 = peg$FAILED;
524
+ }
525
+ }
526
+ return s0;
527
+ }
528
+ function peg$parselinks() {
529
+ let s0, s1, s2, s3;
530
+ s0 = peg$currPos;
531
+ s1 = peg$parsefirstLine();
532
+ if (s1 !== peg$FAILED) {
533
+ s2 = [];
534
+ s3 = peg$parseline();
535
+ while (s3 !== peg$FAILED) {
536
+ s2.push(s3);
537
+ s3 = peg$parseline();
538
+ }
539
+ peg$savedPos = s0;
540
+ s0 = peg$f2(s1, s2);
541
+ } else {
542
+ peg$currPos = s0;
543
+ s0 = peg$FAILED;
544
+ }
545
+ return s0;
546
+ }
547
+ function peg$parsefirstLine() {
548
+ let s0, s1;
549
+ s0 = peg$currPos;
550
+ s1 = peg$parseelement();
551
+ if (s1 !== peg$FAILED) {
552
+ peg$savedPos = s0;
553
+ s1 = peg$f3(s1);
554
+ }
555
+ s0 = s1;
556
+ return s0;
557
+ }
558
+ function peg$parseline() {
559
+ let s0, s1, s2;
560
+ s0 = peg$currPos;
561
+ s1 = peg$parseCHECK_INDENTATION();
562
+ if (s1 !== peg$FAILED) {
563
+ s2 = peg$parseelement();
564
+ if (s2 !== peg$FAILED) {
565
+ peg$savedPos = s0;
566
+ s0 = peg$f4(s2);
567
+ } else {
568
+ peg$currPos = s0;
569
+ s0 = peg$FAILED;
570
+ }
571
+ } else {
572
+ peg$currPos = s0;
573
+ s0 = peg$FAILED;
574
+ }
575
+ return s0;
576
+ }
577
+ function peg$parseelement() {
578
+ let s0, s1, s2, s3;
579
+ s0 = peg$currPos;
580
+ s1 = peg$parseanyLink();
581
+ if (s1 !== peg$FAILED) {
582
+ s2 = peg$parsePUSH_INDENTATION();
583
+ if (s2 !== peg$FAILED) {
584
+ s3 = peg$parselinks();
585
+ if (s3 !== peg$FAILED) {
586
+ peg$savedPos = s0;
587
+ s0 = peg$f5(s1, s3);
588
+ } else {
589
+ peg$currPos = s0;
590
+ s0 = peg$FAILED;
591
+ }
592
+ } else {
593
+ peg$currPos = s0;
594
+ s0 = peg$FAILED;
595
+ }
596
+ } else {
597
+ peg$currPos = s0;
598
+ s0 = peg$FAILED;
599
+ }
600
+ if (s0 === peg$FAILED) {
601
+ s0 = peg$currPos;
602
+ s1 = peg$parseanyLink();
603
+ if (s1 !== peg$FAILED) {
604
+ peg$savedPos = s0;
605
+ s1 = peg$f6(s1);
606
+ }
607
+ s0 = s1;
608
+ }
609
+ return s0;
610
+ }
611
+ function peg$parsereferenceOrLink() {
612
+ let s0, s1;
613
+ s0 = peg$currPos;
614
+ s1 = peg$parsemultiLineAnyLink();
615
+ if (s1 !== peg$FAILED) {
616
+ peg$savedPos = s0;
617
+ s1 = peg$f7(s1);
618
+ }
619
+ s0 = s1;
620
+ if (s0 === peg$FAILED) {
621
+ s0 = peg$currPos;
622
+ s1 = peg$parsereference();
623
+ if (s1 !== peg$FAILED) {
624
+ peg$savedPos = s0;
625
+ s1 = peg$f8(s1);
626
+ }
627
+ s0 = s1;
628
+ }
629
+ return s0;
630
+ }
631
+ function peg$parseanyLink() {
632
+ let s0, s1, s2;
633
+ s0 = peg$currPos;
634
+ s1 = peg$parsemultiLineAnyLink();
635
+ if (s1 !== peg$FAILED) {
636
+ s2 = peg$parseeol();
637
+ if (s2 !== peg$FAILED) {
638
+ peg$savedPos = s0;
639
+ s0 = peg$f9(s1);
640
+ } else {
641
+ peg$currPos = s0;
642
+ s0 = peg$FAILED;
643
+ }
644
+ } else {
645
+ peg$currPos = s0;
646
+ s0 = peg$FAILED;
647
+ }
648
+ if (s0 === peg$FAILED) {
649
+ s0 = peg$currPos;
650
+ s1 = peg$parseindentedIdLink();
651
+ if (s1 !== peg$FAILED) {
652
+ peg$savedPos = s0;
653
+ s1 = peg$f10(s1);
654
+ }
655
+ s0 = s1;
656
+ if (s0 === peg$FAILED) {
657
+ s0 = peg$currPos;
658
+ s1 = peg$parsesingleLineAnyLink();
659
+ if (s1 !== peg$FAILED) {
660
+ peg$savedPos = s0;
661
+ s1 = peg$f11(s1);
662
+ }
663
+ s0 = s1;
664
+ }
665
+ }
666
+ return s0;
667
+ }
668
+ function peg$parsemultiLineAnyLink() {
669
+ let s0;
670
+ s0 = peg$parsemultiLineValueLink();
671
+ if (s0 === peg$FAILED) {
672
+ s0 = peg$parsemultiLineLink();
673
+ }
674
+ return s0;
675
+ }
676
+ function peg$parsesingleLineAnyLink() {
677
+ let s0, s1, s2;
678
+ s0 = peg$currPos;
679
+ s1 = peg$parsesingleLineLink();
680
+ if (s1 !== peg$FAILED) {
681
+ s2 = peg$parseeol();
682
+ if (s2 !== peg$FAILED) {
683
+ peg$savedPos = s0;
684
+ s0 = peg$f12(s1);
685
+ } else {
686
+ peg$currPos = s0;
687
+ s0 = peg$FAILED;
688
+ }
689
+ } else {
690
+ peg$currPos = s0;
691
+ s0 = peg$FAILED;
692
+ }
693
+ if (s0 === peg$FAILED) {
694
+ s0 = peg$currPos;
695
+ s1 = peg$parsesingleLineValueLink();
696
+ if (s1 !== peg$FAILED) {
697
+ s2 = peg$parseeol();
698
+ if (s2 !== peg$FAILED) {
699
+ peg$savedPos = s0;
700
+ s0 = peg$f13(s1);
701
+ } else {
702
+ peg$currPos = s0;
703
+ s0 = peg$FAILED;
704
+ }
705
+ } else {
706
+ peg$currPos = s0;
707
+ s0 = peg$FAILED;
708
+ }
709
+ }
710
+ return s0;
711
+ }
712
+ function peg$parsemultiLineValueAndWhitespace() {
713
+ let s0, s1, s2;
714
+ s0 = peg$currPos;
715
+ s1 = peg$parsereferenceOrLink();
716
+ if (s1 !== peg$FAILED) {
717
+ s2 = peg$parse_();
718
+ peg$savedPos = s0;
719
+ s0 = peg$f14(s1);
720
+ } else {
721
+ peg$currPos = s0;
722
+ s0 = peg$FAILED;
723
+ }
724
+ return s0;
725
+ }
726
+ function peg$parsemultiLineValues() {
727
+ let s0, s1, s2, s3;
728
+ s0 = peg$currPos;
729
+ s1 = peg$parse_();
730
+ s2 = [];
731
+ s3 = peg$parsemultiLineValueAndWhitespace();
732
+ while (s3 !== peg$FAILED) {
733
+ s2.push(s3);
734
+ s3 = peg$parsemultiLineValueAndWhitespace();
735
+ }
736
+ peg$savedPos = s0;
737
+ s0 = peg$f15(s2);
738
+ return s0;
739
+ }
740
+ function peg$parsesingleLineValueAndWhitespace() {
741
+ let s0, s1, s2;
742
+ s0 = peg$currPos;
743
+ s1 = peg$parse__();
744
+ s2 = peg$parsereferenceOrLink();
745
+ if (s2 !== peg$FAILED) {
746
+ peg$savedPos = s0;
747
+ s0 = peg$f16(s2);
748
+ } else {
749
+ peg$currPos = s0;
750
+ s0 = peg$FAILED;
751
+ }
752
+ return s0;
753
+ }
754
+ function peg$parsesingleLineValues() {
755
+ let s0, s1, s2;
756
+ s0 = peg$currPos;
757
+ s1 = [];
758
+ s2 = peg$parsesingleLineValueAndWhitespace();
759
+ if (s2 !== peg$FAILED) {
760
+ while (s2 !== peg$FAILED) {
761
+ s1.push(s2);
762
+ s2 = peg$parsesingleLineValueAndWhitespace();
763
+ }
764
+ } else {
765
+ s1 = peg$FAILED;
766
+ }
767
+ if (s1 !== peg$FAILED) {
768
+ peg$savedPos = s0;
769
+ s1 = peg$f17(s1);
770
+ }
771
+ s0 = s1;
772
+ return s0;
773
+ }
774
+ function peg$parsesingleLineLink() {
775
+ let s0, s1, s2, s3, s4, s5;
776
+ s0 = peg$currPos;
777
+ s1 = peg$parse__();
778
+ s2 = peg$parsereference();
779
+ if (s2 !== peg$FAILED) {
780
+ s3 = peg$parse__();
781
+ if (input.charCodeAt(peg$currPos) === 58) {
782
+ s4 = peg$c0;
783
+ peg$currPos++;
784
+ } else {
785
+ s4 = peg$FAILED;
786
+ if (peg$silentFails === 0) {
787
+ peg$fail(peg$e0);
788
+ }
789
+ }
790
+ if (s4 !== peg$FAILED) {
791
+ s5 = peg$parsesingleLineValues();
792
+ if (s5 !== peg$FAILED) {
793
+ peg$savedPos = s0;
794
+ s0 = peg$f18(s2, s5);
795
+ } else {
796
+ peg$currPos = s0;
797
+ s0 = peg$FAILED;
798
+ }
799
+ } else {
800
+ peg$currPos = s0;
801
+ s0 = peg$FAILED;
802
+ }
803
+ } else {
804
+ peg$currPos = s0;
805
+ s0 = peg$FAILED;
806
+ }
807
+ return s0;
808
+ }
809
+ function peg$parsemultiLineLink() {
810
+ let s0, s1, s2, s3, s4, s5, s6, s7, s8;
811
+ s0 = peg$currPos;
812
+ if (input.charCodeAt(peg$currPos) === 40) {
813
+ s1 = peg$c1;
814
+ peg$currPos++;
815
+ } else {
816
+ s1 = peg$FAILED;
817
+ if (peg$silentFails === 0) {
818
+ peg$fail(peg$e1);
819
+ }
820
+ }
821
+ if (s1 !== peg$FAILED) {
822
+ s2 = peg$parse_();
823
+ s3 = peg$parsereference();
824
+ if (s3 !== peg$FAILED) {
825
+ s4 = peg$parse_();
826
+ if (input.charCodeAt(peg$currPos) === 58) {
827
+ s5 = peg$c0;
828
+ peg$currPos++;
829
+ } else {
830
+ s5 = peg$FAILED;
831
+ if (peg$silentFails === 0) {
832
+ peg$fail(peg$e0);
833
+ }
834
+ }
835
+ if (s5 !== peg$FAILED) {
836
+ s6 = peg$parsemultiLineValues();
837
+ s7 = peg$parse_();
838
+ if (input.charCodeAt(peg$currPos) === 41) {
839
+ s8 = peg$c2;
840
+ peg$currPos++;
841
+ } else {
842
+ s8 = peg$FAILED;
843
+ if (peg$silentFails === 0) {
844
+ peg$fail(peg$e2);
845
+ }
846
+ }
847
+ if (s8 !== peg$FAILED) {
848
+ peg$savedPos = s0;
849
+ s0 = peg$f19(s3, s6);
850
+ } else {
851
+ peg$currPos = s0;
852
+ s0 = peg$FAILED;
853
+ }
854
+ } else {
855
+ peg$currPos = s0;
856
+ s0 = peg$FAILED;
857
+ }
858
+ } else {
859
+ peg$currPos = s0;
860
+ s0 = peg$FAILED;
861
+ }
862
+ } else {
863
+ peg$currPos = s0;
864
+ s0 = peg$FAILED;
865
+ }
866
+ return s0;
867
+ }
868
+ function peg$parsesingleLineValueLink() {
869
+ let s0, s1;
870
+ s0 = peg$currPos;
871
+ s1 = peg$parsesingleLineValues();
872
+ if (s1 !== peg$FAILED) {
873
+ peg$savedPos = s0;
874
+ s1 = peg$f20(s1);
875
+ }
876
+ s0 = s1;
877
+ return s0;
878
+ }
879
+ function peg$parsemultiLineValueLink() {
880
+ let s0, s1, s2, s3, s4;
881
+ s0 = peg$currPos;
882
+ if (input.charCodeAt(peg$currPos) === 40) {
883
+ s1 = peg$c1;
884
+ peg$currPos++;
885
+ } else {
886
+ s1 = peg$FAILED;
887
+ if (peg$silentFails === 0) {
888
+ peg$fail(peg$e1);
889
+ }
890
+ }
891
+ if (s1 !== peg$FAILED) {
892
+ s2 = peg$parsemultiLineValues();
893
+ s3 = peg$parse_();
894
+ if (input.charCodeAt(peg$currPos) === 41) {
895
+ s4 = peg$c2;
896
+ peg$currPos++;
897
+ } else {
898
+ s4 = peg$FAILED;
899
+ if (peg$silentFails === 0) {
900
+ peg$fail(peg$e2);
901
+ }
902
+ }
903
+ if (s4 !== peg$FAILED) {
904
+ peg$savedPos = s0;
905
+ s0 = peg$f21(s2);
906
+ } else {
907
+ peg$currPos = s0;
908
+ s0 = peg$FAILED;
909
+ }
910
+ } else {
911
+ peg$currPos = s0;
912
+ s0 = peg$FAILED;
913
+ }
914
+ return s0;
915
+ }
916
+ function peg$parseindentedIdLink() {
917
+ let s0, s1, s2, s3, s4;
918
+ s0 = peg$currPos;
919
+ s1 = peg$parsereference();
920
+ if (s1 !== peg$FAILED) {
921
+ s2 = peg$parse__();
922
+ if (input.charCodeAt(peg$currPos) === 58) {
923
+ s3 = peg$c0;
924
+ peg$currPos++;
925
+ } else {
926
+ s3 = peg$FAILED;
927
+ if (peg$silentFails === 0) {
928
+ peg$fail(peg$e0);
929
+ }
930
+ }
931
+ if (s3 !== peg$FAILED) {
932
+ s4 = peg$parseeol();
933
+ if (s4 !== peg$FAILED) {
934
+ peg$savedPos = s0;
935
+ s0 = peg$f22(s1);
936
+ } else {
937
+ peg$currPos = s0;
938
+ s0 = peg$FAILED;
939
+ }
940
+ } else {
941
+ peg$currPos = s0;
942
+ s0 = peg$FAILED;
943
+ }
944
+ } else {
945
+ peg$currPos = s0;
946
+ s0 = peg$FAILED;
947
+ }
948
+ return s0;
949
+ }
950
+ function peg$parsereference() {
951
+ let s0;
952
+ s0 = peg$parsedoubleQuotedReference();
953
+ if (s0 === peg$FAILED) {
954
+ s0 = peg$parsesingleQuotedReference();
955
+ if (s0 === peg$FAILED) {
956
+ s0 = peg$parsesimpleReference();
957
+ }
958
+ }
959
+ return s0;
960
+ }
961
+ function peg$parsesimpleReference() {
962
+ let s0, s1, s2;
963
+ s0 = peg$currPos;
964
+ s1 = [];
965
+ s2 = peg$parsereferenceSymbol();
966
+ if (s2 !== peg$FAILED) {
967
+ while (s2 !== peg$FAILED) {
968
+ s1.push(s2);
969
+ s2 = peg$parsereferenceSymbol();
970
+ }
971
+ } else {
972
+ s1 = peg$FAILED;
973
+ }
974
+ if (s1 !== peg$FAILED) {
975
+ peg$savedPos = s0;
976
+ s1 = peg$f23(s1);
977
+ }
978
+ s0 = s1;
979
+ return s0;
980
+ }
981
+ function peg$parsedoubleQuotedReference() {
982
+ let s0, s1, s2, s3;
983
+ s0 = peg$currPos;
984
+ if (input.charCodeAt(peg$currPos) === 34) {
985
+ s1 = peg$c3;
986
+ peg$currPos++;
987
+ } else {
988
+ s1 = peg$FAILED;
989
+ if (peg$silentFails === 0) {
990
+ peg$fail(peg$e3);
991
+ }
992
+ }
993
+ if (s1 !== peg$FAILED) {
994
+ s2 = [];
995
+ s3 = input.charAt(peg$currPos);
996
+ if (peg$r0.test(s3)) {
997
+ peg$currPos++;
998
+ } else {
999
+ s3 = peg$FAILED;
1000
+ if (peg$silentFails === 0) {
1001
+ peg$fail(peg$e4);
1002
+ }
1003
+ }
1004
+ if (s3 !== peg$FAILED) {
1005
+ while (s3 !== peg$FAILED) {
1006
+ s2.push(s3);
1007
+ s3 = input.charAt(peg$currPos);
1008
+ if (peg$r0.test(s3)) {
1009
+ peg$currPos++;
1010
+ } else {
1011
+ s3 = peg$FAILED;
1012
+ if (peg$silentFails === 0) {
1013
+ peg$fail(peg$e4);
1014
+ }
1015
+ }
1016
+ }
1017
+ } else {
1018
+ s2 = peg$FAILED;
1019
+ }
1020
+ if (s2 !== peg$FAILED) {
1021
+ if (input.charCodeAt(peg$currPos) === 34) {
1022
+ s3 = peg$c3;
1023
+ peg$currPos++;
1024
+ } else {
1025
+ s3 = peg$FAILED;
1026
+ if (peg$silentFails === 0) {
1027
+ peg$fail(peg$e3);
1028
+ }
1029
+ }
1030
+ if (s3 !== peg$FAILED) {
1031
+ peg$savedPos = s0;
1032
+ s0 = peg$f24(s2);
1033
+ } else {
1034
+ peg$currPos = s0;
1035
+ s0 = peg$FAILED;
1036
+ }
1037
+ } else {
1038
+ peg$currPos = s0;
1039
+ s0 = peg$FAILED;
1040
+ }
1041
+ } else {
1042
+ peg$currPos = s0;
1043
+ s0 = peg$FAILED;
1044
+ }
1045
+ return s0;
1046
+ }
1047
+ function peg$parsesingleQuotedReference() {
1048
+ let s0, s1, s2, s3;
1049
+ s0 = peg$currPos;
1050
+ if (input.charCodeAt(peg$currPos) === 39) {
1051
+ s1 = peg$c4;
1052
+ peg$currPos++;
1053
+ } else {
1054
+ s1 = peg$FAILED;
1055
+ if (peg$silentFails === 0) {
1056
+ peg$fail(peg$e5);
1057
+ }
1058
+ }
1059
+ if (s1 !== peg$FAILED) {
1060
+ s2 = [];
1061
+ s3 = input.charAt(peg$currPos);
1062
+ if (peg$r1.test(s3)) {
1063
+ peg$currPos++;
1064
+ } else {
1065
+ s3 = peg$FAILED;
1066
+ if (peg$silentFails === 0) {
1067
+ peg$fail(peg$e6);
1068
+ }
1069
+ }
1070
+ if (s3 !== peg$FAILED) {
1071
+ while (s3 !== peg$FAILED) {
1072
+ s2.push(s3);
1073
+ s3 = input.charAt(peg$currPos);
1074
+ if (peg$r1.test(s3)) {
1075
+ peg$currPos++;
1076
+ } else {
1077
+ s3 = peg$FAILED;
1078
+ if (peg$silentFails === 0) {
1079
+ peg$fail(peg$e6);
1080
+ }
1081
+ }
1082
+ }
1083
+ } else {
1084
+ s2 = peg$FAILED;
1085
+ }
1086
+ if (s2 !== peg$FAILED) {
1087
+ if (input.charCodeAt(peg$currPos) === 39) {
1088
+ s3 = peg$c4;
1089
+ peg$currPos++;
1090
+ } else {
1091
+ s3 = peg$FAILED;
1092
+ if (peg$silentFails === 0) {
1093
+ peg$fail(peg$e5);
1094
+ }
1095
+ }
1096
+ if (s3 !== peg$FAILED) {
1097
+ peg$savedPos = s0;
1098
+ s0 = peg$f25(s2);
1099
+ } else {
1100
+ peg$currPos = s0;
1101
+ s0 = peg$FAILED;
1102
+ }
1103
+ } else {
1104
+ peg$currPos = s0;
1105
+ s0 = peg$FAILED;
1106
+ }
1107
+ } else {
1108
+ peg$currPos = s0;
1109
+ s0 = peg$FAILED;
1110
+ }
1111
+ return s0;
1112
+ }
1113
+ function peg$parsePUSH_INDENTATION() {
1114
+ let s0, s1, s2;
1115
+ s0 = peg$currPos;
1116
+ s1 = [];
1117
+ if (input.charCodeAt(peg$currPos) === 32) {
1118
+ s2 = peg$c5;
1119
+ peg$currPos++;
1120
+ } else {
1121
+ s2 = peg$FAILED;
1122
+ if (peg$silentFails === 0) {
1123
+ peg$fail(peg$e7);
1124
+ }
1125
+ }
1126
+ while (s2 !== peg$FAILED) {
1127
+ s1.push(s2);
1128
+ if (input.charCodeAt(peg$currPos) === 32) {
1129
+ s2 = peg$c5;
1130
+ peg$currPos++;
1131
+ } else {
1132
+ s2 = peg$FAILED;
1133
+ if (peg$silentFails === 0) {
1134
+ peg$fail(peg$e7);
1135
+ }
1136
+ }
1137
+ }
1138
+ peg$savedPos = peg$currPos;
1139
+ s2 = peg$f26(s1);
1140
+ if (s2) {
1141
+ s2 = undefined;
1142
+ } else {
1143
+ s2 = peg$FAILED;
1144
+ }
1145
+ if (s2 !== peg$FAILED) {
1146
+ peg$savedPos = s0;
1147
+ s0 = peg$f27(s1);
1148
+ } else {
1149
+ peg$currPos = s0;
1150
+ s0 = peg$FAILED;
1151
+ }
1152
+ return s0;
1153
+ }
1154
+ function peg$parseCHECK_INDENTATION() {
1155
+ let s0, s1, s2;
1156
+ s0 = peg$currPos;
1157
+ s1 = [];
1158
+ if (input.charCodeAt(peg$currPos) === 32) {
1159
+ s2 = peg$c5;
1160
+ peg$currPos++;
1161
+ } else {
1162
+ s2 = peg$FAILED;
1163
+ if (peg$silentFails === 0) {
1164
+ peg$fail(peg$e7);
1165
+ }
1166
+ }
1167
+ while (s2 !== peg$FAILED) {
1168
+ s1.push(s2);
1169
+ if (input.charCodeAt(peg$currPos) === 32) {
1170
+ s2 = peg$c5;
1171
+ peg$currPos++;
1172
+ } else {
1173
+ s2 = peg$FAILED;
1174
+ if (peg$silentFails === 0) {
1175
+ peg$fail(peg$e7);
1176
+ }
1177
+ }
1178
+ }
1179
+ peg$savedPos = peg$currPos;
1180
+ s2 = peg$f28(s1);
1181
+ if (s2) {
1182
+ s2 = undefined;
1183
+ } else {
1184
+ s2 = peg$FAILED;
1185
+ }
1186
+ if (s2 !== peg$FAILED) {
1187
+ s1 = [s1, s2];
1188
+ s0 = s1;
1189
+ } else {
1190
+ peg$currPos = s0;
1191
+ s0 = peg$FAILED;
1192
+ }
1193
+ return s0;
1194
+ }
1195
+ function peg$parseeol() {
1196
+ let s0, s1, s2, s3;
1197
+ s0 = peg$currPos;
1198
+ s1 = peg$parse__();
1199
+ s2 = [];
1200
+ s3 = input.charAt(peg$currPos);
1201
+ if (peg$r2.test(s3)) {
1202
+ peg$currPos++;
1203
+ } else {
1204
+ s3 = peg$FAILED;
1205
+ if (peg$silentFails === 0) {
1206
+ peg$fail(peg$e8);
1207
+ }
1208
+ }
1209
+ if (s3 !== peg$FAILED) {
1210
+ while (s3 !== peg$FAILED) {
1211
+ s2.push(s3);
1212
+ s3 = input.charAt(peg$currPos);
1213
+ if (peg$r2.test(s3)) {
1214
+ peg$currPos++;
1215
+ } else {
1216
+ s3 = peg$FAILED;
1217
+ if (peg$silentFails === 0) {
1218
+ peg$fail(peg$e8);
1219
+ }
1220
+ }
1221
+ }
1222
+ } else {
1223
+ s2 = peg$FAILED;
1224
+ }
1225
+ if (s2 === peg$FAILED) {
1226
+ s2 = peg$parseeof();
1227
+ }
1228
+ if (s2 !== peg$FAILED) {
1229
+ s1 = [s1, s2];
1230
+ s0 = s1;
1231
+ } else {
1232
+ peg$currPos = s0;
1233
+ s0 = peg$FAILED;
1234
+ }
1235
+ return s0;
1236
+ }
1237
+ function peg$parseeof() {
1238
+ let s0, s1;
1239
+ s0 = peg$currPos;
1240
+ peg$silentFails++;
1241
+ if (input.length > peg$currPos) {
1242
+ s1 = input.charAt(peg$currPos);
1243
+ peg$currPos++;
1244
+ } else {
1245
+ s1 = peg$FAILED;
1246
+ if (peg$silentFails === 0) {
1247
+ peg$fail(peg$e9);
1248
+ }
1249
+ }
1250
+ peg$silentFails--;
1251
+ if (s1 === peg$FAILED) {
1252
+ s0 = undefined;
1253
+ } else {
1254
+ peg$currPos = s0;
1255
+ s0 = peg$FAILED;
1256
+ }
1257
+ return s0;
1258
+ }
1259
+ function peg$parse__() {
1260
+ let s0, s1;
1261
+ s0 = [];
1262
+ s1 = input.charAt(peg$currPos);
1263
+ if (peg$r3.test(s1)) {
1264
+ peg$currPos++;
1265
+ } else {
1266
+ s1 = peg$FAILED;
1267
+ if (peg$silentFails === 0) {
1268
+ peg$fail(peg$e10);
1269
+ }
1270
+ }
1271
+ while (s1 !== peg$FAILED) {
1272
+ s0.push(s1);
1273
+ s1 = input.charAt(peg$currPos);
1274
+ if (peg$r3.test(s1)) {
1275
+ peg$currPos++;
1276
+ } else {
1277
+ s1 = peg$FAILED;
1278
+ if (peg$silentFails === 0) {
1279
+ peg$fail(peg$e10);
1280
+ }
1281
+ }
1282
+ }
1283
+ return s0;
1284
+ }
1285
+ function peg$parse_() {
1286
+ let s0, s1;
1287
+ s0 = [];
1288
+ s1 = peg$parsewhiteSpaceSymbol();
1289
+ while (s1 !== peg$FAILED) {
1290
+ s0.push(s1);
1291
+ s1 = peg$parsewhiteSpaceSymbol();
1292
+ }
1293
+ return s0;
1294
+ }
1295
+ function peg$parsewhiteSpaceSymbol() {
1296
+ let s0;
1297
+ s0 = input.charAt(peg$currPos);
1298
+ if (peg$r4.test(s0)) {
1299
+ peg$currPos++;
1300
+ } else {
1301
+ s0 = peg$FAILED;
1302
+ if (peg$silentFails === 0) {
1303
+ peg$fail(peg$e11);
1304
+ }
1305
+ }
1306
+ return s0;
1307
+ }
1308
+ function peg$parsereferenceSymbol() {
1309
+ let s0;
1310
+ s0 = input.charAt(peg$currPos);
1311
+ if (peg$r5.test(s0)) {
1312
+ peg$currPos++;
1313
+ } else {
1314
+ s0 = peg$FAILED;
1315
+ if (peg$silentFails === 0) {
1316
+ peg$fail(peg$e12);
1317
+ }
1318
+ }
1319
+ return s0;
1320
+ }
1321
+ let indentationStack = [0];
1322
+ function pushIndentation(spaces) {
1323
+ indentationStack.push(spaces.length);
1324
+ }
1325
+ function popIndentation() {
1326
+ if (indentationStack.length > 1) {
1327
+ indentationStack.pop();
1328
+ }
1329
+ }
1330
+ function checkIndentation(spaces) {
1331
+ return spaces.length >= indentationStack[indentationStack.length - 1];
1332
+ }
1333
+ function getCurrentIndentation() {
1334
+ return indentationStack[indentationStack.length - 1];
1335
+ }
1336
+ peg$result = peg$startRuleFunction();
1337
+ const peg$success = peg$result !== peg$FAILED && peg$currPos === input.length;
1338
+ function peg$throw() {
1339
+ if (peg$result !== peg$FAILED && peg$currPos < input.length) {
1340
+ peg$fail(peg$endExpectation());
1341
+ }
1342
+ throw peg$buildStructuredError(peg$maxFailExpected, peg$maxFailPos < input.length ? peg$getUnicode(peg$maxFailPos) : null, peg$maxFailPos < input.length ? peg$computeLocation(peg$maxFailPos, peg$maxFailPos + 1) : peg$computeLocation(peg$maxFailPos, peg$maxFailPos));
1343
+ }
1344
+ if (options.peg$library) {
1345
+ return {
1346
+ peg$result,
1347
+ peg$currPos,
1348
+ peg$FAILED,
1349
+ peg$maxFailExpected,
1350
+ peg$maxFailPos,
1351
+ peg$success,
1352
+ peg$throw: peg$success ? undefined : peg$throw
1353
+ };
1354
+ }
1355
+ if (peg$success) {
1356
+ return peg$result;
1357
+ } else {
1358
+ peg$throw();
1359
+ }
1360
+ }
1361
+
1362
+ // src/Parser.js
1363
+ class Parser {
1364
+ constructor() {}
1365
+ parse(input) {
1366
+ try {
1367
+ const rawResult = peg$parse(input);
1368
+ return this.transformResult(rawResult);
1369
+ } catch (error) {
1370
+ throw new Error(`Parse error: ${error.message}`);
1371
+ }
1372
+ }
1373
+ transformResult(rawResult) {
1374
+ const links = [];
1375
+ const items = Array.isArray(rawResult) ? rawResult : [rawResult];
1376
+ for (const item of items) {
1377
+ if (item) {
1378
+ this.collectLinks(item, [], links);
1379
+ }
1380
+ }
1381
+ return links;
1382
+ }
1383
+ collectLinks(item, parentPath, result) {
1384
+ if (!item)
1385
+ return;
1386
+ if (item.children && item.children.length > 0) {
1387
+ if (item.id && (!item.values || item.values.length === 0)) {
1388
+ const childValues = item.children.map((child) => {
1389
+ if (child.values && child.values.length === 1) {
1390
+ return this.transformLink(child.values[0]);
1391
+ }
1392
+ return this.transformLink(child);
1393
+ });
1394
+ const linkWithChildren = {
1395
+ id: item.id,
1396
+ values: childValues
1397
+ };
1398
+ const currentLink = this.transformLink(linkWithChildren);
1399
+ if (parentPath.length === 0) {
1400
+ result.push(currentLink);
1401
+ } else {
1402
+ result.push(this.combinePathElements(parentPath, currentLink));
1403
+ }
1404
+ } else {
1405
+ const currentLink = this.transformLink(item);
1406
+ if (parentPath.length === 0) {
1407
+ result.push(currentLink);
1408
+ } else {
1409
+ result.push(this.combinePathElements(parentPath, currentLink));
1410
+ }
1411
+ const newPath = [...parentPath, currentLink];
1412
+ for (const child of item.children) {
1413
+ this.collectLinks(child, newPath, result);
1414
+ }
1415
+ }
1416
+ } else {
1417
+ const currentLink = this.transformLink(item);
1418
+ if (parentPath.length === 0) {
1419
+ result.push(currentLink);
1420
+ } else {
1421
+ result.push(this.combinePathElements(parentPath, currentLink));
1422
+ }
1423
+ }
1424
+ }
1425
+ combinePathElements(pathElements, current) {
1426
+ if (pathElements.length === 0)
1427
+ return current;
1428
+ if (pathElements.length === 1) {
1429
+ const combined2 = new Link(null, [pathElements[0], current]);
1430
+ combined2._isFromPathCombination = true;
1431
+ return combined2;
1432
+ }
1433
+ const parentPath = pathElements.slice(0, -1);
1434
+ const lastElement = pathElements[pathElements.length - 1];
1435
+ let parent = this.combinePathElements(parentPath, lastElement);
1436
+ const combined = new Link(null, [parent, current]);
1437
+ combined._isFromPathCombination = true;
1438
+ return combined;
1439
+ }
1440
+ transformLink(item) {
1441
+ if (!item)
1442
+ return null;
1443
+ if (item instanceof Link) {
1444
+ return item;
1445
+ }
1446
+ if (item.id !== undefined && !item.values && !item.children) {
1447
+ return new Link(item.id);
1448
+ }
1449
+ if (item.values && Array.isArray(item.values)) {
1450
+ const link = new Link(item.id || null, []);
1451
+ link.values = item.values.map((v) => this.transformLink(v));
1452
+ return link;
1453
+ }
1454
+ return new Link(item.id || null, []);
1455
+ }
1456
+ }
1457
+ export {
1458
+ formatLinks,
1459
+ Parser,
1460
+ LinksGroup,
1461
+ Link
1462
+ };