Source file component.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
(** Stage 3 IR: CSS Syntax 3 (ED) sec. 5.1 component values and rules. *)

type 'a node = { node : 'a; loc : Loc.t }

type t =
  | Preserved of Token.t
  | Block of block node
  | Func of func node
  | Flat of Token.t list node

and block = {
  opening : Token.bracket;
  value : t list;
  closed : bool;
      (** [false] when the lexer reached EOF before the matching closer (CSS
          Syntax 3 (ED) sec. 5.5.9 parse error). Typed validators inspect this
          to reject values that the syntax level only forgives. *)
}

and func = {
  name : string;
  arguments : t list;
  terminated : bool;
      (** [false] when the lexer reached EOF before the matching [)] (CSS Syntax
          3 (ED) sec. 5.4.6 parse error). The serializer still emits the
          synthetic [)] so reserialised output round-trips through the lexer;
          typed validators can inspect this flag to reject values that the
          syntax level only forgives. *)
}

type at_rule_body = {
  name : string;
  prelude : t list;
  block : block node option;
}

type at_rule = at_rule_body node
type qualified_rule_body = { prelude : t list; block : block node }
type qualified_rule = qualified_rule_body node
type rule = Qualified of qualified_rule | At of at_rule
type declaration_body = { name : string; value : t list; important : bool }
type declaration = declaration_body node

(* A {!Loc.t} records where a node was read, not what it holds, so two nodes
   that spell the same component value are the same value however far apart the
   source wrote them. *)
let rank = function
  | Preserved _ -> 0
  | Block _ -> 1
  | Func _ -> 2
  | Flat _ -> 3

let rec compare (a : t) (b : t) =
  match (a, b) with
  | Preserved t1, Preserved t2 -> Token.compare_kind t1.kind t2.kind
  | Block b1, Block b2 -> compare_block b1.node b2.node
  | Func f1, Func f2 -> compare_func f1.node f2.node
  | Flat r1, Flat r2 ->
      List.compare
        (fun (t1 : Token.t) (t2 : Token.t) ->
          Token.compare_kind t1.kind t2.kind)
        r1.node r2.node
  | (Preserved _ | Block _ | Func _ | Flat _), _ ->
      Int.compare (rank a) (rank b)

and compare_block (b1 : block) (b2 : block) =
  let c = Token.compare_bracket b1.opening b2.opening in
  if c <> 0 then c
  else
    let c = Bool.compare b1.closed b2.closed in
    if c <> 0 then c else List.compare compare b1.value b2.value

and compare_func (f1 : func) (f2 : func) =
  let c = String.compare f1.name f2.name in
  if c <> 0 then c
  else
    let c = Bool.compare f1.terminated f2.terminated in
    if c <> 0 then c else List.compare compare f1.arguments f2.arguments

let equal a b = compare a b = 0

let source_loc : t -> Loc.t = function
  | Preserved tok -> tok.Token.loc
  | Block b -> b.loc
  | Func f -> f.loc
  | Flat r -> r.loc

let rule_loc : rule -> Loc.t = function Qualified r -> r.loc | At r -> r.loc

(* One pass over a flat run. The blocks it opens are a byte each in [open_], so
   the scan takes no stack whatever the run nests. A closer is matched only by
   the innermost open block, as sec. 5.5.9 closes one; any other is a stray
   token of the run. *)
type flat_scan = {
  bad_token : bool;
  stray_closer : bool;
  left_open : Token.bracket list;
}

let bracket_of_char = function
  | '{' -> Token.Curly
  | '[' -> Token.Square
  | _ -> Token.Paren

let scan_flat tokens =
  let open_ = Buffer.create 16 in
  let scan acc (tok : Token.t) =
    match tok.kind with
    | Token.Bad_string | Token.Bad_url -> { acc with bad_token = true }
    | Token.Open b ->
        Buffer.add_char open_ (Token.opening_char b);
        acc
    | Token.Function _ ->
        Buffer.add_char open_ (Token.opening_char Paren);
        acc
    | Token.Close b ->
        let depth = Buffer.length open_ in
        if depth > 0 && Buffer.nth open_ (depth - 1) = Token.opening_char b then (
          Buffer.truncate open_ (depth - 1);
          acc)
        else { acc with stray_closer = true }
    | _ -> acc
  in
  let acc =
    List.fold_left scan
      { bad_token = false; stray_closer = false; left_open = [] }
      tokens
  in
  let left_open = ref [] in
  String.iter
    (fun c -> left_open := bracket_of_char c :: !left_open)
    (Buffer.contents open_);
  { acc with left_open = !left_open }

let flat_is_valid ~require_closed tokens =
  let s = scan_flat tokens in
  (not s.bad_token) && (not s.stray_closer)
  && ((not require_closed) || s.left_open = [])

let flat_left_open tokens = (scan_flat tokens).left_open

let flat_calls names tokens =
  List.exists
    (fun (tok : Token.t) ->
      match tok.kind with
      | Token.Function name ->
          List.exists (String.equal (String.lowercase_ascii name)) names
      | _ -> false)
    tokens

(* CSS Values 5 makes [inherit()] an arbitrary substitution function too; Chrome
   153 refuses a declaration holding one, so cascade leaves it to the property
   grammar, which refuses it as well. *)
let is_substitution_function name =
  match String.lowercase_ascii name with
  | "var" | "env" | "attr" | "if" -> true
  | name -> Custom_property_name.is_valid name

let flat_substitutes tokens =
  List.exists
    (fun (tok : Token.t) ->
      match tok.kind with
      | Token.Function name -> is_substitution_function name
      | _ -> false)
    tokens

let flat_var_refs tokens =
  let _, refs =
    List.fold_left
      (fun (after_var, refs) (tok : Token.t) ->
        match tok.kind with
        | Token.Function name when String.lowercase_ascii name = "var" ->
            (true, refs)
        | Token.Ident n when after_var && Custom_property_name.is_valid n ->
            (false, n :: refs)
        | _ -> (after_var, refs))
      (false, []) tokens
  in
  List.rev refs

let shallow = function
  | [ Flat { node; _ } ] -> List.map (fun tok -> Preserved tok) node
  | components -> components

let rec is_any_value components =
  List.for_all
    (function
      | Preserved { kind = Token.Bad_string | Token.Bad_url | Token.Close _; _ }
        ->
          false
      | Block { node = { value; closed; _ }; _ } -> closed && is_any_value value
      | Func { node = { arguments; terminated; _ }; _ } ->
          terminated && is_any_value arguments
      | Flat { node; _ } -> flat_is_valid ~require_closed:true node
      | Preserved _ -> true)
    components

(* Every recursion below is bounded by {!Parser.max_nesting_depth}, past which a
   run is [Flat]; the lists themselves can be long, so they are walked with the
   tail-recursive [List.exists] and [List.map]. *)
(* What a token carries beyond the value it stands for: where it was read and,
   for a string, the quote it was written with. *)
let token_sourced (tok : Token.t) =
  tok.loc != Loc.dummy
  ||
  match tok.kind with
  | Token.String { quote; _ } -> quote <> '"'
  | _ -> false

let token_as_value (tok : Token.t) =
  let kind =
    match tok.kind with
    | Token.String s when s.quote <> '"' -> Token.String { s with quote = '"' }
    | kind -> kind
  in
  { tok with kind; loc = Loc.dummy }

let rec sourced = function
  | Preserved tok -> token_sourced tok
  | Block { node; loc } -> loc != Loc.dummy || List.exists sourced node.value
  | Func { node; loc } -> loc != Loc.dummy || List.exists sourced node.arguments
  | Flat { node; loc } -> loc != Loc.dummy || List.exists token_sourced node

let rec as_value = function
  | Preserved tok -> Preserved (token_as_value tok)
  | Block { node; _ } ->
      Block
        {
          node = { node with value = List.map as_value node.value };
          loc = Loc.dummy;
        }
  | Func { node; _ } ->
      Func
        {
          node = { node with arguments = List.map as_value node.arguments };
          loc = Loc.dummy;
        }
  | Flat { node; _ } ->
      Flat { node = List.map token_as_value node; loc = Loc.dummy }

let as_values values =
  if List.exists sourced values then List.map as_value values else values

let is_whitespace = function
  | Preserved { kind = Token.Whitespace _; _ } -> true
  | Preserved _ | Block _ | Func _ | Flat _ -> false

(* A real [var()] function anywhere in the components, recursing into function
   arguments and bracketed blocks. A [var(] inside a string or a url() is an
   atomic [Preserved] token, never a [Func], so it is data, not a reference. *)
let rec has_var components =
  List.exists
    (fun (c : t) ->
      match c with
      | Func { node = { name; arguments; _ }; _ } ->
          String.lowercase_ascii name = "var" || has_var arguments
      | Block { node = { value; _ }; _ } -> has_var value
      | Flat { node; _ } -> flat_calls [ "var" ] node
      | Preserved _ -> false)
    components

let token_number (tok : Token.t) =
  match tok.kind with
  | Number_tok n | Percentage n | Dimension { number = n; _ } -> Some n
  | _ -> None

let map_token_number f (tok : Token.t) : Token.t =
  match tok.kind with
  | Number_tok n -> { tok with kind = Number_tok (f n) }
  | Percentage n -> { tok with kind = Percentage (f n) }
  | Dimension { number; unit_ } ->
      { tok with kind = Dimension { number = f number; unit_ } }
  | _ -> tok

let rec exists_number p = function
  | Preserved tok -> (
      match token_number tok with Some n -> p n | None -> false)
  | Func { node = { arguments; _ }; _ } ->
      List.exists (exists_number p) arguments
  | Block { node = { value; _ }; _ } -> List.exists (exists_number p) value
  | Flat { node; _ } ->
      List.exists
        (fun tok -> match token_number tok with Some n -> p n | None -> false)
        node

let rec map_numbers f = function
  | Preserved tok -> Preserved (map_token_number f tok)
  | Func fn ->
      Func
        {
          fn with
          node =
            {
              fn.node with
              arguments = List.map (map_numbers f) fn.node.arguments;
            };
        }
  | Block b ->
      Block
        {
          b with
          node = { b.node with value = List.map (map_numbers f) b.node.value };
        }
  | Flat fl -> Flat { fl with node = List.map (map_token_number f) fl.node }

(* A debug dump, not source text: every node shows its own location and the CSS
   Syntax 3 (ED) sec. 5.5.9 and 5.5.10 flags [compare] separates values on.
   [Pp.space] rather than [Pp.sp] because a dump has no minified form, and
   layout whitespace would drop out and run the children together. *)
let rec pp : t Pp.t =
 fun ctx cv ->
  match cv with
  | Preserved tok -> Token.pp ctx tok
  | Block { node = { opening; value; closed }; loc } ->
      Pp.char ctx (Token.opening_char opening);
      Pp.list ~sep:Pp.space pp ctx value;
      Pp.char ctx (Token.closing_char opening);
      if not closed then Pp.string ctx "<unclosed>";
      Pp.char ctx '@';
      Loc.pp ctx loc
  | Func { node = { name; arguments; terminated }; loc } ->
      Pp.string ctx name;
      Pp.char ctx '(';
      Pp.list ~sep:Pp.space pp ctx arguments;
      Pp.char ctx ')';
      if not terminated then Pp.string ctx "<unterminated>";
      Pp.char ctx '@';
      Loc.pp ctx loc
  | Flat { node; loc } ->
      Pp.string ctx "<flat";
      List.iter
        (fun tok ->
          Pp.space ctx ();
          Token.pp ctx tok)
        node;
      Pp.string ctx ">@";
      Loc.pp ctx loc

let to_string t = Pp.to_string pp t