Module: Parsby::Example::ArithmeticParser

Extended by:
ArithmeticParser
Includes:
Combinators
Included in:
ArithmeticParser
Defined in:
lib/parsby/example/arithmetic_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Combinators

#splicer

Methods included from Combinators::ModuleMethods

#define_combinator

Class Method Details

.define_binary_op(name, op) ⇒ Object



12
13
14
15
16
# 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) ⇒ Object



24
25
26
27
28
# 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

Instance Method Details

#left_associative_binary_precedence_level(hpe, operators) ⇒ Object



47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



8
9
10
# File 'lib/parsby/example/arithmetic_parser.rb', line 8

def parse(io)
  expr.parse io
end

#right_associative_binary_precedence_level(hpe, operators) ⇒ Object

hpe - higher precedence level spe - same precedence level



36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# 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