Skip to content

Commit

Permalink
Relax Location#source to be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nixme authored and kddnewton committed Feb 24, 2024
1 parent db78eef commit 9f00fe7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
31 changes: 18 additions & 13 deletions lib/prism/parse_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ def inspect

# The source code that this location represents.
def slice
source.slice(start_offset, length)
source!.slice(start_offset, length)
end

# The character offset from the beginning of the source where this location
# starts.
def start_character_offset
source.character_offset(start_offset)
source!.character_offset(start_offset)
end

# The offset from the start of the file in code units of the given encoding.
Expand All @@ -190,7 +190,7 @@ def end_offset
# The character offset from the beginning of the source where this location
# ends.
def end_character_offset
source.character_offset(end_offset)
source!.character_offset(end_offset)
end

# The offset from the start of the file in code units of the given encoding.
Expand All @@ -200,30 +200,30 @@ def end_code_units_offset(encoding = Encoding::UTF_16LE)

# The line number where this location starts.
def start_line
source.line(start_offset)
source!.line(start_offset)
end

# The content of the line where this location starts before this location.
def start_line_slice
offset = source.line_start(start_offset)
source.slice(offset, start_offset - offset)
offset = source!.line_start(start_offset)
source!.slice(offset, start_offset - offset)
end

# The line number where this location ends.
def end_line
source.line(end_offset)
source!.line(end_offset)
end

# The column number in bytes where this location starts from the start of
# the line.
def start_column
source.column(start_offset)
source!.column(start_offset)
end

# The column number in characters where this location ends from the start of
# the line.
def start_character_column
source.character_column(start_offset)
source!.character_column(start_offset)
end

# The column number in code units of the given encoding where this location
Expand All @@ -235,13 +235,13 @@ def start_code_units_column(encoding = Encoding::UTF_16LE)
# The column number in bytes where this location ends from the start of the
# line.
def end_column
source.column(end_offset)
source!.column(end_offset)
end

# The column number in characters where this location ends from the start of
# the line.
def end_character_column
source.character_column(end_offset)
source!.character_column(end_offset)
end

# The column number in code units of the given encoding where this location
Expand Down Expand Up @@ -281,8 +281,13 @@ def join(other)
# the beginning of the file. Useful for when you want a location object but
# do not care where it points.
def self.null
source = nil #: Source
new(source, 0, 0)
new(nil, 0, 0)
end

private

def source!
source or raise "Missing source"
end
end

Expand Down
6 changes: 6 additions & 0 deletions sig/prism/_private/parse_result.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ module Prism
def compute_offsets: (String) -> Array[Integer]
end

class Location
private

def source!: () -> Source
end

class ParseResult[out T]
def attach_comments!: () -> void
def mark_newlines!: () -> untyped
Expand Down
6 changes: 3 additions & 3 deletions sig/prism/parse_result.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ module Prism
end

class Location
attr_reader source: Source
attr_reader source: Source?
attr_reader start_offset: Integer
attr_reader length: Integer
attr_reader comments: Array[comment]

def initialize: (Source source, Integer start_offset, Integer length) -> void
def copy: (?source: Source, ?start_offset: Integer, ?length: Integer) -> Location
def initialize: (Source? source, Integer start_offset, Integer length) -> void
def copy: (?source: Source?, ?start_offset: Integer, ?length: Integer) -> Location
def slice: () -> String
def start_character_offset: () -> Integer
def end_offset: () -> Integer
Expand Down
1 change: 0 additions & 1 deletion templates/lib/prism/dsl.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ module Prism

# Create a new Location object
def Location(source = nil, start_offset = 0, length = 0)
# @type var source: Source
Location.new(source, start_offset, length)
end
<%- nodes.each do |node| -%>
Expand Down

0 comments on commit 9f00fe7

Please sign in to comment.