-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmatch.el
150 lines (126 loc) · 3.51 KB
/
match.el
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(require 'monads)
(require 'utils)
(require 'recur)
(defstruct pdoublet retval rest)
(defun pdoublet (retval rest)
(make-pdoublet :retval retval :rest rest))
(defvar else t)
(defvar otherwise t)
(lex-defun promote-parser (p)
(cond
((funcall (f-or #'numberp #'stringp #'symbolp #'keywordp) p)
(lex-lambda (input)
(if (empty? input) input
(let ((first (car input)))
(if (equal p first) (list (pdoublet p (cdr input)))
nil)))))
(else p)))
(lex-defun promote-parser<p> (p)
(if (functionp p) p
(lambda (v) (promote-parser p))))
(defun these (&rest rest)
(lexical-let ((n (length rest))
(these these)
(rest rest))
(lambda (input)
(let-seq (match-these leftover)
(split-after-n input n)
(if (equal match-these rest)
(list (pdoublet match-these leftover))
nil)))))
(defun parse-list-bind (parser parser<p>)
(lexical-let ((parser (promote-parser parser))
(parser<p> (promote-parser<p> parser<p>)))
(lex-lambda (input)
(if (empty? input) nil
(recur-let
((prl (funcall parser input))
(acc '()))
(if (empty? prl) acc
(recur (cdr prl)
(append acc
(let* ((pdoub (car prl))
(retval (pdoublet-retval pdoub))
(rest (pdoublet-rest pdoub))
(newp (promote-parser (funcall parser<p> retval))))
(funcall newp rest))))))))))
(lex-defun parse-list-return (item)
(lex-lambda (input)
(list (pdoublet item input))))
(defun parse-list-plus (p1 p2)
(lexical-let ((p1 (promote-parser p1))
(p2 (promote-parser p2)))
(lexical-mlet parse-list-monad
((r1 p1)
(r2 p2))
(m-return r2))))
(lex-defun parse-list-zero (input)
nil)
(setq parse-list-monad
(tbl!
:m-return #'parse-list-return
:m-bind #'parse-list-bind
:m-plus #'parse-list-plus
:m-zero #'parse-list-zero))
(funcall (lexical-mlet parse-list-monad
((x 'x)
(y 'y))
(m-return (list x y))) (list 'x 'y))
(lex-defun @or2 (p1 p2)
(lambda (input)
(when input
(let ((r1 (funcall p1 input)))
(if r1 r1
(funcall p2 input))))))
(lex-defun @or (&rest ps)
(reduce #'@or2 ps))
(lex-defun @and (&rest ps)
(reduce #'parse-list-plus ps))
(lex-defun @satisfies (f)
(lambda (input)
(when input
(let ((first (car input))
(rest (cdr input)))
(if (funcall f first)
(list (pdoublet f rest))
nil)))))
(defun @list-of (p)
(lexical-mlet parse-list-monad
((r p))
(m-return (list r))))
(defun zero-or-more-dealer (dblt p)
(let ((input (pdoublet-rest dblt))
(lst (pdoublet-retval dblt)))
(let-if rs (funcall p input)
(list :continued
(mapcar
(lambda (dblt)
(let* ((prv (pdoublet-retval dblt))
(cont (pdoublet-rest dblt)))
(pdoublet (cons prv lst) cont))) rs))
(list :terminated
(pdoublet (reverse lst) input)))))
(defun @zero-or-more (p)
(lexical-let ((p (promote-parser p)))
(lambda (input)
(when input
(recur-let
((rs
(funcall (@list-of p) input))
(terminals '()))
(let* ((asc
(mapcar/deal (par #'zero-or-more-dealer p) rs))
(cont (reduce #'append (alist asc :continued)))
(term (alist asc :terminated)))
(if (empty? cont)
(append terminals term)
(recur cont
(append terminals term)))))))))
(defun @one-or-more (p)
(lexical-let ((p (promote-parser p)))
(lexical-mlet parse-list-monad
((one p)
(more (@zero-or-more p)))
(m-return (cons one more)))))
(funcall (@zero-or-more 'x) '(x x x ))
(funcall (promote-parser 'x) '(x x x x))