Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit c079ee1

Browse files
committed
Print iflet expressions
1 parent fdfd959 commit c079ee1

File tree

7 files changed

+376
-132
lines changed

7 files changed

+376
-132
lines changed

src/napkin_core.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ end
8383
let jsxAttr = (Location.mknoloc "JSX", Parsetree.PStr [])
8484
let uncurryAttr = (Location.mknoloc "bs", Parsetree.PStr [])
8585
let ternaryAttr = (Location.mknoloc "ns.ternary", Parsetree.PStr [])
86+
let ifLetAttr = (Location.mknoloc "ns.iflet", Parsetree.PStr [])
8687
let makeBracesAttr loc = (Location.mkloc "ns.braces" loc, Parsetree.PStr [])
8788

8889
type typDefOrExt =
@@ -3070,7 +3071,7 @@ and parseIfLet startPos p =
30703071
Ast_helper.Exp.construct ~loc (Location.mkloc (Longident.Lident "()") loc) None
30713072
in
30723073
let loc = mkLoc startPos p.prevEndPos in
3073-
Ast_helper.Exp.match_ ~loc conditionExpr [
3074+
Ast_helper.Exp.match_ ~attrs:[ifLetAttr] ~loc conditionExpr [
30743075
Ast_helper.Exp.case pattern ?guard thenExpr;
30753076
Ast_helper.Exp.case (Ast_helper.Pat.any ()) elseExpr;
30763077
]

src/napkin_parsetree_viewer.ml

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ open Parsetree
4242
in
4343
process false [] attrs
4444

45-
let collectIfExpressions expr =
46-
let rec collect acc expr = match expr.pexp_desc with
47-
| Pexp_ifthenelse (ifExpr, thenExpr, Some elseExpr) ->
48-
collect ((ifExpr, thenExpr)::acc) elseExpr
49-
| Pexp_ifthenelse (ifExpr, thenExpr, (None as elseExpr)) ->
50-
let ifs = List.rev ((ifExpr, thenExpr)::acc) in
51-
(ifs, elseExpr)
52-
| _ ->
53-
(List.rev acc, Some expr)
54-
in
55-
collect [] expr
56-
5745
let collectListExpressions expr =
5846
let rec collect acc expr = match expr.pexp_desc with
5947
| Pexp_construct ({txt = Longident.Lident "[]"}, _) ->
@@ -165,7 +153,7 @@ open Parsetree
165153
let filterParsingAttrs attrs =
166154
List.filter (fun attr ->
167155
match attr with
168-
| ({Location.txt = ("ns.ternary" | "ns.braces" | "bs" | "ns.namedArgLoc")}, _) -> false
156+
| ({Location.txt = ("ns.ternary" | "ns.braces" | "bs" | "ns.iflet" | "ns.namedArgLoc")}, _) -> false
169157
| _ -> true
170158
) attrs
171159

@@ -269,7 +257,7 @@ open Parsetree
269257

270258
let hasAttributes attrs =
271259
List.exists (fun attr -> match attr with
272-
| ({Location.txt = "bs" | "ns.ternary" | "ns.braces"}, _) -> false
260+
| ({Location.txt = "bs" | "ns.ternary" | "ns.braces" | "ns.iflet"}, _) -> false
273261
| _ -> true
274262
) attrs
275263

@@ -280,6 +268,52 @@ open Parsetree
280268
) -> true
281269
| _ -> false
282270

271+
let rec hasIfLetAttribute attrs =
272+
match attrs with
273+
| [] -> false
274+
| ({Location.txt="ns.iflet"},_)::_ -> true
275+
| _::attrs -> hasIfLetAttribute attrs
276+
277+
let isIfLetExpr expr = match expr with
278+
| {
279+
pexp_attributes = attrs;
280+
pexp_desc = Pexp_match _
281+
} when hasIfLetAttribute attrs -> true
282+
| _ -> false
283+
284+
type ifConditionKind =
285+
| If of Parsetree.expression
286+
| IfLet of Parsetree.pattern * Parsetree.expression * Parsetree.expression option
287+
288+
let collectIfExpressions expr =
289+
let rec collect acc expr = match expr.pexp_desc with
290+
| Pexp_ifthenelse (ifExpr, thenExpr, Some elseExpr) ->
291+
collect ((If(ifExpr), thenExpr)::acc) elseExpr
292+
| Pexp_ifthenelse (ifExpr, thenExpr, (None as elseExpr)) ->
293+
let ifs = List.rev ((If(ifExpr), thenExpr)::acc) in
294+
(ifs, elseExpr)
295+
| Pexp_match (condition, [{
296+
pc_lhs = pattern;
297+
pc_guard = guard;
298+
pc_rhs = thenExpr;
299+
}; {
300+
pc_rhs = {pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, _)}
301+
}]) when isIfLetExpr expr ->
302+
let ifs = List.rev ((IfLet(pattern, condition, guard), thenExpr)::acc) in
303+
(ifs, None)
304+
| Pexp_match (condition, [{
305+
pc_lhs = pattern;
306+
pc_guard = guard;
307+
pc_rhs = thenExpr;
308+
}; {
309+
pc_rhs = elseExpr;
310+
}]) when isIfLetExpr expr ->
311+
collect ((IfLet(pattern, condition, guard), thenExpr)::acc) elseExpr
312+
| _ ->
313+
(List.rev acc, Some expr)
314+
in
315+
collect [] expr
316+
283317
let rec hasTernaryAttribute attrs =
284318
match attrs with
285319
| [] -> false
@@ -318,6 +352,12 @@ open Parsetree
318352
| _ -> true
319353
) attrs
320354

355+
let filterIfLetAttributes attrs =
356+
List.filter (fun attr -> match attr with
357+
|({Location.txt="ns.iflet"},_) -> false
358+
| _ -> true
359+
) attrs
360+
321361
let isJsxExpression expr =
322362
let rec loop attrs =
323363
match attrs with
@@ -371,13 +411,13 @@ open Parsetree
371411

372412
let filterPrinteableAttributes attrs =
373413
List.filter (fun attr -> match attr with
374-
| ({Location.txt="bs" | "ns.ternary"}, _) -> false
414+
| ({Location.txt="bs" | "ns.ternary" | "ns.iflet"}, _) -> false
375415
| _ -> true
376416
) attrs
377417

378418
let partitionPrinteableAttributes attrs =
379419
List.partition (fun attr -> match attr with
380-
| ({Location.txt="bs" | "ns.ternary"}, _) -> false
420+
| ({Location.txt="bs" | "ns.ternary" | "ns.iflet"}, _) -> false
381421
| _ -> true
382422
) attrs
383423

@@ -513,4 +553,4 @@ open Parsetree
513553
{ppat_desc = Ppat_var {txt="__x"}},
514554
{pexp_desc = Pexp_apply _}
515555
) -> true
516-
| _ -> false
556+
| _ -> false

src/napkin_parsetree_viewer.mli

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ val functorType: Parsetree.module_type ->
1313
(* filters @bs out of the provided attributes *)
1414
val processUncurriedAttribute: Parsetree.attributes -> bool * Parsetree.attributes
1515

16+
type ifConditionKind =
17+
| If of Parsetree.expression
18+
| IfLet of Parsetree.pattern * Parsetree.expression * Parsetree.expression option
19+
1620
(* if ... else if ... else ... is represented as nested expressions: if ... else { if ... }
1721
* The purpose of this function is to flatten nested ifs into one sequence.
1822
* Basically compute: ([if, else if, else if, else if], else) *)
1923
val collectIfExpressions:
2024
Parsetree.expression ->
21-
(Parsetree.expression * Parsetree.expression) list * Parsetree.expression option
25+
(ifConditionKind * Parsetree.expression) list * Parsetree.expression option
2226

2327
val collectListExpressions:
2428
Parsetree.expression -> (Parsetree.expression list * Parsetree.expression option)
@@ -62,13 +66,15 @@ val hasAttributes: Parsetree.attributes -> bool
6266

6367
val isArrayAccess: Parsetree.expression -> bool
6468
val isTernaryExpr: Parsetree.expression -> bool
69+
val isIfLetExpr: Parsetree.expression -> bool
6570

6671
val collectTernaryParts: Parsetree.expression -> ((Parsetree.expression * Parsetree.expression) list * Parsetree.expression)
6772

6873
val parametersShouldHug:
6974
funParamKind list -> bool
7075

7176
val filterTernaryAttributes: Parsetree.attributes -> Parsetree.attributes
77+
val filterIfLetAttributes: Parsetree.attributes -> Parsetree.attributes
7278

7379
val isJsxExpression: Parsetree.expression -> bool
7480
val hasJsxAttribute: Parsetree.attributes -> bool
@@ -90,7 +96,7 @@ val modExprApply : Parsetree.module_expr -> (
9096
* Example: given a ptyp_arrow type, what are its arguments and what is the
9197
* returnType? *)
9298

93-
99+
94100
val modExprFunctor : Parsetree.module_expr -> (
95101
(Parsetree.attributes * string Asttypes.loc * Parsetree.module_type option) list *
96102
Parsetree.module_expr
@@ -130,4 +136,4 @@ val classifyJsImport: Parsetree.value_description -> jsImportScope
130136
val rewriteUnderscoreApply: Parsetree.expression -> Parsetree.expression
131137

132138
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
133-
val isUnderscoreApplySugar: Parsetree.expression -> bool
139+
val isUnderscoreApplySugar: Parsetree.expression -> bool

0 commit comments

Comments
 (0)