|
| 1 | +### This code is based on code that is: |
| 2 | +### |
| 3 | +### Copyright (c) 2022 Calvin Rose and contributors |
| 4 | +### |
| 5 | +### Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 6 | +### this software and associated documentation files (the "Software"), to deal in |
| 7 | +### the Software without restriction, including without limitation the rights to |
| 8 | +### use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 9 | +### of the Software, and to permit persons to whom the Software is furnished to do |
| 10 | +### so, subject to the following conditions: |
| 11 | +### |
| 12 | +### The above copyright notice and this permission notice shall be included in all |
| 13 | +### copies or substantial portions of the Software. |
| 14 | +### |
| 15 | +### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +### FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +### AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +### LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +### OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +### SOFTWARE. |
| 22 | +### |
| 23 | +### fmt.janet |
| 24 | +### |
| 25 | +### Janet code formatter. |
| 26 | +### |
| 27 | + |
| 28 | +(defn- pnode |
| 29 | + "Make a capture function for a node." |
| 30 | + [tag] |
| 31 | + (fn [x] [tag x])) |
| 32 | + |
| 33 | +(def- parse-peg |
| 34 | + "Peg to parse Janet with extra information, namely comments." |
| 35 | + (peg/compile |
| 36 | + ~{:ws (+ (set " \t\r\f\0\v") '"\n") |
| 37 | + :readermac (set "';~,|") |
| 38 | + :symchars (+ (range "09" "AZ" "az" "\x80\xFF") (set "!$%&*+-./:<?=>@^_")) |
| 39 | + :token (some :symchars) |
| 40 | + :hex (range "09" "af" "AF") |
| 41 | + :escape (* "\\" (+ (set "ntrzfev0ab'?\"\\") |
| 42 | + (* "x" :hex :hex) |
| 43 | + (* "u" :hex :hex :hex :hex) |
| 44 | + (* "U" :hex :hex :hex :hex :hex :hex) |
| 45 | + (error (constant "bad hex escape")))) |
| 46 | + :comment (/ (* "#" '(any (if-not (+ "\n" -1) 1)) (+ "\n" -1)) ,(pnode :comment)) |
| 47 | + :span (/ ':token ,(pnode :span)) |
| 48 | + :bytes '(* "\"" (any (+ :escape (if-not "\"" 1))) "\"") |
| 49 | + :string (/ :bytes ,(pnode :string)) |
| 50 | + :buffer (/ (* "@" :bytes) ,(pnode :buffer)) |
| 51 | + :long-bytes '{:delim (some "`") |
| 52 | + :open (capture :delim :n) |
| 53 | + :close (cmt (* (not (> -1 "`")) (-> :n) ':delim) ,=) |
| 54 | + :main (drop (* :open (any (if-not :close 1)) :close))} |
| 55 | + :long-string (/ :long-bytes ,(pnode :string)) |
| 56 | + :long-buffer (/ (* "@" :long-bytes) ,(pnode :buffer)) |
| 57 | + :ptuple (/ (group (* "(" (any :input) (+ ")" (error)))) ,(pnode :ptuple)) |
| 58 | + :btuple (/ (group (* "[" (any :input) (+ "]" (error)))) ,(pnode :btuple)) |
| 59 | + :struct (/ (group (* "{" (any :input) (+ "}" (error)))) ,(pnode :struct)) |
| 60 | + :parray (/ (group (* "@(" (any :input) (+ ")" (error)))) ,(pnode :array)) |
| 61 | + :barray (/ (group (* "@[" (any :input) (+ "]" (error)))) ,(pnode :array)) |
| 62 | + :table (/ (group (* "@{" (any :input) (+ "}" (error)))) ,(pnode :table)) |
| 63 | + :rmform (/ (group (* ':readermac |
| 64 | + (group (any :non-form)) |
| 65 | + :form)) |
| 66 | + ,(pnode :rmform)) |
| 67 | + :form (choice :rmform |
| 68 | + :parray :barray :ptuple :btuple :table :struct |
| 69 | + :buffer :string :long-buffer :long-string |
| 70 | + :span) |
| 71 | + :non-form (choice :ws :comment) |
| 72 | + :input (choice :non-form :form) |
| 73 | + :main (* (any :input) (+ -1 (error)))})) |
| 74 | + |
| 75 | +(defn- make-tree |
| 76 | + "Turn a string of source code into a tree that will be printed" |
| 77 | + [source] |
| 78 | + [:top (peg/match parse-peg source)]) |
| 79 | + |
| 80 | +(defn- remove-extra-newlines |
| 81 | + "Remove leading and trailing newlines. Also remove |
| 82 | + some extra consecutive newlines." |
| 83 | + [node] |
| 84 | + (match node |
| 85 | + [tag (xs (array? xs))] |
| 86 | + (do |
| 87 | + (while (= "\n" (array/peek xs)) (array/pop xs)) # remove trailing newlines |
| 88 | + (when-let [index (find-index |(not= "\n" $) xs)] |
| 89 | + (array/remove xs 0 index)) # remove leading newlines |
| 90 | + # remove too many consecutive newlines |
| 91 | + (def max-consec (if (= tag :top) 3 2)) |
| 92 | + (var i 0) |
| 93 | + (var consec-count 0) |
| 94 | + (while (< i (length xs)) |
| 95 | + (if (= (in xs i) "\n") |
| 96 | + (if (= consec-count max-consec) (array/remove xs i) (do (++ i) (++ consec-count))) |
| 97 | + (do (set consec-count 0) (++ i)))) |
| 98 | + node) |
| 99 | + node)) |
| 100 | + |
| 101 | +(defdyn *user-indent-2-forms* |
| 102 | + "A user list of forms that are control forms and should be indented two spaces.") |
| 103 | + |
| 104 | +(defn- user-indent-2-forms [] (invert (or (dyn *user-indent-2-forms*) []))) |
| 105 | + |
| 106 | +(def- indent-2-forms |
| 107 | + "A list of forms that are control forms and should be indented two spaces." |
| 108 | + (invert ["fn" "match" "with" "with-dyns" "def" "def-" "var" "var-" "defn" "defn-" |
| 109 | + "varfn" "defmacro" "defmacro-" "defer" "edefer" "loop" "seq" "tabseq" "generate" "coro" |
| 110 | + "for" "each" "eachp" "eachk" "case" "cond" "defglobal" "varglobal" |
| 111 | + "if" "when" "when-let" "when-with" "while" "with-syms" "with-vars" |
| 112 | + "if-let" "if-not" "if-with" "let" "short-fn" "try" "unless" "default" "forever" "upscope" |
| 113 | + "repeat" "eachy" "forv" "compwhen" "compif" "ev/spawn" "ev/do-thread" "ev/with-deadline" |
| 114 | + "label" "prompt"])) |
| 115 | + |
| 116 | +(def- indent-2-peg |
| 117 | + "Peg to use to fuzzy match certain forms." |
| 118 | + (peg/compile ~(+ "with-" "def" "if-" "when-"))) |
| 119 | + |
| 120 | +(defn- check-indent-2 |
| 121 | + "Check if a tuple needs a 2 space indent or not" |
| 122 | + [items] |
| 123 | + (if-let [[tag body] (get items 0)] |
| 124 | + (cond |
| 125 | + (= "\n" (get items 1)) true |
| 126 | + (not= tag :span) nil |
| 127 | + (in indent-2-forms body) true |
| 128 | + (peg/match indent-2-peg body) true |
| 129 | + (in (user-indent-2-forms) body) true))) |
| 130 | + |
| 131 | +(defn- fmt |
| 132 | + "Emit formatted." |
| 133 | + [tree] |
| 134 | + |
| 135 | + (var col 0) |
| 136 | + (def ident-stack @[]) |
| 137 | + (var ident "") |
| 138 | + (def white @"") |
| 139 | + |
| 140 | + (defn emit [& xs] (each x xs (+= col (length x)) (prin x))) |
| 141 | + (defn indent [&opt delta] |
| 142 | + (array/push ident-stack ident) |
| 143 | + (set ident (string/repeat " " (+ col (or delta 0))))) |
| 144 | + (defn dedent [] (set ident (array/pop ident-stack))) |
| 145 | + (defn flushwhite [] (emit white) (buffer/clear white)) |
| 146 | + (defn dropwhite [] (buffer/clear white)) |
| 147 | + (defn addwhite [] (buffer/push-string white " ")) |
| 148 | + (defn newline [] (dropwhite) (print) (buffer/push-string white ident) (set col 0)) |
| 149 | + |
| 150 | + # Mutual recursion |
| 151 | + (var fmt-1-recur nil) |
| 152 | + |
| 153 | + (defn emit-body |
| 154 | + [open xs close &opt delta] |
| 155 | + (emit open) |
| 156 | + (indent delta) |
| 157 | + (each x xs (fmt-1-recur x)) |
| 158 | + (dropwhite) |
| 159 | + (dedent) |
| 160 | + (emit close) |
| 161 | + (addwhite)) |
| 162 | + |
| 163 | + (defn emit-funcall |
| 164 | + [xs] |
| 165 | + (emit "(") |
| 166 | + (def len (length xs)) |
| 167 | + (when (pos? len) |
| 168 | + (fmt-1-recur (xs 0)) |
| 169 | + (indent 1) |
| 170 | + (for i 1 len (fmt-1-recur (xs i))) |
| 171 | + (dropwhite) |
| 172 | + (dedent)) |
| 173 | + (emit ")") |
| 174 | + (addwhite)) |
| 175 | + |
| 176 | + (defn emit-string |
| 177 | + [x] |
| 178 | + (def parts (interpose "\n" (string/split "\n" x))) |
| 179 | + (each p parts (if (= p "\n") (do (newline) (dropwhite)) (emit p)))) |
| 180 | + |
| 181 | + (defn emit-rmform |
| 182 | + [rm nfs form] |
| 183 | + (emit rm) |
| 184 | + (each nf nfs |
| 185 | + (fmt-1-recur nf)) |
| 186 | + (fmt-1-recur form)) |
| 187 | + |
| 188 | + (defn fmt-1 |
| 189 | + [node] |
| 190 | + (remove-extra-newlines node) |
| 191 | + (unless (= node "\n") (flushwhite)) |
| 192 | + (match node |
| 193 | + "\n" (newline) |
| 194 | + [:comment x] (do (emit "#" x) (newline)) |
| 195 | + [:span x] (do (emit x) (addwhite)) |
| 196 | + [:string x] (do (emit-string x) (addwhite)) |
| 197 | + [:buffer x] (do (emit "@") (emit-string x) (addwhite)) |
| 198 | + [:array xs] (emit-body "@[" xs "]") |
| 199 | + [:btuple xs] (emit-body "[" xs "]") |
| 200 | + [:ptuple xs] (if (check-indent-2 xs) |
| 201 | + (emit-body "(" xs ")" 1) |
| 202 | + (emit-funcall xs)) |
| 203 | + [:struct xs] (emit-body "{" xs "}") |
| 204 | + [:table xs] (emit-body "@{" xs "}") |
| 205 | + [:rmform [rm nfs form]] (emit-rmform rm nfs form) |
| 206 | + [:top xs] (emit-body "" xs ""))) |
| 207 | + |
| 208 | + (set fmt-1-recur fmt-1) |
| 209 | + (fmt-1 tree) |
| 210 | + (newline) |
| 211 | + (flush)) |
| 212 | + |
| 213 | +# |
| 214 | +# Public API |
| 215 | +# |
| 216 | + |
| 217 | +(defn format-print |
| 218 | + "Format a string of source code and print the result." |
| 219 | + [source] |
| 220 | + (-> source make-tree fmt)) |
| 221 | + |
| 222 | +(defn format |
| 223 | + "Format a string of source code to a buffer." |
| 224 | + [source] |
| 225 | + (def out @"") |
| 226 | + (with-dyns [:out out] |
| 227 | + (format-print source)) |
| 228 | + out) |
| 229 | + |
| 230 | +(defn format-file |
| 231 | + "Format a file" |
| 232 | + [file] |
| 233 | + (def source (slurp file)) |
| 234 | + (def out (format source)) |
| 235 | + (spit file out)) |
0 commit comments