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

Allow recursive parsing from within the code callbacks. #6

Closed
creationix opened this issue Jan 27, 2010 · 4 comments
Closed

Allow recursive parsing from within the code callbacks. #6

creationix opened this issue Jan 27, 2010 · 4 comments

Comments

@creationix
Copy link

I would like the ability to recursively call parse from within the code hooks in my bnf.

My use case is implementing string interpolation in a programming language. My lexer will recursively tokenize the contents of embedded code in a string and give yyval the embedded token stream. I then want to recursively call parser.parse from within the string token in my bnf.

I can't seem to find a way to get at the current parser instance from within the callback. The this scope is just an object with one key $. Would it be possible to add a reference to the current parser instance to the this scope.

Also, while not required, it would help me a lot if I could specify the starter non-terminal for this recursive parse. I don't want to start at my root node, but at my expression node for the embedded code.

@zaach
Copy link
Owner

zaach commented Jan 27, 2010

It should be possible for you to store a reference to the parser in the yy scope variable:
myparser.yy.parser = myparser;

Then you could access myparser as yy.parser from within semantic actions.

But this may disrupt the currently executing parse, since parse calls lexer.setInput on each invocation. As an alternative, you could instantiate another parser just for use with string interpolation.

If only a subset of the language is allowed in embedded code fragments, you could even use a separate grammar and parser altogether, which may be a bit more efficient for parsing, but would require maintaining two grammars.

@creationix
Copy link
Author

Yeah, I had hacked in a reference to the parser and found that the results are unexpected because of setInput being called again. It looks like I'll have to generate pseudo tokens for start and end of embedded code and then just make it all part of the ine grammer.

@zaach
Copy link
Owner

zaach commented Feb 13, 2010

Forgot to close

@retorquere
Copy link

I'm trying to port my bibtex translator from pegjs to jison and I'm facing a similar issue. Is this then a structural hard problem for jison?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants