diff options
-rwxr-xr-x | cal.lua | 64 |
1 files changed, 64 insertions, 0 deletions
@@ -0,0 +1,64 @@ +#!/usr/bin/env lua + +local io = require "io" +local ioopen = io.open + +local string = require "string" +local stringformat = string.format + +local println = function (...) print (stringformat (...)) end + +local parse_calendar do + local lpeg = require "lpeg" + local lpegmatch = lpeg.match + local P = lpeg.P + local C = lpeg.C + local Cp = lpeg.Cp + + local p_cr = P"\r" + local p_lf = P"\n" + local p_eol = p_cr * p_lf + p_lf + local p_noeol = P(1) - p_eol + + local p_content_line = C (p_noeol^1) * p_eol * Cp () + + local parse_content_line = function (raw, pos0) + local res, pos1 = lpegmatch (p_content_line, raw, pos0) + + return res, pos1 + end + + parse_calendar = function (raw, pos0, consumed) + if pos0 == nil then return parse_calendar (raw, 1, 0) end + + local cline, pos1 = parse_content_line (raw, pos0) + println ("»»» [%d–%d] [%s]", pos0, pos1, cline) + return parse_calendar (raw, pos1, consumed + pos1 - pos0) + end +end + +local print_calendar do +end + +local loaddata = function (fname) + local fh = ioopen (fname, "r") + + if fh == nil then + error ("could not open file " .. fname .. " for reading") + end + + return fh:read ("*a") +end + +local main = function (argv) + if #argv ~= 1 then + error "pass me a file name, I insist!" + end + + local raw = loaddata (argv [1]) + + return parse_calendar (raw) +end + +return main (arg) + |