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



222
223
224
# File 'lib/protobug/compiler/builder.rb', line 222

def content
  @content
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



222
223
224
# File 'lib/protobug/compiler/builder.rb', line 222

def type
  @type
end

Instance Method Details

#compact?Boolean

Returns:

  • (Boolean)


247
248
249
250
251
252
253
254
# File 'lib/protobug/compiler/builder.rb', line 247

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

#empty?Boolean

Returns:

  • (Boolean)


243
# File 'lib/protobug/compiler/builder.rb', line 243

def empty? = false

#render(q) ⇒ Object

rubocop:disable Naming/MethodParameterName



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/protobug/compiler/builder.rb', line 224

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.negative?
      q.text formatted_int
    else
      q.text content.inspect
    end
  else
    raise "Unknown token type: #{type}"
  end
end