Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c212530

Browse files
committed
Add column position to Location
1 parent 3baf60c commit c212530

File tree

3 files changed

+281
-130
lines changed

3 files changed

+281
-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

@@ -2047,13 +2053,15 @@ def initialize(
20472053
@comments = comments
20482054
end
20492055

2050-
def bind(start_char, end_char)
2056+
def bind(start_char, start_column, end_char, end_column)
20512057
@location =
20522058
Location.new(
20532059
start_line: location.start_line,
20542060
start_char: start_char,
2061+
start_column: start_column,
20552062
end_line: location.end_line,
2056-
end_char: end_char
2063+
end_char: end_char,
2064+
end_column: end_column
20572065
)
20582066

20592067
parts = [rescue_clause, else_clause, ensure_clause]
@@ -2062,14 +2070,17 @@ def bind(start_char, end_char)
20622070
consequent = parts.compact.first
20632071
statements.bind(
20642072
start_char,
2065-
consequent ? consequent.location.start_char : end_char
2073+
start_column,
2074+
consequent ? consequent.location.start_char : end_char,
2075+
consequent ? consequent.location.start_column : end_column
20662076
)
20672077

20682078
# Next we're going to determine the rescue clause if there is one
20692079
if rescue_clause
20702080
consequent = parts.drop(1).compact.first
20712081
rescue_clause.bind_end(
2072-
consequent ? consequent.location.start_char : end_char
2082+
consequent ? consequent.location.start_char : end_char,
2083+
consequent ? consequent.location.start_column : end_column
20732084
)
20742085
end
20752086
end
@@ -8413,20 +8424,22 @@ def initialize(
84138424
@comments = comments
84148425
end
84158426

8416-
def bind_end(end_char)
8427+
def bind_end(end_char, end_column)
84178428
@location =
84188429
Location.new(
84198430
start_line: location.start_line,
84208431
start_char: location.start_char,
8432+
start_column: location.start_column,
84218433
end_line: location.end_line,
8422-
end_char: end_char
8434+
end_char: end_char,
8435+
end_column: end_column
84238436
)
84248437

84258438
if consequent
8426-
consequent.bind_end(end_char)
8427-
statements.bind_end(consequent.location.start_char)
8439+
consequent.bind_end(end_char, end_column)
8440+
statements.bind_end(consequent.location.start_char, consequent.location.start_column)
84288441
else
8429-
statements.bind_end(end_char)
8442+
statements.bind_end(end_char, end_column)
84308443
end
84318444
end
84328445

@@ -8885,13 +8898,15 @@ def initialize(parser, body:, location:, comments: [])
88858898
@comments = comments
88868899
end
88878900

8888-
def bind(start_char, end_char)
8901+
def bind(start_char, start_column, end_char, end_column)
88898902
@location =
88908903
Location.new(
88918904
start_line: location.start_line,
88928905
start_char: start_char,
8906+
start_column: start_column,
88938907
end_line: location.end_line,
8894-
end_char: end_char
8908+
end_char: end_char,
8909+
end_column: end_column
88958910
)
88968911

88978912
if body[0].is_a?(VoidStmt)
@@ -8900,8 +8915,10 @@ def bind(start_char, end_char)
89008915
Location.new(
89018916
start_line: location.start_line,
89028917
start_char: start_char,
8918+
start_column: start_column,
89038919
end_line: location.end_line,
8904-
end_char: start_char
8920+
end_char: start_char,
8921+
end_column: end_column
89058922
)
89068923

89078924
body[0] = VoidStmt.new(location: location)
@@ -8910,13 +8927,15 @@ def bind(start_char, end_char)
89108927
attach_comments(start_char, end_char)
89118928
end
89128929

8913-
def bind_end(end_char)
8930+
def bind_end(end_char, end_column)
89148931
@location =
89158932
Location.new(
89168933
start_line: location.start_line,
89178934
start_char: location.start_char,
8935+
start_column: location.start_column,
89188936
end_line: location.end_line,
8919-
end_char: end_char
8937+
end_char: end_char,
8938+
end_column: end_column
89208939
)
89218940
end
89228941

0 commit comments

Comments
 (0)