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, #pretty_print, #to_json

Constructor Details

#initialize(value:, location:) ⇒ CHAR

Returns a new instance of CHAR.



245
246
247
248
249
# File 'lib/syntax_tree/node.rb', line 245

def initialize(value:, location:)
  @value = value
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



243
244
245
# File 'lib/syntax_tree/node.rb', line 243

def comments
  @comments
end

#valueObject (readonly)

String

the value of the character literal



240
241
242
# File 'lib/syntax_tree/node.rb', line 240

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



286
287
288
# File 'lib/syntax_tree/node.rb', line 286

def ===(other)
  other.is_a?(CHAR) && value === other.value
end

#accept(visitor) ⇒ Object



251
252
253
# File 'lib/syntax_tree/node.rb', line 251

def accept(visitor)
  visitor.visit_CHAR(self)
end

#child_nodesObject Also known as: deconstruct



255
256
257
# File 'lib/syntax_tree/node.rb', line 255

def child_nodes
  []
end

#copy(value: nil, location: nil) ⇒ Object



259
260
261
262
263
264
265
266
267
268
# File 'lib/syntax_tree/node.rb', line 259

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



272
273
274
# File 'lib/syntax_tree/node.rb', line 272

def deconstruct_keys(_keys)
  { value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/syntax_tree/node.rb', line 276

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