You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
5.5 KiB
Racket
146 lines
5.5 KiB
Racket
9 years ago
|
#lang racket/base
|
||
8 years ago
|
(require (for-syntax
|
||
|
racket/base
|
||
5 years ago
|
"private/generate-literals.rkt")
|
||
8 years ago
|
racket/list
|
||
8 years ago
|
racket/match
|
||
8 years ago
|
racket/syntax
|
||
8 years ago
|
racket/format
|
||
|
syntax/stx
|
||
8 years ago
|
syntax/strip-context
|
||
7 years ago
|
syntax/parse
|
||
5 years ago
|
"macro.rkt"
|
||
|
"private/syntax-flatten.rkt")
|
||
8 years ago
|
(provide (all-defined-out)
|
||
8 years ago
|
syntax-flatten
|
||
8 years ago
|
stx-map
|
||
6 years ago
|
strip-context ;; under both names
|
||
8 years ago
|
(rename-out [strip-context strip-bindings]
|
||
8 years ago
|
[replace-context replace-bindings]
|
||
8 years ago
|
[stx-map syntax-map]
|
||
|
[syntax-flatten stx-flatten]
|
||
|
[prefix-id prefix-ids]
|
||
|
[suffix-id suffix-ids]
|
||
|
[infix-id infix-ids]))
|
||
9 years ago
|
|
||
7 years ago
|
(module+ test (require rackunit))
|
||
|
|
||
|
|
||
|
(define-macro-cases syntax-parse/easy
|
||
|
[(_ STX [PAT . BODY] ... [else . ELSEBODY])
|
||
|
(with-syntax ([(PAT ...) (map (λ (stx) (literalize-pat stx #'~literal)) (syntax->list #'(PAT ...)))])
|
||
|
#'(syntax-parse (syntax-case STX () [any #'any])
|
||
|
[PAT . BODY] ... [else . ELSEBODY]))])
|
||
|
|
||
|
|
||
|
(define-macro-cases pattern-case
|
||
|
[(_ EXPR ... [else . ELSEBODY]) #'(syntax-parse/easy EXPR ...
|
||
|
[else . ELSEBODY])]
|
||
|
[(_ STX-ARG PAT+BODY ...)
|
||
|
#'(pattern-case STX-ARG
|
||
|
PAT+BODY ...
|
||
|
[else (raise-syntax-error 'pattern-case
|
||
|
(format "unable to match pattern for ~v" (syntax->datum STX-ARG)))])])
|
||
|
|
||
9 years ago
|
(module+ test
|
||
7 years ago
|
(define (pc stx)
|
||
|
(pattern-case stx
|
||
|
[(a 2ND c) #'2ND]
|
||
|
[(LEFT RIGHT) #'LEFT]
|
||
|
[(g) #'hooray]))
|
||
|
(check-equal? (syntax->datum (pc #'(a b c))) 'b)
|
||
|
(check-equal? (syntax->datum (pc #'(x y))) 'x)
|
||
|
(check-equal? (syntax->datum (pc #'(g))) 'hooray)
|
||
|
(check-exn exn:fail:syntax? (λ () (pc #'(f)))))
|
||
|
|
||
9 years ago
|
|
||
8 years ago
|
(define-macro (pattern-case-filter STX-ARG PAT+BODY ...)
|
||
|
#'(let* ([arg STX-ARG]
|
||
|
[stxs (or (and (syntax? arg) (syntax->list arg)) arg)])
|
||
|
(unless (and (list? stxs) (andmap syntax? stxs))
|
||
|
(raise-syntax-error 'pattern-case-filter
|
||
|
(format "~v cannot be made into a list of syntax objects" (syntax->datum arg))))
|
||
|
(for*/list ([stx (in-list stxs)]
|
||
|
[result (in-value (pattern-case stx PAT+BODY ... [else #f]))]
|
||
|
#:when result)
|
||
7 years ago
|
result)))
|
||
7 years ago
|
|
||
|
|
||
7 years ago
|
(module+ test
|
||
|
(define (pcf x)
|
||
|
(pattern-case-filter x
|
||
|
[(1ST 2ND 3RD) #'2ND]
|
||
|
[(LEFT RIGHT) #'LEFT]))
|
||
|
(check-equal? (map syntax-e (pcf #'((a b c) (x y) (f)))) '(b x)))
|
||
8 years ago
|
|
||
|
|
||
|
(define-macro-cases with-pattern
|
||
|
[(_ () . BODY) #'(begin . BODY)]
|
||
8 years ago
|
[(_ ([PAT0 STX0] PAT+STX ...) . BODY)
|
||
7 years ago
|
#'(syntax-parse/easy STX0
|
||
7 years ago
|
[PAT0 (with-pattern (PAT+STX ...) (let () (void) . BODY))]
|
||
7 years ago
|
[else (raise-syntax-error 'with-pattern
|
||
|
(format "unable to match pattern ~a" 'PAT0) STX0)])])
|
||
8 years ago
|
|
||
7 years ago
|
(module+ test
|
||
|
(check-equal? (syntax->datum (with-pattern ([foo 'foo])
|
||
|
#'zzz)) 'zzz)
|
||
|
(check-exn exn:fail:syntax? (λ () (with-pattern ([42 'foo])
|
||
|
#'zzz)))
|
||
|
(check-exn exn:fail:syntax? (λ () (with-pattern ([bar 'foo])
|
||
|
#'zzz)))
|
||
|
(check-equal? (syntax->datum (with-pattern ([(foo BAR) '(foo 42)])
|
||
|
#'BAR)) 42)
|
||
|
(check-exn exn:fail:syntax? (λ () (with-pattern ([(foo BAR) '(zam 42)])
|
||
|
#'BAR))))
|
||
|
|
||
9 years ago
|
|
||
8 years ago
|
(define-macro (format-string FMT ID0 ID ...)
|
||
|
#'(datum->syntax ID0 (format FMT (syntax->datum ID0) (syntax->datum ID) ...)))
|
||
9 years ago
|
|
||
8 years ago
|
(define (->unsyntax x) (if (syntax? x) (syntax->datum x) x))
|
||
9 years ago
|
|
||
8 years ago
|
(define (stx-join stxs)
|
||
|
(apply string-append (map (compose1 ~a ->unsyntax) stxs)))
|
||
|
|
||
8 years ago
|
(define (*fix-base loc-arg ctx-arg prefixes base-or-bases suffixes)
|
||
|
(define list-mode? (or (list? base-or-bases) (syntax->list base-or-bases)))
|
||
8 years ago
|
(define bases (if list-mode?
|
||
|
(or (syntax->list base-or-bases) base-or-bases)
|
||
|
(list base-or-bases)))
|
||
8 years ago
|
(define result (map (λ (base) (format-id (or ctx-arg base) "~a~a~a" (stx-join prefixes) (syntax-e base) (stx-join suffixes)
|
||
8 years ago
|
#:source loc-arg)) bases))
|
||
8 years ago
|
(if list-mode? result (car result)))
|
||
8 years ago
|
|
||
|
|
||
8 years ago
|
(define (prefix-id #:source [loc-arg #f] #:context [ctx-arg #f] . args)
|
||
8 years ago
|
((match-lambda
|
||
|
[(list prefixes ... base-or-bases)
|
||
8 years ago
|
(*fix-base loc-arg ctx-arg prefixes base-or-bases empty)]) args))
|
||
8 years ago
|
|
||
8 years ago
|
(define (infix-id #:source [loc-arg #f] #:context [ctx-arg #f] . args)
|
||
8 years ago
|
((match-lambda
|
||
|
[(list prefix base-or-bases suffixes ...)
|
||
8 years ago
|
(*fix-base loc-arg ctx-arg (list prefix) base-or-bases suffixes)]) args))
|
||
8 years ago
|
|
||
8 years ago
|
(define (suffix-id #:source [loc-arg #f] #:context [ctx-arg #f] . args)
|
||
8 years ago
|
((match-lambda
|
||
|
[(list base-or-bases suffixes ...)
|
||
8 years ago
|
(*fix-base loc-arg ctx-arg empty base-or-bases suffixes)]) args))
|
||
8 years ago
|
|
||
|
(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@)))
|
||
8 years ago
|
|
||
|
|
||
8 years ago
|
(define (syntax-srcloc stx)
|
||
|
(srcloc (syntax-source stx)
|
||
|
(syntax-line stx)
|
||
|
(syntax-column stx)
|
||
|
(syntax-position stx)
|
||
|
(syntax-span stx)))
|