Debian Codegraph @

spec/api/generated-parser-api.spec.js

raw symbols SCIP
1 /* eslint no-console: 0 */
2 /* global peg, console */
3
4 "use strict";
5
6 describe("generated parser API", function() {
7 describe("parse", function() {
8 it("parses input", function() {
9 var parser = peg.generate('start = "a"');
10
11 expect(parser.parse("a")).toBe("a");
12 });
13
14 it("throws an exception on syntax error", function() {
15 var parser = peg.generate('start = "a"');
16
17 expect(function() { parser.parse("b"); }).toThrow();
18 });
19
20 describe("start rule", function() {
21 var parser = peg.generate([
22 'a = "x" { return "a"; }',
23 'b = "x" { return "b"; }',
24 'c = "x" { return "c"; }'
25 ].join("\n"), { allowedStartRules: ["b", "c"] });
26
27 describe("when |startRule| is not set", function() {
28 it("starts parsing from the first allowed rule", function() {
29 expect(parser.parse("x")).toBe("b");
30 });
31 });
32
33 describe("when |startRule| is set to an allowed rule", function() {
34 it("starts parsing from specified rule", function() {
35 expect(parser.parse("x", { startRule: "b" })).toBe("b");
36 expect(parser.parse("x", { startRule: "c" })).toBe("c");
37 });
38 });
39
40 describe("when |startRule| is set to a disallowed start rule", function() {
41 it("throws an exception", function() {
42 expect(
43 function() { parser.parse("x", { startRule: "a" }); }
44 ).toThrow();
45 });
46 });
47 });
48
49 describe("tracing", function() {
50 var parser = peg.generate([
51 'start = a / b',
52 'a = "a"',
53 'b = "b"'
54 ].join("\n"), { trace: true });
55
56 describe("default tracer", function() {
57 it("traces using console.log (if console is defined)", function() {
58 if (typeof console === "object") {
59 spyOn(console, "log");
60 }
61
62 parser.parse("b");
63
64 if (typeof console === "object") {
65 expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.enter start");
66 expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.enter a");
67 expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.fail a");
68 expect(console.log).toHaveBeenCalledWith("1:1-1:1 rule.enter b");
69 expect(console.log).toHaveBeenCalledWith("1:1-1:2 rule.match b");
70 expect(console.log).toHaveBeenCalledWith("1:1-1:2 rule.match start");
71 }
72 });
73 });
74
75 describe("custom tracers", function() {
76 describe("trace", function() {
77 it("receives tracing events", function() {
78 var tracer = jasmine.createSpyObj("tracer", ["trace"]);
79
80 parser.parse("b", { tracer: tracer });
81
82 expect(tracer.trace).toHaveBeenCalledWith({
83 type: "rule.enter",
84 rule: "start",
85 location: {
86 start: { offset: 0, line: 1, column: 1 },
87 end: { offset: 0, line: 1, column: 1 }
88 }
89 });
90 expect(tracer.trace).toHaveBeenCalledWith({
91 type: "rule.enter",
92 rule: "a",
93 location: {
94 start: { offset: 0, line: 1, column: 1 },
95 end: { offset: 0, line: 1, column: 1 }
96 }
97 });
98 expect(tracer.trace).toHaveBeenCalledWith({
99 type: "rule.fail",
100 rule: "a",
101 location: {
102 start: { offset: 0, line: 1, column: 1 },
103 end: { offset: 0, line: 1, column: 1 }
104 }
105 });
106 expect(tracer.trace).toHaveBeenCalledWith({
107 type: "rule.enter",
108 rule: "b",
109 location: {
110 start: { offset: 0, line: 1, column: 1 },
111 end: { offset: 0, line: 1, column: 1 }
112 }
113 });
114 expect(tracer.trace).toHaveBeenCalledWith({
115 type: "rule.match",
116 rule: "b",
117 result: "b",
118 location: {
119 start: { offset: 0, line: 1, column: 1 },
120 end: { offset: 1, line: 1, column: 2 }
121 }
122 });
123 expect(tracer.trace).toHaveBeenCalledWith({
124 type: "rule.match",
125 rule: "start",
126 result: "b",
127 location: {
128 start: { offset: 0, line: 1, column: 1 },
129 end: { offset: 1, line: 1, column: 2 }
130 }
131 });
132 });
133 });
134 });
135 });
136
137 it("accepts custom options", function() {
138 var parser = peg.generate('start = "a"');
139
140 parser.parse("a", { foo: 42 });
141 });
142 });
143 });