adhearsion / ruby_speech

A ruby library for TTS & ASR document preparation
MIT License
101 stars 41 forks source link

Instance methods are not usable inside grammars #32

Open wdrexler opened 10 years ago

wdrexler commented 10 years ago

It seems that when there is a method inside a class that returns a grammar, and that grammar uses the return values of other methods inside the class, RubySpeech throws an error.

Code looks like this: https://gist.github.com/wdrexler/f0b229d7dfb7a9629dbe The error returned is:

/Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:277:in `method_missing': undefined local variable or method `my_method' for <item/>:RubySpeech::GRXML::Item (NameError)
    from ./rs_test.rb:14:in `block (3 levels) in grammar'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `instance_eval'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `eval_dsl_block'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:78:in `build'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:57:in `initialize'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:273:in `new'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:273:in `method_missing'
    from ./rs_test.rb:14:in `block (2 levels) in grammar'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `instance_eval'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `eval_dsl_block'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:78:in `build'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:57:in `initialize'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:273:in `new'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:273:in `method_missing'
    from ./rs_test.rb:13:in `block in grammar'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `instance_eval'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/generic_element.rb:126:in `eval_dsl_block'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/grxml.rb:25:in `block in draw'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/grxml.rb:23:in `tap'
    from /Users/wdrexler/.rvm/gems/ruby-2.1.2/bundler/gems/ruby_speech-a3a837c0106f/lib/ruby_speech/grxml.rb:23:in `draw'
    from ./rs_test.rb:12:in `grammar'
    from ./rs_test.rb:4:in `run'
    from ./rs_test.rb:20:in `<main>'

This error does not occur when either:

  1. The methods are declared at the top level (i.e. outside of a class) or
  2. If a local variable is assigned to the return value of the method before the RubySpeech::GRXML.draw block starts

This was tested against latest develop.

benlangfeld commented 10 years ago

So current theory is that while this fails...

RubySpeech::GRXML.draw root: 'root', tag_format: 'semantics/1.0' do
  rule id: 'root', scope: 'public' do
    item { my_method }
  end
end

...this would work...

RubySpeech::GRXML.draw root: 'root', tag_format: 'semantics/1.0' do
  item { my_method }
end

The existing test for this needs to be extended to cover invocation in a nested block.

wdrexler commented 10 years ago

So I've written a new test that covers invocation in a nested block. It fails, as expected. How would we fix this, @benlangfeld?

benlangfeld commented 10 years ago

First off, submit the spec as a pull request to replace this issue (or convert this one).

As for a fix, either recursively walk up the tree in method_missing or directly grab the block context from the root of the tree. The latter is more efficient but the former more closely respects normal closure binding semantics. Thoughts?

wdrexler commented 10 years ago

So I'm leaning towards walking up the tree instead of just grabbing the root element. Without knowing the actual performance impact of grabbing the block context of the root element (in, say, an Adhearsion app, is walking up the tree really going to be a bottleneck?), I'd say that we're better off going with the more semantically valid approach.

benlangfeld commented 10 years ago

Go for it :)