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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(value:, location:) ⇒ Int

Returns a new instance of Int.



6802
6803
6804
6805
6806
# File 'lib/syntax_tree/node.rb', line 6802

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6800
6801
6802
# File 'lib/syntax_tree/node.rb', line 6800

def comments
  @comments
end

#valueObject (readonly)

String

the value of the integer



6797
6798
6799
# File 'lib/syntax_tree/node.rb', line 6797

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



6842
6843
6844
# File 'lib/syntax_tree/node.rb', line 6842

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

#accept(visitor) ⇒ Object



6808
6809
6810
# File 'lib/syntax_tree/node.rb', line 6808

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

#child_nodesObject Also known as: deconstruct



6812
6813
6814
# File 'lib/syntax_tree/node.rb', line 6812

def child_nodes
  []
end

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



6816
6817
6818
6819
6820
6821
6822
# File 'lib/syntax_tree/node.rb', line 6816

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



6826
6827
6828
# File 'lib/syntax_tree/node.rb', line 6826

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

#format(q) ⇒ Object



6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
# File 'lib/syntax_tree/node.rb', line 6830

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