From a7ea09750252edb05465232db0ee2e4ed8ac4039 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Fri, 13 Mar 2009 16:48:57 +0000 Subject: Modifies try/catch/finally to allow arbitrary e... Modifies try/catch/finally to allow arbitrary expressions for try. Formerly { ... } was required for try. Now expressions like: val x = try Integer.parseInt("xx") catch { case e => 10 } work as one would hope. --- src/compiler/scala/tools/nsc/ast/parser/Parsers.scala | 9 ++++++--- test/files/pos/tryexpr.scala | 10 ++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 test/files/pos/tryexpr.scala diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index ed8a5d1a35..c1c5e26d3d 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -956,7 +956,7 @@ trait Parsers extends NewScanners with MarkupParsers { * ResultExpr ::= (Bindings | Id `:' CompoundType) `=>' Block * | Expr1 * Expr1 ::= if `(' Expr `)' {nl} Expr [[semi] else Expr] - * | try `{' Block `}' [catch `{' CaseClauses `}'] [finally Expr] + * | try (`{' Block `}' | Expr) [catch `{' CaseClauses `}'] [finally Expr] * | while `(' Expr `)' {nl} Expr * | do Expr [semi] while `(' Expr `)' * | for (`(' Enumerators `)' | '{' Enumerators '}') {nl} [yield] Expr @@ -997,13 +997,16 @@ trait Parsers extends NewScanners with MarkupParsers { atPos(pos) { If(cond, thenp, elsep) } case TRY => atPos(inSkipToken) { - val body = surround(LBRACE,RBRACE)(block(), Literal(())) + val body = + if (inToken == LBRACE) surround(LBRACE, RBRACE)(block(), Literal(())) + else if (inToken == LPAREN) surround(LPAREN, RPAREN)(expr(), Literal(())) + else expr() val catches = if (inToken == CATCH) { inNextToken val cases = surround(LBRACE,RBRACE)(caseClauses(), Nil) cases - } else List() + } else Nil val finalizer = if (inToken == FINALLY) { inNextToken; expr() } else EmptyTree diff --git a/test/files/pos/tryexpr.scala b/test/files/pos/tryexpr.scala new file mode 100644 index 0000000000..c16428e7a5 --- /dev/null +++ b/test/files/pos/tryexpr.scala @@ -0,0 +1,10 @@ +// stretching more flexible try/catch's legs a bit +object o { + try Integer.parseInt("xxxx") catch { case e => 5 } + try 5 + try try try 10 + try try try 10 catch { case e => 20 } finally 30 + try try try 10 catch { case e => 20 } finally 30 finally 40 + try try try 10 catch { case e => 20 } finally 30 finally 40 finally 50 + try try try 10 finally 50 +} \ No newline at end of file -- cgit v1.2.3