Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indented most of the source code #409

Merged
merged 12 commits into from
Jun 15, 2017
Merged
262 changes: 131 additions & 131 deletions src/camlp4/pa_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,172 +51,172 @@ let gen_catch mc =
let gen_binding l =
let rec aux n = function
| [] ->
assert false
assert false
| [(_loc, _p, e)] ->
<:binding< $lid:"__pa_lwt_" ^ string_of_int n$ = $e$ >>
<:binding< $lid:"__pa_lwt_" ^ string_of_int n$ = $e$ >>
| (_loc, _p, e) :: l ->
<:binding< $lid:"__pa_lwt_" ^ string_of_int n$ = $e$ and $aux (n + 1) l$ >>
<:binding< $lid:"__pa_lwt_" ^ string_of_int n$ = $e$ and $aux (n + 1) l$ >>
in
aux 0 l

let gen_bind l e =
let rec aux n = function
| [] ->
e
e
| (_loc, p, _e) :: l ->
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_bind (fun exn -> try raise exn with exn -> exn) $lid:"__pa_lwt_" ^ string_of_int n$ (fun $p$ -> $aux (n + 1) l$) >>
else
<:expr< Lwt.bind $lid:"__pa_lwt_" ^ string_of_int n$ (fun $p$ -> $aux (n + 1) l$) >>
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_bind (fun exn -> try raise exn with exn -> exn) $lid:"__pa_lwt_" ^ string_of_int n$ (fun $p$ -> $aux (n + 1) l$) >>
else
<:expr< Lwt.bind $lid:"__pa_lwt_" ^ string_of_int n$ (fun $p$ -> $aux (n + 1) l$) >>
in
aux 0 l

let gen_top_bind _loc l =
let rec aux n vars = function
| [] ->
<:expr< Lwt.return ($tup:Ast.exCom_of_list (List.rev vars)$) >>
<:expr< Lwt.return ($tup:Ast.exCom_of_list (List.rev vars)$) >>
| (_loc, _p, _e) :: l ->
let id = "__pa_lwt_" ^ string_of_int n in
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_bind (fun exn -> try raise exn with exn -> exn) $lid:id$ (fun $lid:id$ -> $aux (n + 1) (<:expr< $lid:id$ >> :: vars) l$) >>
else
<:expr< Lwt.bind $lid:id$ (fun $lid:id$ -> $aux (n + 1) (<:expr< $lid:id$ >> :: vars) l$) >>
let id = "__pa_lwt_" ^ string_of_int n in
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_bind (fun exn -> try raise exn with exn -> exn) $lid:id$ (fun $lid:id$ -> $aux (n + 1) (<:expr< $lid:id$ >> :: vars) l$) >>
else
<:expr< Lwt.bind $lid:id$ (fun $lid:id$ -> $aux (n + 1) (<:expr< $lid:id$ >> :: vars) l$) >>
in
aux 0 [] l

EXTEND Gram
GLOBAL: expr str_item;

cases:
[ [ "with"; c = match_case -> Some(gen_catch c)
| -> None ] ];
cases:
[ [ "with"; c = match_case -> Some(gen_catch c)
| -> None ] ];

finally:
[ [ "finally"; f = sequence -> Some f
| -> None ] ];
finally:
[ [ "finally"; f = sequence -> Some f
| -> None ] ];

letb_binding:
[ [ b1 = SELF; "and"; b2 = SELF -> b1 @ b2
| p = patt; "="; e = expr -> [(_loc, p, e)]
] ];
letb_binding:
[ [ b1 = SELF; "and"; b2 = SELF -> b1 @ b2
| p = patt; "="; e = expr -> [(_loc, p, e)]
] ];

for_scheme:
[ [ "="; s = sequence; "to"; e = sequence ->
`CountTo(s, e)
| "="; s = sequence; "downto"; e = sequence ->
`CountDownTo(s, e)
| "in"; e = sequence ->
`IterOver(e) ] ];
for_scheme:
[ [ "="; s = sequence; "to"; e = sequence ->
`CountTo(s, e)
| "="; s = sequence; "downto"; e = sequence ->
`CountDownTo(s, e)
| "in"; e = sequence ->
`IterOver(e) ] ];

expr: LEVEL "top"
expr: LEVEL "top"
[ [ "try_lwt"; e = expr LEVEL ";"; c = cases; f = finally ->
begin match c, f with
| None, None ->
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_catch (fun exn -> try raise exn with exn -> exn) (fun () -> $e$) Lwt.fail >>
else
<:expr< Lwt.catch (fun () -> $e$) Lwt.fail >>
| Some c, None ->
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_catch (fun exn -> try raise exn with exn -> exn) (fun () -> $e$) (function $c$) >>
else
<:expr< Lwt.catch (fun () -> $e$) (function $c$) >>
| None, Some f ->
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_finalize (fun exn -> try raise exn with exn -> exn) (fun () -> $e$) (fun () -> (begin $f$ end)) >>
else
<:expr< Lwt.finalize (fun () -> $e$) (fun () -> (begin $f$ end)) >>
| Some c, Some f ->
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_try_bind (fun exn -> try raise exn with exn -> exn) (fun () -> $e$)
(fun __pa_lwt_x -> Lwt.backtrace_bind (fun exn -> try raise exn with exn -> exn) (begin $f$ end) (fun () -> Lwt.return __pa_lwt_x))
(fun __pa_lwt_e -> Lwt.backtrace_bind (fun exn -> try raise exn with exn -> exn) (begin $f$ end) (fun () -> match __pa_lwt_e with $c$))
>>
else
<:expr< Lwt.try_bind (fun () -> $e$)
(fun __pa_lwt_x -> Lwt.bind (begin $f$ end) (fun () -> Lwt.return __pa_lwt_x))
(fun __pa_lwt_e -> Lwt.bind (begin $f$ end) (fun () -> match __pa_lwt_e with $c$))
>>
end
begin match c, f with
| None, None ->
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_catch (fun exn -> try raise exn with exn -> exn) (fun () -> $e$) Lwt.fail >>
else
<:expr< Lwt.catch (fun () -> $e$) Lwt.fail >>
| Some c, None ->
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_catch (fun exn -> try raise exn with exn -> exn) (fun () -> $e$) (function $c$) >>
else
<:expr< Lwt.catch (fun () -> $e$) (function $c$) >>
| None, Some f ->
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_finalize (fun exn -> try raise exn with exn -> exn) (fun () -> $e$) (fun () -> (begin $f$ end)) >>
else
<:expr< Lwt.finalize (fun () -> $e$) (fun () -> (begin $f$ end)) >>
| Some c, Some f ->
if !Pa_lwt_options.debug then
<:expr< Lwt.backtrace_try_bind (fun exn -> try raise exn with exn -> exn) (fun () -> $e$)
(fun __pa_lwt_x -> Lwt.backtrace_bind (fun exn -> try raise exn with exn -> exn) (begin $f$ end) (fun () -> Lwt.return __pa_lwt_x))
(fun __pa_lwt_e -> Lwt.backtrace_bind (fun exn -> try raise exn with exn -> exn) (begin $f$ end) (fun () -> match __pa_lwt_e with $c$))
>>
else
<:expr< Lwt.try_bind (fun () -> $e$)
(fun __pa_lwt_x -> Lwt.bind (begin $f$ end) (fun () -> Lwt.return __pa_lwt_x))
(fun __pa_lwt_e -> Lwt.bind (begin $f$ end) (fun () -> match __pa_lwt_e with $c$))
>>
end

| "lwt"; l = letb_binding; "in"; e = expr LEVEL ";" ->
<:expr< let $gen_binding l$ in $gen_bind l e$ >>
<:expr< let $gen_binding l$ in $gen_bind l e$ >>

| "for_lwt"; p = patt; scheme = for_scheme; "do"; seq = do_sequence ->
(match p, scheme with
| <:patt< $lid:id$ >>, `CountTo(s, e) ->
<:expr< let __pa_lwt_max = $e$ in
let rec __pa_lwt_loop $lid:id$ =
if $lid:id$ > __pa_lwt_max then
Lwt.return ()
else
Lwt.bind (begin $seq$ end) (fun () -> __pa_lwt_loop ($lid:id$ + 1))
in
__pa_lwt_loop $s$
>>

| <:patt< $lid:id$ >>, `CountDownTo(s, e) ->
<:expr< let __pa_lwt_min = $e$ in
let rec __pa_lwt_loop $lid:id$ =
if $lid:id$ < __pa_lwt_min then
Lwt.return ()
else
Lwt.bind (begin $seq$ end) (fun () -> __pa_lwt_loop ($lid:id$ - 1))
in
__pa_lwt_loop $s$
>>

| p, `IterOver(e) ->
<:expr< Lwt_stream.iter_s (fun $p$ -> $seq$) $e$ >>

| _ ->
Loc.raise _loc (Failure "syntax error"))
(match p, scheme with
| <:patt< $lid:id$ >>, `CountTo(s, e) ->
<:expr< let __pa_lwt_max = $e$ in
let rec __pa_lwt_loop $lid:id$ =
if $lid:id$ > __pa_lwt_max then
Lwt.return ()
else
Lwt.bind (begin $seq$ end) (fun () -> __pa_lwt_loop ($lid:id$ + 1))
in
__pa_lwt_loop $s$
>>

| <:patt< $lid:id$ >>, `CountDownTo(s, e) ->
<:expr< let __pa_lwt_min = $e$ in
let rec __pa_lwt_loop $lid:id$ =
if $lid:id$ < __pa_lwt_min then
Lwt.return ()
else
Lwt.bind (begin $seq$ end) (fun () -> __pa_lwt_loop ($lid:id$ - 1))
in
__pa_lwt_loop $s$
>>

| p, `IterOver(e) ->
<:expr< Lwt_stream.iter_s (fun $p$ -> $seq$) $e$ >>

| _ ->
Loc.raise _loc (Failure "syntax error"))

| "raise_lwt"; e = SELF ->
if !Pa_lwt_options.debug then
<:expr< Lwt.fail (try raise $e$ with exn -> exn) >>
else
<:expr< Lwt.fail $e$ >>
if !Pa_lwt_options.debug then
<:expr< Lwt.fail (try raise $e$ with exn -> exn) >>
else
<:expr< Lwt.fail $e$ >>

| "assert_lwt"; e = SELF ->
<:expr< try Lwt.return (assert $e$) with exn -> Lwt.fail exn >>
<:expr< try Lwt.return (assert $e$) with exn -> Lwt.fail exn >>

| "while_lwt"; cond = sequence; "do"; body = sequence; "done" ->
<:expr<
let rec __pa_lwt_loop () =
if $cond$ then
Lwt.bind (begin $body$ end) __pa_lwt_loop
else
Lwt.return ()
in
__pa_lwt_loop ()
>>
<:expr<
let rec __pa_lwt_loop () =
if $cond$ then
Lwt.bind (begin $body$ end) __pa_lwt_loop
else
Lwt.return ()
in
__pa_lwt_loop ()
>>

| "match_lwt"; e = sequence; "with"; c = match_case ->
<:expr<
Lwt.bind (begin $e$ end) (function $c$)
>>
] ];

str_item:
[ [ "lwt"; l = letb_binding -> begin
match l with
| [(_loc, p, e)] ->
<:str_item<
let $p$ = Lwt_main.run $e$
>>
| _ ->
<:str_item<
let $tup:Ast.paCom_of_list (List.map (fun (_loc, p, _e) -> p) l)$ =
Lwt_main.run begin
let $gen_binding l$ in
$gen_top_bind _loc l$
end
>>
end
| "lwt"; l = letb_binding; "in"; e = expr ->
<:str_item< let () = Lwt_main.run (let $gen_binding l$ in $gen_bind l e$) >>
] ];
<:expr<
Lwt.bind (begin $e$ end) (function $c$)
>>
] ];

str_item:
[ [ "lwt"; l = letb_binding -> begin
match l with
| [(_loc, p, e)] ->
<:str_item<
let $p$ = Lwt_main.run $e$
>>
| _ ->
<:str_item<
let $tup:Ast.paCom_of_list (List.map (fun (_loc, p, _e) -> p) l)$ =
Lwt_main.run begin
let $gen_binding l$ in
$gen_top_bind _loc l$
end
>>
end
| "lwt"; l = letb_binding; "in"; e = expr ->
<:str_item< let () = Lwt_main.run (let $gen_binding l$ in $gen_bind l e$) >>
] ];
END

(* Replace the anonymous bind [x >> y] by [x >>= fun _ -> y] or [x >>= fun () ->
Expand All @@ -225,10 +225,10 @@ let map_anonymous_bind = object
inherit Ast.map as super
method! expr e = match super#expr e with
| <:expr@_loc< $lid:f$ $a$ $b$ >> when f = ">>" ->
if !Pa_lwt_options.strict_sequence then
<:expr< Lwt.bind $a$ (fun () -> $b$) >>
else
<:expr< Lwt.bind $a$ (fun _ -> $b$) >>
if !Pa_lwt_options.strict_sequence then
<:expr< Lwt.bind $a$ (fun () -> $b$) >>
else
<:expr< Lwt.bind $a$ (fun _ -> $b$) >>
| e -> e
end

Expand Down
8 changes: 4 additions & 4 deletions src/core/lwt_condition.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ let wait ?mutex cvar =
let waiter = Lwt.add_task_r cvar in
let () =
match mutex with
| Some m -> Lwt_mutex.unlock m
| None -> ()
| Some m -> Lwt_mutex.unlock m
| None -> ()
in
Lwt.finalize
(fun () -> waiter)
(fun () ->
match mutex with
| Some m -> Lwt_mutex.lock m
| None -> Lwt.return_unit)
| Some m -> Lwt_mutex.lock m
| None -> Lwt.return_unit)

let signal cvar arg =
try
Expand Down
Loading