-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspec-check.rkt
77 lines (71 loc) · 2.35 KB
/
spec-check.rkt
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
#lang racket
(provide in-spec?/edit)
(define (in-spec?-module/edit mod)
(match mod
[`(Module . ,stmts)
(match-let ([(list in? nstmts) (in-spec?-stmts/edit stmts)])
`(,in? (Module ,@nstmts)))]
[else '(#f ())]))
(define (in-spec?-stmts/edit stmts)
(foldr
(lambda (v acc)
(match-let ([(list in? stmt) (in-spec?-stmt/edit v)]
[(list cin? done) acc])
(list (and in? cin?) (if (not (null? stmt)) (cons stmt done) done))))
'(#t ())
stmts))
(define (in-spec?-expr/edit expr)
(match expr
[`(Starred ,expr) '(#f ())]
; Assume that sxpy catches everything else
[else (list #t else)]))
(define (in-spec?-args/edit args)
(match args
[(and `(Arguments
,(list 'args (? symbol?) ...)
,(list 'arg-types (? false?) ...)
(vararg ,(or (? symbol?) #f))
,(list 'kwonlyargs (? symbol?) ...)
,(list 'kwonlyarg-types (? false?) ...)
,(list 'kw_defaults (? false?) ...)
(kwarg ,(or (? symbol?) #f))
,(list 'defaults (? false?) ...)) args)
(list #t args)]
[else '(#f ())]))
(define (in-spec?-stmt/edit stmt)
(match stmt
[`(FunctionDef
(name ,(? symbol? name))
(args ,args)
(body . ,body)
(decorator_list . ,_)
(returns #f))
(match-let ([(list in-args? nargs) (in-spec?-args/edit args)]
[(list in-body? nbody) (in-spec?-stmts/edit body)])
(list (and in-args? in-body?)
`(FunctionDef
(name ,name)
(args ,nargs)
(body . ,nbody)
(decorator_list)
(returns #f))))]
[`(ClassDef . ,rest) '(#f ())]
[`(Return . ,expr)
(if (not (= (length expr) 0))
(match-let ([(list in? nexp) (in-spec?-expr/edit (car expr))])
`(,in? (Return ,nexp)))
'(#f ()))]
[`(Assign (targets . ,texpr) (value ,vexpr))
(if (not (= (length texpr) 1))
'(#f ())
(match-let ([(list in-targets? nt) (in-spec?-expr/edit (car texpr))]
[(list in-value? nv) (in-spec?-expr/edit vexpr)])
(list (and in-targets? in-value)
`(Assign (targets ,nt) (value ,nv)))))]
; Assume that sxpy will catch anything else
[else (list #t else)]))
(define (in-spec?/edit tree)
(if (not (null? tree))
(match-let ([(list in? module) (in-spec?-module/edit (car tree))])
(list in? (list module)))
'(#f ())))