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,9 @@
(executable
(name print_greetings)
(libraries ppxlib)
(preprocess
(pps ppxlib.metaquot)))
(cram
(package ppxlib)
(deps print_greetings.exe))

View file

@ -0,0 +1,20 @@
open Ppxlib
let hi_rule =
let expand ~loc ~path:_ = [%expr print_endline "hi"] in
Extension.declare "print_hi" Extension.Context.expression
Ast_pattern.(pstr nil)
expand
|> Context_free.Rule.extension
let bye_rule =
let expand ~loc ~path:_ = [%expr print_endline "bye"] in
Extension.declare "print_bye" Extension.Context.expression
Ast_pattern.(pstr nil)
expand
|> Context_free.Rule.extension
(* the two rules need to be registered separately in order to test the `-apply` flag in run.t *)
let () = Driver.register_transformation ~rules:[ hi_rule ] "print_hi"
let () = Driver.register_transformation ~rules:[ bye_rule ] "print_bye"
let () = Ppxlib.Driver.run_as_ppx_rewriter ()

View file

@ -0,0 +1,92 @@
Keep the error output short in order to avoid different error output between
different compiler versions in the subsequent tests
$ export OCAML_ERROR_STYLE=short
The registered rewriters get applied when using `run_as_ppx_rewriter` as entry point
$ cat > file.ml << EOF
> let () = [%print_hi]
> let () = [%print_bye]
> EOF
$ ocaml -ppx './print_greetings.exe' file.ml
hi
bye
The driver's `shared_args` are taken into account, such as `-apply`...
$ ocaml -ppx './print_greetings.exe -apply print_hi' file.ml
hi
File "./file.ml", line 2, characters 11-20:
Error: Uninterpreted extension 'print_bye'.
[2]
... and `-check`
$ echo "[@@@attr non_registered_attr]" > attribute_file.ml
$ ocaml -ppx './print_greetings.exe -check' attribute_file.ml
File "./attribute_file.ml", line 1, characters 4-8:
Error: Attribute `attr' was not used
[2]
If a non-compatible file gets fed, the file name is reported correctly
$ touch no_binary_ast.ml
$ ./print_greetings.exe no_binary_ast.ml some_output
File "no_binary_ast.ml", line 1:
Error: Expected a binary AST as input
[1]
The only possible usage is [extra_args] <infile> <outfile>...
$ ./print_greetings.exe some_input
Usage: print_greetings.exe [extra_args] <infile> <outfile>
[2]
...in particular the order between the flags and the input/output matters.
$ touch some_output
$ ./print_greetings.exe some_input some_output -check
./print_greetings.exe: anonymous arguments not accepted.
print_greetings.exe [extra_args] <infile> <outfile>
-loc-filename <string> File name to use in locations
-reserve-namespace <string> Mark the given namespace as reserved
-no-check Disable checks (unsafe)
-check Enable checks
-no-check-on-extensions Disable checks on extension point only
-check-on-extensions Enable checks on extension point only
-no-locations-check Disable locations check only
-locations-check Enable locations check only
-apply <names> Apply these transformations in order (comma-separated list)
-dont-apply <names> Exclude these transformations
-no-merge Do not merge context free transformations (better for debugging rewriters). As a result, the context-free transformations are not all applied before all impl and intf.
-cookie NAME=EXPR Set the cookie NAME to EXPR
--cookie Same as -cookie
-raise-embedded-errors Raise the first embedded error found in the processed AST
-allow-deriving-end Whether to allow [@@@deriving.end], which will soon be deprecated.
-help Display this list of options
--help Display this list of options
[2]
The only exception is consulting help
$ ./print_greetings.exe -help
print_greetings.exe [extra_args] <infile> <outfile>
-loc-filename <string> File name to use in locations
-reserve-namespace <string> Mark the given namespace as reserved
-no-check Disable checks (unsafe)
-check Enable checks
-no-check-on-extensions Disable checks on extension point only
-check-on-extensions Enable checks on extension point only
-no-locations-check Disable locations check only
-locations-check Enable locations check only
-apply <names> Apply these transformations in order (comma-separated list)
-dont-apply <names> Exclude these transformations
-no-merge Do not merge context free transformations (better for debugging rewriters). As a result, the context-free transformations are not all applied before all impl and intf.
-cookie NAME=EXPR Set the cookie NAME to EXPR
--cookie Same as -cookie
-raise-embedded-errors Raise the first embedded error found in the processed AST
-allow-deriving-end Whether to allow [@@@deriving.end], which will soon be deprecated.
-help Display this list of options
--help Display this list of options