[latex3-commits] [git/LaTeX3-latex3-latex3] luacmd: Implement \token_if_primitive:N in Lua (d1e4c5874)

Marcel Fabian Krüger tex at 2krueger.de
Fri Sep 11 22:42:27 CEST 2020


Repository : https://github.com/latex3/latex3
On branch  : luacmd
Link       : https://github.com/latex3/latex3/commit/d1e4c58742febaf88e3bb8c9c1c292b58a8a1071

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

commit d1e4c58742febaf88e3bb8c9c1c292b58a8a1071
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Fri Sep 11 21:12:13 2020 +0200

    Implement \token_if_primitive:N in Lua
    
    Here we include \luadef and related commands as "primitives"


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

d1e4c58742febaf88e3bb8c9c1c292b58a8a1071
 l3kernel/l3.ins          |   3 +-
 l3kernel/l3bootstrap.dtx |   7 ++
 l3kernel/l3luatex.dtx    |   4 +
 l3kernel/l3token.dtx     | 192 ++++++++++++++++++++++++++++++++++-------------
 4 files changed, 151 insertions(+), 55 deletions(-)

diff --git a/l3kernel/l3.ins b/l3kernel/l3.ins
index 874790b95..ad3b5267d 100644
--- a/l3kernel/l3.ins
+++ b/l3kernel/l3.ins
@@ -72,7 +72,7 @@ and all files in that bundle must be distributed together.
         \from{l3prg.dtx}        {package}
         \from{l3sys.dtx}        {package,tex}
         \from{l3clist.dtx}      {package}
-        \from{l3token.dtx}      {package}
+        \from{l3token.dtx}      {package,tex}
         \from{l3prop.dtx}       {package}
         \from{l3msg.dtx}        {package}
         \from{l3file.dtx}       {package}
@@ -169,6 +169,7 @@ and all files in that bundle must be distributed together.
   \from{l3luatex.dtx}{package,lua}
   \from{l3names.dtx}{package,lua}
   \from{l3sys.dtx}{package,lua}
+  \from{l3token.dtx}{package,lua}
 }}
 
 \endbatchfile
diff --git a/l3kernel/l3bootstrap.dtx b/l3kernel/l3bootstrap.dtx
index 41631ada1..3ead7a107 100644
--- a/l3kernel/l3bootstrap.dtx
+++ b/l3kernel/l3bootstrap.dtx
@@ -181,11 +181,18 @@
 %    \end{macrocode}
 %   For \LuaTeX{} we make sure the basic support is loaded:
 %   this is only necessary in plain.
+%
+%   Additionally we just ensure that \TeX{} has seen the csnames \cs{prg_return_true:N}
+%   and \cs{prg_return_false:N} before the Lua code builds these tokens.
 %    \begin{macrocode}
     \begingroup\expandafter\expandafter\expandafter\endgroup
     \expandafter\ifx\csname newcatcodetable\endcsname\relax
       \input{ltluatex}%
     \fi
+    \begingroup\edef\ignored{%
+      \expandafter\noexpand\csname prg_return_true:N\endcsname
+      \expandafter\noexpand\csname prg_return_false:N\endcsname
+    }\endgroup
     \directlua{require("expl3")}%
 %    \end{macrocode}
 %   As the user might be making a custom format, no assumption is made about
diff --git a/l3kernel/l3luatex.dtx b/l3kernel/l3luatex.dtx
index c02e6a3ca..51696fd61 100644
--- a/l3kernel/l3luatex.dtx
+++ b/l3kernel/l3luatex.dtx
@@ -369,6 +369,10 @@ local utf8_char  = utf8.char
 local scan_int     = token.scan_int or token.scan_integer
 local scan_string  = token.scan_string
 local scan_keyword = token.scan_keyword
+local put_next     = token.put_next
+
+local true_tok     = token.create'prg_return_true:'
+local false_tok    = token.create'prg_return_false:'
 %    \end{macrocode}
 %
 %    \begin{macrocode}
diff --git a/l3kernel/l3token.dtx b/l3kernel/l3token.dtx
index 56e2b6720..8fa2f84b2 100644
--- a/l3kernel/l3token.dtx
+++ b/l3kernel/l3token.dtx
@@ -1112,6 +1112,10 @@
 %    \end{macrocode}
 %
 %    \begin{macrocode}
+%<*tex>
+%    \end{macrocode}
+%
+%    \begin{macrocode}
 %<@@=char>
 %    \end{macrocode}
 %
@@ -2358,7 +2362,8 @@
 %     \@@_if_primitive_nullfont:N,
 %     \@@_if_primitive_loop:N,
 %     \@@_if_primitive:Nw,
-%     \@@_if_primitive_undefined:N}
+%     \@@_if_primitive_undefined:N,
+%     \@@_if_primitive_lua:N}
 %^^A See http://groups.google.com/group/comp.text.tex/browse_thread/thread/0a72666873f8753d#
 %
 %   We filter out macros first, because they cause endless trouble later
@@ -2403,67 +2408,142 @@
 %   \cs{tex_undefined:D}, and if it is |"| or a digit, then the token
 %   is not a primitive.
 %
+%   For \LuaTeX{} we use a different implementation which just looks at the
+%   command code for the token and compaes it to a list of non-primitives.
+%   Again, \tn{nullfont} is a special case because it is the only primitive
+%   with the normally non-primitive |set_font| command code.
+%
 %    \begin{macrocode}
-\tex_chardef:D \c_@@_A_int = `A ~ %
-\use:x
+\sys_if_engine_luatex:TF
   {
-    \prg_new_conditional:Npnn \exp_not:N \token_if_primitive:N ##1
-      { p , T , F , TF }
+%</tex>
+%<*lua>
+do
+  local get_next = token.get_next
+  local get_command = token.get_command
+  local get_index = token.get_index
+  local get_mode = token.get_mode or token.get_index
+  local cmd = token.command_id
+  local set_font = cmd'get_font'
+  local biggest_char = token.biggest_char()
+
+  local mode_below_biggest_char = {}
+  local index_not_nil = {}
+  local mode_not_null = {}
+  local non_primitive = {
+    [cmd'left_brace'] = true,
+    [cmd'right_brace'] = true,
+    [cmd'math_shift'] = true,
+    [cmd'mac_param'] = mode_below_biggest_char,
+    [cmd'sup_mark'] = true,
+    [cmd'sub_mark'] = true,
+    [cmd'endv'] = true,
+    [cmd'spacer'] = true,
+    [cmd'letter'] = true,
+    [cmd'other_char'] = true,
+    [cmd'tab_mark'] = mode_below_biggest_char,
+    [cmd'char_given'] = true,
+    [cmd'math_given'] = true,
+    [cmd'xmath_given'] = true,
+    [cmd'set_font'] = mode_not_null,
+    [cmd'undefined_cs'] = true,
+    [cmd'call'] = true,
+    [cmd'long_call'] = true,
+    [cmd'outer_call'] = true,
+    [cmd'long_outer_call'] = true,
+    [cmd'assign_glue'] = index_not_nil,
+    [cmd'assign_mu_glue'] = index_not_nil,
+    [cmd'assign_toks'] = index_not_nil,
+    [cmd'assign_int'] = index_not_nil,
+    [cmd'assign_attr'] = true,
+    [cmd'assign_dimen'] = index_not_nil,
+  }
+
+  luacmd("@@_if_primitive_lua:N", function()
+    local tok = get_next()
+    local is_non_primitive = non_primitive[get_command(tok)]
+    return put_next(
+           is_non_primitive == true
+             and false_tok
+        or is_non_primitive == nil
+             and true_tok
+        or is_non_primitive == mode_not_null
+             and (get_mode(tok) == 0 and true_tok or false_tok)
+        or is_non_primitive == index_not_nil
+             and (get_index(tok) and false_tok or true_tok)
+        or is_non_primitive == mode_below_biggest_char
+             and (get_mode(tok) > biggest_char and true_tok or false_tok))
+  end, "global")
+end
+%</lua>
+%<*tex>
+    \prg_new_conditional:Npnn \token_if_primitive:N #1 { p , T , F , TF }
       {
-        \exp_not:N \token_if_macro:NTF ##1
-          \exp_not:N \prg_return_false:
-          {
-            \exp_not:N \exp_after:wN \exp_not:N \@@_if_primitive:NNw
-            \exp_not:N \token_to_meaning:N ##1
-              \tl_to_str:n { : : : } \s_@@_stop ##1
-          }
+        \@@_if_primitive_lua:N #1
       }
-    \cs_new:Npn \exp_not:N \@@_if_primitive:NNw
-      ##1##2 ##3 \c_colon_str ##4 \s_@@_stop
+  }
+  {
+    \tex_chardef:D \c_@@_A_int = `A ~ %
+    \use:x
       {
-        \exp_not:N \tl_if_empty:oTF
-          { \exp_not:N \@@_if_primitive_space:w ##3 ~ }
+        \prg_new_conditional:Npnn \exp_not:N \token_if_primitive:N ##1
+          { p , T , F , TF }
+          {
+            \exp_not:N \token_if_macro:NTF ##1
+              \exp_not:N \prg_return_false:
+              {
+                \exp_not:N \exp_after:wN \exp_not:N \@@_if_primitive:NNw
+                \exp_not:N \token_to_meaning:N ##1
+                  \tl_to_str:n { : : : } \s_@@_stop ##1
+              }
+          }
+        \cs_new:Npn \exp_not:N \@@_if_primitive:NNw
+          ##1##2 ##3 \c_colon_str ##4 \s_@@_stop
           {
-            \exp_not:N \@@_if_primitive_loop:N ##3
-              \c_colon_str \s_@@_stop
+            \exp_not:N \tl_if_empty:oTF
+              { \exp_not:N \@@_if_primitive_space:w ##3 ~ }
+              {
+                \exp_not:N \@@_if_primitive_loop:N ##3
+                  \c_colon_str \s_@@_stop
+              }
+              { \exp_not:N \@@_if_primitive_nullfont:N }
           }
-          { \exp_not:N \@@_if_primitive_nullfont:N }
       }
-  }
-\cs_new:Npn \@@_if_primitive_space:w #1 ~ { }
-\cs_new:Npn \@@_if_primitive_nullfont:N #1
-  {
-    \if_meaning:w \tex_nullfont:D #1
-      \prg_return_true:
-    \else:
-      \prg_return_false:
-    \fi:
-  }
-\cs_new:Npn \@@_if_primitive_loop:N #1
-  {
-    \if_int_compare:w `#1 < \c_@@_A_int %
-      \exp_after:wN \@@_if_primitive:Nw
-      \exp_after:wN #1
-    \else:
-      \exp_after:wN \@@_if_primitive_loop:N
-    \fi:
-  }
-\cs_new:Npn \@@_if_primitive:Nw #1 #2 \s_@@_stop
-  {
-    \if:w : #1
-      \exp_after:wN \@@_if_primitive_undefined:N
-    \else:
-      \prg_return_false:
-      \exp_after:wN \use_none:n
-    \fi:
-  }
-\cs_new:Npn \@@_if_primitive_undefined:N #1
-  {
-    \if_cs_exist:N #1
-      \prg_return_true:
-    \else:
-      \prg_return_false:
-    \fi:
+    \cs_new:Npn \@@_if_primitive_space:w #1 ~ { }
+    \cs_new:Npn \@@_if_primitive_nullfont:N #1
+      {
+        \if_meaning:w \tex_nullfont:D #1
+          \prg_return_true:
+        \else:
+          \prg_return_false:
+        \fi:
+      }
+    \cs_new:Npn \@@_if_primitive_loop:N #1
+      {
+        \if_int_compare:w `#1 < \c_@@_A_int %
+          \exp_after:wN \@@_if_primitive:Nw
+          \exp_after:wN #1
+        \else:
+          \exp_after:wN \@@_if_primitive_loop:N
+        \fi:
+      }
+    \cs_new:Npn \@@_if_primitive:Nw #1 #2 \s_@@_stop
+      {
+        \if:w : #1
+          \exp_after:wN \@@_if_primitive_undefined:N
+        \else:
+          \prg_return_false:
+          \exp_after:wN \use_none:n
+        \fi:
+      }
+    \cs_new:Npn \@@_if_primitive_undefined:N #1
+      {
+        \if_cs_exist:N #1
+          \prg_return_true:
+        \else:
+          \prg_return_false:
+        \fi:
+      }
   }
 %    \end{macrocode}
 % \end{macro}
@@ -2858,6 +2938,10 @@
 % \end{macro}
 %
 %    \begin{macrocode}
+%</tex>
+%    \end{macrocode}
+%
+%    \begin{macrocode}
 %</package>
 %    \end{macrocode}
 %





More information about the latex3-commits mailing list.