changeset 30144:3c2dee80b542

attempt more uniform handling of list concatenation in the parser * parse.h, oct-parse.yy (append_if_clause, append_switch_case, append_function_body, append_classdef_property, append_classdef_event, append_classdef_enum, append_classdef_superclass, append_classdef_attribute, append_classdef_properties_block, append_classdef_methods_block, append_classdef_events_block, append_classdef_enum_block, append_classdef_method, append_decl_init_list, append_argument_list): New base_parser methods. Use them to simplify actions in parser grammar. * pt-idx.h, pt-idx.cc (tree_index_expression::append): Return pointer to this. Change uses where needed.
author John W. Eaton <jwe@octave.org>
date Thu, 09 Sep 2021 13:27:49 -0400
parents 45c45295a84a
children cd6f7957c889
files libinterp/parse-tree/oct-parse.yy libinterp/parse-tree/parse.h libinterp/parse-tree/pt-idx.cc libinterp/parse-tree/pt-idx.h
diffstat 4 files changed, 238 insertions(+), 98 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/oct-parse.yy	Thu Sep 09 08:18:52 2021 -0700
+++ b/libinterp/parse-tree/oct-parse.yy	Thu Sep 09 13:27:49 2021 -0400
@@ -530,10 +530,7 @@
 word_list       : string
                   { $$ = parser.make_argument_list ($1); }
                 | word_list string
-                  {
-                    $1->append ($2);
-                    $$ = $1;
-                  }
+                  { $$ = parser.append_argument_list ($1, $2); }
                 ;
 
 // ===========
@@ -713,22 +710,19 @@
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_argument_list ($1, $3);
                   }
                 | arg_list ',' magic_tilde
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_argument_list ($1, $3);
                   }
                 | arg_list ',' expression
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_argument_list ($1, $3);
                   }
                 ;
 
@@ -1081,10 +1075,7 @@
 decl_init_list   : decl_elt
                   { $$ = parser.make_decl_init_list ($1); }
                 | decl_init_list decl_elt
-                  {
-                    $1->append ($2);
-                    $$ = $1;
-                  }
+                  { $$ = parser.append_decl_init_list ($1, $2); }
                 ;
 
 decl_elt        : identifier
@@ -1120,10 +1111,7 @@
 if_cmd_list     : if_cmd_list1
                   { $$ = $1; }
                 | if_cmd_list1 else_clause
-                  {
-                    $1->append ($2);
-                    $$ = $1;
-                  }
+                  { $$ = parser.append_if_clause ($1, $2); }
                 ;
 
 if_cmd_list1    : expression stmt_begin opt_sep opt_list
@@ -1135,10 +1123,7 @@
                     $$ = parser.start_if_command ($1, $4);
                   }
                 | if_cmd_list1 elseif_clause
-                  {
-                    $1->append ($2);
-                    $$ = $1;
-                  }
+                  { $$ = parser.append_if_clause ($1, $2); }
                 ;
 
 elseif_clause   : ELSEIF stash_comment opt_sep expression stmt_begin opt_sep opt_list
@@ -1183,19 +1168,13 @@
                 | case_list1
                   { $$ = $1; }
                 | case_list1 default_case
-                  {
-                    $1->append ($2);
-                    $$ = $1;
-                  }
+                  { $$ = parser.append_switch_case ($1, $2); }
                 ;
 
 case_list1      : switch_case
                   { $$ = parser.make_switch_case_list ($1); }
                 | case_list1 switch_case
-                  {
-                    $1->append ($2);
-                    $$ = $1;
-                  }
+                  { $$ = parser.append_switch_case ($1, $2); }
                 ;
 
 switch_case     : CASE stash_comment opt_sep expression stmt_begin opt_sep opt_list
@@ -1465,8 +1444,7 @@
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_parameter_list ($1, $3);
                   }
                 ;
 
@@ -1737,16 +1715,7 @@
                   {
                     OCTAVE_YYUSE ($2);
 
-                    if ($4)
-                      {
-                        for (const auto& elt : *($4))
-                          $1->append (elt);
-                      }
-
-                    $4->clear ();
-                    delete ($4);
-
-                    $$ = $1;
+                    $$ = parser.append_function_body ($1, $4);
                   }
                 ;
 
@@ -1953,8 +1922,7 @@
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_classdef_attribute ($1, $3);
                   }
                 ;
 
@@ -2003,8 +1971,7 @@
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_classdef_superclass ($1, $3);
                   }
                 ;
 
@@ -2038,29 +2005,25 @@
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_classdef_properties_block ($1, $3);
                   }
                 | class_body1 opt_sep methods_block
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_classdef_methods_block ($1, $3);
                   }
                 | class_body1 opt_sep events_block
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_classdef_events_block ($1, $3);
                   }
                 | class_body1 opt_sep enum_block
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_classdef_enum_block ($1, $3);
                   }
                 ;
 
@@ -2133,8 +2096,7 @@
                           }
                       }
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_classdef_property ($1, $3);
                   }
                 ;
 
@@ -2222,13 +2184,7 @@
                   {
                     OCTAVE_YYUSE ($2);
 
-                    octave_value fcn;
-                    if ($3)
-                      fcn = $3->function ();
-                    delete $3;
-
-                    $1->append (fcn);
-                    $$ = $1;
+                    $$ = parser.append_classdef_method ($1, $3);
                   }
                 ;
 
@@ -2276,8 +2232,7 @@
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_classdef_event ($1, $3);
                   }
                 ;
 
@@ -2329,8 +2284,7 @@
                   {
                     OCTAVE_YYUSE ($2);
 
-                    $1->append ($3);
-                    $$ = $1;
+                    $$ = parser.append_classdef_enum ($1, $3);
                   }
                 ;
 
@@ -2617,6 +2571,14 @@
     std::list<parse_exception> m_error_list;
   };
 
+  template <typename LIST_T, typename ELT_T>
+  static LIST_T *
+  list_append (LIST_T *list, ELT_T elt)
+  {
+    list->append (elt);
+    return list;
+  }
+
   std::size_t
   base_parser::parent_scope_info::size (void) const
   {
@@ -3692,6 +3654,13 @@
     return new tree_if_clause (list, lc);
   }
 
+  tree_if_command_list *
+  base_parser::append_if_clause (tree_if_command_list *list,
+                                 tree_if_clause *clause)
+  {
+    return list_append (list, clause);
+  }
+
   // Finish a switch command.
 
   tree_switch_command *
@@ -3766,6 +3735,13 @@
     return new tree_switch_case (list, lc, l, c);
   }
 
+  tree_switch_case_list *
+  base_parser::append_switch_case (tree_switch_case_list *list,
+                                   tree_switch_case *elt)
+  {
+    return list_append (list, elt);
+  }
+
   // Build an assignment to a variable.
 
   tree_expression *
@@ -4229,6 +4205,22 @@
     return retval;
   }
 
+  tree_statement_list *
+  base_parser::append_function_body (tree_statement_list *body,
+                                     tree_statement_list *list)
+  {
+    if (list)
+      {
+        for (const auto& elt : *list)
+          list_append (body, elt);
+
+        list->clear ();
+        delete (list);
+      }
+
+    return body;
+  }
+
   tree_arguments_block *
   base_parser::make_arguments_block (token *arguments_tok,
                                      tree_args_block_attribute_list *attr_list,
@@ -4291,9 +4283,7 @@
   base_parser::append_args_validation_list (tree_args_block_validation_list *list,
                                             tree_arg_validation *arg_validation)
   {
-    list->append (arg_validation);
-
-    return list;
+    return list_append (list, arg_validation);
   }
 
   tree_arg_size_spec *
@@ -4636,6 +4626,27 @@
     return new tree_classdef_enum (id, expr, lc);
   }
 
+  tree_classdef_property_list *
+  base_parser::append_classdef_property (tree_classdef_property_list *list,
+                                         tree_classdef_property *elt)
+  {
+    return list_append (list, elt);
+  }
+
+  tree_classdef_events_list *
+  base_parser::append_classdef_event (tree_classdef_events_list *list,
+                                      tree_classdef_event *elt)
+  {
+    return list_append (list, elt);
+  }
+
+  tree_classdef_enum_list *
+  base_parser::append_classdef_enum (tree_classdef_enum_list *list,
+                                     tree_classdef_enum *elt)
+  {
+    return list_append (list, elt);
+  }
+
   tree_classdef_superclass_list *
   base_parser::make_classdef_superclass_list (tree_classdef_superclass *sc)
   {
@@ -4648,6 +4659,13 @@
     return new tree_classdef_superclass (fqident->text ());
   }
 
+  tree_classdef_superclass_list *
+  base_parser::append_classdef_superclass (tree_classdef_superclass_list *list,
+                                           tree_classdef_superclass *elt)
+  {
+    return list_append (list, elt);
+  }
+
   tree_classdef_attribute_list *
   base_parser::make_classdef_attribute_list (tree_classdef_attribute *attr)
   {
@@ -4669,6 +4687,13 @@
     return new tree_classdef_attribute (id, false);
   }
 
+  tree_classdef_attribute_list *
+  base_parser::append_classdef_attribute (tree_classdef_attribute_list *list,
+                                          tree_classdef_attribute *elt)
+  {
+    return list_append (list, elt);
+  }
+
   tree_classdef_body *
   base_parser::make_classdef_body (tree_classdef_properties_block *pb)
   {
@@ -4693,6 +4718,34 @@
     return new tree_classdef_body (enb);
   }
 
+  tree_classdef_body *
+  base_parser::append_classdef_properties_block (tree_classdef_body *body,
+                                                 tree_classdef_properties_block *block)
+  {
+    return list_append (body, block);
+  }
+
+  tree_classdef_body *
+  base_parser::append_classdef_methods_block (tree_classdef_body *body,
+                                              tree_classdef_methods_block *block)
+  {
+    return list_append (body, block);
+  }
+
+  tree_classdef_body *
+  base_parser::append_classdef_events_block (tree_classdef_body *body,
+                                             tree_classdef_events_block *block)
+  {
+    return list_append (body, block);
+  }
+
+  tree_classdef_body *
+  base_parser::append_classdef_enum_block (tree_classdef_body *body,
+                                           tree_classdef_enum_block *block)
+  {
+    return list_append (body, block);
+  }
+
   octave_user_function*
   base_parser::start_classdef_external_method (tree_identifier *id,
                                                tree_parameter_list *pl)
@@ -4774,6 +4827,22 @@
     return new tree_classdef_methods_list (fcn);
   }
 
+  tree_classdef_methods_list *
+  base_parser::append_classdef_method (tree_classdef_methods_list *list,
+                                       tree_function_def *fcn_def)
+  {
+    octave_value fcn;
+
+    if (fcn_def)
+      {
+        fcn = fcn_def->function ();
+
+        delete fcn_def;
+      }
+
+    return list_append (list, fcn);
+  }
+
   bool
   base_parser::finish_classdef_file (tree_classdef *cls,
                                      tree_statement_list *local_fcns)
@@ -4868,9 +4937,7 @@
             tree_index_expression *tmp
               = dynamic_cast<tree_index_expression *> (expr);
 
-            tmp->append (args, type);
-
-            retval = tmp;
+            retval = tmp->append (args, type);
           }
         else
           retval = new tree_index_expression (expr, args, l, c, type);
@@ -4898,9 +4965,7 @@
         tree_index_expression *tmp
           = dynamic_cast<tree_index_expression *> (expr);
 
-        tmp->append (elt);
-
-        retval = tmp;
+        retval = tmp->append (elt);
       }
     else
       retval = new tree_index_expression (expr, elt, l, c);
@@ -4929,9 +4994,7 @@
         tree_index_expression *tmp
           = dynamic_cast<tree_index_expression *> (expr);
 
-        tmp->append (elt);
-
-        retval = tmp;
+        retval = list_append (tmp, elt);
       }
     else
       retval = new tree_index_expression (expr, elt, l, c);
@@ -4994,6 +5057,13 @@
     return new tree_decl_init_list (elt);
   }
 
+  tree_decl_init_list *
+  base_parser::append_decl_init_list (tree_decl_init_list *list,
+                                      tree_decl_elt *elt)
+  {
+    return list_append (list, elt);
+  }
+
   tree_decl_elt *
   base_parser::make_decl_elt (tree_identifier *id, token */*eq_op*/,
                               tree_expression *expr)
@@ -5240,10 +5310,7 @@
     if (! matrix)
       return make_matrix (row);
 
-    if (row)
-      matrix->append (row);
-
-    return matrix;
+    return row ? list_append (matrix, row) : matrix;
   }
 
   // Finish building a cell list.
@@ -5270,10 +5337,7 @@
     if (! cell)
       return make_cell (row);
 
-    if (row)
-      cell->append (row);
-
-    return cell;
+    return row ? list_append (cell, row) : cell;
   }
 
   tree_identifier *
@@ -5376,9 +5440,7 @@
   {
     set_stmt_print_flag (list, sep, warn_missing_semi);
 
-    list->append (stmt);
-
-    return list;
+    return list_append (list, stmt);
   }
 
   tree_argument_list *
@@ -5387,6 +5449,13 @@
     return new tree_argument_list (expr);
   }
 
+  tree_argument_list *
+  base_parser::append_argument_list (tree_argument_list *list,
+                                     tree_expression *expr)
+  {
+    return list_append (list, expr);
+  }
+
   tree_parameter_list *
   base_parser::make_parameter_list (tree_parameter_list::in_or_out io)
   {
@@ -5409,10 +5478,16 @@
 
   tree_parameter_list *
   base_parser::append_parameter_list (tree_parameter_list *list,
+                                      tree_decl_elt *t)
+  {
+    return list_append (list, t);
+  }
+
+  tree_parameter_list *
+  base_parser::append_parameter_list (tree_parameter_list *list,
                                       tree_identifier *id)
   {
-    list->append (new tree_decl_elt (id));
-    return list;
+    return list_append (list, new tree_decl_elt (id));
   }
 
   void
--- a/libinterp/parse-tree/parse.h	Thu Sep 09 08:18:52 2021 -0700
+++ b/libinterp/parse-tree/parse.h	Thu Sep 09 13:27:49 2021 -0400
@@ -350,6 +350,9 @@
     make_else_clause (token *else_tok, comment_list *lc,
                       tree_statement_list *list);
 
+    OCTINTERP_API tree_if_command_list *
+    append_if_clause (tree_if_command_list *list, tree_if_clause *clause);
+
     // Finish a switch command.
     OCTINTERP_API tree_switch_command *
     finish_switch_command (token *switch_tok, tree_expression *expr,
@@ -368,6 +371,9 @@
     make_default_switch_case (token *default_tok, comment_list *lc,
                               tree_statement_list *list);
 
+    OCTINTERP_API tree_switch_case_list *
+    append_switch_case (tree_switch_case_list *list, tree_switch_case *elt);
+
     // Build an assignment to a variable.
     OCTINTERP_API tree_expression *
     make_assign_op (int op, tree_argument_list *lhs, token *eq_tok,
@@ -408,6 +414,9 @@
                      octave_user_function *fcn, comment_list *lc,
                      int l, int c);
 
+    OCTINTERP_API tree_statement_list *
+    append_function_body (tree_statement_list *body, tree_statement_list *list);
+
     // Make an arguments validation block.
     OCTINTERP_API tree_arguments_block *
     make_arguments_block (token *arguments_tok,
@@ -466,6 +475,10 @@
     make_classdef_property (comment_list *lc, tree_identifier *id,
                             tree_arg_validation *av);
 
+    OCTINTERP_API tree_classdef_property_list *
+    append_classdef_property (tree_classdef_property_list *list,
+                              tree_classdef_property *elt);
+
     OCTINTERP_API tree_classdef_methods_block *
     make_classdef_methods_block (token *tok_val,
                                  tree_classdef_attribute_list *a,
@@ -486,6 +499,10 @@
     OCTINTERP_API tree_classdef_event *
     make_classdef_event (comment_list *lc, tree_identifier *id);
 
+    OCTINTERP_API tree_classdef_events_list *
+    append_classdef_event (tree_classdef_events_list *list,
+                           tree_classdef_event *elt);
+
     OCTINTERP_API tree_classdef_enum_block *
     make_classdef_enum_block (token *tok_val,
                               tree_classdef_attribute_list *a,
@@ -500,12 +517,20 @@
     make_classdef_enum (tree_identifier *id, tree_expression *expr,
                         comment_list *lc);
 
+    OCTINTERP_API tree_classdef_enum_list *
+    append_classdef_enum (tree_classdef_enum_list *list,
+                          tree_classdef_enum *elt);
+
     OCTINTERP_API tree_classdef_superclass_list *
     make_classdef_superclass_list (tree_classdef_superclass *sc);
 
     OCTINTERP_API tree_classdef_superclass *
     make_classdef_superclass (token *fqident);
 
+    OCTINTERP_API tree_classdef_superclass_list *
+    append_classdef_superclass (tree_classdef_superclass_list *list,
+                                tree_classdef_superclass *elt);
+
     OCTINTERP_API tree_classdef_attribute_list *
     make_classdef_attribute_list (tree_classdef_attribute *attr);
 
@@ -516,6 +541,10 @@
     OCTINTERP_API tree_classdef_attribute *
     make_not_classdef_attribute (tree_identifier *id);
 
+    OCTINTERP_API tree_classdef_attribute_list *
+    append_classdef_attribute (tree_classdef_attribute_list *list,
+                               tree_classdef_attribute *elt);
+
     OCTINTERP_API tree_classdef_body *
     make_classdef_body (tree_classdef_properties_block *pb);
 
@@ -528,6 +557,22 @@
     OCTINTERP_API tree_classdef_body *
     make_classdef_body  (tree_classdef_enum_block *enb);
 
+    OCTINTERP_API tree_classdef_body *
+    append_classdef_properties_block (tree_classdef_body *body,
+                                      tree_classdef_properties_block *block);
+
+    OCTINTERP_API tree_classdef_body *
+    append_classdef_methods_block (tree_classdef_body *body,
+                                   tree_classdef_methods_block *block);
+
+    OCTINTERP_API tree_classdef_body *
+    append_classdef_events_block (tree_classdef_body *body,
+                                  tree_classdef_events_block *block);
+
+    OCTINTERP_API tree_classdef_body *
+    append_classdef_enum_block (tree_classdef_body *body,
+                                tree_classdef_enum_block *block);
+
     OCTINTERP_API octave_user_function *
     start_classdef_external_method (tree_identifier *id,
                                     tree_parameter_list *pl);
@@ -540,6 +585,10 @@
     OCTINTERP_API tree_classdef_methods_list *
     make_classdef_methods_list (tree_function_def *fcn_def);
 
+    OCTINTERP_API tree_classdef_methods_list *
+    append_classdef_method (tree_classdef_methods_list *list,
+                            tree_function_def *fcn_def);
+
     OCTINTERP_API bool
     finish_classdef_file (tree_classdef *cls,
                           tree_statement_list *local_fcns);
@@ -568,6 +617,9 @@
     make_decl_elt (tree_identifier *id, token *eq_op = nullptr,
                    tree_expression *expr = nullptr);
 
+    OCTINTERP_API tree_decl_init_list *
+    append_decl_init_list (tree_decl_init_list *list, tree_decl_elt *elt);
+
     // Validate an function parameter list.
     OCTINTERP_API bool
     validate_param_list (tree_parameter_list *lst,
@@ -622,6 +674,9 @@
     OCTINTERP_API tree_argument_list *
     make_argument_list (tree_expression *expr);
 
+    OCTINTERP_API tree_argument_list *
+    append_argument_list (tree_argument_list *list, tree_expression *expr);
+
     OCTINTERP_API tree_parameter_list *
     make_parameter_list (tree_parameter_list::in_or_out io);
 
@@ -633,6 +688,9 @@
                          tree_identifier *id);
 
     OCTINTERP_API tree_parameter_list *
+    append_parameter_list (tree_parameter_list *list, tree_decl_elt *t);
+
+    OCTINTERP_API tree_parameter_list *
     append_parameter_list (tree_parameter_list *list, tree_identifier *id);
 
     // Don't allow parsing command syntax.  If the parser/lexer is
--- a/libinterp/parse-tree/pt-idx.cc	Thu Sep 09 08:18:52 2021 -0700
+++ b/libinterp/parse-tree/pt-idx.cc	Thu Sep 09 13:27:49 2021 -0400
@@ -77,7 +77,7 @@
     append (df);
   }
 
-  void
+  tree_index_expression *
   tree_index_expression::append (tree_argument_list *lst, char t)
   {
     m_args.push_back (lst);
@@ -87,24 +87,30 @@
 
     if (lst && lst->has_magic_tilde ())
       error ("invalid use of empty argument (~) in index expression");
+
+    return this;
   }
 
-  void
+  tree_index_expression *
   tree_index_expression::append (const std::string& n)
   {
     m_args.push_back (static_cast<tree_argument_list *> (nullptr));
     m_type += '.';
     m_arg_nm.push_back (n);
     m_dyn_field.push_back (static_cast<tree_expression *> (nullptr));
+
+    return this;
   }
 
-  void
+  tree_index_expression *
   tree_index_expression::append (tree_expression *df)
   {
     m_args.push_back (static_cast<tree_argument_list *> (nullptr));
     m_type += '.';
     m_arg_nm.push_back ("");
     m_dyn_field.push_back (df);
+
+    return this;
   }
 
   tree_index_expression::~tree_index_expression (void)
--- a/libinterp/parse-tree/pt-idx.h	Thu Sep 09 08:18:52 2021 -0700
+++ b/libinterp/parse-tree/pt-idx.h	Thu Sep 09 13:27:49 2021 -0400
@@ -70,11 +70,12 @@
 
     ~tree_index_expression (void);
 
-    void append (tree_argument_list *lst = nullptr, char t = '(');
+    tree_index_expression *
+    append (tree_argument_list *lst = nullptr, char t = '(');
 
-    void append (const std::string& n);
+    tree_index_expression * append (const std::string& n);
 
-    void append (tree_expression *df);
+    tree_index_expression * append (tree_expression *df);
 
     bool is_index_expression (void) const { return true; }