From ff7193776e62d451b740b391a803246d89d31108 Mon Sep 17 00:00:00 2001 From: David Majda Date: Thu, 22 Sep 2016 09:25:31 +0200 Subject: [PATCH] Avoid aligning "=" The only exception left are instances where aligning "=" helps to express symmetry between lines. See #443. --- benchmark/index.js | 6 +- benchmark/run | 4 +- benchmark/server | 8 +- bin/pegjs | 6 +- lib/compiler/passes/generate-bytecode.js | 20 ++--- lib/compiler/passes/generate-js.js | 90 +++++++++---------- .../passes/report-duplicate-labels.js | 2 +- lib/compiler/passes/report-duplicate-rules.js | 2 +- .../passes/report-infinite-recursion.js | 4 +- .../passes/report-infinite-repetition.js | 4 +- lib/compiler/passes/report-undefined-rules.js | 4 +- lib/parser.js | 22 ++--- lib/peg.js | 2 +- spec/api/pegjs-api.spec.js | 2 +- spec/api/plugin-api.spec.js | 8 +- .../generated-parser-behavior.spec.js | 72 +++++++-------- spec/index.html | 2 +- spec/server | 8 +- .../passes/report-infinite-recursion.spec.js | 6 +- 19 files changed, 136 insertions(+), 136 deletions(-) diff --git a/benchmark/index.js b/benchmark/index.js index ecd7b47..137fb5a 100644 --- a/benchmark/index.js +++ b/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() }; diff --git a/benchmark/run b/benchmark/run index 79caf7f..90ebc6f 100755 --- a/benchmark/run +++ b/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("│ " diff --git a/benchmark/server b/benchmark/server index 2e3d296..b5bbf0b 100755 --- a/benchmark/server +++ b/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(); diff --git a/bin/pegjs b/bin/pegjs index e5eb792..8554e84 100755 --- a/bin/pegjs +++ b/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 diff --git a/lib/compiler/passes/generate-bytecode.js b/lib/compiler/passes/generate-bytecode.js index 774cf9a..2204049 100644 --- a/lib/compiler/passes/generate-bytecode.js +++ b/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 + ", " diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 8d37f6f..ac502b3 100644 --- a/lib/compiler/passes/generate-js.js +++ b/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(), diff --git a/lib/compiler/passes/report-duplicate-labels.js b/lib/compiler/passes/report-duplicate-labels.js index 5bb22d6..c3f4da4 100644 --- a/lib/compiler/passes/report-duplicate-labels.js +++ b/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) { diff --git a/lib/compiler/passes/report-duplicate-rules.js b/lib/compiler/passes/report-duplicate-rules.js index a4e764a..73ca831 100644 --- a/lib/compiler/passes/report-duplicate-rules.js +++ b/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) { diff --git a/lib/compiler/passes/report-infinite-recursion.js b/lib/compiler/passes/report-infinite-recursion.js index ac65cdb..229f0d8 100644 --- a/lib/compiler/passes/report-infinite-recursion.js +++ b/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. diff --git a/lib/compiler/passes/report-infinite-repetition.js b/lib/compiler/passes/report-infinite-repetition.js index f6766ea..088a596 100644 --- a/lib/compiler/passes/report-infinite-repetition.js +++ b/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. diff --git a/lib/compiler/passes/report-undefined-rules.js b/lib/compiler/passes/report-undefined-rules.js index a2430f3..9fd8276 100644 --- a/lib/compiler/passes/report-undefined-rules.js +++ b/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) { diff --git a/lib/parser.js b/lib/parser.js index 4df66a2..4090398 100644 --- a/lib/parser.js +++ b/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: { diff --git a/lib/peg.js b/lib/peg.js index 28b628c..1557ba2 100644 --- a/lib/peg.js +++ b/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) }; diff --git a/spec/api/pegjs-api.spec.js b/spec/api/pegjs-api.spec.js index 1843d23..047ff91 100644 --- a/spec/api/pegjs-api.spec.js +++ b/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() { diff --git a/spec/api/plugin-api.spec.js b/spec/api/plugin-api.spec.js index 21c3c54..4c0b1ee 100644 --- a/spec/api/plugin-api.spec.js +++ b/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] }); diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index 08b2b04..8da9b64 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/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("(**)", "(**)"); diff --git a/spec/index.html b/spec/index.html index a0734bd..a643290 100644 --- a/spec/index.html +++ b/spec/index.html @@ -9,7 +9,7 @@