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, 14 Jul 2023 20:31:05 -0400 (EDT)

branch: master
commit 244ebf293cc2412530d4c7dd700c3a34e2197d2e
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Sat Jul 15 02:30:53 2023 +0200

    * tp/Texinfo/ParserNonXS.pm (_parse_macro_command_line)
    (_expand_macro_arguments, _end_line, _handle_macro)
    (_process_remaining_on_line), tp/Texinfo/XS/parsetexi/end_line.c
    (end_line_misc_line), tp/Texinfo/XS/parsetexi/macro.c
    (parse_macro_command_line, expand_macro_arguments, handle_macro),
    tp/Texinfo/XS/parsetexi/parser.c (process_remaining_on_line):
    synchronize debug messages, move code around to be more consistent
    across parsers,
    
    * tp/Texinfo/ParserNonXS.pm (_handle_comma): remove retval return
    value, it is only set to the same value as the default in the caller.
    
    * tp/Texinfo/ParserNonXS.pm (_new_macro, _process_remaining_on_line):
    add _new_macro function to be more in line with XS parser.
    
    * tp/Texinfo/ParserNonXS.pm (_expand_macro_body, _handle_macro): allow
    macrobody to be undef if there is no content, similar to XS parser.
---
 ChangeLog                          |  20 +++++++
 tp/Texinfo/ParserNonXS.pm          | 108 +++++++++++++++++++++++--------------
 tp/Texinfo/XS/parsetexi/end_line.c |   4 +-
 tp/Texinfo/XS/parsetexi/macro.c    |  51 +++++++++++-------
 tp/Texinfo/XS/parsetexi/parser.c   |   4 +-
 5 files changed, 123 insertions(+), 64 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 246e6d0f58..1b2f211cb0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2023-07-14  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/Texinfo/ParserNonXS.pm (_parse_macro_command_line)
+       (_expand_macro_arguments, _end_line, _handle_macro)
+       (_process_remaining_on_line), tp/Texinfo/XS/parsetexi/end_line.c
+       (end_line_misc_line), tp/Texinfo/XS/parsetexi/macro.c
+       (parse_macro_command_line, expand_macro_arguments, handle_macro),
+       tp/Texinfo/XS/parsetexi/parser.c (process_remaining_on_line):
+       synchronize debug messages, move code around to be more consistent
+       across parsers, 
+
+       * tp/Texinfo/ParserNonXS.pm (_handle_comma): remove retval return
+       value, it is only set to the same value as the default in the caller.
+
+       * tp/Texinfo/ParserNonXS.pm (_new_macro, _process_remaining_on_line):
+       add _new_macro function to be more in line with XS parser.
+
+       * tp/Texinfo/ParserNonXS.pm (_expand_macro_body, _handle_macro): allow
+       macrobody to be undef if there is no content, similar to XS parser.
+
 2023-07-14  Patrice Dumas  <pertusus@free.fr>
 
        * tp/t/60macro.t: add spaces after bad macro specification argument.
diff --git a/tp/Texinfo/ParserNonXS.pm b/tp/Texinfo/ParserNonXS.pm
index 31eccb4723..558f1a6a10 100644
--- a/tp/Texinfo/ParserNonXS.pm
+++ b/tp/Texinfo/ParserNonXS.pm
@@ -1258,15 +1258,16 @@ sub _parse_macro_command_line($$$$$;$)
     my $args_def = $2;
     my @args;
 
-    if ($args_def =~ s/^\s*{\s*(.*?)\s*}\s*//) {
-      @args = split(/\s*,\s*/, $1);
-    }
-
     print STDERR "MACRO \@$command $macro_name\n" if ($self->{'DEBUG'});
 
     $macro->{'args'} = [
       { 'type' => 'macro_name', 'text' => $macro_name,
           'parent' => $macro } ];
+
+    if ($args_def =~ s/^\s*{\s*(.*?)\s*}\s*//) {
+      @args = split(/\s*,\s*/, $1);
+    }
+
     foreach my $formal_arg (@args) {
       push @{$macro->{'args'}},
         { 'type' => 'macro_arg', 'text' => $formal_arg,
@@ -2545,11 +2546,8 @@ sub _expand_macro_arguments($$$$$)
                 __("use %s instead of %s in macro arg"), '@comma{}', '\\,'),
               $source_info);
           }
-          print STDERR "MACRO ARG: $separator: $protected_char\n"
-            if ($self->{'DEBUG'});
         } else {
           $argument_content->{'text'} .= '\\';
-          print STDERR "MACRO ARG: $separator\n" if ($self->{'DEBUG'});
         }
       } elsif ($separator eq ',') {
         if ($braces_level > 1) {
@@ -2640,6 +2638,8 @@ sub _expand_macro_body($$$$) {
 
   my $macrobody = $macro->{'macrobody'};
 
+  return undef if (!defined($macrobody));
+
   my $result = '';
   while ($macrobody ne '') {
     if ($macrobody =~ s/^([^\\]*)\\//) {
@@ -4376,7 +4376,7 @@ sub _end_line($$$)
       # if in a linemacro command call nested on a line, we do not close
       # the preceding commands yet, as they might use the expansion
       print STDERR "Expanded \@$in_macro_expansion still line/block"
-       ." command $top_context: "
+       ." $top_context: "
        .Texinfo::Common::debug_print_element($current, 1)."\n"
         if ($self->{'DEBUG'});
       return $current;
@@ -4793,11 +4793,13 @@ sub _handle_macro($$$$$)
   my $command = shift;
 
   my $expanded_macro = $self->{'macros'}->{$command}->{'element'};
-  my $args_number = scalar(@{$expanded_macro->{'args'}}) -1;
+
   my $macro_call_element = {'type' => $expanded_macro->{'cmdname'}.'_call',
                             'extra' => {'name' => $command},
                             'args' => []};
+
   if ($expanded_macro->{'cmdname'} ne 'linemacro') {
+    my $args_number = scalar(@{$expanded_macro->{'args'}}) -1;
     if ($line =~ s/^\s*{(\s*)//) { # } macro with args
       if ($1 ne '') {
         $macro_call_element->{'info'}
@@ -4884,28 +4886,41 @@ sub _handle_macro($$$$$)
   my $expanded = _expand_macro_body($self,
                             $self->{'macros'}->{$command},
                             $macro_call_element->{'args'}, $source_info);
-  print STDERR "MACROBODY: $expanded".'||||||'."\n"
-    if ($self->{'DEBUG'});
 
-  chomp($expanded);
+  delete $macro_call_element->{'args'}
+     if (scalar(@{$macro_call_element->{'args'}}) == 0);
+
+  my $expanded_macro_text;
+  if (defined($expanded)) {
+    chomp($expanded);
+    $expanded_macro_text = $expanded;
+  } else {
+    # we want to always have a text for the source mark
+    $expanded_macro_text = "";
+  }
+
+  print STDERR "MACROBODY: $expanded_macro_text".'||||||'."\n"
+    if ($self->{'DEBUG'});
 
-  # first put the line that was interrupted by the macro call
-  # on the input pending text with information stack
-  _input_push_text($self, $line, $source_info->{'line_nr'});
-  # then put the following macro expansion lines with information on the
-  # pending text
-  _input_push_text($self, $expanded, $source_info->{'line_nr'},
-                   $expanded_macro->{'args'}->[0]->{'text'});
   my $macro_source_mark = {'sourcemark_type' => 'macro_expansion',
                            'status' => 'start'};
-  delete $macro_call_element->{'args'}
-     if (scalar(@{$macro_call_element->{'args'}}) == 0);
   $macro_source_mark->{'element'} = $macro_call_element;
   _register_source_mark($self, $current, $macro_source_mark);
+
+  # first put the line that was interrupted by the macro call
+  # on the input pending text stack
+  _input_push_text($self, $line, $source_info->{'line_nr'});
+
+  # Put expansion in front of the current line.
+  _input_push_text($self, $expanded_macro_text, $source_info->{'line_nr'},
+                   $expanded_macro->{'args'}->[0]->{'text'});
+
   $self->{'input'}->[0]->{'input_source_mark'} = $macro_source_mark;
+
   # not really important as line is ignored by the caller if there
   # was no macro expansion error
   $line = '';
+
  #funexit:
   return ($macro_call_element, $line, $source_info);
 }
@@ -6160,8 +6175,6 @@ sub _handle_comma($$$$)
   my $line = shift;
   my $source_info = shift;
 
-  my $retval = $STILL_MORE_TO_PROCESS;
-
   _abort_empty_line($self, $current);
   _isolate_last_space($self, $current);
   # type corresponds to three possible containers: in brace commands,
@@ -6256,7 +6269,7 @@ sub _handle_comma($$$$)
           # Second argument is missing.
           $current = $current->{'args'}->[-1];
           $line = '}' . $line;
-          return ($current, $line, $source_info, $retval);
+          return ($current, $line, $source_info);
           # goto funexit;  # used in XS code
         } else {
           $current->{'remaining_args'}--;
@@ -6309,7 +6322,7 @@ sub _handle_comma($$$$)
       $current->{'remaining_args'}--;
       $current = $current->{'args'}->[-1];
       $line = '}' . $line;
-      return ($current, $line, $source_info, $retval);
+      return ($current, $line, $source_info);
       # goto funexit;  # used in XS code
     }
   }
@@ -6325,7 +6338,29 @@ sub _handle_comma($$$$)
                      };
   push @{$current->{'contents'}}, $space_before;
 
-  return ($current, $line, $source_info, $retval);
+  return ($current, $line, $source_info);
+}
+
+sub _new_macro($$$)
+{
+  my $self = shift;
+  my $name = shift;
+  my $current = shift;
+
+  my $macrobody;
+  if (defined($current->{'contents'})) {
+    $macrobody =
+       Texinfo::Convert::Texinfo::convert_to_texinfo(
+                    { 'contents' => $current->{'contents'} });
+  }
+  $self->{'macros'}->{$name} = {
+    'element' => $current,
+    'macrobody' => $macrobody
+  };
+  delete $self->{'aliases'}->{$name};
+  # FIXME check that this is still true with linemacro
+  # could be cleaner to delete definfoenclose'd too, but macros
+  # are expanded earlier
 }
 
 sub _process_remaining_on_line($$$$)
@@ -6371,10 +6406,6 @@ sub _process_remaining_on_line($$$$)
         if ($current->{'cmdname'} eq 'macro'
             or $current->{'cmdname'} eq 'rmacro'
             or $current->{'cmdname'} eq 'linemacro') {
-          # store toplevel macro specification
-          my $macrobody =
-             Texinfo::Convert::Texinfo::convert_to_texinfo(
-                                 { 'contents' => $current->{'contents'} });
           if ($current->{'args'} and $current->{'args'}->[0]) {
             my $name = $current->{'args'}->[0]->{'text'};
             if (exists($self->{'macros'}->{$name})) {
@@ -6393,14 +6424,7 @@ sub _process_remaining_on_line($$$$)
             }
             if (!($current->{'extra'}
                   and $current->{'extra'}->{'invalid_syntax'})) {
-              $self->{'macros'}->{$name} = {
-                'element' => $current,
-                'macrobody' => $macrobody
-              };
-              delete $self->{'aliases'}->{$name};
-              # FIXME check that this is still true with linemacro
-              # could be cleaner to delete definfoenclose'd too, but macros
-              # are expanded earlier
+              _new_macro($self, $name, $current);
             }
           }
         }
@@ -6854,12 +6878,14 @@ sub _process_remaining_on_line($$$$)
         if ($command eq 'value') {
           if (not exists($self->{'values'}->{$value})) {
             _abort_empty_line($self, $current);
+
+            $self->_line_warn(
+               sprintf(__("undefined flag: %s"), $value), $source_info);
+
             # caller should expand something along
             # gdt($self, '@{No value for `{value}\'@}', {'value' => $value});
             my $new_element = _new_value_element($command, $value, $current);
             push @{$current->{'contents'}}, $new_element;
-            $self->_line_warn(
-               sprintf(__("undefined flag: %s"), $value), $source_info);
           # expansion of value already done above
           #} else {
           }
@@ -7008,7 +7034,7 @@ sub _process_remaining_on_line($$$$)
     substr ($line, 0, 1) = '';
     if ($current->{'parent'}
         and $current->{'parent'}->{'remaining_args'}) {
-      ($current, $line, $source_info, $retval)
+      ($current, $line, $source_info)
          = _handle_comma($self, $current, $line, $source_info);
     } elsif ($current->{'type'}
              and $current->{'type'} eq 'line_arg'
diff --git a/tp/Texinfo/XS/parsetexi/end_line.c 
b/tp/Texinfo/XS/parsetexi/end_line.c
index 8a63bd66f2..b8556badbd 100644
--- a/tp/Texinfo/XS/parsetexi/end_line.c
+++ b/tp/Texinfo/XS/parsetexi/end_line.c
@@ -1417,8 +1417,6 @@ end_line_misc_line (ELEMENT *current)
               int status;
               char *fullpath, *sys_filename;
 
-              debug ("Include %s", text);
-
               sys_filename = encode_file_name (text);
               fullpath = locate_include_file (sys_filename);
 
@@ -1443,6 +1441,8 @@ end_line_misc_line (ELEMENT *current)
                   else
                     {
                       included_file = 1;
+                      debug ("Included %s", fullpath);
+
                       include_source_mark = new_source_mark (SM_type_include);
                       include_source_mark->status = SM_status_start;
                       set_input_source_mark (include_source_mark);
diff --git a/tp/Texinfo/XS/parsetexi/macro.c b/tp/Texinfo/XS/parsetexi/macro.c
index 32d35161f8..afe2e1eb8d 100644
--- a/tp/Texinfo/XS/parsetexi/macro.c
+++ b/tp/Texinfo/XS/parsetexi/macro.c
@@ -114,14 +114,10 @@ parse_macro_command_line (enum command_id cmd, char 
**line_inout,
   macro->source_info = current_source_info;
 
   add_info_string_dup (macro, "arg_line", line);
-  /* Note this extra value isn't used much, so it might be possible
-     to get rid of it. */
 
   line += strspn (line, whitespace_chars);
   name = read_command_name (&line);
 
-  debug ("MACRO @%s %s", command_name (cmd), name);
-
   if (*line && *line != '{' && !strchr (whitespace_chars, *line))
     {
       line_error ("bad name for @%s", command_name (cmd));
@@ -135,6 +131,8 @@ parse_macro_command_line (enum command_id cmd, char 
**line_inout,
       return macro;
     }
 
+  debug ("MACRO @%s %s", command_name (cmd), name);
+
   macro_name = new_element (ET_macro_name);
   text_append (&macro_name->text, name);
   free (name);
@@ -222,13 +220,20 @@ parse_macro_command_line (enum command_id cmd, char 
**line_inout,
       index++;
     }
 
-check_trailing:
+ check_trailing:
   line = args_ptr;
   line += strspn (line, whitespace_chars);
   if (*line && *line != '@')
     {
+      char *argument_str = strdup (line);
+      /* remove new line for the message */
+      char *end_line = strchr (argument_str, '\n');
+
+      if (end_line)
+            *end_line = '\0';
       line_error ("bad syntax for @%s argument: %s",
-                  command_name(cmd), line);
+                  command_name(cmd), argument_str);
+      free (argument_str);
       add_extra_integer (macro, "invalid_syntax", 1);
     }
   //line += strlen (line); /* Discard rest of line. */
@@ -400,7 +405,6 @@ expand_macro_arguments (ELEMENT *macro, char **line_inout, 
enum command_id cmd,
         }
     }
 
-  debug ("END MACRO ARGS EXPANSION");
   line = pline;
 
   if (args_total == 0
@@ -411,6 +415,7 @@ expand_macro_arguments (ELEMENT *macro, char **line_inout, 
enum command_id cmd,
         ("macro `%s' declared without argument called with an argument",
          command_name(cmd));
     }
+  debug ("END MACRO ARGS EXPANSION");
 
 funexit:
   *line_inout = line;
@@ -701,30 +706,38 @@ handle_macro (ELEMENT *current, char **line_inout, enum 
command_id cmd)
     goto funexit;
 
   expand_macro_body (macro_record, macro_call_element, &expanded);
-  debug ("MACROBODY: %s||||||", expanded.text);
 
-  if (expanded.end > 0 && expanded.text[expanded.end - 1] == '\n')
-    expanded.text[--expanded.end] = '\0';
+  if (expanded.text)
+    {
+      if (expanded.text[expanded.end - 1] == '\n')
+        expanded.text[--expanded.end] = '\0';
+      expanded_macro_text = expanded.text;
+    }
+  else
+    /* we want to always have a text for the source mark */
+    expanded_macro_text = strdup ("");
+
+  debug ("MACROBODY: %s||||||", expanded_macro_text);
 
   macro_source_mark = new_source_mark (SM_type_macro_expansion);
   macro_source_mark->status = SM_status_start;
   macro_source_mark->element = macro_call_element;
   register_source_mark (current, macro_source_mark);
 
-  /* Put expansion in front of the current line. */
+  /* first put the line that was interrupted by the macro call
+     on the input pending text stack */
   input_push_text (strdup (line), current_source_info.line_nr, 0, 0);
-  /* not really important as line is ignored by the caller if there
-     was no macro expansion error */
-  line = strchr (line, '\0');
-  if (expanded.text)
-    expanded_macro_text = expanded.text;
-  else
-    /* we want to always have a text for the source mark */
-    expanded_macro_text = strdup ("");
+
+  /* Put expansion in front of the current line. */
   input_push_text (expanded_macro_text, current_source_info.line_nr,
                    command_name(cmd), 0);
+
   set_input_source_mark (macro_source_mark);
 
+  /* not really important as line is ignored by the caller if there
+     was no macro expansion error */
+  line = strchr (line, '\0');
+
  funexit:
 
   if (error)
diff --git a/tp/Texinfo/XS/parsetexi/parser.c b/tp/Texinfo/XS/parsetexi/parser.c
index 876ee1432a..f54216bab8 100644
--- a/tp/Texinfo/XS/parsetexi/parser.c
+++ b/tp/Texinfo/XS/parsetexi/parser.c
@@ -2116,10 +2116,10 @@ process_remaining_on_line (ELEMENT **current_inout, 
char **line_inout)
                        for the converters to handle */
                       ELEMENT *value_elt;
 
-                      line_warn ("undefined flag: %s", flag);
-
                       abort_empty_line (&current, NULL);
 
+                      line_warn ("undefined flag: %s", flag);
+
                       value_elt = new_value_element (cmd, flag);
                       add_to_element_contents (current, value_elt);
 



reply via email to

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