Class: SyntaxTree::CHAR

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

#initialize(value:, location:, comments: []) ⇒ CHAR



464
465
466
467
468
# File 'lib/syntax_tree.rb', line 464

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



462
463
464
# File 'lib/syntax_tree.rb', line 462

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



459
460
461
# File 'lib/syntax_tree.rb', line 459

def location
  @location
end

#valueObject (readonly)

String

the value of the character literal



456
457
458
# File 'lib/syntax_tree.rb', line 456

def value
  @value
end

Instance Method Details

#child_nodesObject



470
471
472
# File 'lib/syntax_tree.rb', line 470

def child_nodes
  []
end

#format(q) ⇒ Object



474
475
476
477
478
479
480
481
482
# File 'lib/syntax_tree.rb', line 474

def format(q)
  if value.length != 2
    q.text(value)
  else
    q.text(q.quote)
    q.text(value[1])
    q.text(q.quote)
  end
end

#pretty_print(q) ⇒ Object



484
485
486
487
488
489
490
491
492
493
# File 'lib/syntax_tree.rb', line 484

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("CHAR")

    q.breakable
    q.pp(value)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



495
496
497
498
499
# File 'lib/syntax_tree.rb', line 495

def to_json(*opts)
  { type: :CHAR, value: value, loc: location, cmts: comments }.to_json(
    *opts
  )
end