summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2018-06-23 14:51:03 +0200
committerPhilipp Gesang <phg@phi-gamma.net>2018-06-23 14:51:03 +0200
commit0b9144c3ca46c1138f37db3de4b1698818178085 (patch)
tree4258186822c3d0360b42691415dcc529b5d1a59e
downloadcaldr-0b9144c3ca46c1138f37db3de4b1698818178085.tar.gz
initial: basic line parser
-rwxr-xr-xcal.lua64
1 files changed, 64 insertions, 0 deletions
diff --git a/cal.lua b/cal.lua
new file mode 100755
index 0000000..7fcbbed
--- /dev/null
+++ b/cal.lua
@@ -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)
+