Replace some functions with arrow functions

Because arrow functions work rather differently than normal functions (a
bad design mistake if you ask me), I decided to be conservative with the
conversion.

I converted:

  * event handlers
  * callbacks
  * arguments to Array.prototype.map & co.
  * small standalone lambda functions

I didn't convert:

  * functions assigned to object literal properties (the new shorthand
    syntax would be better here)
  * functions passed to "describe", "it", etc. in specs (because Jasmine
    relies on dynamic "this")

See #442.
redux
David Majda 8 years ago
parent d00e9526c3
commit 6fa8ad63f9

@ -3,7 +3,7 @@
let benchmarks = require("./benchmarks.js"),
Runner = require("./runner.js");
$("#run").click(function() {
$("#run").click(() => {
"use strict";
/* Results Table Manipulation */
@ -135,7 +135,7 @@ $("#run").click(function() {
});
$(document).ready(function() {
$(document).ready(() => {
"use strict";
$("#run").focus();

@ -24,7 +24,7 @@ let Runner = {
* We can't use |arguments.callee| here because |this| would get
* messed-up in that case.
*/
setTimeout(function() { Q.run(); }, 0);
setTimeout(() => { Q.run(); }, 0);
}
}
};
@ -107,9 +107,9 @@ let Runner = {
/* Main */
Q.add(initialize);
benchmarks.forEach(function(benchmark) {
benchmarks.forEach(benchmark => {
Q.add(benchmarkInitializer(benchmark));
benchmark.tests.forEach(function(test) {
benchmark.tests.forEach(test => {
Q.add(testRunner(benchmark, test));
});
Q.add(benchmarkFinalizer(benchmark));

@ -21,7 +21,7 @@ app.use(logger("dev"));
app.use(express.static(__dirname));
app.use("/examples", express.static(__dirname + "/../examples"));
app.get("/bundle.js", function(req, res) {
app.get("/bundle.js", (req, res) => {
let files = glob.sync(__dirname + "/**/*.js", {
ignore: __dirname + "/vendor/**/*"
});
@ -32,7 +32,7 @@ app.get("/bundle.js", function(req, res) {
.pipe(res);
});
app.listen(8000, function() {
app.listen(8000, () => {
console.log("Benchmark server running at http://localhost:8000...");
});

@ -100,8 +100,8 @@ function nextArg() {
function readStream(inputStream, callback) {
let input = "";
inputStream.on("data", function(data) { input += data; });
inputStream.on("end", function() { callback(input); });
inputStream.on("data", (data) => { input += data; });
inputStream.on("end", () => { callback(input); });
}
/* Main */
@ -296,7 +296,7 @@ if (outputFile === null) {
if (inputFile === "-") {
process.stdin.resume();
inputStream = process.stdin;
inputStream.on("error", function() {
inputStream.on("error", () => {
abort("Can't read from file \"" + inputFile + "\".");
});
} else {
@ -307,12 +307,12 @@ if (outputFile === "-") {
outputStream = process.stdout;
} else {
outputStream = fs.createWriteStream(outputFile);
outputStream.on("error", function() {
outputStream.on("error", () => {
abort("Can't write to file \"" + outputFile + "\".");
});
}
readStream(inputStream, function(input) {
readStream(inputStream, (input) => {
let source;
try {

@ -6,11 +6,11 @@ let arrays = require("../utils/arrays"),
/* AST utilities. */
let asts = {
findRule: function(ast, name) {
return arrays.find(ast.rules, function(r) { return r.name === name; });
return arrays.find(ast.rules, r => r.name === name);
},
indexOfRule: function(ast, name) {
return arrays.indexOf(ast.rules, function(r) { return r.name === name; });
return arrays.indexOf(ast.rules, r => r.name === name);
},
alwaysConsumesOnSuccess: function(ast, node) {

@ -56,7 +56,7 @@ let compiler = {
for (let stage in passes) {
if (passes.hasOwnProperty(stage)) {
passes[stage].forEach(function(p) { p(ast, options); });
passes[stage].forEach(p => { p(ast, options); });
}
}

@ -23,10 +23,10 @@ let js = {
.replace(/\v/g, '\\v') // vertical tab
.replace(/\f/g, '\\f') // form feed
.replace(/\r/g, '\\r') // carriage return
.replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); })
.replace(/[\x10-\x1F\x7F-\xFF]/g, function(ch) { return '\\x' + hex(ch); })
.replace(/[\u0100-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); })
.replace(/[\u1000-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); });
.replace(/[\x00-\x0F]/g, ch => '\\x0' + hex(ch))
.replace(/[\x10-\x1F\x7F-\xFF]/g, ch => '\\x' + hex(ch))
.replace(/[\u0100-\u0FFF]/g, ch => '\\u0' + hex(ch))
.replace(/[\u1000-\uFFFF]/g, ch => '\\u' + hex(ch));
},
regexpClassEscape: function(s) {
@ -48,10 +48,10 @@ let js = {
.replace(/\v/g, '\\v') // vertical tab
.replace(/\f/g, '\\f') // form feed
.replace(/\r/g, '\\r') // carriage return
.replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); })
.replace(/[\x10-\x1F\x7F-\xFF]/g, function(ch) { return '\\x' + hex(ch); })
.replace(/[\u0100-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); })
.replace(/[\u1000-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); });
.replace(/[\x00-\x0F]/g, ch => '\\x0' + hex(ch))
.replace(/[\x10-\x1F\x7F-\xFF]/g, ch => '\\x' + hex(ch))
.replace(/[\u0100-\u0FFF]/g, ch => '\\u0' + hex(ch))
.replace(/[\u1000-\uFFFF]/g, ch => '\\u' + hex(ch));
}
};

@ -222,7 +222,7 @@ function generateBytecode(ast) {
}
function buildCall(functionIndex, delta, env, sp) {
let params = objects.values(env).map(function(p) { return sp - p; });
let params = objects.values(env).map(p => sp - p);
return [op.CALL, functionIndex, delta, params.length].concat(params);
}
@ -567,20 +567,20 @@ function generateBytecode(ast) {
"class": function(node) {
let regexp = '/^['
+ (node.inverted ? '^' : '')
+ node.parts.map(function(part) {
return Array.isArray(part)
+ node.parts.map(part =>
Array.isArray(part)
? js.regexpClassEscape(part[0])
+ '-'
+ js.regexpClassEscape(part[1])
: js.regexpClassEscape(part);
}).join('')
: js.regexpClassEscape(part)
).join('')
+ ']/' + (node.ignoreCase ? 'i' : ''),
parts = '['
+ node.parts.map(function(part) {
return Array.isArray(part)
+ node.parts.map(part =>
Array.isArray(part)
? '["' + js.stringEscape(part[0]) + '", "' + js.stringEscape(part[1]) + '"]'
: '"' + js.stringEscape(part) + '"';
}).join(', ')
: '"' + js.stringEscape(part) + '"'
).join(', ')
+ ']',
regexpIndex = addConst(regexp),
expectedIndex = addConst(

@ -21,19 +21,17 @@ function generateJS(ast, options) {
'],',
'',
'peg$bytecode = [',
indent2(ast.rules.map(function(rule) {
return 'peg$decode("'
+ js.stringEscape(rule.bytecode.map(
function(b) { return String.fromCharCode(b + 32); }
).join(''))
+ '")';
}).join(',\n')),
indent2(ast.rules.map(rule =>
'peg$decode("'
+ js.stringEscape(rule.bytecode.map(
b => String.fromCharCode(b + 32)
).join(''))
+ '")'
).join(',\n')),
'],'
].join('\n');
} else {
return ast.consts.map(
function(c, i) { return 'peg$c' + i + ' = ' + c + ','; }
).join('\n');
return ast.consts.map((c, i) => 'peg$c' + i + ' = ' + c + ',').join('\n');
}
}
@ -513,7 +511,7 @@ function generateJS(ast, options) {
let value = c(bc[ip + 1]) + '('
+ bc.slice(ip + baseLength, ip + baseLength + paramsLength).map(
function(p) { return stack.index(p); }
p => stack.index(p)
).join(', ')
+ ')';
stack.pop(bc[ip + 2]);
@ -937,7 +935,7 @@ function generateJS(ast, options) {
if (options.optimize === "size") {
let startRuleIndices = '{ '
+ options.allowedStartRules.map(
function(r) { return r + ': ' + asts.indexOfRule(ast, r); }
r => r + ': ' + asts.indexOfRule(ast, r)
).join(', ')
+ ' }';
let startRuleIndex = asts.indexOfRule(ast, options.allowedStartRules[0]);
@ -949,7 +947,7 @@ function generateJS(ast, options) {
} else {
let startRuleFunctions = '{ '
+ options.allowedStartRules.map(
function(r) { return r + ': peg$parse' + r; }
r => r + ': peg$parse' + r
).join(', ')
+ ' }';
let startRuleFunction = 'peg$parse' + options.allowedStartRules[0];
@ -986,7 +984,7 @@ function generateJS(ast, options) {
if (options.optimize === "size") {
let ruleNames = '['
+ ast.rules.map(
function(r) { return '"' + js.stringEscape(r.name) + '"'; }
r => '"' + js.stringEscape(r.name) + '"'
).join(', ')
+ ']';
@ -1156,7 +1154,7 @@ function generateJS(ast, options) {
parts.push(indent2(generateInterpreter()));
parts.push('');
} else {
ast.rules.forEach(function(rule) {
ast.rules.forEach(rule => {
parts.push(indent2(generateRuleFunction(rule)));
parts.push('');
});
@ -1242,12 +1240,10 @@ function generateJS(ast, options) {
let parts = [],
dependencyVars = Object.keys(options.dependencies),
requires = dependencyVars.map(
function(variable) {
return variable
+ ' = require("'
+ js.stringEscape(options.dependencies[variable])
+ '")';
}
variable => variable
+ ' = require("'
+ js.stringEscape(options.dependencies[variable])
+ '")'
);
parts.push([
@ -1277,7 +1273,7 @@ function generateJS(ast, options) {
dependencyVars = Object.keys(options.dependencies),
dependencies = '['
+ dependencyIds.map(
function(id) { return '"' + js.stringEscape(id) + '"'; }
id => '"' + js.stringEscape(id) + '"'
).join(', ')
+ ']',
params = dependencyVars.join(', ');
@ -1315,11 +1311,11 @@ function generateJS(ast, options) {
dependencyVars = Object.keys(options.dependencies),
dependencies = '['
+ dependencyIds.map(
function(id) { return '"' + js.stringEscape(id) + '"'; }
id => '"' + js.stringEscape(id) + '"'
).join(', ')
+ ']',
requires = dependencyIds.map(
function(id) { return 'require("' + js.stringEscape(id) + '")'; }
id => 'require("' + js.stringEscape(id) + '")'
).join(', '),
params = dependencyVars.join(', ');

@ -25,7 +25,7 @@ function removeProxyRules(ast, options) {
let indices = [];
ast.rules.forEach(function(rule, i) {
ast.rules.forEach((rule, i) => {
if (isProxyRule(rule)) {
replaceRuleRefs(ast, rule.name, rule.expression.name);
if (!arrays.contains(options.allowedStartRules, rule.name)) {
@ -36,7 +36,7 @@ function removeProxyRules(ast, options) {
indices.reverse();
indices.forEach(function(i) { ast.rules.splice(i, 1); });
indices.forEach(i => { ast.rules.splice(i, 1); });
}
module.exports = removeProxyRules;

@ -16,7 +16,7 @@ function reportDuplicateLabels(ast) {
},
choice: function(node, env) {
node.alternatives.forEach(function(alternative) {
node.alternatives.forEach(alternative => {
check(alternative, objects.clone(env));
});
},

@ -28,7 +28,7 @@ function reportInfiniteRecursion(ast) {
},
sequence: function(node) {
node.elements.every(function(element) {
node.elements.every(element => {
check(element);
return !asts.alwaysConsumesOnSuccess(ast, element);

@ -21,7 +21,7 @@ let visitor = {
return function(node) {
let extraArgs = Array.prototype.slice.call(arguments, 1);
node[property].forEach(function(child) {
node[property].forEach(child => {
visit.apply(null, [child].concat(extraArgs));
});
};
@ -35,7 +35,7 @@ let visitor = {
visit.apply(null, [node.initializer].concat(extraArgs));
}
node.rules.forEach(function(rule) {
node.rules.forEach(rule => {
visit.apply(null, [rule].concat(extraArgs));
});
},

@ -330,7 +330,7 @@ function peg$parse(input, options) {
peg$c98 = function(inverted, parts, ignoreCase) {
return {
type: "class",
parts: parts.filter(function(part) { return part !== ""; }),
parts: parts.filter(part => part !== ""),
inverted: inverted !== null,
ignoreCase: ignoreCase !== null,
location: location()
@ -4982,7 +4982,7 @@ function peg$parse(input, options) {
}
function extractList(list, index) {
return list.map(function(element) { return element[index]; });
return list.map(element => element[index]);
}
function buildList(head, tail, index) {

@ -44,7 +44,7 @@ let peg = {
passes: convertPasses(peg.compiler.passes)
};
plugins.forEach(function(p) { p.use(config, options); });
plugins.forEach(p => { p.use(config, options); });
return peg.compiler.compile(
config.parser.parse(grammar),

@ -16,7 +16,7 @@ describe("generated parser API", function() {
it("throws an exception on syntax error", function() {
let parser = peg.generate('start = "a"');
expect(function() { parser.parse("b"); }).toThrow();
expect(() => { parser.parse("b"); }).toThrow();
});
describe("start rule", function() {
@ -41,9 +41,7 @@ describe("generated parser API", function() {
describe("when |startRule| is set to a disallowed start rule", function() {
it("throws an exception", function() {
expect(
function() { parser.parse("x", { startRule: "a" }); }
).toThrow();
expect(() => { parser.parse("x", { startRule: "a" }); }).toThrow();
});
});
});

@ -12,11 +12,11 @@ describe("PEG.js API", function() {
});
it("throws an exception on syntax error", function() {
expect(function() { peg.generate('start = @'); }).toThrow();
expect(() => { peg.generate('start = @'); }).toThrow();
});
it("throws an exception on semantic error", function() {
expect(function() { peg.generate('start = undefined'); }).toThrow();
expect(() => { peg.generate('start = undefined'); }).toThrow();
});
describe("allowed start rules", function() {
@ -37,12 +37,8 @@ describe("PEG.js API", function() {
let parser = peg.generate(grammar, { optimize: "speed" });
expect(parser.parse("x", { startRule: "a" })).toBe("x");
expect(
function() { parser.parse("x", { startRule: "b" }); }
).toThrow();
expect(
function() { parser.parse("x", { startRule: "c" }); }
).toThrow();
expect(() => { parser.parse("x", { startRule: "b" }); }).toThrow();
expect(() => { parser.parse("x", { startRule: "c" }); }).toThrow();
});
});
@ -53,9 +49,7 @@ describe("PEG.js API", function() {
allowedStartRules: ["b", "c"]
});
expect(
function() { parser.parse("x", { startRule: "a" }); }
).toThrow();
expect(() => { parser.parse("x", { startRule: "a" }); }).toThrow();
expect(parser.parse("x", { startRule: "b" })).toBe("x");
expect(parser.parse("x", { startRule: "c" })).toBe("x");
});
@ -68,12 +62,8 @@ describe("PEG.js API", function() {
let parser = peg.generate(grammar, { optimize: "size" });
expect(parser.parse("x", { startRule: "a" })).toBe("x");
expect(
function() { parser.parse("x", { startRule: "b" }); }
).toThrow();
expect(
function() { parser.parse("x", { startRule: "c" }); }
).toThrow();
expect(() => { parser.parse("x", { startRule: "b" }); }).toThrow();
expect(() => { parser.parse("x", { startRule: "c" }); }).toThrow();
});
});
@ -84,9 +74,7 @@ describe("PEG.js API", function() {
allowedStartRules: ["b", "c"]
});
expect(
function() { parser.parse("x", { startRule: "a" }); }
).toThrow();
expect(() => { parser.parse("x", { startRule: "a" }); }).toThrow();
expect(parser.parse("x", { startRule: "b" })).toBe("x");
expect(parser.parse("x", { startRule: "c" })).toBe("x");
});

@ -6,31 +6,28 @@ describe("plugin API", function() {
beforeEach(function() {
this.addMatchers({
toBeObject: function() {
this.message = function() {
return "Expected " + jasmine.pp(this.actual) + " "
+ (this.isNot ? "not " : "")
+ "to be an object.";
};
this.message = () =>
"Expected " + jasmine.pp(this.actual) + " "
+ (this.isNot ? "not " : "")
+ "to be an object.";
return this.actual !== null && typeof this.actual === "object";
},
toBeArray: function() {
this.message = function() {
return "Expected " + jasmine.pp(this.actual) + " "
+ (this.isNot ? "not " : "")
+ "to be an array.";
};
this.message = () =>
"Expected " + jasmine.pp(this.actual) + " "
+ (this.isNot ? "not " : "")
+ "to be an array.";
return Object.prototype.toString.apply(this.actual) === "[object Array]";
},
toBeFunction: function() {
this.message = function() {
return "Expected " + jasmine.pp(this.actual) + " "
+ (this.isNot ? "not " : "")
+ "to be a function.";
};
this.message = () =>
"Expected " + jasmine.pp(this.actual) + " "
+ (this.isNot ? "not " : "")
+ "to be a function.";
return typeof this.actual === "function";
}
@ -64,17 +61,17 @@ describe("plugin API", function() {
expect(config.passes).toBeObject();
expect(config.passes.check).toBeArray();
config.passes.check.forEach(function(pass) {
config.passes.check.forEach(pass => {
expect(pass).toBeFunction();
});
expect(config.passes.transform).toBeArray();
config.passes.transform.forEach(function(pass) {
config.passes.transform.forEach(pass => {
expect(pass).toBeFunction();
});
expect(config.passes.generate).toBeArray();
config.passes.generate.forEach(function(pass) {
config.passes.generate.forEach(pass => {
expect(pass).toBeFunction();
});
}
@ -123,7 +120,7 @@ describe("plugin API", function() {
it("can change compiler passes", function() {
let plugin = {
use: function(config) {
let pass = function(ast) {
let pass = ast => {
ast.code = '({ parse: function() { return 42; } })';
};
@ -151,7 +148,7 @@ describe("plugin API", function() {
plugins: [plugin]
});
expect(function() { parser.parse("x", { startRule: "a" }); }).toThrow();
expect(() => { parser.parse("x", { startRule: "a" }); }).toThrow();
expect(parser.parse("x", { startRule: "b" })).toBe("x");
expect(parser.parse("x", { startRule: "c" })).toBe("x");
});

@ -30,7 +30,7 @@ describe("generated parser behavior", function() {
{ cache: true, optimize: "size", trace: true }
];
optionsVariants.forEach(function(variant) {
optionsVariants.forEach(variant => {
describe(
"with options " + jasmine.pp(variant),
function() { block(clone(variant)); }
@ -48,25 +48,23 @@ describe("generated parser behavior", function() {
try {
result = this.actual.parse(input, options);
} catch (e) {
this.message = function() {
return "Expected " + jasmine.pp(input) + " "
+ "with options " + jasmine.pp(options) + " "
+ "to parse" + (expected !== undefined ? " as " + jasmine.pp(expected) : "") + ", "
+ "but it failed to parse with message "
+ jasmine.pp(e.message) + ".";
};
this.message = () =>
"Expected " + jasmine.pp(input) + " "
+ "with options " + jasmine.pp(options) + " "
+ "to parse" + (expected !== undefined ? " as " + jasmine.pp(expected) : "") + ", "
+ "but it failed to parse with message "
+ jasmine.pp(e.message) + ".";
return false;
}
if (expected !== undefined) {
this.message = function() {
return "Expected " + jasmine.pp(input) + " "
+ "with options " + jasmine.pp(options) + " "
+ (this.isNot ? "not " : "")
+ "to parse as " + jasmine.pp(expected) + ", "
+ "but it parsed as " + jasmine.pp(result) + ".";
};
this.message = () =>
"Expected " + jasmine.pp(input) + " "
+ "with options " + jasmine.pp(options) + " "
+ (this.isNot ? "not " : "")
+ "to parse as " + jasmine.pp(expected) + ", "
+ "but it parsed as " + jasmine.pp(result) + ".";
return this.env.equals_(result, expected);
} else {
@ -83,26 +81,24 @@ describe("generated parser behavior", function() {
result = this.actual.parse(input, options);
} catch (e) {
if (this.isNot) {
this.message = function() {
return "Expected " + jasmine.pp(input)
+ "with options " + jasmine.pp(options) + " "
+ "to parse, "
+ "but it failed with message "
+ jasmine.pp(e.message) + ".";
};
this.message = () =>
"Expected " + jasmine.pp(input)
+ "with options " + jasmine.pp(options) + " "
+ "to parse, "
+ "but it failed with message "
+ jasmine.pp(e.message) + ".";
} else {
if (details) {
for (let key in details) {
if (details.hasOwnProperty(key)) {
if (!this.env.equals_(e[key], details[key])) {
this.message = function() {
return "Expected " + jasmine.pp(input) + " "
+ "with options " + jasmine.pp(options) + " "
+ "to fail to parse"
+ (details ? " with details " + jasmine.pp(details) : "") + ", "
+ "but " + jasmine.pp(key) + " "
+ "is " + jasmine.pp(e[key]) + ".";
};
this.message = () =>
"Expected " + jasmine.pp(input) + " "
+ "with options " + jasmine.pp(options) + " "
+ "to fail to parse"
+ (details ? " with details " + jasmine.pp(details) : "") + ", "
+ "but " + jasmine.pp(key) + " "
+ "is " + jasmine.pp(e[key]) + ".";
return false;
}
@ -114,13 +110,12 @@ describe("generated parser behavior", function() {
return true;
}
this.message = function() {
return "Expected " + jasmine.pp(input) + " "
+ "with options " + jasmine.pp(options) + " "
+ "to fail to parse"
+ (details ? " with details " + jasmine.pp(details) : "") + ", "
+ "but it parsed as " + jasmine.pp(result) + ".";
};
this.message = () =>
"Expected " + jasmine.pp(input) + " "
+ "with options " + jasmine.pp(options) + " "
+ "to fail to parse"
+ (details ? " with details " + jasmine.pp(details) : "") + ", "
+ "but it parsed as " + jasmine.pp(result) + ".";
return false;
}
@ -515,7 +510,7 @@ describe("generated parser behavior", function() {
],
parser;
testcases.forEach(function(testcase) {
testcases.forEach(testcase => {
parser = peg.generate(testcase.grammar, options);
expect(parser).toFailToParse(testcase.input);
});
@ -711,7 +706,7 @@ describe("generated parser behavior", function() {
],
parser;
testcases.forEach(function(testcase) {
testcases.forEach(testcase => {
parser = peg.generate(testcase.grammar, options);
expect(parser).toFailToParse(testcase.input);
});
@ -1080,7 +1075,7 @@ describe("generated parser behavior", function() {
],
parser;
testcases.forEach(function(testcase) {
testcases.forEach(testcase => {
parser = peg.generate(testcase.grammar, options);
expect(parser).toFailToParse(testcase.input);
});

@ -20,7 +20,7 @@ let app = express();
app.use(logger("dev"));
app.use(express.static(__dirname));
app.get("/bundle.js", function(req, res) {
app.get("/bundle.js", (req, res) => {
let files = glob.sync(__dirname + "/**/*.js", {
ignore: __dirname + "/vendor/**/*"
});
@ -31,7 +31,7 @@ app.get("/bundle.js", function(req, res) {
.pipe(res);
});
app.listen(8000, function() {
app.listen(8000, () => {
console.log("Spec server running at http://localhost:8000...");
});

@ -46,14 +46,13 @@ beforeEach(function() {
this.actual(ast, options);
this.message = function() {
return "Expected the pass "
+ "with options " + jasmine.pp(options) + " "
+ (this.isNot ? "not " : "")
+ "to change the AST " + jasmine.pp(ast) + " "
+ "to match " + jasmine.pp(details) + ", "
+ "but it " + (this.isNot ? "did" : "didn't") + ".";
};
this.message = () =>
"Expected the pass "
+ "with options " + jasmine.pp(options) + " "
+ (this.isNot ? "not " : "")
+ "to change the AST " + jasmine.pp(ast) + " "
+ "to match " + jasmine.pp(details) + ", "
+ "but it " + (this.isNot ? "did" : "didn't") + ".";
return matchDetails(ast, details);
},
@ -65,23 +64,21 @@ beforeEach(function() {
this.actual(ast);
} catch (e) {
if (this.isNot) {
this.message = function() {
return "Expected the pass not to report an error "
+ "for grammar " + jasmine.pp(grammar) + ", "
+ "but it did.";
};
this.message = () =>
"Expected the pass not to report an error "
+ "for grammar " + jasmine.pp(grammar) + ", "
+ "but it did.";
} else {
if (details) {
for (let key in details) {
if (details.hasOwnProperty(key)) {
if (!this.env.equals_(e[key], details[key])) {
this.message = function() {
return "Expected the pass to report an error "
+ "with details " + jasmine.pp(details) + " "
+ "for grammar " + jasmine.pp(grammar) + ", "
+ "but " + jasmine.pp(key) + " "
+ "is " + jasmine.pp(e[key]) + ".";
};
this.message = () =>
"Expected the pass to report an error "
+ "with details " + jasmine.pp(details) + " "
+ "for grammar " + jasmine.pp(grammar) + ", "
+ "but " + jasmine.pp(key) + " "
+ "is " + jasmine.pp(e[key]) + ".";
return false;
}
@ -93,12 +90,11 @@ beforeEach(function() {
return true;
}
this.message = function() {
return "Expected the pass to report an error "
+ (details ? "with details " + jasmine.pp(details) + " ": "")
+ "for grammar " + jasmine.pp(grammar) + ", "
+ "but it didn't.";
};
this.message = () =>
"Expected the pass to report an error "
+ (details ? "with details " + jasmine.pp(details) + " ": "")
+ "for grammar " + jasmine.pp(grammar) + ", "
+ "but it didn't.";
return false;
}

@ -171,24 +171,22 @@ describe("PEG.js grammar parser", function() {
try {
result = peg.parser.parse(this.actual);
} catch (e) {
this.message = function() {
return "Expected " + jasmine.pp(this.actual) + " "
+ "to parse as " + jasmine.pp(expected) + ", "
+ "but it failed to parse with message "
+ jasmine.pp(e.message) + ".";
};
this.message = () =>
"Expected " + jasmine.pp(this.actual) + " "
+ "to parse as " + jasmine.pp(expected) + ", "
+ "but it failed to parse with message "
+ jasmine.pp(e.message) + ".";
return false;
}
stripLocation(result);
this.message = function() {
return "Expected " + jasmine.pp(this.actual) + " "
+ (this.isNot ? "not " : "")
+ "to parse as " + jasmine.pp(expected) + ", "
+ "but it parsed as " + jasmine.pp(result) + ".";
};
this.message = () =>
"Expected " + jasmine.pp(this.actual) + " "
+ (this.isNot ? "not " : "")
+ "to parse as " + jasmine.pp(expected) + ", "
+ "but it parsed as " + jasmine.pp(result) + ".";
return this.env.equals_(result, expected);
},
@ -200,22 +198,20 @@ describe("PEG.js grammar parser", function() {
result = peg.parser.parse(this.actual);
} catch (e) {
if (this.isNot) {
this.message = function() {
return "Expected " + jasmine.pp(this.actual) + " to parse, "
+ "but it failed with message "
+ jasmine.pp(e.message) + ".";
};
this.message = () =>
"Expected " + jasmine.pp(this.actual) + " to parse, "
+ "but it failed with message "
+ jasmine.pp(e.message) + ".";
} else {
if (details) {
for (let key in details) {
if (details.hasOwnProperty(key)) {
if (!this.env.equals_(e[key], details[key])) {
this.message = function() {
return "Expected " + jasmine.pp(this.actual) + " to fail to parse"
+ (details ? " with details " + jasmine.pp(details) : "") + ", "
+ "but " + jasmine.pp(key) + " "
+ "is " + jasmine.pp(e[key]) + ".";
};
this.message = () =>
"Expected " + jasmine.pp(this.actual) + " to fail to parse"
+ (details ? " with details " + jasmine.pp(details) : "") + ", "
+ "but " + jasmine.pp(key) + " "
+ "is " + jasmine.pp(e[key]) + ".";
return false;
}
@ -229,11 +225,10 @@ describe("PEG.js grammar parser", function() {
stripLocation(result);
this.message = function() {
return "Expected " + jasmine.pp(this.actual) + " to fail to parse"
+ (details ? " with details " + jasmine.pp(details) : "") + ", "
+ "but it parsed as " + jasmine.pp(result) + ".";
};
this.message = () =>
"Expected " + jasmine.pp(this.actual) + " to fail to parse"
+ (details ? " with details " + jasmine.pp(details) : "") + ", "
+ "but it parsed as " + jasmine.pp(result) + ".";
return false;
}

@ -46,7 +46,7 @@
}
function extractList(list, index) {
return list.map(function(element) { return element[index]; });
return list.map(element => element[index]);
}
function buildList(head, tail, index) {
@ -364,7 +364,7 @@ CharacterClassMatcher "character class"
{
return {
type: "class",
parts: parts.filter(function(part) { return part !== ""; }),
parts: parts.filter(part => part !== ""),
inverted: inverted !== null,
ignoreCase: ignoreCase !== null,
location: location()

Loading…
Cancel
Save