I've recently been rewriting some ugly code to make it fully hygienic with syntax-parse. I have an implementation that appears to work when testing it from the module that contains the macros, but it fails when I run it from my main program/another module.
The macro in question is parse-level0, defined in expander-utils.rkt:
----
(begin-for-syntax
(define-syntax-class mag-lvl
(pattern ({~datum level})))
(define-syntax-class mag-line
(pattern ({~literal line} expr ...))))
(define-syntax (parse-level0 stx)
(printf "parse-level0 input: ")
(display stx) (printf "~n")
(syntax-parse stx
[(_ ln:mag-line) #'ln]
[(_ ln:mag-line (~seq lvl:mag-lvl ...+ ln2:mag-line) ...+)
#:with branch-lines #'((~@ lvl ... ln2) ...)
(printf "parse-level0 1~n")
#`(when* ln (level
#,@(parse-level1 #'branch-lines)))]
[(_ lvl:mag-lvl ...+ expr ...)
#'(error "no line at level 0")]
[_
(printf "parse-level0 error on input: ")
(display stx) (printf "~n")
#'(error "syntax error at level 0")]))
----
This macro is provided to expander.rkt. It works when run from the REPL in expander-utils.rkt, but not when run from expander.rkt. Here's an example with some debug info printed:
expander-utils.rkt> (parse-level0 (line 1))
parse-level0 input: #<syntax:stdin::10915 (parse-level0 (line 1))>
'(1)
expander-utils.rkt>
expander.rkt> (parse-level0 (line 1))
parse-level0 input: #<syntax:stdin::11004 (parse-level0 (line 1))>
parse-level0 error on input: #<syntax:stdin::11004 (parse-level0 (line 1))>
; syntax error at level 0
; Context:
; /usr/local/racket/collects/racket/repl.rkt:11:26
The syntax objects there are printed with 'display' and are identical as far as I can tell, but I wonder if it could be related to interpreting line or level as a datum or literal as defined in the syntax classes I created. In this example I'm not using level so I guess that it wouldn't matter, but line is defined as a macro in expander.rkt. But it still works in expander-utils.rkt if I define the line macro there.
Anyone have an idea what might be happening here?
Thanks,
Jonathan