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.



8
9
10
11
12
13
14
15
# File 'lib/syntax_tree/node.rb', line 8

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

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



64
65
66
# File 'lib/syntax_tree/node.rb', line 64

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



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

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



21
22
23
24
25
# File 'lib/syntax_tree/node.rb', line 21

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



38
39
40
# File 'lib/syntax_tree/node.rb', line 38

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

#deconstruct_keys(keys) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/syntax_tree/node.rb', line 42

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



17
18
19
# File 'lib/syntax_tree/node.rb', line 17

def lines
  start_line..end_line
end

#to(other) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/syntax_tree/node.rb', line 27

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