summaryrefslogtreecommitdiff
path: root/cal.lua
blob: 7fcbbeddbf42ce289805dfe3aefee4a5b020b5ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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)