L
Logan Capaldo
You can use the lexer built into irb. I forget how it's
done, but I did it two years ago.
You certainly can steal irb's lexer. Here's an example from some code
I've been kicking around for a while, so you can see how it works:
(This code understands a very minimal set of ruby and turns it into a
sort of Sexpr)
require 'irb/ruby-lex'
module RubyMerge
class ParseSkeleton
def initialize(io)
@io = io
end
def skeleton
lexer = RubyLex.new
stack = []
results = []
lexer.set_input( @io )
while current = lexer.token
case current
when RubyLex::TkDEF, RubyLex::TkMODULE
if stack.empty?
results << current.name.to_sym
ident = nil
1 while ( ident = lexer.token ).kind_of? RubyLex::TkSPACE
results << ident.name
else
if results.last.kind_of? Array
results.last << current.name.to_sym
ident = nil
1 while ( ident = lexer.token ).kind_of? RubyLex::TkSPACE
results.last << ident.name
else
results << []
results.last << current.name.to_sym
ident = nil
1 while ( ident = lexer.token ).kind_of? RubyLex::TkSPACE
results.last << ident.name
end
end
stack.push current
when RubyLex::TkEND
stack.pop
end
end
results
end
end
end