Skip to content

Commit 26f7d61

Browse files
committed
Update metaquot to produce Ppx_ast types instead of Parsetree.
Also remove metaquot patterns in favor of ppx_view.
1 parent 8afee50 commit 26f7d61

27 files changed

+489
-425
lines changed

ast/builder.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,13 @@ module Common = struct
609609
module Located = struct
610610
let longident ~loc longident = Astlib.Loc.create ~loc ~txt:longident ()
611611
let lident ~loc x = longident ~loc (Longident.lident x)
612+
613+
let dotted ~loc list =
614+
match list with
615+
| [] -> invalid_arg "Located.dotted: empty list"
616+
| head :: tail ->
617+
longident ~loc
618+
(List.fold_left ~init:(Longident.lident head) tail ~f:Longident.ldot)
612619
end
613620

614621
let echar ~loc x = pexp_constant ~loc (Constant.pconst_char x)
@@ -630,6 +637,8 @@ module Common = struct
630637
let efloat ~loc x =
631638
pexp_constant ~loc (Constant.pconst_float (Float.to_string x) None)
632639

640+
let evar ~loc x = pexp_ident ~loc (Located.lident ~loc x)
641+
633642
let eunit ~loc =
634643
pexp_construct ~loc (Located.lident ~loc "()") None
635644

@@ -657,6 +666,10 @@ module Common = struct
657666
fun_expr
658667
(List.map args ~f:(fun expr -> (Arg_label.nolabel, expr)))
659668

669+
let eabstract ~loc pats body =
670+
List.fold_right pats ~init:body ~f:(fun pat body ->
671+
pexp_fun ~loc Arg_label.nolabel None pat body)
672+
660673
let pchar ~loc x = ppat_constant ~loc (Constant.pconst_char x)
661674

662675
let pstring ~loc x = ppat_constant ~loc (Constant.pconst_string x None)

ast/builder.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,7 @@ module Common : sig
13931393
val longident
13941394
: loc: Astlib.Location.t -> Versions.longident -> Versions.longident Astlib.Loc.t
13951395
val lident : loc: Astlib.Location.t -> string -> Versions.longident Astlib.Loc.t
1396+
val dotted : loc: Astlib.Location.t -> string list -> Versions.longident Astlib.Loc.t
13961397
end
13971398

13981399
val echar : loc: Astlib.Location.t -> char -> Versions.expression
@@ -1402,11 +1403,17 @@ module Common : sig
14021403
val eint64 : loc: Astlib.Location.t -> int64 -> Versions.expression
14031404
val enativeint : loc: Astlib.Location.t -> nativeint -> Versions.expression
14041405
val efloat : loc: Astlib.Location.t -> float -> Versions.expression
1406+
val evar : loc: Astlib.Location.t -> string -> Versions.expression
14051407
val eunit : loc: Astlib.Location.t -> Versions.expression
14061408
val ebool : loc: Astlib.Location.t -> bool -> Versions.expression
14071409
val enil : loc: Astlib.Location.t -> Versions.expression
14081410
val elist : loc: Astlib.Location.t -> Versions.expression list -> Versions.expression
14091411
val etuple : loc: Astlib.Location.t -> Versions.expression list -> Versions.expression
1412+
val eabstract
1413+
: loc:Astlib.Location.t
1414+
-> Versions.pattern list
1415+
-> Versions.expression
1416+
-> Versions.expression
14101417
val eapply :
14111418
loc : Astlib.Location.t ->
14121419
Versions.expression ->

ast/cinaps/dune

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(library
2-
(name ppx_ast_cinaps)
2+
(name ppx_ast_cinaps)
3+
(public_name ppx.ast_cinaps)
34
(libraries astlib stdppx ocaml-compiler-libs.shadow)
45
(flags (:standard -open Ocaml_shadow -safe-string)))

ast/cinaps/gen_traverse.ml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ module Lift = struct
401401
let res_type = Ml.tvar "res"
402402

403403
let extra_methods () =
404+
Ml.declare_method
405+
~virtual_:true
406+
~name:"node"
407+
~signature:"(string * int) option -> 'res -> 'res"
408+
();
404409
Ml.declare_method
405410
~virtual_:true
406411
~name:"record"
@@ -432,7 +437,7 @@ module Lift = struct
432437
let name_and_val var_name = Printf.sprintf "(%S, %s)" var_name var_name in
433438
Ml.list_lit (List.map ~f:name_and_val var_names)
434439

435-
let typ_arg ~value_kind =
440+
let make_node_arg ~value_kind =
436441
match value_kind with
437442
| Abstract -> "None"
438443
| Ast_type { node_name; targs } ->
@@ -445,14 +450,17 @@ module Lift = struct
445450
~f:(fun {var; recursive_call} ->
446451
Printf.sprintf "let %s = %s %s in" var recursive_call var)
447452
in
453+
let node_arg = make_node_arg ~value_kind in
448454
let result =
449455
match kind with
450-
| Kalias -> pattern
451-
| Ktuple -> Printf.sprintf "self#tuple %s" (tuple_arg vars)
456+
| Kalias ->
457+
Printf.sprintf "self#node %s %s" node_arg pattern
458+
| Ktuple ->
459+
Printf.sprintf "self#node %s (self#tuple %s)" node_arg (tuple_arg vars)
452460
| Krecord ->
453-
Printf.sprintf "self#record %s %s" (typ_arg ~value_kind) (record_arg vars)
461+
Printf.sprintf "self#record %s %s" node_arg (record_arg vars)
454462
| Kconstr name ->
455-
Printf.sprintf "self#constr %s %S %s" (typ_arg ~value_kind) name (tuple_arg vars)
463+
Printf.sprintf "self#constr %s %S %s" node_arg name (tuple_arg vars)
456464
in
457465
recurse @ [result]
458466

ast/cinaps/ml.ml

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,10 @@ let is_keyword s =
66
let map_keyword s =
77
if is_keyword s then s ^ "_" else s
88

9-
let is_id_char = function
10-
| 'A' .. 'Z' -> true
11-
| 'a' .. 'z' -> true
12-
| '0' .. '9' -> true
13-
| '_' | '\'' -> true
14-
| _ -> false
15-
16-
let to_id_char char =
17-
if is_id_char char
18-
then char
19-
else '_'
20-
21-
let raw_id string = String.map string ~f:to_id_char
22-
23-
let id string = map_keyword (String.lowercase_ascii (raw_id string))
9+
let id string = map_keyword (String.lowercase_ascii string)
2410
let tvar string = "'" ^ id string
25-
let module_name string = String.capitalize_ascii (raw_id string)
26-
let tag string = String.capitalize_ascii (raw_id string)
11+
let module_name string = String.capitalize_ascii string
12+
let tag string = String.capitalize_ascii string
2713

2814
let list_lit elms =
2915
Printf.sprintf "[%s]" (String.concat ~sep:"; " elms)

ast/traverse_builtins.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ class virtual ['res] lift =
253253
method virtual nativeint : (nativeint, 'res) T.lift
254254
method virtual unit : (unit, 'res) T.lift
255255

256+
method virtual node : (string * int) option -> 'res -> 'res
256257
method virtual record : (string * int) option -> (string * 'res) list -> 'res
257258
method virtual constr : (string * int) option -> string -> 'res list -> 'res
258259
method virtual tuple : 'res list -> 'res
@@ -300,6 +301,7 @@ class type ['res] std_lifters =
300301
method bool : (bool , 'res) T.lift
301302
method char : (char , 'res) T.lift
302303
method array : 'a. ('a, 'res) T.lift -> ('a array, 'res) T.lift
304+
method node : (string * int) option -> 'res -> 'res
303305
method record : (string * int) option -> (string * 'res) list -> 'res
304306
method constr : (string * int) option -> string -> 'res list -> 'res
305307
method tuple : 'res list -> 'res

ast/traverse_builtins.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class virtual ['res] lift :
9494
method virtual int64 : (int64, 'res) T.lift
9595
method virtual nativeint : (nativeint, 'res) T.lift
9696
method virtual unit : (unit, 'res) T.lift
97+
method virtual node : (string * int) option -> 'res -> 'res
9798
method virtual record : (string * int) option -> (string * 'res) list -> 'res
9899
method virtual constr : (string * int) option -> string -> 'res list -> 'res
99100
method virtual tuple : 'res list -> 'res
@@ -112,6 +113,7 @@ class type ['res] std_lifters =
112113
method bool : (bool , 'res) T.lift
113114
method char : (char , 'res) T.lift
114115
method array : 'a. ('a, 'res) T.lift -> ('a array, 'res) T.lift
116+
method node : (string * int) option -> 'res -> 'res
115117
method record : (string * int) option -> (string * 'res) list -> 'res
116118
method constr : (string * int) option -> string -> 'res list -> 'res
117119
method tuple : 'res list -> 'res

ast/versions.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ type label = string
150150
type longident_loc = longident Astlib.Loc.t
151151

152152
module Unstable_for_testing = struct
153-
let version = "unstable-for-testing"
153+
let version = "unstable_for_testing"
154154
let node name data = Node.of_node ~version { name; data }
155155

156156
module Directive_argument = struct
@@ -4675,7 +4675,7 @@ module Unstable_for_testing = struct
46754675
end
46764676

46774677
module V4_07 = struct
4678-
let version = "v4.07"
4678+
let version = "v4_07"
46794679
let node name data = Node.of_node ~version { name; data }
46804680

46814681
module Longident = struct

0 commit comments

Comments
 (0)