Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Reformat main entry
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jan 3, 2024
1 parent 2c18703 commit 6c9b6b3
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions lib/parser/prism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def parse(source_buffer)
@source_buffer = source_buffer
source = source_buffer.source

offset_cache = build_offset_cache(source)
result = ::Prism.parse(source, filepath: source_buffer.name).value

build_ast(result, offset_cache)
build_ast(
::Prism.parse(source, filepath: source_buffer.name).value,
build_offset_cache(source)
)
ensure
@source_buffer = nil
end
Expand All @@ -47,10 +47,12 @@ def parse_with_comments(source_buffer)
@source_buffer = source_buffer
source = source_buffer.source

offset_cache = build_offset_cache(source)
result = ::Prism.parse(source, filepath: source_buffer.name)

[build_ast(result.value, offset_cache), build_comments(result.comments)]
[
build_ast(result.value, build_offset_cache(source)),
build_comments(result.comments)
]
ensure
@source_buffer = nil
end
Expand All @@ -64,13 +66,17 @@ def parse_with_comments(source_buffer)
#
def tokenize(source_buffer, _recover = false)
@source_buffer = source_buffer
souce = source_buffer.source
source = source_buffer.source

offset_cache = build_offset_cache(source)
result = ::Prism.parse_lex(source, filepath: source_buffer.name)

program, tokens = result.value
[build_ast(program, offset_cache), build_comments(result.comments), build_tokens(tokens, offset_cache)]

[
build_ast(program, offset_cache),
build_comments(result.comments),
build_tokens(tokens, offset_cache)
]
ensure
@source_buffer = nil
end
Expand All @@ -83,6 +89,13 @@ def try_declare_numparam(node)

private

# Prism deals with offsets in bytes, while the parser gem deals with offsets
# in characters. We need to handle this conversion in order to build the
# parser gem AST.
#
# If the bytesize of the source is the same as the length, then we can just
# use the offset directly. Otherwise, we build a hash that functions as a
# cache for the conversion.
def build_offset_cache(source)
if source.bytesize == source.length
-> (offset) { offset }
Expand All @@ -91,17 +104,20 @@ def build_offset_cache(source)
end
end

# Build the parser gem AST from the prism AST.
def build_ast(program, offset_cache)
program.accept(Compiler.new(self, offset_cache))
end

# Build the parser gem comments from the prism comments.
def build_comments(comments)
comments.map do |comment|
location = comment.location
Source::Comment.new(Source::Range.new(source_buffer, location.start_offset, location.end_offset))
end
end

# Build the parser gem tokens from the prism tokens.
def build_tokens(tokens, offset_cache)
Lexer.new(source_buffer, tokens.map(&:first), offset_cache).to_a
end
Expand Down

0 comments on commit 6c9b6b3

Please sign in to comment.