factor/basis/prettyprint/prettyprint.factor

112 lines
2.7 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2009 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: arrays accessors assocs colors combinators grouping io
io.streams.string io.styles kernel make math math.parser namespaces
parser prettyprint.backend prettyprint.config prettyprint.custom
prettyprint.sections quotations sequences sorting strings vocabs
vocabs.prettyprint words sets ;
2008-08-01 18:32:30 -04:00
IN: prettyprint
2007-09-20 18:09:08 -04:00
: with-use ( obj quot -- )
make-pprint (pprint-manifest
[ pprint-manifest) ] [ [ drop nl ] unless-empty ] bi
do-pprint ; inline
2007-09-20 18:09:08 -04:00
: with-in ( obj quot -- )
make-pprint current-vocab>> [ pprint-in bl ] when* do-pprint ; inline
2007-09-20 18:09:08 -04:00
: pprint ( obj -- ) [ pprint* ] with-pprint ;
2008-08-12 04:31:48 -04:00
: . ( obj -- ) pprint nl ;
2007-09-20 18:09:08 -04:00
: pprint-use ( obj -- ) [ pprint* ] with-use ;
: unparse ( obj -- str ) [ pprint ] with-string-writer ;
2007-09-20 18:09:08 -04:00
: unparse-use ( obj -- str ) [ pprint-use ] with-string-writer ;
2007-09-20 18:09:08 -04:00
: pprint-short ( obj -- )
H{
{ line-limit 1 }
{ length-limit 15 }
{ nesting-limit 2 }
{ string-limit? t }
{ boa-tuples? t }
2007-09-20 18:09:08 -04:00
} clone [ pprint ] bind ;
2008-02-21 02:26:44 -05:00
: unparse-short ( obj -- str )
[ pprint-short ] with-string-writer ;
2007-09-20 18:09:08 -04:00
: short. ( obj -- ) pprint-short nl ;
: .b ( n -- ) >bin print ;
: .o ( n -- ) >oct print ;
: .h ( n -- ) >hex print ;
: stack. ( seq -- ) [ short. ] each ;
: .s ( -- ) datastack stack. ;
: .r ( -- ) retainstack stack. ;
<PRIVATE
2007-10-05 01:08:34 -04:00
SYMBOL: ->
\ ->
2008-08-01 18:32:30 -04:00
{ { foreground T{ rgba f 1 1 1 1 } } { background T{ rgba f 0 0 0 1 } } }
2007-10-05 01:08:34 -04:00
"word-style" set-word-prop
2008-02-21 00:13:31 -05:00
: remove-step-into ( word -- )
2008-09-06 20:13:59 -04:00
building get [ nip pop wrapped>> ] unless-empty , ;
2008-02-21 00:13:31 -05:00
: (remove-breakpoints) ( quot -- newquot )
[
[
{
{ [ dup word? not ] [ , ] }
{ [ dup "break?" word-prop ] [ drop ] }
{ [ dup "step-into?" word-prop ] [ remove-step-into ] }
2008-04-11 13:53:22 -04:00
[ , ]
2008-02-21 00:13:31 -05:00
} cond
] each
] [ ] make ;
: remove-breakpoints ( quot pos -- quot' )
over quotation? [
2008-03-29 21:36:58 -04:00
1+ cut [ (remove-breakpoints) ] bi@
2008-12-03 20:10:41 -05:00
[ -> ] glue
2008-02-21 00:13:31 -05:00
] [
drop
] if ;
2007-10-05 01:08:34 -04:00
PRIVATE>
2007-09-20 18:09:08 -04:00
: callstack. ( callstack -- )
2007-10-05 01:08:34 -04:00
callstack>array 2 <groups> [
2008-02-21 00:13:31 -05:00
remove-breakpoints
[
3 nesting-limit set
100 length-limit set
.
] with-scope
2007-10-05 01:08:34 -04:00
] assoc-each ;
2007-09-20 18:09:08 -04:00
: .c ( -- ) callstack callstack. ;
: pprint-cell ( obj -- ) [ pprint-short ] with-cell ;
SYMBOL: pprint-string-cells?
2007-09-20 18:09:08 -04:00
: simple-table. ( values -- )
standard-table-style [
[
[
[
dup string? pprint-string-cells? get not and
[ [ write ] with-cell ]
[ pprint-cell ]
if
] each
] with-row
] each
] tabular-output nl ;