factor/library/syntax/prettyprint.factor

133 lines
3.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
2005-07-06 03:29:42 -04:00
USING: alien errors generic hashtables io kernel lists math
2005-07-30 02:08:59 -04:00
memory namespaces parser presentation sequences strings
2005-07-06 03:29:42 -04:00
styles 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 )
2005-07-27 01:46:06 -04:00
: object. ( str obj -- )
presented swons unit format ;
: unparse. ( obj -- )
2005-07-27 01:46:06 -04:00
[ unparse ] keep object. ;
M: object prettyprint* ( indent obj -- indent )
unparse. ;
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: unparse. bl ] when unparse. ;
2005-02-09 20:57:19 -05:00
: indent ( indent -- )
#! Print the given number of spaces.
2005-04-19 20:28:01 -04:00
CHAR: \s fill write ;
2005-02-09 20:57:19 -05:00
: prettyprint-newline ( indent -- )
"\n" write indent ;
2005-04-13 20:44:06 -04:00
: ?prettyprint-newline ( indent -- )
one-line get [ bl drop ] [ prettyprint-newline ] ifte ;
2005-02-09 20:57:19 -05:00
2005-04-13 20:44:06 -04:00
: <prettyprint ( indent -- indent )
tab-size get + dup ?prettyprint-newline ;
2005-02-09 20:57:19 -05:00
: 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
2005-07-28 23:33:18 -04:00
] ifte ; inline
2005-02-09 20:57:19 -05:00
: prettyprint-elements ( indent list -- indent )
[ prettyprint* bl ] each ;
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 unparse. <prettyprint
2005-02-09 20:57:19 -05:00
r> prettyprint-elements
prettyprint> r> unparse.
2005-02-06 00:21:26 -05:00
] [
>r >r unparse. bl r> drop r> unparse.
2005-02-06 00:21:26 -05:00
] ifte ;
2005-08-01 16:22:53 -04:00
M: cons prettyprint* ( indent list -- indent )
2005-02-10 23:58:28 -05:00
[
2005-08-01 16:22:53 -04:00
dup list? [
\ [ swap \ ]
] [
\ [[ swap uncons 2list \ ]]
] ifte prettyprint-sequence
2005-02-10 23:58:28 -05:00
] check-recursion ;
2004-07-19 16:10:18 -04:00
M: vector prettyprint* ( indent vector -- indent )
2005-02-09 20:57:19 -05:00
[
\ { swap \ } 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 <mirror> \ >> prettyprint-sequence
2005-02-09 20:57:19 -05:00
] check-recursion ;
2004-07-16 02:26:21 -04:00
M: alien prettyprint* ( alien -- )
\ ALIEN: unparse. bl alien-address unparse write ;
2005-04-10 18:58:30 -04:00
M: wrapper prettyprint* ( wrapper -- )
dup wrapped word? [
\ \ unparse. bl wrapped unparse.
] [
\ W[ unparse. bl wrapped prettyprint* \ ]W unparse.
] ifte ;
: 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
: . ( obj -- )
[
2005-02-06 00:21:26 -05:00
one-line on
16 prettyprint-limit set
prettyprint
] with-scope ;
: [.] ( sequence -- )
2004-10-27 21:21:31 -04:00
#! Unparse each element on its own line.
[ . ] each ;
2004-10-27 21:21:31 -04:00
: .s datastack reverse [.] flush ;
: .r callstack reverse [.] flush ;
! 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