pa_tryfinally.ml

   1: (* The function that returns unique identifiers *)
   2: let new_id = 
   3:   let counter = ref 0 in
   4:   fun () ->
   5:     incr counter;
   6:     "__finally" ^ string_of_int !counter
   7: 
   8: (* The function that converts our syntax into a single OCaml expression,
   9:    i.e. an "expr" node of the syntax tree *)
  10: let expand loc e1 e2 =
  11:   let id = new_id () in
  12:   let id_patt = <:patt< $lid:id$ >> in
  13:   let id_expr = <:expr< $lid:id$ >> in
  14:   <:expr<
  15:   let $id_patt$ =
  16:     try do { $e1$; None } 
  17:     with [ exn -> Some exn ] in
  18:   do { $e2$;
  19:        match $id_expr$ with
  20:            [ None -> ()
  21:            | Some exn -> raise exn ] }
  22:   >>
  23: 
  24: (* The statement that extends the default grammar, 
  25:    i.e. the regular syntax of OCaml if we use camlp5o 
  26:    or the revised syntax if we use camlp5r *)
  27: EXTEND
  28:   Pcaml.expr: LEVEL "expr1" [
  29:     [ "try"; e1 = Pcaml.expr; "finally"; e2 = Pcaml.expr -> expand loc e1 e2 ]
  30:   ];
  31: END;;

This document was generated using caml2html