Re: [racket-users] how to match unbound identifier as literal in `syntax-parse`?

2018-04-03 Thread Matthew Butterick

> On Apr 3, 2018, at 4:37 AM, Ryan Culpepper  wrote:
> 
> Here's one way:
> 
>  (~and z:id
>   (~fail #:unless (free-identifier=? #'z #'zeta)
>  "expected the identifier `zeta`"))
> 
> Another way is to make a syntax class (either specifically for `zeta` or 
> parameterized by the identifier) that does the same check.



What about just using `~literal`? It seems to be more lenient than `#:literals` 
(because it accepts unbound identifiers) but also relies on the 
`free-identifier=?` predicate. [1] 


[1] 
https://docs.racket-lang.org/syntax/stxparse-patterns.html?q=~literal#%28form._%28%28lib._syntax%2Fparse..rkt%29._~7eliteral%29%29
 



-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] how to match unbound identifier as literal in `syntax-parse`?

2018-04-03 Thread Ryan Culpepper

Here's one way:

  (~and z:id
   (~fail #:unless (free-identifier=? #'z #'zeta)
  "expected the identifier `zeta`"))

Another way is to make a syntax class (either specifically for `zeta` or 
parameterized by the identifier) that does the same check.


Ryan


On 4/3/18 8:33 AM, Matthew Butterick wrote:

Consider the difference in results below. How would I persuade `syntax-parse` 
to match literal identifiers in the `free-identifier=?` sense that 
`syntax-case` does? (and thereby allow unbound identifiers to be compared also)

IIUC, `#:literals` is too strict, as it insists that every listed identifier 
have a binding; `#:datum-literals` is too lenient, as it does not compare 
bindings at all, but rather just the symbol.

;

#lang racket
(require math rackunit)

(module mod racket
  (require (for-syntax syntax/parse))
  (define-syntax (mac stx)
(syntax-parse stx
  #:literals () ;; <- can't put `zeta` here, produces an error
  #:datum-literals (zeta)
  [(_ zeta) #''match]
  [else #''nope]))
  (provide mac))
(require 'mod)

(check-equal? (mac zeta) 'match)


(module mod2 racket
  (require (for-syntax syntax/parse))
  (define-syntax (mac2 stx)
(syntax-case stx (zeta)
  [(_ zeta) #''match]
  [else #''nope]))
  (provide mac2))
(require 'mod2)

(check-equal? (mac2 zeta) 'nope)



--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] how to match unbound identifier as literal in `syntax-parse`?

2018-04-03 Thread Matthew Butterick
Consider the difference in results below. How would I persuade `syntax-parse` 
to match literal identifiers in the `free-identifier=?` sense that 
`syntax-case` does? (and thereby allow unbound identifiers to be compared also)

IIUC, `#:literals` is too strict, as it insists that every listed identifier 
have a binding; `#:datum-literals` is too lenient, as it does not compare 
bindings at all, but rather just the symbol.

;

#lang racket
(require math rackunit)

(module mod racket
  (require (for-syntax syntax/parse))
  (define-syntax (mac stx)
(syntax-parse stx
  #:literals () ;; <- can't put `zeta` here, produces an error
  #:datum-literals (zeta)
  [(_ zeta) #''match]
  [else #''nope]))
  (provide mac))
(require 'mod)

(check-equal? (mac zeta) 'match) 


(module mod2 racket
  (require (for-syntax syntax/parse))
  (define-syntax (mac2 stx)
(syntax-case stx (zeta)
  [(_ zeta) #''match]
  [else #''nope]))
  (provide mac2))
(require 'mod2)

(check-equal? (mac2 zeta) 'nope)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.