Skip to content

Commit

Permalink
improvements on special functions
Browse files Browse the repository at this point in the history
Signed-off-by: Burnleydev1 <[email protected]>
  • Loading branch information
Burnleydev1 authored and NathanReb committed Jan 11, 2024
1 parent cc0838f commit 08c23fb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
20 changes: 16 additions & 4 deletions src/context_free.ml
Original file line number Diff line number Diff line change
Expand Up @@ -518,18 +518,30 @@ class map_top_down ?(expect_mismatch_handler = Expect_mismatch_handler.nop)
| None ->
self#pexp_apply_without_traversing_function base_ctxt e func args
| Some pattern -> (
match pattern e with
let pat_expr =
try (pattern e, [])
with exn when embed_errors -> (None, [ exn_to_error exn ])
in
pat_expr >>= fun expr ->
match expr with
| None ->
self#pexp_apply_without_traversing_function base_ctxt e func
args
| Some e -> self#expression base_ctxt e))
| Some e -> self#expression base_ctxt e
(* with exn when embed_errors -> (e, [ exn_to_error exn ]) *)))
| Pexp_ident id -> (
match Hashtbl.find_opt special_functions id.txt with
| None -> super#expression base_ctxt e
| Some pattern -> (
match pattern e with
let pat_exp =
try (pattern e, [])
with exn when embed_errors -> (None, [ exn_to_error exn ])
in
pat_exp >>= fun expr ->
match expr with
| None -> super#expression base_ctxt e
| Some e -> self#expression base_ctxt e))
| Some e -> self#expression base_ctxt e
(* with exn when embed_errors -> (e, [ exn_to_error exn ]) *)))
| Pexp_constant (Pconst_integer (s, Some c)) -> (
try expand_constant Integer c s
with exn when embed_errors -> (e, [ exn_to_error exn ]))
Expand Down
7 changes: 4 additions & 3 deletions test/driver/exception_handling/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,14 @@ when the -embed-errors flag is not passed
$ echo "n_args2 " >> impl.ml
When embed-errors is not passed
$ ./special_functions.exe impl.ml
File "_none_", line 1:
Error: error special function
File "impl.ml", lines 1-2, characters 0-7:
Error: error special function
[1]

When embed-errors is not passed
$ ./special_functions.exe -embed-errors impl.ml
[%%ocaml.error "error special function"]
[%%ocaml.error "error special function "]
[%%ocaml.error "second error special function"]
;;n_args n_args2

In the case of whole file transformations:
Expand Down
12 changes: 2 additions & 10 deletions test/driver/exception_handling/special_functions.ml
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
open Ppxlib

let expand e =
let return n = Some (Ast_builder.Default.eint ~loc:e.pexp_loc n) in
match e.pexp_desc with
| Pexp_apply (_, _arg_list) -> Location.raise_errorf "error special function"
| _ -> return 0
let expand e = Location.raise_errorf ~loc:e.pexp_loc "error special function "

let expand2 e =
let return n = Some (Ast_builder.Default.eint ~loc:e.pexp_loc n) in
match e.pexp_desc with
| Pexp_apply (_, _arg_list2) ->
Location.raise_errorf "error special function 2"
| _ -> return 0
Location.raise_errorf ~loc:e.pexp_loc "second error special function"

let rule = Context_free.Rule.special_function "n_args" expand
let rule2 = Context_free.Rule.special_function "n_args2" expand2;;
Expand Down

0 comments on commit 08c23fb

Please sign in to comment.