1 // Generated by Peggy 1.2.0.
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
= [
32 function peg
$subclass(child
, parent
) {
33 function C() { this.constructor = child
; }
34 C
.prototype = parent
.prototype;
35 child
.prototype = new C();
38 function peg
$SyntaxError(message
, expected
, found
, location
) {
39 var self
= Error
.call(this, message
);
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 str
+= "\n --> " + loc
+ "\n"
80 + s
.line
+ " | " + line
+ "\n"
81 + filler
+ " | " + peg
$padEnd("", s
.column
- 1)
82 + peg
$padEnd("", last
- s
.column
, "^");
84 str
+= "\n at " + loc
;
90 peg
$SyntaxError
.buildMessage = function(expected
, found
) {
91 var DESCRIBE_EXPECTATION_FNS
= {
92 literal: function(expectation
) {
93 return "\"" + literalEscape(expectation
.text
) + "\"";
96 class: function(expectation
) {
97 var escapedParts
= expectation
.parts
.map(function(part
) {
98 return Array
.isArray(part
)
99 ? classEscape(part
[0]) + "-" + classEscape(part
[1])
103 return "[" + (expectation
.inverted
? "^" : "") + escapedParts
+ "]";
107 return "any character";
111 return "end of input";
114 other: function(expectation
) {
115 return expectation
.description
;
120 return ch
.charCodeAt(0).toString(16).toUpperCase();
123 function literalEscape(s
) {
125 .replace(/\\/g
, "\\\\")
126 .replace(/"/g, "\\\"")
127 .replace(/\0/g, "\\0")
128 .replace(/\t/g, "\\t
")
129 .replace(/\n/g, "\\n
")
130 .replace(/\r/g, "\\r
")
131 .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0
" + hex(ch); })
132 .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x
" + hex(ch); });
135 function classEscape(s) {
137 .replace(/\\/g, "\\\\")
138 .replace(/\]/g, "\\]")
139 .replace(/\^/g, "\\^")
140 .replace(/-/g, "\\-")
141 .replace(/\0/g, "\\0")
142 .replace(/\t/g, "\\t
")
143 .replace(/\n/g, "\\n
")
144 .replace(/\r/g, "\\r
")
145 .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0
" + hex(ch); })
146 .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x
" + hex(ch); });
149 function describeExpectation(expectation) {
150 return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
153 function describeExpected(expected) {
154 var descriptions = expected.map(describeExpectation);
159 if (descriptions.length > 0) {
160 for (i = 1, j = 1; i < descriptions.length; i++) {
161 if (descriptions[i - 1] !== descriptions[i]) {
162 descriptions[j] = descriptions[i];
166 descriptions.length = j;
169 switch (descriptions.length) {
171 return descriptions[0];
174 return descriptions[0] + " or
" + descriptions[1];
177 return descriptions.slice(0, -1).join(", ")
179 + descriptions[descriptions.length - 1];
183 function describeFound(found) {
184 return found ? "\"" + literalEscape(found) + "\"" : "end
of input
";
187 return "Expected
" + describeExpected(expected) + " but
" + describeFound(found) + " found
.";
190 function peg$parse(input, options) {
191 options = options !== undefined ? options : {};
194 var peg$source = options.grammarSource;
196 var peg$startRuleFunctions = { links: peg$parselinks, extendedValue: peg$parseextendedValue };
197 var peg$startRuleFunction = peg$parselinks;
206 var peg$r0 = /^[^>]/;
207 var peg$r1 = /^[a-zA-Z]/;
209 var peg
$r3
= /^[^"]/;
210 var peg
$r4
= /^[^";,]/;
212 var peg
$r6
= /^[^']/;
215 var peg
$e0
= peg
$otherExpectation("links");
216 var peg
$e1
= peg
$otherExpectation("link-value");
217 var peg
$e2
= peg
$literalExpectation(",", false);
218 var peg
$e3
= peg
$otherExpectation("uri-reference");
219 var peg
$e4
= peg
$literalExpectation("<", false);
220 var peg
$e5
= peg
$literalExpectation(">", false);
221 var peg
$e6
= peg
$literalExpectation(";", false);
222 var peg
$e7
= peg
$otherExpectation("uri");
223 var peg
$e8
= peg
$classExpectation([">"], true, false);
224 var peg
$e9
= peg
$otherExpectation("attributes");
225 var peg
$e10
= peg
$otherExpectation("link-param");
226 var peg
$e11
= peg
$literalExpectation("=", false);
227 var peg
$e12
= peg
$otherExpectation("name");
228 var peg
$e13
= peg
$classExpectation([["a", "z"], ["A", "Z"]], false, false);
229 var peg
$e14
= peg
$literalExpectation("*", false);
230 var peg
$e15
= peg
$otherExpectation("value");
231 var peg
$e16
= peg
$classExpectation(["\""], false, false);
232 var peg
$e17
= peg
$classExpectation(["\""], true, false);
233 var peg
$e18
= peg
$classExpectation(["\"", ";", ","], true, false);
234 var peg
$e19
= peg
$otherExpectation("whitespace");
235 var peg
$e20
= peg
$classExpectation([" "], false, false);
236 var peg
$e21
= peg
$otherExpectation("bad whitespace");
237 var peg
$e22
= peg
$otherExpectation("extended-value");
238 var peg
$e23
= peg
$classExpectation(["'"], true, false);
239 var peg
$e24
= peg
$classExpectation(["'"], false, false);
240 var peg
$e25
= peg
$anyExpectation();
242 var peg
$f0 = function(links
) {
245 var peg
$f1 = function(uriReference
, attributes
) {
248 target: uriReference
,
252 var peg
$f2 = function(uri
) {
255 var peg
$f3 = function(uri
) {
256 return makeString(uri
);
258 var peg
$f4 = function(attrs
) {
259 return attrs
.filter((a
) => a
);
261 var peg
$f5 = function(name
, value
) {
262 if (onlyFirstAttributes
.includes(name
.name
)) {
263 if (seenAttributes
.includes(name
.name
)) {
264 // Repeat of singleton attribute, ignore it.
267 seenAttributes
.push(name
.name
);
274 var peg
$f6 = function(name
) {
280 var peg
$f7 = function(name
, extended
) {
282 name: makeString(name
.concat(extended
)).toLowerCase(),
283 extended: !!extended
,
286 var peg
$f8 = function(value
) {
288 value: makeString(value
),
291 var peg
$f9 = function(encoding
, language
, value
) {
293 encoding: encoding
.length
? makeString(encoding
) : 'UTF-8',
294 language: language
.length
? makeString(language
) : null,
295 value: decodeURIComponent(makeString(value
)),
298 var peg
$f10 = function(value
) {
302 value: makeString(value
),
307 var peg
$savedPos
= 0;
308 var peg
$posDetailsCache
= [{ line: 1, column: 1 }];
309 var peg
$maxFailPos
= 0;
310 var peg
$maxFailExpected
= [];
311 var peg
$silentFails
= 0;
315 if ("startRule" in options
) {
316 if (!(options
.startRule
in peg
$startRuleFunctions
)) {
317 throw new Error("Can't start parsing from rule \"" + options
.startRule
+ "\".");
320 peg
$startRuleFunction
= peg
$startRuleFunctions
[options
.startRule
];
324 return input
.substring(peg
$savedPos
, peg
$currPos
);
339 function location() {
340 return peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
343 function expected(description
, location
) {
344 location
= location
!== undefined
346 : peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
348 throw peg
$buildStructuredError(
349 [peg
$otherExpectation(description
)],
350 input
.substring(peg
$savedPos
, peg
$currPos
),
355 function error(message
, location
) {
356 location
= location
!== undefined
358 : peg
$computeLocation(peg
$savedPos
, peg
$currPos
);
360 throw peg
$buildSimpleError(message
, location
);
363 function peg
$literalExpectation(text
, ignoreCase
) {
364 return { type: "literal", text: text
, ignoreCase: ignoreCase
};
367 function peg
$classExpectation(parts
, inverted
, ignoreCase
) {
368 return { type: "class", parts: parts
, inverted: inverted
, ignoreCase: ignoreCase
};
371 function peg
$anyExpectation() {
372 return { type: "any" };
375 function peg
$endExpectation() {
376 return { type: "end" };
379 function peg
$otherExpectation(description
) {
380 return { type: "other", description: description
};
383 function peg
$computePosDetails(pos
) {
384 var details
= peg
$posDetailsCache
[pos
];
391 while (!peg
$posDetailsCache
[p
]) {
395 details
= peg
$posDetailsCache
[p
];
398 column: details
.column
402 if (input
.charCodeAt(p
) === 10) {
412 peg
$posDetailsCache
[pos
] = details
;
418 function peg
$computeLocation(startPos
, endPos
) {
419 var startPosDetails
= peg
$computePosDetails(startPos
);
420 var endPosDetails
= peg
$computePosDetails(endPos
);
426 line: startPosDetails
.line
,
427 column: startPosDetails
.column
431 line: endPosDetails
.line
,
432 column: endPosDetails
.column
437 function peg
$fail(expected
) {
438 if (peg
$currPos
< peg
$maxFailPos
) { return; }
440 if (peg
$currPos
> peg
$maxFailPos
) {
441 peg
$maxFailPos
= peg
$currPos
;
442 peg
$maxFailExpected
= [];
445 peg
$maxFailExpected
.push(expected
);
448 function peg
$buildSimpleError(message
, location
) {
449 return new peg
$SyntaxError(message
, null, null, location
);
452 function peg
$buildStructuredError(expected
, found
, location
) {
453 return new peg
$SyntaxError(
454 peg
$SyntaxError
.buildMessage(expected
, found
),
461 function peg
$parselinks() {
467 s2
= peg
$parselinkValue();
468 if (s2
!== peg
$FAILED
) {
469 while (s2
!== peg
$FAILED
) {
471 s2
= peg
$parselinkValue();
476 if (s1
!== peg
$FAILED
) {
482 if (s0
=== peg
$FAILED
) {
484 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e0
); }
490 function peg
$parselinkValue() {
491 var s0
, s1
, s2
, s3
, s4
, s5
;
495 s1
= peg
$parseuriReference();
496 if (s1
!== peg
$FAILED
) {
498 s3
= peg
$parseattributes();
499 if (s3
!== peg
$FAILED
) {
500 if (input
.charCodeAt(peg
$currPos
) === 44) {
505 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e2
); }
507 if (s4
=== peg
$FAILED
) {
522 if (s0
=== peg
$FAILED
) {
524 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e1
); }
530 function peg
$parseuriReference() {
531 var s0
, s1
, s2
, s3
, s4
;
535 if (input
.charCodeAt(peg
$currPos
) === 60) {
540 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e4
); }
542 if (s1
!== peg
$FAILED
) {
544 if (s2
!== peg
$FAILED
) {
545 if (input
.charCodeAt(peg
$currPos
) === 62) {
550 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e5
); }
552 if (s3
!== peg
$FAILED
) {
553 if (input
.charCodeAt(peg
$currPos
) === 59) {
558 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
560 if (s4
!== peg
$FAILED
) {
580 if (s0
=== peg
$FAILED
) {
582 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e3
); }
588 function peg
$parseuri() {
594 if (peg
$r0
.test(input
.charAt(peg
$currPos
))) {
595 s2
= input
.charAt(peg
$currPos
);
599 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e8
); }
601 if (s2
!== peg
$FAILED
) {
602 while (s2
!== peg
$FAILED
) {
604 if (peg
$r0
.test(input
.charAt(peg
$currPos
))) {
605 s2
= input
.charAt(peg
$currPos
);
609 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e8
); }
615 if (s1
!== peg
$FAILED
) {
621 if (s0
=== peg
$FAILED
) {
623 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e7
); }
629 function peg
$parseattributes() {
635 s2
= peg
$parselinkParam();
636 if (s2
!== peg
$FAILED
) {
637 while (s2
!== peg
$FAILED
) {
639 s2
= peg
$parselinkParam();
644 if (s1
!== peg
$FAILED
) {
650 if (s0
=== peg
$FAILED
) {
652 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e9
); }
658 function peg
$parselinkParam() {
659 var s0
, s1
, s2
, s3
, s4
, s5
, s6
, s7
;
663 s1
= peg
$parsename();
664 if (s1
!== peg
$FAILED
) {
666 if (input
.charCodeAt(peg
$currPos
) === 61) {
671 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e11
); }
673 if (s3
!== peg
$FAILED
) {
675 s5
= peg
$parsevalue();
676 if (s5
!== peg
$FAILED
) {
677 if (input
.charCodeAt(peg
$currPos
) === 59) {
682 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
684 if (s6
=== peg
$FAILED
) {
702 if (s0
=== peg
$FAILED
) {
704 s1
= peg
$parsename();
705 if (s1
!== peg
$FAILED
) {
707 if (input
.charCodeAt(peg
$currPos
) === 59) {
712 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e6
); }
714 if (s3
=== peg
$FAILED
) {
726 if (s0
=== peg
$FAILED
) {
728 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e10
); }
734 function peg
$parsename() {
740 if (peg
$r1
.test(input
.charAt(peg
$currPos
))) {
741 s2
= input
.charAt(peg
$currPos
);
745 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e13
); }
747 if (s2
!== peg
$FAILED
) {
748 while (s2
!== peg
$FAILED
) {
750 if (peg
$r1
.test(input
.charAt(peg
$currPos
))) {
751 s2
= input
.charAt(peg
$currPos
);
755 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e13
); }
761 if (s1
!== peg
$FAILED
) {
762 if (input
.charCodeAt(peg
$currPos
) === 42) {
767 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e14
); }
769 if (s2
=== peg
$FAILED
) {
779 if (s0
=== peg
$FAILED
) {
781 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e12
); }
787 function peg
$parsevalue() {
792 if (peg
$r2
.test(input
.charAt(peg
$currPos
))) {
793 s1
= input
.charAt(peg
$currPos
);
797 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e16
); }
799 if (s1
!== peg
$FAILED
) {
801 if (peg
$r3
.test(input
.charAt(peg
$currPos
))) {
802 s3
= input
.charAt(peg
$currPos
);
806 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e17
); }
808 if (s3
!== peg
$FAILED
) {
809 while (s3
!== peg
$FAILED
) {
811 if (peg
$r3
.test(input
.charAt(peg
$currPos
))) {
812 s3
= input
.charAt(peg
$currPos
);
816 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e17
); }
822 if (s2
!== peg
$FAILED
) {
823 if (peg
$r2
.test(input
.charAt(peg
$currPos
))) {
824 s3
= input
.charAt(peg
$currPos
);
828 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e16
); }
830 if (s3
!== peg
$FAILED
) {
845 if (s0
=== peg
$FAILED
) {
848 if (peg
$r4
.test(input
.charAt(peg
$currPos
))) {
849 s2
= input
.charAt(peg
$currPos
);
853 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e18
); }
855 if (s2
!== peg
$FAILED
) {
856 while (s2
!== peg
$FAILED
) {
858 if (peg
$r4
.test(input
.charAt(peg
$currPos
))) {
859 s2
= input
.charAt(peg
$currPos
);
863 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e18
); }
869 if (s1
!== peg
$FAILED
) {
876 if (s0
=== peg
$FAILED
) {
878 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e15
); }
884 function peg
$parseOWS() {
889 if (peg
$r5
.test(input
.charAt(peg
$currPos
))) {
890 s1
= input
.charAt(peg
$currPos
);
894 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e20
); }
896 while (s1
!== peg
$FAILED
) {
898 if (peg
$r5
.test(input
.charAt(peg
$currPos
))) {
899 s1
= input
.charAt(peg
$currPos
);
903 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e20
); }
908 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e19
); }
913 function peg
$parseBWS() {
920 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e21
); }
925 function peg
$parseextendedValue() {
926 var s0
, s1
, s2
, s3
, s4
, s5
, s6
;
931 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
932 s2
= input
.charAt(peg
$currPos
);
936 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
938 while (s2
!== peg
$FAILED
) {
940 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
941 s2
= input
.charAt(peg
$currPos
);
945 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
948 if (peg
$r7
.test(input
.charAt(peg
$currPos
))) {
949 s2
= input
.charAt(peg
$currPos
);
953 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e24
); }
955 if (s2
!== peg
$FAILED
) {
957 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
958 s4
= input
.charAt(peg
$currPos
);
962 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
964 while (s4
!== peg
$FAILED
) {
966 if (peg
$r6
.test(input
.charAt(peg
$currPos
))) {
967 s4
= input
.charAt(peg
$currPos
);
971 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e23
); }
974 if (peg
$r7
.test(input
.charAt(peg
$currPos
))) {
975 s4
= input
.charAt(peg
$currPos
);
979 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e24
); }
981 if (s4
!== peg
$FAILED
) {
983 if (input
.length
> peg
$currPos
) {
984 s6
= input
.charAt(peg
$currPos
);
988 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
990 while (s6
!== peg
$FAILED
) {
992 if (input
.length
> peg
$currPos
) {
993 s6
= input
.charAt(peg
$currPos
);
997 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1001 s0
= peg
$f9(s1
, s3
, s5
);
1010 if (s0
=== peg
$FAILED
) {
1013 if (input
.length
> peg
$currPos
) {
1014 s2
= input
.charAt(peg
$currPos
);
1018 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1020 while (s2
!== peg
$FAILED
) {
1022 if (input
.length
> peg
$currPos
) {
1023 s2
= input
.charAt(peg
$currPos
);
1027 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e25
); }
1035 if (s0
=== peg
$FAILED
) {
1037 if (peg
$silentFails
=== 0) { peg
$fail(peg
$e22
); }
1044 // Per-parsing tracking of attributes which should be ignored after first occurrence.
1045 let seenAttributes
= [];
1048 peg
$result
= peg
$startRuleFunction();
1050 if (peg
$result
!== peg
$FAILED
&& peg
$currPos
=== input
.length
) {
1053 if (peg
$result
!== peg
$FAILED
&& peg
$currPos
< input
.length
) {
1054 peg
$fail(peg
$endExpectation());
1057 throw peg
$buildStructuredError(
1058 peg
$maxFailExpected
,
1059 peg
$maxFailPos
< input
.length
? input
.charAt(peg
$maxFailPos
) : null,
1060 peg
$maxFailPos
< input
.length
1061 ? peg
$computeLocation(peg
$maxFailPos
, peg
$maxFailPos
+ 1)
1062 : peg
$computeLocation(peg
$maxFailPos
, peg
$maxFailPos
)
1068 SyntaxError: peg
$SyntaxError
,