factor/basis/prettyprint/prettyprint.factor

347 lines
8.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: arrays generic generic.standard assocs io kernel math
namespaces make sequences strings io.styles io.streams.string
2007-09-20 18:09:08 -04:00
vectors words prettyprint.backend prettyprint.sections
2008-06-09 06:22:21 -04:00
prettyprint.config sorting splitting grouping math.parser vocabs
2008-04-03 22:19:20 -04:00
definitions effects classes.builtin classes.tuple io.files
classes continuations hashtables classes.mixin classes.union
2008-05-11 01:41:47 -04:00
classes.intersection classes.predicate classes.singleton
2008-08-01 18:32:30 -04:00
combinators quotations sets accessors colors ;
IN: prettyprint
2007-09-20 18:09:08 -04:00
: make-pprint ( obj quot -- block in use )
[
0 position set
H{ } clone pprinter-use set
V{ } clone recursion-check set
V{ } clone pprinter-stack set
over <object
call
pprinter-block
pprinter-in get
pprinter-use get keys
] with-scope ; inline
: with-pprint ( obj quot -- )
make-pprint 2drop do-pprint ; inline
: pprint-vocab ( vocab -- )
dup vocab present-text ;
: write-in ( vocab -- )
[ \ IN: pprint-word pprint-vocab ] with-pprint ;
: in. ( vocab -- )
[ write-in nl ] when* ;
: use. ( seq -- )
2008-09-06 20:13:59 -04:00
[
2007-09-20 18:09:08 -04:00
natural-sort [
\ USING: pprint-word
[ pprint-vocab ] each
\ ; pprint-word
] with-pprint nl
2008-09-06 20:13:59 -04:00
] unless-empty ;
2007-09-20 18:09:08 -04:00
: vocabs. ( in use -- )
dupd remove [ { "syntax" "scratchpad" } member? not ] filter
2007-09-20 18:09:08 -04:00
use. in. ;
: with-use ( obj quot -- )
make-pprint vocabs. do-pprint ; inline
: with-in ( obj quot -- )
make-pprint drop [ write-in bl ] when* do-pprint ; inline
: 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-02-21 00:13:31 -05:00
[ -> ] swap 3append
] [
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 ] with-cell ;
GENERIC: see ( defspec -- )
: comment. ( string -- )
[ H{ { font-style italic } } styled-text ] when* ;
: seeing-word ( word -- )
vocabulary>> pprinter-in set ;
2007-09-20 18:09:08 -04:00
2008-01-06 11:13:44 -05:00
: definer. ( defspec -- )
definer drop pprint-word ;
2007-09-20 18:09:08 -04:00
: stack-effect. ( word -- )
2008-06-08 16:32:55 -04:00
[ [ parsing-word? ] [ symbol? ] bi or not ] [ stack-effect ] bi and
2007-09-20 18:09:08 -04:00
[ effect>string comment. ] when* ;
2008-01-06 11:13:44 -05:00
: word-synopsis ( word -- )
2008-06-08 16:32:55 -04:00
{
[ seeing-word ]
[ definer. ]
[ pprint-word ]
[ stack-effect. ]
} cleave ;
2007-09-20 18:09:08 -04:00
2008-01-06 11:13:44 -05:00
M: word synopsis* word-synopsis ;
2007-09-20 18:09:08 -04:00
2008-01-06 11:13:44 -05:00
M: simple-generic synopsis* word-synopsis ;
2007-10-09 17:41:04 -04:00
2007-09-20 18:09:08 -04:00
M: standard-generic synopsis*
2008-06-08 16:32:55 -04:00
{
[ definer. ]
[ seeing-word ]
[ pprint-word ]
[ dispatch# pprint* ]
[ stack-effect. ]
} cleave ;
2007-09-20 18:09:08 -04:00
M: hook-generic synopsis*
2008-06-08 16:32:55 -04:00
{
[ definer. ]
[ seeing-word ]
[ pprint-word ]
[ "combination" word-prop var>> pprint* ]
2008-06-08 16:32:55 -04:00
[ stack-effect. ]
} cleave ;
2007-09-20 18:09:08 -04:00
M: method-spec synopsis*
2008-03-18 22:43:29 -04:00
first2 method synopsis* ;
2008-01-06 11:13:44 -05:00
2008-02-23 23:29:29 -05:00
M: method-body synopsis*
2008-06-08 16:32:55 -04:00
[ definer. ]
[ "method-class" word-prop pprint-word ]
[ "method-generic" word-prop pprint-word ] tri ;
2008-02-23 23:29:29 -05:00
2008-01-06 11:13:44 -05:00
M: mixin-instance synopsis*
2008-06-08 16:32:55 -04:00
[ definer. ]
[ class>> pprint-word ]
[ mixin>> pprint-word ] tri ;
2007-09-20 18:09:08 -04:00
M: pathname synopsis* pprint* ;
: synopsis ( defspec -- str )
[
0 margin set
1 line-limit set
[ synopsis* ] with-in
] with-string-writer ;
2007-09-20 18:09:08 -04:00
2008-02-23 23:29:29 -05:00
: synopsis-alist ( definitions -- alist )
[ dup synopsis swap ] { } map>assoc ;
: definitions. ( alist -- )
[ write-object nl ] assoc-each ;
: sorted-definitions. ( definitions -- )
synopsis-alist sort-keys definitions. ;
2007-09-20 18:09:08 -04:00
GENERIC: declarations. ( obj -- )
M: object declarations. drop ;
: declaration. ( word prop -- )
tuck name>> word-prop [ pprint-word ] [ drop ] if ;
2007-09-20 18:09:08 -04:00
M: word declarations.
{
POSTPONE: parsing
POSTPONE: delimiter
POSTPONE: inline
2008-07-26 19:58:53 -04:00
POSTPONE: recursive
2007-09-20 18:09:08 -04:00
POSTPONE: foldable
POSTPONE: flushable
2008-01-09 17:36:30 -05:00
} [ declaration. ] with each ;
2007-09-20 18:09:08 -04:00
2008-06-08 16:32:55 -04:00
: pprint-; ( -- ) \ ; pprint-word ;
2007-09-20 18:09:08 -04:00
: (see) ( spec -- )
2007-12-25 18:10:05 -05:00
<colon dup synopsis*
<block dup definition pprint-elements block>
dup definer nip [ pprint-word ] when* declarations.
block> ;
2007-09-20 18:09:08 -04:00
2007-12-25 18:10:05 -05:00
M: object see
[ (see) ] with-use nl ;
2007-09-20 18:09:08 -04:00
GENERIC: see-class* ( word -- )
M: union-class see-class*
2007-12-25 18:10:05 -05:00
<colon \ UNION: pprint-word
2007-09-20 18:09:08 -04:00
dup pprint-word
2007-12-25 18:10:05 -05:00
members pprint-elements pprint-; block> ;
2007-09-20 18:09:08 -04:00
2008-05-11 01:41:47 -04:00
M: intersection-class see-class*
<colon \ INTERSECTION: pprint-word
dup pprint-word
participants pprint-elements pprint-; block> ;
2007-09-20 18:09:08 -04:00
M: mixin-class see-class*
2007-12-25 18:10:05 -05:00
<block \ MIXIN: pprint-word
2007-09-20 18:09:08 -04:00
dup pprint-word <block
dup members [
hard line-break
2007-09-20 18:09:08 -04:00
\ INSTANCE: pprint-word pprint-word pprint-word
2008-01-09 17:36:30 -05:00
] with each block> block> ;
2007-09-20 18:09:08 -04:00
M: predicate-class see-class*
<colon \ PREDICATE: pprint-word
dup pprint-word
2008-03-26 19:23:19 -04:00
"<" text
dup superclass pprint-word
2007-09-20 18:09:08 -04:00
<block
"predicate-definition" word-prop pprint-elements
pprint-; block> block> ;
2008-04-02 16:41:29 -04:00
M: singleton-class see-class* ( class -- )
\ SINGLETON: pprint-word pprint-word ;
2008-06-29 22:37:57 -04:00
GENERIC: pprint-slot-name ( object -- )
M: string pprint-slot-name text ;
M: array pprint-slot-name
<flow \ { pprint-word
f <inset unclip text pprint-elements block>
\ } pprint-word block> ;
2008-07-13 22:06:50 -04:00
: unparse-slot ( slot-spec -- array )
[
dup name>> ,
dup class>> object eq? [
dup class>> ,
initial: ,
dup initial>> ,
] unless
dup read-only>> [
read-only ,
] when
drop
] { } make ;
: pprint-slot ( slot-spec -- )
unparse-slot
dup length 1 = [ first ] when
pprint-slot-name ;
2007-09-20 18:09:08 -04:00
M: tuple-class see-class*
2007-12-25 18:10:05 -05:00
<colon \ TUPLE: pprint-word
2007-09-20 18:09:08 -04:00
dup pprint-word
2008-03-26 19:37:28 -04:00
dup superclass tuple eq? [
"<" text dup superclass pprint-word
] unless
2008-07-13 22:06:50 -04:00
<block "slots" word-prop [ pprint-slot ] each block>
2007-12-25 18:10:05 -05:00
pprint-; block> ;
2007-09-20 18:09:08 -04:00
M: word see-class* drop ;
M: builtin-class see-class*
drop "! Built-in class" comment. ;
: see-class ( class -- )
dup class? [
2008-01-04 21:10:49 -05:00
[
dup seeing-word dup see-class*
] with-use nl
2007-09-20 18:09:08 -04:00
] when drop ;
M: word see
2008-01-04 21:10:49 -05:00
dup see-class
dup class? over symbol? not and [
nl
] when
dup class? over symbol? and not [
[ dup (see) ] with-use nl
] when
2008-06-29 22:37:57 -04:00
drop ;
: see-all ( seq -- )
natural-sort [ nl ] [ see ] interleave ;
: (see-implementors) ( class -- seq )
dup implementors [ method ] with map natural-sort ;
: (see-methods) ( generic -- seq )
"methods" word-prop values natural-sort ;
: see-methods ( word -- )
2007-09-20 18:09:08 -04:00
[
2008-06-29 22:37:57 -04:00
dup class? [ dup (see-implementors) % ] when
dup generic? [ dup (see-methods) % ] when
2007-09-20 18:09:08 -04:00
drop
] { } make prune see-all ;