caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Camlp4] Quotation expander with OCaml syntax
       [not found] <AANLkTikuoN4H0Hsx74JwW66J9jmtq+usDxtQPpYfSGbd@mail.gmail.com>
@ 2010-07-26 14:41 ` Raphael Proust
  2010-07-26 15:13   ` [Caml-list] " Nicolas Pouillard
                     ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Raphael Proust @ 2010-07-26 14:41 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 2195 bytes --]

Hi all,

I'm working on a syntax extension as part of an internship in the
Ocsigen team. The aim of the syntax extension is to split the code of a web
application in two separate files: one for the client and one for the
server. A
few transformations are to take place in the process.

Quotations are to be transformed into client code while antiquotations can
refer
to server side values transmitted to the client at runtime.


In order to avoid any XY problems, here is an abstracted and simplified
example
of the expected behavior:

(* Pre-parsed code: *)
let start = <:on< f $y$ >> in
let html_node =
 span ~a:[onclick start] "some text" (* a is used for (html) attributes *)

(* Server side post-parsed code: *)
let start _arg1 =
 "call_closure(some_unique_name," ^ mymarshall _arg1 ")"
in
let html_node = span ~a:[onclick (start y)] "some text"

(* Client side post-parsed code: *)
let _ = register_closure some_unique_name (fun _arg1 -> f _arg1)



If the example isn't clear enough I can detail it a little bit more.


I'm unsure of what is the standard way of doing such a thing in Camlp4. What
I
have in mind is to use the original Ocaml syntax for the quotation expander.
This would (IIUC) allow me to filter the AST to transform every
antiquotation
found inside the quotation itself.

I'm not sure this is the ideal way of doing such a thing because of the size
of
the pattern matching in the AST filter. On the other hand, because the
quotation
is supposed to contain valid OCaml code, it seems normal to reuse the
original
parser.

I considered an alternative solution: treating quotations as raw text (with
a
custom quotation expander) but that would destroy any _loc information and
make
compile time warnings and errors quite difficult to locate.


Is there a simpler/fitter way of doing that? (Is the Y correct wrt the X?)

How can one embed the original parser in a quotation expander? (I couldn't
find
a function of type string -> Ast.expr in the Camlp4 doc/mlis, but I'd be
happy
to be pointed to one if it exists. I think it would at least require some
functor application.)

Does anyone know of any example that resemble what I'm trying to achieve?



-- 
_______
Raphael

[-- Attachment #2: Type: text/html, Size: 7544 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-26 14:41 ` [Camlp4] Quotation expander with OCaml syntax Raphael Proust
@ 2010-07-26 15:13   ` Nicolas Pouillard
  2010-07-26 15:41     ` Joel Reymont
  2010-07-26 15:41     ` Raphael Proust
  2010-07-26 20:08   ` bluestorm
  2010-07-27 13:22   ` Thomas.Gazagnaire
  2 siblings, 2 replies; 13+ messages in thread
From: Nicolas Pouillard @ 2010-07-26 15:13 UTC (permalink / raw)
  To: Raphael Proust, caml-list

On Mon, 26 Jul 2010 16:41:46 +0200, Raphael Proust <raphlalou@gmail.com> wrote:
> Hi all,

Hi,

> I'm working on a syntax extension as part of an internship in the
> Ocsigen team. The aim of the syntax extension is to split the code of a web
> application in two separate files: one for the client and one for the
> server. A
> few transformations are to take place in the process.
> 
> Quotations are to be transformed into client code while antiquotations can
> refer
> to server side values transmitted to the client at runtime.
> 
> 
> In order to avoid any XY problems, here is an abstracted and simplified
> example
> of the expected behavior:
> 
> (* Pre-parsed code: *)
> let start = <:on< f $y$ >> in
> let html_node =
>  span ~a:[onclick start] "some text" (* a is used for (html) attributes *)
> 
> (* Server side post-parsed code: *)
> let start _arg1 =
>  "call_closure(some_unique_name," ^ mymarshall _arg1 ")"
> in
> let html_node = span ~a:[onclick (start y)] "some text"
> 
> (* Client side post-parsed code: *)
> let _ = register_closure some_unique_name (fun _arg1 -> f _arg1)
> 
> 
> 
> If the example isn't clear enough I can detail it a little bit more.
> 
> 
> I'm unsure of what is the standard way of doing such a thing in Camlp4. What
> I
> have in mind is to use the original Ocaml syntax for the quotation expander.
> This would (IIUC) allow me to filter the AST to transform every
> antiquotation
> found inside the quotation itself.
> 
> I'm not sure this is the ideal way of doing such a thing because of the size
> of
> the pattern matching in the AST filter. On the other hand, because the
> quotation
> is supposed to contain valid OCaml code, it seems normal to reuse the
> original
> parser.

If the <:on<...>> contents is valid OCaml syntax I would suggest you to not go
in the quotations direction. You will avoid a ton of syntactic issues.

What I suggest you is to directly use camlp4 filters, for instance by changing
the meaning of some constructor application and labels for antiquotations:

(* Pre-parsed code: *)
let start = On (f ~y) in
let html_node =
  span ~a:[onclick start] "some text" (* a is used for (html) attributes *)

Have fun,

-- 
Nicolas Pouillard
http://nicolaspouillard.fr


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-26 15:13   ` [Caml-list] " Nicolas Pouillard
@ 2010-07-26 15:41     ` Joel Reymont
  2010-07-26 16:05       ` Jake Donham
  2010-07-26 15:41     ` Raphael Proust
  1 sibling, 1 reply; 13+ messages in thread
From: Joel Reymont @ 2010-07-26 15:41 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: Raphael Proust, caml-list

What if the quotation is not valid OCaml syntax, e.g. C#, and a Camlp4 parser for it exists?

How would I tie it all together to parse the quotation, plug in  antiquotations and end up with the custom AST that my parser outputs?

    Thanks, Joel

Sent from my iPad

On Jul 26, 2010, at 16:13, Nicolas Pouillard <nicolas.pouillard@gmail.com> wrote:

> On Mon, 26 Jul 2010 16:41:46 +0200, Raphael Proust <raphlalou@gmail.com> wrote:
>> Hi all,
> 
> Hi,
> 
>> I'm working on a syntax extension as part of an internship in the
>> Ocsigen team. The aim of the syntax extension is to split the code of a web
>> application in two separate files: one for the client and one for the
>> server. A
>> few transformations are to take place in the process.
>> 
>> Quotations are to be transformed into client code while antiquotations can
>> refer
>> to server side values transmitted to the client at runtime.
>> 
>> 
>> In order to avoid any XY problems, here is an abstracted and simplified
>> example
>> of the expected behavior:
>> 
>> (* Pre-parsed code: *)
>> let start = <:on< f $y$ >> in
>> let html_node =
>> span ~a:[onclick start] "some text" (* a is used for (html) attributes *)
>> 
>> (* Server side post-parsed code: *)
>> let start _arg1 =
>> "call_closure(some_unique_name," ^ mymarshall _arg1 ")"
>> in
>> let html_node = span ~a:[onclick (start y)] "some text"
>> 
>> (* Client side post-parsed code: *)
>> let _ = register_closure some_unique_name (fun _arg1 -> f _arg1)
>> 
>> 
>> 
>> If the example isn't clear enough I can detail it a little bit more.
>> 
>> 
>> I'm unsure of what is the standard way of doing such a thing in Camlp4. What
>> I
>> have in mind is to use the original Ocaml syntax for the quotation expander.
>> This would (IIUC) allow me to filter the AST to transform every
>> antiquotation
>> found inside the quotation itself.
>> 
>> I'm not sure this is the ideal way of doing such a thing because of the size
>> of
>> the pattern matching in the AST filter. On the other hand, because the
>> quotation
>> is supposed to contain valid OCaml code, it seems normal to reuse the
>> original
>> parser.
> 
> If the <:on<...>> contents is valid OCaml syntax I would suggest you to not go
> in the quotations direction. You will avoid a ton of syntactic issues.
> 
> What I suggest you is to directly use camlp4 filters, for instance by changing
> the meaning of some constructor application and labels for antiquotations:
> 
> (* Pre-parsed code: *)
> let start = On (f ~y) in
> let html_node =
>  span ~a:[onclick start] "some text" (* a is used for (html) attributes *)
> 
> Have fun,
> 
> -- 
> Nicolas Pouillard
> http://nicolaspouillard.fr
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-26 15:13   ` [Caml-list] " Nicolas Pouillard
  2010-07-26 15:41     ` Joel Reymont
@ 2010-07-26 15:41     ` Raphael Proust
  2010-07-26 16:27       ` Nicolas Pouillard
  2010-07-26 16:30       ` Jake Donham
  1 sibling, 2 replies; 13+ messages in thread
From: Raphael Proust @ 2010-07-26 15:41 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: caml-list

On Mon, Jul 26, 2010 at 5:13 PM, Nicolas Pouillard
<nicolas.pouillard@gmail.com> wrote:
> On Mon, 26 Jul 2010 16:41:46 +0200, Raphael Proust <raphlalou@gmail.com> wrote:
> > Hi all,
> Hi,
>
> > [...]
> >
> > (* Pre-parsed code: *)
> > let start = <:on< f $y$ >> in
> > let html_node =
> >  span ~a:[onclick start] "some text" (* a is used for (html) attributes *)
> >
> > (* Server side post-parsed code: *)
> > let start _arg1 =
> >  "call_closure(some_unique_name," ^ mymarshall _arg1 ")"
> > in
> > let html_node = span ~a:[onclick (start y)] "some text"
> >
> > (* Client side post-parsed code: *)
> > let _ = register_closure some_unique_name (fun _arg1 -> f _arg1)
> >
> >
> > I'm unsure of what is the standard way of doing such a thing in Camlp4. What
> > I
> > have in mind is to use the original Ocaml syntax for the quotation expander.
> > This would (IIUC) allow me to filter the AST to transform every
> > antiquotation
> > found inside the quotation itself.
> >
> > [...]
>
> If the <:on<...>> contents is valid OCaml syntax I would suggest you to not go
> in the quotations direction. You will avoid a ton of syntactic issues.
>
> What I suggest you is to directly use camlp4 filters, for instance by changing
> the meaning of some constructor application and labels for antiquotations:
>
> (* Pre-parsed code: *)
> let start = On (f ~y) in
> let html_node =
>  span ~a:[onclick start] "some text" (* a is used for (html) attributes *)

I'd rather not use a constructor because it imposes a strong limitation on the
user (it basically reserves a keyword) whereas the <:on< ... >> solution does
not (<:smthg< ... >> is already "reserved").

And labeling antiquotations is far from ideal since it prevents complex
expressions from being inlined. You basically need to write:
  let y = h a in
  let z = g x z in
  On (f ~y ~z)
instead of:
  <:on< f $h a$ $g x z$ >>

What I basically need is to get an AST with antiquotations and quotations being
special nodes. How is this achievable w/o reimplementing a whole grammar?

The alternative solution is to use raw strings, to find antiquotation marks, to
split the string and to reinject it in the different files. Is there a way to
keep precise _loc information this way?




--
_______
Raphael


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-26 15:41     ` Joel Reymont
@ 2010-07-26 16:05       ` Jake Donham
  0 siblings, 0 replies; 13+ messages in thread
From: Jake Donham @ 2010-07-26 16:05 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Nicolas Pouillard, caml-list

On Mon, Jul 26, 2010 at 8:41 AM, Joel Reymont <joelr1@gmail.com> wrote:
> What if the quotation is not valid OCaml syntax, e.g. C#, and a Camlp4 parser for it exists?
>
> How would I tie it all together to parse the quotation, plug in  antiquotations and end up with the custom AST that my parser outputs?

There is an example of this in the jslib library in ocamljs:

  http://github.com/jaked/ocamljs/tree/master/src/jslib/

and a somewhat simpler one for JSON here:

  http://github.com/jaked/cufp-metaprogramming-tutorial/tree/master/ocaml/json_quot/

I hope to post a full explanation of this shortly. The magic is the
Camlp4MetaGenerator filter, which generates functions to "lift" your
custom AST into the OCaml AST, either as an expr or a patt. So e.g.
your custom constructor Foo becomes ExId (_, IdUid (_, "Foo"))
(respectively PaId). When you write <:lang< foo >> (where "foo" parses
to Foo) in an expression, you get Foo in the expanded OCaml AST.

Jake


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-26 15:41     ` Raphael Proust
@ 2010-07-26 16:27       ` Nicolas Pouillard
  2010-07-26 16:30       ` Jake Donham
  1 sibling, 0 replies; 13+ messages in thread
From: Nicolas Pouillard @ 2010-07-26 16:27 UTC (permalink / raw)
  To: Raphael Proust; +Cc: caml-list

On Mon, 26 Jul 2010 17:41:58 +0200, Raphael Proust <raphlalou@gmail.com> wrote:
> On Mon, Jul 26, 2010 at 5:13 PM, Nicolas Pouillard
> <nicolas.pouillard@gmail.com> wrote:
> > On Mon, 26 Jul 2010 16:41:46 +0200, Raphael Proust <raphlalou@gmail.com> wrote:
> > > Hi all,
> > Hi,
> >
> > > [...]
> > >
> > > (* Pre-parsed code: *)
> > > let start = <:on< f $y$ >> in
> > > let html_node =
> > >  span ~a:[onclick start] "some text" (* a is used for (html) attributes *)
> > >
> > > (* Server side post-parsed code: *)
> > > let start _arg1 =
> > >  "call_closure(some_unique_name," ^ mymarshall _arg1 ")"
> > > in
> > > let html_node = span ~a:[onclick (start y)] "some text"
> > >
> > > (* Client side post-parsed code: *)
> > > let _ = register_closure some_unique_name (fun _arg1 -> f _arg1)
> > >
> > >
> > > I'm unsure of what is the standard way of doing such a thing in Camlp4. What
> > > I
> > > have in mind is to use the original Ocaml syntax for the quotation expander.
> > > This would (IIUC) allow me to filter the AST to transform every
> > > antiquotation
> > > found inside the quotation itself.
> > >
> > > [...]
> >
> > If the <:on<...>> contents is valid OCaml syntax I would suggest you to not go
> > in the quotations direction. You will avoid a ton of syntactic issues.
> >
> > What I suggest you is to directly use camlp4 filters, for instance by changing
> > the meaning of some constructor application and labels for antiquotations:
> >
> > (* Pre-parsed code: *)
> > let start = On (f ~y) in
> > let html_node =
> >  span ~a:[onclick start] "some text" (* a is used for (html) attributes *)
> 
> I'd rather not use a constructor because it imposes a strong limitation on the
> user (it basically reserves a keyword) whereas the <:on< ... >> solution does
> not (<:smthg< ... >> is already "reserved").

You still qualify it if you want to avoid clashes: MyModule.On

> And labeling antiquotations is far from ideal since it prevents complex
> expressions from being inlined. You basically need to write:
>   let y = h a in
>   let z = g x z in
>   On (f ~y ~z)
> instead of:
>   <:on< f $h a$ $g x z$ >>

Nothing prevents you to reserve another syntax for going back to plain OCaml code.

> What I basically need is to get an AST with antiquotations and quotations being
> special nodes. How is this achievable w/o reimplementing a whole grammar?

Doing this amounts to making the almost the same thing than
Camlp4OCamlOriginalQuotationExpander but with some filtering code.

> The alternative solution is to use raw strings, to find antiquotation marks, to
> split the string and to reinject it in the different files. Is there a way to
> keep precise _loc information this way?

Yes _loc would be hard to preserve this way.

-- 
Nicolas Pouillard
http://nicolaspouillard.fr


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-26 15:41     ` Raphael Proust
  2010-07-26 16:27       ` Nicolas Pouillard
@ 2010-07-26 16:30       ` Jake Donham
  2010-07-27  7:57         ` Raphael Proust
  1 sibling, 1 reply; 13+ messages in thread
From: Jake Donham @ 2010-07-26 16:30 UTC (permalink / raw)
  To: Raphael Proust; +Cc: Nicolas Pouillard, caml-list

On Mon, Jul 26, 2010 at 8:41 AM, Raphael Proust <raphlalou@gmail.com> wrote:
> What I basically need is to get an AST with antiquotations and quotations being
> special nodes. How is this achievable w/o reimplementing a whole grammar?

There is already a special node for antiquotations, but not
quotations; see below.

> The alternative solution is to use raw strings, to find antiquotation marks, to
> split the string and to reinject it in the different files. Is there a way to
> keep precise _loc information this way?

This is more or less what already happens with quotations /
antiquotations. The lexer reads the whole quotation into a string
(checking that nested antiquotations / quotations are balanced, so it
doesn't stop too soon, but not otherwise processing them). The string
is then expanded (for OCaml AST quotations at least) by parsing it
into an OCaml AST, then lifting the AST (see my previous response),
then filtering the resulting AST to parsed antiquotations and insert
conversions (e.g. $`int:i$ becomes ExInt (_, string_of_int i)). The
nested parsing begins with the current location, so _loc information
is kept precisely. Antiquotations are represented as special nodes in
the OCaml AST containing the string (again, the whole antiquotation,
even if there is further nesting), which are parsed during the AST
filtering phase.

I'm not sure what syntactic problems Nicolas suggests you would run
into by reusing the existing parser and quotation mechanism. But it
might be a bit hairy to implement. One issue is that you can't just
drop antiquotations anywhere; where they can appear (and with what
tag) is given by the parser. If you want to go in this direction, the
place to start would be Camlp4QuotationCommon.ml, which implements
OCaml AST quotations / antiquotations. You can see there that you can
parse starting at arbitrary non-terminals, and you can filter the
OCaml AST without a giant pattern match using the Ast.map object.

Jake


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-26 14:41 ` [Camlp4] Quotation expander with OCaml syntax Raphael Proust
  2010-07-26 15:13   ` [Caml-list] " Nicolas Pouillard
@ 2010-07-26 20:08   ` bluestorm
  2010-07-26 20:53     ` Raphael Proust
  2010-07-27 13:22   ` Thomas.Gazagnaire
  2 siblings, 1 reply; 13+ messages in thread
From: bluestorm @ 2010-07-26 20:08 UTC (permalink / raw)
  To: Raphael Proust; +Cc: caml-list

On Mon, Jul 26, 2010 at 4:41 PM, Raphael Proust <raphlalou@gmail.com> wrote:
> I'm working on a syntax extension as part of an internship in the
> Ocsigen team. The aim of the syntax extension is to split the code of a web
> application in two separate files: one for the client and one for the
> server. A few transformations are to take place in the process.

Are you sure you need to split the code in two different files ? Could
you not produce only one file, emitting code for both the server and
the client behavior, with two separate entry point (say server_main()
and client_main()) to call from the outside ?

Pros :
- no need to split files (naming issues, location issues, etc.)
- easier to ensure consistency between the two sides (for example, if
you want type information to flow from one side to the other); might
be an important factor if you're doing non-trivial things

Cons :
- possibly less convenient to use
- client has to load the server code as well


> I'm unsure of what is the standard way of doing such a thing in Camlp4. What
> I have in mind is to use the original Ocaml syntax for the quotation expander.
> This would (IIUC) allow me to filter the AST to transform every
> antiquotation found inside the quotation itself.
> I'm not sure this is the ideal way of doing such a thing because of the size
> of the pattern matching in the AST filter.

It seems to me that you would only handle antiquotations in
expressions (or do you have a "client-server communication" meaning
for antiquotations inside other syntaxic constructs such as types or
patterns ?). Matching some cases (in that case, one) in one of the
syntaxic classes is *very* easy and quite elegant thanks to the `map`
and `fold` classes of Camlp4Ast `foldmap` could also be helpful but it
seems it is not generated by default for Camlp4Ast. If you need it
(and I think you'd need it), you can simulate a mapfold by adding a
mutable state variable to a mapper class.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-26 20:08   ` bluestorm
@ 2010-07-26 20:53     ` Raphael Proust
  0 siblings, 0 replies; 13+ messages in thread
From: Raphael Proust @ 2010-07-26 20:53 UTC (permalink / raw)
  To: bluestorm; +Cc: caml-list

On Mon, Jul 26, 2010 at 10:08 PM, bluestorm <bluestorm.dylc@gmail.com> wrote:
> On Mon, Jul 26, 2010 at 4:41 PM, Raphael Proust <raphlalou@gmail.com> wrote:
>> I'm working on a syntax extension as part of an internship in the
>> Ocsigen team. The aim of the syntax extension is to split the code of a web
>> application in two separate files: one for the client and one for the
>> server. A few transformations are to take place in the process.
>
> Are you sure you need to split the code in two different files ? Could
> you not produce only one file, emitting code for both the server and
> the client behavior, with two separate entry point (say server_main()
> and client_main()) to call from the outside ?

That would probably not be that convenient because (a) the server rarely has a
main function in these modules (although a let server_main () = () would do),
(b) the client needs to be compiled with specific libraries (for DOM
manipulation and other Javascript things), (c) the client code has to be
compiled with a different build process (because of the Javascript based target
environment)

>
> Pros :
> - no need to split files (naming issues, location issues, etc.)
The naming issues are not that difficult to manage since the two files are
generated together. Location is harder to manage, but dumping the AST should
preserves locations (I think).

> - easier to ensure consistency between the two sides (for example, if
> you want type information to flow from one side to the other); might
> be an important factor if you're doing non-trivial things

There's another quotation (<:shared< ... >>) that duplicates code amongst
the two sides. That takes care of type declarations being shared.

It could be good to have this all-in-one file compiled to ensure type checking.
We are actually implementing another solution, but I'll keep this one in mind.

>
> Cons :
> - possibly less convenient to use
> - client has to load the server code as well
>
>
>> I'm unsure of what is the standard way of doing such a thing in Camlp4. What
>> I have in mind is to use the original Ocaml syntax for the quotation expander.
>> This would (IIUC) allow me to filter the AST to transform every
>> antiquotation found inside the quotation itself.
>> I'm not sure this is the ideal way of doing such a thing because of the size
>> of the pattern matching in the AST filter.
>
> It seems to me that you would only handle antiquotations in
> expressions (or do you have a "client-server communication" meaning
> for antiquotations inside other syntaxic constructs such as types or
> patterns ?). Matching some cases (in that case, one) in one of the
> syntaxic classes is *very* easy and quite elegant thanks to the `map`
> and `fold` classes of Camlp4Ast `foldmap` could also be helpful but it
> seems it is not generated by default for Camlp4Ast. If you need it
> (and I think you'd need it), you can simulate a mapfold by adding a
> mutable state variable to a mapper class.

The antiquotations might only occur inside quotations that are either expr (for
event handler) or str_item (for shared declarations). The antiquotations have a
slightly different meaning in the two cases, but it would be long to explain.

I'll look for these functions. Thanks for the pointers.



-- 
_______
Raphael


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-26 16:30       ` Jake Donham
@ 2010-07-27  7:57         ` Raphael Proust
  0 siblings, 0 replies; 13+ messages in thread
From: Raphael Proust @ 2010-07-27  7:57 UTC (permalink / raw)
  To: Jake Donham; +Cc: Nicolas Pouillard, caml-list

On Mon, Jul 26, 2010 at 6:30 PM, Jake Donham <jake@donham.org> wrote:
> On Mon, Jul 26, 2010 at 8:41 AM, Raphael Proust <raphlalou@gmail.com> wrote:
>> What I basically need is to get an AST with antiquotations and quotations being
>> special nodes. How is this achievable w/o reimplementing a whole grammar?
>
> There is already a special node for antiquotations, but not
> quotations; see below.
>
>> The alternative solution is to use raw strings, to find antiquotation marks, to
>> split the string and to reinject it in the different files. Is there a way to
>> keep precise _loc information this way?
>
> [...]
>
> I'm not sure what syntactic problems Nicolas suggests you would run
> into by reusing the existing parser and quotation mechanism. But it
> might be a bit hairy to implement. One issue is that you can't just
> drop antiquotations anywhere; where they can appear (and with what
> tag) is given by the parser. If you want to go in this direction, the
> place to start would be Camlp4QuotationCommon.ml, which implements
> OCaml AST quotations / antiquotations. You can see there that you can
> parse starting at arbitrary non-terminals, and you can filter the
> OCaml AST without a giant pattern match using the Ast.map object.

I'll read the Camlp4QuotationCommon source code and try to have something like
{:on{ f $y$ }} working. Thanks for the answer and thanks for the Camlp4 blog
post series, it helped me a lot to get the first version of my extension
working!



-- 
_______
Raphael


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-26 14:41 ` [Camlp4] Quotation expander with OCaml syntax Raphael Proust
  2010-07-26 15:13   ` [Caml-list] " Nicolas Pouillard
  2010-07-26 20:08   ` bluestorm
@ 2010-07-27 13:22   ` Thomas.Gazagnaire
  2010-07-27 14:38     ` Raphael Proust
  2010-07-27 14:47     ` Vincent Balat
  2 siblings, 2 replies; 13+ messages in thread
From: Thomas.Gazagnaire @ 2010-07-27 13:22 UTC (permalink / raw)
  To: Raphael Proust; +Cc: caml-list

Are you trying to the same thing as HOP [1] ?

What kind of restrictions do you impose on the expressions you can
transfer from the server to the client (only non-functional values) ? How
do you ensure them in your program ?

Are you sure splitting the code into two parts is sufficient ? How will
you handle branching than you can decide only at runtime, as example ? HOP
is using a javascrip compiler embedded in a library to compile efficiently
the right client code at runtime.

Btw, would be glad to have more details on what you have done and plan to
do as I am working on similar things (not very actively currently) : I've
been trying  to make camloo, a caml-light to bigloo compiler, working
again (current version[2] is quite working - still few things to finish).
At one point, the goal would be to extand the source language with similar
constructs than yours  and to compile to HOP ...

[1] http://hop.inria.fr
[2] http://www-sop.inria.fr/members/Thomas.Gazagnaire/
--
Thomas

> Hi all,
>
> I'm working on a syntax extension as part of an internship in the
Ocsigen team. The aim of the syntax extension is to split the code of a
web
> application in two separate files: one for the client and one for the
server. A
> few transformations are to take place in the process.
>
> Quotations are to be transformed into client code while antiquotations
can
> refer
> to server side values transmitted to the client at runtime.
>
>
> In order to avoid any XY problems, here is an abstracted and simplified
example
> of the expected behavior:
>
> (* Pre-parsed code: *)
> let start = <:on< f $y$ >> in
> let html_node =
>  span ~a:[onclick start] "some text" (* a is used for (html) attributes
*)
>
> (* Server side post-parsed code: *)
> let start _arg1 =
>  "call_closure(some_unique_name," ^ mymarshall _arg1 ")"
> in
> let html_node = span ~a:[onclick (start y)] "some text"
>
> (* Client side post-parsed code: *)
> let _ = register_closure some_unique_name (fun _arg1 -> f _arg1)
>
>
>
> If the example isn't clear enough I can detail it a little bit more.
>
>
> I'm unsure of what is the standard way of doing such a thing in Camlp4.
What
> I
> have in mind is to use the original Ocaml syntax for the quotation
expander.
> This would (IIUC) allow me to filter the AST to transform every
> antiquotation
> found inside the quotation itself.
>
> I'm not sure this is the ideal way of doing such a thing because of the
size
> of
> the pattern matching in the AST filter. On the other hand, because the
quotation
> is supposed to contain valid OCaml code, it seems normal to reuse the
original
> parser.
>
> I considered an alternative solution: treating quotations as raw text (with
> a
> custom quotation expander) but that would destroy any _loc information
and
> make
> compile time warnings and errors quite difficult to locate.
>
>
> Is there a simpler/fitter way of doing that? (Is the Y correct wrt the
X?)
>
> How can one embed the original parser in a quotation expander? (I
couldn't
> find
> a function of type string -> Ast.expr in the Camlp4 doc/mlis, but I'd be
happy
> to be pointed to one if it exists. I think it would at least require
some
> functor application.)
>
> Does anyone know of any example that resemble what I'm trying to
achieve?
>
>
>
> --
> _______
> Raphael
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-27 13:22   ` Thomas.Gazagnaire
@ 2010-07-27 14:38     ` Raphael Proust
  2010-07-27 14:47     ` Vincent Balat
  1 sibling, 0 replies; 13+ messages in thread
From: Raphael Proust @ 2010-07-27 14:38 UTC (permalink / raw)
  To: Thomas.Gazagnaire; +Cc: caml-list

On Tue, Jul 27, 2010 at 3:22 PM,  <Thomas.Gazagnaire@sophia.inria.fr> wrote:
> Are you trying to the same thing as HOP [1] ?

I haven't looked at HOP yet, but I heard a lot and will probably try to read
about it in the future.

>
> What kind of restrictions do you impose on the expressions you can
> transfer from the server to the client (only non-functional values) ? How
> do you ensure them in your program ?

We try to minimize restriction by providing users a way to define their own
[un]wrappers for exotic values. Such a value is (1) wrapped, (2) marshalled, (3)
sent to the client, (4) unmarshalled and (5) unwrapped. It is not possible to
send a value that has no associated wrapper (but their a basic wrapper for
generic values). The "weirder" type we currently support is probably ['a
React.E.t][1].

Types are currently checked (and inferred) via the wrap and unwrap function.

>
> Are you sure splitting the code into two parts is sufficient ? How will
> you handle branching than you can decide only at runtime, as example ? HOP
> is using a javascrip compiler embedded in a library to compile efficiently
> the right client code at runtime.

The client code is obtained with js_of_ocaml[2], an ocaml byte-code to
Javascript compiler. Code is compiled statically. Branching is handled by the
client at runtime.

>
> Btw, would be glad to have more details on what you have done and plan to
> do as I am working on similar things (not very actively currently) : I've
> been trying  to make camloo, a caml-light to bigloo compiler, working
> again (current version[2] is quite working - still few things to finish).
> At one point, the goal would be to extand the source language with similar
> constructs than yours  and to compile to HOP ...



[1] http://erratique.ch/software/react/doc/React
[2] http://ocsigen.org/js_of_ocaml
-- 
_______
Raphael


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
  2010-07-27 13:22   ` Thomas.Gazagnaire
  2010-07-27 14:38     ` Raphael Proust
@ 2010-07-27 14:47     ` Vincent Balat
  1 sibling, 0 replies; 13+ messages in thread
From: Vincent Balat @ 2010-07-27 14:47 UTC (permalink / raw)
  To: caml-list; +Cc: Thomas.Gazagnaire, Raphael Proust

> Are you trying to the same thing as HOP [1] ?

No. Our precise goal and solutions are very different from Hop. Obviously we 
share the main general idea (from the beginning 7 years ago -- and also with 
many other projects) which is to write both sides of Web applications with the 
same high level language. But that is not new (we have been doing this for 
years). Raphaël's work is to make easier to mix client and server code in the 
same file and simplify the interaction between the client and server. We 
already have a syntax for this but it is not exactly the one we want (but this 
is only a problem with camlp4).

> What kind of restrictions do you impose on the expressions you can
> transfer from the server to the client (only non-functional values) ? How
> do you ensure them in your program ?
> 
> Are you sure splitting the code into two parts is sufficient ? How will
> you handle branching than you can decide only at runtime, as example ? HOP
> is using a javascrip compiler embedded in a library to compile efficiently
> the right client code at runtime.
> 
> Btw, would be glad to have more details on what you have done and plan to
> do as I am working on similar things (not very actively currently) : I've
> been trying  to make camloo, a caml-light to bigloo compiler, working
> again (current version[2] is quite working - still few things to finish).
> At one point, the goal would be to extand the source language with similar
> constructs than yours  and to compile to HOP ...

You can find Eliom's documentation on http://ocsigen.org . The documentation 
for the development version is not yet written (we will put it online probably 
in september, and release version 2 of Eliom a few months later). If you are 
working on this with Manuel, we will probably meet soon during the next 
meeting of the PWD project and I will explain you our solution in details.

Vincent Balat

> [1] http://hop.inria.fr
> [2] http://www-sop.inria.fr/members/Thomas.Gazagnaire/
> --
> Thomas
> 
> > Hi all,
> > 
> > I'm working on a syntax extension as part of an internship in the
> 
> Ocsigen team. The aim of the syntax extension is to split the code of a
> web
> 
> > application in two separate files: one for the client and one for the
> 
> server. A
> 
> > few transformations are to take place in the process.
> > 
> > Quotations are to be transformed into client code while antiquotations
> 
> can
> 
> > refer
> > to server side values transmitted to the client at runtime.
> > 
> > 
> > In order to avoid any XY problems, here is an abstracted and simplified
> 
> example
> 
> > of the expected behavior:
> > 
> > (* Pre-parsed code: *)
> > let start = <:on< f $y$ >> in
> > let html_node =
> > 
> >  span ~a:[onclick start] "some text" (* a is used for (html) attributes
> 
> *)
> 
> > (* Server side post-parsed code: *)
> > let start _arg1 =
> > 
> >  "call_closure(some_unique_name," ^ mymarshall _arg1 ")"
> > 
> > in
> > let html_node = span ~a:[onclick (start y)] "some text"
> > 
> > (* Client side post-parsed code: *)
> > let _ = register_closure some_unique_name (fun _arg1 -> f _arg1)
> > 
> > 
> > 
> > If the example isn't clear enough I can detail it a little bit more.
> > 
> > 
> > I'm unsure of what is the standard way of doing such a thing in Camlp4.
> 
> What
> 
> > I
> > have in mind is to use the original Ocaml syntax for the quotation
> 
> expander.
> 
> > This would (IIUC) allow me to filter the AST to transform every
> > antiquotation
> > found inside the quotation itself.
> > 
> > I'm not sure this is the ideal way of doing such a thing because of the
> 
> size
> 
> > of
> > the pattern matching in the AST filter. On the other hand, because the
> 
> quotation
> 
> > is supposed to contain valid OCaml code, it seems normal to reuse the
> 
> original
> 
> > parser.
> > 
> > I considered an alternative solution: treating quotations as raw text
> > (with a
> > custom quotation expander) but that would destroy any _loc information
> 
> and
> 
> > make
> > compile time warnings and errors quite difficult to locate.
> > 
> > 
> > Is there a simpler/fitter way of doing that? (Is the Y correct wrt the
> 
> X?)
> 
> > How can one embed the original parser in a quotation expander? (I
> 
> couldn't
> 
> > find
> > a function of type string -> Ast.expr in the Camlp4 doc/mlis, but I'd be
> 
> happy
> 
> > to be pointed to one if it exists. I think it would at least require
> 
> some
> 
> > functor application.)
> > 
> > Does anyone know of any example that resemble what I'm trying to
> 
> achieve?
> 
> > --
> > _______
> > Raphael
> > _______________________________________________
> > Caml-list mailing list. Subscription management:
> > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> > Archives: http://caml.inria.fr
> > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> > Bug reports: http://caml.inria.fr/bin/caml-bugs
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2010-07-27 14:47 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <AANLkTikuoN4H0Hsx74JwW66J9jmtq+usDxtQPpYfSGbd@mail.gmail.com>
2010-07-26 14:41 ` [Camlp4] Quotation expander with OCaml syntax Raphael Proust
2010-07-26 15:13   ` [Caml-list] " Nicolas Pouillard
2010-07-26 15:41     ` Joel Reymont
2010-07-26 16:05       ` Jake Donham
2010-07-26 15:41     ` Raphael Proust
2010-07-26 16:27       ` Nicolas Pouillard
2010-07-26 16:30       ` Jake Donham
2010-07-27  7:57         ` Raphael Proust
2010-07-26 20:08   ` bluestorm
2010-07-26 20:53     ` Raphael Proust
2010-07-27 13:22   ` Thomas.Gazagnaire
2010-07-27 14:38     ` Raphael Proust
2010-07-27 14:47     ` Vincent Balat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).