Class: Markbridge::AST::Text

Inherits:
Node
  • Object
show all
Defined in:
lib/markbridge/ast/text.rb

Overview

Represents a text node (leaf node) in the AST. Text nodes contain the actual text content and cannot have children.

Examples:

Creating a text node

text = AST::Text.new("Hello, world!")

Merging text nodes

text1 = AST::Text.new("Hello")
text2 = AST::Text.new(" world")
text1.merge(text2)
text1.text # => "Hello world"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Text

Create a new text node with the given content.

Parameters:

  • text (String)

    the text content



23
24
25
# File 'lib/markbridge/ast/text.rb', line 23

def initialize(text)
  @text = +text
end

Instance Attribute Details

#textString (readonly)

Returns the text content of this node.

Returns:

  • (String)

    the text content of this node



18
19
20
# File 'lib/markbridge/ast/text.rb', line 18

def text
  @text
end

Instance Method Details

#merge(other) ⇒ Text

Merge another text node’s content into this one. This mutates the current text node by appending the other’s text.

Parameters:

  • other (Text)

    the text node to merge from

Returns:

  • (Text)

    self for method chaining



32
33
34
35
# File 'lib/markbridge/ast/text.rb', line 32

def merge(other)
  @text << other.text
  self
end