Ruby parsing in Ruby

Hi all,

I've been doing Ruby for a small while now, but I'm a newbie when it
comes to parsing. I've been digging around, but still can't seem to
find the answer. I was wondering if Ruby itself has classes that I can
use to parse ruby source code into a parse tree.

My first inclination was to use Racc
(http://i.loveruby.net/en/projects/racc/), but I didn't want to have to
write the BNF(en.wikipedia.org/wiki/Backus-Naur_form) for Ruby if I
didn't have to (maybe it's not that hard?). I wasn't able to find a
library of BNF for different languages (including Ruby), so then I
started looking elsewhere.

Both irb and ri probably use a Ruby parser of some sort, but looking at
the source code, it looked like each of them rolled their own. I
wasn't able to dig out the ruby parsing functionality from either by
including what I thought were necessary code modules.

What would be the easiest way to parse Ruby source into parse trees in
Ruby? Thanks!

Wil

are you create a game as ruby?

···

2006/11/11, Wil <iamwil@gmail.com>:

Hi all,

I've been doing Ruby for a small while now, but I'm a newbie when it
comes to parsing. I've been digging around, but still can't seem to
find the answer. I was wondering if Ruby itself has classes that I can
use to parse ruby source code into a parse tree.

My first inclination was to use Racc
(http://i.loveruby.net/en/projects/racc/\), but I didn't want to have to
write the BNF(en.wikipedia.org/wiki/Backus-Naur_form) for Ruby if I
didn't have to (maybe it's not that hard?). I wasn't able to find a
library of BNF for different languages (including Ruby), so then I
started looking elsewhere.

Both irb and ri probably use a Ruby parser of some sort, but looking at
the source code, it looked like each of them rolled their own. I
wasn't able to dig out the ruby parsing functionality from either by
including what I thought were necessary code modules.

What would be the easiest way to parse Ruby source into parse trees in
Ruby? Thanks!

Wil

gem install ParseTree

j`ey
http://www.eachmapinject.com

···

On 11/11/06, Wil <iamwil@gmail.com> wrote:

Hi all,

I've been doing Ruby for a small while now, but I'm a newbie when it
comes to parsing. I've been digging around, but still can't seem to
find the answer. I was wondering if Ruby itself has classes that I can
use to parse ruby source code into a parse tree.

My first inclination was to use Racc
(http://i.loveruby.net/en/projects/racc/\), but I didn't want to have to
write the BNF(en.wikipedia.org/wiki/Backus-Naur_form) for Ruby if I
didn't have to (maybe it's not that hard?). I wasn't able to find a
library of BNF for different languages (including Ruby), so then I
started looking elsewhere.

Both irb and ri probably use a Ruby parser of some sort, but looking at
the source code, it looked like each of them rolled their own. I
wasn't able to dig out the ruby parsing functionality from either by
including what I thought were necessary code modules.

What would be the easiest way to parse Ruby source into parse trees in
Ruby? Thanks!

Wil

Wil wrote:

Hi all,

I've been doing Ruby for a small while now, but I'm a newbie when it
comes to parsing. I've been digging around, but still can't seem to
find the answer. I was wondering if Ruby itself has classes that I can
use to parse ruby source code into a parse tree.

My first inclination was to use Racc
(http://i.loveruby.net/en/projects/racc/\), but I didn't want to have to
write the BNF(en.wikipedia.org/wiki/Backus-Naur_form) for Ruby if I
didn't have to (maybe it's not that hard?). I wasn't able to find a
library of BNF for different languages (including Ruby), so then I
started looking elsewhere.

Both irb and ri probably use a Ruby parser of some sort, but looking at
the source code, it looked like each of them rolled their own. I
wasn't able to dig out the ruby parsing functionality from either by
including what I thought were necessary code modules.

What would be the easiest way to parse Ruby source into parse trees in
Ruby? Thanks!

Wil

Take a look at parsetree. It should be able to do this quite happily
and uses Ruby's own parser. This avoids compatibility issues.

i said with ruby

···

2006/11/11, juan pedro meriño <juapdiaz@gmail.com>:

are you create a game as ruby?

2006/11/11, Wil <iamwil@gmail.com>:
>
> Hi all,
>
> I've been doing Ruby for a small while now, but I'm a newbie when it
> comes to parsing. I've been digging around, but still can't seem to
> find the answer. I was wondering if Ruby itself has classes that I can
> use to parse ruby source code into a parse tree.
>
> My first inclination was to use Racc
> (http://i.loveruby.net/en/projects/racc/\), but I didn't want to have to
> write the BNF(en.wikipedia.org/wiki/Backus-Naur_form) for Ruby if I
> didn't have to (maybe it's not that hard?). I wasn't able to find a
> library of BNF for different languages (including Ruby), so then I
> started looking elsewhere.
>
> Both irb and ri probably use a Ruby parser of some sort, but looking at
> the source code, it looked like each of them rolled their own. I
> wasn't able to dig out the ruby parsing functionality from either by
> including what I thought were necessary code modules.
>
> What would be the easiest way to parse Ruby source into parse trees in
> Ruby? Thanks!
>
> Wil
>

juan pedro meriño wrote:

are you create a game as ruby?

Stop spamming arbitrary threads with unrelated chiming in. This isn't IRC.

David Vallner

Hrm, I remember stumbling over parsetree, and it seems like the classes
need to already exist in object/class space (at least from the example
that they give). What if you just had a string of the source of the
classes? Perhaps they're one in the same. I should look at Parsetree
a little bit closer.

Wil

Timothy Goddard wrote:

···

Wil wrote:
> Hi all,
>
> I've been doing Ruby for a small while now, but I'm a newbie when it
> comes to parsing. I've been digging around, but still can't seem to
> find the answer. I was wondering if Ruby itself has classes that I can
> use to parse ruby source code into a parse tree.
>
> My first inclination was to use Racc
> (http://i.loveruby.net/en/projects/racc/\), but I didn't want to have to
> write the BNF(en.wikipedia.org/wiki/Backus-Naur_form) for Ruby if I
> didn't have to (maybe it's not that hard?). I wasn't able to find a
> library of BNF for different languages (including Ruby), so then I
> started looking elsewhere.
>
> Both irb and ri probably use a Ruby parser of some sort, but looking at
> the source code, it looked like each of them rolled their own. I
> wasn't able to dig out the ruby parsing functionality from either by
> including what I thought were necessary code modules.
>
> What would be the easiest way to parse Ruby source into parse trees in
> Ruby? Thanks!
>
> Wil

Take a look at parsetree. It should be able to do this quite happily
and uses Ruby's own parser. This avoids compatibility issues.

David Vallner wrote:

juan pedro meriño wrote:

are you create a game as ruby?

Stop spamming arbitrary threads with unrelated chiming in. This isn't IRC.

What are you talking about? The only one who appears to have chimed into an arbitrary thread with something unrelated is you. Is this some kind of hip irony, or am I missing something?

Well, why not eval the string in that case then? Then the classes
would exist inside the interpreter.

···

On 11/13/06, Wil <iamwil@gmail.com> wrote:

Hrm, I remember stumbling over parsetree, and it seems like the classes
need to already exist in object/class space (at least from the example
that they give). What if you just had a string of the source of the
classes? Perhaps they're one in the same. I should look at Parsetree
a little bit closer.

$ ri ParseTree | grep string
      parse_tree, parse_tree_for_method, parse_tree_for_string, process
$ ri ParseTree#parse_tree_for_string
---------------------------------------- ParseTree#parse_tree_for_string
      parse_tree_for_string(source, filename = nil, line = nil, newlines
      = false)

···

On Nov 12, 2006, at 8:35 AM, Wil wrote:

Timothy Goddard wrote:

Wil wrote:

What would be the easiest way to parse Ruby source into parse trees in
Ruby? Thanks!

Take a look at parsetree. It should be able to do this quite happily
and uses Ruby's own parser. This avoids compatibility issues.

Hrm, I remember stumbling over parsetree, and it seems like the classes
need to already exist in object/class space (at least from the example
that they give). What if you just had a string of the source of the
classes? Perhaps they're one in the same. I should look at Parsetree
a little bit closer.

------------------------------------------------------------------------
      Returns the parse tree for a string +source+.

      Format:

        [[sexps] ... ]

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

Jeffrey Schwab wrote:

David Vallner wrote:

juan pedro meriño wrote:

are you create a game as ruby?

Stop spamming arbitrary threads with unrelated chiming in. This isn't IRC.

What are you talking about? The only one who appears to have chimed into an arbitrary thread with something unrelated is you. Is this some kind of hip irony, or am I missing something?

You're missing something. :slight_smile: David was right.

Go read the context...

Cheers,
Hal

Hal Fulton wrote:

Jeffrey Schwab wrote:

David Vallner wrote:

juan pedro meriño wrote:

are you create a game as ruby?

Stop spamming arbitrary threads with unrelated chiming in. This isn't IRC.

What are you talking about? The only one who appears to have chimed into an arbitrary thread with something unrelated is you. Is this some kind of hip irony, or am I missing something?

You're missing something. :slight_smile: David was right.

Go read the context...

Thanks. Sorry, David!

I don't see Juan Pedro's post at all, though I see David's reply. Hm.