changeset 11223:64e7538db12a

fix printing of newlines for anonymous function handle bodies
author John W. Eaton <jwe@octave.org>
date Wed, 10 Nov 2010 04:07:14 -0500
parents 6eba18ec59b6
children e0db3f9e9267
files src/ChangeLog src/ov-fcn-handle.cc src/pt-pr-code.cc src/pt-pr-code.h
diffstat 4 files changed, 65 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Wed Nov 10 01:35:50 2010 -0500
+++ b/src/ChangeLog	Wed Nov 10 04:07:14 2010 -0500
@@ -1,3 +1,16 @@
+2010-11-10  John W. Eaton  <jwe@octave.org>
+
+	Bug #31491.
+
+	* pt-pr-code.cc, pt-pr-code.h (tree_print_code::print_fcn_handle_body):
+	New function.
+	* ov-fcn-handle (octave_fcn_handle::print_raw): Use it.
+	* pt-pr-code.cc (tree_print_code::visit_anon_fcn_handle): Likewise.
+	* pt-pr-code.h (tree_print_code::suppress_newline): Rename from
+	printing_newlines.  Now int.  Change all uses.
+	* pt-pr-code.cc (tree_print_code::newline): Only set
+	beginning_of_line if newline is printed.
+
 2010-11-10  John W. Eaton  <jwe@octave.org>
 
 	Bug #31567.
--- a/src/ov-fcn-handle.cc	Wed Nov 10 01:35:50 2010 -0500
+++ b/src/ov-fcn-handle.cc	Wed Nov 10 04:07:14 2010 -0500
@@ -1321,33 +1321,7 @@
 
           os << ") ";
 
-          tree_statement_list *b = f->body ();
-
-          if (b)
-            {
-              assert (b->length () == 1);
-
-              tree_statement *s = b->front ();
-
-              if (s)
-                {
-                  if (s->is_expression ())
-                    {
-                      tree_expression *e = s->expression ();
-
-                      if (e)
-                        e->accept (tpc);
-                    }
-                  else
-                    {
-                      tree_command *c = s->command ();
-
-                      tpc.suspend_newline ();
-                      c->accept (tpc);
-                      tpc.resume_newline ();
-                    }
-                }
-            }
+          tpc.print_fcn_handle_body (f->body ());
 
           printed = true;
         }
--- a/src/pt-pr-code.cc	Wed Nov 10 01:35:50 2010 -0500
+++ b/src/pt-pr-code.cc	Wed Nov 10 04:07:14 2010 -0500
@@ -51,10 +51,7 @@
 
   os << ") ";
 
-  tree_statement_list *body = afh.body ();
-
-  if (body)
-    body->accept (*this);
+  print_fcn_handle_body (afh.body ());
 
   print_parens (afh, ")");
 }
@@ -1150,6 +1147,40 @@
   newline ();
 }
 
+void
+tree_print_code::print_fcn_handle_body (tree_statement_list *b)
+{
+  if (b)
+    {
+      assert (b->length () == 1);
+
+      tree_statement *s = b->front ();
+
+      if (s)
+        {
+          if (s->is_expression ())
+            {
+              tree_expression *e = s->expression ();
+
+              if (e)
+                {
+                  suppress_newlines++;
+                  e->accept (*this);
+                  suppress_newlines--;
+                }
+            }
+          else
+            {
+              tree_command *c = s->command ();
+
+              suppress_newlines++;
+              c->accept (*this);
+              suppress_newlines--;
+            }
+        }
+    }
+}
+
 // Each print_code() function should call this before printing
 // anything.
 //
@@ -1160,17 +1191,14 @@
 {
   assert (curr_print_indent_level >= 0);
 
-  if (printing_newlines)
+  if (beginning_of_line)
     {
-      if (beginning_of_line)
-        {
-          os << prefix;
+      os << prefix;
 
-          for (int i = 0; i < curr_print_indent_level; i++)
-            os << " ";
+      for (int i = 0; i < curr_print_indent_level; i++)
+        os << " ";
 
-          beginning_of_line = false;
-        }
+      beginning_of_line = false;
     }
 }
 
@@ -1179,9 +1207,14 @@
 void
 tree_print_code::newline (const char *alt_txt)
 {
-  os << (printing_newlines ? "\n" : alt_txt);
+  if (suppress_newlines)
+    os << alt_txt;
+  else
+    {
+      os << "\n";
 
-  beginning_of_line = true;
+      beginning_of_line = true;
+    }
 }
 
 // For ressetting print_code state.
--- a/src/pt-pr-code.h	Wed Nov 10 01:35:50 2010 -0500
+++ b/src/pt-pr-code.h	Wed Nov 10 04:07:14 2010 -0500
@@ -46,7 +46,7 @@
     : os (os_arg), prefix (pfx), nesting (),
       print_original_text (pr_orig_txt),
       curr_print_indent_level (0), beginning_of_line (true),
-      printing_newlines (true)
+      suppress_newlines (0)
   {
     // For "none".
     nesting.push ('n');
@@ -140,9 +140,7 @@
 
   void visit_do_until_command (tree_do_until_command&);
 
-  void suspend_newline (void) { printing_newlines = false; }
-
-  void resume_newline (void) { printing_newlines = true; }
+  void print_fcn_handle_body (tree_statement_list *);
 
 private:
 
@@ -160,8 +158,8 @@
   // TRUE means we are at the beginning of a line.
   bool beginning_of_line;
 
-  // TRUE means we are printing newlines and indenting.
-  bool printing_newlines;
+  // Nonzero means we are not printing newlines and indenting.
+  int suppress_newlines;
 
   void do_decl_command (tree_decl_command& cmd);