changeset 17693:efbe746f8fa8

eliminate octave_comment_buffer singleton * lex.h (octave_base_lexer::comment_buffer): New class adapted from octave_comment_buffer class. No longer a singleton class. (octave_base_lexer::comment_buffer::reset): New function. (octave_base_lexer::reset): Call comment_buf.reset. (octave_base_lexer::comment_buf): New data member. (octave_base_lexer::get_comment): New function. * comment-list.h, comment-list.cc (octave_comment_buffer): Delete. Change all uses of octave_comment_buffer to use local comment_buf object instead. * parse.h (octave_base_parser::make_statement): New member function. * oct-parse.in.yy (make_statement): Delete. Change all uses of make_statement to use the member function instead. (safe_fclose): Don't extract and delete comment list here.
author John W. Eaton <jwe@octave.org>
date Fri, 18 Oct 2013 21:00:33 -0400
parents 38cf56b77274
children 1efe4c65c5cf
files libinterp/corefcn/comment-list.cc libinterp/corefcn/comment-list.h libinterp/parse-tree/lex.h libinterp/parse-tree/lex.ll libinterp/parse-tree/oct-parse.in.yy libinterp/parse-tree/parse.h
diffstat 6 files changed, 77 insertions(+), 125 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/comment-list.cc	Fri Oct 18 16:27:44 2013 -0700
+++ b/libinterp/corefcn/comment-list.cc	Fri Oct 18 21:00:33 2013 -0400
@@ -25,13 +25,10 @@
 #endif
 
 #include "lo-utils.h"
-#include "singleton-cleanup.h"
 
 #include "comment-list.h"
 #include "error.h"
 
-octave_comment_buffer *octave_comment_buffer::instance = 0;
-
 octave_comment_list *
 octave_comment_list::dup (void) const
 {
@@ -46,61 +43,3 @@
 
   return new_cl;
 }
-
-bool
-octave_comment_buffer::instance_ok (void)
-{
-  bool retval = true;
-
-  if (! instance)
-    {
-      instance = new octave_comment_buffer ();
-
-      if (instance)
-        singleton_cleanup_list::add (cleanup_instance);
-    }
-
-  if (! instance)
-    {
-      ::error ("unable to create comment buffer object");
-
-      retval = false;
-    }
-
-  return retval;
-}
-
-void
-octave_comment_buffer::append (const std::string& s,
-                               octave_comment_elt::comment_type t)
-{
-  if (instance_ok ())
-    instance->do_append (s, t);
-}
-
-octave_comment_list *
-octave_comment_buffer::get_comment (void)
-{
-  return (instance_ok ()) ? instance->do_get_comment () : 0;
-}
-
-void
-octave_comment_buffer::do_append (const std::string& s,
-                                  octave_comment_elt::comment_type t)
-{
-  comment_list->append (s, t);
-}
-
-octave_comment_list *
-octave_comment_buffer::do_get_comment (void)
-{
-  octave_comment_list *retval = 0;
-
-  if (comment_list && comment_list->length () > 0)
-    {
-      retval = comment_list;
-      comment_list = new octave_comment_list ();
-    }
-
-  return retval;
-}
--- a/libinterp/corefcn/comment-list.h	Fri Oct 18 16:27:44 2013 -0700
+++ b/libinterp/corefcn/comment-list.h	Fri Oct 18 21:00:33 2013 -0400
@@ -98,35 +98,4 @@
   octave_comment_list *dup (void) const;
 };
 
-class
-octave_comment_buffer
-{
-public:
-
-  octave_comment_buffer (void)
-    : comment_list (new octave_comment_list ()) { }
-
-  ~octave_comment_buffer (void) { delete comment_list; }
-
-  static bool instance_ok (void);
-
-  static void append
-    (const std::string& s,
-     octave_comment_elt::comment_type t = octave_comment_elt::unknown);
-
-  static octave_comment_list *get_comment (void);
-
-private:
-
-  void do_append (const std::string& s, octave_comment_elt::comment_type t);
-
-  octave_comment_list *do_get_comment (void);
-
-  octave_comment_list *comment_list;
-
-  static octave_comment_buffer *instance;
-
-  static void cleanup_instance (void) { delete instance; instance = 0; }
-};
-
 #endif
--- a/libinterp/parse-tree/lex.h	Fri Oct 18 16:27:44 2013 -0700
+++ b/libinterp/parse-tree/lex.h	Fri Oct 18 21:00:33 2013 -0400
@@ -493,8 +493,50 @@
     bool eof;
   };
 
+  // Collect comment text.
+
+  class
+  comment_buffer
+  {
+  public:
+
+    comment_buffer (void) : comment_list (0) { }
+
+    ~comment_buffer (void) { delete comment_list; }
+
+    void append (const std::string& s, octave_comment_elt::comment_type t)
+    {
+      if (! comment_list)
+        comment_list = new octave_comment_list ();
+
+      comment_list->append (s, t);
+    }
+
+    // Caller is expected to delete the returned value.
+
+    octave_comment_list *get_comment (void)
+    {
+      octave_comment_list *retval = comment_list;
+
+      comment_list = 0;
+
+      return retval;
+    }
+
+    void reset (void)
+    {
+      delete comment_list;
+
+      comment_list = 0;
+    }
+
+  private:
+
+    octave_comment_list *comment_list;
+  };
+
   octave_base_lexer (void)
-    : lexical_feedback (), scanner (0), input_buf ()
+    : lexical_feedback (), scanner (0), input_buf (), comment_buf ()
   {
     init ();
   }
@@ -545,6 +587,8 @@
 
   void finish_comment (octave_comment_elt::comment_type typ);
 
+  octave_comment_list *get_comment (void) { return comment_buf.get_comment (); }
+
   int handle_close_bracket (int bracket_type);
 
   bool looks_like_command_arg (void);
@@ -583,6 +627,9 @@
   // Object that reads and buffers input.
   input_buffer input_buf;
 
+  // Object that collects comment text.
+  comment_buffer comment_buf;
+
   virtual void increment_promptflag (void) = 0;
 
   virtual void decrement_promptflag (void) = 0;
--- a/libinterp/parse-tree/lex.ll	Fri Oct 18 16:27:44 2013 -0700
+++ b/libinterp/parse-tree/lex.ll	Fri Oct 18 21:00:33 2013 -0400
@@ -2037,6 +2037,8 @@
     yyrestart (stdin, scanner);
 
   lexical_feedback::reset ();
+
+  comment_buf.reset ();
 }
 
 void
@@ -2537,7 +2539,7 @@
   if (copyright)
     typ = octave_comment_elt::copyright;
 
-  octave_comment_buffer::append (comment_text, typ);
+  comment_buf.append (comment_text, typ);
 
   comment_text = "";
 
--- a/libinterp/parse-tree/oct-parse.in.yy	Fri Oct 18 16:27:44 2013 -0700
+++ b/libinterp/parse-tree/oct-parse.in.yy	Fri Oct 18 21:00:33 2013 -0400
@@ -105,16 +105,6 @@
 
 static void yyerror (octave_base_parser& parser, const char *s);
 
-// Finish building a statement.
-template <class T>
-static tree_statement *
-make_statement (T *arg)
-{
-  octave_comment_list *comment = octave_comment_buffer::get_comment ();
-
-  return new tree_statement (arg, comment);
-}
-
 #define ABORT_PARSE \
   do \
     { \
@@ -396,11 +386,11 @@
                 ;
 
 statement       : expression
-                  { $$ = make_statement ($1); }
+                  { $$ = parser.make_statement ($1); }
                 | command
-                  { $$ = make_statement ($1); }
+                  { $$ = parser.make_statement ($1); }
                 | word_list_cmd
-                  { $$ = make_statement ($1); }
+                  { $$ = parser.make_statement ($1); }
                 ;
 
 // =================
@@ -1579,7 +1569,7 @@
                 ;
 
 stash_comment   : // empty
-                  { $$ = octave_comment_buffer::get_comment (); }
+                  { $$ = lexer.get_comment (); }
                 ;
 
 parse_error     : LEXICAL_ERROR
@@ -2338,7 +2328,7 @@
 
   if (end_token_ok (end_tok, token::unwind_protect_end))
     {
-      octave_comment_list *tc = octave_comment_buffer::get_comment ();
+      octave_comment_list *tc = lexer.comment_buf.get_comment ();
 
       int l = unwind_tok->line ();
       int c = unwind_tok->column ();
@@ -2370,7 +2360,7 @@
 
   if (end_token_ok (end_tok, token::try_catch_end))
     {
-      octave_comment_list *tc = octave_comment_buffer::get_comment ();
+      octave_comment_list *tc = lexer.comment_buf.get_comment ();
 
       int l = try_tok->line ();
       int c = try_tok->column ();
@@ -2424,7 +2414,7 @@
 
   if (end_token_ok (end_tok, token::while_end))
     {
-      octave_comment_list *tc = octave_comment_buffer::get_comment ();
+      octave_comment_list *tc = lexer.comment_buf.get_comment ();
 
       lexer.looping--;
 
@@ -2452,7 +2442,7 @@
 {
   maybe_warn_assign_as_truth_value (expr);
 
-  octave_comment_list *tc = octave_comment_buffer::get_comment ();
+  octave_comment_list *tc = lexer.comment_buf.get_comment ();
 
   lexer.looping--;
 
@@ -2479,7 +2469,7 @@
 
   if (end_token_ok (end_tok, parfor ? token::parfor_end : token::for_end))
     {
-      octave_comment_list *tc = octave_comment_buffer::get_comment ();
+      octave_comment_list *tc = lexer.comment_buf.get_comment ();
 
       lexer.looping--;
 
@@ -2573,7 +2563,7 @@
 
   if (end_token_ok (end_tok, token::if_end))
     {
-      octave_comment_list *tc = octave_comment_buffer::get_comment ();
+      octave_comment_list *tc = lexer.comment_buf.get_comment ();
 
       int l = if_tok->line ();
       int c = if_tok->column ();
@@ -2626,7 +2616,7 @@
 
   if (end_token_ok (end_tok, token::switch_end))
     {
-      octave_comment_list *tc = octave_comment_buffer::get_comment ();
+      octave_comment_list *tc = lexer.comment_buf.get_comment ();
 
       int l = switch_tok->line ();
       int c = switch_tok->column ();
@@ -2810,7 +2800,7 @@
 
   if (fcn)
     {
-      octave_comment_list *tc = octave_comment_buffer::get_comment ();
+      octave_comment_list *tc = lexer.comment_buf.get_comment ();
 
       fcn->stash_trailing_comment (tc);
       fcn->stash_fcn_end_location (end_fcn_stmt->line (),
@@ -3344,6 +3334,16 @@
   return list;
 }
 
+// Finish building a statement.
+template <class T>
+tree_statement *
+octave_base_parser::make_statement (T *arg)
+{
+  octave_comment_list *comment = lexer.get_comment ();
+
+  return new tree_statement (arg, comment);
+}
+
 tree_statement_list *
 octave_base_parser::make_statement_list (tree_statement *stmt)
 {
@@ -3465,15 +3465,6 @@
 static void
 safe_fclose (FILE *f)
 {
-  // FIXME -- comments at the end of an input file are
-  // discarded (otherwise, they would be appended to the next
-  // statement, possibly from the command line or another file, which
-  // can be quite confusing).
-
-  octave_comment_list *tc = octave_comment_buffer::get_comment ();
-
-  delete tc;
-
   if (f)
     fclose (static_cast<FILE *> (f));
 }
--- a/libinterp/parse-tree/parse.h	Fri Oct 18 16:27:44 2013 -0700
+++ b/libinterp/parse-tree/parse.h	Fri Oct 18 21:00:33 2013 -0400
@@ -327,6 +327,10 @@
   tree_statement_list *
   set_stmt_print_flag (tree_statement_list *, char, bool);
 
+  // Finish building a statement.
+  template <class T>
+  tree_statement *make_statement (T *arg);
+
   // Create a statement list.
   tree_statement_list *make_statement_list (tree_statement *stmt);