takikawa / sweet-racket

A port of sweet expressions to Racket
http://pkg-build.racket-lang.org/doc/sweet/index.html
Other
47 stars 10 forks source link

read / unsweeten #49

Closed t0mpr1c3 closed 1 year ago

t0mpr1c3 commented 1 year ago

I would like to read a sweetened source file like so:

#lang sweet-exp typed/racket

(read (open-input-file "something-sweet.sscm"))

I'm fairly new to Racket, so I am not sure why that doesn't work or how to get it to work.

A workaround would be to use the unsweeten utility -- do you know of a Racket port of this?

takikawa commented 1 year ago

There isn't really a documented way to do this (because sweet-exp is intended to be used as a language, rather than a parsing utility or library), but it's possible by using some of the internal modules. For example:

#lang racket/base

(require (submod sweet-exp/main link-reader))

(define-values (read* read-syntax*)
  (sweet-link read read-syntax)) ;; parameterize this with the underlying `read` that you want to use

(read* (open-input-file "sweet.rkt"))

The reason why your example didn't work is because the read binding of the underlying language is not overridden by sweet-exp, because the language mixin essentially only changes the surface syntax.

AlexKnauth commented 1 year ago

Just like at-expressions have scribble/reader, we could add a module sweet-exp/reader that provided those read and read-syntax functions.

Though I would name them sweet-read and sweet-read-syntax to avoid naming collisions.

t0mpr1c3 commented 1 year ago

Asumu, that works perfectly, thanks.