Source file file_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
let find_ml_end =
let regex = Re.compile Re.(alt [ str "(*"; str "*)" ]) in
fun file_contents start_pos ->
let rec ~depth current_pos =
match Re.exec_opt regex file_contents ~pos:current_pos with
| None -> None
| Some m ->
let next_pos = Re.Group.stop m 0 in
(match Re.Group.get m 0 with
| "(*" -> nest_comments ~depth:(depth + 1) next_pos
| "*)" ->
if depth = 0
then Some (next_pos - 2, next_pos)
else nest_comments ~depth:(depth - 1) next_pos
| _ ->
failwith
"ML regex matched something other than an ML comment start/end!"
[@coverage off])
in
nest_comments ~depth:0 start_pos
;;
let find_non_nesting_end end_regex file_contents start_pos =
match Re.exec_opt end_regex file_contents ~pos:(start_pos + 1) with
| None -> None
| Some m ->
let start, stop = Re.Group.offset m 0 in
Some (start, stop)
;;
let regex file_contents start_pos =
match Re.exec_opt regex file_contents ~pos:(start_pos + 1) with
| None ->
String.length file_contents - 1
| Some m -> Re.Group.start m 0 - 1
;;
let =
let c_end_regex = Re.compile Re.(seq [ rep1 (char '*'); char '/' ]) in
let xml_end_regex = Re.compile Re.(str "-->") in
let not_char c = Re.(compl [ char ' '; char '\t'; char c ]) in
let line_start = Re.(seq [ char '\n'; rep (alt [ char ' '; char '\t' ]) ]) in
let sh_regex = Re.compile Re.(seq [ line_start; not_char '#' ]) in
let lisp_regex = Re.compile Re.(seq [ line_start; not_char ';' ]) in
let c_line_regex = Re.compile Re.(seq [ line_start; opt (char '/'); not_char '/' ]) in
let sql_regex = Re.compile Re.(seq [ line_start; opt (char '-'); not_char '-' ]) in
fun file_contents content_start_pos ->
let end_block kind =
let find_end =
match kind with
| `ml -> find_ml_end
| `c -> find_non_nesting_end c_end_regex
| `xml -> find_non_nesting_end xml_end_regex
in
match find_end file_contents content_start_pos with
| None -> None
| Some (end_contents, end_cr) ->
let contents =
String.sub
file_contents
~pos:content_start_pos
~len:(end_contents - content_start_pos)
in
Some (comment_start_pos, end_cr - 1, contents)
in
let end_lines regex =
let end_pos = find_line_comment_end regex file_contents content_start_pos in
let contents =
String.sub
file_contents
~pos:content_start_pos
~len:(end_pos + 1 - content_start_pos)
in
comment_start_pos, end_pos, contents
in
let rec check_backwards ~last pos =
let end_lines regex = Some (end_lines regex (pos + 1)) in
if pos < 0
then (
match last with
| `semi -> end_lines lisp_regex
| `hash -> end_lines sh_regex
| `slashes n -> if n >= 2 then end_lines c_line_regex else None
| `dashes n -> if n >= 2 then end_lines sql_regex else None
| `star | `not_special -> None)
else (
let curr_char = file_contents.[pos] in
let check_backwards last = check_backwards ~last (pos - 1) in
match last, curr_char with
| `star, '*' -> check_backwards `star
| `star, '/' -> end_block `c pos
| `star, '(' -> end_block `ml pos
| `star, _ -> None
| `slashes n, '/' -> check_backwards (`slashes (n + 1))
| `slashes n, _ -> if n >= 2 then end_lines c_line_regex else None
| `semi, ';' -> check_backwards `semi
| `semi, _ -> end_lines lisp_regex
| `hash, '#' -> check_backwards `hash
| `hash, _ -> end_lines sh_regex
| `dashes n, '-' -> check_backwards (`dashes (n + 1))
| `dashes n, '!'
when n >= 2 && pos > 0 && Char.( = ) '<' file_contents.[pos - 1] ->
end_block `xml (pos - 1)
| `dashes n, _ -> if n >= 2 then end_lines sql_regex else None
| `not_special, '/' -> check_backwards (`slashes 1)
| `not_special, '*' -> check_backwards `star
| `not_special, ';' -> check_backwards `semi
| `not_special, '#' -> check_backwards `hash
| `not_special, '-' -> check_backwards (`dashes 1)
| `not_special, (' ' | '\t' | '\n') -> check_backwards `not_special
| `not_special, _ -> None)
in
check_backwards ~last:`not_special (content_start_pos - 1)
;;
let cr_pattern_egrep = "\\bX?CR[-v: \\t]"
let cr_regex =
Re.compile
Re.(
seq
[ bow
; opt (char 'X')
; str "CR"
; alt [ char '-'; char 'v'; char ':'; char ' '; char '\t' ]
])
;;
let condense_whitespace =
let regex = Re.compile Re.(rep1 (set " \t\n")) in
fun s -> Re.replace_string regex ~by:" " s
;;
let parse_file ~path ~(file_contents : Vcs.File_contents.t) =
let file_contents = (file_contents :> string) in
let file_cache =
lazy (Loc.File_cache.create ~path:(Vcs.Path_in_repo.to_fpath path) ~file_contents)
in
let ms = Re.all cr_regex file_contents in
let ( let* ) a f = Option.bind a ~f in
List.filter_map ms ~f:(fun m ->
let content_start_offset = Re.Group.start m 0 in
let* start_offset, end_offset, content =
find_comment_bounds file_contents content_start_offset
in
let content = String.rstrip content in
let file_cache = Lazy.force file_cache in
let = Header_parser.parse ~file_cache ~content_start_offset ~content in
let is_considered_a_CR =
match header with
| Ok _ -> true
| Error _ ->
(match Invalid_cr_parser.parse ~file_cache ~content_start_offset ~content with
| Invalid_cr _ -> true
| Not_a_cr -> false)
in
match is_considered_a_CR with
| false -> None
| true ->
let =
String.sub
file_contents
~pos:start_offset
~len:(content_start_offset - start_offset)
|> String.strip
in
let start_position = Loc.Offset.to_position start_offset ~file_cache in
let stop_position = Loc.Offset.to_position (end_offset + 1) ~file_cache in
let whole_loc = Loc.create (start_position, stop_position) in
let digest_of_condensed_content =
Cr_comment.Digest_hex.create (condense_whitespace content)
in
let =
Cr_comment.Private.create
~path
~whole_loc
~content_start_offset
~header
~comment_prefix
~digest_of_condensed_content
~content
in
Some cr_comment)
;;