From: Logan Capaldo Date: 2006-09-09T10:49:59+09:00 Subject: Re: [ANN] Wirble 0.1.1: Irb Enhancements for the Masses On Sep 8, 2006, at 8:01 PM, Hal Fulton wrote: > Paul Duncan wrote: >> I looked into highlighting the input in Irb, too. It's a lot more >> work, >> but it might be doable. At a minimum, I'd need to write, borrow, or >> steal a full-blown Ruby lexer. I'd probably need a more terminal- >> friendly replacement for readlines (probably curses). > > 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 > > Hal >