Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   https://anggtwu.net/LUA/DednatParse1.lua.html
--   https://anggtwu.net/LUA/DednatParse1.lua
--           (find-angg "LUA/DednatParse1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- DednatParse1.lua: functions to parse words keeping track of the column.
-- The functions in this file are used to parse:
--   1. tree segments (in "%:" lines),
--   2. 2D grids (in both "%D 2Dx" and "%D 2D" lines), and
--   3. underbrace diagrams (in experimental modules).
-- Keeping track of the column in UTF8 is tricky, and uses these functions:
--   (find-angg "LUA/lua50init.lua" "strlen8")
--   (find-angg "LUA/lua50init.lua" "untabify8")
-- Supersedes:
--   (find-dn6 "parse.lua")
-- See:
--   (find-angg "LUA/DiagForth1.lua" "DiagForth")
--   (find-angg "LUA/DiagForth1.lua" "DiagForth" "dxyrun =")
--   (find-angg "LUA/DiagForth1.lua" "DiagForth" "dxyrun =" "oldvalues =")
--
-- (defun e () (interactive) (find-angg "LUA/DednatParse1.lua"))
-- (defun o () (interactive) (find-dn6 "parse.lua"))
--
-- «.setsubj»		(to "setsubj")
-- «.getword»		(to "getword")
-- «.getword-tests»	(to "getword-tests")



-- «setsubj»  (to ".setsubj")
-- Based on: (find-dn6 "parse.lua" "setsubj")
setsubj = function (subj_, pos_)
    subj     = subj_
    pos      = pos_ or 3
    startcol = 1
    endcol   = pos
  end

-- «getword»  (to ".getword")
-- Based on: (find-dn6 "parse.lua" "getword")
getword = function ()
    local spaces, word_, newpos = subj:match("( *)([^ ]+)()", pos)
    if spaces then
      startcol = endcol + #spaces
      endcol   = endcol + #spaces + word_:len8()   -- UTF-8
      word     = word_
      pos      = newpos
      return word
    end
  end

getwordasluaexpr = function ()
    local expr = getword()
    local code = "return "..expr
    return assert(loadstring(code))()
  end

getrestofline = function ()
    local spaces, word_, newpos = subj:match("( *)(.*)()", pos)
    PP(spaces, word_, newpos)
    if spaces then
      startcol = endcol + #spaces
      endcol   = endcol + #spaces + word_:len8()   -- UTF-8
      word     = word_
      pos      = newpos
      return word
    end
  end


-- «getword-tests»  (to ".getword-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "DednatParse1.lua"
carets = function ()
    return string.rep(" ", startcol-1) .. string.rep("^", endcol-startcol)
  end
pr = function (o)
    print(subj)
    print(carets())
    PP(startcol, endcol, pos, word, o)
  end
setsubj "%Dfoo b∩r  plic ploc"
pr()
pr(getword())
pr(getword())
pr(getrestofline())

--]]



-- Not yet:
-- (find-dn6 "parse.lua" "getword-with-errmsgs")




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