模組 Parsetree

module Parsetree: sig .. end

解析產生的抽象語法樹

警告:此模組不穩定,屬於 compiler-libs 的一部分。


type constant = 
| Pconst_integer of string * char option (*

整數常數,例如 33l3L3n

解析器接受 [g-z][G-Z] 後綴。類型檢查器會拒絕除了 'l''L''n' 以外的後綴。

*)
| Pconst_char of char (*

字元,例如 'c'

*)
| Pconst_string of string * Location.t * string option (*

字串常數,例如 "constant"{delim|other constant|delim}

位置跨度是字串的內容,不包含分隔符號。

*)
| Pconst_float of string * char option (*

浮點數常數,例如 3.42e51.4e-4

解析器接受 g-z G-Z 後綴。類型檢查器會拒絕後綴。

*)
type location_stack = Location.t list 

擴充點

type attribute = {
   attr_name : string Asttypes.loc;
   attr_payload : payload;
   attr_loc : Location.t;
}

屬性,例如 [@id ARG][@@id ARG]

在 AST 中傳遞的中繼資料容器。編譯器會忽略未知的屬性。

type extension = string Asttypes.loc * payload 

擴充點,例如 [%id ARGand [%%id ARG]

子語言佔位符 —— 會被類型檢查器拒絕。

type attributes = attribute list 
type payload = 
| PStr of structure
| PSig of signature (*

屬性或擴充點中的 SIG

*)
| PTyp of core_type (*

屬性或擴充點中的 T

*)
| PPat of pattern * expression option (*

屬性或擴充點中的 PP when E

*)

核心語言

類型表達式

type core_type = {
   ptyp_desc : core_type_desc;
   ptyp_loc : Location.t;
   ptyp_loc_stack : location_stack;
   ptyp_attributes : attributes; (*

... [@id1] [@id2]

*)
}
type core_type_desc = 
| Ptyp_any (*

_

*)
| Ptyp_var of string (*

類型變數,例如 'a

*)
| Ptyp_arrow of Asttypes.arg_label * core_type * core_type (*

Ptyp_arrow(lbl, T1T2) 表示

  • lblNolabel 時,T1 -> T2
  • lblLabelled 時,~l:T1 -> T2
  • lblOptional 時,?l:T1 -> T2
*)
| Ptyp_tuple of core_type list (*

Ptyp_tuple([T1 ; ... ; Tn]) 表示乘積類型 T1 * ... * Tn

不變性:n >= 2

*)
| Ptyp_constr of Longident.t Asttypes.loc * core_type list (*

Ptyp_constr(lident, l) 表示

  • l=[] 時,tconstr
  • l=[T] 時,T tconstr
  • l=[T1 ; ... ; Tn] 時,(T1, ..., Tn) tconstr
*)
| Ptyp_object of object_field list * Asttypes.closed_flag (*

Ptyp_object([ l1:T1; ...; ln:Tn ], flag) 表示

  • flagClosed 時,< l1:T1; ...; ln:Tn >
  • flagOpen 時,< l1:T1; ...; ln:Tn; .. >
*)
| Ptyp_class of Longident.t Asttypes.loc * core_type list (*

Ptyp_class(tconstr, l) 表示

  • l=[] 時,#tconstr
  • l=[T] 時,T #tconstr
  • l=[T1 ; ... ; Tn] 時,(T1, ..., Tn#tconstr
*)
| Ptyp_alias of core_type * string Asttypes.loc (*

T as 'a.

*)
| Ptyp_variant of row_field list * Asttypes.closed_flag * Asttypes.label list option (*

Ptyp_variant([`A;`B], flag, labels) 表示

  • flagClosedlabelsNone 時,`A|`B ]
  • flagOpenlabelsNone 時,[> `A|`B ]
  • flagClosedlabelsSome [] 時,[< `A|`B ]
  • flagClosedlabelsSome ["X";"Y"] 時,[< `A|`B > `X `Y ]
*)
| Ptyp_poly of string Asttypes.loc list * core_type (*

'a1 ... 'an. T

只能出現在下列情境中

*)
| Ptyp_package of package_type (*

(module S).

*)
| Ptyp_open of Longident.t Asttypes.loc * core_type (*

M.(T)

*)
| Ptyp_extension of extension (*

[%id].

*)
type package_type = Longident.t Asttypes.loc *
(Longident.t Asttypes.loc * core_type) list

作為型別值 Parsetree.package_type

  • (S, []) 表示 (module S),
  • (S, [(t1, T1) ; ... ; (tn, Tn)]) 表示 (module S with type t1 = T1 and ... and tn = Tn)
type row_field = {
   prf_desc : row_field_desc;
   prf_loc : Location.t;
   prf_attributes : attributes;
}
type row_field_desc = 
| Rtag of Asttypes.label Asttypes.loc * bool * core_type list (*

Rtag(`A, b, l) 表示

  • `Abtruel[] 時,
  • `A of Tbfalsel[T] 時,
  • `A of T1 & .. & Tnbfalsel[T1;...Tn] 時,
  • `A of & T1 & .. & Tnbtruel[T1;...Tn] 時。
  • 如果標籤包含一個常數(空的)建構子,則 bool 欄位為 true。
  • 當多個型別用於同一個建構子時,會出現 &(請參閱手冊中的 4.2)。
*)
| Rinherit of core_type (*

| t ]

*)
type object_field = {
   pof_desc : object_field_desc;
   pof_loc : Location.t;
   pof_attributes : attributes;
}
type object_field_desc = 
| Otag of Asttypes.label Asttypes.loc * core_type
| Oinherit of core_type

模式

type pattern = {
   ppat_desc : pattern_desc;
   ppat_loc : Location.t;
   ppat_loc_stack : location_stack;
   ppat_attributes : attributes; (*

... [@id1] [@id2]

*)
}
type pattern_desc = 
| Ppat_any (*

樣式 _

*)
| Ppat_var of string Asttypes.loc (*

變數樣式,例如 x

*)
| Ppat_alias of pattern * string Asttypes.loc (*

別名樣式,例如 P as 'a

*)
| Ppat_constant of constant (*

樣式,例如 1'a'"true"1.01l1L1n

*)
| Ppat_interval of constant * constant (*

樣式,例如 'a'..'z'

剖析器可以識別其他形式的間隔,但型別檢查器會拒絕它們。

*)
| Ppat_tuple of pattern list (*

樣式 (P1, ..., Pn)

不變量:n >= 2

*)
| Ppat_construct of Longident.t Asttypes.loc
* (string Asttypes.loc list * pattern) option
(*

Ppat_construct(C, args) 表示

  • CargsNone 時,
  • C PargsSome ([], P)
  • C (P1, ..., Pn)argsSome ([], Ppat_tuple [P1; ...; Pn])
  • C (type a b) PargsSome ([a; b], P)
*)
| Ppat_variant of Asttypes.label * pattern option (*

Ppat_variant(`A, pat) 表示

  • `ApatNone 時,
  • `A PpatSome P
*)
| Ppat_record of (Longident.t Asttypes.loc * pattern) list * Asttypes.closed_flag (*

Ppat_record([(l1, P1) ; ... ; (ln, Pn)], flag) 表示

  • { l1=P1; ...; ln=Pn }flagClosed
  • { l1=P1; ...; ln=Pn; _}flagOpen

不變量:n > 0

*)
| Ppat_array of pattern list (*

樣式 [| P1; ...; Pn |]

*)
| Ppat_or of pattern * pattern (*

樣式 P1 | P2

*)
| Ppat_constraint of pattern * core_type (*

樣式 (P : T)

*)
| Ppat_type of Longident.t Asttypes.loc (*

樣式 #tconst

*)
| Ppat_lazy of pattern (*

模式 lazy P

*)
| Ppat_unpack of string option Asttypes.loc (*

Ppat_unpack(s) 代表

  • (module P)sSome "P"
  • (module _)sNone

注意:(module P : S) 被表示為 Ppat_constraint(Ppat_unpack(Some "P"), Ptyp_package S)

*)
| Ppat_exception of pattern (*

模式 exception P

*)
| Ppat_extension of extension (*

模式 [%id]

*)
| Ppat_open of Longident.t Asttypes.loc * pattern (*

模式 M.(P)

*)

值表達式

type expression = {
   pexp_desc : expression_desc;
   pexp_loc : Location.t;
   pexp_loc_stack : location_stack;
   pexp_attributes : attributes; (*

... [@id1] [@id2]

*)
}
type expression_desc = 
| Pexp_ident of Longident.t Asttypes.loc (*

識別符號,例如 xM.x

*)
| Pexp_constant of constant (*

常數表達式,例如 1, 'a', "true", 1.0, 1l, 1L, 1n

*)
| Pexp_let of Asttypes.rec_flag * value_binding list * expression (*

Pexp_let(flag, [(P1,E1) ; ... ; (Pn,En)], E) 代表

  • let P1 = E1 and ... and Pn = EN in EflagNonrecursive 時,
  • let rec P1 = E1 and ... and Pn = EN in EflagRecursive 時。
*)
| Pexp_function of function_param list * type_constraint option
* function_body
(*

Pexp_function ([P1; ...; Pn], C, body) 代表任何包含 funfunction 的結構,包括

  • fun P1 ... Pn -> Ebody = Pfunction_body E
  • fun P1 ... Pn -> function p1 -> e1 | ... | pm -> embody = Pfunction_cases [ p1 -> e1; ...; pm -> em ]

C 代表緊接在箭頭之前的型別約束或強制轉換,例如 fun P1 ... Pn : ty -> ...C = Some (Pconstraint ty) 時。

函式必須有參數。Pexp_function (params, _, body) 必須有非空的 paramsPfunction_cases _ 主體。

*)
| Pexp_apply of expression * (Asttypes.arg_label * expression) list (*

Pexp_apply(E0, [(l1, E1) ; ... ; (ln, En)]) 代表 E0 ~l1:E1 ... ~ln:En

li 可以是 Nolabel (未標記的參數)、Labelled (已標記的參數) 或 Optional (可選參數)。

不變量:n > 0

*)
| Pexp_match of expression * case list (*

match E0 with P1 -> E1 | ... | Pn -> En

*)
| Pexp_try of expression * case list (*

try E0 with P1 -> E1 | ... | Pn -> En

*)
| Pexp_tuple of expression list (*

表達式 (E1, ..., En)

不變量:n >= 2

*)
| Pexp_construct of Longident.t Asttypes.loc * expression option (*

Pexp_construct(C, exp) 代表

  • CexpNone 時,
  • C EexpSome E 時,
  • C (E1, ..., En)expSome (Pexp_tuple[E1;...;En])
*)
| Pexp_variant of Asttypes.label * expression option (*

Pexp_variant(`A, exp) 代表

  • `AexpNone
  • `A EexpSome E
*)
| Pexp_record of (Longident.t Asttypes.loc * expression) list
* expression option
(*

Pexp_record([(l1,P1) ; ... ; (ln,Pn)], exp0) 代表

  • { l1=P1; ...; ln=Pn }exp0None
  • E0 with l1=P1; ...; ln=Pn }exp0Some E0

不變量:n > 0

*)
| Pexp_field of expression * Longident.t Asttypes.loc (*

E.l

*)
| Pexp_setfield of expression * Longident.t Asttypes.loc * expression (*

E1.l <- E2

*)
| Pexp_array of expression list (*

[| E1; ...; En |]

*)
| Pexp_ifthenelse of expression * expression * expression option (*

if E1 then E2 else E3

*)
| Pexp_sequence of expression * expression (*

E1E2

*)
| Pexp_while of expression * expression (*

while E1 do E2 done

*)
| Pexp_for of pattern * expression * expression
* Asttypes.direction_flag * expression
(*

Pexp_for(i, E1E2, direction, E3) 代表

  • for i = E1 to E2 do E3 donedirectionUpto
  • for i = E1 downto E2 do E3 donedirectionDownto
*)
| Pexp_constraint of expression * core_type (*

(E : T)

*)
| Pexp_coerce of expression * core_type option * core_type (*

Pexp_coerce(E, from, T) 代表

  • (E :> T)fromNone 時,
  • (E : T0 :> T)fromSome T0 時。
*)
| Pexp_send of expression * Asttypes.label Asttypes.loc (*

E # m

*)
| Pexp_new of Longident.t Asttypes.loc (*

new M.c

*)
| Pexp_setinstvar of Asttypes.label Asttypes.loc * expression (*

x <- 2

*)
| Pexp_override of (Asttypes.label Asttypes.loc * expression) list (*

{< x1 = E1; ...; xn = En >}

*)
| Pexp_letmodule of string option Asttypes.loc * module_expr * expression (*

let module M = ME in E

*)
| Pexp_letexception of extension_constructor * expression (*

let exception C in E

*)
| Pexp_assert of expression (*

assert E.

注意:類型檢查器會以特殊方式處理 assert false

*)
| Pexp_lazy of expression (*

lazy E

*)
| Pexp_poly of expression * core_type option (*

用於方法主體。

只能用作方法(而非值)中 Cfk_concrete 下的表達式。

*)
| Pexp_object of class_structure (*

object ... end

*)
| Pexp_newtype of string Asttypes.loc * expression (*

fun (type t) -> E

*)
| Pexp_pack of module_expr (*

(module ME).

(module ME : S) 表示為 Pexp_constraint(Pexp_pack MEPtyp_package S)

*)
| Pexp_open of open_declaration * expression (*

- M.(E)

  • let open M in E
  • let openM in E
*)
| Pexp_letop of letop (*

- letP = E0 in E1

  • letP0 = E00 andP1 = E01 in E1
*)
| Pexp_extension of extension (*

[%id]

*)
| Pexp_unreachable (*

.

*)
type case = {
   pc_lhs : pattern;
   pc_guard : expression option;
   pc_rhs : expression;
}

類型為 Parsetree.case 的值表示 (P -> E)(P when E0 -> E)

type letop = {
   let_ : binding_op;
   ands : binding_op list;
   body : expression;
}
type binding_op = {
   pbop_op : string Asttypes.loc;
   pbop_pat : pattern;
   pbop_exp : expression;
   pbop_loc : Location.t;
}
type function_param_desc = 
| Pparam_val of Asttypes.arg_label * expression option * pattern (*

Pparam_val (lbl, exp0, P) 表示參數

  • PlblNolabelexp0None
  • ~l:PlblLabelled lexp0None
  • ?l:PlblOptional lexp0None
  • ?l:(P = E0)lblOptional lexp0Some E0

注意:如果提供了 E0,則只允許 Optional

*)
| Pparam_newtype of string Asttypes.loc (*

Pparam_newtype x 表示參數 (type x)x 攜帶識別符的位置,而封閉 function_param 節點上的 pparam_loc 則是整個 (type x) 的位置。

多個參數 (type a b c) 表示為多個 Pparam_newtype 節點,例如

 [ { pparam_kind = Pparam_newtype a; pparam_loc = loc1 };
           { pparam_kind = Pparam_newtype b; pparam_loc = loc2 };
           { pparam_kind = Pparam_newtype c; pparam_loc = loc3 };
         ]
      

這裡,第一個位置 loc1(type a b c) 的位置,而後續的位置 loc2loc3loc1 相同,只是標記為虛擬位置。abc 上的位置對應於原始碼中的變數 abc

*)
type function_param = {
   pparam_loc : Location.t;
   pparam_desc : function_param_desc;
}
type function_body = 
| Pfunction_body of expression
| Pfunction_cases of case list * Location.t * attributes (*

Pfunction_cases (_, loc, attrs) 中,位置從 function 關鍵字的開始延伸到最後一個 case 的結尾。編譯器只會使用 attrs 中與類型檢查相關的屬性,例如啟用或停用警告。

*)

請參閱關於 Pexp_function 的註解。

type type_constraint = 
| Pconstraint of core_type
| Pcoerce of core_type option * core_type (*

請參閱關於 Pexp_function 的註解。

*)

值描述

type value_description = {
   pval_name : string Asttypes.loc;
   pval_type : core_type;
   pval_prim : string list;
   pval_attributes : attributes; (*

... [@@id1] [@@id2]

*)
   pval_loc : Location.t;
}

類型為 Parsetree.value_description 的值表示

  • val x: T,當 pval_prim[]
  • external x: T = "s1" ... "sn",當 pval_prim["s1";..."sn"]

類型宣告

type type_declaration = {
   ptype_name : string Asttypes.loc;
   ptype_params : (core_type * (Asttypes.variance * Asttypes.injectivity)) list; (*

('a1,...'an) t

*)
   ptype_cstrs : (core_type * core_type * Location.t) list; (*

... constraint T1=T1'  ... constraint Tn=Tn'

*)
   ptype_kind : type_kind;
   ptype_private : Asttypes.private_flag; (*

對於 private ...

*)
   ptype_manifest : core_type option; (*

代表 T

*)
   ptype_attributes : attributes; (*

... [@@id1] [@@id2]

*)
   ptype_loc : Location.t;
}

以下是類型宣告及其表示法,適用於各種 ptype_kindptype_manifest 的值

  • type ttype_kindPtype_abstract,且 manifestNone 時,
  • type t = T0type_kindPtype_abstract,且 manifestSome T0 時,
  • type t = C of T | ...type_kindPtype_variant,且 manifestNone 時,
  • type t = T0 = C of T | ...type_kindPtype_variant,且 manifestSome T0 時,
  • type t = {l: T; ...}type_kindPtype_record,且 manifestNone 時,
  • type t = T0 = {l : T; ...}type_kindPtype_record,且 manifestSome T0 時,
  • type t = ..type_kindPtype_open,且 manifestNone 時。
type type_kind = 
| Ptype_abstract
| Ptype_variant of constructor_declaration list
| Ptype_record of label_declaration list (*

不變性:非空列表

*)
| Ptype_open
type label_declaration = {
   pld_name : string Asttypes.loc;
   pld_mutable : Asttypes.mutable_flag;
   pld_type : core_type;
   pld_loc : Location.t;
   pld_attributes : attributes; (*

l : T [@id1] [@id2]

*)
}

- { ...; l: T; ... }pld_mutableImmutable 時,

注意:T 可以是 Ptyp_poly

type constructor_declaration = {
   pcd_name : string Asttypes.loc;
   pcd_vars : string Asttypes.loc list;
   pcd_args : constructor_arguments;
   pcd_res : core_type option;
   pcd_loc : Location.t;
   pcd_attributes : attributes; (*

C of ... [@id1] [@id2]

*)
}
type constructor_arguments = 
| Pcstr_tuple of core_type list
| Pcstr_record of label_declaration list (*

類型為 Parsetree.constructor_declaration 的值表示構造函數的參數

  • C of T1 * ... * Tnres = Noneargs = Pcstr_tuple [T1; ... ; Tn] 時,
  • CT0res = Some T0args = Pcstr_tuple [] 時,
  • CT1 * ... * Tn -> T0res = Some T0args = Pcstr_tuple [T1; ... ; Tn] 時,
  • C of {...}res = Noneargs = Pcstr_record [...] 時,
  • C: {...} -> T0res = Some T0args = Pcstr_record [...] 時。
*)
type type_extension = {
   ptyext_path : Longident.t Asttypes.loc;
   ptyext_params : (core_type * (Asttypes.variance * Asttypes.injectivity)) list;
   ptyext_constructors : extension_constructor list;
   ptyext_private : Asttypes.private_flag;
   ptyext_loc : Location.t;
   ptyext_attributes : attributes; (*

... @@id1 @@id2

*)
}

為擴充總和類型 t 定義新的擴充建構子 (type t += ...)。

type extension_constructor = {
   pext_name : string Asttypes.loc;
   pext_kind : extension_constructor_kind;
   pext_loc : Location.t;
   pext_attributes : attributes; (*

C of ... [@id1] [@id2]

*)
}
type type_exception = {
   ptyexn_constructor : extension_constructor;
   ptyexn_loc : Location.t;
   ptyexn_attributes : attributes; (*

... [@@id1] [@@id2]

*)
}

定義新的例外 (exception E)。

type extension_constructor_kind = 
| Pext_decl of string Asttypes.loc list * constructor_arguments
* core_type option
(*

Pext_decl(existentials, c_args, t_opt) 描述新的擴充建構子。它可以是

  • C of T1 * ... * Tn
    • existentials[] 時,
    • c_args[T1; ...; Tn] 時,
    • t_optNone
  • CT0
    • existentials[] 時,
    • c_args[] 時,
    • t_optSome T0 時。
  • CT1 * ... * Tn -> T0
    • existentials[] 時,
    • c_args[T1; ...; Tn] 時,
    • t_optSome T0 時。
  • C'a... . T1 * ... * Tn -> T0
    • existentials['a;...] 時,
    • c_args[T1; ... ; Tn] 時,
    • t_optSome T0 時。
*)
| Pext_rebind of Longident.t Asttypes.loc (*

Pext_rebind(D) 使用新名稱 C 重新匯出建構子 D

*)

類別語言

類別語言的類型表達式

type class_type = {
   pcty_desc : class_type_desc;
   pcty_loc : Location.t;
   pcty_attributes : attributes; (*

... [@id1] [@id2]

*)
}
type class_type_desc = 
| Pcty_constr of Longident.t Asttypes.loc * core_type list (*

- c

  • ['a1, ..., 'an] c
*)
| Pcty_signature of class_signature (*

object ... end

*)
| Pcty_arrow of Asttypes.arg_label * core_type * class_type (*

Pcty_arrow(lbl, TCT) 表示

*)
| Pcty_extension of extension (*

%id

*)
| Pcty_open of open_description * class_type (*

let open M in CT

*)
type class_signature = {
   pcsig_self : core_type;
   pcsig_fields : class_type_field list;
}

類型為 class_signature 的值表示

type class_type_field = {
   pctf_desc : class_type_field_desc;
   pctf_loc : Location.t;
   pctf_attributes : attributes; (*

... [@@id1] [@@id2]

*)
}
type class_type_field_desc = 
| Pctf_inherit of class_type (*

inherit CT

*)
| Pctf_val of (Asttypes.label Asttypes.loc * Asttypes.mutable_flag *
Asttypes.virtual_flag * core_type)
(*

val x: T

*)
| Pctf_method of (Asttypes.label Asttypes.loc * Asttypes.private_flag *
Asttypes.virtual_flag * core_type)
(*

method x: T

注意:T 可以是 Ptyp_poly

*)
| Pctf_constraint of (core_type * core_type) (*

constraint T1 = T2

*)
| Pctf_attribute of attribute (*

[@@@id]

*)
| Pctf_extension of extension (*

[%%id]

*)
type 'a class_infos = {
   pci_virt : Asttypes.virtual_flag;
   pci_params : (core_type * (Asttypes.variance * Asttypes.injectivity)) list;
   pci_name : string Asttypes.loc;
   pci_expr : 'a;
   pci_loc : Location.t;
   pci_attributes : attributes; (*

... [@@id1] [@@id2]

*)
}

類型為 class_expr class_infos 的值表示

  • class c = ...
  • class ['a1,...,'an] c = ...
  • class virtual c = ...

它們也用於「類別類型」宣告。

type class_description = class_type class_infos 
type class_type_declaration = class_type class_infos 

類別語言的值表達式

type class_expr = {
   pcl_desc : class_expr_desc;
   pcl_loc : Location.t;
   pcl_attributes : attributes; (*

... [@id1] [@id2]

*)
}
type class_expr_desc = 
| Pcl_constr of Longident.t Asttypes.loc * core_type list (*

c['a1, ..., 'an] c

*)
| Pcl_structure of class_structure (*

object ... end

*)
| Pcl_fun of Asttypes.arg_label * expression option * pattern
* class_expr
(*

Pcl_fun(lbl, exp0, PCE) 表示

  • fun P -> CE,當 lblNolabelexp0None 時,
  • fun ~l:P -> CE,當 lblLabelled lexp0None 時,
  • fun ?l:P -> CE,當 lblOptional lexp0None 時,
  • fun ?l:(P = E0-> CE,當 lblOptional lexp0Some E0 時。
*)
| Pcl_apply of class_expr * (Asttypes.arg_label * expression) list (*

Pcl_apply(CE, [(l1,E1) ; ... ; (ln,En)]) 表示 CE ~l1:E1 ... ~ln:Enli 可以是空的 (未標記的參數),或以 ? (選擇性參數) 開頭。

不變量:n > 0

*)
| Pcl_let of Asttypes.rec_flag * value_binding list * class_expr (*

Pcl_let(rec, [(P1E1); ... ; (PnEn)], CE) 表示

  • let P1 = E1 and ... and Pn = EN in CE,當 recNonrecursive 時,
  • let rec P1 = E1 and ... and Pn = EN in CE,當 recRecursive 時。
*)
| Pcl_constraint of class_expr * class_type (*

(CE : CT)

*)
| Pcl_extension of extension (*

[%id]

*)
| Pcl_open of open_description * class_expr (*

let open M in CE

*)
type class_structure = {
   pcstr_self : pattern;
   pcstr_fields : class_field list;
}

類型為 Parsetree.class_structure 的值表示

type class_field = {
   pcf_desc : class_field_desc;
   pcf_loc : Location.t;
   pcf_attributes : attributes; (*

... [@@id1] [@@id2]

*)
}
type class_field_desc = 
| Pcf_inherit of Asttypes.override_flag * class_expr * string Asttypes.loc option (*

Pcf_inherit(flag, CE, s) 表示

  • inherit CE,當 flagFreshsNone 時,
  • inherit CE as x,當 flagFreshsSome x 時,
  • inheritCE,當 flagOverridesNone 時,
  • flagOverridesSome x 時,表示 inheritCE as x
*)
| Pcf_val of (Asttypes.label Asttypes.loc * Asttypes.mutable_flag *
class_field_kind)
(*

Pcf_val(x,flag, kind) 代表

*)
| Pcf_method of (Asttypes.label Asttypes.loc * Asttypes.private_flag *
class_field_kind)
(*

- method x = E ( E 可以是 Pexp_poly )

  • method virtual x: T ( T 可以是 Ptyp_poly )
*)
| Pcf_constraint of (core_type * core_type) (*

constraint T1 = T2

*)
| Pcf_initializer of expression (*

initializer E

*)
| Pcf_attribute of attribute (*

[@@@id]

*)
| Pcf_extension of extension (*

[%%id]

*)
type class_field_kind = 
| Cfk_virtual of core_type
| Cfk_concrete of Asttypes.override_flag * expression
type class_declaration = class_expr class_infos 

模組語言

模組語言的類型表達式

type module_type = {
   pmty_desc : module_type_desc;
   pmty_loc : Location.t;
   pmty_attributes : attributes; (*

... [@id1] [@id2]

*)
}
type module_type_desc = 
| Pmty_ident of Longident.t Asttypes.loc (*

Pmty_ident(S) 代表 S

*)
| Pmty_signature of signature (*

sig ... end

*)
| Pmty_functor of functor_parameter * module_type (*

functor(X : MT1-> MT2

*)
| Pmty_with of module_type * with_constraint list (*

MT with ...

*)
| Pmty_typeof of module_expr (*

module type of ME

*)
| Pmty_extension of extension (*

[%id]

*)
| Pmty_alias of Longident.t Asttypes.loc (*

(module M)

*)
type functor_parameter = 
| Unit (*

()

*)
| Named of string option Asttypes.loc * module_type (*

Named(name, MT) 代表

  • nameSome X 時,表示 (X : MT)
  • nameNone 時,表示 (_ : MT)
*)
type signature = signature_item list 
type signature_item = {
   psig_desc : signature_item_desc;
   psig_loc : Location.t;
}
type signature_item_desc = 
| Psig_value of value_description (*

- val x: T

  • external x: T = "s1" ... "sn"
*)
| Psig_type of Asttypes.rec_flag * type_declaration list (*

type t1 = ... and ... and tn  = ...

*)
| Psig_typesubst of type_declaration list (*

type t1 := ... and ... and tn := ...

*)
| Psig_typext of type_extension (*

type t1 += ...

*)
| Psig_exception of type_exception (*

exception C of T

*)
| Psig_module of module_declaration (*

module X = Mmodule X : MT

*)
| Psig_modsubst of module_substitution (*

module X := M

*)
| Psig_recmodule of module_declaration list (*

module rec X1 : MT1 and ... and Xn : MTn

*)
| Psig_modtype of module_type_declaration (*

module type S = MTmodule type S

*)
| Psig_modtypesubst of module_type_declaration (*

module type S :=  ...

*)
| Psig_open of open_description (*

open X

*)
| Psig_include of include_description (*

include MT

*)
| Psig_class of class_description list (*

class c1 : ... and ... and cn : ...

*)
| Psig_class_type of class_type_declaration list (*

class type ct1 = ... and ... and ctn = ...

*)
| Psig_attribute of attribute (*

[@@@id]

*)
| Psig_extension of extension * attributes (*

[%%id]

*)
type module_declaration = {
   pmd_name : string option Asttypes.loc;
   pmd_type : module_type;
   pmd_attributes : attributes; (*

... [@@id1] [@@id2]

*)
   pmd_loc : Location.t;
}

類型為 module_declaration 的值代表 S : MT

type module_substitution = {
   pms_name : string Asttypes.loc;
   pms_manifest : Longident.t Asttypes.loc;
   pms_attributes : attributes; (*

... [@@id1] [@@id2]

*)
   pms_loc : Location.t;
}

類型為 module_substitution 的值代表 S := M

type module_type_declaration = {
   pmtd_name : string Asttypes.loc;
   pmtd_type : module_type option;
   pmtd_attributes : attributes; (*

... [@@id1] [@@id2]

*)
   pmtd_loc : Location.t;
}

類型為 module_type_declaration 的值代表

  • S = MT,
  • pmtd_typeNone 時,S 代表抽象模組類型宣告。
type 'a open_infos = {
   popen_expr : 'a;
   popen_override : Asttypes.override_flag;
   popen_loc : Location.t;
   popen_attributes : attributes;
}

類型為 'a open_infos 的值代表

type open_description = Longident.t Asttypes.loc open_infos 

類型為 open_description 的值代表

  • open M.N
  • open M(N).O
type open_declaration = module_expr open_infos 

類型為 open_declaration 的值代表

  • open M.N
  • open M(N).O
  • open struct ... end
type 'a include_infos = {
   pincl_mod : 'a;
   pincl_loc : Location.t;
   pincl_attributes : attributes;
}
type include_description = module_type include_infos 

類型為 include_description 的值代表 include MT

type include_declaration = module_expr include_infos 

類型為 include_declaration 的值代表 include ME

type with_constraint = 
| Pwith_type of Longident.t Asttypes.loc * type_declaration (*

with type X.t = ...

注意:長識別字的最後一個組件必須與 type_declaration 的名稱相符。

*)
| Pwith_module of Longident.t Asttypes.loc * Longident.t Asttypes.loc (*

with module X.Y = Z

*)
| Pwith_modtype of Longident.t Asttypes.loc * module_type (*

with module type X.Y = Z

*)
| Pwith_modtypesubst of Longident.t Asttypes.loc * module_type (*

with module type X.Y := sig end

*)
| Pwith_typesubst of Longident.t Asttypes.loc * type_declaration (*

with type X.t := ..., 格式與 [Pwith_type] 相同

*)
| Pwith_modsubst of Longident.t Asttypes.loc * Longident.t Asttypes.loc (*

with module X.Y := Z

*)

模組語言的值表達式

type module_expr = {
   pmod_desc : module_expr_desc;
   pmod_loc : Location.t;
   pmod_attributes : attributes; (*

... [@id1] [@id2]

*)
}
type module_expr_desc = 
| Pmod_ident of Longident.t Asttypes.loc (*

X

*)
| Pmod_structure of structure (*

struct ... end

*)
| Pmod_functor of functor_parameter * module_expr (*

functor(X : MT1-> ME

*)
| Pmod_apply of module_expr * module_expr (*

ME1(ME2)

*)
| Pmod_apply_unit of module_expr (*

ME1()

*)
| Pmod_constraint of module_expr * module_type (*

(ME : MT)

*)
| Pmod_unpack of expression (*

(val E)

*)
| Pmod_extension of extension (*

[%id]

*)
type structure = structure_item list 
type structure_item = {
   pstr_desc : structure_item_desc;
   pstr_loc : Location.t;
}
type structure_item_desc = 
| Pstr_eval of expression * attributes (*

E

*)
| Pstr_value of Asttypes.rec_flag * value_binding list (*

Pstr_value(rec, [(P1E1 ; ... ; (PnEn))]) 代表

  • recNonrecursive 時,let P1 = E1 and ... and Pn = EN,
  • recRecursive 時,let rec P1 = E1 and ... and Pn = EN 
*)
| Pstr_primitive of value_description (*

- val x: T

  • external x: T = "s1" ... "sn" 
*)
| Pstr_type of Asttypes.rec_flag * type_declaration list (*

type t1 = ... and ... and tn = ...

*)
| Pstr_typext of type_extension (*

type t1 += ...

*)
| Pstr_exception of type_exception (*

- exception C of T

  • exception C = M.X
*)
| Pstr_module of module_binding (*

module X = ME

*)
| Pstr_recmodule of module_binding list (*

module rec X1 = ME1 and ... and Xn = MEn

*)
| Pstr_modtype of module_type_declaration (*

module type S = MT

*)
| Pstr_open of open_declaration (*

open X

*)
| Pstr_class of class_declaration list (*

class c1 = ... and ... and cn = ...

*)
| Pstr_class_type of class_type_declaration list (*

class type ct1 = ... and ... and ctn = ...

*)
| Pstr_include of include_declaration (*

include ME

*)
| Pstr_attribute of attribute (*

[@@@id]

*)
| Pstr_extension of extension * attributes (*

[%%id]

*)
type value_constraint = 
| Pvc_constraint of {
   locally_abstract_univars : string Asttypes.loc list;
   typ : core_type;
}
| Pvc_coercion of {
   ground : core_type option;
   coercion : core_type;
}
(*

- Pvc_constraint { locally_abstract_univars=[]; typ} 是一個對值綁定的簡單類型約束:  let x : typ

  • 更廣泛地說,在 Pvc_constraint { locally_abstract_univars; typ} 中, locally_abstract_univars let x: type a ... . typ  中局部抽象類型變數的列表
  • Pvc_coercion { ground=None; coercion } 表示 let x :> typ
  • Pvc_coercion { ground=Some g; coercion } 表示 let x : g :> typ
*)
type value_binding = {
   pvb_pat : pattern;
   pvb_expr : expression;
   pvb_constraint : value_constraint option;
   pvb_attributes : attributes;
   pvb_loc : Location.t;
}

let pat : type_constraint = exp

type module_binding = {
   pmb_name : string option Asttypes.loc;
   pmb_expr : module_expr;
   pmb_attributes : attributes;
   pmb_loc : Location.t;
}

類型為 module_binding 的值表示 module X = ME

頂層

頂層短語

type toplevel_phrase = 
| Ptop_def of structure
| Ptop_dir of toplevel_directive (*

#use, #load ...

*)
type toplevel_directive = {
   pdir_name : string Asttypes.loc;
   pdir_arg : directive_argument option;
   pdir_loc : Location.t;
}
type directive_argument = {
   pdira_desc : directive_argument_desc;
   pdira_loc : Location.t;
}
type directive_argument_desc = 
| Pdir_string of string
| Pdir_int of string * char option
| Pdir_ident of Longident.t
| Pdir_bool of bool