From 627d164a46dcb85c212e8901685b7f3ba9629be2 Mon Sep 17 00:00:00 2001 From: lkh01 Date: Thu, 14 Nov 2019 23:16:53 +0100 Subject: [PATCH] make `syntax->string` work with pairs and `_` fixes #1628 and #1629 --- pkgs/racket-test/tests/syntax/to-string.rkt | 9 +++++++++ racket/collects/syntax/to-string.rkt | 19 +++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 pkgs/racket-test/tests/syntax/to-string.rkt diff --git a/pkgs/racket-test/tests/syntax/to-string.rkt b/pkgs/racket-test/tests/syntax/to-string.rkt new file mode 100644 index 0000000000..aac5d117ea --- /dev/null +++ b/pkgs/racket-test/tests/syntax/to-string.rkt @@ -0,0 +1,9 @@ +#lang racket +(require syntax/to-string + rackunit) + +(check-equal? (syntax->string #'((a . + b))) "(a . \n b)") +(check-equal? (syntax->string #'((a b c d))) "(a b c d)") +(check-equal? (syntax->string #'(a 'b #(a b c) c)) "a 'b #(a b c) c") +(check-equal? (syntax->string #'((a b _ d))) "(a b _ d)") diff --git a/racket/collects/syntax/to-string.rkt b/racket/collects/syntax/to-string.rkt index 5254121fc0..a35664d56e 100644 --- a/racket/collects/syntax/to-string.rkt +++ b/racket/collects/syntax/to-string.rkt @@ -27,7 +27,10 @@ (cond [(eq? 'code:blank (syntax-e c)) (advance c init-line!)] - [(eq? '_ (syntax-e c)) (void)] + [(eq? '_ (syntax-e c)) + (advance c init-line!) + (printf "_") + (set! col (+ col 1))] [(eq? '... (syntax-e c)) (void)] [(and (pair? (syntax-e c)) @@ -58,7 +61,19 @@ (define c-paren-shape (syntax-property c 'paren-shape)) (printf "~a" (or c-paren-shape #\()) (set! col (+ col 1)) - (map (loop init-line!) (syntax->list c)) + (define se (syntax-e c)) + (define (build-string-from-pair sp) + (cond + [(syntax? sp) + (printf " . ") + (set! col (+ col 3)) + ((loop init-line!) sp)] + [else + ((loop init-line!) (car sp)) + (build-string-from-pair (cdr sp))])) + (if (list? se) + (map (loop init-line!) se) + (build-string-from-pair se)) (printf (case c-paren-shape [(#\[) "]"] [(#\{) "}"]