tags:

views:

12

answers:

1

I'm using ripper to doing ruby-code lexing in mri-1.9., I would like to do the same thing in JRuby, i noticed there is this org.jruby.lexer.yacc.RubyYaccLexer used in org.jruby.parser.DefaultRubyParser, i'm thinking that i can use it to do what ripper in mri-1.9. does, though definitely at a lower level as compared to ripper. Being a noob in java, i couldn't figure out how to use it from within jruby. I'm not sure if it is doable at all, hope to get some advice on this.

A: 

Take a look at this post from JRuby committer Ola Bini. In it he shows some brief usage of JRuby's AST. You can use the code from JRuby to create an AST and navigate it in memory, manipulate it, and turn it back into executable code.

require 'jruby'
JRuby.ast_for "puts 'hello'"
# => RootNode
#   NewlineNode
#     FCallOneArgNode |puts|
#       ArrayNode
#         StrNode =="hello"

It doesn't give you the same event-like approach like Ripper does, but by traversing the AST you can get similar information.

Nick Sieger
Yes, but JRuby.ast requires valid ruby syntax ? There are occasions when i need to handle invalid ruby code, eg. ':x => 1', where i'm concerned with the lexed tokens, and would very much like to avoid syntax checking.
ngty
You might also check out the [jruby-parser](http://kenai.com/projects/jruby-parser/sources/mercurial/show) project, which doesn't need the full JRuby install. The problem with standalone lexing is that the parser and lexer are intertwined, so lexing by itself may not give you the results you expect.
Nick Sieger