Class: SyntaxTree::StringConcat

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

StringConcat represents concatenating two strings together using a backward slash.

"first" \
  "second"

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(left:, right:, location:) ⇒ StringConcat

Returns a new instance of StringConcat.



10118
10119
10120
10121
10122
10123
# File 'lib/syntax_tree/node.rb', line 10118

def initialize(left:, right:, location:)
  @left = left
  @right = right
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10116
10117
10118
# File 'lib/syntax_tree/node.rb', line 10116

def comments
  @comments
end

#leftObject (readonly)

Heredoc | StringConcat | StringLiteral

the left side of the

concatenation



10110
10111
10112
# File 'lib/syntax_tree/node.rb', line 10110

def left
  @left
end

#rightObject (readonly)

StringLiteral

the right side of the concatenation



10113
10114
10115
# File 'lib/syntax_tree/node.rb', line 10113

def right
  @right
end

Instance Method Details

#===(other) ⇒ Object



10162
10163
10164
# File 'lib/syntax_tree/node.rb', line 10162

def ===(other)
  other.is_a?(StringConcat) && left === other.left && right === other.right
end

#accept(visitor) ⇒ Object



10125
10126
10127
# File 'lib/syntax_tree/node.rb', line 10125

def accept(visitor)
  visitor.visit_string_concat(self)
end

#child_nodesObject Also known as: deconstruct



10129
10130
10131
# File 'lib/syntax_tree/node.rb', line 10129

def child_nodes
  [left, right]
end

#copy(left: nil, right: nil, location: nil) ⇒ Object



10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
# File 'lib/syntax_tree/node.rb', line 10133

def copy(left: nil, right: nil, location: nil)
  node =
    StringConcat.new(
      left: left || self.left,
      right: right || self.right,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



10147
10148
10149
# File 'lib/syntax_tree/node.rb', line 10147

def deconstruct_keys(_keys)
  { left: left, right: right, location: location, comments: comments }
end

#format(q) ⇒ Object



10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
# File 'lib/syntax_tree/node.rb', line 10151

def format(q)
  q.group do
    q.format(left)
    q.text(" \\")
    q.indent do
      q.breakable_force
      q.format(right)
    end
  end
end