Skip to content

Commit 3542994

Browse files
authored
Support 4.10 ASTs (#25)
Resolves #23.
1 parent c326f16 commit 3542994

10 files changed

+2855
-3
lines changed

ast_convenience_410.ml

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
open Migrate_parsetree.Ast_410
2+
3+
(* This file is part of the ppx_tools package. It is released *)
4+
(* under the terms of the MIT license (see LICENSE file). *)
5+
(* Copyright 2013 Alain Frisch and LexiFi *)
6+
7+
open Parsetree
8+
open Asttypes
9+
open Location
10+
open Ast_helper
11+
12+
13+
module Label = struct
14+
15+
type t = Asttypes.arg_label
16+
17+
type desc = Asttypes.arg_label =
18+
Nolabel
19+
| Labelled of string
20+
| Optional of string
21+
22+
let explode x = x
23+
24+
let nolabel = Nolabel
25+
let labelled x = Labelled x
26+
let optional x = Optional x
27+
28+
end
29+
30+
module Constant = struct
31+
type t = Parsetree.constant =
32+
Pconst_integer of string * char option
33+
| Pconst_char of char
34+
| Pconst_string of string * string option
35+
| Pconst_float of string * char option
36+
37+
let of_constant x = x
38+
39+
let to_constant x = x
40+
41+
end
42+
43+
let may_tuple ?loc tup = function
44+
| [] -> None
45+
| [x] -> Some x
46+
| l -> Some (tup ?loc ?attrs:None l)
47+
48+
let lid ?(loc = !default_loc) s = mkloc (Longident.parse s) loc
49+
let constr ?loc ?attrs s args = Exp.construct ?loc ?attrs (lid ?loc s) (may_tuple ?loc Exp.tuple args)
50+
let nil ?loc ?attrs () = constr ?loc ?attrs "[]" []
51+
let unit ?loc ?attrs () = constr ?loc ?attrs "()" []
52+
let tuple ?loc ?attrs = function
53+
| [] -> unit ?loc ?attrs ()
54+
| [x] -> x
55+
| xs -> Exp.tuple ?loc ?attrs xs
56+
let cons ?loc ?attrs hd tl = constr ?loc ?attrs "::" [hd; tl]
57+
let list ?loc ?attrs l = List.fold_right (cons ?loc ?attrs) l (nil ?loc ?attrs ())
58+
let str ?loc ?attrs s = Exp.constant ?loc ?attrs (Pconst_string (s, None))
59+
let int ?loc ?attrs x = Exp.constant ?loc ?attrs (Pconst_integer (string_of_int x, None))
60+
let int32 ?loc ?attrs x = Exp.constant ?loc ?attrs (Pconst_integer (Int32.to_string x, Some 'l'))
61+
let int64 ?loc ?attrs x = Exp.constant ?loc ?attrs (Pconst_integer (Int64.to_string x, Some 'L'))
62+
let char ?loc ?attrs x = Exp.constant ?loc ?attrs (Pconst_char x)
63+
let float ?loc ?attrs x = Exp.constant ?loc ?attrs (Pconst_float (string_of_float x, None))
64+
let record ?loc ?attrs ?over l =
65+
Exp.record ?loc ?attrs (List.map (fun (s, e) -> (lid ~loc:e.pexp_loc s, e)) l) over
66+
let func ?loc ?attrs l = Exp.function_ ?loc ?attrs (List.map (fun (p, e) -> Exp.case p e) l)
67+
let lam ?loc ?attrs ?(label = Label.nolabel) ?default pat exp = Exp.fun_ ?loc ?attrs label default pat exp
68+
let app ?loc ?attrs f l = if l = [] then f else Exp.apply ?loc ?attrs f (List.map (fun a -> Label.nolabel, a) l)
69+
let evar ?loc ?attrs s = Exp.ident ?loc ?attrs (lid ?loc s)
70+
let let_in ?loc ?attrs ?(recursive = false) b body =
71+
Exp.let_ ?loc ?attrs (if recursive then Recursive else Nonrecursive) b body
72+
73+
let sequence ?loc ?attrs = function
74+
| [] -> unit ?loc ?attrs ()
75+
| hd :: tl -> List.fold_left (fun e1 e2 -> Exp.sequence ?loc ?attrs e1 e2) hd tl
76+
77+
let pvar ?(loc = !default_loc) ?attrs s = Pat.var ~loc ?attrs (mkloc s loc)
78+
let pconstr ?loc ?attrs s args = Pat.construct ?loc ?attrs (lid ?loc s) (may_tuple ?loc Pat.tuple args)
79+
let precord ?loc ?attrs ?(closed = Open) l =
80+
Pat.record ?loc ?attrs (List.map (fun (s, e) -> (lid ~loc:e.ppat_loc s, e)) l) closed
81+
let pnil ?loc ?attrs () = pconstr ?loc ?attrs "[]" []
82+
let pcons ?loc ?attrs hd tl = pconstr ?loc ?attrs "::" [hd; tl]
83+
let punit ?loc ?attrs () = pconstr ?loc ?attrs "()" []
84+
let ptuple ?loc ?attrs = function
85+
| [] -> punit ?loc ?attrs ()
86+
| [x] -> x
87+
| xs -> Pat.tuple ?loc ?attrs xs
88+
let plist ?loc ?attrs l = List.fold_right (pcons ?loc ?attrs) l (pnil ?loc ?attrs ())
89+
90+
let pstr ?loc ?attrs s = Pat.constant ?loc ?attrs (Pconst_string (s, None))
91+
let pint ?loc ?attrs x = Pat.constant ?loc ?attrs (Pconst_integer (string_of_int x, None))
92+
let pchar ?loc ?attrs x = Pat.constant ?loc ?attrs (Pconst_char x)
93+
let pfloat ?loc ?attrs x = Pat.constant ?loc ?attrs (Pconst_float (string_of_float x, None))
94+
95+
let tconstr ?loc ?attrs c l = Typ.constr ?loc ?attrs (lid ?loc c) l
96+
97+
let get_str = function
98+
| {pexp_desc=Pexp_constant (Pconst_string (s, _)); _} -> Some s
99+
| _ -> None
100+
101+
let get_str_with_quotation_delimiter = function
102+
| {pexp_desc=Pexp_constant (Pconst_string (s, d)); _} -> Some (s, d)
103+
| _ -> None
104+
105+
let get_lid = function
106+
| {pexp_desc=Pexp_ident{txt=id;_};_} ->
107+
Some (String.concat "." (Longident.flatten id))
108+
| _ -> None
109+
110+
let find_attr s attrs =
111+
try Some ((List.find (fun {attr_name=x;_} -> x.txt = s) attrs).attr_payload)
112+
with Not_found -> None
113+
114+
let expr_of_payload = function
115+
| PStr [{pstr_desc=Pstr_eval(e, _); _}] -> Some e
116+
| _ -> None
117+
118+
let find_attr_expr s attrs =
119+
match find_attr s attrs with
120+
| Some e -> expr_of_payload e
121+
| None -> None
122+
123+
let has_attr s attrs =
124+
find_attr s attrs <> None

ast_convenience_410.mli

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
open Migrate_parsetree.Ast_410
2+
3+
(* This file is part of the ppx_tools package. It is released *)
4+
(* under the terms of the MIT license (see LICENSE file). *)
5+
(* Copyright 2013 Alain Frisch and LexiFi *)
6+
7+
(** {1 Convenience functions to help build and deconstruct AST fragments.} *)
8+
9+
open Asttypes
10+
open Ast_helper
11+
open Parsetree
12+
13+
(** {2 Compatibility modules} *)
14+
15+
module Label : sig
16+
type t = Asttypes.arg_label
17+
18+
type desc = Asttypes.arg_label =
19+
Nolabel
20+
| Labelled of string
21+
| Optional of string
22+
23+
val explode : t -> desc
24+
25+
val nolabel : t
26+
val labelled : string -> t
27+
val optional : string -> t
28+
29+
end
30+
31+
(** {2 Provides a unified abstraction over differences in Parsetree.constant and Asttypes.constant
32+
* types defined in ocaml 4.03 and 4.02 respectively}*)
33+
module Constant : sig
34+
type t = Parsetree.constant =
35+
Pconst_integer of string * char option
36+
| Pconst_char of char
37+
| Pconst_string of string * string option
38+
| Pconst_float of string * char option
39+
40+
(** Convert Asttypes.constant to Constant.t *)
41+
val of_constant : Parsetree.constant -> t
42+
43+
(** Convert Constant.t to Asttypes.constant *)
44+
val to_constant : t -> Parsetree.constant
45+
46+
end
47+
48+
(** {2 Misc} *)
49+
50+
val lid: ?loc:loc -> string -> lid
51+
52+
(** {2 Expressions} *)
53+
54+
val evar: ?loc:loc -> ?attrs:attrs -> string -> expression
55+
val let_in: ?loc:loc -> ?attrs:attrs -> ?recursive:bool -> value_binding list -> expression -> expression
56+
57+
val constr: ?loc:loc -> ?attrs:attrs -> string -> expression list -> expression
58+
val record: ?loc:loc -> ?attrs:attrs -> ?over:expression -> (string * expression) list -> expression
59+
val tuple: ?loc:loc -> ?attrs:attrs -> expression list -> expression
60+
61+
val nil: ?loc:loc -> ?attrs:attrs -> unit -> expression
62+
val cons: ?loc:loc -> ?attrs:attrs -> expression -> expression -> expression
63+
val list: ?loc:loc -> ?attrs:attrs -> expression list -> expression
64+
65+
val unit: ?loc:loc -> ?attrs:attrs -> unit -> expression
66+
67+
val func: ?loc:loc -> ?attrs:attrs -> (pattern * expression) list -> expression
68+
val lam: ?loc:loc -> ?attrs:attrs -> ?label:Label.t -> ?default:expression -> pattern -> expression -> expression
69+
val app: ?loc:loc -> ?attrs:attrs -> expression -> expression list -> expression
70+
71+
val str: ?loc:loc -> ?attrs:attrs -> string -> expression
72+
val int: ?loc:loc -> ?attrs:attrs -> int -> expression
73+
val int32: ?loc:loc -> ?attrs:attrs -> int32 -> expression
74+
val int64: ?loc:loc -> ?attrs:attrs -> int64 -> expression
75+
val char: ?loc:loc -> ?attrs:attrs -> char -> expression
76+
val float: ?loc:loc -> ?attrs:attrs -> float -> expression
77+
78+
val sequence: ?loc:loc -> ?attrs:attrs -> expression list -> expression
79+
(** Return [()] if the list is empty. Tail rec. *)
80+
81+
(** {2 Patterns} *)
82+
83+
val pvar: ?loc:loc -> ?attrs:attrs -> string -> pattern
84+
val pconstr: ?loc:loc -> ?attrs:attrs -> string -> pattern list -> pattern
85+
val precord: ?loc:loc -> ?attrs:attrs -> ?closed:closed_flag -> (string * pattern) list -> pattern
86+
val ptuple: ?loc:loc -> ?attrs:attrs -> pattern list -> pattern
87+
88+
val pnil: ?loc:loc -> ?attrs:attrs -> unit -> pattern
89+
val pcons: ?loc:loc -> ?attrs:attrs -> pattern -> pattern -> pattern
90+
val plist: ?loc:loc -> ?attrs:attrs -> pattern list -> pattern
91+
92+
val pstr: ?loc:loc -> ?attrs:attrs -> string -> pattern
93+
val pint: ?loc:loc -> ?attrs:attrs -> int -> pattern
94+
val pchar: ?loc:loc -> ?attrs:attrs -> char -> pattern
95+
val pfloat: ?loc:loc -> ?attrs:attrs -> float -> pattern
96+
97+
val punit: ?loc:loc -> ?attrs:attrs -> unit -> pattern
98+
99+
100+
(** {2 Types} *)
101+
102+
val tconstr: ?loc:loc -> ?attrs:attrs -> string -> core_type list -> core_type
103+
104+
(** {2 AST deconstruction} *)
105+
106+
val get_str: expression -> string option
107+
val get_str_with_quotation_delimiter: expression -> (string * string option) option
108+
val get_lid: expression -> string option
109+
110+
val has_attr: string -> attributes -> bool
111+
val find_attr: string -> attributes -> payload option
112+
val find_attr_expr: string -> attributes -> expression option

0 commit comments

Comments
 (0)