Previous Contents
Appendix A Quotations for creating abstract syntax trees
The file ``q_MLast.cmo'' in Camlp4 library directory provides some quotations expanders to create abstract syntax tree nodes using a concrete syntax.

The types generated are defined in the module ``MLast'' provided in the library, but not documented, for the normal usage is to use these quotations.

A.1 General rules

The syntax used in quotations use the revised concrete syntax (chapter 6) with a system of ``antiquotations'' to insert expressions or patterns of the language in the concrete syntax, to make all possible values of the syntax tree nodes. These antiquotations have the format:
antiquotation ::= $ { name : } string $
name ::= identifier
where string is any sequence of characters not holding any $.

A.2 Quotation expr

Syntax tree nodes for language expressions, of type MLast.expr. The basic nodes are:
<:expr< $e1$ . $e2$ >>: access in records and in modules.
<:expr< $anti:e$ >>: location node for Ast antiquotations (temporary node).
<:expr< $e1$ $e2$ >>: application.
<:expr< $e1$ .( $e2$ ) >>: array access.
<:expr< [| $list:el$ |] >>: array.
<:expr< $e1$ := $e2$ >>: assignment.
<:expr< $chr:c$ >>: (escaped) character constant.
<:expr< $flo:s$ >>: float constant.
<:expr< for $s$ = $e1$ $to:b$ $e2$ do { $list:el$ } >>: for loop.
<:expr< fun [ $list:pwel$ ] >>: function.
<:expr< if $e1$ then $e2$ else $e3$ >>: if statement.
<:expr< $int:s$ >>: integer constant.
<:expr< let $rec:b$ $list:pel$ in $e$ >>: let statement.
<:expr< $lid:s$ >>: identifier starting with a lowercase letter.
<:expr< match $e$ with [ $list:pwel$ ] >>: match statement.
<:expr< { $list:eel$ } >>: record.
<:expr< do { $list:el$ } >>: sequence.
<:expr< $e1$ .[ $e2$ ] >>: string access.
<:expr< $str:s$ >>: (escaped) string constant.
<:expr< try $e$ with [ $list:pwel$ ] >>: try statement.
<:expr< ( $list:el$ ) >>: tuple.
<:expr< ( $e$ : $t$ ) >>: type constraint.
<:expr< $uid:s$ >>: identifier starring with an uppercase letter.
<:expr< while $e$ do { $list:el$ } >>: while statement.
A.3 Quotation patt

Syntax tree nodes for language patterns, of type MLast.patt. The basic nodes are:
<:patt< $p1$ . $p2$ >>: access in module.
<:patt< $anti:e$ >>: location node for Ast antiquotations (temporary node).
<:patt< ( $p1$ as $p2$ ) >>: alias.
<:patt< _ >>: wildcard.
<:patt< $p1$ $p2$ >>: application.
<:patt< $chr:c$ >>: (escaped) character constant.
<:patt< $int:s$ >>: integer constant.
<:patt< $lid:i$ >>: identifier starting with a lowercase letter.
<:patt< $p1$ | $p2$ >>: ``or'' pattern.
<:patt< $p1$ .. $p2$ >>: range.
<:patt< { $ppl$ } >>: record.
<:patt< $str:s$ >>: (escaped) string constant.
<:patt< ( $list:pl$ ) >>: tuple.
<:patt< ( $p$ : $t$ ) >>: type constraint.
<:patt< $uid:s$ >>: identifier starting with an uppercase letter.
A.4 Important remark about lists

In the syntax tree, there is no node directly representing lists. Therefore, if ``el'' is a list of values of type MLast.expr, these codes DO NOT create the node of their list:

     <:expr< $list:el$ >>       (* does not work! *)
     <:expr< [ $list:el$ ] >>   (* does not work either! *)
The only available nodes are: the ``cons'' constructor (``::'') and the ``nil'' constructor (``[]''). To create a a list node, you have therefore to apply them to your list elements, one by one. This can be done by this code:

     List.fold_right (fun x l -> <:expr< [$x$ :: $l$] >>) el <:expr< [] >>
Notice that it is different with arrays. This code:

     <:expr< [| $list:el$ |] >>
actually builds an array whose elements are given in the expression list.

A.5 Quotation ctyp

Syntax tree nodes for language types, of type MLast.ctyp. The basic nodes are:
<:ctyp< $t1$ . $t2$ >>: access in module.
<:ctyp< $t1$ as $t2$ >>: type alias.
<:ctyp< _ >>: wildcard.
<:ctyp< $t1$ $t2$ >>: application.
<:ctyp< $t1$ -> $t2$ >>: arrow.
<:ctyp< # $list:sl$ >>: class type.
<:ctyp< ~ $s$ : $t$ >>: label type.
<:ctyp< $lid:s$ >>: identifier starting with a lowercase letter.
<:ctyp< $t1$ == $t2$ >>: type manifest.
<:ctyp< < $list:fl$ > >>: object type.
<:ctyp< ? $s$ : $t$ >>: optional label type.
<:ctyp< ! $list:sl$ : $t$ >>: polymorphic type.
<:ctyp< '$s$ >>: type variable.
<:ctyp< { $list:sbtl$ } >>: record definition.
<:ctyp< [ $list:stll$ ] >>: concrete type definition.
<:ctyp< ( $list:tl$ ) >>: tuple.
<:ctyp< $uid:s$ >>: identifier starting with an uppercase letter.
<:ctyp< [| $list:rfl$ |] >>: variant type definition.
A.6 Quotation sig_item

Syntax tree nodes for language signature items, of type MLast.sig_item. The basic nodes are:
<:sig_item< declare $list:sil$ end >>: declare.
<:sig_item< exception $s$ of $list:tl$ >>: exception declaration.
<:sig_item< external $s$ : $t$ = $list:sl$ >>: declaration of external.
<:sig_item< module $s$ : $mt$ >>: module declaration.
<:sig_item< module type $s$ = $mt$ >>: module type declaration.
<:sig_item< open $sl$ >>: open.
<:sig_item< type $list:sslt$ >>: type declaration.
<:sig_item< value $s$ : $t$ >>: variable declaration.
A.7 Quotation str_item

Syntax tree nodes for language structure items, of type MLast.str_item. The basic nodes are:
<:str_item< declare $list:stl$ end >>: declare.
<:str_item< exception $s$ of $list:tl$ >>: exception declaration.
<:str_item< $exp:e$ >>: toplevel expression.
<:str_item< external $s$ : $t$ = $list:sl$ >>: declaration of external.
<:str_item< module $s$ = $me$ >>: module declaration.
<:str_item< module type $s$ = $mt$ >>: module type declaration.
<:str_item< open $sl$ >>: open.
<:str_item< type $list:sslt$ >>: type declaration.
<:str_item< value $rec:b$ $list:pel$ >>: variables definition.
A.8 Quotation module_type

Syntax tree nodes for language module types, of type MLast.module_type. The basic nodes are:
<:module_type< $mt1$ . $mt2$ >>: access in module.
<:module_type< $mt1$ $mt2$ >>: application.
<:module_type< functor ( $s$ : $mt1$ ) -> $mt2$ >>: functor.
<:module_type< $lid:i$ >>: identifier starting with a lowercase letter.
<:module_type< sig $list:sil$ end >>: signature.
<:module_type< $uid:i$ >>: identifier starting with an uppercase letter.
<:module_type< $mt$ with $list:wcl$ >>: module type with constraint.
A.9 Quotation module_expr

Syntax tree nodes for language module expressions, of type MLast.module_expr. The basic nodes are:
<:module_expr< $me1$ . $me2$ >>: access in module.
<:module_expr< $me1$ $me2$ >>: application.
<:module_expr< functor ( $s$ : $mt$ ) -> $me$ >>: functor.
<:module_expr< struct $list:stl$ end >>: structure.
<:module_expr< ( $me$ : $mt$ ) ] >>: module type constraint.
<:module_expr< $uid:i$ >>: identifier starting with an uppercase letter.
A.10 Quotation class_expr

Syntax tree nodes for language classes, of type MLast.class_expr. The basic nodes are:
<:class_expr< $ce$ $e$ >>: application of a class to an expression.
<:class_expr< $list:sl$ [ $list:tl$ ] >>: class constructor with its parameters (sl is a string list representing its access list, and tl a type list). If no parameter, the [ ] part can be omitted.
<:class_expr< fun $p$ -> $ce$ >>: class function
<:class_expr< let $rec:b$ $list:lb$ in $ce$ >>: let binding
<:class_expr< object $p$ $list:csil$ end >>: object
<:class_expr< ( $ce$ : $ct$ ) >>: type constraint
A.11 Quotation class_type

Syntax tree nodes for language classes types, of type MLast.class_type. The basic nodes are:
<:class_type< $list:sl$ [ $list:tl$ ] >>: class constructor with its parameters (sl is a string list representing its access list, and tl a type list). If no parameter, the [ ] part can be omitted.
<:class_type< [ $t$ ] -> $ct$ >>: arrow class type
<:class_type< object $s$ $list:csil$ end >>: class object type
A.12 Quotation class_sig_item

Syntax tree nodes for language classes signatures (object types) items, of type MLast.class_sig_item. The basic nodes are:

<:class_sig_item< type $t1$ = $t2$ >>: constraint.
<:class_sig_item< declare $list:csil$ end >>: declare.
<:class_sig_item< inherit $ct$ >>: inheritance.
<:class_sig_item< method $s$ : $t$ >>: method type.
<:class_sig_item< method private $s$ : $t$ >>: private method type.
<:class_sig_item< value $mut:m$ $s$ = $t$ >>: value.
<:class_sig_item< method virtual $s$ : $t$ >>: virtual method type.
<:class_sig_item< method virtual private $s$ : $t$ >>: virtual private method type.
A.13 Quotation class_str_item

Syntax tree nodes for language classes structures (objects) item, of type MLast.class_str_item. The basic nodes are:

<:class_str_item< declare $list:csil$ end >>: declare.
<:class_str_item< type $t1$ = $t2$ >>: constraint.
<:class_str_item< inherit $ct$ >>: inheritance.
<:class_str_item< initializer $e$ >>: initialization.
<:class_str_item< method $s$ : $t$ >>: method.
<:class_str_item< method private $s$ : $t$ >>: private method.
<:class_str_item< method virtual $s$ : $t$ >>: virtual method.
<:class_str_item< method virtual private $s$ : $t$ >>: virtual private method.
<:class_str_item< value $mut:m$ $s$ = $t$ >>: value.

Previous Contents