From c6e8c53f1b8bc7178bf9afed56251c9c1956f9eb Mon Sep 17 00:00:00 2001 From: David Majda Date: Mon, 13 Jun 2016 11:28:32 +0200 Subject: [PATCH] Expectation refactoring 2/7: Restructure "class" expectations Changes: * Remove the "value" property (it is replaced by other properties). * Add the "parts", "inverted", and "ignoreCase" properties (which allow more structured access to expectation data). --- lib/compiler/passes/generate-bytecode.js | 14 ++++++++++++-- spec/behavior/generated-parser-behavior.spec.js | 2 +- .../unit/compiler/passes/generate-bytecode.spec.js | 12 ++++++------ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/compiler/passes/generate-bytecode.js b/lib/compiler/passes/generate-bytecode.js index 06fc78f..b009b92 100644 --- a/lib/compiler/passes/generate-bytecode.js +++ b/lib/compiler/passes/generate-bytecode.js @@ -603,7 +603,7 @@ function generateBytecode(ast) { }, "class": function(node) { - var regexp, description, regexpIndex, expectedIndex; + var regexp, parts, description, regexpIndex, expectedIndex; if (node.parts.length > 0) { regexp = '/^[' @@ -624,6 +624,14 @@ function generateBytecode(ast) { regexp = node.inverted ? '/^[\\S\\s]/' : '/^(?!)/'; } + parts = '[' + + arrays.map(node.parts, function(part) { + return part instanceof Array + ? '["' + js.stringEscape(part[0]) + '", "' + js.stringEscape(part[1]) + '"]' + : '"' + js.stringEscape(part) + '"'; + }).join(', ') + + ']'; + description = "[" + (node.inverted ? "^" : "") + arrays.map(node.parts, function(part) { @@ -639,7 +647,9 @@ function generateBytecode(ast) { expectedIndex = addConst([ '{', 'type: "class",', - 'value: "' + js.stringEscape(node.rawText) + '",', + 'parts: ' + parts + ',', + 'inverted: ' + node.inverted + ',', + 'ignoreCase: ' + node.ignoreCase + ',', 'description: "' + js.stringEscape(description) + '"', '}' ].join(' ')); diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index cb2955c..24975a2 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/spec/behavior/generated-parser-behavior.spec.js @@ -344,7 +344,7 @@ describe("generated parser behavior", function() { var parser = peg.generate('start = [a]', options); expect(parser).toFailToParse("b", { - expected: [{ type: "class", value: "[a]", description: "[a]" }] + expected: [{ type: "class", parts: ["a"], inverted: false, ignoreCase: false, description: "[a]" }] }); }); }); diff --git a/spec/unit/compiler/passes/generate-bytecode.spec.js b/spec/unit/compiler/passes/generate-bytecode.spec.js index 0e8ebfa..a73effd 100644 --- a/spec/unit/compiler/passes/generate-bytecode.spec.js +++ b/spec/unit/compiler/passes/generate-bytecode.spec.js @@ -598,7 +598,7 @@ describe("compiler pass |generateBytecode|", function() { it("defines correct constants", function() { expect(pass).toChangeAST('start = [a]', constsDetails([ '/^[a]/', - '{ type: "class", value: "[a]", description: "[a]" }' + '{ type: "class", parts: ["a"], inverted: false, ignoreCase: false, description: "[a]" }' ])); }); }); @@ -607,7 +607,7 @@ describe("compiler pass |generateBytecode|", function() { it("defines correct constants", function() { expect(pass).toChangeAST('start = [^a]', constsDetails([ '/^[^a]/', - '{ type: "class", value: "[^a]", description: "[^a]" }' + '{ type: "class", parts: ["a"], inverted: true, ignoreCase: false, description: "[^a]" }' ])); }); }); @@ -616,7 +616,7 @@ describe("compiler pass |generateBytecode|", function() { it("defines correct constants", function() { expect(pass).toChangeAST('start = [a]i', constsDetails([ '/^[a]/i', - '{ type: "class", value: "[a]i", description: "[a]" }' + '{ type: "class", parts: ["a"], inverted: false, ignoreCase: true, description: "[a]" }' ])); }); }); @@ -625,7 +625,7 @@ describe("compiler pass |generateBytecode|", function() { it("defines correct constants", function() { expect(pass).toChangeAST('start = [ab-def-hij-l]', constsDetails([ '/^[ab-def-hij-l]/', - '{ type: "class", value: "[ab-def-hij-l]", description: "[ab-def-hij-l]" }' + '{ type: "class", parts: ["a", ["b", "d"], "e", ["f", "h"], "i", ["j", "l"]], inverted: false, ignoreCase: false, description: "[ab-def-hij-l]" }' ])); }); }); @@ -634,7 +634,7 @@ describe("compiler pass |generateBytecode|", function() { it("defines correct constants", function() { expect(pass).toChangeAST('start = []', constsDetails([ '/^(?!)/', - '{ type: "class", value: "[]", description: "[]" }' + '{ type: "class", parts: [], inverted: false, ignoreCase: false, description: "[]" }' ])); }); }); @@ -643,7 +643,7 @@ describe("compiler pass |generateBytecode|", function() { it("defines correct constants", function() { expect(pass).toChangeAST('start = [^]', constsDetails([ '/^[\\S\\s]/', - '{ type: "class", value: "[^]", description: "[^]" }' + '{ type: "class", parts: [], inverted: true, ignoreCase: false, description: "[^]" }' ])); }); });