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

[FR] tree → string emitter #140

Open
arrdem opened this issue Jun 29, 2016 · 2 comments
Open

[FR] tree → string emitter #140

arrdem opened this issue Jun 29, 2016 · 2 comments

Comments

@arrdem
Copy link

arrdem commented Jun 29, 2016

I know there's a ticket open for generating strings which conform to a grammar, but it'd be awesome if there was a way to take a tree (as if parsed from a grammar & transformed remaining legal within that grammar) and a grammar, then generate a (the?) string which would parse the same way.

This would make it super easy to write source to source transformation tools, because you just parse a tree in, manipulate it, emit another tree in the same language and get re-encoding of the new expression tree "for free". Obviously for a whitespace sensitive grammar or a rewriting tool which cares about newlines/whitespace the parsing and manipulation language would have to encode those features as such but for simple expression oriented languages I'd think this is straightforwards.

@arrdem arrdem changed the title [FR] tree -> string emitter [FR] tree → string emitter Jun 29, 2016
@Engelberg
Copy link
Owner

The leaves of the parse tree are strings, so can't you just do a depth-first traversal of the tree and concatenate the strings at the leaves together in order to recover the original string?

I'm pretty sure I've seen someone use this strategy before with instaparse, but I can't quite remember where I've seen it.

mnemion did some related work on this shortly after instaparse's release to do further parse transformations on the tree, and he called his pull request "re-parse". I like the idea but haven't (yet?) integrated it into instaparse.

@Engelberg
Copy link
Owner

This is the pull request I referred to: #45

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

2 participants