|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- process.lua: process files, lines, and heads.
-- all the lines in a file (by heads)
-- This file:
-- http://angg.twu.net/dednat5/process.lua.html
-- http://angg.twu.net/dednat5/process.lua
-- (find-dn5 "process.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
-- Version: 2011apr09
-- License: GPL3
-- untabify (find-angg "LUA/lua50init.lua" "untabify")
-- parse_pattern (find-blogme4 "eval.lua" "parse_pattern")
-- ProcessLine (find-dn4 "dednat4.lua" "abbrev-head")
-- ProcessBlock (find-dn4 "dednat4.lua" "lua-head")
-- ProcessFile (find-dn4 "dednat4.lua" "process")
-- ProcessWord uses subj and pos; used by trees and dforth
-- Head
-- heads (find-dn4 "dednat4.lua" "heads")
-- registerhead
-- AbbrevHead
-- LuaHead
-- «.registerhead» (to "registerhead")
-- «.main-loop» (to "main-loop")
-- «.abbrev-head» (to "abbrev-head")
-- «.lua-head» (to "lua-head")
require "prefixes" -- (find-dn5 "prefixes.lua")
-- «registerhead» (to ".registerhead")
-- (find-dn4 "dednat4.lua" "heads")
heads = {}
registerhead = function (headstr)
return function (head)
head.headstr = headstr
addabbrev(headstr, head, heads)
end
end
registerhead "" {}
headstrfor_ = function (lstr) return longestprefix(lstr, 1, heads) or "" end
headfor_ = function (lstr) return heads[headstrfor_(lstr)] end
headstrfor = function (lstr) return lstr and headstrfor_(lstr) end
headfor = function (lstr) return lstr and headfor_(lstr) end
--
fname = "<none>" -- used in error messages
flines = {} -- like "subj", but is an array of strings
nline = 1 -- like "pos"
--
linehead = function (n) return headfor (flines[n or nline]) end
lineheadstr = function (n) return headstrfor(flines[n or nline]) end
nextheadstr = function () return lineheadstr(nline + 1) end
--
set_nline = function (nline_) nline = nline_; linestr = flines[nline] end
set_flines = function (flines_, fname_)
fname = fname_ or "<none>"
flines = flines_
allsegments = {} -- (find-dn5 "segments.lua")
set_nline(1)
end
use_bigstr = function (bigstr, fname) set_flines(splitlines(bigstr), fname) end
use_fname = function (fname) use_bigstr(readfile(fname), fname) end
-- «main-loop» (to ".main-loop")
processlines = function ()
while nline <= #flines do
local head = linehead()
if head.action then head.action() end
set_nline(nline + 1)
end
end
process_bigstr = function (bstr, fn) use_bigstr(bstr, fn) processlines() end
process_fname = function (fname) use_fname(fname) processlines() end
-- Two trivial heads:
-- «abbrev-head» (to ".abbrev-head")
-- (find-dn4 "dednat4.lua" "abbrev-head")
-- (find-dn5 "prefixes.lua")
registerhead "%:*" {
action = function ()
local abbrev, expansion = linestr:match("^%%:*(.-)*(.-)*")
assert(abbrev)
addabbrev(abbrev, expansion)
end,
}
-- «lua-head» (to ".lua-head")
-- (find-dn4 "dednat4.lua" "lua-head")
lualinebody = function () return untabify(linestr):match("^%%L ?(.*)") end
registerhead "%L" {
action = function ()
local chunkname = fname..":%L:"..nline
local lualines = { lualinebody() } -- get body of first line
while nextheadstr() == "%L" do -- when the next line is also %L
set_nline(nline + 1) -- advance pointer
table.insert(lualines, lualinebody()) -- add its body to the chunk
end
local luacode = table.concat(lualines, "\n")
assert(loadstring(luacode, chunkname))()
end,
}
-- dump-to: tests
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
ee_dofile "process.lua"
ee_dofile "diags.lua"
ee_dofile "forth.lua"
foo [[
%L PP(abbrevs)
%:!a!<a>!
%L PP(abbrevs)
%L print("hlo"
%L .."bye")
]]
linestr = "%L print('foo')"
--]==]
-- (find-dn4 "dednat4.lua" "processfile")
-- dodemo = function (bigstr)
-- bigstr = bigstr:gsub("!", "*")
-- fname = "demo"
-- set_flines(bigstr) -- now nline = 1
-- processlines()
-- end
foo_ = function (bigstr) process_bigstr(bigstr, "<foo>") end
foo = function (bigstr) foo_(bigstr:gsub("!", "*")) end
-- dump-to: tests
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
ee_dofile "process.lua"
foo [[
%L PP(abbrevs)
%:!a!<a>!
%L PP(abbrevs)
%L print("hlo"
%L .."bye")
]]
linestr = "%L print('foo')"
--]==]
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
ee_dofile "process.lua"
ee_dofile "trees.lua"
foo [[
%L print("hello")
]]
foo [[
%:!a!<a>!
%:!abc!<abc>!
]]
PP(abbrevs)
foo [[
%: 1 2 3
%: =======
%: 1+2+3
%: --------app
%: f(1+2+3)
%:
%: ^f(1+2+3)
]]
--]==]
-- Local Variables:
-- coding: raw-text-unix
-- ee-anchor-format: "«%s»"
-- End: