class XMLScan::Source
Public Class Methods
new(src)
click to toggle source
Source
inherits Array only for speed.
Calls superclass method
# File lib/xmlscan/scanner.rb 102 def initialize(src) 103 super() 104 @src = Input.wrap(src) 105 @eof = false 106 @last = nil 107 end
Public Instance Methods
abort()
click to toggle source
# File lib/xmlscan/scanner.rb 118 def abort 119 @eof = true 120 @last = nil 121 clear 122 self 123 end
close_tag()
click to toggle source
# File lib/xmlscan/scanner.rb 181 def close_tag # tag_end?, and remove a `>'. 182 unless s = last || @last and s[0] != ?< then 183 false 184 else 185 if s == '>' or s.empty? then 186 s1 = get 187 unless s = last || @last and s[0] == ?< then # for speed up 188 out = [ s1 ] 189 out.push get while s = last || @last and s == '>' || s.empty? 190 x=out.pop unless s and s[0] != ?< # De Morgan 191 concat out 192 end 193 end 194 true 195 end 196 end
each() { |s| ... }
click to toggle source
# File lib/xmlscan/scanner.rb 239 def each 240 prepare 241 while s = get 242 yield s 243 end 244 self 245 end
eof?()
click to toggle source
# File lib/xmlscan/scanner.rb 114 def eof? 115 @eof and empty? 116 end
get()
click to toggle source
Managing source in a private array.
-
tag oriented (?< and ?> are the key tokens
-
?> that aren't followed by another ?< or ?> are stripped in splitting
# File lib/xmlscan/scanner.rb 130 def get 131 pop or 132 unless @eof then 133 last = @last 134 begin 135 unless chunk = @src.gets then 136 @eof = true 137 @last = nil 138 return last 139 #unshift last # to be popped after reverse! 140 #last = nil 141 #break 142 end 143 # negative lookahead: < or >< or >> 144 # so don't consume those (but split leaving them always at the 145 # end of chunks) 146 # consume (>) and split on > 147 a = chunk.split(/(?=<|>[<>])|>/, -1) 148 if last then 149 unless /\A[<>]/ =~ a.first then 150 a[0] = last << (a.first || '') 151 else 152 push last 153 end 154 end 155 raise "size #{size}" if size > 1 156 concat a 157 last = pop 158 end while empty? 159 @last = last 160 reverse! 161 pop 162 end 163 end
get_plain()
click to toggle source
# File lib/xmlscan/scanner.rb 207 def get_plain 208 s = get 209 s = '>' << s unless not s or (c = s[0]) == ?< or c == ?> # De Morgan 210 s 211 end
get_tag()
click to toggle source
# File lib/xmlscan/scanner.rb 203 def get_tag # get until tag_end? 204 s = last || @last and s[0] == ?< and get 205 end
get_text()
click to toggle source
# File lib/xmlscan/scanner.rb 199 def get_text # get until tag_start? 200 s = last || @last and s[0] != ?< and get 201 end
inspect()
click to toggle source
The following methods are for debug.
# File lib/xmlscan/scanner.rb 224 def inspect 225 a = [] 226 reverse_each { |i| 227 a.push ">" unless /\A[<>]/ =~ i 228 a.push i.inspect 229 } 230 last = [] 231 if @last then 232 last.push ">" unless /\A[<>]/ =~ @last 233 last.push @last.inspect 234 end 235 a.push '#eof' if @eof 236 "((#{a*' '}) l(#{last*' '}) . #{source.inspect})" 237 end
lineno()
click to toggle source
# File lib/xmlscan/scanner.rb 213 def lineno 214 @src.lineno 215 end
path()
click to toggle source
# File lib/xmlscan/scanner.rb 217 def path 218 @src.path 219 end
prepare()
click to toggle source
# File lib/xmlscan/scanner.rb 166 def prepare 167 s = get 168 s = get and s = '>' << s if s and s.empty? # preserve first `>' 169 s and push s 170 end
source()
click to toggle source
# File lib/xmlscan/scanner.rb 109 def source 110 Input.unwrap @src 111 end
tag_end?()
click to toggle source
# File lib/xmlscan/scanner.rb 173 def tag_end? 174 s = last || @last and s[0] != ?< 175 end
tag_start?()
click to toggle source
# File lib/xmlscan/scanner.rb 177 def tag_start? 178 s = last || @last and s[0] == ?< 179 end
test()
click to toggle source
# File lib/xmlscan/scanner.rb 247 def test 248 last or @last or (s = get and push s and s) 249 end