[racket-dev] can a syntax-class "inherit" attributes?

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Fri Aug 23 00:54:10 EDT 2013

Here's how you want to define AorB:

(define-syntax-class AorB #:auto-nested-attributes
  (pattern :A)
  (pattern :B))

There's two things going on here, both of which I believe are undocumented,
sadly.

One is #:auto-nested-attributes, an option that tells AorB that anything
bound as an attribute by nested sub-patterns in its clauses should
automatically become an attribute of AorB.

The other is that empty identifier does not add "." to its attribute
names.  When you bind a with the attribute X, you get A.X, and when you
bind b, you get b.X, but when you bind the empty identifier, you just get X.

So now you just have X bound in both clauses, and that's automatically made
an attribute of AorB, and it all works like you want.

Carl Eastlund


On Fri, Aug 23, 2013 at 12:22 AM, Stephen Chang <stchang at ccs.neu.edu> wrote:

> Is there a way for a syntax-class to automatically inherit attributes?
>
> For example, the third class below combines the first two. Is there a
> way to automatically get the attributes from the first two classes in
> the third one (ie, I want to drop the "#:attr X #'a.X" part in the
> third class)?
>
> #lang racket
> (require syntax/parse)
>
> (define-syntax-class A (pattern (a b) #:attr X #'b))
> (define-syntax-class B (pattern (a b c) #:attr X #'c))
> (define-syntax-class AorB
>   (pattern a:A #:attr X #'a.X)
>   (pattern b:B #:attr X #'b.X))
>
> (syntax-parse #'(list 1) [ab:AorB (attribute ab.X)])
> _________________________
>   Racket Developers list:
>   http://lists.racket-lang.org/dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/dev/archive/attachments/20130823/5cd1c137/attachment.html>

Posted on the dev mailing list.