blob: 694cb93fc0e8765d5a6b73193de43fae6a1e5ca4 (
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
(.require
[library
[lux (.except parameter)
[abstract
[monad (.only do)]]
[control
["[0]" try (.only Try)]]
[data
["[0]" text (.use "[1]#[0]" equivalence)
["%" \\format (.only format)]]]]]
["[0]" //])
(type .public (Parser a)
(//.Parser (List Text) a))
(def .public (result parser inputs)
(All (_ a) (-> (Parser a) (List Text) (Try a)))
(case (//.result parser inputs)
{try.#Success [remaining output]}
(case remaining
{.#End}
{try.#Success output}
_
{try.#Failure (format "Remaining CLI inputs: " (text.interposed " " remaining))})
{try.#Failure try}
{try.#Failure try}))
(def .public any
(Parser Text)
(function (_ inputs)
(case inputs
{.#Item arg inputs'}
{try.#Success [inputs' arg]}
_
{try.#Failure "Cannot parse empty arguments."})))
(def .public (parse parser)
(All (_ a) (-> (-> Text (Try a)) (Parser a)))
(function (_ inputs)
(do try.monad
[[remaining raw] (any inputs)
output (parser raw)]
(in [remaining output]))))
(def .public (this reference)
(-> Text (Parser Any))
(function (_ inputs)
(do try.monad
[[remaining raw] (any inputs)]
(if (text#= reference raw)
(in [remaining []])
{try.#Failure (format "Missing token: '" reference "'")}))))
(def .public (somewhere cli)
(All (_ a) (-> (Parser a) (Parser a)))
(function (_ inputs)
(loop (again [immediate inputs])
(case (//.result cli immediate)
{try.#Success [remaining output]}
{try.#Success [remaining output]}
{try.#Failure try}
(case immediate
{.#End}
{try.#Failure try}
{.#Item to_omit immediate'}
(do try.monad
[[remaining output] (again immediate')]
(in [{.#Item to_omit remaining}
output])))))))
(def .public end
(Parser Any)
(function (_ inputs)
(case inputs
{.#End} {try.#Success [inputs []]}
_ {try.#Failure (format "Unknown parameters: " (text.interposed " " inputs))})))
(def .public (named name value)
(All (_ a) (-> Text (Parser a) (Parser a)))
(|> value
(//.after (..this name))
..somewhere))
(def .public (parameter [short long] value)
(All (_ a) (-> [Text Text] (Parser a) (Parser a)))
(|> value
(//.after (//.either (..this short) (..this long)))
..somewhere))
|