factor/library/syntax/prettyprint.factor

176 lines
4.1 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2004-07-16 02:26:21 -04:00
IN: prettyprint
USING: errors generic kernel lists math namespaces stdio strings
presentation unparser vectors words hashtables ;
2004-07-16 02:26:21 -04:00
SYMBOL: prettyprint-limit
GENERIC: prettyprint* ( indent obj -- indent )
M: object prettyprint* ( indent obj -- indent )
unparse write ;
2004-07-16 02:26:21 -04:00
: tab-size
#! Change this to suit your tastes.
4 ;
2004-12-25 21:28:47 -05:00
: indent ( indent -- )
2004-07-16 02:26:21 -04:00
#! Print the given number of spaces.
" " fill write ;
: prettyprint-newline ( indent -- )
2004-12-25 21:28:47 -05:00
"\n" write indent ;
2004-07-16 02:26:21 -04:00
: prettyprint-element ( indent obj -- indent )
over prettyprint-limit get >= [
unparse write
] [
prettyprint*
2004-12-29 18:01:23 -05:00
] ifte " " write ;
: <prettyprint ( indent -- indent )
tab-size +
"prettyprint-single-line" get [
2004-12-29 18:01:23 -05:00
" " write
] [
dup prettyprint-newline
] ifte ;
: prettyprint> ( indent -- indent )
tab-size -
"prettyprint-single-line" get [
dup prettyprint-newline
] unless ;
2004-11-25 21:08:09 -05:00
: word-link ( word -- link )
[
2004-12-20 15:29:55 -05:00
dup word-name unparse ,
" [ " ,
word-vocabulary unparse ,
" ] search" ,
2004-11-25 21:08:09 -05:00
] make-string ;
2004-12-20 15:29:55 -05:00
: word-actions ( search -- list )
2004-11-25 21:08:09 -05:00
[
[[ "See" "see" ]]
[[ "Push" "" ]]
[[ "Execute" "execute" ]]
[[ "jEdit" "jedit" ]]
[[ "Usages" "usages." ]]
2004-11-25 21:08:09 -05:00
] ;
: word-attrs ( word -- attrs )
#! Words without a vocabulary do not get a link or an action
#! popup.
dup word-vocabulary [
2004-12-20 15:29:55 -05:00
word-link word-actions <actions> "actions" swons unit
2004-11-25 21:08:09 -05:00
] [
drop [ ]
] ifte ;
M: word prettyprint* ( indent word -- indent )
2004-11-25 21:08:09 -05:00
dup word-name
swap dup word-attrs swap word-style append
write-attr ;
: prettyprint-[ ( indent -- indent )
\ [ prettyprint* <prettyprint ;
: prettyprint-] ( indent -- indent )
prettyprint> \ ] prettyprint* ;
: prettyprint-list ( indent list -- indent )
#! Pretty-print a list, without [ and ].
[ prettyprint-element ] each ;
M: list prettyprint* ( indent list -- indent )
[
swap prettyprint-[ swap prettyprint-list prettyprint-]
] [
f unparse write
] ifte* ;
2004-07-16 02:26:21 -04:00
M: cons prettyprint* ( indent cons -- indent )
\ [[ prettyprint* " " write
uncons >r prettyprint-element r> prettyprint-element
\ ]] prettyprint* ;
2004-07-19 16:10:18 -04:00
: prettyprint-{ ( indent -- indent )
\ { prettyprint* <prettyprint ;
2004-07-19 16:10:18 -04:00
: prettyprint-} ( indent -- indent )
prettyprint> \ } prettyprint* ;
2004-07-19 16:10:18 -04:00
: prettyprint-vector ( indent list -- indent )
#! Pretty-print a vector, without { and }.
[ prettyprint-element ] vector-each ;
2004-07-19 16:10:18 -04:00
M: vector prettyprint* ( indent vector -- indent )
dup vector-length 0 = [
drop
\ { prettyprint*
2004-12-29 18:01:23 -05:00
" " write
\ } prettyprint*
] [
swap prettyprint-{ swap prettyprint-vector prettyprint-}
] ifte ;
2004-07-19 16:10:18 -04:00
2004-11-25 20:37:05 -05:00
: prettyprint-{{ ( indent -- indent )
\ {{ prettyprint* <prettyprint ;
2004-11-25 20:37:05 -05:00
: prettyprint-}} ( indent -- indent )
prettyprint> \ }} prettyprint* ;
2004-11-25 20:37:05 -05:00
M: hashtable prettyprint* ( indent hashtable -- indent )
2004-11-25 20:37:05 -05:00
hash>alist dup length 0 = [
drop
\ {{ prettyprint*
2004-12-29 18:01:23 -05:00
" " write
\ }} prettyprint*
2004-11-25 20:37:05 -05:00
] [
swap prettyprint-{{ swap prettyprint-list prettyprint-}}
] ifte ;
M: tuple prettyprint* ( indent tuple -- indent )
\ << prettyprint*
" " write
tuple>list [ prettyprint-element ] each
\ >> prettyprint* ;
: prettyprint-1 ( obj -- )
0 swap prettyprint* drop ;
2004-07-16 02:26:21 -04:00
: prettyprint ( obj -- )
prettyprint-1 terpri ;
2004-07-16 02:26:21 -04:00
2004-08-17 23:09:16 -04:00
: vocab-link ( vocab -- link )
"vocabularies'" swap cat2 ;
2004-08-17 23:09:16 -04:00
: . ( obj -- )
[
"prettyprint-single-line" on
16 prettyprint-limit set
prettyprint
] with-scope ;
: [.] ( list -- )
#! Unparse each element on its own line.
[ . ] each ;
2004-10-27 21:21:31 -04:00
: {.} ( vector -- )
#! Unparse each element on its own line.
2005-01-23 16:47:28 -05:00
vector>list reverse [ . ] each ;
2004-10-27 21:21:31 -04:00
: .s datastack {.} ;
: .r callstack {.} ;
2004-11-25 23:14:17 -05:00
: .n namestack [.] ;
: .c catchstack [.] ;
! For integers only
: .b >bin print ;
: .o >oct print ;
: .h >hex print ;
global [ 40 prettyprint-limit set ] bind