=begin legal crap rubylexer - a ruby lexer written in ruby Copyright (C) 2004,2005,2008 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 class RubyLexer #------------------------- class Token attr_accessor :ident def to_s @ident || "<#{self.class.name}>" end attr_accessor :offset #file offset of start of this token attr_accessor :as #if defined, a KeywordToken which this token stands in for. attr_accessor :allow_ooo_offset #hack def initialize(ident,offset=nil) @ident=ident @offset=offset end def error; end def has_no_block?; false end end #------------------------- class WToken< Token def ===(pattern) assert @ident pattern===@ident end end #------------------------- class KeywordToken < WToken #also some operators #----------------------------------- def set_callsite!(x=true) @callsite=x end #----------------------------------- def callsite? @callsite if defined? @callsite end #----------------------------------- def set_infix! @infix=true end #----------------------------------- def infix? @infix ||= nil end def prefix?; !infix? end #----------------------------------- def has_end! @has_end=true end #----------------------------------- def has_end? self===RubyLexer::BEGINWORDS and @has_end||=nil end attr_accessor :comma_type, :ternary, :grouping def has_no_block! @has_no_block=true end def has_no_block? @has_no_block if defined? @has_no_block end def infix @infix if defined? @infix end end #------------------------- class OperatorToken < WToken attr_accessor :unary alias prefix? unary def infix?; !prefix? end end #------------------------- module TokenPat @@TokenPats={} def token_pat #used in various case statements... result=self.dup @@TokenPats[self] ||= (class <"%w[", '{'=>'%W{' } SUFFIXERS={ '['=>"]", '{'=>'}' } def has_str_inc? elems.size>1 or RubyCode===elems.first end def to_s transname=:transform assert @char[/[\[{"`\/]/] #" #on output, all single-quoted strings become double-quoted assert(@elems.length==1) if @char=='[' result=open.dup starter=result[-1,1] ender=close elems.each{|e| case e when String: result<