-
Notifications
You must be signed in to change notification settings - Fork 2
/
guruguru-tests.el
197 lines (166 loc) · 5.57 KB
/
guruguru-tests.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
;; Copyright 2009 Dominic Cooney. 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 2
;; 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, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301, USA.
;;; Tests for guruguru.el
(require 'el-expectations "third_party/el-expectations.el")
(require 'guruguru)
(setq max-lisp-eval-depth 3000)
(defmacro with-test-buffer (text &rest body)
"Creates a temporary buffer containing TEXT and executes BODY."
`(with-temp-buffer
(insert ,text)
(goto-char (point-min))
,@body))
(defmacro eval-with-text (text pattern &rest body)
"Evaluates PATTERN in a temporary buffer containing TEXT, then executes BODY."
`(with-test-buffer ,text
(let ((gg-bindings ()))
(gg-eval ,pattern)
,@body)))
(defun digitp (ch)
(and (<= ?0 ch) (<= ch ?9)))
(defun greaterp (ch)
(< prev ch))
(expectations
(desc "guruguru")
; gg-any at a character should succeed
(expect '(t . ?a)
(eval-with-text "a" (gg-any)))
; gg-any at end-of-buffer should fail
(expect nil
(eval-with-text "" (gg-any)))
; gg-seq should succeed if all parsers succeed
(expect '(t . ?b)
(eval-with-text "ab" (gg-seq (gg-any) (gg-any))))
; gg-seq should fail if any parser fails
(expect nil
(eval-with-text "a" (gg-seq (gg-any) (gg-any))))
; gg-seq should rewind on failure
(expect 1
(eval-with-text "a"
(gg-seq (gg-any) (gg-any))
(point)))
; gg-if should fail if the parser fails
(expect nil
(eval-with-text "a" (gg-if nil (gg-seq (gg-any) (gg-any)))))
; gg-if should fail if the predicate fails
(expect nil
(eval-with-text "a" (gg-if 'digitp (gg-any))))
; gg-if should succeed if the parser and the predicate succeed
(expect '(t . ?1)
(eval-with-text "1" (gg-if 'digitp (gg-any))))
; bindings should be in scope in gg-if predicate
(expect '(t . ?b)
(eval-with-text "ab"
(gg-seq
(gg-bind 'prev (gg-any))
(gg-if 'greaterp (gg-any)))))
; gg-alt should succeed with the first parser that succeeds
(expect '(t . ?a)
(eval-with-text "a"
(gg-alt
(gg-seq (gg-any) (gg-any)) ; text too short
(gg-if 'digitp (gg-any)) ; predicate fails
(gg-any))))
; gg-alt should fail if all of the alternatives fail
(expect nil
(eval-with-text "a"
(gg-alt
(gg-seq (gg-any) (gg-any))
(gg-if 'digitp (gg-any)))))
; gg-act should invoke action if the parser succeeds
(expect '(t . (1 . 2))
(eval-with-text "a"
(gg-act (lambda () (cons gg-start gg-end)) (gg-any))))
; gg-bind should introduce bindings visible to gg-act's action
(expect '(t . "b97")
(eval-with-text "ab"
(gg-act
(lambda () (format "%c%d" y x))
(gg-seq (gg-bind 'x (gg-any)) (gg-bind 'y (gg-any))))))
; gg-end should fail if there's still content in the buffer
(expect nil
(eval-with-text "a" (gg-end)))
; gg-end should succeed if there's no content left in the buffer
(expect '(t . nil)
(eval-with-text "a" (gg-seq (gg-any) (gg-end))))
; really parse a small, right-recursive grammar
(expect '(t . 7)
(with-test-buffer "aaaaaaa" ; seven "a"s
; xs := . xs / (end)
(let* ((xs (gg-alt
(gg-act (lambda () (1+ len))
(gg-seq (gg-any) (gg-bind 'len (gg-call 'tail))))
(gg-act (lambda () 0) (gg-end))))
(gg-bindings ())
(gg-memo-table (make-hash-table :test 'equal))
(gg-heads (make-hash-table :test 'equal))
(gg-lr-stack nil)
(gg-grammar (make-gg-grammar :rules `((tail . ,xs)))))
(gg-eval (gg-call 'tail)))))
; parse a small, right-recursive grammar specified in abstract syntax
(expect '(t . 9)
(with-test-buffer "bbbbba" ; 5 * 2 - 1 = 9
(gg-parse
(gg-grammar
(start x := expr eof -> x)
(expr
?b x := expr -> (+ 2 x)
| ?a x := expr -> (- x 1)
| -> 0)))))
; intermediate results should be memoized
(expect '(t . 1)
(with-test-buffer "ac"
(let ((invoke-count 0))
(gg-parse
(gg-grammar
(start
x := a ?b eof -> x
| x := a ?c eof -> x)
(a ?a -> (incf invoke-count)))))))
; left-recursive rules should work
(expect '(t . 37)
(with-test-buffer "3*5*2+7"
(gg-parse
(gg-grammar
(start x := term eof -> x)
(term
x := term ?+ y := fact -> (+ x y)
| x := term ?- y := fact -> (- x y)
| fact)
(fact
x := fact ?* y := num -> (* x y)
| x := fact ?/ y := num -> (/ x y)
| num)
(num
?0 -> 0 | ?1 -> 1 | ?2 -> 2 | ?3 -> 3 | ?4 -> 4
| ?5 -> 5 | ?6 -> 6 | ?7 -> 7 | ?8 -> 8 | ?9 -> 9)))))
; regular expression literals should work
(expect '(t . ("good" "food" "mood"))
(with-test-buffer "goodfoodmood"
(gg-parse
(gg-grammar
(start xs := words eof -> xs)
(words
x := word xs := words -> (cons x xs)
| -> nil)
(word ".o+.")))))
; regular expression literals should not skip forward
(expect nil
(with-test-buffer "xo"
(gg-parse
(gg-grammar
(start "o")))))
)