factor/library/tools/word-tools.factor

110 lines
3.3 KiB
Factor
Raw Normal View History

! :folding=indent:collapseFolds=1:
2004-07-16 02:26:21 -04:00
! $Id$
!
! Copyright (C) 2003, 2004 Slava Pestov.
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
! this list of conditions and the following disclaimer in the documentation
! and/or other materials provided with the distribution.
!
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IN: words
USE: generic
2004-07-16 02:26:21 -04:00
USE: inspector
USE: lists
USE: kernel
USE: namespaces
USE: prettyprint
2004-07-16 02:26:21 -04:00
USE: stdio
USE: strings
USE: unparser
USE: math
2005-01-17 15:33:12 -05:00
USE: hashtables
2004-07-16 02:26:21 -04:00
2005-01-17 15:33:12 -05:00
GENERIC: word-uses? ( of in -- ? )
M: word word-uses? 2drop f ;
M: compound word-uses? ( of in -- ? )
2004-07-16 02:26:21 -04:00
2dup = [
2drop f ! Don't say that a word uses itself
] [
word-parameter tree-contains?
2004-07-16 02:26:21 -04:00
] ifte ;
2005-01-17 15:33:12 -05:00
M: generic word-uses? ( of in -- ? )
"methods" word-property hash>alist tree-contains? ;
2004-07-16 02:26:21 -04:00
: usages-in-vocab ( of vocab -- usages )
#! Push a list of all usages of a word in a vocabulary.
words [
2004-10-01 22:25:19 -04:00
dup compound? [
2004-07-16 02:26:21 -04:00
dupd word-uses?
] [
drop f ! Ignore words without a definition
2004-07-16 02:26:21 -04:00
] ifte
] subset nip ;
: usages-in-vocab. ( of vocab -- )
#! List all usages of a word in a vocabulary.
tuck usages-in-vocab dup [
swap "IN: " write print [.]
] [
2drop
] ifte ;
: usages. ( word -- )
#! List all usages of a word in all vocabularies.
vocabs [ usages-in-vocab. ] each-with ;
2004-07-16 02:26:21 -04:00
: vocab-apropos ( substring vocab -- list )
2004-07-16 02:26:21 -04:00
#! Push a list of all words in a vocabulary whose names
#! contain a string.
words [ word-name dupd str-contains? ] subset nip ;
2004-07-16 02:26:21 -04:00
: vocab-apropos. ( substring vocab -- )
2004-07-16 02:26:21 -04:00
#! List all words in a vocabulary that contain a string.
tuck vocab-apropos dup [
2004-07-16 02:26:21 -04:00
"IN: " write swap print [.]
] [
2drop
] ifte ;
2004-11-24 21:45:30 -05:00
: vocab-completions ( substring vocab -- list )
#! Used by jEdit plugin. Like vocab-apropos, but only
#! matches at the start of a word name are considered.
2004-12-19 03:04:03 -05:00
words [ word-name over ?str-head nip ] subset nip ;
2004-11-24 21:45:30 -05:00
: apropos. ( substring -- )
2004-07-16 02:26:21 -04:00
#! List all words that contain a string.
vocabs [ vocab-apropos. ] each-with ;
2004-07-16 02:26:21 -04:00
: in. ( -- )
#! Print the vocabulary where new words are added in
#! interactive parsers.
"in" get print ;
: use. ( -- )
#! Print the vocabulary search path for interactive parsers.
"use" get . ;
2004-07-30 16:22:20 -04:00
: vocabs. ( -- )
vocabs . ;
: words. ( vocab -- )
words . ;