forked from panicz/grasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-parser.scm
195 lines (150 loc) · 3.51 KB
/
test-parser.scm
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
(import
(language define-interface)
(language define-type)
(language define-object)
(utils conversions)
(editor interfaces elements)
(language infix)
(language match)
(editor types primitive)
(editor types spaces)
(editor document parse)
(language examples)
(utils conversions)
(srfi :11)
(language assert)
(utils print)
(editor document cursor)
)
(let ((doc (string->document "(define (f x y) z)")))
;; The pattern matcher is able to match against
;; parsed atoms, in addition to regular symbols
(and-let* ((`(((define (f x y) z))) doc))))
;; Note that in the case of atoms and symbols,
;; the order of comparison does matter.
;; (This is unfortunate, but we can't fix that
;; without patching Kawa)
(e.g. (is (Atom "a") match/equal? 'a))
(e.g. (isnt 'a match/equal? (Atom "a")))
;; eq? uses objects' identities, so that symbols
;; can be told apart from atoms, and distinct same looking
;; atoms can be as well
(e.g. (isnt 'a eq? (Atom "a")))
(e.g. (isnt (Atom "a") eq? 'a))
(e.g. (isnt (Atom "a") eq? (Atom "a")))
(e.g.
(parameterize ((the-document
(string->document "(define (f x y) z)")))
(show->string (the-expression at: '(5 3 1 1)))) ===> "y")
(e.g.
(parameterize ((the-document
(string->document "(define (f x #;y) z)")))
(show->string (the-expression at: '(2 4 3 1 1)))) ===> "#;y")
(define (check-parser-correctness input::string)
(let* ((parsed ::pair (with-input-from-string input
parse-document))
(output ::string (with-output-to-string
(lambda ()
(show-document parsed)))))
(unless (string=? input output)
(WARN "Invalid parsing, expected: \"\\\n"input
"\ngot: \"\\\n"output"\"\n"))))
(check-parser-correctness "
(let ((quoted 'x)
(quasiquoted `x)
(unquoted ,x)
(spliced ,@x))
`(,'(quoted)
,`(quasiquoted)
,,(un quoted)
,@,@(un quote spliced)))
")
(check-parser-correctness "
(define (factorial n)
(if (<= n 0)
1
(* n (! (- n 1)))))
")
(check-parser-correctness "
(e.g.
(string-append \"abc\" \"def\")
===> \"abcdef\")
")
(check-parser-correctness "
; this input contains line comments
( ; in various places
+ ; like, after an operator
2 ; and an operand
3 ; and at the end of a list
)
( ;as well as in an empty list
)
;and at the end of input
")
(check-parser-correctness "
; line comments with empty input
")
(check-parser-correctness "
(begin
#;(move 10 #;bytes #;(from (beautiful)) source #;to target)
)
")
(check-parser-correctness "
hey!
#| what follows is an empty list
#| and this comment is nested |#|#
( #|this is the aforementioned empty list|#
#|and it seems to work|# )
")
(check-parser-correctness "
(first rest ...)
")
(check-parser-correctness "
(first . rest)
")
(check-parser-correctness "
(first . ...)
")
(check-parser-correctness "
(first . (rest))
")
(check-parser-correctness "
(first . #|and|# #;then rest) ;over
")
(check-parser-correctness "
(first #||# . #|and|# #;then ;finally
rest)
")
(check-parser-correctness "
(first . #|and|# (#;then rest))
")
(check-parser-correctness "
(first . #|and|# (#;the-end))
")
(check-parser-correctness "
(first . ,rest)
")
(check-parser-correctness "
(first . ,@rest)
")
(check-parser-correctness "
(first . 'rest)
")
(check-parser-correctness "
(first . `rest)
")
(parameterize ((debugging? #t))
(check-parser-correctness "\
first-expression
;; first line
;; comment
`',x
`(,(a) ,b ,@c ,(d))
;; second line
;; comment
#|BLOCK COMMENT (title)|#
#|another
block
comment|#
last-expression
"))