[racket] syntax-parse question

From: Alexander D. Knauth (alexander at knauth.org)
Date: Thu Aug 7 15:43:10 EDT 2014

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

> On Aug 7, 2014, at 9:50 AM, Kevin Forchione <lysseus at gmail.com> wrote:
> 
>> 
>> On Aug 7, 2014, at 8:55 AM, Alexander D. Knauth <alexander at knauth.org> wrote:
>> 
>>>> 
>>>> 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 ...) ...) ...) ...) ...) ...)])
>>> 
>>> Sorry I meant this:
>>> (syntax-parse #'(x (1 2 3 4) (5 (6 7) 8))
>>>   [(_ (~or ((~or ((~or ((~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?
>> 
>> Sorry, I should probably clarify the problem I’m attempting to solve. I’ve got an application that creates a composite image using classes of objects that draw themselves. Essentially the macro Compose-A 
>> 
>>     (compose-A (<img  | (img …)> …) …)
>> 
>> would produce something like:
>> 
>>     {compose-A (compose-B <img | (compose-C img …)> …) …)
>> 
>> Compose-A can have an arbitrary number of compose-B clauses. 
>> compose-B clauses can have an arbitrarily number of elements in any order consisting of ing or clause-C. 
>> The clause-C consist of an arbitrary number of img. 
>> 
>> I’ve been wondering about having to go with a recursive macro. Is there any code in the current system that can be modeled from?
>> 
>> -Kevin
> 
> Actually, now that I think about it, the pattern can be generalized:
> 
> (compose-macro [func …) <atom ! (<atom  | (atom …)> …)>  …)
> 
> producing something like:
> 
> {func0 <atom | (func1 <atom | (func2 atom …)> …)> …), etc.
> 
> Essentially each clause consisting of an image or a sub-list, with succeeding levels of sub-list applying a new compose function to its arguments. I have to apologize, I don’t think I’ve captured the idea correctly with my notation. 
> 
> —Kevin

Ok then would this work for what you want?
(define-syntax-class (thing fs)
  #:attributes (norm)
  [pattern x #:when (empty? fs)
           #:with norm #'x]
  [pattern (x ...)
           #:declare x (thing (rest fs))
           #:with f (first fs)
           #:with norm #'(f x.norm ...)]
  [pattern x #:with norm #'x])
(syntax-parse #'(x [f1 f2 f3] (1 2 3 4) (5 (6 7) 8))
  [(_ [f ...] . x)
   #:declare x (thing (syntax->list #'(f ...)))
   #'x.norm])
; #<syntax (f1 (f2 1 2 3 4) (f2 5 (f3 6 7) 8))>



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

Posted on the users mailing list.