texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[no subject]


From: Patrice Dumas
Date: Fri, 19 Apr 2024 06:20:54 -0400 (EDT)

branch: master
commit dafbda473cb2fccb6c7a17121212373f2bf97ea5
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Fri Apr 19 12:20:33 2024 +0200

    * tp/Texinfo/ParserNonXS.pm (_close_command_cleanup)
    (_handle_other_command): remove 'rows_count', instead use
    scalar(@{$parent->{'contents'}}), as in C code.
    
    * tp/Texinfo/XS/parsetexi/parser.c (reset_parser_counters),
    tp/Texinfo/XS/parsetexi/macro.c (expand_linemacro_arguments),
    tp/Texinfo/XS/parsetexi/counter.c (counter_reset): add debugging
    messages optional argument in counter_reset, set for counters reset in
    reset_parser_counters.
    
    * tp/Texinfo/ParserNonXS.pm (_handle_close_brace),
    tp/Texinfo/XS/parsetexi/separator.c (handle_close_brace),
    tp/Texinfo/XS/parsetexi/close.c (close_brace_command): in C,
    remove remaining_args in close_brace_command.  Do not
    remove 'remaining_args' in _handle_close_brace, both in Perl and C.
    
    * tp/Texinfo/XS/parsetexi/close.c (close_command_cleanup),
    tp/Texinfo/XS/parsetexi/counter.c (counter_remove_element): fix removal
    of row count_cells counter, by adding a new function,
    counter_remove_element that remove element counters anywhere in the
    counter list.  This is needed for rows as they are obtained from the
    bottom of the counter list, not from the top.
    
    * tp/Texinfo/XS/parsetexi/handle_commands.c (handle_other_command):
    remove useless counter management code on row parent element that
    never get a counter.
---
 ChangeLog                                 | 29 ++++++++++++++++
 tp/Texinfo/ParserNonXS.pm                 | 12 +++----
 tp/Texinfo/XS/parsetexi/close.c           |  8 +++--
 tp/Texinfo/XS/parsetexi/counter.c         | 55 +++++++++++++++++++++++++++++--
 tp/Texinfo/XS/parsetexi/counter.h         |  3 +-
 tp/Texinfo/XS/parsetexi/handle_commands.c |  4 +--
 tp/Texinfo/XS/parsetexi/macro.c           |  2 +-
 tp/Texinfo/XS/parsetexi/parser.c          |  8 ++---
 tp/Texinfo/XS/parsetexi/separator.c       |  1 -
 9 files changed, 100 insertions(+), 22 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index f47d1582c0..b768359c33 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,32 @@
+2024-04-19  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/Texinfo/ParserNonXS.pm (_close_command_cleanup)
+       (_handle_other_command): remove 'rows_count', instead use
+       scalar(@{$parent->{'contents'}}), as in C code.
+
+       * tp/Texinfo/XS/parsetexi/parser.c (reset_parser_counters),
+       tp/Texinfo/XS/parsetexi/macro.c (expand_linemacro_arguments),
+       tp/Texinfo/XS/parsetexi/counter.c (counter_reset): add debugging
+       messages optional argument in counter_reset, set for counters reset in
+       reset_parser_counters.
+
+       * tp/Texinfo/ParserNonXS.pm (_handle_close_brace),
+       tp/Texinfo/XS/parsetexi/separator.c (handle_close_brace),
+       tp/Texinfo/XS/parsetexi/close.c (close_brace_command): in C,
+       remove remaining_args in close_brace_command.  Do not
+       remove 'remaining_args' in _handle_close_brace, both in Perl and C.
+
+       * tp/Texinfo/XS/parsetexi/close.c (close_command_cleanup),
+       tp/Texinfo/XS/parsetexi/counter.c (counter_remove_element): fix removal
+       of row count_cells counter, by adding a new function,
+       counter_remove_element that remove element counters anywhere in the
+       counter list.  This is needed for rows as they are obtained from the
+       bottom of the counter list, not from the top.
+
+       * tp/Texinfo/XS/parsetexi/handle_commands.c (handle_other_command):
+       remove useless counter management code on row parent element that
+       never get a counter.
+
 2024-04-18  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/Convert/Plaintext.pm (_align_lines): remove $bytes_count
diff --git a/tp/Texinfo/ParserNonXS.pm b/tp/Texinfo/ParserNonXS.pm
index 728e54e65d..67e7fee0d1 100644
--- a/tp/Texinfo/ParserNonXS.pm
+++ b/tp/Texinfo/ParserNonXS.pm
@@ -1456,7 +1456,7 @@ sub _command_error($$$;@)
 }
 
 # register error messages, but otherwise doesn't do much more than
-# return $_[1]->{'parent'}
+# deleting remaining_args and returning $_[1]->{'parent'}
 sub _close_brace_command($$$;$$$)
 {
   my ($self, $current, $source_info, $closed_block_command,
@@ -1953,7 +1953,6 @@ sub _close_command_cleanup($$) {
         $in_head_or_rows = undef;
       }
     }
-    delete $current->{'rows_count'};
   } elsif ($block_commands{$current->{'cmdname'}}
            and $block_commands{$current->{'cmdname'}} eq 'item_container') {
     delete $current->{'items_count'};
@@ -5421,18 +5420,20 @@ sub _handle_other_command($$$$$)
           }
         } else {
           print STDERR "ROW\n" if ($self->{'DEBUG'});
-          $parent->{'rows_count'}++;
           my $row = { 'type' => 'row', 'contents' => [],
                       'cells_count' => 1,
-                      'extra' => {'row_number' => $parent->{'rows_count'} },
                       'parent' => $parent };
           push @{$parent->{'contents'}}, $row;
+          # Note that the "row_number" extra value
+          # isn't actually used anywhere at present.
+          $row->{'extra'}
+              = {'row_number' => scalar(@{$parent->{'contents'}}) - 1};
           $command_e = { 'cmdname' => $command,
                          'parent' => $row,
                          'contents' => [],
                          'extra' => {'cell_number' => 1}};
           push @{$row->{'contents'}}, $command_e;
-          $current = $row->{'contents'}->[-1];
+          $current = $command_e;
         }
         $current = _begin_preformatted($self, $current);
       } elsif ($command eq 'tab') {
@@ -6222,7 +6223,6 @@ sub _handle_close_brace($$$)
     my $closed_command = $current->{'parent'}->{'cmdname'};
     print STDERR "CLOSING(brace) \@$current->{'parent'}->{'cmdname'}\n"
       if ($self->{'DEBUG'});
-    delete $current->{'parent'}->{'remaining_args'};
     if (defined($brace_commands{$closed_command})
          and $brace_commands{$closed_command} eq 'noarg'
          and $current->{'contents'}
diff --git a/tp/Texinfo/XS/parsetexi/close.c b/tp/Texinfo/XS/parsetexi/close.c
index cf34d3998d..3ef1515b36 100644
--- a/tp/Texinfo/XS/parsetexi/close.c
+++ b/tp/Texinfo/XS/parsetexi/close.c
@@ -46,6 +46,8 @@ close_brace_command (ELEMENT *current,
 
   char *delimiter;
 
+  counter_pop (&count_remaining_args);
+
   if (command_data(current->cmd).data == BRACE_context)
     {
       if (current->cmd == CM_math)
@@ -213,11 +215,11 @@ close_command_cleanup (ELEMENT *current)
         {
           ELEMENT *row = old_contents.list[i];
 
-          if (counter_value (&count_cells, row) != -1)
-            counter_pop (&count_cells);
-
           if (row->type == ET_row)
             {
+              int counter_index = counter_remove_element (&count_cells, row);
+              if (counter_index < 0)
+                fprintf (stderr, "BUG: could not remove row counter\n");
               /* Check if we need to open a new container. */
               if (contents_child_by_index (row, 0)->cmd == CM_headitem)
                 {
diff --git a/tp/Texinfo/XS/parsetexi/counter.c 
b/tp/Texinfo/XS/parsetexi/counter.c
index d2b35fcbf4..69b9b9aeb9 100644
--- a/tp/Texinfo/XS/parsetexi/counter.c
+++ b/tp/Texinfo/XS/parsetexi/counter.c
@@ -15,9 +15,11 @@
 
 #include <config.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "tree_types.h"
 #include "utils.h"
+#include "debug.h"
 #include "counter.h"
 
 void
@@ -43,13 +45,42 @@ void
 counter_pop (COUNTER *c)
 {
   if (!c->nvalues)
-    fatal ("could not realloc");
+    fatal ("empty counter");
 
   c->nvalues--;
   c->values[c->nvalues] = 0;
   c->elts[c->nvalues] = 0;
 }
 
+/* remove element anywhere in the counter elements list */
+int
+counter_remove_element (COUNTER *c, ELEMENT *elt)
+{
+  int i;
+
+  if (c->nvalues > 0)
+    {
+      for (i = 0; i < c->nvalues; i++)
+        {
+          if (c->elts[i] == elt)
+            {
+              if (i < c->nvalues - 1)
+                {
+                  memmove (&c->values[i], &c->values[i+1],
+                           (c->nvalues - (i+1)) * sizeof (int));
+                  memmove (&c->elts[i], &c->elts[i+1],
+                           (c->nvalues - (i+1)) * sizeof (ELEMENT *));
+                  c->nvalues--;
+                }
+              else
+                counter_pop (c);
+              return i;
+            }
+        }
+    }
+  return -1;
+}
+
 void
 counter_inc (COUNTER *c)
 {
@@ -73,8 +104,28 @@ counter_value (COUNTER *c, ELEMENT *elt)
     return -1;
 }
 
+/* If NOT_EMPTY_LESSAGE is set, check that the counter values list
+   is empty, if not, show a debugging message */
 void
-counter_reset (COUNTER *c)
+counter_reset (COUNTER *c, const char* not_empty_message)
 {
+  if (not_empty_message && c->nvalues > 0)
+    {
+      int i;
+      fprintf (stderr, "BUG: %s: counter %p not empty: %d remain\n",
+               not_empty_message, c, c->nvalues);
+      for (i = 0; i < c->nvalues; i++)
+        {
+          /* In general elements have been destroyed already
+          ELEMENT *e = c->elts[i];
+          char *element_string = print_element_debug (e, 0);
+           */
+          char *element_string = 0;
+          fprintf (stderr, "  %d: %d, %s\n", i, c->values[i],
+                                             element_string);
+          free (element_string);
+        }
+    }
+
   c->nvalues = 0;
 }
diff --git a/tp/Texinfo/XS/parsetexi/counter.h 
b/tp/Texinfo/XS/parsetexi/counter.h
index 92041aae24..089c2234ef 100644
--- a/tp/Texinfo/XS/parsetexi/counter.h
+++ b/tp/Texinfo/XS/parsetexi/counter.h
@@ -32,8 +32,9 @@ void counter_push (COUNTER *c, ELEMENT *e, int n);
 void counter_pop (COUNTER *c);
 void counter_inc (COUNTER *c);
 void counter_dec (COUNTER *c);
+int counter_remove_element (COUNTER *c, ELEMENT *elt);
 int counter_value (COUNTER *c, ELEMENT *e);
-void counter_reset (COUNTER *c);
+void counter_reset (COUNTER *c, const char* not_empty_message);
 
 /* A large positive number used to represent an unlimited number of remaining
    arguments. */
diff --git a/tp/Texinfo/XS/parsetexi/handle_commands.c 
b/tp/Texinfo/XS/parsetexi/handle_commands.c
index d83bff3067..7ee0a3619f 100644
--- a/tp/Texinfo/XS/parsetexi/handle_commands.c
+++ b/tp/Texinfo/XS/parsetexi/handle_commands.c
@@ -460,7 +460,7 @@ handle_other_command (ELEMENT *current, char **line_inout,
                   row = new_element (ET_row);
                   add_to_element_contents (parent, row);
 
-                  /* Note that the "row_number" extra value,
+                  /* Note that the "row_number" extra value
                      isn't actually used anywhere at present. */
                   add_extra_integer (row, "row_number",
                                      parent->contents.number - 1);
@@ -470,8 +470,6 @@ handle_other_command (ELEMENT *current, char **line_inout,
                   add_to_element_contents (row, command_e);
                   current = command_e;
 
-                  if (counter_value (&count_cells, parent) != -1)
-                    counter_pop (&count_cells);
                   counter_push (&count_cells, row, 1);
                   add_extra_integer (current, "cell_number",
                                      counter_value (&count_cells, row));
diff --git a/tp/Texinfo/XS/parsetexi/macro.c b/tp/Texinfo/XS/parsetexi/macro.c
index 16ace6a9f2..9489e14d7d 100644
--- a/tp/Texinfo/XS/parsetexi/macro.c
+++ b/tp/Texinfo/XS/parsetexi/macro.c
@@ -472,7 +472,7 @@ expand_linemacro_arguments (ELEMENT *macro, char 
**line_inout,
   int i;
   ELEMENT *argument = new_element (ET_NONE);
   ELEMENT *argument_content = new_element (ET_NONE);
-  counter_reset (&count_toplevel_braces);
+  counter_reset (&count_toplevel_braces, 0);
   counter_push (&count_toplevel_braces, argument_content, 0);
 
   add_to_element_args (current, argument);
diff --git a/tp/Texinfo/XS/parsetexi/parser.c b/tp/Texinfo/XS/parsetexi/parser.c
index aaba1ab326..dd49f8f27e 100644
--- a/tp/Texinfo/XS/parsetexi/parser.c
+++ b/tp/Texinfo/XS/parsetexi/parser.c
@@ -302,14 +302,12 @@ COUNTER count_remaining_args;
 COUNTER count_items;
 COUNTER count_cells;
 
-/* TODO there should be debug messages for those counters to check if
-   them being set at the end of parsing is because of bugs or not */
 void
 reset_parser_counters (void)
 {
-  counter_reset (&count_remaining_args);
-  counter_reset (&count_items);
-  counter_reset (&count_cells);
+  counter_reset (&count_remaining_args, "count_remaining_args");
+  counter_reset (&count_items, "count_items");
+  counter_reset (&count_cells, "count_cells");
 }
 
 
diff --git a/tp/Texinfo/XS/parsetexi/separator.c 
b/tp/Texinfo/XS/parsetexi/separator.c
index 3a9e1c61c4..a87213036e 100644
--- a/tp/Texinfo/XS/parsetexi/separator.c
+++ b/tp/Texinfo/XS/parsetexi/separator.c
@@ -307,7 +307,6 @@ handle_close_brace (ELEMENT *current, char **line_inout)
 
       closed_command = current->parent->cmd;
       debug ("CLOSING(brace) @%s", command_data(closed_command).cmdname);
-      counter_pop (&count_remaining_args);
 
       if (current->contents.number > 0
           && command_data(closed_command).data == BRACE_noarg)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]