Class: Protobug::Compiler::Builder::Token

Inherits:
Struct
  • Object
show all
Defined in:
lib/protobug/compiler/builder.rb

Constant Summary collapse

COMPACT_OPERATORS =

rubocop:disable Lint/ConstantDefinitionInBlock

%w[** .. ...].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject

Returns the value of attribute content

Returns:

  • (Object)

    the current value of content



231
232
233
# File 'lib/protobug/compiler/builder.rb', line 231

def content
  @content
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



231
232
233
# File 'lib/protobug/compiler/builder.rb', line 231

def type
  @type
end

Instance Method Details

#compact?Boolean

Returns:

  • (Boolean)


258
259
260
261
262
263
264
265
# File 'lib/protobug/compiler/builder.rb', line 258

def compact?
  case type
  when :operator
    COMPACT_OPERATORS.include?(content)
  when :delimiter
    content == "."
  end
end

#empty?Boolean

Returns:

  • (Boolean)


252
253
254
# File 'lib/protobug/compiler/builder.rb', line 252

def empty?
  false
end

#render(q) ⇒ Object

rubocop:disable Naming/MethodParameterName



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/protobug/compiler/builder.rb', line 233

def render(q) # rubocop:disable Naming/MethodParameterName
  case type
  when :empty, :keyword, :identifier, :delimiter, :operator
    q.text content
  when :literal
    case content
    when Integer
      int_part = Integer(content)
      formatted_int = int_part.abs.to_s.reverse.gsub(/...(?=.)/, '\&_').reverse
      formatted_int.insert(0, "-") if int_part < 0
      q.text formatted_int
    else
      q.text content.inspect
    end
  else
    raise "Unknown token type: #{type}"
  end
end