changeset 33347:ea0da8f24871

replace remaining uses of panic_impossible with error in oct-parse.yy * oct-parse.yy (unexpected_token): New function. Use it to replace panic_impossible in functions that use switch statements to handle a subset of operator tokens. (end_token_as_string): Eliminate default case in switch over enum values. Handle previously missing token::arguments_end case in switch. (base_parser::parse_fcn_file): Call error instead of panic_impossible.
author John W. Eaton <jwe@octave.org>
date Sun, 07 Apr 2024 11:05:18 -0400
parents 56d234504c01
children aa7cd2622228
files libinterp/parse-tree/oct-parse.yy
diffstat 1 files changed, 43 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/oct-parse.yy	Sat Apr 06 17:11:35 2024 -0400
+++ b/libinterp/parse-tree/oct-parse.yy	Sun Apr 07 11:05:18 2024 -0400
@@ -2480,6 +2480,12 @@
   m_parser_state = yypstate_new ();
 }
 
+OCTAVE_NORETURN static void
+unexpected_token (int tok_id, const char *where)
+{
+  error ("unexpected token (= %d) in %s - please report this bug", tok_id, where);
+}
+
 // Error messages for mismatched end tokens.
 
 static std::string
@@ -2493,6 +2499,10 @@
       retval = "end";
       break;
 
+    case token::arguments_end:
+      retval = "endarguments";
+      break;
+
     case token::classdef_end:
       retval = "endclassdef";
       break;
@@ -2549,9 +2559,9 @@
       retval = "endwhile";
       break;
 
-    default:
-      panic_impossible ();
-      break;
+      // We should have handled all possible enum values above.  Rely on
+      // compiler diagnostics to warn if we haven't.  For example, GCC's
+      // -Wswitch option, enabled by -Wall, will provide a warning.
     }
 
   return retval;
@@ -2628,11 +2638,11 @@
 tree_constant *
 base_parser::make_constant (token *tok)
 {
-  int op = tok->token_id ();
+  int tok_id = tok->token_id ();
 
   tree_constant *retval = nullptr;
 
-  switch (op)
+  switch (tok_id)
     {
     case ':':
       retval = new tree_constant (octave_value (octave_value::magic_colon_t), *tok);
@@ -2647,18 +2657,18 @@
       {
         std::string txt = tok->text ();
 
-        char delim = op == DQ_STRING ? '"' : '\'';
+        char delim = tok_id == DQ_STRING ? '"' : '\'';
         octave_value tmp (txt, delim);
 
         if (txt.empty ())
           {
-            if (op == DQ_STRING)
+            if (tok_id == DQ_STRING)
               tmp = octave_null_str::instance;
             else
               tmp = octave_null_sq_str::instance;
           }
 
-        if (op == DQ_STRING)
+        if (tok_id == DQ_STRING)
           txt = undo_string_escapes (txt);
 
         // FIXME: maybe the addition of delims should be handled by
@@ -2669,7 +2679,7 @@
       break;
 
     default:
-      panic_impossible ();
+      unexpected_token (tok_id, "base_parser::make_constant");
       break;
     }
 
@@ -2837,7 +2847,9 @@
 {
   octave_value::binary_op t = octave_value::unknown_binary_op;
 
-  switch (op_tok->token_id ())
+  int tok_id = op_tok->token_id ();
+
+  switch (tok_id)
     {
     case POW:
       t = octave_value::op_pow;
@@ -2912,7 +2924,7 @@
       break;
 
     default:
-      panic_impossible ();
+      unexpected_token (tok_id, "base_parser::make_binary_op");
       break;
     }
 
@@ -2957,7 +2969,9 @@
 {
   tree_boolean_expression::type t;
 
-  switch (op_tok->token_id ())
+  int tok_id = op_tok->token_id ();
+
+  switch (tok_id)
     {
     case EXPR_AND_AND:
       t = tree_boolean_expression::bool_and;
@@ -2968,7 +2982,7 @@
       break;
 
     default:
-      panic_impossible ();
+      unexpected_token (tok_id, "base_parser::make_boolean_op");
       break;
     }
 
@@ -2982,7 +2996,9 @@
 {
   octave_value::unary_op t = octave_value::unknown_unary_op;
 
-  switch (op_tok->token_id ())
+  int tok_id = op_tok->token_id ();
+
+  switch (tok_id)
     {
     case '~':
     case '!':
@@ -3006,7 +3022,7 @@
       break;
 
     default:
-      panic_impossible ();
+      unexpected_token (tok_id, "base_parser::make_prefix_op");
       break;
     }
 
@@ -3020,7 +3036,9 @@
 {
   octave_value::unary_op t = octave_value::unknown_unary_op;
 
-  switch (op_tok->token_id ())
+  int tok_id = op_tok->token_id ();
+
+  switch (tok_id)
     {
     case HERMITIAN:
       t = octave_value::op_hermitian;
@@ -3039,7 +3057,7 @@
       break;
 
     default:
-      panic_impossible ();
+      unexpected_token (tok_id, "base_parser::make_postfix_op");
       break;
     }
 
@@ -3379,7 +3397,9 @@
 {
   octave_value::assign_op t = octave_value::unknown_assign_op;
 
-  switch (eq_tok->token_id ())
+  int tok_id = eq_tok->token_id ();
+
+  switch (tok_id)
     {
     case '=':
       t = octave_value::op_asn_eq;
@@ -3434,7 +3454,7 @@
       break;
 
     default:
-      panic_impossible ();
+      unexpected_token (tok_id, "base_parser::make_assign_op");
       break;
     }
 
@@ -4520,6 +4540,8 @@
   if (lst)
     m_lexer.mark_as_variables (lst->variable_names ());
 
+  int tok_id = tok->token_id ();
+
   switch (tok->token_id ())
     {
     case GLOBAL:
@@ -4548,7 +4570,7 @@
       break;
 
     default:
-      panic_impossible ();
+      unexpected_token (tok_id, "base_parser::make_decl_command");
       break;
     }
 
@@ -5281,7 +5303,7 @@
       // table?).  Return pointer to constructor?
 
       if (ov_fcn.is_defined ())
-        panic_impossible ();
+        error ("unexpected: defining classdef object but primary_fcn is already defined - please report this bug");
 
       bool is_at_folder = ! dispatch_type.empty ();