[racket] syntax-parse and matching ordered optional keyworded expressions

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Sun May 5 16:11:52 EDT 2013

One way is to have your ~or form in an ~and with a catch-all where you can observe the ordering.
That is,
(~and (~seq forms ...) (~seq (~or (~optional (~seq #:kw0 kw0)) ...others...) ...))
Then walk through (forms ...) when you're outputting your changed keyword arguments to determine which to output. Say, build a table of keyword to output for that keyword.
You could also trust that syntax-parse will parse your dotted ~or in order and have ~do forms that populate a data structure via mutation and bump up a counter each time.

-Ian
----- Original Message -----
From: "Eric Dobson" <eric.n.dobson at gmail.com>
To: users at racket-lang.org
Sent: Sunday, May 5, 2013 3:58:31 PM GMT -05:00 US/Canada Eastern
Subject: [racket] syntax-parse and matching ordered optional keyworded expressions

I'm trying to improve the struct: form in TR, and one of the things I
need to do is match a sequence of keyworded expressions, add
annotations on the expressions, and then put them back into the same
order.

I can either do a large ~or clause and have optional constraints on
each clause, but If I do this then each kind of clause needs a
different name and then putting them back in the same order is
difficult. If I make a syntax class with each kind of clause then I
don't know how to enforce the at most one constraint.

Is there a way to do this with out duplicating the parsing work?

Method 1:
(~or
 (~optional guard:guard-form)
 (~optional mutable:mutable-form)
 property:struct-property-form) ...
and
#'(guard.contracted mutable.contracted property.contracted ...))

Method 2:
(define-splicing-syntax-class any-form
 (pattern :guard-form)
 (pattern :mutable-form)
 (pattern :struct-property-form))
(form:any-form ...)
and
#'(form.contracted ...)
____________________
  Racket Users list:
  http://lists.racket-lang.org/users

Posted on the users mailing list.