diff libinterp/parse-tree/oct-parse.in.yy @ 17249:923ce8b42db2

improve try-catch-statement to save exception to a variable (bug #33217) * oct-parse.in.yy (except_command): Handle exception identifiers. * oct-parse.in.yy, parse.h (octave_base_parser::make_try_command): New arg, ID. * pt-except.cc, pt-except.h (tree_try_catch_command::expr_id): New data member. (tree_command::tree_command): Initialize it. (tree_command::~tree_command): Delete it. (tree_command::identifier): New function. * pt-check.cc (tree_checker::visit_try_catch_command): Check for valid expr_id. * pt-pr-code.cc (tree_print_code::visit_try_catch_command): Print expr_id. * pt-eval.cc (tree_evaluator::visit_try_catch_command): Assign message and identifier to variable. * try.tst: New test. * NEWS: Note change.
author Stefan Mahr <dac922@gmx.de>
date Tue, 13 Aug 2013 19:35:53 +0200
parents d6499c14021c
children bc924baa2c4e
line wrap: on
line diff
--- a/libinterp/parse-tree/oct-parse.in.yy	Tue Aug 13 19:31:59 2013 -0400
+++ b/libinterp/parse-tree/oct-parse.in.yy	Tue Aug 13 19:35:53 2013 +0200
@@ -240,7 +240,7 @@
 %type <tree_expression_type> matrix cell
 %type <tree_expression_type> primary_expr oper_expr
 %type <tree_expression_type> simple_expr colon_expr assign_expr expression
-%type <tree_identifier_type> identifier fcn_name magic_tilde
+%type <tree_identifier_type> identifier fcn_name magic_tilde opt_identifier
 %type <tree_identifier_type> superclass_identifier meta_identifier
 %type <octave_user_function_type> function1 function2 classdef1
 %type <tree_index_expression_type> word_list_cmd
@@ -975,19 +975,30 @@
                     if (! ($$ = parser.make_unwind_command ($1, $4, $8, $9, $2, $6)))
                       ABORT_PARSE;
                   }
-                | TRY stash_comment opt_sep opt_list CATCH
+                | TRY stash_comment opt_sep opt_list CATCH list END
+                  {
+                    if (! ($$ = parser.make_try_command ($1, $4, $6, 0, $7, $2, 0)))
+                      ABORT_PARSE;
+                  }
+                | TRY stash_comment opt_sep opt_list CATCH opt_identifier
                   stash_comment opt_sep opt_list END
                   {
-                    if (! ($$ = parser.make_try_command ($1, $4, $8, $9, $2, $6)))
+                    if (! ($$ = parser.make_try_command ($1, $4, $9, $6, $10, $2, $7)))
                       ABORT_PARSE;
                   }
                 | TRY stash_comment opt_sep opt_list END
                   {
-                    if (! ($$ = parser.make_try_command ($1, $4, 0, $5, $2, 0)))
+                    if (! ($$ = parser.make_try_command ($1, $4, 0, 0, $5, $2, 0)))
                       ABORT_PARSE;
                   }
                 ;
 
+opt_identifier: // empty
+                  { $$ = 0; }
+                | identifier sep
+                  { $$ = $1; }
+                ;
+
 // ===========================================
 // Some 'subroutines' for function definitions
 // ===========================================
@@ -2191,6 +2202,7 @@
 octave_base_parser::make_try_command (token *try_tok,
                                       tree_statement_list *body,
                                       tree_statement_list *cleanup_stmts,
+                                      tree_identifier *id,
                                       token *end_tok,
                                       octave_comment_list *lc,
                                       octave_comment_list *mc)
@@ -2204,7 +2216,7 @@
       int l = try_tok->line ();
       int c = try_tok->column ();
 
-      retval = new tree_try_catch_command (body, cleanup_stmts,
+      retval = new tree_try_catch_command (body, cleanup_stmts, id,
                                            lc, mc, tc, l, c);
     }