[racket] heredoc syntax

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Aug 18 18:00:54 EDT 2011

Three minutes ago, Jon Rafkind wrote:
> 
> Ok. For future reference this is how its done:
> 
> #lang at-exp racket
> @string-append{
> foo
> bar
> }

Yeah, and you can use a more convenient name, indent the text, and/or
the whole thing, and get the same result.  For example:

  #lang at-exp racket
  (define str string-append)
  (define text
    @str{
      foo
      bar
    })

(Before you complain: the shorter name is not to make it shorter, it's
because the `-append' thing is just a by-product of the available
function that can append strings, so it shouldn't be there.)


> I would be happier if there was some syntactic support in scribble
> instead of having to write 'string-append'.

It reads the contents as strings, with newlines inserted:

  (str "foo" "\n" "bar")

and the newline syntaxes will have a 'scribble property that has
additional information, like the actual spacing.  This is to
accommodate other uses that might care about the specific
indentation.  I thought about making it all one big string, but that
doesn't make much sense, since it'll need to be a list in any case
whenever there are @s inside the text -- so might as well provide the
above information.


> >> I like python's heredoc syntax -- triple quotes. """a heredoc
> >> string""", if such a thing could be added to racket (I will most
> >> likely do something like this for honu).
> > Don't use it, use the Scribble syntax instead.  Really.
> 
> Probably this discussion should move to the dev list.. but anyway
> how would I re-use the scribble syntax? I have my own lexer so whats
> the easiest way to bridge to the scribble stuff?

Do you use a readtable in any form?  If you do, then it's easy.

Otherwise it's best to hook it up manually somehow, which might
require extending the scribble syntax for you.  The thing is that in
places where it reads sexprs again:

  @HERE[HERE]{blah @AND-HERE blah}

it does so by using the original readtable.  So if you're not using
one, you'll need a way to hook a reader where a readtable is expected.
And in that case, you might need to provide a reader that will not
read too much -- but for the sexpr syntax we avoided, which is why
@foo{blah, @x, blah} works, and @foo{blah, @x. blah} doesn't (it reads
an `x.' identifier as usual, instead of trying to be helpful and stop
at the dot -- helpful but not uniform).

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


Posted on the users mailing list.