Class: Ibex::Runtime::CST::GreenNode

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/generated_parser.rb

Overview

Immutable position-independent syntax node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, children:, flags: 0, annotations: []) ⇒ GreenNode

Returns a new instance of GreenNode.

RBS:

  • (kind: Integer, children: Array[child], ?flags: Integer, ?annotations: Array[SyntaxAnnotation]) -> void



427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/json5/generated_parser.rb', line 427

def initialize(kind:, children:, flags: 0, annotations: [])
  @kind = kind
  @children = children.dup.freeze
  @annotations = annotations.dup.freeze
  @intrinsic_flags = flags
  @intrinsic_flags |= Flags::HAS_ANNOTATION unless @annotations.empty?
  @flags = @children.reduce(@intrinsic_flags) { |value, child| value | child.flags }
  @full_width = @children.sum(&:full_width)
  @leading_width = edge_width(@children, :leading_width)
  @trailing_width = edge_width(@children.reverse_each, :trailing_width)
  @descendant_count = 1 + @children.sum(&:descendant_count)
  freeze
end

Instance Attribute Details

#annotationsObject (readonly)

Signature:

  • Array[SyntaxAnnotation]



420
421
422
# File 'lib/json5/generated_parser.rb', line 420

def annotations
  @annotations
end

#childrenObject (readonly)

Signature:

  • Array[child]



417
418
419
# File 'lib/json5/generated_parser.rb', line 417

def children
  @children
end

#descendant_countObject (readonly)

Signature:

  • Integer



424
425
426
# File 'lib/json5/generated_parser.rb', line 424

def descendant_count
  @descendant_count
end

#flagsObject (readonly)

Signature:

  • Integer



418
419
420
# File 'lib/json5/generated_parser.rb', line 418

def flags
  @flags
end

#full_widthObject (readonly)

Signature:

  • Integer



421
422
423
# File 'lib/json5/generated_parser.rb', line 421

def full_width
  @full_width
end

#intrinsic_flagsObject (readonly)

Signature:

  • Integer



419
420
421
# File 'lib/json5/generated_parser.rb', line 419

def intrinsic_flags
  @intrinsic_flags
end

#kindObject (readonly)

RBS:



416
417
418
# File 'lib/json5/generated_parser.rb', line 416

def kind
  @kind
end

#leading_widthObject (readonly)

Signature:

  • Integer



422
423
424
# File 'lib/json5/generated_parser.rb', line 422

def leading_width
  @leading_width
end

#trailing_widthObject (readonly)

Signature:

  • Integer



423
424
425
# File 'lib/json5/generated_parser.rb', line 423

def trailing_width
  @trailing_width
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

RBS:

  • (Object? other) -> bool



460
461
462
463
# File 'lib/json5/generated_parser.rb', line 460

def ==(other)
  other.is_a?(GreenNode) && @kind == other.kind && @intrinsic_flags == other.intrinsic_flags &&
    @annotations == other.annotations && @children == other.children
end

#hashObject

RBS:

  • () -> Integer



467
# File 'lib/json5/generated_parser.rb', line 467

def hash = [@kind, @children, @intrinsic_flags, @annotations].hash

#to_sourceObject

RBS:

  • () -> String



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/json5/generated_parser.rb', line 442

def to_source
  source = String.new(encoding: Encoding::BINARY)
  pending = @children.reverse
  until pending.empty?
    child = pending.pop || raise("green source traversal underflow")
    if child.is_a?(GreenNode)
      child.children.reverse_each { |nested| pending << nested }
      next
    end

    child.leading.each { |trivia| source << trivia.text }
    source << child.text
    child.trailing.each { |trivia| source << trivia.text }
  end
  source
end