annotate src/scriptcheck.sml @ 647:ae374df5ccbd

Prose for ListEdit
author Adam Chlipala <adamc@hcoop.net>
date Tue, 10 Mar 2009 13:46:45 -0400
parents 1b571a05874c
children b0c1a46b1f15
rev   line source
adamc@643 1 (* Copyright (c) 2009, Adam Chlipala
adamc@643 2 * All rights reserved.
adamc@643 3 *
adamc@643 4 * Redistribution and use in source and binary forms, with or without
adamc@643 5 * modification, are permitted provided that the following conditions are met:
adamc@643 6 *
adamc@643 7 * - Redistributions of source code must retain the above copyright notice,
adamc@643 8 * this list of conditions and the following disclaimer.
adamc@643 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@643 10 * this list of conditions and the following disclaimer in the documentation
adamc@643 11 * and/or other materials provided with the distribution.
adamc@643 12 * - The names of contributors may not be used to endorse or promote products
adamc@643 13 * derived from this software without specific prior written permission.
adamc@643 14 *
adamc@643 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@643 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@643 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@643 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@643 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@643 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@643 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@643 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@643 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@643 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@643 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@643 26 *)
adamc@643 27
adamc@643 28 structure ScriptCheck :> SCRIPT_CHECK = struct
adamc@643 29
adamc@643 30 open Cjr
adamc@643 31
adamc@643 32 structure SS = BinarySetFn(struct
adamc@643 33 type ord_key = string
adamc@643 34 val compare = String.compare
adamc@643 35 end)
adamc@643 36 structure IS = IntBinarySet
adamc@643 37
adamc@643 38 val csBasis = SS.addList (SS.empty,
adamc@643 39 ["new_client_source",
adamc@643 40 "get_client_source",
adamc@645 41 "set_client_source"])
adamc@645 42
adamc@645 43 val scriptWords = ["<script",
adamc@645 44 " onclick="]
adamc@643 45
adamc@643 46 fun classify (ds, ps) =
adamc@643 47 let
adamc@643 48 fun inString {needle, haystack} =
adamc@643 49 let
adamc@643 50 val (_, suffix) = Substring.position needle (Substring.full haystack)
adamc@643 51 in
adamc@643 52 not (Substring.isEmpty suffix)
adamc@643 53 end
adamc@643 54
adamc@643 55 fun hasClient csids =
adamc@643 56 let
adamc@643 57 fun hasClient e =
adamc@643 58 case #1 e of
adamc@645 59 EPrim (Prim.String s) => List.exists (fn n => inString {needle = n, haystack = s}) scriptWords
adamc@643 60 | EPrim _ => false
adamc@643 61 | ERel _ => false
adamc@643 62 | ENamed n => IS.member (csids, n)
adamc@643 63 | ECon (_, _, NONE) => false
adamc@643 64 | ECon (_, _, SOME e) => hasClient e
adamc@643 65 | ENone _ => false
adamc@643 66 | ESome (_, e) => hasClient e
adamc@643 67 | EFfi ("Basis", x) => SS.member (csBasis, x)
adamc@643 68 | EFfi _ => false
adamc@643 69 | EFfiApp ("Basis", x, es) => SS.member (csBasis, x)
adamc@643 70 orelse List.exists hasClient es
adamc@643 71 | EFfiApp (_, _, es) => List.exists hasClient es
adamc@643 72 | EApp (e, es) => hasClient e orelse List.exists hasClient es
adamc@643 73 | EUnop (_, e) => hasClient e
adamc@643 74 | EBinop (_, e1, e2) => hasClient e1 orelse hasClient e2
adamc@643 75 | ERecord (_, xes) => List.exists (hasClient o #2) xes
adamc@643 76 | EField (e, _) => hasClient e
adamc@643 77 | ECase (e, pes, _) => hasClient e orelse List.exists (hasClient o #2) pes
adamc@643 78 | EError (e, _) => hasClient e
adamc@643 79 | EWrite e => hasClient e
adamc@643 80 | ESeq (e1, e2) => hasClient e1 orelse hasClient e2
adamc@643 81 | ELet (_, _, e1, e2) => hasClient e1 orelse hasClient e2
adamc@643 82 | EQuery {query, body, initial, ...} => hasClient query orelse hasClient body
adamc@643 83 orelse hasClient initial
adamc@643 84 | EDml {dml, ...} => hasClient dml
adamc@643 85 | ENextval {seq, ...} => hasClient seq
adamc@643 86 | EUnurlify (e, _) => hasClient e
adamc@643 87 in
adamc@643 88 hasClient
adamc@643 89 end
adamc@643 90
adamc@643 91 fun decl ((d, _), csids) =
adamc@643 92 let
adamc@643 93 val hasClient = hasClient csids
adamc@643 94 in
adamc@643 95 case d of
adamc@643 96 DVal (_, n, _, e) => if hasClient e then
adamc@643 97 IS.add (csids, n)
adamc@643 98 else
adamc@643 99 csids
adamc@643 100 | DFun (_, n, _, _, e) => if hasClient e then
adamc@643 101 IS.add (csids, n)
adamc@643 102 else
adamc@643 103 csids
adamc@643 104 | DFunRec xes => if List.exists (fn (_, _, _, _, e) => hasClient e) xes then
adamc@643 105 foldl (fn ((_, n, _, _, _), csids) => IS.add (csids, n))
adamc@643 106 csids xes
adamc@643 107 else
adamc@643 108 csids
adamc@643 109 | _ => csids
adamc@643 110 end
adamc@643 111
adamc@643 112 val csids = foldl decl IS.empty ds
adamc@643 113
adamc@643 114 val ps = map (fn (ek, x, n, ts, t, _) =>
adamc@643 115 (ek, x, n, ts, t,
adamc@643 116 if IS.member (csids, n) then
adamc@643 117 ServerAndClient
adamc@643 118 else
adamc@643 119 ServerOnly)) ps
adamc@643 120 in
adamc@643 121 (ds, ps)
adamc@643 122 end
adamc@643 123
adamc@643 124 end
adamc@643 125