=begin copyright rubylexer - a ruby lexer written in ruby Copyright (C) 2004,2005 Caleb Clausen This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA =end require "assert" require "charhandler" #require "term" require "rubycode" require "io.each_til_charset" #------------------------------------ class RuLexer WHSP=" \t\r\v\f" WHSPLF=WHSP+"\n" #maybe \r should be in WHSPLF instead LEGALCHARS=/[ -~#{WHSPLF}]/ PAIRS={ '{'=>'}', '['=>']', '('=>')', '<'=>'>'} attr_reader :linenum,:last_operative_token #----------------------------------- def initialize(filename, file, line) @filename=filename String===file && file=IOext::FakeFile.new(file) file.binmode @file=file @linenum=line @toptable=nil #descendants must fill this out @moretokens=[ FileAndLineToken.new(@filename, @linenum, @file.pos) ] @last_operative_token=nil end #----------------------------------- def endoffile_detected s='' EoiToken.new(s,@file,@file.pos-s.size) end #----------------------------------- def get1token @moretokens.empty? or return @moretokens.shift if @file.eof? #@moretokens<=1 and readahead(2)[/^[-+]?[0-9]/] addl_dig_seqs=0 str << b str << (eat_next_if(/[+\-]/)or'') else back1char #return(str.send(interp)) break end #OCTCHARS allowed here to permit constants like this: 01.2 allowed == DECCHARS or allowed == OCTCHARS or error= "floats are always decimal (currently)" allowed = DECCHARS interp=:to_s end } assert(str[/[0-9]/]) lexerror NumberToken.new(str.send(interp)), error end #----------------------------------- def comment(str=nil) #assert str == '#' str=eat_next_if(?#) or return nil Process.kill("INT",0) if readahead(10)==%/breakpoint/ and defined? DEBUGGER__ if false @file.each_byte {|b| if b==?\n #leave \n's on input for newline to eat back1char else str << b end return IgnoreToken.new(str) if b==?\n or b==?# } #eof... else str<