Why does syntax-parser work here but not syntax-parse?

27 views
Skip to first unread message

David Storrs

unread,
Sep 8, 2021, 1:13:03 PM9/8/21
to Racket Users
This is from the documentation and it obviously works:

(define parser1                
  (syntax-parser
    [((~alt (~once (~seq #:a x) #:name "#:a keyword")
            (~optional (~seq #:b y) #:name "#:b keyword")
            (~seq #:c z)) ...)
     'ok]))
(parser1 #'(#:a 1))

When run it yields 'ok.

If I change it to this, it fails and I don't understand why:

(define (parser2 stx)
  (syntax-parse stx
    [(parser2 ((~alt (~once (~seq #:a x) #:name "#:a keyword")
                     (~optional (~seq #:b y) #:name "#:b keyword")
                     (~seq #:c z)) ...))
     #''ok]))
(parser2 (#:a 1))

This yields:
[...source location...] #%datum: keyword misused as an expression
;   at: #:a
                                                        


I can see that (#:a 1) is not valid under the default parser since #:a is not valid for initial position but shouldn't the entire parenthesized expression be given to the macro processor and then replaced with something valid before being rejected?

Stephen Chang

unread,
Sep 8, 2021, 2:03:03 PM9/8/21
to David Storrs, Racket Users
> shouldn't the entire parenthesized expression be given to the macro processor and then replaced with something valid before being rejected?

That would be true if you're defining a macro, i.e. if you use
`define-syntax` instead of `define`.

>
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAE8gKofDYFY8AYb2Lyh7CQXx1i01w8jm4DVcMrJL%3DQYHX9SsRQ%40mail.gmail.com.

David Storrs

unread,
Sep 8, 2021, 3:02:14 PM9/8/21
to Stephen Chang, Racket Users
*headdesk headdesk headdesk headdesk headdesk*

Thank you.
Reply all
Reply to author
Forward
0 new messages