Class: Rsec::Cached

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/rsec/parsers/misc.rb

Overview

parse result is cached in ctx. may improve performance

Instance Attribute Summary

Attributes included from Parser

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parser

#&, #*, #<<, #>>, #^, #cached, #eof, #fail, #inspect, #join, #map, #maybe, #parse, #parse!, #star, #|

Constructor Details

#initialize(parser) ⇒ Cached

Returns a new instance of Cached.



177
178
179
180
# File 'lib/rsec/parsers/misc.rb', line 177

def initialize parser
  @parser = parser
  @salt = object_id() << 32
end

Class Method Details

.[](parser) ⇒ Object



173
174
175
# File 'lib/rsec/parsers/misc.rb', line 173

def self.[] parser
  self.new parser
end

Instance Method Details

#_parse(ctx) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rsec/parsers/misc.rb', line 182

def _parse ctx
  key = ctx.pos | @salt
  cache = ctx.cache
  # result maybe nil, so don't use ||=
  if cache.has_key? key
    ret, pos = cache[key]
    ctx.pos = pos
    ret
  else
    ret = @parser._parse ctx
    pos = ctx.pos
    cache[key] = [ret, pos]
    ret
  end
end