annotate src/source_print.sml @ 270:b9b02613c0c2

Parsing jobs
author Adam Chlipala <adamc@hcoop.net>
date Tue, 02 Sep 2008 10:31:16 -0400
parents 71bafe66dbe1
children 42dfb0d61cf0
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@244 28 (* Pretty-printing Ur/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@223 159 p_kind k,
adamc@223 160 string ")"]
adamc@207 161
adamc@207 162 | CTuple cs => box [string "(",
adamc@207 163 p_list p_con cs,
adamc@207 164 string ")"]
adamc@207 165 | CProj (c, n) => box [p_con c,
adamc@207 166 string ".",
adamc@207 167 string (Int.toString n)]
adamc@1 168
adamc@1 169 and p_con c = p_con' false c
adamc@1 170
adamc@20 171 and p_name (all as (c, _)) =
adamc@20 172 case c of
adamc@20 173 CName s => string s
adamc@20 174 | _ => p_con all
adamc@20 175
adamc@170 176 fun p_pat' par (p, _) =
adamc@170 177 case p of
adamc@170 178 PWild => string "_"
adamc@170 179 | PVar s => string s
adamc@173 180 | PPrim p => Prim.p_t p
adamc@170 181 | PCon (ms, x, NONE) => p_list_sep (string ".") string (ms @ [x])
adamc@170 182 | PCon (ms, x, SOME p) => parenIf par (box [p_list_sep (string ".") string (ms @ [x]),
adamc@170 183 space,
adamc@170 184 p_pat' true p])
adamc@174 185 | PRecord (xps, flex) =>
adamc@174 186 let
adamc@175 187 val pps = map (fn (x, p) => box [string x, space, string "=", space, p_pat p]) xps
adamc@174 188 in
adamc@174 189 box [string "{",
adamc@174 190 p_list_sep (box [string ",", space]) (fn x => x)
adamc@174 191 (if flex then
adamc@175 192 pps @ [string "..."]
adamc@174 193 else
adamc@175 194 pps),
adamc@174 195 string "}"]
adamc@174 196 end
adamc@170 197
adamc@174 198 and p_pat x = p_pat' false x
adamc@170 199
adamc@8 200 fun p_exp' par (e, _) =
adamc@8 201 case e of
adamc@8 202 EAnnot (e, t) => box [string "(",
adamc@8 203 p_exp e,
adamc@8 204 space,
adamc@8 205 string ":",
adamc@8 206 space,
adamc@8 207 p_con t,
adamc@8 208 string ")"]
adamc@8 209
adamc@14 210 | EPrim p => Prim.p_t p
adamc@34 211 | EVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@8 212 | EApp (e1, e2) => parenIf par (box [p_exp e1,
adamc@8 213 space,
adamc@8 214 p_exp' true e2])
adamc@8 215 | EAbs (x, NONE, e) => parenIf par (box [string "fn",
adamc@8 216 space,
adamc@8 217 string x,
adamc@8 218 space,
adamc@8 219 string "=>",
adamc@8 220 space,
adamc@8 221 p_exp e])
adamc@8 222 | EAbs (x, SOME t, e) => parenIf par (box [string "fn",
adamc@8 223 space,
adamc@8 224 string x,
adamc@8 225 space,
adamc@8 226 string ":",
adamc@8 227 space,
adamc@8 228 p_con t,
adamc@8 229 space,
adamc@8 230 string "=>",
adamc@8 231 space,
adamc@8 232 p_exp e])
adamc@8 233 | ECApp (e, c) => parenIf par (box [p_exp e,
adamc@8 234 space,
adamc@8 235 string "[",
adamc@8 236 p_con c,
adamc@8 237 string "]"])
adamc@8 238 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@8 239 space,
adamc@8 240 string x,
adamc@8 241 space,
adamc@8 242 p_explicitness exp,
adamc@8 243 space,
adamc@8 244 p_kind k,
adamc@8 245 space,
adamc@8 246 string "=>",
adamc@8 247 space,
adamc@8 248 p_exp e])
adamc@85 249 | EDisjoint (c1, c2, e) => parenIf par (box [p_con c1,
adamc@85 250 space,
adamc@85 251 string "~",
adamc@85 252 space,
adamc@85 253 p_con c2,
adamc@85 254 space,
adamc@85 255 string "=>",
adamc@85 256 space,
adamc@85 257 p_exp e])
adamc@8 258
adamc@12 259 | ERecord xes => box [string "{",
adamc@12 260 p_list (fn (x, e) =>
adamc@21 261 box [p_name x,
adamc@12 262 space,
adamc@12 263 string "=",
adamc@12 264 space,
adamc@12 265 p_exp e]) xes,
adamc@12 266 string "}"]
adamc@12 267 | EField (e, c) => box [p_exp' true e,
adamc@12 268 string ".",
adamc@12 269 p_con' true c]
adamc@149 270 | ECut (e, c) => parenIf par (box [p_exp' true e,
adamc@149 271 space,
adamc@149 272 string "--",
adamc@149 273 space,
adamc@149 274 p_con' true c])
adamc@71 275 | EFold => string "fold"
adamc@12 276
adamc@170 277 | ECase (e, pes) => parenIf par (box [string "case",
adamc@170 278 space,
adamc@171 279 p_exp e,
adamc@170 280 space,
adamc@170 281 string "of",
adamc@170 282 space,
adamc@170 283 p_list_sep (box [space, string "|", space])
adamc@170 284 (fn (p, e) => box [p_pat p,
adamc@170 285 space,
adamc@170 286 string "=>",
adamc@170 287 space,
adamc@170 288 p_exp e]) pes])
adamc@170 289
adamc@219 290 | EWild => string "_"
adamc@210 291
adamc@8 292 and p_exp e = p_exp' false e
adamc@8 293
adamc@191 294 fun p_datatype (x, xs, cons) =
adamc@156 295 box [string "datatype",
adamc@156 296 space,
adamc@156 297 string x,
adamc@191 298 p_list_sep (box []) (fn x => box [space, string x]) xs,
adamc@156 299 space,
adamc@156 300 string "=",
adamc@156 301 space,
adamc@156 302 p_list_sep (box [space, string "|", space])
adamc@156 303 (fn (x, NONE) => string x
adamc@156 304 | (x, SOME t) => box [string x, space, string "of", space, p_con t])
adamc@156 305 cons]
adamc@156 306
adamc@30 307 fun p_sgn_item (sgi, _) =
adamc@30 308 case sgi of
adamc@30 309 SgiConAbs (x, k) => box [string "con",
adamc@30 310 space,
adamc@30 311 string x,
adamc@30 312 space,
adamc@30 313 string "::",
adamc@30 314 space,
adamc@30 315 p_kind k]
adamc@30 316 | SgiCon (x, NONE, c) => box [string "con",
adamc@30 317 space,
adamc@30 318 string x,
adamc@30 319 space,
adamc@30 320 string "=",
adamc@30 321 space,
adamc@30 322 p_con c]
adamc@30 323 | SgiCon (x, SOME k, c) => box [string "con",
adamc@30 324 space,
adamc@30 325 string x,
adamc@30 326 space,
adamc@30 327 string "::",
adamc@30 328 space,
adamc@30 329 p_kind k,
adamc@30 330 space,
adamc@30 331 string "=",
adamc@30 332 space,
adamc@30 333 p_con c]
adamc@156 334 | SgiDatatype x => p_datatype x
adamc@156 335 | SgiDatatypeImp (x, ms, x') =>
adamc@156 336 box [string "datatype",
adamc@156 337 space,
adamc@156 338 string x,
adamc@156 339 space,
adamc@156 340 string "=",
adamc@156 341 space,
adamc@156 342 string "datatype",
adamc@156 343 space,
adamc@156 344 p_list_sep (string ".") string (ms @ [x'])]
adamc@30 345 | SgiVal (x, c) => box [string "val",
adamc@30 346 space,
adamc@30 347 string x,
adamc@30 348 space,
adamc@30 349 string ":",
adamc@30 350 space,
adamc@30 351 p_con c]
adamc@30 352 | SgiStr (x, sgn) => box [string "structure",
adamc@30 353 space,
adamc@30 354 string x,
adamc@30 355 space,
adamc@30 356 string ":",
adamc@30 357 space,
adamc@30 358 p_sgn sgn]
adamc@59 359 | SgiSgn (x, sgn) => box [string "signature",
adamc@59 360 space,
adamc@59 361 string x,
adamc@59 362 space,
adamc@59 363 string "=",
adamc@59 364 space,
adamc@59 365 p_sgn sgn]
adamc@58 366 | SgiInclude sgn => box [string "include",
adamc@58 367 space,
adamc@58 368 p_sgn sgn]
adamc@88 369 | SgiConstraint (c1, c2) => box [string "constraint",
adamc@88 370 space,
adamc@88 371 p_con c1,
adamc@88 372 space,
adamc@88 373 string "~",
adamc@88 374 space,
adamc@88 375 p_con c2]
adamc@203 376 | SgiTable (x, c) => box [string "table",
adamc@203 377 space,
adamc@203 378 string x,
adamc@203 379 space,
adamc@203 380 string ":",
adamc@203 381 space,
adamc@203 382 p_con c]
adamc@211 383 | SgiClassAbs x => box [string "class",
adamc@211 384 space,
adamc@211 385 string x]
adamc@211 386 | SgiClass (x, c) => box [string "class",
adamc@211 387 space,
adamc@211 388 string x,
adamc@211 389 space,
adamc@211 390 string "=",
adamc@211 391 space,
adamc@211 392 p_con c]
adamc@30 393
adamc@30 394 and p_sgn (sgn, _) =
adamc@30 395 case sgn of
adamc@30 396 SgnConst sgis => box [string "sig",
adamc@30 397 newline,
adamc@30 398 p_list_sep newline p_sgn_item sgis,
adamc@30 399 newline,
adamc@30 400 string "end"]
adamc@30 401 | SgnVar x => string x
adamc@40 402 | SgnFun (x, sgn, sgn') => box [string "functor",
adamc@40 403 space,
adamc@40 404 string "(",
adamc@40 405 string x,
adamc@40 406 space,
adamc@40 407 string ":",
adamc@40 408 p_sgn sgn,
adamc@40 409 string ")",
adamc@40 410 space,
adamc@40 411 string ":",
adamc@40 412 space,
adamc@40 413 p_sgn sgn']
adamc@42 414 | SgnWhere (sgn, x, c) => box [p_sgn sgn,
adamc@42 415 space,
adamc@42 416 string "where",
adamc@42 417 space,
adamc@42 418 string "con",
adamc@42 419 space,
adamc@42 420 string x,
adamc@42 421 space,
adamc@42 422 string "=",
adamc@42 423 space,
adamc@42 424 p_con c]
adamc@59 425 | SgnProj (m, ms, x) => p_list_sep (string ".") string (m :: ms @ [x])
adamc@59 426
adamc@42 427
adamc@123 428 fun p_vali (x, co, e) =
adamc@123 429 case co of
adamc@123 430 NONE => box [string x,
adamc@123 431 space,
adamc@123 432 string "=",
adamc@123 433 space,
adamc@123 434 p_exp e]
adamc@123 435 | SOME t => box [string x,
adamc@123 436 space,
adamc@123 437 string ":",
adamc@123 438 space,
adamc@123 439 p_con t,
adamc@123 440 space,
adamc@123 441 string "=",
adamc@123 442 space,
adamc@123 443 p_exp e]
adamc@123 444
adamc@1 445 fun p_decl ((d, _) : decl) =
adamc@1 446 case d of
adamc@1 447 DCon (x, NONE, c) => box [string "con",
adamc@1 448 space,
adamc@1 449 string x,
adamc@1 450 space,
adamc@1 451 string "=",
adamc@1 452 space,
adamc@1 453 p_con c]
adamc@1 454 | DCon (x, SOME k, c) => box [string "con",
adamc@1 455 space,
adamc@1 456 string x,
adamc@1 457 space,
adamc@1 458 string "::",
adamc@1 459 space,
adamc@1 460 p_kind k,
adamc@1 461 space,
adamc@1 462 string "=",
adamc@1 463 space,
adamc@1 464 p_con c]
adamc@156 465 | DDatatype x => p_datatype x
adamc@156 466 | DDatatypeImp (x, ms, x') =>
adamc@156 467 box [string "datatype",
adamc@156 468 space,
adamc@156 469 string x,
adamc@156 470 space,
adamc@156 471 string "=",
adamc@156 472 space,
adamc@156 473 string "datatype",
adamc@156 474 space,
adamc@156 475 p_list_sep (string ".") string (ms @ [x'])]
adamc@123 476 | DVal vi => box [string "val",
adamc@123 477 space,
adamc@123 478 p_vali vi]
adamc@123 479 | DValRec vis => box [string "val",
adamc@123 480 space,
adamc@123 481 string "rec",
adamc@123 482 space,
adamc@123 483 p_list_sep (box [newline, string "and", space]) p_vali vis]
adamc@1 484
adamc@30 485 | DSgn (x, sgn) => box [string "signature",
adamc@30 486 space,
adamc@30 487 string x,
adamc@30 488 space,
adamc@30 489 string "=",
adamc@30 490 space,
adamc@30 491 p_sgn sgn]
adamc@30 492 | DStr (x, NONE, str) => box [string "structure",
adamc@30 493 space,
adamc@30 494 string x,
adamc@30 495 space,
adamc@30 496 string "=",
adamc@30 497 space,
adamc@30 498 p_str str]
adamc@30 499 | DStr (x, SOME sgn, str) => box [string "structure",
adamc@30 500 space,
adamc@30 501 string x,
adamc@30 502 space,
adamc@30 503 string ":",
adamc@30 504 space,
adamc@30 505 p_sgn sgn,
adamc@30 506 space,
adamc@30 507 string "=",
adamc@30 508 space,
adamc@30 509 p_str str]
adamc@48 510 | DFfiStr (x, sgn) => box [string "extern",
adamc@48 511 space,
adamc@48 512 string "structure",
adamc@48 513 space,
adamc@48 514 string x,
adamc@48 515 space,
adamc@48 516 string ":",
adamc@48 517 space,
adamc@48 518 p_sgn sgn]
adamc@61 519 | DOpen (m, ms) => box [string "open",
adamc@61 520 space,
adamc@61 521 p_list_sep (string ".") string (m :: ms)]
adamc@88 522 | DConstraint (c1, c2) => box [string "constraint",
adamc@88 523 space,
adamc@88 524 p_con c1,
adamc@88 525 space,
adamc@88 526 string "~",
adamc@88 527 space,
adamc@88 528 p_con c2]
adamc@88 529 | DOpenConstraints (m, ms) => box [string "open",
adamc@88 530 space,
adamc@88 531 string "constraints",
adamc@88 532 space,
adamc@88 533 p_list_sep (string ".") string (m :: ms)]
adamc@30 534
adamc@109 535 | DExport str => box [string "export",
adamc@109 536 space,
adamc@109 537 p_str str]
adamc@203 538 | DTable (x, c) => box [string "table",
adamc@203 539 space,
adamc@203 540 string x,
adamc@203 541 space,
adamc@203 542 string ":",
adamc@203 543 space,
adamc@203 544 p_con c]
adamc@211 545 | DClass (x, c) => box [string "class",
adamc@211 546 space,
adamc@211 547 string x,
adamc@211 548 space,
adamc@211 549 string "=",
adamc@211 550 space,
adamc@211 551 p_con c]
adamc@100 552
adamc@30 553 and p_str (str, _) =
adamc@30 554 case str of
adamc@30 555 StrConst ds => box [string "struct",
adamc@30 556 newline,
adamc@30 557 p_list_sep newline p_decl ds,
adamc@30 558 newline,
adamc@30 559 string "end"]
adamc@30 560 | StrVar x => string x
adamc@34 561 | StrProj (str, x) => box [p_str str,
adamc@34 562 string ".",
adamc@34 563 string x]
adamc@40 564 | StrFun (x, sgn, NONE, str) => box [string "functor",
adamc@40 565 space,
adamc@40 566 string "(",
adamc@40 567 string x,
adamc@40 568 space,
adamc@40 569 string ":",
adamc@40 570 p_sgn sgn,
adamc@40 571 string ")",
adamc@40 572 space,
adamc@40 573 string "=>",
adamc@40 574 space,
adamc@40 575 p_str str]
adamc@40 576 | StrFun (x, sgn, SOME sgn', str) => box [string "functor",
adamc@40 577 space,
adamc@40 578 string "(",
adamc@40 579 string x,
adamc@40 580 space,
adamc@40 581 string ":",
adamc@40 582 p_sgn sgn,
adamc@40 583 string ")",
adamc@40 584 space,
adamc@40 585 string ":",
adamc@40 586 space,
adamc@40 587 p_sgn sgn',
adamc@40 588 space,
adamc@40 589 string "=>",
adamc@40 590 space,
adamc@40 591 p_str str]
adamc@44 592 | StrApp (str1, str2) => box [p_str str1,
adamc@44 593 string "(",
adamc@44 594 p_str str2,
adamc@44 595 string ")"]
adamc@30 596
adamc@1 597 val p_file = p_list_sep newline p_decl
adamc@1 598
adamc@1 599 end