kschiess / parslet

A small PEG based parser library. See the Hacking page in the Wiki as well.
kschiess.github.com/parslet
MIT License
805 stars 95 forks source link

Node Transformer data modules #119

Closed karlentwistle closed 9 years ago

karlentwistle commented 9 years ago

Node interpreter now takes an optional data module. The optional data module can supply helper methods to parslet transformers. An example use case would be needing to make an external API call.

We're using a monkey patched version of Parslet at the moment because we need to reference external plugin dependancies. These look like https://github.com/alphagov/smart-answers/blob/master/lib/smartdown_plugins/animal-example-simple/build_time.rb In this example we use the return value of pokemon_regions to build an element using Parslet::Transform. In our DSL we have a notion of references to external 'build_time' functions. When the transformer is run the external 'build_time' functions are available and can return their output to then be be used to transform them into objects.

I made this pull request to hopefully demonstrate this behaviour to you and perhaps to make this behaviour part of Parslet. If you agree this is a sensible approach. If not we'd love some pointers on a better way to do this.

kschiess commented 9 years ago

Hei

Two ways to go about this in vanilla parslet:

a) Use the tree as a functional collapsing return value, see https://github.com/kschiess/parslet/blob/master/example/parens.rb

b) Use the transformer context as a store for your external instances, as just described in https://github.com/kschiess/parslet/blob/master/lib/parslet/transform.rb#L162-L174

Now: Did I understand what you're achieving with this PR? What novelty is presented here that I didn't see? And finally, can your method be substituted by one of these two?

FriedSock commented 9 years ago

Yes! option b is just what we needed, thanks!

karlentwistle commented 9 years ago

Thanks kschiess :+1: :star:

kschiess commented 9 years ago

Perfect!

Hard to document everything I thought about ;)