class Parby::Parser

Public Class Methods

new(&block) click to toggle source

{ |input, index| } -> result or { |input| } -> result

# 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

Public Instance Methods

>>(other) click to toggle source
# File lib/parby/parser.rb, line 46
def >>(other)
  self.and other
end
and(another_parser) click to toggle source
# 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() click to toggle source
# File lib/parby/parser.rb, line 66
def consuming
  self >> Parby.all
end
fmap() { |value| ... } click to toggle source
# 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) click to toggle source
# File lib/parby/parser.rb, line 62
def map(&block)
  fmap(&block)
end
or(another_parser) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/parby/parser.rb, line 30
def |(other)
  self.or other
end