1 ! Copyright (C) 2007, 2010 Eduardo Cavazos, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces make sequences io io.files io.pathnames kernel
4 assocs words vocabs definitions parser continuations hashtables
5 sorting source-files arrays combinators strings system
6 math.parser splitting init accessors sets ;
16 } clone vocab-roots set-global
18 : add-vocab-root ( root -- )
19 vocab-roots get adjoin ;
23 root-cache [ H{ } clone ] initialize
25 ERROR: not-found-in-roots path ;
29 : find-root-for ( path -- path/f )
30 vocab-roots get [ prepend-path exists? ] with find nip ;
32 M: string vocab-path ( string -- path/f )
33 dup find-root-for [ prepend-path ] [ not-found-in-roots ] if* ;
37 : vocab-dir ( vocab -- dir )
38 vocab-name { { CHAR: . CHAR: / } } substitute ;
40 : vocab-dir+ ( vocab str/f -- path )
41 [ vocab-name "." split ] dip
42 [ [ dup last ] dip append suffix ] when*
45 : find-vocab-root ( vocab -- path/f )
46 vocab-name dup root-cache get at
47 [ ] [ ".factor" vocab-dir+ find-root-for ] ?if ;
49 : vocab-append-path ( vocab path -- newpath )
50 swap find-vocab-root dup [ prepend-path ] [ 2drop f ] if ;
52 : vocab-source-path ( vocab -- path/f )
53 dup ".factor" vocab-dir+ vocab-append-path ;
55 : vocab-docs-path ( vocab -- path/f )
56 dup "-docs.factor" vocab-dir+ vocab-append-path ;
60 ! Defined by vocabs.metadata
61 SYMBOL: check-vocab-hook
63 check-vocab-hook [ [ drop ] ] initialize
67 : load-source ( vocab -- )
68 dup check-vocab-hook get call( vocab -- )
70 +parsing+ >>source-loaded?
71 dup vocab-source-path [ parse-file ] [ [ ] ] if*
72 [ +parsing+ >>source-loaded? ] dip
73 [ % ] [ call( -- ) ] if-bootstrapping
74 +done+ >>source-loaded? drop
75 ] [ ] [ f >>source-loaded? ] cleanup ;
77 : load-docs ( vocab -- )
80 +parsing+ >>docs-loaded?
81 [ vocab-docs-path [ ?run-file ] when* ] keep
83 ] [ ] [ f >>docs-loaded? ] cleanup
88 : require ( vocab -- )
93 [ [ load-source ] [ load-docs ] bi ]
98 dup load-vocab vocab-main [
101 "The " write vocab-name write
102 " vocabulary does not define an entry point." print
103 "To define one, refer to \\ MAIN: help" print
110 : add-to-blacklist ( error vocab -- )
111 vocab-name blacklist get dup [ set-at ] [ 3drop ] if ;
113 GENERIC: (load-vocab) ( name -- vocab )
115 M: vocab (load-vocab)
117 dup source-loaded?>> +parsing+ eq? [
118 dup source-loaded?>> [ dup load-source ] unless
119 dup docs-loaded?>> [ dup load-docs ] unless
121 ] [ [ swap add-to-blacklist ] keep rethrow ] recover ;
123 M: vocab-link (load-vocab)
124 vocab-name (load-vocab) ;
126 M: string (load-vocab) create-vocab (load-vocab) ;
131 dup vocab-name blacklist get at* [ rethrow ] [
132 drop dup find-vocab-root
133 [ (load-vocab) ] [ dup vocab [ ] [ no-vocab ] ?if ] if
135 ] load-vocab-hook set-global
137 M: vocab-spec where vocab-source-path dup [ 1 2array ] when ;