Class: SyntaxTree::Int

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Int represents an integer number literal.

1

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

#initialize(value:, location:, comments: []) ⇒ Int

Returns a new instance of Int.



6318
6319
6320
6321
6322
# File 'lib/syntax_tree/node.rb', line 6318

def initialize(value:, location:, comments: [])
  @value = value
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6316
6317
6318
# File 'lib/syntax_tree/node.rb', line 6316

def comments
  @comments
end

#valueObject (readonly)

String

the value of the integer



6313
6314
6315
# File 'lib/syntax_tree/node.rb', line 6313

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



6324
6325
6326
# File 'lib/syntax_tree/node.rb', line 6324

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



6330
6331
6332
# File 'lib/syntax_tree/node.rb', line 6330

def deconstruct_keys(keys)
  { value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
# File 'lib/syntax_tree/node.rb', line 6334

def format(q)
  if !value.start_with?(/\+?0/) && value.length >= 5 && !value.include?("_")
    # If it's a plain integer and it doesn't have any underscores separating
    # the values, then we're going to insert them every 3 characters
    # starting from the right.
    index = (value.length + 2) % 3
    q.text("  #{value}"[index..-1].scan(/.../).join("_").strip)
  else
    q.text(value)
  end
end

#pretty_print(q) ⇒ Object



6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
# File 'lib/syntax_tree/node.rb', line 6346

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("int")

    q.breakable
    q.pp(value)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



6357
6358
6359
# File 'lib/syntax_tree/node.rb', line 6357

def to_json(*opts)
  { type: :int, value: value, loc: location, cmts: comments }.to_json(*opts)
end