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
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(parts:, quote:, location:, comments: []) ⇒ StringLiteral
constructor
A new instance of StringLiteral.
Methods inherited from Node
Constructor Details
#initialize(parts:, quote:, location:, comments: []) ⇒ StringLiteral
Returns a new instance of StringLiteral.
8163 8164 8165 8166 8167 8168 |
# File 'lib/syntax_tree/node.rb', line 8163 def initialize(parts:, quote:, location:, comments: []) @parts = parts @quote = quote @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
8161 8162 8163 |
# File 'lib/syntax_tree/node.rb', line 8161 def comments @comments end |
#parts ⇒ Object (readonly)
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the
string literal
8155 8156 8157 |
# File 'lib/syntax_tree/node.rb', line 8155 def parts @parts end |
#quote ⇒ Object (readonly)
- String
-
which quote was used by the string literal
8158 8159 8160 |
# File 'lib/syntax_tree/node.rb', line 8158 def quote @quote end |
Instance Method Details
#accept(visitor) ⇒ Object
8170 8171 8172 |
# File 'lib/syntax_tree/node.rb', line 8170 def accept(visitor) visitor.visit_string_literal(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
8174 8175 8176 |
# File 'lib/syntax_tree/node.rb', line 8174 def child_nodes parts end |
#deconstruct_keys(keys) ⇒ Object
8180 8181 8182 |
# File 'lib/syntax_tree/node.rb', line 8180 def deconstruct_keys(keys) { parts: parts, quote: quote, location: location, comments: comments } end |
#format(q) ⇒ Object
8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 |
# File 'lib/syntax_tree/node.rb', line 8184 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] elsif quote.start_with?("%") [quote, Quotes.matching(quote[/%[qQ]?(.)/, 1])] else [quote, quote] end q.group(0, opening_quote, closing_quote) do parts.each do |part| if part.is_a?(TStringContent) value = Quotes.normalize(part.value, closing_quote) separator = -> { q.breakable(force: true, indent: false) } q.seplist(value.split(/\r?\n/, -1), separator) do |text| q.text(text) end else q.format(part) end end end end |