texinfo-commits
[Top][All Lists]
Advanced

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

branch master updated: * tp/Texinfo/ParserNonXS.pm (_initialize_parsing)


From: Patrice Dumas
Subject: branch master updated: * tp/Texinfo/ParserNonXS.pm (_initialize_parsing): merge parser and parser state in the function, return the document. Update callers.
Date: Sun, 20 Oct 2024 16:18:50 -0400

This is an automated email from the git hooks/post-receive script.

pertusus pushed a commit to branch master
in repository texinfo.

The following commit(s) were added to refs/heads/master by this push:
     new efd5621f1e * tp/Texinfo/ParserNonXS.pm (_initialize_parsing): merge 
parser and parser state in the function, return the document.  Update callers.
efd5621f1e is described below

commit efd5621f1efa31d79243131c454b80baf4fb56e7
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Sun Oct 20 22:18:42 2024 +0200

    * tp/Texinfo/ParserNonXS.pm (_initialize_parsing): merge parser and
    parser state in the function, return the document.  Update callers.
    
    * tp/Texinfo/ParserNonXS.pm (_input_push_file): return file_name and
    directories even if the file could not be opened.
    
    * tp/Texinfo/ParserNonXS.pm (parse_texi_file),
    tp/Texinfo/XS/main/build_perl_info.c (get_document, fill_document_hv)
    (store_document_texinfo_tree), tp/Texinfo/XS/parsetexi/Parsetexi.xs
    (parse_texi_file), tp/Texinfo/XS/parsetexi/api.c (parse_file),
    tp/t/test_utils.pl (test), tp/texi2any.pl: return a document, with
    information on directories and file name even if the input file could
    not be opened, but do not set the tree.  Use the presence of a tree to
    determine if parsing was successful.
    
    * tp/Texinfo/XS/main/build_perl_info.c (build_texinfo_tree): return 0
    if there is no tree in document.
---
 ChangeLog                            | 20 ++++++++++++++++
 tp/Texinfo/ParserNonXS.pm            | 45 +++++++++++++++++-------------------
 tp/Texinfo/XS/main/build_perl_info.c | 43 ++++++++++++++++++----------------
 tp/Texinfo/XS/parsetexi/Parsetexi.xs | 20 ++++------------
 tp/Texinfo/XS/parsetexi/api.c        | 18 +++++++--------
 tp/t/test_utils.pl                   | 13 +++++------
 tp/texi2any.pl                       | 20 ++++++++++------
 7 files changed, 97 insertions(+), 82 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 191436c6c3..1c9a9fae47 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2024-10-20  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/Texinfo/ParserNonXS.pm (_initialize_parsing): merge parser and
+       parser state in the function, return the document.  Update callers.
+
+       * tp/Texinfo/ParserNonXS.pm (_input_push_file): return file_name and
+       directories even if the file could not be opened.
+
+       * tp/Texinfo/ParserNonXS.pm (parse_texi_file),
+       tp/Texinfo/XS/main/build_perl_info.c (get_document, fill_document_hv)
+       (store_document_texinfo_tree), tp/Texinfo/XS/parsetexi/Parsetexi.xs
+       (parse_texi_file), tp/Texinfo/XS/parsetexi/api.c (parse_file),
+       tp/t/test_utils.pl (test), tp/texi2any.pl: return a document, with
+       information on directories and file name even if the input file could
+       not be opened, but do not set the tree.  Use the presence of a tree to
+       determine if parsing was successful.
+
+       * tp/Texinfo/XS/main/build_perl_info.c (build_texinfo_tree): return 0
+       if there is no tree in document.
+
 2024-10-20  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/XS/parsetexi/Parsetexi.xs: reindent.
diff --git a/tp/Texinfo/ParserNonXS.pm b/tp/Texinfo/ParserNonXS.pm
index 379d046e07..b09a74d65f 100644
--- a/tp/Texinfo/ParserNonXS.pm
+++ b/tp/Texinfo/ParserNonXS.pm
@@ -713,7 +713,11 @@ sub _initialize_parsing($$)
     $parser_state->{'basic_inline_commands'} = 
{%contain_basic_inline_commands};
   }
 
-  return $parser_state;
+  # We rely on parser state overriding the previous state infomation
+  # in self, as documented in perldata:
+  #   If a key appears more than once in the initializer list of a hash, the 
last occurrence wins
+  %$parser = (%$parser, %$parser_state);
+  return $document;
 }
 
 sub _new_text_input($$)
@@ -799,17 +803,13 @@ sub parse_texi_piece($$;$)
 
   $line_nr = 1 if (not defined($line_nr));
 
-  my $parser_state = $self->_initialize_parsing('ct_base');
-  # We rely on parser state overriding the previous state infomation
-  # in self, as documented in perldata:
-  #   If a key appears more than once in the initializer list of a hash, the 
last occurrence wins
-  %$self = (%$self, %$parser_state);
+  my $document = $self->_initialize_parsing('ct_base');
 
   _input_push_text($self, $text, $line_nr);
 
   my ($document_root, $before_node_section)
      = _setup_document_root_and_before_node_section();
-  my $document = $self->_parse_texi($document_root, $before_node_section);
+  $self->_parse_texi($document_root, $before_node_section);
 
   get_parser_info($self);
 
@@ -824,13 +824,12 @@ sub parse_texi_line($$;$)
 
   $line_nr = 1 if (not defined($line_nr));
 
-  my $parser_state = $self->_initialize_parsing('ct_line');
-  %$self = (%$self, %$parser_state);
+  my $document = $self->_initialize_parsing('ct_line');
 
   _input_push_text($self, $text, $line_nr);
 
   my $root = {'type' => 'root_line'};
-  my $document = $self->_parse_texi($root, $root);
+  $self->_parse_texi($root, $root);
   get_parser_info($self);
   return $document->tree();
 }
@@ -843,12 +842,11 @@ sub parse_texi_text($$;$)
 
   $line_nr = 1 if (not defined($line_nr));
 
-  my $parser_state = $self->_initialize_parsing('ct_base');
-  %$self = (%$self, %$parser_state);
+  my $document = $self->_initialize_parsing('ct_base');
 
   _input_push_text($self, $text, $line_nr);
 
-  my $document = $self->_parse_texi_document();
+  $self->_parse_texi_document();
 
   get_parser_info($self);
   return $document;
@@ -861,9 +859,11 @@ sub _input_push_file
 {
   my ($self, $input_file_path, $file_name_encoding) = @_;
 
+  my ($file_name, $directories, $suffix) = fileparse($input_file_path);
+
   my $filehandle = do { local *FH };
   if (!open($filehandle, $input_file_path)) {
-    return 0, undef, undef, $!;
+    return 0, $file_name, $directories, $!;
   }
 
   # to be able to change the encoding in the midst of reading a file,
@@ -881,8 +881,6 @@ sub _input_push_file
   # test.
   binmode($filehandle);
 
-  my ($file_name, $directories, $suffix) = fileparse($input_file_path);
-
   my $file_input = {
        'input_source_info' => {
           # binary
@@ -940,11 +938,14 @@ sub parse_texi_file($$)
 
   return undef if (!defined($self));
 
-  my $parser_state = $self->_initialize_parsing('ct_base');
-  %$self = (%$self, %$parser_state);
+  my $document = $self->_initialize_parsing('ct_base');
 
   my ($status, $file_name, $directories, $error_message)
     = _input_push_file($self, $input_file_path);
+
+  $document->{'global_info'}->{'input_file_name'} = $file_name;
+  $document->{'global_info'}->{'input_directory'} = $directories;
+
   if (!$status) {
     my $decoded_input_file_path = $input_file_path;
     my $encoding = $self->{'conf'}->{'COMMAND_LINE_ENCODING'};
@@ -954,13 +955,11 @@ sub parse_texi_file($$)
     $self->{'registrar'}->document_error(
                  sprintf(__("could not open %s: %s"),
                                   $decoded_input_file_path, $error_message));
-    return undef;
+    return $document;
   }
 
-  my $document = $self->_parse_texi_document();
+  $self->_parse_texi_document();
   get_parser_info($self);
-  $document->{'global_info'}->{'input_file_name'} = $file_name;
-  $document->{'global_info'}->{'input_directory'} = $directories;
 
   return $document;
 }
@@ -8421,8 +8420,6 @@ X<C<parse_texi_file>>
 The file with name I<$file_name> is considered to be a Texinfo file and
 is parsed into a tree.  I<$file_name> should be a binary string.
 
-undef is returned if the file couldn't be read.
-
 =back
 
 The errors collected during the tree parsing are available with
diff --git a/tp/Texinfo/XS/main/build_perl_info.c 
b/tp/Texinfo/XS/main/build_perl_info.c
index aabbb41c84..d1a304b0c9 100644
--- a/tp/Texinfo/XS/main/build_perl_info.c
+++ b/tp/Texinfo/XS/main/build_perl_info.c
@@ -1003,13 +1003,9 @@ element_to_perl_hash (ELEMENT *e, int avoid_recursion)
 HV *
 build_texinfo_tree (ELEMENT *root, int avoid_recursion)
 {
+  /* should not happen because called should make sure to call with a tree */
   if (! root)
-      /* use an empty element with contents if there is nothing.
-         This should only happen if the input file was not opened
-         or no parse_* function was called after initialization
-         and should not happen with the current calling code.
-      */
-      root = new_element (ET_NONE);
+    return 0;
   /*
   fprintf (stderr, "BTT ------------------------------------------------\n");
    */
@@ -1733,7 +1729,6 @@ get_document (size_t document_descriptor)
   HV *hv;
   DOCUMENT *document;
   SV *sv;
-  HV *hv_tree;
   HV *hv_info;
   SV *registrar_sv;
 
@@ -1742,13 +1737,21 @@ get_document (size_t document_descriptor)
   document = retrieve_document (document_descriptor);
 
   hv = newHV ();
-  hv_tree = newHV ();
 
   hv_info = build_global_info (&document->global_info,
                                &document->global_commands);
 
 #define STORE(key, value) hv_store (hv, key, strlen (key), newRV_inc ((SV *) 
value), 0)
-  STORE("tree", hv_tree);
+  if (document->tree)
+    {
+      HV *hv_tree = newHV ();
+      STORE("tree", hv_tree);
+
+      hv_store (hv_tree, "tree_document_descriptor",
+                strlen ("tree_document_descriptor"),
+                newSViv (document_descriptor), 0);
+    }
+
   STORE("global_info", hv_info);
 
   document->modified_information &= ~F_DOCM_global_info;
@@ -1757,10 +1760,6 @@ get_document (size_t document_descriptor)
   hv_store (hv, "document_descriptor", strlen ("document_descriptor"),
             newSViv (document_descriptor), 0);
 
-  hv_store (hv_tree, "tree_document_descriptor",
-            strlen ("tree_document_descriptor"),
-            newSViv (document_descriptor), 0);
-
   /* New error registrar for document to be used after parsing, for
      structuring and tree modifications */
   registrar_sv = new_texinfo_report ();
@@ -1789,7 +1788,7 @@ static void
 fill_document_hv (HV *hv, size_t document_descriptor, int no_store)
 {
   DOCUMENT *document;
-  HV *hv_tree;
+  HV *hv_tree = 0;
   HV *hv_info;
   HV *hv_commands_info;
   HV *hv_index_names;
@@ -1805,7 +1804,8 @@ fill_document_hv (HV *hv, size_t document_descriptor, int 
no_store)
 
   document = retrieve_document (document_descriptor);
 
-  hv_tree = build_texinfo_tree (document->tree, 0);
+  if (document->tree)
+    hv_tree = build_texinfo_tree (document->tree, 0);
 
   hv_info = build_global_info (&document->global_info,
                                &document->global_commands);
@@ -1843,7 +1843,8 @@ fill_document_hv (HV *hv, size_t document_descriptor, int 
no_store)
 #define STORE(key, value) hv_store (hv, key, strlen (key), newRV_inc ((SV *) 
value), 0)
 
   /* must be kept in sync with Texinfo::Document register keys */
-  STORE("tree", hv_tree);
+  if (hv_tree)
+    STORE("tree", hv_tree);
   document->modified_information &= ~F_DOCM_tree;
   STORE("indices", hv_index_names);
   document->modified_information &= ~F_DOCM_index_names;
@@ -1886,9 +1887,10 @@ fill_document_hv (HV *hv, size_t document_descriptor, 
int no_store)
       hv_store (hv, "document_descriptor", strlen ("document_descriptor"),
                 newSViv (document_descriptor), 0);
 
-      hv_store (hv_tree, "tree_document_descriptor",
-                strlen ("tree_document_descriptor"),
-                newSViv (document_descriptor), 0);
+      if (hv_tree)
+        hv_store (hv_tree, "tree_document_descriptor",
+                  strlen ("tree_document_descriptor"),
+                  newSViv (document_descriptor), 0);
 
       if (!document->hv)
         {
@@ -1994,7 +1996,8 @@ store_document_texinfo_tree (DOCUMENT *document, HV 
*document_hv)
 
   dTHX;
 
-  if (document->modified_information & F_DOCM_tree)
+  if (document->modified_information & F_DOCM_tree
+      && document->tree)
     {
       HV *result_hv = build_texinfo_tree (document->tree, 0);
       hv_store (result_hv, "tree_document_descriptor",
diff --git a/tp/Texinfo/XS/parsetexi/Parsetexi.xs 
b/tp/Texinfo/XS/parsetexi/Parsetexi.xs
index 18d40eaf57..45ab56869a 100644
--- a/tp/Texinfo/XS/parsetexi/Parsetexi.xs
+++ b/tp/Texinfo/XS/parsetexi/Parsetexi.xs
@@ -97,19 +97,8 @@ parse_texi_file (SV *parser_sv, input_file_path)
             int status;
             apply_sv_parser_conf (parser_sv);
             document_descriptor = parse_file (input_file_path, &status);
-            if (status)
-              /* if the input file could not be opened */
-              {
-                pass_document_parser_errors_to_registrar (document_descriptor,
-                                                          parser_sv);
-                remove_document_descriptor (document_descriptor);
-                RETVAL = newSV (0);
-              }
-            else
-              {
-                RETVAL
-                 = get_or_build_document (parser_sv, document_descriptor, 0);
-              }
+            RETVAL
+              = get_or_build_document (parser_sv, document_descriptor, 0);
           }
       OUTPUT:
         RETVAL
@@ -299,8 +288,9 @@ void
 parser_conf_set_accept_internalvalue (int value)
 
 # two possibilities
-#   - errors should be in the last parsed document->parser_error_messages
-#   - errors were put in a registrar key in the parser
+#   - errors should be in the last parsed document->parser_error_messages,
+#     which can be found with "last_document_descriptor"
+#   - errors were put as the "registrar" key value in the parser
 void
 errors (SV *parser_sv)
     PREINIT:
diff --git a/tp/Texinfo/XS/parsetexi/api.c b/tp/Texinfo/XS/parsetexi/api.c
index 8b0f2280c8..44469a6b95 100644
--- a/tp/Texinfo/XS/parsetexi/api.c
+++ b/tp/Texinfo/XS/parsetexi/api.c
@@ -137,6 +137,15 @@ parse_file (const char *input_file_path, int *status)
   char *input_file_name_and_directory[2];
   int input_error;
 
+  parse_file_path (input_file_path, input_file_name_and_directory);
+
+  global_info = &parsed_document->global_info;
+
+  free (global_info->input_file_name);
+  free (global_info->input_directory);
+  global_info->input_file_name = input_file_name_and_directory[0];
+  global_info->input_directory = input_file_name_and_directory[1];
+
   input_error = input_push_file (input_file_path);
   if (input_error)
     {
@@ -161,15 +170,6 @@ parse_file (const char *input_file_path, int *status)
       return document_descriptor;
     }
 
-  parse_file_path (input_file_path, input_file_name_and_directory);
-
-  global_info = &parsed_document->global_info;
-
-  free (global_info->input_file_name);
-  free (global_info->input_directory);
-  global_info->input_file_name = input_file_name_and_directory[0];
-  global_info->input_directory = input_file_name_and_directory[1];
-
   parse_texi_document ();
 
   *status = 0;
diff --git a/tp/t/test_utils.pl b/tp/t/test_utils.pl
index 74bfe753b6..96c5d62e00 100644
--- a/tp/t/test_utils.pl
+++ b/tp/t/test_utils.pl
@@ -1082,14 +1082,18 @@ sub test($$)
     $document = $parser->parse_texi_file($test_file);
   }
 
+  # Get the tree object.  Note that if XS structuring in on, the argument
+  # prevents the tree being built as a Perl structure at this stage; only
+  # a "handle" is returned.
+  my $tree = $document->tree($XS_structuring);
+
   my ($errors, $error_nrs) = $parser->errors();
 
-  my $tree;
   my ($sorted_index_entries, $index_entries_sort_strings);
   my $indices_sorted_sort_strings;
   my $indices;
 
-  if (not defined($document)) {
+  if (not defined($tree)) {
     warn "ERROR: $test_name: parsing result undef\n";
     foreach my $error_message (@$errors) {
       warn $error_message->{'error_line'}
@@ -1098,11 +1102,6 @@ sub test($$)
     goto COMPARE;
   }
 
-  # Get the tree object.  Note that if XS structuring in on, the argument
-  # prevents the tree being built as a Perl structure at this stage; only
-  # a "handle" is returned.
-  $tree = $document->tree($XS_structuring);
-
   # Setup main configuration options, used for structuring.
   my $document_information = $document->global_information();
 
diff --git a/tp/texi2any.pl b/tp/texi2any.pl
index 53a0585ead..fffb876822 100755
--- a/tp/texi2any.pl
+++ b/tp/texi2any.pl
@@ -1733,7 +1733,12 @@ while(@input_files) {
   my $parser = Texinfo::Parser::parser($parser_file_options);
   my $document = $parser->parse_texi_file($input_file_name);
 
-  if (defined($document)
+  # Get the tree object.  Note that if XS structuring in on, the argument
+  # prevents the tree being built as a Perl structure at this stage; only
+  # a "handle" is returned.
+  my $tree = $document->tree($XS_structuring);
+
+  if (defined($tree)
       and (defined(get_conf('DUMP_TREE'))
            or (get_conf('DEBUG') and get_conf('DEBUG') >= 10))) {
     my $tree = $document->tree();
@@ -1746,8 +1751,13 @@ while(@input_files) {
     local $Data::Dumper::Sortkeys = 1;
     print STDERR Data::Dumper->Dump([$tree]);
   }
-  # object registering errors and warnings
-  if (!defined($document) or $output_format eq 'parse') {
+
+  if (!defined($tree)) {
+    handle_errors($parser->errors(), $error_count, \%opened_files);
+    goto NEXT;
+  }
+
+  if ($output_format eq 'parse') {
     handle_errors($parser->errors(), $error_count, \%opened_files);
     goto NEXT;
   }
@@ -1803,10 +1813,6 @@ while(@input_files) {
   my $document_options = $main_configuration->get_customization_options_hash();
   $document->register_document_options($document_options);
 
-  # Get the tree object.  Note that if XS structuring in on, the argument
-  # prevents the tree being built as a Perl structure at this stage; only
-  # a "handle" is returned.
-  my $tree = $document->tree($XS_structuring);
 
   if (defined(get_conf('MACRO_EXPAND')) and $file_number == 0) {
     require Texinfo::Convert::Texinfo;



reply via email to

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