1 // Generated by Peggy 2.0.1.
3 // https://peggyjs.org/
8 /* Generated code is not pretty, ignore a lot of this horrorstyle. */
9 /* eslint-disable quotes */
10 /* eslint-disable vars-on-top */
11 /* eslint-disable no-control-regex */
12 /* eslint-disable security/detect-object-injection */
13 /* eslint-disable vars-on-top */
14 /* eslint-disable brace-style */
15 /* eslint-disable comma-dangle */
16 /* eslint-disable no-unused-vars */
18 function makeString(o
) {
22 const onlyFirstAttributes
= [
31 function peg
$subclass(child
, parent
) {
32 function C() { this.constructor = child
; }
33 C
.prototype = parent
.prototype;
34 child
.prototype = new C();
37 function peg
$SyntaxError(message
, expected
, found
, location
) {
38 var self
= Error
.call(this, message
);
39 // istanbul ignore next Check is a necessary evil to support older environments
40 if (Object
.setPrototypeOf
) {
41 Object
.setPrototypeOf(self
, peg
$SyntaxError
.prototype);
43 self
.expected
= expected
;
45 self
.location
= location
;
46 self
.name
= "SyntaxError";
50 peg
$subclass(peg
$SyntaxError
, Error
);
52 function peg
$padEnd(str
, targetLength
, padString
) {
53 padString
= padString
|| " ";
54 if (str
.length
> targetLength
) { return str
; }
55 targetLength
-= str
.length
;
56 padString
+= padString
.repeat(targetLength
);
57 return str
+ padString
.slice(0, targetLength
);
60 peg
$SyntaxError
.prototype.format = function(sources
) {
61 var str
= "Error: " + this.message
;
65 for (k
= 0; k
< sources
.length
; k
++) {
66 if (sources
[k
].source
=== this.location
.source
) {
67 src
= sources
[k
].text
.split(/\r\n|\n|\r/g);
71 var s
= this.location
.start
;
72 var loc
= this.location
.source
+ ":" + s
.line
+ ":" + s
.column
;
74 var e
= this.location
.end
;
75 var filler
= peg
$padEnd("", s
.line
.toString().length
, ' ');
76 var line
= src
[s
.line
- 1];
77 var last
= s
.line
=== e
.line
? e
.column : line
.length
+ 1;
78 var hatLen
= (last
- s
.column
) || 1;
79 str
+= "\n --> " + loc
+ "\n"
81 + s
.line
+ " | " + line
+ "\n"
82 + filler
+ " | " + peg
$padEnd("", s
.column
- 1, ' ')
83 + peg
$padEnd("", hatLen
, "^");
85 str
+= "\n at " + loc
;
91 peg
$SyntaxError
.buildMessage = function(expected
, found
) {
92 var DESCRIBE_EXPECTATION_FNS
= {
93 literal: function(expectation
) {
94 return "\"" + literalEscape(expectation
.text
) + "\"";
97 class: function(expectation
) {
98 var escapedParts
= expectation
.parts
.map(function(part
) {
99 return Array
.isArray(part
)
100 ? classEscape(part
[0]) + "-" + classEscape(part
[1])
104 return "[" + (expectation
.inverted
? "^" : "") + escapedParts
.join("") + "]";
108 return "any character";
112 return "end of input";
115 other: function(expectation
) {
116 return expectation
.description
;
121 return ch
.charCodeAt(0).toString(16).toUpperCase();
124 function literalEscape(s
) {
126 .replace(/\\/g
, "\\\\")
127 .replace(/"/g, "\\\"")
128 .replace(/\0/g, "\\0")
129 .replace(/\t/g, "\\t
")
130 .replace(/\n/g, "\\n
")
131 .replace(/\r/g, "\\r
")
132 .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0
" + hex(ch); })
133 .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x
" + hex(ch); });
136 function classEscape(s) {
138 .replace(/\\/g, "\\\\")
139 .replace(/\]/g, "\\]")
140 .replace(/\^/g, "\\^")
141 .replace(/-/g, "\\-")
142 .replace(/\0/g, "\\0")
143 .replace(/\t/g, "\\t
")
144 .replace(/\n/g, "\\n
")
145 .replace(/\r/g, "\\r
")
146 .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0
" + hex(ch); })
147 .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x
" + hex(ch); });
150 function describeExpectation(expectation) {
151 return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
154 function describeExpected(expected) {
155 var descriptions = expected.map(describeExpectation);
160 if (descriptions.length > 0) {
161 for (i = 1, j = 1; i < descriptions.length; i++) {
162 if (descriptions[i - 1] !== descriptions[i]) {
163 descriptions[j] = descriptions[i];
167 descriptions.length = j;
170 switch (descriptions.length) {
172 return descriptions[0];
175 return descriptions[0] + " or
" + descriptions[1];
178 return descriptions.slice(0, -1).join(", ")
180 + descriptions[descriptions.length - 1];
184 function describeFound(found) {
185 return found ? "\"" + literalEscape(found) + "\"" : "end
of input
";
188 return "Expected
" + describeExpected(expected) + " but
" + describeFound(found) + " found
.";
191 function peg$parse(input, options) {
192 options = options !== undefined ? options : {};
195 var peg$source = options.grammarSource;
197 var peg$startRuleFunctions = { links: peg$parselinks, extendedValue: peg$parseextendedValue };
198 var peg$startRuleFunction = peg$parselinks;
207 var peg$r0 = /^[^>]/;
208 var peg$r1 = /^[a-zA-Z]/;
210 var peg
$r3
= /^[^"]/;
211 var peg
$r4
= /^[^";,]/;
213 var peg
$r6
= /^[^']/;
216 var peg
$e0
= peg
$otherExpectation("links");
217 var peg
$e1
= peg
$otherExpectation("link-value");
218 var peg
$e2
= peg
$literalExpectation(",", false);
219 var peg
$e3
= peg
$otherExpectation("uri-reference");
220 var peg
$e4
= peg
$literalExpectation("<", false);
221 var peg
$e5
= peg
$literalExpectation(">", false);
222 var peg
$e6
= peg
$literalExpectation(";", false);
223 var peg
$e7
= peg
$otherExpectation("uri");
224 var peg
$e8
= peg
$classExpectation([">"], true, false);
225 var peg
$e9
= peg
$otherExpectation("attributes");
226 var peg
$e10
= peg
$otherExpectation("link-param");
227 var peg
$e11
= peg
$literalExpectation("=", false);
228 var peg
$e12
= peg
$otherExpectation("name");
229 var peg
$e13
= peg
$classExpectation([["a", "z"], ["A", "Z"]], false, false);
230 var peg
$e14
= peg
$literalExpectation("*", false);
231 var peg
$e15
= peg
$otherExpectation("value");
232 var peg
$e16
= peg
$classExpectation(["\""], false, false);
233 var peg
$e17
= peg
$classExpectation(["\""], true, false);
234 var peg
$e18
= peg
$classExpectation(["\"", ";", ","], true, false);
235 var peg
$e19
= peg
$otherExpectation("whitespace");
236 var peg
$e20
= peg
$classExpectation([" "], false, false);
237 var peg
$e21
= peg
$otherExpectation("bad whitespace");
238 var peg
$e22
= peg
$otherExpectation("extended-value");
239 var peg
$e23
= peg
$classExpectation(["'"], true, false);
240 var peg
$e24
= peg
$classExpectation(["'"], false, false);
241 var peg
$e25
= peg
$anyExpectation();
243 var peg
$f0 = function(links
) {
246 var peg
$f1 = function(uriReference
, attributes
) {
249 target: uriReference
,
253 var peg
$f2 = function(uri
) {
256 var peg
$f3 = function(uri
) {
257 return makeString(uri
);
259 var peg
$f4 = function(attrs
) {
260 return attrs
.filter((a
) => a
);
262 var peg
$f5 = function(name
, value
) {
263 if (onlyFirstAttributes
.includes(name
.name
)) {
264 if (seenAttributes
.includes(name
.name
)) {
265 // Repeat of singleton attribute, ignore it.
268 seenAttributes
.push(name
.name
);
275 var peg
$f6 = function(name
) {
281 var peg
$f7 = function(name
, extended
) {
283 name: makeString(name
.concat(extended
)).toLowerCase(),
284 extended: !!extended
,
287 var peg
$f8 = function(value
) {
289 value: makeString(value
),
292 var peg
$f9 = function(value
) {
294 value: makeString(value
),
297 var peg
$f10 = function(encoding
, language
, value
) {
299 encoding: encoding
.length
? makeString(encoding
) : 'UTF-8',
300 language: language
.length
? makeString(language
) : null,
301 value: decodeURIComponent(makeString(value
)),
304 var peg
$f11 = function(value
) {
308 value: makeString(value
),
312 var peg
$savedPos
= 0;
313 var peg
$posDetailsCache
= [{ line: 1, column: 1 }];
314 var peg
$maxFailPos
= 0;
315 var peg
$maxFailExpected
= [];
316 var peg
$silentFails
= 0;
320 if ("startRule" in options
) {
321 if (!(options
.startRule
in peg
$startRuleFunctions
)) {
322 throw new Error("Can't start parsing from rule \"" + options
.startRule
+ "\".");
325 peg
$startRuleFunction
= peg
$startRuleFunctions
[options
.startRule
];
329 return input
.substring(peg
$savedPos
, peg
$currPos
);
344 function location() {
345 return peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
348 function expected(description
, location
) {
349 location
= location
!== undefined
351 : peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
353 throw peg
$buildStructuredError(
354 [peg
$otherExpectation(description
)],
355 input
.substring(peg
$savedPos
, peg
$currPos
),
360 function error(message
, location
) {
361 location
= location
!== undefined
363 : peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
365 throw peg
$buildSimpleError(message
, location
);
368 function peg
$literalExpectation(text
, ignoreCase
) {
369 return { type: "literal", text: text
, ignoreCase: ignoreCase
};
372 function peg
$classExpectation(parts
, inverted
, ignoreCase
) {
373 return { type: "class", parts: parts
, inverted: inverted
, ignoreCase: ignoreCase
};
376 function peg
$anyExpectation() {
377 return { type: "any" };
380 function peg
$endExpectation() {
381 return { type: "end" };
384 function peg
$otherExpectation(description
) {
385 return { type: "other", description: description
};
388 function peg
$computePosDetails(pos
) {
389 var details
= peg
$posDetailsCache
[pos
];
396 while (!peg
$posDetailsCache
[p
]) {
400 details
= peg
$posDetailsCache
[p
];
403 column: details
.column
407 if (input
.charCodeAt(p
) === 10) {
417 peg
$posDetailsCache
[pos
] = details
;
423 function peg
$computeLocation(startPos
, endPos
) {
424 var startPosDetails
= peg
$computePosDetails(startPos
);
425 var endPosDetails
= peg
$computePosDetails(endPos
);
431 line: startPosDetails
.line
,
432 column: startPosDetails
.column
436 line: endPosDetails
.line
,
437 column: endPosDetails
.column
442 function peg
$fail(expected
) {
443 if (peg
$currPos
< peg
$maxFailPos
) { return; }
445 if (peg
$currPos
> peg
$maxFailPos
) {
446 peg
$maxFailPos
= peg
$currPos
;
447 peg
$maxFailExpected
= [];
450 peg
$maxFailExpected
.push(expected
);
453 function peg
$buildSimpleError(message
, location
) {
454 return new peg
$SyntaxError(message
, null, null, location
);
457 function peg
$buildStructuredError(expected
, found
, location
) {
458 return new peg
$SyntaxError(
459 peg
$SyntaxError
.buildMessage(expected
, found
),
466 function peg
$parselinks() {
472 s2
= peg
$parselinkValue();
473 if (s2
!== peg
$FAILED
) {
474 while (s2
!== peg
$FAILED
) {
476 s2
= peg
$parselinkValue();
481 if (s1
!== peg
$FAILED
) {
487 if (s0
=== peg
$FAILED
) {
489 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e0
); }
495 function peg
$parselinkValue() {
496 var s0
, s1
, s2
, s3
, s4
, s5
;
500 s1
= peg
$parseuriReference();
501 if (s1
!== peg
$FAILED
) {
503 s3
= peg
$parseattributes();
504 if (s3
!== peg
$FAILED
) {
505 if (input
.charCodeAt(peg
$currPos
) === 44) {
510 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e2
); }
512 if (s4
=== peg
$FAILED
) {
527 if (s0
=== peg
$FAILED
) {
529 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e1
); }
535 function peg
$parseuriReference() {
536 var s0
, s1
, s2
, s3
, s4
;
540 if (input
.charCodeAt(peg
$currPos
) === 60) {
545 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e4
); }
547 if (s1
!== peg
$FAILED
) {
549 if (s2
!== peg
$FAILED
) {
550 if (input
.charCodeAt(peg
$currPos
) === 62) {
555 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e5
); }
557 if (s3
!== peg
$FAILED
) {
558 if (input
.charCodeAt(peg
$currPos
) === 59) {
563 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
565 if (s4
!== peg
$FAILED
) {
585 if (s0
=== peg
$FAILED
) {
587 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e3
); }
593 function peg
$parseuri() {
599 if (peg
$r0
.test(input
.charAt(peg
$currPos
))) {
600 s2
= input
.charAt(peg
$currPos
);
604 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e8
); }
606 if (s2
!== peg
$FAILED
) {
607 while (s2
!== peg
$FAILED
) {
609 if (peg
$r0
.test(input
.charAt(peg
$currPos
))) {
610 s2
= input
.charAt(peg
$currPos
);
614 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e8
); }
620 if (s1
!== peg
$FAILED
) {
626 if (s0
=== peg
$FAILED
) {
628 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e7
); }
634 function peg
$parseattributes() {
640 s2
= peg
$parselinkParam();
641 if (s2
!== peg
$FAILED
) {
642 while (s2
!== peg
$FAILED
) {
644 s2
= peg
$parselinkParam();
649 if (s1
!== peg
$FAILED
) {
655 if (s0
=== peg
$FAILED
) {
657 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e9
); }
663 function peg
$parselinkParam() {
664 var s0
, s1
, s2
, s3
, s4
, s5
, s6
, s7
;
668 s1
= peg
$parsename();
669 if (s1
!== peg
$FAILED
) {
671 if (input
.charCodeAt(peg
$currPos
) === 61) {
676 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e11
); }
678 if (s3
!== peg
$FAILED
) {
680 s5
= peg
$parsevalue();
681 if (s5
!== peg
$FAILED
) {
682 if (input
.charCodeAt(peg
$currPos
) === 59) {
687 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
689 if (s6
=== peg
$FAILED
) {
707 if (s0
=== peg
$FAILED
) {
709 s1
= peg
$parsename();
710 if (s1
!== peg
$FAILED
) {
712 if (input
.charCodeAt(peg
$currPos
) === 59) {
717 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
719 if (s3
=== peg
$FAILED
) {
731 if (s0
=== peg
$FAILED
) {
733 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e10
); }
739 function peg
$parsename() {
745 if (peg
$r1
.test(input
.charAt(peg
$currPos
))) {
746 s2
= input
.charAt(peg
$currPos
);
750 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e13
); }
752 if (s2
!== peg
$FAILED
) {
753 while (s2
!== peg
$FAILED
) {
755 if (peg
$r1
.test(input
.charAt(peg
$currPos
))) {
756 s2
= input
.charAt(peg
$currPos
);
760 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e13
); }
766 if (s1
!== peg
$FAILED
) {
767 if (input
.charCodeAt(peg
$currPos
) === 42) {
772 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e14
); }
774 if (s2
=== peg
$FAILED
) {
784 if (s0
=== peg
$FAILED
) {
786 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e12
); }
792 function peg
$parsevalue() {
797 if (peg
$r2
.test(input
.charAt(peg
$currPos
))) {
798 s1
= input
.charAt(peg
$currPos
);
802 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e16
); }
804 if (s1
!== peg
$FAILED
) {
806 if (peg
$r3
.test(input
.charAt(peg
$currPos
))) {
807 s3
= input
.charAt(peg
$currPos
);
811 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e17
); }
813 if (s3
!== peg
$FAILED
) {
814 while (s3
!== peg
$FAILED
) {
816 if (peg
$r3
.test(input
.charAt(peg
$currPos
))) {
817 s3
= input
.charAt(peg
$currPos
);
821 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e17
); }
827 if (s2
!== peg
$FAILED
) {
828 if (peg
$r2
.test(input
.charAt(peg
$currPos
))) {
829 s3
= input
.charAt(peg
$currPos
);
833 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e16
); }
835 if (s3
!== peg
$FAILED
) {
850 if (s0
=== peg
$FAILED
) {
853 if (peg
$r4
.test(input
.charAt(peg
$currPos
))) {
854 s2
= input
.charAt(peg
$currPos
);
858 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e18
); }
860 if (s2
!== peg
$FAILED
) {
861 while (s2
!== peg
$FAILED
) {
863 if (peg
$r4
.test(input
.charAt(peg
$currPos
))) {
864 s2
= input
.charAt(peg
$currPos
);
868 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e18
); }
874 if (s1
!== peg
$FAILED
) {
881 if (s0
=== peg
$FAILED
) {
883 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e15
); }
889 function peg
$parseOWS() {
894 if (peg
$r5
.test(input
.charAt(peg
$currPos
))) {
895 s1
= input
.charAt(peg
$currPos
);
899 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e20
); }
901 while (s1
!== peg
$FAILED
) {
903 if (peg
$r5
.test(input
.charAt(peg
$currPos
))) {
904 s1
= input
.charAt(peg
$currPos
);
908 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e20
); }
913 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e19
); }
918 function peg
$parseBWS() {
925 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e21
); }
930 function peg
$parseextendedValue() {
931 var s0
, s1
, s2
, s3
, s4
, s5
, s6
;
936 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
937 s2
= input
.charAt(peg
$currPos
);
941 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
943 while (s2
!== peg
$FAILED
) {
945 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
946 s2
= input
.charAt(peg
$currPos
);
950 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
953 if (peg
$r7
.test(input
.charAt(peg
$currPos
))) {
954 s2
= input
.charAt(peg
$currPos
);
958 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e24
); }
960 if (s2
!== peg
$FAILED
) {
962 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
963 s4
= input
.charAt(peg
$currPos
);
967 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
969 while (s4
!== peg
$FAILED
) {
971 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
972 s4
= input
.charAt(peg
$currPos
);
976 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
979 if (peg
$r7
.test(input
.charAt(peg
$currPos
))) {
980 s4
= input
.charAt(peg
$currPos
);
984 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e24
); }
986 if (s4
!== peg
$FAILED
) {
988 if (input
.length
> peg
$currPos
) {
989 s6
= input
.charAt(peg
$currPos
);
993 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
995 while (s6
!== peg
$FAILED
) {
997 if (input
.length
> peg
$currPos
) {
998 s6
= input
.charAt(peg
$currPos
);
1002 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1006 s0
= peg
$f10(s1
, s3
, s5
);
1015 if (s0
=== peg
$FAILED
) {
1018 if (input
.length
> peg
$currPos
) {
1019 s2
= input
.charAt(peg
$currPos
);
1023 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1025 while (s2
!== peg
$FAILED
) {
1027 if (input
.length
> peg
$currPos
) {
1028 s2
= input
.charAt(peg
$currPos
);
1032 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1040 if (s0
=== peg
$FAILED
) {
1042 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e22
); }
1049 // Per-parsing tracking of attributes which should be ignored after first occurrence.
1050 let seenAttributes
= [];
1052 peg
$result
= peg
$startRuleFunction();
1054 if (peg
$result
!== peg
$FAILED
&& peg
$currPos
=== input
.length
) {
1057 if (peg
$result
!== peg
$FAILED
&& peg
$currPos
< input
.length
) {
1058 peg
$fail(peg
$endExpectation());
1061 throw peg
$buildStructuredError(
1062 peg
$maxFailExpected
,
1063 peg
$maxFailPos
< input
.length
? input
.charAt(peg
$maxFailPos
) : null,
1064 peg
$maxFailPos
< input
.length
1065 ? peg
$computeLocation(peg
$maxFailPos
, peg
$maxFailPos
+ 1)
1066 : peg
$computeLocation(peg
$maxFailPos
, peg
$maxFailPos
)
1072 SyntaxError: peg
$SyntaxError
,