annotate src/compiler.sml @ 1332:4dd5d23bace2

minHeap option in .urp files
author Adam Chlipala <adam@chlipala.net>
date Sat, 11 Dec 2010 15:16:04 -0500
parents a6427d1eda6f
children 79557535b843
rev   line source
adamc@1132 1 (* Copyright (c) 2008-2010, 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 structure Compiler :> COMPILER = struct
adamc@1 29
adamc@244 30 structure UrwebLrVals = UrwebLrValsFn(structure Token = LrParser.Token)
adamc@244 31 structure Lex = UrwebLexFn(structure Tokens = UrwebLrVals.Tokens)
adamc@244 32 structure UrwebP = Join(structure ParserData = UrwebLrVals.ParserData
adamc@1 33 structure Lex = Lex
adamc@1 34 structure LrParser = LrParser)
adamc@1 35
adamc@270 36 type job = {
adamc@385 37 prefix : string,
adamc@270 38 database : string option,
adamc@274 39 sources : string list,
adamc@274 40 exe : string,
adamc@274 41 sql : string option,
adamc@502 42 debug : bool,
adamc@673 43 profile : bool,
adamc@764 44 timeout : int,
adamc@764 45 ffi : string list,
adamc@764 46 link : string list,
adamc@765 47 headers : string list,
adamc@766 48 scripts : string list,
adamc@765 49 clientToServer : Settings.ffi list,
adamc@765 50 effectful : Settings.ffi list,
adamc@1171 51 benignEffectful : Settings.ffi list,
adamc@765 52 clientOnly : Settings.ffi list,
adamc@765 53 serverOnly : Settings.ffi list,
adamc@768 54 jsFuncs : (Settings.ffi * string) list,
adamc@769 55 rewrites : Settings.rewrite list,
adamc@769 56 filterUrl : Settings.rule list,
adamc@866 57 filterMime : Settings.rule list,
adamc@866 58 protocol : string option,
adamc@1164 59 dbms : string option,
adamc@1183 60 sigFile : string option,
adam@1294 61 safeGets : string list,
adam@1332 62 onError : (string * string list * string) option,
adam@1332 63 minHeap : int
adamc@270 64 }
adamc@201 65
adamc@201 66 type ('src, 'dst) phase = {
adamc@201 67 func : 'src -> 'dst,
adamc@201 68 print : 'dst -> Print.PD.pp_desc
adamc@201 69 }
adamc@201 70
adamc@201 71 type pmap = (string * Time.time) list
adamc@201 72
adamc@201 73 type ('src, 'dst) transform = {
adamc@201 74 func : 'src -> 'dst option,
adamc@201 75 print : 'dst -> Print.PD.pp_desc,
adamc@201 76 time : 'src * pmap -> 'dst option * pmap
adamc@201 77 }
adamc@201 78
adamc@1079 79 val debug = ref false
adamc@1240 80 val doIflow = ref false
adamc@1079 81
adamc@201 82 fun transform (ph : ('src, 'dst) phase) name = {
adamc@201 83 func = fn input => let
adamc@1079 84 val () = if !debug then
adamc@1079 85 print ("Starting " ^ name ^ "....\n")
adamc@1079 86 else
adamc@1079 87 ()
adamc@201 88 val v = #func ph input
adamc@201 89 in
adamc@1079 90 if !debug then
adamc@1079 91 print ("Finished " ^ name ^ ".\n")
adamc@1079 92 else
adamc@1079 93 ();
adamc@201 94 if ErrorMsg.anyErrors () then
adamc@201 95 NONE
adamc@201 96 else
adamc@201 97 SOME v
adamc@201 98 end,
adamc@201 99 print = #print ph,
adamc@201 100 time = fn (input, pmap) => let
adamc@1186 101 val () = if !debug then
adamc@1186 102 print ("Starting " ^ name ^ "....\n")
adamc@1186 103 else
adamc@1186 104 ()
adamc@201 105 val befor = Time.now ()
adamc@201 106 val v = #func ph input
adamc@201 107 val elapsed = Time.- (Time.now (), befor)
adamc@201 108 in
adamc@1186 109 if !debug then
adamc@1186 110 print ("Finished " ^ name ^ ".\n")
adamc@1186 111 else
adamc@1186 112 ();
adamc@201 113 (if ErrorMsg.anyErrors () then
adamc@201 114 NONE
adamc@201 115 else
adamc@201 116 SOME v,
adamc@201 117 (name, elapsed) :: pmap)
adamc@201 118 end
adamc@201 119 }
adamc@201 120
adamc@346 121 fun check (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@346 122 ignore (#func tr x))
adamc@346 123
adamc@280 124 fun run (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@280 125 #func tr x)
adamc@201 126
adamc@201 127 fun runPrint (tr : ('src, 'dst) transform) input =
adamc@280 128 (ErrorMsg.resetErrors ();
adamc@280 129 case #func tr input of
adamc@280 130 NONE => print "Failure\n"
adamc@280 131 | SOME v =>
adamc@280 132 (print "Success\n";
adamc@280 133 Print.print (#print tr v);
adamc@280 134 print "\n"))
adamc@201 135
adamc@201 136 fun time (tr : ('src, 'dst) transform) input =
adamc@55 137 let
adamc@201 138 val (_, pmap) = #time tr (input, [])
adamc@201 139 in
adamc@201 140 app (fn (name, time) =>
adamc@201 141 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 142 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 143 print "\n"
adamc@201 144 end
adamc@55 145
adamc@201 146 fun timePrint (tr : ('src, 'dst) transform) input =
adamc@201 147 let
adamc@201 148 val (ro, pmap) = #time tr (input, [])
adamc@55 149 in
adamc@201 150 app (fn (name, time) =>
adamc@201 151 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 152 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 153 print "\n";
adamc@201 154 case ro of
adamc@201 155 NONE => print "Failure\n"
adamc@201 156 | SOME v =>
adamc@201 157 (print "Success\n";
adamc@201 158 Print.print (#print tr v);
adamc@201 159 print "\n")
adamc@55 160 end
adamc@55 161
adamc@244 162 val parseUrs =
adamc@201 163 {func = fn filename => let
adamc@201 164 val fname = OS.FileSys.tmpName ()
adamc@201 165 val outf = TextIO.openOut fname
adamc@201 166 val () = TextIO.output (outf, "sig\n")
adamc@201 167 val inf = TextIO.openIn filename
adamc@201 168 fun loop () =
adamc@201 169 case TextIO.inputLine inf of
adamc@201 170 NONE => ()
adamc@201 171 | SOME line => (TextIO.output (outf, line);
adamc@201 172 loop ())
adamc@201 173 val () = loop ()
adamc@201 174 val () = TextIO.closeIn inf
adamc@201 175 val () = TextIO.closeOut outf
adamc@201 176
adamc@201 177 val () = (ErrorMsg.resetErrors ();
adamc@201 178 ErrorMsg.resetPositioning filename;
adamc@201 179 Lex.UserDeclarations.initialize ())
adamc@201 180 val file = TextIO.openIn fname
adamc@201 181 fun get _ = TextIO.input file
adamc@201 182 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 183 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 184 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 185 in
adamc@201 186 TextIO.closeIn file;
adamc@201 187 case absyn of
adamc@201 188 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => sgis
adamc@201 189 | _ => (ErrorMsg.errorAt {file = filename,
adamc@201 190 first = {line = 0,
adamc@201 191 char = 0},
adamc@201 192 last = {line = 0,
adamc@201 193 char = 0}} "Not a signature";
adamc@201 194 [])
adamc@201 195 end
adamc@201 196 handle LrParser.ParseError => [],
adamc@201 197 print = Print.p_list_sep Print.PD.newline SourcePrint.p_sgn_item}
adamc@55 198
adamc@1 199 (* The main parsing routine *)
adamc@244 200 val parseUr = {
adamc@201 201 func = fn filename =>
adamc@201 202 let
adamc@201 203 val () = (ErrorMsg.resetErrors ();
adamc@201 204 ErrorMsg.resetPositioning filename;
adamc@201 205 Lex.UserDeclarations.initialize ())
adamc@201 206 val file = TextIO.openIn filename
adamc@201 207 fun get _ = TextIO.input file
adamc@201 208 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 209 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 210 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 211 in
adamc@201 212 TextIO.closeIn file;
adamc@201 213 case absyn of
adamc@201 214 [(Source.DSgn ("?", _), _)] =>
adamc@201 215 (ErrorMsg.errorAt {file = filename,
adamc@201 216 first = {line = 0,
adamc@201 217 char = 0},
adamc@201 218 last = {line = 0,
adamc@201 219 char = 0}} "File starts with 'sig'";
adamc@201 220 [])
adamc@201 221 | _ => absyn
adamc@201 222 end
adamc@201 223 handle LrParser.ParseError => [],
adamc@201 224 print = SourcePrint.p_file}
adamc@56 225
adamc@768 226 fun p_job ({prefix, database, exe, sql, sources, debug, profile,
adamc@768 227 timeout, ffi, link, headers, scripts,
adamc@1171 228 clientToServer, effectful, benignEffectful, clientOnly, serverOnly, jsFuncs, ...} : job) =
adamc@270 229 let
adamc@270 230 open Print.PD
adamc@270 231 open Print
adamc@765 232
adamc@765 233 fun p_ffi name = p_list_sep (box []) (fn (m, s) =>
adamc@765 234 box [string name, space, string m, string ".", string s, newline])
adamc@270 235 in
adamc@274 236 box [if debug then
adamc@274 237 box [string "DEBUG", newline]
adamc@274 238 else
adamc@274 239 box [],
adamc@502 240 if profile then
adamc@502 241 box [string "PROFILE", newline]
adamc@502 242 else
adamc@502 243 box [],
adamc@274 244 case database of
adamc@270 245 NONE => string "No database."
adamc@270 246 | SOME db => string ("Database: " ^ db),
adamc@270 247 newline,
adamc@274 248 string "Exe: ",
adamc@274 249 string exe,
adamc@274 250 newline,
adamc@274 251 case sql of
adamc@274 252 NONE => string "No SQL file."
adamc@274 253 | SOME sql => string ("SQL fle: " ^ sql),
adamc@673 254 newline,
adamc@673 255 string "Timeout: ",
adamc@673 256 string (Int.toString timeout),
adamc@673 257 newline,
adamc@764 258 p_list_sep (box []) (fn s => box [string "Ffi", space, string s, newline]) ffi,
adamc@764 259 p_list_sep (box []) (fn s => box [string "Header", space, string s, newline]) headers,
adamc@766 260 p_list_sep (box []) (fn s => box [string "Script", space, string s, newline]) scripts,
adamc@764 261 p_list_sep (box []) (fn s => box [string "Link", space, string s, newline]) link,
adamc@765 262 p_ffi "ClientToServer" clientToServer,
adamc@765 263 p_ffi "Effectful" effectful,
adamc@1171 264 p_ffi "BenignEffectful" benignEffectful,
adamc@765 265 p_ffi "ClientOnly" clientOnly,
adamc@765 266 p_ffi "ServerOnly" serverOnly,
adamc@765 267 p_list_sep (box []) (fn ((m, s), s') =>
adamc@765 268 box [string "JsFunc", space, string m, string ".", string s,
adamc@765 269 space, string "=", space, string s', newline]) jsFuncs,
adamc@270 270 string "Sources:",
adamc@270 271 p_list string sources,
adamc@270 272 newline]
adamc@270 273 end
adamc@270 274
adamc@270 275 fun trim s =
adamc@270 276 let
adamc@270 277 val (_, s) = Substring.splitl Char.isSpace s
adamc@270 278 val (s, _) = Substring.splitr Char.isSpace s
adamc@270 279 in
adamc@270 280 s
adamc@270 281 end
adamc@270 282
adamc@794 283 structure M = BinaryMapFn(struct
adamc@794 284 type ord_key = string
adamc@794 285 val compare = String.compare
adamc@794 286 end)
adamc@794 287
adamc@1089 288 val pathmap = ref (M.insert (M.empty, "", Config.libUr))
adamc@1089 289
adamc@1089 290 fun addPath (k, v) = pathmap := M.insert (!pathmap, k, v)
adamc@1089 291
adam@1296 292 fun capitalize "" = ""
adam@1296 293 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adam@1296 294
adam@1296 295 fun institutionalizeJob (job : job) =
adam@1296 296 (Settings.setUrlPrefix (#prefix job);
adam@1296 297 Settings.setTimeout (#timeout job);
adam@1296 298 Settings.setHeaders (#headers job);
adam@1296 299 Settings.setScripts (#scripts job);
adam@1296 300 Settings.setClientToServer (#clientToServer job);
adam@1296 301 Settings.setEffectful (#effectful job);
adam@1296 302 Settings.setBenignEffectful (#benignEffectful job);
adam@1296 303 Settings.setClientOnly (#clientOnly job);
adam@1296 304 Settings.setServerOnly (#serverOnly job);
adam@1296 305 Settings.setJsFuncs (#jsFuncs job);
adam@1296 306 Settings.setRewriteRules (#rewrites job);
adam@1296 307 Settings.setUrlRules (#filterUrl job);
adam@1296 308 Settings.setMimeRules (#filterMime job);
adam@1296 309 Option.app Settings.setProtocol (#protocol job);
adam@1296 310 Option.app Settings.setDbms (#dbms job);
adam@1296 311 Settings.setSafeGets (#safeGets job);
adam@1332 312 Settings.setOnError (#onError job);
adam@1332 313 Settings.setMinHeap (#minHeap job))
adam@1296 314
adam@1331 315 fun inputCommentableLine inf =
adam@1331 316 Option.map (fn s =>
adam@1331 317 let
adam@1331 318 val s = #1 (Substring.splitl (fn ch => ch <> #"#") (Substring.full s))
adam@1332 319 val s = #1 (Substring.splitr (not o Char.isSpace) s)
adam@1331 320 in
adam@1332 321 Substring.string (if Substring.size s > 0 andalso Char.isSpace (Substring.sub (s, Substring.size s - 1)) then
adam@1332 322 Substring.trimr 1 s
adam@1332 323 else
adam@1332 324 s)
adam@1331 325 end) (TextIO.inputLine inf)
adam@1331 326
adamc@1082 327 fun parseUrp' accLibs fname =
adam@1296 328 if not (Posix.FileSys.access (fname ^ ".urp", []) orelse Posix.FileSys.access (fname ^ "/lib.urp", []))
adam@1296 329 andalso Posix.FileSys.access (fname ^ ".ur", []) then
adam@1296 330 let
adam@1296 331 val job = {prefix = "/",
adam@1296 332 database = NONE,
adam@1296 333 sources = [fname],
adam@1296 334 exe = fname ^ ".exe",
adam@1296 335 sql = NONE,
adam@1296 336 debug = false,
adam@1296 337 profile = false,
adam@1296 338 timeout = 60,
adam@1296 339 ffi = [],
adam@1296 340 link = [],
adam@1296 341 headers = [],
adam@1296 342 scripts = [],
adam@1296 343 clientToServer = [],
adam@1296 344 effectful = [],
adam@1296 345 benignEffectful = [],
adam@1296 346 clientOnly = [],
adam@1296 347 serverOnly = [],
adam@1296 348 jsFuncs = [],
adam@1296 349 rewrites = [{pkind = Settings.Any,
adam@1296 350 kind = Settings.Prefix,
adam@1296 351 from = capitalize (OS.Path.file fname) ^ "/", to = ""}],
adam@1296 352 filterUrl = [],
adam@1296 353 filterMime = [],
adam@1296 354 protocol = NONE,
adam@1296 355 dbms = NONE,
adam@1296 356 sigFile = NONE,
adam@1296 357 safeGets = [],
adam@1332 358 onError = NONE,
adam@1332 359 minHeap = 0}
adam@1296 360 in
adam@1296 361 institutionalizeJob job;
adam@1296 362 {Job = job, Libs = []}
adam@1296 363 end
adam@1296 364 else
adam@1296 365 let
adam@1296 366 val pathmap = ref (!pathmap)
adam@1296 367 val bigLibs = ref []
adamc@767 368
adam@1296 369 fun pu filename =
adam@1296 370 let
adam@1296 371 val dir = OS.Path.dir filename
adam@1296 372 fun opener () = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
adamc@1151 373
adam@1296 374 val inf = opener ()
adamc@1151 375
adam@1296 376 fun hasSpaceLine () =
adam@1331 377 case inputCommentableLine inf of
adam@1296 378 NONE => false
adam@1331 379 | SOME s => s = "debug" orelse s = "profile"
adam@1296 380 orelse CharVector.exists (fn ch => ch = #" " orelse ch = #"\t") s orelse hasSpaceLine ()
adamc@1151 381
adam@1296 382 val hasBlankLine = hasSpaceLine ()
adamc@1151 383
adam@1296 384 val inf = (TextIO.closeIn inf; opener ())
adamc@767 385
adam@1296 386 fun pathify fname =
adam@1296 387 if size fname > 0 andalso String.sub (fname, 0) = #"$" then
adam@1296 388 let
adam@1296 389 val fname' = Substring.extract (fname, 1, NONE)
adam@1296 390 val (befor, after) = Substring.splitl (fn ch => ch <> #"/") fname'
adam@1296 391 in
adam@1296 392 if Substring.isEmpty after then
adam@1296 393 fname
adam@1296 394 else
adam@1296 395 case M.find (!pathmap, Substring.string befor) of
adam@1296 396 NONE => fname
adam@1296 397 | SOME rep => rep ^ Substring.string after
adam@1296 398 end
adam@1296 399 else
adam@1296 400 fname
adam@1296 401
adam@1296 402 fun relify fname =
adamc@794 403 let
adam@1296 404 val fname = pathify fname
adamc@794 405 in
adam@1296 406 OS.Path.concat (dir, fname)
adam@1296 407 handle OS.Path.Path => fname
adamc@794 408 end
adamc@767 409
adam@1296 410 fun libify path =
adam@1296 411 (if Posix.FileSys.access (path ^ ".urp", []) then
adam@1296 412 path
adam@1296 413 else
adam@1296 414 path ^ "/lib")
adam@1296 415 handle SysErr => path
adamc@767 416
adam@1296 417 fun libify' path =
adam@1296 418 (if Posix.FileSys.access (relify path ^ ".urp", []) then
adam@1296 419 path
adam@1296 420 else
adam@1296 421 path ^ "/lib")
adam@1296 422 handle SysErr => path
adamc@767 423
adam@1296 424 val absDir = OS.Path.mkAbsolute {path = dir, relativeTo = OS.FileSys.getDir ()}
adamc@794 425
adam@1296 426 fun relifyA fname =
adam@1296 427 OS.Path.mkAbsolute {path = pathify fname, relativeTo = absDir}
adamc@794 428
adam@1296 429 fun readSources acc =
adam@1331 430 case inputCommentableLine inf of
adam@1296 431 NONE => rev acc
adam@1296 432 | SOME line =>
adam@1296 433 let
adam@1296 434 val acc = if CharVector.all Char.isSpace line then
adam@1296 435 acc
adam@1296 436 else
adam@1296 437 let
adam@1296 438 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
adam@1296 439 (String.explode line))
adam@1296 440 val fname = relifyA fname
adam@1296 441 in
adam@1296 442 fname :: acc
adam@1296 443 end
adam@1296 444 in
adam@1296 445 readSources acc
adam@1296 446 end
adamc@794 447
adam@1296 448 val prefix = ref NONE
adam@1296 449 val database = ref (Settings.getDbstring ())
adam@1296 450 val exe = ref (Settings.getExe ())
adam@1296 451 val sql = ref (Settings.getSql ())
adam@1296 452 val debug = ref (Settings.getDebug ())
adam@1296 453 val profile = ref false
adam@1296 454 val timeout = ref NONE
adam@1296 455 val ffi = ref []
adam@1296 456 val link = ref []
adam@1296 457 val headers = ref []
adam@1296 458 val scripts = ref []
adam@1296 459 val clientToServer = ref []
adam@1296 460 val effectful = ref []
adam@1296 461 val benignEffectful = ref []
adam@1296 462 val clientOnly = ref []
adam@1296 463 val serverOnly = ref []
adam@1296 464 val jsFuncs = ref []
adam@1296 465 val rewrites = ref []
adam@1296 466 val url = ref []
adam@1296 467 val mime = ref []
adam@1296 468 val libs = ref []
adam@1296 469 val protocol = ref NONE
adam@1296 470 val dbms = ref NONE
adam@1296 471 val sigFile = ref (Settings.getSigFile ())
adam@1296 472 val safeGets = ref []
adam@1296 473 val onError = ref NONE
adam@1332 474 val minHeap = ref 0
adamc@794 475
adam@1296 476 fun finish sources =
adam@1296 477 let
adam@1296 478 val job = {
adam@1296 479 prefix = Option.getOpt (!prefix, "/"),
adam@1296 480 database = !database,
adam@1296 481 exe = Option.getOpt (!exe, OS.Path.joinBaseExt {base = OS.Path.base filename,
adam@1296 482 ext = SOME "exe"}),
adam@1296 483 sql = !sql,
adam@1296 484 debug = !debug,
adam@1296 485 profile = !profile,
adam@1296 486 timeout = Option.getOpt (!timeout, 60),
adam@1296 487 ffi = rev (!ffi),
adam@1296 488 link = rev (!link),
adam@1296 489 headers = rev (!headers),
adam@1296 490 scripts = rev (!scripts),
adam@1296 491 clientToServer = rev (!clientToServer),
adam@1296 492 effectful = rev (!effectful),
adam@1296 493 benignEffectful = rev (!benignEffectful),
adam@1296 494 clientOnly = rev (!clientOnly),
adam@1296 495 serverOnly = rev (!serverOnly),
adam@1296 496 jsFuncs = rev (!jsFuncs),
adam@1296 497 rewrites = rev (!rewrites),
adam@1296 498 filterUrl = rev (!url),
adam@1296 499 filterMime = rev (!mime),
adam@1296 500 sources = sources,
adam@1296 501 protocol = !protocol,
adam@1296 502 dbms = !dbms,
adam@1296 503 sigFile = !sigFile,
adam@1296 504 safeGets = rev (!safeGets),
adam@1332 505 onError = !onError,
adam@1332 506 minHeap = !minHeap
adam@1296 507 }
adamc@794 508
adam@1296 509 fun mergeO f (old, new) =
adam@1296 510 case (old, new) of
adam@1296 511 (NONE, _) => new
adam@1296 512 | (_, NONE) => old
adam@1296 513 | (SOME v1, SOME v2) => SOME (f (v1, v2))
adamc@794 514
adam@1296 515 fun same desc = mergeO (fn (x : string, y) =>
adam@1296 516 (if x = y then
adam@1296 517 ()
adam@1296 518 else
adam@1296 519 ErrorMsg.error ("Multiple "
adam@1296 520 ^ desc ^ " values that don't agree");
adam@1296 521 x))
adamc@794 522
adam@1296 523 fun merge (old : job, new : job) = {
adam@1296 524 prefix = #prefix old,
adam@1296 525 database = mergeO (fn (old, _) => old) (#database old, #database new),
adam@1296 526 exe = #exe old,
adam@1296 527 sql = #sql old,
adam@1296 528 debug = #debug old orelse #debug new,
adam@1296 529 profile = #profile old orelse #profile new,
adam@1296 530 timeout = #timeout old,
adam@1296 531 ffi = #ffi old @ #ffi new,
adam@1296 532 link = #link old @ #link new,
adam@1296 533 headers = #headers old @ #headers new,
adam@1296 534 scripts = #scripts old @ #scripts new,
adam@1296 535 clientToServer = #clientToServer old @ #clientToServer new,
adam@1296 536 effectful = #effectful old @ #effectful new,
adam@1296 537 benignEffectful = #benignEffectful old @ #benignEffectful new,
adam@1296 538 clientOnly = #clientOnly old @ #clientOnly new,
adam@1296 539 serverOnly = #serverOnly old @ #serverOnly new,
adam@1296 540 jsFuncs = #jsFuncs old @ #jsFuncs new,
adam@1296 541 rewrites = #rewrites old @ #rewrites new,
adam@1296 542 filterUrl = #filterUrl old @ #filterUrl new,
adam@1296 543 filterMime = #filterMime old @ #filterMime new,
adam@1296 544 sources = #sources new
adam@1296 545 @ List.filter (fn s => List.all (fn s' => s' <> s) (#sources new))
adam@1296 546 (#sources old),
adam@1296 547 protocol = mergeO #2 (#protocol old, #protocol new),
adam@1296 548 dbms = mergeO #2 (#dbms old, #dbms new),
adam@1296 549 sigFile = mergeO #2 (#sigFile old, #sigFile new),
adam@1296 550 safeGets = #safeGets old @ #safeGets new,
adam@1332 551 onError = mergeO #2 (#onError old, #onError new),
adam@1332 552 minHeap = Int.max (#minHeap old, #minHeap new)
adam@1296 553 }
adamc@794 554 in
adam@1296 555 if accLibs then
adam@1296 556 foldr (fn (job', job) => merge (job, job')) job (!libs)
adam@1296 557 else
adam@1296 558 job
adamc@794 559 end
adamc@794 560
adam@1296 561 fun parsePkind s =
adam@1296 562 case s of
adam@1296 563 "all" => Settings.Any
adam@1296 564 | "url" => Settings.Url
adam@1296 565 | "table" => Settings.Table
adam@1296 566 | "sequence" => Settings.Sequence
adam@1296 567 | "view" => Settings.View
adam@1296 568 | "relation" => Settings.Relation
adam@1296 569 | "cookie" => Settings.Cookie
adam@1296 570 | "style" => Settings.Style
adam@1296 571 | _ => (ErrorMsg.error "Bad path kind spec";
adam@1296 572 Settings.Any)
adam@1296 573
adam@1296 574 fun parseFrom s =
adam@1296 575 if size s > 1 andalso String.sub (s, size s - 2) = #"/" andalso String.sub (s, size s - 1) = #"*" then
adam@1296 576 (Settings.Prefix, String.substring (s, 0, size s - 1))
adam@1296 577 else
adam@1296 578 (Settings.Exact, s)
adam@1296 579
adam@1296 580 fun parseFkind s =
adam@1296 581 case s of
adam@1296 582 "url" => url
adam@1296 583 | "mime" => mime
adam@1296 584 | _ => (ErrorMsg.error "Bad filter kind";
adam@1296 585 url)
adam@1296 586
adam@1296 587 fun parsePattern s =
adam@1296 588 if size s > 0 andalso String.sub (s, size s - 1) = #"*" then
adam@1296 589 (Settings.Prefix, String.substring (s, 0, size s - 1))
adam@1296 590 else
adam@1296 591 (Settings.Exact, s)
adam@1296 592
adam@1296 593 fun read () =
adam@1331 594 case inputCommentableLine inf of
adam@1296 595 NONE => finish []
adam@1331 596 | SOME "" => finish (readSources [])
adam@1296 597 | SOME line =>
adam@1296 598 let
adam@1296 599 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
adam@1296 600 val cmd = Substring.string (trim cmd)
adam@1296 601 val arg = Substring.string (trim arg)
adam@1296 602
adam@1296 603 fun ffiS () =
adam@1296 604 case String.fields (fn ch => ch = #".") arg of
adam@1296 605 [m, x] => (m, x)
adam@1296 606 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func");
adam@1296 607 ("", ""))
adam@1296 608
adam@1296 609 fun ffiM () =
adam@1296 610 case String.fields (fn ch => ch = #"=") arg of
adam@1296 611 [f, s] =>
adam@1296 612 (case String.fields (fn ch => ch = #".") f of
adam@1296 613 [m, x] => ((m, x), s)
adam@1296 614 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adam@1296 615 (("", ""), "")))
adam@1296 616 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adam@1296 617 (("", ""), ""))
adam@1296 618 in
adam@1296 619 case cmd of
adam@1296 620 "prefix" =>
adam@1296 621 (case !prefix of
adam@1296 622 NONE => ()
adam@1296 623 | SOME _ => ErrorMsg.error "Duplicate 'prefix' directive";
adam@1296 624 prefix := SOME arg)
adam@1296 625 | "database" =>
adam@1296 626 (case !database of
adam@1296 627 NONE => database := SOME arg
adam@1296 628 | SOME _ => ())
adam@1296 629 | "dbms" =>
adam@1296 630 (case !dbms of
adam@1296 631 NONE => dbms := SOME arg
adam@1296 632 | SOME _ => ())
adam@1296 633 | "sigfile" =>
adam@1296 634 (case !sigFile of
adam@1296 635 NONE => sigFile := SOME arg
adam@1296 636 | SOME _ => ())
adam@1296 637 | "exe" =>
adam@1296 638 (case !exe of
adam@1296 639 NONE => exe := SOME (relify arg)
adam@1296 640 | SOME _ => ())
adam@1296 641 | "sql" =>
adam@1296 642 (case !sql of
adam@1296 643 NONE => sql := SOME (relify arg)
adam@1296 644 | SOME _ => ())
adam@1296 645 | "debug" => debug := true
adam@1296 646 | "profile" => profile := true
adam@1296 647 | "timeout" =>
adam@1296 648 (case !timeout of
adam@1296 649 NONE => ()
adam@1296 650 | SOME _ => ErrorMsg.error "Duplicate 'timeout' directive";
adam@1296 651 timeout := SOME (valOf (Int.fromString arg)))
adam@1296 652 | "ffi" => ffi := relify arg :: !ffi
adam@1296 653 | "link" => let
adam@1296 654 val arg = if size arg >= 1
adam@1296 655 andalso String.sub (arg, 0) = #"-" then
adam@1296 656 arg
adam@1296 657 else
adam@1296 658 relifyA arg
adam@1296 659 in
adam@1296 660 link := arg :: !link
adam@1296 661 end
adam@1296 662 | "include" => headers := relifyA arg :: !headers
adam@1296 663 | "script" => scripts := arg :: !scripts
adam@1296 664 | "clientToServer" => clientToServer := ffiS () :: !clientToServer
adam@1296 665 | "safeGet" => safeGets := arg :: !safeGets
adam@1296 666 | "effectful" => effectful := ffiS () :: !effectful
adam@1296 667 | "benignEffectful" => benignEffectful := ffiS () :: !benignEffectful
adam@1296 668 | "clientOnly" => clientOnly := ffiS () :: !clientOnly
adam@1296 669 | "serverOnly" => serverOnly := ffiS () :: !serverOnly
adam@1296 670 | "jsFunc" => jsFuncs := ffiM () :: !jsFuncs
adam@1296 671 | "rewrite" =>
adam@1296 672 let
adam@1296 673 fun doit (pkind, from, to) =
adam@1296 674 let
adam@1296 675 val pkind = parsePkind pkind
adam@1296 676 val (kind, from) = parseFrom from
adam@1296 677 in
adam@1296 678 rewrites := {pkind = pkind, kind = kind, from = from, to = to} :: !rewrites
adam@1296 679 end
adam@1296 680 in
adam@1296 681 case String.tokens Char.isSpace arg of
adam@1296 682 [pkind, from, to] => doit (pkind, from, to)
adam@1296 683 | [pkind, from] => doit (pkind, from, "")
adam@1296 684 | _ => ErrorMsg.error "Bad 'rewrite' syntax"
adam@1296 685 end
adam@1296 686 | "allow" =>
adam@1296 687 (case String.tokens Char.isSpace arg of
adam@1296 688 [fkind, pattern] =>
adam@1296 689 let
adam@1296 690 val fkind = parseFkind fkind
adam@1296 691 val (kind, pattern) = parsePattern pattern
adam@1296 692 in
adam@1296 693 fkind := {action = Settings.Allow, kind = kind, pattern = pattern} :: !fkind
adam@1296 694 end
adam@1296 695 | _ => ErrorMsg.error "Bad 'allow' syntax")
adam@1296 696 | "deny" =>
adam@1296 697 (case String.tokens Char.isSpace arg of
adam@1296 698 [fkind, pattern] =>
adam@1296 699 let
adam@1296 700 val fkind = parseFkind fkind
adam@1296 701 val (kind, pattern) = parsePattern pattern
adam@1296 702 in
adam@1296 703 fkind := {action = Settings.Deny, kind = kind, pattern = pattern} :: !fkind
adam@1296 704 end
adam@1296 705 | _ => ErrorMsg.error "Bad 'deny' syntax")
adam@1296 706 | "library" => if accLibs then
adam@1296 707 libs := pu (libify (relify arg)) :: !libs
adam@1296 708 else
adam@1296 709 bigLibs := libify' arg :: !bigLibs
adam@1296 710 | "path" =>
adam@1296 711 (case String.fields (fn ch => ch = #"=") arg of
adam@1296 712 [n, v] => pathmap := M.insert (!pathmap, n, v)
adam@1296 713 | _ => ErrorMsg.error "path argument not of the form name=value'")
adam@1296 714 | "onError" =>
adam@1296 715 (case String.fields (fn ch => ch = #".") arg of
adam@1296 716 m1 :: (fs as _ :: _) =>
adam@1296 717 onError := SOME (m1, List.take (fs, length fs - 1), List.last fs)
adam@1296 718 | _ => ErrorMsg.error "invalid 'onError' argument")
adam@1307 719 | "limit" =>
adam@1307 720 (case String.fields Char.isSpace arg of
adam@1307 721 [class, num] =>
adam@1307 722 (case Int.fromString num of
adam@1307 723 NONE => ErrorMsg.error ("invalid limit number '" ^ num ^ "'")
adam@1307 724 | SOME n =>
adam@1307 725 if n < 0 then
adam@1307 726 ErrorMsg.error ("invalid limit number '" ^ num ^ "'")
adam@1307 727 else
adam@1307 728 Settings.addLimit (class, n))
adam@1307 729 | _ => ErrorMsg.error "invalid 'limit' arguments")
adam@1332 730 | "minHeap" =>
adam@1332 731 (case Int.fromString arg of
adam@1332 732 NONE => ErrorMsg.error ("invalid min heap '" ^ arg ^ "'")
adam@1332 733 | SOME n => minHeap := n)
adam@1296 734
adam@1296 735 | _ => ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
adam@1296 736 read ()
adam@1296 737 end
adam@1296 738
adam@1296 739 val job = if hasBlankLine then
adam@1296 740 read ()
adam@1296 741 else
adam@1296 742 finish (readSources [])
adam@1296 743 in
adam@1296 744 TextIO.closeIn inf;
adam@1296 745 institutionalizeJob job;
adam@1296 746 job
adam@1296 747 end
adam@1296 748 in
adam@1296 749 {Job = pu fname, Libs = !bigLibs}
adam@1296 750 end
adamc@767 751
adamc@1082 752 fun p_job' {Job = j, Libs = _ : string list} = p_job j
adamc@1082 753
adamc@270 754 val parseUrp = {
adamc@1083 755 func = #Job o parseUrp' true,
adamc@270 756 print = p_job
adamc@270 757 }
adamc@270 758
adamc@1082 759 val parseUrp' = {
adamc@1083 760 func = parseUrp' false,
adamc@1082 761 print = p_job'
adamc@1082 762 }
adamc@1082 763
adamc@270 764 val toParseJob = transform parseUrp "parseJob"
adamc@1082 765 val toParseJob' = transform parseUrp' "parseJob'"
adamc@1082 766
adamc@1082 767 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
adamc@1082 768 func = fn input => case #func tr1 input of
adamc@1082 769 NONE => NONE
adamc@1082 770 | SOME v => #func tr2 v,
adamc@1082 771 print = #print tr2,
adamc@1082 772 time = fn (input, pmap) => let
adamc@1082 773 val (ro, pmap) = #time tr1 (input, pmap)
adamc@1082 774 in
adamc@1082 775 case ro of
adamc@1082 776 NONE => (NONE, pmap)
adamc@1082 777 | SOME v => #time tr2 (v, pmap)
adamc@1082 778 end
adamc@1082 779 }
adamc@270 780
adamc@1090 781 structure SM = BinaryMapFn(struct
adamc@1090 782 type ord_key = string
adamc@1090 783 val compare = String.compare
adamc@1090 784 end)
adamc@1090 785
adamc@1090 786 val moduleRoots = ref ([] : (string * string) list)
adamc@1090 787 fun addModuleRoot (k, v) = moduleRoots := (k, v) :: !moduleRoots
adamc@1090 788
adamc@1090 789 structure SS = BinarySetFn(struct
adamc@1090 790 type ord_key = string
adamc@1090 791 val compare = String.compare
adamc@1090 792 end)
adamc@1090 793
adamc@201 794 val parse = {
adam@1294 795 func = fn {database, sources = fnames, ffi, onError, ...} : job =>
adamc@201 796 let
adamc@1090 797 val mrs = !moduleRoots
adamc@1090 798
adamc@834 799 val anyErrors = ref false
adamc@834 800 fun checkErrors () = anyErrors := (!anyErrors orelse ErrorMsg.anyErrors ())
adamc@201 801 fun nameOf fname = capitalize (OS.Path.file fname)
adamc@109 802
adamc@764 803 fun parseFfi fname =
adamc@764 804 let
adamc@764 805 val mname = nameOf fname
adamc@764 806 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@764 807
adamc@764 808 val loc = {file = urs,
adamc@764 809 first = ErrorMsg.dummyPos,
adamc@764 810 last = ErrorMsg.dummyPos}
adamc@764 811
adamc@764 812 val sgn = (Source.SgnConst (#func parseUrs urs), loc)
adamc@764 813 in
adamc@834 814 checkErrors ();
adamc@764 815 (Source.DFfiStr (mname, sgn), loc)
adamc@764 816 end
adamc@764 817
adamc@1090 818 val defed = ref SS.empty
adamc@1092 819 val fulls = ref SS.empty
adamc@1090 820
adamc@201 821 fun parseOne fname =
adamc@201 822 let
adamc@201 823 val mname = nameOf fname
adamc@244 824 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
adamc@244 825 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@56 826
adamc@201 827 val sgnO =
adamc@244 828 if Posix.FileSys.access (urs, []) then
adamc@244 829 SOME (Source.SgnConst (#func parseUrs urs),
adamc@244 830 {file = urs,
adamc@201 831 first = ErrorMsg.dummyPos,
adamc@201 832 last = ErrorMsg.dummyPos})
adamc@834 833 before checkErrors ()
adamc@201 834 else
adamc@201 835 NONE
adamc@56 836
adamc@244 837 val loc = {file = ur,
adamc@201 838 first = ErrorMsg.dummyPos,
adamc@201 839 last = ErrorMsg.dummyPos}
adamc@56 840
adamc@244 841 val ds = #func parseUr ur
adamc@1090 842 val d = (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
adamc@1090 843
adamc@1126 844 val fname = OS.Path.mkCanonical fname
adamc@1090 845 val d = case List.find (fn (root, name) =>
adamc@1090 846 String.isPrefix (root ^ "/") fname) mrs of
adamc@1090 847 NONE => d
adamc@1090 848 | SOME (root, name) =>
adamc@1090 849 let
adamc@1090 850 val fname = String.extract (fname, size root + 1, NONE)
adamc@1090 851 val pieces = name :: String.tokens (fn ch => ch = #"/") fname
adamc@1090 852 val pieces = List.filter (fn s => size s > 0
adamc@1090 853 andalso Char.isAlpha (String.sub (s, 0)))
adamc@1090 854 pieces
adamc@1090 855 val pieces = map capitalize pieces
adamc@1092 856 val full = String.concatWith "." pieces
adamc@1090 857
adamc@1090 858 fun makeD prefix pieces =
adamc@1090 859 case pieces of
adamc@1090 860 [] => (ErrorMsg.error "Empty module path";
adamc@1090 861 (Source.DStyle "Boo", loc))
adamc@1090 862 | [_] => d
adamc@1090 863 | piece :: pieces =>
adamc@1090 864 let
adamc@1146 865 val this = case prefix of
adamc@1146 866 "" => piece
adamc@1146 867 | _ => prefix ^ "." ^ piece
adamc@1090 868 val old = SS.member (!defed, this)
adamc@1146 869
adamc@1146 870 fun notThere (ch, s) =
adamc@1146 871 Substring.isEmpty (#2 (Substring.splitl
adamc@1146 872 (fn ch' => ch' <> ch) s))
adamc@1146 873
adamc@1146 874 fun simOpen () =
adamc@1146 875 SS.foldl (fn (full, ds) =>
adamc@1146 876 if String.isPrefix (this ^ ".") full
adamc@1146 877 andalso notThere (#".",
adamc@1146 878 Substring.extract (full,
adamc@1146 879 size
adamc@1146 880 this + 1,
adamc@1146 881 NONE)) then
adamc@1146 882 let
adamc@1146 883 val parts = String.tokens
adamc@1146 884 (fn ch => ch = #".") full
adamc@1146 885
adamc@1146 886 val part = List.last parts
adamc@1146 887
adamc@1146 888 val imp = if length parts >= 2 then
adamc@1146 889 (Source.StrProj
adamc@1146 890 ((Source.StrVar
adamc@1146 891 (List.nth (parts,
adamc@1146 892 length
adamc@1146 893 parts
adamc@1146 894 - 2)),
adamc@1146 895 loc),
adamc@1146 896 part), loc)
adamc@1146 897 else
adamc@1146 898 (Source.StrVar part, loc)
adamc@1146 899 in
adamc@1146 900 (Source.DStr (part, NONE, imp),
adamc@1146 901 loc) :: ds
adamc@1146 902 end
adamc@1146 903 else
adamc@1146 904 ds) [] (!fulls)
adamc@1090 905 in
adamc@1090 906 defed := SS.add (!defed, this);
adamc@1090 907 (Source.DStr (piece, NONE,
adamc@1090 908 (Source.StrConst (if old then
adamc@1146 909 simOpen ()
adamc@1146 910 @ [makeD this pieces]
adamc@1090 911 else
adamc@1146 912 [makeD this pieces]), loc)),
adamc@1090 913 loc)
adamc@1090 914 end
adamc@1090 915 in
adamc@1092 916 if SS.member (!fulls, full) then
adamc@1092 917 ErrorMsg.error ("Rooted module " ^ full ^ " has multiple versions.")
adamc@1092 918 else
adamc@1092 919 ();
adamc@1146 920
adamc@1090 921 makeD "" pieces
adamc@1146 922 before ignore (foldl (fn (new, path) =>
adamc@1146 923 let
adamc@1146 924 val new' = case path of
adamc@1146 925 "" => new
adamc@1146 926 | _ => path ^ "." ^ new
adamc@1146 927 in
adamc@1146 928 fulls := SS.add (!fulls, new');
adamc@1146 929 new'
adamc@1146 930 end) "" pieces)
adamc@1090 931 end
adamc@201 932 in
adamc@834 933 checkErrors ();
adamc@1090 934 d
adamc@201 935 end
adamc@56 936
adamc@764 937 val dsFfi = map parseFfi ffi
adamc@201 938 val ds = map parseOne fnames
adamc@1090 939 val loc = ErrorMsg.dummySpan
adamc@201 940 in
adamc@834 941 if !anyErrors then
adamc@834 942 ErrorMsg.error "Parse failure"
adamc@834 943 else
adamc@834 944 ();
adamc@834 945
adamc@201 946 let
adamc@1126 947 val final = List.last fnames
adamc@1126 948 val final = case List.find (fn (root, name) =>
adamc@1126 949 String.isPrefix (root ^ "/") final) mrs of
adamc@1126 950 NONE => (Source.StrVar (nameOf final), loc)
adamc@1126 951 | SOME (root, name) =>
adamc@1126 952 let
adamc@1126 953 val m = (Source.StrVar name, loc)
adamc@1126 954 val final = String.extract (final, size root + 1, NONE)
adamc@1264 955 val fields = String.fields (fn ch => ch = #"/") final
adamc@1264 956 val fields = List.filter (fn s => size s = 0
adamc@1264 957 orelse not (Char.isDigit (String.sub (s, 0))))
adamc@1264 958 fields
adamc@1126 959 in
adamc@1126 960 foldl (fn (x, m) => (Source.StrProj (m, capitalize x), loc))
adamc@1264 961 m fields
adamc@1126 962 end
adamc@271 963
adamc@764 964 val ds = dsFfi @ ds
adamc@1126 965 @ [(Source.DExport final, loc)]
adamc@1090 966
adamc@1090 967 val ds = case database of
adamc@1090 968 NONE => ds
adamc@1090 969 | SOME s => (Source.DDatabase s, loc) :: ds
adam@1294 970
adam@1294 971 val ds = case onError of
adam@1294 972 NONE => ds
adam@1294 973 | SOME v => ds @ [(Source.DOnError v, loc)]
adamc@201 974 in
adamc@1090 975 ds
adamc@201 976 end handle Empty => ds
adamc@201 977 end,
adamc@201 978 print = SourcePrint.p_file
adamc@201 979 }
adamc@56 980
adamc@270 981 val toParse = transform parse "parse" o toParseJob
adamc@38 982
adamc@378 983 fun libFile s = OS.Path.joinDirFile {dir = Config.libUr,
adamc@378 984 file = s}
adamc@378 985 fun clibFile s = OS.Path.joinDirFile {dir = Config.libC,
adamc@378 986 file = s}
adamc@378 987
adamc@201 988 val elaborate = {
adamc@201 989 func = fn file => let
adamc@378 990 val basis = #func parseUrs (libFile "basis.urs")
adamc@378 991 val topSgn = #func parseUrs (libFile "top.urs")
adamc@378 992 val topStr = #func parseUr (libFile "top.ur")
adamc@201 993 in
adamc@325 994 Elaborate.elabFile basis topStr topSgn ElabEnv.empty file
adamc@201 995 end,
adamc@201 996 print = ElabPrint.p_file ElabEnv.empty
adamc@201 997 }
adamc@5 998
adamc@270 999 val toElaborate = transform elaborate "elaborate" o toParse
adamc@201 1000
adamc@448 1001 val unnest = {
adamc@448 1002 func = Unnest.unnest,
adamc@448 1003 print = ElabPrint.p_file ElabEnv.empty
adamc@448 1004 }
adamc@448 1005
adamc@448 1006 val toUnnest = transform unnest "unnest" o toElaborate
adamc@448 1007
adamc@313 1008 val termination = {
adamc@313 1009 func = (fn file => (Termination.check file; file)),
adamc@313 1010 print = ElabPrint.p_file ElabEnv.empty
adamc@313 1011 }
adamc@313 1012
adamc@448 1013 val toTermination = transform termination "termination" o toUnnest
adamc@313 1014
adamc@201 1015 val explify = {
adamc@201 1016 func = Explify.explify,
adamc@201 1017 print = ExplPrint.p_file ExplEnv.empty
adamc@201 1018 }
adamc@201 1019
adamc@625 1020 val toExplify = transform explify "explify" o toUnnest
adamc@201 1021
adamc@201 1022 val corify = {
adamc@201 1023 func = Corify.corify,
adamc@201 1024 print = CorePrint.p_file CoreEnv.empty
adamc@201 1025 }
adamc@201 1026
adamc@270 1027 val toCorify = transform corify "corify" o toExplify
adamc@201 1028
adamc@482 1029 (*val reduce_local = {
adamc@482 1030 func = ReduceLocal.reduce,
adamc@482 1031 print = CorePrint.p_file CoreEnv.empty
adamc@482 1032 }
adamc@482 1033
adamc@482 1034 val toReduce_local = transform reduce_local "reduce_local" o toCorify*)
adamc@482 1035
adamc@443 1036 val especialize = {
adamc@443 1037 func = ESpecialize.specialize,
adamc@443 1038 print = CorePrint.p_file CoreEnv.empty
adamc@443 1039 }
adamc@443 1040
adamc@454 1041 val core_untangle = {
adamc@454 1042 func = CoreUntangle.untangle,
adamc@454 1043 print = CorePrint.p_file CoreEnv.empty
adamc@454 1044 }
adamc@454 1045
adamc@794 1046 val toCore_untangle = transform core_untangle "core_untangle" o toCorify
adamc@454 1047
adamc@202 1048 val shake = {
adamc@202 1049 func = Shake.shake,
adamc@202 1050 print = CorePrint.p_file CoreEnv.empty
adamc@202 1051 }
adamc@39 1052
adamc@454 1053 val toShake1 = transform shake "shake1" o toCore_untangle
adamc@110 1054
adamc@607 1055 val rpcify = {
adamc@607 1056 func = Rpcify.frob,
adamc@607 1057 print = CorePrint.p_file CoreEnv.empty
adamc@607 1058 }
adamc@607 1059
adamc@607 1060 val toRpcify = transform rpcify "rpcify" o toShake1
adamc@607 1061
adamc@642 1062 val toCore_untangle2 = transform core_untangle "core_untangle2" o toRpcify
adamc@642 1063 val toShake2 = transform shake "shake2" o toCore_untangle2
adamc@1181 1064
adamc@1186 1065 val toEspecialize1 = transform especialize "especialize1" o toShake2
adamc@1181 1066
adamc@1062 1067 val toCore_untangle3 = transform core_untangle "core_untangle3" o toEspecialize1
adamc@1062 1068 val toShake3 = transform shake "shake3" o toCore_untangle3
adamc@642 1069
adamc@202 1070 val tag = {
adamc@202 1071 func = Tag.tag,
adamc@202 1072 print = CorePrint.p_file CoreEnv.empty
adamc@202 1073 }
adamc@193 1074
adamc@1062 1075 val toTag = transform tag "tag" o toShake3
adamc@20 1076
adamc@202 1077 val reduce = {
adamc@202 1078 func = Reduce.reduce,
adamc@202 1079 print = CorePrint.p_file CoreEnv.empty
adamc@202 1080 }
adamc@25 1081
adamc@692 1082 val toReduce = transform reduce "reduce" o toTag
adamc@23 1083
adamc@1186 1084 val toShakey = transform shake "shakey" o toReduce
adamc@1186 1085
adamc@1186 1086 val unpoly = {
adamc@1186 1087 func = Unpoly.unpoly,
adamc@1186 1088 print = CorePrint.p_file CoreEnv.empty
adamc@1186 1089 }
adamc@1186 1090
adamc@1186 1091 val toUnpoly = transform unpoly "unpoly" o toShakey
adamc@315 1092
adamc@202 1093 val specialize = {
adamc@202 1094 func = Specialize.specialize,
adamc@202 1095 print = CorePrint.p_file CoreEnv.empty
adamc@202 1096 }
adamc@132 1097
adamc@1186 1098 val toSpecialize = transform specialize "specialize" o toUnpoly
adamc@131 1099
adamc@1062 1100 val toShake4 = transform shake "shake4" o toSpecialize
adamc@133 1101
adamc@1062 1102 val toEspecialize2 = transform especialize "especialize2" o toShake4
adamc@1272 1103 val toShake4' = transform shake "shake4'" o toEspecialize2
adamc@1272 1104 val toUnpoly2 = transform unpoly "unpoly2" o toShake4'
adamc@1276 1105 val toSpecialize2 = transform specialize "specialize2" o toUnpoly2
adamc@1276 1106 val toShake4'' = transform shake "shake4'" o toSpecialize2
adamc@1272 1107 val toEspecialize3 = transform especialize "especialize3" o toShake4''
adamc@794 1108
adamc@1272 1109 val toReduce2 = transform reduce "reduce2" o toEspecialize3
adamc@898 1110
adamc@1062 1111 val toShake5 = transform shake "shake5" o toReduce2
adamc@794 1112
adamc@692 1113 val marshalcheck = {
adamc@692 1114 func = (fn file => (MarshalCheck.check file; file)),
adamc@692 1115 print = CorePrint.p_file CoreEnv.empty
adamc@692 1116 }
adamc@692 1117
adamc@1062 1118 val toMarshalcheck = transform marshalcheck "marshalcheck" o toShake5
adamc@692 1119
adamc@732 1120 val effectize = {
adamc@732 1121 func = Effective.effectize,
adamc@732 1122 print = CorePrint.p_file CoreEnv.empty
adamc@732 1123 }
adamc@732 1124
adamc@732 1125 val toEffectize = transform effectize "effectize" o toMarshalcheck
adamc@732 1126
adamc@1170 1127 val css = {
adamc@1170 1128 func = Css.summarize,
adamc@1170 1129 print = fn _ => Print.box []
adamc@1170 1130 }
adamc@1170 1131
adamc@1170 1132 val toCss = transform css "css" o toShake5
adamc@1170 1133
adamc@202 1134 val monoize = {
adamc@202 1135 func = Monoize.monoize CoreEnv.empty,
adamc@202 1136 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1137 }
adamc@134 1138
adamc@732 1139 val toMonoize = transform monoize "monoize" o toEffectize
adamc@96 1140
adamc@202 1141 val mono_opt = {
adamc@910 1142 func = MonoOpt.optimize,
adamc@202 1143 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1144 }
adamc@29 1145
adamc@270 1146 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 1147
adamc@202 1148 val untangle = {
adamc@202 1149 func = Untangle.untangle,
adamc@202 1150 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1151 }
adamc@1 1152
adamc@270 1153 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 1154
adamc@202 1155 val mono_reduce = {
adamc@202 1156 func = MonoReduce.reduce,
adamc@202 1157 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1158 }
adamc@16 1159
adamc@270 1160 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 1161
adamc@202 1162 val mono_shake = {
adamc@202 1163 func = MonoShake.shake,
adamc@202 1164 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1165 }
adamc@110 1166
adamc@270 1167 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 1168
adamc@572 1169 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@572 1170
adamc@1200 1171 val iflow = {
adamc@1235 1172 func = (fn file => (if !doIflow then Iflow.check file else (); file)),
adamc@1200 1173 print = MonoPrint.p_file MonoEnv.empty
adamc@1200 1174 }
adamc@1200 1175
adamc@1200 1176 val toIflow = transform iflow "iflow" o toMono_opt2
adamc@1200 1177
adamc@567 1178 val jscomp = {
adamc@567 1179 func = JsComp.process,
adamc@567 1180 print = MonoPrint.p_file MonoEnv.empty
adamc@567 1181 }
adamc@567 1182
adamc@1200 1183 val toJscomp = transform jscomp "jscomp" o toIflow
adamc@567 1184
adamc@910 1185 val toMono_opt3 = transform mono_opt "mono_opt3" o toJscomp
adamc@20 1186
adamc@506 1187 val fuse = {
adamc@506 1188 func = Fuse.fuse,
adamc@506 1189 print = MonoPrint.p_file MonoEnv.empty
adamc@506 1190 }
adamc@506 1191
adamc@572 1192 val toFuse = transform fuse "fuse" o toMono_opt3
adamc@506 1193
adamc@506 1194 val toUntangle2 = transform untangle "untangle2" o toFuse
adamc@506 1195
adamc@601 1196 val toMono_reduce2 = transform mono_reduce "mono_reduce2" o toUntangle2
adamc@601 1197 val toMono_shake2 = transform mono_shake "mono_shake2" o toMono_reduce2
adamc@916 1198 val toMono_opt4 = transform mono_opt "mono_opt4" o toMono_shake2
adamc@1017 1199 val toMono_reduce3 = transform mono_reduce "mono_reduce3" o toMono_opt4
adamc@1185 1200 val toFuse2 = transform fuse "fuse2" o toMono_reduce3
adamc@916 1201 val toUntangle3 = transform untangle "untangle3" o toFuse2
adamc@916 1202 val toMono_shake3 = transform mono_shake "mono_shake3" o toUntangle3
adamc@506 1203
adamc@377 1204 val pathcheck = {
adamc@377 1205 func = (fn file => (PathCheck.check file; file)),
adamc@377 1206 print = MonoPrint.p_file MonoEnv.empty
adamc@377 1207 }
adamc@377 1208
adamc@916 1209 val toPathcheck = transform pathcheck "pathcheck" o toMono_shake3
adamc@377 1210
adamc@202 1211 val cjrize = {
adamc@202 1212 func = Cjrize.cjrize,
adamc@202 1213 print = CjrPrint.p_file CjrEnv.empty
adamc@202 1214 }
adamc@23 1215
adamc@377 1216 val toCjrize = transform cjrize "cjrize" o toPathcheck
adamc@29 1217
adamc@643 1218 val scriptcheck = {
adamc@643 1219 func = ScriptCheck.classify,
adamc@643 1220 print = CjrPrint.p_file CjrEnv.empty
adamc@643 1221 }
adamc@643 1222
adamc@643 1223 val toScriptcheck = transform scriptcheck "scriptcheck" o toCjrize
adamc@643 1224
adamc@282 1225 val prepare = {
adamc@282 1226 func = Prepare.prepare,
adamc@282 1227 print = CjrPrint.p_file CjrEnv.empty
adamc@282 1228 }
adamc@282 1229
adamc@643 1230 val toPrepare = transform prepare "prepare" o toScriptcheck
adamc@282 1231
adamc@879 1232 val checknest = {
adamc@879 1233 func = fn f => if #supportsNestedPrepared (Settings.currentDbms ()) then f else Checknest.annotate f,
adamc@879 1234 print = CjrPrint.p_file CjrEnv.empty
adamc@879 1235 }
adamc@879 1236
adamc@879 1237 val toChecknest = transform checknest "checknest" o toPrepare
adamc@879 1238
adamc@274 1239 val sqlify = {
adamc@274 1240 func = Cjrize.cjrize,
adamc@274 1241 print = CjrPrint.p_sql CjrEnv.empty
adamc@274 1242 }
adamc@274 1243
adamc@274 1244 val toSqlify = transform sqlify "sqlify" o toMono_opt2
adamc@274 1245
adamc@764 1246 fun compileC {cname, oname, ename, libs, profile, debug, link = link'} =
adamc@183 1247 let
adamc@855 1248 val proto = Settings.currentProtocol ()
adamc@1095 1249
adam@1317 1250 val (lib, mhash) = if Settings.getStaticLinking () then
adam@1317 1251 (#linkStatic proto ^ " " ^ Config.lib ^ "/../liburweb.a", Config.libMhash ^ "/libmhash.a")
adam@1317 1252 else
adam@1317 1253 ("-L" ^ Config.lib ^ "/.. -lurweb " ^ #linkDynamic proto, "-L" ^ Config.libMhash ^ " -lmhash")
adamc@378 1254
adamc@1094 1255 val compile = "gcc " ^ Config.gccArgs ^ " -Wimplicit -Werror -O3 -fno-inline -I " ^ Config.includ
adamc@1096 1256 ^ " " ^ #compile proto
adamc@832 1257 ^ " -c " ^ cname ^ " -o " ^ oname
adamc@1096 1258
adam@1317 1259 val link = "gcc -Werror -O3 -lm -pthread " ^ Config.gccArgs ^ " " ^ libs ^ " " ^ lib ^ " " ^ mhash ^ " " ^ oname
adamc@1095 1260 ^ " -o " ^ ename
adamc@502 1261
adamc@502 1262 val (compile, link) =
adamc@502 1263 if profile then
adamc@502 1264 (compile ^ " -pg", link ^ " -pg")
adamc@502 1265 else
adamc@502 1266 (compile, link)
adamc@742 1267
adamc@742 1268 val (compile, link) =
adamc@742 1269 if debug then
adamc@742 1270 (compile ^ " -g", link ^ " -g")
adamc@742 1271 else
adamc@742 1272 (compile, link)
adamc@764 1273
adamc@764 1274 val link = foldl (fn (s, link) => link ^ " " ^ s) link link'
adamc@183 1275 in
adamc@1045 1276 OS.Process.isSuccess (OS.Process.system compile)
adamc@1045 1277 andalso OS.Process.isSuccess (OS.Process.system link)
adamc@183 1278 end
adamc@183 1279
adamc@202 1280 fun compile job =
adamc@879 1281 case run toChecknest job of
adamc@1045 1282 NONE => false
adamc@29 1283 | SOME file =>
adamc@202 1284 let
adamc@274 1285 val job = valOf (run (transform parseUrp "parseUrp") job)
adamc@102 1286
adamc@274 1287 val (cname, oname, cleanup) =
adamc@274 1288 if #debug job then
adamc@457 1289 ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ())
adamc@274 1290 else
adamc@274 1291 let
adamc@274 1292 val dir = OS.FileSys.tmpName ()
adamc@403 1293 val () = if OS.FileSys.access (dir, []) then
adamc@403 1294 OS.FileSys.remove dir
adamc@403 1295 else
adamc@403 1296 ()
adamc@457 1297 val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"}
adamc@457 1298 val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"}
adamc@274 1299 in
adamc@274 1300 OS.FileSys.mkDir dir;
adamc@274 1301 (cname, oname,
adamc@274 1302 fn () => (OS.FileSys.remove cname;
adamc@274 1303 OS.FileSys.remove oname;
adamc@473 1304 OS.FileSys.rmDir dir)
adamc@473 1305 handle OS.SysErr _ => OS.FileSys.rmDir dir)
adamc@274 1306 end
adamc@274 1307 val ename = #exe job
adamc@202 1308 in
adamc@274 1309 let
adamc@274 1310 val outf = TextIO.openOut cname
adamc@274 1311 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@432 1312
adamc@432 1313 val hasDb = List.exists (fn (Cjr.DDatabase _, _) => true | _ => false) (#1 file)
adamc@432 1314 val libs =
adamc@432 1315 if hasDb then
adamc@866 1316 #link (Settings.currentDbms ())
adamc@432 1317 else
adamc@432 1318 ""
adamc@274 1319 in
adamc@274 1320 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@415 1321 TextIO.output1 (outf, #"\n");
adamc@274 1322 TextIO.closeOut outf;
adamc@102 1323
adamc@274 1324 case #sql job of
adamc@274 1325 NONE => ()
adamc@274 1326 | SOME sql =>
adamc@274 1327 let
adamc@274 1328 val outf = TextIO.openOut sql
adamc@274 1329 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@274 1330 in
adamc@274 1331 Print.fprint s (CjrPrint.p_sql CjrEnv.empty file);
adamc@274 1332 TextIO.closeOut outf
adamc@274 1333 end;
adamc@274 1334
adamc@742 1335 compileC {cname = cname, oname = oname, ename = ename, libs = libs,
adamc@1045 1336 profile = #profile job, debug = #debug job, link = #link job}
adamc@274 1337
adamc@1045 1338 before cleanup ()
adamc@274 1339 end
adamc@274 1340 handle ex => (((cleanup ()) handle _ => ()); raise ex)
adamc@202 1341 end
adamc@29 1342
adamc@1045 1343 fun compiler job =
adamc@1045 1344 if compile job then
adamc@1045 1345 ()
adamc@1045 1346 else
adamc@1045 1347 OS.Process.exit OS.Process.failure
adamc@1045 1348
adamc@1266 1349 fun moduleOf fname =
adamc@1266 1350 let
adamc@1266 1351 val mrs = !moduleRoots
adamc@1266 1352 val fname = OS.Path.mkCanonical fname
adamc@1266 1353 in
adamc@1266 1354 case List.find (fn (root, _) => String.isPrefix (root ^ "/") fname) mrs of
adamc@1266 1355 NONE => capitalize (OS.Path.base (OS.Path.file fname))
adamc@1266 1356 | SOME (root, name) =>
adamc@1266 1357 let
adamc@1266 1358 val fname = OS.Path.base fname
adamc@1266 1359 val fname = String.extract (fname, size root + 1, NONE)
adamc@1266 1360 val fs = String.fields (fn ch => ch = #"/") fname
adamc@1266 1361 val fs = List.filter (CharVector.exists (fn ch => not (Char.isDigit ch))) fs
adamc@1266 1362 val fs = map capitalize fs
adamc@1266 1363 in
adamc@1266 1364 String.concatWith "." (name :: fs)
adamc@1266 1365 end
adamc@1266 1366 end
adamc@1266 1367
adamc@1 1368 end