Skip to content

Add column position to Location #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
module SyntaxTree
# Represents the location of a node in the tree from the source code.
class Location
attr_reader :start_line, :start_char, :end_line, :end_char
attr_reader :start_line, :start_char, :start_column, :end_line, :end_char, :end_column

def initialize(start_line:, start_char:, end_line:, end_char:)
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

def lines
Expand All @@ -26,22 +28,26 @@ 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_char: other.end_char,
end_column: other.end_column
)
end

def self.token(line:, char:, size:)
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_char: char + size,
end_column: column + size
)
end

def self.fixed(line:, char:)
new(start_line: line, start_char: char, end_line: line, end_char: char)
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
end

Expand Down Expand Up @@ -2047,13 +2053,15 @@ def initialize(
@comments = comments
end

def bind(start_char, end_char)
def bind(start_char, start_column, end_char, end_column)
@location =
Location.new(
start_line: location.start_line,
start_char: start_char,
start_column: start_column,
end_line: location.end_line,
end_char: end_char
end_char: end_char,
end_column: end_column
)

parts = [rescue_clause, else_clause, ensure_clause]
Expand All @@ -2062,14 +2070,17 @@ def bind(start_char, end_char)
consequent = parts.compact.first
statements.bind(
start_char,
consequent ? consequent.location.start_char : end_char
start_column,
consequent ? consequent.location.start_char : end_char,
consequent ? consequent.location.start_column : end_column
)

# Next we're going to determine the rescue clause if there is one
if rescue_clause
consequent = parts.drop(1).compact.first
rescue_clause.bind_end(
consequent ? consequent.location.start_char : end_char
consequent ? consequent.location.start_char : end_char,
consequent ? consequent.location.start_column : end_column
)
end
end
Expand Down Expand Up @@ -8413,20 +8424,22 @@ def initialize(
@comments = comments
end

def bind_end(end_char)
def bind_end(end_char, end_column)
@location =
Location.new(
start_line: location.start_line,
start_char: location.start_char,
start_column: location.start_column,
end_line: location.end_line,
end_char: end_char
end_char: end_char,
end_column: end_column
)

if consequent
consequent.bind_end(end_char)
statements.bind_end(consequent.location.start_char)
consequent.bind_end(end_char, end_column)
statements.bind_end(consequent.location.start_char, consequent.location.start_column)
else
statements.bind_end(end_char)
statements.bind_end(end_char, end_column)
end
end

Expand Down Expand Up @@ -8885,13 +8898,15 @@ def initialize(parser, body:, location:, comments: [])
@comments = comments
end

def bind(start_char, end_char)
def bind(start_char, start_column, end_char, end_column)
@location =
Location.new(
start_line: location.start_line,
start_char: start_char,
start_column: start_column,
end_line: location.end_line,
end_char: end_char
end_char: end_char,
end_column: end_column
)

if body[0].is_a?(VoidStmt)
Expand All @@ -8900,8 +8915,10 @@ def bind(start_char, end_char)
Location.new(
start_line: location.start_line,
start_char: start_char,
start_column: start_column,
end_line: location.end_line,
end_char: start_char
end_char: start_char,
end_column: end_column
)

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

def bind_end(end_char)
def bind_end(end_char, end_column)
@location =
Location.new(
start_line: location.start_line,
start_char: location.start_char,
start_column: location.start_column,
end_line: location.end_line,
end_char: end_char
end_char: end_char,
end_column: end_column
)
end

Expand Down
Loading