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
- 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, #pretty_print, #to_json
Constructor Details
#initialize(parts:, quote:, location:) ⇒ StringLiteral
10163 10164 10165 10166 10167 10168 |
# File 'lib/syntax_tree/node.rb', line 10163 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
10161 10162 10163 |
# File 'lib/syntax_tree/node.rb', line 10161 def comments @comments end |
#parts ⇒ Object (readonly)
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the
string literal
10155 10156 10157 |
# File 'lib/syntax_tree/node.rb', line 10155 def parts @parts end |
#quote ⇒ Object (readonly)
- String
-
which quote was used by the string literal
10158 10159 10160 |
# File 'lib/syntax_tree/node.rb', line 10158 def quote @quote end |
Instance Method Details
#===(other) ⇒ Object
10237 10238 10239 10240 |
# File 'lib/syntax_tree/node.rb', line 10237 def ===(other) other.is_a?(StringLiteral) && ArrayMatch.call(parts, other.parts) && quote === other.quote end |
#accept(visitor) ⇒ Object
10170 10171 10172 |
# File 'lib/syntax_tree/node.rb', line 10170 def accept(visitor) visitor.visit_string_literal(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
10174 10175 10176 |
# File 'lib/syntax_tree/node.rb', line 10174 def child_nodes parts end |
#copy(parts: nil, quote: nil, location: nil) ⇒ Object
10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 |
# File 'lib/syntax_tree/node.rb', line 10178 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
10192 10193 10194 |
# File 'lib/syntax_tree/node.rb', line 10192 def deconstruct_keys(_keys) { parts: parts, quote: quote, location: location, comments: comments } end |
#format(q) ⇒ Object
10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 |
# File 'lib/syntax_tree/node.rb', line 10196 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 |