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.



7345
7346
7347
7348
7349
# File 'lib/syntax_tree.rb', line 7345

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



7343
7344
7345
# File 'lib/syntax_tree.rb', line 7343

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



7340
7341
7342
# File 'lib/syntax_tree.rb', line 7340

def location
  @location
end

#valueObject (readonly)

String

the value of the integer



7337
7338
7339
# File 'lib/syntax_tree.rb', line 7337

def value
  @value
end

Instance Method Details

#child_nodesObject



7351
7352
7353
# File 'lib/syntax_tree.rb', line 7351

def child_nodes
  []
end

#format(q) ⇒ Object



7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
# File 'lib/syntax_tree.rb', line 7355

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



7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
# File 'lib/syntax_tree.rb', line 7367

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



7378
7379
7380
# File 'lib/syntax_tree.rb', line 7378

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