caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Information for writting a ppx
@ 2016-06-17 17:10 Xavier Van de Woestyne
  2016-06-20 14:15 ` Anton Bachin
  0 siblings, 1 reply; 5+ messages in thread
From: Xavier Van de Woestyne @ 2016-06-17 17:10 UTC (permalink / raw)
  To: caml-list

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

Hello,

I have writted some ppx extension but I have a question,

Is there a proper way to add a start "file" structure.  

I have try using the "structure" member of the mapper :

  

`

let new_mapper =

 Ast_mapper.{  
   default_mapper with  
   structure = general_structure  
 }`

  

Where general_structure is just a

`my_begining_structure :: List.map (...)` but my begining

structure is repeated into each module. It is logic but I need

to add my own structure only at the begining of my file. In an

other project, I use a mandatory attribute ([@@@active_this_ppx]

for example), but imho, it is not a good approach.

  

Thanks a lot for your feedback and advices !

  

Xavier Van de Woestyne

Développeur à Derniercri.io

http://derniercri.io

![](https://link.nylas.com/open/3hl20o5fseo0be0el9ic747vo/local-
19c25162-3d3f?r=Y2FtbC1saXN0QGlucmlhLmZy)


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

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

* Re: [Caml-list] Information for writting a ppx
  2016-06-17 17:10 [Caml-list] Information for writting a ppx Xavier Van de Woestyne
@ 2016-06-20 14:15 ` Anton Bachin
  2016-06-20 15:01   ` Xavier Van de Woestyne
  0 siblings, 1 reply; 5+ messages in thread
From: Anton Bachin @ 2016-06-20 14:15 UTC (permalink / raw)
  To: Xavier Van de Woestyne; +Cc: caml-list

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

Hi Xavier,

Bisect_ppx does this by looking at the location of each module it sees in “structure". The file names are stored in a set. If a file name is not yet in the set, Bisect_ppx assumes that the module is the top-level module of a file. We haven’t had any problems with it.

You can see it here: https://github.com/aantron/bisect_ppx/blob/afa95d9f02ab4c95df3af283641c73ec1dc8cfb1/src/syntax/instrumentPpx.ml#L480 <https://github.com/aantron/bisect_ppx/blob/afa95d9f02ab4c95df3af283641c73ec1dc8cfb1/src/syntax/instrumentPpx.ml#L480>

The code is a bit old, but there it is.

Best,
Anton

> On Jun 17, 2016, at 12:10, Xavier Van de Woestyne <xavier@derniercri.io> wrote:
> 
> Hello, 
> I have writted some ppx extension but I have a question, 
> Is there a proper way to add a start "file" structure.
> I have try using the "structure" member of the mapper : 
> 
> `
> let new_mapper =
>  Ast_mapper.{
>    default_mapper with
>    structure = general_structure
>  }`
> 
> Where general_structure is just a
> `my_begining_structure :: List.map (...)` but my begining 
> structure is repeated into each module. It is logic but I need 
> to add my own structure only at the begining of my file. In an 
> other project, I use a mandatory attribute ([@@@active_this_ppx] 
> for example), but imho, it is not a good approach. 
> 
> Thanks a lot for your feedback and advices ! 
> 
> Xavier Van de Woestyne
> Développeur à Derniercri.io
> http://derniercri.io
> 


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

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

* Re: [Caml-list] Information for writting a ppx
  2016-06-20 14:15 ` Anton Bachin
@ 2016-06-20 15:01   ` Xavier Van de Woestyne
  2016-06-20 15:31     ` Marc Lasson
  0 siblings, 1 reply; 5+ messages in thread
From: Xavier Van de Woestyne @ 2016-06-20 15:01 UTC (permalink / raw)
  To: Anton Bachin; +Cc: caml-list

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

Thanks a lot, Mr Marc L explain me how to do that :

  

```

(* Mapper for all transformation *)  
let general_mapper = Ast_mapper.{ default_mapper with structure = structure }  
  
  
(* New Mapper only for the toplevel *)  
let toplevel_mapper =  
 Ast_mapper.{  
   default_mapper with  
   structure   = fun _ str -&gt;  
     let pl = Ast_helper.Str.eval (Ppx.Fabric.print_endline "Hello World") in  
     pl :: (general_mapper.structure general_mapper str)  
 }  

```

  
Xavier Van de Woestyne

http://xvw.github.io

  

![](https://link.nylas.com/open/5y0wh7okaezysfzysovho20yh/local-56fe3f0e-
0d5c?r=Y2FtbC1saXN0QGlucmlhLmZy)

On juin 20 2016, at 4:16 pm, Anton Bachin &lt;antronbachin@gmail.com&gt;
wrote:  

> Hi Xavier,

>

>  
>

>

> Bisect_ppx does this by looking at the location of each module it sees in
“structure". The file names are stored in a set. If a file name is not yet in
the set, Bisect_ppx assumes that the module is the top-level module of a file.
We haven’t had any problems with it.

>

>  
>

>

> You can see it here:
[https://github.com/aantron/bisect_ppx/blob/afa95d9f02ab4c95df3af283641c73ec1dc8cfb1/src/syntax/instrumentPpx.ml#L480](https://github.com/aantron/bisect_ppx/blob/afa95d9f02ab4c95df3af283641c73ec1dc8cfb1/src/syntax/instrumentPpx.ml#L480&r=Y2FtbC1saXN0QGlucmlhLmZy)

>

>  
>

>

> The code is a bit old, but there it is.

>

>  
>

>

> Best,

>

> Anton

>

>  
>

>

>> On Jun 17, 2016, at 12:10, Xavier Van de Woestyne
&lt;[xavier@derniercri.io](mailto:xavier@derniercri.io)&gt; wrote:

>>

>>  
>

>>

>> Hello,

>>

>> I have writted some ppx extension but I have a question,

>>

>> Is there a proper way to add a start "file" structure.  
>

>>

>> I have try using the "structure" member of the mapper :

>>

>>  
>

>>

>> `

>>

>> let new_mapper =

>>

>>  Ast_mapper.{  
>     default_mapper with  
>     structure = general_structure  
>   }`

>>

>>  
>

>>

>> Where general_structure is just a

>>

>> `my_begining_structure :: List.map (...)` but my begining

>>

>> structure is repeated into each module. It is logic but I need

>>

>> to add my own structure only at the begining of my file. In an

>>

>> other project, I use a mandatory attribute ([@@@active_this_ppx]

>>

>> for example), but imho, it is not a good approach.

>>

>>  
>

>>

>> Thanks a lot for your feedback and advices !

>>

>>  
>

>>

>> Xavier Van de Woestyne

>>

>> Développeur à Derniercri.io

>>

>> [http://derniercri.io](http://derniercri.io&r=Y2FtbC1saXN0QGlucmlhLmZy)

>>

>> ![](https://link.nylas.com/open/3hl20o5fseo0be0el9ic747vo/local-
19c25162-3d3f?r=Y2FtbC1saXN0QGlucmlhLmZy)

>

>  
>


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

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

* Re: [Caml-list] Information for writting a ppx
  2016-06-20 15:01   ` Xavier Van de Woestyne
@ 2016-06-20 15:31     ` Marc Lasson
  2016-06-20 15:33       ` Xavier Van de Woestyne
  0 siblings, 1 reply; 5+ messages in thread
From: Marc Lasson @ 2016-06-20 15:31 UTC (permalink / raw)
  To: caml-list

To paraphrase the code, I just advised Xavier to use a mapper that does 
recursively not call itself.

> (* Mapper for all transformation *)
> let general_mapper = Ast_mapper.{ default_mapper with structure = 
> structure }
>
> (* New Mapper only for the toplevel *)
> let toplevel_mapper =
>  Ast_mapper.{
>    default_mapper with
>    structure   = fun _ str ->
>      let pl = Ast_helper.Str.eval (Ppx.Fabric.print_endline "Hello 
> World") in
>      pl :: (general_mapper.structure general_mapper str)
>  }
> ```

Note that if you do not plan to extend general_mapper with a mapper that 
actually does something you could simply write :

let toplevel_mapper =
  Ast_mapper.{
    default_mapper with
    structure = fun _ str ->
      let pl = Ast_helper.Str.eval (Ppx.Fabric.print_endline "Hello 
World") in
      pl :: str
  }

Also, if the ppx is also applied to compile mli interfaces, then this 
will be structures occurring within the toplevel signature (eg. in the 
payloads of attributes). So to be "safe", you should probably do :

let toplevel_mapper =
  Ast_mapper.{
    default_mapper with
    signature = fun _ -> default_mapper.signature default_mapper;
    structure = fun _ str ->
      let pl = Ast_helper.Str.eval (Ppx.Fabric.print_endline "Hello 
World") in
      pl :: str
  }

-- 
Marc.

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

* Re: [Caml-list] Information for writting a ppx
  2016-06-20 15:31     ` Marc Lasson
@ 2016-06-20 15:33       ` Xavier Van de Woestyne
  0 siblings, 0 replies; 5+ messages in thread
From: Xavier Van de Woestyne @ 2016-06-20 15:33 UTC (permalink / raw)
  To: Marc Lasson; +Cc: caml-list

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

Thanks for the point !  
  
Xavier Van de Woestyne

http://xvw.github.io

  
![](https://link.nylas.com/open/5y0wh7okaezysfzysovho20yh/local-37964069-61f2?r=Y2FtbC1saXN0QGlucmlhLmZy)

On juin 20 2016, at 5:32 pm, Marc Lasson &lt;marc.lasson@lexifi.com&gt; wrote:  

> To paraphrase the code, I just advised Xavier to use a mapper that does  
> recursively not call itself.

>

> &gt; (* Mapper for all transformation *)  
> &gt; let general_mapper = Ast_mapper.{ default_mapper with structure =  
> &gt; structure }  
> &gt;  
> &gt; (* New Mapper only for the toplevel *)  
> &gt; let toplevel_mapper =  
> &gt; Ast_mapper.{  
> &gt; default_mapper with  
> &gt; structure = fun _ str -&gt;  
> &gt; let pl = Ast_helper.Str.eval (Ppx.Fabric.print_endline "Hello  
> &gt; World") in  
> &gt; pl :: (general_mapper.structure general_mapper str)  
> &gt; }  
> &gt; ```

>

> Note that if you do not plan to extend general_mapper with a mapper that  
> actually does something you could simply write :

>

> let toplevel_mapper =  
>   Ast_mapper.{  
>     default_mapper with  
>     structure = fun _ str -&gt;  
>       let pl = Ast_helper.Str.eval (Ppx.Fabric.print_endline "Hello  
> World") in  
>       pl :: str  
>   }

>

> Also, if the ppx is also applied to compile mli interfaces, then this  
> will be structures occurring within the toplevel signature (eg. in the  
> payloads of attributes). So to be "safe", you should probably do :

>

> let toplevel_mapper =  
>   Ast_mapper.{  
>     default_mapper with  
>     signature = fun _ -&gt; default_mapper.signature default_mapper;  
>     structure = fun _ str -&gt;  
>       let pl = Ast_helper.Str.eval (Ppx.Fabric.print_endline "Hello  
> World") in  
>       pl :: str  
>   }

>

> \--  
> Marc.

>

> \--  
> Caml-list mailing list. Subscription management and archives:  
> https://sympa.inria.fr/sympa/arc/caml-list  
> Beginner's list:
[http://groups.yahoo.com/group/ocaml_beginners](http://groups.yahoo.com/group/ocaml_beginners&r=Y2FtbC1saXN0QGlucmlhLmZy)  
> Bug reports: [http://caml.inria.fr/bin/caml-bugs](http://caml.inria.fr/bin
/caml-bugs&r=Y2FtbC1saXN0QGlucmlhLmZy)


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

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

end of thread, other threads:[~2016-06-20 15:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-17 17:10 [Caml-list] Information for writting a ppx Xavier Van de Woestyne
2016-06-20 14:15 ` Anton Bachin
2016-06-20 15:01   ` Xavier Van de Woestyne
2016-06-20 15:31     ` Marc Lasson
2016-06-20 15:33       ` Xavier Van de Woestyne

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).