[parenscript-devel] automate javascript -> parenscript conversion

Lui Fungsin fungsin.lui at gmail.com
Fri Feb 15 03:15:04 UTC 2008


Wow, it's really cool. You're right, the javascript parser is not
trivial at all.

BTW, I notice that the experimental/js-on-cl/test/ directory is empty
and for some reason after lispworks 5 (linux) finshed loading the asd
system, the example-usage function is still undefined.

SBCL 1.10 failed the first time I tried to load js-on-cl.asd but on
second trial the error is gone.

The output of (example-usage) is very nice, except for all the CAPS :-)

Thanks a lot. I'll follow up on the mailing lists if I notice other
issues (or if I have any patch for submit)

Regards,
fungsin

On 2/14/08, Red Daly <reddaly at stanford.edu> wrote:
> Lui,
>
> Appologies for a rushed email:
>
> I wrote something to do this awhile ago, and it is fairly functional.
>
> get the Suave source from http://common-lisp.net/project/suave/
>
> look at the experimental/js-on-cl folder.  load up the ASD and use
> JS-TO-PAREN.  the key file is src/js-to-parenscript.lisp, which comes
> with an example:
>
> (defvar *example-script* "function getElementPosition(elemID) {
>    var offsetTrail = document.getElementById(elemID);
>    var offsetLeft = 0;
>    var offsetTop = 0;
>    while (offsetTrail) {
>        offsetLeft += offsetTrail.offsetLeft;
>        offsetTop += offsetTrail.offsetTop;
>        offsetTrail = offsetTrail.offsetParent;
>    }
>    if (navigator.userAgent.indexOf(\"Mac\") != -1 &&
>        typeof document.body.leftMargin != \"undefined\") {
>        offsetLeft += document.body.leftMargin;
>        offsetTop += document.body.topMargin;
>    }
>    return {left:offsetLeft, top:offsetTop};
> }")
>
> (defun example-usage ()
>  (js-to-paren *example-script*))
>
>
> The hard work for this is the Javascript parser, which was stolen from JWACS.
>
> I'm not sure if this all still works as Parenscript has changed somewhat since then.  Your patches are welcome, just send them to the Suave list.
>
>
> Best,
> Red
>
>
> Lui Fungsin wrote:
> > Hi,
> >
> > I found that manually converting javascript (sample code) to
> > parenscript is quite annoying.
> >
> > Does anyone know if writing a parser/converter (in lisp, perhaps) to
> > automate this task is a major undertaking or is relatively easy?
> > (maybe using some of the existing lisp libraries)
> >
> > The reason to convert existing javascript to parenscript is that it's
> > easier to write/maintain afterwards (you have macro, emacs's paredit
> > mode, etc). Also, it's a lot easier to integrate in-line with cl-who
> > or other similar libraries whereas with existing javascript you need
> > to link them with the script tag (otherwise you have to escape all the
> > quotes, backslashes etc to include it as a really longggggg string).
> >
> > >From my experience of converting existing javascript code to
> > parenscript, the oo method call conversion is most troublesome.
> >
> > i.e.  x.getobj().slot1.slot2.method1()
> >
> > /* hard to read backward if you're used to js */
> > => (.method (aref (aref (.getobj x) "slot1") "slot2"))
> >
> > /* easier to read but required a temp variable */
> > or (let ((obj (.getobj x))) (.method1 obj.slot1.slot2))
> >
> > thanks
> > _______________________________________________
> > parenscript-devel mailing list
> > parenscript-devel at common-lisp.net
> > http://common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel
> >
>
> _______________________________________________
> parenscript-devel mailing list
> parenscript-devel at common-lisp.net
> http://common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel
>



More information about the parenscript-devel mailing list