Class: Parby::Parser
- Inherits:
-
Object
- Object
- Parby::Parser
- Defined in:
- lib/parby/parser.rb
Instance Method Summary collapse
- #>>(other) ⇒ Object
- #and(another_parser) ⇒ Object
- #consuming ⇒ Object
- #fmap(&block) ⇒ Object
-
#initialize(&block) ⇒ Parser
constructor
{ |input, index| } -> result or { |input| } -> result.
- #map(&block) ⇒ Object
- #or(another_parser) ⇒ Object
- #parse(*args) ⇒ Object
- #|(other) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Parser
{ |input, index| } -> result or { |input| } -> result
4 5 6 7 8 |
# File 'lib/parby/parser.rb', line 4 def initialize(&block) raise(ArgumentError, 'A bare parser must be initialized with a 1 or 2 argument block') unless block_given? @block = block end |
Instance Method Details
#>>(other) ⇒ Object
46 47 48 |
# File 'lib/parby/parser.rb', line 46 def >>(other) self.and other end |
#and(another_parser) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/parby/parser.rb', line 34 def and(another_parser) Parser.new do |input, index| first = parse(input, index) if first.failed? first else another_parser.parse(first.remaining, first.index) end end end |
#consuming ⇒ Object
66 67 68 |
# File 'lib/parby/parser.rb', line 66 def consuming self >> Parby.all end |
#fmap(&block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/parby/parser.rb', line 50 def fmap(&block) Parser.new do |input, index| result = parse(input, index) if result.succeed? result.value = yield(result.value) end result end end |
#map(&block) ⇒ Object
62 63 64 |
# File 'lib/parby/parser.rb', line 62 def map(&block) fmap(&block) end |
#or(another_parser) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/parby/parser.rb', line 18 def or(another_parser) Parser.new do |input, index| first = parse(input, index) if first.failed? another_parser.parse(input, index) else first end end end |
#parse(*args) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/parby/parser.rb', line 10 def parse(*args) if args.length == 1 @block.call(args[0], 0) else @block.call(args[0], args[1]) end end |
#|(other) ⇒ Object
30 31 32 |
# File 'lib/parby/parser.rb', line 30 def |(other) self.or other end |