Class: StringScanner

Inherits:
Object show all
Defined in:
lib/rmtools/text/regexp.rb,
lib/rmtools/text/string_scanner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lastObject (readonly)

Returns the value of attribute last.



111
112
113
# File 'lib/rmtools/text/regexp.rb', line 111

def last
  @last
end

Class Method Details

.each(string, *args, &b) ⇒ Object



192
193
194
# File 'lib/rmtools/text/regexp.rb', line 192

def self.each string, *args, &b
  new(string).each *args, &b
end

Instance Method Details

#+Object



188
# File 'lib/rmtools/text/regexp.rb', line 188

def +; string[pos, 1] end

#-Object



190
# File 'lib/rmtools/text/regexp.rb', line 190

def -; string[pos-matched_size-1, 1] end

#each(re, cbs = nil, &cb) ⇒ Object

#each( <Regexp>, { <0..255 | :~ | nil> => ->{|self|}, … } ) #each( <Regexp>, [ [ <Regexp>, ->{|self, <MatchData>|} ], … ] ) #each( <Regexp> ) {|self|} Example:

ss = StringScanner.new xpath
ss.each %r{\[-?\d+\]|\{[^\}]+\}}, 
  ?[ => lambda {|ss| 
    if node; node = FindByIndex[node, nslist, ss]
    else     return []     end  }, 
  ?{ => lambda {|ss| 
    if node; node = FindByProc[node, nslist, ss]
    else     return []     end  },
  nil => lambda {|str|
    node = node.is(Array) ?
      node.sum {|n| n.__find(str, nslist).to_a} : node.__find(str, nslist) 
  }


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
# File 'lib/rmtools/text/regexp.rb', line 129

def each(re, cbs=nil, &cb)
  @last = pos
  res = scan_until re
  if cbs
    if cbs.is Hash
      while res
        if cb = cbs[matched[0]] || cbs[:~]
          cb[self]
          @last = pos
          res = !eos? && scan_until(re)
        else break
        end
      end
      if !eos? and cb = cbs[nil]
        cb[tail]
      end
    else
      while res
        if cb = cbs.find {|pattern, fun| pattern and matched[pattern]}
          # patterns should be as explicit as possible
          cb[1][self, $~] if cb[1]
          @last = pos
          res = !eos? && scan_until(re)
        else break
        end
      end
      if !eos? and cb = cbs.find {|pair| pair[0].nil?}
        cb[1][tail]
      end
    end
  else
    while res
      cb[self]
      @last = pos
      res = !eos? && scan_until(re)
    end
  end
end

#headObject



168
169
170
# File 'lib/rmtools/text/regexp.rb', line 168

def head
  string[@last...pos-matched_size.to_i]
end

#hl_next(re) ⇒ Object



176
177
178
# File 'lib/rmtools/text/regexp.rb', line 176

def hl_next(re)
  (res = scan_until re) && Painter.hl(string[[pos-1000, 0].max..pos+1000], res)
end

#next_in(n) ⇒ Object



180
181
182
# File 'lib/rmtools/text/regexp.rb', line 180

def next_in(n)
  string[pos+n-1, 1]
end

#prev_in(n) ⇒ Object



184
185
186
# File 'lib/rmtools/text/regexp.rb', line 184

def prev_in(n)
  string[pos-matched_size-n, 1]
end

#tailObject



172
173
174
# File 'lib/rmtools/text/regexp.rb', line 172

def tail 
  post_match || string[pos..-1]
end