Version française
Home     About     Download     Resources     Contact us    
Browse thread
Equivalent of Quotation.ExStr in new camlp4?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nicolas Pouillard <nicolas.pouillard@g...>
Subject: Re: [Caml-list] Equivalent of Quotation.ExStr in new camlp4?
On 7/25/07, Harrison, John R <john.r.harrison@intel.com> wrote:
> | No ExStr is no more supported. But you can do it your self by calling
> | the parser on your string if you really don't want switch to an AST
> | based quotation expander.
>
> OK, that's fine. But how do I do that? Concretely, this is what I had
> before, so what should I have now?
>
>   Quotation.add "" (Quotation.ExStr (fun x -> quotexpander));;
>
> I don't mind in principle writing an AST-producing expander, but at the
> moment the priority is to get my code working in 3.10 with minimal
> expenditure of effort.
>

You can instanciate an OCaml parser that way:

$ cat ex_str.mlmodule Caml =
  Camlp4OCamlParser.Make
    (Camlp4OCamlRevisedParser.Make
      (Camlp4.OCamlInitSyntax.Make(Ast)(Gram)(Quotation)));;

let quotexpander str =
  "[1; 2; 3]" (* ... do some real code on str *)

let patt_quotexpander loc _loc_name_opt str =
  Gram.parse_string Caml.patt loc (quotexpander str)

let expr_quotexpander loc _loc_name_opt str =
  Gram.parse_string Caml.expr loc (quotexpander str)

let str_item_quotexpander loc loc_name_opt str =
  <:str_item@loc< $exp: expr_quotexpander loc loc_name_opt str$ >>

let () =
  Syntax.Quotation.add "" Syntax.Quotation.DynAst.expr_tag expr_quotexpander;
  Syntax.Quotation.add "" Syntax.Quotation.DynAst.str_item_tag
str_item_quotexpander;
  Syntax.Quotation.add "" Syntax.Quotation.DynAst.patt_tag patt_quotexpander

$ ocamlc -pp camlp4of -I +camlp4 -c ex_str.ml
$ camlp4o ./ex_str.cmo -str '<<>>;;'

HTH

-- 
Nicolas Pouillard