Class: Walrus::Grammar::Predicate

Inherits:
Object
  • Object
show all
Includes:
Memoizing, ParsletCombining
Defined in:
lib/walrus/grammar/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)


31
32
33
34
35
# File 'lib/walrus/grammar/predicate.rb', line 31

def initialize(parseable)
  raise ArgumentError if parseable.nil?
  @parseable = parseable
  @hash = @parseable.hash + hash_offset # fixed offset to avoid collisions with @parseable objects
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



28
29
30
# File 'lib/walrus/grammar/predicate.rb', line 28

def hash
  @hash
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/walrus/grammar/predicate.rb', line 41

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

#to_parseableObject



37
38
39
# File 'lib/walrus/grammar/predicate.rb', line 37

def to_parseable
  self
end