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.
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
$
.
All quotations generate values using the variable named
``loc
'': if this variable is not defined, the OCaml
compiler complains. It is the source location of the node (a couple of
integers: first char - last char plus one). If there is no associated
source of the node, just define it as (0, 0)
. In semantic
actions of Camlp4 extensible grammars, the variable loc
is
defined (by the system).
This chapter defines only the basic syntax trees with the basic
antiquotations. But you can write any complicated program in them,
providing it is of the good type.
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; ``el
'' is the
list of expressions of the array.
-
<:expr< $e1$ := $e2$ >>
: assignment.
-
<:expr< $chr:c$ >>
: character constant; ``c
'' is a
string representing the character, like would be the result of the
OCaml function Char.escaped
.
-
<:expr< ($e$ :> $t$) >>
: simple coercion.
-
<:expr< ($e$ : $t1$ :> $t2$) >>
: coercion with type constraint.
-
<:expr< $flo:s$ >>
: float constant; ``s
'' is
a string representing the float.
-
<:expr< for $s$ = $e1$ $to:b$ $e2$ do { $list:el$ } >>
:
for loop.
-
<:expr< fun [ $list:pwel$ ] >>
: function; the
parameter ``pwel
'' is a list of (patt * option expr * expr)
,
the optional expr
being the optional when
construction.
-
<:expr< if $e1$ then $e2$ else $e3$ >>
: if
statement.
-
<:expr< $int:s$ >>
: integer constant.
-
<:expr< ~ $i$ : $e$ >>
: label.
-
<:expr< lazy $e$ >>
: lazy.
-
<:expr< let $opt:b$ $list:pel$ in $e$ >>
:
let
statement (b = recursive flag)
-
<: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 starting with an
uppercase letter.
-
<:expr< while $e$ do { $list:el$ } >>
:
while
statement.
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.
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.
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.
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 $opt:b$ $list:pel$ >>
: variables
definition (b = recursive flag).
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< '$i$ >>
: module type variable (abstract).
-
<: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 $opt:b$ $list:lb$ in $ce$ >>
: let
binding (b = recursive flag)
-
<:class_expr< object $opt:s$ $list:csil$ end >>
:
object (s = optional self)
-
<: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 $opt:s$ $list:csil$ end >>
: class
object type (s = optional self)
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 $opt:pr$ $s$ : $t$ >>
: method
type (pr = private flag)
-
<:class_sig_item< method virtual $opt:pr$ $s$ : $t$ >>
: virtual
method type (pr = private flag).
-
<:class_sig_item< value $opt:m$ $s$ = $t$ >>
: value (m
= mutable flag)
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 $opt:pr$ $s$ : $t$ >>
: method
(pr = private flag).
-
<:class_str_item< method virtual $opt:pr$ $s$ : $t$ >>
: virtual
method (pr = private flag)
-
<:class_str_item< value $opt:m$ $s$ = $t$ >>
: value (m
= mutable flag).