Re: [racket-users] Trying to build 'routing' macro for dispatch-rules

2016-10-29 Thread Jay McCarthy
Hi David,

The syntax of `dispatch-rules` is that its arguments are dispatch
clauses, not expressions, and it does not implement its own macro
system (like `match` does.)

You should either change `dispatch-rules` to do that by adding
"dispatch-rules-expanders" or make your own macro to expand into a
call to `dispatch-rules`

Jay

On Fri, Oct 28, 2016 at 2:49 PM, David Storrs  wrote:
> I'm trying to build a macro that will generate routing rules for
> 'dispatch-rules'.
>
> (dispatch-rules) from web-server/dispatch has this form:
>
> (dispatch-rules
>(("announce-user") #:method post process-announce-user))
>...more routes here...
> )
>
> This seems like it should be automatable, so I tried to write a macro
> to generate these rules.  I'd like
> (routing announce-user)
> to expand to the line above:(("announce-user") #:method post
> process-announce-user))
>
> Here's my stab at it, which is currently failing:
>
>
> ;;This part works if I use it directly, ie:
> ;;  [("announce-file") #:method post (make-route msg-announce-file
> announce-file*)]
> ;;
> (define (make-route msg-handler pbuf-type)
>   (lambda (req)
> (msg-handler (req->protobuf req pbuf-type
>
>
> ;;Let's try to generate the entire rule using the above
> (define-syntax (routing stx)
>   (syntax-case
>   stx ()
> ([_ pbuf-type]
>  (with-syntax* ([type-name(format-id stx "~a*" #'pbuf-type)]
> [handler-name (format-id stx "msg-~a" #'type-name)]
> [route-name   (symbol->string (syntax->datum
> (format-id stx "~a" (syntax->datum #'pbuf-type]
> )
>#'[(route-name) #:method "post" (make-route
> handler-name type-name)]
> ;;   #'(quote [(route-name) #:method "post"
> (make-route handler-name type-name)])
>
>
> ;;(routing announce-file)  ; If I use this (i.e. outside the
> dispatch-rules) in combination with the 'quote' version above, I see
> the correct thing
>
> (define-values (dispatch-request url-for)
>   (dispatch-rules
>(routing announce-file)
>[("create-user") #:method post process-create-user]
>...more routes here...
> )
>
> Error is:
>
> racket network/routing.rkt
> ~/app/network/routing.rkt:194.2: dispatch-rules: bad syntax
>   in: (dispatch-rules (routing announce-file) (("create-user")
> #:method post process-create-user) ...
>   context...:
>
> /Applications/Racket_v6.6/collects/syntax/parse/private/runtime-report.rkt:698:0:
> error/report
>
> /Applications/Racket_v6.6/collects/syntax/parse/private/runtime-report.rkt:28:0:
> call-current-failure-handler
>standard-module-name-resolver
>
>
> As mentioned in the comments, it does generate (the quoted form of)
> the correct thing if I:
>
> 1) inside the macro, comment in the 'quote' line and comment out the
> line above it
> 2) comment OUT the '(routing announce-file) that is inside dispatch-rules
> 3) comment IN the (routing announce-user) line that is just above 
> dispatch-rules
>
>
> My suspicion is that dispatch-rules is a macro and so my macro is not
> being expanded inside another macro.  That's not how I thought macros
> worked, though, so now I'm really puzzled.
>
> Can anyone clarify this for me?
>
> --
> 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.



-- 
Jay McCarthy
Associate Professor
PLT @ CS @ UMass Lowell
http://jeapostrophe.github.io

   "Wherefore, be not weary in well-doing,
  for ye are laying the foundation of a great work.
And out of small things proceedeth that which is great."
  - D 64:33

-- 
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] Trying to build 'routing' macro for dispatch-rules

2016-10-28 Thread David Storrs
I'm trying to build a macro that will generate routing rules for
'dispatch-rules'.

(dispatch-rules) from web-server/dispatch has this form:

(dispatch-rules
   (("announce-user") #:method post process-announce-user))
   ...more routes here...
)

This seems like it should be automatable, so I tried to write a macro
to generate these rules.  I'd like
(routing announce-user)
to expand to the line above:(("announce-user") #:method post
process-announce-user))

Here's my stab at it, which is currently failing:


;;This part works if I use it directly, ie:
;;  [("announce-file") #:method post (make-route msg-announce-file
announce-file*)]
;;
(define (make-route msg-handler pbuf-type)
  (lambda (req)
(msg-handler (req->protobuf req pbuf-type


;;Let's try to generate the entire rule using the above
(define-syntax (routing stx)
  (syntax-case
  stx ()
([_ pbuf-type]
 (with-syntax* ([type-name(format-id stx "~a*" #'pbuf-type)]
[handler-name (format-id stx "msg-~a" #'type-name)]
[route-name   (symbol->string (syntax->datum
(format-id stx "~a" (syntax->datum #'pbuf-type]
)
   #'[(route-name) #:method "post" (make-route
handler-name type-name)]
;;   #'(quote [(route-name) #:method "post"
(make-route handler-name type-name)])
   

;;(routing announce-file)  ; If I use this (i.e. outside the
dispatch-rules) in combination with the 'quote' version above, I see
the correct thing

(define-values (dispatch-request url-for)
  (dispatch-rules
   (routing announce-file)
   [("create-user") #:method post process-create-user]
   ...more routes here...
)

Error is:

racket network/routing.rkt
~/app/network/routing.rkt:194.2: dispatch-rules: bad syntax
  in: (dispatch-rules (routing announce-file) (("create-user")
#:method post process-create-user) ...
  context...:
   
/Applications/Racket_v6.6/collects/syntax/parse/private/runtime-report.rkt:698:0:
error/report
   
/Applications/Racket_v6.6/collects/syntax/parse/private/runtime-report.rkt:28:0:
call-current-failure-handler
   standard-module-name-resolver


As mentioned in the comments, it does generate (the quoted form of)
the correct thing if I:

1) inside the macro, comment in the 'quote' line and comment out the
line above it
2) comment OUT the '(routing announce-file) that is inside dispatch-rules
3) comment IN the (routing announce-user) line that is just above dispatch-rules


My suspicion is that dispatch-rules is a macro and so my macro is not
being expanded inside another macro.  That's not how I thought macros
worked, though, so now I'm really puzzled.

Can anyone clarify this for me?

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