raz / tools / Pretty.raz

350 lines · 10.7 KiB · raw · history · last change e0d008cd

1(* Pretty: raz source laid out the one way.
2
3 Pretty Module.raz
4 prints the module, formatted.
5
6 Pretty -w File.raz...
7 rewrites each file that isn't formatted already.
8
9 Every line break and comment stays where it is. A line is indented
10 4 spaces for each block it stands in: the statements of if, while,
11 for, repeat and begin, the instructions of code, the fields of a
12 record or variant, the declarations of const, type and var. end, until, else, elsif and a
13 match's arms stand at the level of the statement that opened them;
14 module, import, proc, the sections and begin at the margin, with the
15 comment just before them. A line that goes on with the expression of
16 the line before keeps its indent, moved as far as the statement's
17 first line moved; so does the text of a comment. A raw string stays
18 as written, every byte, since its lines are its value.
19
20 Within a line, a binary operator has one space on each side, a unary
21 minus or ~ none after it, and , and ; none before them and one after. A
22 comment after code keeps its column. Other spaces stay as written,
23 so that lined-up match arms stay lined up. No line ends in spaces. *)
24module Pretty;
25import Out, Rt, Buf, Heap, Scan, Files;
26
27const
28 width = 4; (* the spaces of a level *)
29 openers = "if while for match record variant begin repeat code";
30 declarations = "module import link extern proc const type var begin code";
31 binaryWords = "div mod and or xor shl shr in";
32 binarySymbols = "+ - * / & | = # < <= > >= :=";
33
34var
35 sc: Scan.Scanner; (* the source, at the symbol being written *)
36 src: []const U8;
37 out: Buf.Buf;
38 lineAt: INT; (* where the line being written starts in out *)
39 lineDelta: INT; (* how far this line's indent moved *)
40
41 (* the blocks open, each with the indent of the statement that
42 opened it *)
43 bases: [100]INT;
44 depth: INT;
45 brackets: INT; (* brackets open *)
46 inSection: BOOL; (* among the declarations of const, type or var *)
47 stmtIndent: INT; (* the indent of the current statement's first line *)
48 stmtDelta: INT; (* and how far that moved *)
49
50 (* the last symbol that wasn't a comment, and the one before it *)
51 prev, prev2: Scan.Scanner;
52 prevBinary, prevUnary: BOOL;
53 afterComment: BOOL; (* the last symbol written was a comment *)
54 last: INT; (* where the last symbol written ends in src *)
55
56proc spaces(n: INT);
57begin
58 while n > 0 do Buf.add(out, ' '); DEC(n); end;
59end;
60
61(* Drops the spaces at the end of the line in out. *)
62proc trim;
63var s: []const U8;
64begin
65 s := Buf.bytes(out);
66 while (out.len > lineAt) and ((s[out.len - 1] = ' ') or (s[out.len - 1] = '\t')) do
67 DEC(out.len);
68 end;
69end;
70
71proc newLine;
72begin
73 trim;
74 Buf.add(out, '\n');
75 lineAt := out.len;
76end;
77
78(* Where the line holding p starts in src. *)
79proc lineStart(p: INT): INT;
80begin
81 while (p > 0) and (src[p - 1] # '\n') do DEC(p); end;
82 return p;
83end;
84
85(* The column src[p] is at, a tab going on to the next multiple of 8. *)
86proc column(p: INT): INT;
87var i, col: INT;
88begin
89 col := 0;
90 for i := lineStart(p) to p - 1 do
91 if src[i] = '\t' then col := (col div 8 + 1) * 8; else INC(col); end;
92 end;
93 return col;
94end;
95
96(* Whether s ends an operand, so that a - after it subtracts. *)
97proc endsOperand(const s: Scan.Scanner): BOOL;
98begin
99 if s.kind = Scan.word then return not Scan.reserved(Scan.text(s)); end;
100 return (s.kind = Scan.number) or (s.kind = Scan.literal)
101 or Scan.is(s, ")") or Scan.is(s, "]") or Scan.is(s, "}") or Scan.is(s, "^");
102end;
103
104(* The symbol after the current one that isn't a comment. *)
105proc following: Scan.Scanner;
106var s: Scan.Scanner;
107begin
108 s := sc;
109 repeat Scan.next(s); until s.kind # Scan.comment;
110 return s;
111end;
112
113(* Whether the current symbol is a binary operator. A * after a name
114 and before ; , : = ) or a procedure's parameters exports the name. *)
115proc binary: BOOL;
116var n: Scan.Scanner;
117begin
118 if sc.kind = Scan.word then return Scan.among(Scan.text(sc), binaryWords); end;
119 if (sc.kind # Scan.symbol) or not Scan.among(Scan.text(sc), binarySymbols) then
120 return false;
121 end;
122 (* a | after an operand is or; one after anything else starts an arm *)
123 if Scan.is(sc, "-") or Scan.is(sc, "+") or Scan.is(sc, "|") then
124 return endsOperand(prev);
125 end;
126 if Scan.is(sc, "*") and (prev.kind = Scan.word) then
127 n := following();
128 if Scan.among(Scan.text(n), "; , : = )") then return false; end;
129 if Scan.is(n, "(") and Scan.is(prev2, "proc") then return false; end;
130 end;
131 return true;
132end;
133
134(* Whether the current symbol, first on its line, goes on with the
135 expression of the line before. *)
136proc continues: BOOL;
137var t: []const U8;
138begin
139 if (brackets > 0) or prevBinary or Scan.is(prev, ",") then return true; end;
140 t := Scan.text(sc);
141 if sc.kind = Scan.word then return Scan.among(t, binaryWords) or Scan.among(t, "then do of"); end;
142 if Scan.is(sc, "|") then return endsOperand(prev); end;
143 return (sc.kind = Scan.symbol) and Scan.among(t, binarySymbols);
144end;
145
146(* Whether s starts a declaration at the margin: proc( starts a
147 procedure type. *)
148proc declaration(s: Scan.Scanner): BOOL;
149begin
150 if (s.kind # Scan.word) or not Scan.among(Scan.text(s), declarations) then return false; end;
151 if not Scan.is(s, "proc") then return true; end;
152 Scan.next(s);
153 return not Scan.is(s, "(");
154end;
155
156(* The indent of the block's own lines, or of its end. *)
157proc inner: INT;
158begin
159 if depth > 0 then return bases[depth - 1] + width; end;
160 if inSection then return width; end;
161 return 0;
162end;
163
164proc outer: INT;
165begin
166 if depth > 0 then return bases[depth - 1]; end;
167 return 0;
168end;
169
170(* The indent of the line the current symbol starts, which was at old. *)
171proc indent(old: INT): INT;
172var n: Scan.Scanner;
173begin
174 if continues() then
175 if old + stmtDelta < 0 then return 0; end;
176 return old + stmtDelta;
177 end;
178 if sc.kind = Scan.comment then
179 n := following();
180 if n.lineBreak and declaration(n) then return 0; end;
181 return inner();
182 end;
183 if declaration(sc) then
184 depth := 0;
185 inSection := Scan.among(Scan.text(sc), "const type var");
186 return 0;
187 end;
188 if Scan.among(Scan.text(sc), "end until else elsif |") then
189 return outer();
190 end;
191 return inner();
192end;
193
194(* Starts the line of the current symbol, at its indent. *)
195proc startLine;
196var old, new: INT;
197begin
198 old := column(sc.start);
199 new := indent(old);
200 lineDelta := new - old;
201 if (sc.kind # Scan.comment) and not continues() then
202 stmtIndent := new;
203 stmtDelta := lineDelta;
204 end;
205 spaces(new);
206end;
207
208(* The spaces between the last symbol written and the current one, on
209 the same line. *)
210proc gap(binaryNow: BOOL);
211var n: INT;
212begin
213 if sc.kind = Scan.comment then
214 n := column(sc.start) + lineDelta - (out.len - lineAt);
215 if (n < 1) and (sc.start > last) then n := 1; end;
216 spaces(n);
217 elsif afterComment then
218 Buf.str(out, src[last..sc.start]);
219 elsif Scan.is(sc, ",") or Scan.is(sc, ";") then
220 elsif Scan.is(prev, ",") or Scan.is(prev, ";") or binaryNow or prevBinary then
221 if not prevUnary then spaces(1); end;
222 elsif not prevUnary then
223 Buf.str(out, src[last..sc.start]);
224 end;
225end;
226
227(* The current symbol as written; each line of a comment after its
228 first is moved as far as the line the comment starts on, and a raw
229 string's lines are left as they are. *)
230proc write;
231var i, k: INT;
232begin
233 i := sc.start;
234 while i < sc.stop do
235 if (src[i] = '\n') and (sc.kind = Scan.literal) then
236 Buf.add(out, '\n');
237 lineAt := out.len;
238 INC(i);
239 elsif src[i] = '\n' then
240 newLine;
241 k := i + 1;
242 while (k < sc.stop) and (src[k] = ' ') do INC(k); end;
243 if (k < sc.stop) and (src[k] # '\n') then spaces(k - (i + 1) + lineDelta); end;
244 i := k;
245 else
246 Buf.add(out, src[i]);
247 INC(i);
248 end;
249 end;
250end;
251
252(* After a symbol that isn't a comment: the blocks and brackets it opens
253 or closes. *)
254proc track(binaryNow: BOOL);
255var t: []const U8;
256begin
257 t := Scan.text(sc);
258 if sc.kind = Scan.word then
259 if Scan.among(t, openers) then
260 if depth = LEN(bases) then Rt.stop("Pretty: blocks nested too deep"); end;
261 bases[depth] := stmtIndent;
262 INC(depth);
263 elsif ((t = "end") or (t = "until")) and (depth > 0) then
264 DEC(depth);
265 end;
266 elsif sc.kind = Scan.symbol then
267 if Scan.among(t, "( [ {") then INC(brackets);
268 elsif Scan.among(t, ") ] }") and (brackets > 0) then DEC(brackets);
269 end;
270 end;
271 prevUnary := (Scan.is(sc, "-") or Scan.is(sc, "+")) and not binaryNow
272 or Scan.is(sc, "~");
273 prevBinary := binaryNow;
274 prev2 := prev;
275 prev := sc;
276end;
277
278(* The line breaks in src[from..upto]. *)
279proc lineBreaks(from, upto: INT);
280var i: INT;
281begin
282 for i := from to upto - 1 do
283 if src[i] = '\n' then newLine; end;
284 end;
285end;
286
287(* src, formatted, into out. *)
288proc format(text: []const U8);
289var b: BOOL;
290begin
291 src := text;
292 Buf.reset(out);
293 lineAt := 0;
294 depth := 0;
295 brackets := 0;
296 inSection := false;
297 stmtIndent := 0;
298 stmtDelta := 0;
299 Scan.init(sc, src);
300 prev := sc;
301 prev2 := sc;
302 prevBinary := false;
303 prevUnary := false;
304 afterComment := false;
305 last := 0;
306 Scan.next(sc);
307 while sc.kind # Scan.eof do
308 b := (sc.kind # Scan.comment) and binary();
309 if sc.lineBreak then
310 lineBreaks(last, sc.start);
311 startLine;
312 else
313 gap(b);
314 end;
315 write;
316 afterComment := sc.kind = Scan.comment;
317 if not afterComment then track(b); end;
318 last := sc.stop;
319 Scan.next(sc);
320 end;
321 lineBreaks(last, LEN(src));
322 trim;
323end;
324
325proc main*;
326var b: Buf.Buf; i: INT; path: []const U8;
327begin
328 Buf.init(b, Heap.heap);
329 Buf.init(out, Heap.heap);
330 if (ARGC() >= 3) and (Files.arg(1) = "-w") then
331 for i := 2 to ARGC() - 1 do
332 path := Files.arg(i);
333 Buf.reset(b);
334 if not Files.read(path, b) then Rt.stop("Pretty: can't read a file"); end;
335 format(Buf.bytes(b));
336 if Buf.bytes(out) # Buf.bytes(b) then
337 if not Files.write(path, Buf.bytes(out)) then Rt.stop("Pretty: can't write a file"); end;
338 Out.str(path); Out.ln;
339 end;
340 end;
341 elsif ARGC() = 2 then
342 if not Files.read(Files.arg(1), b) then Rt.stop("Pretty: can't read the file"); end;
343 format(Buf.bytes(b));
344 Out.str(Buf.bytes(out));
345 else
346 Rt.stop("usage: Pretty Module.raz, or Pretty -w File.raz...");
347 end;
348end;
349
350end Pretty.