1 ! Copyright (C) 2007, 2009 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 ;
62 : load-source ( vocab -- )
64 +parsing+ >>source-loaded?
65 dup vocab-source-path [ parse-file ] [ [ ] ] if*
66 [ +parsing+ >>source-loaded? ] dip
67 [ % ] [ call( -- ) ] if-bootstrapping
68 +done+ >>source-loaded? drop
69 ] [ ] [ f >>source-loaded? ] cleanup ;
71 : load-docs ( vocab -- )
74 +parsing+ >>docs-loaded?
75 [ vocab-docs-path [ ?run-file ] when* ] keep
77 ] [ ] [ f >>docs-loaded? ] cleanup
82 : require ( vocab -- )
87 [ [ load-source ] [ load-docs ] bi ]
92 dup load-vocab vocab-main [
95 "The " write vocab-name write
96 " vocabulary does not define an entry point." print
97 "To define one, refer to \\ MAIN: help" print
102 ! Defined by vocabs.metadata
103 SYMBOL: check-vocab-hook
105 check-vocab-hook [ [ drop ] ] initialize
109 : add-to-blacklist ( error vocab -- )
110 vocab-name blacklist get dup [ set-at ] [ 3drop ] if ;
112 GENERIC: (load-vocab) ( name -- vocab )
114 M: vocab (load-vocab)
116 dup source-loaded?>> +parsing+ eq? [
117 dup source-loaded?>> [ dup load-source ] unless
118 dup docs-loaded?>> [ dup load-docs ] unless
120 ] [ [ swap add-to-blacklist ] keep rethrow ] recover ;
122 M: vocab-link (load-vocab)
123 vocab-name (load-vocab) ;
125 M: string (load-vocab)
126 [ check-vocab-hook get call( vocab -- ) ]
127 [ create-vocab (load-vocab) ]
133 dup vocab-name blacklist get at* [ rethrow ] [
134 drop dup find-vocab-root
135 [ (load-vocab) ] [ dup vocab [ ] [ no-vocab ] ?if ] if
137 ] load-vocab-hook set-global
139 M: vocab-spec where vocab-source-path dup [ 1 2array ] when ;