Skip to content

Commit 94b3d0a

Browse files
Copilotjackfirth
andcommitted
Complete refactoring rules for define-syntax-parse-rule
Co-authored-by: jackfirth <[email protected]>
1 parent f248b60 commit 94b3d0a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ test: "define-syntax with syntax-parse using stx name refactorable to define-syn
4949
------------------------------
5050

5151

52+
no-change-test: "define-syntax with syntax-parse using custom name in directives not refactorable"
53+
------------------------------
54+
(define-syntax my-macro
55+
(lambda (input-stx)
56+
(syntax-parse input-stx
57+
[(_ x:id)
58+
#:with loc input-stx
59+
#'(quote (x loc))])))
60+
------------------------------
61+
62+
5263
no-change-test: "define-syntax with syntax-parse and multiple clauses not refactorable"
5364
------------------------------
5465
(define-syntax my-or

default-recommendations/syntax-parse-shortcuts.rkt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,23 @@ equivalent `define-syntax-parse-rule` macro."
2929
(syntax-parse-id:id parsed-stx:id
3030
[(_ . pattern) body ...])))
3131

32+
#: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]))
43+
(define body-has-stx-ref?
44+
(ormap has-stx-ref? (attribute body)))]
45+
3246
#:when (and (free-identifier=? (attribute stx-var) (attribute parsed-stx))
33-
(equal? (syntax-e (attribute syntax-parse-id)) 'syntax-parse))
47+
(equal? (syntax-e (attribute syntax-parse-id)) 'syntax-parse)
48+
(not body-has-stx-ref?))
3449

3550
(define-syntax-parse-rule (macro . pattern) body ...))
3651

0 commit comments

Comments
 (0)