/basis/prettyprint/prettyprint.factor

http://github.com/x6j8x/factor · Factor · 150 lines · 115 code · 33 blank · 2 comment · 12 complexity · 50d1250d94f6947f78e386324ed63087 MD5 · raw file

  1. ! Copyright (C) 2003, 2010 Slava Pestov.
  2. ! See http://factorcode.org/license.txt for BSD license.
  3. USING: arrays accessors assocs classes colors combinators
  4. continuations grouping io io.streams.string io.styles kernel
  5. make math math.parser namespaces parser prettyprint.backend
  6. prettyprint.config prettyprint.custom prettyprint.sections
  7. quotations sequences sorting strings vocabs vocabs.prettyprint
  8. words sets generic ;
  9. FROM: namespaces => set ;
  10. IN: prettyprint
  11. : with-use ( obj quot -- )
  12. t make-pprint (pprint-manifest
  13. [ pprint-manifest) ] [ [ drop nl ] unless-empty ] bi
  14. do-pprint ; inline
  15. : with-in ( obj quot -- )
  16. t make-pprint current-vocab>> [ pprint-in bl ] when* do-pprint ; inline
  17. : pprint ( obj -- ) [ pprint* ] with-pprint ;
  18. : . ( obj -- ) pprint nl ;
  19. : pprint-use ( obj -- ) [ pprint* ] with-use ;
  20. : unparse ( obj -- str ) [ pprint ] with-string-writer ;
  21. : unparse-use ( obj -- str ) [ pprint-use ] with-string-writer ;
  22. : pprint-short ( obj -- )
  23. [ pprint ] with-short-limits ;
  24. : unparse-short ( obj -- str )
  25. [ pprint-short ] with-string-writer ;
  26. : short. ( obj -- ) pprint-short nl ;
  27. : error-in-pprint ( obj -- str )
  28. class-of name>> "~pprint error: " "~" surround ;
  29. : .b ( n -- ) >bin print ;
  30. : .o ( n -- ) >oct print ;
  31. : .h ( n -- ) >hex print ;
  32. : stack. ( seq -- )
  33. [
  34. [ short. ] [
  35. drop [ error-in-pprint ] keep write-object nl
  36. ] recover
  37. ] each ;
  38. : .s ( -- ) datastack stack. ;
  39. : .r ( -- ) retainstack stack. ;
  40. <PRIVATE
  41. SYMBOL: ->
  42. \ ->
  43. { { foreground T{ rgba f 1 1 1 1 } } { background T{ rgba f 0 0 0 1 } } }
  44. "word-style" set-word-prop
  45. : remove-step-into ( word -- )
  46. building get [ nip pop wrapped>> ] unless-empty , ;
  47. : (remove-breakpoints) ( quot -- newquot )
  48. [
  49. [
  50. {
  51. { [ dup word? not ] [ , ] }
  52. { [ dup "break?" word-prop ] [ drop ] }
  53. { [ dup "step-into?" word-prop ] [ remove-step-into ] }
  54. [ , ]
  55. } cond
  56. ] each
  57. ] [ ] make ;
  58. : remove-breakpoints ( quot pos -- quot' )
  59. 1 + short cut [ (remove-breakpoints) ] bi@ [ -> ] glue ;
  60. : optimized-frame? ( triple -- ? ) second word? ;
  61. : frame-word? ( triple -- ? )
  62. first word? ;
  63. : frame-word. ( triple -- )
  64. first {
  65. { [ dup method? ] [ "Method: " write pprint ] }
  66. { [ dup word? ] [ "Word: " write pprint ] }
  67. [ drop ]
  68. } cond ;
  69. : optimized-frame. ( triple -- )
  70. [
  71. [ "(O)" write ] with-cell
  72. [ frame-word. ] with-cell
  73. ] with-row ;
  74. : unoptimized-frame. ( triple -- )
  75. [
  76. [ "(U)" write ] with-cell
  77. [
  78. "Quotation: " write
  79. dup [ second ] [ third ] bi remove-breakpoints
  80. H{
  81. { nesting-limit 3 }
  82. { length-limit 100 }
  83. } clone [ pprint ] with-variables
  84. ] with-cell
  85. ] with-row
  86. dup frame-word? [
  87. [
  88. [ ] with-cell
  89. [ frame-word. ] with-cell
  90. ] with-row
  91. ] [ drop ] if ;
  92. : callframe. ( triple -- )
  93. dup optimized-frame?
  94. [ optimized-frame. ] [ unoptimized-frame. ] if ;
  95. PRIVATE>
  96. : callstack. ( callstack -- )
  97. callstack>array 3 <groups>
  98. { { table-gap { 5 5 } } } [ [ callframe. ] each ] tabular-output nl ;
  99. : .c ( -- ) callstack callstack. ;
  100. : pprint-cell ( obj -- ) [ pprint-short ] with-cell ;
  101. SYMBOL: pprint-string-cells?
  102. : simple-table. ( values -- )
  103. standard-table-style [
  104. [
  105. [
  106. [
  107. dup string? pprint-string-cells? get not and
  108. [ [ write ] with-cell ]
  109. [ pprint-cell ]
  110. if
  111. ] each
  112. ] with-row
  113. ] each
  114. ] tabular-output nl ;
  115. : object-table. ( obj alist -- )
  116. [ [ nip first ] [ second call( obj -- str ) ] 2bi 2array ] with map
  117. simple-table. ;