Class: Packcr::Node::ActionNode

Inherits:
Packcr::Node show all
Defined in:
lib/packcr/node/action_node.rb,
lib/packcr/generated/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.format_code(get_code(gen, onfail, indent, bare, oncut), indent: indent, unwrap: bare)
end

#get_code(gen, onfail, indent, bare, oncut) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/packcr/generated/node/action_node.rb', line 4

def get_code(gen, onfail, indent, bare, oncut)
  case gen.lang
  when :c
    erbout = +""
    erbout << "{\n    packcr_thunk_t *const thunk = packcr_thunk__create_leaf(ctx->auxil, packcr_action_#{gen.rule.name}_#{index}, #{gen.rule.vars.length}, #{gen.rule.capts.length});\n".freeze

    vars.each do |var|
      erbout << "    thunk->data.leaf.values.buf[#{var.index}] = &(chunk->values.buf[#{var.index}]);\n".freeze
    end
    capts.each do |capt|
      erbout << "    thunk->data.leaf.capts.buf[#{capt.index}] = &(chunk->capts.buf[#{capt.index}]);\n".freeze
    end
    erbout << "    thunk->data.leaf.capt0.range.start = chunk->pos;\n    thunk->data.leaf.capt0.range.end = ctx->position_offset;\n".freeze

    if gen.location
      erbout << "    thunk->data.leaf.capt0.range.start_loc = chunk->pos_loc;\n    thunk->data.leaf.capt0.range.end_loc = ctx->position_offset_loc;\n".freeze
    end
    erbout << "    packcr_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);\n}\n".freeze

    erbout
  when :rb
    erbout = +""
    erbout << "answer.thunks.push(\n  ThunkLeaf.new(\n    :action_#{gen.rule.name}_#{index},\n    Capture.new(\n      answer.pos, @position_offset,\n".freeze

    if gen.location
      erbout << "      answer.pos_loc, @position_offset_loc,\n".freeze
    end
    erbout << "    ),\n".freeze

    if vars.empty?
      erbout << "    {},\n".freeze

    else
      erbout << "    answer.values.slice(".freeze
      vars.each_with_index do |var, i|
        erbout << "#{", " if i > 0}#{var.index}".freeze
      end
      erbout << "),\n".freeze
    end
    if capts.empty?
      erbout << "    {},\n".freeze

    else
      erbout << "    answer.capts.slice(".freeze
      capts.each_with_index do |capt, i|
        erbout << "#{", " if i > 0}#{capt.index}".freeze
      end
      erbout << "),\n".freeze
    end
    erbout << "  )\n)\n".freeze

    erbout
  end
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