[wg-camlp4] A new branch to experiment with extension points

Alain Frisch alain.frisch at lexifi.com
Fri Mar 1 18:51:45 GMT 2013

On 03/01/2013 06:52 PM, Leo White wrote:
> The standard way to define a delimiter like "(:", in keeping with how
> the rest of OCaml is parsed, is to make it a single token from the lexer.


> 1. Extensions:
>      (: ident expr_opt )
>    Used as a structure item, signature item, expression or type expression.
> 2. Attributes:
>      [: ident expr_opt ]
>    Attached postfix to structure items, signature items, expressions or
>    type expressions.
> 3. With Attributes
>      with ident ( expr), ident, ident (expr)
>    Attached postfix to structure items or signature items.

This looks good to me.

We also need attributes on individual types in a type declaration.

Also, I think it make sense for structure/signature items, to consider 
that attributes are "between" items, to support things like ocamldoc 
section headers.

I've started to work on a change to the Parsetree (unfinished, not 
committed yet):


where each declared type in a type declaration become a full 
structure/signature item with a marker telling if this is the first type 
in the declaration or not (similar to the representation of signatures 
in the Types module).  This puts some extra invariants in the Parsetree 
(a Pstr_type Prec_nextmust follow another Pstr_type ..., maybe with 
attributes in between), but then we don't need to distinguish 
syntactically attributes on a declared type and attributes on the whole 
declarations, which would be hard to explain to users.

The goal is to be able to write (keeping my original syntax for 
attributes in structure items):

..(:doc 2 "My section header")

type t = {...} ..(:id expr) ..(:id expr)
and  s = {...} ..(:id expr)

..(:doc 2 "Another section header")

Here we have the following sequence of structure items:

  Pstr_attribute "doc"
  Pstr_type (Prec_first, ...)
  Pstr_attributes "id"
  Pstr_attributes "id"
  Pstr_type (Prec_next, ...)
  Pstr_attributes "id"
  Pstr_attributes "doc"

Independently of the syntax, I'd be interested to get feedback on these 
two proposals:

  -  splitting declarations of mutually recursive types into single 
declarations in the Parsetree, and

  -  having attributes as standalone items within signatures and 
structures (so also between types in a single recursive declaration) 
instead of having them attached to other items.


More information about the wg-camlp4 mailing list