Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
camlp4: Understanding class_declaration
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-10-06 (07:35)
From: Nicolas Pouillard <nicolas.pouillard@i...>
Subject: Re: [Caml-list] camlp4: Understanding class_declaration
Excerpts from joelr1's message of Thu Oct 04 18:40:58 +0200 2007:
> Would someone kindly explain LEFTA, SELF, ANTIQUOT and QUOTATION below?

The main page about camlp4 extensible grammars/parsers is:
The syntax of grammars is given in:

* LEFTA is left associative (its the default).
* SELF  refers  to  the  current  rule  (class_declaration here), in most common
cases SELF does what you want.

ANTIQUOT  and  QUOTATION  are  token  types  like STRING, INT... The backquote
syntax  mark  the  begining  of an OCaml pattern. So ANTIQUOT is a constructor
(the  token  type [1]). The lexical syntax of an antiquotation is "$name:...$"
or  "$...$",  it  use  in  order to treat quotations [2] such as
  <:class_expr< myclass = object method m = $e$ end >>
where `e' should better be an expression (Ast.expr).


>     class_declaration:
>        [ LEFTA
>          [ c1 = SELF; "and"; c2 = SELF ->
>              <:class_expr< $c1$ and $c2$ >>
>          | `ANTIQUOT (""|"cdcl"|"anti"|"list" as n) s ->
>              <:class_expr< $anti:mk_anti ~c:"class_expr" n s$ >>
>          | `QUOTATION x -> Quotation.expand _loc x  
> Quotation.DynAst.class_expr_tag
>          | ci = class_info_for_class_expr; ce = class_fun_binding ->
>              <:class_expr< $ci$ = $ce$ >>
>        ] ]
> It seems they are variants but that's about as much as I understand.  
> What are the "cdcl", "anti" or "list", for example? Why are they  
> strings?

They  are  antiquotations  names  so  you  can  write  (don't pay attention to
"anti") <:class_expr< $cdcl:x$ and $list:xs$ >>.

> And why is `QUOTATION x expanded into Quotation.expand _loc x  
> Quotation.DynAst.class_expr_tag?

When  you  see  <:class_expr<...>>  you  give  "..." to the quotation expander
manager  that  will call the registered expanding function (the class_expr_tag
is too indicate the requested type).

Best regards,
Nicolas Pouillard aka Ertai