Class: SyntaxTree::CHAR

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

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

Attributes inherited from Node

#location

Instance Method Summary collapse

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

#commentsObject (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

#valueObject (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_nodesObject 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