From: Trastabuga
Subject: Translating JavaScript data structures to Lisp
Date: 
Message-ID: <10a048a4-a793-46bf-a3f6-3795e82d642e@p25g2000hsf.googlegroups.com>
I have a web page with some JavaScript code where they define a bunch
of arrays and hash tables.
I need to translate those data structures to similar ones in Common
Lisp.
First that came to mind is using Flex/Bison to do the job, but then
I'll have to write the grammar and basically re-implement JavaScript
compiler.
Also regular-expressions could be used to make a quick and dirty job,
but then it would depend on the current structure of the JS page.
I am wondering if there already exists the code/tool to do the job?

Andrew

From: Pascal J. Bourguignon
Subject: Re: Translating JavaScript data structures to Lisp
Date: 
Message-ID: <87tzg29uac.fsf@hubble.informatimago.com>
Trastabuga <·········@gmail.com> writes:

> I have a web page with some JavaScript code where they define a bunch
> of arrays and hash tables.
> I need to translate those data structures to similar ones in Common
> Lisp.
> First that came to mind is using Flex/Bison to do the job, but then
> I'll have to write the grammar and basically re-implement JavaScript
> compiler.
> Also regular-expressions could be used to make a quick and dirty job,
> but then it would depend on the current structure of the JS page.
> I am wondering if there already exists the code/tool to do the job?

If you just want to exchange the data dynamically you can use json and
cl-json.

If you want to parse javascript, there's a parser in jwacs.  You'll
get the syntactic tree as a tree of CLOS object, and you'll be able to
add a method to each node class to translate it to lisp.  I did that to
translate javascript to parenscript once.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"Our users will know fear and cower before our software! Ship it!
Ship it and let them flee like the dogs they are!"
From: Trastabuga
Subject: Re: Translating JavaScript data structures to Lisp
Date: 
Message-ID: <23d1ac49-1a1b-483e-9565-d52be751ef5b@m44g2000hsc.googlegroups.com>
On Jun 9, 5:23 pm, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> Trastabuga <·········@gmail.com> writes:
> > I have a web page with some JavaScript code where they define a bunch
> > of arrays and hash tables.
> > I need to translate those data structures to similar ones in Common
> > Lisp.
> > First that came to mind is using Flex/Bison to do the job, but then
> > I'll have to write the grammar and basically re-implement JavaScript
> > compiler.
> > Also regular-expressions could be used to make a quick and dirty job,
> > but then it would depend on the current structure of the JS page.
> > I am wondering if there already exists the code/tool to do the job?
>
> If you just want to exchange the data dynamically you can use json and
> cl-json.
>
> If you want to parse javascript, there's a parser in jwacs.  You'll
> get the syntactic tree as a tree of CLOS object, and you'll be able to
> add a method to each node class to translate it to lisp.  I did that to
> translate javascript to parenscript once.
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/
>
> "Our users will know fear and cower before our software! Ship it!
> Ship it and let them flee like the dogs they are!"

Thank you, Pascal.
The web page defines data in a form:
var Categories = [
		{name:"Cat1 ", caption:escape("Category 1")},
		{name:"Cat2 ", caption:escape("Category 2")}
	         ];

	var DB = new Object();
	DB[Categories[0].name] = [
           {name:escape("Item1"), item-num:8}
        ];

etc...
I tried using cl-json (decode-json-from-string) but it doesn't like
things like variable definitions.
Probably it won't be able to correctly compile things like
Categories[0].name.
Do you think I should use jwacs? I looked at its web page, but it's
not obvious how to do it. If you have an example, could you post it?

Andrew
From: Scott
Subject: Re: Translating JavaScript data structures to Lisp
Date: 
Message-ID: <6d101534-0739-4a4d-ba00-01a8f956191e@w5g2000prd.googlegroups.com>
On Jun 9, 3:57 pm, Trastabuga <·········@gmail.com> wrote:
> On Jun 9, 5:23 pm, ····@informatimago.com (Pascal J. Bourguignon)
> wrote:
>
>
>
> > Trastabuga <·········@gmail.com> writes:
> > > I have a web page with some JavaScript code where they define a bunch
> > > of arrays and hash tables.
> > > I need to translate those data structures to similar ones in Common
> > > Lisp.
> > > First that came to mind is using Flex/Bison to do the job, but then
> > > I'll have to write the grammar and basically re-implement JavaScript
> > > compiler.
> > > Also regular-expressions could be used to make a quick and dirty job,
> > > but then it would depend on the current structure of the JS page.
> > > I am wondering if there already exists the code/tool to do the job?
>
> > If you just want to exchange the data dynamically you can use json and
> > cl-json.
>
> > If you want to parse javascript, there's a parser in jwacs.  You'll
> > get the syntactic tree as a tree of CLOS object, and you'll be able to
> > add a method to each node class to translate it to lisp.  I did that to
> > translate javascript to parenscript once.
>
> > --
> > __Pascal Bourguignon__                    http://www.informatimago.com/
>
> > "Our users will know fear and cower before our software! Ship it!
> > Ship it and let them flee like the dogs they are!"
>
> Thank you, Pascal.
> The web page defines data in a form:
> var Categories = [
>                 {name:"Cat1 ", caption:escape("Category 1")},
>                 {name:"Cat2 ", caption:escape("Category 2")}
>                  ];
>
>         var DB = new Object();
>         DB[Categories[0].name] = [
>            {name:escape("Item1"), item-num:8}
>         ];
>
> etc...
> I tried using cl-json (decode-json-from-string) but it doesn't like
> things like variable definitions.
> Probably it won't be able to correctly compile things like
> Categories[0].name.
> Do you think I should use jwacs? I looked at its web page, but it's
> not obvious how to do it. If you have an example, could you post it?
>
> Andrew

Have you considered using JavaScript itself to spit out Lisp code/
data?  Why use a third party parser when you have the actual
JavaScript language at your disposal?
From: Trastabuga
Subject: Re: Translating JavaScript data structures to Lisp
Date: 
Message-ID: <62376ee8-0f81-4451-bd08-df062a2efd08@k30g2000hse.googlegroups.com>
On Jun 9, 8:28 pm, Scott <·······@gmail.com> wrote:
> On Jun 9, 3:57 pm, Trastabuga <·········@gmail.com> wrote:
>
>
>
> > On Jun 9, 5:23 pm, ····@informatimago.com (Pascal J. Bourguignon)
> > wrote:
>
> > > Trastabuga <·········@gmail.com> writes:
> > > > I have a web page with some JavaScript code where they define a bunch
> > > > of arrays and hash tables.
> > > > I need to translate those data structures to similar ones in Common
> > > > Lisp.
> > > > First that came to mind is using Flex/Bison to do the job, but then
> > > > I'll have to write the grammar and basically re-implement JavaScript
> > > > compiler.
> > > > Also regular-expressions could be used to make a quick and dirty job,
> > > > but then it would depend on the current structure of the JS page.
> > > > I am wondering if there already exists the code/tool to do the job?
>
> > > If you just want to exchange the data dynamically you can use json and
> > > cl-json.
>
> > > If you want to parse javascript, there's a parser in jwacs.  You'll
> > > get the syntactic tree as a tree of CLOS object, and you'll be able to
> > > add a method to each node class to translate it to lisp.  I did that to
> > > translate javascript to parenscript once.
>
> > > --
> > > __Pascal Bourguignon__                    http://www.informatimago.com/
>
> > > "Our users will know fear and cower before our software! Ship it!
> > > Ship it and let them flee like the dogs they are!"
>
> > Thank you, Pascal.
> > The web page defines data in a form:
> > var Categories = [
> >                 {name:"Cat1 ", caption:escape("Category 1")},
> >                 {name:"Cat2 ", caption:escape("Category 2")}
> >                  ];
>
> >         var DB = new Object();
> >         DB[Categories[0].name] = [
> >            {name:escape("Item1"), item-num:8}
> >         ];
>
> > etc...
> > I tried using cl-json (decode-json-from-string) but it doesn't like
> > things like variable definitions.
> > Probably it won't be able to correctly compile things like
> > Categories[0].name.
> > Do you think I should use jwacs? I looked at its web page, but it's
> > not obvious how to do it. If you have an example, could you post it?
>
> > Andrew
>
> Have you considered using JavaScript itself to spit out Lisp code/
> data?  Why use a third party parser when you have the actual
> JavaScript language at your disposal?

Maybe I didn't make myself clear... The page with JavaScript data that
I have to translate is taken from a remote site (they maintain it and
update, so it's not static).
The whole parsing and translation happens on my LAMP server where I
got Lisp server running.


Running JavaScript on the server?
From: x3j13
Subject: Re: Translating JavaScript data structures to Lisp
Date: 
Message-ID: <780ae5d3-a29b-4b96-ab67-42386edcb02a@l64g2000hse.googlegroups.com>
On Jun 9, 11:15 pm, Trastabuga <·········@gmail.com> wrote:
> On Jun 9, 8:28 pm, Scott <·······@gmail.com> wrote:
>
>
>
> > On Jun 9, 3:57 pm, Trastabuga <·········@gmail.com> wrote:
>
> > > On Jun 9, 5:23 pm, ····@informatimago.com (Pascal J. Bourguignon)
> > > wrote:
>
> > > > Trastabuga <·········@gmail.com> writes:
> > > > > I have a web page with some JavaScript code where they define a bunch
> > > > > of arrays and hash tables.
> > > > > I need to translate those data structures to similar ones in Common
> > > > > Lisp.
> > > > > First that came to mind is using Flex/Bison to do the job, but then
> > > > > I'll have to write the grammar and basically re-implement JavaScript
> > > > > compiler.
> > > > > Also regular-expressions could be used to make a quick and dirty job,
> > > > > but then it would depend on the current structure of the JS page.
> > > > > I am wondering if there already exists the code/tool to do the job?
>
> > > > If you just want to exchange the data dynamically you can use json and
> > > > cl-json.
>
> > > > If you want to parse javascript, there's a parser in jwacs.  You'll
> > > > get the syntactic tree as a tree of CLOS object, and you'll be able to
> > > > add a method to each node class to translate it to lisp.  I did that to
> > > > translate javascript to parenscript once.
>
> > > > --
> > > > __Pascal Bourguignon__                    http://www.informatimago.com/
>
> > > > "Our users will know fear and cower before our software! Ship it!
> > > > Ship it and let them flee like the dogs they are!"
>
> > > Thank you, Pascal.
> > > The web page defines data in a form:
> > > var Categories = [
> > >                 {name:"Cat1 ", caption:escape("Category 1")},
> > >                 {name:"Cat2 ", caption:escape("Category 2")}
> > >                  ];
>
> > >         var DB = new Object();
> > >         DB[Categories[0].name] = [
> > >            {name:escape("Item1"), item-num:8}
> > >         ];
>
> > > etc...
> > > I tried using cl-json (decode-json-from-string) but it doesn't like
> > > things like variable definitions.
> > > Probably it won't be able to correctly compile things like
> > > Categories[0].name.
> > > Do you think I should use jwacs? I looked at its web page, but it's
> > > not obvious how to do it. If you have an example, could you post it?
>
> > > Andrew
>
> > Have you considered using JavaScript itself to spit out Lisp code/
> > data?  Why use a third party parser when you have the actual
> > JavaScript language at your disposal?
>
> Maybe I didn't make myself clear... The page with JavaScript data that
> I have to translate is taken from a remote site (they maintain it and
> update, so it's not static).
> The whole parsing and translation happens on my LAMP server where I
> got Lisp server running.
>
> Running JavaScript on the server?

Rhino or SpiderMonkey might be things to look at.

Chris.
From: Trastabuga
Subject: Re: Translating JavaScript data structures to Lisp
Date: 
Message-ID: <8640c394-776f-472b-a18d-a1e04729c104@a1g2000hsb.googlegroups.com>
On Jun 10, 9:47 am, x3j13 <··········@gmail.com> wrote:
> On Jun 9, 11:15 pm, Trastabuga <·········@gmail.com> wrote:
>
>
>
> > On Jun 9, 8:28 pm, Scott <·······@gmail.com> wrote:
>
> > > On Jun 9, 3:57 pm, Trastabuga <·········@gmail.com> wrote:
>
> > > > On Jun 9, 5:23 pm, ····@informatimago.com (Pascal J. Bourguignon)
> > > > wrote:
>
> > > > > Trastabuga <·········@gmail.com> writes:
> > > > > > I have a web page with some JavaScript code where they define a bunch
> > > > > > of arrays and hash tables.
> > > > > > I need to translate those data structures to similar ones in Common
> > > > > > Lisp.
> > > > > > First that came to mind is using Flex/Bison to do the job, but then
> > > > > > I'll have to write the grammar and basically re-implement JavaScript
> > > > > > compiler.
> > > > > > Also regular-expressions could be used to make a quick and dirty job,
> > > > > > but then it would depend on the current structure of the JS page.
> > > > > > I am wondering if there already exists the code/tool to do the job?
>
> > > > > If you just want to exchange the data dynamically you can use json and
> > > > > cl-json.
>
> > > > > If you want to parse javascript, there's a parser in jwacs.  You'll
> > > > > get the syntactic tree as a tree of CLOS object, and you'll be able to
> > > > > add a method to each node class to translate it to lisp.  I did that to
> > > > > translate javascript to parenscript once.
>
> > > > > --
> > > > > __Pascal Bourguignon__                    http://www.informatimago.com/
>
> > > > > "Our users will know fear and cower before our software! Ship it!
> > > > > Ship it and let them flee like the dogs they are!"
>
> > > > Thank you, Pascal.
> > > > The web page defines data in a form:
> > > > var Categories = [
> > > >                 {name:"Cat1 ", caption:escape("Category 1")},
> > > >                 {name:"Cat2 ", caption:escape("Category 2")}
> > > >                  ];
>
> > > >         var DB = new Object();
> > > >         DB[Categories[0].name] = [
> > > >            {name:escape("Item1"), item-num:8}
> > > >         ];
>
> > > > etc...
> > > > I tried using cl-json (decode-json-from-string) but it doesn't like
> > > > things like variable definitions.
> > > > Probably it won't be able to correctly compile things like
> > > > Categories[0].name.
> > > > Do you think I should use jwacs? I looked at its web page, but it's
> > > > not obvious how to do it. If you have an example, could you post it?
>
> > > > Andrew
>
> > > Have you considered using JavaScript itself to spit out Lisp code/
> > > data?  Why use a third party parser when you have the actual
> > > JavaScript language at your disposal?
>
> > Maybe I didn't make myself clear... The page with JavaScript data that
> > I have to translate is taken from a remote site (they maintain it and
> > update, so it's not static).
> > The whole parsing and translation happens on my LAMP server where I
> > got Lisp server running.
>
> > Running JavaScript on the server?
>
> Rhino or SpiderMonkey might be things to look at.
>
> Chris.

I tried SpiderMonkey, looks like the right tool for the job.
Thank you!

Andrew
From: vanekl
Subject: Re: Translating JavaScript data structures to Lisp
Date: 
Message-ID: <g2m2fh$66q$1@aioe.org>
Trastabuga wrote:
> On Jun 9, 8:28 pm, Scott <·······@gmail.com> wrote:
>> On Jun 9, 3:57 pm, Trastabuga <·········@gmail.com> wrote:
>>
>>
>>
>>> On Jun 9, 5:23 pm, ····@informatimago.com (Pascal J. Bourguignon)
>>> wrote:
>>>> Trastabuga <·········@gmail.com> writes:
>>>>> I have a web page with some JavaScript code where they define a bunch
>>>>> of arrays and hash tables.
>>>>> I need to translate those data structures to similar ones in Common
>>>>> Lisp.
>>>>> First that came to mind is using Flex/Bison to do the job, but then
>>>>> I'll have to write the grammar and basically re-implement JavaScript
>>>>> compiler.
>>>>> Also regular-expressions could be used to make a quick and dirty job,
>>>>> but then it would depend on the current structure of the JS page.
>>>>> I am wondering if there already exists the code/tool to do the job?
>>>> If you just want to exchange the data dynamically you can use json and
>>>> cl-json.
>>>> If you want to parse javascript, there's a parser in jwacs.  You'll
>>>> get the syntactic tree as a tree of CLOS object, and you'll be able to
>>>> add a method to each node class to translate it to lisp.  I did that to
>>>> translate javascript to parenscript once.
>>>> --
>>>> __Pascal Bourguignon__                    http://www.informatimago.com/
>>>> "Our users will know fear and cower before our software! Ship it!
>>>> Ship it and let them flee like the dogs they are!"
>>> Thank you, Pascal.
>>> The web page defines data in a form:
>>> var Categories = [
>>>                 {name:"Cat1 ", caption:escape("Category 1")},
>>>                 {name:"Cat2 ", caption:escape("Category 2")}
>>>                  ];
>>>         var DB = new Object();
>>>         DB[Categories[0].name] = [
>>>            {name:escape("Item1"), item-num:8}
>>>         ];
>>> etc...
>>> I tried using cl-json (decode-json-from-string) but it doesn't like
>>> things like variable definitions.
>>> Probably it won't be able to correctly compile things like
>>> Categories[0].name.
>>> Do you think I should use jwacs? I looked at its web page, but it's
>>> not obvious how to do it. If you have an example, could you post it?
>>> Andrew
>> Have you considered using JavaScript itself to spit out Lisp code/
>> data?  Why use a third party parser when you have the actual
>> JavaScript language at your disposal?
> 
> Maybe I didn't make myself clear... The page with JavaScript data that
> I have to translate is taken from a remote site (they maintain it and
> update, so it's not static).
> The whole parsing and translation happens on my LAMP server where I
> got Lisp server running.
> 
> 
> Running JavaScript on the server?

if the javascript scripts/data all "fit" within the
browser's sandbox, and you only have to do this once, I
would think client-side would be easier. There are a number
of javascript consoles available; Firebug is one example.
But you don't even need a console---it can all be scripted
and the javascript simply reloaded. You don't even need
all the javascript to be from the same server. There is a
small cross-site security breach that you can use to mix
javascript from your site and another site, if necessary.
Examples of how that works:

http://continuations.wenger.us/post/35834226/javascript-security-wonderland
http://www.learningjquery.com/2008/06/updated-jquery-bookmarklet

or you could just download the remote javascript to your own box
prior to manipulating it.
From: Pascal J. Bourguignon
Subject: Re: Translating JavaScript data structures to Lisp
Date: 
Message-ID: <87lk1d9mnt.fsf@hubble.informatimago.com>
Trastabuga <·········@gmail.com> writes:

> Thank you, Pascal.
> The web page defines data in a form:
> var Categories = [
> 		{name:"Cat1 ", caption:escape("Category 1")},
> 		{name:"Cat2 ", caption:escape("Category 2")}
> 	         ];
>
> 	var DB = new Object();
> 	DB[Categories[0].name] = [
>            {name:escape("Item1"), item-num:8}
>         ];
>
> etc...
> I tried using cl-json (decode-json-from-string) but it doesn't like
> things like variable definitions.
> Probably it won't be able to correctly compile things like
> Categories[0].name.
> Do you think I should use jwacs? I looked at its web page, but it's
> not obvious how to do it. If you have an example, could you post it?

From the example you show here, there is only literal data set in the script.

In this case, the simpliest would be to parse this script, translate
it to s-exp, and interpret it to build the corresponding lisp data
structures.


On the other hand, if some data was elaborated by the script, you
would have to patch it to take the data and send it to some place you
can process it.  For example, using dojo json RPC, you could send the
data to a web service written in lisp where you'd get the data
directly with cl-json.

http://dojotoolkit.org/book/dojo-book-0-4/part-5-connecting-pieces/i-o/remote-procedure-calls-rpc


Even if the script is not yours, you may be able to hook your own
version, with the right set of referer,  base-url, frames, cookies,
etc.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"By filing this bug report you have challenged the honor of my
family. Prepare to die!"