package crs

  1. Overview
  2. Docs
A tool for managing code review comments embedded in source code

Install

dune-project
 Dependency

Authors

Maintainers

Sources

crs-0.0.20250914.tbz
sha256=dad0c46a83ef40da150d8b840b01c66af3b979dfa7c3e17c3edef38aa84b804f
sha512=d7a8b2a34f4a3ddda0eaaa3e4248ec2870f944a5d64782bd73b031637b01e94a378d7534b1c02da739f32343b342ed8e8bf491e6442a3bd7b0e748bd61c58770

doc/src/crs.crs-parser/invalid_cr_parser.ml.html

Source file invalid_cr_parser.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(********************************************************************************)
(*  crs - A tool for managing code review comments embedded in source code      *)
(*  Copyright (C) 2024-2025 Mathieu Barbin <mathieu.barbin@gmail.com>           *)
(*                                                                              *)
(*  This file is part of crs.                                                   *)
(*                                                                              *)
(*  crs is free software; you can redistribute it and/or modify it under the    *)
(*  terms of the GNU Lesser General Public License as published by the Free     *)
(*  Software Foundation either version 3 of the License, or any later version,  *)
(*  with the LGPL-3.0 Linking Exception.                                        *)
(*                                                                              *)
(*  crs 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 GNU Lesser General Public License and     *)
(*  the file `NOTICE.md` at the root of this repository for more details.       *)
(*                                                                              *)
(*  You should have received a copy of the GNU Lesser General Public License    *)
(*  and the LGPL-3.0 Linking Exception along with this library. If not, see     *)
(*  <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.        *)
(********************************************************************************)

let word_t =
  Re.compl [ Re.char ' '; Re.char '\t'; Re.char '\n'; Re.char ':'; Re.char '@' ]
;;

let whitespace = Re.alt [ Re.char ' '; Re.char '\n'; Re.char '\t' ]

module Re_helper = struct
  type t =
    { re : Re.re
    ; status : int
    ; qualifier : int
    ; reporter : int
    ; for_or_to : int
    ; recipient : int
    ; contents : int
    }
end

let make_re_helper () =
  let status_g = "status" in
  let qualifier_g = "qualifier" in
  let reporter_g = "reporter" in
  let for_or_to_g = "for_or_to" in
  let recipient_g = "recipient" in
  let contents_g = "contents" in
  let re =
    Re.(
      whole_string
        (seq
           [ rep whitespace
           ; group ~name:status_g (seq [ opt (char 'X'); str "CR" ])
           ; opt (seq [ char '-'; group ~name:qualifier_g (rep1 word_t) ])
           ; rep whitespace
           ; opt (group ~name:reporter_g (rep1 word_t))
           ; opt
               (seq
                  [ rep whitespace
                  ; group ~name:for_or_to_g (alt [ str "for"; str "to" ])
                  ; rep1 whitespace
                  ; group ~name:recipient_g (rep1 word_t)
                  ])
           ; rep whitespace
           ; opt (char ':')
           ; group ~name:contents_g (rep any)
           ]))
    |> Re.compile
  in
  let groups = Re.group_names re |> Map.of_alist_exn (module String) in
  let find_group ~name = Map.find_exn groups name in
  { Re_helper.re
  ; status = find_group ~name:status_g
  ; qualifier = find_group ~name:qualifier_g
  ; reporter = find_group ~name:reporter_g
  ; for_or_to = find_group ~name:for_or_to_g
  ; recipient = find_group ~name:recipient_g
  ; contents = find_group ~name:contents_g
  }
;;

let re_helper = lazy (make_re_helper ())

module Invalid_cr = struct
  type t =
    { status : Cr_comment.Status.t Loc.Txt.t
    ; qualifier : string Loc.Txt.t option
    ; reporter : string Loc.Txt.t option
    ; for_or_to : string Loc.Txt.t option
    ; recipient : string Loc.Txt.t option
    ; contents : string Loc.Txt.t
    }

  let status t = t.status
  let qualifier t = t.qualifier
  let reporter t = t.reporter
  let for_or_to t = t.for_or_to
  let recipient t = t.recipient
  let contents t = t.contents
end

module Maybe_invalid_cr = struct
  type t =
    | Invalid_cr of Invalid_cr.t
    | Not_a_cr
end

let parse ~file_cache ~content_start_offset ~content =
  let re_helper = Lazy.force re_helper in
  match Re.exec_opt re_helper.re content with
  | None -> Maybe_invalid_cr.Not_a_cr
  | Some m ->
    let get index =
      match Re.Group.get_opt m index with
      | None -> None
      | Some v ->
        let start, stop = Re.Group.offset m index in
        let loc =
          Loc.of_file_range
            ~file_cache
            ~range:
              { start = content_start_offset + start; stop = content_start_offset + stop }
        in
        Some (v, loc)
    in
    let reporter =
      match get re_helper.reporter with
      | None -> None
      | Some (reporter, loc) -> Some { Loc.Txt.txt = reporter; loc }
    in
    let status =
      match get re_helper.status with
      | None -> assert false (* Mandatory in the [regexp]. *)
      | Some (status, loc) ->
        let txt : Cr_comment.Status.t =
          match status with
          | "CR" -> CR
          | "XCR" -> XCR
          | _ -> assert false (* Cannot be parsed according to the [regexp]. *)
        in
        { Loc.Txt.txt; loc }
    in
    let for_or_to =
      Option.map (get re_helper.for_or_to) ~f:(fun (user, loc) ->
        { Loc.Txt.txt = user; loc })
    in
    let recipient =
      Option.map (get re_helper.recipient) ~f:(fun (user, loc) ->
        { Loc.Txt.txt = user; loc })
    in
    let qualifier =
      match get re_helper.qualifier with
      | None -> None
      | Some (txt, loc) -> Some { Loc.Txt.txt; loc }
    in
    let contents =
      match get re_helper.contents with
      | None -> assert false (* Mandatory in the [regexp]. *)
      | Some (contents, loc) -> { Loc.Txt.txt = String.strip contents; loc }
    in
    if String.is_empty contents.txt
    then Maybe_invalid_cr.Not_a_cr
    else
      Maybe_invalid_cr.Invalid_cr
        { status; qualifier; reporter; for_or_to; recipient; contents }
;;