Class: SyntaxTree::StringLiteral
Overview
StringLiteral represents a string literal.
"string"
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#parts ⇒ Object
readonly
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the string literal.
-
#quote ⇒ Object
readonly
- nil | String
-
which quote was used by the string literal.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(parts: nil, quote: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(parts:, quote:, location:) ⇒ StringLiteral
constructor
A new instance of StringLiteral.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(parts:, quote:, location:) ⇒ StringLiteral
Returns a new instance of StringLiteral.
10329 10330 10331 10332 10333 10334 |
# File 'lib/syntax_tree/node.rb', line 10329 def initialize(parts:, quote:, location:) @parts = parts @quote = quote @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
10327 10328 10329 |
# File 'lib/syntax_tree/node.rb', line 10327 def comments @comments end |
#parts ⇒ Object (readonly)
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the
string literal
10321 10322 10323 |
# File 'lib/syntax_tree/node.rb', line 10321 def parts @parts end |
#quote ⇒ Object (readonly)
- nil | String
-
which quote was used by the string literal
10324 10325 10326 |
# File 'lib/syntax_tree/node.rb', line 10324 def quote @quote end |
Instance Method Details
#===(other) ⇒ Object
10403 10404 10405 10406 |
# File 'lib/syntax_tree/node.rb', line 10403 def ===(other) other.is_a?(StringLiteral) && ArrayMatch.call(parts, other.parts) && quote === other.quote end |
#accept(visitor) ⇒ Object
10336 10337 10338 |
# File 'lib/syntax_tree/node.rb', line 10336 def accept(visitor) visitor.visit_string_literal(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
10340 10341 10342 |
# File 'lib/syntax_tree/node.rb', line 10340 def child_nodes parts end |
#copy(parts: nil, quote: nil, location: nil) ⇒ Object
10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 |
# File 'lib/syntax_tree/node.rb', line 10344 def copy(parts: nil, quote: nil, location: nil) node = StringLiteral.new( parts: parts || self.parts, quote: quote || self.quote, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
10358 10359 10360 |
# File 'lib/syntax_tree/node.rb', line 10358 def deconstruct_keys(_keys) { parts: parts, quote: quote, location: location, comments: comments } end |
#format(q) ⇒ Object
10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 |
# File 'lib/syntax_tree/node.rb', line 10362 def format(q) if parts.empty? q.text("#{q.quote}#{q.quote}") return end opening_quote, closing_quote = if !Quotes.locked?(self, q.quote) [q.quote, q.quote] elsif quote&.start_with?("%") [quote, Quotes.matching(quote[/%[qQ]?(.)/, 1])] else [quote, quote] end q.text(opening_quote) q.group do parts.each do |part| if part.is_a?(TStringContent) value = Quotes.normalize(part.value, closing_quote) first = true value.each_line(chomp: true) do |line| if first first = false else q.breakable_return end q.text(line) end q.breakable_return if value.end_with?("\n") else q.format(part) end end end q.text(closing_quote) end |