factor/library/syntax/prettyprint.factor

173 lines
4.3 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: alien errors generic hashtables kernel lists math memory
2005-04-10 18:58:30 -04:00
namespaces parser presentation sequences stdio streams strings
unparser vectors words ;
2004-07-16 02:26:21 -04:00
SYMBOL: prettyprint-limit
2005-02-06 00:21:26 -05:00
SYMBOL: one-line
SYMBOL: tab-size
2005-02-09 20:57:19 -05:00
SYMBOL: recursion-check
GENERIC: prettyprint* ( indent obj -- indent )
M: object prettyprint* ( indent obj -- indent )
unparse write ;
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 ;
2005-03-18 19:38:27 -05:00
: word-actions ( -- list )
2004-11-25 21:08:09 -05:00
[
2005-04-10 18:58:30 -04:00
[[ "See" "see" ]]
[[ "Push" "" ]]
[[ "Execute" "execute" ]]
[[ "jEdit" "jedit" ]]
[[ "Usages" "usages ." ]]
[[ "Implements" "implements ." ]]
2004-11-25 21:08:09 -05:00
] ;
2005-03-18 19:38:27 -05:00
: browser-attrs ( word -- style )
#! Return the style values for the HTML word browser
dup word-vocabulary [
swap word-name "browser-link-word" swons
swap "browser-link-vocab" swons
2list
] [
drop [ ]
] ifte* ;
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 [
2005-03-18 19:38:27 -05:00
dup word-link word-actions <actions> "actions" swons unit
swap browser-attrs append
2004-11-25 21:08:09 -05:00
] [
drop [ ]
] ifte ;
: word. ( word -- ) dup word-name swap word-attrs write-attr ;
: word-bl word. " " write ;
2004-11-25 21:08:09 -05:00
2005-02-09 20:57:19 -05:00
M: word prettyprint* ( indent word -- indent )
dup parsing? [ \ POSTPONE: word-bl ] when word. ;
2005-02-09 20:57:19 -05:00
: indent ( indent -- )
#! Print the given number of spaces.
" " fill write ;
: prettyprint-newline ( indent -- )
"\n" write indent ;
: prettyprint-elements ( indent list -- indent )
[ prettyprint* " " write ] each ;
: <prettyprint ( indent -- indent )
tab-size get + one-line get [
" " write
] [
dup prettyprint-newline
] ifte ;
: prettyprint> ( indent -- indent )
tab-size get - one-line get
[ dup prettyprint-newline ] unless ;
: prettyprint-limit? ( indent -- ? )
prettyprint-limit get dup [ >= ] [ nip ] ifte ;
: check-recursion ( indent obj quot -- ? indent )
#! We detect circular structure.
pick prettyprint-limit? >r
over recursion-check get memq? r> or [
2drop "..." write
] [
over recursion-check [ cons ] change
call
recursion-check [ cdr ] change
] ifte ;
2005-02-06 00:21:26 -05:00
: prettyprint-sequence ( indent start list end -- indent )
#! Prettyprint a list, with start/end delimiters; eg, [ ],
#! or { }, or << >>. The body of the list is indented,
#! unless the list is empty.
over [
>r >r word. <prettyprint
2005-02-09 20:57:19 -05:00
r> prettyprint-elements
prettyprint> r> word.
2005-02-06 00:21:26 -05:00
] [
>r >r word. " " write r> drop r> word.
2005-02-06 00:21:26 -05:00
] ifte ;
M: list prettyprint* ( indent list -- indent )
2005-02-10 23:58:28 -05:00
[
\ [ swap \ ] prettyprint-sequence
] check-recursion ;
2004-07-16 02:26:21 -04:00
M: cons prettyprint* ( indent cons -- indent )
2005-02-06 00:21:26 -05:00
#! Here we turn the cons into a list of two elements.
2005-02-09 20:57:19 -05:00
[
\ [[ swap uncons 2list \ ]] prettyprint-sequence
] check-recursion ;
2004-07-19 16:10:18 -04:00
M: vector prettyprint* ( indent vector -- indent )
2005-02-09 20:57:19 -05:00
[
2005-04-02 02:39:33 -05:00
\ { swap >list \ } prettyprint-sequence
2005-02-09 20:57:19 -05:00
] check-recursion ;
2004-11-25 20:37:05 -05:00
M: hashtable prettyprint* ( indent hashtable -- indent )
2005-02-09 20:57:19 -05:00
[
\ {{ swap hash>alist \ }} prettyprint-sequence
] check-recursion ;
2004-11-25 20:37:05 -05:00
M: tuple prettyprint* ( indent tuple -- indent )
2005-02-09 20:57:19 -05:00
[
\ << swap tuple>list \ >> prettyprint-sequence
2005-02-09 20:57:19 -05:00
] check-recursion ;
2004-07-16 02:26:21 -04:00
2005-04-10 18:58:30 -04:00
M: alien prettyprint* ( alien -- str )
\ ALIEN: word-bl alien-address unparse write ;
: prettyprint ( obj -- )
2005-02-09 20:57:19 -05:00
[
recursion-check off
0 swap prettyprint* drop terpri
] with-scope ;
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 -- )
[
2005-02-06 00:21:26 -05:00
one-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-04-02 02:39:33 -05:00
>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 ;
2005-02-09 20:57:19 -05:00
global [ 4 tab-size set ] bind