src/fmt.bend source
src/fmt.bend on the hub · documented module
# src/fmt: bolt's formatter (`bolt fmt`). It changes blank space and nothing# else: every token that is code is kept as it is, in order, and so is each# comment but for the blanks it ends in. Four passes over a file's tokens:# heads a def header that breaks across lines, or is too wide, takes# the shape `wrap` asks for (src/fmt/head.bend);# trim no blank before a line's end: a space token right before a# newline, or before the end of the file, is dropped, and a# comment loses the spaces and tabs it ends in;# squeeze no two blank lines in a row, and none at the top: once two# newlines have run, further ones are dropped, and the file opens# as if two already had;# close one newline at the end: the newlines the tokens end in become# one, and a file that ends without one gains it. An empty file# stays empty, and a file that ends inside a string that never# closes is left as it ends: the string holds its last line# break, so there is nothing to add after it.# Indentation, the spaces inside a line, and where a line that is no def# header breaks are left as they are.# Pure: text in, text out. src/fmt/plan.bend decides which files change and# src/fmt/run.bend writes them.import Baseimport ./lazy/lazy.bend as Lazyimport ./rchars.bend as Rcharsimport ./syntax/lex.bend as Leximport ./fmt/head.bend as Head# a newline token?def is_newline(k: Lex.TokKind) -> Bool: match k: case Lex.TNewline{}: True{} case other: False{}# a space token: a run of spaces and tabs?def is_space(k: Lex.TokKind) -> Bool: match k: case Lex.TSpace{}: True{} case other: False{}# a space or a tab?def blank(+ch: Char) -> Bool: Bool.or(Char.is_eq(ch, ' '), Char.is_eq(ch, '\t'))# the chars after the blanks they open withdef trim.lead(cs: List<&2, Char>) -> List<&2, Char>: match cs: case Nil{}: Nil{} case Con{+c, +t}: Lazy.either(List<&2, Char>, blank(c), _u => trim.lead(t), _v => c <> t)# a text without the spaces and tabs it ends indef trim.tail(t: String) -> String: Rchars.text(trim.lead(List.reverse(&2, Char, String.to_list(t))))# a token's text as trim leaves it: a comment's without the blanks it ends# in, any other as it isdef trim.text(k: Lex.TokKind, +t: String) -> String: match k: case Lex.TComment{}: trim.tail(t) case other: t# does a line end here: at a newline, or at the end of the tokens?def trim.ends(toks: List<&2, Lex.Tok>) -> Bool: match toks: case Nil{}: True{} case Con{Lex.Tok{k, _t, _l, _c}, _rest}: is_newline(k)# a token kept, with its text trimmed, before the rest; or droppeddef trim.keep(drop: Bool, +k: Lex.TokKind, t: String, l: U32, c: U32, more: List<&2, Lex.Tok>) -> List<&2, Lex.Tok>: match drop: case True{}: more case False{}: Lex.Tok{k, trim.text(k, t), l, c} <> more# no blank before a line's enddef trim(toks: List<&2, Lex.Tok>) -> List<&2, Lex.Tok>: match toks: case Nil{}: Nil{} case Con{Lex.Tok{+k, t, l, c}, +rest}: trim.keep(Lazy.and_then(is_space(k), _u => trim.ends(rest)), k, t, l, c, trim(rest))# how many newlines in a row are kept: two, which is one blank linedef squeeze.most() -> U32: 2# the run of newlines after a token: one longer after a newline, none after# anything elsedef squeeze.run(nl: Bool, run: U32) -> U32: match nl: case True{}: (run + 1 : U32) case False{}: 0# no two blank lines in a row. run: how many newlines the tokens so far end# indef squeeze(toks: List<&2, Lex.Tok>, +run: U32) -> List<&2, Lex.Tok>: match toks: case Nil{}: Nil{} case Con{Lex.Tok{+k, t, l, c}, rest}: +nl = is_newline(k) +drop = Bool.and(nl, U32.is_ge(run, squeeze.most())) +more = squeeze(rest, Bool.pick(U32, drop, run, squeeze.run(nl, run))) Bool.pick(List<&2, Lex.Tok>, drop, more, Lex.Tok{k, t, l, c} <> more)# are these all newlines?def close.only(toks: List<&2, Lex.Tok>) -> Bool: match toks: case Nil{}: True{} case Con{Lex.Tok{k, _t, _l, _c}, rest}: Lazy.and_then(is_newline(k), _u => close.only(rest))# the newline a file ends indef close.newline() -> Lex.Tok: Lex.Tok{Lex.TNewline{}, "\n", 0, 0}# the tokens through their last one that is no newline, then one newlinedef close.go(toks: List<&2, Lex.Tok>) -> List<&2, Lex.Tok>: match toks: case Nil{}: [close.newline()] case Con{Lex.Tok{+k, t, l, c}, +rest}: Lazy.stop(List<&2, Lex.Tok>, Lazy.and_then(is_newline(k), _u => close.only(rest)), [close.newline()], _v => Lex.Tok{k, t, l, c} <> close.go(rest))# is the first char a line break?def close.first(cs: List<&2, Char>) -> Bool: match cs: case Nil{}: False{} case Con{c, _more}: Lex.is_newline(c)# does a text end in a line break?def close.broken(t: String) -> Bool: close.first(List.reverse(&2, Char, String.to_list(t)))# do the tokens end in one that is no newline, yet whose text ends in a line# break? A string that never closes does: it runs through every line break# to the end of the filedef close.shut(toks: List<&2, Lex.Tok>) -> Bool: match toks: case Nil{}: False{} case Con{Lex.Tok{k, t, _l, _c}, Nil{}}: Bool.and(Bool.not(is_newline(k)), close.broken(t)) case Con{_tok, rest}: close.shut(rest)# one newline at the end; an empty file stays empty, and so does one that# ends inside a stringdef close(+toks: List<&2, Lex.Tok>) -> List<&2, Lex.Tok>: match toks: case Nil{}: Nil{} case Con{+h, +t}: Lazy.either(List<&2, Lex.Tok>, close.shut(h <> t), _u => h <> t, _v => close.go(h <> t))# a file's tokens, formatted. The line and column of a token after the first# change are the ones it was read at, not where it now sits: only the texts# are read againdef toks(ts: List<&2, Lex.Tok>) -> List<&2, Lex.Tok>: close(squeeze(trim(Head.heads(ts)), squeeze.most()))# a file's text, formatteddef text(source: String) -> String: Lex.text(toks(Lex.tokens(source)))