Skip to content

Commit

Permalink
+ ruby28.y: make brackets madatory for endless def
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Apr 25, 2020
1 parent a7cdea8 commit 67f58c0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
12 changes: 6 additions & 6 deletions doc/AST_FORMAT.md
Expand Up @@ -808,11 +808,11 @@ Format:

~~~
(def_e :foo (args) (int 42))
"def foo = 42"
"def foo() = 42"
~~~ keyword
~~~ name
^ assignment
~~~~~~~~~~~~ expression
^ assignment
~~~~~~~~~~~~~~ expression
~~~


Expand All @@ -822,11 +822,11 @@ Format:

~~~
(defs_e (self) :foo (args) (int 42))
"def self.foo = 42"
"def self.foo() = 42"
~~~ keyword
~~~ name
^ assignment
~~~~~~~~~~~~~~~~~ expression
^ assignment
~~~~~~~~~~~~~~~~~~~ expression
~~~

### Undefinition
Expand Down
14 changes: 5 additions & 9 deletions lib/parser/ruby28.y
Expand Up @@ -846,7 +846,7 @@ rule
result = @builder.ternary(val[0], val[1],
val[2], val[4], val[5])
}
| defn_head f_arglist_opt tEQL arg
| defn_head f_paren_args tEQL arg
{
result = @builder.def_endless_method(*val[0],
val[1], val[2], val[3])
Expand All @@ -857,7 +857,7 @@ rule
@context.pop
@current_arg_stack.pop
}
| defs_head f_arglist_opt tEQL arg
| defs_head f_paren_args tEQL arg
{
result = @builder.def_endless_singleton(*val[0],
val[1], val[2], val[3])
Expand Down Expand Up @@ -2546,18 +2546,14 @@ keyword_variable: kNIL
result = nil
}
f_arglist_opt: # nothing
f_paren_args: tLPAREN2 f_args rparen
{
result = @builder.args(nil, [], nil)
}
| f_arglist
f_arglist: tLPAREN2 f_args rparen
{
result = @builder.args(val[0], val[1], val[2])
@lexer.state = :expr_value
}
f_arglist: f_paren_args
| tLPAREN2 args_forward rparen
{
result = @builder.forward_args(val[0], val[1], val[2])
Expand Down
10 changes: 6 additions & 4 deletions test/test_lexer.rb
Expand Up @@ -3572,11 +3572,13 @@ def test_ambiguous_integer_re
def test_endless_method
setup_lexer(28)

assert_scanned('def foo = 42',
assert_scanned('def foo() = 42',
:kDEF, "def", [0, 3],
:tIDENTIFIER, 'foo', [4, 7],
:tEQL, "=", [8, 9],
:tINTEGER, 42, [10, 12])
:tIDENTIFIER, "foo", [4, 7],
:tLPAREN2, "(", [7, 8],
:tRPAREN, ")", [8, 9],
:tEQL, "=", [10, 11],
:tINTEGER, 42, [12, 14])
end

def lex_numbered_parameter(input)
Expand Down
26 changes: 20 additions & 6 deletions test/test_parser.rb
Expand Up @@ -9434,11 +9434,11 @@ def test_endless_method
s(:def_e, :foo,
s(:args),
s(:int, 42)),
%q{def foo = 42},
%q{def foo() = 42},
%q{~~~ keyword
| ~~~ name
| ^ assignment
|~~~~~~~~~~~~ expression},
| ^ assignment
|~~~~~~~~~~~~~~ expression},
SINCE_2_8)

assert_parses(
Expand All @@ -9458,12 +9458,12 @@ def test_endless_method
s(:defs_e, s(:send, nil, :obj), :foo,
s(:args),
s(:int, 42)),
%q{def obj.foo = 42},
%q{def obj.foo() = 42},
%q{~~~ keyword
| ^ operator
| ~~~ name
| ^ assignment
|~~~~~~~~~~~~~~~~ expression},
| ^ assignment
|~~~~~~~~~~~~~~~~~~ expression},
SINCE_2_8)

assert_parses(
Expand All @@ -9480,4 +9480,18 @@ def test_endless_method
|~~~~~~~~~~~~~~~~~~~~~~ expression},
SINCE_2_8)
end

def test_endless_method_without_brackets
assert_diagnoses(
[:error, :unexpected_token, { :token => 'tEQL' }],
%Q{def foo = 42},
%q{ ^ location},
SINCE_2_8)

assert_diagnoses(
[:error, :unexpected_token, { :token => 'tEQL' }],
%Q{def obj.foo = 42},
%q{ ^ location},
SINCE_2_8)
end
end

0 comments on commit 67f58c0

Please sign in to comment.