Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions default-recommendations/syntax-parse-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,34 @@ no-change-test: "define-syntax-parser with multiple clauses not refactorable"
[(_ a)
#'a])
------------------------------


test: "define-syntax-parser with pattern directives refactorable to define-syntax-parse-rule"
------------------------------
(define-syntax-parser my-macro
[(_ x:id)
#:with y #'(x x)
#'(quote y)])
==============================
(define-syntax-parse-rule (my-macro x:id)
#:with y #'(x x)
(quote y))
------------------------------


no-change-test: "define-syntax-parser with syntax/loc on the output not refactorable"
------------------------------
(define-syntax-parser my-or
[(_ a b)
(syntax/loc this-syntax
(let ([tmp a]) (if tmp tmp b)))])
------------------------------


no-change-test: "define-syntax-parser with non-directive body forms not refactorable"
------------------------------
(define-syntax-parser my-or
[(_ a b)
(printf "expanding my-or\n")
#'(let ([tmp a]) (if tmp tmp b))])
------------------------------
21 changes: 7 additions & 14 deletions default-recommendations/syntax-parse-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,14 @@ equivalent `define-syntax-parse-rule` macro."
#:description
"This `define-syntax-parser` macro with a single clause can be replaced with a simpler, equivalent
`define-syntax-parse-rule` macro."
#:literals (define-syntax-parser)
#:literals (define-syntax-parser [syntax-id syntax #:phase 1])

(define-syntax-parser macro:id
[(_ . pattern) body ...])

#:do [(define (strip-syntax-wrapper stx)
(syntax-parse stx
#:literals (syntax)
[(syntax body) #'body]
[other #'other]))
(define new-body (map strip-syntax-wrapper (attribute body)))]

#:with (new-body-part ...) new-body

(define-syntax-parse-rule (macro . pattern) new-body-part ...))
[(_ . pattern) directive:syntax-parse-pattern-directive ... (syntax-id last-form)])

#:with (new-body ...) #'((~@ . directive) ... last-form)

(define-syntax-parse-rule (macro . pattern) new-body ...))


(define-refactoring-suite syntax-parse-shortcuts
Expand Down
Loading