Skip to content

Commit 6068423

Browse files
committedMay 5, 2015
Fix bug where keyword args would not parse without method parens
This correctly sets the lexer state to :expr_endfn in various circumstances, so might actually fix more bugs at the same time. Fixes #688.
1 parent 485400e commit 6068423

File tree

5 files changed

+17
-0
lines changed

5 files changed

+17
-0
lines changed
 

Diff for: ‎CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## edge (upcoming 0.8.0)
22

3+
* Parser: Fix bug where keyword arguments could not be parsed if
4+
method definition did not have parens around arguments.
5+
36
* Support calling `String#[]` and `String#slice` with a regexp argument
47

58
* `String#[]` and `String#slice` implementation fully compliant with rubyspec

Diff for: ‎lib/opal/parser/grammar.rb

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎lib/opal/parser/grammar.y

+3
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,7 @@ rule
899899
| kDEF fname
900900
{
901901
push_scope
902+
lexer.lex_state = :expr_endfn
902903
}
903904
f_arglist bodystmt kEND
904905
{
@@ -912,6 +913,7 @@ rule
912913
fname
913914
{
914915
push_scope
916+
lexer.lex_state = :expr_endfn
915917
}
916918
f_arglist bodystmt kEND
917919
{
@@ -1456,6 +1458,7 @@ xstring_contents: none
14561458
| f_args term
14571459
{
14581460
result = val[0]
1461+
lexer.lex_state = :expr_beg
14591462
}
14601463

14611464
kwrest_mark: tPOW

Diff for: ‎lib/opal/parser/lexer.rb

+2
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,8 @@ def process_identifier(matched, cmd_start)
620620

621621
if [:expr_beg, :expr_dot, :expr_mid, :expr_arg, :expr_cmdarg].include? @lex_state
622622
@lex_state = cmd_start ? :expr_cmdarg : :expr_arg
623+
elsif @lex_state == :expr_fname
624+
@lex_state = :expr_endfn
623625
else
624626
@lex_state = :expr_end
625627
end

Diff for: ‎spec/lib/parser/def_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,11 @@
7070
it "should parse combinations of keyword args" do
7171
parsed("def foo(a:, b: 1, **c); end")[3].should == [:args, [:kwarg, :a], [:kwoptarg, :b, [:int, 1]], [:kwrestarg, :c]]
7272
end
73+
74+
it "should parse keywords without parens" do
75+
parsed("def foo a:; end")[3].should == [:args, [:kwarg, :a]]
76+
parsed("def foo a:, b:; end")[3].should == [:args, [:kwarg, :a], [:kwarg, :b]]
77+
parsed("def foo a: 1, b: 2; end")[3].should == [:args, [:kwoptarg, :a, [:int, 1]], [:kwoptarg, :b, [:int, 2]]]
78+
end
7379
end
7480
end

2 commit comments

Comments
 (2)

adambeynon commented on May 5, 2015

@adambeynon
ContributorAuthor

First commit in 4 weeks, woop! 🌟

elia commented on May 5, 2015

@elia
Member

💖

Please sign in to comment.