2828
2929open Std
3030
31+ let {Logger. log} = Logger. for_section " mreader_lexer"
32+
3133type keywords = Lexer_raw .keywords
3234
3335type triple = Parser_raw .token * Lexing .position * Lexing .position
@@ -223,6 +225,15 @@ let is_operator = function
223225 »
224226*)
225227
228+ let print_token fmt = function
229+ | LIDENT s -> Format. fprintf fmt " LIDENT %s" s
230+ | UIDENT s -> Format. fprintf fmt " UIDENT %s" s
231+ | LPAREN -> Format. fprintf fmt " LPAREN"
232+ | RPAREN -> Format. fprintf fmt " RPAREN"
233+ | DOT -> Format. fprintf fmt " DOT"
234+ | EOF -> Format. fprintf fmt " EOF"
235+ | _ -> Format. fprintf fmt " OTHER" ;;
236+
226237let reconstruct_identifier_from_tokens tokens pos =
227238 let rec look_for_component acc = function
228239
@@ -246,6 +257,36 @@ let reconstruct_identifier_from_tokens tokens pos =
246257 when is_operator token <> None && acc = [] ->
247258 look_for_dot [item] items
248259
260+ (* RPAREN UIDENT means that we are in presence of a functor application. *)
261+ | (RPAREN , _, end_pos) :: ((UIDENT _, _, _ ) as item) :: items
262+ when acc <> [] ->
263+ let param_items, items = group_until_lparen [item] items in
264+ begin try
265+ begin try
266+ (* Is the cursor on the parameter ? *)
267+ look_for_dot [] (List. rev param_items)
268+ with Not_found ->
269+ (* Is the cursor on the functor or before ? *)
270+ look_for_component [] items
271+ end
272+ with Not_found ->
273+ (* The cursor must be after the application [M.N(F).|t]
274+ We make a single component with the applciation and continue *)
275+ match items with
276+ | (UIDENT f , start_pos , _ ) :: items ->
277+ let app =
278+ let param = List. map ~f: (function
279+ | (DOT, _ , _ ) -> " ."
280+ | (UIDENT s , _ , _ ) -> s
281+ | _ -> raise Not_found
282+ ) param_items
283+ in
284+ Format. sprintf " %s(%s)" f (String. concat ~sep: " " param)
285+ in
286+ look_for_dot ((UIDENT app, start_pos, end_pos ) :: acc) items
287+ | _ -> raise Not_found
288+ end
289+
249290 (* An operator alone is an identifier on its own *)
250291 | (token, _, _ as item) :: items
251292 when is_operator token <> None && acc = [] ->
@@ -257,6 +298,11 @@ let reconstruct_identifier_from_tokens tokens pos =
257298
258299 | [] -> raise Not_found
259300
301+ and group_until_lparen acc = function
302+ | (LPAREN,_ ,_ ) :: items -> acc, items
303+ | item :: items -> group_until_lparen (item::acc) items
304+ | _ -> raise Not_found
305+
260306 and look_for_dot acc = function
261307 | (DOT,_ ,_ ) :: items -> look_for_component acc items
262308 | items -> check acc items
@@ -312,6 +358,9 @@ let reconstruct_identifier config source pos =
312358 let lexbuf = Lexing. from_string (Msource. text source) in
313359 Location. init lexbuf (Mconfig. filename config);
314360 let tokens = lex [] lexbuf in
361+ log ~title: " from_tokens" " %a" Logger. fmt (fun fmt ->
362+ Format. pp_print_list ~pp_sep: (fun fmt () -> Format. fprintf fmt " ;@ " )
363+ (fun fmt (tok , _ , _ ) -> print_token fmt tok) fmt tokens);
315364 reconstruct_identifier_from_tokens tokens pos
316365
317366let is_uppercase {Location. txt = x ; _} =
0 commit comments