Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/prism/node_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ def signature
case param
when MultiTargetNode
names << [:req]
when NoKeywordsParameterNode, KeywordRestParameterNode, ForwardingParameterNode
when NoKeywordsParameterNode, KeywordRestParameterNode,
NoBlockParameterNode, BlockParameterNode,
ForwardingParameterNode
# Invalid syntax, e.g. "def f(**nil, ...)" moves the NoKeywordsParameterNode to posts
raise "Invalid syntax"
else
Expand Down
12 changes: 10 additions & 2 deletions lib/prism/translation/parser/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ class Parser
# A builder that knows how to convert more modern Ruby syntax
# into whitequark/parser gem's syntax tree.
class Builder < ::Parser::Builders::Default
# It represents the `it` block argument, which is not yet implemented in the Parser gem.
# It represents the `it` block argument, which is not yet implemented in
# the Parser gem.
def itarg
n(:itarg, [:it], nil)
end

# The following three lines have been added to support the `it` block parameter syntax in the source code below.
# The following three lines have been added to support the `it` block
# parameter syntax in the source code below.
#
# if args.type == :itarg
# block_type = :itblock
Expand Down Expand Up @@ -56,6 +58,12 @@ def block(method_call, begin_t, args, body, end_t)
method_call.loc.with_expression(join_exprs(method_call, block)))
end
end

# def foo(&nil); end
# ^^^^
def blocknilarg(amper_t, nil_t)
n0(:blocknilarg, arg_prefix_map(amper_t, nil_t))
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,12 @@ def visit_nil_node(node)
builder.nil(token(node.location))
end

# def foo(&nil); end
# ^^^^
def visit_no_block_parameter_node(node)
builder.blocknilarg(token(node.operator_loc), token(node.keyword_loc))
end

# def foo(**nil); end
# ^^^^^
def visit_no_keywords_parameter_node(node)
Expand Down
3 changes: 3 additions & 0 deletions prism/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4065,6 +4065,9 @@ nodes:
- on error: NoKeywordsParameterNode
# On parsing error of `f(..., ...)`, the first forwarding parameter is moved here:
- on error: ForwardingParameterNode
# On parsing error of `f(&nil, &foo)`/`f(&foo, &nil)`, the first forwarding parameter is moved here:
- on error: BlockParameterNode
- on error: NoBlockParameterNode
- name: keywords
type: node[]
kind:
Expand Down
2 changes: 1 addition & 1 deletion prism/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -13949,7 +13949,7 @@ parse_parameters(
pm_token_t operator = parser->previous;
pm_node_t *param;

if (accept1(parser, PM_TOKEN_KEYWORD_NIL)) {
if (parser->version >= PM_OPTIONS_VERSION_CRUBY_4_1 && accept1(parser, PM_TOKEN_KEYWORD_NIL)) {
param = (pm_node_t *) pm_no_block_parameter_node_create(parser, &operator, &parser->previous);
} else {
pm_token_t name = {0};
Expand Down
6 changes: 6 additions & 0 deletions test/prism/errors/3.3-4.0/noblock.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def foo(&nil)
^~~ unexpected 'nil'; expected a `)` to close the parameters
^ unexpected ')', expecting end-of-input
^ unexpected ')', ignoring it
end

12 changes: 12 additions & 0 deletions test/prism/errors/4.1/multiple_blocks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def foo(&nil, &nil); end
^ unexpected parameter order
^~~~ multiple block parameters; only one block is allowed

def foo(&foo, &nil); end
^ unexpected parameter order
^~~~ multiple block parameters; only one block is allowed

def foo(&nil, &foo); end
^ unexpected parameter order
^~~~ multiple block parameters; only one block is allowed

4 changes: 4 additions & 0 deletions test/prism/fixtures/4.1/noblock.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def foo(&nil)
end

-> (&nil) {}
4 changes: 2 additions & 2 deletions test/prism/locals_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LocalsTest < TestCase
"command_method_call_2.txt",

# https://bugs.ruby-lang.org/issues/21669
"4.1/void_value.txt"
"4.1/void_value.txt",
]

Fixture.each_for_current_ruby(except: except) do |fixture|
Expand Down Expand Up @@ -207,7 +207,7 @@ def prism_locals(source)
end
end

if params.block
if params.block.is_a?(BlockParameterNode)
sorted << (params.block.name || :&)
end

Expand Down
3 changes: 3 additions & 0 deletions test/prism/ruby/ripper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class RipperTest < TestCase
# https://bugs.ruby-lang.org/issues/21669
incorrect << "4.1/void_value.txt"

# https://bugs.ruby-lang.org/issues/19979
incorrect << "4.1/noblock.txt"

# Skip these tests that we haven't implemented yet.
omitted_sexp_raw = [
"bom_leading_space.txt",
Expand Down