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 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 offset_s
= (this.location
.source
&& (typeof this.location
.source
.offset
=== "function"))
73 ? this.location
.source
.offset(s
)
75 var loc
= this.location
.source
+ ":" + offset_s
.line
+ ":" + offset_s
.column
;
77 var e
= this.location
.end
;
78 var filler
= peg
$padEnd("", offset_s
.line
.toString().length
, ' ');
79 var line
= src
[s
.line
- 1];
80 var last
= s
.line
=== e
.line
? e
.column : line
.length
+ 1;
81 var hatLen
= (last
- s
.column
) || 1;
82 str
+= "\n --> " + loc
+ "\n"
84 + offset_s
.line
+ " | " + line
+ "\n"
85 + filler
+ " | " + peg
$padEnd("", s
.column
- 1, ' ')
86 + peg
$padEnd("", hatLen
, "^");
88 str
+= "\n at " + loc
;
94 peg
$SyntaxError
.buildMessage = function(expected
, found
) {
95 var DESCRIBE_EXPECTATION_FNS
= {
96 literal: function(expectation
) {
97 return "\"" + literalEscape(expectation
.text
) + "\"";
100 class: function(expectation
) {
101 var escapedParts
= expectation
.parts
.map(function(part
) {
102 return Array
.isArray(part
)
103 ? classEscape(part
[0]) + "-" + classEscape(part
[1])
107 return "[" + (expectation
.inverted
? "^" : "") + escapedParts
.join("") + "]";
111 return "any character";
115 return "end of input";
118 other: function(expectation
) {
119 return expectation
.description
;
124 return ch
.charCodeAt(0).toString(16).toUpperCase();
127 function literalEscape(s
) {
129 .replace(/\\/g
, "\\\\")
130 .replace(/"/g, "\\\"")
131 .replace(/\0/g, "\\0")
132 .replace(/\t/g, "\\t
")
133 .replace(/\n/g, "\\n
")
134 .replace(/\r/g, "\\r
")
135 .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0
" + hex(ch); })
136 .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x
" + hex(ch); });
139 function classEscape(s) {
141 .replace(/\\/g, "\\\\")
142 .replace(/\]/g, "\\]")
143 .replace(/\^/g, "\\^")
144 .replace(/-/g, "\\-")
145 .replace(/\0/g, "\\0")
146 .replace(/\t/g, "\\t
")
147 .replace(/\n/g, "\\n
")
148 .replace(/\r/g, "\\r
")
149 .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0
" + hex(ch); })
150 .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x
" + hex(ch); });
153 function describeExpectation(expectation) {
154 return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
157 function describeExpected(expected) {
158 var descriptions = expected.map(describeExpectation);
163 if (descriptions.length > 0) {
164 for (i = 1, j = 1; i < descriptions.length; i++) {
165 if (descriptions[i - 1] !== descriptions[i]) {
166 descriptions[j] = descriptions[i];
170 descriptions.length = j;
173 switch (descriptions.length) {
175 return descriptions[0];
178 return descriptions[0] + " or
" + descriptions[1];
181 return descriptions.slice(0, -1).join(", ")
183 + descriptions[descriptions.length - 1];
187 function describeFound(found) {
188 return found ? "\"" + literalEscape(found) + "\"" : "end
of input
";
191 return "Expected
" + describeExpected(expected) + " but
" + describeFound(found) + " found
.";
194 function peg$parse(input, options) {
195 options = options !== undefined ? options : {};
198 var peg$source = options.grammarSource;
200 var peg$startRuleFunctions = { links: peg$parselinks, extendedValue: peg$parseextendedValue };
201 var peg$startRuleFunction = peg$parselinks;
210 var peg$r0 = /^[^>]/;
211 var peg$r1 = /^[a-zA-Z]/;
213 var peg
$r3
= /^[^"]/;
214 var peg
$r4
= /^[^";,]/;
216 var peg
$r6
= /^[^']/;
219 var peg
$e0
= peg
$otherExpectation("links");
220 var peg
$e1
= peg
$otherExpectation("link-value");
221 var peg
$e2
= peg
$literalExpectation(",", false);
222 var peg
$e3
= peg
$otherExpectation("uri-reference");
223 var peg
$e4
= peg
$literalExpectation("<", false);
224 var peg
$e5
= peg
$literalExpectation(">", false);
225 var peg
$e6
= peg
$literalExpectation(";", false);
226 var peg
$e7
= peg
$otherExpectation("uri");
227 var peg
$e8
= peg
$classExpectation([">"], true, false);
228 var peg
$e9
= peg
$otherExpectation("attributes");
229 var peg
$e10
= peg
$otherExpectation("link-param");
230 var peg
$e11
= peg
$literalExpectation("=", false);
231 var peg
$e12
= peg
$otherExpectation("name");
232 var peg
$e13
= peg
$classExpectation([["a", "z"], ["A", "Z"]], false, false);
233 var peg
$e14
= peg
$literalExpectation("*", false);
234 var peg
$e15
= peg
$otherExpectation("value");
235 var peg
$e16
= peg
$classExpectation(["\""], false, false);
236 var peg
$e17
= peg
$classExpectation(["\""], true, false);
237 var peg
$e18
= peg
$classExpectation(["\"", ";", ","], true, false);
238 var peg
$e19
= peg
$otherExpectation("whitespace");
239 var peg
$e20
= peg
$classExpectation([" "], false, false);
240 var peg
$e21
= peg
$otherExpectation("bad whitespace");
241 var peg
$e22
= peg
$otherExpectation("extended-value");
242 var peg
$e23
= peg
$classExpectation(["'"], true, false);
243 var peg
$e24
= peg
$classExpectation(["'"], false, false);
244 var peg
$e25
= peg
$anyExpectation();
246 var peg
$f0 = function(links
) {
249 var peg
$f1 = function(uriReference
, attributes
) {
252 target: uriReference
,
256 var peg
$f2 = function(uri
) {
259 var peg
$f3 = function(uri
) {
260 return makeString(uri
);
262 var peg
$f4 = function(attrs
) {
263 return attrs
.filter((a
) => a
);
265 var peg
$f5 = function(name
, value
) {
266 if (onlyFirstAttributes
.includes(name
.name
)) {
267 if (seenAttributes
.includes(name
.name
)) {
268 // Repeat of singleton attribute, ignore it.
271 seenAttributes
.push(name
.name
);
278 var peg
$f6 = function(name
) {
284 var peg
$f7 = function(name
, extended
) {
286 name: makeString(name
.concat(extended
)).toLowerCase(),
287 extended: !!extended
,
290 var peg
$f8 = function(value
) {
292 value: makeString(value
),
295 var peg
$f9 = function(value
) {
297 value: makeString(value
),
300 var peg
$f10 = function(encoding
, language
, value
) {
302 encoding: encoding
.length
? makeString(encoding
) : 'UTF-8',
303 language: language
.length
? makeString(language
) : null,
304 value: decodeURIComponent(makeString(value
)),
307 var peg
$f11 = function(value
) {
311 value: makeString(value
),
315 var peg
$savedPos
= 0;
316 var peg
$posDetailsCache
= [{ line: 1, column: 1 }];
317 var peg
$maxFailPos
= 0;
318 var peg
$maxFailExpected
= [];
319 var peg
$silentFails
= 0;
323 if ("startRule" in options
) {
324 if (!(options
.startRule
in peg
$startRuleFunctions
)) {
325 throw new Error("Can't start parsing from rule \"" + options
.startRule
+ "\".");
328 peg
$startRuleFunction
= peg
$startRuleFunctions
[options
.startRule
];
332 return input
.substring(peg
$savedPos
, peg
$currPos
);
347 function location() {
348 return peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
351 function expected(description
, location
) {
352 location
= location
!== undefined
354 : peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
356 throw peg
$buildStructuredError(
357 [peg
$otherExpectation(description
)],
358 input
.substring(peg
$savedPos
, peg
$currPos
),
363 function error(message
, location
) {
364 location
= location
!== undefined
366 : peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
368 throw peg
$buildSimpleError(message
, location
);
371 function peg
$literalExpectation(text
, ignoreCase
) {
372 return { type: "literal", text: text
, ignoreCase: ignoreCase
};
375 function peg
$classExpectation(parts
, inverted
, ignoreCase
) {
376 return { type: "class", parts: parts
, inverted: inverted
, ignoreCase: ignoreCase
};
379 function peg
$anyExpectation() {
380 return { type: "any" };
383 function peg
$endExpectation() {
384 return { type: "end" };
387 function peg
$otherExpectation(description
) {
388 return { type: "other", description: description
};
391 function peg
$computePosDetails(pos
) {
392 var details
= peg
$posDetailsCache
[pos
];
399 while (!peg
$posDetailsCache
[p
]) {
403 details
= peg
$posDetailsCache
[p
];
406 column: details
.column
410 if (input
.charCodeAt(p
) === 10) {
420 peg
$posDetailsCache
[pos
] = details
;
426 function peg
$computeLocation(startPos
, endPos
, offset
) {
427 var startPosDetails
= peg
$computePosDetails(startPos
);
428 var endPosDetails
= peg
$computePosDetails(endPos
);
434 line: startPosDetails
.line
,
435 column: startPosDetails
.column
439 line: endPosDetails
.line
,
440 column: endPosDetails
.column
443 if (offset
&& peg
$source
&& (typeof peg
$source
.offset
=== "function")) {
444 res
.start
= peg
$source
.offset(res
.start
);
445 res
.end
= peg
$source
.offset(res
.end
);
450 function peg
$fail(expected
) {
451 if (peg
$currPos
< peg
$maxFailPos
) { return; }
453 if (peg
$currPos
> peg
$maxFailPos
) {
454 peg
$maxFailPos
= peg
$currPos
;
455 peg
$maxFailExpected
= [];
458 peg
$maxFailExpected
.push(expected
);
461 function peg
$buildSimpleError(message
, location
) {
462 return new peg
$SyntaxError(message
, null, null, location
);
465 function peg
$buildStructuredError(expected
, found
, location
) {
466 return new peg
$SyntaxError(
467 peg
$SyntaxError
.buildMessage(expected
, found
),
474 function peg
$parselinks() {
480 s2
= peg
$parselinkValue();
481 if (s2
!== peg
$FAILED
) {
482 while (s2
!== peg
$FAILED
) {
484 s2
= peg
$parselinkValue();
489 if (s1
!== peg
$FAILED
) {
495 if (s0
=== peg
$FAILED
) {
497 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e0
); }
503 function peg
$parselinkValue() {
504 var s0
, s1
, s2
, s3
, s4
, s5
;
508 s1
= peg
$parseuriReference();
509 if (s1
!== peg
$FAILED
) {
511 s3
= peg
$parseattributes();
512 if (s3
!== peg
$FAILED
) {
513 if (input
.charCodeAt(peg
$currPos
) === 44) {
518 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e2
); }
520 if (s4
=== peg
$FAILED
) {
535 if (s0
=== peg
$FAILED
) {
537 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e1
); }
543 function peg
$parseuriReference() {
544 var s0
, s1
, s2
, s3
, s4
;
548 if (input
.charCodeAt(peg
$currPos
) === 60) {
553 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e4
); }
555 if (s1
!== peg
$FAILED
) {
557 if (s2
!== peg
$FAILED
) {
558 if (input
.charCodeAt(peg
$currPos
) === 62) {
563 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e5
); }
565 if (s3
!== peg
$FAILED
) {
566 if (input
.charCodeAt(peg
$currPos
) === 59) {
571 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
573 if (s4
!== peg
$FAILED
) {
593 if (s0
=== peg
$FAILED
) {
595 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e3
); }
601 function peg
$parseuri() {
607 if (peg
$r0
.test(input
.charAt(peg
$currPos
))) {
608 s2
= input
.charAt(peg
$currPos
);
612 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e8
); }
614 if (s2
!== peg
$FAILED
) {
615 while (s2
!== peg
$FAILED
) {
617 if (peg
$r0
.test(input
.charAt(peg
$currPos
))) {
618 s2
= input
.charAt(peg
$currPos
);
622 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e8
); }
628 if (s1
!== peg
$FAILED
) {
634 if (s0
=== peg
$FAILED
) {
636 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e7
); }
642 function peg
$parseattributes() {
648 s2
= peg
$parselinkParam();
649 if (s2
!== peg
$FAILED
) {
650 while (s2
!== peg
$FAILED
) {
652 s2
= peg
$parselinkParam();
657 if (s1
!== peg
$FAILED
) {
663 if (s0
=== peg
$FAILED
) {
665 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e9
); }
671 function peg
$parselinkParam() {
672 var s0
, s1
, s2
, s3
, s4
, s5
, s6
, s7
;
676 s1
= peg
$parsename();
677 if (s1
!== peg
$FAILED
) {
679 if (input
.charCodeAt(peg
$currPos
) === 61) {
684 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e11
); }
686 if (s3
!== peg
$FAILED
) {
688 s5
= peg
$parsevalue();
689 if (s5
!== peg
$FAILED
) {
690 if (input
.charCodeAt(peg
$currPos
) === 59) {
695 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
697 if (s6
=== peg
$FAILED
) {
715 if (s0
=== peg
$FAILED
) {
717 s1
= peg
$parsename();
718 if (s1
!== peg
$FAILED
) {
720 if (input
.charCodeAt(peg
$currPos
) === 59) {
725 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
727 if (s3
=== peg
$FAILED
) {
739 if (s0
=== peg
$FAILED
) {
741 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e10
); }
747 function peg
$parsename() {
753 if (peg
$r1
.test(input
.charAt(peg
$currPos
))) {
754 s2
= input
.charAt(peg
$currPos
);
758 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e13
); }
760 if (s2
!== peg
$FAILED
) {
761 while (s2
!== peg
$FAILED
) {
763 if (peg
$r1
.test(input
.charAt(peg
$currPos
))) {
764 s2
= input
.charAt(peg
$currPos
);
768 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e13
); }
774 if (s1
!== peg
$FAILED
) {
775 if (input
.charCodeAt(peg
$currPos
) === 42) {
780 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e14
); }
782 if (s2
=== peg
$FAILED
) {
792 if (s0
=== peg
$FAILED
) {
794 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e12
); }
800 function peg
$parsevalue() {
805 if (peg
$r2
.test(input
.charAt(peg
$currPos
))) {
806 s1
= input
.charAt(peg
$currPos
);
810 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e16
); }
812 if (s1
!== peg
$FAILED
) {
814 if (peg
$r3
.test(input
.charAt(peg
$currPos
))) {
815 s3
= input
.charAt(peg
$currPos
);
819 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e17
); }
821 if (s3
!== peg
$FAILED
) {
822 while (s3
!== peg
$FAILED
) {
824 if (peg
$r3
.test(input
.charAt(peg
$currPos
))) {
825 s3
= input
.charAt(peg
$currPos
);
829 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e17
); }
835 if (s2
!== peg
$FAILED
) {
836 if (peg
$r2
.test(input
.charAt(peg
$currPos
))) {
837 s3
= input
.charAt(peg
$currPos
);
841 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e16
); }
843 if (s3
!== peg
$FAILED
) {
858 if (s0
=== peg
$FAILED
) {
861 if (peg
$r4
.test(input
.charAt(peg
$currPos
))) {
862 s2
= input
.charAt(peg
$currPos
);
866 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e18
); }
868 if (s2
!== peg
$FAILED
) {
869 while (s2
!== peg
$FAILED
) {
871 if (peg
$r4
.test(input
.charAt(peg
$currPos
))) {
872 s2
= input
.charAt(peg
$currPos
);
876 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e18
); }
882 if (s1
!== peg
$FAILED
) {
889 if (s0
=== peg
$FAILED
) {
891 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e15
); }
897 function peg
$parseOWS() {
902 if (peg
$r5
.test(input
.charAt(peg
$currPos
))) {
903 s1
= input
.charAt(peg
$currPos
);
907 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e20
); }
909 while (s1
!== peg
$FAILED
) {
911 if (peg
$r5
.test(input
.charAt(peg
$currPos
))) {
912 s1
= input
.charAt(peg
$currPos
);
916 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e20
); }
921 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e19
); }
926 function peg
$parseBWS() {
933 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e21
); }
938 function peg
$parseextendedValue() {
939 var s0
, s1
, s2
, s3
, s4
, s5
, s6
;
944 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
945 s2
= input
.charAt(peg
$currPos
);
949 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
951 while (s2
!== peg
$FAILED
) {
953 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
954 s2
= input
.charAt(peg
$currPos
);
958 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
961 if (peg
$r7
.test(input
.charAt(peg
$currPos
))) {
962 s2
= input
.charAt(peg
$currPos
);
966 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e24
); }
968 if (s2
!== peg
$FAILED
) {
970 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
971 s4
= input
.charAt(peg
$currPos
);
975 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
977 while (s4
!== peg
$FAILED
) {
979 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
980 s4
= input
.charAt(peg
$currPos
);
984 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
987 if (peg
$r7
.test(input
.charAt(peg
$currPos
))) {
988 s4
= input
.charAt(peg
$currPos
);
992 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e24
); }
994 if (s4
!== peg
$FAILED
) {
996 if (input
.length
> peg
$currPos
) {
997 s6
= input
.charAt(peg
$currPos
);
1001 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1003 while (s6
!== peg
$FAILED
) {
1005 if (input
.length
> peg
$currPos
) {
1006 s6
= input
.charAt(peg
$currPos
);
1010 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1014 s0
= peg
$f10(s1
, s3
, s5
);
1023 if (s0
=== peg
$FAILED
) {
1026 if (input
.length
> peg
$currPos
) {
1027 s2
= input
.charAt(peg
$currPos
);
1031 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1033 while (s2
!== peg
$FAILED
) {
1035 if (input
.length
> peg
$currPos
) {
1036 s2
= input
.charAt(peg
$currPos
);
1040 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1048 if (s0
=== peg
$FAILED
) {
1050 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e22
); }
1057 // Per-parsing tracking of attributes which should be ignored after first occurrence.
1058 let seenAttributes
= [];
1060 peg
$result
= peg
$startRuleFunction();
1062 if (peg
$result
!== peg
$FAILED
&& peg
$currPos
=== input
.length
) {
1065 if (peg
$result
!== peg
$FAILED
&& peg
$currPos
< input
.length
) {
1066 peg
$fail(peg
$endExpectation());
1069 throw peg
$buildStructuredError(
1070 peg
$maxFailExpected
,
1071 peg
$maxFailPos
< input
.length
? input
.charAt(peg
$maxFailPos
) : null,
1072 peg
$maxFailPos
< input
.length
1073 ? peg
$computeLocation(peg
$maxFailPos
, peg
$maxFailPos
+ 1)
1074 : peg
$computeLocation(peg
$maxFailPos
, peg
$maxFailPos
)
1080 SyntaxError: peg
$SyntaxError
,