English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
combining camlp4 extensions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-09-17 (07:22)
From: Pietro Abate <Pietro.Abate@a...>
Subject: combining camlp4 extensions
hi all,

In my project I've multiple similar (independent) extensions. These all
share a common part. In order to avoid code duplication (I don't want to
duplicate the common part in every and each extension), I'm trying to
split them in different modules and then generate one library for each
extensions by linking the common part with each module.

I've cooked up a small example to explain my idea.

I've 3 files: ext_repeat.ml and repeat.ml and main.ml

------------ ext_repeat.ml
open Pcaml
let reexpr = Grammar.Entry.create Pcaml.gram "reexpr";;
let extend () =
    GLOBAL: reexpr;
      reexpr: [[ e = expr -> e ]];

open Pcaml
let reexpr = Grammar.Entry.create Pcaml.gram "reexpr";;
let extend () =
    GLOBAL: reexpr expr ;
      expr: LEVEL "expr1"
        [[ "repeat"; e1 = reexpr; "until"; e2 = expr ->
              <:expr< do { $e1$; while not $e2$ do { $e1$; } } >> ]];

      reexpr: [[]];

let main () =
    Repeat.extend ();
    Ext_repeat.extend ();
in main ()

repeat contains a modified version of the canonical repeat example found
in the camlp4 manual (the common part). The ext_repeat contains a small
extension that is mean to complement the extension defined in the repeat
module (the extension part that differ. I can have many of these in my
project).  The main file is just a device to fire up these extensions.

I compile them with:

ocamlc -pp "camlp4o pa_extend.cmo q_MLast.cmo" -I +camlp4 -c repeat.ml
ocamlc -pp "camlp4o pa_extend.cmo q_MLast.cmo" -I +camlp4 -c ext_repeat.ml
ocamlc -c main.ml
ocamlc ext_repeat.cmo repeat.cmo main.cmo -linkall -a -o pa_combine.cmo

camlp4o ./pa_combine.cmo pr_o.cmo ex.ml

The example that I'm using is (ex.ml):
let main () =
  let i = ref 0 in
  repeat print_int !i; incr i until !i = 10;
  print_newline ()
let _ = main ()

however this arrangement doesn't work:
camlp4o ./pa_combine.cmo pr_o.cmo ex.ml
File "", line 0, characters 0-1:
Parse error: [reexpr] expected after 'repeat' (in [expr])
Uncaught exception: Stream.Error("[reexpr] expected after 'repeat' (in [expr])")

Is there a way of achieving this kind of modularity with camlp4 ?

thanks !

this is not a re-play of an other thread a while ago...

++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html