forked from ocaml/opam
/
file_format.mli
260 lines (184 loc) · 7.6 KB
/
file_format.mli
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
(***********************************************************************)
(* *)
(* Copyright 2012 OCamlPro *)
(* Copyright 2012 INRIA *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Public License version 3.0. *)
(* *)
(* OPAM 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. *)
(* *)
(***********************************************************************)
(** Untyped generic file format. *)
(** Base values *)
type value =
| Bool of bool
| Int of int
| String of string
| Symbol of string
| Ident of string
| List of value list
| Group of value list
| Option of value * value list
(** A file section *)
type section = {
kind : string;
name : string;
items : item list;
}
(** A file is composed of sections and variable definitions *)
and item =
| Section of section
| Variable of string * value
(** A file is a list of items and the filename *)
type file = {
contents : item list;
filename : string;
}
(** The empty file *)
val empty : file
(** Get all the variable definitions from a list of items *)
val variables : item list -> (string * value) list
(** Get all the sections from a list of items *)
val sections : item list -> (string * section) list
(** Check whether a list of items contains only valid variable definitions *)
val is_valid : item list -> string list -> bool
(** Find all the invalid fields *)
val invalid_fields : item list -> string list -> string list
(** {2 Parsing functions} *)
(** All the following parsing function raise [Bad_format] in case the
input does not have the right format. *)
exception Bad_format of string
(** Parse a boolean *)
val parse_bool : value -> bool
(** Parse an integer *)
val parse_int: value -> int
(** Parse an ident *)
val parse_ident : value -> string
(** Parse a symbol *)
val parse_symbol : value -> string
(** Parse a string *)
val parse_string : value -> string
(** Parse a list of 'things' *)
val parse_list : (value -> 'a) -> value -> 'a list
(** Parse a group of 'things' *)
val parse_group : (value -> 'a) -> value -> 'a list
(** Parse a value and its option of 'things' *)
val parse_option : (value -> 'a) -> (value list -> 'b) -> value -> 'a * 'b option
(** Parse a value and a single optional value *)
val parse_single_option : (value -> 'a) -> (value -> 'b) -> value -> 'a * 'b option
(** Parse a string with an optional argument *)
val parse_string_option : (value list -> 'a) -> value -> string * 'a option
(** Parse a list of strings *)
val parse_string_list : value -> string list
(** Parse a single string *)
val parse_single_string: value list -> string
(** Parse a pair of strings *)
val parse_string_pair: value -> string * string
(** Parse a pair of strings from a list of values *)
val parse_string_pair_of_list: value list -> string * string
(** Try to parse the value using function from the list. All the
parsing functions are tried until one succeeds. The first argument
is a debug message. *)
val parse_or: (string * (value -> 'a)) list -> value -> 'a
(** Parse a sequence of values *)
val parse_sequence: (string * (value -> 'a)) list -> value -> 'a list
(** {2 Creation functions} *)
(** Create a boolean *)
val make_bool : bool -> value
(** Create an integer *)
val make_int: int -> value
(** Create an ident *)
val make_ident : string -> value
(** Create a symbol *)
val make_symbol : string -> value
(** Create a string *)
val make_string : string -> value
(** Create a list of 'things' *)
val make_list : ('a -> value) -> 'a list -> value
(** Create a group of 'things' *)
val make_group : ('a -> value) -> 'a list -> value
(** Create a value and its optional arguments *)
val make_option : ('a -> value) -> ('b -> value list) -> ('a * 'b option) -> value
(** Create a pair *)
val make_pair: ('a -> value) -> ('a * 'a) -> value
(** Create a pair of strings *)
val make_string_pair: string * string -> value
(** {2 Printing functions} *)
(** Print a value *)
val string_of_value : value -> string
(** Print a list of values *)
val string_of_values : value list -> string
(** When folding recursively a value [Variable (s, v)] for printing,
we check if we indent the first encountered list below [v]
(i.e. in case [indent_variable s] is [true]). *)
type indent_variable = string -> bool
(** Print an item *)
val string_of_item : ?indent_variable:indent_variable -> item -> string option
(** Print a list of items *)
val string_of_items : ?indent_variable:indent_variable -> item list -> string
(** Print a file *)
val string_of_file : ?indent_variable:indent_variable -> file -> string
(** {2 Finding functions} *)
(** Get the value of a field *)
val assoc : item list -> string -> (value -> 'a) -> 'a
(** Get the value of a field. If the field does not exist, return
None *)
val assoc_option : item list -> string -> (value -> 'a) -> 'a option
(** Get the value of a field. If the variable does not exist, return a
default value *)
val assoc_default : 'a -> item list -> string -> (value -> 'a) -> 'a
(** Get the value associated to a variable. If the variable does not
exists, return [] *)
val assoc_list : item list -> string -> (value -> 'a list) -> 'a list
(** Get the string list associated to a variable. If the variable does
not exist, return [] *)
val assoc_string_list : item list -> string -> string list
(** Get one section of a certain kind *)
val get_section_by_kind : item list -> string -> section
(** Get all the sections of a certain kind *)
val get_all_section_by_kind : item list -> string -> section list
(** Get sections *)
val assoc_sections: item list -> string -> (section -> 'a) -> 'a list
(** {2 Formula} *)
(** This section is dedicated to the parsing and creatin of dependency
and conflict formaulas. It's maybe easier to do that directly in
the parser ... *)
open Types
(** Parse package formula where AND are implicit: [x y -> x & y] *)
val parse_formula : value -> Formula.t
(** Build a formula where AND are implicit. *)
val make_formula : Formula.t -> value
(** Parse optional package formula where OR are implicit: [x y -> x | y] *)
val parse_opt_formula : value -> Formula.t
(** Build a formula where OR are implicit. *)
val make_opt_formula : Formula.t -> value
(** Parse a simple constraint *)
val parse_constraint: value -> ocaml_constraint
(** Build a simple constraint *)
val make_constraint: ocaml_constraint -> value
(** {2 Environment variables} *)
(** Parsing *)
val parse_env_variable: value -> (string * string * string)
(** Making *)
val make_env_variable: (string * string * string) -> value
(** {2 filter expressions} *)
(** Parsing *)
val parse_filter: value list -> filter
(** Creation *)
val make_filter: filter -> value list
(** Parse a command argument *)
val parse_arg: value -> arg
(** Create a command argument *)
val make_arg: arg -> value
(** Parse a command *)
val parse_command: value -> command
(** Create a command *)
val make_command: command -> value
(** Parse a list of commands *)
val parse_commands: value -> command list
(** Create a list of commands *)
val make_commands: command list -> value