annotate src/source_print.sml @ 173:8221b95cc24c

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