package ppx_css

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file css_jane.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
open! Core
open Css_parser

let sexp_of_with_loc sexp_of_a (a, _) = sexp_of_a a

module Sexpers = struct
  (* All the sexp-of functions are written in a single let-rec
     so that I can avoid having to deal with mutually recursive modules. *)
  let rec sexp_of_dimension : Types.dimension -> Sexp.t = function
    | Length -> [%sexp Length]
    | Angle -> [%sexp Angle]
    | Time -> [%sexp Time]
    | Frequency -> [%sexp Frequency]

  and sexp_of_component_value : Types.Component_value.t -> Sexp.t = function
    | Paren_block t_list -> [%sexp Paren_block (t_list : component_value with_loc list)]
    | Bracket_block t_list ->
      [%sexp Bracket_block (t_list : component_value with_loc list)]
    | Percentage s -> [%sexp Percentage (s : string)]
    | Ident s -> [%sexp Ident (s : string)]
    | String s -> [%sexp String (s : string)]
    | Uri s -> [%sexp Uri (s : string)]
    | Operator s -> [%sexp Operator (s : string)]
    | Delim s -> [%sexp Delim (s : string)]
    | Ampersand -> [%sexp Ampersand]
    | Function (s, t_list) ->
      [%sexp
        Function ((s, t_list) : string with_loc * component_value with_loc list with_loc)]
    | Hash s -> [%sexp Hash (s : string)]
    | Number s -> [%sexp Number (s : string)]
    | Unicode_range s -> [%sexp Unicode_range (s : string)]
    | Float_dimension (s1, s2, dim) ->
      [%sexp Float_dimension ((s1, s2, dim) : string * string * dimension)]
    | Dimension (s1, s2) -> [%sexp Dimension ((s1, s2) : string * string)]

  and sexp_of_declaration : Types.Declaration.t -> Sexp.t = function
    | { name; value; important; loc = _ } ->
      [%sexp
        { name : string with_loc
        ; value : component_value with_loc list with_loc
        ; important : bool with_loc
        }]

  and sexp_of_brace_block : Types.Brace_block.t -> Sexp.t = function
    | Empty -> [%sexp Empty]
    | Declaration_list d -> [%sexp Declaration_list (d : declaration_list)]
    | Stylesheet s -> [%sexp Stylesheet (s : stylesheet)]

  and sexp_of_at_rule : Types.At_rule.t -> Sexp.t = function
    | { name; prelude; block; loc = _ } ->
      [%sexp
        { name : string with_loc
        ; prelude : component_value with_loc list with_loc
        ; block : brace_block
        }]

  and sexp_of_declaration_list_kind : Types.Declaration_list.kind -> Sexp.t = function
    | Declaration d -> [%sexp Declaration (d : declaration)]
    | At_rule a -> [%sexp At_rule (a : at_rule)]
    | Style_rule a -> [%sexp Style_rule (a : style_rule)]

  and sexp_of_declaration_list : Types.Declaration_list.t -> Sexp.t = function
    | d -> [%sexp (d : declaration_list_kind list with_loc)]

  and sexp_of_style_rule : Types.Style_rule.t -> Sexp.t = function
    | { prelude; block; loc = _ } ->
      [%sexp
        { prelude : component_value with_loc list with_loc; block : declaration_list }]

  and sexp_of_rule : Types.Rule.t -> Sexp.t = function
    | Style_rule s -> [%sexp Style_rule (s : style_rule)]
    | At_rule s -> [%sexp At_rule (s : at_rule)]

  and sexp_of_stylesheet : Types.Stylesheet.t -> Sexp.t = function
    | s -> [%sexp (s : rule list with_loc)]
  ;;
end

[@@@warning "-30"] (* Disabling duplicate definition of record fields warning. *)

(* NOTE: The reason we have an almost identical type definition AST is because
   [ppxlib_traverse] ppx needs to be derived on a set of types defined like this for
   it to be able to fully instrospect and create traverse classes nicely. *)
type 'a with_loc = 'a * location

and dimension = Types.dimension =
  | Length
  | Angle
  | Time
  | Frequency

and component_value = Types.Component_value.t =
  | Paren_block of component_value with_loc list
  | Bracket_block of component_value with_loc list
  | Percentage of string
  | Ident of string
  | String of string
  | Uri of string
  | Operator of string
  | Delim of string
  | Ampersand
  | Function of string with_loc * component_value with_loc list with_loc
  | Hash of string
  | Number of string
  | Unicode_range of string
  | Float_dimension of (string * string * dimension)
  | Dimension of (string * string)

and declaration = Types.Declaration.t =
  { name : string with_loc
  ; value : component_value with_loc list with_loc
  ; important : bool with_loc
  ; loc : location
  }

and location = Location.t

and brace_block = Types.Brace_block.t =
  | Empty
  | Declaration_list of declaration_list
  | Stylesheet of stylesheet

and at_rule = Types.At_rule.t =
  { name : string with_loc
  ; prelude : component_value with_loc list with_loc
  ; block : brace_block
  ; loc : location
  }

and declaration_list_kind = Types.Declaration_list.kind =
  | Declaration of declaration
  | At_rule of at_rule
  | Style_rule of style_rule

and declaration_list = declaration_list_kind list with_loc

and style_rule = Types.Style_rule.t =
  { prelude : component_value with_loc list with_loc
  ; block : declaration_list
  ; loc : location
  }

and rule = Types.Rule.t =
  | Style_rule of style_rule
  | At_rule of at_rule

and stylesheet = rule list with_loc [@@deriving traverse_map]

module Dimension = struct
  type t = Types.dimension =
    | Length
    | Angle
    | Time
    | Frequency

  let sexp_of_t = Sexpers.sexp_of_dimension
end

module Component_value = struct
  type t = Types.Component_value.t =
    | Paren_block of t with_loc list
    | Bracket_block of t with_loc list
    | Percentage of string
    | Ident of string
    | String of string
    | Uri of string
    | Operator of string
    | Delim of string
    | Ampersand
    | Function of string with_loc * t with_loc list with_loc
    | Hash of string
    | Number of string
    | Unicode_range of string
    | Float_dimension of (string * string * Dimension.t)
    | Dimension of (string * string)

  let sexp_of_t = Sexpers.sexp_of_component_value
end

module Declaration = struct
  type t = Types.Declaration.t =
    { name : string with_loc
    ; value : Component_value.t with_loc list with_loc
    ; important : bool with_loc
    ; loc : Location.t
    }

  let sexp_of_t = Sexpers.sexp_of_declaration
end

module Brace_block = struct
  type t = Types.Brace_block.t =
    | Empty
    | Declaration_list of Types.Declaration_list.t
    | Stylesheet of Types.Stylesheet.t

  let sexp_of_t = Sexpers.sexp_of_brace_block
end

module At_rule = struct
  type t = Types.At_rule.t =
    { name : string with_loc
    ; prelude : Component_value.t with_loc list with_loc
    ; block : Brace_block.t
    ; loc : Location.t
    }

  let sexp_of_t = Sexpers.sexp_of_at_rule
end

module Declaration_list = struct
  type kind = Types.Declaration_list.kind =
    | Declaration of Declaration.t
    | At_rule of At_rule.t
    | Style_rule of Types.Style_rule.t

  and t = kind list with_loc

  let sexp_of_kind = Sexpers.sexp_of_declaration_list_kind
  let sexp_of_t = Sexpers.sexp_of_declaration_list
end

module Style_rule = struct
  type t = Types.Style_rule.t =
    { prelude : Component_value.t with_loc list with_loc
    ; block : Declaration_list.t
    ; loc : Location.t
    }

  let sexp_of_t = Sexpers.sexp_of_style_rule
end

module Rule = struct
  type t = Types.Rule.t =
    | Style_rule of Style_rule.t
    | At_rule of At_rule.t

  let sexp_of_t = Sexpers.sexp_of_rule
end

module Stylesheet = struct
  type t = Rule.t list with_loc

  let to_string which t =
    let buffer = Buffer.create 64 in
    let formatter = Format.formatter_of_buffer buffer in
    Css_printer.Print.(css which) formatter t;
    Buffer.contents buffer
  ;;

  let to_string_hum = to_string Css_printer.Print.pretty_printer
  let to_string_minified = to_string Css_printer.Print.minify_printer

  let of_string ?pos s =
    (* the parser produces different output depending on if there is
       a leading space or not.  They're equivalent semantically, but
       I can add this to remove ambiguity. *)
    let s = " " ^ s in
    Css_parser.Parser.parse_stylesheet ?pos s
  ;;

  let sexp_of_t = Sexpers.sexp_of_stylesheet
end

module Traverse = struct
  class map' =
    object
      inherit map
      method bool : bool -> bool = Fn.id
      method list : 'a. ('a -> 'a) -> 'a list -> 'a list = fun f -> List.map ~f
      method string : string -> string = Fn.id
      method location__t : Location.t -> Location.t = Fn.id
    end

  class map = map'
end
OCaml

Innovation. Community. Security.