[latex3-commits] [git/LaTeX3-latex3-latex2e] callback_rules: Better debuging and error recovery for cycle detection (a1babd33)

Marcel Fabian Krüger tex at 2krueger.de
Mon Oct 3 21:08:17 CEST 2022


Repository : https://github.com/latex3/latex2e
On branch  : callback_rules
Link       : https://github.com/latex3/latex2e/commit/a1babd33761e7e63dcd6793da9412b85ca0aea40

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

commit a1babd33761e7e63dcd6793da9412b85ca0aea40
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Sun Sep 4 14:14:22 2022 +0200

    Better debuging and error recovery for cycle detection


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

a1babd33761e7e63dcd6793da9412b85ca0aea40
 base/ltluatex.dtx | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/base/ltluatex.dtx b/base/ltluatex.dtx
index 8be179de..6c66a96b 100644
--- a/base/ltluatex.dtx
+++ b/base/ltluatex.dtx
@@ -1375,6 +1375,7 @@ local callbacklist = setmetatable({}, {
       for i=1, count do -- The actual sort begins
         local current = list[i]
         if current then
+          meta[current.value.description] = nil
           for j, cur in ipairs(current) do
             local count = cur.count
             if count == 1 then
@@ -1388,7 +1389,39 @@ local callbacklist = setmetatable({}, {
         else
           -- Cycle occured. TODO: Show cycle for debugging
           -- list[i] = ...
-          error'Cycle occured'
+          local remaining = {}
+          for name, entry in next, meta do
+            local value = entry.value
+            list[#list + 1] = entry.value
+            remaining[#remaining + 1] = name
+          end
+          table.sort(remaining)
+          local first_name = remaining[1]
+          for j, name in ipairs(remaining) do
+            local entry = meta[name]
+            list[i + j - 1] = entry.value
+            for _, post_entry in ipairs(entry) do
+              local post_name = post_entry.value.description
+              if not remaining[post_name] then
+                remaining[post_name] = name
+              end
+            end
+          end
+          local cycle = {first_name}
+          local index = 1
+          local last_name = first_name
+          repeat
+            cycle[last_name] = index
+            last_name = remaining[last_name]
+            index = index + 1
+            cycle[index] = last_name
+          until cycle[last_name]
+          local length = index - cycle[last_name] + 1
+          table.move(cycle, cycle[last_name], index, 1)
+          for i=2, length//2 do
+            cycle[i], cycle[length + 1 - i] = cycle[length + 1 - i], cycle[i]
+          end
+          error('Cycle occured at ' .. table.concat(cycle, ' -> ', 1, length))
         end
       end
     end





More information about the latex3-commits mailing list.