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

Commit

Permalink
Update to prism v0.17.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Nov 3, 2023
1 parent 64f78d0 commit cff12ee
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 55 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ GEM
ast (~> 2.4.1)
racc
power_assert (2.0.3)
prism (0.15.1)
racc (1.7.1)
prism (0.17.1)
racc (1.7.2)
rainbow (3.1.1)
rake (13.0.6)
rake (13.1.0)
regexp_parser (2.8.2)
rexml (3.2.6)
rubocop (1.57.2)
Expand All @@ -34,7 +34,7 @@ GEM
rubocop-ast (>= 1.28.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
test-unit (3.6.1)
Expand Down
4 changes: 2 additions & 2 deletions lib/parser/prism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def parse_with_comments(source_buffer)
def tokenize(source_buffer, _recover = false)
@source_buffer = source_buffer

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

[build_ast(program), build_comments(result.comments), build_tokens(tokens)]
Expand All @@ -80,7 +80,7 @@ def build_ast(program)

def build_comments(comments)
comments.each_with_object([]) do |comment, result|
next if comment.type == :__END__
next if comment.is_a?(::Prism::DATAComment)

location = comment.location
range = Source::Range.new(source_buffer, location.start_offset, location.end_offset)
Expand Down
81 changes: 44 additions & 37 deletions lib/parser/prism/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,9 @@ def visit_hash_node(node)
# ^^
def visit_hash_pattern_node(node)
if node.constant
builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.hash_pattern(nil, visit_all(node.assocs), nil), token(node.closing_loc))
builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.hash_pattern(nil, visit_all(node.elements), nil), token(node.closing_loc))
else
builder.hash_pattern(token(node.opening_loc), visit_all(node.assocs), token(node.closing_loc))
builder.hash_pattern(token(node.opening_loc), visit_all(node.elements), token(node.closing_loc))
end
end

Expand Down Expand Up @@ -884,19 +884,6 @@ def visit_keyword_hash_node(node)
builder.associate(nil, visit_all(node.elements), nil)
end

# def foo(bar:); end
# ^^^^
#
# def foo(bar: baz); end
# ^^^^^^^^
def visit_keyword_parameter_node(node)
if node.value
builder.kwoptarg([node.name, srange(node.name_loc)], visit(node.value))
else
builder.kwarg([node.name, srange(node.name_loc)])
end
end

# def foo(**bar); end
# ^^^^^
#
Expand Down Expand Up @@ -1030,15 +1017,13 @@ def visit_module_node(node)
# foo, bar = baz
# ^^^^^^^^
def visit_multi_target_node(node)
if node.targets.length == 1 || (node.targets.length == 2 && node.targets.last.is_a?(::Prism::SplatNode) && node.targets.last.operator == ",")
visit(node.targets.first)
else
builder.multi_lhs(
token(node.lparen_loc),
visit_all(node.targets),
token(node.rparen_loc)
)
end
node = node.copy(rest: nil) if node.rest&.operator == ","

builder.multi_lhs(
token(node.lparen_loc),
visit_all([*node.lefts, *node.rest, *node.rights]),
token(node.rparen_loc)
)
end

# foo, bar = baz
Expand All @@ -1047,7 +1032,7 @@ def visit_multi_write_node(node)
builder.multi_assign(
builder.multi_lhs(
token(node.lparen_loc),
visit_all(node.targets),
visit_all([*node.lefts, *node.rest, *node.rights]),
token(node.rparen_loc)
),
token(node.operator_loc),
Expand Down Expand Up @@ -1088,6 +1073,12 @@ def visit_numbered_reference_read_node(node)
builder.nth_ref([node.number, srange(node.location)])
end

# def foo(bar: baz); end
# ^^^^^^^^
def visit_optional_keyword_parameter_node(node)
builder.kwoptarg([node.name, srange(node.name_loc)], visit(node.value))
end

# def foo(bar = 1); end
# ^^^^^^^
def visit_optional_parameter_node(node)
Expand All @@ -1104,10 +1095,32 @@ def visit_or_node(node)
# ^^^^^^^^^
def visit_parameters_node(node)
params = []
params.concat(visit_all(node.requireds)) if node.requireds.any?

if node.requireds.any?
node.requireds.each do |required|
if required.is_a?(::Prism::RequiredParameterNode)
params << visit(required)
else
compiler = copy_compiler(in_destructure: true)
params << required.accept(compiler)
end
end
end

params.concat(visit_all(node.optionals)) if node.optionals.any?
params << visit(node.rest) if !node.rest.nil? && node.rest.operator != ","
params.concat(visit_all(node.posts)) if node.posts.any?

if node.posts.any?
node.posts.each do |post|
if post.is_a?(::Prism::RequiredParameterNode)
params << visit(post)
else
compiler = copy_compiler(in_destructure: true)
params << post.accept(compiler)
end
end
end

params.concat(visit_all(node.keywords)) if node.keywords.any?
params << visit(node.keyword_rest) if !node.keyword_rest.nil?
params << visit(node.block) if !node.block.nil?
Expand Down Expand Up @@ -1213,16 +1226,10 @@ def visit_regular_expression_node(node)
# ^^^^^
alias visit_match_last_line_node visit_regular_expression_node

# def foo((bar)); end
# ^^^^^
def visit_required_destructured_parameter_node(node)
compiler = copy_compiler(in_destructure: true)

builder.multi_lhs(
token(node.opening_loc),
node.parameters.map { |parameter| parameter.accept(compiler) },
token(node.closing_loc)
)
# def foo(bar:); end
# ^^^^
def visit_required_keyword_parameter_node(node)
builder.kwarg([node.name, srange(node.name_loc)])
end

# def foo(bar); end
Expand Down
17 changes: 5 additions & 12 deletions test/prism_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
class PrismTest < Test::Unit::TestCase
skip = [
# These files contain invalid syntax. We should never try to parse them.
"control_meta_escape_chars_in_regexp__since_31.rb",
"newline_in_hash_argument.rb",
"pattern_match.rb",
"range_endless.rb",
"unary_num_pow_precedence.rb",

# These parse differently from MRI with the parser gem, so nothing we can do
Expand All @@ -31,19 +28,15 @@ class PrismTest < Test::Unit::TestCase
"endless_method_command_syntax.rb",
"ruby_bug_12402.rb",

# These are location bounds issues. They are bugs in translation, but not
# bugs in prism.
"bug_rescue_empty_else.rb",
"ensure_empty.rb",
"rescue_else.rb",
"rescue_else_ensure.rb",
"rescue_in_lambda_block.rb",

# Some kind of issue with the end location of heredocs including newlines.
"dedenting_heredoc.rb",
"parser_bug_640.rb",
"parser_drops_truncated_parts_of_squiggly_heredoc.rb",
"slash_newline_in_heredocs.rb"
"slash_newline_in_heredocs.rb",

# Unclear what to do here when you have nesting, it appears that the parser
# gem is just skipping some levels.
"masgn_nested.rb"
]

# We haven't fully implemented tokenization properly yet. Most of these are
Expand Down

0 comments on commit cff12ee

Please sign in to comment.