[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

parse tree?

Giles Bowkett

1/3/2008 5:57:00 AM

I have a situation where it may make sense, although for some reason I
don't want to, to use something like ParseTree to pull out each
complete Ruby expression and pass it to a block which will then
evaluate it in a particular context and make all kinds of magic
happen.

Is this a reasonable ParseTree use case?

@expressions_parse_tree_found.each |exp|
ArbitraryModule.module_eval(&exp)
end

--
Giles Bowkett

Podcast: http://hollywoodgrit.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....

9 Answers

Florian Frank

1/3/2008 7:54:00 PM

0

Giles Bowkett wrote:
> As far as I can tell you get the whole shebang back, and I just need
> particular expressions.
>
Maybe you can use the ruby_parser gem?

--
Florian Frank


ara.t.howard

1/3/2008 9:33:00 PM

0


On Jan 3, 2008, at 1:10 PM, Ryan Davis wrote:

>
>> >> pt = ParseTree.new.parse_tree_for_string("1+2")
>> => [[:call, [:lit, 1], :+, [:array, [:lit, 2]]]]
>> >> Ruby2Ruby.new.process(pt.first)
>> => "(1 + 2)"
>
> does that help?
>
>

ryan. even though i've seen that before i thought i'd add: that is
insanely cool!

a @ http://codeforp...
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama




Judson Lester

1/3/2008 10:42:00 PM

0

On Jan 3, 2008 1:33 PM, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> On Jan 3, 2008, at 1:10 PM, Ryan Davis wrote:
>
> >
> >> >> pt = ParseTree.new.parse_tree_for_string("1+2")
> >> => [[:call, [:lit, 1], :+, [:array, [:lit, 2]]]]
> >> >> Ruby2Ruby.new.process(pt.first)
> >> => "(1 + 2)"
> ryan. even though i've seen that before i thought i'd add: that is
> insanely cool!
>
It is really cool. I've been using R2R to build an auto-rspec thing
that re-creates the context of an expression so that you can test
against it. It's actually quite fantastic.


Judson
--
Your subnet is currently 169.254.0.0/16. You are likely to be eaten by a grue.

Ryan Davis

1/4/2008 10:39:00 AM

0


On Jan 3, 2008, at 21:06 , Giles Bowkett wrote:

> Well, I'm writing a code generator, and I thought I was very close to
> finished, except I was basically doing this:
>
> generated_code = Generators.module_eval(&block)
>
> And that works great for tiny bits of code, it works at the statement
> level, but when I want to just collect up *all* the statements in a
> block, the whole thing kinds of goes south. So I have something which
> generates all the individual statements perfectly but completely falls
> apart when I want to create several statements.

Does this help?

> >> require 'rubygems' ; require 'ruby2ruby'
> => true
> >> def code_for &block; block.to_ruby; end
> => nil
> >> code_for do
> ?> single_line(statement)
> >> statement(:which => spans,
> ?> :multiple => lines)
> >> end
> => "proc {\n single_line(statement)\n statement(:which =>
> spans, :multiple => lines)\n}"

There are clean ways to ensure it isn't a proc as well.



Eivind Eklund

1/4/2008 12:00:00 PM

0

On Jan 4, 2008 6:06 AM, Giles Bowkett <gilesb@gmail.com> wrote:

> Well, I'm writing a code generator, and I thought I was very close to
> finished, except I was basically doing this:
>
> generated_code = Generators.module_eval(&block)
>
> And that works great for tiny bits of code, it works at the statement
> level, but when I want to just collect up *all* the statements in a
> block, the whole thing kinds of goes south.

From the description you give, it sounds like you could just create a
new block that just calls a list of other blocks, and do ; separation?

Ie, if the line above is in a method called "foo(&block)", call it
with something like

foo do
my_blocks.collect { |block| block.call + ";" }
end

Probably I'm not understanding your requirements, just thought I'd air
it in case it is this simple.

Eivind.

Giles Bowkett

1/4/2008 4:07:00 PM

0

> > And that works great for tiny bits of code, it works at the statement
> > level, but when I want to just collect up *all* the statements in a
> > block, the whole thing kinds of goes south. So I have something which
> > generates all the individual statements perfectly but completely falls
> > apart when I want to create several statements.
>
> Does this help?
>
> > >> require 'rubygems' ; require 'ruby2ruby'
> > => true
> > >> def code_for &block; block.to_ruby; end
> > => nil
> > >> code_for do
> > ?> single_line(statement)
> > >> statement(:which => spans,
> > ?> :multiple => lines)
> > >> end
> > => "proc {\n single_line(statement)\n statement(:which =>
> > spans, :multiple => lines)\n}"
>
> There are clean ways to ensure it isn't a proc as well.

That totally solves my problem. Or at least, ***if*** it's guaranteed
to turn multi-line statements into one-liners every time, that very
probably totally solves my problem. (I haven't tried it yet, I've just
proved it to be true, etc.) I can just pull it out of the proc{} with
a regex and then split it on the newlines. Baddabing baddaboom.

--
Giles Bowkett

Podcast: http://hollywoodgrit.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....

Giles Bowkett

1/4/2008 4:09:00 PM

0

> > Well, I'm writing a code generator, and I thought I was very close to
> > finished, except I was basically doing this:
> >
> > generated_code = Generators.module_eval(&block)
> >
> > And that works great for tiny bits of code, it works at the statement
> > level, but when I want to just collect up *all* the statements in a
> > block, the whole thing kinds of goes south.
>
> From the description you give, it sounds like you could just create a
> new block that just calls a list of other blocks, and do ; separation?
>
> Ie, if the line above is in a method called "foo(&block)", call it
> with something like
>
> foo do
> my_blocks.collect { |block| block.call + ";" }
> end
>
> Probably I'm not understanding your requirements, just thought I'd air
> it in case it is this simple.

The hard part is splitting an existing block into an array of blocks,
or strings, based on the logical division of statements; in other
words to say "turn this block into an array of statements" (in a way
that Ruby understands) is the tricky part.

--
Giles Bowkett

Podcast: http://hollywoodgrit.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....

Ryan Davis

1/4/2008 10:31:00 PM

0


On Jan 4, 2008, at 08:07 , Giles Bowkett wrote:

> That totally solves my problem. Or at least, ***if*** it's guaranteed
> to turn multi-line statements into one-liners every time, that very
> probably totally solves my problem. (I haven't tried it yet, I've just
> proved it to be true, etc.) I can just pull it out of the proc{} with
> a regex and then split it on the newlines. Baddabing baddaboom.

What is your 1-liner requirement for?


Giles Bowkett

1/4/2008 10:37:00 PM

0

> > That totally solves my problem. Or at least, ***if*** it's guaranteed
> > to turn multi-line statements into one-liners every time, that very
> > probably totally solves my problem. (I haven't tried it yet, I've just
> > proved it to be true, etc.) I can just pull it out of the proc{} with
> > a regex and then split it on the newlines. Baddabing baddaboom.
>
> What is your 1-liner requirement for?

Weird synch thing there. I plugged it in and it worked perfectly,
except that I'm splitting blocks now where I don't need to. I can hack
around it by regexing on "do" but it's non perfecto.

--
Giles Bowkett

Podcast: http://hollywoodgrit.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....