Warning: this is an htmlized version!
The original is across this link,
and the conversion rules are here.
-- treesegs.lua:
-- This file:
--   http://angg.twu.net/dednat5/treesegs.lua.html
--   http://angg.twu.net/dednat5/treesegs.lua
--                    (find-dn5 "treesegs.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
-- Version: 2011mar28
-- License: GPL3
--

-- «.allsegments-test»	(to "allsegments-test")

require "eoo"    -- (find-dn5 "eoo.lua")
require "parse"  -- (find-dn5 "parse.lua")

intersects = function (start1, end1, start2, end2)
    if end1 <= start2 then return false end
    if end2 <= start1 then return false end
    return true
  end

allsegments = {}
-- allsegment[5] is a Segments object containing the list of all
-- Segment objects at line 5 of the current file.

Segment = Class {
  type    = "Segment",
  __index = {
    iswithin = function (seg, l, r)
        return intersects(seg.l, seg.r, l, r)
      end,
    intersects = function (seg1, seg2)
        return intersects(seg1.l, seg1.r, seg2.l, seg2.r)
      end,
    segsabove_ = function (seg, dy)
        return allsegments[seg.y - dy] or Segments {}
      end,
    segsabove = function (seg)
        return seg:segsabove_(1):allintersecting(seg)
      end,
    firstsegabove = function (seg) return seg:segsabove()[1] end,
    rootnode = function (seg)
        return seg:segsabove_(2):firstwithin(seg.l, seg.l + 1)
      end,
  },
}
Segments = Class {
  type    = "Segments",
  __index = {
    allwithin = function (segs, l, r)
        local T = {}
        for _,seg in ipairs(segs) do
          if seg:iswithin(l, r) then table.insert(T, seg) end
        end
        return T
      end,
    firstwithin = function (segs, l, r)
        return segs:allwithin(l, r)[1]
      end,
    allintersecting = function (segs, seg)
        return segs:allwithin(seg.l, seg.r)
      end,
    firstintersecting = function (segs, seg)
        return segs:allwithin(seg.l, seg.r)[1]
      end,
  },
}


-- (find-dn5 "parse.lua")
tosegments = function (str, line)
    local T = {}
    setsubj(untabify(str))
    while getword() do
      table.insert(T, Segment {l=startcol, r=endcol, t=word, y=line, i=#T+1})
    end
    return Segments(T)
  end



-- dump-to: tests
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
require "treesegs"
foo = function (str)
    allsegments[y] = tosegments(str, y)
    y = y + 1
  end
y = 4
foo "%:  a  b   "
foo "%:  ----?  "
foo "%:    c    "   
foo "%:         "
foo "%:    ^t   "

PP(allsegments)
PP(allsegments[5])
PP(allsegments[5][1])
PP(allsegments[5][1]:segsabove())
PP(allsegments[8][1])
PP(allsegments[8][1]:rootnode())
PP(allsegments[4][1]:segsabove())
PP(allsegments[4][1]:firstsegabove())

require "trees"    -- (find-dn5 "trees.lua")
PP   (allsegments[6][1]:totreenode())
print(allsegments[6][1]:totreenode():TeX_subtree("  "))
print(allsegments[6][1]:totreenode():TeX_deftree("t"))


-- «allsegments-test»  (to ".allsegments-test")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
require "trees"    -- (find-dn5 "trees.lua")
foo = function (str)
    allsegments[y] = tosegments(str, y)
    y = y + 1
  end
allsegments = {}
y = 2
foo "%:    1  2  3  "
foo "%:    =======  "
foo "%: f   1+2+3   "
foo "%: ---------app"
foo "%:  f(1+2+3)   "
foo "%:             "
foo "%:  ^f(1+2+3)  "
PP(allsegments)
-- {2={1={"i"=1, "l"=7, "r"=8, "t"="1", "y"=2},
--     2={"i"=2, "l"=10, "r"=11, "t"="2", "y"=2},
--     3={"i"=3, "l"=13, "r"=14, "t"="3", "y"=2}},
--  3={1={"i"=1, "l"=7, "r"=14, "t"="=======", "y"=3}},
--  4={1={"i"=1, "l"=4, "r"=5, "t"="f", "y"=4},
--     2={"i"=2, "l"=8, "r"=13, "t"="1+2+3", "y"=4}},
--  5={1={"i"=1, "l"=4, "r"=16, "t"="---------app", "y"=5}},
--  6={1={"i"=1, "l"=5, "r"=13, "t"="f(1+2+3)", "y"=6}},
--  7={},
--  8={1={"i"=1, "l"=5, "r"=14, "t"="^f(1+2+3)", "y"=8}},
-- }
PP(allsegments[6][1])
PP(allsegments[6][1]:totreenode())
print(allsegments[6][1]:totreenode():TeX_deftree("f(1+2+3)"))





--]==]

-- Local Variables:
-- coding:             raw-text-unix
-- ee-anchor-format:   "«%s»"
-- End: