This repository was archived by the owner on Oct 26, 2023. It is now read-only.
forked from jackfirth/resyntax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefactoring-rule.rkt
71 lines (53 loc) · 2.03 KB
/
refactoring-rule.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
#lang racket/base
(require racket/contract/base)
(provide
define-refactoring-rule
(contract-out
[refactoring-rule? predicate/c]
[refactoring-rule-description (-> refactoring-rule? immutable-string?)]
[current-source-code-analysis (-> (or/c source-code-analysis? #false))]))
(module+ private
(provide
(contract-out
[refactoring-rule-refactor
(-> refactoring-rule? syntax? #:analysis source-code-analysis?
(option/c syntax-replacement?))])))
(require (for-syntax racket/base)
rebellion/base/immutable-string
rebellion/base/option
rebellion/type/object
resyntax/private/source
resyntax/private/syntax-replacement
syntax/parse
syntax/parse/define)
;@----------------------------------------------------------------------------------------------------
(define current-source-code-analysis (make-parameter #false #false 'current-source-code-analysis))
(define-object-type refactoring-rule (transformer description)
#:omit-root-binding
#:constructor-name constructor:refactoring-rule)
(define (refactoring-rule-refactor rule syntax #:analysis analysis)
(define rule-introduction-scope (make-syntax-introducer))
(option-map
((refactoring-rule-transformer rule) (rule-introduction-scope syntax) analysis)
(λ (new-syntax)
(syntax-replacement
#:original-syntax syntax
#:new-syntax (rule-introduction-scope new-syntax)
#:introduction-scope rule-introduction-scope))))
(define-simple-macro
(define-refactoring-rule id:id
#:description description
parse-option ...
[pattern pattern-directive ... replacement])
#:declare description (expr/c #'string?)
(define id
(constructor:refactoring-rule
#:name 'id
#:description (string->immutable-string description.c)
#:transformer
(λ (stx analysis)
(parameterize ([current-source-code-analysis analysis])
(syntax-parse stx
parse-option ...
[pattern pattern-directive ... (present #'replacement)]
[_ absent]))))))