From 6c9b6b32e9cae67df7244c991a945b8bfd91bf29 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 3 Jan 2024 09:48:36 -0500 Subject: [PATCH] Reformat main entry --- lib/parser/prism.rb | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/parser/prism.rb b/lib/parser/prism.rb index 55db1b8..31d3191 100644 --- a/lib/parser/prism.rb +++ b/lib/parser/prism.rb @@ -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 @@ -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 @@ -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 @@ -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 } @@ -91,10 +104,12 @@ 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 @@ -102,6 +117,7 @@ def build_comments(comments) 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