Skip to content

Commit

Permalink
source-map: Introduce helper for generate a parse rule function name
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Jul 25, 2021
1 parent 1aabf07 commit d2fabaf
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/compiler/passes/generate-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ function generateJS(ast, options) {
function e(i) { return "peg$e" + i; } // |expectations[i]| of the abstract machine
function f(i) { return "peg$f" + i; } // |actions[i]| of the abstract machine

/** Generates name of the function that parses specified rule. */
function name(name) { return "peg$parse" + name; }

function generateTables() {
function buildLiteral(literal) {
return "\"" + stringEscape(literal) + "\"";
Expand Down Expand Up @@ -551,7 +554,7 @@ function generateJS(ast, options) {
break;

case op.RULE: // RULE r
parts.push(stack.push("peg$parse" + ast.rules[bc[ip + 1]].name + "()"));
parts.push(stack.push(name(ast.rules[bc[ip + 1]].name) + "()"));
ip += 2;
break;

Expand All @@ -575,7 +578,7 @@ function generateJS(ast, options) {

const code = compile(rule.bytecode);

parts.push("function peg$parse" + rule.name + "() {");
parts.push("function " + name(rule.name) + "() {");

if (options.trace) {
parts.push(" var startPos = peg$currPos;");
Expand Down Expand Up @@ -840,10 +843,10 @@ function generateJS(ast, options) {

const startRuleFunctions = "{ "
+ options.allowedStartRules.map(
r => r + ": peg$parse" + r
r => r + ": " + name(r)
).join(", ")
+ " }";
const startRuleFunction = "peg$parse" + options.allowedStartRules[0];
const startRuleFunction = name(options.allowedStartRules[0]);

parts.push(
"function peg$parse(input, options) {",
Expand Down

0 comments on commit d2fabaf

Please sign in to comment.