* [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
* Re: [Caml-list] camlp4: pretty printing not to a file
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-08-30 8:21 ` Daniel de Rauglaudre
1 sibling, 2 replies; 11+ messages in thread
From: Yann Régis-Gianas @ 2002-08-30 7:46 UTC (permalink / raw)
To: jehenrik; +Cc: caml-list
On Thu, Aug 29, 2002 at 02:51:41PM -0400, jehenrik wrote:
> 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,
Yes. The first time I tried to print AST with camlp4, I
thought that camlp4 should be called camlp2 ... In fact, it is due to
the poor documentation about printers. It is possible to print AST in
something different from file using global variables. Look:
(** global buffer. *)
let b = Buffer.create 1024
(** our printer *)
let printer pr x =
Buffer.clear b;
Spretty.print_pretty Format.print_char Format.print_string
Format.print_flush "" "" 78
(fun _ _ -> ("", 0, 0, 0)) 0 (pr.pr_fun "top" x "" [<>]);
Format.print_flush ();
Buffer.contents b
(** initialize the pretty printer output. *)
let init_printer () =
let _ = Buffer.clear b in
let null () = ()
and bufferize s start len =
Buffer.add_substring b s start len;
in
Format.set_formatter_output_functions bufferize null
(** printer for ocaml types *)
let print_ctyp = printer pr_ctyp
(** printer for ocaml expr *)
let print_expr = printer pr_expr
(** printer for ocaml pattern *)
let print_patt = printer pr_patt
--
Yann Régis-Gianas.
-------------------
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
* Re: [Caml-list] camlp4: pretty printing not to a file
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:21 ` Daniel de Rauglaudre
1 sibling, 0 replies; 11+ messages in thread
From: Daniel de Rauglaudre @ 2002-08-30 8:21 UTC (permalink / raw)
To: caml-list
Hi,
On Thu, Aug 29, 2002 at 02:51:41PM -0400, jehenrik wrote:
> 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.
let getsource fname (bp, ep) =
let ic = open_in_bin fname in
let s = String.create (ep - bp) in
seek_in ic bp;
really_input ic s 0 (ep - bp);
close_in ic;
s
> EXTEND
> expr: LEVEL "expr1"
> [ LEFTA [ "REFLECTLIST"; hd = expr; "::"; tl = expr ->
> map_expr reflect_arg <:expr< [$hd$::$tl$] >>
> ] ];
> END;;
>
> let r = Grammar.Entry.parse expr (Stream.of_string "REFLECTLIST
> [1;3;5;11]");;
Input: REFLECTLIST [1;3;5;11]
The rule recognizes the keyword REFLECTLIST.
Then the rule symbol "hd = expr" recognizes the expr "[1;3;5;11]"
Then the rule expects a "::".
There is no "::".
Therefore: syntax error.
> let r = Grammar.Entry.parse expr (Stream.of_string "REFLECTLIST
> 1::3::5::11::[]\
> ");;
Idem. The symbol "hd = expr" recognizes the expr "1::3::5::11::[]".
There is no "::" following it.
Therefore syntax error again.
> Can anyone please enlighten me?
And why not just:
EXTEND
expr: LEVEL "expr1"
[ LEFTA [ "REFLECTLIST"; e = expr ->
map_expr reflect_arg e
] ];
END;;
--
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
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
* Re: [Caml-list] camlp4: pretty printing not to a file
2002-08-30 7:46 ` Yann Régis-Gianas
@ 2002-08-30 8:34 ` Daniel de Rauglaudre
2002-09-05 16:29 ` jehenrik
1 sibling, 0 replies; 11+ messages in thread
From: Daniel de Rauglaudre @ 2002-08-30 8:34 UTC (permalink / raw)
To: caml-list
Hi,
On Fri, Aug 30, 2002 at 09:46:29AM +0200, Yann Régis-Gianas wrote:
> Yes. The first time I tried to print AST with camlp4, I
> thought that camlp4 should be called camlp2 ... In fact, it is due to
> the poor documentation about printers.
Indeed. The pretty printing in Camlp4 is not as developped as the
parsing. For the moment I don't have clear ideas to how to have good
pretty printing facilities with extensibility. This exists, but it is
experimental and incomplete and is likely to change. I prefer not
document that.
Remark: Jeff's question was actually about parsing, not pretty printing.
--
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
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
* Re: [Caml-list] camlp4: pretty printing not to a file
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
1 sibling, 1 reply; 11+ messages in thread
From: jehenrik @ 2002-09-05 16:29 UTC (permalink / raw)
To: yann; +Cc: caml-list
Thanks for the response, this seems to be what I am looking for, but the
code snippet you sent does not work for me. I'm running ocaml+camlp4
3.04, and did not get anything working or comprehensible with my limited
knowledge of the camlp4 code base. I had to edit a couple of
expressions to get things to type check, that may be the source of
problems. But could you verify it please? The behavior now is:
(** printer for ocaml expr *)
let print_expr2 = printer pr_expr;;
let r = Grammar.Entry.parse expr (Stream.of_string
"let foo = x + 2 in foo * foo");;
print_expr2 r;;
<pr_fun: not impl: expr; tag = 14>- : string = ""
^^^ this stuff comes out on stderr, I think. Either that or stdout.
The code with my changes is:
(** global buffer. *)
let b = Buffer.create 1024;;
(** our printer *)
let printer pr x =
Buffer.clear b;
Spretty.print_pretty Format.print_char Format.print_string
Format.print_flush "" "" 78
(fun (_,_) -> ()) (pr.pr_fun "top" x "" [<>]);
Format.print_flush ();
Buffer.contents b;;
(** initialize the pretty printer output. *)
let init_printer () =
let _ = Buffer.clear b in
let null () = ()
and bufferize s start len =
Buffer.add_substring b s start len;
in
Format.set_formatter_output_functions bufferize null;;
(** printer for ocaml types *)
(*let print_ctyp = printer pr_ctyp;;*)
(** printer for ocaml expr *)
let print_expr2 = printer pr_expr;;
(** printer for ocaml pattern *)
let print_patt = printer pr_patt;;
Your help is much appreciated. Thanks.
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
* Re: [Caml-list] camlp4: pretty printing not to a file
2002-09-05 16:29 ` jehenrik
@ 2002-09-06 1:36 ` Daniel de Rauglaudre
2002-09-06 9:09 ` Yann Régis-Gianas
0 siblings, 1 reply; 11+ messages in thread
From: Daniel de Rauglaudre @ 2002-09-06 1:36 UTC (permalink / raw)
To: caml-list
Hi,
On Thu, Sep 05, 2002 at 12:29:23PM -0400, jehenrik wrote:
> Thanks for the response, this seems to be what I am looking for, but the
> code snippet you sent does not work for me. I'm running ocaml+camlp4
> 3.04
Indeed your code does not work in the latest version 3.06, because
the interface of Spretty has changed. Spretty is not documented because
it is likely to change and you use it with your own risks.
The message you get:
> print_expr2 r;;
> <pr_fun: not impl: expr; tag = 14>- : string = ""
is an error from me, I am sorry. Not a bug, but an error in the
message. I changed it in my current version and now the message
will be:
print_expr2 r;;
Exception: Failure "no loaded printer for expr".
indicating that you need to load a printer. Do:
#load "pr_o.cmo";;
to get a printing module (or pr_r.cmo if you want to pretty print
in revised syntax).
> Spretty.print_pretty Format.print_char Format.print_string
> Format.print_flush "" "" 78
> (fun (_,_) -> ()) (pr.pr_fun "top" x "" [<>]);
Mmmm... I see that this code has been borrowed from the module Pcaml
of Camlp4. But I am not sure of my code, in particular the use of
"Format" because Spretty has its own pretty printing features which
does not use Format. I have to look at that.
And in version 3.06, because of interface change, this code does not
work any more. Write it as:
Spretty.print_pretty Format.print_char Format.print_string
Format.print_flush "" "" 78
(fun _ _ -> "", 0, 0, 0) 0 (pr_fun "top" x "" [<>]);
But I repeat that this code is not documented and you use it at
your own risks.
Well, since I see that people would like to use the Camlp4 pretty
printing like that, I am going to see how I could propose that with
a good interface and without these hacks. In the meantime, you can ask
me for hints.
--
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
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
* Re: [Caml-list] camlp4: pretty printing not to a file
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
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Yann Régis-Gianas @ 2002-09-06 9:09 UTC (permalink / raw)
To: Daniel de Rauglaudre; +Cc: caml-list
On Fri, Sep 06, 2002 at 03:36:28AM +0200, Daniel de Rauglaudre wrote:
> Well, since I see that people would like to use the Camlp4 pretty
> printing like that, I am going to see how I could propose that with
> a good interface and without these hacks. In the meantime, you can ask
> me for hints.
>
Something like:
"PCaml.string_of_X : X -> string" where X is in { expr, patt, ctyp ... }
could be simple and useful. It can be the application of this
function with a default format configuration structure:
"PCaml.string_of_X_formatted : format_config -> X -> string"
--
Yann Régis-Gianas.
-------------------
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
* Re: [Caml-list] camlp4: pretty printing not to a file
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 16:45 ` jehenrik
2 siblings, 0 replies; 11+ messages in thread
From: Daniel de Rauglaudre @ 2002-09-06 12:29 UTC (permalink / raw)
To: caml-list
Hi,
On Fri, Sep 06, 2002 at 11:09:09AM +0200, Yann Régis-Gianas wrote:
> "PCaml.string_of_X : X -> string" where X is in { expr, patt, ctyp ... }
Ok, this is implemented and committed in Camlp4 CVS directory.
I added a new function "Pcaml.string_of" of type:
'a printer_t -> 'a -> string
You can use it with Pcaml.pr_expr, Pcaml.pr_patt, and so on.
Example, pretty printing in normal syntax by loading pr_o.cmo:
$ ocaml camlp4o.cma pr_o.cmo q_MLast.cmo
Objective Caml version 3.06
Camlp4s Parsing version 3.06
# let loc = 0, 0;;
val loc : int * int = (0, 0)
# Pcaml.string_of Pcaml.pr_sig_item <:sig_item< value x : int >>;;
- : string = "val x : int"
# Pcaml.string_of Pcaml.pr_str_item <:str_item< value x = 3 >>;;
- : string = "let x = 3"
# Pcaml.string_of Pcaml.pr_module_type
<:module_type< sig value x : int; end >>;;
- : string = "sig val x : int end"
# Pcaml.string_of Pcaml.pr_module_expr
<:module_expr< struct value x = 3; end >>;;
- : string = "struct let x = 3 end"
# Pcaml.string_of Pcaml.pr_expr <:expr< let x = 3 in x + 2 >>;;
- : string = "let x = 3 in x + 2"
# Pcaml.string_of Pcaml.pr_patt <:patt< ([x; y; z :: t], 5) >>;;
- : string = "x :: y :: z :: t, 5"
# Pcaml.string_of Pcaml.pr_ctyp <:ctyp< list (list (int * string)) >>;;
- : string = "(int * string) list list"
# Pcaml.string_of Pcaml.pr_class_sig_item
<:class_sig_item< method virtual foo : bar >>;;
- : string = "method virtual foo : bar"
# Pcaml.string_of Pcaml.pr_class_str_item
<:class_str_item< method private foo = 3 >>;;
- : string = "method private foo = 3"
# Pcaml.string_of Pcaml.pr_class_type
<:class_type< [ a ] -> [ b ] -> object method foo : bar; end >>;;
- : string = "a -> b -> object method foo : bar end"
# Pcaml.string_of Pcaml.pr_class_expr
<:class_expr< object value foo = 3; method foo = bar; end >>;;
- : string = "object val foo = 3 method foo = bar end"
Hope this helps, comme on dit dans ce cas-là.
--
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
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
* Re: [Caml-list] camlp4: pretty printing not to a file
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
2 siblings, 1 reply; 11+ messages in thread
From: jehenrik @ 2002-09-06 16:35 UTC (permalink / raw)
To: yann; +Cc: Daniel de Rauglaudre, caml-list
> Something like:
>
> "PCaml.string_of_X : X -> string" where X is in { expr, patt, ctyp ... }
>
> could be simple and useful. It can be the application of this
> function with a default format configuration structure:
>
> "PCaml.string_of_X_formatted : format_config -> X -> string"
It seems like the Format module provides for whatever "format_config"
would be, no? For example, I changed your code to the following, which
does not require global variables:
(** our printer *)
let printer pr x =
let b = Buffer.create 1024 in
let f = Format.formatter_of_buffer b in
Spretty.print_pretty (Format.pp_print_char f) (Format.pp_print_string
f)
(Format.pp_print_flush f) "" "" 78
(fun (_,_) -> ()) (pr.pr_fun "top" x "" [<>]);
Format.pp_print_flush f ();
Buffer.contents b;;
No worrying about redirecting other formatters necessary. And the
Format.pp_set_all_formatter_output_functions function seems to provide
what you would need in terms of "formatting options," at least what I
can think of. So maybe camlp4 just needs a function:
PCaml.make_formatted_printer : (unit -> Format.formatter) -> 'a
Grammar.Entry.e -> 'a -> string
The thunk is so that optionally a fresh buffer can be allocated each
time. And then a convenience function with a normal "make buffer,
print, dump string" thing plugged in:
PCaml.print_to_string : 'a Grammar.Entry.e -> 'a -> string
Jeff
-------------------
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
* Re: [Caml-list] camlp4: pretty printing not to a file
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 16:45 ` jehenrik
2 siblings, 0 replies; 11+ messages in thread
From: jehenrik @ 2002-09-06 16:45 UTC (permalink / raw)
To: yann; +Cc: Daniel de Rauglaudre, caml-list
Erg, I have a trigger happy send button finger. I didn't get this right:
> PCaml.make_formatted_printer : (unit -> Format.formatter) -> 'a
Grammar.Entry.e -> 'a -> string
Because how do you get the string out. How about
PCaml.make_formatted_printer : (unit -> Format.formatter) -> (unit ->
'b) -> 'a Grammar.Entry.e -> 'a -> 'b
So that way you can get out something besides a string if you wanted.
Jeff
-------------------
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
* Re: [Caml-list] camlp4: pretty printing not to a file
2002-09-06 16:35 ` jehenrik
@ 2002-09-06 17:13 ` Daniel de Rauglaudre
0 siblings, 0 replies; 11+ messages in thread
From: Daniel de Rauglaudre @ 2002-09-06 17:13 UTC (permalink / raw)
To: caml-list
Hi,
On Fri, Sep 06, 2002 at 12:35:59PM -0400, jehenrik wrote:
> It seems like the Format module provides for whatever "format_config"
> would be, no? For example, I changed your code to the following, which
> does not require global variables:
It is not possible to use the Format module because the pretty
printing functions of Camlp4 don't use Format.
Spretty is already a system of pretty print. If you use Format, there
will be conflicts with newlines and tabulations.
--
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
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