2009-03-21 04:17:35 -04:00
|
|
|
! Copyright (C) 2007, 2009 Daniel Ehrenberg, Bruno Deferrari,
|
2008-12-17 19:10:01 -05:00
|
|
|
! Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: assocs hashtables kernel namespaces sequences
|
2009-05-14 23:31:29 -04:00
|
|
|
sets strings vocabs sorting accessors arrays compiler.units
|
|
|
|
combinators vectors splitting continuations ;
|
2008-12-17 19:10:01 -05:00
|
|
|
IN: vocabs.parser
|
|
|
|
|
|
|
|
ERROR: no-word-error name ;
|
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
TUPLE: manifest
|
|
|
|
current-vocab
|
|
|
|
{ search-vocabs vector }
|
|
|
|
{ qualified-vocabs vector }
|
|
|
|
{ extra-words vector } ;
|
2008-12-17 19:10:01 -05:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
: <manifest> ( -- manifest )
|
|
|
|
manifest new
|
|
|
|
V{ } clone >>search-vocabs
|
|
|
|
V{ } clone >>qualified-vocabs
|
|
|
|
V{ } clone >>extra-words ;
|
2008-12-17 19:10:01 -05:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
M: manifest clone
|
|
|
|
call-next-method
|
|
|
|
[ clone ] change-search-vocabs
|
|
|
|
[ clone ] change-qualified-vocabs
|
|
|
|
[ clone ] change-extra-words ;
|
2008-12-17 19:10:01 -05:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
<PRIVATE
|
2008-12-17 19:10:01 -05:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
: clear-manifest ( -- )
|
|
|
|
manifest get
|
|
|
|
[ search-vocabs>> delete-all ]
|
|
|
|
[ qualified-vocabs>> delete-all ]
|
2009-05-16 01:29:21 -04:00
|
|
|
bi ;
|
2008-12-17 19:10:01 -05:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
: (use-vocab) ( vocab -- vocab seq )
|
|
|
|
load-vocab manifest get search-vocabs>> ;
|
2008-12-17 19:10:01 -05:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
: (add-qualified) ( qualified -- )
|
|
|
|
manifest get qualified-vocabs>> push ;
|
|
|
|
|
|
|
|
: (from) ( vocab words -- vocab words words' assoc )
|
|
|
|
2dup swap load-vocab words>> ;
|
|
|
|
|
|
|
|
: extract-words ( seq assoc -- assoc' )
|
|
|
|
extract-keys dup [ [ drop ] [ no-word-error ] if ] assoc-each ;
|
|
|
|
|
|
|
|
: (lookup) ( name assoc -- word/f )
|
|
|
|
at dup forward-reference? [ drop f ] when ;
|
|
|
|
|
2009-05-16 01:29:21 -04:00
|
|
|
TUPLE: extra-words words ;
|
|
|
|
|
|
|
|
C: <extra-words> extra-words
|
|
|
|
|
|
|
|
: (use-words) ( assoc -- extra-words seq )
|
|
|
|
<extra-words> manifest get qualified-vocabs>> ;
|
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: set-current-vocab ( name -- )
|
2009-05-16 01:29:21 -04:00
|
|
|
create-vocab
|
|
|
|
[ manifest get (>>current-vocab) ]
|
|
|
|
[ words>> <extra-words> (add-qualified) ] bi ;
|
2009-05-14 23:31:29 -04:00
|
|
|
|
|
|
|
TUPLE: no-current-vocab ;
|
|
|
|
|
|
|
|
: no-current-vocab ( -- vocab )
|
|
|
|
\ no-current-vocab boa
|
|
|
|
{ { "Define words in scratchpad vocabulary" "scratchpad" } }
|
|
|
|
throw-restarts dup set-current-vocab ;
|
|
|
|
|
|
|
|
: current-vocab ( -- vocab )
|
|
|
|
manifest get current-vocab>> [ no-current-vocab ] unless* ;
|
|
|
|
|
|
|
|
: begin-private ( -- )
|
|
|
|
manifest get current-vocab>> vocab-name ".private" ?tail
|
|
|
|
[ drop ] [ ".private" append set-current-vocab ] if ;
|
|
|
|
|
|
|
|
: end-private ( -- )
|
|
|
|
manifest get current-vocab>> vocab-name ".private" ?tail
|
|
|
|
[ set-current-vocab ] [ drop ] if ;
|
|
|
|
|
|
|
|
: use-vocab ( vocab -- ) (use-vocab) push ;
|
|
|
|
|
|
|
|
: unuse-vocab ( vocab -- ) (use-vocab) delq ;
|
|
|
|
|
|
|
|
: only-use-vocabs ( vocabs -- )
|
|
|
|
clear-manifest
|
|
|
|
[ vocab ] V{ } map-as sift
|
|
|
|
manifest get search-vocabs>> push-all ;
|
|
|
|
|
|
|
|
TUPLE: qualified vocab prefix words ;
|
|
|
|
|
|
|
|
: <qualified> ( vocab prefix -- qualified )
|
|
|
|
2dup
|
|
|
|
[ load-vocab words>> ] [ CHAR: : suffix ] bi*
|
2008-12-17 19:10:01 -05:00
|
|
|
[ swap [ prepend ] dip ] curry assoc-map
|
2009-05-14 23:31:29 -04:00
|
|
|
qualified boa ;
|
2008-12-17 19:10:01 -05:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
: add-qualified ( vocab prefix -- )
|
|
|
|
<qualified> (add-qualified) ;
|
|
|
|
|
|
|
|
TUPLE: from vocab names words ;
|
|
|
|
|
|
|
|
: <from> ( vocab words -- from )
|
|
|
|
(from) extract-words from boa ;
|
2008-12-17 19:10:01 -05:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
: add-words-from ( vocab words -- )
|
|
|
|
<from> (add-qualified) ;
|
2009-05-13 23:15:48 -04:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
TUPLE: exclude vocab names words ;
|
2008-12-17 19:10:01 -05:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
: <exclude> ( vocab words -- from )
|
|
|
|
(from) [ nip ] [ extract-words ] 2bi assoc-diff exclude boa ;
|
2008-12-17 19:10:01 -05:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
: add-words-excluding ( vocab words -- )
|
|
|
|
<exclude> (add-qualified) ;
|
|
|
|
|
|
|
|
TUPLE: rename word vocab words ;
|
|
|
|
|
|
|
|
: <rename> ( word vocab new-name -- rename )
|
|
|
|
[ 2dup load-vocab words>> dupd at [ ] [ no-word-error ] ?if ] dip
|
|
|
|
associate rename boa ;
|
2008-12-17 19:10:01 -05:00
|
|
|
|
|
|
|
: add-renamed-word ( word vocab new-name -- )
|
2009-05-14 23:31:29 -04:00
|
|
|
<rename> (add-qualified) ;
|
|
|
|
|
2009-05-16 01:29:21 -04:00
|
|
|
: use-words ( assoc -- ) (use-words) push ;
|
2009-05-14 23:31:29 -04:00
|
|
|
|
2009-05-16 01:29:21 -04:00
|
|
|
: unuse-words ( assoc -- ) (use-words) delq ;
|
2009-05-14 23:31:29 -04:00
|
|
|
|
|
|
|
ERROR: ambiguous-use-error words ;
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: (vocab-search) ( name assocs -- words n )
|
|
|
|
[ words>> (lookup) ] with map
|
|
|
|
sift dup length ;
|
|
|
|
|
|
|
|
: vocab-search ( name manifest -- word/f )
|
|
|
|
search-vocabs>>
|
|
|
|
(vocab-search) {
|
|
|
|
{ 0 [ drop f ] }
|
|
|
|
{ 1 [ first ] }
|
|
|
|
[ drop ambiguous-use-error ]
|
|
|
|
} case ;
|
|
|
|
|
|
|
|
: qualified-search ( name manifest -- word/f )
|
|
|
|
qualified-vocabs>>
|
|
|
|
(vocab-search) 0 = [ drop f ] [ peek ] if ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: search-manifest ( name manifest -- word/f )
|
2009-05-16 01:29:21 -04:00
|
|
|
2dup qualified-search dup [ 2nip ] [ drop vocab-search ] if ;
|
2009-05-13 23:15:48 -04:00
|
|
|
|
2009-05-14 23:31:29 -04:00
|
|
|
: search ( name -- word/f )
|
|
|
|
manifest get search-manifest ;
|
|
|
|
|
|
|
|
: word-restarts ( name possibilities -- restarts )
|
|
|
|
natural-sort
|
|
|
|
[ [ vocabulary>> "Use the " " vocabulary" surround ] keep ] { } map>assoc
|
|
|
|
swap "Defer word in current vocabulary" swap 2array
|
|
|
|
suffix ;
|
|
|
|
|
|
|
|
: <no-word-error> ( name possibilities -- error restarts )
|
|
|
|
[ drop \ no-word-error boa ] [ word-restarts ] 2bi ;
|