Add error checking for append-vocab-dir and (child-vocabs) in case someone tries to pass them the wrong parameters.
Rename: vocab-dir+ to append-vocab-dir
Document load-from-root and load-root
[ "Hi" ] [ "Hi" present ] unit-test
[ "+" ] [ \ + present ] unit-test
[ "kernel" ] [ "kernel" vocab present ] unit-test
-[ ] [ all-vocabs-recursive no-roots no-prefixes [ present ] map drop ] unit-test
\ No newline at end of file
+[ ] [ all-vocabs-recursive filter-vocabs [ present ] map drop ] unit-test
all-words name-completions ;
: vocabs-matching ( str -- seq )
- all-vocabs-recursive no-roots no-prefixes name-completions ;
+ all-vocabs-recursive filter-vocabs name-completions ;
: chars-matching ( str -- seq )
name-map keys dup zip completions ;
IN: vocabs.files
: vocab-tests-file ( vocab -- path )
- dup "-tests.factor" vocab-dir+ vocab-append-path dup
+ dup "-tests.factor" append-vocab-dir vocab-append-path dup
[ dup exists? [ drop f ] unless ] [ drop f ] if ;
: vocab-tests-dir ( vocab -- paths )
-USING: help.markup help.syntax strings vocabs.loader ;\r
+USING: help.markup help.syntax strings vocabs.loader\r
+sequences ;\r
IN: vocabs.hierarchy\r
\r
ARTICLE: "vocabs.hierarchy" "Vocabulary hierarchy tools"\r
-"These tools operate on all vocabularies found in the current set of " { $link vocab-roots } ", loaded or not."\r
+"These tools operate on all vocabularies found in the current set of " { $link vocab-roots } ", loaded or not. A prefix is the first part of a vocabulary name."\r
$nl\r
"Loading vocabulary hierarchies:"\r
{ $subsections\r
load\r
load-all\r
+ load-root\r
+ load-from-root\r
}\r
"Getting all vocabularies from disk:"\r
{ $subsections\r
{ $subsections\r
no-roots\r
no-prefixes\r
+ filter-vocabs\r
}\r
"Getting " { $link "vocabs.metadata" } " for all vocabularies from disk:"\r
{ $subsections\r
HELP: load-all\r
{ $description "Load all vocabularies in the source tree." } ;\r
\r
+HELP: load-from-root\r
+{ $values\r
+ { "root" "a vocaulary root" } { "prefix" string }\r
+}\r
+{ $description "Attempts to load all of the vocabularies with a certain prefix from a vocabulary root." } ;\r
+\r
+HELP: load-root\r
+{ $values\r
+ { "root" "a vocabulary root" }\r
+}\r
+{ $description "Attempts to load all of the vocabularies in a vocabulary root." } ;\r
\r
: vocab-dir? ( root name -- ? )\r
over\r
- [ ".factor" vocab-dir+ append-path exists? ]\r
+ [ ".factor" append-vocab-dir append-path exists? ]\r
[ 2drop f ]\r
if ;\r
\r
+ERROR: vocab-root-required root ;\r
+\r
+: ensure-vocab-root ( root -- root )\r
+ dup vocab-roots get member? [ vocab-root-required ] unless ;\r
+\r
+: ensure-vocab-root/prefix ( root prefix -- root prefix )\r
+ [ ensure-vocab-root ] [ forbid-absolute-path ] bi* ;\r
+\r
: (child-vocabs) ( root prefix -- vocabs )\r
+ ensure-vocab-root/prefix\r
[ vocab-dir append-path dup exists? [ vocab-subdirs ] [ drop { } ] if ]\r
[ nip [ '[ [ _ "." ] dip 3append ] map ] unless-empty ]\r
[ drop '[ _ over vocab-dir? [ >vocab-link ] [ <vocab-prefix> ] if ] map ]\r
2tri ;\r
\r
-: ((child-vocabs-recursive)) ( root name -- )\r
+: ((child-vocabs-recursive)) ( root prefix -- )\r
dupd vocab-name (child-vocabs)\r
[ dup , ((child-vocabs-recursive)) ] with each ;\r
\r
-: (child-vocabs-recursive) ( root name -- seq )\r
+: (child-vocabs-recursive) ( root prefix -- seq )\r
[ ((child-vocabs-recursive)) ] { } make ;\r
\r
: no-rooted ( seq -- seq' ) [ find-vocab-root not ] filter ;\r
\r
: no-roots ( assoc -- seq ) values concat ;\r
\r
+: filter-vocabs ( assoc -- seq )\r
+ no-roots no-prefixes members ;\r
+\r
: child-vocabs ( prefix -- assoc )\r
[ [ vocab-roots get ] dip '[ dup _ (child-vocabs) ] { } map>assoc ]\r
[ unrooted-child-vocabs [ vocab ] map f swap 2array ]\r
"" child-vocabs-recursive ;\r
\r
: all-vocab-names ( -- seq )\r
- all-vocabs-recursive no-roots no-prefixes [ vocab-name ] map ;\r
+ all-vocabs-recursive filter-vocabs [ vocab-name ] map ;\r
\r
: child-vocab-names ( prefix -- seq )\r
- child-vocabs no-roots no-prefixes [ vocab-name ] map ;\r
+ child-vocabs filter-vocabs [ vocab-name ] map ;\r
\r
<PRIVATE\r
\r
: collect-vocabs ( quot -- seq )\r
- [ all-vocabs-recursive no-roots no-prefixes ] dip\r
+ [ all-vocabs-recursive filter-vocabs ] dip\r
gather natural-sort ; inline\r
\r
+: maybe-include-root/prefix ( root prefix -- vocab-link/f )\r
+ over [\r
+ [ find-vocab-root = ] keep swap\r
+ ] [\r
+ nip dup find-vocab-root\r
+ ] if [ >vocab-link ] [ drop f ] if ;\r
+\r
PRIVATE>\r
\r
-: (load) ( prefix -- failures )\r
- [ child-vocabs-recursive no-roots no-prefixes ]\r
- [ dup find-vocab-root [ >vocab-link prefix ] [ drop ] if ] bi\r
- filter-don't-load\r
+: vocabs-in-root/prefix ( root prefix -- seq )\r
+ [ (child-vocabs-recursive) ]\r
+ [ maybe-include-root/prefix [ prefix ] when* ] 2bi ;\r
+\r
+: vocabs-in-root ( root -- seq )\r
+ "" vocabs-in-root/prefix ;\r
+\r
+: (load-from-root) ( root prefix -- failures )\r
+ vocabs-in-root/prefix\r
+ [ don't-load? not ] filter no-prefixes\r
require-all ;\r
\r
+: load-from-root ( root prefix -- )\r
+ (load-from-root) load-failures. ;\r
+\r
+: load-root ( root -- )\r
+ "" load-from-root ;\r
+\r
+: (load) ( prefix -- failures )\r
+ [ vocab-roots get ] dip '[ _ (load-from-root) ] map concat ;\r
+\r
: load ( prefix -- )\r
- (load) load-failures. ;\r
+ (load) [ load-failures. ] each ;\r
\r
: load-all ( -- )\r
"" load ;\r
: vocab-dir ( vocab -- dir )
vocab-name { { CHAR: . CHAR: / } } substitute ;
-: vocab-dir+ ( vocab str/f -- path )
- [ vocab-name "." split ] dip
+ERROR: absolute-path-forbidden path ;
+
+: forbid-absolute-path ( str -- str )
+ dup absolute-path? [ absolute-path-forbidden ] when ;
+
+: append-vocab-dir ( vocab str/f -- path )
+ [ vocab-name forbid-absolute-path "." split ] dip
[ [ dup last ] dip append suffix ] when*
"/" join ;
: find-vocab-root ( vocab -- path/f )
vocab-name dup root-cache get at
- [ ] [ ".factor" vocab-dir+ find-root-for ] ?if ;
+ [ ] [ ".factor" append-vocab-dir find-root-for ] ?if ;
: vocab-append-path ( vocab path -- newpath )
swap find-vocab-root dup [ prepend-path ] [ 2drop f ] if ;
: vocab-source-path ( vocab -- path/f )
- dup ".factor" vocab-dir+ vocab-append-path ;
+ dup ".factor" append-vocab-dir vocab-append-path ;
: vocab-docs-path ( vocab -- path/f )
- dup "-docs.factor" vocab-dir+ vocab-append-path ;
+ dup "-docs.factor" append-vocab-dir vocab-append-path ;
SYMBOL: load-help?
[ t ] [ "" "io.files" child-vocab? ] unit-test
[ t ] [ "io" "io.files" child-vocab? ] unit-test
[ f ] [ "io.files" "io" child-vocab? ] unit-test
-
-[ t ] [ "io.files" "io" parent-vocab? ] unit-test
-[ f ] [ "io" "io.files" parent-vocab? ] unit-test
: child-vocabs ( vocab -- seq )
vocab-name vocabs [ child-vocab? ] with filter ;
-: parent-vocab? ( suffix name -- ? )
- swap child-vocab? ;
-
-: parent-vocabs ( vocab -- seq )
- vocab-name vocabs [ parent-vocab? ] with filter ;
-
GENERIC: >vocab-link ( name -- vocab )
M: vocab-spec >vocab-link ;
all-words [ name>> ] map ;
: vocab-names ( -- strs )
- all-vocabs-recursive no-roots no-prefixes [ name>> ] map ;
+ all-vocabs-recursive filter-vocabs [ name>> ] map ;
: prefixed-words ( prefix -- words )
'[ _ head? ] word-names swap filter ;