Ver Fonte

Implemented duplicate declaration pruning

Taddeus Kroes há 11 anos atrás
pai
commit
d36f52bf3f
3 ficheiros alterados com 40 adições e 3 exclusões
  1. 2 2
      Makefile
  2. 37 0
      duplicates.ml
  3. 1 1
      main.ml

+ 2 - 2
Makefile

@@ -1,7 +1,7 @@
 RESULT    := mincss
 PRE_TGTS  := types
 MODULES   := color_names util stringify parser lexer parse selector color \
-             shorthand main
+             shorthand duplicates main
 ALL_NAMES := $(PRE_TGTS) $(MODULES)
 
 OCAMLCFLAGS  := -g
@@ -36,7 +36,7 @@ lexer.cmi: lexer.ml
 parser.cmx: parser.cmi lexer.cmx
 parser.mli: parser.ml
 parse.cmx: lexer.cmi parser.cmx
-main.cmx: parse.cmx util.cmx color.cmx shorthand.cmx
+main.cmx: parse.cmx util.cmx color.cmx shorthand.cmx duplicates.cmx
 util.cmx: OCAMLCFLAGS += -pp cpp
 util.cmx color.cmx: color_names.cmx
 stringify.cmx parser.cmx color.cmx shorthand.cmx: util.cmi

+ 37 - 0
duplicates.ml

@@ -0,0 +1,37 @@
+open Types
+
+module SM =  Map.Make(String)
+
+let prune_duplicates decls =
+  let keep = Array.make (List.length decls) true in
+
+  let rec tag db index = function
+    | (name, _, imp) :: tl when SM.mem name db ->
+      (* previous value exists, one needs to be removed *)
+      let prev_index, prev_imp = SM.find name db in
+      if not imp && prev_imp then begin
+        keep.(index) <- false;
+        tag db (index + 1) tl
+      end else begin
+        keep.(prev_index) <- false;
+        tag (SM.add name (index, imp) db) (index + 1) tl
+      end
+    | (name, _, imp) :: tl ->
+      tag (SM.add name (index, imp) db) (index + 1) tl
+    | [] -> ()
+  in
+  tag SM.empty 0 decls;
+
+  let rec prune i = function
+    | []                     -> []
+    | hd :: tl when keep.(i) -> hd :: prune (i + 1) tl
+    | _ :: tl                -> prune (i + 1) tl
+  in
+  prune 0 decls
+
+let transform = function
+  | Statement (Ruleset (selectors, decls)) ->
+    Statement (Ruleset (selectors, prune_duplicates decls))
+  | v -> v
+
+let compress = Util.transform_stylesheet transform

+ 1 - 1
main.ml

@@ -136,7 +136,7 @@ let handle_args args =
     (* unfold before pruning duplicates so that shorthand components are
      * correctly pruned *)
     |> switch args.shorthands Shorthand.unfold_stylesheet
-    (*|> switch args.duplicates Duplicates.compress*)
+    |> switch args.duplicates Duplicates.compress
     |> switch args.shorthands Shorthand.compress
   in
   let output =