caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] ppx_metaquot location passing
@ 2015-09-02  8:23 Dmitry Bely
  2015-09-02  9:09 ` Alain Frisch
  0 siblings, 1 reply; 3+ messages in thread
From: Dmitry Bely @ 2015-09-02  8:23 UTC (permalink / raw)
  To: Caml List

I'm writing a ppx rewriter. It iterates over structure items and for
some of them generates the new ones. A simplified example:

let defs = ref []

let mapper argv =
  let expr mapper expr =
    ...
  let rec structure mapper li =
    let li =
      List.fold_left (fun acc ({pstr_loc = loc} as si) ->
        let si = default_mapper.structure_item mapper si in
        let sli =
          List.map
            (fun (n, v) ->
              [%stri let [%p pvar n] = [%e str v]])
            !defs in
        defs := [];
        (si::sli) @ acc) [] li in
    List.rev li
...

The question is how to pass the location information for [%stri]
generated code (loc in my example)? Of course I can write

{[%stri let [%p pvar n] = [%e str v]] with pstr_loc = loc}

but I would like the location of all nested expressions to be set to
loc as well.

- Dmitry Bely

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

* Re: [Caml-list] ppx_metaquot location passing
  2015-09-02  8:23 [Caml-list] ppx_metaquot location passing Dmitry Bely
@ 2015-09-02  9:09 ` Alain Frisch
  2015-09-02  9:47   ` Dmitry Bely
  0 siblings, 1 reply; 3+ messages in thread
From: Alain Frisch @ 2015-09-02  9:09 UTC (permalink / raw)
  To: Dmitry Bely, Caml List

Hi Dmitry,

Looking at the comments in ppx_metaquot.ml, there are two ways to 
achieve that:

   - By default, the location of all created notes is set to the 
expression "!Ast_helper.default_loc".  You can set this reference 
directly or with Ast_helper.with_default_loc.

   - You can change this expression with a @metaloc attribute (typically 
to specify a locally bound variable).


Hope this helps,

Alain



On 02/09/2015 10:23, Dmitry Bely wrote:
> I'm writing a ppx rewriter. It iterates over structure items and for
> some of them generates the new ones. A simplified example:
>
> let defs = ref []
>
> let mapper argv =
>    let expr mapper expr =
>      ...
>    let rec structure mapper li =
>      let li =
>        List.fold_left (fun acc ({pstr_loc = loc} as si) ->
>          let si = default_mapper.structure_item mapper si in
>          let sli =
>            List.map
>              (fun (n, v) ->
>                [%stri let [%p pvar n] = [%e str v]])
>              !defs in
>          defs := [];
>          (si::sli) @ acc) [] li in
>      List.rev li
> ...
>
> The question is how to pass the location information for [%stri]
> generated code (loc in my example)? Of course I can write
>
> {[%stri let [%p pvar n] = [%e str v]] with pstr_loc = loc}
>
> but I would like the location of all nested expressions to be set to
> loc as well.
>
> - Dmitry Bely
>

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

* Re: [Caml-list] ppx_metaquot location passing
  2015-09-02  9:09 ` Alain Frisch
@ 2015-09-02  9:47   ` Dmitry Bely
  0 siblings, 0 replies; 3+ messages in thread
From: Dmitry Bely @ 2015-09-02  9:47 UTC (permalink / raw)
  Cc: Caml List

On Wed, Sep 2, 2015 at 12:09 PM, Alain Frisch <alain.frisch@lexifi.com> wrote:

Hi Alain,

> Looking at the comments in ppx_metaquot.ml, there are two ways to achieve
> that:
>
>   - By default, the location of all created notes is set to the expression
> "!Ast_helper.default_loc".  You can set this reference directly or with
> Ast_helper.with_default_loc.

Indeed. Thanks! I was struggling with @metaloc and just overlooked that.

- Dmitry Bely

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

end of thread, other threads:[~2015-09-02  9:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-02  8:23 [Caml-list] ppx_metaquot location passing Dmitry Bely
2015-09-02  9:09 ` Alain Frisch
2015-09-02  9:47   ` Dmitry Bely

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