Class: DParse::Parsers::Except

Inherits:
DParse::Parser show all
Defined in:
lib/d-parse/parsers/primitives/except.rb

Instance Method Summary collapse

Methods inherited from DParse::Parser

#apply, #bind, #capture, #compact, #first, #flatten, #ignore, #map, #match?, #second, #select_even, #select_odd, #to_s

Constructor Details

#initialize(parser, bad_parser) ⇒ Except

Returns a new instance of Except.



4
5
6
7
# File 'lib/d-parse/parsers/primitives/except.rb', line 4

def initialize(parser, bad_parser)
  @parser = parser
  @bad_parser = bad_parser
end

Instance Method Details

#expectation_messageObject



28
29
30
# File 'lib/d-parse/parsers/primitives/except.rb', line 28

def expectation_message
  @parser.expectation_message + ', not ' + @bad_parser.expectation_message
end

#inspectObject



24
25
26
# File 'lib/d-parse/parsers/primitives/except.rb', line 24

def inspect
  "except(#{@parser.inspect}, #{@bad_parser.inspect})"
end

#read(input, pos) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/d-parse/parsers/primitives/except.rb', line 9

def read(input, pos)
  res = @parser.read(input, pos)
  case res
  when Success
    bad_res = @bad_parser.read(input, pos)
    if bad_res.is_a?(Success) && bad_res.pos.index == res.pos.index
      Failure.new(input, pos, origin: self)
    else
      res
    end
  when Failure
    res
  end
end