package pfff

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file comment_code.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(* Yoann Padioleau
 *
 * Copyright (C) 2014 Facebook
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation, with the
 * special exception on linking described in file license.txt.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the file
 * license.txt for more details.
 *)
open Common

module PI = Parse_info

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(*
 * todo: extract and factorize more from comment_php.ml
 *)

(*****************************************************************************)
(* Types *)
(*****************************************************************************)

(* todo: duplicate of matcher/parse_fuzzy.ml *)
type 'tok hooks = {
  kind: 'tok -> Parse_info.token_kind;
  tokf: 'tok -> Parse_info.info;
}

(*****************************************************************************)
(* Functions *)
(*****************************************************************************)


let comment_before hooks tok all_toks =
  let pos = Parse_info.pos_of_info tok in
  let before = 
    all_toks +> Common2.take_while (fun tok2 ->
      let info = hooks.tokf tok2 in
      let pos2 = PI.pos_of_info info in
      pos2 < pos
    )
  in
  let first_non_space =
    List.rev before +> Common2.drop_while (fun t ->
      let kind = hooks.kind t in
      match kind with
      | PI.Esthet PI.Newline | PI.Esthet PI.Space -> true
    | _ -> false
    )
  in
  match first_non_space with
  | x::_xs when hooks.kind x =*= PI.Esthet (PI.Comment) ->
      let info = hooks.tokf x in
      if PI.col_of_info info = 0
      then Some info
      else None
  | _ -> None


let comment_after hooks tok all_toks =
  let pos = PI.pos_of_info tok in
  let line = PI.line_of_info tok in
  let after = 
    all_toks +> Common2.drop_while (fun tok2 ->
      let info = hooks.tokf tok2 in
      let pos2 = PI.pos_of_info info in
      pos2 <= pos
    )
  in
  let first_non_space =
    after +> Common2.drop_while (fun t ->
      let kind = hooks.kind t in
      match kind with
      | PI.Esthet PI.Newline | PI.Esthet PI.Space -> true
    | _ -> false
    )
  in
  match first_non_space with
  | x::_xs when hooks.kind x =*= PI.Esthet (PI.Comment) ->
      let info = hooks.tokf x in
      (* for ocaml comments they are not necessarily in
       * column 0, but they must be just after
       *)
      if PI.line_of_info info = line || PI.line_of_info info = line + 1
         (* && PI.col_of_info info > 0 *)
      then Some info
      else None
  | _ -> None
OCaml

Innovation. Community. Security.