Class: Packcr::Node::StringNode

Inherits:
Packcr::Node show all
Defined in:
lib/packcr/node/string_node.rb,
lib/packcr/generated/node/string_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_captures, #verify_variables

Constructor Details

#initialize(value = nil) ⇒ StringNode

Returns a new instance of StringNode.



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

def initialize(value = nil)
  self.value = value
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#debug_dump(indent = 0) ⇒ Object



14
15
16
17
18
# File 'lib/packcr/node/string_node.rb', line 14

def debug_dump(indent = 0)
  $stdout.print "#{" " * indent}String(value:'"
  Packcr.dump_escaped_string(value)
  $stdout.print "')\n"
end

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



20
21
22
23
24
25
26
27
28
29
# File 'lib/packcr/node/string_node.rb', line 20

def generate_code(gen, onfail, indent, bare, oncut: nil)
  n = value&.length || 0
  return unless n > 0

  if n > 1
    gen.write Packcr.format_code(get_many_code(gen, onfail, indent, bare, oncut, n), indent: indent)
  else
    gen.write Packcr.format_code(get_one_code(gen, onfail, indent, bare, oncut, n), indent: indent)
  end
end

#get_many_code(gen, onfail, indent, bare, oncut, n) ⇒ 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
# File 'lib/packcr/generated/node/string_node.rb', line 4

def get_many_code(gen, onfail, indent, bare, oncut, n)
  case gen.lang
  when :c
    erbout = +""
    erbout << "if (\n    packcr_refill_buffer(ctx, #{n}) < #{n} ||\n".freeze

    (n - 1).times do |i|
      erbout << "    (ctx->buffer.buf + ctx->position_offset)[#{i}] != '#{Packcr.escape_character(value[i])}' ||\n".freeze
    end
    erbout << "    (ctx->buffer.buf + ctx->position_offset)[#{n - 1}] != '#{Packcr.escape_character(value[n - 1])}'\n) goto L#{format("%04d", onfail)};\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".freeze

    erbout
  when :rb
    erbout = +""
    erbout << "if (\n  refill_buffer(#{n}) < #{n} ||\n  @buffer[@position_offset, #{n}] != #{value[0, n].dump}\n)\n  throw(#{onfail})\nend\n".freeze

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

    erbout
  when :rs
    erbout = +""
    erbout << "if self.refill_buffer(#{n}) < #{n}\n  || self.input.buffer.as_bytes()[self.input.position_offset..(self.input.position_offset + #{n})] != [#{value[0, n].each_char.map { |c| "b'#{Packcr.escape_character(c)}'" }.join(", ")}]\n{\n    break 'L#{format("%04d", onfail)};\n}\n".freeze

    if gen.location
      erbout << "TODO\n".freeze
    end
    erbout << "self.input.position_offset += #{n};\n".freeze

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

#get_one_code(gen, onfail, indent, bare, oncut, n) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/packcr/generated/node/string_node.rb', line 46

def get_one_code(gen, onfail, indent, bare, oncut, n)
  case gen.lang
  when :c
    erbout = +""
    erbout << "if (\n    packcr_refill_buffer(ctx, 1) < 1 ||\n    ctx->buffer.buf[ctx->position_offset] != '#{Packcr.escape_character(value[0])}'\n) goto L#{format("%04d", onfail)};\n".freeze

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

    erbout
  when :rb
    erbout = +""
    erbout << "if (\n  refill_buffer(1) < 1 ||\n  @buffer[@position_offset] != \"#{Packcr.escape_character(value[0])}\"\n)\n  throw(#{onfail})\nend\n".freeze

    if gen.location
      erbout << "@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)\n".freeze
    end
    erbout << "@position_offset += 1\n".freeze

    erbout
  when :rs
    erbout = +""
    erbout << "if self.refill_buffer(1) < 1\n    || self.input.buffer.as_bytes()[self.input.position_offset] != b'#{Packcr.escape_character(value[0])}'\n{\n    break 'L#{format("%04d", onfail)};\n}\n".freeze

    if gen.location
      erbout << "TODO\n".freeze
    end
    erbout << "self.input.position_offset += 1;\n".freeze

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

#reachabilityObject



31
32
33
34
35
36
37
38
# File 'lib/packcr/node/string_node.rb', line 31

def reachability
  n = value&.length || 0
  if n <= 0
    return Packcr::CODE_REACH__ALWAYS_SUCCEED
  end

  Packcr::CODE_REACH__BOTH
end

#to_hObject



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

def to_h
  {
    type: :string,
    value: value,
  }
end