1 ! Copyright (C) 2007, 2008 Eduardo Cavazos, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs strings kernel sorting namespaces
4 sequences definitions ;
9 TUPLE: vocab < identity-tuple
12 source-loaded? docs-loaded? ;
14 ! sources-loaded? slot is one of these three
19 : <vocab> ( name -- vocab )
24 GENERIC: vocab-name ( vocab-spec -- name )
26 GENERIC: vocab ( vocab-spec -- vocab )
30 M: object vocab ( name -- vocab ) vocab-name dictionary get at ;
32 M: vocab vocab-name name>> ;
34 M: string vocab-name ;
36 GENERIC: vocab-words ( vocab-spec -- words )
38 M: vocab vocab-words words>> ;
40 M: object vocab-words vocab vocab-words ;
44 GENERIC: vocab-help ( vocab-spec -- help )
46 M: vocab vocab-help help>> ;
48 M: object vocab-help vocab vocab-help ;
52 GENERIC: vocab-main ( vocab-spec -- main )
54 M: vocab vocab-main main>> ;
56 M: object vocab-main vocab vocab-main ;
60 : create-vocab ( name -- vocab )
61 dictionary get [ <vocab> ] cache ;
63 ERROR: no-vocab name ;
65 SYMBOL: load-vocab-hook ! ( name -- )
67 : load-vocab ( name -- vocab )
68 dup load-vocab-hook get call vocab ;
71 dictionary get keys natural-sort ;
73 : words ( vocab -- seq )
76 : all-words ( -- seq )
77 dictionary get values [ words ] map concat ;
79 : words-named ( str -- seq )
81 [ vocab-words at ] with map
84 : child-vocab? ( prefix name -- ? )
86 [ 2drop t ] [ swap CHAR: . suffix head? ] if ;
88 : child-vocabs ( vocab -- seq )
89 vocab-name vocabs [ child-vocab? ] with filter ;
91 TUPLE: vocab-link name ;
93 : <vocab-link> ( name -- vocab-link )
96 M: vocab-link hashcode* name>> hashcode* ;
98 M: vocab-link vocab-name name>> ;
100 UNION: vocab-spec vocab vocab-link ;
102 GENERIC: >vocab-link ( name -- vocab )
104 M: vocab-spec >vocab-link ;
106 M: string >vocab-link dup vocab [ ] [ <vocab-link> ] ?if ;
108 : forget-vocab ( vocab -- )
110 vocab-name dictionary get delete-at ;
112 M: vocab-spec forget* forget-vocab ;