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.



6771
6772
6773
6774
6775
# File 'lib/syntax_tree/node.rb', line 6771

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6769
6770
6771
# File 'lib/syntax_tree/node.rb', line 6769

def comments
  @comments
end

#valueObject (readonly)

String

the value of the integer



6766
6767
6768
# File 'lib/syntax_tree/node.rb', line 6766

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



6777
6778
6779
# File 'lib/syntax_tree/node.rb', line 6777

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

#child_nodesObject Also known as: deconstruct



6781
6782
6783
# File 'lib/syntax_tree/node.rb', line 6781

def child_nodes
  []
end

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



6785
6786
6787
6788
6789
6790
6791
# File 'lib/syntax_tree/node.rb', line 6785

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



6795
6796
6797
# File 'lib/syntax_tree/node.rb', line 6795

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

#format(q) ⇒ Object



6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
# File 'lib/syntax_tree/node.rb', line 6799

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