caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Nicolas Pouillard <nicolas.pouillard@gmail.com>
To: Paul Steckler <paul.steckler@nicta.com.au>
Cc: "caml-list@yquem.inria.fr" <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Ampersand in camlp4 grammar extension
Date: Wed, 28 Jan 2009 21:47:15 +0100	[thread overview]
Message-ID: <1233174458-sup-6393@ausone.local> (raw)
In-Reply-To: <2EB36A07AAE8C44BBB1986425E7A22D0034D5BEF2D@atp-mbx1.in.nicta.com.au>

Excerpts from Paul Steckler's message of Wed Jan 28 10:39:16 +0100 2009:
> I'm writing a camlp4 grammar extension for OCaml that tries to match
> ampersands and rewrite expressions containing them.  That wouldn't
> be a good idea if my code contained ordinary ampersands used for conjunctions.
> But the code doesn't.
> 
> I'm struggling how to match the ampersands in my expression rewrite rules.
> I've tried "&" and SYMBOL "&", which don't work -- the preprocessed file contains
> the original ampersands.  OTOH, I've written rules which use things like "->" and
> work fine.

The answer depends on what you want to achieve, if you want to give to "&"
another syntactic form (no longer infix, or change the priorities), then I
would recommend you to just use "&" in your grammar rules. It will declares it
as a keyword (add it into a table of keywords) and then the token filter
between (between the lexer and the parser) will transform it from SYMBOL to
KEYWORD.

In fact, I've just looked at the lexer and the '&' token is *at the beginning*
emitted using the SYMBOL constructor, however by looking at the OCaml parser
files, one can see that "&" is already used literally, so already declared as
a keyword and so already transformed as a KEYWORD "&". Conclusion use "&" to
match it.

However I can guess in your question that you don't really want to change the
syntax of the binary operator "&", but rather to change it's meaning. In
camlp4 there is a much more sane and easier way to do this using filters.
By the way there is an example in the camlp4 sources that does exactly this.

$ cd camlp4/examples
$ cat apply_operator.ml
open Camlp4.PreCast;
AstFilters.register_str_item_filter
  (Ast.map_expr
    (fun
     [ <:expr@loc< $e1$ & $e2$ >> -> <:expr@loc< $e1$ $e2$ >>
     | e -> e ]))#str_item;
$ ocamlbuild -tags camlp4rf,use_camlp4 apply_operator.cmo
$ cat apply_operator_test.ml
(* To force it to be inlined. If not it's not well typed. *)
let ( & ) = ();;

fun f g h x -> f & g & h x
$ camlp4o ./_build/apply_operator.cmo apply_operator_test.ml
(* To force it to be inlined. If not it's not well typed. *)
let ( & ) = ()
    
let _ = fun f g h x -> f (g (h x))

Best regards,

-- 
Nicolas Pouillard


  reply	other threads:[~2009-01-28 20:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-28  9:39 Paul Steckler
2009-01-28 20:47 ` Nicolas Pouillard [this message]
2009-01-29  4:36   ` [Caml-list] " Paul Steckler
2009-01-29  9:17     ` nicolas.pouillard

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=1233174458-sup-6393@ausone.local \
    --to=nicolas.pouillard@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=paul.steckler@nicta.com.au \
    /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).