Skip to content

Commit

Permalink
clarify Obj.magic
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypopp committed Jun 2, 2024
1 parent 1d28867 commit 33500b1
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions ocamlmerlin_mlx/ocamlmerlin_mlx.ml
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
open Merlin_extend
open Merlin_extend.Extend_protocol.Reader
open Ocaml_parsing
open Mlx_ocaml_parsing
open Mlx_kernel

module Conv = Ppxlib_ast.Convert(Ppxlib_ast__Versions.OCaml_501)(Ppxlib_ast.Compiler_version)

let conv_signature intf =
let intf: Astlib.Ast_501.Parsetree.signature = Obj.magic intf in
let intf = Conv.copy_signature intf in
let intf: Ocaml_parsing.Parsetree.signature = Obj.magic intf in
intf

let conv_structure impl =
let impl: Astlib.Ast_501.Parsetree.structure = Obj.magic impl in
let impl = Conv.copy_structure impl in
let impl: Ocaml_parsing.Parsetree.structure = Obj.magic impl in
impl
(* Some notes on usage of Obj.magic here...
- we copy parsetree.ml from merlin' 501 branch which copies AST from OCaml's
5.1.x branch. TODO: instead we should inject ppxlib's Ast_501 module there
- then finally the currently installed merlin-extend should have the same
AST as the one used by compiled. TODO: figure out why compiler doesn't see
that...
*)
module Conv = struct
module Conv =
Ppxlib_ast.Convert
(Ppxlib_ast__Versions.OCaml_501)
(Ppxlib_ast.Compiler_version)

let conv_signature (intf : Mlx_ocaml_parsing.Parsetree.signature) :
Ocaml_parsing.Parsetree.signature =
let intf : Astlib.Ast_501.Parsetree.signature = Obj.magic intf in
let intf = Conv.copy_signature intf in
let intf : Ocaml_parsing.Parsetree.signature = Obj.magic intf in
intf

let conv_structure (impl : Mlx_ocaml_parsing.Parsetree.structure) :
Ocaml_parsing.Parsetree.structure =
let impl : Astlib.Ast_501.Parsetree.structure = Obj.magic impl in
let impl = Conv.copy_structure impl in
let impl : Ocaml_parsing.Parsetree.structure = Obj.magic impl in
impl
end

let parse_string filename str =
let src = Msource.make str in
Expand Down Expand Up @@ -81,17 +95,18 @@ module Mlx_reader = struct
List.filter_map to_extension_node res.lexer_errors
in
match res.parsetree with
| `Interface intf -> Signature (conv_signature intf)
| `Interface intf -> Signature (Conv.conv_signature intf)
| `Implementation impl ->
Structure (conv_structure impl @ parser_errors @ lexer_errors)
Structure
(Conv.conv_structure (impl @ parser_errors @ lexer_errors))

let for_completion t _pos = { complete_labels = true }, parse t

let parse_line _ _ text =
let res = parse_string "*buffer*" text in
match res.parsetree with
| `Interface intf -> Signature (conv_signature intf)
| `Implementation impl -> Structure (conv_structure impl)
| `Interface intf -> Signature (Conv.conv_signature intf)
| `Implementation impl -> Structure (Conv.conv_structure impl)

let ident_at _ _ = []
let pretty_print _ppf _ = ()
Expand Down

0 comments on commit 33500b1

Please sign in to comment.