Skip to content

Commit

Permalink
Parse the keywords item from OCAMLPARAM
Browse files Browse the repository at this point in the history
Signed-off-by: David Allsopp <[email protected]>
  • Loading branch information
dra27 committed Nov 6, 2024
1 parent 8a0cb71 commit dde0675
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ details.

- Add initial OCaml 5.3 support (#487, @NathanReb, @hhugo, @nojb)

- Initialise OCaml 5.3's lexer with the `keywords` setting from `OCAMLPARAM` to
allow the standalone ppx driver to process old packages using `effect` as an
identifier (#535, @dra27)

### Other changes

- Fix `deriving_inline` round-trip check so that it works with 5.01 <-> 5.02
Expand Down
24 changes: 24 additions & 0 deletions astlib/keyword.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,27 @@ let is_keyword = function
| "lsr" -> true
| "asr" -> true
| _ -> false

let apply_keyword_edition () =
match Sys.getenv "OCAMLPARAM" with
| s ->
let items =
if String.equal s "" then []
else
(* cf. Compenv.parse_args *)
match s.[0] with
| (':' | '|' | ';' | ' ' | ',') as c ->
List.tl (String.split_on_char c s)
| _ -> String.split_on_char ',' s
in
let fold_settings acc item =
let len = String.length item in
if len >= 9 && String.sub item 0 9 = "keywords=" then
Some (String.sub item 9 (len - 9))
else acc
in
let keyword_edition = List.fold_left fold_settings None items in
(*IF_AT_LEAST 503 let () = if Option.is_some keyword_edition then Clflags.keyword_edition := keyword_edition in*)
(*IF_NOT_AT_LEAST 503 let () = ignore keyword_edition in*)
()
| exception Not_found -> ()
4 changes: 4 additions & 0 deletions astlib/keyword.mli
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
val is_keyword : string -> bool
(** Check if a string is an OCaml keyword. *)

val apply_keyword_edition : unit -> unit
(** Processes any keywords= sections from the OCAMLPARAM environment variable
and initialises the compiler's lexer with the correct keyword set. *)
1 change: 1 addition & 0 deletions src/driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,7 @@ let get_args ?(standalone_args = standalone_args) () =
let standalone_main () =
let usage = Printf.sprintf "%s [extra_args] [<files>]" exe_name in
let args = get_args () in
Astlib.Keyword.apply_keyword_edition ();
Arg.parse (Arg.align args) set_input usage;
interpret_mask ();
if !request_print_transformations then (
Expand Down

0 comments on commit dde0675

Please sign in to comment.