Module: Walrat::Memoizing

Included in:
Parslet, ParsletCombination, Predicate
Defined in:
lib/walrat/memoizing.rb

Instance Method Summary collapse

Instance Method Details

#check_left_recursion(parseable, options = {}) ⇒ Object

Can only check for left recursion if memoizing is turned on (the help of the memoizer is needed).



49
50
51
52
# File 'lib/walrat/memoizing.rb', line 49

def check_left_recursion parseable, options = {}
  return unless options.has_key?(:memoizer)
  options[:memoizer].check_left_recursion parseable, options
end

#memoizing_parse(string, options = {}) ⇒ Object

This method provides a clean, optional implementation of memoizing by serving as a wrapper for all parse invocations. Rather than calling the parse methods directly, this method should be called; if it is appropriate to use a memoizer then it will be invoked, otherwise control will fall through to the real parse method. Turning off memoizing is as simple as not passing a value with the :memoizer key in the options hash. This method defined is in a separate module so that it can easily be mixed in with all Parslets, ParsletCombinations and Predicates.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/walrat/memoizing.rb', line 35

def memoizing_parse(string, options = {})
  # will use memoizer if available and not instructed to ignore it
  if options.has_key?(:memoizer) and not
     (options.has_key?(:ignore_memoizer) and options[:ignore_memoizer])
    options[:parseable] = self
    options[:memoizer].parse string, options
  else # otherwise will proceed as normal
    options[:ignore_memoizer] = false
    parse string, options
  end
end