# HG changeset patch
# User Stefan Mahr <dac922@gmx.de>
# Date 1365612329 -7200
# Node ID b4f263aebe7b5e2b2f41d00f9fa18eade4686c77
# Parent  6a18d8b0f9821f96345464d3c8f6fccea1f0331c
improve try-catch-statement to save exception to a variable

* oct-parse.in.yy (except_command): extend try-catch-statements to handle optional variable
* oct-parse.in.yy, parse.h (make_try_command): add parameter id, used for optional variable
* pt-except.cc, pt-except.h (tree_try_catch_command): add support of expr_id to class
* pt-check.cc (visit_try_catch_command): add check of expr_id
* pt-pr-code.cc (visit_try_catch_command): add handling of expr_id
* pt-eval.cc (visit_try_catch_command): assign message and identifier to variable

diff -r 6a18d8b0f982 -r b4f263aebe7b libinterp/parse-tree/oct-parse.in.yy
--- a/libinterp/parse-tree/oct-parse.in.yy	Wed Apr 10 06:59:55 2013 +0200
+++ b/libinterp/parse-tree/oct-parse.in.yy	Wed Apr 10 18:45:29 2013 +0200
@@ -973,12 +973,18 @@
                 | TRY stash_comment opt_sep opt_list CATCH
                   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, $8, 0, $9, $2, $6)))
+                      ABORT_PARSE;
+                  }
+                | TRY stash_comment opt_sep opt_list CATCH identifier sep
+                  stash_comment opt_sep opt_list END
+                  {
+                    if (! ($$ = parser.make_try_command ($1, $4, $10, $6, $11, $2, $8)))
                       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;
                   }
                 ;
@@ -2167,6 +2173,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)
@@ -2180,7 +2187,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);
     }
 
diff -r 6a18d8b0f982 -r b4f263aebe7b libinterp/parse-tree/parse.h
--- a/libinterp/parse-tree/parse.h	Wed Apr 10 06:59:55 2013 +0200
+++ b/libinterp/parse-tree/parse.h	Wed Apr 10 18:45:29 2013 +0200
@@ -201,7 +201,7 @@
   // Build a try-catch command.
   tree_command *
   make_try_command (token *try_tok, tree_statement_list *body,
-                    tree_statement_list *cleanup, token *end_tok,
+                    tree_statement_list *cleanup, tree_identifier *id, token *end_tok,
                     octave_comment_list *lc, octave_comment_list *mc);
 
   // Build a while command.
diff -r 6a18d8b0f982 -r b4f263aebe7b libinterp/parse-tree/pt-check.cc
--- a/libinterp/parse-tree/pt-check.cc	Wed Apr 10 06:59:55 2013 +0200
+++ b/libinterp/parse-tree/pt-check.cc	Wed Apr 10 18:45:29 2013 +0200
@@ -499,6 +499,14 @@
 {
   tree_statement_list *try_code = cmd.body ();
 
+  tree_identifier *expr_id = cmd.identifier ();
+
+  if (expr_id)
+    {
+      if (! expr_id->lvalue_ok ())
+        gripe ("invalid lvalue in for command", cmd.line ());
+    }
+
   if (try_code)
     try_code->accept (*this);
 
diff -r 6a18d8b0f982 -r b4f263aebe7b libinterp/parse-tree/pt-eval.cc
--- a/libinterp/parse-tree/pt-eval.cc	Wed Apr 10 06:59:55 2013 +0200
+++ b/libinterp/parse-tree/pt-eval.cc	Wed Apr 10 18:45:29 2013 +0200
@@ -921,6 +921,27 @@
 
           buffer_error_messages--;
 
+          tree_identifier *expr_id = cmd.identifier ();
+          octave_lvalue ult;
+
+          if (expr_id)
+            {
+
+              octave_scalar_map err;
+
+              ult = expr_id->lvalue ();
+            
+              if (error_state)
+                return;
+
+              err.assign ("message", last_error_message ());
+              err.assign ("identifier", last_error_id ());
+
+              if (! error_state)
+                ult.assign(octave_value::op_asn_eq, err);
+
+            }
+
           if (catch_code)
             catch_code->accept (*this);
         }
diff -r 6a18d8b0f982 -r b4f263aebe7b libinterp/parse-tree/pt-except.cc
--- a/libinterp/parse-tree/pt-except.cc	Wed Apr 10 06:59:55 2013 +0200
+++ b/libinterp/parse-tree/pt-except.cc	Wed Apr 10 18:45:29 2013 +0200
@@ -33,6 +33,7 @@
 #include "pt-cmd.h"
 #include "pt-except.h"
 #include "pt-exp.h"
+#include "pt-id.h"
 #include "pt-jump.h"
 #include "pt-stmt.h"
 #include "pt-walk.h"
@@ -43,6 +44,7 @@
 
 tree_try_catch_command::~tree_try_catch_command (void)
 {
+  delete expr_id;
   delete try_code;
   delete catch_code;
   delete lead_comm;
@@ -57,6 +59,7 @@
   return new
     tree_try_catch_command (try_code ? try_code->dup (scope, context) : 0,
                             catch_code ? catch_code->dup (scope, context) : 0,
+                            expr_id ? expr_id->dup (scope, context) : 0,
                             lead_comm ? lead_comm->dup () : 0,
                             mid_comm ? mid_comm->dup () : 0,
                             trail_comm ? trail_comm->dup () : 0,
diff -r 6a18d8b0f982 -r b4f263aebe7b libinterp/parse-tree/pt-except.h
--- a/libinterp/parse-tree/pt-except.h	Wed Apr 10 06:59:55 2013 +0200
+++ b/libinterp/parse-tree/pt-except.h	Wed Apr 10 18:45:29 2013 +0200
@@ -29,6 +29,7 @@
 
 #include "comment-list.h"
 #include "pt-cmd.h"
+#include "pt-id.h"
 #include "symtab.h"
 
 // Simple exception handling.
@@ -39,19 +40,22 @@
 public:
 
   tree_try_catch_command (int l = -1, int c = -1)
-    : tree_command (l, c), try_code (0), catch_code (0), lead_comm (0),
+    : tree_command (l, c), try_code (0), catch_code (0), expr_id (0), lead_comm (0),
       mid_comm (0), trail_comm (0) { }
 
   tree_try_catch_command (tree_statement_list *tc, tree_statement_list *cc,
+                          tree_identifier *id,
                           octave_comment_list *cl = 0,
                           octave_comment_list *cm = 0,
                           octave_comment_list *ct = 0,
                           int l = -1, int c = -1)
-    : tree_command (l, c), try_code (tc), catch_code (cc),
+    : tree_command (l, c), try_code (tc), catch_code (cc), expr_id (id),
       lead_comm (cl), mid_comm (cm), trail_comm (ct) { }
 
   ~tree_try_catch_command (void);
 
+  tree_identifier *identifier (void) { return expr_id; }
+
   tree_statement_list *body (void) { return try_code; }
 
   tree_statement_list *cleanup (void) { return catch_code; }
@@ -75,6 +79,9 @@
   // The code to execute if an error occurs in the first block.
   tree_statement_list *catch_code;
 
+  // Expression to modify.
+  tree_identifier *expr_id;
+  
   // Comment preceding TRY token.
   octave_comment_list *lead_comm;
 
diff -r 6a18d8b0f982 -r b4f263aebe7b libinterp/parse-tree/pt-pr-code.cc
--- a/libinterp/parse-tree/pt-pr-code.cc	Wed Apr 10 06:59:55 2013 +0200
+++ b/libinterp/parse-tree/pt-pr-code.cc	Wed Apr 10 18:45:29 2013 +0200
@@ -993,6 +993,7 @@
   newline ();
 
   tree_statement_list *try_code = cmd.body ();
+  tree_identifier *expr_id = cmd.identifier ();
 
   if (try_code)
     {
@@ -1008,6 +1009,12 @@
   indent ();
 
   os << "catch";
+  
+  if (expr_id)
+    {
+      os << " ";
+      expr_id->accept (*this);
+    }
 
   newline ();
 
