Class: SyntaxTree::CHAR
Overview
CHAR irepresents a single codepoint in the script encoding.
?a
In the example above, the CHAR node represents the string literal “a”. You can use control characters with this as well, as in ?C-a.
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#value ⇒ Object
readonly
- String
-
the value of the character literal.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(value: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(value:, location:) ⇒ CHAR
constructor
A new instance of CHAR.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(value:, location:) ⇒ CHAR
Returns a new instance of CHAR.
255 256 257 258 259 |
# File 'lib/syntax_tree/node.rb', line 255 def initialize(value:, location:) @value = value @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
253 254 255 |
# File 'lib/syntax_tree/node.rb', line 253 def comments @comments end |
#value ⇒ Object (readonly)
- String
-
the value of the character literal
250 251 252 |
# File 'lib/syntax_tree/node.rb', line 250 def value @value end |
Instance Method Details
#===(other) ⇒ Object
296 297 298 |
# File 'lib/syntax_tree/node.rb', line 296 def ===(other) other.is_a?(CHAR) && value === other.value end |
#accept(visitor) ⇒ Object
261 262 263 |
# File 'lib/syntax_tree/node.rb', line 261 def accept(visitor) visitor.visit_CHAR(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
265 266 267 |
# File 'lib/syntax_tree/node.rb', line 265 def child_nodes [] end |
#copy(value: nil, location: nil) ⇒ Object
269 270 271 272 273 274 275 276 277 278 |
# File 'lib/syntax_tree/node.rb', line 269 def copy(value: nil, location: nil) node = CHAR.new( value: value || self.value, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
282 283 284 |
# File 'lib/syntax_tree/node.rb', line 282 def deconstruct_keys(_keys) { value: value, location: location, comments: comments } end |
#format(q) ⇒ Object
286 287 288 289 290 291 292 293 294 |
# File 'lib/syntax_tree/node.rb', line 286 def format(q) if value.length != 2 q.text(value) else q.text(q.quote) q.text(value[1] == "\"" ? "\\\"" : value[1]) q.text(q.quote) end end |