-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for source quotation when the driver reports an exception
Signed-off-by: Nathan Rebours <[email protected]>
- Loading branch information
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
(executable | ||
(name raising_driver) | ||
(libraries ppxlib)) | ||
|
||
(cram | ||
(deps raising_driver.exe)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
open Ppxlib | ||
|
||
let rules = | ||
[ | ||
Extension.V3.declare "raise" Extension.Context.expression | ||
Ast_pattern.(pstr nil) | ||
(fun ~ctxt -> | ||
let loc = Expansion_context.Extension.extension_point_loc ctxt in | ||
Location.raise_errorf ~loc "An exception, raise be!") | ||
|> Context_free.Rule.extension; | ||
] | ||
|
||
let () = Driver.V2.register_transformation ~rules "raise" | ||
let () = Driver.standalone () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
When the ppxlib driver reports an error by itself, source quotation should work | ||
properly. | ||
|
||
We start off by explicitly setting the error reporting style to contextual to | ||
ensure source quotation is enabled: | ||
|
||
$ export OCAML_ERROR_STYLE=contextual | ||
|
||
Here we have a driver compiled with a single rule that will raise a located | ||
exception for every "[%raise]" extension point. | ||
|
||
We need an input file: | ||
|
||
$ cat > file.ml << EOF | ||
> let x = [%raise] | ||
> EOF | ||
|
||
When running the driver on this file, it should report the error and show | ||
the relevant quoted source: | ||
|
||
$ ./raising_driver.exe -impl file.ml | ||
File "file.ml", line 1, characters 8-16: | ||
1 | let x = [%raise] | ||
^^^^^^^^ | ||
Error: An exception, raise be! | ||
[1] |