Class: Citrus::MemoizedInput
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
-
#cache ⇒ Object
readonly
A nested hash of rules to offsets and their respective matches.
-
#cache_hits ⇒ Object
readonly
The number of times the cache was hit.
Attributes inherited from Input
Instance Method Summary collapse
-
#initialize(string) ⇒ MemoizedInput
constructor
A new instance of MemoizedInput.
-
#memoized? ⇒ Boolean
Returns
truewhen using memoization to cache match results. -
#reset ⇒ Object
:nodoc:.
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
#cache ⇒ Object (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_hits ⇒ Object (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.
318 319 320 |
# File 'lib/citrus.rb', line 318 def memoized? true end |
#reset ⇒ Object
:nodoc:
311 312 313 314 315 |
# File 'lib/citrus.rb', line 311 def reset # :nodoc: @cache.clear @cache_hits = 0 super end |