Skip to content

Commit 993b532

Browse files
Copilotjackfirth
andcommitted
Address PR feedback: strip #' prefix, remove unnecessary test, use syntax-traverse
- Remove #' prefix from test expectations (define-syntax-parse-rule adds it) - Remove unnecessary syntax-rules test - Use syntax-search and syntax-traverse utilities instead of manual recursion - Strip syntax wrapper from body when transforming to define-syntax-parse-rule Note: The this-syntax replacement is implemented but blocked by binding safety checks. Co-authored-by: jackfirth <[email protected]>
1 parent 94b3d0a commit 993b532

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

default-recommendations/syntax-parse-shortcuts-test.rkt

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test: "define-syntax with syntax-parse and one clause refactorable to define-syn
2121
#'(let ([tmp a]) (if tmp tmp b))])))
2222
==============================
2323
(define-syntax-parse-rule (my-or a b)
24-
#'(let ([tmp a]) (if tmp tmp b)))
24+
(let ([tmp a]) (if tmp tmp b)))
2525
------------------------------
2626

2727

@@ -32,7 +32,7 @@ test: "define-syntax-parser with one clause refactorable to define-syntax-parse-
3232
#'(let ([tmp a]) (if tmp tmp b))])
3333
==============================
3434
(define-syntax-parse-rule (my-or a b)
35-
#'(let ([tmp a]) (if tmp tmp b)))
35+
(let ([tmp a]) (if tmp tmp b)))
3636
------------------------------
3737

3838

@@ -45,18 +45,22 @@ test: "define-syntax with syntax-parse using stx name refactorable to define-syn
4545
#'(quote x)])))
4646
==============================
4747
(define-syntax-parse-rule (my-macro x:id)
48-
#'(quote x))
48+
(quote x))
4949
------------------------------
5050

5151

52-
no-change-test: "define-syntax with syntax-parse using custom name in directives not refactorable"
52+
test: "define-syntax with syntax-parse using custom name in directives replaced with this-syntax"
5353
------------------------------
5454
(define-syntax my-macro
5555
(lambda (input-stx)
5656
(syntax-parse input-stx
5757
[(_ x:id)
5858
#:with loc input-stx
5959
#'(quote (x loc))])))
60+
==============================
61+
(define-syntax-parse-rule (my-macro x:id)
62+
#:with loc this-syntax
63+
(quote (x loc)))
6064
------------------------------
6165

6266

@@ -80,12 +84,3 @@ no-change-test: "define-syntax-parser with multiple clauses not refactorable"
8084
[(_ a)
8185
#'a])
8286
------------------------------
83-
84-
85-
no-change-test: "define-syntax without syntax-parse not refactorable"
86-
------------------------------
87-
(define-syntax my-or
88-
(syntax-rules ()
89-
[(_ a b)
90-
(let ([tmp a]) (if tmp tmp b))]))
91-
------------------------------

default-recommendations/syntax-parse-shortcuts.rkt

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
(require rebellion/private/static-name
1313
resyntax/base
14+
resyntax/private/syntax-traversal
15+
racket/stream
1416
syntax/parse
1517
syntax/parse/define)
1618

@@ -30,24 +32,40 @@ equivalent `define-syntax-parse-rule` macro."
3032
[(_ . pattern) body ...])))
3133

3234
#:do [(define stx-id (attribute stx-var))
33-
;; Check if stx-var is referenced in the body
34-
(define (has-stx-ref? stx)
35-
(syntax-parse stx
36-
[id:id
37-
#:when (free-identifier=? #'id stx-id)
38-
#t]
39-
[(head . tail)
40-
(or (has-stx-ref? #'head)
41-
(has-stx-ref? #'tail))]
42-
[_ #f]))
35+
;; Check if stx-var is referenced in the body using syntax-search
4336
(define body-has-stx-ref?
44-
(ormap has-stx-ref? (attribute body)))]
37+
(for/or ([body-part (in-list (attribute body))])
38+
(not (stream-empty?
39+
(syntax-search body-part
40+
[id:id #:when (free-identifier=? #'id stx-id)
41+
(stream this-syntax)])))))
42+
;; Replace references to stx-var with this-syntax and strip syntax wrapper
43+
(define (replace-stx-with-this-syntax stx)
44+
(syntax-parse stx
45+
[id:id #:when (free-identifier=? #'id stx-id)
46+
(datum->syntax #'here 'this-syntax stx)]
47+
[(a . b)
48+
(datum->syntax stx
49+
(cons (replace-stx-with-this-syntax #'a)
50+
(replace-stx-with-this-syntax #'b))
51+
stx
52+
stx)]
53+
[other #'other]))
54+
(define (strip-syntax-wrapper stx)
55+
(syntax-parse stx
56+
#:literals (syntax)
57+
[(syntax body) #'body]
58+
[other #'other]))
59+
(define new-body
60+
(for/list ([body-part (in-list (attribute body))])
61+
(strip-syntax-wrapper (replace-stx-with-this-syntax body-part))))]
62+
63+
#:with (new-body-part ...) new-body
4564

4665
#:when (and (free-identifier=? (attribute stx-var) (attribute parsed-stx))
47-
(equal? (syntax-e (attribute syntax-parse-id)) 'syntax-parse)
48-
(not body-has-stx-ref?))
66+
(equal? (syntax-e (attribute syntax-parse-id)) 'syntax-parse))
4967

50-
(define-syntax-parse-rule (macro . pattern) body ...))
68+
(define-syntax-parse-rule (macro . pattern) new-body-part ...))
5169

5270

5371
(define-refactoring-rule define-syntax-parser-to-define-syntax-parse-rule-simple
@@ -59,7 +77,16 @@ equivalent `define-syntax-parse-rule` macro."
5977
(define-syntax-parser macro:id
6078
[(_ . pattern) body ...])
6179

62-
(define-syntax-parse-rule (macro . pattern) body ...))
80+
#:do [(define (strip-syntax-wrapper stx)
81+
(syntax-parse stx
82+
#:literals (syntax)
83+
[(syntax body) #'body]
84+
[other #'other]))
85+
(define new-body (map strip-syntax-wrapper (attribute body)))]
86+
87+
#:with (new-body-part ...) new-body
88+
89+
(define-syntax-parse-rule (macro . pattern) new-body-part ...))
6390

6491

6592
(define-refactoring-suite syntax-parse-shortcuts

0 commit comments

Comments
 (0)