Skip to content

Commit

Permalink
Support one-liner rescues (rescue_mod) (fixes #355)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 6, 2013
1 parent 29dac59 commit bdc894e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
16 changes: 12 additions & 4 deletions lib/opal/grammar.rb

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

6 changes: 6 additions & 0 deletions lib/opal/grammar.y
Expand Up @@ -121,6 +121,9 @@ stmt:
result = s(:until, val[2], val[0], true)
}
| stmt RESCUE_MOD stmt
{
result = s(:rescue_mod, val[0], val[2])
}
| klBEGIN LCURLY compstmt '}'
| klEND LCURLY compstmt '}'
| lhs '=' command_call
Expand Down Expand Up @@ -439,6 +442,9 @@ arg:
result = new_assign val[0], val[2]
}
| lhs '=' arg RESCUE_MOD arg
{
result = new_assign val[0], s(:rescue_mod, val[2], val[4])
}
| var_lhs OP_ASGN arg
{
result = new_op_asgn val[1].intern, val[0], val[2]
Expand Down
24 changes: 23 additions & 1 deletion lib/opal/parser.rb
Expand Up @@ -396,10 +396,14 @@ def returns(sexp)
when :begin
sexp[1] = returns sexp[1]
sexp
when :rescue_mod
sexp[1] = returns sexp[1]
sexp[2] = returns sexp[2]
sexp
when :while
# sexp[2] = returns(sexp[2])
sexp
when :return
when :return, :js_return
sexp
when :xstr
sexp[1] = "return #{sexp[1]};" unless /return|;/ =~ sexp[1]
Expand Down Expand Up @@ -2217,6 +2221,24 @@ def process_resbody(exp, level)
[f("if (", exp), err, f("){#@space", exp), val, body, f("}", exp)]
end

def process_rescue_mod(sexp, level)
body, resc = sexp

unless level == :stmt
body = returns body
resc = returns resc
end

result = [f("try { "), process(body), f(" } catch($err) { "), process(resc), f(" }")]

unless level == :stmt
result.unshift f("(function() {")
result.push f("})()")
end

result
end

# FIXME: Hack.. grammar should remove top level begin.
def process_begin(exp, level)
process exp[0], level
Expand Down
1 change: 0 additions & 1 deletion spec/filters/bugs/language.rb
Expand Up @@ -188,7 +188,6 @@
fails "Operators = %= /= -= += |= &= >>= <<= *= &&= ||= **= have higher precedence than defined? operator"
fails "Operators = %= /= -= += |= &= >>= <<= *= &&= ||= **= are right-associative"
fails "Operators rescue has higher precedence than ="
fails "Operators ? : has higher precedence than rescue"
fails "Operators + - have higher precedence than >> <<"
fails "Operators + - are left-associative"
fails "Operators * / % are left-associative"
Expand Down

0 comments on commit bdc894e

Please sign in to comment.