Skip to content

Commit 3a221e5

Browse files
committed
AST mapping
1 parent 4ac368a commit 3a221e5

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

compiler/ml/ast_mapper_from0.ml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,20 @@ module E = struct
310310
| _ -> true)
311311
attrs
312312

313+
let extract_finally_attribute attrs =
314+
List.find_map
315+
(function
316+
| {Location.txt = "res.finally"}, Pt.PPat (_, Some expr) -> Some expr
317+
| _ -> None)
318+
attrs
319+
320+
let remove_finally_attribute attrs =
321+
List.filter
322+
(function
323+
| {Location.txt = "res.finally"}, _ -> false
324+
| _ -> true)
325+
attrs
326+
313327
let map_jsx_children sub (e : expression) : Pt.jsx_children =
314328
let rec visit (e : expression) : Pt.expression list =
315329
match e.pexp_desc with
@@ -460,8 +474,9 @@ module E = struct
460474
| Pexp_match (e, pel) ->
461475
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
462476
| Pexp_try (e, pel) ->
463-
try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
464-
(Some (ident {txt = Longident.Lident "_"; loc}))
477+
let finally_expr = extract_finally_attribute attrs in
478+
let attrs = remove_finally_attribute attrs in
479+
try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel) finally_expr
465480
| Pexp_tuple el -> tuple ~loc ~attrs (List.map (sub.expr sub) el)
466481
(* <></> *)
467482
| Pexp_construct ({txt = Longident.Lident "[]" | Longident.Lident "::"}, _)

compiler/ml/ast_mapper_to0.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,16 @@ module E = struct
419419
args)
420420
| Pexp_match (e, pel) ->
421421
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
422-
| Pexp_try (e, pel, _) ->
422+
| Pexp_try (e, pel, finally_expr) ->
423+
let attrs =
424+
match finally_expr with
425+
| Some expr ->
426+
let finally_attr =
427+
sub.attribute sub (Location.mknoloc "res.finally", Parsetree.PPat (Ast_helper.Pat.any (), Some expr))
428+
in
429+
finally_attr :: attrs
430+
| None -> attrs
431+
in
423432
try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
424433
| Pexp_tuple el -> tuple ~loc ~attrs (List.map (sub.expr sub) el)
425434
| Pexp_construct (lid, arg) ->
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Test for try..catch (existing syntax)
2+
let tryCatch = try {
3+
Console.log("Trying")
4+
} catch {
5+
| _ => Console.log("Caught")
6+
}
7+
8+
// Test for try..finally (new syntax)
9+
let tryFinally = try {
10+
Console.log("Trying")
11+
} finally {
12+
Console.log("Finally")
13+
}
14+
15+
// Test for try..catch..finally (new syntax)
16+
let tryCatchFinally = try {
17+
Console.log("Trying")
18+
} catch {
19+
| _ => Console.log("Caught")
20+
} finally {
21+
Console.log("Finally")
22+
}
23+
24+
// Test with complex expressions
25+
let complexTry = try {
26+
let x = 1 + 2
27+
let y = x * 3
28+
Some(y)
29+
} catch {
30+
| Not_found => None
31+
| exn => Console.log("Error: " ++ Js.String.make(exn))
32+
}
33+
34+
// Test nested try expressions
35+
let nestedTry = try {
36+
try {
37+
dangerousOperation()
38+
} catch {
39+
| InnerError => Console.log("Inner caught")
40+
}
41+
} catch {
42+
| OuterError => Console.log("Outer caught")
43+
} finally {
44+
Console.log("Outer finally")
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Test for try..catch (existing syntax)
2+
let tryCatch = try {
3+
Console.log("Trying")
4+
} catch {
5+
| _ => Console.log("Caught")
6+
}
7+
8+
// Test for try..finally (new syntax)
9+
let tryFinally = try {
10+
Console.log("Trying")
11+
} finally {
12+
Console.log("Finally")
13+
}
14+
15+
// Test for try..catch..finally (new syntax)
16+
let tryCatchFinally = try {
17+
Console.log("Trying")
18+
} catch {
19+
| _ => Console.log("Caught")
20+
} finally {
21+
Console.log("Finally")
22+
}
23+
24+
// Test with complex expressions
25+
let complexTry = try {
26+
let x = 1 + 2
27+
let y = x * 3
28+
Some(y)
29+
} catch {
30+
| Not_found => None
31+
| exn => Console.log("Error: " ++ Js.String.make(exn))
32+
}
33+
34+
// Test nested try expressions
35+
let nestedTry = try {
36+
try {
37+
dangerousOperation()
38+
} catch {
39+
| InnerError => Console.log("Inner caught")
40+
}
41+
} catch {
42+
| OuterError => Console.log("Outer caught")
43+
} finally {
44+
Console.log("Outer finally")
45+
}

0 commit comments

Comments
 (0)