Browse Source

Avoid aligning "="

The only exception left are instances where aligning "=" helps to
express symmetry between lines.

See #443.
redux
David Majda 5 years ago
parent
commit
ff7193776e
  1. 6
      benchmark/index.js
  2. 4
      benchmark/run
  3. 8
      benchmark/server
  4. 6
      bin/pegjs
  5. 20
      lib/compiler/passes/generate-bytecode.js
  6. 90
      lib/compiler/passes/generate-js.js
  7. 2
      lib/compiler/passes/report-duplicate-labels.js
  8. 2
      lib/compiler/passes/report-duplicate-rules.js
  9. 4
      lib/compiler/passes/report-infinite-recursion.js
  10. 4
      lib/compiler/passes/report-infinite-repetition.js
  11. 4
      lib/compiler/passes/report-undefined-rules.js
  12. 22
      lib/parser.js
  13. 2
      lib/peg.js
  14. 2
      spec/api/pegjs-api.spec.js
  15. 8
      spec/api/plugin-api.spec.js
  16. 72
      spec/behavior/generated-parser-behavior.spec.js
  17. 2
      spec/index.html
  18. 8
      spec/server
  19. 6
      spec/unit/compiler/passes/report-infinite-recursion.spec.js

6
benchmark/index.js

@ -1,7 +1,7 @@
/* eslint-env browser, jquery */
let benchmarks = require("./benchmarks.js");
let Runner = require("./runner.js");
let Runner = require("./runner.js");
$("#run").click(() => {
"use strict";
@ -17,7 +17,7 @@ $("#run").click(() => {
}
function appendResult(klass, title, url, inputSize, parseTime) {
const KB = 1024;
const KB = 1024;
const MS_IN_S = 1000;
resultsTable.append(
@ -61,7 +61,7 @@ $("#run").click(() => {
// 2. To minimize random errors.
let runCount = parseInt($("#run-count").val(), 10);
let options = {
let options = {
cache: $("#cache").is(":checked"),
optimize: $("#optimize").val()
};

4
benchmark/run

@ -5,7 +5,7 @@
"use strict";
let fs = require("fs");
let fs = require("fs");
let benchmarks = require("./benchmarks.js");
let Runner = require("./runner.js");
@ -47,7 +47,7 @@ function writeHeading(heading) {
}
function writeResult(title, inputSize, parseTime) {
const KB = 1024;
const KB = 1024;
const MS_IN_S = 1000;
console.log("│ "

8
benchmark/server

@ -9,11 +9,11 @@
// and the benchmark get passed through Babel & Browserify before they are
// served to the browser.
let express = require("express");
let logger = require("morgan");
let glob = require("glob");
let express = require("express");
let logger = require("morgan");
let glob = require("glob");
let browserify = require("browserify");
let babelify = require("babelify");
let babelify = require("babelify");
let app = express();

6
bin/pegjs

@ -2,9 +2,9 @@
"use strict";
let fs = require("fs");
let path = require("path");
let peg = require("../lib/peg");
let fs = require("fs");
let path = require("path");
let peg = require("../lib/peg");
// Helpers

20
lib/compiler/passes/generate-bytecode.js

@ -1,9 +1,9 @@
"use strict";
let asts = require("../asts");
let asts = require("../asts");
let visitor = require("../visitor");
let op = require("../opcodes");
let js = require("../js");
let op = require("../opcodes");
let js = require("../js");
// Generates bytecode.
//
@ -344,15 +344,15 @@ function generateBytecode(ast) {
},
action: function(node, context) {
let env = cloneEnv(context.env);
let emitCall = node.expression.type !== "sequence"
|| node.expression.elements.length === 0;
let env = cloneEnv(context.env);
let emitCall = node.expression.type !== "sequence"
|| node.expression.elements.length === 0;
let expressionCode = generate(node.expression, {
sp: context.sp + (emitCall ? 1 : 0),
env: env,
action: node
});
let functionIndex = addFunctionConst(Object.keys(env), node.code);
let functionIndex = addFunctionConst(Object.keys(env), node.code);
return emitCall
? buildSequence(
@ -568,7 +568,7 @@ function generateBytecode(ast) {
},
"class": function(node) {
let regexp = "/^["
let regexp = "/^["
+ (node.inverted ? "^" : "")
+ node.parts.map(part =>
Array.isArray(part)
@ -578,14 +578,14 @@ function generateBytecode(ast) {
: js.regexpClassEscape(part)
).join("")
+ "]/" + (node.ignoreCase ? "i" : "");
let parts = "["
let parts = "["
+ node.parts.map(part =>
Array.isArray(part)
? "[\"" + js.stringEscape(part[0]) + "\", \"" + js.stringEscape(part[1]) + "\"]"
: "\"" + js.stringEscape(part) + "\""
).join(", ")
+ "]";
let regexpIndex = addConst(regexp);
let regexpIndex = addConst(regexp);
let expectedIndex = addConst(
"peg$classExpectation("
+ parts + ", "

90
lib/compiler/passes/generate-js.js

@ -1,8 +1,8 @@
"use strict";
let asts = require("../asts");
let op = require("../opcodes");
let js = require("../js");
let asts = require("../asts");
let op = require("../opcodes");
let js = require("../js");
// Generates parser JavaScript code.
function generateJS(ast, options) {
@ -50,7 +50,7 @@ function generateJS(ast, options) {
if (options.cache) {
parts.push([
"var key = peg$currPos * " + ast.rules.length + " + " + ruleIndexCode + ";",
"var key = peg$currPos * " + ast.rules.length + " + " + ruleIndexCode + ";",
"var cached = peg$resultsCache[key];",
"",
"if (cached) {",
@ -130,7 +130,7 @@ function generateJS(ast, options) {
let parts = [];
function generateCondition(cond, argsLength) {
let baseLength = argsLength + 3;
let baseLength = argsLength + 3;
let thenLengthCode = "bc[ip + " + (baseLength - 2) + "]";
let elseLengthCode = "bc[ip + " + (baseLength - 1) + "]";
@ -151,7 +151,7 @@ function generateJS(ast, options) {
}
function generateLoop(cond) {
let baseLength = 2;
let baseLength = 2;
let bodyLengthCode = "bc[ip + " + (baseLength - 1) + "]";
return [
@ -170,7 +170,7 @@ function generateJS(ast, options) {
}
function generateCall() {
let baseLength = 4;
let baseLength = 4;
let paramsLengthCode = "bc[ip + " + (baseLength - 1) + "]";
return [
@ -198,22 +198,22 @@ function generateJS(ast, options) {
if (options.trace) {
parts.push([
" var bc = peg$bytecode[index];",
" var ip = 0;",
" var ips = [];",
" var end = bc.length;",
" var ends = [];",
" var stack = [];",
" var bc = peg$bytecode[index];",
" var ip = 0;",
" var ips = [];",
" var end = bc.length;",
" var ends = [];",
" var stack = [];",
" var startPos = peg$currPos;",
" var params;"
].join("\n"));
} else {
parts.push([
" var bc = peg$bytecode[index];",
" var ip = 0;",
" var ips = [];",
" var end = bc.length;",
" var ends = [];",
" var bc = peg$bytecode[index];",
" var ip = 0;",
" var ips = [];",
" var end = bc.length;",
" var ends = [];",
" var stack = [];",
" var params;"
].join("\n"));
@ -403,7 +403,7 @@ function generateJS(ast, options) {
}
function generateRuleFunction(rule) {
let parts = [];
let parts = [];
let stackVars = [];
let code;
@ -448,8 +448,8 @@ function generateJS(ast, options) {
};
function compile(bc) {
let ip = 0;
let end = bc.length;
let ip = 0;
let end = bc.length;
let parts = [];
let value;
@ -457,7 +457,7 @@ function generateJS(ast, options) {
let baseLength = argCount + 3;
let thenLength = bc[ip + baseLength - 2];
let elseLength = bc[ip + baseLength - 1];
let baseSp = stack.sp;
let baseSp = stack.sp;
let thenCode, elseCode, thenSp, elseSp;
ip += baseLength;
@ -490,7 +490,7 @@ function generateJS(ast, options) {
function compileLoop(cond) {
let baseLength = 2;
let bodyLength = bc[ip + baseLength - 1];
let baseSp = stack.sp;
let baseSp = stack.sp;
let bodyCode, bodySp;
ip += baseLength;
@ -508,7 +508,7 @@ function generateJS(ast, options) {
}
function compileCall() {
let baseLength = 4;
let baseLength = 4;
let paramsLength = bc[ip + baseLength - 1];
let value = c(bc[ip + 1]) + "("
@ -752,11 +752,11 @@ function generateJS(ast, options) {
"}",
"",
"function peg$SyntaxError(message, expected, found, location) {",
" this.message = message;",
" this.message = message;",
" this.expected = expected;",
" this.found = found;",
" this.found = found;",
" this.location = location;",
" this.name = \"SyntaxError\";",
" this.name = \"SyntaxError\";",
"",
" if (typeof Error.captureStackTrace === \"function\") {",
" Error.captureStackTrace(this, peg$SyntaxError);",
@ -943,7 +943,7 @@ function generateJS(ast, options) {
parts.push([
" var peg$startRuleIndices = " + startRuleIndices + ";",
" var peg$startRuleIndex = " + startRuleIndex + ";"
" var peg$startRuleIndex = " + startRuleIndex + ";"
].join("\n"));
} else {
let startRuleFunctions = "{ "
@ -955,7 +955,7 @@ function generateJS(ast, options) {
parts.push([
" var peg$startRuleFunctions = " + startRuleFunctions + ";",
" var peg$startRuleFunction = " + startRuleFunction + ";"
" var peg$startRuleFunction = " + startRuleFunction + ";"
].join("\n"));
}
@ -965,12 +965,12 @@ function generateJS(ast, options) {
parts.push([
"",
" var peg$currPos = 0;",
" var peg$savedPos = 0;",
" var peg$posDetailsCache = [{ line: 1, column: 1 }];",
" var peg$maxFailPos = 0;",
" var peg$maxFailExpected = [];",
" var peg$silentFails = 0;", // 0 = report failures, > 0 = silence failures
" var peg$currPos = 0;",
" var peg$savedPos = 0;",
" var peg$posDetailsCache = [{ line: 1, column: 1 }];",
" var peg$maxFailPos = 0;",
" var peg$maxFailExpected = [];",
" var peg$silentFails = 0;", // 0 = report failures, > 0 = silence failures
""
].join("\n"));
@ -1110,7 +1110,7 @@ function generateJS(ast, options) {
"",
" function peg$computeLocation(startPos, endPos) {",
" var startPosDetails = peg$computePosDetails(startPos);",
" var endPosDetails = peg$computePosDetails(endPos);",
" var endPosDetails = peg$computePosDetails(endPos);",
"",
" return {",
" start: {",
@ -1237,7 +1237,7 @@ function generateJS(ast, options) {
},
commonjs: function() {
let parts = [];
let parts = [];
let dependencyVars = Object.keys(options.dependencies);
parts.push([
@ -1270,13 +1270,13 @@ function generateJS(ast, options) {
amd: function() {
let dependencyVars = Object.keys(options.dependencies);
let dependencyIds = dependencyIds.map(v => options.dependencies[v]);
let dependencies = "["
let dependencyIds = dependencyIds.map(v => options.dependencies[v]);
let dependencies = "["
+ dependencyIds.map(
id => "\"" + js.stringEscape(id) + "\""
).join(", ")
+ "]";
let params = dependencyVars.join(", ");
let params = dependencyVars.join(", ");
return [
generateGeneratedByComment(),
@ -1306,18 +1306,18 @@ function generateJS(ast, options) {
},
umd: function() {
let parts = [];
let parts = [];
let dependencyVars = Object.keys(options.dependencies);
let dependencyIds = dependencyIds.map(v => options.dependencies[v]);
let dependencies = "["
let dependencyIds = dependencyIds.map(v => options.dependencies[v]);
let dependencies = "["
+ dependencyIds.map(
id => "\"" + js.stringEscape(id) + "\""
).join(", ")
+ "]";
let requires = dependencyIds.map(
let requires = dependencyIds.map(
id => "require(\"" + js.stringEscape(id) + "\")"
).join(", ");
let params = dependencyVars.join(", ");
let params = dependencyVars.join(", ");
parts.push([
generateGeneratedByComment(),

2
lib/compiler/passes/report-duplicate-labels.js

@ -1,7 +1,7 @@
"use strict";
let GrammarError = require("../../grammar-error");
let visitor = require("../visitor");
let visitor = require("../visitor");
// Checks that each label is defined only once within each scope.
function reportDuplicateLabels(ast) {

2
lib/compiler/passes/report-duplicate-rules.js

@ -1,7 +1,7 @@
"use strict";
let GrammarError = require("../../grammar-error");
let visitor = require("../visitor");
let visitor = require("../visitor");
// Checks that each rule is defined only once.
function reportDuplicateRules(ast) {

4
lib/compiler/passes/report-infinite-recursion.js

@ -1,8 +1,8 @@
"use strict";
let GrammarError = require("../../grammar-error");
let asts = require("../asts");
let visitor = require("../visitor");
let asts = require("../asts");
let visitor = require("../visitor");
// Reports left recursion in the grammar, which prevents infinite recursion in
// the generated parser.

4
lib/compiler/passes/report-infinite-repetition.js

@ -1,8 +1,8 @@
"use strict";
let GrammarError = require("../../grammar-error");
let asts = require("../asts");
let visitor = require("../visitor");
let asts = require("../asts");
let visitor = require("../visitor");
// Reports expressions that don't consume any input inside |*| or |+| in the
// grammar, which prevents infinite loops in the generated parser.

4
lib/compiler/passes/report-undefined-rules.js

@ -1,8 +1,8 @@
"use strict";
let GrammarError = require("../../grammar-error");
let asts = require("../asts");
let visitor = require("../visitor");
let asts = require("../asts");
let visitor = require("../visitor");
// Checks that all referenced rules exist.
function reportUndefinedRules(ast) {

22
lib/parser.js

@ -14,11 +14,11 @@ function peg$subclass(child, parent) {
}
function peg$SyntaxError(message, expected, found, location) {
this.message = message;
this.message = message;
this.expected = expected;
this.found = found;
this.found = found;
this.location = location;
this.name = "SyntaxError";
this.name = "SyntaxError";
if (typeof Error.captureStackTrace === "function") {
Error.captureStackTrace(this, peg$SyntaxError);
@ -133,7 +133,7 @@ function peg$parse(input, options) {
var peg$FAILED = {};
var peg$startRuleFunctions = { Grammar: peg$parseGrammar };
var peg$startRuleFunction = peg$parseGrammar;
var peg$startRuleFunction = peg$parseGrammar;
var peg$c0 = function(initializer, rules) {
return {
@ -484,12 +484,12 @@ function peg$parse(input, options) {
var peg$c238 = ";";
var peg$c239 = peg$literalExpectation(";", false);
var peg$currPos = 0;
var peg$savedPos = 0;
var peg$posDetailsCache = [{ line: 1, column: 1 }];
var peg$maxFailPos = 0;
var peg$maxFailExpected = [];
var peg$silentFails = 0;
var peg$currPos = 0;
var peg$savedPos = 0;
var peg$posDetailsCache = [{ line: 1, column: 1 }];
var peg$maxFailPos = 0;
var peg$maxFailExpected = [];
var peg$silentFails = 0;
var peg$result;
@ -581,7 +581,7 @@ function peg$parse(input, options) {
function peg$computeLocation(startPos, endPos) {
var startPosDetails = peg$computePosDetails(startPos);
var endPosDetails = peg$computePosDetails(endPos);
var endPosDetails = peg$computePosDetails(endPos);
return {
start: {

2
lib/peg.js

@ -32,7 +32,7 @@ let peg = {
}
let plugins = "plugins" in options ? options.plugins : [];
let config = {
let config = {
parser: peg.parser,
passes: convertPasses(peg.compiler.passes)
};

2
spec/api/pegjs-api.spec.js

@ -84,7 +84,7 @@ describe("PEG.js API", function() {
let grammar = [
"{ var n = 0; }",
"start = (a 'b') / (a 'c') { return n; }",
"a = 'a' { n++; }"
"a = 'a' { n++; }"
].join("\n");
describe("when |cache| is not set", function() {

8
spec/api/plugin-api.spec.js

@ -39,7 +39,7 @@ describe("plugin API", function() {
it("is called for each plugin", function() {
let pluginsUsed = [false, false, false];
let plugins = [
let plugins = [
{ use: function() { pluginsUsed[0] = true; } },
{ use: function() { pluginsUsed[1] = true; } },
{ use: function() { pluginsUsed[2] = true; } }
@ -81,7 +81,7 @@ describe("plugin API", function() {
});
it("receives options", function() {
let plugin = {
let plugin = {
use: function(config, options) {
expect(options).toEqual(generateOptions);
}
@ -138,12 +138,12 @@ describe("plugin API", function() {
"b = 'x'",
"c = 'x'"
].join("\n");
let plugin = {
let plugin = {
use: function(config, options) {
options.allowedStartRules = ["b", "c"];
}
};
let parser = peg.generate(grammar, {
let parser = peg.generate(grammar, {
allowedStartRules: ["a"],
plugins: [plugin]
});

72
spec/behavior/generated-parser-behavior.spec.js

@ -575,12 +575,12 @@ describe("generated parser behavior", function() {
it("|location| returns current location info", function() {
let parser = peg.generate([
"{ var result; }",
"start = line (nl+ line)* { return result; }",
"line = thing (' '+ thing)*",
"thing = digit / mark",
"digit = [0-9]",
"mark = &{ result = location(); return true; } 'x'",
"nl = '\\r'? '\\n'"
"start = line (nl+ line)* { return result; }",
"line = thing (' '+ thing)*",
"thing = digit / mark",
"digit = [0-9]",
"mark = &{ result = location(); return true; } 'x'",
"nl = '\\r'? '\\n'"
].join("\n"), options);
expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", {
@ -769,12 +769,12 @@ describe("generated parser behavior", function() {
it("|location| returns current location info", function() {
let parser = peg.generate([
"{ var result; }",
"start = line (nl+ line)* { return result; }",
"line = thing (' '+ thing)*",
"thing = digit / mark",
"digit = [0-9]",
"mark = !{ result = location(); return false; } 'x'",
"nl = '\\r'? '\\n'"
"start = line (nl+ line)* { return result; }",
"line = thing (' '+ thing)*",
"thing = digit / mark",
"digit = [0-9]",
"mark = !{ result = location(); return false; } 'x'",
"nl = '\\r'? '\\n'"
].join("\n"), options);
expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", {
@ -1147,12 +1147,12 @@ describe("generated parser behavior", function() {
it("|location| returns location info of the expression", function() {
let parser = peg.generate([
"{ var result; }",
"start = line (nl+ line)* { return result; }",
"line = thing (' '+ thing)*",
"thing = digit / mark",
"digit = [0-9]",
"mark = 'x' { result = location(); }",
"nl = '\\r'? '\\n'"
"start = line (nl+ line)* { return result; }",
"line = thing (' '+ thing)*",
"thing = digit / mark",
"digit = [0-9]",
"mark = 'x' { result = location(); }",
"nl = '\\r'? '\\n'"
].join("\n"), options);
expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", {
@ -1441,10 +1441,10 @@ describe("generated parser behavior", function() {
it("reports position correctly in complex cases", function() {
let parser = peg.generate([
"start = line (nl+ line)*",
"line = digit (' '+ digit)*",
"digit = [0-9]",
"nl = '\\r'? '\\n'"
"start = line (nl+ line)*",
"line = digit (' '+ digit)*",
"digit = [0-9]",
"nl = '\\r'? '\\n'"
].join("\n"), options);
expect(parser).toFailToParse("1\n2\n\n3\n\n\n4 5 x", {
@ -1480,21 +1480,21 @@ describe("generated parser behavior", function() {
// Sum ← Product (('+' / '-') Product)*
// Expr ← Sum
let parser = peg.generate([
"Expr = Sum",
"Sum = head:Product tail:(('+' / '-') Product)* {",
" return tail.reduce(function(result, element) {",
" if (element[0] === '+') { return result + element[1]; }",
" if (element[0] === '-') { return result - element[1]; }",
" }, head);",
" }",
"Expr = Sum",
"Sum = head:Product tail:(('+' / '-') Product)* {",
" return tail.reduce(function(result, element) {",
" if (element[0] === '+') { return result + element[1]; }",
" if (element[0] === '-') { return result - element[1]; }",
" }, head);",
" }",
"Product = head:Value tail:(('*' / '/') Value)* {",
" return tail.reduce(function(result, element) {",
" if (element[0] === '*') { return result * element[1]; }",
" if (element[0] === '/') { return result / element[1]; }",
" }, head);",
" }",
"Value = digits:[0-9]+ { return parseInt(digits.join(''), 10); }",
" / '(' expr:Expr ')' { return expr; }"
"Value = digits:[0-9]+ { return parseInt(digits.join(''), 10); }",
" / '(' expr:Expr ')' { return expr; }"
].join("\n"), options);
// The "value" rule
@ -1553,12 +1553,12 @@ describe("generated parser behavior", function() {
// N ← C / (!Begin !End Z)
// Z ← any single character
let parser = peg.generate([
"C = begin:Begin ns:N* end:End { return begin + ns.join('') + end; }",
"N = C",
" / !Begin !End z:Z { return z; }",
"Z = .",
"C = begin:Begin ns:N* end:End { return begin + ns.join('') + end; }",
"N = C",
" / !Begin !End z:Z { return z; }",
"Z = .",
"Begin = '(*'",
"End = '*)'"
"End = '*)'"
].join("\n"), options);
expect(parser).toParse("(**)", "(**)");

2
spec/index.html

@ -9,7 +9,7 @@
<script src="bundle.js"></script>
<script>
(function() {
var env = jasmine.getEnv(),
var env = jasmine.getEnv(),
reporter = new jasmine.HtmlReporter();
env.addReporter(reporter);

8
spec/server

@ -9,11 +9,11 @@
// the specs get passed through Babel & Browserify before they are served to the
// browser.
let express = require("express");
let logger = require("morgan");
let glob = require("glob");
let express = require("express");
let logger = require("morgan");
let glob = require("glob");
let browserify = require("browserify");
let babelify = require("babelify");
let babelify = require("babelify");
let app = express();

6
spec/unit/compiler/passes/report-infinite-recursion.spec.js

@ -18,12 +18,12 @@ describe("compiler pass |reportInfiniteRecursion|", function() {
it("reports indirect left recursion", function() {
expect(pass).toReportError([
"start = stop",
"stop = start"
"stop = start"
].join("\n"), {
message: "Possible infinite loop when parsing (left recursion: start -> stop -> start).",
location: {
start: { offset: 21, line: 2, column: 9 },
end: { offset: 26, line: 2, column: 14 }
start: { offset: 20, line: 2, column: 8 },
end: { offset: 25, line: 2, column: 13 }
}
});
});

Loading…
Cancel
Save