Class: Packcr::Node::QuantityNode

Inherits:
Packcr::Node show all
Defined in:
lib/packcr/node/quantity_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Packcr::Node

#alt, #link_references, #reversible?, #seq, #sequence?, #setup, #setup_rule, #verify_captures, #verify_variables

Constructor Details

#initialize(expr = nil, min = 0, max = 0) ⇒ QuantityNode

Returns a new instance of QuantityNode.



6
7
8
9
10
11
# File 'lib/packcr/node/quantity_node.rb', line 6

def initialize(expr = nil, min = 0, max = 0)
  super()
  @expr = expr
  @min = min
  @max = max
end

Instance Attribute Details

#exprObject

Returns the value of attribute expr.



4
5
6
# File 'lib/packcr/node/quantity_node.rb', line 4

def expr
  @expr
end

#maxObject

Returns the value of attribute max.



4
5
6
# File 'lib/packcr/node/quantity_node.rb', line 4

def max
  @max
end

#minObject

Returns the value of attribute min.



4
5
6
# File 'lib/packcr/node/quantity_node.rb', line 4

def min
  @min
end

Instance Method Details

#debug_dump(indent = 0) ⇒ Object



13
14
15
16
17
# File 'lib/packcr/node/quantity_node.rb', line 13

def debug_dump(indent = 0)
  $stdout.print "#{" " * indent}Quantity(min:#{min}, max:#{max}) {\n"
  expr.debug_dump(indent + 2)
  $stdout.print "#{" " * indent}}\n"
end

#generate_code(gen, onfail, indent, bare, oncut: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/packcr/node/quantity_node.rb', line 19

def generate_code(gen, onfail, indent, bare, oncut: nil)
  if max > 1 || max < 0
    gen.write Packcr.template("node/quantify_many.#{gen.lang}.erb", binding, indent: indent, unwrap: bare)
  elsif max == 1
    if min > 0
      gen.write gen.generate_code(expr, onfail, indent, bare)
    else
      gen.write Packcr.template("node/quantify_one.#{gen.lang}.erb", binding, indent: indent, unwrap: bare)
    end
  end
end

#nodesObject



51
52
53
# File 'lib/packcr/node/quantity_node.rb', line 51

def nodes
  [expr]
end

#reachabilityObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/packcr/node/quantity_node.rb', line 31

def reachability
  if max > 1 || max < 0
    if min <= 0
      return Packcr::CODE_REACH__ALWAYS_SUCCEED
    end
    if expr.reachability == Packcr::CODE_REACH__ALWAYS_FAIL
      return Packcr::CODE_REACH__ALWAYS_FAIL
    end
    return Packcr::CODE_REACH__BOTH
  elsif max == 1
    if min > 0
      return expr.reachability
    else
      return Packcr::CODE_REACH__ALWAYS_SUCCEED
    end
  else
    return Packcr::CODE_REACH__ALWAYS_SUCCEED
  end
end

#to_hObject



55
56
57
58
59
60
61
62
# File 'lib/packcr/node/quantity_node.rb', line 55

def to_h
  {
    type: :predicate,
    expr: expr&.to_h,
    min: min,
    max: max,
  }
end