Why are these @defthing blocks typeset incorrectly?

21 views
Skip to first unread message

Sage Gerard

unread,
Sep 23, 2020, 7:17:31 PM9/23/20
to users\@racket-lang.org

I create an application to `(defthing)` as a datum due to circumstances in the surrounding code. The hacky part being `get-contract-datum`, which tries to produce a datum usable as a contract expression. I'm assuming that this leaves no whitespace to preserve, so how do I modify a syntax object to include space in desired spots?

~slg


Screenshot from 2020-09-23 18-48-34.png

Sorawee Porncharoenwase

unread,
Sep 23, 2020, 7:37:25 PM9/23/20
to Sage Gerard, users\@racket-lang.org

Caution: I haven’t tried your code yet.

I think Scribble uses source location equipped with syntax objects to figure out spacing. Since you generate stuff on the fly, there’s no source location, so the rendered text is screwed up.

An idea I have (which might not work) is to use write / pretty-write on the generated code to a string port, and then read-syntax it back. That way, you get the source locations correctly.


--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/ulye9ZNaZmcspsE7fqy6ctobuGcF21jQvGQ4-PG2Eynt09KBU9AbF_kP9UZ_OgouhcvBOF8JLbaCJpVbnve-6RF_E752tVqp-xR9Va3oRsE%3D%40sagegerard.com.

Philip McGrath

unread,
Sep 23, 2020, 8:07:12 PM9/23/20
to Sorawee Porncharoenwase, Sage Gerard, users\@racket-lang.org
I have encountered this problem. I don't have a real solution, but maybe my hacky solution will be good enough.

On Wed, Sep 23, 2020 at 7:37 PM Sorawee Porncharoenwase <sorawe...@gmail.com> wrote:

I think Scribble uses source location equipped with syntax objects to figure out spacing. Since you generate stuff on the fly, there’s no source location, so the rendered text is screwed up.

This is indeed the explanation. My hacky solution (you can see it here) is to build up term for Scribble to typeset—the equivalent of what you do here, I think—using `syntax` rather than `quasiquote`. IIRC (I haven't touched this code in a while), it didn't work to use `quasisyntax`/`unsyntax`, so I used `define/syntax-parse` to bind pattern variables to the generated sub-terms. This way, Scribble picks up the source locations from the template and uses spaces as it should.

This is a hack, though: in my case, I'm building a `let*` expression with potentially several binding pairs, and they will run off the margin of the page. There are probably various other problems: I remember this code being a bit annoying to write.

-Philip
 

Sage Gerard

unread,
Sep 24, 2020, 9:55:22 AM9/24/20
to users\@racket-lang.org
I opted for the read-syntax based solution. For those reading, it's convenient to use `replace-context` from `syntax/strip-context` to donate lexical info.

The form of the transformer I used is:

@(define-syntax (reformat stx)
  (replace-context stx
    (read-syntax #f (open-input-string (with-output-to-string
      (lambda () (pretty-write `(begin . [...]))))))))

~slg


‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Wednesday, September 23, 2020 8:07 PM, Philip McGrath <phi...@philipmcgrath.com> wrote:

I have encountered this problem. I don't have a real solution, but maybe my hacky solution will be good enough.

On Wed, Sep 23, 2020 at 7:37 PM Sorawee Porncharoenwase <sorawe...@gmail.com> wrote:

I think Scribble uses source location equipped with syntax objects to figure out spacing. Since you generate stuff on the fly, there’s no source location, so the rendered text is screwed up.

This is indeed the explanation. My hacky solution (you can see it here) is to build up term for Scribble to typeset—the equivalent of what you do here, I think—using `syntax` rather than `quasiquote`. IIRC (I haven't touched this code in a while), it didn't work to use `quasisyntax`/`unsyntax`, so I used `define/syntax-parse` to bind pattern variables to the generated sub-terms. This way, Scribble picks up the source locations from the template and uses spaces as it should.
Reply all
Reply to author
Forward
0 new messages