|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://angg.twu.net/LUA/EScript.lua.html
-- http://angg.twu.net/LUA/EScript.lua
-- (find-angg "LUA/EScript.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- Parse e-scripts.
--
-- (defun e () (interactive) (find-angg "LUA/EScript.lua"))
-- (find-es "lpeg" "globals")
require "lpeg"
B,C,P,R,S,V = lpeg.B,lpeg.C,lpeg.P,lpeg.R,lpeg.S,lpeg.V
Cb,Cc,Cf,Cg = lpeg.Cb,lpeg.Cc,lpeg.Cf,lpeg.Cg
Cp,Cs,Ct = lpeg.Cp,lpeg.Cs,lpeg.Ct
Carg,Cmt = lpeg.Carg,lpeg.Cmt
-- (find-es "lpeg" "lpeg-quickref")
p_tagchar = R"!~" - S'"'
p_tag = p_tagchar^1
p_anchor = P"«" * p_tag:C() * P"»"
p_to = P'(to "' * p_tag:C() * '")'
p_anchorto = R" ~"^0 * p_anchor * S" \t"^0 * p_to
p_headermidline = P'# ' * ((1-P"\n")^0):C() * P'\n'
p_header = P'#####\n#\n' * p_headermidline * p_headermidline * P"#\n#####\n\n"
p_headeranchorto = p_header * p_anchorto
Lines = Class {
type = "Lines",
from = function (bigstr)
local n,line,ltob,btol = 0, {}, {}, {}
for pos,li in bigstr:gmatch("()([^\n]-)\n") do
n = n + 1
line[n] = li
ltob[n] = pos
btol[pos] = n
end
return Lines {bigstr=bigstr, n=n, line=line, ltob=ltob, btol=btol}
end,
read = function (fname)
local lis = Lines.from(ee_readfile(fname))
lis.fname = fname
return lis
end,
__tostring = function (lis)
return format("Lines object with %d lines", lis.n)
end,
__index = {
fline = function (lis, linen)
return format('(find-fline "%s" %d)', lis.fname, linen)
end,
lmatch = function (lis, linen, lpegpat)
return lpegpat:match(lis.bigstr, lis.ltob[linen])
end,
isindex = function (lis, linen)
local dtag,tag = lis:lmatch(linen, p_anchorto)
if dtag and dtag == "."..tag then return tag end
end,
isheader = function (lis, linen)
local title,date,tag,dtag = lis:lmatch(linen, p_headeranchorto)
if title and dtag == "."..tag then return tag,title,date end
end,
--
error_index_twice = function (lis, tag, line1, line2)
local firstline = format('The tag "%s" appears twice in the index:', tag)
local link1 = lis:fline(line1)
local link2 = lis:fline(line2)
return firstline .."\n".. link1 .."\n".. link2
end,
error_block_twice = function (lis, tag, line1, line2)
local firstline = format('The tag "%s" appears twice in blocks:', tag)
local link1 = lis:fline(line1)
local link2 = lis:fline(line2)
return firstline .."\n".. link1 .."\n".. link2
end,
getindex = function (lis)
local index, n = {}, 0
for linen=1,lis.n do
local tag = lis:isindex(linen)
if tag then
if index[tag] then
print(lis:error_index_twice(tag, index[tag].linen, linen))
end
n = n + 1
local A = {n=n, linen=linen, tag=tag}
index[tag] = A
index[n] = A
end
end
return index
end,
getblocks = function (lis)
local blocks = {}
for linen=1,lis.n do
local tag,title,date = lis:isheader(linen)
if tag then
if blocks[tag] then
print(lis:error_block_twice(tag, blocks[tag].linen, linen))
end
local n = #blocks + 1
local block = {n=n, linen=linen, tag=tag, title=title, date=date}
blocks[tag] = block
blocks[n] = block
end
end
return blocks
end,
},
}
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "EScript.lua"
-- (find-es "lpeg")
lis = Lines.read "~/e/lpeg.e"
lis = Lines.read "~/e/lua5.e"
lis = Lines.read "~/e/maxima.e"
= lis
= lis.n
= lis:fline(274)
lis:getindex()
lis:getblocks()
PPPV(lis:getindex())
PPPV(lis:getblocks())
--]]
EScript = Class {
type = "EScript",
from = function (fname)
local bigstr = ee_readfile(fname)
return EScript {fname=fname, bigstr=bigstr}
end,
__tostring = function (es) return "fname: "..es.fname end,
__index = {
},
}
-- Local Variables:
-- coding: utf-8-unix
-- End: