1 // @generated by Peggy 4.0.2.
3 // https://peggyjs.org/
9 /* Generated code is not pretty, ignore a lot of this horrorstyle. */
10 /* eslint-disable no-control-regex */
11 /* eslint-disable no-empty */
12 /* eslint-disable security/detect-object-injection */
13 /* eslint-disable brace-style */
14 /* eslint-disable comma-dangle */
15 /* eslint-disable vars-on-top */
16 /* eslint-disable no-unused-vars */
17 /* eslint-disable camelcase */
18 /* eslint-disable indent */
19 /* eslint-disable semi */
20 /* eslint-disable jsdoc/require-jsdoc */
21 /* eslint-disable sonarjs/cognitive-complexity */
22 /* eslint-disable sonarjs/no-identical-functions */
24 function makeString(o
) {
28 const onlyFirstAttributes
= [
37 function peg
$subclass(child
, parent
) {
38 function C() { this.constructor = child
; }
39 C
.prototype = parent
.prototype;
40 child
.prototype = new C();
43 function peg
$SyntaxError(message
, expected
, found
, location
) {
44 var self
= Error
.call(this, message
);
45 // istanbul ignore next Check is a necessary evil to support older environments
46 if (Object
.setPrototypeOf
) {
47 Object
.setPrototypeOf(self
, peg
$SyntaxError
.prototype);
49 self
.expected
= expected
;
51 self
.location
= location
;
52 self
.name
= "SyntaxError";
56 peg
$subclass(peg
$SyntaxError
, Error
);
58 function peg
$padEnd(str
, targetLength
, padString
) {
59 padString
= padString
|| " ";
60 if (str
.length
> targetLength
) { return str
; }
61 targetLength
-= str
.length
;
62 padString
+= padString
.repeat(targetLength
);
63 return str
+ padString
.slice(0, targetLength
);
66 peg
$SyntaxError
.prototype.format = function(sources
) {
67 var str
= "Error: " + this.message
;
71 for (k
= 0; k
< sources
.length
; k
++) {
72 if (sources
[k
].source
=== this.location
.source
) {
73 src
= sources
[k
].text
.split(/\r\n|\n|\r/g);
77 var s
= this.location
.start
;
78 var offset_s
= (this.location
.source
&& (typeof this.location
.source
.offset
=== "function"))
79 ? this.location
.source
.offset(s
)
81 var loc
= this.location
.source
+ ":" + offset_s
.line
+ ":" + offset_s
.column
;
83 var e
= this.location
.end
;
84 var filler
= peg
$padEnd("", offset_s
.line
.toString().length
, ' ');
85 var line
= src
[s
.line
- 1];
86 var last
= s
.line
=== e
.line
? e
.column : line
.length
+ 1;
87 var hatLen
= (last
- s
.column
) || 1;
88 str
+= "\n --> " + loc
+ "\n"
90 + offset_s
.line
+ " | " + line
+ "\n"
91 + filler
+ " | " + peg
$padEnd("", s
.column
- 1, ' ')
92 + peg
$padEnd("", hatLen
, "^");
94 str
+= "\n at " + loc
;
100 peg
$SyntaxError
.buildMessage = function(expected
, found
) {
101 var DESCRIBE_EXPECTATION_FNS
= {
102 literal: function(expectation
) {
103 return "\"" + literalEscape(expectation
.text
) + "\"";
106 class: function(expectation
) {
107 var escapedParts
= expectation
.parts
.map(function(part
) {
108 return Array
.isArray(part
)
109 ? classEscape(part
[0]) + "-" + classEscape(part
[1])
113 return "[" + (expectation
.inverted
? "^" : "") + escapedParts
.join("") + "]";
117 return "any character";
121 return "end of input";
124 other: function(expectation
) {
125 return expectation
.description
;
130 return ch
.charCodeAt(0).toString(16).toUpperCase();
133 function literalEscape(s
) {
135 .replace(/\\/g
, "\\\\")
136 .replace(/"/g, "\\\"")
137 .replace(/\0/g, "\\0")
138 .replace(/\t/g, "\\t
")
139 .replace(/\n/g, "\\n
")
140 .replace(/\r/g, "\\r
")
141 .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0
" + hex(ch); })
142 .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x
" + hex(ch); });
145 function classEscape(s) {
147 .replace(/\\/g, "\\\\")
148 .replace(/\]/g, "\\]")
149 .replace(/\^/g, "\\^")
150 .replace(/-/g, "\\-")
151 .replace(/\0/g, "\\0")
152 .replace(/\t/g, "\\t
")
153 .replace(/\n/g, "\\n
")
154 .replace(/\r/g, "\\r
")
155 .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0
" + hex(ch); })
156 .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x
" + hex(ch); });
159 function describeExpectation(expectation) {
160 return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
163 function describeExpected(expected) {
164 var descriptions = expected.map(describeExpectation);
169 if (descriptions.length > 0) {
170 for (i = 1, j = 1; i < descriptions.length; i++) {
171 if (descriptions[i - 1] !== descriptions[i]) {
172 descriptions[j] = descriptions[i];
176 descriptions.length = j;
179 switch (descriptions.length) {
181 return descriptions[0];
184 return descriptions[0] + " or
" + descriptions[1];
187 return descriptions.slice(0, -1).join(", ")
189 + descriptions[descriptions.length - 1];
193 function describeFound(found) {
194 return found ? "\"" + literalEscape(found) + "\"" : "end
of input
";
197 return "Expected
" + describeExpected(expected) + " but
" + describeFound(found) + " found
.";
200 function peg$parse(input, options) {
201 options = options !== undefined ? options : {};
204 var peg$source = options.grammarSource;
206 var peg$startRuleFunctions = { links: peg$parselinks, extendedValue: peg$parseextendedValue };
207 var peg$startRuleFunction = peg$parselinks;
216 var peg$r0 = /^[^>]/;
217 var peg$r1 = /^[a-zA-Z]/;
219 var peg
$r3
= /^[^"]/;
220 var peg
$r4
= /^[^";,]/;
222 var peg
$r6
= /^[^']/;
225 var peg
$e0
= peg
$otherExpectation("links");
226 var peg
$e1
= peg
$otherExpectation("link-value");
227 var peg
$e2
= peg
$literalExpectation(",", false);
228 var peg
$e3
= peg
$otherExpectation("uri-reference");
229 var peg
$e4
= peg
$literalExpectation("<", false);
230 var peg
$e5
= peg
$literalExpectation(">", false);
231 var peg
$e6
= peg
$literalExpectation(";", false);
232 var peg
$e7
= peg
$otherExpectation("uri");
233 var peg
$e8
= peg
$classExpectation([">"], true, false);
234 var peg
$e9
= peg
$otherExpectation("attributes");
235 var peg
$e10
= peg
$otherExpectation("link-param");
236 var peg
$e11
= peg
$literalExpectation("=", false);
237 var peg
$e12
= peg
$otherExpectation("name");
238 var peg
$e13
= peg
$classExpectation([["a", "z"], ["A", "Z"]], false, false);
239 var peg
$e14
= peg
$literalExpectation("*", false);
240 var peg
$e15
= peg
$otherExpectation("value");
241 var peg
$e16
= peg
$classExpectation(["\""], false, false);
242 var peg
$e17
= peg
$classExpectation(["\""], true, false);
243 var peg
$e18
= peg
$classExpectation(["\"", ";", ","], true, false);
244 var peg
$e19
= peg
$otherExpectation("whitespace");
245 var peg
$e20
= peg
$classExpectation([" "], false, false);
246 var peg
$e21
= peg
$otherExpectation("bad whitespace");
247 var peg
$e22
= peg
$otherExpectation("extended-value");
248 var peg
$e23
= peg
$classExpectation(["'"], true, false);
249 var peg
$e24
= peg
$classExpectation(["'"], false, false);
250 var peg
$e25
= peg
$anyExpectation();
252 var peg
$f0 = function(links
) {
255 var peg
$f1 = function(uriReference
, attributes
) {
258 target: uriReference
,
262 var peg
$f2 = function(uri
) {
265 var peg
$f3 = function(uri
) {
266 return makeString(uri
);
268 var peg
$f4 = function(attrs
) {
269 return attrs
.filter((a
) => a
);
271 var peg
$f5 = function(name
, value
) {
272 if (onlyFirstAttributes
.includes(name
.name
)) {
273 if (seenAttributes
.includes(name
.name
)) {
274 // Repeat of singleton attribute, ignore it.
277 seenAttributes
.push(name
.name
);
284 var peg
$f6 = function(name
) {
290 var peg
$f7 = function(name
, extended
) {
292 name: makeString(name
.concat(extended
)).toLowerCase(),
293 extended: !!extended
,
296 var peg
$f8 = function(value
) {
298 value: makeString(value
),
301 var peg
$f9 = function(value
) {
303 value: makeString(value
),
306 var peg
$f10 = function(encoding
, language
, value
) {
308 encoding: encoding
.length
? makeString(encoding
) : 'UTF-8',
309 language: language
.length
? makeString(language
) : null,
310 value: decodeURIComponent(makeString(value
)),
313 var peg
$f11 = function(value
) {
317 value: makeString(value
),
320 var peg
$currPos
= options
.peg
$currPos
| 0;
321 var peg
$savedPos
= peg
$currPos
;
322 var peg
$posDetailsCache
= [{ line: 1, column: 1 }];
323 var peg
$maxFailPos
= peg
$currPos
;
324 var peg
$maxFailExpected
= options
.peg
$maxFailExpected
|| [];
325 var peg
$silentFails
= options
.peg
$silentFails
| 0;
329 if (options
.startRule
) {
330 if (!(options
.startRule
in peg
$startRuleFunctions
)) {
331 throw new Error("Can't start parsing from rule \"" + options
.startRule
+ "\".");
334 peg
$startRuleFunction
= peg
$startRuleFunctions
[options
.startRule
];
338 return input
.substring(peg
$savedPos
, peg
$currPos
);
353 function location() {
354 return peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
357 function expected(description
, location
) {
358 location
= location
!== undefined
360 : peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
362 throw peg
$buildStructuredError(
363 [peg
$otherExpectation(description
)],
364 input
.substring(peg
$savedPos
, peg
$currPos
),
369 function error(message
, location
) {
370 location
= location
!== undefined
372 : peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
374 throw peg
$buildSimpleError(message
, location
);
377 function peg
$literalExpectation(text
, ignoreCase
) {
378 return { type: "literal", text: text
, ignoreCase: ignoreCase
};
381 function peg
$classExpectation(parts
, inverted
, ignoreCase
) {
382 return { type: "class", parts: parts
, inverted: inverted
, ignoreCase: ignoreCase
};
385 function peg
$anyExpectation() {
386 return { type: "any" };
389 function peg
$endExpectation() {
390 return { type: "end" };
393 function peg
$otherExpectation(description
) {
394 return { type: "other", description: description
};
397 function peg
$computePosDetails(pos
) {
398 var details
= peg
$posDetailsCache
[pos
];
404 if (pos
>= peg
$posDetailsCache
.length
) {
405 p
= peg
$posDetailsCache
.length
- 1;
408 while (!peg
$posDetailsCache
[--p
]) {}
411 details
= peg
$posDetailsCache
[p
];
414 column: details
.column
418 if (input
.charCodeAt(p
) === 10) {
428 peg
$posDetailsCache
[pos
] = details
;
434 function peg
$computeLocation(startPos
, endPos
, offset
) {
435 var startPosDetails
= peg
$computePosDetails(startPos
);
436 var endPosDetails
= peg
$computePosDetails(endPos
);
442 line: startPosDetails
.line
,
443 column: startPosDetails
.column
447 line: endPosDetails
.line
,
448 column: endPosDetails
.column
451 if (offset
&& peg
$source
&& (typeof peg
$source
.offset
=== "function")) {
452 res
.start
= peg
$source
.offset(res
.start
);
453 res
.end
= peg
$source
.offset(res
.end
);
458 function peg
$fail(expected
) {
459 if (peg
$currPos
< peg
$maxFailPos
) { return; }
461 if (peg
$currPos
> peg
$maxFailPos
) {
462 peg
$maxFailPos
= peg
$currPos
;
463 peg
$maxFailExpected
= [];
466 peg
$maxFailExpected
.push(expected
);
469 function peg
$buildSimpleError(message
, location
) {
470 return new peg
$SyntaxError(message
, null, null, location
);
473 function peg
$buildStructuredError(expected
, found
, location
) {
474 return new peg
$SyntaxError(
475 peg
$SyntaxError
.buildMessage(expected
, found
),
482 function peg
$parselinks() {
488 s2
= peg
$parselinkValue();
489 if (s2
!== peg
$FAILED
) {
490 while (s2
!== peg
$FAILED
) {
492 s2
= peg
$parselinkValue();
497 if (s1
!== peg
$FAILED
) {
503 if (s0
=== peg
$FAILED
) {
505 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e0
); }
511 function peg
$parselinkValue() {
512 var s0
, s1
, s2
, s3
, s4
, s5
;
516 s1
= peg
$parseuriReference();
517 if (s1
!== peg
$FAILED
) {
519 s3
= peg
$parseattributes();
520 if (s3
!== peg
$FAILED
) {
521 if (input
.charCodeAt(peg
$currPos
) === 44) {
526 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e2
); }
528 if (s4
=== peg
$FAILED
) {
543 if (s0
=== peg
$FAILED
) {
545 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e1
); }
551 function peg
$parseuriReference() {
552 var s0
, s1
, s2
, s3
, s4
;
556 if (input
.charCodeAt(peg
$currPos
) === 60) {
561 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e4
); }
563 if (s1
!== peg
$FAILED
) {
565 if (s2
!== peg
$FAILED
) {
566 if (input
.charCodeAt(peg
$currPos
) === 62) {
571 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e5
); }
573 if (s3
!== peg
$FAILED
) {
574 if (input
.charCodeAt(peg
$currPos
) === 59) {
579 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
581 if (s4
!== peg
$FAILED
) {
601 if (s0
=== peg
$FAILED
) {
603 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e3
); }
609 function peg
$parseuri() {
615 s2
= input
.charAt(peg
$currPos
);
616 if (peg
$r0
.test(s2
)) {
620 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e8
); }
622 if (s2
!== peg
$FAILED
) {
623 while (s2
!== peg
$FAILED
) {
625 s2
= input
.charAt(peg
$currPos
);
626 if (peg
$r0
.test(s2
)) {
630 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e8
); }
636 if (s1
!== peg
$FAILED
) {
642 if (s0
=== peg
$FAILED
) {
644 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e7
); }
650 function peg
$parseattributes() {
656 s2
= peg
$parselinkParam();
657 if (s2
!== peg
$FAILED
) {
658 while (s2
!== peg
$FAILED
) {
660 s2
= peg
$parselinkParam();
665 if (s1
!== peg
$FAILED
) {
671 if (s0
=== peg
$FAILED
) {
673 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e9
); }
679 function peg
$parselinkParam() {
680 var s0
, s1
, s2
, s3
, s4
, s5
, s6
, s7
;
684 s1
= peg
$parsename();
685 if (s1
!== peg
$FAILED
) {
687 if (input
.charCodeAt(peg
$currPos
) === 61) {
692 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e11
); }
694 if (s3
!== peg
$FAILED
) {
696 s5
= peg
$parsevalue();
697 if (s5
!== peg
$FAILED
) {
698 if (input
.charCodeAt(peg
$currPos
) === 59) {
703 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
705 if (s6
=== peg
$FAILED
) {
723 if (s0
=== peg
$FAILED
) {
725 s1
= peg
$parsename();
726 if (s1
!== peg
$FAILED
) {
728 if (input
.charCodeAt(peg
$currPos
) === 59) {
733 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
735 if (s3
=== peg
$FAILED
) {
747 if (s0
=== peg
$FAILED
) {
749 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e10
); }
755 function peg
$parsename() {
761 s2
= input
.charAt(peg
$currPos
);
762 if (peg
$r1
.test(s2
)) {
766 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e13
); }
768 if (s2
!== peg
$FAILED
) {
769 while (s2
!== peg
$FAILED
) {
771 s2
= input
.charAt(peg
$currPos
);
772 if (peg
$r1
.test(s2
)) {
776 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e13
); }
782 if (s1
!== peg
$FAILED
) {
783 if (input
.charCodeAt(peg
$currPos
) === 42) {
788 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e14
); }
790 if (s2
=== peg
$FAILED
) {
800 if (s0
=== peg
$FAILED
) {
802 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e12
); }
808 function peg
$parsevalue() {
813 s1
= input
.charAt(peg
$currPos
);
814 if (peg
$r2
.test(s1
)) {
818 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e16
); }
820 if (s1
!== peg
$FAILED
) {
822 s3
= input
.charAt(peg
$currPos
);
823 if (peg
$r3
.test(s3
)) {
827 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e17
); }
829 if (s3
!== peg
$FAILED
) {
830 while (s3
!== peg
$FAILED
) {
832 s3
= input
.charAt(peg
$currPos
);
833 if (peg
$r3
.test(s3
)) {
837 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e17
); }
843 if (s2
!== peg
$FAILED
) {
844 s3
= input
.charAt(peg
$currPos
);
845 if (peg
$r2
.test(s3
)) {
849 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e16
); }
851 if (s3
!== peg
$FAILED
) {
866 if (s0
=== peg
$FAILED
) {
869 s2
= input
.charAt(peg
$currPos
);
870 if (peg
$r4
.test(s2
)) {
874 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e18
); }
876 if (s2
!== peg
$FAILED
) {
877 while (s2
!== peg
$FAILED
) {
879 s2
= input
.charAt(peg
$currPos
);
880 if (peg
$r4
.test(s2
)) {
884 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e18
); }
890 if (s1
!== peg
$FAILED
) {
897 if (s0
=== peg
$FAILED
) {
899 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e15
); }
905 function peg
$parseOWS() {
910 s1
= input
.charAt(peg
$currPos
);
911 if (peg
$r5
.test(s1
)) {
915 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e20
); }
917 while (s1
!== peg
$FAILED
) {
919 s1
= input
.charAt(peg
$currPos
);
920 if (peg
$r5
.test(s1
)) {
924 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e20
); }
929 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e19
); }
934 function peg
$parseBWS() {
941 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e21
); }
946 function peg
$parseextendedValue() {
947 var s0
, s1
, s2
, s3
, s4
, s5
, s6
;
952 s2
= input
.charAt(peg
$currPos
);
953 if (peg
$r6
.test(s2
)) {
957 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
959 while (s2
!== peg
$FAILED
) {
961 s2
= input
.charAt(peg
$currPos
);
962 if (peg
$r6
.test(s2
)) {
966 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
969 s2
= input
.charAt(peg
$currPos
);
970 if (peg
$r7
.test(s2
)) {
974 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e24
); }
976 if (s2
!== peg
$FAILED
) {
978 s4
= input
.charAt(peg
$currPos
);
979 if (peg
$r6
.test(s4
)) {
983 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
985 while (s4
!== peg
$FAILED
) {
987 s4
= input
.charAt(peg
$currPos
);
988 if (peg
$r6
.test(s4
)) {
992 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
995 s4
= input
.charAt(peg
$currPos
);
996 if (peg
$r7
.test(s4
)) {
1000 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e24
); }
1002 if (s4
!== peg
$FAILED
) {
1004 if (input
.length
> peg
$currPos
) {
1005 s6
= input
.charAt(peg
$currPos
);
1009 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1011 while (s6
!== peg
$FAILED
) {
1013 if (input
.length
> peg
$currPos
) {
1014 s6
= input
.charAt(peg
$currPos
);
1018 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1022 s0
= peg
$f10(s1
, s3
, s5
);
1031 if (s0
=== peg
$FAILED
) {
1034 if (input
.length
> peg
$currPos
) {
1035 s2
= input
.charAt(peg
$currPos
);
1039 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1041 while (s2
!== peg
$FAILED
) {
1043 if (input
.length
> peg
$currPos
) {
1044 s2
= input
.charAt(peg
$currPos
);
1048 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1056 if (s0
=== peg
$FAILED
) {
1058 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e22
); }
1065 // Per-parsing tracking of attributes which should be ignored after first occurrence.
1066 let seenAttributes
= [];
1068 peg
$result
= peg
$startRuleFunction();
1070 if (options
.peg
$library
) {
1071 return /** @type {any} */ ({
1075 peg
$maxFailExpected
,
1079 if (peg
$result
!== peg
$FAILED
&& peg
$currPos
=== input
.length
) {
1082 if (peg
$result
!== peg
$FAILED
&& peg
$currPos
< input
.length
) {
1083 peg
$fail(peg
$endExpectation());
1086 throw peg
$buildStructuredError(
1087 peg
$maxFailExpected
,
1088 peg
$maxFailPos
< input
.length
? input
.charAt(peg
$maxFailPos
) : null,
1089 peg
$maxFailPos
< input
.length
1090 ? peg
$computeLocation(peg
$maxFailPos
, peg
$maxFailPos
+ 1)
1091 : peg
$computeLocation(peg
$maxFailPos
, peg
$maxFailPos
)
1097 StartRules: ["links", "extendedValue"],
1098 SyntaxError: peg
$SyntaxError
,