-
Notifications
You must be signed in to change notification settings - Fork 2
/
gopher-index.lua
49 lines (45 loc) · 2.1 KB
/
gopher-index.lua
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
-- ************************************************************************
--
-- Parse a gopher index file.
-- Copyright 2018 by Sean Conner. All Rights Reserved.
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
--
-- Comments, questions and criticisms can be sent to: sean@conman.org
--
-- ************************************************************************
-- luacheck: ignore 611
local abnf = require "org.conman.parsers.abnf"
local types = require "org.conman.const.gopher-types"
local control = require "org.conman.parsers.utf8.control"
+ require "org.conman.parsers.iso.control"
+ require "org.conman.parsers.ascii.control"
local lpeg = require "lpeg"
local Cc = lpeg.Cc
local Cg = lpeg.Cg
local Cs = lpeg.Cs
local Ct = lpeg.Ct
local P = lpeg.P
local R = lpeg.R
local text = Cs((#-(abnf.CRLF + abnf.HTAB) * control / "" + R" \255")^0)
local type = Cg(R" ~" / types,'type')
local display = Cg(text,'display') + Cg(Cc"",'display')
local selector = abnf.HTAB * Cg(R" \255"^0,'selector') + Cg(Cc"",'selector')
local host = abnf.HTAB * Cg(R" \255"^0,'host') + Cg(Cc"example.com",'host')
local port = abnf.HTAB * Cg(R"09"^1 / tonumber + Cc(0),'port') + Cg(Cc(0),'port')
local gplus = abnf.HTAB * Cg(R" \255"^0,'gplus')
local line = (P"." * abnf.CRLF) * Cc(nil)
+ abnf.CRLF * Cc(nil)
+ Ct(type * display * selector * host * port * gplus^-1) * (abnf.CRLF + P(-1))
return Ct(line^1)