[latex3-commits] [git/LaTeX3-latex3-latex3] cctab: Check mismatched cctabs and group (f72fdde5b)

PhelypeOleinik tex.phelype at gmail.com
Fri Jul 3 03:33:25 CEST 2020


Repository : https://github.com/latex3/latex3
On branch  : cctab
Link       : https://github.com/latex3/latex3/commit/f72fdde5b9a900d9f72a26536d8f3351d1b4a068

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

commit f72fdde5b9a900d9f72a26536d8f3351d1b4a068
Author: PhelypeOleinik <tex.phelype at gmail.com>
Date:   Thu Jul 2 22:33:25 2020 -0300

    Check mismatched cctabs and group


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

f72fdde5b9a900d9f72a26536d8f3351d1b4a068
 l3kernel/l3cctab.dtx              | 71 ++++++++++++++++++++++++++++++++++
 l3kernel/testfiles/m3cctab003.lvt | 80 +++++++++++++++++++++++++++++++++++++++
 l3kernel/testfiles/m3cctab003.tlg | 57 ++++++++++++++++++++++++++++
 3 files changed, 208 insertions(+)

diff --git a/l3kernel/l3cctab.dtx b/l3kernel/l3cctab.dtx
index 558ec9929..ec269efe1 100644
--- a/l3kernel/l3cctab.dtx
+++ b/l3kernel/l3cctab.dtx
@@ -196,6 +196,13 @@
 %    \end{macrocode}
 % \end{variable}
 %
+% \begin{variable}{\g_@@_group_seq}
+%   A stack to store the group level when a catcode table started.
+%    \begin{macrocode}
+\seq_new:N \g_@@_group_seq
+%    \end{macrocode}
+% \end{variable}
+%
 % \begin{variable}{\g_@@_allocate_int}
 %   Integer to keep track of what category code table to allocate.  In
 %   \LuaTeX{} it is only used in format mode to implement
@@ -509,6 +516,7 @@
   {
     \@@_chk_if_valid:NT #1
       {
+        \@@_chk_group_begin:
         \seq_gpop:NNF \g_@@_unused_seq \l_@@_internal_tl
           { \@@_begin_aux: }
         \seq_gpush:NV \g_@@_stack_seq \l_@@_internal_tl
@@ -533,12 +541,65 @@
       {
         \seq_gpush:NV \g_@@_unused_seq \l_@@_internal_tl
         \@@_select:N \l_@@_internal_tl
+        \@@_chk_group_end:
       }
       { \__kernel_msg_error:nn { kernel } { cctab-extra-end } }
   }
 %    \end{macrocode}
 % \end{macro}
 %
+% \begin{macro}{\@@_chk_group_begin:,\@@_chk_group_end:}
+%   Catcode tables are not allowed to be intermixed with groups, so here
+%   we check that they are properly nested regarding \TeX{} groups.
+%   \cs{@@_chk_group_begin:} stores the current group level in a stack,
+%   and locally defines a dummy control sequence
+%   \cs[no-index]{@@_group_\meta{group-level}_chk:}.
+%
+%   \cs{@@_chk_group_end:} pops the stack, and compares the returned
+%   value with \cs{tex_currentgrouplevel:D}.  If they differ,
+%   \cs{cctab_end:} is in a different grouping level than the matching
+%   \cs{cctab_begin:N}.  If they are the same, both happened at the same
+%   level, but a group might have ended and another started between
+%   \cs{cctab_begin:N} and \cs{cctab_end:}, which is the case if
+%   \cs[no-index]{@@_group_\meta{group-level}_chk:} doesn't exist
+%   since it was locally defined.
+%
+%   Any of these cases just raise an error, but no recovery is
+%   attempted:  usually interleaving groups and catcode tables will work
+%   as expected.
+%    \begin{macrocode}
+\cs_new_protected:Npn \@@_chk_group_begin:
+  {
+    \seq_gpush:Nx \g_@@_group_seq
+      { \int_use:N \tex_currentgrouplevel:D }
+    \cs_set_eq:cN
+      { @@_group_ \int_use:N \tex_currentgrouplevel:D _chk: }
+      \prg_do_nothing:
+  }
+\cs_new_protected:Npn \@@_chk_group_end:
+  {
+    \seq_gpop:NN \g_@@_group_seq \l_@@_internal_tl
+    \int_compare:nNnTF
+        { \tex_currentgrouplevel:D } = { \l_@@_internal_tl }
+      {
+        \cs_if_exist:cF { @@_group_ \l_@@_internal_tl _chk: }
+          {
+            \__kernel_msg_error:nnx { kernel } { cctab-group-mismatch }
+              { different~group }
+          }
+      }
+      {
+        \__kernel_msg_error:nnx { kernel } { cctab-group-mismatch }
+          {
+            \int_compare:nNnTF
+                { \tex_currentgrouplevel:D } < { \l_@@_internal_tl }
+              { lower } { higher } ~group~level
+          }
+      }
+  }
+%    \end{macrocode}
+% \end{macro}
+%
 % \subsection{Category code table conditionals}
 %
 % \begin{macro}{\cctab_if_exist:N,\cctab_if_exist:c}
@@ -685,6 +746,16 @@
     initialized~using~\iow_char:N\\cctab_new:N~or~
     \iow_char:N\\cctab_const:Nn.
   }
+\__kernel_msg_new:nnnn { kernel } { cctab-group-mismatch }
+  {
+    \iow_char:N\\cctab_end:~occurred~in~a~#1~than~
+    the~matching~\iow_char:N\\cctab_begin:N.
+  }
+  {
+    Catcode~tables~and~groups~must~be~properly~nested,~but~
+    you~tried~to~interleave~them.~LaTeX~will~try~to~proceed,~
+    but~results~may~be~unexpected.
+  }
 %    \end{macrocode}
 %
 %    \begin{macrocode}
diff --git a/l3kernel/testfiles/m3cctab003.lvt b/l3kernel/testfiles/m3cctab003.lvt
new file mode 100644
index 000000000..c30a89b43
--- /dev/null
+++ b/l3kernel/testfiles/m3cctab003.lvt
@@ -0,0 +1,80 @@
+%
+% Copyright (C) 2020 The LaTeX3 Project
+%
+
+\documentclass{minimal}
+\input{regression-test}
+
+\RequirePackage[enable-debug]{expl3}
+\ExplSyntaxOn
+\debug_on:n { check-declarations , deprecation }
+\ExplSyntaxOff
+
+\START
+\AUTHOR{Phelype Oleinik}
+
+\ExplSyntaxOn
+\OMIT
+\use:n
+  { % Allocation
+    \cctab_begin:N \c_code_cctab
+      \cctab_begin:N \c_code_cctab
+      \cctab_end:
+    \cctab_end:
+    { \cctab_select:N \c_code_cctab }
+    { { \cctab_select:N \c_code_cctab } }
+  }
+\TIMO
+
+\TEST { \cctab_end:~higher~than~\cctab_begin:N }
+  {
+    \cctab_begin:N \c_code_cctab
+    \group_begin:
+      \cctab_end:
+    \group_end:
+    %
+    \cctab_begin:N \c_code_cctab
+      \cctab_begin:N \c_code_cctab
+      \group_begin:
+        \cctab_end:
+      \group_end:
+    \cctab_end:
+  }
+
+\TEST { \cctab_end:~lower~than~\cctab_begin:N }
+  {
+    \group_begin:
+      \cctab_begin:N \c_code_cctab
+    \group_end:
+    \cctab_end:
+    %
+    \cctab_begin:N \c_code_cctab
+      \group_begin:
+        \cctab_begin:N \c_code_cctab
+      \group_end:
+      \cctab_end:
+    \cctab_end:
+  }
+
+\TEST { Group~end~and~begin~between~\cctab_begin:N~and~\cctab_end: }
+  {
+    \group_begin:
+      \cctab_begin:N \c_code_cctab
+    \group_end:
+    \group_begin:
+      \cctab_end:
+    \group_end:
+    %
+    \cctab_begin:N \c_code_cctab
+      \group_begin:
+        \cctab_begin:N \c_code_cctab
+      \group_end:
+      \group_begin:
+        \cctab_end:
+      \group_end:
+    \cctab_end:
+  }
+
+\ExplSyntaxOff
+
+\END
diff --git a/l3kernel/testfiles/m3cctab003.tlg b/l3kernel/testfiles/m3cctab003.tlg
new file mode 100644
index 000000000..a08e45206
--- /dev/null
+++ b/l3kernel/testfiles/m3cctab003.tlg
@@ -0,0 +1,57 @@
+This is a generated file for the LaTeX (2e + expl3) validation system.
+Don't change this file in any respect.
+Author: Phelype Oleinik
+============================================================
+TEST 1: \cctab_end: higher than \cctab_begin:N 
+============================================================
+! LaTeX3 Error: \cctab_end: occurred in a higher group level than the matching
+(LaTeX3)        \cctab_begin:N.
+For immediate help type H <return>.
+ ...                                              
+l. ...  }
+Catcode tables and groups must be properly nested, but you tried to interleave
+them. LaTeX will try to proceed, but results may be unexpected.
+! LaTeX3 Error: \cctab_end: occurred in a higher group level than the matching
+(LaTeX3)        \cctab_begin:N.
+For immediate help type H <return>.
+ ...                                              
+l. ...  }
+Catcode tables and groups must be properly nested, but you tried to interleave
+them. LaTeX will try to proceed, but results may be unexpected.
+============================================================
+============================================================
+TEST 2: \cctab_end: lower than \cctab_begin:N 
+============================================================
+! LaTeX3 Error: \cctab_end: occurred in a lower group level than the matching
+(LaTeX3)        \cctab_begin:N.
+For immediate help type H <return>.
+ ...                                              
+l. ...  }
+Catcode tables and groups must be properly nested, but you tried to interleave
+them. LaTeX will try to proceed, but results may be unexpected.
+! LaTeX3 Error: \cctab_end: occurred in a lower group level than the matching
+(LaTeX3)        \cctab_begin:N.
+For immediate help type H <return>.
+ ...                                              
+l. ...  }
+Catcode tables and groups must be properly nested, but you tried to interleave
+them. LaTeX will try to proceed, but results may be unexpected.
+============================================================
+============================================================
+TEST 3: Group end and begin between \cctab_begin:N and \cctab_end: 
+============================================================
+! LaTeX3 Error: \cctab_end: occurred in a different group than the matching
+(LaTeX3)        \cctab_begin:N.
+For immediate help type H <return>.
+ ...                                              
+l. ...  }
+Catcode tables and groups must be properly nested, but you tried to interleave
+them. LaTeX will try to proceed, but results may be unexpected.
+! LaTeX3 Error: \cctab_end: occurred in a different group than the matching
+(LaTeX3)        \cctab_begin:N.
+For immediate help type H <return>.
+ ...                                              
+l. ...  }
+Catcode tables and groups must be properly nested, but you tried to interleave
+them. LaTeX will try to proceed, but results may be unexpected.
+============================================================





More information about the latex3-commits mailing list.