annotate src/source_print.sml @ 196:890a61991263

Lists all the way through
author Adam Chlipala <adamc@hcoop.net>
date Sat, 09 Aug 2008 16:48:32 -0400
parents aa54250f58ac
children dd82457fda82
rev   line source
adamc@1 1 (* Copyright (c) 2008, Adam Chlipala
adamc@1 2 * All rights reserved.
adamc@1 3 *
adamc@1 4 * Redistribution and use in source and binary forms, with or without
adamc@1 5 * modification, are permitted provided that the following conditions are met:
adamc@1 6 *
adamc@1 7 * - Redistributions of source code must retain the above copyright notice,
adamc@1 8 * this list of conditions and the following disclaimer.
adamc@1 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@1 10 * this list of conditions and the following disclaimer in the documentation
adamc@1 11 * and/or other materials provided with the distribution.
adamc@1 12 * - The names of contributors may not be used to endorse or promote products
adamc@1 13 * derived from this software without specific prior written permission.
adamc@1 14 *
adamc@1 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@1 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@1 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@1 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@1 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@1 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@1 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@1 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@1 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@1 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@1 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@1 26 *)
adamc@1 27
adamc@1 28 (* Pretty-printing Laconic/Web *)
adamc@1 29
adamc@4 30 structure SourcePrint :> SOURCE_PRINT = struct
adamc@1 31
adamc@1 32 open Print.PD
adamc@1 33 open Print
adamc@1 34
adamc@4 35 open Source
adamc@1 36
adamc@1 37 fun p_kind' par (k, _) =
adamc@1 38 case k of
adamc@1 39 KType => string "Type"
adamc@1 40 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1,
adamc@1 41 space,
adamc@1 42 string "->",
adamc@1 43 space,
adamc@1 44 p_kind k2])
adamc@1 45 | KName => string "Name"
adamc@1 46 | KRecord k => box [string "{", p_kind k, string "}"]
adamc@82 47 | KUnit => string "Unit"
adamc@18 48 | KWild => string "_"
adamc@1 49
adamc@1 50 and p_kind k = p_kind' false k
adamc@1 51
adamc@1 52 fun p_explicitness e =
adamc@1 53 case e of
adamc@1 54 Explicit => string "::"
adamc@1 55 | Implicit => string ":::"
adamc@1 56
adamc@1 57 fun p_con' par (c, _) =
adamc@1 58 case c of
adamc@1 59 CAnnot (c, k) => box [string "(",
adamc@1 60 p_con c,
adamc@1 61 space,
adamc@1 62 string "::",
adamc@1 63 space,
adamc@1 64 p_kind k,
adamc@1 65 string ")"]
adamc@1 66
adamc@1 67 | TFun (t1, t2) => parenIf par (box [p_con' true t1,
adamc@1 68 space,
adamc@1 69 string "->",
adamc@1 70 space,
adamc@1 71 p_con t2])
adamc@1 72 | TCFun (e, x, k, c) => parenIf par (box [string x,
adamc@1 73 space,
adamc@1 74 p_explicitness e,
adamc@1 75 space,
adamc@1 76 p_kind k,
adamc@1 77 space,
adamc@1 78 string "->",
adamc@1 79 space,
adamc@1 80 p_con c])
adamc@85 81 | TDisjoint (c1, c2, c3) => parenIf par (box [p_con c1,
adamc@85 82 space,
adamc@85 83 string "~",
adamc@85 84 space,
adamc@85 85 p_con c2,
adamc@85 86 space,
adamc@85 87 string "->",
adamc@85 88 space,
adamc@85 89 p_con c3])
adamc@1 90 | TRecord (CRecord xcs, _) => box [string "{",
adamc@1 91 p_list (fn (x, c) =>
adamc@20 92 box [p_name x,
adamc@1 93 space,
adamc@1 94 string ":",
adamc@1 95 space,
adamc@1 96 p_con c]) xcs,
adamc@1 97 string "}"]
adamc@1 98 | TRecord c => box [string "$",
adamc@1 99 p_con' true c]
adamc@1 100
adamc@34 101 | CVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@1 102 | CApp (c1, c2) => parenIf par (box [p_con c1,
adamc@1 103 space,
adamc@1 104 p_con' true c2])
adamc@67 105 | CAbs (x, NONE, c) => parenIf par (box [string "fn",
adamc@67 106 space,
adamc@67 107 string x,
adamc@67 108 space,
adamc@67 109 string "=>",
adamc@67 110 space,
adamc@67 111 p_con c])
adamc@67 112 | CAbs (x, SOME k, c) => parenIf par (box [string "fn",
adamc@67 113 space,
adamc@67 114 string x,
adamc@67 115 space,
adamc@67 116 string "::",
adamc@67 117 space,
adamc@67 118 p_kind k,
adamc@67 119 space,
adamc@67 120 string "=>",
adamc@67 121 space,
adamc@67 122 p_con c])
adamc@84 123 | CDisjoint (c1, c2, c3) => parenIf par (box [p_con c1,
adamc@84 124 space,
adamc@84 125 string "~",
adamc@84 126 space,
adamc@84 127 p_con c2,
adamc@84 128 space,
adamc@84 129 string "=>",
adamc@84 130 space,
adamc@84 131 p_con c3])
adamc@1 132
adamc@1 133 | CName s => box [string "#", string s]
adamc@1 134
adamc@1 135 | CRecord xcs => box [string "[",
adamc@1 136 p_list (fn (x, c) =>
adamc@1 137 box [p_con x,
adamc@1 138 space,
adamc@1 139 string "=",
adamc@1 140 space,
adamc@1 141 p_con c]) xcs,
adamc@1 142 string "]"]
adamc@1 143 | CConcat (c1, c2) => parenIf par (box [p_con' true c1,
adamc@1 144 space,
adamc@1 145 string "++",
adamc@1 146 space,
adamc@1 147 p_con c2])
adamc@67 148 | CFold => string "fold"
adamc@82 149
adamc@82 150 | CUnit => string "()"
adamc@82 151
adamc@18 152 | CWild k => box [string "(_",
adamc@18 153 space,
adamc@18 154 string "::",
adamc@18 155 space,
adamc@18 156 p_kind k]
adamc@1 157
adamc@1 158 and p_con c = p_con' false c
adamc@1 159
adamc@20 160 and p_name (all as (c, _)) =
adamc@20 161 case c of
adamc@20 162 CName s => string s
adamc@20 163 | _ => p_con all
adamc@20 164
adamc@170 165 fun p_pat' par (p, _) =
adamc@170 166 case p of
adamc@170 167 PWild => string "_"
adamc@170 168 | PVar s => string s
adamc@173 169 | PPrim p => Prim.p_t p
adamc@170 170 | PCon (ms, x, NONE) => p_list_sep (string ".") string (ms @ [x])
adamc@170 171 | PCon (ms, x, SOME p) => parenIf par (box [p_list_sep (string ".") string (ms @ [x]),
adamc@170 172 space,
adamc@170 173 p_pat' true p])
adamc@174 174 | PRecord (xps, flex) =>
adamc@174 175 let
adamc@175 176 val pps = map (fn (x, p) => box [string x, space, string "=", space, p_pat p]) xps
adamc@174 177 in
adamc@174 178 box [string "{",
adamc@174 179 p_list_sep (box [string ",", space]) (fn x => x)
adamc@174 180 (if flex then
adamc@175 181 pps @ [string "..."]
adamc@174 182 else
adamc@175 183 pps),
adamc@174 184 string "}"]
adamc@174 185 end
adamc@170 186
adamc@174 187 and p_pat x = p_pat' false x
adamc@170 188
adamc@8 189 fun p_exp' par (e, _) =
adamc@8 190 case e of
adamc@8 191 EAnnot (e, t) => box [string "(",
adamc@8 192 p_exp e,
adamc@8 193 space,
adamc@8 194 string ":",
adamc@8 195 space,
adamc@8 196 p_con t,
adamc@8 197 string ")"]
adamc@8 198
adamc@14 199 | EPrim p => Prim.p_t p
adamc@34 200 | EVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@8 201 | EApp (e1, e2) => parenIf par (box [p_exp e1,
adamc@8 202 space,
adamc@8 203 p_exp' true e2])
adamc@8 204 | EAbs (x, NONE, e) => parenIf par (box [string "fn",
adamc@8 205 space,
adamc@8 206 string x,
adamc@8 207 space,
adamc@8 208 string "=>",
adamc@8 209 space,
adamc@8 210 p_exp e])
adamc@8 211 | EAbs (x, SOME t, e) => parenIf par (box [string "fn",
adamc@8 212 space,
adamc@8 213 string x,
adamc@8 214 space,
adamc@8 215 string ":",
adamc@8 216 space,
adamc@8 217 p_con t,
adamc@8 218 space,
adamc@8 219 string "=>",
adamc@8 220 space,
adamc@8 221 p_exp e])
adamc@8 222 | ECApp (e, c) => parenIf par (box [p_exp e,
adamc@8 223 space,
adamc@8 224 string "[",
adamc@8 225 p_con c,
adamc@8 226 string "]"])
adamc@8 227 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@8 228 space,
adamc@8 229 string x,
adamc@8 230 space,
adamc@8 231 p_explicitness exp,
adamc@8 232 space,
adamc@8 233 p_kind k,
adamc@8 234 space,
adamc@8 235 string "=>",
adamc@8 236 space,
adamc@8 237 p_exp e])
adamc@85 238 | EDisjoint (c1, c2, e) => parenIf par (box [p_con c1,
adamc@85 239 space,
adamc@85 240 string "~",
adamc@85 241 space,
adamc@85 242 p_con c2,
adamc@85 243 space,
adamc@85 244 string "=>",
adamc@85 245 space,
adamc@85 246 p_exp e])
adamc@8 247
adamc@12 248 | ERecord xes => box [string "{",
adamc@12 249 p_list (fn (x, e) =>
adamc@21 250 box [p_name x,
adamc@12 251 space,
adamc@12 252 string "=",
adamc@12 253 space,
adamc@12 254 p_exp e]) xes,
adamc@12 255 string "}"]
adamc@12 256 | EField (e, c) => box [p_exp' true e,
adamc@12 257 string ".",
adamc@12 258 p_con' true c]
adamc@149 259 | ECut (e, c) => parenIf par (box [p_exp' true e,
adamc@149 260 space,
adamc@149 261 string "--",
adamc@149 262 space,
adamc@149 263 p_con' true c])
adamc@71 264 | EFold => string "fold"
adamc@12 265
adamc@170 266 | ECase (e, pes) => parenIf par (box [string "case",
adamc@170 267 space,
adamc@171 268 p_exp e,
adamc@170 269 space,
adamc@170 270 string "of",
adamc@170 271 space,
adamc@170 272 p_list_sep (box [space, string "|", space])
adamc@170 273 (fn (p, e) => box [p_pat p,
adamc@170 274 space,
adamc@170 275 string "=>",
adamc@170 276 space,
adamc@170 277 p_exp e]) pes])
adamc@170 278
adamc@8 279 and p_exp e = p_exp' false e
adamc@8 280
adamc@191 281 fun p_datatype (x, xs, cons) =
adamc@156 282 box [string "datatype",
adamc@156 283 space,
adamc@156 284 string x,
adamc@191 285 p_list_sep (box []) (fn x => box [space, string x]) xs,
adamc@156 286 space,
adamc@156 287 string "=",
adamc@156 288 space,
adamc@156 289 p_list_sep (box [space, string "|", space])
adamc@156 290 (fn (x, NONE) => string x
adamc@156 291 | (x, SOME t) => box [string x, space, string "of", space, p_con t])
adamc@156 292 cons]
adamc@156 293
adamc@30 294 fun p_sgn_item (sgi, _) =
adamc@30 295 case sgi of
adamc@30 296 SgiConAbs (x, k) => box [string "con",
adamc@30 297 space,
adamc@30 298 string x,
adamc@30 299 space,
adamc@30 300 string "::",
adamc@30 301 space,
adamc@30 302 p_kind k]
adamc@30 303 | SgiCon (x, NONE, c) => box [string "con",
adamc@30 304 space,
adamc@30 305 string x,
adamc@30 306 space,
adamc@30 307 string "=",
adamc@30 308 space,
adamc@30 309 p_con c]
adamc@30 310 | SgiCon (x, SOME k, c) => box [string "con",
adamc@30 311 space,
adamc@30 312 string x,
adamc@30 313 space,
adamc@30 314 string "::",
adamc@30 315 space,
adamc@30 316 p_kind k,
adamc@30 317 space,
adamc@30 318 string "=",
adamc@30 319 space,
adamc@30 320 p_con c]
adamc@156 321 | SgiDatatype x => p_datatype x
adamc@156 322 | SgiDatatypeImp (x, ms, x') =>
adamc@156 323 box [string "datatype",
adamc@156 324 space,
adamc@156 325 string x,
adamc@156 326 space,
adamc@156 327 string "=",
adamc@156 328 space,
adamc@156 329 string "datatype",
adamc@156 330 space,
adamc@156 331 p_list_sep (string ".") string (ms @ [x'])]
adamc@30 332 | SgiVal (x, c) => box [string "val",
adamc@30 333 space,
adamc@30 334 string x,
adamc@30 335 space,
adamc@30 336 string ":",
adamc@30 337 space,
adamc@30 338 p_con c]
adamc@30 339 | SgiStr (x, sgn) => box [string "structure",
adamc@30 340 space,
adamc@30 341 string x,
adamc@30 342 space,
adamc@30 343 string ":",
adamc@30 344 space,
adamc@30 345 p_sgn sgn]
adamc@59 346 | SgiSgn (x, sgn) => box [string "signature",
adamc@59 347 space,
adamc@59 348 string x,
adamc@59 349 space,
adamc@59 350 string "=",
adamc@59 351 space,
adamc@59 352 p_sgn sgn]
adamc@58 353 | SgiInclude sgn => box [string "include",
adamc@58 354 space,
adamc@58 355 p_sgn sgn]
adamc@88 356 | SgiConstraint (c1, c2) => box [string "constraint",
adamc@88 357 space,
adamc@88 358 p_con c1,
adamc@88 359 space,
adamc@88 360 string "~",
adamc@88 361 space,
adamc@88 362 p_con c2]
adamc@30 363
adamc@30 364 and p_sgn (sgn, _) =
adamc@30 365 case sgn of
adamc@30 366 SgnConst sgis => box [string "sig",
adamc@30 367 newline,
adamc@30 368 p_list_sep newline p_sgn_item sgis,
adamc@30 369 newline,
adamc@30 370 string "end"]
adamc@30 371 | SgnVar x => string x
adamc@40 372 | SgnFun (x, sgn, sgn') => box [string "functor",
adamc@40 373 space,
adamc@40 374 string "(",
adamc@40 375 string x,
adamc@40 376 space,
adamc@40 377 string ":",
adamc@40 378 p_sgn sgn,
adamc@40 379 string ")",
adamc@40 380 space,
adamc@40 381 string ":",
adamc@40 382 space,
adamc@40 383 p_sgn sgn']
adamc@42 384 | SgnWhere (sgn, x, c) => box [p_sgn sgn,
adamc@42 385 space,
adamc@42 386 string "where",
adamc@42 387 space,
adamc@42 388 string "con",
adamc@42 389 space,
adamc@42 390 string x,
adamc@42 391 space,
adamc@42 392 string "=",
adamc@42 393 space,
adamc@42 394 p_con c]
adamc@59 395 | SgnProj (m, ms, x) => p_list_sep (string ".") string (m :: ms @ [x])
adamc@59 396
adamc@42 397
adamc@123 398 fun p_vali (x, co, e) =
adamc@123 399 case co of
adamc@123 400 NONE => box [string x,
adamc@123 401 space,
adamc@123 402 string "=",
adamc@123 403 space,
adamc@123 404 p_exp e]
adamc@123 405 | SOME t => box [string x,
adamc@123 406 space,
adamc@123 407 string ":",
adamc@123 408 space,
adamc@123 409 p_con t,
adamc@123 410 space,
adamc@123 411 string "=",
adamc@123 412 space,
adamc@123 413 p_exp e]
adamc@123 414
adamc@1 415 fun p_decl ((d, _) : decl) =
adamc@1 416 case d of
adamc@1 417 DCon (x, NONE, c) => box [string "con",
adamc@1 418 space,
adamc@1 419 string x,
adamc@1 420 space,
adamc@1 421 string "=",
adamc@1 422 space,
adamc@1 423 p_con c]
adamc@1 424 | DCon (x, SOME k, c) => box [string "con",
adamc@1 425 space,
adamc@1 426 string x,
adamc@1 427 space,
adamc@1 428 string "::",
adamc@1 429 space,
adamc@1 430 p_kind k,
adamc@1 431 space,
adamc@1 432 string "=",
adamc@1 433 space,
adamc@1 434 p_con c]
adamc@156 435 | DDatatype x => p_datatype x
adamc@156 436 | DDatatypeImp (x, ms, x') =>
adamc@156 437 box [string "datatype",
adamc@156 438 space,
adamc@156 439 string x,
adamc@156 440 space,
adamc@156 441 string "=",
adamc@156 442 space,
adamc@156 443 string "datatype",
adamc@156 444 space,
adamc@156 445 p_list_sep (string ".") string (ms @ [x'])]
adamc@123 446 | DVal vi => box [string "val",
adamc@123 447 space,
adamc@123 448 p_vali vi]
adamc@123 449 | DValRec vis => box [string "val",
adamc@123 450 space,
adamc@123 451 string "rec",
adamc@123 452 space,
adamc@123 453 p_list_sep (box [newline, string "and", space]) p_vali vis]
adamc@1 454
adamc@30 455 | DSgn (x, sgn) => box [string "signature",
adamc@30 456 space,
adamc@30 457 string x,
adamc@30 458 space,
adamc@30 459 string "=",
adamc@30 460 space,
adamc@30 461 p_sgn sgn]
adamc@30 462 | DStr (x, NONE, str) => box [string "structure",
adamc@30 463 space,
adamc@30 464 string x,
adamc@30 465 space,
adamc@30 466 string "=",
adamc@30 467 space,
adamc@30 468 p_str str]
adamc@30 469 | DStr (x, SOME sgn, str) => box [string "structure",
adamc@30 470 space,
adamc@30 471 string x,
adamc@30 472 space,
adamc@30 473 string ":",
adamc@30 474 space,
adamc@30 475 p_sgn sgn,
adamc@30 476 space,
adamc@30 477 string "=",
adamc@30 478 space,
adamc@30 479 p_str str]
adamc@48 480 | DFfiStr (x, sgn) => box [string "extern",
adamc@48 481 space,
adamc@48 482 string "structure",
adamc@48 483 space,
adamc@48 484 string x,
adamc@48 485 space,
adamc@48 486 string ":",
adamc@48 487 space,
adamc@48 488 p_sgn sgn]
adamc@61 489 | DOpen (m, ms) => box [string "open",
adamc@61 490 space,
adamc@61 491 p_list_sep (string ".") string (m :: ms)]
adamc@88 492 | DConstraint (c1, c2) => box [string "constraint",
adamc@88 493 space,
adamc@88 494 p_con c1,
adamc@88 495 space,
adamc@88 496 string "~",
adamc@88 497 space,
adamc@88 498 p_con c2]
adamc@88 499 | DOpenConstraints (m, ms) => box [string "open",
adamc@88 500 space,
adamc@88 501 string "constraints",
adamc@88 502 space,
adamc@88 503 p_list_sep (string ".") string (m :: ms)]
adamc@30 504
adamc@109 505 | DExport str => box [string "export",
adamc@109 506 space,
adamc@109 507 p_str str]
adamc@100 508
adamc@30 509 and p_str (str, _) =
adamc@30 510 case str of
adamc@30 511 StrConst ds => box [string "struct",
adamc@30 512 newline,
adamc@30 513 p_list_sep newline p_decl ds,
adamc@30 514 newline,
adamc@30 515 string "end"]
adamc@30 516 | StrVar x => string x
adamc@34 517 | StrProj (str, x) => box [p_str str,
adamc@34 518 string ".",
adamc@34 519 string x]
adamc@40 520 | StrFun (x, sgn, NONE, str) => box [string "functor",
adamc@40 521 space,
adamc@40 522 string "(",
adamc@40 523 string x,
adamc@40 524 space,
adamc@40 525 string ":",
adamc@40 526 p_sgn sgn,
adamc@40 527 string ")",
adamc@40 528 space,
adamc@40 529 string "=>",
adamc@40 530 space,
adamc@40 531 p_str str]
adamc@40 532 | StrFun (x, sgn, SOME sgn', str) => box [string "functor",
adamc@40 533 space,
adamc@40 534 string "(",
adamc@40 535 string x,
adamc@40 536 space,
adamc@40 537 string ":",
adamc@40 538 p_sgn sgn,
adamc@40 539 string ")",
adamc@40 540 space,
adamc@40 541 string ":",
adamc@40 542 space,
adamc@40 543 p_sgn sgn',
adamc@40 544 space,
adamc@40 545 string "=>",
adamc@40 546 space,
adamc@40 547 p_str str]
adamc@44 548 | StrApp (str1, str2) => box [p_str str1,
adamc@44 549 string "(",
adamc@44 550 p_str str2,
adamc@44 551 string ")"]
adamc@30 552
adamc@1 553 val p_file = p_list_sep newline p_decl
adamc@1 554
adamc@1 555 end