Skip to content

Commit

Permalink
Remove zsuper from ast: a super is a super() is a super(x)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Sep 25, 2013
1 parent da95ec6 commit 26eaf56
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 40 deletions.
20 changes: 15 additions & 5 deletions lib/opal/grammar.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions lib/opal/grammar.y
Expand Up @@ -816,8 +816,13 @@ primary:
| method_call
| method_call brace_block
{
result = val[1]
result[1] = val[0]
if val[0][0] == :super
val[0] << val[1]
result = val[0]
else
result = val[1]
result[1] = val[0]
end
}
| LAMBDA lambda
{
Expand Down Expand Up @@ -1129,8 +1134,13 @@ do_block:
block_call:
command do_block
{
result = val[1]
result[1] = val[0]
if val[0][0] == :super
val[0] << val[1]
result = val[0]
else
result = val[1]
result[1] = val[0]
end
}
| block_call '.' operation2 opt_paren_args
| block_call '::' operation2 opt_paren_args
Expand Down Expand Up @@ -1162,7 +1172,7 @@ method_call:
}
| SUPER
{
result = s(:zsuper)
result = s(:super, nil)
}

brace_block:
Expand Down
9 changes: 7 additions & 2 deletions lib/opal/grammar_helpers.rb
Expand Up @@ -291,8 +291,13 @@ def new_var_ref(ref)
end

def new_super(args)
args = (args || s(:arglist))[1..-1]
s(:super, *args)
args = (args || s(:arglist))

if args[0] == :array
args[0] = :arglist
end

s(:super, args)
end

def new_yield(args)
Expand Down
33 changes: 16 additions & 17 deletions lib/opal/parser.rb
Expand Up @@ -2054,27 +2054,26 @@ def process_colon3(exp, level)
#
# s(:super, arg1, arg2, ...)
def process_super(sexp, level)
splat = sexp.any? { |s| s.first == :splat }
args = s(:arglist, *sexp)
args = process args
args, passes_block = sexp[0], true

unless splat
args = [f("["), args, f("]")]
end
if args
passes_block = false
splat = args[1..-1].any? { |s| s.first == :splat }
args = process args

js_super args, false, sexp
end

# super
#
# s(:zsuper)
def process_zsuper(exp, level)
if @scope.def?
@scope.uses_zuper = true
js_super f("$zuper", exp), true, exp
unless splat
args = [f("["), args, f("]")]
end
else
js_super f("$slice.call(arguments)", exp), true, exp
if @scope.def?
@scope.uses_zuper = true
args = f("$zuper", sexp)
else
args = f("$slice.call(arguments)", sexp)
end
end

js_super args, passes_block, sexp
end

def js_super args, pass_block, sexp
Expand Down
22 changes: 11 additions & 11 deletions spec/parser/super_spec.rb
@@ -1,20 +1,20 @@
require 'spec_helper'

describe "The super keyword" do
it "should return s(:zsuper) when no arguments or parans" do
opal_parse("super").should == [:zsuper]
it "should return s(:super) for any arguments" do
opal_parse("super 1").should == [:super, [:arglist, [:int, 1]]]
opal_parse("super 1, 2").should == [:super, [:arglist, [:int, 1], [:int, 2]]]
opal_parse("super 1, *2").should == [:super, [:arglist, [:int, 1], [:splat, [:int, 2]]]]
end

it "should return s(:super) for any arguments" do
opal_parse("super 1").should == [:super, [:int, 1]]
opal_parse("super 1, 2").should == [:super, [:int, 1], [:int, 2]]
opal_parse("super 1, *2").should == [:super, [:int, 1], [:splat, [:int, 2]]]
it "should set nil for args when no arguments or parans" do
opal_parse("super").should == [:super, nil]
end

it "should always return s(:super) when parans are used" do
opal_parse("super()").should == [:super]
opal_parse("super(1)").should == [:super, [:int, 1]]
opal_parse("super(1, 2)").should == [:super, [:int, 1], [:int, 2]]
opal_parse("super(1, *2)").should == [:super, [:int, 1], [:splat, [:int, 2]]]
it "should always return s(:super) with :arglist when parans are used" do
opal_parse("super()").should == [:super, [:arglist]]
opal_parse("super(1)").should == [:super, [:arglist, [:int, 1]]]
opal_parse("super(1, 2)").should == [:super, [:arglist, [:int, 1], [:int, 2]]]
opal_parse("super(1, *2)").should == [:super, [:arglist, [:int, 1], [:splat, [:int, 2]]]]
end
end

0 comments on commit 26eaf56

Please sign in to comment.