Skip to content

Commit

Permalink
(hide internals) Remove access form user code to pegjs internals. Thi…
Browse files Browse the repository at this point in the history
…s break some grammars, that need that,

for example, BerTLV or intendation-based parsers, that need access to `peg$currPos`.
  • Loading branch information
Mingun committed Jan 21, 2018
1 parent b68d239 commit d71ef95
Show file tree
Hide file tree
Showing 2 changed files with 360 additions and 297 deletions.
44 changes: 30 additions & 14 deletions lib/compiler/passes/generate-js.js
Expand Up @@ -64,20 +64,12 @@ function generateJS(ast, options) {
}
}

function buildFunc(a) {
return "function(" + a.params.join(", ") + ") {"
+ a.body
+ "}";
}

return ast.literals.map(
(c, i) => "var " + l(i) + " = " + buildLiteral(c) + ";"
).concat("", ast.classes.map(
(c, i) => "var " + r(i) + " = " + buildRegexp(c) + ";"
)).concat("", ast.expectations.map(
(c, i) => "var " + e(i) + " = " + buildExpectation(c) + ";"
)).concat("", ast.functions.map(
(c, i) => "var " + f(i) + " = " + buildFunc(c) + ";"
)).join("\n");
}

Expand Down Expand Up @@ -577,9 +569,32 @@ function generateJS(ast, options) {
return parts.join("\n");
}

function generateSandbox() {
let parts = [
"function peg$sandbox(input, options, text, offset, range, location, expected, error) {"
];

if (ast.initializer) {
parts.push(indent2(ast.initializer.code));
parts.push("");
}

parts.push(" return [");
ast.functions.forEach(f => {
parts.push(" function(" + f.params.join(", ") + ") {" + f.body + "},");
});
parts.push(" ];");

parts.push("}");

return parts.join("\n");
}

function generateToplevel() {
let parts = [];

parts.push(generateSandbox());

parts.push([
"function peg$subclass(child, parent) {",
" function C() { this.constructor = child; }",
Expand Down Expand Up @@ -985,19 +1000,20 @@ function generateJS(ast, options) {
" : peg$computeLocation(failPos, failPos)",
" );",
" }",
""
"",
" var peg$functions = peg$sandbox(input, options, text, offset, range, location, expected, error);",
"",
].join("\n"));

ast.functions.forEach((_, i) => {
parts.push(indent2("var " + f(i) + " = peg$functions[" + i + "];"));
});
parts.push("");
ast.rules.forEach(rule => {
parts.push(indent2(generateRuleFunction(rule)));
parts.push("");
});

if (ast.initializer) {
parts.push(indent2(ast.initializer.code));
parts.push("");
}

parts.push([
" peg$begin();",
" peg$result = peg$startRuleFunction();",
Expand Down

0 comments on commit d71ef95

Please sign in to comment.