|
|
@ -1,14 +1,18 @@
|
|
|
|
#lang racket/base
|
|
|
|
#lang racket/base
|
|
|
|
(require (for-syntax racket/base racket/syntax)
|
|
|
|
(require (for-syntax racket/base racket/syntax)
|
|
|
|
racket/list
|
|
|
|
racket/list
|
|
|
|
|
|
|
|
racket/match
|
|
|
|
racket/syntax
|
|
|
|
racket/syntax
|
|
|
|
|
|
|
|
racket/format
|
|
|
|
|
|
|
|
syntax/stx
|
|
|
|
syntax/strip-context
|
|
|
|
syntax/strip-context
|
|
|
|
br/define
|
|
|
|
br/define
|
|
|
|
br/private/syntax-flatten)
|
|
|
|
br/private/syntax-flatten)
|
|
|
|
(provide (all-defined-out)
|
|
|
|
(provide (all-defined-out)
|
|
|
|
syntax-flatten
|
|
|
|
syntax-flatten
|
|
|
|
(rename-out [strip-context strip-bindings]
|
|
|
|
(rename-out [strip-context strip-bindings]
|
|
|
|
[replace-context replace-bindings]))
|
|
|
|
[replace-context replace-bindings]
|
|
|
|
|
|
|
|
[stx-map syntax-map]))
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(module+ test
|
|
|
|
(require rackunit))
|
|
|
|
(require rackunit))
|
|
|
@ -55,45 +59,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
(define-macro (format-string FMT ID0 ID ...)
|
|
|
|
(define-macro (format-string FMT ID0 ID ...)
|
|
|
|
#'(datum->syntax ID0 (format FMT (syntax->datum ID0) (syntax->datum ID) ...)))
|
|
|
|
#'(datum->syntax ID0 (format FMT (syntax->datum ID0) (syntax->datum ID) ...)))
|
|
|
|
|
|
|
|
#|
|
|
|
|
|
|
|
|
(define (format-string FMT ID0 . IDS)
|
|
|
|
|
|
|
|
(datum->syntax ID0 (apply format FMT (syntax->datum ID0) (map syntax->datum IDS))))
|
|
|
|
|
|
|
|
|#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-macro (->unsyntax X)
|
|
|
|
(define (->unsyntax x)
|
|
|
|
#'(if (syntax? X)
|
|
|
|
(if (syntax? x) (syntax->datum x) x))
|
|
|
|
(syntax->datum X)
|
|
|
|
|
|
|
|
X))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-macro (prefix-id PREFIX ... BASE-OR-BASES)
|
|
|
|
(define (fix-base loc-arg prefixes base-or-bases suffixes)
|
|
|
|
#'(let* ([bobs BASE-OR-BASES]
|
|
|
|
(define single-mode? (and (not (list? base-or-bases)) (not (syntax->list base-or-bases))))
|
|
|
|
[got-single? (and (not (list? bobs)) (not (syntax->list bobs)))]
|
|
|
|
(define bases (if single-mode? (list base-or-bases) (or (syntax->list base-or-bases) base-or-bases)))
|
|
|
|
[bases (if got-single?
|
|
|
|
(define (stx-join stxs) (apply string-append (map (compose1 ~a ->unsyntax) stxs)))
|
|
|
|
(list bobs)
|
|
|
|
(define result (map (λ (base) (format-id base "~a~a~a" (stx-join prefixes) (syntax-e base) (stx-join suffixes)
|
|
|
|
bobs)]
|
|
|
|
#:source loc-arg)) bases))
|
|
|
|
[result (syntax-case-map
|
|
|
|
(if single-mode? (car result) result))
|
|
|
|
bases ()
|
|
|
|
|
|
|
|
[base (format-id #'base "~a~a"
|
|
|
|
|
|
|
|
(string-append (format "~a" (->unsyntax PREFIX)) ...)
|
|
|
|
|
|
|
|
(syntax-e #'base))])])
|
|
|
|
|
|
|
|
(if got-single? (car result) result)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-macro (infix-id PREFIX BASE-OR-BASES SUFFIX ...)
|
|
|
|
(define (prefix-id #:source [loc-arg #f] . args)
|
|
|
|
#'(let* ([bobs BASE-OR-BASES]
|
|
|
|
((match-lambda
|
|
|
|
[got-single? (and (not (list? bobs)) (not (syntax->list bobs)))]
|
|
|
|
[(list prefixes ... base-or-bases)
|
|
|
|
[bases (if got-single?
|
|
|
|
(fix-base loc-arg prefixes base-or-bases empty)]) args))
|
|
|
|
(list bobs)
|
|
|
|
|
|
|
|
bobs)]
|
|
|
|
|
|
|
|
[result (syntax-case-map
|
|
|
|
|
|
|
|
bases ()
|
|
|
|
|
|
|
|
[base (format-id #'base "~a~a~a"
|
|
|
|
|
|
|
|
(->unsyntax PREFIX)
|
|
|
|
|
|
|
|
(syntax-e #'base)
|
|
|
|
|
|
|
|
(string-append (format "~a" (->unsyntax SUFFIX)) ...))])])
|
|
|
|
|
|
|
|
(if got-single? (car result) result)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (infix-id #:source [loc-arg #f] . args)
|
|
|
|
|
|
|
|
((match-lambda
|
|
|
|
|
|
|
|
[(list prefix base-or-bases suffixes ...)
|
|
|
|
|
|
|
|
(fix-base loc-arg (list prefix) base-or-bases suffixes)]) args))
|
|
|
|
|
|
|
|
|
|
|
|
(define-macro (suffix-id BASE-OR-BASES SUFFIX ...)
|
|
|
|
(define (suffix-id #:source [loc-arg #f] . args)
|
|
|
|
#'(infix-id "" BASE-OR-BASES SUFFIX ...))
|
|
|
|
((match-lambda
|
|
|
|
|
|
|
|
[(list base-or-bases suffixes ...)
|
|
|
|
|
|
|
|
(fix-base loc-arg empty base-or-bases suffixes)]) args))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
|
|
|
(define-check (check-stx-equal? stx1 stx2)
|
|
|
|
|
|
|
|
(define stxs (list stx1 stx2))
|
|
|
|
|
|
|
|
(apply equal? (map syntax->datum stxs)))
|
|
|
|
|
|
|
|
(check-stx-equal? (prefix-id "foo" "bar" #'id) #'foobarid)
|
|
|
|
|
|
|
|
(check-stx-equal? (infix-id "foo" #'id "bar" "zam") #'fooidbarzam)
|
|
|
|
|
|
|
|
(check-stx-equal? (suffix-id #'id "foo" "bar" "zam") #'idfoobarzam)
|
|
|
|
|
|
|
|
(for-each check-stx-equal? (suffix-id #'(this that) "@") (list #'this@ #'that@)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-macro-cases syntax-property*
|
|
|
|
(define-macro-cases syntax-property*
|
|
|
@ -110,6 +117,23 @@
|
|
|
|
[(_ STX ['PROP0 VAL0 . PRESERVED0] ['PROP VAL . PRESERVED] ...) ; write multiple
|
|
|
|
[(_ STX ['PROP0 VAL0 . PRESERVED0] ['PROP VAL . PRESERVED] ...) ; write multiple
|
|
|
|
#'(syntax-property* (syntax-property STX 'PROP0 VAL0 . PRESERVED0) ['PROP VAL . PRESERVED] ...)])
|
|
|
|
#'(syntax-property* (syntax-property STX 'PROP0 VAL0 . PRESERVED0) ['PROP VAL . PRESERVED] ...)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#|
|
|
|
|
|
|
|
|
(define (syntax-property* . args)
|
|
|
|
|
|
|
|
((match-lambda*
|
|
|
|
|
|
|
|
[(list x) x]
|
|
|
|
|
|
|
|
[(list stx (list prop val others ...)) ; write one
|
|
|
|
|
|
|
|
(apply syntax-property stx prop val others)]
|
|
|
|
|
|
|
|
[(list stx (list prop val others ...) args ...) ; write multiple
|
|
|
|
|
|
|
|
#'(apply syntax-property* (apply syntax-property stx prop val others) args)]
|
|
|
|
|
|
|
|
[(list stx prop) ; read one
|
|
|
|
|
|
|
|
(syntax-property stx prop)]
|
|
|
|
|
|
|
|
[(list stx prop0 props ...) ; read multiple
|
|
|
|
|
|
|
|
(map (λ (prop) (syntax-property stx prop)) (cons prop0 props))]
|
|
|
|
|
|
|
|
[else 'huh]) args))
|
|
|
|
|
|
|
|
|#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (filter-stx-prop prop stxs)
|
|
|
|
|
|
|
|
(filter (λ (stx) (syntax-property stx prop)) stxs))
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(module+ test
|
|
|
|
(define x (syntax-property* #'foo ['bar #t] ['zam 'boni]))
|
|
|
|
(define x (syntax-property* #'foo ['bar #t] ['zam 'boni]))
|
|
|
|