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:, end_line:, end_char:) ⇒ Location

Returns a new instance of Location.



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

def initialize(start_line:, start_char:, end_line:, end_char:)
  @start_line = start_line
  @start_char = start_char
  @end_line = end_line
  @end_char = end_char
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_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_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:) ⇒ Object



47
48
49
# File 'lib/syntax_tree/node.rb', line 47

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

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



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

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

Instance Method Details

#==(other) ⇒ Object



19
20
21
22
23
# File 'lib/syntax_tree/node.rb', line 19

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

#linesObject



15
16
17
# File 'lib/syntax_tree/node.rb', line 15

def lines
  start_line..end_line
end

#to(other) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/syntax_tree/node.rb', line 25

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

#to_json(*opts) ⇒ Object



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

def to_json(*opts)
  [start_line, start_char, end_line, end_char].to_json(*opts)
end