]> gitweb.factorcode.org Git - factor.git/blob - core/vocabs/loader/loader.factor
io.pathnames: Allow vocab: to find paths that do not exist as long as the vocab exists.
[factor.git] / core / vocabs / loader / loader.factor
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 ;
6 IN: vocabs.loader
7
8 SYMBOL: vocab-roots
9
10 SYMBOL: add-vocab-root-hook
11
12 CONSTANT: default-vocab-roots {
13     "resource:core"
14     "resource:basis"
15     "resource:extra"
16     "resource:work"
17 }
18
19 [
20     default-vocab-roots V{ } like vocab-roots set-global
21
22     [ drop ] add-vocab-root-hook set-global
23 ] "vocabs.loader" add-startup-hook
24
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 ;
28
29 SYMBOL: root-cache
30 root-cache [ H{ } clone ] initialize
31
32 ERROR: not-found-in-roots path ;
33
34 <PRIVATE
35
36 : find-root-for ( path -- path/f )
37     vocab-roots get [ prepend-path file-exists? ] with find nip ;
38
39 : find-root-for-vocab-pathname ( path -- path/f )
40     dup find-root-for [ prepend-path ] [ not-found-in-roots ] if* ;
41
42 : ensure-parent-directory-is-not-dot ( path -- parent-directory )
43     dup parent-directory dup "." =
44     [ drop not-found-in-roots ]
45     [ nip ] if ;
46
47 M: string vocab-path
48     {
49         { [ dup ?last path-separator? ] [ find-root-for-vocab-pathname ] }
50         { [ dup has-file-extension? ] [
51             [ ensure-parent-directory-is-not-dot find-root-for-vocab-pathname ]
52             [ file-name ] bi append-path
53         ] }
54         [ find-root-for-vocab-pathname ]
55     } cond ;
56
57 PRIVATE>
58
59 : vocab-dir ( vocab -- dir )
60     vocab-name H{ { CHAR: . CHAR: / } } substitute ;
61
62 : append-vocab-dir ( vocab str/f -- path )
63     [ vocab-name "." split ] dip
64     [ [ dup last ] dip append suffix ] when*
65     "/" join ;
66
67 : find-vocab-root ( vocab -- path/f )
68     vocab-name dup ".private" tail? [ drop f ] [
69         root-cache get 2dup at [ 2nip ] [
70             over ".factor" append-vocab-dir find-root-for
71             [ [ -rot set-at ] [ 2drop ] if* ] keep
72         ] if*
73     ] if ;
74
75 : vocab-exists? ( name -- ? )
76     dup lookup-vocab [ ] [ find-vocab-root ] ?if ;
77
78 : vocab-append-path ( vocab path -- newpath )
79     swap find-vocab-root [ prepend-path ] [ drop f ] if* ;
80
81 : vocab-source-path ( vocab -- path/f )
82     vocab-name ".private" ?tail drop
83     dup ".factor" append-vocab-dir vocab-append-path ;
84
85 : vocab-docs-path ( vocab -- path/f )
86     vocab-name ".private" ?tail drop
87     dup "-docs.factor" append-vocab-dir vocab-append-path ;
88
89 SYMBOL: load-help?
90
91 ! Defined by vocabs.metadata
92 SYMBOL: check-vocab-hook
93 check-vocab-hook [ [ drop ] ] initialize
94
95 <PRIVATE
96
97 SYMBOL: require-when-vocabs
98 require-when-vocabs [ HS{ } clone ] initialize
99
100 SYMBOL: require-when-table
101 require-when-table [ V{ } clone ] initialize
102
103 : load-conditional-requires ( vocab -- )
104     vocab-name require-when-vocabs get in? [
105         require-when-table get [
106             [ [ lookup-vocab dup [ source-loaded?>> +done+ = ] when ] all? ] dip
107             [ require ] curry when
108         ] assoc-each
109     ] when ;
110
111 : load-source ( vocab -- )
112     dup check-vocab-hook get call( vocab -- )
113     [
114         +parsing+ >>source-loaded?
115         dup vocab-source-path [ parse-file ] [ [ ] ] if*
116         [ +parsing+ >>source-loaded? ] dip
117         [ % ] [ call( -- ) ] if-bootstrapping
118         +done+ >>source-loaded?
119         load-conditional-requires
120     ] [ ] [ f >>source-loaded? ] cleanup ;
121
122 : load-docs ( vocab -- )
123     load-help? get [
124         [
125             +parsing+ >>docs-loaded?
126             dup vocab-docs-path [ ?run-file ] when*
127             +done+ >>docs-loaded?
128         ] [ ] [ f >>docs-loaded? ] cleanup
129     ] when drop ;
130
131 PRIVATE>
132
133 : require-when ( if then -- )
134     over [ lookup-vocab ] all? [
135         require drop
136     ] [
137         [ drop require-when-vocabs get adjoin-all ]
138         [ 2array require-when-table get push ] 2bi
139     ] if ;
140
141 : reload ( name -- )
142     dup lookup-vocab
143     [ [ load-source ] [ load-docs ] bi ]
144     [ require ]
145     ?if ;
146
147 : run ( vocab -- )
148     dup load-vocab vocab-main [
149         execute( -- )
150     ] [
151         "The " write vocab-name write
152         " vocabulary does not define an entry point." print
153         "To define one, refer to \\ MAIN: help" print
154     ] ?if ;
155
156 SYMBOL: errorlist
157
158 <PRIVATE
159
160 : add-to-errorlist ( error vocab -- )
161     vocab-name errorlist get [ set-at ] [ 2drop ] if* ;
162
163 : remove-from-errorlist ( vocab -- )
164     vocab-name errorlist get [ delete-at ] [ drop ] if* ;
165
166 GENERIC: (require) ( name -- )
167
168 M: vocab (require)
169     [
170         [
171             dup source-loaded?>> +parsing+ eq? [ drop ] [
172                 dup source-loaded?>> [ dup load-source ] unless
173                 dup docs-loaded?>> [ dup load-docs ] unless
174                 drop
175             ] if
176         ] [
177             remove-from-errorlist
178         ] bi
179     ] [ [ swap add-to-errorlist ] keep rethrow ] recover ;
180
181 M: vocab-link (require)
182     vocab-name (require) ;
183
184 M: string (require)
185     dup check-vocab-hook get call( vocab -- )
186     create-vocab (require) ;
187
188 PRIVATE>
189
190 : require-all ( vocabs -- )
191     V{ } clone errorlist [ [ require ] each ] with-variable ;
192
193 [
194     dup vocab-name errorlist get at*
195     [ rethrow ]
196     [
197         drop dup find-vocab-root
198         [ (require) ]
199         [ dup lookup-vocab [ drop ] [ no-vocab ] if ]
200         if
201     ] if
202 ] require-hook set-global
203
204 M: vocab-spec where vocab-source-path dup [ 1 2array ] when ;
205
206 ! put here to avoid circularity between vocabs.loader and source-files.errors
207 { "source-files.errors" "debugger" } "source-files.errors.debugger" require-when