Skip to content

Commit 365a10f

Browse files
authored
Merge pull request #44 from vinistock/add_absolute_columns_to_locations
Add column position to Location
2 parents 6e0e53e + 48ce375 commit 365a10f

File tree

3 files changed

+292
-130
lines changed

3 files changed

+292
-130
lines changed

lib/syntax_tree/node.rb

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
module SyntaxTree
44
# Represents the location of a node in the tree from the source code.
55
class Location
6-
attr_reader :start_line, :start_char, :end_line, :end_char
6+
attr_reader :start_line, :start_char, :start_column, :end_line, :end_char, :end_column
77

8-
def initialize(start_line:, start_char:, end_line:, end_char:)
8+
def initialize(start_line:, start_char:, start_column:, end_line:, end_char:, end_column:)
99
@start_line = start_line
1010
@start_char = start_char
11+
@start_column = start_column
1112
@end_line = end_line
1213
@end_char = end_char
14+
@end_column = end_column
1315
end
1416

1517
def lines
@@ -26,22 +28,26 @@ def to(other)
2628
Location.new(
2729
start_line: start_line,
2830
start_char: start_char,
31+
start_column: start_column,
2932
end_line: [end_line, other.end_line].max,
30-
end_char: other.end_char
33+
end_char: other.end_char,
34+
end_column: other.end_column
3135
)
3236
end
3337

34-
def self.token(line:, char:, size:)
38+
def self.token(line:, char:, column:, size:)
3539
new(
3640
start_line: line,
3741
start_char: char,
42+
start_column: column,
3843
end_line: line,
39-
end_char: char + size
44+
end_char: char + size,
45+
end_column: column + size
4046
)
4147
end
4248

43-
def self.fixed(line:, char:)
44-
new(start_line: line, start_char: char, end_line: line, end_char: char)
49+
def self.fixed(line:, char:, column:)
50+
new(start_line: line, start_char: char, start_column: column, end_line: line, end_char: char, end_column: column)
4551
end
4652
end
4753

@@ -1722,13 +1728,15 @@ def initialize(
17221728
@comments = comments
17231729
end
17241730

1725-
def bind(start_char, end_char)
1731+
def bind(start_char, start_column, end_char, end_column)
17261732
@location =
17271733
Location.new(
17281734
start_line: location.start_line,
17291735
start_char: start_char,
1736+
start_column: start_column,
17301737
end_line: location.end_line,
1731-
end_char: end_char
1738+
end_char: end_char,
1739+
end_column: end_column
17321740
)
17331741

17341742
parts = [rescue_clause, else_clause, ensure_clause]
@@ -1737,14 +1745,17 @@ def bind(start_char, end_char)
17371745
consequent = parts.compact.first
17381746
statements.bind(
17391747
start_char,
1740-
consequent ? consequent.location.start_char : end_char
1748+
start_column,
1749+
consequent ? consequent.location.start_char : end_char,
1750+
consequent ? consequent.location.start_column : end_column
17411751
)
17421752

17431753
# Next we're going to determine the rescue clause if there is one
17441754
if rescue_clause
17451755
consequent = parts.drop(1).compact.first
17461756
rescue_clause.bind_end(
1747-
consequent ? consequent.location.start_char : end_char
1757+
consequent ? consequent.location.start_char : end_char,
1758+
consequent ? consequent.location.start_column : end_column
17481759
)
17491760
end
17501761
end
@@ -6898,20 +6909,22 @@ def initialize(
68986909
@comments = comments
68996910
end
69006911

6901-
def bind_end(end_char)
6912+
def bind_end(end_char, end_column)
69026913
@location =
69036914
Location.new(
69046915
start_line: location.start_line,
69056916
start_char: location.start_char,
6917+
start_column: location.start_column,
69066918
end_line: location.end_line,
6907-
end_char: end_char
6919+
end_char: end_char,
6920+
end_column: end_column
69086921
)
69096922

69106923
if consequent
6911-
consequent.bind_end(end_char)
6912-
statements.bind_end(consequent.location.start_char)
6924+
consequent.bind_end(end_char, end_column)
6925+
statements.bind_end(consequent.location.start_char, consequent.location.start_column)
69136926
else
6914-
statements.bind_end(end_char)
6927+
statements.bind_end(end_char, end_column)
69156928
end
69166929
end
69176930

@@ -7268,13 +7281,15 @@ def initialize(parser, body:, location:, comments: [])
72687281
@comments = comments
72697282
end
72707283

7271-
def bind(start_char, end_char)
7284+
def bind(start_char, start_column, end_char, end_column)
72727285
@location =
72737286
Location.new(
72747287
start_line: location.start_line,
72757288
start_char: start_char,
7289+
start_column: start_column,
72767290
end_line: location.end_line,
7277-
end_char: end_char
7291+
end_char: end_char,
7292+
end_column: end_column
72787293
)
72797294

72807295
if body[0].is_a?(VoidStmt)
@@ -7283,8 +7298,10 @@ def bind(start_char, end_char)
72837298
Location.new(
72847299
start_line: location.start_line,
72857300
start_char: start_char,
7301+
start_column: start_column,
72867302
end_line: location.end_line,
7287-
end_char: start_char
7303+
end_char: start_char,
7304+
end_column: end_column
72887305
)
72897306

72907307
body[0] = VoidStmt.new(location: location)
@@ -7293,13 +7310,15 @@ def bind(start_char, end_char)
72937310
attach_comments(start_char, end_char)
72947311
end
72957312

7296-
def bind_end(end_char)
7313+
def bind_end(end_char, end_column)
72977314
@location =
72987315
Location.new(
72997316
start_line: location.start_line,
73007317
start_char: location.start_char,
7318+
start_column: location.start_column,
73017319
end_line: location.end_line,
7302-
end_char: end_char
7320+
end_char: end_char,
7321+
end_column: end_column
73037322
)
73047323
end
73057324

0 commit comments

Comments
 (0)