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.



7418
7419
7420
7421
7422
# File 'lib/syntax_tree.rb', line 7418

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



7416
7417
7418
# File 'lib/syntax_tree.rb', line 7416

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



7413
7414
7415
# File 'lib/syntax_tree.rb', line 7413

def location
  @location
end

#valueObject (readonly)

String

the value of the integer



7410
7411
7412
# File 'lib/syntax_tree.rb', line 7410

def value
  @value
end

Instance Method Details

#child_nodesObject



7424
7425
7426
# File 'lib/syntax_tree.rb', line 7424

def child_nodes
  []
end

#format(q) ⇒ Object



7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
# File 'lib/syntax_tree.rb', line 7428

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



7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
# File 'lib/syntax_tree.rb', line 7440

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



7451
7452
7453
# File 'lib/syntax_tree.rb', line 7451

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