summaryrefslogtreecommitdiff
path: root/perly.y
diff options
context:
space:
mode:
authorPaul "LeoNerd" Evans <leonerd@leonerd.org.uk>2022-06-08 12:35:25 +0100
committerPaul Evans <leonerd@leonerd.org.uk>2022-06-13 22:07:14 +0100
commit0065df430e3145136f9718fe1a6ba3810ef9bc91 (patch)
tree025c26e7d4780f8daf0a0dce3a29a15253fb13d1 /perly.y
parentfecef29147d516d89233c43e3a9c9800e6c7f9f4 (diff)
downloadperl-0065df430e3145136f9718fe1a6ba3810ef9bc91.tar.gz
A better error message for `try {} catch {}` missing its (VAR)
As suggested in https://github.com/Perl/perl5/issues/19811, this now outputs the message: $ ./perl -Mexperimental=try try { A() } catch { B() } catch block requires a (VAR) at - line 2, near "catch {" Execution of - aborted due to compilation errors.
Diffstat (limited to 'perly.y')
-rw-r--r--perly.y26
1 files changed, 21 insertions, 5 deletions
diff --git a/perly.y b/perly.y
index 55321fa41e..db1daf7747 100644
--- a/perly.y
+++ b/perly.y
@@ -87,6 +87,7 @@
%type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else finally
%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
%type <opval> condition
+%type <opval> catch_paren
%type <opval> empty
%type <opval> sliceme kvslice gelem
%type <opval> listexpr nexpr texpr iexpr mexpr mnexpr
@@ -255,6 +256,18 @@ mremember: %empty /* start a partial lexical scope */
parser->parsed_sub = 0; }
;
+/* The parenthesized variable of a catch block */
+catch_paren: empty
+ /* not really valid grammar but we detect it in the
+ * action block to throw a nicer error message */
+ | PERLY_PAREN_OPEN
+ { parser->in_my = 1; }
+ scalar
+ { parser->in_my = 0; intro_my(); }
+ PERLY_PAREN_CLOSE
+ { $$ = $scalar; }
+ ;
+
/* A sequence of statements in the program */
stmtseq
: empty
@@ -475,11 +488,14 @@ barestmt: PLUGSTMT
newFOROP(0, NULL, $mexpr, $mblock, $cont));
parser->copline = (line_t)$FOR;
}
- | TRY mblock[try] CATCH PERLY_PAREN_OPEN
- { parser->in_my = 1; }
- remember scalar
- { parser->in_my = 0; intro_my(); }
- PERLY_PAREN_CLOSE mblock[catch] finally
+ | TRY mblock[try] CATCH remember catch_paren[scalar]
+ {
+ if(!$scalar) {
+ yyerror("catch block requires a (VAR)");
+ YYERROR;
+ }
+ }
+ mblock[catch] finally
{
$$ = newTRYCATCHOP(0,
$try, $scalar, block_end($remember, op_scope($catch)));