|
| 1 | +(** Code transformations using the parser/printer and ast operations *) |
| 2 | + |
| 3 | +let posInLoc ~pos ~loc = |
| 4 | + Utils.tupleOfLexing loc.Location.loc_start <= pos |
| 5 | + && pos < Utils.tupleOfLexing loc.loc_end |
| 6 | + |
| 7 | +module IfThenElse = struct |
| 8 | + (* Convert if-then-else to switch *) |
| 9 | + |
| 10 | + let rec listToPat ~itemToPat = function |
| 11 | + | [] -> Some [] |
| 12 | + | x :: xList -> ( |
| 13 | + match (itemToPat x, listToPat ~itemToPat xList) with |
| 14 | + | Some p, Some pList -> Some (p :: pList) |
| 15 | + | _ -> None) |
| 16 | + |
| 17 | + let rec expToPat (exp : Parsetree.expression) = |
| 18 | + let mkPat ppat_desc = |
| 19 | + Ast_helper.Pat.mk ~loc:exp.pexp_loc ~attrs:exp.pexp_attributes ppat_desc |
| 20 | + in |
| 21 | + match exp.pexp_desc with |
| 22 | + | Pexp_construct (lid, None) -> Some (mkPat (Ppat_construct (lid, None))) |
| 23 | + | Pexp_construct (lid, Some e1) -> ( |
| 24 | + match expToPat e1 with |
| 25 | + | None -> None |
| 26 | + | Some p1 -> Some (mkPat (Ppat_construct (lid, Some p1)))) |
| 27 | + | Pexp_variant (label, None) -> Some (mkPat (Ppat_variant (label, None))) |
| 28 | + | Pexp_variant (label, Some e1) -> ( |
| 29 | + match expToPat e1 with |
| 30 | + | None -> None |
| 31 | + | Some p1 -> Some (mkPat (Ppat_variant (label, Some p1)))) |
| 32 | + | Pexp_constant c -> Some (mkPat (Ppat_constant c)) |
| 33 | + | Pexp_tuple eList -> ( |
| 34 | + match listToPat ~itemToPat:expToPat eList with |
| 35 | + | None -> None |
| 36 | + | Some patList -> Some (mkPat (Ppat_tuple patList))) |
| 37 | + | Pexp_record (items, None) -> ( |
| 38 | + let itemToPat (x, e) = |
| 39 | + match expToPat e with None -> None | Some p -> Some (x, p) |
| 40 | + in |
| 41 | + match listToPat ~itemToPat items with |
| 42 | + | None -> None |
| 43 | + | Some patItems -> Some (mkPat (Ppat_record (patItems, Closed)))) |
| 44 | + | Pexp_record (_, Some _) -> None |
| 45 | + | _ -> None |
| 46 | + |
| 47 | + let mkMapper ~pos ~changed = |
| 48 | + let expr (mapper : Ast_mapper.mapper) (e : Parsetree.expression) = |
| 49 | + let newExp = |
| 50 | + match e.pexp_desc with |
| 51 | + | Pexp_ifthenelse |
| 52 | + ( { |
| 53 | + pexp_desc = |
| 54 | + Pexp_apply |
| 55 | + ( { |
| 56 | + pexp_desc = |
| 57 | + Pexp_ident {txt = Lident (("=" | "<>") as op)}; |
| 58 | + }, |
| 59 | + [(Nolabel, arg1); (Nolabel, arg2)] ); |
| 60 | + }, |
| 61 | + e1, |
| 62 | + Some e2 ) |
| 63 | + when posInLoc ~pos ~loc:e.pexp_loc -> ( |
| 64 | + let e1, e2 = if op = "=" then (e1, e2) else (e2, e1) in |
| 65 | + let mkMatch ~arg ~pat = |
| 66 | + let cases = |
| 67 | + [ |
| 68 | + Ast_helper.Exp.case pat e1; |
| 69 | + Ast_helper.Exp.case (Ast_helper.Pat.any ()) e2; |
| 70 | + ] |
| 71 | + in |
| 72 | + Ast_helper.Exp.match_ ~loc:e.pexp_loc ~attrs:e.pexp_attributes arg |
| 73 | + cases |
| 74 | + in |
| 75 | + |
| 76 | + match expToPat arg2 with |
| 77 | + | None -> ( |
| 78 | + match expToPat arg1 with |
| 79 | + | None -> None |
| 80 | + | Some pat1 -> |
| 81 | + let newExp = mkMatch ~arg:arg2 ~pat:pat1 in |
| 82 | + Some newExp) |
| 83 | + | Some pat2 -> |
| 84 | + let newExp = mkMatch ~arg:arg1 ~pat:pat2 in |
| 85 | + Some newExp) |
| 86 | + | _ -> None |
| 87 | + in |
| 88 | + match newExp with |
| 89 | + | Some newExp -> |
| 90 | + changed := true; |
| 91 | + newExp |
| 92 | + | None -> Ast_mapper.default_mapper.expr mapper e |
| 93 | + in |
| 94 | + |
| 95 | + {Ast_mapper.default_mapper with expr} |
| 96 | + |
| 97 | + let xform ~pos structure = |
| 98 | + let changed = ref false in |
| 99 | + let mapper = mkMapper ~pos ~changed in |
| 100 | + let newStructure = mapper.structure mapper structure in |
| 101 | + if !changed then Some newStructure else None |
| 102 | +end |
| 103 | + |
| 104 | +let command ~path ~pos = |
| 105 | + if Filename.check_suffix path ".res" then |
| 106 | + let parser = |
| 107 | + Res_driver.parsingEngine.parseImplementation ~forPrinter:false |
| 108 | + in |
| 109 | + let {Res_driver.parsetree = structure; comments} = parser ~filename:path in |
| 110 | + let printer = |
| 111 | + Res_printer.printImplementation ~width:!Res_cli.ResClflags.width ~comments |
| 112 | + in |
| 113 | + match IfThenElse.xform ~pos structure with |
| 114 | + | None -> () |
| 115 | + | Some newStructure -> |
| 116 | + let formatted = printer newStructure in |
| 117 | + Printf.printf "Hit IfThenElse. Formatted:\n%s" formatted |
0 commit comments