Module: MilkTea::ImportedBindings::Generator::GeneratorNaming

Included in:
MilkTea::ImportedBindings::Generator
Defined in:
lib/milk_tea/bindings/imported_bindings/naming.rb

Constant Summary collapse

OPENGL_TYPED_SUFFIX_TOKENS =
[
  ["ui64v", %w[uint64 values]],
  ["i64v", %w[int64 values]],
  ["uiv", %w[uint values]],
  ["iv", %w[int values]],
  ["fi", %w[float int]],
  ["fv", %w[float values]],
  ["dv", %w[double values]],
  ["ubv", %w[ubyte values]],
  ["usv", %w[ushort values]],
  ["bv", %w[byte values]],
  ["sv", %w[short values]],
  ["ui64", %w[uint64]],
  ["i64", %w[int64]],
  ["ui", %w[uint]],
  ["i", %w[int]],
  ["f", %w[float]],
  ["d", %w[double]],
  ["ub", %w[ubyte]],
  ["us", %w[ushort]],
  ["b", %w[byte]],
  ["s", %w[short]],
  ["v", %w[values]],
].freeze
OPENGL_DOMAIN_MARKERS =
{
  "i" => "integer",
  "l" => "long",
  "p" => "packed",
}.freeze
OPENGL_INDEXED_PARAM_NAMES =
%w[buf index mask_number].freeze
OPENGL_TYPED_ALPHA_STEMS =
%w[
  array
  attrib
  block
  boolean
  buffer
  double
  feedback
  float
  framebuffer
  indexed
  integer
  internalformat
  interface
  multisample
  object
  parameter
  pipeline
  pointer
  program
  query
  resource
  shader
  stage
  subroutine
  subroutines
  sync
  uniform
  uniforms
].freeze
OPENGL_TYPED_QUERY_BASES =
%w[boolean double float integer].freeze
OPENGL_NUMERIC_SUFFIX_CONTEXTS =
%w[
  attrib
  boolean
  double
  feedback
  float
  indexed
  int
  integer
  integer64
  long
  matrix
  normalized
  object
  packed
  parameter
  uint
  uniform
].freeze

Instance Method Summary collapse

Instance Method Details

#camelize_binding_name(name) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/milk_tea/bindings/imported_bindings/naming.rb', line 262

def camelize_binding_name(name)
  parts = name.to_s.split("_").reject(&:empty?)
  return "" if parts.empty?

  parts.map do |part|
    if part.match?(/\A\d+\z/)
      part
    else
      part[0].upcase + part[1..].to_s.downcase
    end
  end.join
end

#expand_opengl_special_token(token, numeric_context:) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/milk_tea/bindings/imported_bindings/naming.rb', line 216

def expand_opengl_special_token(token, numeric_context:)
  return %w[integer int values] if token == "iiv"
  return %w[integer uint values] if token == "iuiv"

  if token.start_with?("n") && token.length > 1
    expanded = expand_opengl_typed_token(token[1..], numeric_context: true)
    return ["normalized", *expanded] if expanded
  end

  expand_opengl_typed_token(token, numeric_context:)
end

#expand_opengl_typed_token(token, numeric_context:) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/milk_tea/bindings/imported_bindings/naming.rb', line 228

def expand_opengl_typed_token(token, numeric_context:)
  OPENGL_TYPED_SUFFIX_TOKENS.each do |suffix, replacement|
    next unless token.end_with?(suffix)

    prefix = token.delete_suffix(suffix)
    if prefix.empty?
      return nil if suffix == "v"
      return replacement if numeric_context

      next
    end

    if prefix.match?(/\A\d+\z|\A\d+x\d+\z/)
      return nil unless numeric_context

      return [prefix, *replacement]
    end

    if prefix.match?(/\A[a-z]+\z/) && OPENGL_TYPED_ALPHA_STEMS.include?(prefix)
      return [prefix, *replacement]
    end
  end

  nil
end

#normalize_opengl_snake_case(name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/milk_tea/bindings/imported_bindings/naming.rb', line 88

def normalize_opengl_snake_case(name)
  tokens = name.to_s.split("_").reject(&:empty?)
  normalized = []
  index = 0

  while index < tokens.length
    token = tokens[index]
    next_token = tokens[index + 1]

    case token
    when "getn"
      normalized.concat(%w[get n])
      index += 1
      next
    when "64i"
      if next_token == "v"
        if normalized.last == "integer"
          normalized[-1] = "integer64"
          normalized.concat(%w[indexed values])
        else
          normalized.concat(%w[int64 indexed values])
        end
        index += 2
        next
      end
    when "i"
      if next_token == "v"
        if OPENGL_TYPED_QUERY_BASES.include?(normalized.last)
          normalized.concat(%w[indexed values])
        else
          normalized.concat(%w[int indexed values])
        end
        index += 2
        next
      end
    when "i64"
      if next_token == "v"
        normalized.concat(%w[int64 indexed values])
        index += 2
        next
      end
    when "ui64"
      if next_token == "v"
        normalized.concat(%w[uint64 indexed values])
        index += 2
        next
      end
    when "64v"
      case normalized.last
      when "integer"
        normalized[-1] = "integer64"
        normalized << "values"
        index += 1
        next
      when "int"
        normalized[-1] = "int64"
        normalized << "values"
        index += 1
        next
      when "uint"
        normalized[-1] = "uint64"
        normalized << "values"
        index += 1
        next
      end
    end

    if (domain_marker = opengl_domain_marker(token, next_token))
      normalized << domain_marker
      index += 1
      next
    end

    if (expanded = expand_opengl_special_token(token, numeric_context: opengl_numeric_suffix_context?(normalized)))
      normalized.concat(expanded)
      index += 1
      next
    end

    normalized << token
    index += 1
  end

  normalized.join("_")
end

#normalize_opengl_terminal_suffix(name, raw_name) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/milk_tea/bindings/imported_bindings/naming.rb', line 174

def normalize_opengl_terminal_suffix(name, raw_name)
  normalized = if name.end_with?("indexedfv")
    "#{name.delete_suffix('indexedfv')}indexed_float_values"
  elsif name.end_with?("indexedf")
    "#{name.delete_suffix('indexedf')}indexed_float"
  else
    name
  end

  raw_text = raw_name.to_s
  if raw_text.end_with?("i") && normalized.end_with?("i")
    suffix = opengl_indexed_terminal_variant?(raw_name) ? "indexed" : "int"
    return "#{normalized.delete_suffix('i')}_#{suffix}"
  end

  return "#{normalized.delete_suffix('f')}_float" if raw_text.end_with?("f") && normalized.end_with?("f")

  normalized
end

#opengl_domain_marker(token, next_token) ⇒ Object



203
204
205
206
207
208
209
# File 'lib/milk_tea/bindings/imported_bindings/naming.rb', line 203

def opengl_domain_marker(token, next_token)
  return unless OPENGL_DOMAIN_MARKERS.key?(token)
  return unless next_token
  return unless next_token == "format" || next_token == "pointer" || next_token.match?(/\A\d/)

  OPENGL_DOMAIN_MARKERS.fetch(token)
end

#opengl_indexed_terminal_variant?(raw_name) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
197
198
199
200
201
# File 'lib/milk_tea/bindings/imported_bindings/naming.rb', line 194

def opengl_indexed_terminal_variant?(raw_name)
  raw_declaration = @raw_function_declarations[raw_name]
  return false unless raw_declaration

  raw_declaration.params
    .map { |param| snake_case(param.name) }
    .any? { |name| OPENGL_INDEXED_PARAM_NAMES.include?(name) }
end

#opengl_numeric_suffix_context?(normalized_tokens) ⇒ Boolean

Returns:

  • (Boolean)


211
212
213
214
# File 'lib/milk_tea/bindings/imported_bindings/naming.rb', line 211

def opengl_numeric_suffix_context?(normalized_tokens)
  last_token = normalized_tokens.last
  last_token && OPENGL_NUMERIC_SUFFIX_CONTEXTS.include?(last_token)
end

#openglize_binding_name(name) ⇒ Object



275
276
277
278
279
280
# File 'lib/milk_tea/bindings/imported_bindings/naming.rb', line 275

def openglize_binding_name(name)
  name.to_s
    .gsub(/([A-Za-z])((?:i64|i|ui)_v)(?=\z|_)/, '\\1_\\2')
    .gsub(/(?<!_)([A-Za-z])(\d[A-Za-z0-9]*)(?=[A-Z_]|\z)/, '\1_\2')
    .gsub(/_(\d)D(?=[A-Z_]|\z)/, '_\1d')
end

#snake_case(name) ⇒ Object



254
255
256
257
258
259
260
# File 'lib/milk_tea/bindings/imported_bindings/naming.rb', line 254

def snake_case(name)
  name.to_s
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\\1_\\2')
    .gsub(/([a-z0-9])([A-Z])/, '\\1_\\2')
    .downcase
    .gsub(/([a-z])(\d+)_d\b/, '\1_\2d')
end