|
| 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 |
0 commit comments