annotate src/prepare.sml @ 548:b3b06fef7bb5

Treat user-provided error messages as XML
author Adam Chlipala <adamc@hcoop.net>
date Sun, 07 Dec 2008 11:19:13 -0500
parents 0fd65c50e0e2
children 162d5308e34f
rev   line source
adamc@282 1 (* Copyright (c) 2008, Adam Chlipala
adamc@282 2 * All rights reserved.
adamc@282 3 *
adamc@282 4 * Redistribution and use in source and binary forms, with or without
adamc@282 5 * modification, are permitted provided that the following conditions are met:
adamc@282 6 *
adamc@282 7 * - Redistributions of source code must retain the above copyright notice,
adamc@282 8 * this list of conditions and the following disclaimer.
adamc@282 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@282 10 * this list of conditions and the following disclaimer in the documentation
adamc@282 11 * and/or other materials provided with the distribution.
adamc@282 12 * - The names of contributors may not be used to endorse or promote products
adamc@282 13 * derived from this software without specific prior written permission.
adamc@282 14 *
adamc@282 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@282 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@282 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@282 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@282 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@282 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@282 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@282 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@282 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@282 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@282 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@282 26 *)
adamc@282 27
adamc@282 28 structure Prepare :> PREPARE = struct
adamc@282 29
adamc@282 30 open Cjr
adamc@282 31
adamc@282 32 fun prepString (e, ss, n) =
adamc@282 33 case #1 e of
adamc@282 34 EPrim (Prim.String s) =>
adamc@282 35 SOME (s :: ss, n)
adamc@282 36 | EFfiApp ("Basis", "strcat", [e1, e2]) =>
adamc@282 37 (case prepString (e1, ss, n) of
adamc@282 38 NONE => NONE
adamc@282 39 | SOME (ss, n) => prepString (e2, ss, n))
adamc@282 40 | EFfiApp ("Basis", "sqlifyInt", [e]) =>
adamc@282 41 SOME ("$" ^ Int.toString (n + 1) ^ "::int8" :: ss, n + 1)
adamc@282 42 | EFfiApp ("Basis", "sqlifyFloat", [e]) =>
adamc@282 43 SOME ("$" ^ Int.toString (n + 1) ^ "::float8" :: ss, n + 1)
adamc@282 44 | EFfiApp ("Basis", "sqlifyString", [e]) =>
adamc@282 45 SOME ("$" ^ Int.toString (n + 1) ^ "::text" :: ss, n + 1)
adamc@282 46 | EFfiApp ("Basis", "sqlifyBool", [e]) =>
adamc@282 47 SOME ("$" ^ Int.toString (n + 1) ^ "::bool" :: ss, n + 1)
adamc@439 48 | EFfiApp ("Basis", "sqlifyTime", [e]) =>
adamc@439 49 SOME ("$" ^ Int.toString (n + 1) ^ "::timestamp" :: ss, n + 1)
adamc@468 50
adamc@468 51 | EFfiApp ("Basis", "sqlifyIntN", [e]) =>
adamc@468 52 SOME ("$" ^ Int.toString (n + 1) ^ "::int8" :: ss, n + 1)
adamc@468 53 | EFfiApp ("Basis", "sqlifyFloatN", [e]) =>
adamc@468 54 SOME ("$" ^ Int.toString (n + 1) ^ "::float8" :: ss, n + 1)
adamc@468 55 | EFfiApp ("Basis", "sqlifyStringN", [e]) =>
adamc@468 56 SOME ("$" ^ Int.toString (n + 1) ^ "::text" :: ss, n + 1)
adamc@468 57 | EFfiApp ("Basis", "sqlifyBoolN", [e]) =>
adamc@468 58 SOME ("$" ^ Int.toString (n + 1) ^ "::bool" :: ss, n + 1)
adamc@468 59 | EFfiApp ("Basis", "sqlifyTimeN", [e]) =>
adamc@468 60 SOME ("$" ^ Int.toString (n + 1) ^ "::timestamp" :: ss, n + 1)
adamc@468 61
adamc@322 62 | ECase (e,
adamc@322 63 [((PCon (_, PConFfi {mod = "Basis", con = "True", ...}, _), _),
adamc@322 64 (EPrim (Prim.String "TRUE"), _)),
adamc@322 65 ((PCon (_, PConFfi {mod = "Basis", con = "False", ...}, _), _),
adamc@322 66 (EPrim (Prim.String "FALSE"), _))],
adamc@322 67 _) => SOME ("$" ^ Int.toString (n + 1) ^ "::bool" :: ss, n + 1)
adamc@282 68
adamc@282 69 | _ => NONE
adamc@282 70
adamc@282 71 fun prepExp (e as (_, loc), sns) =
adamc@282 72 case #1 e of
adamc@282 73 EPrim _ => (e, sns)
adamc@282 74 | ERel _ => (e, sns)
adamc@282 75 | ENamed _ => (e, sns)
adamc@282 76 | ECon (_, _, NONE) => (e, sns)
adamc@282 77 | ECon (dk, pc, SOME e) =>
adamc@282 78 let
adamc@282 79 val (e, sns) = prepExp (e, sns)
adamc@282 80 in
adamc@282 81 ((ECon (dk, pc, SOME e), loc), sns)
adamc@282 82 end
adamc@297 83 | ENone t => (e, sns)
adamc@291 84 | ESome (t, e) =>
adamc@291 85 let
adamc@291 86 val (e, sns) = prepExp (e, sns)
adamc@291 87 in
adamc@291 88 ((ESome (t, e), loc), sns)
adamc@291 89 end
adamc@282 90 | EFfi _ => (e, sns)
adamc@282 91 | EFfiApp (m, x, es) =>
adamc@282 92 let
adamc@282 93 val (es, sns) = ListUtil.foldlMap prepExp sns es
adamc@282 94 in
adamc@282 95 ((EFfiApp (m, x, es), loc), sns)
adamc@282 96 end
adamc@316 97 | EApp (e1, es) =>
adamc@282 98 let
adamc@282 99 val (e1, sns) = prepExp (e1, sns)
adamc@316 100 val (es, sns) = ListUtil.foldlMap prepExp sns es
adamc@282 101 in
adamc@316 102 ((EApp (e1, es), loc), sns)
adamc@282 103 end
adamc@282 104
adamc@387 105 | EUnop (s, e1) =>
adamc@387 106 let
adamc@387 107 val (e1, sns) = prepExp (e1, sns)
adamc@387 108 in
adamc@387 109 ((EUnop (s, e1), loc), sns)
adamc@387 110 end
adamc@387 111 | EBinop (s, e1, e2) =>
adamc@387 112 let
adamc@387 113 val (e1, sns) = prepExp (e1, sns)
adamc@387 114 val (e2, sns) = prepExp (e2, sns)
adamc@387 115 in
adamc@387 116 ((EBinop (s, e1, e2), loc), sns)
adamc@387 117 end
adamc@387 118
adamc@282 119 | ERecord (rn, xes) =>
adamc@282 120 let
adamc@282 121 val (xes, sns) = ListUtil.foldlMap (fn ((x, e), sns) =>
adamc@282 122 let
adamc@282 123 val (e, sns) = prepExp (e, sns)
adamc@282 124 in
adamc@282 125 ((x, e), sns)
adamc@282 126 end) sns xes
adamc@282 127 in
adamc@282 128 ((ERecord (rn, xes), loc), sns)
adamc@282 129 end
adamc@282 130 | EField (e, s) =>
adamc@282 131 let
adamc@282 132 val (e, sns) = prepExp (e, sns)
adamc@282 133 in
adamc@282 134 ((EField (e, s), loc), sns)
adamc@282 135 end
adamc@282 136
adamc@282 137 | ECase (e, pes, ts) =>
adamc@282 138 let
adamc@282 139 val (e, sns) = prepExp (e, sns)
adamc@282 140 val (pes, sns) = ListUtil.foldlMap (fn ((p, e), sns) =>
adamc@282 141 let
adamc@282 142 val (e, sns) = prepExp (e, sns)
adamc@282 143 in
adamc@282 144 ((p, e), sns)
adamc@282 145 end) sns pes
adamc@282 146 in
adamc@282 147 ((ECase (e, pes, ts), loc), sns)
adamc@282 148 end
adamc@282 149
adamc@283 150 | EError (e, t) =>
adamc@283 151 let
adamc@283 152 val (e, sns) = prepExp (e, sns)
adamc@283 153 in
adamc@283 154 ((EError (e, t), loc), sns)
adamc@283 155 end
adamc@283 156
adamc@282 157 | EWrite e =>
adamc@282 158 let
adamc@282 159 val (e, sns) = prepExp (e, sns)
adamc@282 160 in
adamc@282 161 ((EWrite e, loc), sns)
adamc@282 162 end
adamc@282 163 | ESeq (e1, e2) =>
adamc@282 164 let
adamc@282 165 val (e1, sns) = prepExp (e1, sns)
adamc@282 166 val (e2, sns) = prepExp (e2, sns)
adamc@282 167 in
adamc@282 168 ((ESeq (e1, e2), loc), sns)
adamc@282 169 end
adamc@282 170 | ELet (x, t, e1, e2) =>
adamc@282 171 let
adamc@282 172 val (e1, sns) = prepExp (e1, sns)
adamc@282 173 val (e2, sns) = prepExp (e2, sns)
adamc@282 174 in
adamc@282 175 ((ELet (x, t, e1, e2), loc), sns)
adamc@282 176 end
adamc@282 177
adamc@282 178 | EQuery {exps, tables, rnum, state, query, body, initial, ...} =>
adamc@486 179 let
adamc@486 180 val (body, sns) = prepExp (body, sns)
adamc@486 181 in
adamc@486 182 case prepString (query, [], 0) of
adamc@486 183 NONE =>
adamc@486 184 ((EQuery {exps = exps, tables = tables, rnum = rnum,
adamc@486 185 state = state, query = query, body = body,
adamc@491 186 initial = initial, prepared = NONE}, loc),
adamc@486 187 sns)
adamc@486 188 | SOME (ss, n) =>
adamc@486 189 ((EQuery {exps = exps, tables = tables, rnum = rnum,
adamc@486 190 state = state, query = query, body = body,
adamc@486 191 initial = initial, prepared = SOME (#2 sns)}, loc),
adamc@486 192 ((String.concat (rev ss), n) :: #1 sns, #2 sns + 1))
adamc@486 193 end
adamc@282 194
adamc@307 195 | EDml {dml, ...} =>
adamc@307 196 (case prepString (dml, [], 0) of
adamc@307 197 NONE => (e, sns)
adamc@307 198 | SOME (ss, n) =>
adamc@307 199 ((EDml {dml = dml, prepared = SOME (#2 sns)}, loc),
adamc@307 200 ((String.concat (rev ss), n) :: #1 sns, #2 sns + 1)))
adamc@307 201
adamc@338 202 | ENextval {seq, ...} =>
adamc@338 203 let
adamc@486 204 val s = case seq of
adamc@486 205 (EPrim (Prim.String s), loc) =>
adamc@486 206 (EPrim (Prim.String ("SELECT NEXTVAL('" ^ s ^ "')")), loc)
adamc@486 207 | _ =>
adamc@486 208 let
adamc@486 209 val s' = (EFfiApp ("Basis", "strcat", [seq, (EPrim (Prim.String "')"), loc)]), loc)
adamc@486 210 in
adamc@486 211 (EFfiApp ("Basis", "strcat", [(EPrim (Prim.String "SELECT NEXTVAL('"), loc), s']), loc)
adamc@486 212 end
adamc@338 213 in
adamc@338 214 case prepString (s, [], 0) of
adamc@338 215 NONE => (e, sns)
adamc@338 216 | SOME (ss, n) =>
adamc@338 217 ((ENextval {seq = seq, prepared = SOME (#2 sns)}, loc),
adamc@338 218 ((String.concat (rev ss), n) :: #1 sns, #2 sns + 1))
adamc@338 219 end
adamc@338 220
adamc@463 221 | EUnurlify (e, t) =>
adamc@463 222 let
adamc@463 223 val (e, sns) = prepExp (e, sns)
adamc@463 224 in
adamc@463 225 ((EUnurlify (e, t), loc), sns)
adamc@463 226 end
adamc@463 227
adamc@282 228 fun prepDecl (d as (_, loc), sns) =
adamc@282 229 case #1 d of
adamc@282 230 DStruct _ => (d, sns)
adamc@282 231 | DDatatype _ => (d, sns)
adamc@282 232 | DDatatypeForward _ => (d, sns)
adamc@282 233 | DVal (x, n, t, e) =>
adamc@282 234 let
adamc@282 235 val (e, sns) = prepExp (e, sns)
adamc@282 236 in
adamc@282 237 ((DVal (x, n, t, e), loc), sns)
adamc@282 238 end
adamc@282 239 | DFun (x, n, xts, t, e) =>
adamc@282 240 let
adamc@282 241 val (e, sns) = prepExp (e, sns)
adamc@282 242 in
adamc@282 243 ((DFun (x, n, xts, t, e), loc), sns)
adamc@282 244 end
adamc@282 245 | DFunRec fs =>
adamc@282 246 let
adamc@282 247 val (fs, sns) = ListUtil.foldlMap (fn ((x, n, xts, t, e), sns) =>
adamc@282 248 let
adamc@282 249 val (e, sns) = prepExp (e, sns)
adamc@282 250 in
adamc@282 251 ((x, n, xts, t, e), sns)
adamc@282 252 end) sns fs
adamc@282 253 in
adamc@282 254 ((DFunRec fs, loc), sns)
adamc@282 255 end
adamc@282 256
adamc@282 257 | DTable _ => (d, sns)
adamc@338 258 | DSequence _ => (d, sns)
adamc@282 259 | DDatabase _ => (d, sns)
adamc@282 260 | DPreparedStatements _ => (d, sns)
adamc@282 261
adamc@282 262 fun prepare (ds, ps) =
adamc@282 263 let
adamc@282 264 val (ds, (sns, _)) = ListUtil.foldlMap prepDecl ([], 0) ds
adamc@282 265 in
adamc@282 266 ((DPreparedStatements (rev sns), ErrorMsg.dummySpan) :: ds, ps)
adamc@282 267 end
adamc@282 268
adamc@282 269 end
adamc@282 270