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.
10312 10313 10314 10315 10316 10317 |
# File 'lib/syntax_tree/node.rb', line 10312 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
10310 10311 10312 |
# File 'lib/syntax_tree/node.rb', line 10310 def comments @comments end |
#parts ⇒ Object (readonly)
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the
string literal
10304 10305 10306 |
# File 'lib/syntax_tree/node.rb', line 10304 def parts @parts end |
#quote ⇒ Object (readonly)
- nil | String
-
which quote was used by the string literal
10307 10308 10309 |
# File 'lib/syntax_tree/node.rb', line 10307 def quote @quote end |
Instance Method Details
#===(other) ⇒ Object
10386 10387 10388 10389 |
# File 'lib/syntax_tree/node.rb', line 10386 def ===(other) other.is_a?(StringLiteral) && ArrayMatch.call(parts, other.parts) && quote === other.quote end |
#accept(visitor) ⇒ Object
10319 10320 10321 |
# File 'lib/syntax_tree/node.rb', line 10319 def accept(visitor) visitor.visit_string_literal(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
10323 10324 10325 |
# File 'lib/syntax_tree/node.rb', line 10323 def child_nodes parts end |
#copy(parts: nil, quote: nil, location: nil) ⇒ Object
10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 |
# File 'lib/syntax_tree/node.rb', line 10327 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
10341 10342 10343 |
# File 'lib/syntax_tree/node.rb', line 10341 def deconstruct_keys(_keys) { parts: parts, quote: quote, location: location, comments: comments } end |
#format(q) ⇒ Object
10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 |
# File 'lib/syntax_tree/node.rb', line 10345 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 |