]> gitweb.factorcode.org Git - factor.git/blob - core/vocabs/loader/loader.factor
vocabs: change from errorlist to requiring list
[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 ! If path exists use it, otherwise try to find a vocab that exists
48 M: string vocab-path
49     dup find-root-for [
50         prepend-path
51     ] [
52         {
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
57             ] }
58             [ find-root-for-vocab-pathname ]
59         } cond
60     ] if* ;
61
62 PRIVATE>
63
64 : vocab-dir ( vocab -- dir )
65     vocab-name H{ { CHAR: . CHAR: / } } substitute ;
66
67 : append-vocab-dir ( vocab str/f -- path )
68     [ vocab-name "." split ] dip
69     [ [ dup last ] dip append suffix ] when*
70     "/" join ;
71
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
77         ] if*
78     ] if ;
79
80 : vocab-exists? ( name -- ? )
81     dup lookup-vocab [ ] [ find-vocab-root ] ?if ;
82
83 : vocab-append-path ( vocab path -- newpath )
84     swap find-vocab-root [ prepend-path ] [ drop f ] if* ;
85
86 : vocab-source-path ( vocab -- path/f )
87     vocab-name ".private" ?tail drop
88     dup ".factor" append-vocab-dir vocab-append-path ;
89
90 : vocab-docs-path ( vocab -- path/f )
91     vocab-name ".private" ?tail drop
92     dup "-docs.factor" append-vocab-dir vocab-append-path ;
93
94 SYMBOL: load-help?
95
96 ! Defined by vocabs.metadata
97 SYMBOL: check-vocab-hook
98 check-vocab-hook [ [ drop ] ] initialize
99
100 <PRIVATE
101
102 SYMBOL: require-when-vocabs
103 require-when-vocabs [ HS{ } clone ] initialize
104
105 SYMBOL: require-when-table
106 require-when-table [ V{ } clone ] initialize
107
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
113         ] assoc-each
114     ] when ;
115
116 : load-source ( vocab -- )
117     dup check-vocab-hook get call( vocab -- )
118     [
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 ;
126
127 : load-docs ( vocab -- )
128     load-help? get [
129         [
130             +parsing+ >>docs-loaded?
131             dup vocab-docs-path [ ?run-file ] when*
132             +done+ >>docs-loaded?
133         ] [ ] [ f >>docs-loaded? ] cleanup
134     ] when drop ;
135
136 PRIVATE>
137
138 : require-when ( if then -- )
139     over [ lookup-vocab ] all? [
140         require drop
141     ] [
142         [ drop require-when-vocabs get adjoin-all ]
143         [ 2array require-when-table get push ] 2bi
144     ] if ;
145
146 : reload ( name -- )
147     dup lookup-vocab [
148         f >>source-loaded? f >>docs-loaded? drop
149     ] when* require ;
150
151 : run ( vocab -- )
152     dup load-vocab vocab-main [
153         execute( -- )
154     ] [
155         "The " write vocab-name write
156         " vocabulary does not define an entry point." print
157         "To define one, refer to \\ MAIN: help" print
158     ] ?if ;
159
160 <PRIVATE
161
162 GENERIC: (require) ( name -- )
163
164 M: vocab (require)
165     dup source-loaded?>> +parsing+ eq? [
166         dup source-loaded?>> [ dup load-source ] unless
167         dup docs-loaded?>> [ dup load-docs ] unless
168     ] unless drop ;
169
170 M: vocab-link (require)
171     vocab-name (require) ;
172
173 M: string (require)
174     dup check-vocab-hook get call( vocab -- )
175     create-vocab (require) ;
176
177 PRIVATE>
178
179 [
180     dup find-vocab-root
181     [ (require) ]
182     [ dup lookup-vocab [ drop ] [ no-vocab ] if ]
183     if
184 ] require-hook set-global
185
186 M: vocab-spec where vocab-source-path dup [ 1 2array ] when ;
187
188 ! put here to avoid circularity between vocabs.loader and source-files.errors
189 { "source-files.errors" "debugger" } "source-files.errors.debugger" require-when