Debian Codegraph @

gcc/m2/mc/mcPretty.mod

raw
1 (* Copyright (C) 2015-2025 Free Software Foundation, Inc. *)
2 (* This file is part of GNU Modula-2.
3
4 GNU Modula-2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 3, or (at your option) any later
7 version.
8
9 GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. *)
17
18 IMPLEMENTATION MODULE mcPretty ;
19
20 FROM DynamicStrings IMPORT String, InitString, KillString, Length, char ;
21 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
22
23
24 TYPE
25 pretty = POINTER TO RECORD
26 write : writeProc ;
27 writeln : writeLnProc ;
28 needsSpace,
29 needsIndent: BOOLEAN ;
30 seekPos,
31 curLine,
32 curPos,
33 indent : CARDINAL ;
34 stacked : pretty ;
35 END ;
36
37
38 (*
39 initPretty - initialise a pretty print data structure.
40 *)
41
42 PROCEDURE initPretty (w: writeProc; l: writeLnProc) : pretty ;
43 VAR
44 p: pretty ;
45 BEGIN
46 NEW (p) ;
47 WITH p^ DO
48 write := w ;
49 writeln := l ;
50 needsSpace := FALSE ;
51 needsIndent := FALSE ;
52 curPos := 0 ;
53 curLine := 0 ;
54 seekPos := 0 ;
55 indent := 0 ;
56 stacked := NIL
57 END ;
58 RETURN p
59 END initPretty ;
60
61
62 (*
63 dupPretty - duplicate a pretty print data structure.
64 *)
65
66 PROCEDURE dupPretty (p: pretty) : pretty ;
67 VAR
68 q: pretty ;
69 BEGIN
70 NEW (q) ;
71 q^ := p^ ;
72 RETURN q
73 END dupPretty ;
74
75
76 (*
77 killPretty - destroy a pretty print data structure.
78 Post condition: p is assigned to NIL.
79 *)
80
81 PROCEDURE killPretty (VAR p: pretty) ;
82 BEGIN
83 p := NIL ;
84 RETURN ;
85 DISPOSE (p) ;
86 p := NIL
87 END killPretty ;
88
89
90 (*
91 pushPretty - duplicate, p. Push, p, and return the duplicate.
92 *)
93
94 PROCEDURE pushPretty (p: pretty) : pretty ;
95 VAR
96 q: pretty ;
97 BEGIN
98 q := dupPretty (p) ;
99 q^.stacked := p ;
100 RETURN q
101 END pushPretty ;
102
103
104 (*
105 popPretty - pops the pretty object from the stack.
106 *)
107
108 PROCEDURE popPretty (p: pretty) : pretty ;
109 VAR
110 q: pretty ;
111 BEGIN
112 q := p^.stacked ;
113 q^.needsIndent := p^.needsIndent ;
114 q^.needsSpace := p^.needsSpace ;
115 q^.curPos := p^.curPos ;
116 q^.seekPos := p^.seekPos ;
117 q^.curLine := p^.curLine ;
118 killPretty (p) ;
119 RETURN q
120 END popPretty ;
121
122
123 (*
124 getindent - returns the current indent value.
125 *)
126
127 PROCEDURE getindent (p: pretty) : CARDINAL ;
128 BEGIN
129 RETURN p^.indent
130 END getindent ;
131
132
133 (*
134 setindent - sets the current indent to, n.
135 *)
136
137 PROCEDURE setindent (p: pretty; n: CARDINAL) ;
138 BEGIN
139 p^.indent := n
140 END setindent ;
141
142
143 (*
144 getcurpos - returns the current cursor position.
145 *)
146
147 PROCEDURE getcurpos (s: pretty) : CARDINAL ;
148 BEGIN
149 IF s^.needsSpace
150 THEN
151 RETURN s^.curPos+1
152 ELSE
153 RETURN s^.curPos
154 END
155 END getcurpos ;
156
157
158 (*
159 getcurline - returns the current line number.
160 *)
161
162 PROCEDURE getcurline (s: pretty) : CARDINAL ;
163 BEGIN
164 RETURN s^.curLine
165 END getcurline ;
166
167
168 (*
169 getseekpos - returns the seek position.
170 *)
171
172 PROCEDURE getseekpos (s: pretty) : CARDINAL ;
173 BEGIN
174 RETURN s^.seekPos
175 END getseekpos ;
176
177
178 (*
179 setneedSpace - sets needSpace flag to TRUE.
180 *)
181
182 PROCEDURE setNeedSpace (s: pretty) ;
183 BEGIN
184 s^.needsSpace := TRUE
185 END setNeedSpace ;
186
187
188 (*
189 noSpace - unset needsSpace.
190 *)
191
192 PROCEDURE noSpace (s: pretty) ;
193 BEGIN
194 s^.needsSpace := FALSE
195 END noSpace ;
196
197
198 (*
199 flushSpace -
200 *)
201
202 PROCEDURE flushSpace (p: pretty) ;
203 BEGIN
204 IF p^.needsSpace
205 THEN
206 p^.write (' ') ;
207 p^.needsSpace := FALSE ;
208 INC (p^.curPos) ;
209 INC (p^.seekPos)
210 END
211 END flushSpace ;
212
213
214 (*
215 flushIndent -
216 *)
217
218 PROCEDURE flushIndent (p: pretty) ;
219 VAR
220 i: CARDINAL ;
221 BEGIN
222 flushSpace (p) ;
223 IF p^.needsIndent
224 THEN
225 WHILE p^.curPos<p^.indent DO
226 p^.write (' ') ;
227 INC (p^.curPos) ;
228 INC (p^.seekPos)
229 END ;
230 p^.needsIndent := FALSE
231 END
232 END flushIndent ;
233
234
235 (*
236 print - print a string using, p.
237 *)
238
239 PROCEDURE print (p: pretty; a: ARRAY OF CHAR) ;
240 VAR
241 s: String ;
242 BEGIN
243 s := InitString (a) ;
244 prints (p, s) ;
245 s := KillString (s)
246 END print ;
247
248
249 (*
250 prints - print a string using, p.
251 *)
252
253 PROCEDURE prints (p: pretty; s: String) ;
254 VAR
255 l, i: CARDINAL ;
256 BEGIN
257 l := Length (s) ;
258 i := 0 ;
259 flushSpace (p) ;
260 WHILE i<l DO
261 IF (i+2<=l) AND (char (s, i)='\') AND (char (s, i+1)='n')
262 THEN
263 p^.needsIndent := TRUE ;
264 p^.needsSpace := FALSE ;
265 p^.curPos := 0 ;
266 p^.writeln ;
267 INC (p^.seekPos) ;
268 INC (p^.curLine) ;
269 INC (i)
270 ELSE
271 flushIndent (p) ;
272 p^.write (char (s, i)) ;
273 INC (p^.curPos) ;
274 INC (p^.seekPos)
275 END ;
276 INC (i)
277 END
278 END prints ;
279
280
281 (*
282 raw - print out string, s, without any translation of
283 escape sequences.
284 *)
285
286 PROCEDURE raw (p: pretty; s: String) ;
287 VAR
288 l, i: CARDINAL ;
289 BEGIN
290 l := Length (s) ;
291 i := 0 ;
292 flushSpace (p) ;
293 flushIndent (p) ;
294 WHILE i < l DO
295 p^.write (char (s, i)) ;
296 INC (p^.curPos) ;
297 INC (p^.seekPos) ;
298 INC (i)
299 END
300 END raw ;
301
302
303 END mcPretty.