diff --git a/test/unit/compiler/passes/generate-bytecode.spec.js b/test/unit/compiler/passes/generate-bytecode.spec.js index 10130c64..57615520 100644 --- a/test/unit/compiler/passes/generate-bytecode.spec.js +++ b/test/unit/compiler/passes/generate-bytecode.spec.js @@ -15,6 +15,13 @@ describe("compiler pass |generateBytecode|", () => { }; } + function bytecodeLocationDetails(bytecode, locations) { + return { + rules: [{ bytecode }], + locations, + }; + } + function constsDetails(literals, classes, expectations, functions) { return { literals, classes, expectations, functions }; } @@ -51,6 +58,24 @@ describe("compiler pass |generateBytecode|", () => { )); }); + it("generates correct source mapping", () => { + expect(pass).to.changeAST([ + "a = 'a'", + ].join("\n"), bytecodeLocationDetails( + [37, 0, 18, 0, 2, 2, 22, 0, 23, 0, 38], + [ + { + source: "-", + start: { offset: 4, line: 1, column: 5 }, + end: { offset: 7, line: 1, column: 8 }, + }, + ] + ), { + grammarSource: "-", + output: "source-and-map", + }); + }); + it("generates correct plucking bytecode", () => { expect(pass).to.changeAST("start = 'a' @'b' 'c'", bytecodeDetails([ 5, // PUSH_CURR_POS @@ -129,16 +154,48 @@ describe("compiler pass |generateBytecode|", () => { }); describe("for choice", () => { - it("generates correct bytecode", () => { - expect(pass).to.changeAST("start = 'a' / 'b' / 'c'", bytecodeDetails([ + it("generates correct bytecode and source mapping", () => { + expect(pass).to.changeAST("start = 'a' / 'b' / 'c'", bytecodeLocationDetails([ + 37, 3, // SOURCE_MAP_PUSH 3 + 37, 0, // SOURCE_MAP_PUSH 0 18, 0, 2, 2, 22, 0, 23, 0, // - 14, 21, 0, // IF_ERROR + 38, // SOURCE_MAP_POP + 14, 27, 0, // IF_ERROR 6, // * POP + 37, 1, // SOURCE_MAP_PUSH 1 18, 1, 2, 2, 22, 1, 23, 1, // - 14, 9, 0, // IF_ERROR + 38, // SOURCE_MAP_POP + 14, 12, 0, // IF_ERROR 6, // * POP - 18, 2, 2, 2, 22, 2, 23, 2, // - ])); + 37, 2, // SOURCE_MAP_PUSH 2 + 18, 2, 2, 2, 22, 2, 23, 2, // + 38, // SOURCE_MAP_POP + 38, // SOURCE_MAP_POP + ], [ + { + source: "-", + start: { offset: 8, line: 1, column: 9 }, + end: { offset: 11, line: 1, column: 12 }, + }, + { + source: "-", + start: { offset: 14, line: 1, column: 15 }, + end: { offset: 17, line: 1, column: 18 }, + }, + { + source: "-", + start: { offset: 20, line: 1, column: 21 }, + end: { offset: 23, line: 1, column: 24 }, + }, + { + source: "-", + start: { offset: 8, line: 1, column: 9 }, + end: { offset: 23, line: 1, column: 24 }, + }, + ]), { + grammarSource: "-", + output: "source-and-map", + }); }); }); diff --git a/test/unit/compiler/passes/helpers.js b/test/unit/compiler/passes/helpers.js index 0979b114..494dcfd3 100644 --- a/test/unit/compiler/passes/helpers.js +++ b/test/unit/compiler/passes/helpers.js @@ -46,7 +46,7 @@ module.exports = function(chai, utils) { } } - const ast = parser.parse(grammar); + const ast = parser.parse(grammar, options); utils.flag(this, "object")(ast, options, new Session({ error(stage, ...args) { throw new GrammarError(...args); },