From a16f8d760a3c454717fd92b1b373d034cb4ebea7 Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 26 Jul 2021 00:00:06 +0500 Subject: [PATCH] source-map: Add mapping for the rule names to the names of the parse functions --- lib/compiler/passes/generate-js.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 48e6bd1a..a54340ed 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -127,7 +127,11 @@ function wrapInSourceNode(prefix, chunk, location, suffix, name) { // Generates parser JavaScript code. function generateJS(ast, options) { // These only indent non-empty lines to avoid trailing whitespace. - function indent2(code) { return code.replace(/^(.+)$/gm, " $1"); } + function indent2(code) { + return code instanceof SourceNode + ? code + : code.replace(/^(.+)$/gm, " $1"); + } function l(i) { return "peg$c" + i; } // |literals[i]| of the abstract machine function r(i) { return "peg$r" + i; } // |classes[i]| of the abstract machine @@ -578,7 +582,13 @@ function generateJS(ast, options) { const code = compile(rule.bytecode); - parts.push("function " + name(rule.name) + "() {"); + parts.push(wrapInSourceNode( + " function ", + name(rule.name), + rule.nameLocation, + "() {\n", + rule.name + )); if (options.trace) { parts.push(" var startPos = peg$currPos;");