1 ! Copyright (C) 2007, 2010 Eduardo Cavazos, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators continuations
4 definitions init io io.files io.pathnames kernel make namespaces
5 parser sequences sets splitting strings vocabs words ;
10 SYMBOL: add-vocab-root-hook
12 CONSTANT: default-vocab-roots {
20 default-vocab-roots V{ } like vocab-roots set-global
22 [ drop ] add-vocab-root-hook set-global
23 ] "vocabs.loader" add-startup-hook
25 : add-vocab-root ( root -- )
26 trim-tail-separators dup vocab-roots get ?adjoin
27 [ add-vocab-root-hook get-global call( root -- ) ] [ drop ] if ;
30 root-cache [ H{ } clone ] initialize
32 ERROR: not-found-in-roots path ;
36 : find-root-for ( path -- path/f )
37 vocab-roots get [ prepend-path file-exists? ] with find nip ;
39 : find-root-for-vocab-pathname ( path -- path/f )
40 dup find-root-for [ prepend-path ] [ not-found-in-roots ] if* ;
42 : ensure-parent-directory-is-not-dot ( path -- parent-directory )
43 dup parent-directory dup "." =
44 [ drop not-found-in-roots ]
47 ! If path exists use it, otherwise try to find a vocab that exists
53 { [ dup ?last path-separator? ] [ find-root-for-vocab-pathname ] }
54 { [ dup has-file-extension? ] [
55 [ ensure-parent-directory-is-not-dot find-root-for-vocab-pathname ]
56 [ file-name ] bi append-path
58 [ find-root-for-vocab-pathname ]
64 : vocab-dir ( vocab -- dir )
65 vocab-name H{ { CHAR: . CHAR: / } } substitute ;
67 : append-vocab-dir ( vocab str/f -- path )
68 [ vocab-name "." split ] dip
69 [ [ dup last ] dip append suffix ] when*
72 : find-vocab-root ( vocab -- path/f )
73 vocab-name dup ".private" tail? [ drop f ] [
74 root-cache get 2dup at [ 2nip ] [
75 over ".factor" append-vocab-dir find-root-for
76 [ [ -rot set-at ] [ 2drop ] if* ] keep
80 : vocab-exists? ( name -- ? )
81 dup lookup-vocab [ ] [ find-vocab-root ] ?if ;
83 : vocab-append-path ( vocab path -- newpath )
84 swap find-vocab-root [ prepend-path ] [ drop f ] if* ;
86 : vocab-source-path ( vocab -- path/f )
87 vocab-name ".private" ?tail drop
88 dup ".factor" append-vocab-dir vocab-append-path ;
90 : vocab-docs-path ( vocab -- path/f )
91 vocab-name ".private" ?tail drop
92 dup "-docs.factor" append-vocab-dir vocab-append-path ;
96 ! Defined by vocabs.metadata
97 SYMBOL: check-vocab-hook
98 check-vocab-hook [ [ drop ] ] initialize
102 SYMBOL: require-when-vocabs
103 require-when-vocabs [ HS{ } clone ] initialize
105 SYMBOL: require-when-table
106 require-when-table [ V{ } clone ] initialize
108 : load-conditional-requires ( vocab -- )
109 vocab-name require-when-vocabs get in? [
110 require-when-table get [
111 [ [ lookup-vocab dup [ source-loaded?>> +done+ = ] when ] all? ] dip
112 [ require ] curry when
116 : load-source ( vocab -- )
117 dup check-vocab-hook get call( vocab -- )
119 +parsing+ >>source-loaded?
120 dup vocab-source-path [ parse-file ] [ [ ] ] if*
121 [ +parsing+ >>source-loaded? ] dip
122 [ % ] [ call( -- ) ] if-bootstrapping
123 +done+ >>source-loaded?
124 load-conditional-requires
125 ] [ ] [ f >>source-loaded? ] cleanup ;
127 : load-docs ( vocab -- )
130 +parsing+ >>docs-loaded?
131 dup vocab-docs-path [ ?run-file ] when*
132 +done+ >>docs-loaded?
133 ] [ ] [ f >>docs-loaded? ] cleanup
138 : require-when ( if then -- )
139 over [ lookup-vocab ] all? [
142 [ drop require-when-vocabs get adjoin-all ]
143 [ 2array require-when-table get push ] 2bi
148 [ [ load-source ] [ load-docs ] bi ]
153 dup load-vocab vocab-main [
156 "The " write vocab-name write
157 " vocabulary does not define an entry point." print
158 "To define one, refer to \\ MAIN: help" print
165 : add-to-errorlist ( error vocab -- )
166 vocab-name errorlist get [ set-at ] [ 2drop ] if* ;
168 : remove-from-errorlist ( vocab -- )
169 vocab-name errorlist get [ delete-at ] [ drop ] if* ;
171 GENERIC: (require) ( name -- )
176 dup source-loaded?>> +parsing+ eq? [ drop ] [
177 dup source-loaded?>> [ dup load-source ] unless
178 dup docs-loaded?>> [ dup load-docs ] unless
182 remove-from-errorlist
184 ] [ [ swap add-to-errorlist ] keep rethrow ] recover ;
186 M: vocab-link (require)
187 vocab-name (require) ;
190 dup check-vocab-hook get call( vocab -- )
191 create-vocab (require) ;
195 : require-all ( vocabs -- )
196 V{ } clone errorlist [ [ require ] each ] with-variable ;
199 dup vocab-name errorlist get at*
202 drop dup find-vocab-root
204 [ dup lookup-vocab [ drop ] [ no-vocab ] if ]
207 ] require-hook set-global
209 M: vocab-spec where vocab-source-path dup [ 1 2array ] when ;
211 ! put here to avoid circularity between vocabs.loader and source-files.errors
212 { "source-files.errors" "debugger" } "source-files.errors.debugger" require-when