changeset 27509:fefc780b4e2e

move parse_fcn_file from interpreter member function to parser friend In 24b7e6326e26 I moved parse_fcn_file to the interpreter class, but on reflection it seems that this function is too low-level to be in the public interface for the interpreter. There are about 10 parameters and it is fairly confusing to use them properly. It also needs access to many internal parser and lexer settings, so making it a friend of the parser makes more sense to me. * parse.h, oct-parse.yy (parse_fcn_file): Now a friend of the base_parser class instead of an interpreter class member function. Change all uses.
author John W. Eaton <jwe@octave.org>
date Fri, 11 Oct 2019 09:36:08 -0400
parents 290d424b9d10
children 5438a82a18fb
files libinterp/corefcn/interpreter.cc libinterp/corefcn/interpreter.h libinterp/parse-tree/oct-parse.yy libinterp/parse-tree/parse.h libinterp/parse-tree/pt-eval.cc
diffstat 5 files changed, 116 insertions(+), 112 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/interpreter.cc	Thu Oct 10 16:56:39 2019 -0400
+++ b/libinterp/corefcn/interpreter.cc	Fri Oct 11 09:36:08 2019 -0400
@@ -1413,96 +1413,6 @@
     m_evaluator.source_file (file_name, context, verbose, require_file);
   }
 
-  octave_value
-  interpreter::parse_fcn_file (const std::string& full_file,
-                               const std::string& file,
-                               const std::string& dir_name,
-                               const std::string& dispatch_type,
-                               const std::string& package_name,
-                               bool require_file,
-                               bool force_script, bool autoload,
-                               bool relative_lookup)
-  {
-    octave_value retval;
-
-    FILE *ffile = nullptr;
-
-    if (! full_file.empty ())
-      ffile = sys::fopen (full_file, "rb");
-
-    if (! ffile)
-      {
-        if (require_file)
-          error ("no such file, '%s'", full_file.c_str ());
-
-        return octave_value ();
-      }
-
-    unwind_action act ([ffile] (void)
-                       {
-                         fclose (ffile);
-                       });
-
-    parser parser (ffile, *this);
-
-    parser.m_curr_class_name = dispatch_type;
-    parser.m_curr_package_name = package_name;
-    parser.m_autoloading = autoload;
-    parser.m_fcn_file_from_relative_lookup = relative_lookup;
-
-    parser.m_lexer.m_force_script = force_script;
-    parser.m_lexer.prep_for_file ();
-    parser.m_lexer.m_parsing_class_method = ! dispatch_type.empty ();
-
-    parser.m_lexer.m_fcn_file_name = file;
-    parser.m_lexer.m_fcn_file_full_name = full_file;
-    parser.m_lexer.m_dir_name = dir_name;
-    parser.m_lexer.m_package_name = package_name;
-
-    int err = parser.run ();
-
-    if (err)
-      error ("parse error while reading file %s", full_file.c_str ());
-
-    octave_value ov_fcn = parser.m_primary_fcn;
-
-    if (parser.m_lexer.m_reading_classdef_file
-        && parser.classdef_object ())
-      {
-        // Convert parse tree for classdef object to
-        // meta.class info (and stash it in the symbol
-        // table?).  Return pointer to constructor?
-
-        if (ov_fcn.is_defined ())
-          panic_impossible ();
-
-        bool is_at_folder = ! dispatch_type.empty ();
-
-        std::shared_ptr<tree_classdef> cdef_obj
-          = parser.classdef_object();
-
-        return cdef_obj->make_meta_class (*this, is_at_folder);
-      }
-    else if (ov_fcn.is_defined ())
-      {
-        octave_function *fcn = ov_fcn.function_value ();
-
-        fcn->maybe_relocate_end ();
-
-        if (parser.m_parsing_subfunctions)
-          {
-            if (! parser.m_endfunction_found)
-              parser.m_subfunction_names.reverse ();
-
-            fcn->stash_subfunction_names (parser.m_subfunction_names);
-          }
-
-        return ov_fcn;
-      }
-
-    return octave_value ();
-  }
-
   bool interpreter::at_top_level (void) const
   {
     return m_evaluator.at_top_level ();
--- a/libinterp/corefcn/interpreter.h	Thu Oct 10 16:56:39 2019 -0400
+++ b/libinterp/corefcn/interpreter.h	Fri Oct 11 09:36:08 2019 -0400
@@ -350,14 +350,6 @@
                       const std::string& context = "",
                       bool verbose = false, bool require_file = true);
 
-    octave_value parse_fcn_file (const std::string& full_file,
-                                 const std::string& file,
-                                 const std::string& dir_name,
-                                 const std::string& dispatch_type,
-                                 const std::string& package_name,
-                                 bool require_file, bool force_script,
-                                 bool autoload, bool relative_lookup);
-
     bool at_top_level (void) const;
 
     bool isglobal (const std::string& name) const;
--- a/libinterp/parse-tree/oct-parse.yy	Thu Oct 10 16:56:39 2019 -0400
+++ b/libinterp/parse-tree/oct-parse.yy	Fri Oct 11 09:36:08 2019 -0400
@@ -4506,6 +4506,93 @@
     return status;
   }
 
+  octave_value
+  parse_fcn_file (interpreter& interp, const std::string& full_file,
+                  const std::string& file, const std::string& dir_name,
+                  const std::string& dispatch_type,
+                  const std::string& package_name, bool require_file,
+                  bool force_script, bool autoload, bool relative_lookup)
+  {
+    octave_value retval;
+
+    FILE *ffile = nullptr;
+
+    if (! full_file.empty ())
+      ffile = sys::fopen (full_file, "rb");
+
+    if (! ffile)
+      {
+        if (require_file)
+          error ("no such file, '%s'", full_file.c_str ());
+
+        return octave_value ();
+      }
+
+    unwind_action act ([ffile] (void)
+                       {
+                         fclose (ffile);
+                       });
+
+    parser parser (ffile, interp);
+
+    parser.m_curr_class_name = dispatch_type;
+    parser.m_curr_package_name = package_name;
+    parser.m_autoloading = autoload;
+    parser.m_fcn_file_from_relative_lookup = relative_lookup;
+
+    parser.m_lexer.m_force_script = force_script;
+    parser.m_lexer.prep_for_file ();
+    parser.m_lexer.m_parsing_class_method = ! dispatch_type.empty ();
+
+    parser.m_lexer.m_fcn_file_name = file;
+    parser.m_lexer.m_fcn_file_full_name = full_file;
+    parser.m_lexer.m_dir_name = dir_name;
+    parser.m_lexer.m_package_name = package_name;
+
+    int err = parser.run ();
+
+    if (err)
+      error ("parse error while reading file %s", full_file.c_str ());
+
+    octave_value ov_fcn = parser.m_primary_fcn;
+
+    if (parser.m_lexer.m_reading_classdef_file
+        && parser.classdef_object ())
+      {
+        // Convert parse tree for classdef object to
+        // meta.class info (and stash it in the symbol
+        // table?).  Return pointer to constructor?
+
+        if (ov_fcn.is_defined ())
+          panic_impossible ();
+
+        bool is_at_folder = ! dispatch_type.empty ();
+
+        std::shared_ptr<tree_classdef> cdef_obj
+          = parser.classdef_object();
+
+        return cdef_obj->make_meta_class (interp, is_at_folder);
+      }
+    else if (ov_fcn.is_defined ())
+      {
+        octave_function *fcn = ov_fcn.function_value ();
+
+        fcn->maybe_relocate_end ();
+
+        if (parser.m_parsing_subfunctions)
+          {
+            if (! parser.m_endfunction_found)
+              parser.m_subfunction_names.reverse ();
+
+            fcn->stash_subfunction_names (parser.m_subfunction_names);
+          }
+
+        return ov_fcn;
+      }
+
+    return octave_value ();
+  }
+
   std::string
   get_help_from_file (const std::string& nm, bool& symbol_found,
                       std::string& full_file)
@@ -4537,8 +4624,8 @@
         symbol_found = true;
 
         octave_value ov_fcn
-          = interp.parse_fcn_file (full_file, file, "", "", "", true,
-                                   false, false, false);
+          = parse_fcn_file (interp, full_file, file, "", "", "", true,
+                            false, false, false);
 
         if (ov_fcn.is_defined ())
           {
@@ -4624,9 +4711,9 @@
         std::string doc_string;
 
         octave_value ov_fcn
-          = interp.parse_fcn_file (file.substr (0, len - 2), nm, dir_name,
-                                   dispatch_type, package_name, false,
-                                   autoload, autoload, relative_lookup);
+          = parse_fcn_file (interp, file.substr (0, len - 2), nm, dir_name,
+                            dispatch_type, package_name, false,
+                            autoload, autoload, relative_lookup);
 
         if (ov_fcn.is_defined ())
           {
@@ -4649,9 +4736,9 @@
       }
     else if (len > 2)
       {
-        retval = interp.parse_fcn_file (file, nm, dir_name, dispatch_type,
-                                        package_name, true, autoload,
-                                        autoload, relative_lookup);
+        retval = parse_fcn_file (interp, file, nm, dir_name,
+                                 dispatch_type, package_name, true,
+                                 autoload, autoload, relative_lookup);
       }
 
     return retval;
@@ -5390,8 +5477,8 @@
     octave_stdout << "parsing " << full_file << std::endl;
 
   octave_value ov_fcn
-    = interp.parse_fcn_file (full_file, file, dir_name, "", "", true, false,
-                             false, false);
+    = parse_fcn_file (interp, full_file, file, dir_name, "", "", true,
+                      false, false, false);
 
   return retval;
 }
--- a/libinterp/parse-tree/parse.h	Thu Oct 10 16:56:39 2019 -0400
+++ b/libinterp/parse-tree/parse.h	Fri Oct 11 09:36:08 2019 -0400
@@ -429,6 +429,13 @@
     // Generic error messages.
     void bison_error (const std::string& s, int l = -1, int c = -1);
 
+    friend octave_value
+    parse_fcn_file (interpreter& interp, const std::string& full_file,
+                    const std::string& file, const std::string& dir_name,
+                    const std::string& dispatch_type,
+                    const std::string& package_name, bool require_file,
+                    bool force_script, bool autoload, bool relative_lookup);
+
     // Contains error message if Bison-generated parser returns non-zero
     // status.
     std::string m_parse_error_msg;
@@ -499,6 +506,15 @@
     void *m_parser_state;
   };
 
+  // Publish externally used friend functions.
+
+  extern OCTAVE_API octave_value
+  parse_fcn_file (interpreter& interp, const std::string& full_file,
+                  const std::string& file, const std::string& dir_name,
+                  const std::string& dispatch_type,
+                  const std::string& package_name, bool require_file,
+                  bool force_script, bool autoload, bool relative_lookup);
+
   class parser : public base_parser
   {
   public:
--- a/libinterp/parse-tree/pt-eval.cc	Thu Oct 10 16:56:39 2019 -0400
+++ b/libinterp/parse-tree/pt-eval.cc	Fri Oct 11 09:36:08 2019 -0400
@@ -1245,10 +1245,9 @@
       {
         try
           {
-            ov_code = m_interpreter.parse_fcn_file (file_full_name, file_name,
-                                                    dir_name, "", "",
-                                                    require_file, true, false,
-                                                    false);
+            ov_code = parse_fcn_file (m_interpreter, file_full_name,
+                                      file_name, dir_name, "", "",
+                                      require_file, true, false, false);
           }
         catch (execution_exception& e)
           {