From: Tom Cloyd Date: 2009-03-25T19:52:38+09:00 Subject: Re: how to call instance method from within Treetop gem Justin Collins wrote: > Tom Cloyd wrote: >> 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 >> > > 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! > > 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 > > 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 Cloyd, MS MA, LMHC - Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 << tc@tomcloyd.com >> (email) << TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~