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

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(value:, location:) ⇒ Int

Returns a new instance of Int.



6699
6700
6701
6702
6703
# File 'lib/syntax_tree/node.rb', line 6699

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6697
6698
6699
# File 'lib/syntax_tree/node.rb', line 6697

def comments
  @comments
end

#valueObject (readonly)

String

the value of the integer



6694
6695
6696
# File 'lib/syntax_tree/node.rb', line 6694

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



6739
6740
6741
# File 'lib/syntax_tree/node.rb', line 6739

def ===(other)
  other.is_a?(Int) && value === other.value
end

#accept(visitor) ⇒ Object



6705
6706
6707
# File 'lib/syntax_tree/node.rb', line 6705

def accept(visitor)
  visitor.visit_int(self)
end

#child_nodesObject Also known as: deconstruct



6709
6710
6711
# File 'lib/syntax_tree/node.rb', line 6709

def child_nodes
  []
end

#copy(value: nil, location: nil) ⇒ Object



6713
6714
6715
6716
6717
6718
6719
# File 'lib/syntax_tree/node.rb', line 6713

def copy(value: nil, location: nil)
  node =
    Int.new(value: value || self.value, location: location || self.location)

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



6723
6724
6725
# File 'lib/syntax_tree/node.rb', line 6723

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

#format(q) ⇒ Object



6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
# File 'lib/syntax_tree/node.rb', line 6727

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..].scan(/.../).join("_").strip)
  else
    q.text(value)
  end
end