syntax-parse ~literal vs #:literals

21 views
Skip to first unread message

Roger Keays

unread,
Mar 12, 2021, 12:30:01 PM3/12/21
to racket...@googlegroups.com
According to the docs for syntax-parse/#:literals [1], "syntax-parse requires all literals to have a binding", and "the syntax-patterns are interpreted as if each occurrence of pattern-id were replaced with the following pattern: (~literal literal-id #:phase phase-expr)"

So, this error makes sense, because foo has no binding:

> (require syntax/parse)
> (syntax-parse #'(a foo b) #:literals (foo) [(x foo y) 'ok])
; readline-input:31:38: syntax-parse: literal is unbound in phase 0 (phase 0 relative to the enclosing module) at: foo

but when I use the expanded version, there is no error:

> (syntax-parse #'(a foo b) [(x (~literal foo) y) 'ok])
'ok
> (syntax-parse #'(a foo b) [(x (~literal foo #:phase 0) y) 'ok])
'ok
> (syntax-parse #'(a foo b) [(x (~literal foo #:phase 1) y) 'ok])
'ok

What am I missing here?

[1] https://docs.racket-lang.org/syntax/Parsing_Syntax.html#(form._((lib._syntax%2Fparse..rkt)._syntax-parse))

Reply all
Reply to author
Forward
0 new messages