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 to call transform's method from the given block #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

taichi-ishitani
Copy link

I think defining helper methods is good solution to simplify complex transform logics like we do that for the parser class.
However we cannot do that; for exmaple:

# test.rb
require 'parslet'

class Transform < Parslet::Transform
  rule(a: simple(:a)) { foo(a) }
  def foo(a); a.to_sym; end
end

p Transform.new.apply({ a: 'a' })
$ ruby ../../temp/test.rb
../../temp/test.rb:4:in `block in <class:Transform>': undefined method `foo' for #<Parslet::Context:0x00007f0be690ece8 @a="a"> (NoMethodError)

  rule(a: simple(:a)) { foo(a) }
                        ^^^
Did you mean?  for
        from /opt/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/parslet-2.0.0/lib/parslet/transform.rb:217:in `instance_eval'
        from /opt/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/parslet-2.0.0/lib/parslet/transform.rb:217:in `call_on_match'
        from /opt/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/parslet-2.0.0/lib/parslet/transform.rb:235:in `block in transform_elt'
        from /opt/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/parslet-2.0.0/lib/parslet/transform.rb:232:in `each'
        from /opt/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/parslet-2.0.0/lib/parslet/transform.rb:232:in `transform_elt'
        from /opt/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/parslet-2.0.0/lib/parslet/transform.rb:185:in `apply'
        from ../../temp/test.rb:8:in `<main>'

This PR is to allow to call methods defined in the transform class from the given transform block.

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

Successfully merging this pull request may close these issues.

1 participant