1 ! Copyright (C) 2007, 2008 Slava Pestov.
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
3 USING: kernel io io.styles io.files io.files.info io.directories
\r
4 io.pathnames io.encodings.utf8 vocabs.loader vocabs sequences
\r
5 namespaces make math.parser arrays hashtables assocs memoize
\r
6 summary sorting splitting combinators source-files debugger
\r
7 continuations compiler.errors init checksums checksums.crc32
\r
8 sets accessors generic definitions words ;
\r
11 : vocab-xref ( vocab quot -- vocabs )
\r
12 [ [ vocab-name ] [ words [ generic? not ] filter ] bi ] dip map
\r
14 [ [ word? ] [ generic? not ] bi and ] filter [
\r
16 [ "method-generic" word-prop ] when
\r
19 ] gather natural-sort remove sift ; inline
\r
21 : vocabs. ( seq -- )
\r
22 [ dup >vocab-link write-object nl ] each ;
\r
24 : vocab-uses ( vocab -- vocabs ) [ uses ] vocab-xref ;
\r
26 : vocab-uses. ( vocab -- ) vocab-uses vocabs. ;
\r
28 : vocab-usage ( vocab -- vocabs ) [ usage ] vocab-xref ;
\r
30 : vocab-usage. ( vocab -- ) vocab-usage vocabs. ;
\r
32 : vocab-tests-file ( vocab -- path )
\r
33 dup "-tests.factor" vocab-dir+ vocab-append-path dup
\r
34 [ dup exists? [ drop f ] unless ] [ drop f ] if ;
\r
36 : vocab-tests-dir ( vocab -- paths )
\r
37 dup vocab-dir "tests" append-path vocab-append-path dup [
\r
39 dup directory-files [ ".factor" tail? ] filter
\r
40 [ append-path ] with map
\r
44 : vocab-tests ( vocab -- tests )
\r
46 [ vocab-tests-file [ , ] when* ]
\r
47 [ vocab-tests-dir [ % ] when* ] bi
\r
50 : vocab-files ( vocab -- seq )
\r
52 [ vocab-source-path [ , ] when* ]
\r
53 [ vocab-docs-path [ , ] when* ]
\r
54 [ vocab-tests % ] tri
\r
57 : vocab-heading. ( vocab -- )
\r
60 [ vocab-name ] [ vocab write-object ] bi ":" print
\r
63 : load-error. ( triple -- )
\r
64 [ first vocab-heading. ] [ second print-error ] bi ;
\r
66 : load-failures. ( failures -- )
\r
67 [ load-error. nl ] each ;
\r
71 : require-all ( vocabs -- failures )
\r
73 V{ } clone blacklist set
\r
74 V{ } clone failures set
\r
77 [ swap vocab-name failures get set-at ]
\r
81 ] with-compiler-errors ;
\r
83 : source-modified? ( path -- ? )
\r
84 dup source-files get at [
\r
87 utf8 file-lines crc32 checksum-lines
\r
88 swap checksum>> = not
\r
96 SYMBOL: changed-vocabs
\r
98 [ f changed-vocabs set-global ] "tools.vocabs" add-init-hook
\r
100 : changed-vocab ( vocab -- )
\r
101 dup vocab changed-vocabs get and
\r
102 [ dup changed-vocabs get set-at ] [ drop ] if ;
\r
104 : unchanged-vocab ( vocab -- )
\r
105 changed-vocabs get delete-at ;
\r
107 : unchanged-vocabs ( vocabs -- )
\r
108 [ unchanged-vocab ] each ;
\r
110 : changed-vocab? ( vocab -- ? )
\r
111 changed-vocabs get dup [ key? ] [ 2drop t ] if ;
\r
113 : filter-changed ( vocabs -- vocabs' )
\r
114 [ changed-vocab? ] filter ;
\r
116 SYMBOL: modified-sources
\r
117 SYMBOL: modified-docs
\r
119 : (to-refresh) ( vocab variable loaded? path -- )
\r
122 pick changed-vocab? [
\r
123 source-modified? [ get push ] [ 2drop ] if
\r
125 ] [ drop get push ] if
\r
126 ] [ 2drop 2drop ] if ;
\r
128 : to-refresh ( prefix -- modified-sources modified-docs unchanged )
\r
130 V{ } clone modified-sources set
\r
131 V{ } clone modified-docs set
\r
136 [ modified-sources ]
\r
137 [ vocab source-loaded?>> ]
\r
138 [ vocab-source-path ]
\r
142 [ vocab docs-loaded?>> ]
\r
143 [ vocab-docs-path ]
\r
148 modified-sources get
\r
151 [ modified-docs get modified-sources get append diff ] bi
\r
154 : do-refresh ( modified-sources modified-docs unchanged -- )
\r
157 [ [ vocab f >>source-loaded? drop ] each ]
\r
158 [ [ vocab f >>docs-loaded? drop ] each ] bi*
\r
162 [ unchanged-vocabs ]
\r
163 [ require-all load-failures. ] bi
\r
166 : refresh ( prefix -- ) to-refresh do-refresh ;
\r
168 : refresh-all ( -- ) "" refresh ;
\r
170 MEMO: vocab-file-contents ( vocab name -- seq )
\r
171 vocab-append-path dup
\r
172 [ dup exists? [ utf8 file-lines ] [ drop f ] if ] when ;
\r
174 : set-vocab-file-contents ( seq vocab name -- )
\r
175 dupd vocab-append-path [
\r
176 utf8 set-file-lines
\r
177 \ vocab-file-contents reset-memoized
\r
179 "The " swap vocab-name
\r
180 " vocabulary was not loaded from the file system"
\r
184 : vocab-summary-path ( vocab -- string )
\r
185 vocab-dir "summary.txt" append-path ;
\r
187 : vocab-summary ( vocab -- summary )
\r
188 dup dup vocab-summary-path vocab-file-contents
\r
190 vocab-name " vocabulary" append
\r
197 dup vocab-summary %
\r
199 words>> assoc-size #
\r
203 M: vocab-link summary vocab-summary ;
\r
205 : set-vocab-summary ( string vocab -- )
\r
207 dup vocab-summary-path
\r
208 set-vocab-file-contents ;
\r
210 : vocab-tags-path ( vocab -- string )
\r
211 vocab-dir "tags.txt" append-path ;
\r
213 : vocab-tags ( vocab -- tags )
\r
214 dup vocab-tags-path vocab-file-contents harvest ;
\r
216 : set-vocab-tags ( tags vocab -- )
\r
217 dup vocab-tags-path set-vocab-file-contents ;
\r
219 : add-vocab-tags ( tags vocab -- )
\r
220 [ vocab-tags append prune ] keep set-vocab-tags ;
\r
222 : vocab-authors-path ( vocab -- string )
\r
223 vocab-dir "authors.txt" append-path ;
\r
225 : vocab-authors ( vocab -- authors )
\r
226 dup vocab-authors-path vocab-file-contents harvest ;
\r
228 : set-vocab-authors ( authors vocab -- )
\r
229 dup vocab-authors-path set-vocab-file-contents ;
\r
231 : subdirs ( dir -- dirs )
\r
233 [ link-info directory? ] filter
\r
234 ] with-directory-files natural-sort ;
\r
236 : (all-child-vocabs) ( root name -- vocabs )
\r
238 vocab-dir append-path dup exists?
\r
239 [ subdirs ] [ drop { } ] if
\r
241 swap [ "." glue ] with map
\r
244 : vocabs-in-dir ( root name -- )
\r
245 dupd (all-child-vocabs) [
\r
246 2dup vocab-dir? [ dup >vocab-link , ] when
\r
250 : all-vocabs ( -- assoc )
\r
252 dup [ "" vocabs-in-dir ] { } make
\r
255 MEMO: all-vocabs-seq ( -- seq )
\r
256 all-vocabs values concat ;
\r
258 : unportable? ( name -- ? )
\r
259 vocab-tags "unportable" swap member? ;
\r
261 : filter-unportable ( seq -- seq' )
\r
262 [ vocab-name unportable? not ] filter ;
\r
264 : try-everything ( -- failures )
\r
269 : load-everything ( -- )
\r
270 try-everything load-failures. ;
\r
272 : unrooted-child-vocabs ( prefix -- seq )
\r
273 dup empty? [ CHAR: . suffix ] unless
\r
275 [ find-vocab-root not ] filter
\r
277 vocab-name swap ?head CHAR: . rot member? not and
\r
281 : all-child-vocabs ( prefix -- assoc )
\r
283 dup pick (all-child-vocabs) [ >vocab-link ] map
\r
285 swap unrooted-child-vocabs f swap 2array suffix ;
\r
287 : all-child-vocabs-seq ( prefix -- assoc )
\r
288 vocab-roots get swap [
\r
289 dupd (all-child-vocabs)
\r
290 [ vocab-dir? ] with filter
\r
291 ] curry map concat ;
\r
293 MEMO: all-tags ( -- seq )
\r
294 all-vocabs-seq [ vocab-tags ] gather natural-sort ;
\r
296 MEMO: all-authors ( -- seq )
\r
297 all-vocabs-seq [ vocab-authors ] gather natural-sort ;
\r
299 : reset-cache ( -- )
\r
300 root-cache get-global clear-assoc
\r
301 \ vocab-file-contents reset-memoized
\r
302 \ all-vocabs-seq reset-memoized
\r
303 \ all-authors reset-memoized
\r
304 \ all-tags reset-memoized ;
\r