/usr/share/racket/collects/syntax/id-table.rkt is in racket-common 6.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | #lang racket/base
(require (for-syntax racket/base
racket/syntax)
(for-meta 2 racket/base)
racket/contract/base
racket/contract/combinator
racket/dict
(rename-in (except-in "private/id-table.rkt"
make-free-id-table
make-immutable-free-id-table
make-bound-id-table
make-immutable-bound-id-table
mutable-free-id-table?
immutable-free-id-table?
mutable-bound-id-table?
immutable-bound-id-table?
free-id-table-set
free-id-table-remove
bound-id-table-set
bound-id-table-remove)
[mutable-free-id-table mutable-free-id-table*]
[immutable-free-id-table immutable-free-id-table*]
[mutable-bound-id-table mutable-bound-id-table*]
[immutable-bound-id-table immutable-bound-id-table*]))
;; ========
(define dict-contract-methods
(vector-immutable identifier?
any/c
id-table-iter?
#f #f #f))
(begin-for-syntax
(define (replace old new template)
(datum->syntax new
(string->symbol
(regexp-replace
(regexp-quote old)
(symbol->string template)
(regexp-replace-quote (symbol->string (syntax-e new)))))))
(define-syntax (define-templates stx)
(syntax-case stx ()
[(_ old new (template ...))
#`(begin
(define/with-syntax template (replace old new 'template)) ...)])))
;; ========
(define-struct base-id-table/c (dom rng immutable))
(define-values (id-table/c-dom-pos-proj
id-table/c-dom-neg-proj
id-table/c-rng-pos-proj
id-table/c-rng-neg-proj)
(let ()
(define (proj acc location swap)
(lambda (ctc blame)
((contract-projection (acc ctc))
(blame-add-context blame location #:swap? swap))))
(values
(proj base-id-table/c-dom "the keys of" #f)
(proj base-id-table/c-dom "the keys of" #t)
(proj base-id-table/c-rng "the values of" #f)
(proj base-id-table/c-rng "the values of" #t))))
(define (make-id-table/c idtbl/c-symbol
idtbl?
mutable-idtbl?
immutable-idtbl?
immutable-idtbl)
(define (id-table/c-name ctc)
(apply build-compound-type-name
idtbl/c-symbol
(base-id-table/c-dom ctc)
(base-id-table/c-rng ctc)
(case (base-id-table/c-immutable ctc)
[(dont-care) null]
[(#t)
(list '#:immutable #t)]
[(#f)
(list '#:immutable #f)])))
(define (id-table/c-first-order ctc)
(define dom-ctc (base-id-table/c-dom ctc))
(define rng-ctc (base-id-table/c-rng ctc))
(define immutable (base-id-table/c-immutable ctc))
(λ (val)
(and (idtbl? val)
(case immutable
[(#t) (immutable-idtbl? val)]
[(#f) (mutable-idtbl? val)]
[else #t])
(for/and ([(k v) (in-dict val)])
(and (contract-first-order-passes? dom-ctc k)
(contract-first-order-passes? rng-ctc v))))))
(define (check-id-table/c ctc val blame)
(define immutable (base-id-table/c-immutable ctc))
(case immutable
[(#t)
(unless (immutable-idtbl? val)
(raise-blame-error blame val
'(expected "an immutable ~a," given: "~e") 'idtbl val))]
[(#f)
(unless (mutable-idtbl? val)
(raise-blame-error blame val
'(expected "a mutable ~a," given: "~e") 'idtbl val))]
[(dont-care)
(unless (idtbl? val)
(raise-blame-error blame val
'(expected "a ~a," given: "~e") 'idtbl val))]))
(define (fo-projection ctc)
(λ (blame)
(define dom-proj (id-table/c-dom-pos-proj ctc blame))
(define rng-proj (id-table/c-rng-pos-proj ctc blame))
(λ (val)
(check-id-table/c ctc val blame)
(for ([(k v) (in-dict val)])
(dom-proj k)
(rng-proj v))
val)))
(define (ho-projection ctc)
(lambda (blame)
(define pos-dom-proj (id-table/c-dom-pos-proj ctc blame))
(define neg-dom-proj (id-table/c-dom-neg-proj ctc blame))
(define pos-rng-proj (id-table/c-rng-pos-proj ctc blame))
(define neg-rng-proj (id-table/c-rng-neg-proj ctc blame))
(lambda (tbl)
(check-id-table/c ctc tbl blame)
;;TODO for immutable hash tables optimize this chaperone to a flat
;;check if possible
(if (immutable-idtbl? tbl)
(chaperone-immutable-id-table tbl pos-dom-proj pos-rng-proj
impersonator-prop:contracted ctc)
(chaperone-mutable-id-table tbl
neg-dom-proj
pos-dom-proj
neg-rng-proj
pos-rng-proj
impersonator-prop:contracted ctc)))))
(struct flat-id-table/c base-id-table/c ()
#:omit-define-syntaxes
#:property prop:flat-contract
(build-flat-contract-property
#:name id-table/c-name
#:first-order id-table/c-first-order
#:projection fo-projection))
(struct chaperone-id-table/c base-id-table/c ()
#:omit-define-syntaxes
#:property prop:chaperone-contract
(build-chaperone-contract-property
#:name id-table/c-name
#:first-order id-table/c-first-order
#:projection ho-projection))
;; Note: impersonator contracts not currently supported.
(struct impersonator-id-table/c base-id-table/c ()
#:omit-define-syntaxes
#:property prop:contract
(build-contract-property
#:name id-table/c-name
#:first-order id-table/c-first-order
#:projection ho-projection))
(define (id-table/c key/c value/c #:immutable [immutable 'dont-care])
(define key/ctc (coerce-contract idtbl/c-symbol key/c))
(define value/ctc (coerce-contract idtbl/c-symbol value/c))
(cond [(and (eq? immutable #t)
(flat-contract? key/ctc)
(flat-contract? value/ctc))
(flat-id-table/c key/ctc value/ctc immutable)]
[(chaperone-contract? value/ctc)
(chaperone-id-table/c key/ctc value/ctc immutable)]
[else
(impersonator-id-table/c key/ctc value/ctc immutable)]))
(procedure-rename id-table/c idtbl/c-symbol))
;; ========
(define-syntax (make-code stx)
(syntax-case stx ()
[(_ idtbl)
(let ()
(define-templates "idtbl" #'idtbl
(mutable-idtbl* immutable-idtbl* mutable-idtbl immutable-idtbl
make-idtbl make-mutable-idtbl make-immutable-idtbl
idtbl? immutable-idtbl? mutable-idtbl?
idtbl-hash idtbl-phase
idtbl-ref
idtbl-set! idtbl-set
idtbl-remove! idtbl-remove
idtbl-set/constructor idtbl-remove/constructor
idtbl-count
idtbl-iterate-first idtbl-iterate-next
idtbl-iterate-key idtbl-iterate-value
idtbl-map idtbl-for-each
idtbl-mutable-methods idtbl-immutable-methods
idtbl/c))
#'(begin
;; Struct defs at end, so that dict methods can refer to earlier procs
(define (make-idtbl [init-dict null]
#:phase [phase (syntax-local-phase-level)])
(let ([t (mutable-idtbl (make-hasheq) phase)])
(for ([(k v) (in-dict init-dict)])
(unless (identifier? k)
(raise-type-error 'make-idtbl
"dictionary with identifier keys" init-dict))
(idtbl-set! t k v))
t))
(define (make-immutable-idtbl [init-dict null]
#:phase [phase (syntax-local-phase-level)])
(for/fold ([t (immutable-idtbl '#hasheq() phase)])
([(k v) (in-dict init-dict)])
(unless (identifier? k)
(raise-type-error 'make-immutable-idtbl
"dictionary with identifier keys" init-dict))
(idtbl-set t k v)))
;; Replace to use new constructor
(define (idtbl-set d id v)
(idtbl-set/constructor d id v immutable-idtbl))
(define (idtbl-remove d id)
(idtbl-remove/constructor d id immutable-idtbl))
(define idtbl-immutable-methods
(vector-immutable idtbl-ref
#f
idtbl-set
#f
idtbl-remove
idtbl-count
idtbl-iterate-first
idtbl-iterate-next
idtbl-iterate-key
idtbl-iterate-value))
(struct mutable-idtbl mutable-idtbl* ()
#:property prop:dict/contract
(list idtbl-mutable-methods
dict-contract-methods))
(struct immutable-idtbl immutable-idtbl* ()
#:property prop:dict/contract
(list idtbl-immutable-methods
dict-contract-methods))
(define idtbl/c
(make-id-table/c 'idtbl/c
idtbl? mutable-idtbl? immutable-idtbl?
immutable-idtbl))
(provide/contract
[make-idtbl
(->* () (dict? #:phase (or/c #f exact-integer?)) mutable-idtbl?)]
[make-immutable-idtbl
(->* () (dict? #:phase (or/c #f exact-integer?)) immutable-idtbl?)]
[idtbl?
(-> any/c boolean?)]
[mutable-idtbl?
(-> any/c boolean?)]
[immutable-idtbl?
(-> any/c boolean?)]
[idtbl-ref
(->* (idtbl? identifier?) (any/c) any)]
[idtbl-set!
(-> mutable-idtbl? identifier? any/c void?)]
[idtbl-set
(-> immutable-idtbl? identifier? any/c immutable-idtbl?)]
[idtbl-remove!
(-> mutable-idtbl? identifier? void?)]
[idtbl-remove
(-> immutable-idtbl? identifier? immutable-idtbl?)]
[idtbl-count
(-> idtbl? exact-nonnegative-integer?)]
[idtbl-iterate-first
(-> idtbl? (or/c #f id-table-iter?))]
[idtbl-iterate-next
(-> idtbl? id-table-iter? (or/c #f id-table-iter?))]
[idtbl-iterate-key
(-> idtbl? id-table-iter? identifier?)]
[idtbl-iterate-value
(-> idtbl? id-table-iter? any)]
[idtbl-map
(-> idtbl? (-> identifier? any/c any) list?)]
[idtbl-for-each
(-> idtbl? (-> identifier? any/c any) any)]
[idtbl/c
(->* (flat-contract? chaperone-contract?)
(#:immutable (or/c 'dont-care #t #f))
contract?)])))]))
(make-code bound-id-table)
(make-code free-id-table)
(provide/contract
[id-table-iter? (-> any/c boolean?)])
|