From e8be76ee3a2631f253e3628ea3441f395e79bcce Mon Sep 17 00:00:00 2001 From: David Majda Date: Mon, 4 Jul 2016 08:17:00 +0200 Subject: [PATCH] Don't expose the "parser" variable in parser code The "parser" variable allowed access to the parser object. Among other things, this made it possible to invoke the parser recursively using "parser.parse". One problem with the "parser" variable is that it bakes in the idea that the parser is an *object*, not a *module*. While this is true now, it won't necessarily be in the future, when parsers may be exported as ES6 modules. Also, people tend to use parsers as modules even today, e.g. like this: var parse = require("parser").parse; var result = parse(...); Such usage broke the "parser" variable (as it was implemented). For this reasons I decided to remove the "parser" variable. If someone needs to do tricks like recursive invocation of the parser, he/she must pass the parser or the "parse" function itself using options. Related to #433. --- README.md | 19 +++++----- lib/compiler/passes/generate-js.js | 4 +-- .../generated-parser-behavior.spec.js | 36 ------------------- 3 files changed, 10 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index d97f9d3..06d78a7 100644 --- a/README.md +++ b/README.md @@ -227,10 +227,9 @@ The first rule can be preceded by an *initializer* — a piece of JavaScript cod in curly braces (“{” and “}”). This code is executed before the generated parser starts parsing. All variables and functions defined in the initializer are accessible in rule actions and semantic predicates. The code inside the -initializer can access the parser object using the `parser` variable and options -passed to the parser using the `options` variable. Curly braces in the -initializer code must be balanced. Let's look at the example grammar from above -using a simple initializer. +initializer can access options passed to the parser using the `options` +variable. Curly braces in the initializer code must be balanced. Let's look at +the example grammar from above using a simple initializer. ```pegjs { @@ -376,8 +375,8 @@ The `start` and `end` properties both refer to the current parse position. The `offset` property contains an offset as a zero-based index and `line` and `column` properties contain a line and a column as one-based indices. -The code inside the predicate can also access the parser object using the -`parser` variable and options passed to the parser using the `options` variable. +The code inside the predicate can also access options passed to the parser using +the `options` variable. Note that curly braces in the predicate code must be balanced. @@ -407,8 +406,8 @@ The `start` and `end` properties both refer to the current parse position. The `offset` property contains an offset as a zero-based index and `line` and `column` properties contain a line and a column as one-based indices. -The code inside the predicate can also access the parser object using the -`parser` variable and options passed to the parser using the `options` variable. +The code inside the predicate can also access options passed to the parser using +the `options` variable. Note that curly braces in the predicate code must be balanced. @@ -475,8 +474,8 @@ the `end` property refers to position after the end of the expression. The `offset` property contains an offset as a zero-based index and `line` and `column` properties contain a line and a column as one-based indices. -The code inside the action can also access the parser object using the `parser` -variable and options passed to the parser using the `options` variable. +The code inside the action can also access options passed to the parser using +the `options` variable. Note that curly braces in the action code must be balanced. diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index a03a2cc..7f5c9fd 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -953,9 +953,7 @@ function generateJS(ast, options) { 'function peg$parse(input, options) {', ' options = options !== void 0 ? options : {};', '', - ' var parser = this,', - '', - ' peg$FAILED = {},', + ' var peg$FAILED = {},', '' ].join('\n')); diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index 0dc9a39..c4c5881 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/spec/behavior/generated-parser-behavior.spec.js @@ -133,15 +133,6 @@ describe("generated parser behavior", function() { }); describe("available variables and functions", function() { - it("|parser| contains the parser object", function() { - var parser = peg.generate([ - '{ var result = parser; }', - 'start = "a" { return result; }' - ].join("\n"), options); - - expect(parser).toParse("a", parser); - }); - it("|options| contains options", function() { var parser = peg.generate([ '{ var result = options; }', @@ -568,15 +559,6 @@ describe("generated parser behavior", function() { }); describe("available variables & functions", function() { - it("|parser| contains the parser object", function() { - var parser = peg.generate([ - '{ var result; }', - 'start = &{ result = parser; return true; } { return result; }' - ].join("\n"), options); - - expect(parser).toParse("", parser); - }); - it("|options| contains options", function() { var parser = peg.generate([ '{ var result; }', @@ -773,15 +755,6 @@ describe("generated parser behavior", function() { }); describe("available variables & functions", function() { - it("|parser| contains the parser object", function() { - var parser = peg.generate([ - '{ var result; }', - 'start = !{ result = parser; return false; } { return result; }' - ].join("\n"), options); - - expect(parser).toParse("", parser); - }); - it("|options| contains options", function() { var parser = peg.generate([ '{ var result; }', @@ -1151,15 +1124,6 @@ describe("generated parser behavior", function() { }); describe("available variables & functions", function() { - it("|parser| contains the parser object", function() { - var parser = peg.generate( - 'start = "a" { return parser; }', - options - ); - - expect(parser).toParse("a", parser); - }); - it("|options| contains options", function() { var parser = peg.generate( 'start = "a" { return options; }',