Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an optional argument 'options' in parse() #37

Closed
ceymard opened this issue Aug 15, 2011 · 5 comments
Closed

Create an optional argument 'options' in parse() #37

ceymard opened this issue Aug 15, 2011 · 5 comments
Assignees
Labels
Milestone

Comments

@ceymard
Copy link

ceymard commented Aug 15, 2011

It could be useful to pass variables to the parse() method to customize the behaviour of the parser on-demand.

My proposition is to have the following signature : parse(input, startRule, options), with the following behaviour ;

if (options === undefined && startRule instanceof Object) {
    options = startRule; startRule = undefined;
}

Or something of the like.

Right now, I believe the only way to do so is to have var options = arguments[2] || {};, which is not very elegant. You also have to generate the module that way to be able to call parse without the full parse(input, undefined, options) ;

grammar_source = "module.exports = #{parser.toSource()}; var _parse = module.exports.parse; module.exports.parse = function (input, startRule, options) {
        if (startRule instanceof Object) { options = startRule; startRule = undefined; }
        return _parse (input, startRule, options);
    };"
@dmajda
Copy link
Contributor

dmajda commented Aug 20, 2011

While probably useful to you. I am not entirely convinced this is needed so much to warrant inclusion into PEG.js. Is there any other parser generator you know about that has this feature?

Also, I'd probably have the signature be just parse(input, options). The options param would contain both PEG.js-specific options (like startRule) and parser-specific ones. Inside parse the PEG.js-specific options would get stripped and the resulting object would be available as options inside parser's action/predicate/initializer code. What do you think about this?

@ceymard
Copy link
Author

ceymard commented Aug 20, 2011

I like your proposal better.

If String, options is the startRule. If an object, it can have a { startRule: "..." } option. Neat. (I suppose this is what you meant)

@tehmou
Copy link

tehmou commented Oct 23, 2011

I would like to see this feature as described.

I am doing color coding with the generated parser, but I do not want to hardcode the logic for this into the grammar. What I am trying to do, is passing the parser a formatter object, which is then called at appropriate times from the parser.

In the proposed solution, I could do this then by inserting something like the following in right places:

{ input.formatter.format("comment", comment); }

@mcasimir
Copy link

If having a stateful parser is not a concern the same behaviour can be achieved by writing a function in the grammar that is capable to access the parser interface, like that:

var thisParser = this;

function resolveSymbol(parser, sym){
  return (parser.symbols || {})[sym];
}

and then using it in the semantic rules of the grammar itself:

GrammarRule = symbol:[a-z] { return resolveSymbol(thisParser, symbol); }

Now you can pass parameters to your grammar setting the 'symbols' proprerty on the parser

pegParser.symbols = {x: 5};
pegParser.parse("x");

Here is a complete example: https://gist.github.com/1627855

@malko
Copy link

malko commented May 22, 2012

while the proposal of replacing startRule parameter with options as stated above is adopted, it's already possible to make this inside the grammar definition in the starting block code doing something like this with actual 7.0 version:

var options = arguments[2] || {};

this seems sufficient for me if stated as a way of doing in the documentation.
Advantage of this solution it already work in current version.

What do you think of this proposal ?

@dmajda dmajda closed this as completed in 98ff2eb Sep 19, 2012
@ghost ghost assigned dmajda Sep 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants