=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 module IOext #read until a character in a user-supplied set is found. #charrex must be a regexp that contains _only_ a single character class def til_charset(charrex,blocksize=16) blocks=[] m=nil until eof? block=read blocksize #if near eof, less than a full block may have been read if m=charrex .match(block) self.pos-=m.post_match.length+1 #'self.' shouldn't be needed... but is blocks.push m.pre_match if m.pre_match.length>0 break end blocks<size and @pos=size self[pos ... @pos] end def getc eof? and return nil pos = @pos @pos += 1 self[pos] end def eof? @pos >= size end def each_byte until eof? yield getc end end def stat #cheezy cheat to make #stat.size work self end def close; end def binmode; end include IOext #----------------------------------- #read and return next char if it matches ch #else, leave input unread and return nil or false def eat_next_if(ch) c=self[@pos,1] ch.kind_of? Integer and ch=ch.chr case c when ch then @pos+=1;c when '' then nil else false end end #----------------------------------- #returns previous character in stream #without changing stream position #or '' if at beginning def prevchar #returns Fixnum pos==0 ? '' : self[@pos-1] end #----------------------------------- #returns next character in stream #without changing stream position #or nil if at end def nextchar #returns Fixnum self[@pos] end #----------------------------------- def getchar #returns String eof? and return '' pos = @pos @pos += 1 self[pos,1] end #----------------------------------- def back1char() @pos-=1 end #----------------------------------- def readahead(len) self[@pos,len] end #----------------------------------- def readback(len) assert @pos-len>=0 self[@pos-len,len] end end end class IO include IOext end