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.
8504 8505 8506 8507 8508 8509 |
# File 'lib/syntax_tree/node.rb', line 8504 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
8502 8503 8504 |
# File 'lib/syntax_tree/node.rb', line 8502 def comments @comments end |
#contents ⇒ Object (readonly)
- nil | Node
-
the expression inside the parentheses
8499 8500 8501 |
# File 'lib/syntax_tree/node.rb', line 8499 def contents @contents end |
#lparen ⇒ Object (readonly)
- LParen
-
the left parenthesis that opened this statement
8496 8497 8498 |
# File 'lib/syntax_tree/node.rb', line 8496 def lparen @lparen end |
Instance Method Details
#===(other) ⇒ Object
8560 8561 8562 8563 |
# File 'lib/syntax_tree/node.rb', line 8560 def ===(other) other.is_a?(Paren) && lparen === other.lparen && contents === other.contents end |
#accept(visitor) ⇒ Object
8511 8512 8513 |
# File 'lib/syntax_tree/node.rb', line 8511 def accept(visitor) visitor.visit_paren(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
8515 8516 8517 |
# File 'lib/syntax_tree/node.rb', line 8515 def child_nodes [lparen, contents] end |
#copy(lparen: nil, contents: nil, location: nil) ⇒ Object
8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 |
# File 'lib/syntax_tree/node.rb', line 8519 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
8533 8534 8535 8536 8537 8538 8539 8540 |
# File 'lib/syntax_tree/node.rb', line 8533 def deconstruct_keys(_keys) { lparen: lparen, contents: contents, location: location, comments: comments } end |
#format(q) ⇒ Object
8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 |
# File 'lib/syntax_tree/node.rb', line 8542 def format(q) contents = self.contents 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 |