cisco / ChezScheme

Chez Scheme
Apache License 2.0
6.97k stars 986 forks source link

syntax definition with extra keyword not exported from library #41

Closed jhidding closed 8 years ago

jhidding commented 8 years ago

Example::

(library (test1)
  (export some-macro)
  (import (rnrs (6))
          (rnrs syntax-case (6)))

  (define-syntax some-macro
    (lambda (x)
      (syntax-case x (extra-keyword)
        [(_ a extra-keyword b) (syntax (+ a b))])))
)

When I use this code with (import (test1)), it fails, while the same code works when used inside the same module.

> (import (rnrs (6)))
> (import (test1))
> (some-macro 3 extra-keyword 4)
Exception: invalid syntax (some-macro 3 extra-keyword 4)
jhidding commented 8 years ago

The issue is somewhat fixed when I add `(define extra-keyword #f) to the library and export it. I ran into this when trying to use srfi-26 with chez.

dybvig commented 8 years ago

This is a consequence of the interaction-environment semantics, and your solution is the recommended one.

akeep commented 8 years ago

@jhidding Another way to handle this (which is what I do in the nanopass framework) is to use define-syntax to create an identifier for auxiliary keyword and export it with your library. I do something like:

(define-syntax extra-keyword
  (lambda (x)
    (syntax-violation #f "misplaced use of auxiliary keyword" x)))

and then add extra-keyword to your export list.

I actually have a couple of macros I wrote to make this easier (and to make it clear what I'm doing):

(define-syntax define-auxiliary-keyword
  (syntax-rules ()
    [(_ name)
     (define-syntax name 
       (lambda (x)
         (syntax-violation #f "misplaced use of auxiliary keyword" x)))]))

(define-syntax define-auxiliary-keywords
  (syntax-rules ()
    [(_ name* ...)
     (begin (define-auxiliary-keyword name*) ...)]))
jhidding commented 8 years ago

Thanks!