Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 98 lines (83 sloc) 2.253 kB
ed41b25 @VincentToups monad transformers
VincentToups authored
1 ;;; More Combinators
2
3 (defun -zero-or-more (parser)
4 (lexical-let ((parser parser))
5 (lambda (input)
6 (unless (empty? input)
7 (recur-let ((result (funcall parser input))
8 (acc nil)
9 (last-input input))
10 (if result
11 (recur
12 (funcall parser (parsed-leftover result))
13 (cons (parsed-value result) acc)
14 (parsed-leftover result))
15 (pair (reverse acc)
16 last-input)))))))
17
18 (funcall (-zero-or-more
19 (-matches "a"))
20 "aaaab")
2438d90 @VincentToups parser-pres
VincentToups authored
21 (funcall (-zero-or-more
22 (-matches "a"))
23 "bbbb")
ed41b25 @VincentToups monad transformers
VincentToups authored
24
25 (defun -one-or-more (parser)
26 (lexical-let ((parser parser))
27 (parser-let* ((first parser)
28 (rest (-zero-or-more parser)))
29 (simple-parser-return (cons first rest)))))
30
31 (funcall (-one-or-more
32 (-matches "dog "))
409eb8e @VincentToups defn-readme, docs
VincentToups authored
33 "dog dog dog dog cat")
ed41b25 @VincentToups monad transformers
VincentToups authored
34
2438d90 @VincentToups parser-pres
VincentToups authored
35 (funcall (-one-or-more
ed41b25 @VincentToups monad transformers
VincentToups authored
36 (-matches "dog "))
37 "cat dog dog dog cat")
38
39 (defun -maybe (parser)
40 (lexical-let ((parser parser))
41 (lambda (input)
42 (unless (empty? input)
43 (let ((r (funcall parser input)))
44 (if r r
45 (pair nil input)))))))
46
47 (defun pempty? (input)
48 "Check to see if you have hit the end of the input."
49 (if (empty? input) (pair t input)
50 (pair nil input)))
51
52 (defun -list (parser)
53 (lexical-let ((parser parser))
54 (parser-let* ((r parser))
55 (simple-parser-return
56 (list r)))))
57
58 (defun -not (parser)
59 (lexical-let ((parser parser))
60 (lambda (input)
409eb8e @VincentToups defn-readme, docs
VincentToups authored
61 (unless (empty? input)
62 (let ((r (funcall parser input)))
63 (if r nil
64 (pair t input)))))))
ed41b25 @VincentToups monad transformers
VincentToups authored
65
66 (defun -and2 (p1 p2)
67 (lexical-let ((p1 p1)
68 (p2 p2))
69 (parser-let* ((v1 p1)
70 (v2 p2))
71 (simple-parser-return v2))))
72
73 (defun -and (&rest ps)
74 (reduce #'-and2 ps))
75
76 (defun -and-list (&rest ps)
77 (lexical-let ((ps ps))
78 (if (empty? ps)
79 (lambda (input)
80 (pair nil input))
81 (parser-let*
82 ((v (car ps))
83 (rest (apply #'-and-list (cdr ps))))
84 (simple-parser-return (cons v rest))))))
85
86 (defun -n-of (n parser)
87 (if (= n 1) (-list parser)
88 (lexical-let ((n n)
89 (parser parser))
90 (parser-let*
91 ((head parser)
92 (rest (-n-of (- n 1) parser)))
93 (simple-parser-return (cons head rest))))))
94
95 (funcall (-n-of 3 (-matches "a")) "aaab")
96
2438d90 @VincentToups parser-pres
VincentToups authored
97 ;;;Controls Home <<< . >>> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
ed41b25 @VincentToups monad transformers
VincentToups authored
98 ;;; Index
Something went wrong with that request. Please try again.