Class: KPeg::MatchComposition

Inherits:
Match
  • Object
show all
Defined in:
lib/kpeg/match.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, matches) ⇒ MatchComposition

Returns a new instance of MatchComposition.



31
32
33
34
# File 'lib/kpeg/match.rb', line 31

def initialize(op, matches)
  @op = op
  @matches = matches
end

Instance Attribute Details

#matchesObject (readonly)

Returns the value of attribute matches.



36
37
38
# File 'lib/kpeg/match.rb', line 36

def matches
  @matches
end

#opObject (readonly)

Returns the value of attribute op.



36
37
38
# File 'lib/kpeg/match.rb', line 36

def op
  @op
end

Instance Method Details

#explain(indent = "") ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/kpeg/match.rb', line 38

def explain(indent="")
  puts "#{indent}KPeg::Match:#{object_id.to_s(16)}"
  puts "#{indent}  op: #{@op.inspect}"
  puts "#{indent}  matches:"
  @matches.each do |m|
    m.explain("#{indent}    ")
  end
end

#total_stringObject



47
48
49
# File 'lib/kpeg/match.rb', line 47

def total_string
  @matches.map { |m| m.total_string }.join
end

#value(obj = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kpeg/match.rb', line 51

def value(obj=nil)
  values = @matches.map { |m| m.value(obj) }

  values = @op.prune_values(values)

  unless @op.action
    return values.first if values.size == 1
    return values
  end

  if obj
    obj.instance_exec(*values, &@op.action)
  else
    @op.action.call(*values)
  end
end