| 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. *) |
| 24 | module Pretty; |
| 25 | import Out, Rt, Buf, Heap, Scan, Files; |
| 26 | |
| 27 | const |
| 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 | |
| 34 | var |
| 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 | |
| 56 | proc spaces(n: INT); |
| 57 | begin |
| 58 | while n > 0 do Buf.add(out, ' '); DEC(n); end; |
| 59 | end; |
| 60 | |
| 61 | (* Drops the spaces at the end of the line in out. *) |
| 62 | proc trim; |
| 63 | var s: []const U8; |
| 64 | begin |
| 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; |
| 69 | end; |
| 70 | |
| 71 | proc newLine; |
| 72 | begin |
| 73 | trim; |
| 74 | Buf.add(out, '\n'); |
| 75 | lineAt := out.len; |
| 76 | end; |
| 77 | |
| 78 | (* Where the line holding p starts in src. *) |
| 79 | proc lineStart(p: INT): INT; |
| 80 | begin |
| 81 | while (p > 0) and (src[p - 1] # '\n') do DEC(p); end; |
| 82 | return p; |
| 83 | end; |
| 84 | |
| 85 | (* The column src[p] is at, a tab going on to the next multiple of 8. *) |
| 86 | proc column(p: INT): INT; |
| 87 | var i, col: INT; |
| 88 | begin |
| 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; |
| 94 | end; |
| 95 | |
| 96 | (* Whether s ends an operand, so that a - after it subtracts. *) |
| 97 | proc endsOperand(const s: Scan.Scanner): BOOL; |
| 98 | begin |
| 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, "^"); |
| 102 | end; |
| 103 | |
| 104 | (* The symbol after the current one that isn't a comment. *) |
| 105 | proc following: Scan.Scanner; |
| 106 | var s: Scan.Scanner; |
| 107 | begin |
| 108 | s := sc; |
| 109 | repeat Scan.next(s); until s.kind # Scan.comment; |
| 110 | return s; |
| 111 | end; |
| 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. *) |
| 115 | proc binary: BOOL; |
| 116 | var n: Scan.Scanner; |
| 117 | begin |
| 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; |
| 132 | end; |
| 133 | |
| 134 | (* Whether the current symbol, first on its line, goes on with the |
| 135 | expression of the line before. *) |
| 136 | proc continues: BOOL; |
| 137 | var t: []const U8; |
| 138 | begin |
| 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); |
| 144 | end; |
| 145 | |
| 146 | (* Whether s starts a declaration at the margin: proc( starts a |
| 147 | procedure type. *) |
| 148 | proc declaration(s: Scan.Scanner): BOOL; |
| 149 | begin |
| 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, "("); |
| 154 | end; |
| 155 | |
| 156 | (* The indent of the block's own lines, or of its end. *) |
| 157 | proc inner: INT; |
| 158 | begin |
| 159 | if depth > 0 then return bases[depth - 1] + width; end; |
| 160 | if inSection then return width; end; |
| 161 | return 0; |
| 162 | end; |
| 163 | |
| 164 | proc outer: INT; |
| 165 | begin |
| 166 | if depth > 0 then return bases[depth - 1]; end; |
| 167 | return 0; |
| 168 | end; |
| 169 | |
| 170 | (* The indent of the line the current symbol starts, which was at old. *) |
| 171 | proc indent(old: INT): INT; |
| 172 | var n: Scan.Scanner; |
| 173 | begin |
| 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(); |
| 192 | end; |
| 193 | |
| 194 | (* Starts the line of the current symbol, at its indent. *) |
| 195 | proc startLine; |
| 196 | var old, new: INT; |
| 197 | begin |
| 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); |
| 206 | end; |
| 207 | |
| 208 | (* The spaces between the last symbol written and the current one, on |
| 209 | the same line. *) |
| 210 | proc gap(binaryNow: BOOL); |
| 211 | var n: INT; |
| 212 | begin |
| 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; |
| 225 | end; |
| 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. *) |
| 230 | proc write; |
| 231 | var i, k: INT; |
| 232 | begin |
| 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; |
| 250 | end; |
| 251 | |
| 252 | (* After a symbol that isn't a comment: the blocks and brackets it opens |
| 253 | or closes. *) |
| 254 | proc track(binaryNow: BOOL); |
| 255 | var t: []const U8; |
| 256 | begin |
| 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; |
| 276 | end; |
| 277 | |
| 278 | (* The line breaks in src[from..upto]. *) |
| 279 | proc lineBreaks(from, upto: INT); |
| 280 | var i: INT; |
| 281 | begin |
| 282 | for i := from to upto - 1 do |
| 283 | if src[i] = '\n' then newLine; end; |
| 284 | end; |
| 285 | end; |
| 286 | |
| 287 | (* src, formatted, into out. *) |
| 288 | proc format(text: []const U8); |
| 289 | var b: BOOL; |
| 290 | begin |
| 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; |
| 323 | end; |
| 324 | |
| 325 | proc main*; |
| 326 | var b: Buf.Buf; i: INT; path: []const U8; |
| 327 | begin |
| 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; |
| 348 | end; |
| 349 | |
| 350 | end Pretty. |