Rename the "buildParser" function to "generate"

In most places, we talk about "generating a parser", not "building a
parser", which the function name should reflect. Also, mentioning a
parser in the name is not necessary as in case of a parser generator
it's pretty clear what is generated.
redux
David Majda 8 years ago
parent 0847a69643
commit f4504a93fe

@ -87,9 +87,9 @@ You can tweak the generated parser with several options:
* `--plugin` — makes PEG.js use a specified plugin (can be specified multiple * `--plugin` — makes PEG.js use a specified plugin (can be specified multiple
times) times)
* `--extra-options` — additional options (in JSON format) to pass to * `--extra-options` — additional options (in JSON format) to pass to
`peg.buildParser` `peg.generate`
* `--extra-options-file` — file with additional options (in JSON format) to * `--extra-options-file` — file with additional options (in JSON format) to
pass to `peg.buildParser` pass to `peg.generate`
* `--trace` — makes the parser trace its progress * `--trace` — makes the parser trace its progress
### JavaScript API ### JavaScript API
@ -102,10 +102,10 @@ In browser, include the PEG.js library in your web page or application using the
`<script>` tag. If PEG.js detects an AMD loader, it will define itself as a `<script>` tag. If PEG.js detects an AMD loader, it will define itself as a
module, otherwise the API will be available in the `peg` global object. module, otherwise the API will be available in the `peg` global object.
To generate a parser, call the `peg.buildParser` method and pass your grammar as To generate a parser, call the `peg.generate` method and pass your grammar as a
a parameter: parameter:
var parser = peg.buildParser("start = ('a' / 'b')+"); var parser = peg.generate("start = ('a' / 'b')+");
The method will return generated parser object or its source code as a string The method will return generated parser object or its source code as a string
(depending on the value of the `output` option — see below). It will throw an (depending on the value of the `output` option — see below). It will throw an
@ -113,7 +113,7 @@ exception if the grammar is invalid. The exception will contain `message`
property with more details about the error. property with more details about the error.
You can tweak the generated parser by passing a second parameter with an options You can tweak the generated parser by passing a second parameter with an options
object to `peg.buildParser`. The following options are supported: object to `peg.generate`. The following options are supported:
* `cache` — if `true`, makes the parser cache results, avoiding exponential * `cache` — if `true`, makes the parser cache results, avoiding exponential
parsing time in pathological cases but making the parser slower (default: parsing time in pathological cases but making the parser slower (default:

@ -63,7 +63,7 @@
return function() { return function() {
callbacks.benchmarkStart(benchmarks[i]); callbacks.benchmarkStart(benchmarks[i]);
state.parser = peg.buildParser( state.parser = peg.generate(
callbacks.readFile("../examples/" + benchmarks[i].id + ".pegjs"), callbacks.readFile("../examples/" + benchmarks[i].id + ".pegjs"),
options options
); );

@ -39,9 +39,9 @@ function printHelp() {
console.log(" --plugin <plugin> use a specified plugin (can be specified"); console.log(" --plugin <plugin> use a specified plugin (can be specified");
console.log(" multiple times)"); console.log(" multiple times)");
console.log(" --extra-options <options> additional options (in JSON format) to pass"); console.log(" --extra-options <options> additional options (in JSON format) to pass");
console.log(" to peg.buildParser"); console.log(" to peg.generate");
console.log(" --extra-options-file <file> file with additional options (in JSON"); console.log(" --extra-options-file <file> file with additional options (in JSON");
console.log(" format) to pass to peg.buildParser"); console.log(" format) to pass to peg.generate");
console.log(" -v, --version print version information and exit"); console.log(" -v, --version print version information and exit");
console.log(" -h, --help print help and exit"); console.log(" -h, --help print help and exit");
} }
@ -272,7 +272,7 @@ readStream(inputStream, function(input) {
var source; var source;
try { try {
source = peg.buildParser(input, options); source = peg.generate(input, options);
} catch (e) { } catch (e) {
if (e.location !== undefined) { if (e.location !== undefined) {
abort(e.location.start.line + ":" + e.location.start.column + ": " + e.message); abort(e.location.start.line + ":" + e.location.start.column + ": " + e.message);

@ -22,7 +22,7 @@ var peg = {
* errors are detected during the generation and some may protrude to the * errors are detected during the generation and some may protrude to the
* generated parser and cause its malfunction. * generated parser and cause its malfunction.
*/ */
buildParser: function(grammar, options) { generate: function(grammar, options) {
options = options !== void 0 ? options : {}; options = options !== void 0 ? options : {};
function convertPasses(passes) { function convertPasses(passes) {

@ -6,19 +6,19 @@
describe("generated parser API", function() { describe("generated parser API", function() {
describe("parse", function() { describe("parse", function() {
it("parses input", function() { it("parses input", function() {
var parser = peg.buildParser('start = "a"'); var parser = peg.generate('start = "a"');
expect(parser.parse("a")).toBe("a"); expect(parser.parse("a")).toBe("a");
}); });
it("throws an exception on syntax error", function() { it("throws an exception on syntax error", function() {
var parser = peg.buildParser('start = "a"'); var parser = peg.generate('start = "a"');
expect(function() { parser.parse("b"); }).toThrow(); expect(function() { parser.parse("b"); }).toThrow();
}); });
describe("start rule", function() { describe("start rule", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'a = "x" { return "a"; }', 'a = "x" { return "a"; }',
'b = "x" { return "b"; }', 'b = "x" { return "b"; }',
'c = "x" { return "c"; }' 'c = "x" { return "c"; }'
@ -47,7 +47,7 @@ describe("generated parser API", function() {
}); });
describe("tracing", function() { describe("tracing", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'start = a / b', 'start = a / b',
'a = "a"', 'a = "a"',
'b = "b"' 'b = "b"'
@ -135,7 +135,7 @@ describe("generated parser API", function() {
}); });
it("accepts custom options", function() { it("accepts custom options", function() {
var parser = peg.buildParser('start = "a"'); var parser = peg.generate('start = "a"');
parser.parse("a", { foo: 42 }); parser.parse("a", { foo: 42 });
}); });

@ -3,20 +3,20 @@
"use strict"; "use strict";
describe("PEG.js API", function() { describe("PEG.js API", function() {
describe("buildParser", function() { describe("generate", function() {
it("builds a parser", function() { it("builds a parser", function() {
var parser = peg.buildParser('start = "a"'); var parser = peg.generate('start = "a"');
expect(typeof parser).toBe("object"); expect(typeof parser).toBe("object");
expect(parser.parse("a")).toBe("a"); expect(parser.parse("a")).toBe("a");
}); });
it("throws an exception on syntax error", function() { it("throws an exception on syntax error", function() {
expect(function() { peg.buildParser('start = @'); }).toThrow(); expect(function() { peg.generate('start = @'); }).toThrow();
}); });
it("throws an exception on semantic error", function() { it("throws an exception on semantic error", function() {
expect(function() { peg.buildParser('start = missing'); }).toThrow(); expect(function() { peg.generate('start = missing'); }).toThrow();
}); });
describe("allowed start rules", function() { describe("allowed start rules", function() {
@ -34,7 +34,7 @@ describe("PEG.js API", function() {
describe("when optimizing for parsing speed", function() { describe("when optimizing for parsing speed", function() {
describe("when |allowedStartRules| is not set", function() { describe("when |allowedStartRules| is not set", function() {
it("generated parser can start only from the first rule", function() { it("generated parser can start only from the first rule", function() {
var parser = peg.buildParser(grammar, { optimize: "speed" }); var parser = peg.generate(grammar, { optimize: "speed" });
expect(parser.parse("x", { startRule: "a" })).toBe("x"); expect(parser.parse("x", { startRule: "a" })).toBe("x");
expect( expect(
@ -48,7 +48,7 @@ describe("PEG.js API", function() {
describe("when |allowedStartRules| is set", function() { describe("when |allowedStartRules| is set", function() {
it("generated parser can start only from specified rules", function() { it("generated parser can start only from specified rules", function() {
var parser = peg.buildParser(grammar, { var parser = peg.generate(grammar, {
optimize: "speed", optimize: "speed",
allowedStartRules: ["b", "c"] allowedStartRules: ["b", "c"]
}); });
@ -65,7 +65,7 @@ describe("PEG.js API", function() {
describe("when optimizing for code size", function() { describe("when optimizing for code size", function() {
describe("when |allowedStartRules| is not set", function() { describe("when |allowedStartRules| is not set", function() {
it("generated parser can start only from the first rule", function() { it("generated parser can start only from the first rule", function() {
var parser = peg.buildParser(grammar, { optimize: "size" }); var parser = peg.generate(grammar, { optimize: "size" });
expect(parser.parse("x", { startRule: "a" })).toBe("x"); expect(parser.parse("x", { startRule: "a" })).toBe("x");
expect( expect(
@ -79,7 +79,7 @@ describe("PEG.js API", function() {
describe("when |allowedStartRules| is set", function() { describe("when |allowedStartRules| is set", function() {
it("generated parser can start only from specified rules", function() { it("generated parser can start only from specified rules", function() {
var parser = peg.buildParser(grammar, { var parser = peg.generate(grammar, {
optimize: "size", optimize: "size",
allowedStartRules: ["b", "c"] allowedStartRules: ["b", "c"]
}); });
@ -103,7 +103,7 @@ describe("PEG.js API", function() {
describe("when |cache| is not set", function() { describe("when |cache| is not set", function() {
it("generated parser doesn't cache intermediate parse results", function() { it("generated parser doesn't cache intermediate parse results", function() {
var parser = peg.buildParser(grammar); var parser = peg.generate(grammar);
expect(parser.parse("ac")).toBe(2); expect(parser.parse("ac")).toBe(2);
}); });
@ -111,7 +111,7 @@ describe("PEG.js API", function() {
describe("when |cache| is set to |false|", function() { describe("when |cache| is set to |false|", function() {
it("generated parser doesn't cache intermediate parse results", function() { it("generated parser doesn't cache intermediate parse results", function() {
var parser = peg.buildParser(grammar, { cache: false }); var parser = peg.generate(grammar, { cache: false });
expect(parser.parse("ac")).toBe(2); expect(parser.parse("ac")).toBe(2);
}); });
@ -119,7 +119,7 @@ describe("PEG.js API", function() {
describe("when |cache| is set to |true|", function() { describe("when |cache| is set to |true|", function() {
it("generated parser caches intermediate parse results", function() { it("generated parser caches intermediate parse results", function() {
var parser = peg.buildParser(grammar, { cache: true }); var parser = peg.generate(grammar, { cache: true });
expect(parser.parse("ac")).toBe(1); expect(parser.parse("ac")).toBe(1);
}); });
@ -131,7 +131,7 @@ describe("PEG.js API", function() {
describe("when |trace| is not set", function() { describe("when |trace| is not set", function() {
it("generated parser doesn't trace", function() { it("generated parser doesn't trace", function() {
var parser = peg.buildParser(grammar), var parser = peg.generate(grammar),
tracer = jasmine.createSpyObj("tracer", ["trace"]); tracer = jasmine.createSpyObj("tracer", ["trace"]);
parser.parse("a", { tracer: tracer }); parser.parse("a", { tracer: tracer });
@ -142,7 +142,7 @@ describe("PEG.js API", function() {
describe("when |trace| is set to |false|", function() { describe("when |trace| is set to |false|", function() {
it("generated parser doesn't trace", function() { it("generated parser doesn't trace", function() {
var parser = peg.buildParser(grammar, { trace: false }), var parser = peg.generate(grammar, { trace: false }),
tracer = jasmine.createSpyObj("tracer", ["trace"]); tracer = jasmine.createSpyObj("tracer", ["trace"]);
parser.parse("a", { tracer: tracer }); parser.parse("a", { tracer: tracer });
@ -153,7 +153,7 @@ describe("PEG.js API", function() {
describe("when |trace| is set to |true|", function() { describe("when |trace| is set to |true|", function() {
it("generated parser traces", function() { it("generated parser traces", function() {
var parser = peg.buildParser(grammar, { trace: true }), var parser = peg.generate(grammar, { trace: true }),
tracer = jasmine.createSpyObj("tracer", ["trace"]); tracer = jasmine.createSpyObj("tracer", ["trace"]);
parser.parse("a", { tracer: tracer }); parser.parse("a", { tracer: tracer });
@ -173,7 +173,7 @@ describe("PEG.js API", function() {
describe("when |output| is not set", function() { describe("when |output| is not set", function() {
it("returns generated parser object", function() { it("returns generated parser object", function() {
var parser = peg.buildParser(grammar); var parser = peg.generate(grammar);
expect(typeof parser).toBe("object"); expect(typeof parser).toBe("object");
expect(parser.parse("a")).toBe("a"); expect(parser.parse("a")).toBe("a");
@ -182,7 +182,7 @@ describe("PEG.js API", function() {
describe("when |output| is set to |\"parser\"|", function() { describe("when |output| is set to |\"parser\"|", function() {
it("returns generated parser object", function() { it("returns generated parser object", function() {
var parser = peg.buildParser(grammar, { output: "parser" }); var parser = peg.generate(grammar, { output: "parser" });
expect(typeof parser).toBe("object"); expect(typeof parser).toBe("object");
expect(parser.parse("a")).toBe("a"); expect(parser.parse("a")).toBe("a");
@ -191,7 +191,7 @@ describe("PEG.js API", function() {
describe("when |output| is set to |\"source\"|", function() { describe("when |output| is set to |\"source\"|", function() {
it("returns generated parser source code", function() { it("returns generated parser source code", function() {
var source = peg.buildParser(grammar, { output: "source" }); var source = peg.generate(grammar, { output: "source" });
expect(typeof source).toBe("string"); expect(typeof source).toBe("string");
expect(eval(source).parse("a")).toBe("a"); expect(eval(source).parse("a")).toBe("a");
@ -208,7 +208,7 @@ describe("PEG.js API", function() {
/* The |plugins| option is tested in plugin API specs. */ /* The |plugins| option is tested in plugin API specs. */
it("accepts custom options", function() { it("accepts custom options", function() {
peg.buildParser('start = "a"', { foo: 42 }); peg.generate('start = "a"', { foo: 42 });
}); });
}); });
}); });

@ -48,7 +48,7 @@ describe("plugin API", function() {
{ use: function() { pluginsUsed[2] = true; } } { use: function() { pluginsUsed[2] = true; } }
]; ];
peg.buildParser(grammar, { plugins: plugins }); peg.generate(grammar, { plugins: plugins });
expect(pluginsUsed).toEqual([true, true, true]); expect(pluginsUsed).toEqual([true, true, true]);
}); });
@ -82,24 +82,24 @@ describe("plugin API", function() {
} }
}; };
peg.buildParser(grammar, { plugins: [plugin] }); peg.generate(grammar, { plugins: [plugin] });
}); });
it("receives options", function() { it("receives options", function() {
var plugin = { var plugin = {
use: function(config, options) { use: function(config, options) {
expect(options).toEqual(buildParserOptions); expect(options).toEqual(generateOptions);
} }
}, },
buildParserOptions = { plugins: [plugin], foo: 42 }; generateOptions = { plugins: [plugin], foo: 42 };
peg.buildParser(grammar, buildParserOptions); peg.generate(grammar, generateOptions);
}); });
it("can replace parser", function() { it("can replace parser", function() {
var plugin = { var plugin = {
use: function(config) { use: function(config) {
var parser = peg.buildParser([ var parser = peg.generate([
'start = .* {', 'start = .* {',
' return {', ' return {',
' type: "grammar",', ' type: "grammar",',
@ -117,7 +117,7 @@ describe("plugin API", function() {
config.parser = parser; config.parser = parser;
} }
}, },
parser = peg.buildParser('a', { plugins: [plugin] }); parser = peg.generate('a', { plugins: [plugin] });
expect(parser.parse("a")).toBe("a"); expect(parser.parse("a")).toBe("a");
}); });
@ -132,7 +132,7 @@ describe("plugin API", function() {
config.passes.generate = [pass]; config.passes.generate = [pass];
} }
}, },
parser = peg.buildParser(grammar, { plugins: [plugin] }); parser = peg.generate(grammar, { plugins: [plugin] });
expect(parser.parse("a")).toBe(42); expect(parser.parse("a")).toBe(42);
}); });
@ -148,7 +148,7 @@ describe("plugin API", function() {
options.allowedStartRules = ["b", "c"]; options.allowedStartRules = ["b", "c"];
} }
}, },
parser = peg.buildParser(grammar, { parser = peg.generate(grammar, {
allowedStartRules: ["a"], allowedStartRules: ["a"],
plugins: [plugin] plugins: [plugin]
}); });

@ -130,7 +130,7 @@ describe("generated parser behavior", function() {
varyOptimizationOptions(function(options) { varyOptimizationOptions(function(options) {
describe("initializer", function() { describe("initializer", function() {
it("executes the code before parsing starts", function() { it("executes the code before parsing starts", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var result = 42; }', '{ var result = 42; }',
'start = "a" { return result; }' 'start = "a" { return result; }'
].join("\n"), options); ].join("\n"), options);
@ -140,7 +140,7 @@ describe("generated parser behavior", function() {
describe("available variables and functions", function() { describe("available variables and functions", function() {
it("|parser| contains the parser object", function() { it("|parser| contains the parser object", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var result = parser; }', '{ var result = parser; }',
'start = "a" { return result; }' 'start = "a" { return result; }'
].join("\n"), options); ].join("\n"), options);
@ -149,7 +149,7 @@ describe("generated parser behavior", function() {
}); });
it("|options| contains options", function() { it("|options| contains options", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var result = options; }', '{ var result = options; }',
'start = "a" { return result; }' 'start = "a" { return result; }'
].join("\n"), options); ].join("\n"), options);
@ -162,7 +162,7 @@ describe("generated parser behavior", function() {
describe("rule", function() { describe("rule", function() {
if (options.cache) { if (options.cache) {
it("caches rule match results", function() { it("caches rule match results", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var n = 0; }', '{ var n = 0; }',
'start = (a "b") / (a "c") { return n; }', 'start = (a "b") / (a "c") { return n; }',
'a = "a" { n++; }' 'a = "a" { n++; }'
@ -172,7 +172,7 @@ describe("generated parser behavior", function() {
}); });
} else { } else {
it("doesn't cache rule match results", function() { it("doesn't cache rule match results", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var n = 0; }', '{ var n = 0; }',
'start = (a "b") / (a "c") { return n; }', 'start = (a "b") / (a "c") { return n; }',
'a = "a" { n++; }' 'a = "a" { n++; }'
@ -184,7 +184,7 @@ describe("generated parser behavior", function() {
describe("when the expression matches", function() { describe("when the expression matches", function() {
it("returns its match result", function() { it("returns its match result", function() {
var parser = peg.buildParser('start = "a"'); var parser = peg.generate('start = "a"');
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
@ -193,7 +193,7 @@ describe("generated parser behavior", function() {
describe("when the expression doesn't match", function() { describe("when the expression doesn't match", function() {
describe("without display name", function() { describe("without display name", function() {
it("reports match failure and doesn't record any expectation", function() { it("reports match failure and doesn't record any expectation", function() {
var parser = peg.buildParser('start = "a"'); var parser = peg.generate('start = "a"');
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "literal", value: "a", description: '"a"' }] expected: [{ type: "literal", value: "a", description: '"a"' }]
@ -203,7 +203,7 @@ describe("generated parser behavior", function() {
describe("with display name", function() { describe("with display name", function() {
it("reports match failure and records an expectation of type \"other\"", function() { it("reports match failure and records an expectation of type \"other\"", function() {
var parser = peg.buildParser('start "start" = "a"'); var parser = peg.generate('start "start" = "a"');
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "other", description: "start" }] expected: [{ type: "other", description: "start" }]
@ -211,7 +211,7 @@ describe("generated parser behavior", function() {
}); });
it("discards any expectations recorded when matching the expression", function() { it("discards any expectations recorded when matching the expression", function() {
var parser = peg.buildParser('start "start" = "a"'); var parser = peg.generate('start "start" = "a"');
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "other", description: "start" }] expected: [{ type: "other", description: "start" }]
@ -224,34 +224,34 @@ describe("generated parser behavior", function() {
describe("literal", function() { describe("literal", function() {
describe("matching", function() { describe("matching", function() {
it("matches empty literals", function() { it("matches empty literals", function() {
var parser = peg.buildParser('start = ""', options); var parser = peg.generate('start = ""', options);
expect(parser).toParse(""); expect(parser).toParse("");
}); });
it("matches one-character literals", function() { it("matches one-character literals", function() {
var parser = peg.buildParser('start = "a"', options); var parser = peg.generate('start = "a"', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toFailToParse("b"); expect(parser).toFailToParse("b");
}); });
it("matches multi-character literals", function() { it("matches multi-character literals", function() {
var parser = peg.buildParser('start = "abcd"', options); var parser = peg.generate('start = "abcd"', options);
expect(parser).toParse("abcd"); expect(parser).toParse("abcd");
expect(parser).toFailToParse("efgh"); expect(parser).toFailToParse("efgh");
}); });
it("is case sensitive without the \"i\" flag", function() { it("is case sensitive without the \"i\" flag", function() {
var parser = peg.buildParser('start = "a"', options); var parser = peg.generate('start = "a"', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toFailToParse("A"); expect(parser).toFailToParse("A");
}); });
it("is case insensitive with the \"i\" flag", function() { it("is case insensitive with the \"i\" flag", function() {
var parser = peg.buildParser('start = "a"i', options); var parser = peg.generate('start = "a"i', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toParse("A"); expect(parser).toParse("A");
@ -260,13 +260,13 @@ describe("generated parser behavior", function() {
describe("when it matches", function() { describe("when it matches", function() {
it("returns the matched text", function() { it("returns the matched text", function() {
var parser = peg.buildParser('start = "a"', options); var parser = peg.generate('start = "a"', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
it("consumes the matched text", function() { it("consumes the matched text", function() {
var parser = peg.buildParser('start = "a" .', options); var parser = peg.generate('start = "a" .', options);
expect(parser).toParse("ab"); expect(parser).toParse("ab");
}); });
@ -274,7 +274,7 @@ describe("generated parser behavior", function() {
describe("when it doesn't match", function() { describe("when it doesn't match", function() {
it("reports match failure and records an expectation of type \"literal\"", function() { it("reports match failure and records an expectation of type \"literal\"", function() {
var parser = peg.buildParser('start = "a"', options); var parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "literal", value: "a", description: '"a"' }] expected: [{ type: "literal", value: "a", description: '"a"' }]
@ -286,13 +286,13 @@ describe("generated parser behavior", function() {
describe("character class", function() { describe("character class", function() {
describe("matching", function() { describe("matching", function() {
it("matches empty classes", function() { it("matches empty classes", function() {
var parser = peg.buildParser('start = []', options); var parser = peg.generate('start = []', options);
expect(parser).toFailToParse("a"); expect(parser).toFailToParse("a");
}); });
it("matches classes with a character list", function() { it("matches classes with a character list", function() {
var parser = peg.buildParser('start = [abc]', options); var parser = peg.generate('start = [abc]', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toParse("b"); expect(parser).toParse("b");
@ -301,7 +301,7 @@ describe("generated parser behavior", function() {
}); });
it("matches classes with a character range", function() { it("matches classes with a character range", function() {
var parser = peg.buildParser('start = [a-c]', options); var parser = peg.generate('start = [a-c]', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toParse("b"); expect(parser).toParse("b");
@ -310,21 +310,21 @@ describe("generated parser behavior", function() {
}); });
it("matches inverted classes", function() { it("matches inverted classes", function() {
var parser = peg.buildParser('start = [^a]', options); var parser = peg.generate('start = [^a]', options);
expect(parser).toFailToParse("a"); expect(parser).toFailToParse("a");
expect(parser).toParse("b"); expect(parser).toParse("b");
}); });
it("is case sensitive without the \"i\" flag", function() { it("is case sensitive without the \"i\" flag", function() {
var parser = peg.buildParser('start = [a]', options); var parser = peg.generate('start = [a]', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toFailToParse("A"); expect(parser).toFailToParse("A");
}); });
it("is case insensitive with the \"i\" flag", function() { it("is case insensitive with the \"i\" flag", function() {
var parser = peg.buildParser('start = [a]i', options); var parser = peg.generate('start = [a]i', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toParse("A"); expect(parser).toParse("A");
@ -333,13 +333,13 @@ describe("generated parser behavior", function() {
describe("when it matches", function() { describe("when it matches", function() {
it("returns the matched character", function() { it("returns the matched character", function() {
var parser = peg.buildParser('start = [a]', options); var parser = peg.generate('start = [a]', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
it("consumes the matched character", function() { it("consumes the matched character", function() {
var parser = peg.buildParser('start = [a] .', options); var parser = peg.generate('start = [a] .', options);
expect(parser).toParse("ab"); expect(parser).toParse("ab");
}); });
@ -347,7 +347,7 @@ describe("generated parser behavior", function() {
describe("when it doesn't match", function() { describe("when it doesn't match", function() {
it("reports match failure and records an expectation of type \"class\"", function() { it("reports match failure and records an expectation of type \"class\"", function() {
var parser = peg.buildParser('start = [a]', options); var parser = peg.generate('start = [a]', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "class", value: "[a]", description: "[a]" }] expected: [{ type: "class", value: "[a]", description: "[a]" }]
@ -359,7 +359,7 @@ describe("generated parser behavior", function() {
describe("dot", function() { describe("dot", function() {
describe("matching", function() { describe("matching", function() {
it("matches any character", function() { it("matches any character", function() {
var parser = peg.buildParser('start = .', options); var parser = peg.generate('start = .', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toParse("b"); expect(parser).toParse("b");
@ -369,13 +369,13 @@ describe("generated parser behavior", function() {
describe("when it matches", function() { describe("when it matches", function() {
it("returns the matched character", function() { it("returns the matched character", function() {
var parser = peg.buildParser('start = .', options); var parser = peg.generate('start = .', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
it("consumes the matched character", function() { it("consumes the matched character", function() {
var parser = peg.buildParser('start = . .', options); var parser = peg.generate('start = . .', options);
expect(parser).toParse("ab"); expect(parser).toParse("ab");
}); });
@ -383,7 +383,7 @@ describe("generated parser behavior", function() {
describe("when it doesn't match", function() { describe("when it doesn't match", function() {
it("reports match failure and records an expectation of type \"any\"", function() { it("reports match failure and records an expectation of type \"any\"", function() {
var parser = peg.buildParser('start = .', options); var parser = peg.generate('start = .', options);
expect(parser).toFailToParse("", { expect(parser).toFailToParse("", {
expected: [{ type: "any", description: "any character" }] expected: [{ type: "any", description: "any character" }]
@ -395,7 +395,7 @@ describe("generated parser behavior", function() {
describe("rule reference", function() { describe("rule reference", function() {
describe("when referenced rule's expression matches", function() { describe("when referenced rule's expression matches", function() {
it("returns its result", function() { it("returns its result", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'start = a', 'start = a',
'a = "a"' 'a = "a"'
].join("\n"), options); ].join("\n"), options);
@ -406,7 +406,7 @@ describe("generated parser behavior", function() {
describe("when referenced rule's expression doesn't match", function() { describe("when referenced rule's expression doesn't match", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'start = a', 'start = a',
'a = "a"' 'a = "a"'
].join("\n"), options); ].join("\n"), options);
@ -424,7 +424,7 @@ describe("generated parser behavior", function() {
* |undefined| which we can't compare against in |toParse| due to the * |undefined| which we can't compare against in |toParse| due to the
* way optional parameters work. * way optional parameters work.
*/ */
var parser = peg.buildParser('start = &{ return true; } ""', options); var parser = peg.generate('start = &{ return true; } ""', options);
expect(parser).toParse("", [undefined, ""]); expect(parser).toParse("", [undefined, ""]);
}); });
@ -432,7 +432,7 @@ describe("generated parser behavior", function() {
describe("when the code returns a falsey value", function() { describe("when the code returns a falsey value", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser('start = &{ return false; }', options); var parser = peg.generate('start = &{ return false; }', options);
expect(parser).toFailToParse(""); expect(parser).toFailToParse("");
}); });
@ -441,7 +441,7 @@ describe("generated parser behavior", function() {
describe("label variables", function() { describe("label variables", function() {
describe("in containing sequence", function() { describe("in containing sequence", function() {
it("can access variables defined by preceding labeled elements", function() { it("can access variables defined by preceding labeled elements", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = a:"a" &{ return a === "a"; }', 'start = a:"a" &{ return a === "a"; }',
options options
); );
@ -450,7 +450,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variable defined by labeled predicate element", function() { it("cannot access variable defined by labeled predicate element", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = "a" b:&{ return b === undefined; } "c"', 'start = "a" b:&{ return b === undefined; } "c"',
options options
); );
@ -459,7 +459,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variables defined by following labeled elements", function() { it("cannot access variables defined by following labeled elements", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = &{ return a === "a"; } a:"a"', 'start = &{ return a === "a"; } a:"a"',
options options
); );
@ -517,7 +517,7 @@ describe("generated parser behavior", function() {
parser, i; parser, i;
for (i = 0; i < testcases.length; i++) { for (i = 0; i < testcases.length; i++) {
parser = peg.buildParser(testcases[i].grammar, options); parser = peg.generate(testcases[i].grammar, options);
expect(parser).toFailToParse(testcases[i].input); expect(parser).toFailToParse(testcases[i].input);
} }
}); });
@ -525,7 +525,7 @@ describe("generated parser behavior", function() {
describe("in outer sequence", function() { describe("in outer sequence", function() {
it("can access variables defined by preceding labeled elements", function() { it("can access variables defined by preceding labeled elements", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = a:"a" ("b" &{ return a === "a"; })', 'start = a:"a" ("b" &{ return a === "a"; })',
options options
); );
@ -534,7 +534,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variable defined by labeled predicate element", function() { it("cannot access variable defined by labeled predicate element", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = "a" b:("b" &{ return b === undefined; }) "c"', 'start = "a" b:("b" &{ return b === undefined; }) "c"',
options options
); );
@ -543,7 +543,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variables defined by following labeled elements", function() { it("cannot access variables defined by following labeled elements", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = ("a" &{ return b === "b"; }) b:"b"', 'start = ("a" &{ return b === "b"; }) b:"b"',
options options
); );
@ -555,7 +555,7 @@ describe("generated parser behavior", function() {
describe("initializer variables & functions", function() { describe("initializer variables & functions", function() {
it("can access variables defined in the initializer", function() { it("can access variables defined in the initializer", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var v = 42 }', '{ var v = 42 }',
'start = &{ return v === 42; }' 'start = &{ return v === 42; }'
].join("\n"), options); ].join("\n"), options);
@ -564,7 +564,7 @@ describe("generated parser behavior", function() {
}); });
it("can access functions defined in the initializer", function() { it("can access functions defined in the initializer", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ function f() { return 42; } }', '{ function f() { return 42; } }',
'start = &{ return f() === 42; }' 'start = &{ return f() === 42; }'
].join("\n"), options); ].join("\n"), options);
@ -575,7 +575,7 @@ describe("generated parser behavior", function() {
describe("available variables & functions", function() { describe("available variables & functions", function() {
it("|parser| contains the parser object", function() { it("|parser| contains the parser object", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var result; }', '{ var result; }',
'start = &{ result = parser; return true; } { return result; }' 'start = &{ result = parser; return true; } { return result; }'
].join("\n"), options); ].join("\n"), options);
@ -584,7 +584,7 @@ describe("generated parser behavior", function() {
}); });
it("|options| contains options", function() { it("|options| contains options", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var result; }', '{ var result; }',
'start = &{ result = options; return true; } { return result; }' 'start = &{ result = options; return true; } { return result; }'
].join("\n"), options); ].join("\n"), options);
@ -593,7 +593,7 @@ describe("generated parser behavior", function() {
}); });
it("|location| returns current location info", function() { it("|location| returns current location info", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var result; }', '{ var result; }',
'start = line (nl+ line)* { return result; }', 'start = line (nl+ line)* { return result; }',
'line = thing (" "+ thing)*', 'line = thing (" "+ thing)*',
@ -629,7 +629,7 @@ describe("generated parser behavior", function() {
* |undefined| which we can't compare against in |toParse| due to the * |undefined| which we can't compare against in |toParse| due to the
* way optional parameters work. * way optional parameters work.
*/ */
var parser = peg.buildParser('start = !{ return false; } ""', options); var parser = peg.generate('start = !{ return false; } ""', options);
expect(parser).toParse("", [undefined, ""]); expect(parser).toParse("", [undefined, ""]);
}); });
@ -637,7 +637,7 @@ describe("generated parser behavior", function() {
describe("when the code returns a truthy value", function() { describe("when the code returns a truthy value", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser('start = !{ return true; }', options); var parser = peg.generate('start = !{ return true; }', options);
expect(parser).toFailToParse(""); expect(parser).toFailToParse("");
}); });
@ -646,7 +646,7 @@ describe("generated parser behavior", function() {
describe("label variables", function() { describe("label variables", function() {
describe("in containing sequence", function() { describe("in containing sequence", function() {
it("can access variables defined by preceding labeled elements", function() { it("can access variables defined by preceding labeled elements", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = a:"a" !{ return a !== "a"; }', 'start = a:"a" !{ return a !== "a"; }',
options options
); );
@ -655,7 +655,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variable defined by labeled predicate element", function() { it("cannot access variable defined by labeled predicate element", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = "a" b:!{ return b !== undefined; } "c"', 'start = "a" b:!{ return b !== undefined; } "c"',
options options
); );
@ -664,7 +664,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variables defined by following labeled elements", function() { it("cannot access variables defined by following labeled elements", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = !{ return a !== "a"; } a:"a"', 'start = !{ return a !== "a"; } a:"a"',
options options
); );
@ -722,7 +722,7 @@ describe("generated parser behavior", function() {
parser, i; parser, i;
for (i = 0; i < testcases.length; i++) { for (i = 0; i < testcases.length; i++) {
parser = peg.buildParser(testcases[i].grammar, options); parser = peg.generate(testcases[i].grammar, options);
expect(parser).toFailToParse(testcases[i].input); expect(parser).toFailToParse(testcases[i].input);
} }
}); });
@ -730,7 +730,7 @@ describe("generated parser behavior", function() {
describe("in outer sequence", function() { describe("in outer sequence", function() {
it("can access variables defined by preceding labeled elements", function() { it("can access variables defined by preceding labeled elements", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = a:"a" ("b" !{ return a !== "a"; })', 'start = a:"a" ("b" !{ return a !== "a"; })',
options options
); );
@ -739,7 +739,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variable defined by labeled predicate element", function() { it("cannot access variable defined by labeled predicate element", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = "a" b:("b" !{ return b !== undefined; }) "c"', 'start = "a" b:("b" !{ return b !== undefined; }) "c"',
options options
); );
@ -748,7 +748,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variables defined by following labeled elements", function() { it("cannot access variables defined by following labeled elements", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = ("a" !{ return b !== "b"; }) b:"b"', 'start = ("a" !{ return b !== "b"; }) b:"b"',
options options
); );
@ -760,7 +760,7 @@ describe("generated parser behavior", function() {
describe("initializer variables & functions", function() { describe("initializer variables & functions", function() {
it("can access variables defined in the initializer", function() { it("can access variables defined in the initializer", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var v = 42 }', '{ var v = 42 }',
'start = !{ return v !== 42; }' 'start = !{ return v !== 42; }'
].join("\n"), options); ].join("\n"), options);
@ -769,7 +769,7 @@ describe("generated parser behavior", function() {
}); });
it("can access functions defined in the initializer", function() { it("can access functions defined in the initializer", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ function f() { return 42; } }', '{ function f() { return 42; } }',
'start = !{ return f() !== 42; }' 'start = !{ return f() !== 42; }'
].join("\n"), options); ].join("\n"), options);
@ -780,7 +780,7 @@ describe("generated parser behavior", function() {
describe("available variables & functions", function() { describe("available variables & functions", function() {
it("|parser| contains the parser object", function() { it("|parser| contains the parser object", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var result; }', '{ var result; }',
'start = !{ result = parser; return false; } { return result; }' 'start = !{ result = parser; return false; } { return result; }'
].join("\n"), options); ].join("\n"), options);
@ -789,7 +789,7 @@ describe("generated parser behavior", function() {
}); });
it("|options| contains options", function() { it("|options| contains options", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var result; }', '{ var result; }',
'start = !{ result = options; return false; } { return result; }' 'start = !{ result = options; return false; } { return result; }'
].join("\n"), options); ].join("\n"), options);
@ -798,7 +798,7 @@ describe("generated parser behavior", function() {
}); });
it("|location| returns current location info", function() { it("|location| returns current location info", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var result; }', '{ var result; }',
'start = line (nl+ line)* { return result; }', 'start = line (nl+ line)* { return result; }',
'line = thing (" "+ thing)*', 'line = thing (" "+ thing)*',
@ -829,7 +829,7 @@ describe("generated parser behavior", function() {
describe("group", function() { describe("group", function() {
describe("when the expression matches", function() { describe("when the expression matches", function() {
it("returns its match result", function() { it("returns its match result", function() {
var parser = peg.buildParser('start = ("a")', options); var parser = peg.generate('start = ("a")', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
@ -837,7 +837,7 @@ describe("generated parser behavior", function() {
describe("when the expression doesn't match", function() { describe("when the expression doesn't match", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser('start = ("a")', options); var parser = peg.generate('start = ("a")', options);
expect(parser).toFailToParse("b"); expect(parser).toFailToParse("b");
}); });
@ -847,7 +847,7 @@ describe("generated parser behavior", function() {
describe("optional", function() { describe("optional", function() {
describe("when the expression matches", function() { describe("when the expression matches", function() {
it("returns its match result", function() { it("returns its match result", function() {
var parser = peg.buildParser('start = "a"?', options); var parser = peg.generate('start = "a"?', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
@ -855,7 +855,7 @@ describe("generated parser behavior", function() {
describe("when the expression doesn't match", function() { describe("when the expression doesn't match", function() {
it("returns |null|", function() { it("returns |null|", function() {
var parser = peg.buildParser('start = "a"?', options); var parser = peg.generate('start = "a"?', options);
expect(parser).toParse("", null); expect(parser).toParse("", null);
}); });
@ -865,7 +865,7 @@ describe("generated parser behavior", function() {
describe("zero or more", function() { describe("zero or more", function() {
describe("when the expression matches zero or more times", function() { describe("when the expression matches zero or more times", function() {
it("returns an array of its match results", function() { it("returns an array of its match results", function() {
var parser = peg.buildParser('start = "a"*', options); var parser = peg.generate('start = "a"*', options);
expect(parser).toParse("", []); expect(parser).toParse("", []);
expect(parser).toParse("a", ["a"]); expect(parser).toParse("a", ["a"]);
@ -877,7 +877,7 @@ describe("generated parser behavior", function() {
describe("one or more", function() { describe("one or more", function() {
describe("when the expression matches one or more times", function() { describe("when the expression matches one or more times", function() {
it("returns an array of its match results", function() { it("returns an array of its match results", function() {
var parser = peg.buildParser('start = "a"+', options); var parser = peg.generate('start = "a"+', options);
expect(parser).toParse("a", ["a"]); expect(parser).toParse("a", ["a"]);
expect(parser).toParse("aaa", ["a", "a", "a"]); expect(parser).toParse("aaa", ["a", "a", "a"]);
@ -886,7 +886,7 @@ describe("generated parser behavior", function() {
describe("when the expression doesn't match", function() { describe("when the expression doesn't match", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser('start = "a"+', options); var parser = peg.generate('start = "a"+', options);
expect(parser).toFailToParse(""); expect(parser).toFailToParse("");
}); });
@ -896,7 +896,7 @@ describe("generated parser behavior", function() {
describe("text", function() { describe("text", function() {
describe("when the expression matches", function() { describe("when the expression matches", function() {
it("returns the matched text", function() { it("returns the matched text", function() {
var parser = peg.buildParser('start = $("a" "b" "c")', options); var parser = peg.generate('start = $("a" "b" "c")', options);
expect(parser).toParse("abc", "abc"); expect(parser).toParse("abc", "abc");
}); });
@ -904,7 +904,7 @@ describe("generated parser behavior", function() {
describe("when the expression doesn't match", function() { describe("when the expression doesn't match", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser('start = $("a")', options); var parser = peg.generate('start = $("a")', options);
expect(parser).toFailToParse("b"); expect(parser).toFailToParse("b");
}); });
@ -914,13 +914,13 @@ describe("generated parser behavior", function() {
describe("positive simple predicate", function() { describe("positive simple predicate", function() {
describe("when the expression matches", function() { describe("when the expression matches", function() {
it("returns |undefined|", function() { it("returns |undefined|", function() {
var parser = peg.buildParser('start = &"a" "a"', options); var parser = peg.generate('start = &"a" "a"', options);
expect(parser).toParse("a", [undefined, "a"]); expect(parser).toParse("a", [undefined, "a"]);
}); });
it("resets parse position", function() { it("resets parse position", function() {
var parser = peg.buildParser('start = &"a" "a"', options); var parser = peg.generate('start = &"a" "a"', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
}); });
@ -928,13 +928,13 @@ describe("generated parser behavior", function() {
describe("when the expression doesn't match", function() { describe("when the expression doesn't match", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser('start = &"a"', options); var parser = peg.generate('start = &"a"', options);
expect(parser).toFailToParse("b"); expect(parser).toFailToParse("b");
}); });
it("discards any expectations recorded when matching the expression", function() { it("discards any expectations recorded when matching the expression", function() {
var parser = peg.buildParser('start = "a" / &"b" / "c"', options); var parser = peg.generate('start = "a" / &"b" / "c"', options);
expect(parser).toFailToParse("d", { expect(parser).toFailToParse("d", {
expected: [ expected: [
@ -949,7 +949,7 @@ describe("generated parser behavior", function() {
describe("negative simple predicate", function() { describe("negative simple predicate", function() {
describe("when the expression matches", function() { describe("when the expression matches", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser('start = !"a"', options); var parser = peg.generate('start = !"a"', options);
expect(parser).toFailToParse("a"); expect(parser).toFailToParse("a");
}); });
@ -957,19 +957,19 @@ describe("generated parser behavior", function() {
describe("when the expression doesn't match", function() { describe("when the expression doesn't match", function() {
it("returns |undefined|", function() { it("returns |undefined|", function() {
var parser = peg.buildParser('start = !"a" "b"', options); var parser = peg.generate('start = !"a" "b"', options);
expect(parser).toParse("b", [undefined, "b"]); expect(parser).toParse("b", [undefined, "b"]);
}); });
it("resets parse position", function() { it("resets parse position", function() {
var parser = peg.buildParser('start = !"a" "b"', options); var parser = peg.generate('start = !"a" "b"', options);
expect(parser).toParse("b"); expect(parser).toParse("b");
}); });
it("discards any expectations recorded when matching the expression", function() { it("discards any expectations recorded when matching the expression", function() {
var parser = peg.buildParser('start = "a" / !"b" / "c"', options); var parser = peg.generate('start = "a" / !"b" / "c"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [ expected: [
@ -984,7 +984,7 @@ describe("generated parser behavior", function() {
describe("label", function() { describe("label", function() {
describe("when the expression matches", function() { describe("when the expression matches", function() {
it("returns its match result", function() { it("returns its match result", function() {
var parser = peg.buildParser('start = a:"a"', options); var parser = peg.generate('start = a:"a"', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
@ -992,7 +992,7 @@ describe("generated parser behavior", function() {
describe("when the expression doesn't match", function() { describe("when the expression doesn't match", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser('start = a:"a"', options); var parser = peg.generate('start = a:"a"', options);
expect(parser).toFailToParse("b"); expect(parser).toFailToParse("b");
}); });
@ -1002,7 +1002,7 @@ describe("generated parser behavior", function() {
describe("sequence", function() { describe("sequence", function() {
describe("when all expressions match", function() { describe("when all expressions match", function() {
it("returns an array of their match results", function() { it("returns an array of their match results", function() {
var parser = peg.buildParser('start = "a" "b" "c"', options); var parser = peg.generate('start = "a" "b" "c"', options);
expect(parser).toParse("abc", ["a", "b", "c"]); expect(parser).toParse("abc", ["a", "b", "c"]);
}); });
@ -1010,7 +1010,7 @@ describe("generated parser behavior", function() {
describe("when any expression doesn't match", function() { describe("when any expression doesn't match", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser('start = "a" "b" "c"', options); var parser = peg.generate('start = "a" "b" "c"', options);
expect(parser).toFailToParse("dbc"); expect(parser).toFailToParse("dbc");
expect(parser).toFailToParse("adc"); expect(parser).toFailToParse("adc");
@ -1018,7 +1018,7 @@ describe("generated parser behavior", function() {
}); });
it("resets parse position", function() { it("resets parse position", function() {
var parser = peg.buildParser('start = "a" "b" / "a"', options); var parser = peg.generate('start = "a" "b" / "a"', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
@ -1028,7 +1028,7 @@ describe("generated parser behavior", function() {
describe("action", function() { describe("action", function() {
describe("when the expression matches", function() { describe("when the expression matches", function() {
it("returns the value returned by the code", function() { it("returns the value returned by the code", function() {
var parser = peg.buildParser('start = "a" { return 42; }', options); var parser = peg.generate('start = "a" { return 42; }', options);
expect(parser).toParse("a", 42); expect(parser).toParse("a", 42);
}); });
@ -1036,13 +1036,13 @@ describe("generated parser behavior", function() {
describe("label variables", function() { describe("label variables", function() {
describe("in the expression", function() { describe("in the expression", function() {
it("can access variable defined by labeled expression", function() { it("can access variable defined by labeled expression", function() {
var parser = peg.buildParser('start = a:"a" { return a; }', options); var parser = peg.generate('start = a:"a" { return a; }', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
it("can access variables defined by labeled sequence elements", function() { it("can access variables defined by labeled sequence elements", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = a:"a" b:"b" c:"c" { return [a, b, c]; }', 'start = a:"a" b:"b" c:"c" { return [a, b, c]; }',
options options
); );
@ -1100,7 +1100,7 @@ describe("generated parser behavior", function() {
parser, i; parser, i;
for (i = 0; i < testcases.length; i++) { for (i = 0; i < testcases.length; i++) {
parser = peg.buildParser(testcases[i].grammar, options); parser = peg.generate(testcases[i].grammar, options);
expect(parser).toFailToParse(testcases[i].input); expect(parser).toFailToParse(testcases[i].input);
} }
}); });
@ -1108,7 +1108,7 @@ describe("generated parser behavior", function() {
describe("in outer sequence", function() { describe("in outer sequence", function() {
it("can access variables defined by preceding labeled elements", function() { it("can access variables defined by preceding labeled elements", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = a:"a" ("b" { return a; })', 'start = a:"a" ("b" { return a; })',
options options
); );
@ -1117,7 +1117,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variable defined by labeled action element", function() { it("cannot access variable defined by labeled action element", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = "a" b:("b" { return b; }) c:"c"', 'start = "a" b:("b" { return b; }) c:"c"',
options options
); );
@ -1126,7 +1126,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variables defined by following labeled elements", function() { it("cannot access variables defined by following labeled elements", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = ("a" { return b; }) b:"b"', 'start = ("a" { return b; }) b:"b"',
options options
); );
@ -1138,7 +1138,7 @@ describe("generated parser behavior", function() {
describe("initializer variables & functions", function() { describe("initializer variables & functions", function() {
it("can access variables defined in the initializer", function() { it("can access variables defined in the initializer", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var v = 42 }', '{ var v = 42 }',
'start = "a" { return v; }' 'start = "a" { return v; }'
].join("\n"), options); ].join("\n"), options);
@ -1147,7 +1147,7 @@ describe("generated parser behavior", function() {
}); });
it("can access functions defined in the initializer", function() { it("can access functions defined in the initializer", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ function f() { return 42; } }', '{ function f() { return 42; } }',
'start = "a" { return f(); }' 'start = "a" { return f(); }'
].join("\n"), options); ].join("\n"), options);
@ -1158,7 +1158,7 @@ describe("generated parser behavior", function() {
describe("available variables & functions", function() { describe("available variables & functions", function() {
it("|parser| contains the parser object", function() { it("|parser| contains the parser object", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = "a" { return parser; }', 'start = "a" { return parser; }',
options options
); );
@ -1167,7 +1167,7 @@ describe("generated parser behavior", function() {
}); });
it("|options| contains options", function() { it("|options| contains options", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = "a" { return options; }', 'start = "a" { return options; }',
options options
); );
@ -1176,7 +1176,7 @@ describe("generated parser behavior", function() {
}); });
it("|text| returns text matched by the expression", function() { it("|text| returns text matched by the expression", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = "a" { return text(); }', 'start = "a" { return text(); }',
options options
); );
@ -1185,7 +1185,7 @@ describe("generated parser behavior", function() {
}); });
it("|location| returns location info of the expression", function() { it("|location| returns location info of the expression", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'{ var result; }', '{ var result; }',
'start = line (nl+ line)* { return result; }', 'start = line (nl+ line)* { return result; }',
'line = thing (" "+ thing)*', 'line = thing (" "+ thing)*',
@ -1213,7 +1213,7 @@ describe("generated parser behavior", function() {
describe("|expected|", function() { describe("|expected|", function() {
it("terminates parsing and throws an exception", function() { it("terminates parsing and throws an exception", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = "a" { expected("a"); }', 'start = "a" { expected("a"); }',
options options
); );
@ -1229,7 +1229,7 @@ describe("generated parser behavior", function() {
}); });
it("allows to set custom location info", function() { it("allows to set custom location info", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'start = "a" {', 'start = "a" {',
' expected("a", {', ' expected("a", {',
' start: { offset: 1, line: 1, column: 2 },', ' start: { offset: 1, line: 1, column: 2 },',
@ -1251,7 +1251,7 @@ describe("generated parser behavior", function() {
describe("|error|", function() { describe("|error|", function() {
it("terminates parsing and throws an exception", function() { it("terminates parsing and throws an exception", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = "a" { error("a"); }', 'start = "a" { error("a"); }',
options options
); );
@ -1267,7 +1267,7 @@ describe("generated parser behavior", function() {
}); });
it("allows to set custom location info", function() { it("allows to set custom location info", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'start = "a" {', 'start = "a" {',
' error("a", {', ' error("a", {',
' start: { offset: 1, line: 1, column: 2 },', ' start: { offset: 1, line: 1, column: 2 },',
@ -1291,13 +1291,13 @@ describe("generated parser behavior", function() {
describe("when the expression doesn't match", function() { describe("when the expression doesn't match", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser('start = "a" { return 42; }', options); var parser = peg.generate('start = "a" { return 42; }', options);
expect(parser).toFailToParse("b"); expect(parser).toFailToParse("b");
}); });
it("doesn't execute the code", function() { it("doesn't execute the code", function() {
var parser = peg.buildParser( var parser = peg.generate(
'start = "a" { throw "Boom!"; } / "b"', 'start = "a" { throw "Boom!"; } / "b"',
options options
); );
@ -1310,7 +1310,7 @@ describe("generated parser behavior", function() {
describe("choice", function() { describe("choice", function() {
describe("when any expression matches", function() { describe("when any expression matches", function() {
it("returns its match result", function() { it("returns its match result", function() {
var parser = peg.buildParser('start = "a" / "b" / "c"', options); var parser = peg.generate('start = "a" / "b" / "c"', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
expect(parser).toParse("b", "b"); expect(parser).toParse("b", "b");
@ -1320,7 +1320,7 @@ describe("generated parser behavior", function() {
describe("when all expressions don't match", function() { describe("when all expressions don't match", function() {
it("reports match failure", function() { it("reports match failure", function() {
var parser = peg.buildParser('start = "a" / "b" / "c"', options); var parser = peg.generate('start = "a" / "b" / "c"', options);
expect(parser).toFailToParse("d"); expect(parser).toFailToParse("d");
}); });
@ -1330,7 +1330,7 @@ describe("generated parser behavior", function() {
describe("error reporting", function() { describe("error reporting", function() {
describe("behavior", function() { describe("behavior", function() {
it("reports only the rightmost error", function() { it("reports only the rightmost error", function() {
var parser = peg.buildParser('start = "a" "b" / "a" "c" "d"', options); var parser = peg.generate('start = "a" "b" / "a" "c" "d"', options);
expect(parser).toFailToParse("ace", { expect(parser).toFailToParse("ace", {
expected: [{ type: "literal", value: "d", description: '"d"' }] expected: [{ type: "literal", value: "d", description: '"d"' }]
@ -1340,7 +1340,7 @@ describe("generated parser behavior", function() {
describe("expectations reporting", function() { describe("expectations reporting", function() {
it("reports expectations correctly with no alternative", function() { it("reports expectations correctly with no alternative", function() {
var parser = peg.buildParser('start = "a"', options); var parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("ab", { expect(parser).toFailToParse("ab", {
expected: [{ type: "end", description: "end of input" }] expected: [{ type: "end", description: "end of input" }]
@ -1348,7 +1348,7 @@ describe("generated parser behavior", function() {
}); });
it("reports expectations correctly with one alternative", function() { it("reports expectations correctly with one alternative", function() {
var parser = peg.buildParser('start = "a"', options); var parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "literal", value: "a", description: '"a"' }] expected: [{ type: "literal", value: "a", description: '"a"' }]
@ -1356,7 +1356,7 @@ describe("generated parser behavior", function() {
}); });
it("reports expectations correctly with multiple alternatives", function() { it("reports expectations correctly with multiple alternatives", function() {
var parser = peg.buildParser('start = "a" / "b" / "c"', options); var parser = peg.generate('start = "a" / "b" / "c"', options);
expect(parser).toFailToParse("d", { expect(parser).toFailToParse("d", {
expected: [ expected: [
@ -1375,7 +1375,7 @@ describe("generated parser behavior", function() {
* *
* See https://github.com/pegjs/pegjs/pull/146. * See https://github.com/pegjs/pegjs/pull/146.
*/ */
var parser = peg.buildParser('start = "a" / "a" / "a"', options); var parser = peg.generate('start = "a" / "a" / "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "literal", value: "a", description: '"a"' }] expected: [{ type: "literal", value: "a", description: '"a"' }]
@ -1383,7 +1383,7 @@ describe("generated parser behavior", function() {
}); });
it("sorts expectations", function() { it("sorts expectations", function() {
var parser = peg.buildParser('start = "c" / "b" / "a"', options); var parser = peg.generate('start = "c" / "b" / "a"', options);
expect(parser).toFailToParse("d", { expect(parser).toFailToParse("d", {
expected: [ expected: [
@ -1397,7 +1397,7 @@ describe("generated parser behavior", function() {
describe("message building", function() { describe("message building", function() {
it("builds message correctly with no alternative", function() { it("builds message correctly with no alternative", function() {
var parser = peg.buildParser('start = "a"', options); var parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("ab", { expect(parser).toFailToParse("ab", {
message: 'Expected end of input.' message: 'Expected end of input.'
@ -1405,7 +1405,7 @@ describe("generated parser behavior", function() {
}); });
it("builds message correctly with one alternative", function() { it("builds message correctly with one alternative", function() {
var parser = peg.buildParser('start = "a"', options); var parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
message: 'Expected "a".' message: 'Expected "a".'
@ -1413,7 +1413,7 @@ describe("generated parser behavior", function() {
}); });
it("builds message correctly with multiple alternatives", function() { it("builds message correctly with multiple alternatives", function() {
var parser = peg.buildParser('start = "a" / "b" / "c"', options); var parser = peg.generate('start = "a" / "b" / "c"', options);
expect(parser).toFailToParse("d", { expect(parser).toFailToParse("d", {
message: 'Expected "a", "b" or "c".' message: 'Expected "a", "b" or "c".'
@ -1423,7 +1423,7 @@ describe("generated parser behavior", function() {
describe("position reporting", function() { describe("position reporting", function() {
it("reports position correctly with no trailing input", function() { it("reports position correctly with no trailing input", function() {
var parser = peg.buildParser('start = "a"', options); var parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
location: { location: {
@ -1434,7 +1434,7 @@ describe("generated parser behavior", function() {
}); });
it("reports position correctly with trailing input", function() { it("reports position correctly with trailing input", function() {
var parser = peg.buildParser('start = "a"', options); var parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("aa", { expect(parser).toFailToParse("aa", {
location: { location: {
@ -1445,7 +1445,7 @@ describe("generated parser behavior", function() {
}); });
it("reports position correctly in complex cases", function() { it("reports position correctly in complex cases", function() {
var parser = peg.buildParser([ var parser = peg.generate([
'start = line (nl+ line)*', 'start = line (nl+ line)*',
'line = digit (" "+ digit)*', 'line = digit (" "+ digit)*',
'digit = [0-9]', 'digit = [0-9]',
@ -1488,7 +1488,7 @@ describe("generated parser behavior", function() {
* Sum Product (('+' / '-') Product)* * Sum Product (('+' / '-') Product)*
* Expr Sum * Expr Sum
*/ */
var parser = peg.buildParser([ var parser = peg.generate([
'Expr = Sum', 'Expr = Sum',
'Sum = first:Product rest:(("+" / "-") Product)* {', 'Sum = first:Product rest:(("+" / "-") Product)* {',
' var result = first, i;', ' var result = first, i;',
@ -1544,7 +1544,7 @@ describe("generated parser behavior", function() {
* A a A? b * A a A? b
* B b B? c * B b B? c
*/ */
var parser = peg.buildParser([ var parser = peg.generate([
'S = &(A "c") a:"a"+ B:B !("a" / "b" / "c") { return a.join("") + B; }', 'S = &(A "c") a:"a"+ B:B !("a" / "b" / "c") { return a.join("") + B; }',
'A = a:"a" A:A? b:"b" { return [a, A, b].join(""); }', 'A = a:"a" A:A? b:"b" { return [a, A, b].join(""); }',
'B = b:"b" B:B? c:"c" { return [b, B, c].join(""); }' 'B = b:"b" B:B? c:"c" { return [b, B, c].join(""); }'
@ -1568,7 +1568,7 @@ describe("generated parser behavior", function() {
* N C / (!Begin !End Z) * N C / (!Begin !End Z)
* Z any single character * Z any single character
*/ */
var parser = peg.buildParser([ var parser = peg.generate([
'C = begin:Begin ns:N* end:End { return begin + ns.join("") + end; }', 'C = begin:Begin ns:N* end:End { return begin + ns.join("") + end; }',
'N = C', 'N = C',
' / !Begin !End z:Z { return z; }', ' / !Begin !End z:Z { return z; }',

Loading…
Cancel
Save