[latex3-commits] [git/LaTeX3-latex3-latex3] master: Implement \file_get:nnNTF and simplify how it avoids end-of-file error (8bd1f20)

Bruno Le Floch bruno at le-floch.fr
Wed Jan 16 22:52:23 CET 2019


Repository : https://github.com/latex3/latex3
On branch  : master
Link       : https://github.com/latex3/latex3/commit/8bd1f204f1609e1974376628a331b9349dbe744c

>---------------------------------------------------------------

commit 8bd1f204f1609e1974376628a331b9349dbe744c
Author: Bruno Le Floch <bruno at le-floch.fr>
Date:   Wed Jan 16 22:51:18 2019 +0100

    Implement \file_get:nnNTF and simplify how it avoids end-of-file error


>---------------------------------------------------------------

8bd1f204f1609e1974376628a331b9349dbe744c
 l3kernel/CHANGELOG.md                   |    2 +-
 l3kernel/l3file.dtx                     |   66 ++++++++++++++++++++-----------
 l3kernel/testfiles/m3expl001.luatex.tlg |    7 +++-
 l3kernel/testfiles/m3expl001.ptex.tlg   |    7 +++-
 l3kernel/testfiles/m3expl001.tlg        |    7 +++-
 l3kernel/testfiles/m3expl001.uptex.tlg  |    7 +++-
 l3kernel/testfiles/m3expl001.xetex.tlg  |    7 +++-
 l3kernel/testfiles/m3expl003.luatex.tlg |    7 +++-
 l3kernel/testfiles/m3expl003.ptex.tlg   |    7 +++-
 l3kernel/testfiles/m3expl003.tlg        |    7 +++-
 l3kernel/testfiles/m3expl003.uptex.tlg  |    7 +++-
 l3kernel/testfiles/m3expl003.xetex.tlg  |    7 +++-
 12 files changed, 93 insertions(+), 45 deletions(-)

diff --git a/l3kernel/CHANGELOG.md b/l3kernel/CHANGELOG.md
index 687052e..c80334c 100644
--- a/l3kernel/CHANGELOG.md
+++ b/l3kernel/CHANGELOG.md
@@ -9,7 +9,7 @@ this project uses date-based 'snapshot' version identifiers.
 
 ### Added
 
-- `\file_get:nnN`
+- `\file_get:nnN` and `\file_get:nnNTF`
 - Experimental function `\sys_shell_get:nnN`
 
 ### Changed
diff --git a/l3kernel/l3file.dtx b/l3kernel/l3file.dtx
index 8523b47..63236b4 100644
--- a/l3kernel/l3file.dtx
+++ b/l3kernel/l3file.dtx
@@ -593,12 +593,18 @@
 % \end{function}
 %
 % \begin{function}[added = 2019-01-16]{\file_get:nnN}
+% \begin{function}[TF, added = 2019-01-16]{\file_get:nnN}
 %   \begin{syntax}
 %     \cs{file_get:nnN} \Arg{filename} \Arg{setup} \meta{tl}
+%     \cs{file_get:nnNTF} \Arg{filename} \Arg{setup} \meta{tl} \Arg{true code} \Arg{false code}
 %   \end{syntax}
 %   Defines \meta{tl} to the contents of \meta{filename}.
 %   Category codes may need to be set appropriately via the \meta{setup}
 %   argument.
+%   While \cs{file_get:nnN} produces an error if the file is not found,
+%   \cs{file_get:nnNTF} runs the \meta{true code} (after the assignment
+%   to \meta{tl}) if the file is found and \meta{false code} otherwise.
+% \end{function}
 % \end{function}
 %
 % \begin{function}[updated = 2017-06-26]
@@ -2164,52 +2170,64 @@
 % \end{macro}
 %
 % \begin{variable}{\c_@@_marker_tl}
-%   The same idea as the marker for rescanning token lists.
+%   The same idea as the marker for rescanning token lists: this pair of
+%   tokens cannot appear in a file that is being input.
 %    \begin{macrocode}
 \tl_const:Nx \c_@@_marker_tl { : \token_to_str:N : }
 %    \end{macrocode}
 % \end{variable}
 %
-% \begin{macro}{\file_get:nnN}
-% \begin{macro}{\@@_get_do:w}
-% \begin{macro}{\@@_set_from:nNNn}
-%   The approach here is similar to that for doing a rescan, and so the same
-%   internals can be reused. Thus the plan is to insert a pair of tokens of
-%   the same charcode but different catcodes after the file has been read.
-%   This plus \cs{exp_not:N} allows the primitive to be used to carry out
-%   a set operation.
+% \begin{macro}[TF]{\file_get:nnN}
+% \begin{macro}{\file_get:nnN,\@@_get_aux:nnN,\@@_get_do:Nw}
+%   The approach here is similar to that for \cs{tl_set_rescan:Nnn}.
+%   The file contents are grabbed as an argument delimited by
+%   \cs{c_@@_marker_tl}.  A few subtleties: braces in \cs{if_false:}
+%   \ldots{} \cs{fi:} to deal with possible alignment tabs,
+%   \tn{tracingnesting} to avoid a warning about a group being closed
+%   inside the \tn{scantokens}, and \cs{prg_return_true:} is placed
+%   after the end-of-file marker.
 %    \begin{macrocode}
 \cs_new_protected:Npn \file_get:nnN #1#2#3
   {
+    \file_get:nnNTF {#1} {#2} #3
+      { }
+      { \__kernel_file_missing:n {#1} }
+  }
+\prg_new_protected_conditional:Npnn \file_get:nnN #1#2#3 { T , F , TF }
+  {
     \file_get_full_name:nN {#1} \l_@@_full_name_str
     \str_if_empty:NTF \l_@@_full_name_str
-      { \__kernel_file_missing:n {#1} }
+      { \prg_return_false: }
       {
-        \exp_args:NV \@@_set_from:nNn
+        \exp_args:NV \@@_get_aux:nnN
           \l_@@_full_name_str
-          #3 {#2}
+          {#2} #3
+        \prg_return_true:
       }
   }
-\exp_args:Nno \use:nn
-  { \cs_new_protected:Npn \@@_get_do:w #1 }
-  { \c_@@_marker_tl }
-  { \tl_set:No \l_@@_internal_tl {#1} }
-\cs_new_protected:Npn \@@_set_from:nNn #1#2#3
+\cs_new_protected:Npn \@@_get_aux:nnN #1#2#3
   {
+    \if_false: { \fi:
     \group_begin:
-      \exp_args:No \tex_everyeof:D
-        { \c_@@_marker_tl \exp_not:N }
-      #3 \scan_stop:
-      \exp_after:wN \@@_get_do:w
+      \int_set_eq:NN \tex_tracingnesting:D \c_zero_int
+      \exp_args:No \tex_everyeof:D { \c_@@_marker_tl }
+      #2 \scan_stop:
+      \exp_after:wN \@@_get_do:Nw
+      \exp_after:wN #3
       \exp_after:wN \prg_do_nothing:
         \tex_input:D #1 \scan_stop:
-    \exp_args:NNNo \group_end:
-    \tl_set:Nn #2 \l_@@_internal_tl
+    \if_false: } \fi:
+  }
+\exp_args:Nno \use:nn
+  { \cs_new_protected:Npn \@@_get_do:Nw #1#2 }
+  { \c_@@_marker_tl }
+  {
+    \group_end:
+    \tl_set:No #1 {#2}
   }
 %    \end{macrocode}
 % \end{macro}
 % \end{macro}
-% \end{macro}
 %
 % \begin{macro}{\file_get_full_name:nN, \file_get_full_name:VN}
 % \begin{macro}{\@@_get_full_name_search:nN}
diff --git a/l3kernel/testfiles/m3expl001.luatex.tlg b/l3kernel/testfiles/m3expl001.luatex.tlg
index e2d67da..3f6c244 100644
--- a/l3kernel/testfiles/m3expl001.luatex.tlg
+++ b/l3kernel/testfiles/m3expl001.luatex.tlg
@@ -2902,8 +2902,11 @@ Defining \__file_name_quote:nN on line ...
 Defining \__file_name_quote_aux:n on line ...
 Defining \c__file_marker_tl on line ...
 Defining \file_get:nnN on line ...
-Defining \__file_get_do:w on line ...
-Defining \__file_set_from:nNn on line ...
+Defining \file_get:nnNT on line ...
+Defining \file_get:nnNF on line ...
+Defining \file_get:nnNTF on line ...
+Defining \__file_get_aux:nnN on line ...
+Defining \__file_get_do:Nw on line ...
 Defining \file_get_full_name:nN on line ...
 Defining \file_get_full_name:VN on line ...
 Defining \__file_get_full_name_search:nN on line ...
diff --git a/l3kernel/testfiles/m3expl001.ptex.tlg b/l3kernel/testfiles/m3expl001.ptex.tlg
index da9f057..abd1b1c 100644
--- a/l3kernel/testfiles/m3expl001.ptex.tlg
+++ b/l3kernel/testfiles/m3expl001.ptex.tlg
@@ -3195,8 +3195,11 @@ Defining \__file_name_quote:nN on line ...
 Defining \__file_name_quote_aux:n on line ...
 Defining \c__file_marker_tl on line ...
 Defining \file_get:nnN on line ...
-Defining \__file_get_do:w on line ...
-Defining \__file_set_from:nNn on line ...
+Defining \file_get:nnNT on line ...
+Defining \file_get:nnNF on line ...
+Defining \file_get:nnNTF on line ...
+Defining \__file_get_aux:nnN on line ...
+Defining \__file_get_do:Nw on line ...
 Defining \file_get_full_name:nN on line ...
 Defining \file_get_full_name:VN on line ...
 Defining \__file_get_full_name_search:nN on line ...
diff --git a/l3kernel/testfiles/m3expl001.tlg b/l3kernel/testfiles/m3expl001.tlg
index c6749a5..2acf3cb 100644
--- a/l3kernel/testfiles/m3expl001.tlg
+++ b/l3kernel/testfiles/m3expl001.tlg
@@ -3195,8 +3195,11 @@ Defining \__file_name_quote:nN on line ...
 Defining \__file_name_quote_aux:n on line ...
 Defining \c__file_marker_tl on line ...
 Defining \file_get:nnN on line ...
-Defining \__file_get_do:w on line ...
-Defining \__file_set_from:nNn on line ...
+Defining \file_get:nnNT on line ...
+Defining \file_get:nnNF on line ...
+Defining \file_get:nnNTF on line ...
+Defining \__file_get_aux:nnN on line ...
+Defining \__file_get_do:Nw on line ...
 Defining \file_get_full_name:nN on line ...
 Defining \file_get_full_name:VN on line ...
 Defining \__file_get_full_name_search:nN on line ...
diff --git a/l3kernel/testfiles/m3expl001.uptex.tlg b/l3kernel/testfiles/m3expl001.uptex.tlg
index 1c930f0..d9c250d 100644
--- a/l3kernel/testfiles/m3expl001.uptex.tlg
+++ b/l3kernel/testfiles/m3expl001.uptex.tlg
@@ -3195,8 +3195,11 @@ Defining \__file_name_quote:nN on line ...
 Defining \__file_name_quote_aux:n on line ...
 Defining \c__file_marker_tl on line ...
 Defining \file_get:nnN on line ...
-Defining \__file_get_do:w on line ...
-Defining \__file_set_from:nNn on line ...
+Defining \file_get:nnNT on line ...
+Defining \file_get:nnNF on line ...
+Defining \file_get:nnNTF on line ...
+Defining \__file_get_aux:nnN on line ...
+Defining \__file_get_do:Nw on line ...
 Defining \file_get_full_name:nN on line ...
 Defining \file_get_full_name:VN on line ...
 Defining \__file_get_full_name_search:nN on line ...
diff --git a/l3kernel/testfiles/m3expl001.xetex.tlg b/l3kernel/testfiles/m3expl001.xetex.tlg
index 29771e5..25b0000 100644
--- a/l3kernel/testfiles/m3expl001.xetex.tlg
+++ b/l3kernel/testfiles/m3expl001.xetex.tlg
@@ -2940,8 +2940,11 @@ Defining \__file_name_quote:nN on line ...
 Defining \__file_name_quote_aux:n on line ...
 Defining \c__file_marker_tl on line ...
 Defining \file_get:nnN on line ...
-Defining \__file_get_do:w on line ...
-Defining \__file_set_from:nNn on line ...
+Defining \file_get:nnNT on line ...
+Defining \file_get:nnNF on line ...
+Defining \file_get:nnNTF on line ...
+Defining \__file_get_aux:nnN on line ...
+Defining \__file_get_do:Nw on line ...
 Defining \file_get_full_name:nN on line ...
 Defining \file_get_full_name:VN on line ...
 Defining \__file_get_full_name_search:nN on line ...
diff --git a/l3kernel/testfiles/m3expl003.luatex.tlg b/l3kernel/testfiles/m3expl003.luatex.tlg
index e2d67da..3f6c244 100644
--- a/l3kernel/testfiles/m3expl003.luatex.tlg
+++ b/l3kernel/testfiles/m3expl003.luatex.tlg
@@ -2902,8 +2902,11 @@ Defining \__file_name_quote:nN on line ...
 Defining \__file_name_quote_aux:n on line ...
 Defining \c__file_marker_tl on line ...
 Defining \file_get:nnN on line ...
-Defining \__file_get_do:w on line ...
-Defining \__file_set_from:nNn on line ...
+Defining \file_get:nnNT on line ...
+Defining \file_get:nnNF on line ...
+Defining \file_get:nnNTF on line ...
+Defining \__file_get_aux:nnN on line ...
+Defining \__file_get_do:Nw on line ...
 Defining \file_get_full_name:nN on line ...
 Defining \file_get_full_name:VN on line ...
 Defining \__file_get_full_name_search:nN on line ...
diff --git a/l3kernel/testfiles/m3expl003.ptex.tlg b/l3kernel/testfiles/m3expl003.ptex.tlg
index da9f057..abd1b1c 100644
--- a/l3kernel/testfiles/m3expl003.ptex.tlg
+++ b/l3kernel/testfiles/m3expl003.ptex.tlg
@@ -3195,8 +3195,11 @@ Defining \__file_name_quote:nN on line ...
 Defining \__file_name_quote_aux:n on line ...
 Defining \c__file_marker_tl on line ...
 Defining \file_get:nnN on line ...
-Defining \__file_get_do:w on line ...
-Defining \__file_set_from:nNn on line ...
+Defining \file_get:nnNT on line ...
+Defining \file_get:nnNF on line ...
+Defining \file_get:nnNTF on line ...
+Defining \__file_get_aux:nnN on line ...
+Defining \__file_get_do:Nw on line ...
 Defining \file_get_full_name:nN on line ...
 Defining \file_get_full_name:VN on line ...
 Defining \__file_get_full_name_search:nN on line ...
diff --git a/l3kernel/testfiles/m3expl003.tlg b/l3kernel/testfiles/m3expl003.tlg
index c6749a5..2acf3cb 100644
--- a/l3kernel/testfiles/m3expl003.tlg
+++ b/l3kernel/testfiles/m3expl003.tlg
@@ -3195,8 +3195,11 @@ Defining \__file_name_quote:nN on line ...
 Defining \__file_name_quote_aux:n on line ...
 Defining \c__file_marker_tl on line ...
 Defining \file_get:nnN on line ...
-Defining \__file_get_do:w on line ...
-Defining \__file_set_from:nNn on line ...
+Defining \file_get:nnNT on line ...
+Defining \file_get:nnNF on line ...
+Defining \file_get:nnNTF on line ...
+Defining \__file_get_aux:nnN on line ...
+Defining \__file_get_do:Nw on line ...
 Defining \file_get_full_name:nN on line ...
 Defining \file_get_full_name:VN on line ...
 Defining \__file_get_full_name_search:nN on line ...
diff --git a/l3kernel/testfiles/m3expl003.uptex.tlg b/l3kernel/testfiles/m3expl003.uptex.tlg
index 1c930f0..d9c250d 100644
--- a/l3kernel/testfiles/m3expl003.uptex.tlg
+++ b/l3kernel/testfiles/m3expl003.uptex.tlg
@@ -3195,8 +3195,11 @@ Defining \__file_name_quote:nN on line ...
 Defining \__file_name_quote_aux:n on line ...
 Defining \c__file_marker_tl on line ...
 Defining \file_get:nnN on line ...
-Defining \__file_get_do:w on line ...
-Defining \__file_set_from:nNn on line ...
+Defining \file_get:nnNT on line ...
+Defining \file_get:nnNF on line ...
+Defining \file_get:nnNTF on line ...
+Defining \__file_get_aux:nnN on line ...
+Defining \__file_get_do:Nw on line ...
 Defining \file_get_full_name:nN on line ...
 Defining \file_get_full_name:VN on line ...
 Defining \__file_get_full_name_search:nN on line ...
diff --git a/l3kernel/testfiles/m3expl003.xetex.tlg b/l3kernel/testfiles/m3expl003.xetex.tlg
index 29771e5..25b0000 100644
--- a/l3kernel/testfiles/m3expl003.xetex.tlg
+++ b/l3kernel/testfiles/m3expl003.xetex.tlg
@@ -2940,8 +2940,11 @@ Defining \__file_name_quote:nN on line ...
 Defining \__file_name_quote_aux:n on line ...
 Defining \c__file_marker_tl on line ...
 Defining \file_get:nnN on line ...
-Defining \__file_get_do:w on line ...
-Defining \__file_set_from:nNn on line ...
+Defining \file_get:nnNT on line ...
+Defining \file_get:nnNF on line ...
+Defining \file_get:nnNTF on line ...
+Defining \__file_get_aux:nnN on line ...
+Defining \__file_get_do:Nw on line ...
 Defining \file_get_full_name:nN on line ...
 Defining \file_get_full_name:VN on line ...
 Defining \__file_get_full_name_search:nN on line ...





More information about the latex3-commits mailing list