factor/library/tools/word-tools.factor

130 lines
3.8 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
2004-07-16 02:26:21 -04:00
: word-uses? ( of in -- ? )
2dup = [
2drop f ! Don't say that a word uses itself
] [
word-parameter tree-contains?
2004-07-16 02:26:21 -04:00
] ifte ;
: 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 [ dupd usages-in-vocab. ] each drop ;
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.
words [ word-name over str-head? ] subset nip ;
: apropos. ( substring -- )
2004-07-16 02:26:21 -04:00
#! List all words that contain a string.
vocabs [ dupd vocab-apropos. ] each drop ;
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 . ;
: usage+ ( key -- )
dup "usages" word-property
[ succ ] [ 1 ] ifte*
"usages" set-word-property ;
GENERIC: count-usages ( quot -- )
M: object count-usages drop ;
M: word count-usages usage+ ;
M: cons count-usages unswons count-usages count-usages ;
: tally-usages ( -- )
[ f "usages" set-word-property ] each-word
[ word-parameter count-usages ] each-word ;
: auto-inline ( count -- )
#! Automatically inline all words called less than a count
#! number of times.
[
2dup "usages" word-property dup 0 ? >= [
t "inline" set-word-property
] [
drop
] ifte
] each-word drop ;