/
cmt.janet
63 lines (49 loc) · 1.45 KB
/
cmt.janet
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
(import ../margaret/meg :as peg)
# `(cmt patt fun ?tag)`
# Invokes `fun` with all of the captures of `patt` as arguments (if
# `patt` matches).
# If the result is truthy, then captures the result.
# The whole expression fails if `fun` returns false or nil.
(comment
(peg/match ~(cmt (capture 1)
,(fn [cap]
(= cap "a")))
"a")
# =>
@[true]
(peg/match ~(cmt (capture 1)
,(fn [cap]
(= cap "a")))
"b")
# =>
nil
(peg/match ~(cmt (capture "hello")
,(fn [cap]
(string cap "!")))
"hello")
# =>
@["hello!"]
(peg/match ~(cmt (sequence (capture "hello")
(some (set " ,"))
(capture "world"))
,(fn [cap1 cap2]
(string cap2 ": yes, " cap1 "!")))
"hello, world")
# =>
@["world: yes, hello!"]
)
(comment
(peg/match ~{:main :pair
:pair (sequence (cmt (capture :key)
,identity)
"="
(cmt (capture :value)
,identity))
:key (any (sequence (not "=")
1))
:value (any (sequence (not "&")
1))}
"name=tao")
# =>
@["name" "tao"]
)