changeset 21063:202cfd2b4514

store parser state in base class; use it in push and pull parsers * parse.h, oct-parse.in.yy (octave_base_parser::parser_state): Move here from octave_push_parser class. (octave_base_parser::octave_base_parser): Initialize parser_state in initializer list. (octave_base_parser::~octave_base_parser): Delete parser_state. (octave_base_parser::reset): Delete and re-create parser_state. (octave_push_parser::init): Delete method and all uses. (octave_push_parser::~octave_push_parser): Don't delete parser_state.
author John W. Eaton <jwe@octave.org>
date Thu, 14 Jan 2016 05:35:36 -0500
parents d9c1884d1aaa
children a9f2c2d72892
files libinterp/parse-tree/oct-parse.in.yy libinterp/parse-tree/parse.h
diffstat 2 files changed, 30 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/oct-parse.in.yy	Wed Jan 13 22:30:10 2016 -0800
+++ b/libinterp/parse-tree/oct-parse.in.yy	Thu Jan 14 05:35:36 2016 -0500
@@ -2029,11 +2029,30 @@
   parser.bison_error (s);
 }
 
+octave_base_parser::octave_base_parser (octave_base_lexer& lxr)
+  : endfunction_found (false), autoloading (false),
+    fcn_file_from_relative_lookup (false), parsing_subfunctions (false),
+    max_fcn_depth (0), curr_fcn_depth (0), primary_fcn_scope (-1),
+    curr_class_name (), curr_package_name (), function_scopes (),
+    primary_fcn_ptr (0), subfunction_names (), classdef_object (0),
+    stmt_list (0), lexer (lxr), parser_state (yypstate_new ())
+{ }
+
 octave_base_parser::~octave_base_parser (void)
 {
   delete stmt_list;
 
   delete &lexer;
+
+  // FIXME: Deleting the internal Bison parser state structure does
+  // not clean up any partial parse trees in the event of an interrupt or
+  // error.  It's not clear how to safely do that with the C language
+  // parser that Bison generates.  The C++ language parser that Bison
+  // generates would do it for us automatically whenever an exception
+  // is thrown while parsing input, but there is currently no C++
+  // interface for a push parser.
+
+  yypstate_delete (static_cast<yypstate *> (parser_state));
 }
 
 void
@@ -2056,6 +2075,9 @@
   stmt_list = 0;
 
   lexer.reset ();
+
+  yypstate_delete (static_cast<yypstate *> (parser_state));
+  parser_state = yypstate_new ();
 }
 
 // Error mesages for mismatched end tokens.
@@ -4004,17 +4026,6 @@
   return octave_parse (*this);
 }
 
-octave_push_parser::~octave_push_parser (void)
-{
-  yypstate_delete (static_cast<yypstate *> (parser_state));
-}
-
-void
-octave_push_parser::init (void)
-{
-  parser_state = yypstate_new ();
-}
-
 // Parse input from INPUT.  Pass TRUE for EOF if the end of INPUT should
 // finish the parse.
 
--- a/libinterp/parse-tree/parse.h	Wed Jan 13 22:30:10 2016 -0800
+++ b/libinterp/parse-tree/parse.h	Thu Jan 14 05:35:36 2016 -0500
@@ -145,22 +145,14 @@
 {
 public:
 
-  octave_base_parser (octave_base_lexer& lxr)
-    : endfunction_found (false),
-      autoloading (false), fcn_file_from_relative_lookup (false),
-      parsing_subfunctions (false), max_fcn_depth (0),
-      curr_fcn_depth (0), primary_fcn_scope (-1),
-      curr_class_name (), curr_package_name (), function_scopes (),
-      primary_fcn_ptr (0), subfunction_names (), classdef_object (0),
-      stmt_list (0), lexer (lxr)
-  { }
+  octave_base_parser (octave_base_lexer& lxr);
 
   ~octave_base_parser (void);
 
   void reset (void);
 
   // Error mesages for mismatched end tokens.
-  void end_error (const char *type, token::end_tok_type ettype, int l, int c);
+  void end_error (const char *type, token::end_tok_type expected, int l, int c);
 
   // Check to see that end tokens are properly matched.
   bool end_token_ok (token *tok, token::end_tok_type expected);
@@ -458,6 +450,9 @@
   // State of the lexer.
   octave_base_lexer& lexer;
 
+  // Internal state of the Bison parser.
+  void *parser_state;
+
 private:
 
   // No copying!
@@ -507,22 +502,15 @@
 public:
 
   octave_push_parser (void)
-    : octave_base_parser (*(new octave_push_lexer ())), parser_state (0)
-  {
-    init ();
-  }
+    : octave_base_parser (*(new octave_push_lexer ()))
+  { }
 
-  ~octave_push_parser (void);
-
-  void init (void);
+  ~octave_push_parser (void) { }
 
   int run (const std::string& input, bool eof);
 
 private:
 
-  // Internal state of the Bison parser.
-  void *parser_state;
-
   // No copying!
 
   octave_push_parser (const octave_push_parser&);