module Parsby::Example::ArithmeticParser

Public Class Methods

define_binary_op(name, op) click to toggle source
# File lib/parsby/example/arithmetic_parser.rb, line 12
def self.define_binary_op(name, op)
  define_combinator name do |left_subexpr, right_subexpr|
    group(left_subexpr, spaced(ilit(op)), right_subexpr)
  end
end
define_unary_op(name, op) click to toggle source
# File lib/parsby/example/arithmetic_parser.rb, line 24
def self.define_unary_op(name, op)
  define_combinator name do |subexpr|
    group(ilit(op), ws > subexpr)
  end
end

Public Instance Methods

left_associative_binary_precedence_level(hpe, operators) click to toggle source
# File lib/parsby/example/arithmetic_parser.rb, line 47
def left_associative_binary_precedence_level(hpe, operators)
  reduce hpe do |left_expr|
    choice(
      *operators.map do |op|
        send(op, pure(left_expr), hpe)
      end
    )
  end
end
parse(io) click to toggle source
# File lib/parsby/example/arithmetic_parser.rb, line 8
def parse(io)
  expr.parse io
end
right_associative_binary_precedence_level(hpe, operators) click to toggle source

hpe - higher precedence level spe - same precedence level

# File lib/parsby/example/arithmetic_parser.rb, line 36
def right_associative_binary_precedence_level(hpe, operators)
  recursive do |spe|
    choice(
      *operators.map do |op|
        send(op, hpe, spe)
      end,
      hpe,
    )
  end
end
unary_precedence_level(hpe, operators) click to toggle source
# File lib/parsby/example/arithmetic_parser.rb, line 57
def unary_precedence_level(hpe, operators)
  recursive do |spe|
    choice(
      *operators.map do |op|
        send(op, spe)
      end,
      hpe,
    )
  end
end