Class: Farseer::And

Inherits:
Object
  • Object
show all
Includes:
MapFactory
Defined in:
lib/farseer/and.rb

Instance Method Summary collapse

Methods included from MapFactory

#map

Constructor Details

#initialize(*parsers) ⇒ And

Returns a new instance of And.



7
8
9
10
# File 'lib/farseer/and.rb', line 7

def initialize(*parsers)
  @parsers = parsers.flatten
  freeze
end

Instance Method Details

#bind_accumulator(maybe_acc, parser) ⇒ Object



22
23
24
25
26
# File 'lib/farseer/and.rb', line 22

def bind_accumulator(maybe_acc, parser)
  maybe_acc.bind do |acc_result|
    parse_with_rest(parser, acc_result)
  end
end

#combine_results(acc_result, result) ⇒ Object



34
35
36
37
# File 'lib/farseer/and.rb', line 34

def combine_results(acc_result, result)
  combined_tokens = acc_result.token + [result.token]
  Maybe.return(Result.new(combined_tokens, result.rest))
end

#parse(input) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/farseer/and.rb', line 12

def parse(input)
  return Maybe.none if @parsers.empty?

  initial = Maybe.return(Result.new([], input))

  @parsers.reduce(initial) do |maybe_acc, parser|
    bind_accumulator(maybe_acc, parser)
  end
end

#parse_with_rest(parser, acc_result) ⇒ Object



28
29
30
31
32
# File 'lib/farseer/and.rb', line 28

def parse_with_rest(parser, acc_result)
  parser.parse(acc_result.rest).bind do |result|
    combine_results(acc_result, result)
  end
end