Attention une mise à jour du serveur va être effectuée le lundi 17 mai entre 13h et 13h30. Cette mise à jour va générer une interruption du service de quelques minutes.

Commit f46f4add authored by POTTIER Francois's avatar POTTIER Francois

Added Mezzo's grammar to bench/good.

parent bb1fbaf1
Warning: you are using the standard library and/or the %inline keyword. We
recommend switching on --infer in order to avoid obscure type error messages.
(*****************************************************************************)
(* Mezzo, a programming language based on permissions *)
(* Copyright (C) 2011, 2012 Jonathan Protzenko and François Pottier *)
(* *)
(* This program is free software: you can redistribute it and/or modify *)
(* it under the terms of the GNU General Public License as published by *)
(* the Free Software Foundation, either version 3 of the License, or *)
(* (at your option) any later version. *)
(* *)
(* This program is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
(* GNU General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU General Public License *)
(* along with this program. If not, see <http://www.gnu.org/licenses/>. *)
(* *)
(*****************************************************************************)
(** The grammar of Mezzo. *)
(* ---------------------------------------------------------------------------- *)
(* Syntactic categories of names. *)
(* Term variables, type variables, type constructors, and fields are not
syntactically distinguished. Placing term variables, type variables, and
type constructors within a single syntactic category is natural because
they share certain mechanisms (e.g. types and terms can be abstracted over
them). They will be distinguished using sorts. Placing term variables and
fields within a single syntactic category is required because we wish to
allow puns. *)
%token<string> LIDENT
(* As in ocaml, we set up a separate namespace for data constructors. This allows
distinguishing variables and data constructors in a pattern. (Another solution
would be to require data constructors to be explicitly followed with braces.) *)
%token<string> UIDENT
(* ---------------------------------------------------------------------------- *)
(* Other tokens. *)
%token OPEN BUILTIN
%token VALUE TYPE PERM
%token UNKNOWN DYNAMIC EXCLUSIVE MUTABLE
%token DATA ALIAS BAR UNDERSCORE
%token LBRACKET RBRACKET LBRACE RBRACE LPAREN RPAREN
%token COMMA COLON COLONCOLON SEMI AT AS
%token ARROW LARROW DBLARROW TAGOF FUN
%token EMPTY ASSERT EXPLAIN FAIL
%token CONSUMES DUPLICABLE FACT ABSTRACT
%token FLEX PACK WITNESS
%token VAL LET REC AND IN DOT WITH BEGIN END MATCH FOR ABOVE BELOW DOWNTO
%token IF THEN ELSE PRESERVING WHILE DO
%token TAKE FROM GIVE TO ADOPTS TAKING
%token<int> INT
%token<string> OPPREFIX OPINFIX0a OPINFIX0b OPINFIX0c OPINFIX0d OPINFIX1 OPINFIX2 OPINFIX3 OPINFIX4
%token<string> EQUAL STAR PLUS MINUS COLONEQUAL (* special cases of operators *)
%token EOF
%nonassoc THEN
%nonassoc ELSE
%nonassoc ADOPTS
%nonassoc COLONEQUAL
%left OPINFIX0a
%left OPINFIX0b
%left OPINFIX0c EQUAL (* EQUAL is also a OPINFIX0c *)
%left OPINFIX0d
%right OPINFIX1
%left OPINFIX2 PLUS MINUS (* MINUS is also an OPINFIX2 *)
%left OPINFIX3 STAR (* STAR is also an OPINFIX3 *)
%right OPINFIX4
(* ---------------------------------------------------------------------------- *)
(* Miscellaneous directives. *)
%start <SurfaceSyntax.implementation> implementation
%start <SurfaceSyntax.interface> interface
%start <(ClFlags.flag * (int * int)) list> warn_error_list
%{
open Kind
open SurfaceSyntax
open ParserUtils
%}
%%
(* ---------------------------------------------------------------------------- *)
(* Namespaces. *)
(* We work with several namespaces, each of which is obtained by applying
the functor [Identifier.Make] and defines an abstract type [name]. This
should help us avoid confusions between namespaces. *)
(* At the moment, there are three namespaces: variables, data constructors,
and modules. *)
%inline infix_operator:
| o = OPINFIX0a
| o = OPINFIX0b
| o = OPINFIX0c
| o = OPINFIX0d
| o = OPINFIX1
| o = OPINFIX2
| o = OPINFIX3
| o = OPINFIX4
| o = EQUAL
| o = STAR
| o = MINUS
| o = PLUS
| o = COLONEQUAL
{ o }
variable:
(* A identifier that begins with a lowercase letter is a variable. *)
| x = LIDENT
(* As per the OCaml convention, a parenthesized operator is a variable. *)
(* TEMPORARY maybe this could be recognized by the lexer, saving about 30 states in the LR automaton? *)
| LPAREN x = OPPREFIX RPAREN
| LPAREN x = infix_operator RPAREN
{ Variable.register x }
%inline datacon:
datacon = UIDENT
{ Datacon.register datacon }
%inline module_name:
(* A module name must begin with a lowercase letter. *)
name = LIDENT
{ Module.register name }
(* ---------------------------------------------------------------------------- *)
(* A variable or data constructor can be qualified with a module name. *)
maybe_qualified(X):
x = X
{ Unqualified x }
| m = module_name COLONCOLON x = X
{ Qualified (m, x) }
%inline maybe_qualified_type_variable:
x = maybe_qualified(variable)
{ TyVar x }
%inline datacon_reference:
d = maybe_qualified(datacon)
{ mk_datacon_reference d }
%inline maybe_qualified_variable:
x = maybe_qualified(variable)
{ EVar x }
(* ---------------------------------------------------------------------------- *)
(* In a right-flexible list, the last delimiter is optional, i.e., [delim]
can be viewed as a terminator or a separator, as desired. *)
(* There are several ways of expressing this. One could say it is either a
separated list or a terminated list; this works if one uses right
recursive lists. Or, one could say that it is separated list followed
with an optional delimiter; this works if one uses a left-recursive
list. The following formulation is direct and seems most natural. It
should lead to the smallest possible automaton. *)
right_flexible_list(delim, X):
| (* nothing *)
{ [] }
| x = X
{ [x] }
| x = X delim xs = right_flexible_list(delim, X)
{ x :: xs }
(* In a left-flexible list, the first delimiter is optional, i.e., [delim]
can be viewed as an opening or as a separator, as desired. *)
(* Again, there are several ways of expressing this, and again, I suppose
the following formulation is simplest. It is the mirror image of the
above definition, so it is naturally left-recursive, this time. *)
reverse_left_flexible_list(delim, X):
| (* nothing *)
{ [] }
| x = X
{ [x] }
| xs = reverse_left_flexible_list(delim, X) delim x = X
{ x :: xs }
%inline left_flexible_list(delim, X):
xs = reverse_left_flexible_list(delim, X)
{ List.rev xs }
(* A separated list of at least two elements. *)
%inline separated_list_of_at_least_two(sep, X):
| x1 = X sep x2 = separated_nonempty_list(sep, X)
{ x1 :: x2 }
(* ---------------------------------------------------------------------------- *)
(* Syntax for type/type applications. *)
(* Applications of types to types are based on juxtaposition, just like
applications of terms to terms. *)
(* In the abstract syntax, type/type applications are n-ary. In the
grammar of types, though, they must be binary, in order to avoid
ambiguity. *)
%inline type_type_application(X, Y):
ty1 = X ty2 = Y (* juxtaposition *)
{ mktyapp ty1 ty2 }
%inline iterated_type_type_application(X, Y):
x = X ys = Y* (* iterated juxtaposition *)
{ x, ys }
(* ---------------------------------------------------------------------------- *)
(* Syntax for type abstraction and universal quantification. *)
type_parameters:
| LBRACKET bs = right_flexible_list(COMMA, type_binding) RBRACKET
{ bs }
(* Syntax for existential quantification. *)
existential_quantifiers:
| LBRACE bs = right_flexible_list(COMMA, type_binding) RBRACE
{ bs }
(* ---------------------------------------------------------------------------- *)
(* Syntax for binding type variables. *)
(* Because the syntax of type/type applications is juxtaposition, the
syntax of type variable bindings must be atomic (well-delimited). *)
atomic_type_binding:
| x = variable (* TYPE is the default kind *)
{ x, KType, ($startpos(x), $endpos) }
| LPAREN b = type_binding RPAREN
{ b }
variance:
| PLUS
{ Covariant }
| MINUS
{ Contravariant }
|
{ Invariant }
atomic_type_binding_with_variance:
| v = variance b = atomic_type_binding
{ v, b }
type_binding:
| b = atomic_type_binding
{ b }
| x = variable COLON kind = kind
{ x, kind, ($startpos(x), $endpos) }
(* ---------------------------------------------------------------------------- *)
(* Kinds. *)
atomic_kind:
| LPAREN kind = kind RPAREN
{ kind }
| VALUE
{ KValue }
| TYPE
{ KType }
| PERM
{ KPerm }
%inline kind:
| kind = atomic_kind
{ kind }
(* ---------------------------------------------------------------------------- *)
(* Types and permissions. *)
(* Because types and permissions are distinguished via the kind system, they
are not (and must not be) distinguished in the syntax. Thus, the
productions that concern permissions (the empty permission, anchored
permissions, permission conjunction, etc.) appear as part of the syntax of
types. *)
(* The syntax of types is stratified into the following levels:
parenthetic_type
atomic_type
tight_type
normal_type
loose_type
consumes_type
very_loose_type
fat_type
*)
%inline tlocated (X):
| x = X
{ TyLocated (x, ($startpos(x), $endpos)) }
(* A parenthetic type is a type that is delimited by parentheses. This
syntactic category is used as the formal parameter in a function
definition. *)
%inline parenthetic_type:
| ty = tlocated(raw_parenthetic_type)
{ ty }
raw_parenthetic_type:
(* The empty tuple type. *)
| LPAREN RPAREN
{ TyTuple [] }
(* Parentheses are used as a disambiguation device, as is standard. *)
| LPAREN ty = arbitrary_type RPAREN
{ ty }
(* An atomic type is one that is clearly delimited. This includes the
parenthetic types, plus various kinds of atoms (type variables, etc.) *)
%inline atomic_type:
| ty = tlocated(raw_atomic_type)
{ ty }
raw_atomic_type:
| ty = raw_parenthetic_type
{ ty }
(* The top type. *)
| UNKNOWN
{ TyUnknown }
(* The type [dynamic] represents a permission to test membership in a dynamic region. *)
| DYNAMIC
{ TyDynamic }
(* The top permission. A neutral element for permission conjunction. *)
| EMPTY
{ TyEmpty }
(* The wildcard, which allows a limited form of type inference for functions. *)
| UNDERSCORE
{ TyWildcard }
(* Term variable, type variable, permission variable, abstract type, or concrete type. *)
| x = maybe_qualified_type_variable
{ x }
(* A structural type without an [adopts] clause (which is the usual case)
is an atomic type. *)
| b = data_type_branch
{ mk_concrete ($startpos(b), $endpos) b None }
%inline tight_type:
| ty = tlocated(raw_tight_type)
{ ty }
raw_tight_type:
| ty = raw_atomic_type
{ ty }
(* A singleton type. *)
| EQUAL x = maybe_qualified_type_variable
{ TySingleton x }
(* A type application. *)
| ty = type_type_application(tight_type, atomic_type)
{ ty }
%inline normal_type:
| ty = tlocated(raw_normal_type)
{ ty }
%inline raw_normal_type_no_adopts_rec(X):
| ty = raw_tight_type
{ ty }
(* The syntax of function types is [t -> t], as usual. *)
| ty1 = tight_type ARROW ty2 = X
{ TyArrow (ty1, ty2) }
(* A polymorphic type. *)
| bs = type_parameters ty = X
{ List.fold_right (fun b ty -> TyForall (b, ty)) bs ty }
(* An existential type. *)
| bs = existential_quantifiers ty = X
{ List.fold_right (fun b ty -> TyExists (b, ty)) bs ty }
(* A type that carries a mode constraint (implication). *)
| c = mode_constraint DBLARROW ty = X
{ TyImply (c, ty) }
raw_normal_type_no_adopts:
| x = raw_normal_type_no_adopts_rec(raw_normal_type_no_adopts)
{ x }
raw_normal_type:
| t = raw_normal_type_no_adopts_rec(raw_normal_type)
{ t }
(* A structural type with an [adopts] clause is a considered a normal type.
This allows the type in the [adopts] clause to be itself a normal type. *)
| b = data_type_branch ADOPTS t = raw_normal_type
{ mk_concrete ($startpos(b), $endpos) b (Some t) }
%inline loose_type:
| ty = tlocated(raw_loose_type)
{ ty }
raw_loose_type:
| ty = raw_normal_type
{ ty }
(* In an anchored permission [x@t], the name [x] is free. This
represents an assertion that we have permission to use [x] at
type [t]. *)
| x = maybe_qualified_type_variable AT ty = normal_type
{ TyAnchoredPermission (x, ty) }
(* [x = y] is also an anchored permission; it is sugar for [x@=y]. *)
| x = maybe_qualified_type_variable EQUAL y = maybe_qualified_type_variable
{ TyAnchoredPermission (x, TySingleton y) }
(* In a name introduction form [x:t], the name [x] is bound. The scope
of [x] is defined by somewhat subtle rules that need not concern us
here. These rules are spelled out later on when we desugar the surface-level
types into a lower-level representation. *)
| x = variable COLON ty = normal_type
{ TyNameIntro (x, ty) }
(* We also allow a name introduction form where the name is [_]. *)
| UNDERSCORE COLON ty = normal_type
{ ty }
%inline consumes_type:
| ty = tlocated(raw_consumes_type)
{ ty }
raw_consumes_type:
| ty = raw_loose_type
{ ty }
(* A type can be annotated with the [CONSUMES] keyword. This really
makes sense only in certain contexts, e.g. in the left-hand side of an
arrow, and possibly further down under tuples, stars, etc. The grammar
allows this everywhere. This notation is checked for consistency and
desugared in a separate pass. *)
| CONSUMES ty = loose_type
{ TyConsumes ty }
%inline very_loose_type:
| ty = tlocated(raw_very_loose_type)
{ ty }
(* [COMMA] and [STAR] are at the same level, but cannot be mixed with
each other. *)
raw_very_loose_type:
| ty = raw_consumes_type
{ ty }
(* Permission conjunction is a binary operator. *)
| ty1 = consumes_type STAR ty2 = very_loose_type
{ TyStar (ty1, ty2) }
(* A tuple type of length at least two is written [t1, ..., tn], without
parentheses. A tuple type of length one cannot be written -- there is
no syntax for it. *)
| tcs = separated_list_of_at_least_two(COMMA, consumes_type)
{ TyTuple tcs }
%inline fat_type:
| ty = tlocated(raw_fat_type)
{ ty }
raw_fat_type:
| ty = raw_very_loose_type
{ ty }
(* The conjunction of a type and a permission is written [t | p]. It is
typically used as the domain or codomain of a function type. *)
| ty1 = fat_type BAR ty2 = very_loose_type
{ TyBar (ty1, ty2) }
| BAR ty2 = very_loose_type
{ TyBar (TyTuple [], ty2) }
(* A type that carries a mode constraint (conjunction). *)
| ty = fat_type BAR c = mode_constraint
{ TyAnd (c, ty) }
%inline arbitrary_type:
ty = fat_type
{ ty }
(* ---------------------------------------------------------------------------- *)
(* Mode constraints are used as part of toplevel function definitions. *)
(* We allow just one constraint at a time. Multiple constraints, separated
with commas, could perhaps be allowed, but I am afraid that this looks
too much like a tuple. *)
(* There is no syntax for the bottom mode or the top mode. *)
mode:
| EXCLUSIVE
{ Mode.ModeExclusive }
| DUPLICABLE
{ Mode.ModeDuplicable }
%inline mode_constraint:
| m = mode t = atomic_type
{ m, t }
(* ---------------------------------------------------------------------------- *)
(* Some generic definitions concerning applications of data constructors. *)
(* A data constructor application takes the generic form [D { ... }]. As a
special case, a pair of empty braces can be omitted. *)
%inline curly_application(X, Y):
| x = X
{ x, [] }
| x = X LBRACE y = Y RBRACE
{ x, y }
generic_datacon_application(Y):
| x = curly_application (datacon_reference, Y)
{ x }
generic_bare_datacon_application(Y):
| x = curly_application (datacon, Y)
{ x }
(* It is often the case that the contents of the curly braces is a semicolon-
separated (or -terminated) list of things. *)
%inline datacon_application(Y):
| xys = generic_datacon_application(right_flexible_list(SEMI, Y))
{ xys }
(* ---------------------------------------------------------------------------- *)
(* Data type definitions. *)
(* Please note that the distinction between value fields and permission fields
* only exists temporarily (hence, the anonymous variant type); these variants
* disappear as soon as the user of [data_type_branch] calls
* [ParserUtils.mk_concrete]. The reason why we group [`FieldValue]'s and
* [`FieldPermission]'s in a common list is that [curly_brace] hardcodes the
* empty list as a default value. *)
data_field_def:
(* A field definition normally mentions a field name and a field type. Multiple
field names, separated with commas, can be specified: this means that they
share a common type. *)
| fs = separated_nonempty_list(COMMA, variable) COLON ty = normal_type
{ List.map (fun f -> `FieldValue (f, ty)) fs }
(* The double-colon stands for a binding field name whose scope is the entire
* concrete type. *)
| fs = separated_nonempty_list(COMMA, variable) COLONCOLON ty = normal_type
{ List.map (fun f -> `FieldBindingValue (f, ty)) fs }
(* We also allow a field definition to take the form of an equality between
a field name [f] and a term variable [y]. This is understood as sugar for
a definition of the field [f] at the singleton type [=y]. In this case,
only one field name is allowed. This short-hand is useful in the syntax
of structural permissions. *)
| f = variable EQUAL y = maybe_qualified_type_variable
{ [ `FieldValue (f, TySingleton y) ] }
(* Going one step further, we allow a field definition to consist of just
a field name [f]. This is a pun: it means [f = f], or in other words,
[f: =f]. *)
| f = variable
{ [ `FieldValue (f, TySingleton (TyVar (Unqualified f))) ] }
(* Field definitions are semicolon-separated or -terminated. *)
%inline data_fields_def:
fss = right_flexible_list(SEMI, data_field_def)
{ List.flatten fss }
(* A list of field definitions is optionally followed with BAR and a
permission. *)
data_type_def_branch_content:
fs = data_fields_def
{ fs }
| fs = data_fields_def BAR perm = very_loose_type
{ fs @ [ `FieldPermission perm ] }
(* A branch in a data type definition is a constructor application,
where, within the braces, we have the above content. This is also
the syntax of structural permissions. *)
(* The [mutable] keyword may appear either in front of the algebraic
data type definition, in which case it concerns all branches, or
in front of a branch, in which case it concerns this branch only. *)
%inline data_type_flavor:
| (* nothing *)
{ DataTypeFlavor.Immutable }
| MUTABLE
{ DataTypeFlavor.Mutable }
%inline data_type_branch:
dfs = generic_datacon_application(data_type_def_branch_content)
{ dfs }
%inline data_type_def_branch:
flavor = data_type_flavor
t = tlocated(raw_normal_type_no_adopts)
{
flavor, t
}
%inline data_type_def_lhs:
x = variable ys = atomic_type_binding_with_variance*
{ (* A little hack: we don't know the kind yet, so we abstract over it. *)
fun kind ->
(x, kind, ($startpos(x), $endpos(x))),
ys
}
%inline data_type_def_rhs:
bs = left_flexible_list(BAR, data_type_def_branch)
{ bs }
%inline optional_kind_annotation:
| (* nothing *)
{ KType }
| COLON k = kind
{ k }
fact: