Module: Walrus::Grammar::Memoizing

Included in:
Parslet, ParsletCombination, Predicate
Defined in:
lib/walrus/grammar/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).



38
39
40
41
# File 'lib/walrus/grammar/memoizing.rb', line 38

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 parsing 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.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/walrus/grammar/memoizing.rb', line 24

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