Class: SyntaxTree::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



68
69
70
71
72
73
# File 'lib/syntax_tree.rb', line 68

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.



66
67
68
# File 'lib/syntax_tree.rb', line 66

def end_char
  @end_char
end

#end_lineObject (readonly)

Returns the value of attribute end_line.



66
67
68
# File 'lib/syntax_tree.rb', line 66

def end_line
  @end_line
end

#start_charObject (readonly)

Returns the value of attribute start_char.



66
67
68
# File 'lib/syntax_tree.rb', line 66

def start_char
  @start_char
end

#start_lineObject (readonly)

Returns the value of attribute start_line.



66
67
68
# File 'lib/syntax_tree.rb', line 66

def start_line
  @start_line
end

Class Method Details

.fixed(line:, char:) ⇒ Object



103
104
105
# File 'lib/syntax_tree.rb', line 103

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

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



94
95
96
97
98
99
100
101
# File 'lib/syntax_tree.rb', line 94

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



75
76
77
78
79
# File 'lib/syntax_tree.rb', line 75

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

#to(other) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/syntax_tree.rb', line 81

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



90
91
92
# File 'lib/syntax_tree.rb', line 90

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