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.
10281 10282 10283 10284 10285 10286 |
# File 'lib/syntax_tree/node.rb', line 10281 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
10279 10280 10281 |
# File 'lib/syntax_tree/node.rb', line 10279 def comments @comments end |
#parts ⇒ Object (readonly)
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the
string literal
10273 10274 10275 |
# File 'lib/syntax_tree/node.rb', line 10273 def parts @parts end |
#quote ⇒ Object (readonly)
- nil | String
-
which quote was used by the string literal
10276 10277 10278 |
# File 'lib/syntax_tree/node.rb', line 10276 def quote @quote end |
Instance Method Details
#===(other) ⇒ Object
10355 10356 10357 10358 |
# File 'lib/syntax_tree/node.rb', line 10355 def ===(other) other.is_a?(StringLiteral) && ArrayMatch.call(parts, other.parts) && quote === other.quote end |
#accept(visitor) ⇒ Object
10288 10289 10290 |
# File 'lib/syntax_tree/node.rb', line 10288 def accept(visitor) visitor.visit_string_literal(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
10292 10293 10294 |
# File 'lib/syntax_tree/node.rb', line 10292 def child_nodes parts end |
#copy(parts: nil, quote: nil, location: nil) ⇒ Object
10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 |
# File 'lib/syntax_tree/node.rb', line 10296 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
10310 10311 10312 |
# File 'lib/syntax_tree/node.rb', line 10310 def deconstruct_keys(_keys) { parts: parts, quote: quote, location: location, comments: comments } end |
#format(q) ⇒ Object
10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 |
# File 'lib/syntax_tree/node.rb', line 10314 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 |