Forum: Ruby how to call instance method from within Treetop gem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tom C. (Guest)
on 2009-03-25 10:57
(Received via mailing list)
I'm trying to call an instance method from within a gem (Treetop), and
nothing I've tried is working (3 hours into it - no results). I'll do my
best to simplify the problem.

I have a class instance which sets things up with this...

    Treetop.load '../data/sn_grammar' # <= this loads a data file which
will be process as Ruby, after some preprocessing

    @parser = Base_grammarParser.new # < = Treetop class instance

My class method with the code above then calls another method in the
same class - both in my module SN, class SetNet.

The other method then calls a method of the @parser instance, and THAT
one gives to eval some of the text in the sn_grammar file referenced in
the first code line above. That text then calls another method -
showhelp - back in my class instance. But it cannot find it. I'm not
naming it correctly, somehow.  I get this error:

(eval):71:in `act': undefined method `showhelp' for SyntaxNode+HelpMain0
offset=0, ".h":Treetop::Runtime::SyntaxNode (NoMethodError)
        from ../lib/setnet/SN.rb:265:in `proc_command'
        from ../lib/setnet/SN.rb:182:in `user_interface'
        from ../lib/setnet/SN.rb:113:in `manage'
        from ../lib/setnet/SN.rb:38:in `startup'
        from ./setnet:26

I've tried every permutation I can image, to tell eval how to find my
method. No go.

Module SN => class SetNet => instance setnet => method showhelp. <=
That's what I'm trying to get to.

How can I tell eval, executing off in the gem directory, where this
method is found? That's my problem, I think. I'm run out of
possibilities, so I'm failing to grasp something basic most likely.

Any help would be appreciated.

Tom

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Justin C. (Guest)
on 2009-03-25 12:01
(Received via mailing list)
Tom C. wrote:
>
> SyntaxNode+HelpMain0 offset=0, ".h":Treetop::Runtime::SyntaxNode
> Module SN => class SetNet => instance setnet => method showhelp. <=
> That's what I'm trying to get to.
>
> How can I tell eval, executing off in the gem directory, where this
> method is found? That's my problem, I think. I'm run out of
> possibilities, so I'm failing to grasp something basic most likely.
>
> Any help would be appreciated.
>
> Tom
>

Hi Tom,

You seem to be trying to call the showhelp method from within your
grammar. Is this correct? If so, note that the code within the grammar
is attached to SyntaxNodes as the grammar matches your text. So your
showhelp method is expected to be defined for SyntaxNodes.

Here is what I think your setup is:

module SN
  class SetNet
    def other_method
      @parser = Base_grammarParser.new
      @parser.parse(text).eval
    end

    def showhelp
    end
  end
end

grammar Base_grammar
  rule help
    .* {
      def eval
        showhelp
      end
    }
  end
end


If this is the case, you can just make eval take an argument:


module SN
  class SetNet
    def other_method
      @parser = Base_grammarParser.new
      @parser.parse(text).eval(self)
    end

    def showhelp
    end
  end
end

grammar Base_grammar
  rule help
    .* {
      def eval(sn)
        sn.showhelp
      end
    }
  end
end

Hope that helps some.

-Justin
Tom C. (Guest)
on 2009-03-25 12:56
(Received via mailing list)
Justin C. wrote:
>>    @parser = Base_grammarParser.new # < = Treetop class instance
>> (eval):71:in `act': undefined method `showhelp' for
>>
>>
>
> Hi Tom,
>
> You seem to be trying to call the showhelp method from within your
> grammar. Is this correct? If so, note that the code within the grammar
> is attached to SyntaxNodes as the grammar matches your text. So your
> showhelp method is expected to be defined for SyntaxNodes.
That's the problem I didn't know how to address!
>    def showhelp
>    }
>      @parser = Base_grammarParser.new
>    .* {
>
>
Justin,

I am in your debt. I didn't have much faith that I could explain my
problem well enough, and simply enough, that I'd get a response. Your
solution made sense to me (I'd wanted to do what your code does, but
didn't know how). With a minor adjustment, it worked, which was a bit
stunning. Here's the final version (a prototype only, but that'll get me
launched):

#this - in my main class method
    Treetop.load '../data/sn_grammar'
    @parser = Base_grammarParser.new

#this in my secondary method
    if parse_tree = @parser.parse( @opt )
      parse_tree.act(self)
    else...

#this in my grammar rule
  # display main help
  rule help_main
    '.h' {
    def act( sn )
      #puts 'help_main'
      sn.showhelp( [ '0', '0c', '0a', '0q', '0v', '0f', '0m', '0u' ] )
    end
    }
  end

#and this in my main, as suggested by Markus on the Treetop list -
class Treetop::Runtime::SyntaxNode
  #include SN
  def act
    elements.each { |e|
    e.act(self) if e.respond_to? :act }
  end
end

Now I can write my grammar and get this thing working for me.

I much apppreciate your superior knowledge, and your generosity in
sharing it.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This topic is locked and can not be replied to.