colon infix operator in syntax-parse ?

39 views
Skip to first unread message

Thomas Lynch

unread,
Nov 25, 2015, 2:51:01 AM11/25/15
to Racket Developers
Wolfram language has a feature where predicates or head value checks can be added to a pattern.  These occur after the 
_ or other pattern match operator.  

Now I'm looking at syntax-parse and I see that adding a x:id , for example will check that x is an id.  That makes the colon an operator.  It also looks like it is going to mess up code that does such thing as using prefix on require, where that prefix is followed by a colon.  The lex docs, for example, show this.

Shouldn't this be (x id)  rather than x:id ?  As a case of (x predicate).




Alex Knauth

unread,
Nov 25, 2015, 8:54:36 AM11/25/15
to Thomas Lynch, Racket Developers
On Nov 25, 2015, at 2:51 AM, Thomas Lynch <thomas...@reasoningtechnology.com> wrote:

Wolfram language has a feature where predicates or head value checks can be added to a pattern.  These occur after the 
_ or other pattern match operator.  

Now I'm looking at syntax-parse and I see that adding a x:id , for example will check that x is an id.  That makes the colon an operator.  

Not really, because syntax-parse looks for identifiers with names that contain colon as a special case.

It also looks like it is going to mess up code that does such thing as using prefix on require, where that prefix is followed by a colon.  The lex docs, for example, show this.

Shouldn't this be (x id)  rather than x:id ?  As a case of (x predicate).


There is the option to use (~var x id) and (~literal lit-id), which you might have to use when making a macro that expands to a syntax-parse expression. If you want programs that make the most explicit sense possible, (~var x id) is better, but if you want programs that are convenient and readable, x:id looks better.

This means that you can still use syntax classes when they have prefixes with colons, for example:
#lang racket
(require (only-in syntax/parse ~literal ~var)
         (prefix-in rkt: racket/base)
         (prefix-in stxparse: syntax/parse))
(syntax->datum
 (stxparse:syntax-parse #'(define x 3)
   [((~literal rkt:define) (~var x stxparse:id) (~var v stxparse:expr))
    #'(define-values [x] v)]))

So syntax/parse doesn't apply the colon special case within the ~var form or the ~literal form.

Although actually this works too:
#lang racket
(require (only-in syntax/parse ~literal ~var)
         (prefix-in rkt: racket/base)
         (prefix-in stxparse: syntax/parse))
(syntax->datum
 (stxparse:syntax-parse #'(define x 3) #:literals (rkt:define)
   [(rkt:define x:stxparse:id v:stxparse:expr)
    #'(define-values [x] v)]))

Alex Knauth

Reply all
Reply to author
Forward
0 new messages