]> gitweb.factorcode.org Git - factor.git/blob - basis/listener/listener.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / listener / listener.factor
1 ! Copyright (C) 2003, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays hashtables io kernel math math.parser memory
4 namespaces parser lexer sequences strings io.styles
5 vectors words generic system combinators continuations debugger
6 definitions compiler.units accessors colors prettyprint fry
7 sets vocabs.parser source-files.errors locals ;
8 IN: listener
9
10 GENERIC: stream-read-quot ( stream -- quot/f )
11
12 : parse-lines-interactive ( lines -- quot/f )
13     [ parse-lines in get ] with-compilation-unit in set ;
14
15 : read-quot-step ( lines -- quot/f )
16     [ parse-lines-interactive ] [
17         dup error>> unexpected-eof?
18         [ 2drop f ] [ rethrow ] if
19     ] recover ;
20
21 : read-quot-loop ( stream accum -- quot/f )
22     over stream-readln dup [
23         over push
24         dup read-quot-step dup
25         [ 2nip ] [ drop read-quot-loop ] if
26     ] [
27         3drop f
28     ] if ;
29
30 M: object stream-read-quot
31     V{ } clone read-quot-loop ;
32
33 : read-quot ( -- quot/f ) input-stream get stream-read-quot ;
34
35 SYMBOL: visible-vars
36
37 : show-var ( var -- ) visible-vars [ swap suffix ] change ;
38
39 : show-vars ( seq -- ) visible-vars [ swap union ] change ;
40
41 : hide-var ( var -- ) visible-vars [ remove ] change ;
42
43 : hide-vars ( seq -- ) visible-vars [ swap diff ] change ;
44
45 : hide-all-vars ( -- ) visible-vars off ;
46
47 SYMBOL: error-hook
48
49 : call-error-hook ( error -- )
50     error-continuation get error-hook get
51     call( error continuation -- ) ;
52
53 [ drop print-error-and-restarts ] error-hook set-global
54
55 SYMBOL: display-stacks?
56
57 t display-stacks? set-global
58
59 SYMBOL: max-stack-items
60
61 10 max-stack-items set-global
62
63 SYMBOL: error-summary?
64
65 <PRIVATE
66
67 : title. ( string -- )
68     H{ { foreground T{ rgba f 0.3 0.3 0.3 1 } } } format nl ;
69
70 : visible-vars. ( -- )
71     visible-vars get [
72         nl "--- Watched variables:" title.
73         standard-table-style [
74             [
75                 [
76                     [ [ short. ] with-cell ]
77                     [ [ get short. ] with-cell ]
78                     bi
79                 ] with-row
80             ] each
81         ] tabular-output nl
82     ] unless-empty ;
83     
84 : trimmed-stack. ( seq -- )
85     dup length max-stack-items get > [
86         max-stack-items get cut*
87         [
88             [ length number>string "(" " more items)" surround ] keep
89             write-object nl
90         ] dip
91     ] when stack. ;
92
93 : datastack. ( datastack -- )
94     display-stacks? get [
95         [ nl "--- Data stack:" title. trimmed-stack. ] unless-empty
96     ] [ drop ] if ;
97
98 : prompt. ( -- )
99     in get auto-use? get [ " - auto" append ] when "( " " )" surround
100     H{ { background T{ rgba f 1 0.7 0.7 1 } } } format bl flush ;
101
102 :: (listener) ( datastack -- )
103     error-summary? get [ error-summary ] when
104     visible-vars.
105     datastack datastack.
106     prompt.
107
108     [
109         read-quot [
110             '[ datastack _ with-datastack ]
111             [ call-error-hook datastack ]
112             recover
113         ] [ return ] if*
114     ] [
115         dup lexer-error?
116         [ call-error-hook datastack ]
117         [ rethrow ]
118         if
119     ] recover
120
121     (listener) ;
122
123 PRIVATE>
124
125 : listener ( -- )
126     [ [ { } (listener) ] with-interactive-vocabs ] with-return ;
127
128 MAIN: listener