Class: SyntaxTree::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Represents the location of a node in the tree from the source code.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_line:, start_char:, start_column:, end_line:, end_char:, end_column:) ⇒ Location

Returns a new instance of Location.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/syntax_tree/node.rb', line 13

def initialize(
  start_line:,
  start_char:,
  start_column:,
  end_line:,
  end_char:,
  end_column:
)
  @start_line = start_line
  @start_char = start_char
  @start_column = start_column
  @end_line = end_line
  @end_char = end_char
  @end_column = end_column
end

Instance Attribute Details

#end_charObject (readonly)

Returns the value of attribute end_char.



6
7
8
# File 'lib/syntax_tree/node.rb', line 6

def end_char
  @end_char
end

#end_columnObject (readonly)

Returns the value of attribute end_column.



6
7
8
# File 'lib/syntax_tree/node.rb', line 6

def end_column
  @end_column
end

#end_lineObject (readonly)

Returns the value of attribute end_line.



6
7
8
# File 'lib/syntax_tree/node.rb', line 6

def end_line
  @end_line
end

#start_charObject (readonly)

Returns the value of attribute start_char.



6
7
8
# File 'lib/syntax_tree/node.rb', line 6

def start_char
  @start_char
end

#start_columnObject (readonly)

Returns the value of attribute start_column.



6
7
8
# File 'lib/syntax_tree/node.rb', line 6

def start_column
  @start_column
end

#start_lineObject (readonly)

Returns the value of attribute start_line.



6
7
8
# File 'lib/syntax_tree/node.rb', line 6

def start_line
  @start_line
end

Class Method Details

.defaultObject

A convenience method that is typically used when you don’t care about the location of a node, but need to create a Location instance to pass to a constructor.



90
91
92
93
94
95
96
97
98
99
# File 'lib/syntax_tree/node.rb', line 90

def self.default
  new(
    start_line: 1,
    start_char: 0,
    start_column: 0,
    end_line: 1,
    end_char: 0,
    end_column: 0
  )
end

.fixed(line:, char:, column:) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/syntax_tree/node.rb', line 76

def self.fixed(line:, char:, column:)
  new(
    start_line: line,
    start_char: char,
    start_column: column,
    end_line: line,
    end_char: char,
    end_column: column
  )
end

.token(line:, char:, column:, size:) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/syntax_tree/node.rb', line 65

def self.token(line:, char:, column:, size:)
  new(
    start_line: line,
    start_char: char,
    start_column: column,
    end_line: line,
    end_char: char + size,
    end_column: column + size
  )
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
37
# File 'lib/syntax_tree/node.rb', line 33

def ==(other)
  other.is_a?(Location) && start_line == other.start_line &&
    start_char == other.start_char && end_line == other.end_line &&
    end_char == other.end_char
end

#deconstructObject



50
51
52
# File 'lib/syntax_tree/node.rb', line 50

def deconstruct
  [start_line, start_char, start_column, end_line, end_char, end_column]
end

#deconstruct_keys(_keys) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/syntax_tree/node.rb', line 54

def deconstruct_keys(_keys)
  {
    start_line: start_line,
    start_char: start_char,
    start_column: start_column,
    end_line: end_line,
    end_char: end_char,
    end_column: end_column
  }
end

#linesObject



29
30
31
# File 'lib/syntax_tree/node.rb', line 29

def lines
  start_line..end_line
end

#to(other) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/syntax_tree/node.rb', line 39

def to(other)
  Location.new(
    start_line: start_line,
    start_char: start_char,
    start_column: start_column,
    end_line: [end_line, other.end_line].max,
    end_char: other.end_char,
    end_column: other.end_column
  )
end