This repository has been archived by the owner on Jun 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastParseExtendedAttributes.ml
91 lines (78 loc) · 3.14 KB
/
astParseExtendedAttributes.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
open ContextError
type ctx = ContextError.ctx
type 'a arg_handler = 'a -> ctx -> string option -> SimpleAst.arguments option -> 'a
let xattr_plain id (f: ctx -> 'a -> 'a): string * 'a arg_handler =
(id, fun state ctx equ args ->
begin match equ with
| Some equ -> warn ctx "Unexpected `=%s' for %s" equ id
| None -> ()
end;
begin match args with
| Some _ -> warn ctx "Unexpected argument declaration for %s" id
| None -> ()
end;
f ctx state)
let xattr_equals id (f: ctx -> 'a -> string -> 'a): string * 'a arg_handler =
(id, fun state ctx equ args ->
begin match args with
| Some _ -> warn ctx "Unexpected argument declaration for %s" id
| None -> ()
end;
begin match equ with
| Some equ -> f ctx state equ
| None -> error ctx "Missing `=RHS' for %s" id; state
end)
let xattr_maybe_arguments
id (f: ctx -> 'a -> SimpleAst.arguments option -> 'a): string * 'a arg_handler =
(id, fun state ctx equ args ->
begin match equ with
| Some equ -> warn ctx "Unexpected `=%s' for %s" equ id
| None -> ()
end;
f ctx state args)
let xattr_equals_maybe_arguments
id
(f: ctx -> 'a -> string -> SimpleAst.arguments option -> 'a): string * 'a arg_handler =
(id, fun state ctx equ args ->
begin match equ with
| Some equ -> f ctx state equ args
| None -> error ctx "Missing `=RHS' for %s" id; state
end)
let xattr_equals_specific
id (cases: (string * ('a -> 'a)) list): string * 'a arg_handler =
xattr_equals
id
(fun ctx state key ->
try (List.assoc key cases) state
with Not_found ->
error ctx "Unexpected value `%s' for %s" key id;
state)
let handle_one state ctx handlers = let open SimpleAst in fun { name; equals; arguments } ->
try
(true, List.assoc name handlers state ctx equals arguments)
with Not_found -> (false, state)
let handle_non_failing_known state ctx handlers (xattrs: SimpleAst.extended_attribute_list) =
List.fold_left (fun (state, unhandled) xattr ->
let (handled, state') = handle_one state (ctx_push_state ctx) handlers xattr
in if handled && check_and_merge_state_if_not_failed ctx then
(state', unhandled)
else (state, xattr :: unhandled))
(state, []) xattrs
let handle_all_known state ctx handlers xattrs =
List.fold_left (fun (state, unhandled) xattr ->
let (handled, state') = handle_one state ctx handlers xattr
in if handled then
(state', unhandled)
else (state, xattr :: unhandled))
(state, []) xattrs
let get_name ({ SimpleAst.name }: SimpleAst.extended_attribute) = name
let partition_attributes l =
List.partition (fun xattr -> List.mem (get_name xattr) l)
let keep_good_attributes l xattrs = fst (partition_attributes l xattrs)
let drop_bad_attributes ctx l xattrs =
let (good, bad) = partition_attributes l xattrs in
if bad <> [] then begin
warn ctx "Inadmissible extended attributes: %a" (Fmt.list Fmt.string)
(List.map get_name bad)
end;
good