DKL9 GitList
Repositories
DKL9 home
memoire
Code
Commits
Branches
Tags
Search
Tree:
35a7f9b
Branches
Tags
master
memoire
recparse.lua
Initial commit
John Smith
commited
35a7f9b
at 2021-309 22:15:30
recparse.lua
Blame
History
Raw
--[[ a Recfile is (to simplify) a sequence of records a record is a sequence of fields a field is a label-value pair a label is an identifier a value is a string ]] --[[ maps text from a Recfile into a sequence of records returns (on success) a table of tables of tables of two strings (label and value) returns (on failure) nil ]] local function text2recs(str) local ret = {} local cr = {} local cl = nil local cv = nil -- Rec is line-based for line in str:gmatch "([^\n]*)\n" do -- continuation lines if cv and cv:sub(-1) == "\\" then cv = cv:sub(1, -2) .. line elseif line:sub(1, 1) == "+" then cv = cv .. "\n" .. line:gsub("^%+ ?", "") -- blank lines separate records elseif line:match "^%s*$" then -- add field to record if non-empty if cl then table.insert(cr, { k = cl, v = cv }) end cl = nil cv = nil -- add record to return value if non-empty if next(cr) then table.insert(ret, cr) end cr = {} -- lines starting with # are comments elseif line:sub(1, 1) == "#" then if cl then table.insert(cr, { k = cl, v = cv }) end cl = nil cv = nil -- new field else if cl then table.insert(cr, { k = cl, v = cv }) end cl, cv = line:match "^([%a%%][%w_]*):%s(.*)" -- no match? bad field if not cl then return nil end end end if cl then table.insert(cr, { k = cl, v = cv }) end if next(cr) then table.insert(ret, cr) end return ret end return text2recs