/usr/share/r6rs/nanopass/meta-syntax-dispatch.ss is in r6rs-nanopass-dev 1.9+git20160429.g1f7e80b-1build1.
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 | ;;; Copyright (c) 2000-2015 Dipanwita Sarkar, Andrew W. Keep, R. Kent Dybvig, Oscar Waddell
;;; See the accompanying file Copyright for details
(library (nanopass meta-syntax-dispatch)
(export meta-syntax-dispatch)
(import (rnrs)
(nanopass helpers)
(nanopass records))
;; (fields->patterns '(e0 e1 e2)) => (any any any)
;; (fields->patterns '(e0 ...)) => ((each+ any () ()))
;; (fields->patterns '(e0 ... e1)) => ((each+ any (any) ()))
;; (fields->patterns '(e0 ... e1 e2)) => ((each+ any (any any) ()))
;; (fields->patterns '(([x e0] ...) e1 e2 ...)) =>
;; ((each+ (any any) () ())) any (each+ (any) () ()))
;;; syntax-dispatch expects an expression and a pattern. If the expression
;;; matches the pattern a list of the matching expressions for each
;;; "any" is returned. Otherwise, #f is returned.
;;; The expression is matched with the pattern as follows:
;;; p in pattern: matches:
;;; () empty list
;;; any anything
;;; (p1 . p2) pair (list)
;;; each-any any proper list
;;; #(each p) (p*)
;;; #(each+ p1 (p2_1 ...p2_n) p3) (p1* (p2_n ... p2_1) . p3)
(define match-each
(lambda (e p)
(syntax-case e ()
[(a dots . d)
(and (not (ellipsis? #'a)) (not (unquote? #'a)) (ellipsis? #'dots))
(let ([first (match #'a p '())])
(and first
(let ([rest (match-each #'d p)])
(and rest (cons (map make-nano-dots first) rest)))))]
[(a . d)
(and (not (ellipsis? #'a)) (not (unquote? #'a)))
(let ([first (match #'a p '())])
(and first
(let ([rest (match-each #'d p)])
(and rest (cons first rest)))))]
[() '()]
[else #f])))
(define match-each+
(lambda (e x-pat y-pat z-pat r)
(let f ([e e])
(syntax-case e ()
[(a dots . d)
(and (not (ellipsis? #'a)) (not (unquote? #'a)) (ellipsis? #'dots))
(let-values ([(xr* y-pat r) (f #'d)])
(if r
(if (null? y-pat)
(let ([xr (match #'a x-pat '())])
(if xr
(values (cons (map make-nano-dots xr) xr*) y-pat r)
(values #f #f #f)))
(values '() (cdr y-pat) (match #'a (car y-pat) r)))
(values #f #f #f)))]
[(a . d)
(and (not (ellipsis? #'a)) (not (unquote? #'a)))
(let-values ([(xr* y-pat r) (f #'d)])
(if r
(if (null? y-pat)
(let ([xr (match #'a x-pat '())])
(if xr
(values (cons xr xr*) y-pat r)
(values #f #f #f)))
(values '() (cdr y-pat) (match #'a (car y-pat) r)))
(values #f #f #f)))]
[_ (values '() y-pat (match e z-pat r))]))))
(define match-each-any
(lambda (e)
(syntax-case e ()
[(a dots . d)
(and (not (ellipsis? #'a)) (not (unquote? #'a)) (ellipsis? #'dots))
(let ([l (match-each-any #'d)])
(and l (cons (make-nano-dots #'a) l)))]
[(a . d)
(and (not (ellipsis? #'a)) (not (unquote? #'a)))
(let ([l (match-each-any #'d)])
(and l (cons #'a l)))]
[() '()]
[_ #f])))
(define match-empty
(lambda (p r)
(cond
[(null? p) r]
[(eq? p 'any) (cons '() r)]
[(pair? p) (match-empty (car p) (match-empty (cdr p) r))]
[(eq? p 'each-any) (cons '() r)]
[else
(case (vector-ref p 0)
[(each) (match-empty (vector-ref p 1) r)]
[(each+) (match-empty
(vector-ref p 1)
(match-empty
(reverse (vector-ref p 2))
(match-empty (vector-ref p 3) r)))])])))
(define match*
(lambda (e p r)
(cond
[(null? p) (syntax-case e () [() r] [_ #f])]
[(pair? p)
(syntax-case e ()
[(a . d) (match #'a (car p) (match #'d (cdr p) r))]
[_ #f])]
[(eq? p 'each-any)
(let ([l (match-each-any e)]) (and l (cons l r)))]
[else
(case (vector-ref p 0)
[(each)
(syntax-case e ()
[() (match-empty (vector-ref p 1) r)]
[_ (let ([r* (match-each e (vector-ref p 1))])
(and r* (combine r* r)))])]
[(each+)
(let-values ([(xr* y-pat r)
(match-each+ e (vector-ref p 1) (vector-ref p 2)
(vector-ref p 3) r)])
(and r (null? y-pat)
(if (null? xr*)
(match-empty (vector-ref p 1) r)
(combine xr* r))))])])))
(define match
(lambda (e p r)
(cond
[(not r) #f]
[(eq? p 'any)
(and (not (ellipsis? e))
(not (unquote? e)) ; avoid matching unquote
(cons e r))]
[else (match* e p r)])))
(define meta-syntax-dispatch
(lambda (e p)
(match e p '()))))
|