UNPKG

40.6 kBJavaScriptView Raw
1(function (define) {
2 define(function (require, exports, module) {
3
4module.exports = (function(){
5 /*
6 * Generated by PEG.js 0.7.0.
7 *
8 * http://pegjs.majda.cz/
9 */
10
11 function quote(s) {
12 /*
13 * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a
14 * string literal except for the closing quote character, backslash,
15 * carriage return, line separator, paragraph separator, and line feed.
16 * Any character may appear in the form of an escape sequence.
17 *
18 * For portability, we also escape escape all control and non-ASCII
19 * characters. Note that "\0" and "\v" escape sequences are not used
20 * because JSHint does not like the first and IE the second.
21 */
22 return '"' + s
23 .replace(/\\/g, '\\\\') // backslash
24 .replace(/"/g, '\\"') // closing quote character
25 .replace(/\x08/g, '\\b') // backspace
26 .replace(/\t/g, '\\t') // horizontal tab
27 .replace(/\n/g, '\\n') // line feed
28 .replace(/\f/g, '\\f') // form feed
29 .replace(/\r/g, '\\r') // carriage return
30 .replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, escape)
31 + '"';
32 }
33
34 var result = {
35 /*
36 * Parses the input with a generated parser. If the parsing is successfull,
37 * returns a value explicitly or implicitly specified by the grammar from
38 * which the parser was generated (see |PEG.buildParser|). If the parsing is
39 * unsuccessful, throws |PEG.parser.SyntaxError| describing the error.
40 */
41 parse: function(input, startRule) {
42 var parseFunctions = {
43 "start": parse_start,
44 "LinkValue": parse_LinkValue,
45 "LinkParams": parse_LinkParams,
46 "URIReference": parse_URIReference,
47 "LinkParam": parse_LinkParam,
48 "LinkParamName": parse_LinkParamName,
49 "LinkParamValue": parse_LinkParamValue,
50 "PToken": parse_PToken,
51 "PTokenChar": parse_PTokenChar,
52 "OptionalSP": parse_OptionalSP,
53 "QuotedString": parse_QuotedString,
54 "QuotedStringInternal": parse_QuotedStringInternal,
55 "Char": parse_Char,
56 "UpAlpha": parse_UpAlpha,
57 "LoAlpha": parse_LoAlpha,
58 "Alpha": parse_Alpha,
59 "Digit": parse_Digit,
60 "SP": parse_SP,
61 "DQ": parse_DQ,
62 "QDText": parse_QDText,
63 "QuotedPair": parse_QuotedPair
64 };
65
66 if (startRule !== undefined) {
67 if (parseFunctions[startRule] === undefined) {
68 throw new Error("Invalid rule name: " + quote(startRule) + ".");
69 }
70 } else {
71 startRule = "start";
72 }
73
74 var pos = 0;
75 var reportFailures = 0;
76 var rightmostFailuresPos = 0;
77 var rightmostFailuresExpected = [];
78
79 function padLeft(input, padding, length) {
80 var result = input;
81
82 var padLength = length - input.length;
83 for (var i = 0; i < padLength; i++) {
84 result = padding + result;
85 }
86
87 return result;
88 }
89
90 function escape(ch) {
91 var charCode = ch.charCodeAt(0);
92 var escapeChar;
93 var length;
94
95 if (charCode <= 0xFF) {
96 escapeChar = 'x';
97 length = 2;
98 } else {
99 escapeChar = 'u';
100 length = 4;
101 }
102
103 return '\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), '0', length);
104 }
105
106 function matchFailed(failure) {
107 if (pos < rightmostFailuresPos) {
108 return;
109 }
110
111 if (pos > rightmostFailuresPos) {
112 rightmostFailuresPos = pos;
113 rightmostFailuresExpected = [];
114 }
115
116 rightmostFailuresExpected.push(failure);
117 }
118
119 function parse_start() {
120 var result0, result1, result2, result3, result4;
121 var pos0, pos1, pos2, pos3;
122
123 pos0 = pos;
124 pos1 = pos;
125 result0 = [];
126 pos2 = pos;
127 pos3 = pos;
128 result1 = parse_LinkValue();
129 if (result1 !== null) {
130 result2 = parse_OptionalSP();
131 if (result2 !== null) {
132 if (input.charCodeAt(pos) === 44) {
133 result3 = ",";
134 pos++;
135 } else {
136 result3 = null;
137 if (reportFailures === 0) {
138 matchFailed("\",\"");
139 }
140 }
141 if (result3 !== null) {
142 result4 = parse_OptionalSP();
143 if (result4 !== null) {
144 result1 = [result1, result2, result3, result4];
145 } else {
146 result1 = null;
147 pos = pos3;
148 }
149 } else {
150 result1 = null;
151 pos = pos3;
152 }
153 } else {
154 result1 = null;
155 pos = pos3;
156 }
157 } else {
158 result1 = null;
159 pos = pos3;
160 }
161 if (result1 !== null) {
162 result1 = (function(offset, i) {return i;})(pos2, result1[0]);
163 }
164 if (result1 === null) {
165 pos = pos2;
166 }
167 while (result1 !== null) {
168 result0.push(result1);
169 pos2 = pos;
170 pos3 = pos;
171 result1 = parse_LinkValue();
172 if (result1 !== null) {
173 result2 = parse_OptionalSP();
174 if (result2 !== null) {
175 if (input.charCodeAt(pos) === 44) {
176 result3 = ",";
177 pos++;
178 } else {
179 result3 = null;
180 if (reportFailures === 0) {
181 matchFailed("\",\"");
182 }
183 }
184 if (result3 !== null) {
185 result4 = parse_OptionalSP();
186 if (result4 !== null) {
187 result1 = [result1, result2, result3, result4];
188 } else {
189 result1 = null;
190 pos = pos3;
191 }
192 } else {
193 result1 = null;
194 pos = pos3;
195 }
196 } else {
197 result1 = null;
198 pos = pos3;
199 }
200 } else {
201 result1 = null;
202 pos = pos3;
203 }
204 if (result1 !== null) {
205 result1 = (function(offset, i) {return i;})(pos2, result1[0]);
206 }
207 if (result1 === null) {
208 pos = pos2;
209 }
210 }
211 if (result0 !== null) {
212 result1 = parse_LinkValue();
213 if (result1 !== null) {
214 result0 = [result0, result1];
215 } else {
216 result0 = null;
217 pos = pos1;
218 }
219 } else {
220 result0 = null;
221 pos = pos1;
222 }
223 if (result0 !== null) {
224 result0 = (function(offset, start, last) { return start.concat([last]) })(pos0, result0[0], result0[1]);
225 }
226 if (result0 === null) {
227 pos = pos0;
228 }
229 return result0;
230 }
231
232 function parse_LinkValue() {
233 var result0, result1, result2, result3, result4, result5;
234 var pos0, pos1;
235
236 pos0 = pos;
237 pos1 = pos;
238 if (input.charCodeAt(pos) === 60) {
239 result0 = "<";
240 pos++;
241 } else {
242 result0 = null;
243 if (reportFailures === 0) {
244 matchFailed("\"<\"");
245 }
246 }
247 if (result0 !== null) {
248 result1 = parse_URIReference();
249 if (result1 !== null) {
250 if (input.charCodeAt(pos) === 62) {
251 result2 = ">";
252 pos++;
253 } else {
254 result2 = null;
255 if (reportFailures === 0) {
256 matchFailed("\">\"");
257 }
258 }
259 if (result2 !== null) {
260 result3 = parse_OptionalSP();
261 if (result3 !== null) {
262 result4 = [];
263 result5 = parse_LinkParams();
264 while (result5 !== null) {
265 result4.push(result5);
266 result5 = parse_LinkParams();
267 }
268 if (result4 !== null) {
269 result0 = [result0, result1, result2, result3, result4];
270 } else {
271 result0 = null;
272 pos = pos1;
273 }
274 } else {
275 result0 = null;
276 pos = pos1;
277 }
278 } else {
279 result0 = null;
280 pos = pos1;
281 }
282 } else {
283 result0 = null;
284 pos = pos1;
285 }
286 } else {
287 result0 = null;
288 pos = pos1;
289 }
290 if (result0 !== null) {
291 result0 = (function(offset, href, params) {
292 var link = {};
293 params.forEach(function (param) {
294 link[param[0]] = param[1];
295 });
296 link.href = href;
297 return link;
298 })(pos0, result0[1], result0[4]);
299 }
300 if (result0 === null) {
301 pos = pos0;
302 }
303 return result0;
304 }
305
306 function parse_LinkParams() {
307 var result0, result1, result2, result3;
308 var pos0, pos1;
309
310 pos0 = pos;
311 pos1 = pos;
312 if (input.charCodeAt(pos) === 59) {
313 result0 = ";";
314 pos++;
315 } else {
316 result0 = null;
317 if (reportFailures === 0) {
318 matchFailed("\";\"");
319 }
320 }
321 if (result0 !== null) {
322 result1 = parse_OptionalSP();
323 if (result1 !== null) {
324 result2 = parse_LinkParam();
325 if (result2 !== null) {
326 result3 = parse_OptionalSP();
327 if (result3 !== null) {
328 result0 = [result0, result1, result2, result3];
329 } else {
330 result0 = null;
331 pos = pos1;
332 }
333 } else {
334 result0 = null;
335 pos = pos1;
336 }
337 } else {
338 result0 = null;
339 pos = pos1;
340 }
341 } else {
342 result0 = null;
343 pos = pos1;
344 }
345 if (result0 !== null) {
346 result0 = (function(offset, param) { return param })(pos0, result0[2]);
347 }
348 if (result0 === null) {
349 pos = pos0;
350 }
351 return result0;
352 }
353
354 function parse_URIReference() {
355 var result0, result1;
356 var pos0;
357
358 pos0 = pos;
359 if (/^[^>]/.test(input.charAt(pos))) {
360 result1 = input.charAt(pos);
361 pos++;
362 } else {
363 result1 = null;
364 if (reportFailures === 0) {
365 matchFailed("[^>]");
366 }
367 }
368 if (result1 !== null) {
369 result0 = [];
370 while (result1 !== null) {
371 result0.push(result1);
372 if (/^[^>]/.test(input.charAt(pos))) {
373 result1 = input.charAt(pos);
374 pos++;
375 } else {
376 result1 = null;
377 if (reportFailures === 0) {
378 matchFailed("[^>]");
379 }
380 }
381 }
382 } else {
383 result0 = null;
384 }
385 if (result0 !== null) {
386 result0 = (function(offset, url) { return url.join('') })(pos0, result0);
387 }
388 if (result0 === null) {
389 pos = pos0;
390 }
391 return result0;
392 }
393
394 function parse_LinkParam() {
395 var result0, result1;
396 var pos0, pos1;
397
398 pos0 = pos;
399 pos1 = pos;
400 result0 = parse_LinkParamName();
401 if (result0 !== null) {
402 result1 = parse_LinkParamValue();
403 result1 = result1 !== null ? result1 : "";
404 if (result1 !== null) {
405 result0 = [result0, result1];
406 } else {
407 result0 = null;
408 pos = pos1;
409 }
410 } else {
411 result0 = null;
412 pos = pos1;
413 }
414 if (result0 !== null) {
415 result0 = (function(offset, name, value) { return [name, value] })(pos0, result0[0], result0[1]);
416 }
417 if (result0 === null) {
418 pos = pos0;
419 }
420 return result0;
421 }
422
423 function parse_LinkParamName() {
424 var result0, result1;
425 var pos0;
426
427 pos0 = pos;
428 if (/^[a-z]/.test(input.charAt(pos))) {
429 result1 = input.charAt(pos);
430 pos++;
431 } else {
432 result1 = null;
433 if (reportFailures === 0) {
434 matchFailed("[a-z]");
435 }
436 }
437 if (result1 !== null) {
438 result0 = [];
439 while (result1 !== null) {
440 result0.push(result1);
441 if (/^[a-z]/.test(input.charAt(pos))) {
442 result1 = input.charAt(pos);
443 pos++;
444 } else {
445 result1 = null;
446 if (reportFailures === 0) {
447 matchFailed("[a-z]");
448 }
449 }
450 }
451 } else {
452 result0 = null;
453 }
454 if (result0 !== null) {
455 result0 = (function(offset, name) { return name.join('') })(pos0, result0);
456 }
457 if (result0 === null) {
458 pos = pos0;
459 }
460 return result0;
461 }
462
463 function parse_LinkParamValue() {
464 var result0, result1;
465 var pos0, pos1;
466
467 pos0 = pos;
468 pos1 = pos;
469 if (input.charCodeAt(pos) === 61) {
470 result0 = "=";
471 pos++;
472 } else {
473 result0 = null;
474 if (reportFailures === 0) {
475 matchFailed("\"=\"");
476 }
477 }
478 if (result0 !== null) {
479 result1 = parse_PToken();
480 if (result1 === null) {
481 result1 = parse_QuotedString();
482 }
483 if (result1 !== null) {
484 result0 = [result0, result1];
485 } else {
486 result0 = null;
487 pos = pos1;
488 }
489 } else {
490 result0 = null;
491 pos = pos1;
492 }
493 if (result0 !== null) {
494 result0 = (function(offset, str) { return str })(pos0, result0[1]);
495 }
496 if (result0 === null) {
497 pos = pos0;
498 }
499 return result0;
500 }
501
502 function parse_PToken() {
503 var result0, result1;
504 var pos0;
505
506 pos0 = pos;
507 result1 = parse_PTokenChar();
508 if (result1 !== null) {
509 result0 = [];
510 while (result1 !== null) {
511 result0.push(result1);
512 result1 = parse_PTokenChar();
513 }
514 } else {
515 result0 = null;
516 }
517 if (result0 !== null) {
518 result0 = (function(offset, token) { return token.join('') })(pos0, result0);
519 }
520 if (result0 === null) {
521 pos = pos0;
522 }
523 return result0;
524 }
525
526 function parse_PTokenChar() {
527 var result0;
528
529 if (input.charCodeAt(pos) === 33) {
530 result0 = "!";
531 pos++;
532 } else {
533 result0 = null;
534 if (reportFailures === 0) {
535 matchFailed("\"!\"");
536 }
537 }
538 if (result0 === null) {
539 if (input.charCodeAt(pos) === 35) {
540 result0 = "#";
541 pos++;
542 } else {
543 result0 = null;
544 if (reportFailures === 0) {
545 matchFailed("\"#\"");
546 }
547 }
548 if (result0 === null) {
549 if (input.charCodeAt(pos) === 36) {
550 result0 = "$";
551 pos++;
552 } else {
553 result0 = null;
554 if (reportFailures === 0) {
555 matchFailed("\"$\"");
556 }
557 }
558 if (result0 === null) {
559 if (input.charCodeAt(pos) === 37) {
560 result0 = "%";
561 pos++;
562 } else {
563 result0 = null;
564 if (reportFailures === 0) {
565 matchFailed("\"%\"");
566 }
567 }
568 if (result0 === null) {
569 if (input.charCodeAt(pos) === 38) {
570 result0 = "&";
571 pos++;
572 } else {
573 result0 = null;
574 if (reportFailures === 0) {
575 matchFailed("\"&\"");
576 }
577 }
578 if (result0 === null) {
579 if (input.charCodeAt(pos) === 39) {
580 result0 = "'";
581 pos++;
582 } else {
583 result0 = null;
584 if (reportFailures === 0) {
585 matchFailed("\"'\"");
586 }
587 }
588 if (result0 === null) {
589 if (input.charCodeAt(pos) === 40) {
590 result0 = "(";
591 pos++;
592 } else {
593 result0 = null;
594 if (reportFailures === 0) {
595 matchFailed("\"(\"");
596 }
597 }
598 if (result0 === null) {
599 if (input.charCodeAt(pos) === 41) {
600 result0 = ")";
601 pos++;
602 } else {
603 result0 = null;
604 if (reportFailures === 0) {
605 matchFailed("\")\"");
606 }
607 }
608 if (result0 === null) {
609 if (input.charCodeAt(pos) === 42) {
610 result0 = "*";
611 pos++;
612 } else {
613 result0 = null;
614 if (reportFailures === 0) {
615 matchFailed("\"*\"");
616 }
617 }
618 if (result0 === null) {
619 if (input.charCodeAt(pos) === 43) {
620 result0 = "+";
621 pos++;
622 } else {
623 result0 = null;
624 if (reportFailures === 0) {
625 matchFailed("\"+\"");
626 }
627 }
628 if (result0 === null) {
629 if (input.charCodeAt(pos) === 45) {
630 result0 = "-";
631 pos++;
632 } else {
633 result0 = null;
634 if (reportFailures === 0) {
635 matchFailed("\"-\"");
636 }
637 }
638 if (result0 === null) {
639 if (input.charCodeAt(pos) === 46) {
640 result0 = ".";
641 pos++;
642 } else {
643 result0 = null;
644 if (reportFailures === 0) {
645 matchFailed("\".\"");
646 }
647 }
648 if (result0 === null) {
649 if (input.charCodeAt(pos) === 124) {
650 result0 = "|";
651 pos++;
652 } else {
653 result0 = null;
654 if (reportFailures === 0) {
655 matchFailed("\"|\"");
656 }
657 }
658 if (result0 === null) {
659 result0 = parse_Digit();
660 if (result0 === null) {
661 if (input.charCodeAt(pos) === 58) {
662 result0 = ":";
663 pos++;
664 } else {
665 result0 = null;
666 if (reportFailures === 0) {
667 matchFailed("\":\"");
668 }
669 }
670 if (result0 === null) {
671 if (input.charCodeAt(pos) === 60) {
672 result0 = "<";
673 pos++;
674 } else {
675 result0 = null;
676 if (reportFailures === 0) {
677 matchFailed("\"<\"");
678 }
679 }
680 if (result0 === null) {
681 if (input.charCodeAt(pos) === 61) {
682 result0 = "=";
683 pos++;
684 } else {
685 result0 = null;
686 if (reportFailures === 0) {
687 matchFailed("\"=\"");
688 }
689 }
690 if (result0 === null) {
691 if (input.charCodeAt(pos) === 62) {
692 result0 = ">";
693 pos++;
694 } else {
695 result0 = null;
696 if (reportFailures === 0) {
697 matchFailed("\">\"");
698 }
699 }
700 if (result0 === null) {
701 if (input.charCodeAt(pos) === 63) {
702 result0 = "?";
703 pos++;
704 } else {
705 result0 = null;
706 if (reportFailures === 0) {
707 matchFailed("\"?\"");
708 }
709 }
710 if (result0 === null) {
711 if (input.charCodeAt(pos) === 64) {
712 result0 = "@";
713 pos++;
714 } else {
715 result0 = null;
716 if (reportFailures === 0) {
717 matchFailed("\"@\"");
718 }
719 }
720 if (result0 === null) {
721 result0 = parse_Alpha();
722 if (result0 === null) {
723 if (input.charCodeAt(pos) === 91) {
724 result0 = "[";
725 pos++;
726 } else {
727 result0 = null;
728 if (reportFailures === 0) {
729 matchFailed("\"[\"");
730 }
731 }
732 if (result0 === null) {
733 if (input.charCodeAt(pos) === 93) {
734 result0 = "]";
735 pos++;
736 } else {
737 result0 = null;
738 if (reportFailures === 0) {
739 matchFailed("\"]\"");
740 }
741 }
742 if (result0 === null) {
743 if (input.charCodeAt(pos) === 94) {
744 result0 = "^";
745 pos++;
746 } else {
747 result0 = null;
748 if (reportFailures === 0) {
749 matchFailed("\"^\"");
750 }
751 }
752 if (result0 === null) {
753 if (input.charCodeAt(pos) === 95) {
754 result0 = "_";
755 pos++;
756 } else {
757 result0 = null;
758 if (reportFailures === 0) {
759 matchFailed("\"_\"");
760 }
761 }
762 if (result0 === null) {
763 if (input.charCodeAt(pos) === 96) {
764 result0 = "`";
765 pos++;
766 } else {
767 result0 = null;
768 if (reportFailures === 0) {
769 matchFailed("\"`\"");
770 }
771 }
772 if (result0 === null) {
773 if (input.charCodeAt(pos) === 123) {
774 result0 = "{";
775 pos++;
776 } else {
777 result0 = null;
778 if (reportFailures === 0) {
779 matchFailed("\"{\"");
780 }
781 }
782 if (result0 === null) {
783 if (/^[\/\/]/.test(input.charAt(pos))) {
784 result0 = input.charAt(pos);
785 pos++;
786 } else {
787 result0 = null;
788 if (reportFailures === 0) {
789 matchFailed("[\\/\\/]");
790 }
791 }
792 if (result0 === null) {
793 if (input.charCodeAt(pos) === 125) {
794 result0 = "}";
795 pos++;
796 } else {
797 result0 = null;
798 if (reportFailures === 0) {
799 matchFailed("\"}\"");
800 }
801 }
802 if (result0 === null) {
803 if (input.charCodeAt(pos) === 126) {
804 result0 = "~";
805 pos++;
806 } else {
807 result0 = null;
808 if (reportFailures === 0) {
809 matchFailed("\"~\"");
810 }
811 }
812 }
813 }
814 }
815 }
816 }
817 }
818 }
819 }
820 }
821 }
822 }
823 }
824 }
825 }
826 }
827 }
828 }
829 }
830 }
831 }
832 }
833 }
834 }
835 }
836 }
837 }
838 }
839 }
840 }
841 return result0;
842 }
843
844 function parse_OptionalSP() {
845 var result0, result1;
846
847 result0 = [];
848 result1 = parse_SP();
849 while (result1 !== null) {
850 result0.push(result1);
851 result1 = parse_SP();
852 }
853 return result0;
854 }
855
856 function parse_QuotedString() {
857 var result0, result1, result2;
858 var pos0, pos1;
859
860 pos0 = pos;
861 pos1 = pos;
862 result0 = parse_DQ();
863 if (result0 !== null) {
864 result1 = parse_QuotedStringInternal();
865 if (result1 !== null) {
866 result2 = parse_DQ();
867 if (result2 !== null) {
868 result0 = [result0, result1, result2];
869 } else {
870 result0 = null;
871 pos = pos1;
872 }
873 } else {
874 result0 = null;
875 pos = pos1;
876 }
877 } else {
878 result0 = null;
879 pos = pos1;
880 }
881 if (result0 !== null) {
882 result0 = (function(offset, str) { return str })(pos0, result0[1]);
883 }
884 if (result0 === null) {
885 pos = pos0;
886 }
887 return result0;
888 }
889
890 function parse_QuotedStringInternal() {
891 var result0, result1;
892 var pos0;
893
894 pos0 = pos;
895 result0 = [];
896 result1 = parse_QDText();
897 if (result1 === null) {
898 result1 = parse_QuotedPair();
899 }
900 while (result1 !== null) {
901 result0.push(result1);
902 result1 = parse_QDText();
903 if (result1 === null) {
904 result1 = parse_QuotedPair();
905 }
906 }
907 if (result0 !== null) {
908 result0 = (function(offset, str) { return str.join('') })(pos0, result0);
909 }
910 if (result0 === null) {
911 pos = pos0;
912 }
913 return result0;
914 }
915
916 function parse_Char() {
917 var result0;
918
919 if (/^[\0-]/.test(input.charAt(pos))) {
920 result0 = input.charAt(pos);
921 pos++;
922 } else {
923 result0 = null;
924 if (reportFailures === 0) {
925 matchFailed("[\\0-]");
926 }
927 }
928 return result0;
929 }
930
931 function parse_UpAlpha() {
932 var result0;
933
934 if (/^[A-Z]/.test(input.charAt(pos))) {
935 result0 = input.charAt(pos);
936 pos++;
937 } else {
938 result0 = null;
939 if (reportFailures === 0) {
940 matchFailed("[A-Z]");
941 }
942 }
943 return result0;
944 }
945
946 function parse_LoAlpha() {
947 var result0;
948
949 if (/^[a-z]/.test(input.charAt(pos))) {
950 result0 = input.charAt(pos);
951 pos++;
952 } else {
953 result0 = null;
954 if (reportFailures === 0) {
955 matchFailed("[a-z]");
956 }
957 }
958 return result0;
959 }
960
961 function parse_Alpha() {
962 var result0;
963
964 result0 = parse_UpAlpha();
965 if (result0 === null) {
966 result0 = parse_LoAlpha();
967 }
968 return result0;
969 }
970
971 function parse_Digit() {
972 var result0;
973
974 if (/^[0-9]/.test(input.charAt(pos))) {
975 result0 = input.charAt(pos);
976 pos++;
977 } else {
978 result0 = null;
979 if (reportFailures === 0) {
980 matchFailed("[0-9]");
981 }
982 }
983 return result0;
984 }
985
986 function parse_SP() {
987 var result0;
988
989 if (/^[ ]/.test(input.charAt(pos))) {
990 result0 = input.charAt(pos);
991 pos++;
992 } else {
993 result0 = null;
994 if (reportFailures === 0) {
995 matchFailed("[ ]");
996 }
997 }
998 return result0;
999 }
1000
1001 function parse_DQ() {
1002 var result0;
1003
1004 if (/^["]/.test(input.charAt(pos))) {
1005 result0 = input.charAt(pos);
1006 pos++;
1007 } else {
1008 result0 = null;
1009 if (reportFailures === 0) {
1010 matchFailed("[\"]");
1011 }
1012 }
1013 return result0;
1014 }
1015
1016 function parse_QDText() {
1017 var result0;
1018
1019 if (/^[^"]/.test(input.charAt(pos))) {
1020 result0 = input.charAt(pos);
1021 pos++;
1022 } else {
1023 result0 = null;
1024 if (reportFailures === 0) {
1025 matchFailed("[^\"]");
1026 }
1027 }
1028 return result0;
1029 }
1030
1031 function parse_QuotedPair() {
1032 var result0, result1;
1033 var pos0;
1034
1035 pos0 = pos;
1036 if (/^[\\]/.test(input.charAt(pos))) {
1037 result0 = input.charAt(pos);
1038 pos++;
1039 } else {
1040 result0 = null;
1041 if (reportFailures === 0) {
1042 matchFailed("[\\\\]");
1043 }
1044 }
1045 if (result0 !== null) {
1046 result1 = parse_Char();
1047 if (result1 !== null) {
1048 result0 = [result0, result1];
1049 } else {
1050 result0 = null;
1051 pos = pos0;
1052 }
1053 } else {
1054 result0 = null;
1055 pos = pos0;
1056 }
1057 return result0;
1058 }
1059
1060
1061 function cleanupExpected(expected) {
1062 expected.sort();
1063
1064 var lastExpected = null;
1065 var cleanExpected = [];
1066 for (var i = 0; i < expected.length; i++) {
1067 if (expected[i] !== lastExpected) {
1068 cleanExpected.push(expected[i]);
1069 lastExpected = expected[i];
1070 }
1071 }
1072 return cleanExpected;
1073 }
1074
1075 function computeErrorPosition() {
1076 /*
1077 * The first idea was to use |String.split| to break the input up to the
1078 * error position along newlines and derive the line and column from
1079 * there. However IE's |split| implementation is so broken that it was
1080 * enough to prevent it.
1081 */
1082
1083 var line = 1;
1084 var column = 1;
1085 var seenCR = false;
1086
1087 for (var i = 0; i < Math.max(pos, rightmostFailuresPos); i++) {
1088 var ch = input.charAt(i);
1089 if (ch === "\n") {
1090 if (!seenCR) { line++; }
1091 column = 1;
1092 seenCR = false;
1093 } else if (ch === "\r" || ch === "\u2028" || ch === "\u2029") {
1094 line++;
1095 column = 1;
1096 seenCR = true;
1097 } else {
1098 column++;
1099 seenCR = false;
1100 }
1101 }
1102
1103 return { line: line, column: column };
1104 }
1105
1106
1107 var result = parseFunctions[startRule]();
1108
1109 /*
1110 * The parser is now in one of the following three states:
1111 *
1112 * 1. The parser successfully parsed the whole input.
1113 *
1114 * - |result !== null|
1115 * - |pos === input.length|
1116 * - |rightmostFailuresExpected| may or may not contain something
1117 *
1118 * 2. The parser successfully parsed only a part of the input.
1119 *
1120 * - |result !== null|
1121 * - |pos < input.length|
1122 * - |rightmostFailuresExpected| may or may not contain something
1123 *
1124 * 3. The parser did not successfully parse any part of the input.
1125 *
1126 * - |result === null|
1127 * - |pos === 0|
1128 * - |rightmostFailuresExpected| contains at least one failure
1129 *
1130 * All code following this comment (including called functions) must
1131 * handle these states.
1132 */
1133 if (result === null || pos !== input.length) {
1134 var offset = Math.max(pos, rightmostFailuresPos);
1135 var found = offset < input.length ? input.charAt(offset) : null;
1136 var errorPosition = computeErrorPosition();
1137
1138 throw new this.SyntaxError(
1139 cleanupExpected(rightmostFailuresExpected),
1140 found,
1141 offset,
1142 errorPosition.line,
1143 errorPosition.column
1144 );
1145 }
1146
1147 return result;
1148 },
1149
1150 /* Returns the parser source code. */
1151 toSource: function() { return this._source; }
1152 };
1153
1154 /* Thrown when a parser encounters a syntax error. */
1155
1156 result.SyntaxError = function(expected, found, offset, line, column) {
1157 function buildMessage(expected, found) {
1158 var expectedHumanized, foundHumanized;
1159
1160 switch (expected.length) {
1161 case 0:
1162 expectedHumanized = "end of input";
1163 break;
1164 case 1:
1165 expectedHumanized = expected[0];
1166 break;
1167 default:
1168 expectedHumanized = expected.slice(0, expected.length - 1).join(", ")
1169 + " or "
1170 + expected[expected.length - 1];
1171 }
1172
1173 foundHumanized = found ? quote(found) : "end of input";
1174
1175 return "Expected " + expectedHumanized + " but " + foundHumanized + " found.";
1176 }
1177
1178 this.name = "SyntaxError";
1179 this.expected = expected;
1180 this.found = found;
1181 this.message = buildMessage(expected, found);
1182 this.offset = offset;
1183 this.line = line;
1184 this.column = column;
1185 };
1186
1187 result.SyntaxError.prototype = Error.prototype;
1188
1189 return result;
1190})();
1191
1192 });
1193}(
1194 typeof define === 'function' && define.amd ? define : function (factory) { factory(require, module.exports, module); }
1195));