Skip to content

Commit

Permalink
source-map: Add mapping for the rule names to the names of the parse …
Browse files Browse the repository at this point in the history
…functions
  • Loading branch information
Mingun committed Jul 25, 2021
1 parent d2fabaf commit a16f8d7
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/compiler/passes/generate-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;");
Expand Down

0 comments on commit a16f8d7

Please sign in to comment.