summaryrefslogtreecommitdiff
path: root/ebus-racket/3rdparty/bzlib/base/args.ss
blob: dd4659a4e527bfd2735a6bd7945485d47924154c (plain)
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
#lang scheme/base
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BASE.plt 
;; 
;; 
;; Bonzai Lab, LLC.  All rights reserved.
;; 
;; Licensed under LGPL.
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; args.ss - utility for helping processing syntax-based arguments (does not belong here)
;; yc 9/21/2009 - first version 
;; yc 9/25/2009 - move from port.plt to base.plt 
(require (for-syntax scheme/base) 
         scheme/match)

;; convert an argument (and an optional argument) into an identifier
;; p => p 
;; (p v ...) => p 
(define (arg->identifier stx)
  (syntax-case stx ()
    (p
     (symbol? (syntax->datum #'p))
     #'p)
    ;; an optional arg.
    ((p . _)
     #'p)))

;; (a (b v1) #:c (c v2)) => (a b c) 
(define (args->identifiers stx)
  (syntax-case stx () 
    (()
     #'())
    ((p . rest)
     (keyword? (syntax->datum #'p))
     (args->identifiers #'rest))
    ((p . rest)
     #`(#,(arg->identifier #'p) . #,(args->identifiers #'rest)))))

(define (args->kw+identifiers stx)
  (syntax-case stx () 
    (()
     #'())
    ((p . rest)
     (keyword? (syntax->datum #'p))
     #`(p . #,(args->identifiers #'rest)))
    ((p . rest)
     #`(#,(arg->identifier #'p) . #,(args->identifiers #'rest)))))

(define (args->kw-identifiers stx)
  (syntax-case stx () 
    (()
     #'())
    ((p . rest) 
     (keyword? (syntax->datum #'p))
     #`(p . #,(args->identifiers #'rest)))
    ((p . rest)
     (args->kw-identifiers #'rest))))
;; (trace args->kw-identifiers)

(define (args->kw-args stx)
  (syntax-case stx () 
    (() 
     #'())
    ((p . rest)
     (keyword? (syntax->datum #'p))
     #'(p . rest))
    ((p . rest)
     (args->kw-args #'rest))))

(define (args->non-kw-identifiers stx)
  (syntax-case stx () 
    (()
     #'())
    ((p . rest)
     (keyword? (syntax->datum #'p))
     #'())
    ((p . rest)
     #`(#,(arg->identifier #'p) . #,(args->non-kw-identifiers #'rest)))))

(define (args->non-kw-args stx)
  (syntax-case stx () 
    (()
     #'())
    ((p . rest)
     (keyword? (syntax->datum #'p))
     #'())
    ((p . rest)
     #`(p . #,(args->non-kw-args #'rest)))))

(provide arg->identifier 
         args->identifiers 
         args->kw+identifiers
         args->kw-identifiers 
         args->non-kw-identifiers
         args->kw-args 
         args->non-kw-args
         )

;;; typed args... 
;;; a typed args look like an optional argument, except that 
;;; it has the following: 
;;; (id type?) (id type? default) 
(define (typed-arg? stx)
  (match (syntax->datum stx)
    ((list (? symbol? _) _) #t)
    ((list (? symbol? _) _ _) #t)
    (else #f)))

(define (typed-arg->arg stx)
  (syntax-case stx () 
    ((p type)
     #'p)
    ((p type default)
     #'(p default))))

(define (typed-args->args stx)
  (syntax-case stx ()
    (()
     #'())
    ((p . rest)
     (keyword? (syntax->datum #'p))
     #`(p . #,(typed-args->args #'rest)))
    ((p . rest)
     #`(#,(typed-arg->arg #'p) . #,(typed-args->args #'rest)))))

(define (typed-arg->type stx)
  (syntax-case stx () 
    ((p type)
     #'type)
    ((p type default)
     #'type)))

(define (typed-args->types stx)
  (syntax-case stx () 
    (()
     #'())
    ((p . rest)
     (keyword? (syntax->datum #'p))
     (typed-args->types #'rest))
    ((p . rest)
     #`(#,(typed-arg->type #'p) . #,(typed-args->types #'rest)))))

(provide typed-args->args
         typed-args->types
         typed-arg->arg
         typed-arg->type
         )