1 // Generated by Peggy 3.0.2.
3 // https://peggyjs.org/
8 /* Generated code is not pretty, ignore a lot of this horrorstyle. */
9 /* eslint-disable no-control-regex */
10 /* eslint-disable security/detect-object-injection */
11 /* eslint-disable brace-style */
12 /* eslint-disable comma-dangle */
13 /* eslint-disable vars-on-top */
14 /* eslint-disable no-unused-vars */
15 /* eslint-disable camelcase */
16 /* eslint-disable indent */
17 /* eslint-disable semi */
18 /* eslint-disable jsdoc/require-jsdoc */
19 /* eslint-disable sonarjs/cognitive-complexity */
20 /* eslint-disable sonarjs/no-identical-functions */
22 function makeString(o
) {
26 const onlyFirstAttributes
= [
35 function peg
$subclass(child
, parent
) {
36 function C() { this.constructor = child
; }
37 C
.prototype = parent
.prototype;
38 child
.prototype = new C();
41 function peg
$SyntaxError(message
, expected
, found
, location
) {
42 var self
= Error
.call(this, message
);
43 // istanbul ignore next Check is a necessary evil to support older environments
44 if (Object
.setPrototypeOf
) {
45 Object
.setPrototypeOf(self
, peg
$SyntaxError
.prototype);
47 self
.expected
= expected
;
49 self
.location
= location
;
50 self
.name
= "SyntaxError";
54 peg
$subclass(peg
$SyntaxError
, Error
);
56 function peg
$padEnd(str
, targetLength
, padString
) {
57 padString
= padString
|| " ";
58 if (str
.length
> targetLength
) { return str
; }
59 targetLength
-= str
.length
;
60 padString
+= padString
.repeat(targetLength
);
61 return str
+ padString
.slice(0, targetLength
);
64 peg
$SyntaxError
.prototype.format = function(sources
) {
65 var str
= "Error: " + this.message
;
69 for (k
= 0; k
< sources
.length
; k
++) {
70 if (sources
[k
].source
=== this.location
.source
) {
71 src
= sources
[k
].text
.split(/\r\n|\n|\r/g);
75 var s
= this.location
.start
;
76 var offset_s
= (this.location
.source
&& (typeof this.location
.source
.offset
=== "function"))
77 ? this.location
.source
.offset(s
)
79 var loc
= this.location
.source
+ ":" + offset_s
.line
+ ":" + offset_s
.column
;
81 var e
= this.location
.end
;
82 var filler
= peg
$padEnd("", offset_s
.line
.toString().length
, ' ');
83 var line
= src
[s
.line
- 1];
84 var last
= s
.line
=== e
.line
? e
.column : line
.length
+ 1;
85 var hatLen
= (last
- s
.column
) || 1;
86 str
+= "\n --> " + loc
+ "\n"
88 + offset_s
.line
+ " | " + line
+ "\n"
89 + filler
+ " | " + peg
$padEnd("", s
.column
- 1, ' ')
90 + peg
$padEnd("", hatLen
, "^");
92 str
+= "\n at " + loc
;
98 peg
$SyntaxError
.buildMessage = function(expected
, found
) {
99 var DESCRIBE_EXPECTATION_FNS
= {
100 literal: function(expectation
) {
101 return "\"" + literalEscape(expectation
.text
) + "\"";
104 class: function(expectation
) {
105 var escapedParts
= expectation
.parts
.map(function(part
) {
106 return Array
.isArray(part
)
107 ? classEscape(part
[0]) + "-" + classEscape(part
[1])
111 return "[" + (expectation
.inverted
? "^" : "") + escapedParts
.join("") + "]";
115 return "any character";
119 return "end of input";
122 other: function(expectation
) {
123 return expectation
.description
;
128 return ch
.charCodeAt(0).toString(16).toUpperCase();
131 function literalEscape(s
) {
133 .replace(/\\/g
, "\\\\")
134 .replace(/"/g, "\\\"")
135 .replace(/\0/g, "\\0")
136 .replace(/\t/g, "\\t
")
137 .replace(/\n/g, "\\n
")
138 .replace(/\r/g, "\\r
")
139 .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0
" + hex(ch); })
140 .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x
" + hex(ch); });
143 function classEscape(s) {
145 .replace(/\\/g, "\\\\")
146 .replace(/\]/g, "\\]")
147 .replace(/\^/g, "\\^")
148 .replace(/-/g, "\\-")
149 .replace(/\0/g, "\\0")
150 .replace(/\t/g, "\\t
")
151 .replace(/\n/g, "\\n
")
152 .replace(/\r/g, "\\r
")
153 .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0
" + hex(ch); })
154 .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x
" + hex(ch); });
157 function describeExpectation(expectation) {
158 return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
161 function describeExpected(expected) {
162 var descriptions = expected.map(describeExpectation);
167 if (descriptions.length > 0) {
168 for (i = 1, j = 1; i < descriptions.length; i++) {
169 if (descriptions[i - 1] !== descriptions[i]) {
170 descriptions[j] = descriptions[i];
174 descriptions.length = j;
177 switch (descriptions.length) {
179 return descriptions[0];
182 return descriptions[0] + " or
" + descriptions[1];
185 return descriptions.slice(0, -1).join(", ")
187 + descriptions[descriptions.length - 1];
191 function describeFound(found) {
192 return found ? "\"" + literalEscape(found) + "\"" : "end
of input
";
195 return "Expected
" + describeExpected(expected) + " but
" + describeFound(found) + " found
.";
198 function peg$parse(input, options) {
199 options = options !== undefined ? options : {};
202 var peg$source = options.grammarSource;
204 var peg$startRuleFunctions = { links: peg$parselinks, extendedValue: peg$parseextendedValue };
205 var peg$startRuleFunction = peg$parselinks;
214 var peg$r0 = /^[^>]/;
215 var peg$r1 = /^[a-zA-Z]/;
217 var peg
$r3
= /^[^"]/;
218 var peg
$r4
= /^[^";,]/;
220 var peg
$r6
= /^[^']/;
223 var peg
$e0
= peg
$otherExpectation("links");
224 var peg
$e1
= peg
$otherExpectation("link-value");
225 var peg
$e2
= peg
$literalExpectation(",", false);
226 var peg
$e3
= peg
$otherExpectation("uri-reference");
227 var peg
$e4
= peg
$literalExpectation("<", false);
228 var peg
$e5
= peg
$literalExpectation(">", false);
229 var peg
$e6
= peg
$literalExpectation(";", false);
230 var peg
$e7
= peg
$otherExpectation("uri");
231 var peg
$e8
= peg
$classExpectation([">"], true, false);
232 var peg
$e9
= peg
$otherExpectation("attributes");
233 var peg
$e10
= peg
$otherExpectation("link-param");
234 var peg
$e11
= peg
$literalExpectation("=", false);
235 var peg
$e12
= peg
$otherExpectation("name");
236 var peg
$e13
= peg
$classExpectation([["a", "z"], ["A", "Z"]], false, false);
237 var peg
$e14
= peg
$literalExpectation("*", false);
238 var peg
$e15
= peg
$otherExpectation("value");
239 var peg
$e16
= peg
$classExpectation(["\""], false, false);
240 var peg
$e17
= peg
$classExpectation(["\""], true, false);
241 var peg
$e18
= peg
$classExpectation(["\"", ";", ","], true, false);
242 var peg
$e19
= peg
$otherExpectation("whitespace");
243 var peg
$e20
= peg
$classExpectation([" "], false, false);
244 var peg
$e21
= peg
$otherExpectation("bad whitespace");
245 var peg
$e22
= peg
$otherExpectation("extended-value");
246 var peg
$e23
= peg
$classExpectation(["'"], true, false);
247 var peg
$e24
= peg
$classExpectation(["'"], false, false);
248 var peg
$e25
= peg
$anyExpectation();
250 var peg
$f0 = function(links
) {
253 var peg
$f1 = function(uriReference
, attributes
) {
256 target: uriReference
,
260 var peg
$f2 = function(uri
) {
263 var peg
$f3 = function(uri
) {
264 return makeString(uri
);
266 var peg
$f4 = function(attrs
) {
267 return attrs
.filter((a
) => a
);
269 var peg
$f5 = function(name
, value
) {
270 if (onlyFirstAttributes
.includes(name
.name
)) {
271 if (seenAttributes
.includes(name
.name
)) {
272 // Repeat of singleton attribute, ignore it.
275 seenAttributes
.push(name
.name
);
282 var peg
$f6 = function(name
) {
288 var peg
$f7 = function(name
, extended
) {
290 name: makeString(name
.concat(extended
)).toLowerCase(),
291 extended: !!extended
,
294 var peg
$f8 = function(value
) {
296 value: makeString(value
),
299 var peg
$f9 = function(value
) {
301 value: makeString(value
),
304 var peg
$f10 = function(encoding
, language
, value
) {
306 encoding: encoding
.length
? makeString(encoding
) : 'UTF-8',
307 language: language
.length
? makeString(language
) : null,
308 value: decodeURIComponent(makeString(value
)),
311 var peg
$f11 = function(value
) {
315 value: makeString(value
),
319 var peg
$savedPos
= 0;
320 var peg
$posDetailsCache
= [{ line: 1, column: 1 }];
321 var peg
$maxFailPos
= 0;
322 var peg
$maxFailExpected
= [];
323 var peg
$silentFails
= 0;
327 if ("startRule" in options
) {
328 if (!(options
.startRule
in peg
$startRuleFunctions
)) {
329 throw new Error("Can't start parsing from rule \"" + options
.startRule
+ "\".");
332 peg
$startRuleFunction
= peg
$startRuleFunctions
[options
.startRule
];
336 return input
.substring(peg
$savedPos
, peg
$currPos
);
351 function location() {
352 return peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
355 function expected(description
, location
) {
356 location
= location
!== undefined
358 : peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
360 throw peg
$buildStructuredError(
361 [peg
$otherExpectation(description
)],
362 input
.substring(peg
$savedPos
, peg
$currPos
),
367 function error(message
, location
) {
368 location
= location
!== undefined
370 : peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
372 throw peg
$buildSimpleError(message
, location
);
375 function peg
$literalExpectation(text
, ignoreCase
) {
376 return { type: "literal", text: text
, ignoreCase: ignoreCase
};
379 function peg
$classExpectation(parts
, inverted
, ignoreCase
) {
380 return { type: "class", parts: parts
, inverted: inverted
, ignoreCase: ignoreCase
};
383 function peg
$anyExpectation() {
384 return { type: "any" };
387 function peg
$endExpectation() {
388 return { type: "end" };
391 function peg
$otherExpectation(description
) {
392 return { type: "other", description: description
};
395 function peg
$computePosDetails(pos
) {
396 var details
= peg
$posDetailsCache
[pos
];
403 while (!peg
$posDetailsCache
[p
]) {
407 details
= peg
$posDetailsCache
[p
];
410 column: details
.column
414 if (input
.charCodeAt(p
) === 10) {
424 peg
$posDetailsCache
[pos
] = details
;
430 function peg
$computeLocation(startPos
, endPos
, offset
) {
431 var startPosDetails
= peg
$computePosDetails(startPos
);
432 var endPosDetails
= peg
$computePosDetails(endPos
);
438 line: startPosDetails
.line
,
439 column: startPosDetails
.column
443 line: endPosDetails
.line
,
444 column: endPosDetails
.column
447 if (offset
&& peg
$source
&& (typeof peg
$source
.offset
=== "function")) {
448 res
.start
= peg
$source
.offset(res
.start
);
449 res
.end
= peg
$source
.offset(res
.end
);
454 function peg
$fail(expected
) {
455 if (peg
$currPos
< peg
$maxFailPos
) { return; }
457 if (peg
$currPos
> peg
$maxFailPos
) {
458 peg
$maxFailPos
= peg
$currPos
;
459 peg
$maxFailExpected
= [];
462 peg
$maxFailExpected
.push(expected
);
465 function peg
$buildSimpleError(message
, location
) {
466 return new peg
$SyntaxError(message
, null, null, location
);
469 function peg
$buildStructuredError(expected
, found
, location
) {
470 return new peg
$SyntaxError(
471 peg
$SyntaxError
.buildMessage(expected
, found
),
478 function peg
$parselinks() {
484 s2
= peg
$parselinkValue();
485 if (s2
!== peg
$FAILED
) {
486 while (s2
!== peg
$FAILED
) {
488 s2
= peg
$parselinkValue();
493 if (s1
!== peg
$FAILED
) {
499 if (s0
=== peg
$FAILED
) {
501 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e0
); }
507 function peg
$parselinkValue() {
508 var s0
, s1
, s2
, s3
, s4
, s5
;
512 s1
= peg
$parseuriReference();
513 if (s1
!== peg
$FAILED
) {
515 s3
= peg
$parseattributes();
516 if (s3
!== peg
$FAILED
) {
517 if (input
.charCodeAt(peg
$currPos
) === 44) {
522 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e2
); }
524 if (s4
=== peg
$FAILED
) {
539 if (s0
=== peg
$FAILED
) {
541 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e1
); }
547 function peg
$parseuriReference() {
548 var s0
, s1
, s2
, s3
, s4
;
552 if (input
.charCodeAt(peg
$currPos
) === 60) {
557 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e4
); }
559 if (s1
!== peg
$FAILED
) {
561 if (s2
!== peg
$FAILED
) {
562 if (input
.charCodeAt(peg
$currPos
) === 62) {
567 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e5
); }
569 if (s3
!== peg
$FAILED
) {
570 if (input
.charCodeAt(peg
$currPos
) === 59) {
575 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
577 if (s4
!== peg
$FAILED
) {
597 if (s0
=== peg
$FAILED
) {
599 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e3
); }
605 function peg
$parseuri() {
611 if (peg
$r0
.test(input
.charAt(peg
$currPos
))) {
612 s2
= input
.charAt(peg
$currPos
);
616 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e8
); }
618 if (s2
!== peg
$FAILED
) {
619 while (s2
!== peg
$FAILED
) {
621 if (peg
$r0
.test(input
.charAt(peg
$currPos
))) {
622 s2
= input
.charAt(peg
$currPos
);
626 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e8
); }
632 if (s1
!== peg
$FAILED
) {
638 if (s0
=== peg
$FAILED
) {
640 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e7
); }
646 function peg
$parseattributes() {
652 s2
= peg
$parselinkParam();
653 if (s2
!== peg
$FAILED
) {
654 while (s2
!== peg
$FAILED
) {
656 s2
= peg
$parselinkParam();
661 if (s1
!== peg
$FAILED
) {
667 if (s0
=== peg
$FAILED
) {
669 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e9
); }
675 function peg
$parselinkParam() {
676 var s0
, s1
, s2
, s3
, s4
, s5
, s6
, s7
;
680 s1
= peg
$parsename();
681 if (s1
!== peg
$FAILED
) {
683 if (input
.charCodeAt(peg
$currPos
) === 61) {
688 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e11
); }
690 if (s3
!== peg
$FAILED
) {
692 s5
= peg
$parsevalue();
693 if (s5
!== peg
$FAILED
) {
694 if (input
.charCodeAt(peg
$currPos
) === 59) {
699 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
701 if (s6
=== peg
$FAILED
) {
719 if (s0
=== peg
$FAILED
) {
721 s1
= peg
$parsename();
722 if (s1
!== peg
$FAILED
) {
724 if (input
.charCodeAt(peg
$currPos
) === 59) {
729 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
731 if (s3
=== peg
$FAILED
) {
743 if (s0
=== peg
$FAILED
) {
745 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e10
); }
751 function peg
$parsename() {
757 if (peg
$r1
.test(input
.charAt(peg
$currPos
))) {
758 s2
= input
.charAt(peg
$currPos
);
762 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e13
); }
764 if (s2
!== peg
$FAILED
) {
765 while (s2
!== peg
$FAILED
) {
767 if (peg
$r1
.test(input
.charAt(peg
$currPos
))) {
768 s2
= input
.charAt(peg
$currPos
);
772 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e13
); }
778 if (s1
!== peg
$FAILED
) {
779 if (input
.charCodeAt(peg
$currPos
) === 42) {
784 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e14
); }
786 if (s2
=== peg
$FAILED
) {
796 if (s0
=== peg
$FAILED
) {
798 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e12
); }
804 function peg
$parsevalue() {
809 if (peg
$r2
.test(input
.charAt(peg
$currPos
))) {
810 s1
= input
.charAt(peg
$currPos
);
814 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e16
); }
816 if (s1
!== peg
$FAILED
) {
818 if (peg
$r3
.test(input
.charAt(peg
$currPos
))) {
819 s3
= input
.charAt(peg
$currPos
);
823 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e17
); }
825 if (s3
!== peg
$FAILED
) {
826 while (s3
!== peg
$FAILED
) {
828 if (peg
$r3
.test(input
.charAt(peg
$currPos
))) {
829 s3
= input
.charAt(peg
$currPos
);
833 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e17
); }
839 if (s2
!== peg
$FAILED
) {
840 if (peg
$r2
.test(input
.charAt(peg
$currPos
))) {
841 s3
= input
.charAt(peg
$currPos
);
845 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e16
); }
847 if (s3
!== peg
$FAILED
) {
862 if (s0
=== peg
$FAILED
) {
865 if (peg
$r4
.test(input
.charAt(peg
$currPos
))) {
866 s2
= input
.charAt(peg
$currPos
);
870 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e18
); }
872 if (s2
!== peg
$FAILED
) {
873 while (s2
!== peg
$FAILED
) {
875 if (peg
$r4
.test(input
.charAt(peg
$currPos
))) {
876 s2
= input
.charAt(peg
$currPos
);
880 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e18
); }
886 if (s1
!== peg
$FAILED
) {
893 if (s0
=== peg
$FAILED
) {
895 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e15
); }
901 function peg
$parseOWS() {
906 if (peg
$r5
.test(input
.charAt(peg
$currPos
))) {
907 s1
= input
.charAt(peg
$currPos
);
911 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e20
); }
913 while (s1
!== peg
$FAILED
) {
915 if (peg
$r5
.test(input
.charAt(peg
$currPos
))) {
916 s1
= input
.charAt(peg
$currPos
);
920 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e20
); }
925 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e19
); }
930 function peg
$parseBWS() {
937 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e21
); }
942 function peg
$parseextendedValue() {
943 var s0
, s1
, s2
, s3
, s4
, s5
, s6
;
948 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
949 s2
= input
.charAt(peg
$currPos
);
953 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
955 while (s2
!== peg
$FAILED
) {
957 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
958 s2
= input
.charAt(peg
$currPos
);
962 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
965 if (peg
$r7
.test(input
.charAt(peg
$currPos
))) {
966 s2
= input
.charAt(peg
$currPos
);
970 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e24
); }
972 if (s2
!== peg
$FAILED
) {
974 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
975 s4
= input
.charAt(peg
$currPos
);
979 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
981 while (s4
!== peg
$FAILED
) {
983 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
984 s4
= input
.charAt(peg
$currPos
);
988 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
991 if (peg
$r7
.test(input
.charAt(peg
$currPos
))) {
992 s4
= input
.charAt(peg
$currPos
);
996 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e24
); }
998 if (s4
!== peg
$FAILED
) {
1000 if (input
.length
> peg
$currPos
) {
1001 s6
= input
.charAt(peg
$currPos
);
1005 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1007 while (s6
!== peg
$FAILED
) {
1009 if (input
.length
> peg
$currPos
) {
1010 s6
= input
.charAt(peg
$currPos
);
1014 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1018 s0
= peg
$f10(s1
, s3
, s5
);
1027 if (s0
=== peg
$FAILED
) {
1030 if (input
.length
> peg
$currPos
) {
1031 s2
= input
.charAt(peg
$currPos
);
1035 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1037 while (s2
!== peg
$FAILED
) {
1039 if (input
.length
> peg
$currPos
) {
1040 s2
= input
.charAt(peg
$currPos
);
1044 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1052 if (s0
=== peg
$FAILED
) {
1054 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e22
); }
1061 // Per-parsing tracking of attributes which should be ignored after first occurrence.
1062 let seenAttributes
= [];
1064 peg
$result
= peg
$startRuleFunction();
1066 if (peg
$result
!== peg
$FAILED
&& peg
$currPos
=== input
.length
) {
1069 if (peg
$result
!== peg
$FAILED
&& peg
$currPos
< input
.length
) {
1070 peg
$fail(peg
$endExpectation());
1073 throw peg
$buildStructuredError(
1074 peg
$maxFailExpected
,
1075 peg
$maxFailPos
< input
.length
? input
.charAt(peg
$maxFailPos
) : null,
1076 peg
$maxFailPos
< input
.length
1077 ? peg
$computeLocation(peg
$maxFailPos
, peg
$maxFailPos
+ 1)
1078 : peg
$computeLocation(peg
$maxFailPos
, peg
$maxFailPos
)
1084 SyntaxError: peg
$SyntaxError
,