annotate src/source_print.sml @ 203:dd82457fda82

Parsing and elaborating 'table'
author Adam Chlipala <adamc@hcoop.net>
date Thu, 14 Aug 2008 13:20:29 -0400
parents aa54250f58ac
children cc68da3801bc
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@203 363 | SgiTable (x, c) => box [string "table",
adamc@203 364 space,
adamc@203 365 string x,
adamc@203 366 space,
adamc@203 367 string ":",
adamc@203 368 space,
adamc@203 369 p_con c]
adamc@30 370
adamc@30 371 and p_sgn (sgn, _) =
adamc@30 372 case sgn of
adamc@30 373 SgnConst sgis => box [string "sig",
adamc@30 374 newline,
adamc@30 375 p_list_sep newline p_sgn_item sgis,
adamc@30 376 newline,
adamc@30 377 string "end"]
adamc@30 378 | SgnVar x => string x
adamc@40 379 | SgnFun (x, sgn, sgn') => box [string "functor",
adamc@40 380 space,
adamc@40 381 string "(",
adamc@40 382 string x,
adamc@40 383 space,
adamc@40 384 string ":",
adamc@40 385 p_sgn sgn,
adamc@40 386 string ")",
adamc@40 387 space,
adamc@40 388 string ":",
adamc@40 389 space,
adamc@40 390 p_sgn sgn']
adamc@42 391 | SgnWhere (sgn, x, c) => box [p_sgn sgn,
adamc@42 392 space,
adamc@42 393 string "where",
adamc@42 394 space,
adamc@42 395 string "con",
adamc@42 396 space,
adamc@42 397 string x,
adamc@42 398 space,
adamc@42 399 string "=",
adamc@42 400 space,
adamc@42 401 p_con c]
adamc@59 402 | SgnProj (m, ms, x) => p_list_sep (string ".") string (m :: ms @ [x])
adamc@59 403
adamc@42 404
adamc@123 405 fun p_vali (x, co, e) =
adamc@123 406 case co of
adamc@123 407 NONE => box [string x,
adamc@123 408 space,
adamc@123 409 string "=",
adamc@123 410 space,
adamc@123 411 p_exp e]
adamc@123 412 | SOME t => box [string x,
adamc@123 413 space,
adamc@123 414 string ":",
adamc@123 415 space,
adamc@123 416 p_con t,
adamc@123 417 space,
adamc@123 418 string "=",
adamc@123 419 space,
adamc@123 420 p_exp e]
adamc@123 421
adamc@1 422 fun p_decl ((d, _) : decl) =
adamc@1 423 case d of
adamc@1 424 DCon (x, NONE, 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_con c]
adamc@1 431 | DCon (x, SOME k, c) => box [string "con",
adamc@1 432 space,
adamc@1 433 string x,
adamc@1 434 space,
adamc@1 435 string "::",
adamc@1 436 space,
adamc@1 437 p_kind k,
adamc@1 438 space,
adamc@1 439 string "=",
adamc@1 440 space,
adamc@1 441 p_con c]
adamc@156 442 | DDatatype x => p_datatype x
adamc@156 443 | DDatatypeImp (x, ms, x') =>
adamc@156 444 box [string "datatype",
adamc@156 445 space,
adamc@156 446 string x,
adamc@156 447 space,
adamc@156 448 string "=",
adamc@156 449 space,
adamc@156 450 string "datatype",
adamc@156 451 space,
adamc@156 452 p_list_sep (string ".") string (ms @ [x'])]
adamc@123 453 | DVal vi => box [string "val",
adamc@123 454 space,
adamc@123 455 p_vali vi]
adamc@123 456 | DValRec vis => box [string "val",
adamc@123 457 space,
adamc@123 458 string "rec",
adamc@123 459 space,
adamc@123 460 p_list_sep (box [newline, string "and", space]) p_vali vis]
adamc@1 461
adamc@30 462 | DSgn (x, sgn) => box [string "signature",
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_sgn sgn]
adamc@30 469 | DStr (x, NONE, 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_str str]
adamc@30 476 | DStr (x, SOME sgn, str) => box [string "structure",
adamc@30 477 space,
adamc@30 478 string x,
adamc@30 479 space,
adamc@30 480 string ":",
adamc@30 481 space,
adamc@30 482 p_sgn sgn,
adamc@30 483 space,
adamc@30 484 string "=",
adamc@30 485 space,
adamc@30 486 p_str str]
adamc@48 487 | DFfiStr (x, sgn) => box [string "extern",
adamc@48 488 space,
adamc@48 489 string "structure",
adamc@48 490 space,
adamc@48 491 string x,
adamc@48 492 space,
adamc@48 493 string ":",
adamc@48 494 space,
adamc@48 495 p_sgn sgn]
adamc@61 496 | DOpen (m, ms) => box [string "open",
adamc@61 497 space,
adamc@61 498 p_list_sep (string ".") string (m :: ms)]
adamc@88 499 | DConstraint (c1, c2) => box [string "constraint",
adamc@88 500 space,
adamc@88 501 p_con c1,
adamc@88 502 space,
adamc@88 503 string "~",
adamc@88 504 space,
adamc@88 505 p_con c2]
adamc@88 506 | DOpenConstraints (m, ms) => box [string "open",
adamc@88 507 space,
adamc@88 508 string "constraints",
adamc@88 509 space,
adamc@88 510 p_list_sep (string ".") string (m :: ms)]
adamc@30 511
adamc@109 512 | DExport str => box [string "export",
adamc@109 513 space,
adamc@109 514 p_str str]
adamc@203 515 | DTable (x, c) => box [string "table",
adamc@203 516 space,
adamc@203 517 string x,
adamc@203 518 space,
adamc@203 519 string ":",
adamc@203 520 space,
adamc@203 521 p_con c]
adamc@100 522
adamc@30 523 and p_str (str, _) =
adamc@30 524 case str of
adamc@30 525 StrConst ds => box [string "struct",
adamc@30 526 newline,
adamc@30 527 p_list_sep newline p_decl ds,
adamc@30 528 newline,
adamc@30 529 string "end"]
adamc@30 530 | StrVar x => string x
adamc@34 531 | StrProj (str, x) => box [p_str str,
adamc@34 532 string ".",
adamc@34 533 string x]
adamc@40 534 | StrFun (x, sgn, NONE, str) => box [string "functor",
adamc@40 535 space,
adamc@40 536 string "(",
adamc@40 537 string x,
adamc@40 538 space,
adamc@40 539 string ":",
adamc@40 540 p_sgn sgn,
adamc@40 541 string ")",
adamc@40 542 space,
adamc@40 543 string "=>",
adamc@40 544 space,
adamc@40 545 p_str str]
adamc@40 546 | StrFun (x, sgn, SOME sgn', str) => box [string "functor",
adamc@40 547 space,
adamc@40 548 string "(",
adamc@40 549 string x,
adamc@40 550 space,
adamc@40 551 string ":",
adamc@40 552 p_sgn sgn,
adamc@40 553 string ")",
adamc@40 554 space,
adamc@40 555 string ":",
adamc@40 556 space,
adamc@40 557 p_sgn sgn',
adamc@40 558 space,
adamc@40 559 string "=>",
adamc@40 560 space,
adamc@40 561 p_str str]
adamc@44 562 | StrApp (str1, str2) => box [p_str str1,
adamc@44 563 string "(",
adamc@44 564 p_str str2,
adamc@44 565 string ")"]
adamc@30 566
adamc@1 567 val p_file = p_list_sep newline p_decl
adamc@1 568
adamc@1 569 end