Class: Packcr::Node::ExpandNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Packcr::Node

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

Constructor Details

#initialize(index = nil, line = nil, col = nil) ⇒ ExpandNode

Returns a new instance of ExpandNode.



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

def initialize(index = nil, line = nil, col = nil)
  @index = index
  @line = line
  @col = col
end

Instance Attribute Details

#colObject

Returns the value of attribute col.



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

def col
  @col
end

#indexObject

Returns the value of attribute index.



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

def index
  @index
end

#lineObject

Returns the value of attribute line.



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

def line
  @line
end

Instance Method Details

#debug_dump(indent = 0) ⇒ Object



12
13
14
15
16
# File 'lib/packcr/node/expand_node.rb', line 12

def debug_dump(indent = 0)
  $stdout.print "#{" " * indent}Expand(index:"
  Packcr.dump_integer_value(index)
  $stdout.print ")\n"
end

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



18
19
20
# File 'lib/packcr/node/expand_node.rb', line 18

def generate_code(gen, onfail, indent, unwrap, oncut: nil)
  get_code(gen, onfail, indent, unwrap, oncut)
end

#get_code(gen, onfail, indent, unwrap, 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
# File 'lib/packcr/generated/node/expand_node.rb', line 4

def get_code(gen, onfail, indent, unwrap, oncut)
  case gen.lang
  when :c
    erbout = +""
    erbout << "{\n    const size_t n = chunk->capts.buf[#{index}].range.end - chunk->capts.buf[#{index}].range.start;\n    if (packcr_refill_buffer(ctx, n) < n) goto L#{format("%04d", onfail)};\n    if (n > 0) {\n        const char *const p = ctx->buffer.buf + ctx->position_offset;\n        const char *const q = ctx->buffer.buf + chunk->capts.buf[#{index}].range.start;\n        size_t i;\n        for (i = 0; i < n; i++) {\n            if (p[i] != q[i]) goto L#{format("%04d", onfail)};\n        }\n".freeze

    if gen.location
      erbout << "        packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);\n".freeze
    end
    erbout << "        ctx->position_offset += n;\n    }\n}\n".freeze

    erbout
  when :rb
    erbout = +""
    erbout << "capt#{gen.level} = answer.capts[#{index}]\nn#{gen.level} = capt#{gen.level}.range_end - capt#{gen.level}.range_start\nif refill_buffer(n#{gen.level}) < n#{gen.level}\n  throw(#{onfail})\nend\nif n#{gen.level} > 0\n  ptr#{gen.level} = @buffer[@position_offset, n#{gen.level}]\n  q#{gen.level} = @buffer[capt#{gen.level}.range_start, n#{gen.level}]\n  if ptr#{gen.level} != q#{gen.level}\n    throw(#{onfail})\n  end\n".freeze

    if gen.location
      erbout << "  @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, n#{gen.level})\n".freeze
    end
    erbout << "  @position_offset += n#{gen.level}\nend\n".freeze

    erbout
  when :rs
    erbout = +""
    erbout << "/* lib/packcr/templates/node/expand.rs.erb */\n\n".freeze

    erbout
  else
    raise "unknown lang #{gen.lang}"
  end
end

#reachabilityObject



22
23
24
# File 'lib/packcr/node/expand_node.rb', line 22

def reachability
  Packcr::CODE_REACH__BOTH
end

#to_hObject



39
40
41
42
43
44
# File 'lib/packcr/node/expand_node.rb', line 39

def to_h
  {
    type: :expand,
    index: index,
  }
end

#verify_captures(ctx, capts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/packcr/node/expand_node.rb', line 26

def verify_captures(ctx, capts)
  found = capts.any? do |capt|
    unless capt.is_a?(Packcr::Node::CaptureNode)
      raise "unexpected capture: #{capt.class}"
    end

    index == capt.index
  end
  return if found || index.nil?

  ctx.error line + 1, col + 1, "Capture #{index + 1} not available at this position"
end