Class: SyntaxTree::Paren
Overview
Paren represents using balanced parentheses in a couple places in a Ruby program. In general parentheses can be used anywhere a Ruby expression can be used.
(1 + 2)
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#contents ⇒ Object
readonly
- nil | untyped
-
the expression inside the parentheses.
-
#lparen ⇒ Object
readonly
- LParen
-
the left parenthesis that opened this statement.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(lparen: nil, contents: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(lparen:, contents:, location:) ⇒ Paren
constructor
A new instance of Paren.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(lparen:, contents:, location:) ⇒ Paren
Returns a new instance of Paren.
8343 8344 8345 8346 8347 8348 |
# File 'lib/syntax_tree/node.rb', line 8343 def initialize(lparen:, contents:, location:) @lparen = lparen @contents = contents @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
8341 8342 8343 |
# File 'lib/syntax_tree/node.rb', line 8341 def comments @comments end |
#contents ⇒ Object (readonly)
- nil | untyped
-
the expression inside the parentheses
8338 8339 8340 |
# File 'lib/syntax_tree/node.rb', line 8338 def contents @contents end |
#lparen ⇒ Object (readonly)
- LParen
-
the left parenthesis that opened this statement
8335 8336 8337 |
# File 'lib/syntax_tree/node.rb', line 8335 def lparen @lparen end |
Instance Method Details
#===(other) ⇒ Object
8397 8398 8399 8400 |
# File 'lib/syntax_tree/node.rb', line 8397 def ===(other) other.is_a?(Paren) && lparen === other.lparen && contents === other.contents end |
#accept(visitor) ⇒ Object
8350 8351 8352 |
# File 'lib/syntax_tree/node.rb', line 8350 def accept(visitor) visitor.visit_paren(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
8354 8355 8356 |
# File 'lib/syntax_tree/node.rb', line 8354 def child_nodes [lparen, contents] end |
#copy(lparen: nil, contents: nil, location: nil) ⇒ Object
8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 |
# File 'lib/syntax_tree/node.rb', line 8358 def copy(lparen: nil, contents: nil, location: nil) node = Paren.new( lparen: lparen || self.lparen, contents: contents || self.contents, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
8372 8373 8374 8375 8376 8377 8378 8379 |
# File 'lib/syntax_tree/node.rb', line 8372 def deconstruct_keys(_keys) { lparen: lparen, contents: contents, location: location, comments: comments } end |
#format(q) ⇒ Object
8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 |
# File 'lib/syntax_tree/node.rb', line 8381 def format(q) q.group do q.format(lparen) if contents && (!contents.is_a?(Params) || !contents.empty?) q.indent do q.breakable_empty q.format(contents) end end q.breakable_empty q.text(")") end end |