1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#
# rscan.rb
#
# Copyright (c) 1999-2003 Minero Aoki <aamine@loveruby.net>
#
# This program is free software.
# You can distribute/modify this program
# under the same terms of ruby.
#
class StringScanner_R
Id_R = "$Id: rscan.rb,v 1.13 2003/03/25 02:04:21 aamine Exp $"
Version_R = '0.6.7'
Revision_R = %q$Revision: 1.13 $.split[1]
def self.must_C_version
raise NotImplementedError, 'strscan is not C version'
end
def initialize( str, dup = true )
@rest = @str = dup ? str.dup : str
@str.freeze
@prev = nil
@last_match = nil
end
attr_reader :last_match
def inspect
ret = "#<#{self.class} #{@str.size - @rest.size}/#{@str.size} "
pos = @str.size - @rest.size
if pos > 0 then
ret << ' '
beg = pos - 5
beg = 0 if beg < 0
ret << '"...' if beg > 0
ret << @str[beg..pos].inspect[1..-1]
end
ret << ' @'
unless @rest.empty? then
ret << ' ' << @rest[0,5].inspect
ret[-1,1] = '..."' if @rest.size > 5
end
ret << '>'
ret
end
def scan( re )
if m = re.match( @rest ) then
@last_match = m
@prev = @rest
@rest = m.post_match
m[0]
else
@last_match = @prev = nil
nil
end
end
def skip( re )
if m = re.match( @rest ) then
@last_match = m
@prev = @rest
@rest = m.post_match
@prev.size - @rest.size
else
@prev = nil
@last_match = nil
nil
end
end
def match?( re )
if m = re.match( @rest ) then
@last_match = m
@prev = @rest
m.end(0)
else
@prev = nil
@last_match = nil
nil
end
end
def check( re )
if m = re.match( @rest ) then
@last_match = m
@prev = @rest
@rest[ 0, m.end(0) ]
else
@prev = @last_match = nil
nil
end
end
def scan_until( re )
if skip re then
@last_match.pre_match + @last_match[0]
else
nil
end
end
alias skip_until skip
alias exist? match?
alias check_until check
def getch
scan( /./ )
end
def eos?
@rest.empty?
end
alias empty? eos?
def rest?
not @rest.empty?
end
def rest
@rest.dup
end
def restsize
@rest.size
end
def matched?
not @last_match.nil?
end
def matched
@last_match[0]
end
def matchedsize
m = @last_match
m.end(0) - m.begin(0)
end
def [](n)
@last_match[n]
end
def string
@str
end
def string=( s )
@str = @rest = s
@prev = nil
end
def pos
@str.size - @rest.size
end
def pos=( i )
@rest = @str[ i, @str.size - i ]
end
alias pointer pos
alias pointer= pos=
def reset
@rest = @str
@prev = @last_match = nil
end
def clear
@rest = ''
@prev = @last_match = nil
end
def unscan
@prev or raise ScanError, 'cannot unscan: not scanned yet'
@rest = @prev
@prev = nil
end
end # class StringScanner_R
|