annotate src/urweb.grm @ 1602:e44be6ece475

COALESCE
author Adam Chlipala <adam@chlipala.net>
date Sat, 19 Nov 2011 10:43:57 -0500
parents 911ebbd23919
children da788bd72c9e
rev   line source
adamc@1191 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@339 15 * THIS SOFTARE 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@244 28 (* Grammar for Ur/Web programs *)
adamc@1 29
adamc@4 30 open Source
adamc@1 31
adamc@1 32 val s = ErrorMsg.spanOf
adamc@230 33 val dummy = ErrorMsg.dummySpan
adamc@1 34
adamc@204 35 fun capitalize "" = ""
adamc@204 36 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@104 37
adamc@203 38 fun entable t =
adamc@203 39 case #1 t of
adamc@203 40 TRecord c => c
adamc@203 41 | _ => t
adamc@203 42
adamc@207 43 datatype select_item =
adamc@207 44 Field of con * con
adamc@1194 45 | Exp of con option * exp
adamc@341 46 | Fields of con * con
adamc@207 47
adamc@207 48 datatype select =
adamc@207 49 Star
adamc@207 50 | Items of select_item list
adamc@207 51
adamc@226 52 datatype group_item =
adamc@226 53 GField of con * con
adam@1425 54 | GFields of con * con
adamc@226 55
adamc@207 56 fun eqTnames ((c1, _), (c2, _)) =
adamc@207 57 case (c1, c2) of
adamc@207 58 (CVar (ms1, x1), CVar (ms2, x2)) => ms1 = ms2 andalso x1 = x2
adamc@207 59 | (CName x1, CName x2) => x1 = x2
adamc@207 60 | _ => false
adamc@207 61
adam@1424 62 fun nameString (c, _) =
adam@1424 63 case c of
adam@1424 64 CName s => s
adam@1424 65 | CVar (_, x) => x
adam@1424 66 | _ => "?"
adam@1424 67
adamc@1194 68 fun amend_select loc (si, (count, tabs, exps)) =
adamc@233 69 case si of
adamc@233 70 Field (tx, fx) =>
adamc@233 71 let
adamc@233 72 val c = (CRecord ([(fx, (CWild (KType, loc), loc))]), loc)
adamc@207 73
adamc@233 74 val (tabs, found) = ListUtil.foldlMap (fn ((tx', c'), found) =>
adamc@233 75 if eqTnames (tx, tx') then
adamc@233 76 ((tx', (CConcat (c, c'), loc)), true)
adamc@233 77 else
adamc@233 78 ((tx', c'), found))
adamc@233 79 false tabs
adamc@233 80 in
adamc@233 81 if found then
adamc@233 82 ()
adamc@233 83 else
adam@1424 84 ErrorMsg.errorAt loc ("Select of field " ^ nameString fx ^ " from unbound table " ^ nameString tx);
adamc@233 85
adamc@1194 86 (count, tabs, exps)
adamc@233 87 end
adamc@341 88 | Fields (tx, fs) =>
adamc@341 89 let
adamc@341 90 val (tabs, found) = ListUtil.foldlMap (fn ((tx', c'), found) =>
adamc@341 91 if eqTnames (tx, tx') then
adamc@341 92 ((tx', (CConcat (fs, c'), loc)), true)
adamc@341 93 else
adamc@341 94 ((tx', c'), found))
adamc@341 95 false tabs
adamc@341 96 in
adamc@341 97 if found then
adamc@341 98 ()
adamc@341 99 else
adamc@341 100 ErrorMsg.errorAt loc "Select of field from unbound table";
adamc@341 101
adamc@1194 102 (count, tabs, exps)
adamc@341 103 end
adamc@1194 104 | Exp (SOME c, e) => (count, tabs, (c, e) :: exps)
adamc@1194 105 | Exp (NONE, e) => (count+1, tabs, ((CName (Int.toString count), loc), e) :: exps)
adamc@207 106
adamc@226 107 fun amend_group loc (gi, tabs) =
adamc@226 108 let
adamc@226 109 val (tx, c) = case gi of
adamc@226 110 GField (tx, fx) => (tx, (CRecord ([(fx, (CWild (KType, loc), loc))]), loc))
adam@1425 111 | GFields (tx, fxs) => (tx, fxs)
adamc@226 112
adamc@226 113 val (tabs, found) = ListUtil.foldlMap (fn ((tx', c'), found) =>
adamc@226 114 if eqTnames (tx, tx') then
adamc@226 115 ((tx', (CConcat (c, c'), loc)), true)
adamc@226 116 else
adamc@226 117 ((tx', c'), found))
adam@1425 118 false tabs
adamc@226 119 in
adamc@226 120 if found then
adamc@226 121 ()
adamc@226 122 else
adamc@226 123 ErrorMsg.errorAt loc "Select of field from unbound table";
adamc@226 124
adamc@226 125 tabs
adamc@226 126 end
adamc@226 127
adamc@403 128 fun sql_inject (v, loc) =
adamc@403 129 (EApp ((EVar (["Basis"], "sql_inject", Infer), loc), (v, loc)), loc)
adamc@209 130
adamc@220 131 fun sql_binary (oper, sqlexp1, sqlexp2, loc) =
adamc@220 132 let
adamc@403 133 val e = (EVar (["Basis"], "sql_binary", Infer), loc)
adamc@403 134 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper, Infer), loc)), loc)
adamc@220 135 val e = (EApp (e, sqlexp1), loc)
adamc@220 136 in
adamc@220 137 (EApp (e, sqlexp2), loc)
adamc@220 138 end
adamc@220 139
adamc@220 140 fun sql_unary (oper, sqlexp, loc) =
adamc@220 141 let
adamc@403 142 val e = (EVar (["Basis"], "sql_unary", Infer), loc)
adamc@403 143 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper, Infer), loc)), loc)
adamc@220 144 in
adamc@220 145 (EApp (e, sqlexp), loc)
adamc@219 146 end
adamc@219 147
adam@1427 148 fun sql_relop (oper, all, sqlexp1, sqlexp2, loc) =
adamc@229 149 let
adamc@403 150 val e = (EVar (["Basis"], "sql_relop", Infer), loc)
adamc@403 151 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper, Infer), loc)), loc)
adam@1427 152 val e = (EApp (e, (EVar (["Basis"], if all then "True" else "False", Infer), loc)), loc)
adamc@229 153 val e = (EApp (e, sqlexp1), loc)
adamc@229 154 in
adamc@229 155 (EApp (e, sqlexp2), loc)
adamc@229 156 end
adamc@229 157
adamc@441 158 fun sql_nfunc (oper, loc) =
adamc@441 159 let
adamc@441 160 val e = (EVar (["Basis"], "sql_nfunc", Infer), loc)
adamc@441 161 in
adamc@441 162 (EApp (e, (EVar (["Basis"], "sql_" ^ oper, Infer), loc)), loc)
adamc@441 163 end
adamc@441 164
adamc@389 165 fun native_unop (oper, e1, loc) =
adamc@389 166 let
adamc@403 167 val e = (EVar (["Basis"], oper, Infer), loc)
adamc@389 168 in
adamc@389 169 (EApp (e, e1), loc)
adamc@389 170 end
adamc@389 171
adamc@256 172 fun native_op (oper, e1, e2, loc) =
adamc@256 173 let
adamc@403 174 val e = (EVar (["Basis"], oper, Infer), loc)
adamc@256 175 val e = (EApp (e, e1), loc)
adamc@256 176 in
adamc@256 177 (EApp (e, e2), loc)
adamc@256 178 end
adamc@256 179
adamc@310 180 val inDml = ref false
adamc@310 181
adamc@325 182 fun tagIn bt =
adamc@325 183 case bt of
adamc@325 184 "table" => "tabl"
adamc@325 185 | _ => bt
adamc@325 186
adamc@709 187 datatype prop_kind = Delete | Update
adamc@709 188
adamc@721 189 datatype attr = Class of exp | Normal of con * exp
adamc@721 190
adamc@822 191 fun patType loc (p : pat) =
adamc@822 192 case #1 p of
adamc@822 193 PAnnot (_, t) => t
adamc@822 194 | _ => (CWild (KType, loc), loc)
adamc@822 195
adamc@1265 196 fun tnamesOf (e, _) =
adamc@1265 197 case e of
adamc@1265 198 EApp (e1, e2) => tnamesOf e1 @ tnamesOf e2
adamc@1265 199 | ECApp (e, c as (CName _, _)) =>
adamc@1265 200 let
adamc@1265 201 fun isFt (e, _) =
adamc@1265 202 case e of
adamc@1265 203 EVar (["Basis"], "sql_from_table", _) => true
adamc@1265 204 | EVar ([], "sql_from_table", _) => true
adamc@1265 205 | ECApp (e, _) => isFt e
adamc@1265 206 | EApp (e, _) => isFt e
adamc@1265 207 | EDisjointApp e => isFt e
adamc@1265 208 | _ => false
adamc@1265 209 in
adamc@1265 210 (if isFt e then [c] else []) @ tnamesOf e
adamc@1265 211 end
adamc@1265 212 | ECApp (e, _) => tnamesOf e
adamc@1265 213 | EDisjointApp e => tnamesOf e
adamc@1265 214 | _ => []
adamc@1265 215
adamc@1 216 %%
adamc@244 217 %header (functor UrwebLrValsFn(structure Token : TOKEN))
adamc@1 218
adamc@1 219 %term
adamc@1 220 EOF
adamc@821 221 | STRING of string | INT of Int64.int | FLOAT of Real64.real | CHAR of char
adamc@1 222 | SYMBOL of string | CSYMBOL of string
adamc@1 223 | LPAREN | RPAREN | LBRACK | RBRACK | LBRACE | RBRACE
adam@1306 224 | EQ | COMMA | COLON | DCOLON | DCOLONWILD | TCOLON | TCOLONWILD | DOT | HASH | UNDER | UNDERUNDER | BAR
adamc@403 225 | PLUS | MINUS | DIVIDE | DOTDOTDOT | MOD | AT
adamc@623 226 | CON | LTYPE | VAL | REC | AND | FUN | MAP | UNIT | KUNIT | CLASS
adamc@156 227 | DATATYPE | OF
adamc@7 228 | TYPE | NAME
adamc@629 229 | ARROW | LARROW | DARROW | STAR | SEMI | KARROW | DKARROW | BANG
adamc@674 230 | FN | PLUSPLUS | MINUSMINUS | MINUSMINUSMINUS | DOLLAR | TWIDDLE | CARET
adamc@446 231 | LET | IN
adamc@1071 232 | STRUCTURE | SIGNATURE | STRUCT | SIG | END | FUNCTOR | WHERE | EXTERN | SQL | SELECT1
adamc@754 233 | INCLUDE | OPEN | CONSTRAINT | CONSTRAINTS | EXPORT | TABLE | SEQUENCE | VIEW
adamc@1199 234 | COOKIE | STYLE | TASK | POLICY
adamc@842 235 | CASE | IF | THEN | ELSE | ANDALSO | ORELSE
adamc@1 236
adamc@360 237 | XML_BEGIN of string | XML_END | XML_BEGIN_END of string
adamc@91 238 | NOTAGS of string
adamc@91 239 | BEGIN_TAG of string | END_TAG of string
adamc@91 240
adamc@993 241 | SELECT | DISTINCT | FROM | AS | CWHERE | GROUP | ORDER | BY | HAVING
adamc@229 242 | UNION | INTERSECT | EXCEPT
adamc@232 243 | LIMIT | OFFSET | ALL
adamc@220 244 | TRUE | FALSE | CAND | OR | NOT
adamc@236 245 | COUNT | AVG | SUM | MIN | MAX
adamc@268 246 | ASC | DESC
adam@1602 247 | INSERT | INTO | VALUES | UPDATE | SET | DELETE | NULL | IS | COALESCE
adamc@441 248 | CURRENT_TIMESTAMP
adamc@219 249 | NE | LT | LE | GT | GE
adamc@714 250 | CCONSTRAINT | UNIQUE | CHECK | PRIMARY | FOREIGN | KEY | ON | NO | ACTION | RESTRICT | CASCADE | REFERENCES
adamc@751 251 | JOIN | INNER | CROSS | OUTER | LEFT | RIGHT | FULL
kkallio@1572 252 | CIF | CTHEN | CELSE
adamc@204 253
adamc@30 254 %nonterm
adamc@1 255 file of decl list
adamc@1 256 | decls of decl list
adamc@325 257 | decl of decl list
adamc@123 258 | vali of string * con option * exp
adamc@123 259 | valis of (string * con option * exp) list
adamc@242 260 | copt of con option
adamc@1 261
adamc@191 262 | dargs of string list
adamc@156 263 | barOpt of unit
adamc@156 264 | dcons of (string * con option) list
adamc@805 265 | dtype of string * string list * (string * con option) list
adamc@805 266 | dtypes of (string * string list * (string * con option) list) list
adamc@156 267 | dcon of string * con option
adamc@156 268
adamc@707 269 | pkopt of exp
adamc@707 270 | commaOpt of unit
adamc@707 271
adamc@704 272 | cst of exp
adamc@704 273 | csts of exp
adamc@704 274 | cstopt of exp
adamc@704 275
adamc@1001 276 | ckl of (string * kind option) list
adamc@1001 277
adamc@709 278 | pmode of prop_kind * exp
adamc@709 279 | pkind of prop_kind
adamc@709 280 | prule of exp
adamc@709 281 | pmodes of (prop_kind * exp) list
adamc@709 282
adamc@30 283 | sgn of sgn
adamc@42 284 | sgntm of sgn
adamc@30 285 | sgi of sgn_item
adamc@30 286 | sgis of sgn_item list
adamc@30 287
adamc@30 288 | str of str
adamc@30 289
adamc@1 290 | kind of kind
adamc@207 291 | ktuple of kind list
adamc@1 292 | kcolon of explicitness
adamc@240 293 | kopt of kind option
adamc@1 294
adamc@34 295 | path of string list * string
adamc@156 296 | cpath of string list * string
adamc@34 297 | spath of str
adamc@59 298 | mpath of string list
adamc@34 299
adamc@1 300 | cexp of con
adam@1577 301 | cexpO of con option
adamc@1 302 | capps of con
adamc@1 303 | cterm of con
adamc@195 304 | ctuple of con list
adamc@207 305 | ctuplev of con list
adamc@1 306 | ident of con
adamc@200 307 | idents of con list
adamc@1 308 | rcon of (con * con) list
adamc@83 309 | rconn of (con * con) list
adamc@1 310 | rcone of (con * con) list
adamc@239 311 | cargs of con * kind -> con * kind
adamc@239 312 | cargl of con * kind -> con * kind
adamc@240 313 | cargl2 of con * kind -> con * kind
adamc@239 314 | carg of con * kind -> con * kind
adamc@239 315 | cargp of con * kind -> con * kind
adamc@1 316
adamc@8 317 | eexp of exp
adamc@8 318 | eapps of exp
adamc@8 319 | eterm of exp
adamc@195 320 | etuple of exp list
adamc@12 321 | rexp of (con * exp) list
adamc@91 322 | xml of exp
adamc@91 323 | xmlOne of exp
adamc@1045 324 | xmlOpt of exp
adam@1412 325 | tag of (string * exp) * exp option * exp
adamc@141 326 | tagHead of string * exp
adamc@434 327 | bind of string * con option * exp
adamc@446 328 | edecl of edecl
adamc@446 329 | edecls of edecl list
adamc@8 330
adamc@241 331 | earg of exp * con -> exp * con
adamc@241 332 | eargp of exp * con -> exp * con
adamc@822 333 | earga of exp * con -> exp * con
adamc@241 334 | eargs of exp * con -> exp * con
adamc@241 335 | eargl of exp * con -> exp * con
adamc@242 336 | eargl2 of exp * con -> exp * con
adamc@241 337
adamc@170 338 | branch of pat * exp
adamc@170 339 | branchs of (pat * exp) list
adamc@170 340 | pat of pat
adamc@822 341 | patS of pat
adamc@170 342 | pterm of pat
adamc@174 343 | rpat of (string * pat) list * bool
adamc@195 344 | ptuple of pat list
adamc@170 345
adamc@721 346 | attrs of exp option * (con * exp) list
adamc@721 347 | attr of attr
adamc@104 348 | attrv of exp
adamc@104 349
adamc@204 350 | query of exp
adamc@226 351 | query1 of exp
adamc@993 352 | dopt of exp
adamc@748 353 | tables of con list * exp
adamc@749 354 | fitem of con list * exp
adamc@204 355 | tname of con
adamc@705 356 | tnameW of con * con
adamc@705 357 | tnames of (con * con) * (con * con) list
adamc@705 358 | tnames' of (con * con) * (con * con) list
adamc@204 359 | table of con * exp
adamc@748 360 | table' of con * exp
adamc@207 361 | tident of con
adamc@207 362 | fident of con
adamc@207 363 | seli of select_item
adamc@207 364 | selis of select_item list
adamc@207 365 | select of select
adamc@209 366 | sqlexp of exp
adamc@209 367 | wopt of exp
adamc@226 368 | groupi of group_item
adamc@226 369 | groupis of group_item list
adamc@226 370 | gopt of group_item list option
adamc@227 371 | hopt of exp
adamc@230 372 | obopt of exp
adamc@268 373 | obitem of exp * exp
adamc@230 374 | obexps of exp
adamc@268 375 | diropt of exp
adamc@231 376 | lopt of exp
adamc@232 377 | ofopt of exp
adamc@231 378 | sqlint of exp
adamc@236 379 | sqlagg of string
adamc@746 380 | fname of exp
adamc@207 381
adamc@302 382 | texp of exp
adamc@302 383 | fields of con list
adamc@302 384 | sqlexps of exp list
adamc@303 385 | fsets of (con * exp) list
adamc@310 386 | enterDml of unit
adamc@310 387 | leaveDml of unit
adamc@302 388
adamc@204 389
adamc@1 390 %verbose (* print summary of errors *)
adamc@1 391 %pos int (* positions *)
adamc@1 392 %start file
adamc@1 393 %pure
adamc@1 394 %eop EOF
adamc@1 395 %noshift EOF
adamc@1 396
adamc@244 397 %name Urweb
adamc@1 398
adamc@623 399 %right KARROW
adamc@623 400 %nonassoc DKARROW
adamc@243 401 %right SEMI
adamc@243 402 %nonassoc LARROW
adamc@195 403 %nonassoc IF THEN ELSE
adamc@843 404 %nonassoc DARROW
adamc@842 405 %left ANDALSO
adamc@842 406 %left ORELSE
adamc@1 407 %nonassoc COLON
adam@1306 408 %nonassoc DCOLON TCOLON DCOLONWILD TCOLONWILD
adam@1427 409 %left UNION INTERSECT EXCEPT ALL
adamc@1 410 %right COMMA
adamc@751 411 %right JOIN INNER CROSS OUTER LEFT RIGHT FULL
adamc@220 412 %right OR
adamc@220 413 %right CAND
adamc@470 414 %nonassoc EQ NE LT LE GT GE IS
adamc@243 415 %right ARROW
adamc@837 416 %right CARET PLUSPLUS
adamc@837 417 %left MINUSMINUS MINUSMINUSMINUS
adamc@389 418 %left PLUS MINUS
adamc@389 419 %left STAR DIVIDE MOD
adamc@220 420 %left NOT
adamc@84 421 %nonassoc TWIDDLE
adamc@1 422 %nonassoc DOLLAR
adamc@1 423 %left DOT
adamc@221 424 %nonassoc LBRACE RBRACE
adamc@1 425
adamc@1 426 %%
adamc@1 427
adamc@1 428 file : decls (decls)
adamc@54 429 | SIG sgis ([(DSgn ("?", (SgnConst sgis, s (SIGleft, sgisright))),
adamc@54 430 s (SIGleft, sgisright))])
adamc@1 431
adamc@1 432 decls : ([])
adamc@325 433 | decl decls (decl @ decls)
adamc@1 434
adamc@240 435 decl : CON SYMBOL cargl2 kopt EQ cexp (let
adamc@240 436 val loc = s (CONleft, cexpright)
adamc@240 437
adamc@240 438 val k = Option.getOpt (kopt, (KWild, loc))
adamc@240 439 val (c, k) = cargl2 (cexp, k)
adamc@240 440 in
adamc@325 441 [(DCon (SYMBOL, SOME k, c), loc)]
adamc@240 442 end)
adamc@325 443 | LTYPE SYMBOL EQ cexp ([(DCon (SYMBOL, SOME (KType, s (LTYPEleft, cexpright)), cexp),
adamc@325 444 s (LTYPEleft, cexpright))])
adamc@805 445 | DATATYPE dtypes ([(DDatatype dtypes, s (DATATYPEleft, dtypesright))])
adamc@191 446 | DATATYPE SYMBOL dargs EQ DATATYPE CSYMBOL DOT path
adamc@191 447 (case dargs of
adamc@325 448 [] => [(DDatatypeImp (SYMBOL, CSYMBOL :: #1 path, #2 path), s (DATATYPEleft, pathright))]
adamc@191 449 | _ => raise Fail "Arguments specified for imported datatype")
adamc@325 450 | VAL vali ([(DVal vali, s (VALleft, valiright))])
adamc@325 451 | VAL REC valis ([(DValRec valis, s (VALleft, valisright))])
adamc@325 452 | FUN valis ([(DValRec valis, s (FUNleft, valisright))])
adamc@1 453
adamc@325 454 | SIGNATURE CSYMBOL EQ sgn ([(DSgn (CSYMBOL, sgn), s (SIGNATUREleft, sgnright))])
adamc@325 455 | STRUCTURE CSYMBOL EQ str ([(DStr (CSYMBOL, NONE, str), s (STRUCTUREleft, strright))])
adamc@325 456 | STRUCTURE CSYMBOL COLON sgn EQ str ([(DStr (CSYMBOL, SOME sgn, str), s (STRUCTUREleft, strright))])
adamc@42 457 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN EQ str
adamc@325 458 ([(DStr (CSYMBOL1, NONE,
adamc@325 459 (StrFun (CSYMBOL2, sgn1, NONE, str), s (FUNCTORleft, strright))),
adamc@325 460 s (FUNCTORleft, strright))])
adamc@42 461 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN COLON sgn EQ str
adamc@325 462 ([(DStr (CSYMBOL1, NONE,
adamc@325 463 (StrFun (CSYMBOL2, sgn1, SOME sgn2, str), s (FUNCTORleft, strright))),
adamc@325 464 s (FUNCTORleft, strright))])
adamc@325 465 | EXTERN STRUCTURE CSYMBOL COLON sgn ([(DFfiStr (CSYMBOL, sgn), s (EXTERNleft, sgnright))])
adamc@61 466 | OPEN mpath (case mpath of
adamc@61 467 [] => raise Fail "Impossible mpath parse [1]"
adamc@325 468 | m :: ms => [(DOpen (m, ms), s (OPENleft, mpathright))])
adamc@325 469 | OPEN mpath LPAREN str RPAREN (let
adamc@325 470 val loc = s (OPENleft, RPARENright)
adamc@325 471
adamc@325 472 val m = case mpath of
adamc@325 473 [] => raise Fail "Impossible mpath parse [4]"
adamc@325 474 | m :: ms =>
adamc@325 475 foldl (fn (m, str) => (StrProj (str, m), loc))
adamc@325 476 (StrVar m, loc) ms
adamc@325 477 in
adamc@325 478 [(DStr ("anon", NONE, (StrApp (m, str), loc)), loc),
adamc@325 479 (DOpen ("anon", []), loc)]
adamc@325 480 end)
adamc@88 481 | OPEN CONSTRAINTS mpath (case mpath of
adamc@88 482 [] => raise Fail "Impossible mpath parse [3]"
adamc@325 483 | m :: ms => [(DOpenConstraints (m, ms), s (OPENleft, mpathright))])
adamc@325 484 | CONSTRAINT cterm TWIDDLE cterm ([(DConstraint (cterm1, cterm2), s (CONSTRAINTleft, ctermright))])
adamc@325 485 | EXPORT spath ([(DExport spath, s (EXPORTleft, spathright))])
adamc@707 486 | TABLE SYMBOL COLON cterm pkopt commaOpt cstopt([(DTable (SYMBOL, entable cterm, pkopt, cstopt),
adamc@707 487 s (TABLEleft, cstoptright))])
adamc@338 488 | SEQUENCE SYMBOL ([(DSequence SYMBOL, s (SEQUENCEleft, SYMBOLright))])
adamc@754 489 | VIEW SYMBOL EQ query ([(DView (SYMBOL, query),
adamc@754 490 s (VIEWleft, queryright))])
adamc@754 491 | VIEW SYMBOL EQ LBRACE eexp RBRACE ([(DView (SYMBOL, eexp),
adamc@754 492 s (VIEWleft, RBRACEright))])
adamc@563 493 | CLASS SYMBOL EQ cexp (let
adamc@563 494 val loc = s (CLASSleft, cexpright)
adamc@563 495 in
adamc@563 496 [(DClass (SYMBOL, (KWild, loc), cexp), loc)]
adamc@563 497 end)
adamc@563 498 | CLASS SYMBOL DCOLON kind EQ cexp ([(DClass (SYMBOL, kind, cexp), s (CLASSleft, cexpright))])
adamc@211 499 | CLASS SYMBOL SYMBOL EQ cexp (let
adamc@211 500 val loc = s (CLASSleft, cexpright)
adamc@563 501 val k = (KWild, loc)
adamc@211 502 val c = (CAbs (SYMBOL2, SOME k, cexp), loc)
adamc@211 503 in
adamc@563 504 [(DClass (SYMBOL1, k, c), s (CLASSleft, cexpright))]
adamc@563 505 end)
adamc@563 506 | CLASS SYMBOL LPAREN SYMBOL DCOLON kind RPAREN EQ cexp (let
adamc@563 507 val loc = s (CLASSleft, cexpright)
adamc@563 508 val c = (CAbs (SYMBOL2, SOME kind, cexp), loc)
adamc@563 509 in
adamc@563 510 [(DClass (SYMBOL1, kind, c), s (CLASSleft, cexpright))]
adamc@211 511 end)
adamc@459 512 | COOKIE SYMBOL COLON cexp ([(DCookie (SYMBOL, cexp), s (COOKIEleft, cexpright))])
adamc@720 513 | STYLE SYMBOL ([(DStyle SYMBOL, s (STYLEleft, SYMBOLright))])
adamc@1075 514 | TASK eapps EQ eexp ([(DTask (eapps, eexp), s (TASKleft, eexpright))])
adamc@1199 515 | POLICY eexp ([(DPolicy eexp, s (POLICYleft, eexpright))])
adamc@30 516
adamc@805 517 dtype : SYMBOL dargs EQ barOpt dcons (SYMBOL, dargs, dcons)
adamc@805 518
adamc@805 519 dtypes : dtype ([dtype])
adamc@805 520 | dtype AND dtypes (dtype :: dtypes)
adamc@805 521
adamc@240 522 kopt : (NONE)
adamc@240 523 | DCOLON kind (SOME kind)
adam@1302 524 | DCOLONWILD (SOME (KWild, s (DCOLONWILDleft, DCOLONWILDright)))
adamc@240 525
adamc@191 526 dargs : ([])
adamc@191 527 | SYMBOL dargs (SYMBOL :: dargs)
adamc@191 528
adamc@156 529 barOpt : ()
adamc@156 530 | BAR ()
adamc@156 531
adamc@156 532 dcons : dcon ([dcon])
adamc@156 533 | dcon BAR dcons (dcon :: dcons)
adamc@156 534
adamc@156 535 dcon : CSYMBOL (CSYMBOL, NONE)
adamc@156 536 | CSYMBOL OF cexp (CSYMBOL, SOME cexp)
adamc@156 537
adamc@242 538 vali : SYMBOL eargl2 copt EQ eexp (let
adamc@242 539 val loc = s (SYMBOLleft, eexpright)
adamc@242 540 val t = Option.getOpt (copt, (CWild (KType, loc), loc))
adamc@242 541
adamc@242 542 val (e, t) = eargl2 (eexp, t)
adamc@242 543 in
adamc@242 544 (SYMBOL, SOME t, e)
adamc@242 545 end)
adamc@242 546
adamc@242 547 copt : (NONE)
adamc@242 548 | COLON cexp (SOME cexp)
adamc@123 549
adamc@704 550 cstopt : (EVar (["Basis"], "no_constraint", Infer), dummy)
adamc@704 551 | csts (csts)
adamc@704 552
adamc@704 553 csts : CCONSTRAINT tname cst (let
adamc@704 554 val loc = s (CCONSTRAINTleft, cstright)
adamc@704 555
adamc@704 556 val e = (EVar (["Basis"], "one_constraint", Infer), loc)
adamc@704 557 val e = (ECApp (e, tname), loc)
adamc@704 558 in
adamc@704 559 (EApp (e, cst), loc)
adamc@704 560 end)
adamc@704 561 | csts COMMA csts (let
adamc@704 562 val loc = s (csts1left, csts2right)
adamc@704 563
adamc@704 564 val e = (EVar (["Basis"], "join_constraints", Infer), loc)
adamc@704 565 val e = (EApp (e, csts1), loc)
adamc@704 566 in
adamc@704 567 (EApp (e, csts2), loc)
adamc@704 568 end)
adamc@704 569 | LBRACE LBRACE eexp RBRACE RBRACE (eexp)
adamc@704 570
adamc@704 571 cst : UNIQUE tnames (let
adamc@704 572 val loc = s (UNIQUEleft, tnamesright)
adamc@704 573
adamc@704 574 val e = (EVar (["Basis"], "unique", Infer), loc)
adamc@705 575 val e = (ECApp (e, #1 (#1 tnames)), loc)
adamc@705 576 val e = (ECApp (e, (CRecord (#2 tnames), loc)), loc)
adamc@704 577 in
adamc@1093 578 e
adamc@704 579 end)
adamc@709 580
adamc@714 581 | CHECK sqlexp (let
adamc@714 582 val loc = s (CHECKleft, sqlexpright)
adamc@714 583 in
adamc@714 584 (EApp ((EVar (["Basis"], "check", Infer), loc),
adamc@714 585 sqlexp), loc)
adamc@714 586 end)
adamc@714 587
adamc@709 588 | FOREIGN KEY tnames REFERENCES texp LPAREN tnames' RPAREN pmodes
adamc@709 589 (let
adamc@709 590 val loc = s (FOREIGNleft, pmodesright)
adamc@709 591
adamc@709 592 val mat = ListPair.foldrEq
adamc@709 593 (fn ((nm1, _), (nm2, _), mat) =>
adamc@709 594 let
adamc@709 595 val e = (EVar (["Basis"], "mat_cons", Infer), loc)
adamc@709 596 val e = (ECApp (e, nm1), loc)
adamc@709 597 val e = (ECApp (e, nm2), loc)
adamc@709 598 in
adamc@709 599 (EApp (e, mat), loc)
adamc@709 600 end)
adamc@709 601 (EVar (["Basis"], "mat_nil", Infer), loc)
adamc@709 602 (#1 tnames :: #2 tnames, #1 tnames' :: #2 tnames')
adam@1401 603 handle ListPair.UnequalLengths =>
adam@1401 604 (ErrorMsg.errorAt loc ("Unequal foreign key list lengths ("
adam@1401 605 ^ Int.toString (1 + length (#2 tnames))
adam@1401 606 ^ " vs. "
adam@1401 607 ^ Int.toString (1 + length (#2 tnames'))
adam@1401 608 ^ ")");
adam@1401 609 (EVar (["Basis"], "mat_nil", Infer), loc))
adamc@709 610
adamc@709 611 fun findMode mode =
adamc@709 612 let
adamc@709 613 fun findMode' pmodes =
adamc@709 614 case pmodes of
adamc@709 615 [] => (EVar (["Basis"], "no_action", Infer), loc)
adamc@709 616 | (mode', rule) :: pmodes' =>
adamc@709 617 if mode' = mode then
adamc@709 618 (if List.exists (fn (mode', _) => mode' = mode)
adamc@709 619 pmodes' then
adamc@709 620 ErrorMsg.errorAt loc "Duplicate propagation rule"
adamc@709 621 else
adamc@709 622 ();
adamc@709 623 rule)
adamc@709 624 else
adamc@709 625 findMode' pmodes'
adamc@709 626 in
adamc@709 627 findMode' pmodes
adamc@709 628 end
adamc@709 629
adamc@709 630 val e = (EVar (["Basis"], "foreign_key", Infer), loc)
adamc@709 631 val e = (EApp (e, mat), loc)
adamc@709 632 val e = (EApp (e, texp), loc)
adamc@709 633 in
adamc@709 634 (EApp (e, (ERecord [((CName "OnDelete", loc),
adamc@709 635 findMode Delete),
adamc@709 636 ((CName "OnUpdate", loc),
adamc@709 637 findMode Update)], loc)), loc)
adamc@709 638 end)
adamc@709 639
adamc@704 640 | LBRACE eexp RBRACE (eexp)
adamc@704 641
adamc@704 642 tnameW : tname (let
adamc@704 643 val loc = s (tnameleft, tnameright)
adamc@704 644 in
adamc@704 645 (tname, (CWild (KType, loc), loc))
adamc@704 646 end)
adamc@704 647
adamc@705 648 tnames : tnameW (tnameW, [])
adamc@705 649 | LPAREN tnames' RPAREN (tnames')
adamc@704 650
adamc@705 651 tnames': tnameW (tnameW, [])
adamc@705 652 | tnameW COMMA tnames' (#1 tnames', tnameW :: #2 tnames')
adamc@704 653
adamc@709 654 pmode : ON pkind prule (pkind, prule)
adamc@709 655
adamc@709 656 pkind : DELETE (Delete)
adamc@709 657 | UPDATE (Update)
adamc@709 658
adamc@709 659 prule : NO ACTION (EVar (["Basis"], "no_action", Infer), s (NOleft, ACTIONright))
adamc@709 660 | RESTRICT (EVar (["Basis"], "restrict", Infer), s (RESTRICTleft, RESTRICTright))
adamc@709 661 | CASCADE (EVar (["Basis"], "cascade", Infer), s (CASCADEleft, CASCADEright))
adamc@709 662 | SET NULL (EVar (["Basis"], "set_null", Infer), s (SETleft, NULLright))
adamc@709 663
adamc@709 664 pmodes : ([])
adamc@709 665 | pmode pmodes (pmode :: pmodes)
adamc@709 666
adamc@707 667 commaOpt: ()
adamc@707 668 | COMMA ()
adamc@707 669
adamc@993 670 pkopt : (EVar (["Basis"], "no_primary_key", Infer), dummy)
adamc@707 671 | PRIMARY KEY tnames (let
adamc@707 672 val loc = s (PRIMARYleft, tnamesright)
adamc@707 673
adamc@1093 674 val e = (EVar (["Basis"], "primary_key", TypesOnly), loc)
adamc@707 675 val e = (ECApp (e, #1 (#1 tnames)), loc)
adamc@707 676 val e = (ECApp (e, (CRecord (#2 tnames), loc)), loc)
adamc@707 677 val e = (EDisjointApp e, loc)
adamc@707 678 val e = (EDisjointApp e, loc)
adamc@707 679
adamc@707 680 val witness = map (fn (c, _) =>
adamc@707 681 (c, (EWild, loc)))
adamc@707 682 (#1 tnames :: #2 tnames)
adamc@707 683 val witness = (ERecord witness, loc)
adamc@707 684 in
adamc@707 685 (EApp (e, witness), loc)
adamc@707 686 end)
adamc@707 687
adamc@123 688 valis : vali ([vali])
adamc@123 689 | vali AND valis (vali :: valis)
adamc@123 690
adamc@42 691 sgn : sgntm (sgntm)
adamc@40 692 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN COLON sgn
adamc@40 693 (SgnFun (CSYMBOL, sgn1, sgn2), s (FUNCTORleft, sgn2right))
adamc@30 694
adamc@42 695 sgntm : SIG sgis END (SgnConst sgis, s (SIGleft, ENDright))
adamc@59 696 | mpath (case mpath of
adamc@61 697 [] => raise Fail "Impossible mpath parse [2]"
adamc@59 698 | [x] => SgnVar x
adamc@59 699 | m :: ms => SgnProj (m,
adamc@59 700 List.take (ms, length ms - 1),
adamc@59 701 List.nth (ms, length ms - 1)),
adamc@59 702 s (mpathleft, mpathright))
adamc@42 703 | sgntm WHERE CON SYMBOL EQ cexp (SgnWhere (sgntm, SYMBOL, cexp), s (sgntmleft, cexpright))
adamc@42 704 | sgntm WHERE LTYPE SYMBOL EQ cexp(SgnWhere (sgntm, SYMBOL, cexp), s (sgntmleft, cexpright))
adamc@42 705 | LPAREN sgn RPAREN (sgn)
adamc@42 706
adam@1577 707 cexpO : (NONE)
adam@1577 708 | EQ cexp (SOME cexp)
adam@1577 709
adam@1577 710 sgi : LTYPE SYMBOL ((SgiConAbs (SYMBOL, (KType, s (LTYPEleft, SYMBOLright))),
adamc@706 711 s (LTYPEleft, SYMBOLright)))
adam@1577 712 | CON SYMBOL cargl2 kopt cexpO (let
adam@1577 713 val loc = s (CONleft, cexpOright)
adam@1574 714
adam@1574 715 val k = Option.getOpt (kopt, (KWild, loc))
adam@1574 716 in
adam@1577 717 case cexpO of
adam@1577 718 NONE => (SgiConAbs (SYMBOL, k), loc)
adam@1577 719 | SOME cexp =>
adam@1577 720 let
adam@1577 721 val (c, k) = cargl2 (cexp, k)
adam@1577 722 in
adam@1577 723 (SgiCon (SYMBOL, SOME k, c), loc)
adam@1577 724 end
adam@1574 725 end)
adamc@706 726 | LTYPE SYMBOL EQ cexp ((SgiCon (SYMBOL, SOME (KType, s (LTYPEleft, cexpright)), cexp),
adamc@706 727 s (LTYPEleft, cexpright)))
adamc@805 728 | DATATYPE dtypes ((SgiDatatype dtypes, s (DATATYPEleft, dtypesright)))
adamc@191 729 | DATATYPE SYMBOL dargs EQ DATATYPE CSYMBOL DOT path
adamc@191 730 (case dargs of
adamc@191 731 [] => (SgiDatatypeImp (SYMBOL, CSYMBOL :: #1 path, #2 path), s (DATATYPEleft, pathright))
adamc@191 732 | _ => raise Fail "Arguments specified for imported datatype")
adamc@706 733 | VAL SYMBOL COLON cexp ((SgiVal (SYMBOL, cexp), s (VALleft, cexpright)))
adamc@30 734
adamc@706 735 | STRUCTURE CSYMBOL COLON sgn ((SgiStr (CSYMBOL, sgn), s (STRUCTUREleft, sgnright)))
adamc@706 736 | SIGNATURE CSYMBOL EQ sgn ((SgiSgn (CSYMBOL, sgn), s (SIGNATUREleft, sgnright)))
adamc@42 737 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN COLON sgn
adamc@706 738 ((SgiStr (CSYMBOL1,
adamc@706 739 (SgnFun (CSYMBOL2, sgn1, sgn2), s (FUNCTORleft, sgn2right))),
adamc@706 740 s (FUNCTORleft, sgn2right)))
adamc@706 741 | INCLUDE sgn ((SgiInclude sgn, s (INCLUDEleft, sgnright)))
adamc@706 742 | CONSTRAINT cterm TWIDDLE cterm ((SgiConstraint (cterm1, cterm2), s (CONSTRAINTleft, ctermright)))
adamc@707 743 | TABLE SYMBOL COLON cterm pkopt commaOpt cstopt (let
adamc@707 744 val loc = s (TABLEleft, ctermright)
adamc@707 745 in
adamc@707 746 (SgiTable (SYMBOL, entable cterm, pkopt, cstopt), loc)
adamc@707 747 end)
adamc@460 748 | SEQUENCE SYMBOL (let
adamc@460 749 val loc = s (SEQUENCEleft, SYMBOLright)
adamc@460 750 val t = (CVar (["Basis"], "sql_sequence"), loc)
adamc@460 751 in
adamc@460 752 (SgiVal (SYMBOL, t), loc)
adamc@460 753 end)
adamc@754 754 | VIEW SYMBOL COLON cexp (let
adamc@754 755 val loc = s (VIEWleft, cexpright)
adamc@754 756 val t = (CVar (["Basis"], "sql_view"), loc)
adamc@1076 757 val t = (CApp (t, entable cexp), loc)
adamc@754 758 in
adamc@754 759 (SgiVal (SYMBOL, t), loc)
adamc@754 760 end)
adamc@563 761 | CLASS SYMBOL (let
adamc@563 762 val loc = s (CLASSleft, SYMBOLright)
adamc@711 763 val k = (KArrow ((KType, loc), (KType, loc)), loc)
adamc@563 764 in
adamc@711 765 (SgiClassAbs (SYMBOL, k), loc)
adamc@563 766 end)
adamc@563 767 | CLASS SYMBOL DCOLON kind (let
adamc@563 768 val loc = s (CLASSleft, kindright)
adamc@563 769 in
adamc@563 770 (SgiClassAbs (SYMBOL, kind), loc)
adamc@563 771 end)
adamc@563 772 | CLASS SYMBOL EQ cexp (let
adamc@563 773 val loc = s (CLASSleft, cexpright)
adamc@563 774 in
adamc@563 775 (SgiClass (SYMBOL, (KWild, loc), cexp), loc)
adamc@563 776 end)
adamc@563 777 | CLASS SYMBOL DCOLON kind EQ cexp (let
adamc@563 778 val loc = s (CLASSleft, cexpright)
adamc@563 779 in
adamc@563 780 (SgiClass (SYMBOL, kind, cexp), loc)
adamc@563 781 end)
adamc@211 782 | CLASS SYMBOL SYMBOL EQ cexp (let
adamc@211 783 val loc = s (CLASSleft, cexpright)
adamc@563 784 val k = (KWild, loc)
adamc@211 785 val c = (CAbs (SYMBOL2, SOME k, cexp), loc)
adamc@211 786 in
adamc@563 787 (SgiClass (SYMBOL1, k, c), s (CLASSleft, cexpright))
adamc@563 788 end)
adamc@563 789 | CLASS SYMBOL LPAREN SYMBOL DCOLON kind RPAREN EQ cexp (let
adamc@563 790 val loc = s (CLASSleft, cexpright)
adamc@563 791 val c = (CAbs (SYMBOL2, SOME kind, cexp), loc)
adamc@563 792 in
adamc@563 793 (SgiClass (SYMBOL1, kind, c), s (CLASSleft, cexpright))
adamc@211 794 end)
adamc@460 795 | COOKIE SYMBOL COLON cexp (let
adamc@460 796 val loc = s (COOKIEleft, cexpright)
adamc@460 797 val t = (CApp ((CVar (["Basis"], "http_cookie"), loc),
adamc@460 798 entable cexp), loc)
adamc@460 799 in
adamc@460 800 (SgiVal (SYMBOL, t), loc)
adamc@460 801 end)
adamc@720 802 | STYLE SYMBOL (let
adamc@720 803 val loc = s (STYLEleft, SYMBOLright)
adamc@720 804 val t = (CVar (["Basis"], "css_class"), loc)
adamc@718 805 in
adamc@718 806 (SgiVal (SYMBOL, t), loc)
adamc@718 807 end)
adamc@30 808
adamc@30 809 sgis : ([])
adamc@30 810 | sgi sgis (sgi :: sgis)
adamc@30 811
adamc@30 812 str : STRUCT decls END (StrConst decls, s (STRUCTleft, ENDright))
adamc@34 813 | spath (spath)
adamc@40 814 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN DARROW str
adamc@40 815 (StrFun (CSYMBOL, sgn, NONE, str), s (FUNCTORleft, strright))
adamc@40 816 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN COLON sgn DARROW str
adamc@40 817 (StrFun (CSYMBOL, sgn1, SOME sgn2, str), s (FUNCTORleft, strright))
adamc@44 818 | spath LPAREN str RPAREN (StrApp (spath, str), s (spathleft, RPARENright))
adamc@34 819
adamc@34 820 spath : CSYMBOL (StrVar CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@34 821 | spath DOT CSYMBOL (StrProj (spath, CSYMBOL), s (spathleft, CSYMBOLright))
adamc@30 822
adamc@1 823 kind : TYPE (KType, s (TYPEleft, TYPEright))
adamc@1 824 | NAME (KName, s (NAMEleft, NAMEright))
adamc@1 825 | LBRACE kind RBRACE (KRecord kind, s (LBRACEleft, RBRACEright))
adamc@1 826 | kind ARROW kind (KArrow (kind1, kind2), s (kind1left, kind2right))
adamc@1 827 | LPAREN kind RPAREN (#1 kind, s (LPARENleft, RPARENright))
adamc@82 828 | KUNIT (KUnit, s (KUNITleft, KUNITright))
adamc@18 829 | UNDERUNDER (KWild, s (UNDERUNDERleft, UNDERUNDERright))
adamc@207 830 | LPAREN ktuple RPAREN (KTuple ktuple, s (LPARENleft, RPARENright))
adamc@623 831 | CSYMBOL (KVar CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@623 832 | CSYMBOL KARROW kind (KFun (CSYMBOL, kind), s (CSYMBOLleft, kindright))
adamc@207 833
adamc@207 834 ktuple : kind STAR kind ([kind1, kind2])
adamc@207 835 | kind STAR ktuple (kind :: ktuple)
adamc@1 836
adamc@1 837 capps : cterm (cterm)
adamc@1 838 | capps cterm (CApp (capps, cterm), s (cappsleft, ctermright))
adamc@1 839
adamc@1 840 cexp : capps (capps)
adamc@1 841 | cexp ARROW cexp (TFun (cexp1, cexp2), s (cexp1left, cexp2right))
adamc@15 842 | SYMBOL kcolon kind ARROW cexp (TCFun (kcolon, SYMBOL, kind, cexp), s (SYMBOLleft, cexpright))
adamc@623 843 | CSYMBOL KARROW cexp (TKFun (CSYMBOL, cexp), s (CSYMBOLleft, cexpright))
adamc@1 844
adamc@1 845 | cexp PLUSPLUS cexp (CConcat (cexp1, cexp2), s (cexp1left, cexp1right))
adamc@1 846
adamc@239 847 | FN cargs DARROW cexp (#1 (cargs (cexp, (KWild, s (FNleft, cexpright)))))
adamc@628 848 | LBRACK cexp TWIDDLE cexp RBRACK DARROW cexp (TDisjoint (cexp1, cexp2, cexp3), s (LBRACKleft, cexp3right))
adamc@623 849 | CSYMBOL DKARROW cexp (CKAbs (CSYMBOL, cexp), s (CSYMBOLleft, cexpright))
adamc@1 850
adamc@8 851 | LPAREN cexp RPAREN DCOLON kind (CAnnot (cexp, kind), s (LPARENleft, kindright))
adamc@6 852
adamc@18 853 | UNDER DCOLON kind (CWild kind, s (UNDERleft, UNDERright))
adamc@195 854 | ctuple (let
adamc@195 855 val loc = s (ctupleleft, ctupleright)
adamc@195 856 in
adamc@195 857 (TRecord (CRecord (ListUtil.mapi (fn (i, c) =>
adamc@195 858 ((CName (Int.toString (i + 1)), loc),
adamc@195 859 c)) ctuple),
adamc@195 860 loc), loc)
adamc@195 861 end)
adamc@18 862
adamc@1 863 kcolon : DCOLON (Explicit)
adamc@1 864 | TCOLON (Implicit)
adamc@1 865
adamc@239 866 cargs : carg (carg)
adamc@239 867 | cargl (cargl)
adamc@239 868
adamc@239 869 cargl : cargp cargp (cargp1 o cargp2)
adamc@239 870 | cargp cargl (cargp o cargl)
adamc@239 871
adamc@240 872 cargl2 : (fn x => x)
adamc@240 873 | cargp cargl2 (cargp o cargl2)
adamc@240 874
adamc@241 875 carg : SYMBOL DCOLON kind (fn (c, k) =>
adamc@239 876 let
adamc@239 877 val loc = s (SYMBOLleft, kindright)
adamc@239 878 in
adamc@239 879 ((CAbs (SYMBOL, SOME kind, c), loc),
adamc@239 880 (KArrow (kind, k), loc))
adamc@239 881 end)
adamc@329 882 | UNDER DCOLON kind (fn (c, k) =>
adamc@329 883 let
adamc@329 884 val loc = s (UNDERleft, kindright)
adamc@329 885 in
adamc@329 886 ((CAbs ("_", SOME kind, c), loc),
adamc@329 887 (KArrow (kind, k), loc))
adamc@329 888 end)
adam@1302 889 | SYMBOL DCOLONWILD (fn (c, k) =>
adam@1302 890 let
adam@1302 891 val loc = s (SYMBOLleft, DCOLONWILDright)
adam@1302 892 val kind = (KWild, loc)
adam@1302 893 in
adam@1302 894 ((CAbs (SYMBOL, NONE, c), loc),
adam@1302 895 (KArrow (kind, k), loc))
adam@1302 896 end)
adam@1302 897 | UNDER DCOLONWILD (fn (c, k) =>
adam@1302 898 let
adam@1302 899 val loc = s (UNDERleft, DCOLONWILDright)
adam@1302 900 val kind = (KWild, loc)
adam@1302 901 in
adam@1302 902 ((CAbs ("_", NONE, c), loc),
adam@1302 903 (KArrow (kind, k), loc))
adam@1302 904 end)
adamc@241 905 | cargp (cargp)
adamc@239 906
adamc@239 907 cargp : SYMBOL (fn (c, k) =>
adamc@239 908 let
adamc@239 909 val loc = s (SYMBOLleft, SYMBOLright)
adamc@239 910 in
adamc@239 911 ((CAbs (SYMBOL, NONE, c), loc),
adamc@239 912 (KArrow ((KWild, loc), k), loc))
adamc@239 913 end)
adamc@329 914 | UNDER (fn (c, k) =>
adamc@329 915 let
adamc@329 916 val loc = s (UNDERleft, UNDERright)
adamc@329 917 in
adamc@329 918 ((CAbs ("_", NONE, c), loc),
adamc@329 919 (KArrow ((KWild, loc), k), loc))
adamc@329 920 end)
adamc@1001 921 | LPAREN SYMBOL kopt ckl RPAREN (fn (c, k) =>
adamc@239 922 let
adamc@239 923 val loc = s (LPARENleft, RPARENright)
adamc@1001 924 val ckl = (SYMBOL, kopt) :: ckl
adamc@1001 925 val ckl = map (fn (x, ko) => (x, case ko of
adamc@1001 926 NONE => (KWild, loc)
adamc@1001 927 | SOME k => k)) ckl
adamc@239 928 in
adamc@1001 929 case ckl of
adamc@1001 930 [(x, k')] => ((CAbs (SYMBOL, SOME k', c), loc),
adamc@1001 931 (KArrow (k', k), loc))
adamc@1001 932 | _ =>
adamc@1001 933 let
adamc@1001 934 val k' = (KTuple (map #2 ckl), loc)
adamc@1001 935
adamc@1001 936 val c = foldr (fn ((x, k), c) =>
adamc@1001 937 (CAbs (x, SOME k, c), loc)) c ckl
adamc@1001 938 val v = (CVar ([], "$x"), loc)
adamc@1001 939 val c = ListUtil.foldli (fn (i, _, c) =>
adamc@1001 940 (CApp (c, (CProj (v, i + 1), loc)),
adamc@1001 941 loc)) c ckl
adamc@1001 942 in
adamc@1001 943 ((CAbs ("$x", SOME k', c), loc),
adamc@1001 944 (KArrow (k', k), loc))
adamc@1001 945 end
adamc@239 946 end)
adamc@628 947
adamc@1001 948 ckl : ([])
adamc@1001 949 | COMMA SYMBOL kopt ckl ((SYMBOL, kopt) :: ckl)
adamc@239 950
adamc@34 951 path : SYMBOL ([], SYMBOL)
adamc@34 952 | CSYMBOL DOT path (let val (ms, x) = path in (CSYMBOL :: ms, x) end)
adamc@34 953
adamc@156 954 cpath : CSYMBOL ([], CSYMBOL)
adamc@156 955 | CSYMBOL DOT cpath (let val (ms, x) = cpath in (CSYMBOL :: ms, x) end)
adamc@156 956
adamc@59 957 mpath : CSYMBOL ([CSYMBOL])
adamc@59 958 | CSYMBOL DOT mpath (CSYMBOL :: mpath)
adamc@59 959
adamc@1 960 cterm : LPAREN cexp RPAREN (#1 cexp, s (LPARENleft, RPARENright))
adamc@1 961 | LBRACK rcon RBRACK (CRecord rcon, s (LBRACKleft, RBRACKright))
adamc@83 962 | LBRACK rconn RBRACK (CRecord rconn, s (LBRACKleft, RBRACKright))
adamc@1 963 | LBRACE rcone RBRACE (TRecord (CRecord rcone, s (LBRACEleft, RBRACEright)),
adamc@1 964 s (LBRACEleft, RBRACEright))
adamc@1 965 | DOLLAR cterm (TRecord cterm, s (DOLLARleft, ctermright))
adamc@1 966 | HASH CSYMBOL (CName CSYMBOL, s (HASHleft, CSYMBOLright))
adamc@195 967 | HASH INT (CName (Int64.toString INT), s (HASHleft, INTright))
adamc@1 968
adamc@34 969 | path (CVar path, s (pathleft, pathright))
adamc@207 970 | path DOT INT (CProj ((CVar path, s (pathleft, pathright)), Int64.toInt INT),
adamc@207 971 s (pathleft, INTright))
adamc@18 972 | UNDER (CWild (KWild, s (UNDERleft, UNDERright)), s (UNDERleft, UNDERright))
adamc@621 973 | MAP (CMap, s (MAPleft, MAPright))
adamc@82 974 | UNIT (CUnit, s (UNITleft, UNITright))
adamc@207 975 | LPAREN ctuplev RPAREN (CTuple ctuplev, s (LPARENleft, RPARENright))
adamc@207 976
adamc@207 977 ctuplev: cexp COMMA cexp ([cexp1, cexp2])
adamc@207 978 | cexp COMMA ctuplev (cexp :: ctuplev)
adamc@1 979
adamc@196 980 ctuple : capps STAR capps ([capps1, capps2])
adamc@196 981 | capps STAR ctuple (capps :: ctuple)
adamc@195 982
adamc@1 983 rcon : ([])
adamc@1 984 | ident EQ cexp ([(ident, cexp)])
adamc@1 985 | ident EQ cexp COMMA rcon ((ident, cexp) :: rcon)
adamc@1 986
adamc@83 987 rconn : ident ([(ident, (CUnit, s (identleft, identright)))])
adamc@83 988 | ident COMMA rconn ((ident, (CUnit, s (identleft, identright))) :: rconn)
adamc@83 989
adamc@1 990 rcone : ([])
adamc@1 991 | ident COLON cexp ([(ident, cexp)])
adamc@1 992 | ident COLON cexp COMMA rcone ((ident, cexp) :: rcone)
adamc@1 993
adamc@1 994 ident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@195 995 | INT (CName (Int64.toString INT), s (INTleft, INTright))
adamc@200 996 | SYMBOL (CVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright))
adamc@8 997
adamc@8 998 eapps : eterm (eterm)
adamc@8 999 | eapps eterm (EApp (eapps, eterm), s (eappsleft, etermright))
adamc@8 1000 | eapps LBRACK cexp RBRACK (ECApp (eapps, cexp), s (eappsleft, RBRACKright))
adamc@629 1001 | eapps BANG (EDisjointApp eapps, s (eappsleft, BANGright))
adamc@8 1002
adamc@8 1003 eexp : eapps (eapps)
adamc@241 1004 | FN eargs DARROW eexp (let
adamc@93 1005 val loc = s (FNleft, eexpright)
adamc@93 1006 in
adamc@241 1007 #1 (eargs (eexp, (CWild (KType, loc), loc)))
adamc@93 1008 end)
adamc@623 1009 | CSYMBOL DKARROW eexp (EKAbs (CSYMBOL, eexp), s (CSYMBOLleft, eexpright))
adamc@196 1010 | eexp COLON cexp (EAnnot (eexp, cexp), s (eexpleft, cexpright))
adamc@149 1011 | eexp MINUSMINUS cexp (ECut (eexp, cexp), s (eexpleft, cexpright))
adamc@493 1012 | eexp MINUSMINUSMINUS cexp (ECutMulti (eexp, cexp), s (eexpleft, cexpright))
adamc@170 1013 | CASE eexp OF barOpt branch branchs (ECase (eexp, branch :: branchs), s (CASEleft, branchsright))
adamc@190 1014 | IF eexp THEN eexp ELSE eexp (let
adamc@190 1015 val loc = s (IFleft, eexp3right)
adamc@190 1016 in
adamc@190 1017 (ECase (eexp1, [((PCon (["Basis"], "True", NONE), loc), eexp2),
adamc@190 1018 ((PCon (["Basis"], "False", NONE), loc), eexp3)]), loc)
adamc@190 1019 end)
adamc@434 1020 | bind SEMI eexp (let
adamc@434 1021 val loc = s (bindleft, eexpright)
adamc@434 1022 val (v, to, e1) = bind
adamc@403 1023 val e = (EVar (["Basis"], "bind", Infer), loc)
adamc@434 1024 val e = (EApp (e, e1), loc)
adamc@243 1025 in
adamc@434 1026 (EApp (e, (EAbs (v, to, eexp), loc)), loc)
adamc@299 1027 end)
adamc@256 1028 | eexp EQ eexp (native_op ("eq", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@257 1029 | eexp NE eexp (native_op ("ne", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@389 1030 | MINUS eterm (native_unop ("neg", eterm, s (MINUSleft, etermright)))
adamc@389 1031 | eexp PLUS eexp (native_op ("plus", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@389 1032 | eexp MINUS eexp (native_op ("minus", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@1116 1033 | eapps STAR eexp (native_op ("times", eapps, eexp, s (eappsleft, eexpright)))
adamc@964 1034 | eexp DIVIDE eexp (native_op ("divide", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@389 1035 | eexp MOD eexp (native_op ("mod", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@391 1036
adamc@391 1037 | eexp LT eexp (native_op ("lt", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@391 1038 | eexp LE eexp (native_op ("le", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@391 1039 | eexp GT eexp (native_op ("gt", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@391 1040 | eexp GE eexp (native_op ("ge", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@391 1041
adamc@842 1042 | eexp ANDALSO eexp (let
adamc@842 1043 val loc = s (eexp1left, eexp2right)
adamc@842 1044 in
adamc@842 1045 (ECase (eexp1, [((PCon (["Basis"], "True", NONE), loc),
adamc@842 1046 eexp2),
adamc@842 1047 ((PCon (["Basis"], "False", NONE), loc),
adamc@842 1048 (EVar (["Basis"], "False", Infer), loc))]), loc)
adamc@842 1049 end)
adamc@842 1050 | eexp ORELSE eexp (let
adamc@842 1051 val loc = s (eexp1left, eexp2right)
adamc@842 1052 in
adamc@842 1053 (ECase (eexp1, [((PCon (["Basis"], "True", NONE), loc),
adamc@842 1054 (EVar (["Basis"], "True", Infer), loc)),
adamc@842 1055 ((PCon (["Basis"], "False", NONE), loc),
adamc@842 1056 eexp2)]), loc)
adamc@842 1057 end)
adamc@842 1058
adamc@445 1059 | eexp PLUSPLUS eexp (EConcat (eexp1, eexp2), s (eexp1left, eexp2right))
adamc@8 1060
adamc@674 1061 | eexp CARET eexp (native_op ("strcat", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@674 1062
adamc@794 1063 | eapps DCOLON eexp (let
adamc@794 1064 val loc = s (eappsleft, eexpright)
adamc@762 1065 in
adamc@762 1066 (EApp ((EVar (["Basis"], "Cons", Infer), loc),
adamc@762 1067 (ERecord [((CName "1", loc),
adamc@794 1068 eapps),
adamc@762 1069 ((CName "2", loc),
adamc@762 1070 eexp)], loc)), loc)
adamc@762 1071 end)
adamc@762 1072
adamc@434 1073 bind : SYMBOL LARROW eapps (SYMBOL, NONE, eapps)
adamc@434 1074 | eapps (let
adamc@434 1075 val loc = s (eappsleft, eappsright)
adamc@434 1076 in
adamc@434 1077 ("_", SOME (TRecord (CRecord [], loc), loc), eapps)
adamc@434 1078 end)
adamc@434 1079
adamc@241 1080 eargs : earg (earg)
adamc@241 1081 | eargl (eargl)
adamc@241 1082
adamc@241 1083 eargl : eargp eargp (eargp1 o eargp2)
adamc@241 1084 | eargp eargl (eargp o eargl)
adamc@241 1085
adamc@242 1086 eargl2 : (fn x => x)
adamc@242 1087 | eargp eargl2 (eargp o eargl2)
adamc@242 1088
adamc@822 1089 earg : patS (fn (e, t) =>
adamc@241 1090 let
adamc@822 1091 val loc = s (patSleft, patSright)
adamc@822 1092 val pt = patType loc patS
adamc@822 1093
adamc@822 1094 val e' = case #1 patS of
adamc@822 1095 PVar x => (EAbs (x, NONE, e), loc)
adamc@823 1096 | PAnnot ((PVar x, _), t) => (EAbs (x, SOME t, e), loc)
adamc@822 1097 | _ => (EAbs ("$x", SOME pt,
adamc@822 1098 (ECase ((EVar ([], "$x", DontInfer),
adamc@822 1099 loc),
adamc@822 1100 [(patS, e)]), loc)), loc)
adamc@241 1101 in
adamc@822 1102 (e', (TFun (pt, t), loc))
adamc@241 1103 end)
adamc@822 1104 | earga (earga)
adamc@822 1105
adamc@822 1106 eargp : pterm (fn (e, t) =>
adamc@241 1107 let
adamc@822 1108 val loc = s (ptermleft, ptermright)
adamc@822 1109 val pt = patType loc pterm
adamc@822 1110
adamc@822 1111 val e' = case #1 pterm of
adamc@822 1112 PVar x => (EAbs (x, NONE, e), loc)
adamc@823 1113 | PAnnot ((PVar x, _), t) => (EAbs (x, SOME t, e), loc)
adamc@822 1114 | _ => (EAbs ("$x", SOME pt,
adamc@822 1115 (ECase ((EVar ([], "$x", DontInfer),
adamc@822 1116 loc),
adamc@822 1117 [(pterm, e)]), loc)), loc)
adamc@241 1118 in
adamc@822 1119 (e', (TFun (pt, t), loc))
adamc@241 1120 end)
adamc@822 1121 | earga (earga)
adamc@241 1122
adamc@822 1123 earga : LBRACK SYMBOL RBRACK (fn (e, t) =>
adamc@241 1124 let
adamc@822 1125 val loc = s (LBRACKleft, RBRACKright)
adamc@822 1126 val kind = (KWild, loc)
adamc@822 1127 in
adamc@822 1128 ((ECAbs (Implicit, SYMBOL, kind, e), loc),
adamc@822 1129 (TCFun (Implicit, SYMBOL, kind, t), loc))
adamc@822 1130 end)
adam@1302 1131 | LBRACK SYMBOL DCOLONWILD RBRACK (fn (e, t) =>
adam@1302 1132 let
adam@1302 1133 val loc = s (LBRACKleft, RBRACKright)
adam@1302 1134 val kind = (KWild, loc)
adam@1302 1135 in
adam@1302 1136 ((ECAbs (Explicit, SYMBOL, kind, e), loc),
adam@1302 1137 (TCFun (Explicit, SYMBOL, kind, t), loc))
adam@1302 1138 end)
adamc@822 1139 | LBRACK SYMBOL kcolon kind RBRACK(fn (e, t) =>
adamc@822 1140 let
adamc@822 1141 val loc = s (LBRACKleft, RBRACKright)
adamc@241 1142 in
adamc@241 1143 ((ECAbs (kcolon, SYMBOL, kind, e), loc),
adamc@241 1144 (TCFun (kcolon, SYMBOL, kind, t), loc))
adamc@241 1145 end)
adam@1306 1146 | LBRACK SYMBOL TCOLONWILD RBRACK (fn (e, t) =>
adam@1306 1147 let
adam@1306 1148 val loc = s (LBRACKleft, RBRACKright)
adam@1306 1149 val kind = (KWild, loc)
adam@1306 1150 in
adam@1306 1151 ((ECAbs (Implicit, SYMBOL, kind, e), loc),
adam@1306 1152 (TCFun (Implicit, SYMBOL, kind, t), loc))
adam@1306 1153 end)
adamc@478 1154 | LBRACK cexp TWIDDLE cexp RBRACK(fn (e, t) =>
adamc@356 1155 let
adamc@356 1156 val loc = s (LBRACKleft, RBRACKright)
adamc@356 1157 in
adamc@478 1158 ((EDisjoint (cexp1, cexp2, e), loc),
adamc@628 1159 (TDisjoint (cexp1, cexp2, t), loc))
adamc@356 1160 end)
adamc@822 1161 | LBRACK CSYMBOL RBRACK (fn (e, t) =>
adamc@623 1162 let
adamc@623 1163 val loc = s (CSYMBOLleft, CSYMBOLright)
adamc@623 1164 in
adamc@623 1165 ((EKAbs (CSYMBOL, e), loc),
adamc@623 1166 (TKFun (CSYMBOL, t), loc))
adamc@623 1167 end)
adamc@241 1168
adamc@8 1169 eterm : LPAREN eexp RPAREN (#1 eexp, s (LPARENleft, RPARENright))
adamc@195 1170 | LPAREN etuple RPAREN (let
adamc@195 1171 val loc = s (LPARENleft, RPARENright)
adamc@195 1172 in
adamc@195 1173 (ERecord (ListUtil.mapi (fn (i, e) =>
adamc@195 1174 ((CName (Int.toString (i + 1)), loc),
adamc@195 1175 e)) etuple), loc)
adamc@195 1176 end)
adamc@8 1177
adamc@403 1178 | path (EVar (#1 path, #2 path, Infer), s (pathleft, pathright))
adamc@403 1179 | cpath (EVar (#1 cpath, #2 cpath, Infer), s (cpathleft, cpathright))
adamc@403 1180 | AT path (EVar (#1 path, #2 path, TypesOnly), s (ATleft, pathright))
adamc@403 1181 | AT AT path (EVar (#1 path, #2 path, DontInfer), s (AT1left, pathright))
adamc@403 1182 | AT cpath (EVar (#1 cpath, #2 cpath, TypesOnly), s (ATleft, cpathright))
adamc@403 1183 | AT AT cpath (EVar (#1 cpath, #2 cpath, DontInfer), s (AT1left, cpathright))
adamc@12 1184 | LBRACE rexp RBRACE (ERecord rexp, s (LBRACEleft, RBRACEright))
adamc@110 1185 | UNIT (ERecord [], s (UNITleft, UNITright))
adamc@12 1186
adamc@14 1187 | INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@14 1188 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@14 1189 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@821 1190 | CHAR (EPrim (Prim.Char CHAR), s (CHARleft, CHARright))
adamc@14 1191
adamc@200 1192 | path DOT idents (let
adamc@200 1193 val loc = s (pathleft, identsright)
adamc@200 1194 in
adamc@200 1195 foldl (fn (ident, e) =>
adamc@200 1196 (EField (e, ident), loc))
adamc@403 1197 (EVar (#1 path, #2 path, Infer), s (pathleft, pathright)) idents
adamc@403 1198 end)
adamc@910 1199 | LPAREN eexp RPAREN DOT idents (let
adamc@910 1200 val loc = s (LPARENleft, identsright)
adamc@910 1201 in
adamc@910 1202 foldl (fn (ident, e) =>
adamc@910 1203 (EField (e, ident), loc))
adamc@910 1204 eexp idents
adamc@910 1205 end)
adamc@403 1206 | AT path DOT idents (let
adamc@403 1207 val loc = s (ATleft, identsright)
adamc@403 1208 in
adamc@403 1209 foldl (fn (ident, e) =>
adamc@403 1210 (EField (e, ident), loc))
adamc@403 1211 (EVar (#1 path, #2 path, TypesOnly), s (pathleft, pathright)) idents
adamc@403 1212 end)
adamc@403 1213 | AT AT path DOT idents (let
adamc@403 1214 val loc = s (AT1left, identsright)
adamc@403 1215 in
adamc@403 1216 foldl (fn (ident, e) =>
adamc@403 1217 (EField (e, ident), loc))
adamc@403 1218 (EVar (#1 path, #2 path, DontInfer), s (pathleft, pathright)) idents
adamc@200 1219 end)
adamc@71 1220
adamc@360 1221 | XML_BEGIN xml XML_END (let
adamc@360 1222 val loc = s (XML_BEGINleft, XML_ENDright)
adamc@360 1223 in
adamc@360 1224 if XML_BEGIN = "xml" then
adamc@360 1225 ()
adamc@360 1226 else
adamc@360 1227 ErrorMsg.errorAt loc "Initial XML tag pair aren't both tagged \"xml\".";
adamc@360 1228 xml
adamc@360 1229 end)
adamc@360 1230 | XML_BEGIN XML_END (let
adamc@360 1231 val loc = s (XML_BEGINleft, XML_ENDright)
adamc@360 1232 in
adamc@360 1233 if XML_BEGIN = "xml" then
adamc@360 1234 ()
adamc@360 1235 else
adamc@360 1236 ErrorMsg.errorAt loc "Initial XML tag pair aren't both tagged \"xml\".";
adamc@403 1237 (EApp ((EVar (["Basis"], "cdata", Infer), loc),
adamc@360 1238 (EPrim (Prim.String ""), loc)),
adamc@360 1239 loc)
adamc@360 1240 end)
adamc@360 1241 | XML_BEGIN_END (let
adamc@360 1242 val loc = s (XML_BEGIN_ENDleft, XML_BEGIN_ENDright)
adamc@360 1243 in
adamc@360 1244 if XML_BEGIN_END = "xml" then
adamc@360 1245 ()
adamc@360 1246 else
adamc@360 1247 ErrorMsg.errorAt loc "Initial XML tag pair aren't both tagged \"xml\".";
adamc@403 1248 (EApp ((EVar (["Basis"], "cdata", Infer), loc),
adamc@360 1249 (EPrim (Prim.String ""), loc)),
adamc@360 1250 loc)
adamc@360 1251 end)
adamc@302 1252
adamc@204 1253 | LPAREN query RPAREN (query)
adamc@300 1254 | LPAREN CWHERE sqlexp RPAREN (sqlexp)
adamc@339 1255 | LPAREN SQL sqlexp RPAREN (sqlexp)
adamc@1070 1256 | LPAREN FROM tables RPAREN (#2 tables)
adamc@1071 1257 | LPAREN SELECT1 query1 RPAREN (query1)
adamc@302 1258
adamc@302 1259 | LPAREN INSERT INTO texp LPAREN fields RPAREN VALUES LPAREN sqlexps RPAREN RPAREN
adamc@302 1260 (let
adamc@302 1261 val loc = s (LPAREN1left, RPAREN3right)
adamc@302 1262
adamc@403 1263 val e = (EVar (["Basis"], "insert", Infer), loc)
adamc@302 1264 val e = (EApp (e, texp), loc)
adamc@302 1265 in
adamc@302 1266 if length fields <> length sqlexps then
adam@1360 1267 ErrorMsg.errorAt loc ("Length mismatch in INSERT field specification ("
adam@1360 1268 ^ Int.toString (length fields)
adam@1360 1269 ^ " vs. " ^ Int.toString (length sqlexps) ^ ")")
adamc@302 1270 else
adamc@302 1271 ();
adamc@302 1272 (EApp (e, (ERecord (ListPair.zip (fields, sqlexps)), loc)), loc)
adamc@302 1273 end)
adamc@310 1274 | LPAREN enterDml UPDATE texp SET fsets CWHERE sqlexp leaveDml RPAREN
adamc@303 1275 (let
adamc@303 1276 val loc = s (LPARENleft, RPARENright)
adamc@303 1277
adamc@403 1278 val e = (EVar (["Basis"], "update", Infer), loc)
adamc@342 1279 val e = (ECApp (e, (CWild (KRecord (KType, loc), loc), loc)), loc)
adamc@306 1280 val e = (EApp (e, (ERecord fsets, loc)), loc)
adamc@305 1281 val e = (EApp (e, texp), loc)
adamc@303 1282 in
adamc@303 1283 (EApp (e, sqlexp), loc)
adamc@303 1284 end)
adamc@310 1285 | LPAREN enterDml DELETE FROM texp CWHERE sqlexp leaveDml RPAREN
adamc@304 1286 (let
adamc@304 1287 val loc = s (LPARENleft, RPARENright)
adamc@304 1288
adamc@403 1289 val e = (EVar (["Basis"], "delete", Infer), loc)
adamc@304 1290 val e = (EApp (e, texp), loc)
adamc@304 1291 in
adamc@304 1292 (EApp (e, sqlexp), loc)
adamc@304 1293 end)
adamc@302 1294
adamc@211 1295 | UNDER (EWild, s (UNDERleft, UNDERright))
adamc@91 1296
adamc@446 1297 | LET edecls IN eexp END (ELet (edecls, eexp), s (LETleft, ENDright))
adamc@446 1298
adamc@762 1299 | LBRACK RBRACK (EVar (["Basis"], "Nil", Infer), s (LBRACKleft, RBRACKright))
adamc@762 1300
adamc@446 1301 edecls : ([])
adamc@446 1302 | edecl edecls (edecl :: edecls)
adamc@446 1303
adamc@825 1304 edecl : VAL pat EQ eexp ((EDVal (pat, eexp), s (VALleft, eexpright)))
adamc@446 1305 | VAL REC valis ((EDValRec valis, s (VALleft, valisright)))
adamc@446 1306 | FUN valis ((EDValRec valis, s (FUNleft, valisright)))
adamc@446 1307
adamc@310 1308 enterDml : (inDml := true)
adamc@310 1309 leaveDml : (inDml := false)
adamc@310 1310
adamc@403 1311 texp : SYMBOL (EVar ([], SYMBOL, Infer), s (SYMBOLleft, SYMBOLright))
adamc@302 1312 | LBRACE LBRACE eexp RBRACE RBRACE (eexp)
adamc@302 1313
adamc@302 1314 fields : fident ([fident])
adamc@302 1315 | fident COMMA fields (fident :: fields)
adamc@302 1316
adamc@302 1317 sqlexps: sqlexp ([sqlexp])
adamc@302 1318 | sqlexp COMMA sqlexps (sqlexp :: sqlexps)
adamc@302 1319
adamc@303 1320 fsets : fident EQ sqlexp ([(fident, sqlexp)])
adamc@303 1321 | fident EQ sqlexp COMMA fsets ((fident, sqlexp) :: fsets)
adamc@303 1322
adamc@200 1323 idents : ident ([ident])
adamc@200 1324 | ident DOT idents (ident :: idents)
adamc@200 1325
adamc@195 1326 etuple : eexp COMMA eexp ([eexp1, eexp2])
adamc@195 1327 | eexp COMMA etuple (eexp :: etuple)
adamc@195 1328
adamc@170 1329 branch : pat DARROW eexp (pat, eexp)
adamc@170 1330
adamc@170 1331 branchs: ([])
adamc@170 1332 | BAR branch branchs (branch :: branchs)
adamc@170 1333
adamc@822 1334 patS : pterm (pterm)
adamc@822 1335 | pterm DCOLON patS (let
adamc@822 1336 val loc = s (ptermleft, patSright)
adamc@762 1337 in
adamc@762 1338 (PCon (["Basis"], "Cons", SOME (PRecord ([("1", pterm),
adamc@822 1339 ("2", patS)], false), loc)),
adamc@762 1340 loc)
adamc@762 1341 end)
adamc@822 1342 | patS COLON cexp (PAnnot (patS, cexp), s (patSleft, cexpright))
adamc@822 1343
adamc@822 1344 pat : patS (patS)
adamc@822 1345 | cpath pterm (PCon (#1 cpath, #2 cpath, SOME pterm), s (cpathleft, ptermright))
adamc@170 1346
adamc@170 1347 pterm : SYMBOL (PVar SYMBOL, s (SYMBOLleft, SYMBOLright))
adamc@170 1348 | cpath (PCon (#1 cpath, #2 cpath, NONE), s (cpathleft, cpathright))
adamc@170 1349 | UNDER (PWild, s (UNDERleft, UNDERright))
adamc@173 1350 | INT (PPrim (Prim.Int INT), s (INTleft, INTright))
adam@1420 1351 | MINUS INT (PPrim (Prim.Int (~INT)), s (MINUSleft, INTright))
adamc@173 1352 | STRING (PPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@821 1353 | CHAR (PPrim (Prim.Char CHAR), s (CHARleft, CHARright))
adamc@170 1354 | LPAREN pat RPAREN (pat)
adamc@174 1355 | LBRACE RBRACE (PRecord ([], false), s (LBRACEleft, RBRACEright))
adamc@174 1356 | UNIT (PRecord ([], false), s (UNITleft, UNITright))
adamc@174 1357 | LBRACE rpat RBRACE (PRecord rpat, s (LBRACEleft, RBRACEright))
adamc@195 1358 | LPAREN ptuple RPAREN (PRecord (ListUtil.mapi (fn (i, p) => (Int.toString (i + 1), p)) ptuple,
adamc@195 1359 false),
adamc@195 1360 s (LPARENleft, RPARENright))
adamc@762 1361 | LBRACK RBRACK (PCon (["Basis"], "Nil", NONE), s (LBRACKleft, RBRACKright))
adamc@174 1362
adamc@175 1363 rpat : CSYMBOL EQ pat ([(CSYMBOL, pat)], false)
adamc@243 1364 | INT EQ pat ([(Int64.toString INT, pat)], false)
adamc@174 1365 | DOTDOTDOT ([], true)
adamc@175 1366 | CSYMBOL EQ pat COMMA rpat ((CSYMBOL, pat) :: #1 rpat, #2 rpat)
adamc@243 1367 | INT EQ pat COMMA rpat ((Int64.toString INT, pat) :: #1 rpat, #2 rpat)
adamc@170 1368
adamc@195 1369 ptuple : pat COMMA pat ([pat1, pat2])
adamc@195 1370 | pat COMMA ptuple (pat :: ptuple)
adamc@195 1371
adamc@12 1372 rexp : ([])
adamc@12 1373 | ident EQ eexp ([(ident, eexp)])
adamc@12 1374 | ident EQ eexp COMMA rexp ((ident, eexp) :: rexp)
adamc@91 1375
adamc@141 1376 xml : xmlOne xml (let
adamc@141 1377 val pos = s (xmlOneleft, xmlright)
adamc@141 1378 in
adamc@720 1379 (EApp ((EApp (
adamc@720 1380 (EVar (["Basis"], "join", Infer), pos),
adamc@720 1381 xmlOne), pos),
adamc@720 1382 xml), pos)
adamc@141 1383 end)
adamc@141 1384 | xmlOne (xmlOne)
adamc@91 1385
adamc@1045 1386 xmlOpt : xml (xml)
adamc@1045 1387 | (EApp ((EVar (["Basis"], "cdata", Infer), dummy),
adamc@1045 1388 (EPrim (Prim.String ""), dummy)),
adamc@1045 1389 dummy)
adamc@1045 1390
adamc@403 1391 xmlOne : NOTAGS (EApp ((EVar (["Basis"], "cdata", Infer), s (NOTAGSleft, NOTAGSright)),
adamc@141 1392 (EPrim (Prim.String NOTAGS), s (NOTAGSleft, NOTAGSright))),
adamc@141 1393 s (NOTAGSleft, NOTAGSright))
adamc@141 1394 | tag DIVIDE GT (let
adamc@141 1395 val pos = s (tagleft, GTright)
adamc@710 1396
adamc@710 1397 val cdata =
adamc@756 1398 if #1 (#1 tag) = "submit" orelse #1 (#1 tag) = "dyn" then
adamc@710 1399 let
adamc@710 1400 val e = (EVar (["Basis"], "cdata", DontInfer), pos)
adamc@710 1401 val e = (ECApp (e, (CWild (KWild, pos), pos)), pos)
adamc@710 1402 in
adamc@710 1403 (ECApp (e, (CRecord [], pos)), pos)
adamc@710 1404 end
adamc@710 1405 else
adamc@710 1406 (EVar (["Basis"], "cdata", Infer), pos)
adamc@710 1407
adamc@710 1408 val cdata = (EApp (cdata,
adamc@710 1409 (EPrim (Prim.String ""), pos)),
adamc@710 1410 pos)
adamc@141 1411 in
adam@1412 1412 (EApp (#3 tag, cdata), pos)
adamc@141 1413 end)
adamc@141 1414
adamc@1045 1415 | tag GT xmlOpt END_TAG (let
adam@1563 1416 fun tagOut s =
adam@1563 1417 case s of
adam@1563 1418 "tabl" => "table"
adam@1563 1419 | _ => s
adam@1563 1420
adamc@141 1421 val pos = s (tagleft, GTright)
adamc@325 1422 val et = tagIn END_TAG
adamc@141 1423 in
adamc@756 1424 if #1 (#1 tag) = et then
adamc@361 1425 if et = "form" then
adam@1412 1426 let
adam@1412 1427 val e = (EVar (["Basis"], "form", Infer), pos)
adam@1412 1428 val e = (EApp (e, case #2 tag of
adam@1412 1429 NONE => (EVar (["Basis"], "None", Infer), pos)
adam@1412 1430 | SOME c => (EApp ((EVar (["Basis"], "Some", Infer), pos), c), pos)), pos)
adam@1412 1431 in
adam@1412 1432 (EApp (e, xmlOpt), pos)
adam@1412 1433 end
adamc@1093 1434 else if et = "subform" orelse et = "subforms" then
adamc@1093 1435 (EApp (#2 (#1 tag),
adamc@1045 1436 xmlOpt), pos)
adamc@758 1437 else if et = "entry" then
adamc@758 1438 (EApp ((EVar (["Basis"], "entry", Infer), pos),
adamc@1045 1439 xmlOpt), pos)
adamc@141 1440 else
adam@1412 1441 (EApp (#3 tag, xmlOpt), pos)
adamc@141 1442 else
adamc@325 1443 (if ErrorMsg.anyErrors () then
adamc@325 1444 ()
adamc@325 1445 else
adamc@1189 1446 ErrorMsg.errorAt pos ("Begin tag <"
adam@1563 1447 ^ tagOut (#1 (#1 tag))
adamc@1189 1448 ^ "> and end tag </"
adam@1563 1449 ^ tagOut et
adamc@1189 1450 ^ "> don't match.");
adamc@623 1451 (EWild, pos))
adamc@141 1452 end)
adamc@141 1453 | LBRACE eexp RBRACE (eexp)
adamc@391 1454 | LBRACE LBRACK eexp RBRACK RBRACE (let
adamc@391 1455 val loc = s (LBRACEleft, RBRACEright)
adamc@403 1456 val e = (EVar (["Top"], "txt", Infer), loc)
adamc@391 1457 in
adamc@391 1458 (EApp (e, eexp), loc)
adamc@391 1459 end)
adamc@92 1460
adamc@141 1461 tag : tagHead attrs (let
adamc@141 1462 val pos = s (tagHeadleft, attrsright)
adamc@721 1463
adamc@721 1464 val e = (EVar (["Basis"], "tag", Infer), pos)
adamc@721 1465 val eo = case #1 attrs of
adamc@721 1466 NONE => (EVar (["Basis"], "None", Infer), pos)
adamc@721 1467 | SOME e => (EApp ((EVar (["Basis"], "Some", Infer), pos),
adamc@721 1468 e), pos)
adamc@721 1469 val e = (EApp (e, eo), pos)
adamc@721 1470 val e = (EApp (e, (ERecord (#2 attrs), pos)), pos)
adamc@721 1471 val e = (EApp (e, (EApp (#2 tagHead,
adamc@721 1472 (ERecord [], pos)), pos)), pos)
adamc@141 1473 in
adam@1412 1474 (tagHead, #1 attrs, e)
adamc@141 1475 end)
adamc@141 1476
adamc@141 1477 tagHead: BEGIN_TAG (let
adamc@325 1478 val bt = tagIn BEGIN_TAG
adamc@141 1479 val pos = s (BEGIN_TAGleft, BEGIN_TAGright)
adamc@141 1480 in
adamc@325 1481 (bt,
adamc@473 1482 (EVar (["Basis"], bt, Infer), pos))
adamc@141 1483 end)
adamc@141 1484 | tagHead LBRACE cexp RBRACE (#1 tagHead, (ECApp (#2 tagHead, cexp), s (tagHeadleft, RBRACEright)))
adamc@92 1485
adamc@721 1486 attrs : (NONE, [])
adamc@721 1487 | attr attrs (let
adamc@721 1488 val loc = s (attrleft, attrsright)
adamc@721 1489 in
adamc@721 1490 case attr of
adamc@721 1491 Class e =>
adamc@721 1492 (case #1 attrs of
adamc@721 1493 NONE => ()
adamc@721 1494 | SOME _ => ErrorMsg.errorAt loc "Multiple classes specified for tag";
adamc@721 1495 (SOME e, #2 attrs))
adamc@721 1496 | Normal xe =>
adamc@721 1497 (#1 attrs, xe :: #2 attrs)
adamc@721 1498 end)
adamc@104 1499
adamc@721 1500 attr : SYMBOL EQ attrv (if SYMBOL = "class" then
adamc@721 1501 Class attrv
adamc@717 1502 else
adamc@724 1503 let
adamc@724 1504 val sym =
adamc@724 1505 case SYMBOL of
adamc@724 1506 "type" => "Typ"
adamc@724 1507 | x => capitalize x
adamc@724 1508 in
adamc@724 1509 Normal ((CName sym, s (SYMBOLleft, SYMBOLright)),
adamc@724 1510 if (sym = "Href" orelse sym = "Src")
adamc@724 1511 andalso (case #1 attrv of
adamc@724 1512 EPrim _ => true
adamc@724 1513 | _ => false) then
adamc@724 1514 let
adamc@724 1515 val loc = s (attrvleft, attrvright)
adamc@724 1516 in
adamc@724 1517 (EApp ((EVar (["Basis"], "bless", Infer), loc),
adamc@724 1518 attrv), loc)
adamc@724 1519 end
adamc@724 1520 else
adamc@724 1521 attrv)
adamc@724 1522 end)
adamc@204 1523
adamc@104 1524 attrv : INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@104 1525 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@104 1526 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@110 1527 | LBRACE eexp RBRACE (eexp)
adamc@226 1528
adamc@232 1529 query : query1 obopt lopt ofopt (let
adamc@229 1530 val loc = s (query1left, query1right)
adamc@230 1531
adamc@230 1532 val re = (ERecord [((CName "Rows", loc),
adamc@230 1533 query1),
adamc@230 1534 ((CName "OrderBy", loc),
adamc@231 1535 obopt),
adamc@231 1536 ((CName "Limit", loc),
adamc@232 1537 lopt),
adamc@232 1538 ((CName "Offset", loc),
adamc@232 1539 ofopt)], loc)
adamc@229 1540 in
adamc@403 1541 (EApp ((EVar (["Basis"], "sql_query", Infer), loc), re), loc)
adamc@229 1542 end)
adamc@993 1543
adamc@993 1544 dopt : (EVar (["Basis"], "False", Infer), dummy)
adamc@993 1545 | DISTINCT (EVar (["Basis"], "True", Infer),
adamc@993 1546 s (DISTINCTleft, DISTINCTright))
adamc@993 1547
adamc@993 1548 query1 : SELECT dopt select FROM tables wopt gopt hopt
adamc@209 1549 (let
adamc@204 1550 val loc = s (SELECTleft, tablesright)
adamc@207 1551
adamc@1070 1552 val (empties, sel, exps) =
adamc@207 1553 case select of
adamc@1070 1554 Star => ([],
adamc@1070 1555 map (fn nm =>
adamc@233 1556 (nm, (CTuple [(CWild (KRecord (KType, loc), loc),
adamc@233 1557 loc),
adamc@233 1558 (CRecord [], loc)],
adamc@748 1559 loc))) (#1 tables),
adamc@233 1560 [])
adamc@207 1561 | Items sis =>
adamc@207 1562 let
adamc@748 1563 val tabs = map (fn nm => (nm, (CRecord [], loc))) (#1 tables)
adamc@1194 1564 val (_, tabs, exps) = foldl (amend_select loc)
adamc@1194 1565 (1, tabs, []) sis
adamc@1070 1566 val empties = List.mapPartial (fn (nm, (CRecord [], _)) =>
adamc@1070 1567 SOME nm
adamc@1070 1568 | _ => NONE) tabs
adamc@207 1569 in
adamc@1070 1570 (empties,
adamc@1070 1571 map (fn (nm, c) => (nm,
adamc@233 1572 (CTuple [c,
adamc@233 1573 (CWild (KRecord (KType, loc), loc),
adamc@233 1574 loc)], loc))) tabs,
adamc@233 1575 exps)
adamc@207 1576 end
adamc@207 1577
adamc@207 1578 val sel = (CRecord sel, loc)
adamc@207 1579
adamc@226 1580 val grp = case gopt of
adamc@403 1581 NONE => (ECApp ((EVar (["Basis"], "sql_subset_all",
adamc@403 1582 Infer), loc),
adamc@226 1583 (CWild (KRecord (KRecord (KType, loc), loc),
adamc@226 1584 loc), loc)), loc)
adamc@226 1585 | SOME gis =>
adamc@226 1586 let
adamc@748 1587 val tabs = map (fn nm =>
adamc@748 1588 (nm, (CRecord [], loc))) (#1 tables)
adamc@226 1589 val tabs = foldl (amend_group loc) tabs gis
adamc@226 1590
adamc@226 1591 val tabs = map (fn (nm, c) =>
adamc@226 1592 (nm,
adamc@226 1593 (CTuple [c,
adamc@226 1594 (CWild (KRecord (KType, loc),
adamc@226 1595 loc),
adamc@226 1596 loc)], loc))) tabs
adamc@226 1597 in
adamc@403 1598 (ECApp ((EVar (["Basis"], "sql_subset", Infer), loc),
adamc@226 1599 (CRecord tabs, loc)), loc)
adamc@226 1600 end
adamc@226 1601
adamc@403 1602 val e = (EVar (["Basis"], "sql_query1", Infer), loc)
adamc@1070 1603 val e = (ECApp (e, (CRecord (map (fn nm => (nm, (CUnit, loc))) empties),
adamc@1070 1604 loc)), loc)
adamc@993 1605 val re = (ERecord [((CName "Distinct", loc),
adamc@993 1606 dopt),
adamc@993 1607 ((CName "From", loc),
adamc@748 1608 #2 tables),
adamc@209 1609 ((CName "Where", loc),
adamc@223 1610 wopt),
adamc@224 1611 ((CName "GroupBy", loc),
adamc@226 1612 grp),
adamc@224 1613 ((CName "Having", loc),
adamc@224 1614 hopt),
adamc@223 1615 ((CName "SelectFields", loc),
adamc@403 1616 (ECApp ((EVar (["Basis"], "sql_subset", Infer), loc),
adamc@233 1617 sel), loc)),
adamc@233 1618 ((CName "SelectExps", loc),
adamc@233 1619 (ERecord exps, loc))], loc)
adamc@223 1620
adamc@209 1621 val e = (EApp (e, re), loc)
adamc@204 1622 in
adamc@207 1623 e
adamc@204 1624 end)
adam@1427 1625 | query1 UNION query1 (sql_relop ("union", false, query11, query12, s (query11left, query12right)))
adam@1427 1626 | query1 INTERSECT query1 (sql_relop ("intersect", false, query11, query12, s (query11left, query12right)))
adam@1427 1627 | query1 EXCEPT query1 (sql_relop ("except", false, query11, query12, s (query11left, query12right)))
adam@1427 1628 | query1 UNION ALL query1 (sql_relop ("union", true, query11, query12, s (query11left, query12right)))
adam@1427 1629 | query1 INTERSECT ALL query1 (sql_relop ("intersect", true, query11, query12, s (query11left, query12right)))
adam@1427 1630 | query1 EXCEPT ALL query1 (sql_relop ("except", true, query11, query12, s (query11left, query12right)))
adamc@1070 1631 | LBRACE LBRACE LBRACE eexp RBRACE RBRACE RBRACE (eexp)
adamc@204 1632
adamc@749 1633 tables : fitem (fitem)
adamc@749 1634 | fitem COMMA tables (let
adamc@749 1635 val loc = s (fitemleft, tablesright)
adamc@748 1636
adamc@748 1637 val e = (EVar (["Basis"], "sql_from_comma", Infer), loc)
adamc@749 1638 val e = (EApp (e, #2 fitem), loc)
adamc@748 1639 in
adamc@749 1640 (#1 fitem @ #1 tables,
adamc@748 1641 (EApp (e, #2 tables), loc))
adamc@748 1642 end)
adamc@204 1643
adamc@749 1644 fitem : table' ([#1 table'], #2 table')
adamc@1265 1645 | LBRACE LBRACE eexp RBRACE RBRACE (tnamesOf eexp, eexp)
adamc@749 1646 | fitem JOIN fitem ON sqlexp (let
adamc@749 1647 val loc = s (fitem1left, sqlexpright)
adamc@749 1648
adamc@749 1649 val e = (EVar (["Basis"], "sql_inner_join", Infer), loc)
adamc@749 1650 val e = (EApp (e, #2 fitem1), loc)
adamc@749 1651 val e = (EApp (e, #2 fitem2), loc)
adamc@749 1652 in
adamc@749 1653 (#1 fitem1 @ #1 fitem2,
adamc@749 1654 (EApp (e, sqlexp), loc))
adamc@749 1655 end)
adamc@749 1656 | fitem INNER JOIN fitem ON sqlexp (let
adamc@749 1657 val loc = s (fitem1left, sqlexpright)
adamc@749 1658
adamc@749 1659 val e = (EVar (["Basis"], "sql_inner_join", Infer), loc)
adamc@749 1660 val e = (EApp (e, #2 fitem1), loc)
adamc@749 1661 val e = (EApp (e, #2 fitem2), loc)
adamc@749 1662 in
adamc@749 1663 (#1 fitem1 @ #1 fitem2,
adamc@749 1664 (EApp (e, sqlexp), loc))
adamc@749 1665 end)
adamc@749 1666 | fitem CROSS JOIN fitem (let
adamc@749 1667 val loc = s (fitem1left, fitem2right)
adamc@749 1668
adamc@749 1669 val e = (EVar (["Basis"], "sql_inner_join", Infer), loc)
adamc@749 1670 val e = (EApp (e, #2 fitem1), loc)
adamc@749 1671 val e = (EApp (e, #2 fitem2), loc)
adamc@749 1672 val tru = sql_inject (EVar (["Basis"], "True", Infer), loc)
adamc@749 1673 in
adamc@749 1674 (#1 fitem1 @ #1 fitem2,
adamc@749 1675 (EApp (e, tru), loc))
adamc@749 1676 end)
adamc@750 1677 | fitem LEFT JOIN fitem ON sqlexp (let
adamc@750 1678 val loc = s (fitem1left, sqlexpright)
adamc@750 1679
adamc@750 1680 val e = (EVar (["Basis"], "sql_left_join", Infer), loc)
adamc@750 1681 val e = (EApp (e, #2 fitem1), loc)
adamc@750 1682 val e = (EApp (e, #2 fitem2), loc)
adamc@750 1683 in
adamc@750 1684 (#1 fitem1 @ #1 fitem2,
adamc@750 1685 (EApp (e, sqlexp), loc))
adamc@750 1686 end)
adamc@751 1687 | fitem LEFT OUTER JOIN fitem ON sqlexp (let
adamc@751 1688 val loc = s (fitem1left, sqlexpright)
adamc@751 1689
adamc@751 1690 val e = (EVar (["Basis"], "sql_left_join", Infer), loc)
adamc@751 1691 val e = (EApp (e, #2 fitem1), loc)
adamc@751 1692 val e = (EApp (e, #2 fitem2), loc)
adamc@751 1693 in
adamc@751 1694 (#1 fitem1 @ #1 fitem2,
adamc@751 1695 (EApp (e, sqlexp), loc))
adamc@751 1696 end)
adamc@751 1697 | fitem RIGHT JOIN fitem ON sqlexp (let
adamc@751 1698 val loc = s (fitem1left, sqlexpright)
adamc@751 1699
adamc@751 1700 val e = (EVar (["Basis"], "sql_right_join", Infer), loc)
adamc@751 1701 val e = (EApp (e, #2 fitem1), loc)
adamc@751 1702 val e = (EApp (e, #2 fitem2), loc)
adamc@751 1703 in
adamc@751 1704 (#1 fitem1 @ #1 fitem2,
adamc@751 1705 (EApp (e, sqlexp), loc))
adamc@751 1706 end)
adamc@751 1707 | fitem RIGHT OUTER JOIN fitem ON sqlexp (let
adamc@751 1708 val loc = s (fitem1left, sqlexpright)
adamc@751 1709
adamc@751 1710 val e = (EVar (["Basis"], "sql_right_join", Infer), loc)
adamc@751 1711 val e = (EApp (e, #2 fitem1), loc)
adamc@751 1712 val e = (EApp (e, #2 fitem2), loc)
adamc@751 1713 in
adamc@751 1714 (#1 fitem1 @ #1 fitem2,
adamc@751 1715 (EApp (e, sqlexp), loc))
adamc@751 1716 end)
adamc@751 1717 | fitem FULL JOIN fitem ON sqlexp (let
adamc@751 1718 val loc = s (fitem1left, sqlexpright)
adamc@751 1719
adamc@751 1720 val e = (EVar (["Basis"], "sql_full_join", Infer), loc)
adamc@751 1721 val e = (EApp (e, #2 fitem1), loc)
adamc@751 1722 val e = (EApp (e, #2 fitem2), loc)
adamc@751 1723 in
adamc@751 1724 (#1 fitem1 @ #1 fitem2,
adamc@751 1725 (EApp (e, sqlexp), loc))
adamc@751 1726 end)
adamc@751 1727 | fitem FULL OUTER JOIN fitem ON sqlexp (let
adamc@751 1728 val loc = s (fitem1left, sqlexpright)
adamc@751 1729
adamc@751 1730 val e = (EVar (["Basis"], "sql_full_join", Infer), loc)
adamc@751 1731 val e = (EApp (e, #2 fitem1), loc)
adamc@751 1732 val e = (EApp (e, #2 fitem2), loc)
adamc@751 1733 in
adamc@751 1734 (#1 fitem1 @ #1 fitem2,
adamc@751 1735 (EApp (e, sqlexp), loc))
adamc@751 1736 end)
adamc@1192 1737 | LPAREN query RPAREN AS tname (let
adamc@1192 1738 val loc = s (LPARENleft, RPARENright)
adamc@1192 1739
adamc@1192 1740 val e = (EVar (["Basis"], "sql_from_query", Infer), loc)
adamc@1192 1741 val e = (ECApp (e, tname), loc)
adamc@1192 1742 in
adamc@1192 1743 ([tname], (EApp (e, query), loc))
adamc@1192 1744 end)
adamc@749 1745
adamc@204 1746 tname : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@204 1747 | LBRACE cexp RBRACE (cexp)
adamc@204 1748
adamc@243 1749 table : SYMBOL ((CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright)),
adamc@403 1750 (EVar ([], SYMBOL, Infer), s (SYMBOLleft, SYMBOLright)))
adamc@403 1751 | SYMBOL AS tname (tname, (EVar ([], SYMBOL, Infer), s (SYMBOLleft, SYMBOLright)))
adamc@221 1752 | LBRACE LBRACE eexp RBRACE RBRACE AS tname (tname, eexp)
adamc@207 1753
adamc@748 1754 table' : table (let
adamc@748 1755 val loc = s (tableleft, tableright)
adamc@748 1756 val e = (EVar (["Basis"], "sql_from_table", Infer), loc)
adamc@748 1757 val e = (ECApp (e, #1 table), loc)
adamc@748 1758 in
adamc@748 1759 (#1 table, (EApp (e, #2 table), loc))
adamc@748 1760 end)
adamc@748 1761
adamc@243 1762 tident : SYMBOL (CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright))
adamc@207 1763 | CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@221 1764 | LBRACE LBRACE cexp RBRACE RBRACE (cexp)
adamc@207 1765
adamc@207 1766 fident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@207 1767 | LBRACE cexp RBRACE (cexp)
adamc@207 1768
adamc@207 1769 seli : tident DOT fident (Field (tident, fident))
adamc@1194 1770 | sqlexp (Exp (NONE, sqlexp))
adamc@1194 1771 | sqlexp AS fident (Exp (SOME fident, sqlexp))
adamc@341 1772 | tident DOT LBRACE LBRACE cexp RBRACE RBRACE (Fields (tident, cexp))
adamc@207 1773
adamc@207 1774 selis : seli ([seli])
adamc@207 1775 | seli COMMA selis (seli :: selis)
adamc@207 1776
adamc@207 1777 select : STAR (Star)
adamc@207 1778 | selis (Items selis)
adamc@209 1779
adamc@403 1780 sqlexp : TRUE (sql_inject (EVar (["Basis"], "True", Infer),
adamc@209 1781 s (TRUEleft, TRUEright)))
adamc@403 1782 | FALSE (sql_inject (EVar (["Basis"], "False", Infer),
adamc@209 1783 s (FALSEleft, FALSEright)))
adamc@209 1784
adamc@222 1785 | INT (sql_inject (EPrim (Prim.Int INT),
adamc@222 1786 s (INTleft, INTright)))
adamc@222 1787 | FLOAT (sql_inject (EPrim (Prim.Float FLOAT),
adamc@222 1788 s (FLOATleft, FLOATright)))
adamc@229 1789 | STRING (sql_inject (EPrim (Prim.String STRING),
adamc@229 1790 s (STRINGleft, STRINGright)))
adamc@441 1791 | CURRENT_TIMESTAMP (sql_nfunc ("current_timestamp",
adamc@441 1792 s (CURRENT_TIMESTAMPleft, CURRENT_TIMESTAMPright)))
adamc@222 1793
adamc@221 1794 | tident DOT fident (let
adamc@221 1795 val loc = s (tidentleft, fidentright)
adamc@403 1796 val e = (EVar (["Basis"], "sql_field", Infer), loc)
adamc@221 1797 val e = (ECApp (e, tident), loc)
adamc@221 1798 in
adamc@221 1799 (ECApp (e, fident), loc)
adamc@221 1800 end)
adamc@234 1801 | CSYMBOL (let
adamc@234 1802 val loc = s (CSYMBOLleft, CSYMBOLright)
adamc@310 1803 in
adamc@310 1804 if !inDml then
adamc@310 1805 let
adamc@403 1806 val e = (EVar (["Basis"], "sql_field", Infer), loc)
adamc@310 1807 val e = (ECApp (e, (CName "T", loc)), loc)
adamc@310 1808 in
adamc@310 1809 (ECApp (e, (CName CSYMBOL, loc)), loc)
adamc@310 1810 end
adamc@310 1811 else
adamc@310 1812 let
adamc@403 1813 val e = (EVar (["Basis"], "sql_exp", Infer), loc)
adamc@310 1814 in
adamc@310 1815 (ECApp (e, (CName CSYMBOL, loc)), loc)
adamc@310 1816 end
adamc@310 1817 end)
adamc@221 1818
adamc@471 1819 | LBRACE eexp RBRACE (eexp)
adamc@470 1820
adamc@559 1821 | sqlexp EQ sqlexp (sql_binary ("eq", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1822 | sqlexp NE sqlexp (sql_binary ("ne", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1823 | sqlexp LT sqlexp (sql_binary ("lt", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1824 | sqlexp LE sqlexp (sql_binary ("le", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1825 | sqlexp GT sqlexp (sql_binary ("gt", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1826 | sqlexp GE sqlexp (sql_binary ("ge", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1827
adamc@559 1828 | sqlexp PLUS sqlexp (sql_binary ("plus", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1829 | sqlexp MINUS sqlexp (sql_binary ("minus", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1830 | sqlexp STAR sqlexp (sql_binary ("times", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1831 | sqlexp DIVIDE sqlexp (sql_binary ("div", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1832 | sqlexp MOD sqlexp (sql_binary ("mod", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 1833
adamc@220 1834 | sqlexp CAND sqlexp (sql_binary ("and", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@220 1835 | sqlexp OR sqlexp (sql_binary ("or", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1836
adamc@220 1837 | NOT sqlexp (sql_unary ("not", sqlexp, s (NOTleft, sqlexpright)))
adamc@559 1838 | MINUS sqlexp (sql_unary ("neg", sqlexp, s (MINUSleft, sqlexpright)))
adamc@220 1839
adamc@470 1840 | sqlexp IS NULL (let
adamc@470 1841 val loc = s (sqlexpleft, NULLright)
adamc@470 1842 in
adamc@470 1843 (EApp ((EVar (["Basis"], "sql_is_null", Infer), loc),
adamc@470 1844 sqlexp), loc)
adamc@470 1845 end)
adamc@470 1846
kkallio@1572 1847 | CIF sqlexp CTHEN sqlexp CELSE sqlexp (let
kkallio@1572 1848 val loc = s (CIFleft, sqlexp3right)
kkallio@1572 1849 val e = (EVar (["Basis"], "sql_if_then_else", Infer), loc)
kkallio@1572 1850 in
kkallio@1572 1851 (EApp ((EApp ((EApp (e, sqlexp1), loc), sqlexp2), loc), sqlexp3), loc)
kkallio@1572 1852 end)
kkallio@1572 1853
adamc@471 1854 | LBRACE LBRACK eexp RBRACK RBRACE (sql_inject (#1 eexp,
adamc@471 1855 s (LBRACEleft, RBRACEright)))
adamc@220 1856 | LPAREN sqlexp RPAREN (sqlexp)
adamc@210 1857
adamc@467 1858 | NULL (sql_inject ((EVar (["Basis"], "None", Infer),
adamc@467 1859 s (NULLleft, NULLright))))
adamc@467 1860
adamc@235 1861 | COUNT LPAREN STAR RPAREN (let
adamc@235 1862 val loc = s (COUNTleft, RPARENright)
adamc@235 1863 in
adamc@544 1864 (EVar (["Basis"], "sql_count", Infer), loc)
adamc@235 1865 end)
adamc@1187 1866 | COUNT LPAREN sqlexp RPAREN (let
adamc@1187 1867 val loc = s (COUNTleft, RPARENright)
adamc@1187 1868
adamc@1187 1869 val e = (EVar (["Basis"], "sql_count_col", Infer), loc)
adamc@1187 1870 val e = (EApp ((EVar (["Basis"], "sql_aggregate", Infer), loc),
adamc@1187 1871 e), loc)
adamc@1187 1872 in
adamc@1187 1873 (EApp (e, sqlexp), loc)
adamc@1187 1874 end)
adamc@236 1875 | sqlagg LPAREN sqlexp RPAREN (let
adamc@236 1876 val loc = s (sqlaggleft, RPARENright)
adamc@236 1877
adamc@403 1878 val e = (EVar (["Basis"], "sql_" ^ sqlagg, Infer), loc)
adamc@403 1879 val e = (EApp ((EVar (["Basis"], "sql_aggregate", Infer), loc),
adamc@236 1880 e), loc)
adamc@236 1881 in
adamc@236 1882 (EApp (e, sqlexp), loc)
adamc@236 1883 end)
adam@1602 1884 | COALESCE LPAREN sqlexp COMMA sqlexp RPAREN
adam@1602 1885 (let
adam@1602 1886 val loc = s (COALESCEright, sqlexp2right)
adam@1602 1887 val e = (EVar (["Basis"], "sql_coalesce", Infer), loc)
adam@1602 1888 val e = (EApp (e, sqlexp1), loc)
adam@1602 1889 in
adam@1602 1890 (EApp (e, sqlexp2), loc)
adam@1602 1891 end)
adamc@746 1892 | fname LPAREN sqlexp RPAREN (let
adamc@746 1893 val loc = s (fnameleft, RPARENright)
adamc@746 1894
adamc@746 1895 val e = (EVar (["Basis"], "sql_ufunc", Infer), loc)
adamc@746 1896 val e = (EApp (e, fname), loc)
adamc@746 1897 in
adamc@746 1898 (EApp (e, sqlexp), loc)
adamc@746 1899 end)
adamc@1191 1900 | LPAREN query RPAREN (let
adamc@1191 1901 val loc = s (LPARENleft, RPARENright)
adamc@1191 1902
adamc@1191 1903 val e = (EVar (["Basis"], "sql_subquery", Infer), loc)
adamc@1191 1904 in
adamc@1191 1905 (EApp (e, query), loc)
adamc@1191 1906 end)
adamc@746 1907
adamc@746 1908 fname : SYMBOL (EVar (["Basis"], "sql_" ^ SYMBOL, Infer), s (SYMBOLleft, SYMBOLright))
adamc@746 1909 | LBRACE eexp RBRACE (eexp)
adamc@235 1910
adamc@403 1911 wopt : (sql_inject (EVar (["Basis"], "True", Infer),
adamc@230 1912 dummy))
adamc@209 1913 | CWHERE sqlexp (sqlexp)
adamc@226 1914
adamc@226 1915 groupi : tident DOT fident (GField (tident, fident))
adam@1425 1916 | tident DOT LBRACE LBRACE cexp RBRACE RBRACE (GFields (tident, cexp))
adamc@226 1917
adamc@226 1918 groupis: groupi ([groupi])
adamc@226 1919 | groupi COMMA groupis (groupi :: groupis)
adamc@226 1920
adamc@226 1921 gopt : (NONE)
adamc@226 1922 | GROUP BY groupis (SOME groupis)
adamc@227 1923
adamc@403 1924 hopt : (sql_inject (EVar (["Basis"], "True", Infer),
adamc@230 1925 dummy))
adamc@227 1926 | HAVING sqlexp (sqlexp)
adamc@230 1927
adamc@403 1928 obopt : (ECApp ((EVar (["Basis"], "sql_order_by_Nil", Infer), dummy),
adamc@234 1929 (CWild (KRecord (KType, dummy), dummy), dummy)),
adamc@230 1930 dummy)
adamc@230 1931 | ORDER BY obexps (obexps)
adamc@230 1932
adamc@268 1933 obitem : sqlexp diropt (sqlexp, diropt)
adamc@268 1934
adamc@268 1935 obexps : obitem (let
adamc@268 1936 val loc = s (obitemleft, obitemright)
adamc@230 1937
adamc@403 1938 val e' = (ECApp ((EVar (["Basis"], "sql_order_by_Nil", Infer), loc),
adamc@234 1939 (CWild (KRecord (KType, loc), loc), loc)),
adamc@230 1940 loc)
adamc@403 1941 val e = (EApp ((EVar (["Basis"], "sql_order_by_Cons", Infer), loc),
adamc@268 1942 #1 obitem), loc)
adamc@268 1943 val e = (EApp (e, #2 obitem), loc)
adamc@230 1944 in
adamc@230 1945 (EApp (e, e'), loc)
adamc@230 1946 end)
adamc@268 1947 | obitem COMMA obexps (let
adamc@268 1948 val loc = s (obitemleft, obexpsright)
adamc@230 1949
adamc@403 1950 val e = (EApp ((EVar (["Basis"], "sql_order_by_Cons", Infer), loc),
adamc@268 1951 #1 obitem), loc)
adamc@268 1952 val e = (EApp (e, #2 obitem), loc)
adamc@230 1953 in
adamc@230 1954 (EApp (e, obexps), loc)
adamc@230 1955 end)
adamc@231 1956
adamc@403 1957 diropt : (EVar (["Basis"], "sql_asc", Infer), dummy)
adamc@403 1958 | ASC (EVar (["Basis"], "sql_asc", Infer), s (ASCleft, ASCright))
adamc@403 1959 | DESC (EVar (["Basis"], "sql_desc", Infer), s (DESCleft, DESCright))
adam@1543 1960 | LBRACE eexp RBRACE (eexp)
adamc@268 1961
adamc@403 1962 lopt : (EVar (["Basis"], "sql_no_limit", Infer), dummy)
adamc@403 1963 | LIMIT ALL (EVar (["Basis"], "sql_no_limit", Infer), dummy)
adamc@231 1964 | LIMIT sqlint (let
adamc@231 1965 val loc = s (LIMITleft, sqlintright)
adamc@231 1966 in
adamc@403 1967 (EApp ((EVar (["Basis"], "sql_limit", Infer), loc), sqlint), loc)
adamc@231 1968 end)
adamc@231 1969
adamc@403 1970 ofopt : (EVar (["Basis"], "sql_no_offset", Infer), dummy)
adamc@232 1971 | OFFSET sqlint (let
adamc@232 1972 val loc = s (OFFSETleft, sqlintright)
adamc@232 1973 in
adamc@403 1974 (EApp ((EVar (["Basis"], "sql_offset", Infer), loc), sqlint), loc)
adamc@232 1975 end)
adamc@232 1976
adamc@231 1977 sqlint : INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@231 1978 | LBRACE eexp RBRACE (eexp)
adamc@236 1979
adamc@236 1980 sqlagg : AVG ("avg")
adamc@236 1981 | SUM ("sum")
adamc@236 1982 | MIN ("min")
adamc@236 1983 | MAX ("max")