This commit is contained in:
swrup 2025-11-11 02:07:51 +01:00
parent aa2ff7b2f0
commit 2f3113f55d
11742 changed files with 1223940 additions and 0 deletions

View file

@ -0,0 +1,15 @@
(executable
(name raising_driver)
(modules raising_driver)
(libraries ppxlib))
(executable
(name identity_driver)
(modules identity_driver)
(libraries ppxlib))
(cram
(package ppxlib)
(enabled_if
(>= %{ocaml_version} "4.08.0"))
(deps raising_driver.exe identity_driver.exe))

View file

@ -0,0 +1 @@
let () = Ppxlib.Driver.standalone ()

View file

@ -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 ()

View file

@ -0,0 +1,45 @@
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]
This should also work when the input is a binary AST as the file contains the name
of the original source file. Our driver should be able to properly set the input
lexbuf and get the source quotation to work, assuming the information in the binary
AST file is correct.
Here we use an identity driver to generate the binary AST for our .ml file above:
$ ./identity_driver.exe -impl file.ml -dump-ast -o file.pp.ml
We then call our raising driver on the binary AST, it should be able to report the
error with source quotation:
$ ./raising_driver.exe -impl file.pp.ml
File "file.ml", line 1, characters 8-16:
1 | let x = [%raise]
^^^^^^^^
Error: An exception, raise be!
[1]