Class: Packcr::Node::CaptureNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Packcr::Node

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

Constructor Details

#initialize(expr = nil) ⇒ CaptureNode

Returns a new instance of CaptureNode.



6
7
8
# File 'lib/packcr/node/capture_node.rb', line 6

def initialize(expr = nil)
  @expr = expr
end

Instance Attribute Details

#exprObject

Returns the value of attribute expr.



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

def expr
  @expr
end

#indexObject

Returns the value of attribute index.



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

def index
  @index
end

Instance Method Details

#debug_dump(indent = 0) ⇒ Object



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

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

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



18
19
20
# File 'lib/packcr/node/capture_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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/packcr/generated/node/capture_node.rb', line 4

def get_code(gen, onfail, indent, unwrap, oncut)
  case gen.lang
  when :c
    erbout = +""
    erbout << "{\n    const size_t p = ctx->position_offset;\n    size_t q;\n".freeze

    if gen.location
      erbout << "    packcr_location_t p_loc = ctx->position_offset_loc;\n    packcr_location_t q_loc;\n".freeze
    end
    erbout << "#{gen.generate_code(expr, onfail, 4, false)}    q = ctx->position_offset;\n    chunk->capts.buf[#{index}].range.start = p;\n    chunk->capts.buf[#{index}].range.end = q;\n".freeze

    if gen.location
      erbout << "    q_loc = ctx->position_offset_loc;\n    chunk->capts.buf[#{index}].range.start_loc = p_loc;\n    chunk->capts.buf[#{index}].range.end_loc = q_loc;\n".freeze
    end
    erbout << "}\n".freeze

    erbout
  when :rb
    erbout = +""
    erbout << "pos#{gen.level} = @position_offset\n".freeze

    if gen.location
      erbout << "p_loc#{gen.level} = @position_offset_loc\n".freeze
    end

    erbout << "#{gen.generate_code(expr, onfail, 0, false)}q#{gen.level} = @position_offset\ncapt#{gen.level} = answer.capts[#{index}]\ncapt#{gen.level}.range_start = pos#{gen.level}\ncapt#{gen.level}.range_end = q#{gen.level}\n".freeze

    if gen.location
      erbout << "q_loc#{gen.level} = @position_offset_loc\ncapt#{gen.level}.start_loc = p_loc#{gen.level}\ncapt#{gen.level}.end_loc = q_loc#{gen.level}\n".freeze
    end
    erbout
  when :rs
    erbout = +""
    erbout << "let p_inner = self.input.position_offset;\n".freeze

    if gen.location
      erbout << "TODO\n".freeze
    end
    erbout << "{\n#{gen.generate_code(expr, onfail, 4, false)}}\nlet q = self.input.position_offset;\nanswer.capts[#{index}].start = p_inner;\nanswer.capts[#{index}].end = q;\n".freeze

    if gen.location
      erbout << "TODO\n".freeze
    end
    erbout
  else
    raise "unknown lang #{gen.lang}"
  end
end

#nodesObject



31
32
33
# File 'lib/packcr/node/capture_node.rb', line 31

def nodes
  [expr]
end

#reachabilityObject



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

def reachability
  expr.reachability
end

#setup_rule(rule) ⇒ Object



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

def setup_rule(rule)
  @index = rule.capts.length
  rule.capts << self
end

#to_hObject



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

def to_h
  {
    type: :capture,
    expr: expr&.to_h,
  }
end

#verify_captures(ctx, capts) ⇒ Object



26
27
28
29
# File 'lib/packcr/node/capture_node.rb', line 26

def verify_captures(ctx, capts)
  super
  capts.push(self)
end