forked from bcpierce00/unison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pred.ml
179 lines (155 loc) · 6.56 KB
/
pred.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
(* Unison file synchronizer: src/pred.ml *)
(* Copyright 1999-2020, Benjamin C. Pierce
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
let debug = Util.debug "pred"
(********************************************************************)
(* TYPES *)
(********************************************************************)
type t =
{ pref: string list Prefs.t;
name: string; (* XXX better to get it from Prefs! *)
mutable default: string list;
mutable last_pref : string list;
mutable last_def : string list;
mutable last_mode : Case.mode;
mutable compiled: Rx.t;
mutable associated_strings : (Rx.t * string) list;
}
let error_msg s =
Printf.sprintf "bad pattern: %s\n\
A pattern must be introduced by one of the following keywords:\n\
\032 Name, Path, BelowPath or Regex." s
(* [select str [(p1, f1), ..., (pN, fN)] fO]: (roughly) *)
(* match str with *)
(* p1 p' -> f1 p' *)
(* ... *)
(* pN p' -> fN p' *)
(* otherwise -> fO str *)
let rec select str l f =
match l with
[] -> f str
| (pref, g)::r ->
if Util.startswith str pref then
let l = String.length pref in
let s =
Util.trimWhitespace (String.sub str l (String.length str - l)) in
g ((Case.ops())#normalizePattern s)
else
select str r f
let mapSeparator = "->"
(* Compile a pattern (in string form) to a regular expression *)
let compile_pattern clause =
let (p,v) =
let sep = " "^mapSeparator^" " in
(* Surround by spaces to make it less likely to appear in a pathspec *)
match Util.splitAtString ~reverse:true (" "^clause^" ") sep with
(* Actually find "(^| )mapSep( |$)" (by surrounding [clause] by spaces
possibly removed by previous trimming) to detect an empty pattern
and/or an empty string *)
("", _) -> raise (Prefs.IllegalValue "Empty pattern")
| (p, None) -> (p, None)
| (p, Some v) -> (p, Some (Util.trimWhitespace v)) in
let compiled =
begin try
select (String.sub p 1 ((String.length p)-1)) (* Remove prepended space *)
[("Name ", fun str -> Rx.seq [Rx.rx "(.*/)?"; Rx.globx str]);
("Path ", fun str ->
if str<>"" && str.[0] = '/' then
raise (Prefs.IllegalValue
("Malformed pattern: "
^ "\"" ^ p ^ "\"\n"
^ "'Path' patterns may not begin with a slash; "
^ "only relative paths are allowed."));
Rx.globx str);
("BelowPath ", fun str ->
if str<>"" && str.[0] = '/' then
raise (Prefs.IllegalValue
("Malformed pattern: "
^ "\"" ^ p ^ "\"\n"
^ "'BelowPath' patterns may not begin with a slash; "
^ "only relative paths are allowed."));
Rx.seq [Rx.globx str; Rx.rx "(/.*)?"]);
("Regex ", Rx.rx)]
(fun str -> raise (Prefs.IllegalValue (error_msg p)))
with
Rx.Parse_error | Rx.Not_supported ->
raise (Prefs.IllegalValue ("Malformed pattern \"" ^ p ^ "\"."))
end in
(compiled, v)
let create name ~category ?(local=false) ?send ?(initial = []) fulldoc =
let pref =
Prefs.create name ~category ~local ?send initial
("add a pattern to the " ^ name ^ " list")
fulldoc
(fun oldList string ->
ignore (compile_pattern string); (* Check well-formedness *)
string :: oldList)
(fun l -> l) Umarshal.(list string) in
{pref = pref; name = name;
last_pref = []; default = []; last_def = []; last_mode = (Case.ops())#mode;
compiled = Rx.empty; associated_strings = []}
let addDefaultPatterns p pats =
p.default <- Safelist.append pats p.default
let alias p n = Prefs.alias p.pref n
let recompile mode p =
let pref = Prefs.read p.pref in
let compiledList = Safelist.map compile_pattern (Safelist.append p.default pref) in
let compiled = Rx.alt (Safelist.map fst compiledList) in
let handleCase rx =
if (Case.ops())#caseInsensitiveMatch then Rx.case_insensitive rx
else rx
in
let strings = Safelist.filterMap
(fun (rx,vo) ->
match vo with
None -> None
| Some v -> Some (handleCase rx,v))
compiledList in
p.compiled <- handleCase compiled;
p.associated_strings <- strings;
p.last_pref <- pref;
p.last_def <- p.default;
p.last_mode <- mode
let recompile_if_needed p =
let mode = (Case.ops())#mode in
if
p.last_mode <> mode ||
p.last_pref != Prefs.read p.pref ||
p.last_def != p.default
then
recompile mode p
(********************************************************************)
(* IMPORT / EXPORT *)
(********************************************************************)
let intern p regexpStringList = Prefs.set p.pref regexpStringList
let extern p = Prefs.read p.pref
let extern_associated_strings p =
recompile_if_needed p;
Safelist.map snd p.associated_strings
(********************************************************************)
(* TESTING *)
(********************************************************************)
let test p s =
recompile_if_needed p;
let res = Rx.match_string p.compiled ((Case.ops())#normalizeMatchedString s) in
debug (fun() -> Util.msg "%s '%s' = %b\n" p.name s res);
res
let assoc p s =
recompile_if_needed p;
let s = (Case.ops())#normalizeMatchedString s in
snd (Safelist.find (fun (rx,v) -> Rx.match_string rx s) p.associated_strings)
let assoc_all p s =
recompile_if_needed p;
let s = (Case.ops())#normalizeMatchedString s in
Safelist.map snd
(Safelist.filter (fun (rx,v) -> Rx.match_string rx s) p.associated_strings)