annotate src/source_print.sml @ 207:cc68da3801bc

Non-star SELECT
author Adam Chlipala <adamc@hcoop.net>
date Thu, 14 Aug 2008 18:35:08 -0400
parents dd82457fda82
children f4033abd6ab1
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@207 49 | KTuple ks => box [string "(",
adamc@207 50 p_list_sep (box [space, string "*", space]) p_kind ks,
adamc@207 51 string ")"]
adamc@1 52
adamc@1 53 and p_kind k = p_kind' false k
adamc@1 54
adamc@1 55 fun p_explicitness e =
adamc@1 56 case e of
adamc@1 57 Explicit => string "::"
adamc@1 58 | Implicit => string ":::"
adamc@1 59
adamc@1 60 fun p_con' par (c, _) =
adamc@1 61 case c of
adamc@1 62 CAnnot (c, k) => box [string "(",
adamc@1 63 p_con c,
adamc@1 64 space,
adamc@1 65 string "::",
adamc@1 66 space,
adamc@1 67 p_kind k,
adamc@1 68 string ")"]
adamc@1 69
adamc@1 70 | TFun (t1, t2) => parenIf par (box [p_con' true t1,
adamc@1 71 space,
adamc@1 72 string "->",
adamc@1 73 space,
adamc@1 74 p_con t2])
adamc@1 75 | TCFun (e, x, k, c) => parenIf par (box [string x,
adamc@1 76 space,
adamc@1 77 p_explicitness e,
adamc@1 78 space,
adamc@1 79 p_kind k,
adamc@1 80 space,
adamc@1 81 string "->",
adamc@1 82 space,
adamc@1 83 p_con c])
adamc@85 84 | TDisjoint (c1, c2, c3) => parenIf par (box [p_con c1,
adamc@85 85 space,
adamc@85 86 string "~",
adamc@85 87 space,
adamc@85 88 p_con c2,
adamc@85 89 space,
adamc@85 90 string "->",
adamc@85 91 space,
adamc@85 92 p_con c3])
adamc@1 93 | TRecord (CRecord xcs, _) => box [string "{",
adamc@1 94 p_list (fn (x, c) =>
adamc@20 95 box [p_name x,
adamc@1 96 space,
adamc@1 97 string ":",
adamc@1 98 space,
adamc@1 99 p_con c]) xcs,
adamc@1 100 string "}"]
adamc@1 101 | TRecord c => box [string "$",
adamc@1 102 p_con' true c]
adamc@1 103
adamc@34 104 | CVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@1 105 | CApp (c1, c2) => parenIf par (box [p_con c1,
adamc@1 106 space,
adamc@1 107 p_con' true c2])
adamc@67 108 | CAbs (x, NONE, c) => parenIf par (box [string "fn",
adamc@67 109 space,
adamc@67 110 string x,
adamc@67 111 space,
adamc@67 112 string "=>",
adamc@67 113 space,
adamc@67 114 p_con c])
adamc@67 115 | CAbs (x, SOME k, c) => parenIf par (box [string "fn",
adamc@67 116 space,
adamc@67 117 string x,
adamc@67 118 space,
adamc@67 119 string "::",
adamc@67 120 space,
adamc@67 121 p_kind k,
adamc@67 122 space,
adamc@67 123 string "=>",
adamc@67 124 space,
adamc@67 125 p_con c])
adamc@84 126 | CDisjoint (c1, c2, c3) => parenIf par (box [p_con c1,
adamc@84 127 space,
adamc@84 128 string "~",
adamc@84 129 space,
adamc@84 130 p_con c2,
adamc@84 131 space,
adamc@84 132 string "=>",
adamc@84 133 space,
adamc@84 134 p_con c3])
adamc@1 135
adamc@1 136 | CName s => box [string "#", string s]
adamc@1 137
adamc@1 138 | CRecord xcs => box [string "[",
adamc@1 139 p_list (fn (x, c) =>
adamc@1 140 box [p_con x,
adamc@1 141 space,
adamc@1 142 string "=",
adamc@1 143 space,
adamc@1 144 p_con c]) xcs,
adamc@1 145 string "]"]
adamc@1 146 | CConcat (c1, c2) => parenIf par (box [p_con' true c1,
adamc@1 147 space,
adamc@1 148 string "++",
adamc@1 149 space,
adamc@1 150 p_con c2])
adamc@67 151 | CFold => string "fold"
adamc@82 152
adamc@82 153 | CUnit => string "()"
adamc@82 154
adamc@18 155 | CWild k => box [string "(_",
adamc@18 156 space,
adamc@18 157 string "::",
adamc@18 158 space,
adamc@18 159 p_kind k]
adamc@207 160
adamc@207 161 | CTuple cs => box [string "(",
adamc@207 162 p_list p_con cs,
adamc@207 163 string ")"]
adamc@207 164 | CProj (c, n) => box [p_con c,
adamc@207 165 string ".",
adamc@207 166 string (Int.toString n)]
adamc@1 167
adamc@1 168 and p_con c = p_con' false c
adamc@1 169
adamc@20 170 and p_name (all as (c, _)) =
adamc@20 171 case c of
adamc@20 172 CName s => string s
adamc@20 173 | _ => p_con all
adamc@20 174
adamc@170 175 fun p_pat' par (p, _) =
adamc@170 176 case p of
adamc@170 177 PWild => string "_"
adamc@170 178 | PVar s => string s
adamc@173 179 | PPrim p => Prim.p_t p
adamc@170 180 | PCon (ms, x, NONE) => p_list_sep (string ".") string (ms @ [x])
adamc@170 181 | PCon (ms, x, SOME p) => parenIf par (box [p_list_sep (string ".") string (ms @ [x]),
adamc@170 182 space,
adamc@170 183 p_pat' true p])
adamc@174 184 | PRecord (xps, flex) =>
adamc@174 185 let
adamc@175 186 val pps = map (fn (x, p) => box [string x, space, string "=", space, p_pat p]) xps
adamc@174 187 in
adamc@174 188 box [string "{",
adamc@174 189 p_list_sep (box [string ",", space]) (fn x => x)
adamc@174 190 (if flex then
adamc@175 191 pps @ [string "..."]
adamc@174 192 else
adamc@175 193 pps),
adamc@174 194 string "}"]
adamc@174 195 end
adamc@170 196
adamc@174 197 and p_pat x = p_pat' false x
adamc@170 198
adamc@8 199 fun p_exp' par (e, _) =
adamc@8 200 case e of
adamc@8 201 EAnnot (e, t) => box [string "(",
adamc@8 202 p_exp e,
adamc@8 203 space,
adamc@8 204 string ":",
adamc@8 205 space,
adamc@8 206 p_con t,
adamc@8 207 string ")"]
adamc@8 208
adamc@14 209 | EPrim p => Prim.p_t p
adamc@34 210 | EVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@8 211 | EApp (e1, e2) => parenIf par (box [p_exp e1,
adamc@8 212 space,
adamc@8 213 p_exp' true e2])
adamc@8 214 | EAbs (x, NONE, e) => parenIf par (box [string "fn",
adamc@8 215 space,
adamc@8 216 string x,
adamc@8 217 space,
adamc@8 218 string "=>",
adamc@8 219 space,
adamc@8 220 p_exp e])
adamc@8 221 | EAbs (x, SOME t, e) => parenIf par (box [string "fn",
adamc@8 222 space,
adamc@8 223 string x,
adamc@8 224 space,
adamc@8 225 string ":",
adamc@8 226 space,
adamc@8 227 p_con t,
adamc@8 228 space,
adamc@8 229 string "=>",
adamc@8 230 space,
adamc@8 231 p_exp e])
adamc@8 232 | ECApp (e, c) => parenIf par (box [p_exp e,
adamc@8 233 space,
adamc@8 234 string "[",
adamc@8 235 p_con c,
adamc@8 236 string "]"])
adamc@8 237 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@8 238 space,
adamc@8 239 string x,
adamc@8 240 space,
adamc@8 241 p_explicitness exp,
adamc@8 242 space,
adamc@8 243 p_kind k,
adamc@8 244 space,
adamc@8 245 string "=>",
adamc@8 246 space,
adamc@8 247 p_exp e])
adamc@85 248 | EDisjoint (c1, c2, e) => parenIf par (box [p_con c1,
adamc@85 249 space,
adamc@85 250 string "~",
adamc@85 251 space,
adamc@85 252 p_con c2,
adamc@85 253 space,
adamc@85 254 string "=>",
adamc@85 255 space,
adamc@85 256 p_exp e])
adamc@8 257
adamc@12 258 | ERecord xes => box [string "{",
adamc@12 259 p_list (fn (x, e) =>
adamc@21 260 box [p_name x,
adamc@12 261 space,
adamc@12 262 string "=",
adamc@12 263 space,
adamc@12 264 p_exp e]) xes,
adamc@12 265 string "}"]
adamc@12 266 | EField (e, c) => box [p_exp' true e,
adamc@12 267 string ".",
adamc@12 268 p_con' true c]
adamc@149 269 | ECut (e, c) => parenIf par (box [p_exp' true e,
adamc@149 270 space,
adamc@149 271 string "--",
adamc@149 272 space,
adamc@149 273 p_con' true c])
adamc@71 274 | EFold => string "fold"
adamc@12 275
adamc@170 276 | ECase (e, pes) => parenIf par (box [string "case",
adamc@170 277 space,
adamc@171 278 p_exp e,
adamc@170 279 space,
adamc@170 280 string "of",
adamc@170 281 space,
adamc@170 282 p_list_sep (box [space, string "|", space])
adamc@170 283 (fn (p, e) => box [p_pat p,
adamc@170 284 space,
adamc@170 285 string "=>",
adamc@170 286 space,
adamc@170 287 p_exp e]) pes])
adamc@170 288
adamc@8 289 and p_exp e = p_exp' false e
adamc@8 290
adamc@191 291 fun p_datatype (x, xs, cons) =
adamc@156 292 box [string "datatype",
adamc@156 293 space,
adamc@156 294 string x,
adamc@191 295 p_list_sep (box []) (fn x => box [space, string x]) xs,
adamc@156 296 space,
adamc@156 297 string "=",
adamc@156 298 space,
adamc@156 299 p_list_sep (box [space, string "|", space])
adamc@156 300 (fn (x, NONE) => string x
adamc@156 301 | (x, SOME t) => box [string x, space, string "of", space, p_con t])
adamc@156 302 cons]
adamc@156 303
adamc@30 304 fun p_sgn_item (sgi, _) =
adamc@30 305 case sgi of
adamc@30 306 SgiConAbs (x, k) => box [string "con",
adamc@30 307 space,
adamc@30 308 string x,
adamc@30 309 space,
adamc@30 310 string "::",
adamc@30 311 space,
adamc@30 312 p_kind k]
adamc@30 313 | SgiCon (x, NONE, c) => box [string "con",
adamc@30 314 space,
adamc@30 315 string x,
adamc@30 316 space,
adamc@30 317 string "=",
adamc@30 318 space,
adamc@30 319 p_con c]
adamc@30 320 | SgiCon (x, SOME k, c) => box [string "con",
adamc@30 321 space,
adamc@30 322 string x,
adamc@30 323 space,
adamc@30 324 string "::",
adamc@30 325 space,
adamc@30 326 p_kind k,
adamc@30 327 space,
adamc@30 328 string "=",
adamc@30 329 space,
adamc@30 330 p_con c]
adamc@156 331 | SgiDatatype x => p_datatype x
adamc@156 332 | SgiDatatypeImp (x, ms, x') =>
adamc@156 333 box [string "datatype",
adamc@156 334 space,
adamc@156 335 string x,
adamc@156 336 space,
adamc@156 337 string "=",
adamc@156 338 space,
adamc@156 339 string "datatype",
adamc@156 340 space,
adamc@156 341 p_list_sep (string ".") string (ms @ [x'])]
adamc@30 342 | SgiVal (x, c) => box [string "val",
adamc@30 343 space,
adamc@30 344 string x,
adamc@30 345 space,
adamc@30 346 string ":",
adamc@30 347 space,
adamc@30 348 p_con c]
adamc@30 349 | SgiStr (x, sgn) => box [string "structure",
adamc@30 350 space,
adamc@30 351 string x,
adamc@30 352 space,
adamc@30 353 string ":",
adamc@30 354 space,
adamc@30 355 p_sgn sgn]
adamc@59 356 | SgiSgn (x, sgn) => box [string "signature",
adamc@59 357 space,
adamc@59 358 string x,
adamc@59 359 space,
adamc@59 360 string "=",
adamc@59 361 space,
adamc@59 362 p_sgn sgn]
adamc@58 363 | SgiInclude sgn => box [string "include",
adamc@58 364 space,
adamc@58 365 p_sgn sgn]
adamc@88 366 | SgiConstraint (c1, c2) => box [string "constraint",
adamc@88 367 space,
adamc@88 368 p_con c1,
adamc@88 369 space,
adamc@88 370 string "~",
adamc@88 371 space,
adamc@88 372 p_con c2]
adamc@203 373 | SgiTable (x, c) => box [string "table",
adamc@203 374 space,
adamc@203 375 string x,
adamc@203 376 space,
adamc@203 377 string ":",
adamc@203 378 space,
adamc@203 379 p_con c]
adamc@30 380
adamc@30 381 and p_sgn (sgn, _) =
adamc@30 382 case sgn of
adamc@30 383 SgnConst sgis => box [string "sig",
adamc@30 384 newline,
adamc@30 385 p_list_sep newline p_sgn_item sgis,
adamc@30 386 newline,
adamc@30 387 string "end"]
adamc@30 388 | SgnVar x => string x
adamc@40 389 | SgnFun (x, sgn, sgn') => box [string "functor",
adamc@40 390 space,
adamc@40 391 string "(",
adamc@40 392 string x,
adamc@40 393 space,
adamc@40 394 string ":",
adamc@40 395 p_sgn sgn,
adamc@40 396 string ")",
adamc@40 397 space,
adamc@40 398 string ":",
adamc@40 399 space,
adamc@40 400 p_sgn sgn']
adamc@42 401 | SgnWhere (sgn, x, c) => box [p_sgn sgn,
adamc@42 402 space,
adamc@42 403 string "where",
adamc@42 404 space,
adamc@42 405 string "con",
adamc@42 406 space,
adamc@42 407 string x,
adamc@42 408 space,
adamc@42 409 string "=",
adamc@42 410 space,
adamc@42 411 p_con c]
adamc@59 412 | SgnProj (m, ms, x) => p_list_sep (string ".") string (m :: ms @ [x])
adamc@59 413
adamc@42 414
adamc@123 415 fun p_vali (x, co, e) =
adamc@123 416 case co of
adamc@123 417 NONE => box [string x,
adamc@123 418 space,
adamc@123 419 string "=",
adamc@123 420 space,
adamc@123 421 p_exp e]
adamc@123 422 | SOME t => box [string x,
adamc@123 423 space,
adamc@123 424 string ":",
adamc@123 425 space,
adamc@123 426 p_con t,
adamc@123 427 space,
adamc@123 428 string "=",
adamc@123 429 space,
adamc@123 430 p_exp e]
adamc@123 431
adamc@1 432 fun p_decl ((d, _) : decl) =
adamc@1 433 case d of
adamc@1 434 DCon (x, NONE, c) => box [string "con",
adamc@1 435 space,
adamc@1 436 string x,
adamc@1 437 space,
adamc@1 438 string "=",
adamc@1 439 space,
adamc@1 440 p_con c]
adamc@1 441 | DCon (x, SOME k, c) => box [string "con",
adamc@1 442 space,
adamc@1 443 string x,
adamc@1 444 space,
adamc@1 445 string "::",
adamc@1 446 space,
adamc@1 447 p_kind k,
adamc@1 448 space,
adamc@1 449 string "=",
adamc@1 450 space,
adamc@1 451 p_con c]
adamc@156 452 | DDatatype x => p_datatype x
adamc@156 453 | DDatatypeImp (x, ms, x') =>
adamc@156 454 box [string "datatype",
adamc@156 455 space,
adamc@156 456 string x,
adamc@156 457 space,
adamc@156 458 string "=",
adamc@156 459 space,
adamc@156 460 string "datatype",
adamc@156 461 space,
adamc@156 462 p_list_sep (string ".") string (ms @ [x'])]
adamc@123 463 | DVal vi => box [string "val",
adamc@123 464 space,
adamc@123 465 p_vali vi]
adamc@123 466 | DValRec vis => box [string "val",
adamc@123 467 space,
adamc@123 468 string "rec",
adamc@123 469 space,
adamc@123 470 p_list_sep (box [newline, string "and", space]) p_vali vis]
adamc@1 471
adamc@30 472 | DSgn (x, sgn) => box [string "signature",
adamc@30 473 space,
adamc@30 474 string x,
adamc@30 475 space,
adamc@30 476 string "=",
adamc@30 477 space,
adamc@30 478 p_sgn sgn]
adamc@30 479 | DStr (x, NONE, str) => box [string "structure",
adamc@30 480 space,
adamc@30 481 string x,
adamc@30 482 space,
adamc@30 483 string "=",
adamc@30 484 space,
adamc@30 485 p_str str]
adamc@30 486 | DStr (x, SOME sgn, str) => box [string "structure",
adamc@30 487 space,
adamc@30 488 string x,
adamc@30 489 space,
adamc@30 490 string ":",
adamc@30 491 space,
adamc@30 492 p_sgn sgn,
adamc@30 493 space,
adamc@30 494 string "=",
adamc@30 495 space,
adamc@30 496 p_str str]
adamc@48 497 | DFfiStr (x, sgn) => box [string "extern",
adamc@48 498 space,
adamc@48 499 string "structure",
adamc@48 500 space,
adamc@48 501 string x,
adamc@48 502 space,
adamc@48 503 string ":",
adamc@48 504 space,
adamc@48 505 p_sgn sgn]
adamc@61 506 | DOpen (m, ms) => box [string "open",
adamc@61 507 space,
adamc@61 508 p_list_sep (string ".") string (m :: ms)]
adamc@88 509 | DConstraint (c1, c2) => box [string "constraint",
adamc@88 510 space,
adamc@88 511 p_con c1,
adamc@88 512 space,
adamc@88 513 string "~",
adamc@88 514 space,
adamc@88 515 p_con c2]
adamc@88 516 | DOpenConstraints (m, ms) => box [string "open",
adamc@88 517 space,
adamc@88 518 string "constraints",
adamc@88 519 space,
adamc@88 520 p_list_sep (string ".") string (m :: ms)]
adamc@30 521
adamc@109 522 | DExport str => box [string "export",
adamc@109 523 space,
adamc@109 524 p_str str]
adamc@203 525 | DTable (x, c) => box [string "table",
adamc@203 526 space,
adamc@203 527 string x,
adamc@203 528 space,
adamc@203 529 string ":",
adamc@203 530 space,
adamc@203 531 p_con c]
adamc@100 532
adamc@30 533 and p_str (str, _) =
adamc@30 534 case str of
adamc@30 535 StrConst ds => box [string "struct",
adamc@30 536 newline,
adamc@30 537 p_list_sep newline p_decl ds,
adamc@30 538 newline,
adamc@30 539 string "end"]
adamc@30 540 | StrVar x => string x
adamc@34 541 | StrProj (str, x) => box [p_str str,
adamc@34 542 string ".",
adamc@34 543 string x]
adamc@40 544 | StrFun (x, sgn, NONE, str) => box [string "functor",
adamc@40 545 space,
adamc@40 546 string "(",
adamc@40 547 string x,
adamc@40 548 space,
adamc@40 549 string ":",
adamc@40 550 p_sgn sgn,
adamc@40 551 string ")",
adamc@40 552 space,
adamc@40 553 string "=>",
adamc@40 554 space,
adamc@40 555 p_str str]
adamc@40 556 | StrFun (x, sgn, SOME sgn', str) => box [string "functor",
adamc@40 557 space,
adamc@40 558 string "(",
adamc@40 559 string x,
adamc@40 560 space,
adamc@40 561 string ":",
adamc@40 562 p_sgn sgn,
adamc@40 563 string ")",
adamc@40 564 space,
adamc@40 565 string ":",
adamc@40 566 space,
adamc@40 567 p_sgn sgn',
adamc@40 568 space,
adamc@40 569 string "=>",
adamc@40 570 space,
adamc@40 571 p_str str]
adamc@44 572 | StrApp (str1, str2) => box [p_str str1,
adamc@44 573 string "(",
adamc@44 574 p_str str2,
adamc@44 575 string ")"]
adamc@30 576
adamc@1 577 val p_file = p_list_sep newline p_decl
adamc@1 578
adamc@1 579 end