From e117e4f919bf524040ff33c2854eec7fadb10567 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= <georges.duperon@gmail.com>
Date: Mon, 23 Jan 2017 19:55:26 +0100
Subject: [PATCH] Modified syntax-case & friends to use with-pvars. Closes 181
 current-pvars: use in syntax-case, with-syntax, define/with-syntax, is there
 any with-parse?

---
 case.rkt                        | 10 ++++++++++
 case/stxcase.rkt                |  9 +++++++--
 case/syntax.rkt                 |  4 +++-
 scribblings/stxparse-info.scrbl | 34 +++++++++++++++++++++++++++------
 4 files changed, 48 insertions(+), 9 deletions(-)
 create mode 100644 case.rkt

diff --git a/case.rkt b/case.rkt
new file mode 100644
index 0000000..37657c0
--- /dev/null
+++ b/case.rkt
@@ -0,0 +1,10 @@
+(module case '#%kernel
+  (#%require "case/stxloc.rkt"
+             "case/syntax.rkt"
+             "case/with-stx.rkt")
+  (#%provide syntax-case
+             syntax-case*
+             define/with-syntax
+             with-syntax
+             datum-case
+             with-datum))
\ No newline at end of file
diff --git a/case/stxcase.rkt b/case/stxcase.rkt
index ccc5015..4ccca56 100644
--- a/case/stxcase.rkt
+++ b/case/stxcase.rkt
@@ -4,6 +4,7 @@
 (module stxcase '#%kernel
   (#%require racket/private/stx racket/private/small-scheme '#%paramz '#%unsafe
              racket/private/ellipses
+             stxparse-info/current-pvars
              (for-syntax racket/private/stx racket/private/small-scheme
                          racket/private/member racket/private/sc '#%kernel))
 
@@ -484,9 +485,13 @@
                                              null
                                              (if fender
                                                  (list (quote-syntax if) fender
-                                                       answer
+                                                       (list (quote-syntax with-pvars)
+                                                             pattern-vars
+                                                             answer)
                                                        do-try-next)
-                                                 answer)))
+                                                 (list (quote-syntax with-pvars)
+                                                       pattern-vars
+                                                       answer))))
                                            do-try-next))])
                               (if fender
                                   (list
diff --git a/case/syntax.rkt b/case/syntax.rkt
index 99782d0..3e585f3 100644
--- a/case/syntax.rkt
+++ b/case/syntax.rkt
@@ -1,5 +1,6 @@
 #lang racket/base
 (require (only-in "stxloc.rkt" syntax-case)
+         stxparse-info/current-pvars
          (for-syntax racket/base
                      racket/private/sc))
 (provide define/with-syntax
@@ -45,7 +46,8 @@
                       (values (pvar-value pvar) ...)))
                   (define-syntax pvar
                     (make-syntax-mapping 'depth (quote-syntax valvar)))
-                  ...)))]))
+                  ...
+                  (define-pvars (pvar ...)))))]))
 ;; Ryan: alternative name: define/syntax-pattern ??
 
 ;; auxiliary macro
diff --git a/scribblings/stxparse-info.scrbl b/scribblings/stxparse-info.scrbl
index d6e7a26..b5a2b51 100644
--- a/scribblings/stxparse-info.scrbl
+++ b/scribblings/stxparse-info.scrbl
@@ -19,12 +19,12 @@
 
 Source code: @url{https://github.com/jsmaniac/stxparse-info}
 
-@defmodule[stxparse-info/parse]
+@defmodule[stxparse-info]
 
-The module @racketmodname[stxparse-info/parse] is a patched version of
-@racketmodname[syntax/parse] which tracks which syntax pattern variables are
-bound. This allows some libraries to change the way syntax pattern variables
-work.
+This library provides some patched versions of @racketmodname[syntax/parse]
+and the @racket[syntax-case] family. These patched versions track which syntax
+pattern variables are bound. This allows some libraries to change the way
+syntax pattern variables work.
 
 For example, @racketmodname[phc-graph/subtemplate] automatically derives
 temporary identifiers when a template contains @racket[yᵢ …], and @racket[xᵢ]
@@ -32,6 +32,23 @@ is a pattern variable. To know from which @racket[varᵢ] the @racket[yᵢ …]
 identifiers must be derived, @racketmodname[phc-graph/subtemplate] needs to
 know which syntax pattern variables are within scope.
 
+@section{Tracking currently-bound pattern variables with @racket[syntax-parse]}
+
+@defmodule[stxparse-info/parse]
+
+The module @racketmodname[stxparse-info/parse] provides patched versions of
+@racketmodname[syntax/parse] @racketmodname[define/syntax-parse] which track
+which syntax pattern variables are bound.
+
+@section{Tracking currently-bound pattern variables with @racket[syntax-parse]}
+
+@defmodule[stxparse-info/case]
+
+The module @racketmodname[stxparse-info/case] provides patched versions of
+@racket[syntax-case], @racket[syntax-case*], @racket[with-syntax],
+@racket[define/with-syntax], @racket[datum-case] and @racket[with-datum] which
+track which syntax or datum pattern variables are bound.
+
 @section{Reading and updating the list of currently-bound pattern variables}
 
 @defmodule[stxparse-info/current-pvars] 
@@ -40,7 +57,12 @@ know which syntax pattern variables are within scope.
          (current-pvars) (listof identifier?)]{
  This for-syntax procedure returns the list of syntax pattern variables which
  are known to be bound. The most recently bound variables are at the beginning
- of the list.}
+ of the list.
+
+ It is the responsibility of the reader to check that the identifiers are
+ bound, and that they are bound to syntax pattern variables, for example using
+ @racket[identifier-binding] and @racket[syntax-pattern-variable?]. This allows
+ libraries to also track variables bound by match-like forms, for example.}
 
 @defform[(with-pvars (pvar ...) . body)
          #:contracts ([pvar identifier?])]{