1 ! Copyright (C) 2022 CapitalEx
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs compiler.units formatting
4 hash-sets hashtables io io.encodings.utf8 io.files
5 kernel namespaces regexp sequences sequences.deep sets sorting
6 splitting unicode vocabs vocabs.loader ;
7 FROM: namespaces => set ;
11 SYMBOL: old-dictionary
13 : save-dictionary ( -- )
17 : restore-dictionary ( -- )
18 dictionary get keys >hash-set
19 old-dictionary get keys >hash-set
20 diff members [ [ forget-vocab ] each ] with-compilation-unit ;
22 : vocab-loaded? ( name -- ? )
25 : (get-words) ( name -- vocab )
26 dup load-vocab words>> keys 2array ;
28 : no-vocab-found ( name -- empty )
31 : nl>space ( string -- string )
34 : find-import-statements ( string -- seq )
35 "USING: [^;]+ ;|USE: \\S+" <regexp> all-matching-subseqs ;
37 : clean-up-source ( string -- string )
38 "\"(\\\\\"|[^\"])*\"|R/ (\\\\/|[^/])*/|\\\\\\s+(USE:|USING:)|POSTPONE:\\s+(USE:|USING:)|! [^\n]*|CHAR:\\s+\\S+\\s+" <regexp> "" re-replace ;
40 : strip-syntax ( seq -- seq )
41 [ "USING: | ;|USE: " <regexp> " " re-replace ] map ;
43 : split-when-blank ( string -- seq )
44 [ blank? ] split-when ;
46 : split-words ( line -- words )
47 [ split-when-blank ] map flatten harvest ;
49 : get-unique-words ( seq -- hash-set )
50 harvest split-words >hash-set ;
52 : [is-used?] ( hash-set -- quot )
53 '[ nip [ _ in? ] any? ] ; inline
55 : reject-unused-vocabs ( assoc hash-set -- seq )
56 [is-used?] assoc-reject keys ;
58 : print-unused-vocabs ( name seq -- )
59 swap "The following vocabs are unused in %s: \n" printf
60 [ " - " prepend print ] each ;
62 : print-no-unused-vocabs ( name _ -- )
63 drop "No unused vocabs found in %s.\n" printf ;
67 : get-words ( name -- assoc )
70 [ no-vocab-found ] if ;
72 : get-vocabs ( string -- seq )
73 nl>space find-import-statements strip-syntax split-words harvest ;
75 : get-imported-words ( string -- hashtable )
77 get-vocabs [ get-words ] map >hashtable
81 : find-unused-in-string ( string -- seq )
83 [ get-imported-words ] [ "\n" split get-unique-words ] bi
84 reject-unused-vocabs natural-sort ; inline
86 : find-unused-in-file ( path -- seq )
87 utf8 file-contents find-unused-in-string ;
89 : find-unused ( name -- seq )
90 vocab-source-path dup [ find-unused-in-file ] when ;
92 : find-unused. ( name -- )
93 dup find-unused dup empty?
94 [ print-no-unused-vocabs ]
95 [ print-unused-vocabs ] if ;