[wg-camlp4] Against the use of syntactically-valid OCaml code for syntax extension purposes

Alain Frisch alain at frisch.fr
Tue Jan 29 12:21:51 GMT 2013


On 01/29/2013 11:52 AM, Gabriel Scherer wrote:
> Code with a special semantics should
> have a special syntax.

I agree, for most cases, although I don't think this should be a hard 
constraint.  Generic extension points are parts of the plan!

It is a good objective to make clear that an extension is used, but it 
is also quite important to keep good support from editors (coloring, 
indentation, parentheses matching), which is quite contradictory with 
the notion of quotation.  Also, relying on already parsed fragments 
discourage the creation of sub-languages with ad hoc grammar, and this 
is good (in particular, users don't need to learn new precedence rules).

I propose to start discussing the addition of attributes to most 
syntactic categories (expressions, type expressions, patterns, type 
declarations, module expressions, structure/signature items, etc).  I 
suggest to adopt "expressions" as the (default) nature of attribute's 
content, because the syntax for expressions is quite flexible.  We need 
some syntax to attach such an attribute to various syntactic categories. 
  It might be a good idea to support both prefix and postfix attributes. 
  Personally I don't care too much about the choice of syntax, and I'm 
very bad at picking good syntax.  Good proposal are welcome!  To avoid 
ambiguities, one might need to use different syntaxes for attributes on 
structure/signature items and other components.

To illustrate the idea, though, let's assume the following syntax:

   postfix:    .... (% e)
   prefix:     (@ e) ....

on structure/signature items:

   postfix:    .... (%% e)
   prefix:     (@@ e) ....


So one could write:

  (@PGSQL) dbh "select id, name from users"

  type t = { a : int (% default(2), sexp_drop_if (fun x -> x = 2)) }

  type f = {foo:int} (%sexp)

  let () = (@TEST) expr

  module M = (@ifdef SYMBOL)(struct ... end)(struct ... end)

  (@@nonrec) type t = ...


   val foo: int -> int  (%% DEPRECATED)


Alain



More information about the wg-camlp4 mailing list