Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expansion of templates with leading ellipsis - do not allow invalid templates #1013

Open
jpellegrini opened this issue Dec 27, 2024 · 5 comments

Comments

@jpellegrini
Copy link
Contributor

jpellegrini commented Dec 27, 2024

Hello!

From what I understand of R7RS, the templates beginning with ellipses, like (... a b c etc) are expressed as a list, but not expanded as one (that is, they should be spliced into the code generated from the template).

A template of the form (<ellipsis> <template>) is identical to <template>, except that ellipses within the template have no special meaning

That the text says "(<ellipsis> <template>) is identical to <template>" seems to indicate that the list should be spliced (no?)
Also,

In particular, the template (<ellipsis> <ellipsis>) produces a single <ellipsis>

seems to be concordant with that.

For example:

(define-syntax f
    (syntax-rules ()
      ((f) '(a b (... c ... d) e))))

would splice the (c ... d) list into the expanded form:

(f) => (a b c ... d e)

But Chibi seems to not splice the list:

(define-syntax f
   (syntax-rules ()
     ((f) '(a b (... c ... d) e))))

(f) => (a b (c ... d) e)

unless it's a single ellipsis (?)

(define-syntax f
    (syntax-rules ()
      ((f) '(a (... ...) b))))

(f) => (a ... b)

Maybe I didn't get some detail (sorry for the noise if this is the case).

@ashinn
Copy link
Owner

ashinn commented Dec 27, 2024

The point is the template pattern is (<ellipsis> <template>), i.e. a single element after the ellipsis. So:

(... c ... d)

is simply invalid. I should probably update it to throw an exception in that case.

@jpellegrini
Copy link
Contributor Author

Ah, I misread the standard. Only the improper tail is a "subtemplate". Sorry!

@jpellegrini jpellegrini changed the title Expansion of templates with leading ellipsis Expansion of templates with leading ellipsis - do not allow invalid templates Dec 27, 2024
@jpellegrini
Copy link
Contributor Author

jpellegrini commented Jan 29, 2025

Wait... If a single element should be after the ellipsis, then it only makes sense to use (... ...), since (... something-else) would be meaningless -- I could have written something-else instead.

Let me see if I understand this right: in page 24 of R7RS,

  • A rule must be (<pattern> <template>)
  • A <template> can be (<element> <element> ... . <template>) second case for templates described in R7RS, page 24)
  • "where an <element> is a <template> optionally followed by an <ellipsis>.

Then,

  • A template of the form (<ellipsis> <template>) is identical to <template>, except that ellipses within the template have no special meaning. That is, any ellipses contained within <template> are treated as ordinary identifiers. (page 25)

So there could be several templates, in sequence, and any of them could be of the form (<ellipsis> <template>), if I understand correctly. And there could be several elements after the ellipsis.

And since (<ellipsis> <template) is equivalent to <template>, the effect would be to both turn the ellipsis to an ordinary symbol inside the template, and splice the list.

Is my interpretation correct?

@ashinn
Copy link
Owner

ashinn commented Jan 29, 2025

It's an escaping mechanism. ... inside <template> are not handled specially.

So the most trivial case is indeed (... ...) to insert a literal ... which you otherwise can't do. However, you can also use this to insert larger templates with nested ....

@jpellegrini
Copy link
Contributor Author

Sorry, I misunderstood what you had said. Right, I re-read R7RS, and see where my misinterpretation was.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants