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, #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, unwrap, oncut: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/packcr/node/string_node.rb', line 24

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

  if n > 1
    get_many_code(gen, onfail, indent, unwrap, oncut, n)
  else
    get_one_code(gen, onfail, indent, unwrap, oncut, n)
  end
end

#generate_reverse_code(gen, onsuccess, indent, unwrap, oncut: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/packcr/node/string_node.rb', line 35

def generate_reverse_code(gen, onsuccess, indent, unwrap, oncut: nil)
  n = value&.length || 0

  if n > 1
    get_many_reverse_code(gen, onsuccess, indent, unwrap, oncut, n)
  else
    get_one_reverse_code(gen, onsuccess, indent, unwrap, oncut, n)
  end
end

#get_many_code(gen, onfail, indent, unwrap, 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, unwrap, 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.input.refill_buffer(#{n}) < #{n}\n    || !self.input.buffer[self.input.position_offset..] .starts_with(\"#{value[0, n].each_char.map { |c| "#{Packcr.escape_character(c)}" }.join}\")\n{\n    return throw(#{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_many_reverse_code(gen, onsuccess, indent, unwrap, oncut, n) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/packcr/generated/node/string_node.rb', line 46

def get_many_reverse_code(gen, onsuccess, indent, unwrap, oncut, n)
  case gen.lang
  when :rs
    erbout = +""
    erbout << "if self.input.refill_buffer(#{n}) >= #{n}\n    && self.input.buffer[self.input.position_offset..].starts_with(\"#{value[0, n].each_char.map { |c| "#{Packcr.escape_character(c)}" }.join}\")\n{\n    self.input.position_offset += #{n};\n    return throw(#{onsuccess});\n}\n".freeze

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

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/packcr/generated/node/string_node.rb', line 58

def get_one_code(gen, onfail, indent, unwrap, 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.input.refill_buffer(1) < 1\n    || !self.input.buffer[self.input.position_offset..].starts_with(\"#{Packcr.escape_character(value[0])}\")\n{\n    return throw(#{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

#get_one_reverse_code(gen, onsuccess, indent, unwrap, oncut, n) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/packcr/generated/node/string_node.rb', line 95

def get_one_reverse_code(gen, onsuccess, indent, unwrap, oncut, n)
  case gen.lang
  when :rs
    erbout = +""
    erbout << "if self.input.refill_buffer(1) >= 1\n    && self.input.buffer[self.input.position_offset..].starts_with(\"#{Packcr.escape_character(value[0])}\")\n{\n    self.input.position_offset += 1;\n    return throw(#{onsuccess});\n}\n".freeze

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

#reachabilityObject



45
46
47
48
49
50
51
52
# File 'lib/packcr/node/string_node.rb', line 45

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

  Packcr::CODE_REACH__BOTH
end

#reversible?(gen) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/packcr/node/string_node.rb', line 20

def reversible?(gen)
  gen.lang == :rs
end

#to_hObject



54
55
56
57
58
59
# File 'lib/packcr/node/string_node.rb', line 54

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