Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://angg.twu.net/LATEX/2021dn6-error-handling.lua.html
--   http://angg.twu.net/LATEX/2021dn6-error-handling.lua
--           (find-angg "LATEX/2021dn6-error-handling.lua")
--                (find-LATEX "2021dn6-error-handling.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- See: (find-LATEX "2020dn6-error-handling.lua")

-- «.getword»		(to "getword")
-- «.arrow-modifiers»	(to "arrow-modifiers")
-- «.dxyrun»		(to "dxyrun")




-- «getword»  (to ".getword")
-- (find-dn6 "parse.lua" "getword")
-- (find-dn6 "parse.lua" "getword" "getwordasluaexpr =")
--
-- These are higher-level variants of getword() that accept error
-- messages. Mnemonic: they have "_"s in their names, and
-- "getword_lua" is more readable than "getwordlua".

diagword1 = function (funname, errmsg)
    errmsg = errmsg or "argument"
    return getword() or diagerror(format("In '%s': missing %s", funname, errmsg))
  end
diagword2 = function (funname, errmsg1, errmsg2)
    local a = diagword1(funname, errmsg1 or "first argument")
    local b = diagword2(funname, errmsg2 or "second argument")
    return a,b
  end
diagwordlua = function (funname, errmsg)
    local w = diagword1(funname, errmsg)
    return expr(w)
  end
diagwordlualua = function (funname, errmsg1, errmsg2)
    local w1,w2 = diagword2(funname, errmsg)
    return expr(w1), expr(w2)
  end


-- «arrow-modifiers»  (to ".arrow-modifiers")
-- (find-dn6 "diagforth.lua" "arrow-modifiers")

forths[".plabel="] = function ()
    local p, label = diagword2(".plabel=", "first argument (placement)",
                                           "second argument (label)")
    ds:pick(0).placement, ds:pick(0).label = p, label
  end
forths[".slide="] = function ()
    ds:pick(0).slide = diagword1(".slide=")
  end
forths[".curve="] = function ()
    ds:pick(0).curve = diagword1(".curve=")
  end

forths["xy+="] = function ()
    local dx,dy = diagwordlualua(".plabel=", "first argument (dx)",
                                             "second argument (dy)")
    ds:pick(0).x = ds:pick(0).x + dx
    ds:pick(0).y = ds:pick(0).y + dy
  end


-- «dxyrun»  (to ".dxyrun")
-- (find-dn6 "diagforth.lua" "diag-head" "dxyrun =")
-- (find-dn6file "parse.lua" "setsubj =")
-- (find-es "lua5" "xpcall-2020")

-- It should be possible to use dxyrun0 recursively (for macros!) and
-- it should be possible to call it using xpcall and display the
-- offending line before the traceback...

-- (find-dn6 "heads6.lua" "diag-head")
-- (find-es "lua5" "xpcall-2020")
-- dxyrun = function (str, pos, linenum)
--     dxyrun0(str, pos, linenum)
--   end

dxyrun0 = function (str, pos)
    local a,b,c,d = subj,pos,startcol,endcol -- backup globals
    setsubj(str, pos or 1)
    while getword() do
      -- PP(word)
      if    forths[word] then forths[word]()
      elseif nodes[word] then ds:push(nodes[word])
      else Error("Unknown word: "..word)
      end
    end
    local subj,pos,startcol,endcol = a,b,c,d -- restore globals
  end

dxyrun_errhandler = function (errmsg)
    local fname = tf.name
    local b = lastheadblock
    local blockinfo = format("%s-block in lines %d-%d", b.head, b.i, b.j)
    local where = format("In %s, in the %s, in line %d",
			 fname, blockinfo, dxyrun_linenum)
    local whereinsubj = (" "):rep(startcol - 1)..("^"):rep(endcol - startcol)
    local errmsg0 = errmsg:gsub("^[^ ]* ", "")
    print(errmsg0)
    print(where..":")
    print()
    print(subj)
    print(whereinsubj)
    -- print(debug.traceback())
    print()
    print("Dednat6 error - aborting!")
  end

-- TODO: rewrite dxyrun using the MyXpcall class.
-- (find-angg "edrxrepl/edrxrepl.lua" "MyXpcall-class")
-- (find-angg "LUA/lua50init.lua" "MyXpcall")

dxyrun = function (str, pos, linenum)
    dxyrun_linenum = linenum
    local f = function () dxyrun0(str, pos) end
    local status = xpcall(f, dxyrun_errhandler)
    if status == false then os.exit(1) end
  end




--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
loaddednat6()
dofile "2021dn6-error-handling.lua"

--]]



-- Local Variables:
-- coding: utf-8-unix
-- End: