Class: SyntaxTree::Int

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



7550
7551
7552
7553
7554
# File 'lib/syntax_tree.rb', line 7550

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



7548
7549
7550
# File 'lib/syntax_tree.rb', line 7548

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



7545
7546
7547
# File 'lib/syntax_tree.rb', line 7545

def location
  @location
end

#valueObject (readonly)

String

the value of the integer



7542
7543
7544
# File 'lib/syntax_tree.rb', line 7542

def value
  @value
end

Instance Method Details

#child_nodesObject



7556
7557
7558
# File 'lib/syntax_tree.rb', line 7556

def child_nodes
  []
end

#format(q) ⇒ Object



7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
# File 'lib/syntax_tree.rb', line 7560

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



7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
# File 'lib/syntax_tree.rb', line 7572

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



7583
7584
7585
# File 'lib/syntax_tree.rb', line 7583

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