annotate src/compiler.sml @ 1437:493e087f5479

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