[parenscript-devel] automate javascript -> parenscript conversion

Red Daly reddaly at stanford.edu
Fri Feb 15 01:58:50 UTC 2008


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
>   




More information about the parenscript-devel mailing list