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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Int.



6537
6538
6539
6540
6541
# File 'lib/syntax_tree/node.rb', line 6537

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



6535
6536
6537
# File 'lib/syntax_tree/node.rb', line 6535

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



6532
6533
6534
# File 'lib/syntax_tree/node.rb', line 6532

def location
  @location
end

#valueObject (readonly)

String

the value of the integer



6529
6530
6531
# File 'lib/syntax_tree/node.rb', line 6529

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



6543
6544
6545
# File 'lib/syntax_tree/node.rb', line 6543

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



6549
6550
6551
# File 'lib/syntax_tree/node.rb', line 6549

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

#format(q) ⇒ Object



6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
# File 'lib/syntax_tree/node.rb', line 6553

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



6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
# File 'lib/syntax_tree/node.rb', line 6565

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



6576
6577
6578
# File 'lib/syntax_tree/node.rb', line 6576

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