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 | Node
-
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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(lparen:, contents:, location:) ⇒ Paren
Returns a new instance of Paren.
8474 8475 8476 8477 8478 8479 |
# File 'lib/syntax_tree/node.rb', line 8474 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
8472 8473 8474 |
# File 'lib/syntax_tree/node.rb', line 8472 def comments @comments end |
#contents ⇒ Object (readonly)
- nil | Node
-
the expression inside the parentheses
8469 8470 8471 |
# File 'lib/syntax_tree/node.rb', line 8469 def contents @contents end |
#lparen ⇒ Object (readonly)
- LParen
-
the left parenthesis that opened this statement
8466 8467 8468 |
# File 'lib/syntax_tree/node.rb', line 8466 def lparen @lparen end |
Instance Method Details
#===(other) ⇒ Object
8528 8529 8530 8531 |
# File 'lib/syntax_tree/node.rb', line 8528 def ===(other) other.is_a?(Paren) && lparen === other.lparen && contents === other.contents end |
#accept(visitor) ⇒ Object
8481 8482 8483 |
# File 'lib/syntax_tree/node.rb', line 8481 def accept(visitor) visitor.visit_paren(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
8485 8486 8487 |
# File 'lib/syntax_tree/node.rb', line 8485 def child_nodes [lparen, contents] end |
#copy(lparen: nil, contents: nil, location: nil) ⇒ Object
8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 |
# File 'lib/syntax_tree/node.rb', line 8489 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
8503 8504 8505 8506 8507 8508 8509 8510 |
# File 'lib/syntax_tree/node.rb', line 8503 def deconstruct_keys(_keys) { lparen: lparen, contents: contents, location: location, comments: comments } end |
#format(q) ⇒ Object
8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 |
# File 'lib/syntax_tree/node.rb', line 8512 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 |