Skip to content

Commit

Permalink
Simplify function depth_first_search()
Browse files Browse the repository at this point in the history
As discussed in
<#482 (comment)>
and below.

Co-authored-by: Tim Taubitz <[email protected]>
  • Loading branch information
Witiko and Yggdrasil128 committed Aug 19, 2024
1 parent 572d826 commit 5184be2
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions markdown.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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)
Expand Down

0 comments on commit 5184be2

Please sign in to comment.