Debian Codegraph @

spec/api/pegjs-api.spec.js

raw symbols SCIP
1 /* global peg */
2
3 "use strict";
4
5 describe("PEG.js API", function() {
6 describe("generate", function() {
7 it("generates a parser", function() {
8 var parser = peg.generate('start = "a"');
9
10 expect(typeof parser).toBe("object");
11 expect(parser.parse("a")).toBe("a");
12 });
13
14 it("throws an exception on syntax error", function() {
15 expect(function() { peg.generate('start = @'); }).toThrow();
16 });
17
18 it("throws an exception on semantic error", function() {
19 expect(function() { peg.generate('start = undefined'); }).toThrow();
20 });
21
22 describe("allowed start rules", function() {
23 var grammar = [
24 'a = "x"',
25 'b = "x"',
26 'c = "x"'
27 ].join("\n");
28
29 /*
30 * The |allowedStartRules| option is implemented separately for each
31 * optimization mode, so we need to test it in both.
32 */
33
34 describe("when optimizing for parsing speed", function() {
35 describe("when |allowedStartRules| is not set", function() {
36 it("generated parser can start only from the first rule", function() {
37 var parser = peg.generate(grammar, { optimize: "speed" });
38
39 expect(parser.parse("x", { startRule: "a" })).toBe("x");
40 expect(
41 function() { parser.parse("x", { startRule: "b" }); }
42 ).toThrow();
43 expect(
44 function() { parser.parse("x", { startRule: "c" }); }
45 ).toThrow();
46 });
47 });
48
49 describe("when |allowedStartRules| is set", function() {
50 it("generated parser can start only from specified rules", function() {
51 var parser = peg.generate(grammar, {
52 optimize: "speed",
53 allowedStartRules: ["b", "c"]
54 });
55
56 expect(
57 function() { parser.parse("x", { startRule: "a" }); }
58 ).toThrow();
59 expect(parser.parse("x", { startRule: "b" })).toBe("x");
60 expect(parser.parse("x", { startRule: "c" })).toBe("x");
61 });
62 });
63 });
64
65 describe("when optimizing for code size", function() {
66 describe("when |allowedStartRules| is not set", function() {
67 it("generated parser can start only from the first rule", function() {
68 var parser = peg.generate(grammar, { optimize: "size" });
69
70 expect(parser.parse("x", { startRule: "a" })).toBe("x");
71 expect(
72 function() { parser.parse("x", { startRule: "b" }); }
73 ).toThrow();
74 expect(
75 function() { parser.parse("x", { startRule: "c" }); }
76 ).toThrow();
77 });
78 });
79
80 describe("when |allowedStartRules| is set", function() {
81 it("generated parser can start only from specified rules", function() {
82 var parser = peg.generate(grammar, {
83 optimize: "size",
84 allowedStartRules: ["b", "c"]
85 });
86
87 expect(
88 function() { parser.parse("x", { startRule: "a" }); }
89 ).toThrow();
90 expect(parser.parse("x", { startRule: "b" })).toBe("x");
91 expect(parser.parse("x", { startRule: "c" })).toBe("x");
92 });
93 });
94 });
95 });
96
97 describe("intermediate results caching", function() {
98 var grammar = [
99 '{ var n = 0; }',
100 'start = (a "b") / (a "c") { return n; }',
101 'a = "a" { n++; }'
102 ].join("\n");
103
104 describe("when |cache| is not set", function() {
105 it("generated parser doesn't cache intermediate parse results", function() {
106 var parser = peg.generate(grammar);
107
108 expect(parser.parse("ac")).toBe(2);
109 });
110 });
111
112 describe("when |cache| is set to |false|", function() {
113 it("generated parser doesn't cache intermediate parse results", function() {
114 var parser = peg.generate(grammar, { cache: false });
115
116 expect(parser.parse("ac")).toBe(2);
117 });
118 });
119
120 describe("when |cache| is set to |true|", function() {
121 it("generated parser caches intermediate parse results", function() {
122 var parser = peg.generate(grammar, { cache: true });
123
124 expect(parser.parse("ac")).toBe(1);
125 });
126 });
127 });
128
129 describe("tracing", function() {
130 var grammar = 'start = "a"';
131
132 describe("when |trace| is not set", function() {
133 it("generated parser doesn't trace", function() {
134 var parser = peg.generate(grammar),
135 tracer = jasmine.createSpyObj("tracer", ["trace"]);
136
137 parser.parse("a", { tracer: tracer });
138
139 expect(tracer.trace).not.toHaveBeenCalled();
140 });
141 });
142
143 describe("when |trace| is set to |false|", function() {
144 it("generated parser doesn't trace", function() {
145 var parser = peg.generate(grammar, { trace: false }),
146 tracer = jasmine.createSpyObj("tracer", ["trace"]);
147
148 parser.parse("a", { tracer: tracer });
149
150 expect(tracer.trace).not.toHaveBeenCalled();
151 });
152 });
153
154 describe("when |trace| is set to |true|", function() {
155 it("generated parser traces", function() {
156 var parser = peg.generate(grammar, { trace: true }),
157 tracer = jasmine.createSpyObj("tracer", ["trace"]);
158
159 parser.parse("a", { tracer: tracer });
160
161 expect(tracer.trace).toHaveBeenCalled();
162 });
163 });
164 });
165
166 /*
167 * The |optimize| option isn't tested because there is no meaningful way to
168 * write the specs without turning this into a performance test.
169 */
170
171 describe("output", function() {
172 var grammar = 'start = "a"';
173
174 describe("when |output| is not set", function() {
175 it("returns generated parser object", function() {
176 var parser = peg.generate(grammar);
177
178 expect(typeof parser).toBe("object");
179 expect(parser.parse("a")).toBe("a");
180 });
181 });
182
183 describe("when |output| is set to |\"parser\"|", function() {
184 it("returns generated parser object", function() {
185 var parser = peg.generate(grammar, { output: "parser" });
186
187 expect(typeof parser).toBe("object");
188 expect(parser.parse("a")).toBe("a");
189 });
190 });
191
192 describe("when |output| is set to |\"source\"|", function() {
193 it("returns generated parser source code", function() {
194 var source = peg.generate(grammar, { output: "source" });
195
196 expect(typeof source).toBe("string");
197 expect(eval(source).parse("a")).toBe("a");
198 });
199 });
200 });
201
202 /*
203 * The |format|, |exportVars|, and |dependencies| options are not tested
204 * becasue there is no meaningful way to thest their effects without turning
205 * this into an integration test.
206 */
207
208 /* The |plugins| option is tested in plugin API specs. */
209
210 it("accepts custom options", function() {
211 peg.generate('start = "a"', { foo: 42 });
212 });
213 });
214 });