[latex3-commits] [git/LaTeX3-latex3-latex3] main: Add \peek_remove_filler:n (a78588254)

Joseph Wright joseph.wright at morningstar2.co.uk
Tue Jan 11 21:25:31 CET 2022


Repository : https://github.com/latex3/latex3
On branch  : main
Link       : https://github.com/latex3/latex3/commit/a78588254394d0f1e5d7c655f912563094d9ae19

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

commit a78588254394d0f1e5d7c655f912563094d9ae19
Author: Joseph Wright <joseph.wright at morningstar2.co.uk>
Date:   Tue Jan 11 19:34:01 2022 +0000

    Add \peek_remove_filler:n


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

a78588254394d0f1e5d7c655f912563094d9ae19
 l3kernel/CHANGELOG.md                              |  1 +
 l3kernel/l3token.dtx                               | 85 ++++++++++++++++++++++
 .../{m3peek001.ptex.tlg => m3peek001.luatex.tlg}   | 67 +++++------------
 l3kernel/testfiles/m3peek001.lvt                   |  8 ++
 l3kernel/testfiles/m3peek001.ptex.tlg              | 19 +++++
 l3kernel/testfiles/m3peek001.tlg                   | 19 +++++
 l3kernel/testfiles/m3peek001.uptex.tlg             | 19 +++++
 7 files changed, 170 insertions(+), 48 deletions(-)

diff --git a/l3kernel/CHANGELOG.md b/l3kernel/CHANGELOG.md
index ad6751961..ecbb0fe13 100644
--- a/l3kernel/CHANGELOG.md
+++ b/l3kernel/CHANGELOG.md
@@ -9,6 +9,7 @@ this project uses date-based 'snapshot' version identifiers.
 
 ### Added
 - Support for validity scope for keys
+- `\peek_remove_filler:n`
 - `\prop_to_keyval:N`
 - `\regex_match_case:nn(TF)`, `\regex_replace_case_once:nN(TF)`,
   `\regex_replace_case_all:nN(TF)`
diff --git a/l3kernel/l3token.dtx b/l3kernel/l3token.dtx
index 7a3073674..7867caceb 100644
--- a/l3kernel/l3token.dtx
+++ b/l3kernel/l3token.dtx
@@ -974,6 +974,29 @@
 %   appropriate to the result of the test).
 % \end{function}
 %
+% \begin{function}[added = 2022-01-10]{\peek_remove_filler:n}
+%   \begin{syntax}
+%     \cs{peek_remove_filler:n} \Arg{code}
+%   \end{syntax}
+%   Peeks ahead and detect if the following token is a space (category code
+%   $10$) or has meaning equal to \cs{scan_stop:}. If so,
+%   removes the token and checks the next token. If neither of these cases
+%   apply, expands the next token using \texttt{f}-type expansion, then checks
+%   the resulting leading token in the same way. If after expansion the next
+%   token is neither of the two test cases, the \meta{code} will be inserted
+%   into the input stream. Typically this will contain a \texttt{peek}
+%   operation, but this is not required.
+%   \begin{texnote}
+%     This is essentially a macro-based implementation of how \TeX{} handles
+%     the search for a left brace after for example \tn{everypar}, except that
+%     any non-expandable token cleanly ends the \meta{filler} (i.e.~it does not
+%     lead to a \TeX{} error).
+%
+%     In contrast to \TeX{}'s filler removal, a construct
+%     \verb|\exp_not:N \foo| will be treated in the same way as \verb|\foo|.
+%   \end{texnote}
+% \end{function}
+%
 % \begin{function}[updated = 2012-12-05, TF]
 %   {\peek_meaning_remove_ignore_spaces:N}
 %   \begin{syntax}
@@ -2914,6 +2937,68 @@ end
 %    \end{macrocode}
 % \end{macro}
 %
+% \begin{macro}{\peek_remove_filler:n}
+% \begin{macro}{\@@_remove_filler:w}
+% \begin{macro}{\@@_remove_filler:}
+% \begin{macro}{\@@_remove_filler_expand:w}
+%   Here we expand the input, removing spaces and \cs{scan_stop:} tokens until
+%   we reach a non-expandable token. At that stage we re-insert the payload.
+%   To deal with the problem of |&| tokens, we have to put the align-safe
+%   group in the correct place.
+%    \begin{macrocode}
+\cs_new_protected:Npn \peek_remove_filler:n #1
+  {
+    \cs_set:Npn \@@_true_aux:w { \@@_remove_filler:w }
+    \cs_set:Npx \@@_false:w
+      {
+        \exp_not:N \group_align_safe_end:
+        \exp_not:n {#1}
+      }
+    \group_align_safe_begin:
+    \@@_remove_filler:w
+  }
+\cs_new_protected:Npn \@@_remove_filler:w
+  {
+    \exp_after:wN \peek_after:Nw \exp_after:wN \@@_remove_filler:
+    \exp:w \exp_end_continue_f:w
+  }
+%    \end{macrocode}
+%   Here we can nest conditionals as \cs{l_peek_token} is only skipped over in
+%   the nested one if it's a space: no problems with conditionals or outer
+%   tokens.
+%    \begin{macrocode}
+\cs_new_protected:Npn \@@_remove_filler:
+  {
+    \if_catcode:w \exp_not:N \l_peek_token \c_space_token
+      \exp_after:wN \@@_true_remove:w
+    \else:
+      \if_meaning:w \l_peek_token \scan_stop:
+        \exp_after:wN \exp_after:wN \exp_after:wN
+          \@@_true_remove:w
+      \else:
+        \exp_after:wN \exp_after:wN \exp_after:wN
+          \@@_remove_filler_expand:w
+      \fi:
+    \fi:
+  }
+%    \end{macrocode}
+%   To deal with undefined control sequences in the same way \TeX{} does,
+%   we need to check for expansion manually.
+%    \begin{macrocode}
+\cs_new_protected:Npn \@@_remove_filler_expand:w
+  {
+    \exp_after:wN \if_meaning:w \exp_not:N \l_peek_token \l_peek_token
+      \exp_after:wN \@@_false:w
+    \else:
+      \exp_after:wN \@@_remove_filler:w
+    \fi:
+  }
+%    \end{macrocode}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+%
 % \begin{macro}{\@@_token_generic_aux:NNNTF}
 %   The generic functions store the test token in both implicit and
 %   explicit modes, and the \texttt{true} and \texttt{false} code as
diff --git a/l3kernel/testfiles/m3peek001.ptex.tlg b/l3kernel/testfiles/m3peek001.luatex.tlg
similarity index 84%
copy from l3kernel/testfiles/m3peek001.ptex.tlg
copy to l3kernel/testfiles/m3peek001.luatex.tlg
index 49b9f48b8..744e972c9 100644
--- a/l3kernel/testfiles/m3peek001.ptex.tlg
+++ b/l3kernel/testfiles/m3peek001.luatex.tlg
@@ -20,14 +20,6 @@ TRUE
 -
 FALSE
 !
-TRUE
-!
-TRUE
-!
-TRUE
--
-TRUE
--
 ============================================================
 TRUE
 !
@@ -37,14 +29,6 @@ TRUE
 -
 FALSE
 !
-TRUE
-!
-TRUE
-!
-TRUE
--
-TRUE
--
 ============================================================
 ============================================================
 TEST 3: peek_meaning again
@@ -67,14 +51,6 @@ TRUE
 -
 FALSE
 !
-TRUE
-!
-TRUE
-!
-TRUE
--
-TRUE
--
 ============================================================
 TRUE
 !
@@ -84,14 +60,6 @@ TRUE
 -
 FALSE
 !
-TRUE
-!
-TRUE
-!
-TRUE
--
-TRUE
--
 ============================================================
 ============================================================
 TEST 5: peek_charcode again
@@ -114,14 +82,6 @@ TRUE
 -
 FALSE
 ?
-TRUE
-?
-TRUE
-?
-TRUE
--
-TRUE
--
 ============================================================
 TRUE
 ?
@@ -131,14 +91,6 @@ TRUE
 -
 FALSE
 ?
-TRUE
-?
-TRUE
-?
-TRUE
--
-TRUE
--
 ============================================================
 ============================================================
 TEST 7: peek_catcode again
@@ -217,3 +169,22 @@ l. ...  }
 <recently read> *
 l. ...  }
 ============================================================
+============================================================
+TEST 12: \peek_remove_filler:n 
+============================================================
+> the letter _.
+<recently read> _
+l. ...  }
+! Undefined control sequence.
+<argument> ...\peek_remove_filler:n {\tex_show:D }\undefined 
+                                                  \scan_stop: _
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+> the letter _.
+<recently read> _
+l. ...  }
+============================================================
diff --git a/l3kernel/testfiles/m3peek001.lvt b/l3kernel/testfiles/m3peek001.lvt
index d86c76721..152a8b775 100644
--- a/l3kernel/testfiles/m3peek001.lvt
+++ b/l3kernel/testfiles/m3peek001.lvt
@@ -213,6 +213,14 @@
     \tex_lowercase:D { \peek_remove_spaces:n { \tex_show:D } ~ }
   }
 
+
+\TEST { \peek_remove_filler:n }
+  {
+    \tl_set:Nx \l_tmpa_tl { \use:n { \scan_stop: \c_space_tl } ~ \scan_stop: } 
+    \use:n { \peek_remove_filler:n { \tex_show:D } ~ \c_space_token } ~ \c_space_tl
+      \l_tmpa_tl _
+    \peek_remove_filler:n { \tex_show:D } \undefined \scan_stop: _
+  }
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 \END
diff --git a/l3kernel/testfiles/m3peek001.ptex.tlg b/l3kernel/testfiles/m3peek001.ptex.tlg
index 49b9f48b8..576a9ab8a 100644
--- a/l3kernel/testfiles/m3peek001.ptex.tlg
+++ b/l3kernel/testfiles/m3peek001.ptex.tlg
@@ -217,3 +217,22 @@ l. ...  }
 <recently read> *
 l. ...  }
 ============================================================
+============================================================
+TEST 12: \peek_remove_filler:n 
+============================================================
+> the letter _.
+<recently read> _
+l. ...  }
+! Undefined control sequence.
+<argument> ...e_filler:n {\tex_show:D }\undefined 
+                                                  \scan_stop: _
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+> the letter _.
+<recently read> _
+l. ...  }
+============================================================
diff --git a/l3kernel/testfiles/m3peek001.tlg b/l3kernel/testfiles/m3peek001.tlg
index 49b9f48b8..576a9ab8a 100644
--- a/l3kernel/testfiles/m3peek001.tlg
+++ b/l3kernel/testfiles/m3peek001.tlg
@@ -217,3 +217,22 @@ l. ...  }
 <recently read> *
 l. ...  }
 ============================================================
+============================================================
+TEST 12: \peek_remove_filler:n 
+============================================================
+> the letter _.
+<recently read> _
+l. ...  }
+! Undefined control sequence.
+<argument> ...e_filler:n {\tex_show:D }\undefined 
+                                                  \scan_stop: _
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+> the letter _.
+<recently read> _
+l. ...  }
+============================================================
diff --git a/l3kernel/testfiles/m3peek001.uptex.tlg b/l3kernel/testfiles/m3peek001.uptex.tlg
index 49b9f48b8..576a9ab8a 100644
--- a/l3kernel/testfiles/m3peek001.uptex.tlg
+++ b/l3kernel/testfiles/m3peek001.uptex.tlg
@@ -217,3 +217,22 @@ l. ...  }
 <recently read> *
 l. ...  }
 ============================================================
+============================================================
+TEST 12: \peek_remove_filler:n 
+============================================================
+> the letter _.
+<recently read> _
+l. ...  }
+! Undefined control sequence.
+<argument> ...e_filler:n {\tex_show:D }\undefined 
+                                                  \scan_stop: _
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+> the letter _.
+<recently read> _
+l. ...  }
+============================================================





More information about the latex3-commits mailing list.