From 5184be2519a2170f95c811c132ffc61a7f8d6a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Star=C3=BD=20Novotn=C3=BD?= Date: Mon, 19 Aug 2024 13:04:45 +0200 Subject: [PATCH] Simplify function `depth_first_search()` As discussed in and below. Co-authored-by: Tim Taubitz --- markdown.dtx | 47 ++++++++++++++--------------------------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/markdown.dtx b/markdown.dtx index 941f0471..e2a28547 100644 --- a/markdown.dtx +++ b/markdown.dtx @@ -26671,33 +26671,16 @@ parsers.ascii_punctuation = S("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~") % % \end{markdown} % \begin{macrocode} - local function depth_first_search(root, visit, leave) - local to_visit = {root} - local paths = {""} - local visited = {} - while #to_visit > 0 do - local node = to_visit[#to_visit] - local path = paths[#paths] - if visited[node] == nil then - visit(node, path) - visited[node] = true - if type(node) == "table" then - for label, child in pairs(node) do - local child_path = path - if type(label) == "string" then - assert(#label == 1) - child_path = child_path .. label - end - table.insert(to_visit, child) - table.insert(paths, child_path) - end - end + local function depth_first_search(node, path, visit, leave) + visit(node, path) + for label, child in pairs(node) do + if type(child) == "table" then + depth_first_search(child, path .. label, visit, leave) else - leave(node, path) - table.remove(to_visit) - table.remove(paths) + visit(child, path) end end + leave(node, path) end parsers.punctuation = {} @@ -26711,15 +26694,13 @@ parsers.ascii_punctuation = S("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~") subparsers[path] = subparsers[path] + S(node) end end, function(node, path) - if type(node) == "table" then - if #path > 0 then - local byte = path:sub(#path, #path) - local parent_path = path:sub(1, #path-1) - subparsers[parent_path] = subparsers[parent_path] - + S(byte) * subparsers[path] - else - parsers.punctuation[length] = subparsers[path] - end + if #path > 0 then + local byte = path:sub(#path, #path) + local parent_path = path:sub(1, #path-1) + subparsers[parent_path] = subparsers[parent_path] + + S(byte) * subparsers[path] + else + parsers.punctuation[length] = subparsers[path] end end) assert(parsers.punctuation[length] ~= nil)