--- lib/ruby_lexer.rb 2008-04-27 01:07:24.000000000 +0200 +++ lib/ruby_lexer.rb 2008-04-27 01:07:03.000000000 +0200 @@ -45,7 +45,7 @@ raise "bad val: #{str.inspect}" unless String === str self.file = file - self.lexer.src = StringIO.new(str) + self.lexer.src = RubyParser::StringIO.new(str) @yydebug = ENV.has_key? 'DEBUG' @@ -2604,104 +2604,106 @@ end end -class StringIO # HACK: everything in here is a hack - attr_accessor :begin_of_line, :was_begin_of_line - alias :begin_of_line? :begin_of_line - alias :read_all :read +class RubyParser + class StringIO < StringIO # HACK: everything in here is a hack + attr_accessor :begin_of_line, :was_begin_of_line + alias :begin_of_line? :begin_of_line + alias :read_all :read - alias :old_initialize :initialize + alias :old_initialize :initialize - def initialize(*args) - self.begin_of_line = true - self.was_begin_of_line = false - old_initialize(*args) - @original_string = self.string.dup - end + def initialize(*args) + self.begin_of_line = true + self.was_begin_of_line = false + old_initialize(*args) + @original_string = self.string.dup + end - def rest - self.string[self.pos..-1] - end + def rest + self.string[self.pos..-1] + end - def current_line # HAHA fuck you - @original_string[0..self.pos][/\A.*__LINE__/m].split(/\n/).size - end + def current_line # HAHA fuck you + @original_string[0..self.pos][/\A.*__LINE__/m].split(/\n/).size + end - def read - c = self.getc + def read + c = self.getc - if c == ?\r then - d = self.getc - self.ungetc d if d and d != ?\n - c = ?\n - end + if c == ?\r then + d = self.getc + self.ungetc d if d and d != ?\n + c = ?\n + end - self.was_begin_of_line = self.begin_of_line - self.begin_of_line = c == ?\n - if c and c != 0 then - c.chr - else - ::RubyLexer::EOF + self.was_begin_of_line = self.begin_of_line + self.begin_of_line = c == ?\n + if c and c != 0 then + c.chr + else + ::RubyLexer::EOF + end end - end - def match_string term, indent=false # TODO: add case insensitivity, or just remove - buffer = [] + def match_string term, indent=false # TODO: add case insensitivity, or just remove + buffer = [] - if indent - while c = self.read do - if c !~ /\s/ or c == "\n" or c == "\r" then - self.unread c - break + if indent + while c = self.read do + if c !~ /\s/ or c == "\n" or c == "\r" then + self.unread c + break + end + buffer << c end - buffer << c end - end - term.each_byte do |c2| - c = self.read - c = self.read if c and c == "\r" - buffer << c - if c and c2 != c[0] then - self.unread_many buffer.join # HACK omg - return false + term.each_byte do |c2| + c = self.read + c = self.read if c and c == "\r" + buffer << c + if c and c2 != c[0] then + self.unread_many buffer.join # HACK omg + return false + end end + + return true end - return true - end + def read_line + self.begin_of_line = true + self.was_begin_of_line = false + gets.sub(/\r\n?$/, "\n") # HACK + end - def read_line - self.begin_of_line = true - self.was_begin_of_line = false - gets.sub(/\r\n?$/, "\n") # HACK - end - - def peek expected = nil # FIX: barf - c = self.getc - return RubyLexer::EOF if c.nil? - self.ungetc c if c - c = c.chr if c - if expected then - c == expected - else - c + def peek expected = nil # FIX: barf + c = self.getc + return RubyLexer::EOF if c.nil? + self.ungetc c if c + c = c.chr if c + if expected then + c == expected + else + c + end end - end - def unread(c) - return if c.nil? # UGH + def unread(c) + return if c.nil? # UGH - # HACK: only depth is 2... who cares? really I want to remove all of this - self.begin_of_line = self.was_begin_of_line || true - self.was_begin_of_line = nil + # HACK: only depth is 2... who cares? really I want to remove all of this + self.begin_of_line = self.was_begin_of_line || true + self.was_begin_of_line = nil - c = c[0] if String === c - self.ungetc c - end + c = c[0] if String === c + self.ungetc c + end - def unread_many str - str.split(//).reverse.each do |c| - unread c + def unread_many str + str.split(//).reverse.each do |c| + unread c + end end end end