[plt-scheme] ajax and plt

From: Eli Barzilay (eli at barzilay.org)
Date: Fri Jul 3 03:40:46 EDT 2009

On Jul  3, Dave Gurnell wrote:
> > I'm curious -- is there any advantage for this over something like:
> >
> > (let ([msg "Hello world"])
> >   @js{alert(@msg)})
> >
> > (Random example, since using `string-append' is even more boring in  
> > this style.)
> 
> I didn't know you could set things up to switch between readers like
> that with such simple syntax. It could be really useful. I have
> contemplated an @-style syntax but I have lacked the PLT know-how to
> set it up and experiment.

Well, that's very easy now.  All you need is:

  #lang at-exp scheme

and then you have the scribble reader syntax.  You can then use any
definition for `js' -- for example, you could define it as
`string-append', or as a version of `string-append' that turns
non-string values to strings, or construct some specific kind of
values, or just use `list' and make the component that renders the
final text spit out the pieces.  (The last one is what I usually
prefer, which you can see in the `scribble/text' language.  The
documentation language(s) uses the one before that.)


> One thought. An @-reader solution would benefit from being backed by
> an AST - when you unquote to Scheme code, you need to know whether
> the surrounding context is a statement/expression and so on to work
> out how to splice the fragments back together. For example:
> 
>      (javascript->string (js ,(+ 1 2)))          => "3;" ; semicolon  
> after the unquote
>      (javascript->string (js (+ ,(+ 1 2) 4))) => "3+4;" ; no semicolon  
> after the unquote
> 
> The context can be inferred by the programmer in these examples, but
> we assemble fragments of JS from all over the shop so protection
> against typos is quite important.

Well, both approaches are possible, of course.  IIUC, the latter
involves having several Scheme functions that render their inputs in a
different way -- in this example, an `expression' and a `statement'
kind of bindings.  The former is more difficult, but doable too -- it
requires some parsing of the text to know how to deal with the nested
escapes -- something like making the above `js' function check its
contents and add a semicolon if necessary.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!


Posted on the users mailing list.