Class: Ast::Merge::Text::WordNode

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/text/word_node.rb

Overview

Represents a word within a line of text. Words are the nested level of the text-based AST. They are identified by word boundaries (regex b).

Examples:

word = WordNode.new("hello", line_number: 1, word_index: 0, start_col: 0, end_col: 5)
word.content    # => "hello"
word.signature  # => [:word, "hello"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, line_number:, word_index:, start_col:, end_col:) ⇒ WordNode

Initialize a new WordNode

Parameters:

  • content (String)

    The word content

  • line_number (Integer)

    1-based line number

  • word_index (Integer)

    0-based word index within line

  • start_col (Integer)

    0-based start column

  • end_col (Integer)

    0-based end column (exclusive)



37
38
39
40
41
42
43
# File 'lib/ast/merge/text/word_node.rb', line 37

def initialize(content, line_number:, word_index:, start_col:, end_col:)
  @content = content
  @line_number = line_number
  @word_index = word_index
  @start_col = start_col
  @end_col = end_col
end

Instance Attribute Details

#contentString (readonly)

Returns The word content.

Returns:

  • (String)

    The word content



16
17
18
# File 'lib/ast/merge/text/word_node.rb', line 16

def content
  @content
end

#end_colInteger (readonly)

Returns 0-based ending column position (exclusive).

Returns:

  • (Integer)

    0-based ending column position (exclusive)



28
29
30
# File 'lib/ast/merge/text/word_node.rb', line 28

def end_col
  @end_col
end

#line_numberInteger (readonly)

Returns 1-based line number containing this word.

Returns:

  • (Integer)

    1-based line number containing this word



19
20
21
# File 'lib/ast/merge/text/word_node.rb', line 19

def line_number
  @line_number
end

#start_colInteger (readonly)

Returns 0-based starting column position.

Returns:

  • (Integer)

    0-based starting column position



25
26
27
# File 'lib/ast/merge/text/word_node.rb', line 25

def start_col
  @start_col
end

#word_indexInteger (readonly)

Returns 0-based index of this word within the line.

Returns:

  • (Integer)

    0-based index of this word within the line



22
23
24
# File 'lib/ast/merge/text/word_node.rb', line 22

def word_index
  @word_index
end

Instance Method Details

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

Check equality with another WordNode

Parameters:

  • other (WordNode)

    Other node to compare

Returns:

  • (Boolean)

    True if content matches



57
58
59
# File 'lib/ast/merge/text/word_node.rb', line 57

def ==(other)
  other.is_a?(WordNode) && @content == other.content
end

#hashInteger

Hash code for use in Hash keys

Returns:

  • (Integer)

    Hash code



66
67
68
# File 'lib/ast/merge/text/word_node.rb', line 66

def hash
  @content.hash
end

#inspectString

String representation for debugging

Returns:

  • (String)

    Debug representation



73
74
75
# File 'lib/ast/merge/text/word_node.rb', line 73

def inspect
  "#<WordNode #{@content.inspect} line=#{@line_number} col=#{@start_col}..#{@end_col}>"
end

#signatureArray

Generate a signature for this word node. The signature is used for matching words across template/destination.

Returns:

  • (Array)

    Signature array [:word, content]



49
50
51
# File 'lib/ast/merge/text/word_node.rb', line 49

def signature
  [:word, @content]
end

#to_sString

Convert to string (returns content)

Returns:

  • (String)

    Word content



80
81
82
# File 'lib/ast/merge/text/word_node.rb', line 80

def to_s
  @content
end