caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] camlp4: pretty printing not to a file
@ 2002-08-29 18:51 jehenrik
  2002-08-30  7:46 ` Yann Régis-Gianas
  2002-08-30  8:21 ` Daniel de Rauglaudre
  0 siblings, 2 replies; 11+ messages in thread
From: jehenrik @ 2002-08-29 18:51 UTC (permalink / raw)
  To: caml-list

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


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

end of thread, other threads:[~2002-09-06 17:13 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-29 18:51 [Caml-list] camlp4: pretty printing not to a file jehenrik
2002-08-30  7:46 ` Yann Régis-Gianas
2002-08-30  8:34   ` Daniel de Rauglaudre
2002-09-05 16:29   ` jehenrik
2002-09-06  1:36     ` Daniel de Rauglaudre
2002-09-06  9:09       ` Yann Régis-Gianas
2002-09-06 12:29         ` Daniel de Rauglaudre
2002-09-06 16:35         ` jehenrik
2002-09-06 17:13           ` Daniel de Rauglaudre
2002-09-06 16:45         ` jehenrik
2002-08-30  8:21 ` Daniel de Rauglaudre

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