Class: Citrus::MemoizedInput

Inherits:
Input show all
Defined in:
lib/citrus.rb

Overview

A MemoizedInput is an Input that caches segments of the event stream for particular rules in a parse. This technique (also known as “Packrat” parsing) guarantees parsers will operate in linear time but costs significantly more in terms of time and memory required to perform a parse. For more information, please read the paper on Packrat parsing at pdos.csail.mit.edu/~baford/packrat/icfp02/.

Instance Attribute Summary collapse

Attributes inherited from Input

#max_offset, #source

Instance Method Summary collapse

Methods inherited from Input

#exec, #line, #line_index, #line_number, #line_offset, #lines, #test

Constructor Details

#initialize(string) ⇒ MemoizedInput

Returns a new instance of MemoizedInput.



299
300
301
302
303
# File 'lib/citrus.rb', line 299

def initialize(string)
  super(string)
  @cache = {}
  @cache_hits = 0
end

Instance Attribute Details

#cacheObject (readonly)

A nested hash of rules to offsets and their respective matches.



306
307
308
# File 'lib/citrus.rb', line 306

def cache
  @cache
end

#cache_hitsObject (readonly)

The number of times the cache was hit.



309
310
311
# File 'lib/citrus.rb', line 309

def cache_hits
  @cache_hits
end

Instance Method Details

#memoized?Boolean

Returns true when using memoization to cache match results.

Returns:

  • (Boolean)


318
319
320
# File 'lib/citrus.rb', line 318

def memoized?
  true
end

#resetObject

:nodoc:



311
312
313
314
315
# File 'lib/citrus.rb', line 311

def reset # :nodoc:
  @cache.clear
  @cache_hits = 0
  super
end