This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathracket-version.rkt
220 lines (175 loc) · 8.16 KB
/
racket-version.rkt
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#lang racket/base
; Define and compare Racket version intervals
;-------------------------------------------------------------------------------
; Basic definitions
(define (unbounded? v)
(equal? v UNBOUNDED_RACKET_VERSION))
(define UNBOUNDED_RACKET_VERSION "*")
(define PRESUMED_MINIMUM_RACKET_VERSION "0.0")
(define PRESUMED_MAXIMUM_RACKET_VERSION "9.99.999.999")
(define maybe-racket-version/c
(or/c unbounded? valid-version?))
(define current-racket-version-relationship/c
(or/c 'supported 'unsupported 'undeclared))
(define racket-version-range/c
(or/c valid-version?
(list/c maybe-racket-version/c
maybe-racket-version/c)))
(define racket-version-ranges/c
(listof racket-version-range/c))
(require racket/contract
syntax/parse
version/utils
"l10n.rkt"
"message.rkt"
"string.rkt")
(provide racket-version-selection
(contract-out
[UNBOUNDED_RACKET_VERSION non-empty-string?]
[PRESUMED_MINIMUM_RACKET_VERSION valid-version?]
[PRESUMED_MAXIMUM_RACKET_VERSION valid-version?]
[racket-version-range/c flat-contract?]
[racket-version-ranges/c flat-contract?]
[unbounded? predicate/c]
[make-racket-version-interval
(-> maybe-racket-version/c
maybe-racket-version/c
(values exact-integer? exact-integer?))]
[racket-version-in-range?
(-> valid-version?
maybe-racket-version/c
maybe-racket-version/c
boolean?)]
[check-racket-version-ranges
(-> valid-version?
racket-version-ranges/c
current-racket-version-relationship/c)]))
(define+provide-message $racket-version:invalid-interval (min-v max-v))
(define (make-racket-version-interval min-v max-v)
(define lo (version->integer (normalize-minimum-version min-v)))
(define hi (version->integer (normalize-maximum-version max-v)))
(if (< hi lo)
(raise ($racket-version:invalid-interval min-v max-v))
(values lo hi)))
(define (normalize-minimum-version v)
(if (unbounded? v)
PRESUMED_MINIMUM_RACKET_VERSION
v))
(define (normalize-maximum-version v)
(if (unbounded? v)
PRESUMED_MAXIMUM_RACKET_VERSION
v))
(define (racket-version-in-range? given-v min-v max-v)
(define-values (lo hi) (make-racket-version-interval min-v max-v))
(define v (version->integer given-v))
(and (<= lo v)
(<= v hi)))
(define (check-racket-version-ranges v ranges)
(if (null? ranges)
'undeclared
(if (for/or ([variant (in-list ranges)])
(define pair
(if (list? variant)
variant
(list variant variant)))
(racket-version-in-range? v (car pair) (cadr pair)))
'supported
'unsupported)))
;-------------------------------------------------------------------------------
; Syntax classes for expressing version sets
(define-syntax-class racket-version-or-*
(pattern (~var bound string)
#:when (let ([v (syntax-e #'bound)])
(or (unbounded? v)
(valid-version? v)))))
(define-syntax-class racket-version-selection
#:attributes (min max)
(pattern (~and (min-variant:racket-version-or-* max-variant:racket-version-or-*)
(~bind [min (datum->syntax #'min-variant (normalize-minimum-version (syntax-e #'min-variant)))]
[max (datum->syntax #'max-variant (normalize-maximum-version (syntax-e #'max-variant)))])
(~fail #:unless (version<=? (syntax-e #'min) (syntax-e #'max))
(get-localized-string 'backwards-racket-version-interval))))
(pattern (~and (~var v racket-version-or-*)
(~bind [min (datum->syntax #'max-variant (normalize-minimum-version (syntax-e #'v)))]
[max (datum->syntax #'max-variant (normalize-maximum-version (syntax-e #'v)))]))))
(module+ test
(require rackunit)
(define (test-invalid-interval min-v max-v)
(test-exn (format "Flag [~a, ~a] as an invalid interval" min-v max-v)
(λ (e)
(and ($racket-version:invalid-interval? e)
(check-eq? ($racket-version:invalid-interval-min-v e) min-v)
(check-eq? ($racket-version:invalid-interval-max-v e) max-v)))
(λ ()
(racket-version-in-range? "0.0" min-v max-v))))
(for ([v (in-list (list null
`((,UNBOUNDED_RACKET_VERSION ,UNBOUNDED_RACKET_VERSION))
`((,UNBOUNDED_RACKET_VERSION "9.9") "6.3")
'(("0.0" "7.7.0.5"))))])
(test-true (format "Allow ~s as racket-version-ranges/c" v)
(racket-version-ranges/c v)))
(for ([v (in-list (list `(,UNBOUNDED_RACKET_VERSION ,UNBOUNDED_RACKET_VERSION)
"0.0"
'(())
'(("999.999" "0.0"))))])
(test-false (format "Forbid ~s as racket-version-ranges/c" v)
(racket-version-ranges/c v)))
(test-true "Check (inclusive) minimum"
(racket-version-in-range? "7.7.0.5" "7.7.0.5" "8.0"))
(test-true "Check (inclusive) maximum"
(racket-version-in-range? "8.0" "7.7.0.5" "8.0"))
(test-true "Check exact"
(racket-version-in-range? "7.7.0.5" "7.7.0.5" "7.7.0.5"))
(test-true "Check unbounded minimum"
(and (racket-version-in-range? "0.0" UNBOUNDED_RACKET_VERSION "8.0")
(racket-version-in-range? "1.0" UNBOUNDED_RACKET_VERSION "8.0")
(not (racket-version-in-range? "8.1" UNBOUNDED_RACKET_VERSION "8.0"))))
(test-true "Check unbounded maximum"
(and (not (racket-version-in-range? "1.0" "3.0" UNBOUNDED_RACKET_VERSION))
(racket-version-in-range? "9.99" "3.0" UNBOUNDED_RACKET_VERSION)))
; Only one case works here since we're not offering exclusive endpoints
(test-invalid-interval "1.0" "0.0")
(test-eq? "Detect undeclared version ranges"
(check-racket-version-ranges (version) null)
'undeclared)
(test-eq? "Match a version against any one range"
(check-racket-version-ranges "1.2"
'(("0.5" "0.8")
("1.0" "2.0")))
'supported)
(test-eq? "Match a version against any one range"
(check-racket-version-ranges "1.2"
'(("0.5" "0.8")
("1.0" "2.0")))
'supported)
(test-eq? "Match an exact version among ranges"
(check-racket-version-ranges "1.2"
'(("0.5" "0.8") "1.2"))
'supported)
(test-eq? "An unbounded range anywhere is equivalent to matching every version"
(check-racket-version-ranges "1.2"
`(("0.1" "0.2")
(,UNBOUNDED_RACKET_VERSION ,UNBOUNDED_RACKET_VERSION)))
'supported)
(test-eq? "Flag any version matching no range as unsupported"
(check-racket-version-ranges "1.2"
'(("0.1" "0.2")))
'unsupported)
(test-true "Detect valid Racket versions in a macro"
(syntax-parse #'"7.7.0.5" [v:racket-version-or-* #t] [_ #f]))
(test-true "Allow unbounded versions"
(syntax-parse #'"*" [v:racket-version-or-* #t] [_ #f]))
(test-equal? "Express version intervals"
(syntax-parse #'("*" ("*" "*") "6.5" ("9.1" "9.3.4"))
[(v:racket-version-selection ...+)
(syntax->datum #'((v.min v.max) ...))]
[_ #f])
`((,PRESUMED_MINIMUM_RACKET_VERSION ,PRESUMED_MAXIMUM_RACKET_VERSION)
(,PRESUMED_MINIMUM_RACKET_VERSION ,PRESUMED_MAXIMUM_RACKET_VERSION)
("6.5" "6.5")
("9.1" "9.3.4")))
(test-exn "Capture backwards version intervals during expansion"
exn:fail:syntax?
(λ ()
(syntax-parse #'("9.3.4" "9.1")
[v:racket-version-selection #f]))))