[racket] syntax-parse question

From: Alexander D. Knauth (alexander at knauth.org)
Date: Thu Aug 7 09:39:49 EDT 2014

On Aug 7, 2014, at 1:41 AM, Kevin Forchione <lysseus at gmail.com> wrote:

> 
> On Aug 6, 2014, at 11:02 AM, Alexander D. Knauth <alexander at knauth.org> wrote:
> 
>> 
>> On Aug 6, 2014, at 1:47 PM, Alexander D. Knauth <alexander at knauth.org> wrote:
>> 
>>> 
>>> 
>>> On Aug 6, 2014, at 1:10 PM, Kevin Forchione <lysseus at gmail.com> wrote:
>>> 
>>>> 
>>>> On Aug 5, 2014, at 2:21 PM, Jens Axel Søgaard <jensaxel at soegaard.net> wrote:
>>>> 
>>>>> Is this a step in the right direction?
>>>>> 
>>>>> (define-syntax (x stx)
>>>>> (syntax-parse stx
>>>>>  [(_ (y ... (z ...) w ...))
>>>>>   #'(xf (yf y ... (zf z ...) w ...))]))
>>>>> 
>>>>> The pattern (z ...) ... will match a sequence of lists such as (4 5 6) (7 8)
>>>>> but it won't match (4 5 6) 7 8 from your example.
>>>>> 
>>>>> /Jens Axel
>>>> 
>>>> Closer. It doesn’t match something like ‘( 1 2 3 (4 5 6) 7 (8 9) 10), for instance. 
>>> 
>>> For that I think you want something like this:
>>> (syntax-parse stx
>>>   [(_ (~or (z ...)
>>>            y)
>>>       ...)
>>>    #'(xf (yf y ... (zf z ...)))])
>> 
>> Sorry I forgot an ellipsis.  I meant this:
>> (syntax-parse stx
>>   [(_ (~or (z ...)
>>            y)
>>       ...)
>>    #'(xf (yf y ... (zf z ...) ...))])
> 
> 
> Remarkably difficult just to parse what amounts to nested lists! This doesn’t quite do what I want either. For instance:
> 
> #lang racket
> 
> (require (for-syntax syntax/parse))
> 
> #;(define-syntax (x stx)
>   
>   (define-syntax-class binding
>     #:description "binding list"
>     (pattern (z:number ...)))
>   
>   (syntax-parse stx
>     [(_ b:binding ...)
>      #'(list (list b.z ...) ...)]))
> 
> #;(define-syntax (x stx)
>   
>   (define-syntax-class lbinding
>     #:description "binding list"
>     (pattern (z:number ...)))
>   
>   (define-syntax-class orbinding
>     #:description "binding or"
>     (pattern (y:number ...)
>              #:with (z ...) #'(y ...))
>     (pattern (lb:lbinding)
>              #:with (z ...) #'(lb.z ...)))
>   
>   (syntax-parse stx
>     [(_ ob:orbinding ...)
>      #'(list (list ob.z ...) ...)]))
> 
> #;(define-syntax (x stx)
>   
>   (define-syntax-class lbinding
>     #:description "binding list"
>     (pattern (z:number ...)))
>   
>   (define-syntax-class orbinding
>     #:description "binding or"
>     (pattern (y:number ...)
>              #:with (z ...) #'(y ...))
>     (pattern (lb:lbinding)
>              #:with (z ...) #'(lb.z ...)))
>   
>   (syntax-parse stx
>     [(_ ob:orbinding ...)
>      #'(list (list ob.z ...) ...)]))
> 
> (define-syntax (x stx)
>   (syntax-parse stx
>     [(_ (~or (z ...)
>              y)
>         ...)
>      #'(list (list y ... (list z ...) ...))]))
> 
> (x (1 2 3 4) (5 (6 7) 8))
> 
> expands to:  (list (list (list 1 2 3 4) (list 5 (6 7) 8))))) instead of  (list (list (list 1 2 3 4) (list 5 (list 6 7) 8))))). 
> 
> -Kevin
> 

Do you want something like this then:
(syntax-parse stx
  [(_ (~or ((((e ...) ...) ...) ...)
           (((d ...) ...) ...)
           ((c ...) ...)
           (b ...)
           a)
      . . .)
   #’(list (list a ... (list b ... (list c ... (list d ... (list e ...) ...) ...) ...) ...) ...)])
Except going infinitely?  For that I think you would need a recursive helper function.  

Or do you want to just replace all instances of (a ...) with (list a ...) ?:
(define-syntax-class thing
  #:attributes (norm)
  [pattern (a:thing ...)
           #:with norm (list a.norm ...)]
  [pattern a
           #:with norm a])

Or what?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140807/7035877f/attachment-0001.html>

Posted on the users mailing list.