Class: Packcr::Node::ErrorNode

Inherits:
Packcr::Node show all
Defined in:
lib/packcr/node/error_node.rb,
lib/packcr/generated/node/error_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Packcr::Node

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

Constructor Details

#initialize(expr = nil, code = nil, index = nil) ⇒ ErrorNode

Returns a new instance of ErrorNode.



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

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

Instance Attribute Details

#captsObject

Returns the value of attribute capts.



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

def capts
  @capts
end

#codeObject

Returns the value of attribute code.



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

def code
  @code
end

#exprObject

Returns the value of attribute expr.



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

def expr
  @expr
end

#indexObject

Returns the value of attribute index.



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

def index
  @index
end

#varsObject

Returns the value of attribute vars.



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

def vars
  @vars
end

Instance Method Details

#debug_dump(indent = 0) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/packcr/node/error_node.rb', line 15

def debug_dump(indent = 0)
  $stdout.print "#{" " * indent}Error(index:"
  Packcr.dump_integer_value(index)
  $stdout.print ", code:{"
  Packcr.dump_escaped_string(code.text)
  $stdout.print "}, vars:\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"
  expr.debug_dump(indent + 2)
  $stdout.print "#{" " * indent}}\n"
end

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



32
33
34
# File 'lib/packcr/node/error_node.rb', line 32

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
58
59
60
61
62
63
64
# File 'lib/packcr/generated/node/error_node.rb', line 4

def get_code(gen, onfail, indent, bare, oncut)
  case gen.lang
  when :c
    erbout = +""
    erbout << "{\n".freeze

    l = gen.next_label
    m = gen.next_label

    erbout << "#{gen.generate_code(expr, l, 4, true)}    goto L#{format("%04d", m)};\nL#{format("%04d", l)}:;\n    {\n        packcr_value_t null;\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 << "        memset(&null, 0, sizeof(packcr_value_t)); /* in case */\n        thunk->data.leaf.action(ctx, thunk, &null);\n        packcr_thunk__destroy(ctx->auxil, thunk);\n    }\n    goto L#{format("%04d", onfail)};\nL#{format("%04d", m)}:;\n}\n".freeze

    erbout
  when :rb
    erbout = +""
    l = gen.next_label
    m = gen.next_label
    erbout << "catch(#{m}) do\n  catch(#{l}) do\n#{gen.generate_code(expr, l, 4, false)}    throw(#{m})\n  end\n\n  action_#{gen.rule.name}_#{index}(\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    nil,\n    0\n  )\n\n  throw(#{onfail})\nend\n".freeze

    erbout
  end
end

#nodesObject



50
51
52
# File 'lib/packcr/node/error_node.rb', line 50

def nodes
  [expr]
end

#reachabilityObject



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

def reachability
  expr.reachability
end

#setup_rule(rule) ⇒ Object



54
55
56
57
# File 'lib/packcr/node/error_node.rb', line 54

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

#to_hObject



59
60
61
62
63
64
65
66
67
# File 'lib/packcr/node/error_node.rb', line 59

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

#verify_captures(ctx, capts) ⇒ Object



45
46
47
48
# File 'lib/packcr/node/error_node.rb', line 45

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

#verify_variables(vars) ⇒ Object



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

def verify_variables(vars)
  @vars = vars
  super
end