Debian Codegraph @

spec/behavior/generated-parser-behavior.spec.js

raw symbols SCIP
1 /* eslint no-console: 0 */
2 /* global peg, console */
3
4 "use strict";
5
6 describe("generated parser behavior", function() {
7 function varyOptimizationOptions(block) {
8 function clone(object) {
9 var result = {}, key;
10
11 for (key in object) {
12 if (object.hasOwnProperty(key)) {
13 result[key] = object[key];
14 }
15 }
16
17 return result;
18 }
19
20 var optionsVariants = [
21 { cache: false, optimize: "speed", trace: false },
22 { cache: false, optimize: "speed", trace: true },
23 { cache: false, optimize: "size", trace: false },
24 { cache: false, optimize: "size", trace: true },
25 { cache: true, optimize: "speed", trace: false },
26 { cache: true, optimize: "speed", trace: true },
27 { cache: true, optimize: "size", trace: false },
28 { cache: true, optimize: "size", trace: true }
29 ],
30 i;
31
32 for (i = 0; i < optionsVariants.length; i++) {
33 describe(
34 "with options " + jasmine.pp(optionsVariants[i]),
35 function() { block(clone(optionsVariants[i])); }
36 );
37 }
38 }
39
40 beforeEach(function() {
41 this.addMatchers({
42 toParse: function(input, expected, options) {
43 options = options !== undefined ? options : {};
44
45 var result;
46
47 try {
48 result = this.actual.parse(input, options);
49 } catch (e) {
50 this.message = function() {
51 return "Expected " + jasmine.pp(input) + " "
52 + "with options " + jasmine.pp(options) + " "
53 + "to parse" + (expected !== undefined ? " as " + jasmine.pp(expected) : "") + ", "
54 + "but it failed to parse with message "
55 + jasmine.pp(e.message) + ".";
56 };
57
58 return false;
59 }
60
61 if (expected !== undefined) {
62 this.message = function() {
63 return "Expected " + jasmine.pp(input) + " "
64 + "with options " + jasmine.pp(options) + " "
65 + (this.isNot ? "not " : "")
66 + "to parse as " + jasmine.pp(expected) + ", "
67 + "but it parsed as " + jasmine.pp(result) + ".";
68 };
69
70 return this.env.equals_(result, expected);
71 } else {
72 return true;
73 }
74 },
75
76 toFailToParse: function(input, details, options) {
77 options = options !== undefined ? options : {};
78
79 var result, key;
80
81 try {
82 result = this.actual.parse(input, options);
83 } catch (e) {
84 if (this.isNot) {
85 this.message = function() {
86 return "Expected " + jasmine.pp(input)
87 + "with options " + jasmine.pp(options) + " "
88 + "to parse, "
89 + "but it failed with message "
90 + jasmine.pp(e.message) + ".";
91 };
92 } else {
93 if (details) {
94 for (key in details) {
95 if (details.hasOwnProperty(key)) {
96 if (!this.env.equals_(e[key], details[key])) {
97 this.message = function() {
98 return "Expected " + jasmine.pp(input) + " "
99 + "with options " + jasmine.pp(options) + " "
100 + "to fail to parse"
101 + (details ? " with details " + jasmine.pp(details) : "") + ", "
102 + "but " + jasmine.pp(key) + " "
103 + "is " + jasmine.pp(e[key]) + ".";
104 };
105
106 return false;
107 }
108 }
109 }
110 }
111 }
112
113 return true;
114 }
115
116 this.message = function() {
117 return "Expected " + jasmine.pp(input) + " "
118 + "with options " + jasmine.pp(options) + " "
119 + "to fail to parse"
120 + (details ? " with details " + jasmine.pp(details) : "") + ", "
121 + "but it parsed as " + jasmine.pp(result) + ".";
122 };
123
124 return false;
125 }
126 });
127
128 /*
129 * Stub out |console.log| so that the default tracer doesn't clutter
130 * test output.
131 */
132 if (typeof console === "object") {
133 spyOn(console, "log");
134 }
135 });
136
137 varyOptimizationOptions(function(options) {
138 describe("initializer", function() {
139 it("executes the code before parsing starts", function() {
140 var parser = peg.generate([
141 '{ var result = 42; }',
142 'start = "a" { return result; }'
143 ].join("\n"), options);
144
145 expect(parser).toParse("a", 42);
146 });
147
148 describe("available variables and functions", function() {
149 it("|options| contains options", function() {
150 var parser = peg.generate([
151 '{ var result = options; }',
152 'start = "a" { return result; }'
153 ].join("\n"), options);
154
155 expect(parser).toParse("a", { a: 42 }, { a: 42 });
156 });
157 });
158 });
159
160 describe("rule", function() {
161 if (options.cache) {
162 it("caches rule match results", function() {
163 var parser = peg.generate([
164 '{ var n = 0; }',
165 'start = (a "b") / (a "c") { return n; }',
166 'a = "a" { n++; }'
167 ].join("\n"), options);
168
169 expect(parser).toParse("ac", 1);
170 });
171 } else {
172 it("doesn't cache rule match results", function() {
173 var parser = peg.generate([
174 '{ var n = 0; }',
175 'start = (a "b") / (a "c") { return n; }',
176 'a = "a" { n++; }'
177 ].join("\n"), options);
178
179 expect(parser).toParse("ac", 2);
180 });
181 }
182
183 describe("when the expression matches", function() {
184 it("returns its match result", function() {
185 var parser = peg.generate('start = "a"');
186
187 expect(parser).toParse("a", "a");
188 });
189 });
190
191 describe("when the expression doesn't match", function() {
192 describe("without display name", function() {
193 it("reports match failure and doesn't record any expectation", function() {
194 var parser = peg.generate('start = "a"');
195
196 expect(parser).toFailToParse("b", {
197 expected: [{ type: "literal", text: "a", ignoreCase: false }]
198 });
199 });
200 });
201
202 describe("with display name", function() {
203 it("reports match failure and records an expectation of type \"other\"", function() {
204 var parser = peg.generate('start "start" = "a"');
205
206 expect(parser).toFailToParse("b", {
207 expected: [{ type: "other", description: "start" }]
208 });
209 });
210
211 it("discards any expectations recorded when matching the expression", function() {
212 var parser = peg.generate('start "start" = "a"');
213
214 expect(parser).toFailToParse("b", {
215 expected: [{ type: "other", description: "start" }]
216 });
217 });
218 });
219 });
220 });
221
222 describe("literal", function() {
223 describe("matching", function() {
224 it("matches empty literals", function() {
225 var parser = peg.generate('start = ""', options);
226
227 expect(parser).toParse("");
228 });
229
230 it("matches one-character literals", function() {
231 var parser = peg.generate('start = "a"', options);
232
233 expect(parser).toParse("a");
234 expect(parser).toFailToParse("b");
235 });
236
237 it("matches multi-character literals", function() {
238 var parser = peg.generate('start = "abcd"', options);
239
240 expect(parser).toParse("abcd");
241 expect(parser).toFailToParse("efgh");
242 });
243
244 it("is case sensitive without the \"i\" flag", function() {
245 var parser = peg.generate('start = "a"', options);
246
247 expect(parser).toParse("a");
248 expect(parser).toFailToParse("A");
249 });
250
251 it("is case insensitive with the \"i\" flag", function() {
252 var parser = peg.generate('start = "a"i', options);
253
254 expect(parser).toParse("a");
255 expect(parser).toParse("A");
256 });
257 });
258
259 describe("when it matches", function() {
260 it("returns the matched text", function() {
261 var parser = peg.generate('start = "a"', options);
262
263 expect(parser).toParse("a", "a");
264 });
265
266 it("consumes the matched text", function() {
267 var parser = peg.generate('start = "a" .', options);
268
269 expect(parser).toParse("ab");
270 });
271 });
272
273 describe("when it doesn't match", function() {
274 it("reports match failure and records an expectation of type \"literal\"", function() {
275 var parser = peg.generate('start = "a"', options);
276
277 expect(parser).toFailToParse("b", {
278 expected: [{ type: "literal", text: "a", ignoreCase: false }]
279 });
280 });
281 });
282 });
283
284 describe("character class", function() {
285 describe("matching", function() {
286 it("matches empty classes", function() {
287 var parser = peg.generate('start = []', options);
288
289 expect(parser).toFailToParse("a");
290 });
291
292 it("matches classes with a character list", function() {
293 var parser = peg.generate('start = [abc]', options);
294
295 expect(parser).toParse("a");
296 expect(parser).toParse("b");
297 expect(parser).toParse("c");
298 expect(parser).toFailToParse("d");
299 });
300
301 it("matches classes with a character range", function() {
302 var parser = peg.generate('start = [a-c]', options);
303
304 expect(parser).toParse("a");
305 expect(parser).toParse("b");
306 expect(parser).toParse("c");
307 expect(parser).toFailToParse("d");
308 });
309
310 it("matches inverted classes", function() {
311 var parser = peg.generate('start = [^a]', options);
312
313 expect(parser).toFailToParse("a");
314 expect(parser).toParse("b");
315 });
316
317 it("is case sensitive without the \"i\" flag", function() {
318 var parser = peg.generate('start = [a]', options);
319
320 expect(parser).toParse("a");
321 expect(parser).toFailToParse("A");
322 });
323
324 it("is case insensitive with the \"i\" flag", function() {
325 var parser = peg.generate('start = [a]i', options);
326
327 expect(parser).toParse("a");
328 expect(parser).toParse("A");
329 });
330 });
331
332 describe("when it matches", function() {
333 it("returns the matched character", function() {
334 var parser = peg.generate('start = [a]', options);
335
336 expect(parser).toParse("a", "a");
337 });
338
339 it("consumes the matched character", function() {
340 var parser = peg.generate('start = [a] .', options);
341
342 expect(parser).toParse("ab");
343 });
344 });
345
346 describe("when it doesn't match", function() {
347 it("reports match failure and records an expectation of type \"class\"", function() {
348 var parser = peg.generate('start = [a]', options);
349
350 expect(parser).toFailToParse("b", {
351 expected: [{ type: "class", parts: ["a"], inverted: false, ignoreCase: false }]
352 });
353 });
354 });
355 });
356
357 describe("dot", function() {
358 describe("matching", function() {
359 it("matches any character", function() {
360 var parser = peg.generate('start = .', options);
361
362 expect(parser).toParse("a");
363 expect(parser).toParse("b");
364 expect(parser).toParse("c");
365 });
366 });
367
368 describe("when it matches", function() {
369 it("returns the matched character", function() {
370 var parser = peg.generate('start = .', options);
371
372 expect(parser).toParse("a", "a");
373 });
374
375 it("consumes the matched character", function() {
376 var parser = peg.generate('start = . .', options);
377
378 expect(parser).toParse("ab");
379 });
380 });
381
382 describe("when it doesn't match", function() {
383 it("reports match failure and records an expectation of type \"any\"", function() {
384 var parser = peg.generate('start = .', options);
385
386 expect(parser).toFailToParse("", {
387 expected: [{ type: "any" }]
388 });
389 });
390 });
391 });
392
393 describe("rule reference", function() {
394 describe("when referenced rule's expression matches", function() {
395 it("returns its result", function() {
396 var parser = peg.generate([
397 'start = a',
398 'a = "a"'
399 ].join("\n"), options);
400
401 expect(parser).toParse("a", "a");
402 });
403 });
404
405 describe("when referenced rule's expression doesn't match", function() {
406 it("reports match failure", function() {
407 var parser = peg.generate([
408 'start = a',
409 'a = "a"'
410 ].join("\n"), options);
411
412 expect(parser).toFailToParse("b");
413 });
414 });
415 });
416
417 describe("positive semantic predicate", function() {
418 describe("when the code returns a truthy value", function() {
419 it("returns |undefined|", function() {
420 /*
421 * The |""| is needed so that the parser doesn't return just
422 * |undefined| which we can't compare against in |toParse| due to the
423 * way optional parameters work.
424 */
425 var parser = peg.generate('start = &{ return true; } ""', options);
426
427 expect(parser).toParse("", [undefined, ""]);
428 });
429 });
430
431 describe("when the code returns a falsey value", function() {
432 it("reports match failure", function() {
433 var parser = peg.generate('start = &{ return false; }', options);
434
435 expect(parser).toFailToParse("");
436 });
437 });
438
439 describe("label variables", function() {
440 describe("in containing sequence", function() {
441 it("can access variables defined by preceding labeled elements", function() {
442 var parser = peg.generate(
443 'start = a:"a" &{ return a === "a"; }',
444 options
445 );
446
447 expect(parser).toParse("a");
448 });
449
450 it("cannot access variable defined by labeled predicate element", function() {
451 var parser = peg.generate(
452 'start = "a" b:&{ return b === undefined; } "c"',
453 options
454 );
455
456 expect(parser).toFailToParse("ac");
457 });
458
459 it("cannot access variables defined by following labeled elements", function() {
460 var parser = peg.generate(
461 'start = &{ return a === "a"; } a:"a"',
462 options
463 );
464
465 expect(parser).toFailToParse("a");
466 });
467
468 it("cannot access variables defined by subexpressions", function() {
469 var testcases = [
470 {
471 grammar: 'start = (a:"a") &{ return a === "a"; }',
472 input: "a"
473 },
474 {
475 grammar: 'start = (a:"a")? &{ return a === "a"; }',
476 input: "a"
477 },
478 {
479 grammar: 'start = (a:"a")* &{ return a === "a"; }',
480 input: "a"
481 },
482 {
483 grammar: 'start = (a:"a")+ &{ return a === "a"; }',
484 input: "a"
485 },
486 {
487 grammar: 'start = $(a:"a") &{ return a === "a"; }',
488 input: "a"
489 },
490 {
491 grammar: 'start = &(a:"a") "a" &{ return a === "a"; }',
492 input: "a"
493 },
494 {
495 grammar: 'start = !(a:"a") "b" &{ return a === "a"; }',
496 input: "b"
497 },
498 {
499 grammar: 'start = b:(a:"a") &{ return a === "a"; }',
500 input: "a"
501 },
502 {
503 grammar: 'start = ("a" b:"b" "c") &{ return b === "b"; }',
504 input: "abc"
505 },
506 {
507 grammar: 'start = (a:"a" { return a; }) &{ return a === "a"; }',
508 input: "a"
509 },
510 {
511 grammar: 'start = ("a" / b:"b" / "c") &{ return b === "b"; }',
512 input: "b"
513 }
514 ],
515 parser, i;
516
517 for (i = 0; i < testcases.length; i++) {
518 parser = peg.generate(testcases[i].grammar, options);
519 expect(parser).toFailToParse(testcases[i].input);
520 }
521 });
522 });
523
524 describe("in outer sequence", function() {
525 it("can access variables defined by preceding labeled elements", function() {
526 var parser = peg.generate(
527 'start = a:"a" ("b" &{ return a === "a"; })',
528 options
529 );
530
531 expect(parser).toParse("ab");
532 });
533
534 it("cannot access variable defined by labeled predicate element", function() {
535 var parser = peg.generate(
536 'start = "a" b:("b" &{ return b === undefined; }) "c"',
537 options
538 );
539
540 expect(parser).toFailToParse("abc");
541 });
542
543 it("cannot access variables defined by following labeled elements", function() {
544 var parser = peg.generate(
545 'start = ("a" &{ return b === "b"; }) b:"b"',
546 options
547 );
548
549 expect(parser).toFailToParse("ab");
550 });
551 });
552 });
553
554 describe("initializer variables & functions", function() {
555 it("can access variables defined in the initializer", function() {
556 var parser = peg.generate([
557 '{ var v = 42 }',
558 'start = &{ return v === 42; }'
559 ].join("\n"), options);
560
561 expect(parser).toParse("");
562 });
563
564 it("can access functions defined in the initializer", function() {
565 var parser = peg.generate([
566 '{ function f() { return 42; } }',
567 'start = &{ return f() === 42; }'
568 ].join("\n"), options);
569
570 expect(parser).toParse("");
571 });
572 });
573
574 describe("available variables & functions", function() {
575 it("|options| contains options", function() {
576 var parser = peg.generate([
577 '{ var result; }',
578 'start = &{ result = options; return true; } { return result; }'
579 ].join("\n"), options);
580
581 expect(parser).toParse("", { a: 42 }, { a: 42 });
582 });
583
584 it("|location| returns current location info", function() {
585 var parser = peg.generate([
586 '{ var result; }',
587 'start = line (nl+ line)* { return result; }',
588 'line = thing (" "+ thing)*',
589 'thing = digit / mark',
590 'digit = [0-9]',
591 'mark = &{ result = location(); return true; } "x"',
592 'nl = "\\r"? "\\n"'
593 ].join("\n"), options);
594
595 expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", {
596 start: { offset: 13, line: 7, column: 5 },
597 end: { offset: 13, line: 7, column: 5 }
598 });
599
600 /* Newline representations */
601 expect(parser).toParse("1\nx", { // Unix
602 start: { offset: 2, line: 2, column: 1 },
603 end: { offset: 2, line: 2, column: 1 }
604 });
605 expect(parser).toParse("1\r\nx", { // Windows
606 start: { offset: 3, line: 2, column: 1 },
607 end: { offset: 3, line: 2, column: 1 }
608 });
609 });
610 });
611 });
612
613 describe("negative semantic predicate", function() {
614 describe("when the code returns a falsey value", function() {
615 it("returns |undefined|", function() {
616 /*
617 * The |""| is needed so that the parser doesn't return just
618 * |undefined| which we can't compare against in |toParse| due to the
619 * way optional parameters work.
620 */
621 var parser = peg.generate('start = !{ return false; } ""', options);
622
623 expect(parser).toParse("", [undefined, ""]);
624 });
625 });
626
627 describe("when the code returns a truthy value", function() {
628 it("reports match failure", function() {
629 var parser = peg.generate('start = !{ return true; }', options);
630
631 expect(parser).toFailToParse("");
632 });
633 });
634
635 describe("label variables", function() {
636 describe("in containing sequence", function() {
637 it("can access variables defined by preceding labeled elements", function() {
638 var parser = peg.generate(
639 'start = a:"a" !{ return a !== "a"; }',
640 options
641 );
642
643 expect(parser).toParse("a");
644 });
645
646 it("cannot access variable defined by labeled predicate element", function() {
647 var parser = peg.generate(
648 'start = "a" b:!{ return b !== undefined; } "c"',
649 options
650 );
651
652 expect(parser).toFailToParse("ac");
653 });
654
655 it("cannot access variables defined by following labeled elements", function() {
656 var parser = peg.generate(
657 'start = !{ return a !== "a"; } a:"a"',
658 options
659 );
660
661 expect(parser).toFailToParse("a");
662 });
663
664 it("cannot access variables defined by subexpressions", function() {
665 var testcases = [
666 {
667 grammar: 'start = (a:"a") !{ return a !== "a"; }',
668 input: "a"
669 },
670 {
671 grammar: 'start = (a:"a")? !{ return a !== "a"; }',
672 input: "a"
673 },
674 {
675 grammar: 'start = (a:"a")* !{ return a !== "a"; }',
676 input: "a"
677 },
678 {
679 grammar: 'start = (a:"a")+ !{ return a !== "a"; }',
680 input: "a"
681 },
682 {
683 grammar: 'start = $(a:"a") !{ return a !== "a"; }',
684 input: "a"
685 },
686 {
687 grammar: 'start = &(a:"a") "a" !{ return a !== "a"; }',
688 input: "a"
689 },
690 {
691 grammar: 'start = !(a:"a") "b" !{ return a !== "a"; }',
692 input: "b"
693 },
694 {
695 grammar: 'start = b:(a:"a") !{ return a !== "a"; }',
696 input: "a"
697 },
698 {
699 grammar: 'start = ("a" b:"b" "c") !{ return b !== "b"; }',
700 input: "abc"
701 },
702 {
703 grammar: 'start = (a:"a" { return a; }) !{ return a !== "a"; }',
704 input: "a"
705 },
706 {
707 grammar: 'start = ("a" / b:"b" / "c") !{ return b !== "b"; }',
708 input: "b"
709 }
710 ],
711 parser, i;
712
713 for (i = 0; i < testcases.length; i++) {
714 parser = peg.generate(testcases[i].grammar, options);
715 expect(parser).toFailToParse(testcases[i].input);
716 }
717 });
718 });
719
720 describe("in outer sequence", function() {
721 it("can access variables defined by preceding labeled elements", function() {
722 var parser = peg.generate(
723 'start = a:"a" ("b" !{ return a !== "a"; })',
724 options
725 );
726
727 expect(parser).toParse("ab");
728 });
729
730 it("cannot access variable defined by labeled predicate element", function() {
731 var parser = peg.generate(
732 'start = "a" b:("b" !{ return b !== undefined; }) "c"',
733 options
734 );
735
736 expect(parser).toFailToParse("abc");
737 });
738
739 it("cannot access variables defined by following labeled elements", function() {
740 var parser = peg.generate(
741 'start = ("a" !{ return b !== "b"; }) b:"b"',
742 options
743 );
744
745 expect(parser).toFailToParse("ab");
746 });
747 });
748 });
749
750 describe("initializer variables & functions", function() {
751 it("can access variables defined in the initializer", function() {
752 var parser = peg.generate([
753 '{ var v = 42 }',
754 'start = !{ return v !== 42; }'
755 ].join("\n"), options);
756
757 expect(parser).toParse("");
758 });
759
760 it("can access functions defined in the initializer", function() {
761 var parser = peg.generate([
762 '{ function f() { return 42; } }',
763 'start = !{ return f() !== 42; }'
764 ].join("\n"), options);
765
766 expect(parser).toParse("");
767 });
768 });
769
770 describe("available variables & functions", function() {
771 it("|options| contains options", function() {
772 var parser = peg.generate([
773 '{ var result; }',
774 'start = !{ result = options; return false; } { return result; }'
775 ].join("\n"), options);
776
777 expect(parser).toParse("", { a: 42 }, { a: 42 });
778 });
779
780 it("|location| returns current location info", function() {
781 var parser = peg.generate([
782 '{ var result; }',
783 'start = line (nl+ line)* { return result; }',
784 'line = thing (" "+ thing)*',
785 'thing = digit / mark',
786 'digit = [0-9]',
787 'mark = !{ result = location(); return false; } "x"',
788 'nl = "\\r"? "\\n"'
789 ].join("\n"), options);
790
791 expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", {
792 start: { offset: 13, line: 7, column: 5 },
793 end: { offset: 13, line: 7, column: 5 }
794 });
795
796 /* Newline representations */
797 expect(parser).toParse("1\nx", { // Unix
798 start: { offset: 2, line: 2, column: 1 },
799 end: { offset: 2, line: 2, column: 1 }
800 });
801 expect(parser).toParse("1\r\nx", { // Windows
802 start: { offset: 3, line: 2, column: 1 },
803 end: { offset: 3, line: 2, column: 1 }
804 });
805 });
806 });
807 });
808
809 describe("group", function() {
810 describe("when the expression matches", function() {
811 it("returns its match result", function() {
812 var parser = peg.generate('start = ("a")', options);
813
814 expect(parser).toParse("a", "a");
815 });
816 });
817
818 describe("when the expression doesn't match", function() {
819 it("reports match failure", function() {
820 var parser = peg.generate('start = ("a")', options);
821
822 expect(parser).toFailToParse("b");
823 });
824 });
825 });
826
827 describe("optional", function() {
828 describe("when the expression matches", function() {
829 it("returns its match result", function() {
830 var parser = peg.generate('start = "a"?', options);
831
832 expect(parser).toParse("a", "a");
833 });
834 });
835
836 describe("when the expression doesn't match", function() {
837 it("returns |null|", function() {
838 var parser = peg.generate('start = "a"?', options);
839
840 expect(parser).toParse("", null);
841 });
842 });
843 });
844
845 describe("zero or more", function() {
846 describe("when the expression matches zero or more times", function() {
847 it("returns an array of its match results", function() {
848 var parser = peg.generate('start = "a"*', options);
849
850 expect(parser).toParse("", []);
851 expect(parser).toParse("a", ["a"]);
852 expect(parser).toParse("aaa", ["a", "a", "a"]);
853 });
854 });
855 });
856
857 describe("one or more", function() {
858 describe("when the expression matches one or more times", function() {
859 it("returns an array of its match results", function() {
860 var parser = peg.generate('start = "a"+', options);
861
862 expect(parser).toParse("a", ["a"]);
863 expect(parser).toParse("aaa", ["a", "a", "a"]);
864 });
865 });
866
867 describe("when the expression doesn't match", function() {
868 it("reports match failure", function() {
869 var parser = peg.generate('start = "a"+', options);
870
871 expect(parser).toFailToParse("");
872 });
873 });
874 });
875
876 describe("text", function() {
877 describe("when the expression matches", function() {
878 it("returns the matched text", function() {
879 var parser = peg.generate('start = $("a" "b" "c")', options);
880
881 expect(parser).toParse("abc", "abc");
882 });
883 });
884
885 describe("when the expression doesn't match", function() {
886 it("reports match failure", function() {
887 var parser = peg.generate('start = $("a")', options);
888
889 expect(parser).toFailToParse("b");
890 });
891 });
892 });
893
894 describe("positive simple predicate", function() {
895 describe("when the expression matches", function() {
896 it("returns |undefined|", function() {
897 var parser = peg.generate('start = &"a" "a"', options);
898
899 expect(parser).toParse("a", [undefined, "a"]);
900 });
901
902 it("resets parse position", function() {
903 var parser = peg.generate('start = &"a" "a"', options);
904
905 expect(parser).toParse("a");
906 });
907 });
908
909 describe("when the expression doesn't match", function() {
910 it("reports match failure", function() {
911 var parser = peg.generate('start = &"a"', options);
912
913 expect(parser).toFailToParse("b");
914 });
915
916 it("discards any expectations recorded when matching the expression", function() {
917 var parser = peg.generate('start = "a" / &"b" / "c"', options);
918
919 expect(parser).toFailToParse("d", {
920 expected: [
921 { type: "literal", text: "a", ignoreCase: false },
922 { type: "literal", text: "c", ignoreCase: false }
923 ]
924 });
925 });
926 });
927 });
928
929 describe("negative simple predicate", function() {
930 describe("when the expression matches", function() {
931 it("reports match failure", function() {
932 var parser = peg.generate('start = !"a"', options);
933
934 expect(parser).toFailToParse("a");
935 });
936 });
937
938 describe("when the expression doesn't match", function() {
939 it("returns |undefined|", function() {
940 var parser = peg.generate('start = !"a" "b"', options);
941
942 expect(parser).toParse("b", [undefined, "b"]);
943 });
944
945 it("resets parse position", function() {
946 var parser = peg.generate('start = !"a" "b"', options);
947
948 expect(parser).toParse("b");
949 });
950
951 it("discards any expectations recorded when matching the expression", function() {
952 var parser = peg.generate('start = "a" / !"b" / "c"', options);
953
954 expect(parser).toFailToParse("b", {
955 expected: [
956 { type: "literal", text: "a", ignoreCase: false },
957 { type: "literal", text: "c", ignoreCase: false }
958 ]
959 });
960 });
961 });
962 });
963
964 describe("label", function() {
965 describe("when the expression matches", function() {
966 it("returns its match result", function() {
967 var parser = peg.generate('start = a:"a"', options);
968
969 expect(parser).toParse("a", "a");
970 });
971 });
972
973 describe("when the expression doesn't match", function() {
974 it("reports match failure", function() {
975 var parser = peg.generate('start = a:"a"', options);
976
977 expect(parser).toFailToParse("b");
978 });
979 });
980 });
981
982 describe("sequence", function() {
983 describe("when all expressions match", function() {
984 it("returns an array of their match results", function() {
985 var parser = peg.generate('start = "a" "b" "c"', options);
986
987 expect(parser).toParse("abc", ["a", "b", "c"]);
988 });
989 });
990
991 describe("when any expression doesn't match", function() {
992 it("reports match failure", function() {
993 var parser = peg.generate('start = "a" "b" "c"', options);
994
995 expect(parser).toFailToParse("dbc");
996 expect(parser).toFailToParse("adc");
997 expect(parser).toFailToParse("abd");
998 });
999
1000 it("resets parse position", function() {
1001 var parser = peg.generate('start = "a" "b" / "a"', options);
1002
1003 expect(parser).toParse("a", "a");
1004 });
1005 });
1006 });
1007
1008 describe("action", function() {
1009 describe("when the expression matches", function() {
1010 it("returns the value returned by the code", function() {
1011 var parser = peg.generate('start = "a" { return 42; }', options);
1012
1013 expect(parser).toParse("a", 42);
1014 });
1015
1016 describe("label variables", function() {
1017 describe("in the expression", function() {
1018 it("can access variable defined by labeled expression", function() {
1019 var parser = peg.generate('start = a:"a" { return a; }', options);
1020
1021 expect(parser).toParse("a", "a");
1022 });
1023
1024 it("can access variables defined by labeled sequence elements", function() {
1025 var parser = peg.generate(
1026 'start = a:"a" b:"b" c:"c" { return [a, b, c]; }',
1027 options
1028 );
1029
1030 expect(parser).toParse("abc", ["a", "b", "c"]);
1031 });
1032
1033 it("cannot access variables defined by subexpressions", function() {
1034 var testcases = [
1035 {
1036 grammar: 'start = (a:"a") { return a; }',
1037 input: "a"
1038 },
1039 {
1040 grammar: 'start = (a:"a")? { return a; }',
1041 input: "a"
1042 },
1043 {
1044 grammar: 'start = (a:"a")* { return a; }',
1045 input: "a"
1046 },
1047 {
1048 grammar: 'start = (a:"a")+ { return a; }',
1049 input: "a"
1050 },
1051 {
1052 grammar: 'start = $(a:"a") { return a; }',
1053 input: "a"
1054 },
1055 {
1056 grammar: 'start = &(a:"a") "a" { return a; }',
1057 input: "a"
1058 },
1059 {
1060 grammar: 'start = !(a:"a") "b" { return a; }',
1061 input: "b"
1062 },
1063 {
1064 grammar: 'start = b:(a:"a") { return a; }',
1065 input: "a"
1066 },
1067 {
1068 grammar: 'start = ("a" b:"b" "c") { return b; }',
1069 input: "abc"
1070 },
1071 {
1072 grammar: 'start = (a:"a" { return a; }) { return a; }',
1073 input: "a"
1074 },
1075 {
1076 grammar: 'start = ("a" / b:"b" / "c") { return b; }',
1077 input: "b"
1078 }
1079 ],
1080 parser, i;
1081
1082 for (i = 0; i < testcases.length; i++) {
1083 parser = peg.generate(testcases[i].grammar, options);
1084 expect(parser).toFailToParse(testcases[i].input);
1085 }
1086 });
1087 });
1088
1089 describe("in outer sequence", function() {
1090 it("can access variables defined by preceding labeled elements", function() {
1091 var parser = peg.generate(
1092 'start = a:"a" ("b" { return a; })',
1093 options
1094 );
1095
1096 expect(parser).toParse("ab", ["a", "a"]);
1097 });
1098
1099 it("cannot access variable defined by labeled action element", function() {
1100 var parser = peg.generate(
1101 'start = "a" b:("b" { return b; }) c:"c"',
1102 options
1103 );
1104
1105 expect(parser).toFailToParse("abc");
1106 });
1107
1108 it("cannot access variables defined by following labeled elements", function() {
1109 var parser = peg.generate(
1110 'start = ("a" { return b; }) b:"b"',
1111 options
1112 );
1113
1114 expect(parser).toFailToParse("ab");
1115 });
1116 });
1117 });
1118
1119 describe("initializer variables & functions", function() {
1120 it("can access variables defined in the initializer", function() {
1121 var parser = peg.generate([
1122 '{ var v = 42 }',
1123 'start = "a" { return v; }'
1124 ].join("\n"), options);
1125
1126 expect(parser).toParse("a", 42);
1127 });
1128
1129 it("can access functions defined in the initializer", function() {
1130 var parser = peg.generate([
1131 '{ function f() { return 42; } }',
1132 'start = "a" { return f(); }'
1133 ].join("\n"), options);
1134
1135 expect(parser).toParse("a", 42);
1136 });
1137 });
1138
1139 describe("available variables & functions", function() {
1140 it("|options| contains options", function() {
1141 var parser = peg.generate(
1142 'start = "a" { return options; }',
1143 options
1144 );
1145
1146 expect(parser).toParse("a", { a: 42 }, { a: 42 });
1147 });
1148
1149 it("|text| returns text matched by the expression", function() {
1150 var parser = peg.generate(
1151 'start = "a" { return text(); }',
1152 options
1153 );
1154
1155 expect(parser).toParse("a", "a");
1156 });
1157
1158 it("|location| returns location info of the expression", function() {
1159 var parser = peg.generate([
1160 '{ var result; }',
1161 'start = line (nl+ line)* { return result; }',
1162 'line = thing (" "+ thing)*',
1163 'thing = digit / mark',
1164 'digit = [0-9]',
1165 'mark = "x" { result = location(); }',
1166 'nl = "\\r"? "\\n"'
1167 ].join("\n"), options);
1168
1169 expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", {
1170 start: { offset: 13, line: 7, column: 5 },
1171 end: { offset: 14, line: 7, column: 6 }
1172 });
1173
1174 /* Newline representations */
1175 expect(parser).toParse("1\nx", { // Unix
1176 start: { offset: 2, line: 2, column: 1 },
1177 end: { offset: 3, line: 2, column: 2 }
1178 });
1179 expect(parser).toParse("1\r\nx", { // Windows
1180 start: { offset: 3, line: 2, column: 1 },
1181 end: { offset: 4, line: 2, column: 2 }
1182 });
1183 });
1184
1185 describe("|expected|", function() {
1186 it("terminates parsing and throws an exception", function() {
1187 var parser = peg.generate(
1188 'start = "a" { expected("a"); }',
1189 options
1190 );
1191
1192 expect(parser).toFailToParse("a", {
1193 message: 'Expected a but "a" found.',
1194 expected: [{ type: "other", description: "a" }],
1195 found: "a",
1196 location: {
1197 start: { offset: 0, line: 1, column: 1 },
1198 end: { offset: 1, line: 1, column: 2 }
1199 }
1200 });
1201 });
1202
1203 it("allows to set custom location info", function() {
1204 var parser = peg.generate([
1205 'start = "a" {',
1206 ' expected("a", {',
1207 ' start: { offset: 1, line: 1, column: 2 },',
1208 ' end: { offset: 2, line: 1, column: 3 }',
1209 ' });',
1210 '}'
1211 ].join("\n"), options);
1212
1213 expect(parser).toFailToParse("a", {
1214 message: 'Expected a but "a" found.',
1215 expected: [{ type: "other", description: "a" }],
1216 found: "a",
1217 location: {
1218 start: { offset: 1, line: 1, column: 2 },
1219 end: { offset: 2, line: 1, column: 3 }
1220 }
1221 });
1222 });
1223 });
1224
1225 describe("|error|", function() {
1226 it("terminates parsing and throws an exception", function() {
1227 var parser = peg.generate(
1228 'start = "a" { error("a"); }',
1229 options
1230 );
1231
1232 expect(parser).toFailToParse("a", {
1233 message: "a",
1234 found: null,
1235 expected: null,
1236 location: {
1237 start: { offset: 0, line: 1, column: 1 },
1238 end: { offset: 1, line: 1, column: 2 }
1239 }
1240 });
1241 });
1242
1243 it("allows to set custom location info", function() {
1244 var parser = peg.generate([
1245 'start = "a" {',
1246 ' error("a", {',
1247 ' start: { offset: 1, line: 1, column: 2 },',
1248 ' end: { offset: 2, line: 1, column: 3 }',
1249 ' });',
1250 '}'
1251 ].join("\n"), options);
1252
1253 expect(parser).toFailToParse("a", {
1254 message: "a",
1255 expected: null,
1256 found: null,
1257 location: {
1258 start: { offset: 1, line: 1, column: 2 },
1259 end: { offset: 2, line: 1, column: 3 }
1260 }
1261 });
1262 });
1263 });
1264 });
1265 });
1266
1267 describe("when the expression doesn't match", function() {
1268 it("reports match failure", function() {
1269 var parser = peg.generate('start = "a" { return 42; }', options);
1270
1271 expect(parser).toFailToParse("b");
1272 });
1273
1274 it("doesn't execute the code", function() {
1275 var parser = peg.generate(
1276 'start = "a" { throw "Boom!"; } / "b"',
1277 options
1278 );
1279
1280 expect(parser).toParse("b");
1281 });
1282 });
1283 });
1284
1285 describe("choice", function() {
1286 describe("when any expression matches", function() {
1287 it("returns its match result", function() {
1288 var parser = peg.generate('start = "a" / "b" / "c"', options);
1289
1290 expect(parser).toParse("a", "a");
1291 expect(parser).toParse("b", "b");
1292 expect(parser).toParse("c", "c");
1293 });
1294 });
1295
1296 describe("when all expressions don't match", function() {
1297 it("reports match failure", function() {
1298 var parser = peg.generate('start = "a" / "b" / "c"', options);
1299
1300 expect(parser).toFailToParse("d");
1301 });
1302 });
1303 });
1304
1305 describe("error reporting", function() {
1306 describe("behavior", function() {
1307 it("reports only the rightmost error", function() {
1308 var parser = peg.generate('start = "a" "b" / "a" "c" "d"', options);
1309
1310 expect(parser).toFailToParse("ace", {
1311 expected: [{ type: "literal", text: "d", ignoreCase: false }]
1312 });
1313 });
1314 });
1315
1316 describe("expectations reporting", function() {
1317 it("reports expectations correctly with no alternative", function() {
1318 var parser = peg.generate('start = "a"', options);
1319
1320 expect(parser).toFailToParse("ab", {
1321 expected: [{ type: "end" }]
1322 });
1323 });
1324
1325 it("reports expectations correctly with one alternative", function() {
1326 var parser = peg.generate('start = "a"', options);
1327
1328 expect(parser).toFailToParse("b", {
1329 expected: [{ type: "literal", text: "a", ignoreCase: false }]
1330 });
1331 });
1332
1333 it("reports expectations correctly with multiple alternatives", function() {
1334 var parser = peg.generate('start = "a" / "b" / "c"', options);
1335
1336 expect(parser).toFailToParse("d", {
1337 expected: [
1338 { type: "literal", text: "a", ignoreCase: false },
1339 { type: "literal", text: "b", ignoreCase: false },
1340 { type: "literal", text: "c", ignoreCase: false }
1341 ]
1342 });
1343 });
1344 });
1345
1346 describe("found string reporting", function() {
1347 it("reports found string correctly at the end of input", function() {
1348 var parser = peg.generate('start = "a"', options);
1349
1350 expect(parser).toFailToParse("", { found: null });
1351 });
1352
1353 it("reports found string correctly in the middle of input", function() {
1354 var parser = peg.generate('start = "a"', options);
1355
1356 expect(parser).toFailToParse("b", { found: "b" });
1357 });
1358 });
1359
1360 describe("message building", function() {
1361 it("builds message correctly with no alternative", function() {
1362 var parser = peg.generate('start = "a"', options);
1363
1364 expect(parser).toFailToParse("ab", {
1365 message: 'Expected end of input but "b" found.'
1366 });
1367 });
1368
1369 it("builds message correctly with one alternative", function() {
1370 var parser = peg.generate('start = "a"', options);
1371
1372 expect(parser).toFailToParse("b", {
1373 message: 'Expected "a" but "b" found.'
1374 });
1375 });
1376
1377 it("builds message correctly with multiple alternatives", function() {
1378 var parser = peg.generate('start = "a" / "b" / "c"', options);
1379
1380 expect(parser).toFailToParse("d", {
1381 message: 'Expected "a", "b", or "c" but "d" found.'
1382 });
1383 });
1384
1385 it("builds message correctly at the end of input", function() {
1386 var parser = peg.generate('start = "a"', options);
1387
1388 expect(parser).toFailToParse("", {
1389 message: 'Expected "a" but end of input found.'
1390 });
1391 });
1392
1393 it("builds message correctly in the middle of input", function() {
1394 var parser = peg.generate('start = "a"', options);
1395
1396 expect(parser).toFailToParse("b", {
1397 message: 'Expected "a" but "b" found.'
1398 });
1399 });
1400
1401 it("removes duplicates from expectations", function() {
1402 var parser = peg.generate('start = "a" / "a"', options);
1403
1404 expect(parser).toFailToParse("b", {
1405 message: 'Expected "a" but "b" found.'
1406 });
1407 });
1408
1409 it("sorts expectations", function() {
1410 var parser = peg.generate('start = "c" / "b" / "a"', options);
1411
1412 expect(parser).toFailToParse("d", {
1413 message: 'Expected "a", "b", or "c" but "d" found.'
1414 });
1415 });
1416
1417 });
1418
1419 describe("position reporting", function() {
1420 it("reports position correctly at the end of input", function() {
1421 var parser = peg.generate('start = "a"', options);
1422
1423 expect(parser).toFailToParse("", {
1424 location: {
1425 start: { offset: 0, line: 1, column: 1 },
1426 end: { offset: 0, line: 1, column: 1 }
1427 }
1428 });
1429 });
1430
1431 it("reports position correctly in the middle of input", function() {
1432 var parser = peg.generate('start = "a"', options);
1433
1434 expect(parser).toFailToParse("b", {
1435 location: {
1436 start: { offset: 0, line: 1, column: 1 },
1437 end: { offset: 1, line: 1, column: 2 }
1438 }
1439 });
1440 });
1441
1442 it("reports position correctly with trailing input", function() {
1443 var parser = peg.generate('start = "a"', options);
1444
1445 expect(parser).toFailToParse("aa", {
1446 location: {
1447 start: { offset: 1, line: 1, column: 2 },
1448 end: { offset: 2, line: 1, column: 3 }
1449 }
1450 });
1451 });
1452
1453 it("reports position correctly in complex cases", function() {
1454 var parser = peg.generate([
1455 'start = line (nl+ line)*',
1456 'line = digit (" "+ digit)*',
1457 'digit = [0-9]',
1458 'nl = "\\r"? "\\n"'
1459 ].join("\n"), options);
1460
1461 expect(parser).toFailToParse("1\n2\n\n3\n\n\n4 5 x", {
1462 location: {
1463 start: { offset: 13, line: 7, column: 5 },
1464 end: { offset: 14, line: 7, column: 6 }
1465 }
1466 });
1467
1468 /* Newline representations */
1469 expect(parser).toFailToParse("1\nx", { // Old Mac
1470 location: {
1471 start: { offset: 2, line: 2, column: 1 },
1472 end: { offset: 3, line: 2, column: 2 }
1473 }
1474 });
1475 expect(parser).toFailToParse("1\r\nx", { // Windows
1476 location: {
1477 start: { offset: 3, line: 2, column: 1 },
1478 end: { offset: 4, line: 2, column: 2 }
1479 }
1480 });
1481 });
1482 });
1483 });
1484
1485 /*
1486 * Following examples are from Wikipedia, see
1487 * http://en.wikipedia.org/w/index.php?title=Parsing_expression_grammar&oldid=335106938.
1488 */
1489 describe("complex examples", function() {
1490 it("handles arithmetics example correctly", function() {
1491 /*
1492 * Value ← [0-9]+ / '(' Expr ')'
1493 * Product ← Value (('*' / '/') Value)*
1494 * Sum ← Product (('+' / '-') Product)*
1495 * Expr ← Sum
1496 */
1497 var parser = peg.generate([
1498 'Expr = Sum',
1499 'Sum = first:Product rest:(("+" / "-") Product)* {',
1500 ' var result = first, i;',
1501 ' for (i = 0; i < rest.length; i++) {',
1502 ' if (rest[i][0] == "+") { result += rest[i][1]; }',
1503 ' if (rest[i][0] == "-") { result -= rest[i][1]; }',
1504 ' }',
1505 ' return result;',
1506 ' }',
1507 'Product = first:Value rest:(("*" / "/") Value)* {',
1508 ' var result = first, i;',
1509 ' for (i = 0; i < rest.length; i++) {',
1510 ' if (rest[i][0] == "*") { result *= rest[i][1]; }',
1511 ' if (rest[i][0] == "/") { result /= rest[i][1]; }',
1512 ' }',
1513 ' return result;',
1514 ' }',
1515 'Value = digits:[0-9]+ { return parseInt(digits.join(""), 10); }',
1516 ' / "(" expr:Expr ")" { return expr; }'
1517 ].join("\n"), options);
1518
1519 /* The "value" rule */
1520 expect(parser).toParse("0", 0);
1521 expect(parser).toParse("123", 123);
1522 expect(parser).toParse("(42+43)", 42+43);
1523
1524 /* The "product" rule */
1525 expect(parser).toParse("42", 42);
1526 expect(parser).toParse("42*43", 42*43);
1527 expect(parser).toParse("42*43*44*45", 42*43*44*45);
1528 expect(parser).toParse("42/43", 42/43);
1529 expect(parser).toParse("42/43/44/45", 42/43/44/45);
1530
1531 /* The "sum" rule */
1532 expect(parser).toParse("42*43", 42*43);
1533 expect(parser).toParse("42*43+44*45", 42*43+44*45);
1534 expect(parser).toParse("42*43+44*45+46*47+48*49", 42*43+44*45+46*47+48*49);
1535 expect(parser).toParse("42*43-44*45", 42*43-44*45);
1536 expect(parser).toParse("42*43-44*45-46*47-48*49", 42*43-44*45-46*47-48*49);
1537
1538 /* The "expr" rule */
1539 expect(parser).toParse("42+43", 42+43);
1540
1541 /* Complex test */
1542 expect(parser).toParse("(1+2)*(3+4)", (1+2)*(3+4));
1543 });
1544
1545 it("handles non-context-free language correctly", function() {
1546 /* The following parsing expression grammar describes the classic
1547 * non-context-free language { a^n b^n c^n : n >= 1 }:
1548 *
1549 * S ← &(A c) a+ B !(a/b/c)
1550 * A ← a A? b
1551 * B ← b B? c
1552 */
1553 var parser = peg.generate([
1554 'S = &(A "c") a:"a"+ B:B !("a" / "b" / "c") { return a.join("") + B; }',
1555 'A = a:"a" A:A? b:"b" { return [a, A, b].join(""); }',
1556 'B = b:"b" B:B? c:"c" { return [b, B, c].join(""); }'
1557 ].join("\n"), options);
1558
1559 expect(parser).toParse("abc", "abc");
1560 expect(parser).toParse("aaabbbccc", "aaabbbccc");
1561 expect(parser).toFailToParse("aabbbccc");
1562 expect(parser).toFailToParse("aaaabbbccc");
1563 expect(parser).toFailToParse("aaabbccc");
1564 expect(parser).toFailToParse("aaabbbbccc");
1565 expect(parser).toFailToParse("aaabbbcc");
1566 expect(parser).toFailToParse("aaabbbcccc");
1567 });
1568
1569 it("handles nested comments example correctly", function() {
1570 /*
1571 * Begin ← "(*"
1572 * End ← "*)"
1573 * C ← Begin N* End
1574 * N ← C / (!Begin !End Z)
1575 * Z ← any single character
1576 */
1577 var parser = peg.generate([
1578 'C = begin:Begin ns:N* end:End { return begin + ns.join("") + end; }',
1579 'N = C',
1580 ' / !Begin !End z:Z { return z; }',
1581 'Z = .',
1582 'Begin = "(*"',
1583 'End = "*)"'
1584 ].join("\n"), options);
1585
1586 expect(parser).toParse("(**)", "(**)");
1587 expect(parser).toParse("(*abc*)", "(*abc*)");
1588 expect(parser).toParse("(*(**)*)", "(*(**)*)");
1589 expect(parser).toParse(
1590 "(*abc(*def*)ghi(*(*(*jkl*)*)*)mno*)",
1591 "(*abc(*def*)ghi(*(*(*jkl*)*)*)mno*)"
1592 );
1593 });
1594 });
1595 });
1596 });