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

Commit

Permalink
Change namespace for speed
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jan 3, 2024
1 parent aabf999 commit 2c18703
Show file tree
Hide file tree
Showing 2 changed files with 1,531 additions and 1,515 deletions.
39 changes: 25 additions & 14 deletions lib/parser/prism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ def yyerror
#
def parse(source_buffer)
@source_buffer = source_buffer
source = source_buffer.source

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

build_ast(result, offset_cache)
ensure
@source_buffer = nil
end
Expand All @@ -41,9 +45,12 @@ def parse(source_buffer)
#
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)

result = ::Prism.parse(source_buffer.source, filepath: source_buffer.name)
[build_ast(result.value), build_comments(result.comments)]
[build_ast(result.value, offset_cache), build_comments(result.comments)]
ensure
@source_buffer = nil
end
Expand All @@ -57,11 +64,13 @@ def parse_with_comments(source_buffer)
#
def tokenize(source_buffer, _recover = false)
@source_buffer = source_buffer
souce = source_buffer.source

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

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

private

def build_ast(program)
def build_offset_cache(source)
if source.bytesize == source.length
-> (offset) { offset }
else
Hash.new { |hash, offset| hash[offset] = source.byteslice(0, offset).length }
end
end

def build_ast(program, offset_cache)
program.accept(Compiler.new(self, offset_cache))
end

Expand All @@ -85,15 +102,9 @@ def build_comments(comments)
end
end

def build_tokens(tokens)
def build_tokens(tokens, offset_cache)
Lexer.new(source_buffer, tokens.map(&:first), offset_cache).to_a
end

def offset_cache
@offset_cache ||= Hash.new do |h, k|
h[k] = @source_buffer.source.byteslice(0, k).length
end
end
end
end

Expand Down
Loading

0 comments on commit 2c18703

Please sign in to comment.