Replace "var" with "let" & "const"

This is purely a mechanical change, not taking advantage of block scope
of "let" and "const". Minimizing variable scope will come in the next
commit.

In general, "var" is converted into "let" and "const" is used only for
immutable variables of permanent character (generally spelled in
ALL_CAPS). Using it for any immutable variable regardless on its
permanence would feel confusing.

Any code which is not transpiled and needs to run in ES6 environment
(examples, code in grammars embedded in specs, ...) is kept unchanged.
This is also true for code generated by PEG.js.

See #442.
redux
David Majda 8 years ago
parent 5249814fc0
commit bdf91b5941

@ -1,6 +1,6 @@
"use strict"; "use strict";
var benchmarks = [ let benchmarks = [
{ {
id: "json", id: "json",
title: "JSON", title: "JSON",

@ -1,6 +1,6 @@
/* eslint-env browser, jquery */ /* eslint-env browser, jquery */
var benchmarks = require("./benchmarks.js"), let benchmarks = require("./benchmarks.js"),
Runner = require("./runner.js"); Runner = require("./runner.js");
$("#run").click(function() { $("#run").click(function() {
@ -8,7 +8,7 @@ $("#run").click(function() {
/* Results Table Manipulation */ /* Results Table Manipulation */
var resultsTable = $("#results-table"); let resultsTable = $("#results-table");
function appendHeading(heading) { function appendHeading(heading) {
resultsTable.append( resultsTable.append(
@ -17,8 +17,8 @@ $("#run").click(function() {
} }
function appendResult(klass, title, url, inputSize, parseTime) { function appendResult(klass, title, url, inputSize, parseTime) {
var KB = 1024, const KB = 1024,
MS_IN_S = 1000; MS_IN_S = 1000;
resultsTable.append( resultsTable.append(
"<tr class='" + klass + "'>" "<tr class='" + klass + "'>"
@ -62,7 +62,7 @@ $("#run").click(function() {
* 2. To minimize random errors. * 2. To minimize random errors.
*/ */
var runCount = parseInt($("#run-count").val(), 10), let runCount = parseInt($("#run-count").val(), 10),
options = { options = {
cache: $("#cache").is(":checked"), cache: $("#cache").is(":checked"),
optimize: $("#optimize").val() optimize: $("#optimize").val()

@ -5,16 +5,16 @@
"use strict"; "use strict";
var fs = require("fs"); let fs = require("fs");
var benchmarks = require("./benchmarks.js"); let benchmarks = require("./benchmarks.js");
var Runner = require("./runner.js"); let Runner = require("./runner.js");
/* Results Table Manipulation */ /* Results Table Manipulation */
function dup(text, count) { function dup(text, count) {
var result = ""; let result = "";
for (var i = 1; i <= count; i++) { for (let i = 1; i <= count; i++) {
result += text; result += text;
} }
return result; return result;
@ -29,7 +29,7 @@ function padRight(text, length) {
} }
function center(text, length) { function center(text, length) {
var padLength = (length - text.length) / 2; let padLength = (length - text.length) / 2;
return dup(" ", Math.floor(padLength)) return dup(" ", Math.floor(padLength))
+ text + text
+ dup(" ", Math.ceil(padLength)); + dup(" ", Math.ceil(padLength));
@ -47,8 +47,8 @@ function writeHeading(heading) {
} }
function writeResult(title, inputSize, parseTime) { function writeResult(title, inputSize, parseTime) {
var KB = 1024; const KB = 1024;
var MS_IN_S = 1000; const MS_IN_S = 1000;
console.log("│ " console.log("│ "
+ padRight(title, 35) + padRight(title, 35)
@ -99,7 +99,7 @@ function abort(message) {
/* Arguments */ /* Arguments */
var args = process.argv.slice(2); // Trim "node" and the script path. let args = process.argv.slice(2); // Trim "node" and the script path.
function isOption(arg) { function isOption(arg) {
return (/^-/).test(arg); return (/^-/).test(arg);
@ -111,8 +111,8 @@ function nextArg() {
/* Main */ /* Main */
var runCount = 10; let runCount = 10;
var options = { let options = {
cache: false, cache: false,
optimize: "speed" optimize: "speed"
}; };

@ -2,14 +2,14 @@
"use strict"; "use strict";
var peg = require("../lib/peg"); let peg = require("../lib/peg");
var Runner = { let Runner = {
run: function(benchmarks, runCount, options, callbacks) { run: function(benchmarks, runCount, options, callbacks) {
/* Queue */ /* Queue */
var Q = { let Q = {
functions: [], functions: [],
add: function(f) { add: function(f) {
@ -44,7 +44,7 @@ var Runner = {
* of the |state| object. * of the |state| object.
*/ */
var state = {}; let state = {};
function initialize() { function initialize() {
callbacks.start(); callbacks.start();
@ -68,7 +68,7 @@ var Runner = {
function testRunner(benchmark, test) { function testRunner(benchmark, test) {
return function() { return function() {
var input, parseTime, averageParseTime, i, t; let input, parseTime, averageParseTime, i, t;
callbacks.testStart(benchmark, test); callbacks.testStart(benchmark, test);

@ -9,20 +9,20 @@
* served to the browser. * served to the browser.
*/ */
var express = require("express"), let express = require("express"),
logger = require("morgan"), logger = require("morgan"),
glob = require("glob"), glob = require("glob"),
browserify = require("browserify"), browserify = require("browserify"),
babelify = require("babelify"); babelify = require("babelify");
var app = express(); let app = express();
app.use(logger("dev")); app.use(logger("dev"));
app.use(express.static(__dirname)); app.use(express.static(__dirname));
app.use("/examples", express.static(__dirname + "/../examples")); app.use("/examples", express.static(__dirname + "/../examples"));
app.get("/bundle.js", function(req, res) { app.get("/bundle.js", function(req, res) {
var files = glob.sync(__dirname + "/**/*.js", { let files = glob.sync(__dirname + "/**/*.js", {
ignore: __dirname + "/vendor/**/*" ignore: __dirname + "/vendor/**/*"
}); });

@ -2,9 +2,9 @@
"use strict"; "use strict";
var fs = require("fs"); let fs = require("fs");
var path = require("path"); let path = require("path");
var peg = require("../lib/peg"); let peg = require("../lib/peg");
/* Helpers */ /* Helpers */
@ -56,7 +56,7 @@ function abort(message) {
} }
function addExtraOptions(options, json) { function addExtraOptions(options, json) {
var extraOptions; let extraOptions;
try { try {
extraOptions = JSON.parse(json); extraOptions = JSON.parse(json);
@ -69,7 +69,7 @@ function addExtraOptions(options, json) {
abort("The JSON with extra options has to represent an object."); abort("The JSON with extra options has to represent an object.");
} }
for (var key in extraOptions) { for (let key in extraOptions) {
if (extraOptions.hasOwnProperty(key)) { if (extraOptions.hasOwnProperty(key)) {
options[key] = extraOptions[key]; options[key] = extraOptions[key];
} }
@ -86,7 +86,7 @@ function trim(s) {
/* Arguments */ /* Arguments */
var args = process.argv.slice(2); // Trim "node" and the script path. let args = process.argv.slice(2); // Trim "node" and the script path.
function isOption(arg) { function isOption(arg) {
return (/^-.+/).test(arg); return (/^-.+/).test(arg);
@ -99,17 +99,17 @@ function nextArg() {
/* Files */ /* Files */
function readStream(inputStream, callback) { function readStream(inputStream, callback) {
var input = ""; let input = "";
inputStream.on("data", function(data) { input += data; }); inputStream.on("data", function(data) { input += data; });
inputStream.on("end", function() { callback(input); }); inputStream.on("end", function() { callback(input); });
} }
/* Main */ /* Main */
var inputFile = null; let inputFile = null;
var outputFile = null; let outputFile = null;
var options = { let options = {
cache: false, cache: false,
dependencies: {}, dependencies: {},
exportVar: null, exportVar: null,
@ -121,6 +121,8 @@ var options = {
}; };
while (args.length > 0 && isOption(args[0])) { while (args.length > 0 && isOption(args[0])) {
let json, id, mod;
switch (args[0]) { switch (args[0]) {
case "--allowed-start-rules": case "--allowed-start-rules":
nextArg(); nextArg();
@ -143,7 +145,7 @@ while (args.length > 0 && isOption(args[0])) {
abort("Missing parameter of the -d/--dependency option."); abort("Missing parameter of the -d/--dependency option.");
} }
if (args[0].indexOf(":") !== -1) { if (args[0].indexOf(":") !== -1) {
var parts = args[0].split(":"); let parts = args[0].split(":");
options.dependencies[parts[0]] = parts[1]; options.dependencies[parts[0]] = parts[1];
} else { } else {
options.dependencies[args[0]] = args[0]; options.dependencies[args[0]] = args[0];
@ -173,7 +175,7 @@ while (args.length > 0 && isOption(args[0])) {
abort("Missing parameter of the --extra-options-file option."); abort("Missing parameter of the --extra-options-file option.");
} }
try { try {
var json = fs.readFileSync(args[0]); json = fs.readFileSync(args[0]);
} catch(e) { } catch(e) {
abort("Can't read from file \"" + args[0] + "\"."); abort("Can't read from file \"" + args[0] + "\".");
} }
@ -223,8 +225,8 @@ while (args.length > 0 && isOption(args[0])) {
if (args.length === 0) { if (args.length === 0) {
abort("Missing parameter of the --plugin option."); abort("Missing parameter of the --plugin option.");
} }
var id = /^(\.\/|\.\.\/)/.test(args[0]) ? path.resolve(args[0]) : args[0]; id = /^(\.\/|\.\.\/)/.test(args[0]) ? path.resolve(args[0]) : args[0];
var mod; mod;
try { try {
mod = require(id); mod = require(id);
} catch (e) { } catch (e) {
@ -267,8 +269,8 @@ if (options.exportVar !== null) {
} }
} }
var inputStream; let inputStream;
var outputStream; let outputStream;
switch (args.length) { switch (args.length) {
case 0: case 0:
@ -311,7 +313,7 @@ if (outputFile === "-") {
} }
readStream(inputStream, function(input) { readStream(inputStream, function(input) {
var source; let source;
try { try {
source = peg.generate(input, options); source = peg.generate(input, options);

@ -1,10 +1,10 @@
"use strict"; "use strict";
var arrays = require("../utils/arrays"), let arrays = require("../utils/arrays"),
visitor = require("./visitor"); visitor = require("./visitor");
/* AST utilities. */ /* AST utilities. */
var asts = { let asts = {
findRule: function(ast, name) { findRule: function(ast, name) {
return arrays.find(ast.rules, function(r) { return r.name === name; }); return arrays.find(ast.rules, function(r) { return r.name === name; });
}, },
@ -21,7 +21,7 @@ var asts = {
return consumes(node.expression); return consumes(node.expression);
} }
var consumes = visitor.build({ let consumes = visitor.build({
rule: consumesExpression, rule: consumesExpression,
named: consumesExpression, named: consumesExpression,

@ -1,8 +1,8 @@
"use strict"; "use strict";
var objects = require("../utils/objects"); let objects = require("../utils/objects");
var compiler = { let compiler = {
/* /*
* AST node visitor builder. Useful mainly for plugins which manipulate the * AST node visitor builder. Useful mainly for plugins which manipulate the
* AST. * AST.
@ -42,7 +42,7 @@ var compiler = {
compile: function(ast, passes, options) { compile: function(ast, passes, options) {
options = options !== undefined ? options : {}; options = options !== undefined ? options : {};
var stage; let stage;
options = objects.clone(options); options = objects.clone(options);
objects.defaults(options, { objects.defaults(options, {

@ -3,7 +3,7 @@
function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); } function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); }
/* JavaScript code generation helpers. */ /* JavaScript code generation helpers. */
var js = { let js = {
stringEscape: function(s) { stringEscape: function(s) {
/* /*
* ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string

@ -1,7 +1,7 @@
"use strict"; "use strict";
/* Bytecode instruction opcodes. */ /* Bytecode instruction opcodes. */
var opcodes = { let opcodes = {
/* Stack Manipulation */ /* Stack Manipulation */
PUSH: 0, // PUSH c PUSH: 0, // PUSH c

@ -1,6 +1,6 @@
"use strict"; "use strict";
var arrays = require("../../utils/arrays"), let arrays = require("../../utils/arrays"),
objects = require("../../utils/objects"), objects = require("../../utils/objects"),
asts = require("../asts"), asts = require("../asts"),
visitor = require("../visitor"), visitor = require("../visitor"),
@ -191,10 +191,10 @@ var arrays = require("../../utils/arrays"),
* silentFails--; * silentFails--;
*/ */
function generateBytecode(ast) { function generateBytecode(ast) {
var consts = []; let consts = [];
function addConst(value) { function addConst(value) {
var index = arrays.indexOf(consts, value); let index = arrays.indexOf(consts, value);
return index === -1 ? consts.push(value) - 1 : index; return index === -1 ? consts.push(value) - 1 : index;
} }
@ -222,7 +222,7 @@ function generateBytecode(ast) {
} }
function buildCall(functionIndex, delta, env, sp) { function buildCall(functionIndex, delta, env, sp) {
var params = objects.values(env).map(function(p) { return sp - p; }); let params = objects.values(env).map(function(p) { return sp - p; });
return [op.CALL, functionIndex, delta, params.length].concat(params); return [op.CALL, functionIndex, delta, params.length].concat(params);
} }
@ -254,7 +254,7 @@ function generateBytecode(ast) {
} }
function buildSemanticPredicate(code, negative, context) { function buildSemanticPredicate(code, negative, context) {
var functionIndex = addFunctionConst(Object.keys(context.env), code); let functionIndex = addFunctionConst(Object.keys(context.env), code);
return buildSequence( return buildSequence(
[op.UPDATE_SAVED_POS], [op.UPDATE_SAVED_POS],
@ -280,7 +280,7 @@ function generateBytecode(ast) {
); );
} }
var generate = visitor.build({ let generate = visitor.build({
grammar: function(node) { grammar: function(node) {
node.rules.forEach(generate); node.rules.forEach(generate);
@ -296,7 +296,7 @@ function generateBytecode(ast) {
}, },
named: function(node, context) { named: function(node, context) {
var nameIndex = addConst( let nameIndex = addConst(
'peg$otherExpectation("' + js.stringEscape(node.name) + '")' 'peg$otherExpectation("' + js.stringEscape(node.name) + '")'
); );
@ -339,7 +339,7 @@ function generateBytecode(ast) {
}, },
action: function(node, context) { action: function(node, context) {
var env = objects.clone(context.env), let env = objects.clone(context.env),
emitCall = node.expression.type !== "sequence" emitCall = node.expression.type !== "sequence"
|| node.expression.elements.length === 0, || node.expression.elements.length === 0,
expressionCode = generate(node.expression, { expressionCode = generate(node.expression, {
@ -368,7 +368,7 @@ function generateBytecode(ast) {
sequence: function(node, context) { sequence: function(node, context) {
function buildElementsCode(elements, context) { function buildElementsCode(elements, context) {
var processedCount, functionIndex; let processedCount, functionIndex;
if (elements.length > 0) { if (elements.length > 0) {
processedCount = node.elements.length - elements.slice(1).length; processedCount = node.elements.length - elements.slice(1).length;
@ -427,7 +427,7 @@ function generateBytecode(ast) {
}, },
labeled: function(node, context) { labeled: function(node, context) {
var env = objects.clone(context.env); let env = objects.clone(context.env);
context.env[node.label] = context.sp + 1; context.env[node.label] = context.sp + 1;
@ -478,7 +478,7 @@ function generateBytecode(ast) {
}, },
zero_or_more: function(node, context) { zero_or_more: function(node, context) {
var expressionCode = generate(node.expression, { let expressionCode = generate(node.expression, {
sp: context.sp + 1, sp: context.sp + 1,
env: objects.clone(context.env), env: objects.clone(context.env),
action: null action: null
@ -493,7 +493,7 @@ function generateBytecode(ast) {
}, },
one_or_more: function(node, context) { one_or_more: function(node, context) {
var expressionCode = generate(node.expression, { let expressionCode = generate(node.expression, {
sp: context.sp + 1, sp: context.sp + 1,
env: objects.clone(context.env), env: objects.clone(context.env),
action: null action: null
@ -531,7 +531,7 @@ function generateBytecode(ast) {
}, },
literal: function(node) { literal: function(node) {
var stringIndex, expectedIndex; let stringIndex, expectedIndex;
if (node.value.length > 0) { if (node.value.length > 0) {
stringIndex = addConst('"' stringIndex = addConst('"'
@ -569,7 +569,7 @@ function generateBytecode(ast) {
}, },
"class": function(node) { "class": function(node) {
var regexp = '/^[' let regexp = '/^['
+ (node.inverted ? '^' : '') + (node.inverted ? '^' : '')
+ node.parts.map(function(part) { + node.parts.map(function(part) {
return Array.isArray(part) return Array.isArray(part)
@ -603,7 +603,7 @@ function generateBytecode(ast) {
}, },
any: function() { any: function() {
var expectedIndex = addConst('peg$anyExpectation()'); let expectedIndex = addConst('peg$anyExpectation()');
return buildCondition( return buildCondition(
[op.MATCH_ANY], [op.MATCH_ANY],

@ -1,6 +1,6 @@
"use strict"; "use strict";
var arrays = require("../../utils/arrays"), let arrays = require("../../utils/arrays"),
objects = require("../../utils/objects"), objects = require("../../utils/objects"),
asts = require("../asts"), asts = require("../asts"),
op = require("../opcodes"), op = require("../opcodes"),
@ -38,7 +38,7 @@ function generateJS(ast, options) {
} }
function generateRuleHeader(ruleNameCode, ruleIndexCode) { function generateRuleHeader(ruleNameCode, ruleIndexCode) {
var parts = []; let parts = [];
parts.push(''); parts.push('');
@ -94,7 +94,7 @@ function generateJS(ast, options) {
} }
function generateRuleFooter(ruleNameCode, resultCode) { function generateRuleFooter(ruleNameCode, resultCode) {
var parts = []; let parts = [];
if (options.cache) { if (options.cache) {
parts.push([ parts.push([
@ -132,10 +132,10 @@ function generateJS(ast, options) {
} }
function generateInterpreter() { function generateInterpreter() {
var parts = []; let parts = [];
function generateCondition(cond, argsLength) { function generateCondition(cond, argsLength) {
var baseLength = argsLength + 3, let baseLength = argsLength + 3,
thenLengthCode = 'bc[ip + ' + (baseLength - 2) + ']', thenLengthCode = 'bc[ip + ' + (baseLength - 2) + ']',
elseLengthCode = 'bc[ip + ' + (baseLength - 1) + ']'; elseLengthCode = 'bc[ip + ' + (baseLength - 1) + ']';
@ -156,7 +156,7 @@ function generateJS(ast, options) {
} }
function generateLoop(cond) { function generateLoop(cond) {
var baseLength = 2, let baseLength = 2,
bodyLengthCode = 'bc[ip + ' + (baseLength - 1) + ']'; bodyLengthCode = 'bc[ip + ' + (baseLength - 1) + ']';
return [ return [
@ -175,7 +175,7 @@ function generateJS(ast, options) {
} }
function generateCall() { function generateCall() {
var baseLength = 4, let baseLength = 4,
paramsLengthCode = 'bc[ip + ' + (baseLength - 1) + ']'; paramsLengthCode = 'bc[ip + ' + (baseLength - 1) + ']';
return [ return [
@ -410,17 +410,17 @@ function generateJS(ast, options) {
} }
function generateRuleFunction(rule) { function generateRuleFunction(rule) {
var parts = [], code; let parts = [], code;
function c(i) { return "peg$c" + i; } // |consts[i]| of the abstract machine function c(i) { return "peg$c" + i; } // |consts[i]| of the abstract machine
function s(i) { return "s" + i; } // |stack[i]| of the abstract machine function s(i) { return "s" + i; } // |stack[i]| of the abstract machine
var stack = { let stack = {
sp: -1, sp: -1,
maxSp: -1, maxSp: -1,
push: function(exprCode) { push: function(exprCode) {
var code = s(++this.sp) + ' = ' + exprCode + ';'; let code = s(++this.sp) + ' = ' + exprCode + ';';
if (this.sp > this.maxSp) { this.maxSp = this.sp; } if (this.sp > this.maxSp) { this.maxSp = this.sp; }
@ -428,7 +428,7 @@ function generateJS(ast, options) {
}, },
pop: function(n) { pop: function(n) {
var values; let values;
if (n === undefined) { if (n === undefined) {
return s(this.sp--); return s(this.sp--);
@ -450,13 +450,13 @@ function generateJS(ast, options) {
}; };
function compile(bc) { function compile(bc) {
var ip = 0, let ip = 0,
end = bc.length, end = bc.length,
parts = [], parts = [],
value; value;
function compileCondition(cond, argCount) { function compileCondition(cond, argCount) {
var baseLength = argCount + 3, let baseLength = argCount + 3,
thenLength = bc[ip + baseLength - 2], thenLength = bc[ip + baseLength - 2],
elseLength = bc[ip + baseLength - 1], elseLength = bc[ip + baseLength - 1],
baseSp = stack.sp, baseSp = stack.sp,
@ -490,7 +490,7 @@ function generateJS(ast, options) {
} }
function compileLoop(cond) { function compileLoop(cond) {
var baseLength = 2, let baseLength = 2,
bodyLength = bc[ip + baseLength - 1], bodyLength = bc[ip + baseLength - 1],
baseSp = stack.sp, baseSp = stack.sp,
bodyCode, bodySp; bodyCode, bodySp;
@ -510,10 +510,10 @@ function generateJS(ast, options) {
} }
function compileCall() { function compileCall() {
var baseLength = 4, let baseLength = 4,
paramsLength = bc[ip + baseLength - 1]; paramsLength = bc[ip + baseLength - 1];
var value = c(bc[ip + 1]) + '(' let value = c(bc[ip + 1]) + '('
+ bc.slice(ip + baseLength, ip + baseLength + paramsLength).map( + bc.slice(ip + baseLength, ip + baseLength + paramsLength).map(
function(p) { return stack.index(p); } function(p) { return stack.index(p); }
).join(', ') ).join(', ')
@ -745,7 +745,7 @@ function generateJS(ast, options) {
} }
function generateToplevel() { function generateToplevel() {
var parts = [], let parts = [],
startRuleIndices, startRuleIndex, startRuleIndices, startRuleIndex,
startRuleFunctions, startRuleFunction, startRuleFunctions, startRuleFunction,
ruleNames; ruleNames;
@ -1229,7 +1229,7 @@ function generateJS(ast, options) {
].join('\n'); ].join('\n');
} }
var generators = { let generators = {
bare: function() { bare: function() {
return [ return [
generateGeneratedByComment(), generateGeneratedByComment(),
@ -1244,7 +1244,7 @@ function generateJS(ast, options) {
}, },
commonjs: function() { commonjs: function() {
var parts = [], let parts = [],
dependencyVars = Object.keys(options.dependencies), dependencyVars = Object.keys(options.dependencies),
requires = dependencyVars.map( requires = dependencyVars.map(
function(variable) { function(variable) {
@ -1278,7 +1278,7 @@ function generateJS(ast, options) {
}, },
amd: function() { amd: function() {
var dependencyIds = objects.values(options.dependencies), let dependencyIds = objects.values(options.dependencies),
dependencyVars = Object.keys(options.dependencies), dependencyVars = Object.keys(options.dependencies),
dependencies = '[' dependencies = '['
+ dependencyIds.map( + dependencyIds.map(
@ -1315,7 +1315,7 @@ function generateJS(ast, options) {
}, },
umd: function() { umd: function() {
var parts = [], let parts = [],
dependencyIds = objects.values(options.dependencies), dependencyIds = objects.values(options.dependencies),
dependencyVars = Object.keys(options.dependencies), dependencyVars = Object.keys(options.dependencies),
dependencies = '[' dependencies = '['

@ -1,6 +1,6 @@
"use strict"; "use strict";
var arrays = require("../../utils/arrays"), let arrays = require("../../utils/arrays"),
visitor = require("../visitor"); visitor = require("../visitor");
/* /*
@ -12,7 +12,7 @@ function removeProxyRules(ast, options) {
} }
function replaceRuleRefs(ast, from, to) { function replaceRuleRefs(ast, from, to) {
var replace = visitor.build({ let replace = visitor.build({
rule_ref: function(node) { rule_ref: function(node) {
if (node.name === from) { if (node.name === from) {
node.name = to; node.name = to;
@ -23,7 +23,7 @@ function removeProxyRules(ast, options) {
replace(ast); replace(ast);
} }
var indices = []; let indices = [];
ast.rules.forEach(function(rule, i) { ast.rules.forEach(function(rule, i) {
if (isProxyRule(rule)) { if (isProxyRule(rule)) {

@ -1,6 +1,6 @@
"use strict"; "use strict";
var GrammarError = require("../../grammar-error"), let GrammarError = require("../../grammar-error"),
objects = require("../../utils/objects"), objects = require("../../utils/objects"),
visitor = require("../visitor"); visitor = require("../visitor");
@ -10,7 +10,7 @@ function reportDuplicateLabels(ast) {
check(node.expression, objects.clone(env)); check(node.expression, objects.clone(env));
} }
var check = visitor.build({ let check = visitor.build({
rule: function(node) { rule: function(node) {
check(node.expression, { }); check(node.expression, { });
}, },

@ -1,13 +1,13 @@
"use strict"; "use strict";
var GrammarError = require("../../grammar-error"), let GrammarError = require("../../grammar-error"),
visitor = require("../visitor"); visitor = require("../visitor");
/* Checks that each rule is defined only once. */ /* Checks that each rule is defined only once. */
function reportDuplicateRules(ast) { function reportDuplicateRules(ast) {
var rules = {}; let rules = {};
var check = visitor.build({ let check = visitor.build({
rule: function(node) { rule: function(node) {
if (rules.hasOwnProperty(node.name)) { if (rules.hasOwnProperty(node.name)) {
throw new GrammarError( throw new GrammarError(

@ -1,6 +1,6 @@
"use strict"; "use strict";
var arrays = require("../../utils/arrays"), let arrays = require("../../utils/arrays"),
GrammarError = require("../../grammar-error"), GrammarError = require("../../grammar-error"),
asts = require("../asts"), asts = require("../asts"),
visitor = require("../visitor"); visitor = require("../visitor");
@ -18,9 +18,9 @@ var arrays = require("../../utils/arrays"),
* it can lead to left recursion. * it can lead to left recursion.
*/ */
function reportInfiniteRecursion(ast) { function reportInfiniteRecursion(ast) {
var visitedRules = []; let visitedRules = [];
var check = visitor.build({ let check = visitor.build({
rule: function(node) { rule: function(node) {
visitedRules.push(node.name); visitedRules.push(node.name);
check(node.expression); check(node.expression);

@ -1,6 +1,6 @@
"use strict"; "use strict";
var GrammarError = require("../../grammar-error"), let GrammarError = require("../../grammar-error"),
asts = require("../asts"), asts = require("../asts"),
visitor = require("../visitor"); visitor = require("../visitor");
@ -9,7 +9,7 @@ var GrammarError = require("../../grammar-error"),
* grammar, which prevents infinite loops in the generated parser. * grammar, which prevents infinite loops in the generated parser.
*/ */
function reportInfiniteRepetition(ast) { function reportInfiniteRepetition(ast) {
var check = visitor.build({ let check = visitor.build({
zero_or_more: function(node) { zero_or_more: function(node) {
if (!asts.alwaysConsumesOnSuccess(ast, node.expression)) { if (!asts.alwaysConsumesOnSuccess(ast, node.expression)) {
throw new GrammarError( throw new GrammarError(

@ -1,12 +1,12 @@
"use strict"; "use strict";
var GrammarError = require("../../grammar-error"), let GrammarError = require("../../grammar-error"),
asts = require("../asts"), asts = require("../asts"),
visitor = require("../visitor"); visitor = require("../visitor");
/* Checks that all referenced rules exist. */ /* Checks that all referenced rules exist. */
function reportUndefinedRules(ast) { function reportUndefinedRules(ast) {
var check = visitor.build({ let check = visitor.build({
rule_ref: function(node) { rule_ref: function(node) {
if (!asts.findRule(ast, node.name)) { if (!asts.findRule(ast, node.name)) {
throw new GrammarError( throw new GrammarError(

@ -1,9 +1,9 @@
"use strict"; "use strict";
var objects = require("../utils/objects"); let objects = require("../utils/objects");
/* Simple AST node visitor builder. */ /* Simple AST node visitor builder. */
var visitor = { let visitor = {
build: function(functions) { build: function(functions) {
function visit(node) { function visit(node) {
return functions[node.type].apply(null, arguments); return functions[node.type].apply(null, arguments);
@ -12,14 +12,14 @@ var visitor = {
function visitNop() { } function visitNop() { }
function visitExpression(node) { function visitExpression(node) {
var extraArgs = Array.prototype.slice.call(arguments, 1); let extraArgs = Array.prototype.slice.call(arguments, 1);
visit.apply(null, [node.expression].concat(extraArgs)); visit.apply(null, [node.expression].concat(extraArgs));
} }
function visitChildren(property) { function visitChildren(property) {
return function(node) { return function(node) {
var extraArgs = Array.prototype.slice.call(arguments, 1); let extraArgs = Array.prototype.slice.call(arguments, 1);
node[property].forEach(function(child) { node[property].forEach(function(child) {
visit.apply(null, [child].concat(extraArgs)); visit.apply(null, [child].concat(extraArgs));
@ -27,9 +27,9 @@ var visitor = {
}; };
} }
var DEFAULT_FUNCTIONS = { const DEFAULT_FUNCTIONS = {
grammar: function(node) { grammar: function(node) {
var extraArgs = Array.prototype.slice.call(arguments, 1); let extraArgs = Array.prototype.slice.call(arguments, 1);
if (node.initializer) { if (node.initializer) {
visit.apply(null, [node.initializer].concat(extraArgs)); visit.apply(null, [node.initializer].concat(extraArgs));

@ -1,6 +1,6 @@
"use strict"; "use strict";
var classes = require("./utils/classes"); let classes = require("./utils/classes");
/* Thrown when the grammar contains an error. */ /* Thrown when the grammar contains an error. */
function GrammarError(message, location) { function GrammarError(message, location) {

@ -4960,19 +4960,19 @@ function peg$parse(input, options) {
} }
var OPS_TO_PREFIXED_TYPES = { const OPS_TO_PREFIXED_TYPES = {
"$": "text", "$": "text",
"&": "simple_and", "&": "simple_and",
"!": "simple_not" "!": "simple_not"
}; };
var OPS_TO_SUFFIXED_TYPES = { const OPS_TO_SUFFIXED_TYPES = {
"?": "optional", "?": "optional",
"*": "zero_or_more", "*": "zero_or_more",
"+": "one_or_more" "+": "one_or_more"
}; };
var OPS_TO_SEMANTIC_PREDICATE_TYPES = { const OPS_TO_SEMANTIC_PREDICATE_TYPES = {
"&": "semantic_and", "&": "semantic_and",
"!": "semantic_not" "!": "semantic_not"
}; };

@ -1,8 +1,8 @@
"use strict"; "use strict";
var objects = require("./utils/objects"); let objects = require("./utils/objects");
var peg = { let peg = {
/* PEG.js version (uses semantic versioning). */ /* PEG.js version (uses semantic versioning). */
VERSION: "0.10.0", VERSION: "0.10.0",
@ -25,7 +25,7 @@ var peg = {
options = options !== undefined ? options : {}; options = options !== undefined ? options : {};
function convertPasses(passes) { function convertPasses(passes) {
var converted = {}, stage; let converted = {}, stage;
for (stage in passes) { for (stage in passes) {
if (passes.hasOwnProperty(stage)) { if (passes.hasOwnProperty(stage)) {
@ -38,7 +38,7 @@ var peg = {
options = objects.clone(options); options = objects.clone(options);
var plugins = "plugins" in options ? options.plugins : [], let plugins = "plugins" in options ? options.plugins : [],
config = { config = {
parser: peg.parser, parser: peg.parser,
passes: convertPasses(peg.compiler.passes) passes: convertPasses(peg.compiler.passes)

@ -1,9 +1,9 @@
"use strict"; "use strict";
/* Array utilities. */ /* Array utilities. */
var arrays = { let arrays = {
range: function(start, stop) { range: function(start, stop) {
var length = stop - start, let length = stop - start,
result = new Array(length), result = new Array(length),
i, j; i, j;
@ -15,7 +15,7 @@ var arrays = {
}, },
find: function(array, valueOrPredicate) { find: function(array, valueOrPredicate) {
var length = array.length, i; let length = array.length, i;
if (typeof valueOrPredicate === "function") { if (typeof valueOrPredicate === "function") {
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
@ -33,7 +33,7 @@ var arrays = {
}, },
indexOf: function(array, valueOrPredicate) { indexOf: function(array, valueOrPredicate) {
var length = array.length, i; let length = array.length, i;
if (typeof valueOrPredicate === "function") { if (typeof valueOrPredicate === "function") {
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {

@ -1,7 +1,7 @@
"use strict"; "use strict";
/* Class utilities */ /* Class utilities */
var classes = { let classes = {
subclass: function(child, parent) { subclass: function(child, parent) {
function ctor() { this.constructor = child; } function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype; ctor.prototype = parent.prototype;

@ -1,9 +1,9 @@
"use strict"; "use strict";
/* Object utilities. */ /* Object utilities. */
var objects = { let objects = {
values: function(object) { values: function(object) {
var result = [], key; let result = [], key;
for (key in object) { for (key in object) {
if (object.hasOwnProperty(key)) { if (object.hasOwnProperty(key)) {
@ -15,7 +15,7 @@ var objects = {
}, },
clone: function(object) { clone: function(object) {
var result = {}, key; let result = {}, key;
for (key in object) { for (key in object) {
if (object.hasOwnProperty(key)) { if (object.hasOwnProperty(key)) {
@ -27,7 +27,7 @@ var objects = {
}, },
defaults: function(object, defaults) { defaults: function(object, defaults) {
var key; let key;
for (key in defaults) { for (key in defaults) {
if (defaults.hasOwnProperty(key)) { if (defaults.hasOwnProperty(key)) {

@ -3,24 +3,24 @@
"use strict"; "use strict";
var peg = require("../../lib/peg"); let peg = require("../../lib/peg");
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.generate('start = "a"'); let 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.generate('start = "a"'); let 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.generate([ let 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"; }'
@ -49,7 +49,7 @@ describe("generated parser API", function() {
}); });
describe("tracing", function() { describe("tracing", function() {
var parser = peg.generate([ let parser = peg.generate([
'start = a / b', 'start = a / b',
'a = "a"', 'a = "a"',
'b = "b"' 'b = "b"'
@ -77,7 +77,7 @@ describe("generated parser API", function() {
describe("custom tracers", function() { describe("custom tracers", function() {
describe("trace", function() { describe("trace", function() {
it("receives tracing events", function() { it("receives tracing events", function() {
var tracer = jasmine.createSpyObj("tracer", ["trace"]); let tracer = jasmine.createSpyObj("tracer", ["trace"]);
parser.parse("b", { tracer: tracer }); parser.parse("b", { tracer: tracer });
@ -137,7 +137,7 @@ describe("generated parser API", function() {
}); });
it("accepts custom options", function() { it("accepts custom options", function() {
var parser = peg.generate('start = "a"'); let parser = peg.generate('start = "a"');
parser.parse("a", { foo: 42 }); parser.parse("a", { foo: 42 });
}); });

@ -1,11 +1,11 @@
"use strict"; "use strict";
var peg = require("../../lib/peg"); let peg = require("../../lib/peg");
describe("PEG.js API", function() { describe("PEG.js API", function() {
describe("generate", function() { describe("generate", function() {
it("generates a parser", function() { it("generates a parser", function() {
var parser = peg.generate('start = "a"'); let 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");
@ -20,7 +20,7 @@ describe("PEG.js API", function() {
}); });
describe("allowed start rules", function() { describe("allowed start rules", function() {
var grammar = [ let grammar = [
'a = "x"', 'a = "x"',
'b = "x"', 'b = "x"',
'c = "x"' 'c = "x"'
@ -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.generate(grammar, { optimize: "speed" }); let 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.generate(grammar, { let 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.generate(grammar, { optimize: "size" }); let 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.generate(grammar, { let parser = peg.generate(grammar, {
optimize: "size", optimize: "size",
allowedStartRules: ["b", "c"] allowedStartRules: ["b", "c"]
}); });
@ -95,7 +95,7 @@ describe("PEG.js API", function() {
}); });
describe("intermediate results caching", function() { describe("intermediate results caching", function() {
var grammar = [ let grammar = [
'{ 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++; }'
@ -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.generate(grammar); let 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.generate(grammar, { cache: false }); let 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.generate(grammar, { cache: true }); let parser = peg.generate(grammar, { cache: true });
expect(parser.parse("ac")).toBe(1); expect(parser.parse("ac")).toBe(1);
}); });
@ -127,11 +127,11 @@ describe("PEG.js API", function() {
}); });
describe("tracing", function() { describe("tracing", function() {
var grammar = 'start = "a"'; let grammar = 'start = "a"';
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.generate(grammar), let 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.generate(grammar, { trace: false }), let 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.generate(grammar, { trace: true }), let 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 });
@ -169,11 +169,11 @@ describe("PEG.js API", function() {
*/ */
describe("output", function() { describe("output", function() {
var grammar = 'start = "a"'; let grammar = 'start = "a"';
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.generate(grammar); let 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.generate(grammar, { output: "parser" }); let 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.generate(grammar, { output: "source" }); let 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");

@ -1,6 +1,6 @@
"use strict"; "use strict";
var peg = require("../../lib/peg"); let peg = require("../../lib/peg");
describe("plugin API", function() { describe("plugin API", function() {
beforeEach(function() { beforeEach(function() {
@ -38,10 +38,10 @@ describe("plugin API", function() {
}); });
describe("use", function() { describe("use", function() {
var grammar = 'start = "a"'; let grammar = 'start = "a"';
it("is called for each plugin", function() { it("is called for each plugin", function() {
var pluginsUsed = [false, false, false], let pluginsUsed = [false, false, false],
plugins = [ plugins = [
{ use: function() { pluginsUsed[0] = true; } }, { use: function() { pluginsUsed[0] = true; } },
{ use: function() { pluginsUsed[1] = true; } }, { use: function() { pluginsUsed[1] = true; } },
@ -54,7 +54,7 @@ describe("plugin API", function() {
}); });
it("receives configuration", function() { it("receives configuration", function() {
var plugin = { let plugin = {
use: function(config) { use: function(config) {
expect(config).toBeObject(); expect(config).toBeObject();
@ -84,7 +84,7 @@ describe("plugin API", function() {
}); });
it("receives options", function() { it("receives options", function() {
var plugin = { let plugin = {
use: function(config, options) { use: function(config, options) {
expect(options).toEqual(generateOptions); expect(options).toEqual(generateOptions);
} }
@ -95,9 +95,9 @@ describe("plugin API", function() {
}); });
it("can replace parser", function() { it("can replace parser", function() {
var plugin = { let plugin = {
use: function(config) { use: function(config) {
var parser = peg.generate([ let parser = peg.generate([
'start = .* {', 'start = .* {',
' return {', ' return {',
' type: "grammar",', ' type: "grammar",',
@ -121,9 +121,9 @@ describe("plugin API", function() {
}); });
it("can change compiler passes", function() { it("can change compiler passes", function() {
var plugin = { let plugin = {
use: function(config) { use: function(config) {
var pass = function(ast) { let pass = function(ast) {
ast.code = '({ parse: function() { return 42; } })'; ast.code = '({ parse: function() { return 42; } })';
}; };
@ -136,7 +136,7 @@ describe("plugin API", function() {
}); });
it("can change options", function() { it("can change options", function() {
var grammar = [ let grammar = [
'a = "x"', 'a = "x"',
'b = "x"', 'b = "x"',
'c = "x"' 'c = "x"'

@ -3,12 +3,12 @@
"use strict"; "use strict";
var peg = require("../../lib/peg"); let peg = require("../../lib/peg");
describe("generated parser behavior", function() { describe("generated parser behavior", function() {
function varyOptimizationOptions(block) { function varyOptimizationOptions(block) {
function clone(object) { function clone(object) {
var result = {}, key; let result = {}, key;
for (key in object) { for (key in object) {
if (object.hasOwnProperty(key)) { if (object.hasOwnProperty(key)) {
@ -19,7 +19,7 @@ describe("generated parser behavior", function() {
return result; return result;
} }
var optionsVariants = [ let optionsVariants = [
{ cache: false, optimize: "speed", trace: false }, { cache: false, optimize: "speed", trace: false },
{ cache: false, optimize: "speed", trace: true }, { cache: false, optimize: "speed", trace: true },
{ cache: false, optimize: "size", trace: false }, { cache: false, optimize: "size", trace: false },
@ -43,7 +43,7 @@ describe("generated parser behavior", function() {
toParse: function(input, expected, options) { toParse: function(input, expected, options) {
options = options !== undefined ? options : {}; options = options !== undefined ? options : {};
var result; let result;
try { try {
result = this.actual.parse(input, options); result = this.actual.parse(input, options);
@ -77,7 +77,7 @@ describe("generated parser behavior", function() {
toFailToParse: function(input, details, options) { toFailToParse: function(input, details, options) {
options = options !== undefined ? options : {}; options = options !== undefined ? options : {};
var result, key; let result, key;
try { try {
result = this.actual.parse(input, options); result = this.actual.parse(input, options);
@ -138,7 +138,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.generate([ let parser = peg.generate([
'{ var result = 42; }', '{ var result = 42; }',
'start = "a" { return result; }' 'start = "a" { return result; }'
].join("\n"), options); ].join("\n"), options);
@ -148,7 +148,7 @@ describe("generated parser behavior", function() {
describe("available variables and functions", function() { describe("available variables and functions", function() {
it("|options| contains options", function() { it("|options| contains options", function() {
var parser = peg.generate([ let parser = peg.generate([
'{ var result = options; }', '{ var result = options; }',
'start = "a" { return result; }' 'start = "a" { return result; }'
].join("\n"), options); ].join("\n"), options);
@ -161,7 +161,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.generate([ let 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++; }'
@ -171,7 +171,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.generate([ let 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++; }'
@ -183,7 +183,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.generate('start = "a"'); let parser = peg.generate('start = "a"');
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
@ -192,7 +192,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.generate('start = "a"'); let parser = peg.generate('start = "a"');
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "literal", text: "a", ignoreCase: false }] expected: [{ type: "literal", text: "a", ignoreCase: false }]
@ -202,7 +202,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.generate('start "start" = "a"'); let parser = peg.generate('start "start" = "a"');
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "other", description: "start" }] expected: [{ type: "other", description: "start" }]
@ -210,7 +210,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.generate('start "start" = "a"'); let parser = peg.generate('start "start" = "a"');
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "other", description: "start" }] expected: [{ type: "other", description: "start" }]
@ -223,34 +223,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.generate('start = ""', options); let 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.generate('start = "a"', options); let 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.generate('start = "abcd"', options); let 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.generate('start = "a"', options); let 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.generate('start = "a"i', options); let parser = peg.generate('start = "a"i', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toParse("A"); expect(parser).toParse("A");
@ -259,13 +259,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.generate('start = "a"', options); let 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.generate('start = "a" .', options); let parser = peg.generate('start = "a" .', options);
expect(parser).toParse("ab"); expect(parser).toParse("ab");
}); });
@ -273,7 +273,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.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "literal", text: "a", ignoreCase: false }] expected: [{ type: "literal", text: "a", ignoreCase: false }]
@ -285,13 +285,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.generate('start = []', options); let 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.generate('start = [abc]', options); let parser = peg.generate('start = [abc]', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toParse("b"); expect(parser).toParse("b");
@ -300,7 +300,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.generate('start = [a-c]', options); let parser = peg.generate('start = [a-c]', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toParse("b"); expect(parser).toParse("b");
@ -309,21 +309,21 @@ describe("generated parser behavior", function() {
}); });
it("matches inverted classes", function() { it("matches inverted classes", function() {
var parser = peg.generate('start = [^a]', options); let 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.generate('start = [a]', options); let 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.generate('start = [a]i', options); let parser = peg.generate('start = [a]i', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toParse("A"); expect(parser).toParse("A");
@ -332,13 +332,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.generate('start = [a]', options); let 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.generate('start = [a] .', options); let parser = peg.generate('start = [a] .', options);
expect(parser).toParse("ab"); expect(parser).toParse("ab");
}); });
@ -346,7 +346,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.generate('start = [a]', options); let parser = peg.generate('start = [a]', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "class", parts: ["a"], inverted: false, ignoreCase: false }] expected: [{ type: "class", parts: ["a"], inverted: false, ignoreCase: false }]
@ -358,7 +358,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.generate('start = .', options); let parser = peg.generate('start = .', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
expect(parser).toParse("b"); expect(parser).toParse("b");
@ -368,13 +368,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.generate('start = .', options); let 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.generate('start = . .', options); let parser = peg.generate('start = . .', options);
expect(parser).toParse("ab"); expect(parser).toParse("ab");
}); });
@ -382,7 +382,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.generate('start = .', options); let parser = peg.generate('start = .', options);
expect(parser).toFailToParse("", { expect(parser).toFailToParse("", {
expected: [{ type: "any" }] expected: [{ type: "any" }]
@ -394,7 +394,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.generate([ let parser = peg.generate([
'start = a', 'start = a',
'a = "a"' 'a = "a"'
].join("\n"), options); ].join("\n"), options);
@ -405,7 +405,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.generate([ let parser = peg.generate([
'start = a', 'start = a',
'a = "a"' 'a = "a"'
].join("\n"), options); ].join("\n"), options);
@ -423,7 +423,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.generate('start = &{ return true; } ""', options); let parser = peg.generate('start = &{ return true; } ""', options);
expect(parser).toParse("", [undefined, ""]); expect(parser).toParse("", [undefined, ""]);
}); });
@ -431,7 +431,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.generate('start = &{ return false; }', options); let parser = peg.generate('start = &{ return false; }', options);
expect(parser).toFailToParse(""); expect(parser).toFailToParse("");
}); });
@ -440,7 +440,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.generate( let parser = peg.generate(
'start = a:"a" &{ return a === "a"; }', 'start = a:"a" &{ return a === "a"; }',
options options
); );
@ -449,7 +449,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.generate( let parser = peg.generate(
'start = "a" b:&{ return b === undefined; } "c"', 'start = "a" b:&{ return b === undefined; } "c"',
options options
); );
@ -458,7 +458,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.generate( let parser = peg.generate(
'start = &{ return a === "a"; } a:"a"', 'start = &{ return a === "a"; } a:"a"',
options options
); );
@ -467,7 +467,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variables defined by subexpressions", function() { it("cannot access variables defined by subexpressions", function() {
var testcases = [ let testcases = [
{ {
grammar: 'start = (a:"a") &{ return a === "a"; }', grammar: 'start = (a:"a") &{ return a === "a"; }',
input: "a" input: "a"
@ -524,7 +524,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.generate( let parser = peg.generate(
'start = a:"a" ("b" &{ return a === "a"; })', 'start = a:"a" ("b" &{ return a === "a"; })',
options options
); );
@ -533,7 +533,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.generate( let parser = peg.generate(
'start = "a" b:("b" &{ return b === undefined; }) "c"', 'start = "a" b:("b" &{ return b === undefined; }) "c"',
options options
); );
@ -542,7 +542,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.generate( let parser = peg.generate(
'start = ("a" &{ return b === "b"; }) b:"b"', 'start = ("a" &{ return b === "b"; }) b:"b"',
options options
); );
@ -554,7 +554,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.generate([ let parser = peg.generate([
'{ var v = 42 }', '{ var v = 42 }',
'start = &{ return v === 42; }' 'start = &{ return v === 42; }'
].join("\n"), options); ].join("\n"), options);
@ -563,7 +563,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.generate([ let 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);
@ -574,7 +574,7 @@ describe("generated parser behavior", function() {
describe("available variables & functions", function() { describe("available variables & functions", function() {
it("|options| contains options", function() { it("|options| contains options", function() {
var parser = peg.generate([ let 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);
@ -583,7 +583,7 @@ describe("generated parser behavior", function() {
}); });
it("|location| returns current location info", function() { it("|location| returns current location info", function() {
var parser = peg.generate([ let 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)*',
@ -619,7 +619,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.generate('start = !{ return false; } ""', options); let parser = peg.generate('start = !{ return false; } ""', options);
expect(parser).toParse("", [undefined, ""]); expect(parser).toParse("", [undefined, ""]);
}); });
@ -627,7 +627,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.generate('start = !{ return true; }', options); let parser = peg.generate('start = !{ return true; }', options);
expect(parser).toFailToParse(""); expect(parser).toFailToParse("");
}); });
@ -636,7 +636,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.generate( let parser = peg.generate(
'start = a:"a" !{ return a !== "a"; }', 'start = a:"a" !{ return a !== "a"; }',
options options
); );
@ -645,7 +645,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.generate( let parser = peg.generate(
'start = "a" b:!{ return b !== undefined; } "c"', 'start = "a" b:!{ return b !== undefined; } "c"',
options options
); );
@ -654,7 +654,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.generate( let parser = peg.generate(
'start = !{ return a !== "a"; } a:"a"', 'start = !{ return a !== "a"; } a:"a"',
options options
); );
@ -663,7 +663,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variables defined by subexpressions", function() { it("cannot access variables defined by subexpressions", function() {
var testcases = [ let testcases = [
{ {
grammar: 'start = (a:"a") !{ return a !== "a"; }', grammar: 'start = (a:"a") !{ return a !== "a"; }',
input: "a" input: "a"
@ -720,7 +720,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.generate( let parser = peg.generate(
'start = a:"a" ("b" !{ return a !== "a"; })', 'start = a:"a" ("b" !{ return a !== "a"; })',
options options
); );
@ -729,7 +729,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.generate( let parser = peg.generate(
'start = "a" b:("b" !{ return b !== undefined; }) "c"', 'start = "a" b:("b" !{ return b !== undefined; }) "c"',
options options
); );
@ -738,7 +738,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.generate( let parser = peg.generate(
'start = ("a" !{ return b !== "b"; }) b:"b"', 'start = ("a" !{ return b !== "b"; }) b:"b"',
options options
); );
@ -750,7 +750,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.generate([ let parser = peg.generate([
'{ var v = 42 }', '{ var v = 42 }',
'start = !{ return v !== 42; }' 'start = !{ return v !== 42; }'
].join("\n"), options); ].join("\n"), options);
@ -759,7 +759,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.generate([ let 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);
@ -770,7 +770,7 @@ describe("generated parser behavior", function() {
describe("available variables & functions", function() { describe("available variables & functions", function() {
it("|options| contains options", function() { it("|options| contains options", function() {
var parser = peg.generate([ let 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);
@ -779,7 +779,7 @@ describe("generated parser behavior", function() {
}); });
it("|location| returns current location info", function() { it("|location| returns current location info", function() {
var parser = peg.generate([ let 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)*',
@ -810,7 +810,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.generate('start = ("a")', options); let parser = peg.generate('start = ("a")', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
@ -818,7 +818,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.generate('start = ("a")', options); let parser = peg.generate('start = ("a")', options);
expect(parser).toFailToParse("b"); expect(parser).toFailToParse("b");
}); });
@ -828,7 +828,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.generate('start = "a"?', options); let parser = peg.generate('start = "a"?', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
@ -836,7 +836,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.generate('start = "a"?', options); let parser = peg.generate('start = "a"?', options);
expect(parser).toParse("", null); expect(parser).toParse("", null);
}); });
@ -846,7 +846,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.generate('start = "a"*', options); let parser = peg.generate('start = "a"*', options);
expect(parser).toParse("", []); expect(parser).toParse("", []);
expect(parser).toParse("a", ["a"]); expect(parser).toParse("a", ["a"]);
@ -858,7 +858,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.generate('start = "a"+', options); let 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"]);
@ -867,7 +867,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.generate('start = "a"+', options); let parser = peg.generate('start = "a"+', options);
expect(parser).toFailToParse(""); expect(parser).toFailToParse("");
}); });
@ -877,7 +877,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.generate('start = $("a" "b" "c")', options); let parser = peg.generate('start = $("a" "b" "c")', options);
expect(parser).toParse("abc", "abc"); expect(parser).toParse("abc", "abc");
}); });
@ -885,7 +885,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.generate('start = $("a")', options); let parser = peg.generate('start = $("a")', options);
expect(parser).toFailToParse("b"); expect(parser).toFailToParse("b");
}); });
@ -895,13 +895,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.generate('start = &"a" "a"', options); let 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.generate('start = &"a" "a"', options); let parser = peg.generate('start = &"a" "a"', options);
expect(parser).toParse("a"); expect(parser).toParse("a");
}); });
@ -909,13 +909,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.generate('start = &"a"', options); let 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.generate('start = "a" / &"b" / "c"', options); let parser = peg.generate('start = "a" / &"b" / "c"', options);
expect(parser).toFailToParse("d", { expect(parser).toFailToParse("d", {
expected: [ expected: [
@ -930,7 +930,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.generate('start = !"a"', options); let parser = peg.generate('start = !"a"', options);
expect(parser).toFailToParse("a"); expect(parser).toFailToParse("a");
}); });
@ -938,19 +938,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.generate('start = !"a" "b"', options); let 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.generate('start = !"a" "b"', options); let 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.generate('start = "a" / !"b" / "c"', options); let parser = peg.generate('start = "a" / !"b" / "c"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [ expected: [
@ -965,7 +965,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.generate('start = a:"a"', options); let parser = peg.generate('start = a:"a"', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
@ -973,7 +973,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.generate('start = a:"a"', options); let parser = peg.generate('start = a:"a"', options);
expect(parser).toFailToParse("b"); expect(parser).toFailToParse("b");
}); });
@ -983,7 +983,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.generate('start = "a" "b" "c"', options); let parser = peg.generate('start = "a" "b" "c"', options);
expect(parser).toParse("abc", ["a", "b", "c"]); expect(parser).toParse("abc", ["a", "b", "c"]);
}); });
@ -991,7 +991,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.generate('start = "a" "b" "c"', options); let parser = peg.generate('start = "a" "b" "c"', options);
expect(parser).toFailToParse("dbc"); expect(parser).toFailToParse("dbc");
expect(parser).toFailToParse("adc"); expect(parser).toFailToParse("adc");
@ -999,7 +999,7 @@ describe("generated parser behavior", function() {
}); });
it("resets parse position", function() { it("resets parse position", function() {
var parser = peg.generate('start = "a" "b" / "a"', options); let parser = peg.generate('start = "a" "b" / "a"', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
}); });
@ -1009,7 +1009,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.generate('start = "a" { return 42; }', options); let parser = peg.generate('start = "a" { return 42; }', options);
expect(parser).toParse("a", 42); expect(parser).toParse("a", 42);
}); });
@ -1017,13 +1017,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.generate('start = a:"a" { return a; }', options); let 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.generate( let 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
); );
@ -1032,7 +1032,7 @@ describe("generated parser behavior", function() {
}); });
it("cannot access variables defined by subexpressions", function() { it("cannot access variables defined by subexpressions", function() {
var testcases = [ let testcases = [
{ {
grammar: 'start = (a:"a") { return a; }', grammar: 'start = (a:"a") { return a; }',
input: "a" input: "a"
@ -1089,7 +1089,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.generate( let parser = peg.generate(
'start = a:"a" ("b" { return a; })', 'start = a:"a" ("b" { return a; })',
options options
); );
@ -1098,7 +1098,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.generate( let parser = peg.generate(
'start = "a" b:("b" { return b; }) c:"c"', 'start = "a" b:("b" { return b; }) c:"c"',
options options
); );
@ -1107,7 +1107,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.generate( let parser = peg.generate(
'start = ("a" { return b; }) b:"b"', 'start = ("a" { return b; }) b:"b"',
options options
); );
@ -1119,7 +1119,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.generate([ let parser = peg.generate([
'{ var v = 42 }', '{ var v = 42 }',
'start = "a" { return v; }' 'start = "a" { return v; }'
].join("\n"), options); ].join("\n"), options);
@ -1128,7 +1128,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.generate([ let 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);
@ -1139,7 +1139,7 @@ describe("generated parser behavior", function() {
describe("available variables & functions", function() { describe("available variables & functions", function() {
it("|options| contains options", function() { it("|options| contains options", function() {
var parser = peg.generate( let parser = peg.generate(
'start = "a" { return options; }', 'start = "a" { return options; }',
options options
); );
@ -1148,7 +1148,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.generate( let parser = peg.generate(
'start = "a" { return text(); }', 'start = "a" { return text(); }',
options options
); );
@ -1157,7 +1157,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.generate([ let 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)*',
@ -1185,7 +1185,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.generate( let parser = peg.generate(
'start = "a" { expected("a"); }', 'start = "a" { expected("a"); }',
options options
); );
@ -1202,7 +1202,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.generate([ let 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 },',
@ -1225,7 +1225,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.generate( let parser = peg.generate(
'start = "a" { error("a"); }', 'start = "a" { error("a"); }',
options options
); );
@ -1242,7 +1242,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.generate([ let 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 },',
@ -1267,13 +1267,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.generate('start = "a" { return 42; }', options); let 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.generate( let parser = peg.generate(
'start = "a" { throw "Boom!"; } / "b"', 'start = "a" { throw "Boom!"; } / "b"',
options options
); );
@ -1286,7 +1286,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.generate('start = "a" / "b" / "c"', options); let 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");
@ -1296,7 +1296,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.generate('start = "a" / "b" / "c"', options); let parser = peg.generate('start = "a" / "b" / "c"', options);
expect(parser).toFailToParse("d"); expect(parser).toFailToParse("d");
}); });
@ -1306,7 +1306,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.generate('start = "a" "b" / "a" "c" "d"', options); let parser = peg.generate('start = "a" "b" / "a" "c" "d"', options);
expect(parser).toFailToParse("ace", { expect(parser).toFailToParse("ace", {
expected: [{ type: "literal", text: "d", ignoreCase: false }] expected: [{ type: "literal", text: "d", ignoreCase: false }]
@ -1316,7 +1316,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.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("ab", { expect(parser).toFailToParse("ab", {
expected: [{ type: "end" }] expected: [{ type: "end" }]
@ -1324,7 +1324,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.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "literal", text: "a", ignoreCase: false }] expected: [{ type: "literal", text: "a", ignoreCase: false }]
@ -1332,7 +1332,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.generate('start = "a" / "b" / "c"', options); let parser = peg.generate('start = "a" / "b" / "c"', options);
expect(parser).toFailToParse("d", { expect(parser).toFailToParse("d", {
expected: [ expected: [
@ -1346,13 +1346,13 @@ describe("generated parser behavior", function() {
describe("found string reporting", function() { describe("found string reporting", function() {
it("reports found string correctly at the end of input", function() { it("reports found string correctly at the end of input", function() {
var parser = peg.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("", { found: null }); expect(parser).toFailToParse("", { found: null });
}); });
it("reports found string correctly in the middle of input", function() { it("reports found string correctly in the middle of input", function() {
var parser = peg.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("b", { found: "b" }); expect(parser).toFailToParse("b", { found: "b" });
}); });
@ -1360,7 +1360,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.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("ab", { expect(parser).toFailToParse("ab", {
message: 'Expected end of input but "b" found.' message: 'Expected end of input but "b" found.'
@ -1368,7 +1368,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.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
message: 'Expected "a" but "b" found.' message: 'Expected "a" but "b" found.'
@ -1376,7 +1376,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.generate('start = "a" / "b" / "c"', options); let parser = peg.generate('start = "a" / "b" / "c"', options);
expect(parser).toFailToParse("d", { expect(parser).toFailToParse("d", {
message: 'Expected "a", "b", or "c" but "d" found.' message: 'Expected "a", "b", or "c" but "d" found.'
@ -1384,7 +1384,7 @@ describe("generated parser behavior", function() {
}); });
it("builds message correctly at the end of input", function() { it("builds message correctly at the end of input", function() {
var parser = peg.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("", { expect(parser).toFailToParse("", {
message: 'Expected "a" but end of input found.' message: 'Expected "a" but end of input found.'
@ -1392,7 +1392,7 @@ describe("generated parser behavior", function() {
}); });
it("builds message correctly in the middle of input", function() { it("builds message correctly in the middle of input", function() {
var parser = peg.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
message: 'Expected "a" but "b" found.' message: 'Expected "a" but "b" found.'
@ -1400,7 +1400,7 @@ describe("generated parser behavior", function() {
}); });
it("removes duplicates from expectations", function() { it("removes duplicates from expectations", function() {
var parser = peg.generate('start = "a" / "a"', options); let parser = peg.generate('start = "a" / "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
message: 'Expected "a" but "b" found.' message: 'Expected "a" but "b" found.'
@ -1408,7 +1408,7 @@ describe("generated parser behavior", function() {
}); });
it("sorts expectations", function() { it("sorts expectations", function() {
var parser = peg.generate('start = "c" / "b" / "a"', options); let parser = peg.generate('start = "c" / "b" / "a"', options);
expect(parser).toFailToParse("d", { expect(parser).toFailToParse("d", {
message: 'Expected "a", "b", or "c" but "d" found.' message: 'Expected "a", "b", or "c" but "d" found.'
@ -1419,7 +1419,7 @@ describe("generated parser behavior", function() {
describe("position reporting", function() { describe("position reporting", function() {
it("reports position correctly at the end of input", function() { it("reports position correctly at the end of input", function() {
var parser = peg.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("", { expect(parser).toFailToParse("", {
location: { location: {
@ -1430,7 +1430,7 @@ describe("generated parser behavior", function() {
}); });
it("reports position correctly in the middle of input", function() { it("reports position correctly in the middle of input", function() {
var parser = peg.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
location: { location: {
@ -1441,7 +1441,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.generate('start = "a"', options); let parser = peg.generate('start = "a"', options);
expect(parser).toFailToParse("aa", { expect(parser).toFailToParse("aa", {
location: { location: {
@ -1452,7 +1452,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.generate([ let parser = peg.generate([
'start = line (nl+ line)*', 'start = line (nl+ line)*',
'line = digit (" "+ digit)*', 'line = digit (" "+ digit)*',
'digit = [0-9]', 'digit = [0-9]',
@ -1495,7 +1495,7 @@ describe("generated parser behavior", function() {
* Sum Product (('+' / '-') Product)* * Sum Product (('+' / '-') Product)*
* Expr Sum * Expr Sum
*/ */
var parser = peg.generate([ let parser = peg.generate([
'Expr = Sum', 'Expr = Sum',
'Sum = head:Product tail:(("+" / "-") Product)* {', 'Sum = head:Product tail:(("+" / "-") Product)* {',
' return tail.reduce(function(result, element) {', ' return tail.reduce(function(result, element) {',
@ -1547,7 +1547,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.generate([ let 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(""); }'
@ -1571,7 +1571,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.generate([ let 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; }',

@ -9,19 +9,19 @@
* browser. * browser.
*/ */
var express = require("express"), let express = require("express"),
logger = require("morgan"), logger = require("morgan"),
glob = require("glob"), glob = require("glob"),
browserify = require("browserify"), browserify = require("browserify"),
babelify = require("babelify"); babelify = require("babelify");
var app = express(); let app = express();
app.use(logger("dev")); app.use(logger("dev"));
app.use(express.static(__dirname)); app.use(express.static(__dirname));
app.get("/bundle.js", function(req, res) { app.get("/bundle.js", function(req, res) {
var files = glob.sync(__dirname + "/**/*.js", { let files = glob.sync(__dirname + "/**/*.js", {
ignore: __dirname + "/vendor/**/*" ignore: __dirname + "/vendor/**/*"
}); });

@ -1,9 +1,9 @@
"use strict"; "use strict";
var peg = require("../../../../lib/peg"); let peg = require("../../../../lib/peg");
describe("compiler pass |generateBytecode|", function() { describe("compiler pass |generateBytecode|", function() {
var pass = peg.compiler.passes.generate.generateBytecode; let pass = peg.compiler.passes.generate.generateBytecode;
function bytecodeDetails(bytecode) { function bytecodeDetails(bytecode) {
return { return {
@ -53,7 +53,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("for named", function() { describe("for named", function() {
var grammar = 'start "start" = "a"'; let grammar = 'start "start" = "a"';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -90,7 +90,7 @@ describe("compiler pass |generateBytecode|", function() {
describe("for action", function() { describe("for action", function() {
describe("without labels", function() { describe("without labels", function() {
var grammar = 'start = "a" { code }'; let grammar = 'start = "a" { code }';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -113,7 +113,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("with one label", function() { describe("with one label", function() {
var grammar = 'start = a:"a" { code }'; let grammar = 'start = a:"a" { code }';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -136,7 +136,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("with multiple labels", function() { describe("with multiple labels", function() {
var grammar = 'start = a:"a" b:"b" c:"c" { code }'; let grammar = 'start = a:"a" b:"b" c:"c" { code }';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -177,7 +177,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("for sequence", function() { describe("for sequence", function() {
var grammar = 'start = "a" "b" "c"'; let grammar = 'start = "a" "b" "c"';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -236,7 +236,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("for simple_and", function() { describe("for simple_and", function() {
var grammar = 'start = &"a"'; let grammar = 'start = &"a"';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -263,7 +263,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("for simple_not", function() { describe("for simple_not", function() {
var grammar = 'start = !"a"'; let grammar = 'start = !"a"';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -290,7 +290,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("for optional", function() { describe("for optional", function() {
var grammar = 'start = "a"?'; let grammar = 'start = "a"?';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -310,7 +310,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("for zero_or_more", function() { describe("for zero_or_more", function() {
var grammar = 'start = "a"*'; let grammar = 'start = "a"*';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -332,7 +332,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("for one_or_more", function() { describe("for one_or_more", function() {
var grammar = 'start = "a"+'; let grammar = 'start = "a"+';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -367,7 +367,7 @@ describe("compiler pass |generateBytecode|", function() {
describe("for semantic_and", function() { describe("for semantic_and", function() {
describe("without labels", function() { describe("without labels", function() {
var grammar = 'start = &{ code }'; let grammar = 'start = &{ code }';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -390,7 +390,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("with labels", function() { describe("with labels", function() {
var grammar = 'start = a:"a" b:"b" c:"c" &{ code }'; let grammar = 'start = a:"a" b:"b" c:"c" &{ code }';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -442,7 +442,7 @@ describe("compiler pass |generateBytecode|", function() {
describe("for semantic_not", function() { describe("for semantic_not", function() {
describe("without labels", function() { describe("without labels", function() {
var grammar = 'start = !{ code }'; let grammar = 'start = !{ code }';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -465,7 +465,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("with labels", function() { describe("with labels", function() {
var grammar = 'start = a:"a" b:"b" c:"c" !{ code }'; let grammar = 'start = a:"a" b:"b" c:"c" !{ code }';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -533,7 +533,7 @@ describe("compiler pass |generateBytecode|", function() {
describe("for literal", function() { describe("for literal", function() {
describe("empty", function() { describe("empty", function() {
var grammar = 'start = ""'; let grammar = 'start = ""';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -547,7 +547,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("non-empty case-sensitive", function() { describe("non-empty case-sensitive", function() {
var grammar = 'start = "a"'; let grammar = 'start = "a"';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -566,7 +566,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("non-empty case-insensitive", function() { describe("non-empty case-insensitive", function() {
var grammar = 'start = "A"i'; let grammar = 'start = "A"i';
it("generates correct bytecode", function() { it("generates correct bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([
@ -632,7 +632,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
describe("for any", function() { describe("for any", function() {
var grammar = 'start = .'; let grammar = 'start = .';
it("generates bytecode", function() { it("generates bytecode", function() {
expect(pass).toChangeAST(grammar, bytecodeDetails([ expect(pass).toChangeAST(grammar, bytecodeDetails([

@ -1,6 +1,6 @@
"use strict"; "use strict";
var peg = require("../../../../lib/peg"); let peg = require("../../../../lib/peg");
beforeEach(function() { beforeEach(function() {
this.addMatchers({ this.addMatchers({
@ -16,7 +16,7 @@ beforeEach(function() {
return value !== null && typeof value === "object"; return value !== null && typeof value === "object";
} }
var i, key; let i, key;
if (isArray(details)) { if (isArray(details)) {
if (!isArray(value)) { return false; } if (!isArray(value)) { return false; }
@ -44,7 +44,7 @@ beforeEach(function() {
} }
} }
var ast = peg.parser.parse(grammar); let ast = peg.parser.parse(grammar);
this.actual(ast, options); this.actual(ast, options);
@ -61,7 +61,7 @@ beforeEach(function() {
}, },
toReportError: function(grammar, details) { toReportError: function(grammar, details) {
var ast = peg.parser.parse(grammar), let ast = peg.parser.parse(grammar),
key; key;
try { try {

@ -1,9 +1,9 @@
"use strict"; "use strict";
var peg = require("../../../../lib/peg"); let peg = require("../../../../lib/peg");
describe("compiler pass |removeProxyRules|", function() { describe("compiler pass |removeProxyRules|", function() {
var pass = peg.compiler.passes.transform.removeProxyRules; let pass = peg.compiler.passes.transform.removeProxyRules;
describe("when a proxy rule isn't listed in |allowedStartRules|", function() { describe("when a proxy rule isn't listed in |allowedStartRules|", function() {
it("updates references and removes it", function() { it("updates references and removes it", function() {

@ -1,9 +1,9 @@
"use strict"; "use strict";
var peg = require("../../../../lib/peg"); let peg = require("../../../../lib/peg");
describe("compiler pass |reportDuplicateLabels|", function() { describe("compiler pass |reportDuplicateLabels|", function() {
var pass = peg.compiler.passes.check.reportDuplicateLabels; let pass = peg.compiler.passes.check.reportDuplicateLabels;
describe("in a sequence", function() { describe("in a sequence", function() {
it("reports labels duplicate with labels of preceding elements", function() { it("reports labels duplicate with labels of preceding elements", function() {

@ -1,9 +1,9 @@
"use strict"; "use strict";
var peg = require("../../../../lib/peg"); let peg = require("../../../../lib/peg");
describe("compiler pass |reportDuplicateRules|", function() { describe("compiler pass |reportDuplicateRules|", function() {
var pass = peg.compiler.passes.check.reportDuplicateRules; let pass = peg.compiler.passes.check.reportDuplicateRules;
it("reports duplicate rules", function() { it("reports duplicate rules", function() {
expect(pass).toReportError([ expect(pass).toReportError([

@ -1,9 +1,9 @@
"use strict"; "use strict";
var peg = require("../../../../lib/peg"); let peg = require("../../../../lib/peg");
describe("compiler pass |reportInfiniteRecursion|", function() { describe("compiler pass |reportInfiniteRecursion|", function() {
var pass = peg.compiler.passes.check.reportInfiniteRecursion; let pass = peg.compiler.passes.check.reportInfiniteRecursion;
it("reports direct left recursion", function() { it("reports direct left recursion", function() {
expect(pass).toReportError('start = start', { expect(pass).toReportError('start = start', {

@ -1,9 +1,9 @@
"use strict"; "use strict";
var peg = require("../../../../lib/peg"); let peg = require("../../../../lib/peg");
describe("compiler pass |reportInfiniteRepetition|", function() { describe("compiler pass |reportInfiniteRepetition|", function() {
var pass = peg.compiler.passes.check.reportInfiniteRepetition; let pass = peg.compiler.passes.check.reportInfiniteRepetition;
it("reports infinite loops for zero_or_more", function() { it("reports infinite loops for zero_or_more", function() {
expect(pass).toReportError('start = ("")*', { expect(pass).toReportError('start = ("")*', {

@ -1,9 +1,9 @@
"use strict"; "use strict";
var peg = require("../../../../lib/peg"); let peg = require("../../../../lib/peg");
describe("compiler pass |reportUndefinedRules|", function() { describe("compiler pass |reportUndefinedRules|", function() {
var pass = peg.compiler.passes.check.reportUndefinedRules; let pass = peg.compiler.passes.check.reportUndefinedRules;
it("reports undefined rules", function() { it("reports undefined rules", function() {
expect(pass).toReportError('start = undefined', { expect(pass).toReportError('start = undefined', {

@ -1,9 +1,9 @@
"use strict"; "use strict";
var peg = require("../../lib/peg"); let peg = require("../../lib/peg");
describe("PEG.js grammar parser", function() { describe("PEG.js grammar parser", function() {
var literalAbcd = { type: "literal", value: "abcd", ignoreCase: false }, let literalAbcd = { type: "literal", value: "abcd", ignoreCase: false },
literalEfgh = { type: "literal", value: "efgh", ignoreCase: false }, literalEfgh = { type: "literal", value: "efgh", ignoreCase: false },
literalIjkl = { type: "literal", value: "ijkl", ignoreCase: false }, literalIjkl = { type: "literal", value: "ijkl", ignoreCase: false },
literalMnop = { type: "literal", value: "mnop", ignoreCase: false }, literalMnop = { type: "literal", value: "mnop", ignoreCase: false },
@ -96,14 +96,14 @@ describe("PEG.js grammar parser", function() {
return oneRuleGrammar({ type: "rule_ref", name: name }); return oneRuleGrammar({ type: "rule_ref", name: name });
} }
var trivialGrammar = literalGrammar("abcd", false), let trivialGrammar = literalGrammar("abcd", false),
twoRuleGrammar = { twoRuleGrammar = {
type: "grammar", type: "grammar",
initializer: null, initializer: null,
rules: [ruleA, ruleB] rules: [ruleA, ruleB]
}; };
var stripLocation = (function() { let stripLocation = (function() {
function buildVisitor(functions) { function buildVisitor(functions) {
return function(node) { return function(node) {
return functions[node.type].apply(null, arguments); return functions[node.type].apply(null, arguments);
@ -128,7 +128,7 @@ describe("PEG.js grammar parser", function() {
}; };
} }
var strip = buildVisitor({ let strip = buildVisitor({
grammar: function(node) { grammar: function(node) {
delete node.location; delete node.location;
@ -166,7 +166,7 @@ describe("PEG.js grammar parser", function() {
beforeEach(function() { beforeEach(function() {
this.addMatchers({ this.addMatchers({
toParseAs: function(expected) { toParseAs: function(expected) {
var result; let result;
try { try {
result = peg.parser.parse(this.actual); result = peg.parser.parse(this.actual);
@ -194,7 +194,7 @@ describe("PEG.js grammar parser", function() {
}, },
toFailToParse: function(details) { toFailToParse: function(details) {
var result, key; let result, key;
try { try {
result = peg.parser.parse(this.actual); result = peg.parser.parse(this.actual);

@ -24,19 +24,19 @@
*/ */
{ {
var OPS_TO_PREFIXED_TYPES = { const OPS_TO_PREFIXED_TYPES = {
"$": "text", "$": "text",
"&": "simple_and", "&": "simple_and",
"!": "simple_not" "!": "simple_not"
}; };
var OPS_TO_SUFFIXED_TYPES = { const OPS_TO_SUFFIXED_TYPES = {
"?": "optional", "?": "optional",
"*": "zero_or_more", "*": "zero_or_more",
"+": "one_or_more" "+": "one_or_more"
}; };
var OPS_TO_SEMANTIC_PREDICATE_TYPES = { const OPS_TO_SEMANTIC_PREDICATE_TYPES = {
"&": "semantic_and", "&": "semantic_and",
"!": "semantic_not" "!": "semantic_not"
}; };

Loading…
Cancel
Save