caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Hendrik Tews <tews@os.inf.tu-dresden.de>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Camlp4 type constructor transformation
Date: Wed, 23 Mar 2011 09:57:47 +0100	[thread overview]
Message-ID: <6x7hbqxlh0.fsf@blau.inf.tu-dresden.de> (raw)
In-Reply-To: <sympa.1300129576.5894.159@inria.fr> (eagriffis@gmail.com's message of "Mon, 14 Mar 2011 20:32:15 +0100")

eagriffis@gmail.com writes:

   I am having trouble getting Camlp4 to detect and transform type constructors
   that take parameters. I believe the problem is with my grammar. Here is a base
   case (call it base.ml):

If you want to transform the syntax tree after parsing you should
not modify the grammar. Instead you should install a filter
function. Transforming the syntax tree is the very purpose of
filter functions.

If you look at the sources of the camlp4 parser, you see

    expr: LEVEL "apply" (* LEFTA *)
      [ [ e1 = SELF; e2 = SELF ->
            match (is_expr_constr_call e1, e2) with
            [ (True, <:expr< ( $tup:e$ ) >>) ->
                List.fold_left (fun e1 e2 -> <:expr< $e1$ $e2$ >>) e1
                                (Ast.list_of_expr e [])
            | _ -> <:expr< $e1$ $e2$ >> ]
      ] ];

i.e, your expression "Foo(1,1)" is first parsed as Foo applied to
a pair and only if is_expr_constr_call detects a constructor it
is transformed into Foo applied to 1 applied to 1.

Your rule

     expr: LEVEL "apply"
       [ LEFTA
   	[ e1 = SELF; e2 = SELF -> apply _loc e1 e2 ] ];

does not curry constructor calls. I don't understand the code in
Camlp4/Struct/Camlp4Ast2OCamlAst.ml, but I would expect that you
get strange errors if you connect this parser to the ocaml
compiler.

   It seems that my constructor of arity 2 can be located as an uppercase
   identifier nested twice deep in ExApps. So either I do not understand how to
   modify the grammar rule to catch this, or the problem is not in the rule?

This is true, but only after the transformation in the original
parser. Filter functions run after the parsing completed, so a
filter function sees precisely what you expect.


Bye,

Hendrik

      reply	other threads:[~2011-03-23  8:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-14 19:32 eagriffis
2011-03-23  8:57 ` Hendrik Tews [this message]

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=6x7hbqxlh0.fsf@blau.inf.tu-dresden.de \
    --to=tews@os.inf.tu-dresden.de \
    --cc=caml-list@yquem.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;
as well as URLs for NNTP newsgroup(s).