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
[Caml-list] camlp4: pretty printing not to a file
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-08-29 (18:50)
From: jehenrik <jehenrik@y...>
Subject: [Caml-list] camlp4: pretty printing not to a file
Hi, I want a camlp4 macro which takes a caml expression for a list and 
replaces it by a list of (source code as string, value) tuples.  I have 
two problems: printing not to a file, and pattern matching the list.  It 
seems that the good camlp4 pretty printing facilities are only able to 
go to files, which could be temporary and read back in, but that seems 
like a hack.  Also it seems like there might be a way to take the 
parsing offsets and look back in the original pre-parsed string to get 
the real source snippet.  That might be okay.  But anyway, assume I 
figure that out or write my own simple cases:

let rec print_expr e =
   match e with
     <:expr< $lid:s$ >> -> s
   | <:expr< $e1$ $e2$ >> ->
       ("(" ^ (print_expr e1) ^ " " ^ (print_expr e2) ^ ")")
   | _ -> "?";;

Or whatever.  Now I assume reading the comment in the camlp4 manual 
about only being able to match on the :: constructor, I'd do something 
like this, below.  But I'm not sure enough of the parsing algorithm to 
know if it's well formed.

let reflect_arg e =
   let pe = (print_expr e) in
   <:expr< ($str:pe$,e) >>;;

let rec map_expr f l =
   match l with
     <:expr< [$hd$::$tl$] >> ->
       (let hd2 = f hd in
        let tl2 = map_expr f tl in
        <:expr< [$hd2$ :: $tl2$] >>)
   | <:expr< [] >> -> <:expr< [] >>
   | x -> x;;  (* this just removes the incomplete matching 
warning (???) *)

   expr: LEVEL "expr1"
   [ LEFTA [ "REFLECTLIST"; hd = expr; "::"; tl = expr ->
     map_expr reflect_arg <:expr< [$hd$::$tl$] >>
   ]  ];

In any case, this butchers my grammar:

let r = Grammar.Entry.parse expr (Stream.of_string "REFLECTLIST 

let r = Grammar.Entry.parse expr (Stream.of_string "REFLECTLIST 

Can anyone please enlighten me?

Jeff Henrikson

To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners