diff --git a/CHANGES.md b/CHANGES.md index fb20f846..7c0d422f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/astlib/keyword.ml b/astlib/keyword.ml index 4e03a18b..e2beb24a 100644 --- a/astlib/keyword.ml +++ b/astlib/keyword.ml @@ -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 -> () diff --git a/astlib/keyword.mli b/astlib/keyword.mli index 36c490f6..a7977796 100644 --- a/astlib/keyword.mli +++ b/astlib/keyword.mli @@ -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. *) diff --git a/src/driver.ml b/src/driver.ml index 5d91a6b4..ac20acf0 100644 --- a/src/driver.ml +++ b/src/driver.ml @@ -1417,6 +1417,7 @@ let get_args ?(standalone_args = standalone_args) () = let standalone_main () = let usage = Printf.sprintf "%s [extra_args] []" 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 (