Class: Ragel::Bitmap::Replace

Inherits:
Ripper::SexpBuilderPP
  • Object
show all
Defined in:
lib/ragel/bitmap/replace.rb

Overview

A ruby parser that finds instances of table declarations in a ragel-outputted file.

Defined Under Namespace

Classes: Buffer, Table

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReplace

Returns a new instance of Replace.



125
126
127
128
# File 'lib/ragel/bitmap/replace.rb', line 125

def initialize(*)
  super
  @tables = []
end

Instance Attribute Details

#tablesObject (readonly)

Returns the value of attribute tables.



123
124
125
# File 'lib/ragel/bitmap/replace.rb', line 123

def tables
  @tables
end

Class Method Details

.bitmap_args_from(numbers) ⇒ Object

Get the required args for a bitmap from a set of numbers rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength



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
# File 'lib/ragel/bitmap/replace.rb', line 13

def bitmap_args_from(numbers)
  size = (Math.log2(numbers.max) / 8).ceil

  case size
  when 1
    [:Array8, [numbers.pack('C*')]]
  when 2
    [:Array16, strings_from(2, numbers)]
  when 3
    [:Array24, strings_from(3, numbers)]
  when 4
    if offset?
      [:Array32Offset, [numbers.pack('L*')]]
    else
      [:Array32, strings_from(4, numbers)]
    end
  when 8
    if offset?
      [:Array64Offset, [numbers.pack('Q*')]]
    else
      [:ArrayGeneric, strings_from(size, numbers)]
    end
  else
    [:ArrayGeneric, strings_from(size, numbers)]
  end
end

.offset?Boolean

Check if we can use the offset keyword argument for the unpack1 method introduced here: bugs.ruby-lang.org/issues/18254. If we can, then it’ll be more efficient than storing multiple strings and shifting.

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/ragel/bitmap/replace.rb', line 45

def offset?
  String.instance_method(:unpack1).parameters.any? do |(type, name)|
    type == :key && name == :offset
  end
end

.replace(source) ⇒ Object



141
142
143
144
145
# File 'lib/ragel/bitmap/replace.rb', line 141

def self.replace(source)
  buffer = Buffer.new(source)
  new(source).each_table { |table| buffer.replace(table) }
  buffer.to_source
end

Instance Method Details

#each_table(&block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/ragel/bitmap/replace.rb', line 130

def each_table(&block)
  parse

  if error?
    warn 'Invalid ruby'
    exit 1
  end

  tables.reverse_each(&block)
end