1 | // $Id: Stream.rf 3953 2008-10-02 14:13:11Z yura $ |
---|
2 | |
---|
3 | //$module Stream : IStream; |
---|
4 | |
---|
5 | $use Access Apply Box Class Compare Error StdIO; |
---|
6 | |
---|
7 | //** $func Stream s.func e.args = stream; |
---|
8 | |
---|
9 | Stream s.func e.args, { |
---|
10 | # <IsFunc s.func> = |
---|
11 | <Lib_Error (Stream Stream) "Arg 1 is not a Function">, $fail; |
---|
12 | <Box () (s.func e.args)>; |
---|
13 | }; |
---|
14 | |
---|
15 | |
---|
16 | $func? Read_Expr s.box = v.expr; |
---|
17 | |
---|
18 | Read_Expr s.box = |
---|
19 | <Get s.box> : v.expr, |
---|
20 | <Store s.box /*empty*/>, |
---|
21 | v.expr; |
---|
22 | |
---|
23 | //** $func Expr-Open expr = stream; |
---|
24 | |
---|
25 | Expr_Open expr = |
---|
26 | <Box expr> :: s.box, |
---|
27 | <Stream &Read_Expr s.box>; |
---|
28 | |
---|
29 | |
---|
30 | //** $func File-Open e.filename = stream; |
---|
31 | //** $func File-Close stream = ; |
---|
32 | |
---|
33 | File_Open e.filename = |
---|
34 | <Channel> :: s.channel, |
---|
35 | <PrimOpenFile s.channel e.filename R>, |
---|
36 | <Stream &PrimRead s.channel &ReadChunkSize>; |
---|
37 | |
---|
38 | File_Close stream, { |
---|
39 | <Get stream> : (e.buf) (s.func e.args), { |
---|
40 | e.args : s.ch e.rest, <IsChannel s.ch> = |
---|
41 | <PrimCloseChannel s.ch>; |
---|
42 | <Lib_Error (Stream "File_Close") "Arg 1 is not a Streamed File"> = $fail; |
---|
43 | }; |
---|
44 | <Lib_Error (Stream "File_Close") "Arg 1 is not a Stream"> = $fail; |
---|
45 | }; |
---|
46 | |
---|
47 | |
---|
48 | //** $func StdIStream = stream; |
---|
49 | |
---|
50 | $box StdIStream_Box; |
---|
51 | |
---|
52 | StdIStream, { |
---|
53 | <Get &StdIStream_Box> : stream = stream; |
---|
54 | <Stream &PrimRead &StdIn 1> :: stream, |
---|
55 | <Store &StdIStream_Box stream>, |
---|
56 | stream; |
---|
57 | }; |
---|
58 | |
---|
59 | |
---|
60 | //** $func? Getc stream = term; |
---|
61 | |
---|
62 | Getc stream = <Gets stream 1> : term, term; |
---|
63 | |
---|
64 | |
---|
65 | //** $func? Gets stream s.n = expr; |
---|
66 | |
---|
67 | Gets stream s.n, { |
---|
68 | <Get stream> : (e.buf) (s.func e.args) = |
---|
69 | e.buf $iter { |
---|
70 | e.buf <Apply s.func e.args>; |
---|
71 | <Store stream (e.buf) (s.func e.args)> = $fail; |
---|
72 | } :: e.buf, |
---|
73 | <Ge (<Length e.buf>) (s.n)> = |
---|
74 | <Store stream (<Middle s.n 0 e.buf>) (s.func e.args)>, |
---|
75 | <Left 0 s.n e.buf>; |
---|
76 | <Lib_Error (Stream Gets) "Arg 1 is not a Stream"> = $fail; |
---|
77 | }; |
---|
78 | |
---|
79 | |
---|
80 | //** $func Ungets stream expr = ; |
---|
81 | |
---|
82 | Ungets stream expr, { |
---|
83 | <Get stream> : (e.buf) (s.func e.args) = |
---|
84 | <Store stream (expr e.buf) (s.func e.args)>; |
---|
85 | <Lib_Error (Stream Ungets) "Arg 1 is not a Stream"> = $fail; |
---|
86 | }; |
---|
87 | |
---|
88 | |
---|
89 | //** $func Scanc stream t1 = empty-or-t1; |
---|
90 | |
---|
91 | Scanc stream t1, { |
---|
92 | <Getc stream> : { |
---|
93 | t1 = t1; |
---|
94 | t2 = <Ungets stream t2>; |
---|
95 | }; |
---|
96 | /*empty*/; |
---|
97 | }; |
---|
98 | |
---|
99 | Scans stream e1, { |
---|
100 | <Gets stream <Length e1>> : { |
---|
101 | e1 = e1; |
---|
102 | e2 = <Ungets stream e2>; |
---|
103 | }; |
---|
104 | /*empty*/; |
---|
105 | }; |
---|
106 | |
---|
107 | //** $func Get-Delim stream t.delim = expr; |
---|
108 | |
---|
109 | Get_Delims stream e.delim, { |
---|
110 | <Scans stream e.delim> : e.delim = e.delim; |
---|
111 | <Getc stream> :: t1 = t1 <Get_Delims stream e.delim>; |
---|
112 | /*empty*/; |
---|
113 | }; |
---|
114 | |
---|
115 | //** $func Get-Delim stream t.delim = expr; |
---|
116 | |
---|
117 | Get_Delim stream t.delim, { |
---|
118 | <Get stream> : (e.buf) (s.func e.args) = |
---|
119 | (/*e.str*/) (e.buf) $iter { |
---|
120 | e.buf : e1 t.delim e2 = |
---|
121 | (e.str e1 t.delim) (e2) Stop; |
---|
122 | (e.str e.buf) (<Apply s.func e.args>); |
---|
123 | (e.str e.buf) () Stop; |
---|
124 | } :: (e.str) (e.buf) e.Isstop, |
---|
125 | e.Isstop : Stop = |
---|
126 | <Store stream (e.buf) (s.func e.args)>, |
---|
127 | e.str; |
---|
128 | <Lib_Error (Stream "Get_Delim") "Arg 1 is not a Stream"> = $fail; |
---|
129 | }; |
---|
130 | |
---|
131 | |
---|
132 | //** $func Get-Line stream = expr; |
---|
133 | |
---|
134 | //Get-Line stream = <Get-Delim stream '\n'>; |
---|
135 | |
---|
136 | $func? Not_EOL t1 = ; |
---|
137 | Not_EOL t1 = # t1 : \{ '\n'; '\r'; }; |
---|
138 | |
---|
139 | Get_Line stream = |
---|
140 | <Get_While stream &Not_EOL> <Scanc stream '\r'> <Scanc stream '\n'>; |
---|
141 | |
---|
142 | |
---|
143 | //** $func Get-While stream s.pred = expr; |
---|
144 | |
---|
145 | Get_While stream s.pred, { |
---|
146 | <Get stream> : (e.buf) (s.func e.args) = |
---|
147 | (/*e.str*/) (e.buf) $iter { |
---|
148 | e.buf : t1 e2, { |
---|
149 | <Apply s.pred t1> : e = (e.str t1) (e2); |
---|
150 | (e.str) (e.buf) Stop; |
---|
151 | }; |
---|
152 | (e.str) (<Apply s.func e.args>); |
---|
153 | (e.str) (e.buf) Stop; |
---|
154 | } :: (e.str) (e.buf) e.Isstop, |
---|
155 | e.Isstop : Stop = |
---|
156 | <Store stream (e.buf) (s.func e.args)>, |
---|
157 | e.str; |
---|
158 | <Lib_Error (Stream "Get_While") "Arg 1 is not a Stream"> = $fail; |
---|
159 | }; |
---|
160 | |
---|
161 | |
---|
162 | //** $func? End-of-Stream? stream = ; |
---|
163 | |
---|
164 | IsEnd_of_Stream stream, { |
---|
165 | <Get stream> : (e.buf) (s.func e.args) = |
---|
166 | { |
---|
167 | e.buf $iter |
---|
168 | <Apply s.func e.args> |
---|
169 | :: e.buf, |
---|
170 | e.buf : v = |
---|
171 | <Store stream (e.buf) (s.func e.args)>, |
---|
172 | $fail; |
---|
173 | /*We are at the end of the stream*/; |
---|
174 | }; |
---|
175 | <Lib_Error (Stream "IsEnd_of_Stream") "Arg 1 is not a Stream"> = $fail; |
---|
176 | }; |
---|
177 | |
---|