From: jehenrik <jehenrik@yahoo.com>
To: caml-list@inria.fr
Subject: [Caml-list] camlp4: pretty printing not to a file
Date: Thu, 29 Aug 2002 14:51:41 -0400 [thread overview]
Message-ID: <5B049D08-BB80-11D6-9EB3-00039375801A@yahoo.com> (raw)
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
next reply other threads:[~2002-08-29 18:50 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-08-29 18:51 jehenrik [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5B049D08-BB80-11D6-9EB3-00039375801A@yahoo.com \
--to=jehenrik@yahoo.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox