Class: Packcr::Node::ActionNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Packcr::Node

#alt, #link_references, #nodes, #reversible?, #seq, #sequence?, #setup

Constructor Details

#initialize(code = nil) ⇒ ActionNode

Returns a new instance of ActionNode.



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

def initialize(code = nil)
  super()
  @code = code
  self.vars = []
  self.capts = []
end

Instance Attribute Details

#captsObject

Returns the value of attribute capts.



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

def capts
  @capts
end

#codeObject

Returns the value of attribute code.



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

def code
  @code
end

#indexObject

Returns the value of attribute index.



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

def index
  @index
end

#varsObject

Returns the value of attribute vars.



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

def vars
  @vars
end

Instance Method Details

#debug_dump(indent = 0) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/packcr/node/action_node.rb', line 13

def debug_dump(indent = 0)
  $stdout.print "#{" " * indent}Action(index:"
  Packcr.dump_integer_value(index)
  $stdout.print ", code:{"
  Packcr.dump_escaped_string(code.text)
  $stdout.print "}, vars:"

  vars = self.vars
  capts = self.capts
  if vars.length + capts.length > 0
    $stdout.print "\n"
    vars.each do |ref|
      $stdout.print "#{" " * (indent + 2)}'#{ref.var}'\n"
    end
    capts.each do |capt|
      $stdout.print "#{" " * (indent + 2)}$#{capt.index + 1}\n"
    end
    $stdout.print "#{" " * indent})\n"
  else
    $stdout.print "none)\n"
  end
end

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



36
37
38
# File 'lib/packcr/node/action_node.rb', line 36

def generate_code(gen, onfail, indent, bare, oncut: nil)
  gen.write Packcr.template("node/action.#{gen.lang}.erb", binding, indent: indent, unwrap: bare)
end

#reachabilityObject



40
41
42
# File 'lib/packcr/node/action_node.rb', line 40

def reachability
  Packcr::CODE_REACH__ALWAYS_SUCCEED
end

#setup_rule(rule) ⇒ Object



52
53
54
55
# File 'lib/packcr/node/action_node.rb', line 52

def setup_rule(rule)
  @index = rule.codes.length
  rule.codes.push(self)
end

#to_hObject



57
58
59
60
61
62
63
64
# File 'lib/packcr/node/action_node.rb', line 57

def to_h
  {
    type: :action,
    code: code&.text,
    vars: vars&.map(&:to_h),
    capts: capts&.map(&:to_h),
  }
end

#verify_captures(ctx, capts) ⇒ Object



48
49
50
# File 'lib/packcr/node/action_node.rb', line 48

def verify_captures(ctx, capts)
  @capts = capts
end

#verify_variables(vars) ⇒ Object



44
45
46
# File 'lib/packcr/node/action_node.rb', line 44

def verify_variables(vars)
  @vars = vars
end