I never used syntax-parse before. I have to look into it.
Many thanks, of course,
Jos Koot

  _____  

From: racket-users@googlegroups.com [mailto:racket-users@googlegroups.com]
On Behalf Of Alexander D. Knauth
Sent: domingo, 05 de abril de 2015 15:08
To: Jos Koot
Cc: racket-users@googlegroups.com
Subject: Re: [racket-users] Questions: free-identifier=?; literal-id in
syntax-case



On Apr 5, 2015, at 4:07 AM, Jos Koot <jos.k...@gmail.com> wrote:


Your argument that a literal-id may occur more than once in a pattern makes
sense.
I'll play with syntax-parse and (_ (~and + (~literal +)) (~literal +)).
May be I am able to make a syntax transformer, say my-syntax-case,
available in expansion-phase,
that does what you have suggested.
 
Thanks again,
Jos Koot


You could also use this, from Ryan Culpepper's reply on another thread:
#lang racket
(require (for-syntax syntax/parse))
(define-syntax (a stx)
 (syntax-parse stx #:literals (+)
  [(_ op:+) (raise-syntax-error 'a "msg" stx #'op)]))
(a +)



  _____  

On Apr 4, 2015, at 11:22 AM, Jos Koot <jos.k...@gmail.com> wrote:


As another question: the docs on syntax-case state:

 
"An id that has the same binding as a literal-id matches a syntax object
that is an identifier with the same binding in the sense of
free-identifier=?
<file:///C:/Program%20Files/Racket/doc/reference/stxcmp.html?q=syntax-case#%
28def._%28%28quote._~23~25kernel%29._free-identifier~3d~3f%29%29> .
The match does not introduce any
<file:///C:/Program%20Files/Racket/doc/reference/stx-patterns.html?q=syntax-
case#%28tech._pattern._variable%29> pattern variables."
 
Why isn't or cant't the match introduce a pattern variable?
Without binding the literal-id as a pattern variable,
location information is lost, for example:
 
#lang racket
(error-print-source-location #t)
(begin-for-syntax (error-print-source-location #t))
(define-syntax (a stx)
 (syntax-case stx (+)
  ((_ +) (raise-syntax-error 'a "msg" stx #'+)))) ; <---
(a +)
 
raises a syntax-error as expected, but it highlights + in the line marked
<---,
not in the last line.
I would prefer the + in the last line to be highlighted.


I mostly agree with that idea, but there is a reason why it doesn't do that.
If it did that, then I don't think you could do things like put multiple of
these in a single pattern, so for instance:
(define-syntax (a stx)
  (syntax-case stx (+)
    [(_ + +) (raise-syntax-error 'a "msg" stx #'+)]))
(a +)
It wouldn't know which + to bind.

The way I do this which feels a bit clunky like there should be a better
solution:
(define-syntax (a stx)
  (syntax-parse stx
    [(_ (~and + (~literal +)) (~literal +)) (raise-syntax-error 'a "msg" stx
#'+)]))
To tell it specifically to bind it.

To make it a little less verbose, you could define a pattern-expander that
expanded to (~and + (~literal +)), but I'm not sure how to do better than
that.


-- 
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.

-- 
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.

Reply via email to