From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA20567; Thu, 29 Aug 2002 20:50:37 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA20455 for ; Thu, 29 Aug 2002 20:50:36 +0200 (MET DST) Received: from c007.snv.cp.net (h014.c007.snv.cp.net [209.228.33.242]) by concorde.inria.fr (8.11.1/8.11.1) with SMTP id g7TInOX15690 for ; Thu, 29 Aug 2002 20:49:30 +0200 (MET DST) Received: (cpmta 18340 invoked from network); 29 Aug 2002 11:49:17 -0700 Received: from 65.187.194.217 (HELO localhost) by smtp.directvinternet.com (209.228.33.242) with SMTP; 29 Aug 2002 11:49:17 -0700 X-Sent: 29 Aug 2002 18:49:17 GMT Date: Thu, 29 Aug 2002 14:51:41 -0400 Mime-Version: 1.0 (Apple Message framework v482) Content-Type: text/plain; charset=US-ASCII; format=flowed Subject: [Caml-list] camlp4: pretty printing not to a file From: jehenrik To: caml-list@inria.fr Content-Transfer-Encoding: 7bit Message-Id: <5B049D08-BB80-11D6-9EB3-00039375801A@yahoo.com> X-Mailer: Apple Mail (2.482) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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 (???) *) EXTEND expr: LEVEL "expr1" [ LEFTA [ "REFLECTLIST"; hd = expr; "::"; tl = expr -> map_expr reflect_arg <:expr< [$hd$::$tl$] >> ] ]; END;; In any case, this butchers my grammar: let r = Grammar.Entry.parse expr (Stream.of_string "REFLECTLIST [1;3;5;11]");; let r = Grammar.Entry.parse expr (Stream.of_string "REFLECTLIST 1::3::5::11::[]\ ");; 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