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] Chain Camlp4 syntax extensions?
Date: Wed, 29 Sep 2010 10:27:10 +0200	[thread overview]
Message-ID: <6xiq1posyp.fsf@blau.inf.tu-dresden.de> (raw)
In-Reply-To: <AANLkTi=E_O2gnY=rStjW_UOweTGGBfXO9f_j0VjUkX4B@mail.gmail.com> (Dmitry Bely's message of "Tue, 28 Sep 2010 17:13:13 +0400")

Dmitry Bely <dmitry.bely@gmail.com> writes:

   I have two similar syntax extension that generate some functions for
   each type in the module:

   EXTEND Gram
     str_item:
       [ [ "type"; tdl = LIST1 type_declaration SEP "and" ->
           ...
   END;

I would suggest to have an additional module TypeDeclMod where
you can register functions to modify type declarations. This
module would also change the str_item parser to call all
registered functions on tdl. 

Your two modules pa_ext1 and pa_ext2 would then only register one
function with TypeDeclMod (without changing the grammar).

This is probably more or less what type-conv does.


For a type-conv example you can look at sexplib.


   And another question. For camlp5 it was possible to inject some code
   into the beginning of the generated file (the code is taken from IoXML
   syntax extension):

You can use Camlp4.Register.register_str_item_parser, to my
surprise it turns out that this requires actually less effort
than in Camlp5. The only problem is that
Register.register_str_item_parser changes a reference cell and
there is no way to get its contents. So the one who registers
last wins. The default value of that reference is
Camlp4.PreCast.Syntax.parse_implem, which gets implicitely
changed by all syntax extensions (because they modify
PreCast.Syntax). Here is an example:

==============================================================================
open Camlp4.PreCast

(* Extend or modify the ast of an implementation.
 *
 * val extend_ast : Ast.str_item -> Ast.str_item
 *)
let extend_ast str_item =
  let _loc = Loc.ghost in
  <:str_item< open Bla ; $str_item$ >>


(* Implementation parser to be used instead of the default one. To 
 * be registered with Register.register_str_item_parser.
 * 
 * my_parse_implem dir_handler start_loc char_stream  parses char_stream 
 * with the default parser from the PreCast.Syntax module. Argument 
 * start_loc is the initial location obtained for example by Loc.mk.
 * Argument dir_handler is called whenever a top-level directive is 
 * encountered in the char_stream.
 * 
 * val my_parse_implem :
 *   ?directive_handler:(Syntax.Ast.str_item -> Syntax.Ast.str_item option) ->
 *   Syntax.Ast.loc -> char Stream.t -> Ast.str_item
 *)
let my_parse_implem ?(directive_handler = fun _ -> None) _loc cs =
  let str_item = Syntax.parse_implem ~directive_handler _loc cs in
  extend_ast str_item


(* Register my_parse_implem as default implementation parser. This
 * will implicitely change Register.CurrentParser.parse_implem, which
 * is called by camlp4 to parse an implementation.
 * 
 * Note that there is currently (as of 3.12.0) no way to retrieve the
 * current contents of the reference cell that is changed by
 * Register.register_str_item_parser. However, currently no standard camlp4 
 * parser calls Register.register_str_item_parser. So our approach 
 * to register my_parse_implem which in turn calls Syntax.parse_implem 
 * works as long we are the only parsing extension which does that. 
 * If there are more parsing extensions that want to modify the ast 
 * after parsing they need to synchronize in some way.
 *)
let _ = Camlp4.Register.register_str_item_parser my_parse_implem
==============================================================================


Compile with ocamlc -I +camlp4 -c -pp camlp4orf example.ml
Use with ocamlc -pp 'camlp4o example.cmo'

Bye,

Hendrik


      parent reply	other threads:[~2010-09-29  8:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-28 13:13 Dmitry Bely
2010-09-28 16:07 ` [Caml-list] " Jake Donham
2010-09-28 16:55   ` Dmitry Bely
2010-09-29  8:27 ` 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=6xiq1posyp.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).