Class: Ibex::Runtime::CST::GreenNode
- Inherits:
-
Object
- Object
- Ibex::Runtime::CST::GreenNode
- Defined in:
- lib/json5/generated_parser.rb
Overview
Immutable position-independent syntax node.
Instance Attribute Summary collapse
- #annotations ⇒ Object readonly
- #children ⇒ Object readonly
- #descendant_count ⇒ Object readonly
- #flags ⇒ Object readonly
- #full_width ⇒ Object readonly
- #intrinsic_flags ⇒ Object readonly
- #kind ⇒ Object readonly
- #leading_width ⇒ Object readonly
- #trailing_width ⇒ Object readonly
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(kind:, children:, flags: 0, annotations: []) ⇒ GreenNode
constructor
A new instance of GreenNode.
- #to_source ⇒ Object
Constructor Details
#initialize(kind:, children:, flags: 0, annotations: []) ⇒ GreenNode
Returns a new instance of GreenNode.
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
#annotations ⇒ Object (readonly)
420 421 422 |
# File 'lib/json5/generated_parser.rb', line 420 def annotations @annotations end |
#children ⇒ Object (readonly)
417 418 419 |
# File 'lib/json5/generated_parser.rb', line 417 def children @children end |
#descendant_count ⇒ Object (readonly)
424 425 426 |
# File 'lib/json5/generated_parser.rb', line 424 def descendant_count @descendant_count end |
#flags ⇒ Object (readonly)
418 419 420 |
# File 'lib/json5/generated_parser.rb', line 418 def flags @flags end |
#full_width ⇒ Object (readonly)
421 422 423 |
# File 'lib/json5/generated_parser.rb', line 421 def full_width @full_width end |
#intrinsic_flags ⇒ Object (readonly)
419 420 421 |
# File 'lib/json5/generated_parser.rb', line 419 def intrinsic_flags @intrinsic_flags end |
#kind ⇒ Object (readonly)
416 417 418 |
# File 'lib/json5/generated_parser.rb', line 416 def kind @kind end |
#leading_width ⇒ Object (readonly)
422 423 424 |
# File 'lib/json5/generated_parser.rb', line 422 def leading_width @leading_width end |
#trailing_width ⇒ Object (readonly)
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?
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 |
#hash ⇒ Object
467 |
# File 'lib/json5/generated_parser.rb', line 467 def hash = [@kind, @children, @intrinsic_flags, @annotations].hash |
#to_source ⇒ Object
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 |