Class: Walrat::Predicate

Inherits:
Object
  • Object
show all
Includes:
Memoizing, ParsletCombining
Defined in:
lib/walrat/predicate.rb

Overview

Predicates parse input without consuming it. On success they throw a subclass-specific symbol (see the AndPredicate and NotPredicate classes). On failure they raise a ParseError.

Direct Known Subclasses

AndPredicate, NotPredicate

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Memoizing

#check_left_recursion, #memoizing_parse

Methods included from ParsletCombining

#&, #>>, #and?, #and_predicate, #choice, #memoizing_parse, #merge, #not!, #not_predicate, #omission, #one_or_more, #optional, #repeat, #repeat_with_default, #repetition, #repetition_with_default, #sequence, #skip, #zero_or_more, #zero_or_one, #|

Constructor Details

#initialize(parseable) ⇒ Predicate

Raises if parseable is nil.

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
# File 'lib/walrat/predicate.rb', line 37

def initialize parseable
  raise ArgumentError, 'nil parseable' if parseable.nil?
  @parseable = parseable

  # fixed offset to avoid collisions with @parseable objects
  @hash = @parseable.hash + hash_offset
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



34
35
36
# File 'lib/walrat/predicate.rb', line 34

def hash
  @hash
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/walrat/predicate.rb', line 53

def eql? other
  other.instance_of? self.class and other.parseable.eql? @parseable
end

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

Raises:

  • (NotImplementedError)


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

def parse string, options = {}
  raise NotImplementedError # subclass responsibility
end

#to_parseableObject



45
46
47
# File 'lib/walrat/predicate.rb', line 45

def to_parseable
  self
end