Class: SyntaxTree::Int
Overview
Int represents an integer number literal.
1
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#value ⇒ Object
readonly
- String
-
the value of the integer.
Attributes inherited from Node
Instance Method Summary collapse
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(value:, location:, comments: []) ⇒ Int
constructor
A new instance of Int.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(value:, location:, comments: []) ⇒ Int
Returns a new instance of Int.
6318 6319 6320 6321 6322 |
# File 'lib/syntax_tree/node.rb', line 6318 def initialize(value:, location:, comments: []) @value = value @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
6316 6317 6318 |
# File 'lib/syntax_tree/node.rb', line 6316 def comments @comments end |
#value ⇒ Object (readonly)
- String
-
the value of the integer
6313 6314 6315 |
# File 'lib/syntax_tree/node.rb', line 6313 def value @value end |
Instance Method Details
#child_nodes ⇒ Object Also known as: deconstruct
6324 6325 6326 |
# File 'lib/syntax_tree/node.rb', line 6324 def child_nodes [] end |
#deconstruct_keys(keys) ⇒ Object
6330 6331 6332 |
# File 'lib/syntax_tree/node.rb', line 6330 def deconstruct_keys(keys) { value: value, location: location, comments: comments } end |
#format(q) ⇒ Object
6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 |
# File 'lib/syntax_tree/node.rb', line 6334 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
6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 |
# File 'lib/syntax_tree/node.rb', line 6346 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
6357 6358 6359 |
# File 'lib/syntax_tree/node.rb', line 6357 def to_json(*opts) { type: :int, value: value, loc: location, cmts: comments }.to_json(*opts) end |