annotate src/urweb.grm @ 707:d8217b4cb617

PRIMARY KEY
author Adam Chlipala <adamc@hcoop.net>
date Tue, 07 Apr 2009 16:14:31 -0400
parents 1fb318c17546
children 0406e9cccb72
rev   line source
adamc@695 1 (* Copyright (c) 2008-2009, 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@233 45 | Exp of con * 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
adamc@226 54
adamc@207 55 fun eqTnames ((c1, _), (c2, _)) =
adamc@207 56 case (c1, c2) of
adamc@207 57 (CVar (ms1, x1), CVar (ms2, x2)) => ms1 = ms2 andalso x1 = x2
adamc@207 58 | (CName x1, CName x2) => x1 = x2
adamc@207 59 | _ => false
adamc@207 60
adamc@233 61 fun amend_select loc (si, (tabs, exps)) =
adamc@233 62 case si of
adamc@233 63 Field (tx, fx) =>
adamc@233 64 let
adamc@233 65 val c = (CRecord ([(fx, (CWild (KType, loc), loc))]), loc)
adamc@207 66
adamc@233 67 val (tabs, found) = ListUtil.foldlMap (fn ((tx', c'), found) =>
adamc@233 68 if eqTnames (tx, tx') then
adamc@233 69 ((tx', (CConcat (c, c'), loc)), true)
adamc@233 70 else
adamc@233 71 ((tx', c'), found))
adamc@233 72 false tabs
adamc@233 73 in
adamc@233 74 if found then
adamc@233 75 ()
adamc@233 76 else
adamc@233 77 ErrorMsg.errorAt loc "Select of field from unbound table";
adamc@233 78
adamc@233 79 (tabs, exps)
adamc@233 80 end
adamc@341 81 | Fields (tx, fs) =>
adamc@341 82 let
adamc@341 83 val (tabs, found) = ListUtil.foldlMap (fn ((tx', c'), found) =>
adamc@341 84 if eqTnames (tx, tx') then
adamc@341 85 ((tx', (CConcat (fs, c'), loc)), true)
adamc@341 86 else
adamc@341 87 ((tx', c'), found))
adamc@341 88 false tabs
adamc@341 89 in
adamc@341 90 if found then
adamc@341 91 ()
adamc@341 92 else
adamc@341 93 ErrorMsg.errorAt loc "Select of field from unbound table";
adamc@341 94
adamc@341 95 (tabs, exps)
adamc@341 96 end
adamc@233 97 | Exp (c, e) => (tabs, (c, e) :: exps)
adamc@207 98
adamc@226 99 fun amend_group loc (gi, tabs) =
adamc@226 100 let
adamc@226 101 val (tx, c) = case gi of
adamc@226 102 GField (tx, fx) => (tx, (CRecord ([(fx, (CWild (KType, loc), loc))]), loc))
adamc@226 103
adamc@226 104 val (tabs, found) = ListUtil.foldlMap (fn ((tx', c'), found) =>
adamc@226 105 if eqTnames (tx, tx') then
adamc@226 106 ((tx', (CConcat (c, c'), loc)), true)
adamc@226 107 else
adamc@226 108 ((tx', c'), found))
adamc@226 109 false tabs
adamc@226 110 in
adamc@226 111 if found then
adamc@226 112 ()
adamc@226 113 else
adamc@226 114 ErrorMsg.errorAt loc "Select of field from unbound table";
adamc@226 115
adamc@226 116 tabs
adamc@226 117 end
adamc@226 118
adamc@403 119 fun sql_inject (v, loc) =
adamc@403 120 (EApp ((EVar (["Basis"], "sql_inject", Infer), loc), (v, loc)), loc)
adamc@209 121
adamc@220 122 fun sql_binary (oper, sqlexp1, sqlexp2, loc) =
adamc@220 123 let
adamc@403 124 val e = (EVar (["Basis"], "sql_binary", Infer), loc)
adamc@403 125 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper, Infer), loc)), loc)
adamc@220 126 val e = (EApp (e, sqlexp1), loc)
adamc@220 127 in
adamc@220 128 (EApp (e, sqlexp2), loc)
adamc@220 129 end
adamc@220 130
adamc@220 131 fun sql_unary (oper, sqlexp, loc) =
adamc@220 132 let
adamc@403 133 val e = (EVar (["Basis"], "sql_unary", Infer), loc)
adamc@403 134 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper, Infer), loc)), loc)
adamc@220 135 in
adamc@220 136 (EApp (e, sqlexp), loc)
adamc@219 137 end
adamc@219 138
adamc@229 139 fun sql_relop (oper, sqlexp1, sqlexp2, loc) =
adamc@229 140 let
adamc@403 141 val e = (EVar (["Basis"], "sql_relop", Infer), loc)
adamc@403 142 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper, Infer), loc)), loc)
adamc@229 143 val e = (EApp (e, sqlexp1), loc)
adamc@229 144 in
adamc@229 145 (EApp (e, sqlexp2), loc)
adamc@229 146 end
adamc@229 147
adamc@441 148 fun sql_nfunc (oper, loc) =
adamc@441 149 let
adamc@441 150 val e = (EVar (["Basis"], "sql_nfunc", Infer), loc)
adamc@441 151 in
adamc@441 152 (EApp (e, (EVar (["Basis"], "sql_" ^ oper, Infer), loc)), loc)
adamc@441 153 end
adamc@441 154
adamc@389 155 fun native_unop (oper, e1, loc) =
adamc@389 156 let
adamc@403 157 val e = (EVar (["Basis"], oper, Infer), loc)
adamc@389 158 in
adamc@389 159 (EApp (e, e1), loc)
adamc@389 160 end
adamc@389 161
adamc@256 162 fun native_op (oper, e1, e2, loc) =
adamc@256 163 let
adamc@403 164 val e = (EVar (["Basis"], oper, Infer), loc)
adamc@256 165 val e = (EApp (e, e1), loc)
adamc@256 166 in
adamc@256 167 (EApp (e, e2), loc)
adamc@256 168 end
adamc@256 169
adamc@310 170 val inDml = ref false
adamc@310 171
adamc@325 172 fun tagIn bt =
adamc@325 173 case bt of
adamc@325 174 "table" => "tabl"
adamc@325 175 | _ => bt
adamc@325 176
adamc@1 177 %%
adamc@244 178 %header (functor UrwebLrValsFn(structure Token : TOKEN))
adamc@1 179
adamc@1 180 %term
adamc@1 181 EOF
adamc@14 182 | STRING of string | INT of Int64.int | FLOAT of Real64.real
adamc@1 183 | SYMBOL of string | CSYMBOL of string
adamc@1 184 | LPAREN | RPAREN | LBRACK | RBRACK | LBRACE | RBRACE
adamc@156 185 | EQ | COMMA | COLON | DCOLON | TCOLON | DOT | HASH | UNDER | UNDERUNDER | BAR
adamc@403 186 | PLUS | MINUS | DIVIDE | DOTDOTDOT | MOD | AT
adamc@623 187 | CON | LTYPE | VAL | REC | AND | FUN | MAP | UNIT | KUNIT | CLASS
adamc@156 188 | DATATYPE | OF
adamc@7 189 | TYPE | NAME
adamc@629 190 | ARROW | LARROW | DARROW | STAR | SEMI | KARROW | DKARROW | BANG
adamc@674 191 | FN | PLUSPLUS | MINUSMINUS | MINUSMINUSMINUS | DOLLAR | TWIDDLE | CARET
adamc@446 192 | LET | IN
adamc@445 193 | STRUCTURE | SIGNATURE | STRUCT | SIG | END | FUNCTOR | WHERE | EXTERN | SQL
adamc@338 194 | INCLUDE | OPEN | CONSTRAINT | CONSTRAINTS | EXPORT | TABLE | SEQUENCE
adamc@459 195 | COOKIE
adamc@190 196 | CASE | IF | THEN | ELSE
adamc@1 197
adamc@360 198 | XML_BEGIN of string | XML_END | XML_BEGIN_END of string
adamc@91 199 | NOTAGS of string
adamc@91 200 | BEGIN_TAG of string | END_TAG of string
adamc@91 201
adamc@230 202 | SELECT | FROM | AS | CWHERE | GROUP | ORDER | BY | HAVING
adamc@229 203 | UNION | INTERSECT | EXCEPT
adamc@232 204 | LIMIT | OFFSET | ALL
adamc@220 205 | TRUE | FALSE | CAND | OR | NOT
adamc@236 206 | COUNT | AVG | SUM | MIN | MAX
adamc@268 207 | ASC | DESC
adamc@470 208 | INSERT | INTO | VALUES | UPDATE | SET | DELETE | NULL | IS
adamc@441 209 | CURRENT_TIMESTAMP
adamc@219 210 | NE | LT | LE | GT | GE
adamc@707 211 | CCONSTRAINT | UNIQUE | PRIMARY | KEY
adamc@204 212
adamc@30 213 %nonterm
adamc@1 214 file of decl list
adamc@1 215 | decls of decl list
adamc@325 216 | decl of decl list
adamc@123 217 | vali of string * con option * exp
adamc@123 218 | valis of (string * con option * exp) list
adamc@242 219 | copt of con option
adamc@1 220
adamc@191 221 | dargs of string list
adamc@156 222 | barOpt of unit
adamc@156 223 | dcons of (string * con option) list
adamc@156 224 | dcon of string * con option
adamc@156 225
adamc@707 226 | pkopt of exp
adamc@707 227 | commaOpt of unit
adamc@707 228
adamc@704 229 | cst of exp
adamc@704 230 | csts of exp
adamc@704 231 | cstopt of exp
adamc@704 232
adamc@30 233 | sgn of sgn
adamc@42 234 | sgntm of sgn
adamc@30 235 | sgi of sgn_item
adamc@30 236 | sgis of sgn_item list
adamc@30 237
adamc@30 238 | str of str
adamc@30 239
adamc@1 240 | kind of kind
adamc@207 241 | ktuple of kind list
adamc@1 242 | kcolon of explicitness
adamc@240 243 | kopt of kind option
adamc@1 244
adamc@34 245 | path of string list * string
adamc@156 246 | cpath of string list * string
adamc@34 247 | spath of str
adamc@59 248 | mpath of string list
adamc@34 249
adamc@1 250 | cexp of con
adamc@1 251 | capps of con
adamc@1 252 | cterm of con
adamc@195 253 | ctuple of con list
adamc@207 254 | ctuplev of con list
adamc@1 255 | ident of con
adamc@200 256 | idents of con list
adamc@1 257 | rcon of (con * con) list
adamc@83 258 | rconn of (con * con) list
adamc@1 259 | rcone of (con * con) list
adamc@239 260 | cargs of con * kind -> con * kind
adamc@239 261 | cargl of con * kind -> con * kind
adamc@240 262 | cargl2 of con * kind -> con * kind
adamc@239 263 | carg of con * kind -> con * kind
adamc@239 264 | cargp of con * kind -> con * kind
adamc@1 265
adamc@8 266 | eexp of exp
adamc@8 267 | eapps of exp
adamc@8 268 | eterm of exp
adamc@195 269 | etuple of exp list
adamc@12 270 | rexp of (con * exp) list
adamc@91 271 | xml of exp
adamc@91 272 | xmlOne of exp
adamc@141 273 | tag of string * exp
adamc@141 274 | tagHead of string * exp
adamc@434 275 | bind of string * con option * exp
adamc@446 276 | edecl of edecl
adamc@446 277 | edecls of edecl list
adamc@8 278
adamc@241 279 | earg of exp * con -> exp * con
adamc@241 280 | eargp of exp * con -> exp * con
adamc@241 281 | eargs of exp * con -> exp * con
adamc@241 282 | eargl of exp * con -> exp * con
adamc@242 283 | eargl2 of exp * con -> exp * con
adamc@241 284
adamc@170 285 | branch of pat * exp
adamc@170 286 | branchs of (pat * exp) list
adamc@170 287 | pat of pat
adamc@170 288 | pterm of pat
adamc@174 289 | rpat of (string * pat) list * bool
adamc@195 290 | ptuple of pat list
adamc@170 291
adamc@104 292 | attrs of (con * exp) list
adamc@104 293 | attr of con * exp
adamc@104 294 | attrv of exp
adamc@104 295
adamc@204 296 | query of exp
adamc@226 297 | query1 of exp
adamc@204 298 | tables of (con * exp) list
adamc@204 299 | tname of con
adamc@705 300 | tnameW of con * con
adamc@705 301 | tnames of (con * con) * (con * con) list
adamc@705 302 | tnames' of (con * con) * (con * con) list
adamc@204 303 | table of con * exp
adamc@207 304 | tident of con
adamc@207 305 | fident of con
adamc@207 306 | seli of select_item
adamc@207 307 | selis of select_item list
adamc@207 308 | select of select
adamc@209 309 | sqlexp of exp
adamc@209 310 | wopt of exp
adamc@226 311 | groupi of group_item
adamc@226 312 | groupis of group_item list
adamc@226 313 | gopt of group_item list option
adamc@227 314 | hopt of exp
adamc@230 315 | obopt of exp
adamc@268 316 | obitem of exp * exp
adamc@230 317 | obexps of exp
adamc@268 318 | diropt of exp
adamc@231 319 | lopt of exp
adamc@232 320 | ofopt of exp
adamc@231 321 | sqlint of exp
adamc@236 322 | sqlagg of string
adamc@207 323
adamc@302 324 | texp of exp
adamc@302 325 | fields of con list
adamc@302 326 | sqlexps of exp list
adamc@303 327 | fsets of (con * exp) list
adamc@310 328 | enterDml of unit
adamc@310 329 | leaveDml of unit
adamc@302 330
adamc@204 331
adamc@1 332 %verbose (* print summary of errors *)
adamc@1 333 %pos int (* positions *)
adamc@1 334 %start file
adamc@1 335 %pure
adamc@1 336 %eop EOF
adamc@1 337 %noshift EOF
adamc@1 338
adamc@244 339 %name Urweb
adamc@1 340
adamc@623 341 %right KARROW
adamc@623 342 %nonassoc DKARROW
adamc@243 343 %right SEMI
adamc@243 344 %nonassoc LARROW
adamc@195 345 %nonassoc IF THEN ELSE
adamc@1 346 %nonassoc DARROW
adamc@1 347 %nonassoc COLON
adamc@6 348 %nonassoc DCOLON TCOLON
adamc@229 349 %left UNION INTERSECT EXCEPT
adamc@1 350 %right COMMA
adamc@220 351 %right OR
adamc@220 352 %right CAND
adamc@470 353 %nonassoc EQ NE LT LE GT GE IS
adamc@243 354 %right ARROW
adamc@674 355 %right CARET PLUSPLUS MINUSMINUS MINUSMINUSMINUS
adamc@389 356 %left PLUS MINUS
adamc@389 357 %left STAR DIVIDE MOD
adamc@220 358 %left NOT
adamc@84 359 %nonassoc TWIDDLE
adamc@1 360 %nonassoc DOLLAR
adamc@1 361 %left DOT
adamc@221 362 %nonassoc LBRACE RBRACE
adamc@1 363
adamc@1 364 %%
adamc@1 365
adamc@1 366 file : decls (decls)
adamc@54 367 | SIG sgis ([(DSgn ("?", (SgnConst sgis, s (SIGleft, sgisright))),
adamc@54 368 s (SIGleft, sgisright))])
adamc@1 369
adamc@1 370 decls : ([])
adamc@325 371 | decl decls (decl @ decls)
adamc@1 372
adamc@240 373 decl : CON SYMBOL cargl2 kopt EQ cexp (let
adamc@240 374 val loc = s (CONleft, cexpright)
adamc@240 375
adamc@240 376 val k = Option.getOpt (kopt, (KWild, loc))
adamc@240 377 val (c, k) = cargl2 (cexp, k)
adamc@240 378 in
adamc@325 379 [(DCon (SYMBOL, SOME k, c), loc)]
adamc@240 380 end)
adamc@325 381 | LTYPE SYMBOL EQ cexp ([(DCon (SYMBOL, SOME (KType, s (LTYPEleft, cexpright)), cexp),
adamc@325 382 s (LTYPEleft, cexpright))])
adamc@325 383 | DATATYPE SYMBOL dargs EQ barOpt dcons([(DDatatype (SYMBOL, dargs, dcons), s (DATATYPEleft, dconsright))])
adamc@191 384 | DATATYPE SYMBOL dargs EQ DATATYPE CSYMBOL DOT path
adamc@191 385 (case dargs of
adamc@325 386 [] => [(DDatatypeImp (SYMBOL, CSYMBOL :: #1 path, #2 path), s (DATATYPEleft, pathright))]
adamc@191 387 | _ => raise Fail "Arguments specified for imported datatype")
adamc@325 388 | VAL vali ([(DVal vali, s (VALleft, valiright))])
adamc@325 389 | VAL REC valis ([(DValRec valis, s (VALleft, valisright))])
adamc@325 390 | FUN valis ([(DValRec valis, s (FUNleft, valisright))])
adamc@1 391
adamc@325 392 | SIGNATURE CSYMBOL EQ sgn ([(DSgn (CSYMBOL, sgn), s (SIGNATUREleft, sgnright))])
adamc@325 393 | STRUCTURE CSYMBOL EQ str ([(DStr (CSYMBOL, NONE, str), s (STRUCTUREleft, strright))])
adamc@325 394 | STRUCTURE CSYMBOL COLON sgn EQ str ([(DStr (CSYMBOL, SOME sgn, str), s (STRUCTUREleft, strright))])
adamc@42 395 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN EQ str
adamc@325 396 ([(DStr (CSYMBOL1, NONE,
adamc@325 397 (StrFun (CSYMBOL2, sgn1, NONE, str), s (FUNCTORleft, strright))),
adamc@325 398 s (FUNCTORleft, strright))])
adamc@42 399 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN COLON sgn EQ str
adamc@325 400 ([(DStr (CSYMBOL1, NONE,
adamc@325 401 (StrFun (CSYMBOL2, sgn1, SOME sgn2, str), s (FUNCTORleft, strright))),
adamc@325 402 s (FUNCTORleft, strright))])
adamc@325 403 | EXTERN STRUCTURE CSYMBOL COLON sgn ([(DFfiStr (CSYMBOL, sgn), s (EXTERNleft, sgnright))])
adamc@61 404 | OPEN mpath (case mpath of
adamc@61 405 [] => raise Fail "Impossible mpath parse [1]"
adamc@325 406 | m :: ms => [(DOpen (m, ms), s (OPENleft, mpathright))])
adamc@325 407 | OPEN mpath LPAREN str RPAREN (let
adamc@325 408 val loc = s (OPENleft, RPARENright)
adamc@325 409
adamc@325 410 val m = case mpath of
adamc@325 411 [] => raise Fail "Impossible mpath parse [4]"
adamc@325 412 | m :: ms =>
adamc@325 413 foldl (fn (m, str) => (StrProj (str, m), loc))
adamc@325 414 (StrVar m, loc) ms
adamc@325 415 in
adamc@325 416 [(DStr ("anon", NONE, (StrApp (m, str), loc)), loc),
adamc@325 417 (DOpen ("anon", []), loc)]
adamc@325 418 end)
adamc@88 419 | OPEN CONSTRAINTS mpath (case mpath of
adamc@88 420 [] => raise Fail "Impossible mpath parse [3]"
adamc@325 421 | m :: ms => [(DOpenConstraints (m, ms), s (OPENleft, mpathright))])
adamc@325 422 | CONSTRAINT cterm TWIDDLE cterm ([(DConstraint (cterm1, cterm2), s (CONSTRAINTleft, ctermright))])
adamc@325 423 | EXPORT spath ([(DExport spath, s (EXPORTleft, spathright))])
adamc@707 424 | TABLE SYMBOL COLON cterm pkopt commaOpt cstopt([(DTable (SYMBOL, entable cterm, pkopt, cstopt),
adamc@707 425 s (TABLEleft, cstoptright))])
adamc@338 426 | SEQUENCE SYMBOL ([(DSequence SYMBOL, s (SEQUENCEleft, SYMBOLright))])
adamc@563 427 | CLASS SYMBOL EQ cexp (let
adamc@563 428 val loc = s (CLASSleft, cexpright)
adamc@563 429 in
adamc@563 430 [(DClass (SYMBOL, (KWild, loc), cexp), loc)]
adamc@563 431 end)
adamc@563 432 | CLASS SYMBOL DCOLON kind EQ cexp ([(DClass (SYMBOL, kind, cexp), s (CLASSleft, cexpright))])
adamc@211 433 | CLASS SYMBOL SYMBOL EQ cexp (let
adamc@211 434 val loc = s (CLASSleft, cexpright)
adamc@563 435 val k = (KWild, loc)
adamc@211 436 val c = (CAbs (SYMBOL2, SOME k, cexp), loc)
adamc@211 437 in
adamc@563 438 [(DClass (SYMBOL1, k, c), s (CLASSleft, cexpright))]
adamc@563 439 end)
adamc@563 440 | CLASS SYMBOL LPAREN SYMBOL DCOLON kind RPAREN EQ cexp (let
adamc@563 441 val loc = s (CLASSleft, cexpright)
adamc@563 442 val c = (CAbs (SYMBOL2, SOME kind, cexp), loc)
adamc@563 443 in
adamc@563 444 [(DClass (SYMBOL1, kind, c), s (CLASSleft, cexpright))]
adamc@211 445 end)
adamc@459 446 | COOKIE SYMBOL COLON cexp ([(DCookie (SYMBOL, cexp), s (COOKIEleft, cexpright))])
adamc@30 447
adamc@240 448 kopt : (NONE)
adamc@240 449 | DCOLON kind (SOME kind)
adamc@240 450
adamc@191 451 dargs : ([])
adamc@191 452 | SYMBOL dargs (SYMBOL :: dargs)
adamc@191 453
adamc@156 454 barOpt : ()
adamc@156 455 | BAR ()
adamc@156 456
adamc@156 457 dcons : dcon ([dcon])
adamc@156 458 | dcon BAR dcons (dcon :: dcons)
adamc@156 459
adamc@156 460 dcon : CSYMBOL (CSYMBOL, NONE)
adamc@156 461 | CSYMBOL OF cexp (CSYMBOL, SOME cexp)
adamc@156 462
adamc@242 463 vali : SYMBOL eargl2 copt EQ eexp (let
adamc@242 464 val loc = s (SYMBOLleft, eexpright)
adamc@242 465 val t = Option.getOpt (copt, (CWild (KType, loc), loc))
adamc@242 466
adamc@242 467 val (e, t) = eargl2 (eexp, t)
adamc@242 468 in
adamc@242 469 (SYMBOL, SOME t, e)
adamc@242 470 end)
adamc@242 471
adamc@242 472 copt : (NONE)
adamc@242 473 | COLON cexp (SOME cexp)
adamc@123 474
adamc@704 475 cstopt : (EVar (["Basis"], "no_constraint", Infer), dummy)
adamc@704 476 | csts (csts)
adamc@704 477
adamc@704 478 csts : CCONSTRAINT tname cst (let
adamc@704 479 val loc = s (CCONSTRAINTleft, cstright)
adamc@704 480
adamc@704 481 val e = (EVar (["Basis"], "one_constraint", Infer), loc)
adamc@704 482 val e = (ECApp (e, tname), loc)
adamc@704 483 in
adamc@704 484 (EApp (e, cst), loc)
adamc@704 485 end)
adamc@704 486 | csts COMMA csts (let
adamc@704 487 val loc = s (csts1left, csts2right)
adamc@704 488
adamc@704 489 val e = (EVar (["Basis"], "join_constraints", Infer), loc)
adamc@704 490 val e = (EApp (e, csts1), loc)
adamc@704 491 in
adamc@704 492 (EApp (e, csts2), loc)
adamc@704 493 end)
adamc@704 494 | LBRACE LBRACE eexp RBRACE RBRACE (eexp)
adamc@704 495
adamc@704 496 cst : UNIQUE tnames (let
adamc@704 497 val loc = s (UNIQUEleft, tnamesright)
adamc@704 498
adamc@704 499 val e = (EVar (["Basis"], "unique", Infer), loc)
adamc@705 500 val e = (ECApp (e, #1 (#1 tnames)), loc)
adamc@705 501 val e = (ECApp (e, (CRecord (#2 tnames), loc)), loc)
adamc@705 502 val e = (EDisjointApp e, loc)
adamc@704 503 in
adamc@704 504 (EDisjointApp e, loc)
adamc@704 505 end)
adamc@704 506 | LBRACE eexp RBRACE (eexp)
adamc@704 507
adamc@704 508 tnameW : tname (let
adamc@704 509 val loc = s (tnameleft, tnameright)
adamc@704 510 in
adamc@704 511 (tname, (CWild (KType, loc), loc))
adamc@704 512 end)
adamc@704 513
adamc@705 514 tnames : tnameW (tnameW, [])
adamc@705 515 | LPAREN tnames' RPAREN (tnames')
adamc@704 516
adamc@705 517 tnames': tnameW (tnameW, [])
adamc@705 518 | tnameW COMMA tnames' (#1 tnames', tnameW :: #2 tnames')
adamc@704 519
adamc@707 520 commaOpt: ()
adamc@707 521 | COMMA ()
adamc@707 522
adamc@707 523 pkopt : (EVar (["Basis"], "no_primary_key", Infer), ErrorMsg.dummySpan)
adamc@707 524 | PRIMARY KEY tnames (let
adamc@707 525 val loc = s (PRIMARYleft, tnamesright)
adamc@707 526
adamc@707 527 val e = (EVar (["Basis"], "primary_key", Infer), loc)
adamc@707 528 val e = (ECApp (e, #1 (#1 tnames)), loc)
adamc@707 529 val e = (ECApp (e, (CRecord (#2 tnames), loc)), loc)
adamc@707 530 val e = (EDisjointApp e, loc)
adamc@707 531 val e = (EDisjointApp e, loc)
adamc@707 532
adamc@707 533 val witness = map (fn (c, _) =>
adamc@707 534 (c, (EWild, loc)))
adamc@707 535 (#1 tnames :: #2 tnames)
adamc@707 536 val witness = (ERecord witness, loc)
adamc@707 537 in
adamc@707 538 (EApp (e, witness), loc)
adamc@707 539 end)
adamc@707 540
adamc@123 541 valis : vali ([vali])
adamc@123 542 | vali AND valis (vali :: valis)
adamc@123 543
adamc@42 544 sgn : sgntm (sgntm)
adamc@40 545 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN COLON sgn
adamc@40 546 (SgnFun (CSYMBOL, sgn1, sgn2), s (FUNCTORleft, sgn2right))
adamc@30 547
adamc@42 548 sgntm : SIG sgis END (SgnConst sgis, s (SIGleft, ENDright))
adamc@59 549 | mpath (case mpath of
adamc@61 550 [] => raise Fail "Impossible mpath parse [2]"
adamc@59 551 | [x] => SgnVar x
adamc@59 552 | m :: ms => SgnProj (m,
adamc@59 553 List.take (ms, length ms - 1),
adamc@59 554 List.nth (ms, length ms - 1)),
adamc@59 555 s (mpathleft, mpathright))
adamc@42 556 | sgntm WHERE CON SYMBOL EQ cexp (SgnWhere (sgntm, SYMBOL, cexp), s (sgntmleft, cexpright))
adamc@42 557 | sgntm WHERE LTYPE SYMBOL EQ cexp(SgnWhere (sgntm, SYMBOL, cexp), s (sgntmleft, cexpright))
adamc@42 558 | LPAREN sgn RPAREN (sgn)
adamc@42 559
adamc@706 560 sgi : CON SYMBOL DCOLON kind ((SgiConAbs (SYMBOL, kind), s (CONleft, kindright)))
adamc@706 561 | LTYPE SYMBOL ((SgiConAbs (SYMBOL, (KType, s (LTYPEleft, SYMBOLright))),
adamc@706 562 s (LTYPEleft, SYMBOLright)))
adamc@706 563 | CON SYMBOL EQ cexp ((SgiCon (SYMBOL, NONE, cexp), s (CONleft, cexpright)))
adamc@706 564 | CON SYMBOL DCOLON kind EQ cexp ((SgiCon (SYMBOL, SOME kind, cexp), s (CONleft, cexpright)))
adamc@706 565 | LTYPE SYMBOL EQ cexp ((SgiCon (SYMBOL, SOME (KType, s (LTYPEleft, cexpright)), cexp),
adamc@706 566 s (LTYPEleft, cexpright)))
adamc@706 567 | DATATYPE SYMBOL dargs EQ barOpt dcons((SgiDatatype (SYMBOL, dargs, dcons), s (DATATYPEleft, dconsright)))
adamc@191 568 | DATATYPE SYMBOL dargs EQ DATATYPE CSYMBOL DOT path
adamc@191 569 (case dargs of
adamc@191 570 [] => (SgiDatatypeImp (SYMBOL, CSYMBOL :: #1 path, #2 path), s (DATATYPEleft, pathright))
adamc@191 571 | _ => raise Fail "Arguments specified for imported datatype")
adamc@706 572 | VAL SYMBOL COLON cexp ((SgiVal (SYMBOL, cexp), s (VALleft, cexpright)))
adamc@30 573
adamc@706 574 | STRUCTURE CSYMBOL COLON sgn ((SgiStr (CSYMBOL, sgn), s (STRUCTUREleft, sgnright)))
adamc@706 575 | SIGNATURE CSYMBOL EQ sgn ((SgiSgn (CSYMBOL, sgn), s (SIGNATUREleft, sgnright)))
adamc@42 576 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN COLON sgn
adamc@706 577 ((SgiStr (CSYMBOL1,
adamc@706 578 (SgnFun (CSYMBOL2, sgn1, sgn2), s (FUNCTORleft, sgn2right))),
adamc@706 579 s (FUNCTORleft, sgn2right)))
adamc@706 580 | INCLUDE sgn ((SgiInclude sgn, s (INCLUDEleft, sgnright)))
adamc@706 581 | CONSTRAINT cterm TWIDDLE cterm ((SgiConstraint (cterm1, cterm2), s (CONSTRAINTleft, ctermright)))
adamc@707 582 | TABLE SYMBOL COLON cterm pkopt commaOpt cstopt (let
adamc@707 583 val loc = s (TABLEleft, ctermright)
adamc@707 584 in
adamc@707 585 (SgiTable (SYMBOL, entable cterm, pkopt, cstopt), loc)
adamc@707 586 end)
adamc@460 587 | SEQUENCE SYMBOL (let
adamc@460 588 val loc = s (SEQUENCEleft, SYMBOLright)
adamc@460 589 val t = (CVar (["Basis"], "sql_sequence"), loc)
adamc@460 590 in
adamc@460 591 (SgiVal (SYMBOL, t), loc)
adamc@460 592 end)
adamc@563 593 | CLASS SYMBOL (let
adamc@563 594 val loc = s (CLASSleft, SYMBOLright)
adamc@563 595 in
adamc@563 596 (SgiClassAbs (SYMBOL, (KWild, loc)), loc)
adamc@563 597 end)
adamc@563 598 | CLASS SYMBOL DCOLON kind (let
adamc@563 599 val loc = s (CLASSleft, kindright)
adamc@563 600 in
adamc@563 601 (SgiClassAbs (SYMBOL, kind), loc)
adamc@563 602 end)
adamc@563 603 | CLASS SYMBOL EQ cexp (let
adamc@563 604 val loc = s (CLASSleft, cexpright)
adamc@563 605 in
adamc@563 606 (SgiClass (SYMBOL, (KWild, loc), cexp), loc)
adamc@563 607 end)
adamc@563 608 | CLASS SYMBOL DCOLON kind EQ cexp (let
adamc@563 609 val loc = s (CLASSleft, cexpright)
adamc@563 610 in
adamc@563 611 (SgiClass (SYMBOL, kind, cexp), loc)
adamc@563 612 end)
adamc@211 613 | CLASS SYMBOL SYMBOL EQ cexp (let
adamc@211 614 val loc = s (CLASSleft, cexpright)
adamc@563 615 val k = (KWild, loc)
adamc@211 616 val c = (CAbs (SYMBOL2, SOME k, cexp), loc)
adamc@211 617 in
adamc@563 618 (SgiClass (SYMBOL1, k, c), s (CLASSleft, cexpright))
adamc@563 619 end)
adamc@563 620 | CLASS SYMBOL LPAREN SYMBOL DCOLON kind RPAREN EQ cexp (let
adamc@563 621 val loc = s (CLASSleft, cexpright)
adamc@563 622 val c = (CAbs (SYMBOL2, SOME kind, cexp), loc)
adamc@563 623 in
adamc@563 624 (SgiClass (SYMBOL1, kind, c), s (CLASSleft, cexpright))
adamc@211 625 end)
adamc@460 626 | COOKIE SYMBOL COLON cexp (let
adamc@460 627 val loc = s (COOKIEleft, cexpright)
adamc@460 628 val t = (CApp ((CVar (["Basis"], "http_cookie"), loc),
adamc@460 629 entable cexp), loc)
adamc@460 630 in
adamc@460 631 (SgiVal (SYMBOL, t), loc)
adamc@460 632 end)
adamc@30 633
adamc@30 634 sgis : ([])
adamc@30 635 | sgi sgis (sgi :: sgis)
adamc@30 636
adamc@30 637 str : STRUCT decls END (StrConst decls, s (STRUCTleft, ENDright))
adamc@34 638 | spath (spath)
adamc@40 639 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN DARROW str
adamc@40 640 (StrFun (CSYMBOL, sgn, NONE, str), s (FUNCTORleft, strright))
adamc@40 641 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN COLON sgn DARROW str
adamc@40 642 (StrFun (CSYMBOL, sgn1, SOME sgn2, str), s (FUNCTORleft, strright))
adamc@44 643 | spath LPAREN str RPAREN (StrApp (spath, str), s (spathleft, RPARENright))
adamc@34 644
adamc@34 645 spath : CSYMBOL (StrVar CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@34 646 | spath DOT CSYMBOL (StrProj (spath, CSYMBOL), s (spathleft, CSYMBOLright))
adamc@30 647
adamc@1 648 kind : TYPE (KType, s (TYPEleft, TYPEright))
adamc@1 649 | NAME (KName, s (NAMEleft, NAMEright))
adamc@1 650 | LBRACE kind RBRACE (KRecord kind, s (LBRACEleft, RBRACEright))
adamc@1 651 | kind ARROW kind (KArrow (kind1, kind2), s (kind1left, kind2right))
adamc@1 652 | LPAREN kind RPAREN (#1 kind, s (LPARENleft, RPARENright))
adamc@82 653 | KUNIT (KUnit, s (KUNITleft, KUNITright))
adamc@18 654 | UNDERUNDER (KWild, s (UNDERUNDERleft, UNDERUNDERright))
adamc@207 655 | LPAREN ktuple RPAREN (KTuple ktuple, s (LPARENleft, RPARENright))
adamc@623 656 | CSYMBOL (KVar CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@623 657 | CSYMBOL KARROW kind (KFun (CSYMBOL, kind), s (CSYMBOLleft, kindright))
adamc@207 658
adamc@207 659 ktuple : kind STAR kind ([kind1, kind2])
adamc@207 660 | kind STAR ktuple (kind :: ktuple)
adamc@1 661
adamc@1 662 capps : cterm (cterm)
adamc@1 663 | capps cterm (CApp (capps, cterm), s (cappsleft, ctermright))
adamc@1 664
adamc@1 665 cexp : capps (capps)
adamc@1 666 | cexp ARROW cexp (TFun (cexp1, cexp2), s (cexp1left, cexp2right))
adamc@15 667 | SYMBOL kcolon kind ARROW cexp (TCFun (kcolon, SYMBOL, kind, cexp), s (SYMBOLleft, cexpright))
adamc@623 668 | CSYMBOL KARROW cexp (TKFun (CSYMBOL, cexp), s (CSYMBOLleft, cexpright))
adamc@1 669
adamc@1 670 | cexp PLUSPLUS cexp (CConcat (cexp1, cexp2), s (cexp1left, cexp1right))
adamc@1 671
adamc@239 672 | FN cargs DARROW cexp (#1 (cargs (cexp, (KWild, s (FNleft, cexpright)))))
adamc@628 673 | LBRACK cexp TWIDDLE cexp RBRACK DARROW cexp (TDisjoint (cexp1, cexp2, cexp3), s (LBRACKleft, cexp3right))
adamc@623 674 | CSYMBOL DKARROW cexp (CKAbs (CSYMBOL, cexp), s (CSYMBOLleft, cexpright))
adamc@1 675
adamc@8 676 | LPAREN cexp RPAREN DCOLON kind (CAnnot (cexp, kind), s (LPARENleft, kindright))
adamc@6 677
adamc@18 678 | UNDER DCOLON kind (CWild kind, s (UNDERleft, UNDERright))
adamc@195 679 | ctuple (let
adamc@195 680 val loc = s (ctupleleft, ctupleright)
adamc@195 681 in
adamc@195 682 (TRecord (CRecord (ListUtil.mapi (fn (i, c) =>
adamc@195 683 ((CName (Int.toString (i + 1)), loc),
adamc@195 684 c)) ctuple),
adamc@195 685 loc), loc)
adamc@195 686 end)
adamc@18 687
adamc@1 688 kcolon : DCOLON (Explicit)
adamc@1 689 | TCOLON (Implicit)
adamc@1 690
adamc@239 691 cargs : carg (carg)
adamc@239 692 | cargl (cargl)
adamc@239 693
adamc@239 694 cargl : cargp cargp (cargp1 o cargp2)
adamc@239 695 | cargp cargl (cargp o cargl)
adamc@239 696
adamc@240 697 cargl2 : (fn x => x)
adamc@240 698 | cargp cargl2 (cargp o cargl2)
adamc@240 699
adamc@241 700 carg : SYMBOL DCOLON kind (fn (c, k) =>
adamc@239 701 let
adamc@239 702 val loc = s (SYMBOLleft, kindright)
adamc@239 703 in
adamc@239 704 ((CAbs (SYMBOL, SOME kind, c), loc),
adamc@239 705 (KArrow (kind, k), loc))
adamc@239 706 end)
adamc@329 707 | UNDER DCOLON kind (fn (c, k) =>
adamc@329 708 let
adamc@329 709 val loc = s (UNDERleft, kindright)
adamc@329 710 in
adamc@329 711 ((CAbs ("_", SOME kind, c), loc),
adamc@329 712 (KArrow (kind, k), loc))
adamc@329 713 end)
adamc@241 714 | cargp (cargp)
adamc@239 715
adamc@239 716 cargp : SYMBOL (fn (c, k) =>
adamc@239 717 let
adamc@239 718 val loc = s (SYMBOLleft, SYMBOLright)
adamc@239 719 in
adamc@239 720 ((CAbs (SYMBOL, NONE, c), loc),
adamc@239 721 (KArrow ((KWild, loc), k), loc))
adamc@239 722 end)
adamc@329 723 | UNDER (fn (c, k) =>
adamc@329 724 let
adamc@329 725 val loc = s (UNDERleft, UNDERright)
adamc@329 726 in
adamc@329 727 ((CAbs ("_", NONE, c), loc),
adamc@329 728 (KArrow ((KWild, loc), k), loc))
adamc@329 729 end)
adamc@239 730 | LPAREN SYMBOL DCOLON kind RPAREN (fn (c, k) =>
adamc@239 731 let
adamc@239 732 val loc = s (LPARENleft, RPARENright)
adamc@239 733 in
adamc@239 734 ((CAbs (SYMBOL, SOME kind, c), loc),
adamc@239 735 (KArrow (kind, k), loc))
adamc@239 736 end)
adamc@628 737
adamc@239 738
adamc@34 739 path : SYMBOL ([], SYMBOL)
adamc@34 740 | CSYMBOL DOT path (let val (ms, x) = path in (CSYMBOL :: ms, x) end)
adamc@34 741
adamc@156 742 cpath : CSYMBOL ([], CSYMBOL)
adamc@156 743 | CSYMBOL DOT cpath (let val (ms, x) = cpath in (CSYMBOL :: ms, x) end)
adamc@156 744
adamc@59 745 mpath : CSYMBOL ([CSYMBOL])
adamc@59 746 | CSYMBOL DOT mpath (CSYMBOL :: mpath)
adamc@59 747
adamc@1 748 cterm : LPAREN cexp RPAREN (#1 cexp, s (LPARENleft, RPARENright))
adamc@1 749 | LBRACK rcon RBRACK (CRecord rcon, s (LBRACKleft, RBRACKright))
adamc@83 750 | LBRACK rconn RBRACK (CRecord rconn, s (LBRACKleft, RBRACKright))
adamc@1 751 | LBRACE rcone RBRACE (TRecord (CRecord rcone, s (LBRACEleft, RBRACEright)),
adamc@1 752 s (LBRACEleft, RBRACEright))
adamc@1 753 | DOLLAR cterm (TRecord cterm, s (DOLLARleft, ctermright))
adamc@1 754 | HASH CSYMBOL (CName CSYMBOL, s (HASHleft, CSYMBOLright))
adamc@195 755 | HASH INT (CName (Int64.toString INT), s (HASHleft, INTright))
adamc@1 756
adamc@34 757 | path (CVar path, s (pathleft, pathright))
adamc@207 758 | path DOT INT (CProj ((CVar path, s (pathleft, pathright)), Int64.toInt INT),
adamc@207 759 s (pathleft, INTright))
adamc@18 760 | UNDER (CWild (KWild, s (UNDERleft, UNDERright)), s (UNDERleft, UNDERright))
adamc@621 761 | MAP (CMap, s (MAPleft, MAPright))
adamc@82 762 | UNIT (CUnit, s (UNITleft, UNITright))
adamc@207 763 | LPAREN ctuplev RPAREN (CTuple ctuplev, s (LPARENleft, RPARENright))
adamc@207 764
adamc@207 765 ctuplev: cexp COMMA cexp ([cexp1, cexp2])
adamc@207 766 | cexp COMMA ctuplev (cexp :: ctuplev)
adamc@1 767
adamc@196 768 ctuple : capps STAR capps ([capps1, capps2])
adamc@196 769 | capps STAR ctuple (capps :: ctuple)
adamc@195 770
adamc@1 771 rcon : ([])
adamc@1 772 | ident EQ cexp ([(ident, cexp)])
adamc@1 773 | ident EQ cexp COMMA rcon ((ident, cexp) :: rcon)
adamc@1 774
adamc@83 775 rconn : ident ([(ident, (CUnit, s (identleft, identright)))])
adamc@83 776 | ident COMMA rconn ((ident, (CUnit, s (identleft, identright))) :: rconn)
adamc@83 777
adamc@1 778 rcone : ([])
adamc@1 779 | ident COLON cexp ([(ident, cexp)])
adamc@1 780 | ident COLON cexp COMMA rcone ((ident, cexp) :: rcone)
adamc@1 781
adamc@1 782 ident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@195 783 | INT (CName (Int64.toString INT), s (INTleft, INTright))
adamc@200 784 | SYMBOL (CVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright))
adamc@8 785
adamc@8 786 eapps : eterm (eterm)
adamc@8 787 | eapps eterm (EApp (eapps, eterm), s (eappsleft, etermright))
adamc@8 788 | eapps LBRACK cexp RBRACK (ECApp (eapps, cexp), s (eappsleft, RBRACKright))
adamc@629 789 | eapps BANG (EDisjointApp eapps, s (eappsleft, BANGright))
adamc@8 790
adamc@8 791 eexp : eapps (eapps)
adamc@241 792 | FN eargs DARROW eexp (let
adamc@93 793 val loc = s (FNleft, eexpright)
adamc@93 794 in
adamc@241 795 #1 (eargs (eexp, (CWild (KType, loc), loc)))
adamc@93 796 end)
adamc@623 797 | CSYMBOL DKARROW eexp (EKAbs (CSYMBOL, eexp), s (CSYMBOLleft, eexpright))
adamc@196 798 | eexp COLON cexp (EAnnot (eexp, cexp), s (eexpleft, cexpright))
adamc@149 799 | eexp MINUSMINUS cexp (ECut (eexp, cexp), s (eexpleft, cexpright))
adamc@493 800 | eexp MINUSMINUSMINUS cexp (ECutMulti (eexp, cexp), s (eexpleft, cexpright))
adamc@170 801 | CASE eexp OF barOpt branch branchs (ECase (eexp, branch :: branchs), s (CASEleft, branchsright))
adamc@190 802 | IF eexp THEN eexp ELSE eexp (let
adamc@190 803 val loc = s (IFleft, eexp3right)
adamc@190 804 in
adamc@190 805 (ECase (eexp1, [((PCon (["Basis"], "True", NONE), loc), eexp2),
adamc@190 806 ((PCon (["Basis"], "False", NONE), loc), eexp3)]), loc)
adamc@190 807 end)
adamc@434 808 | bind SEMI eexp (let
adamc@434 809 val loc = s (bindleft, eexpright)
adamc@434 810 val (v, to, e1) = bind
adamc@403 811 val e = (EVar (["Basis"], "bind", Infer), loc)
adamc@434 812 val e = (EApp (e, e1), loc)
adamc@243 813 in
adamc@434 814 (EApp (e, (EAbs (v, to, eexp), loc)), loc)
adamc@299 815 end)
adamc@256 816 | eexp EQ eexp (native_op ("eq", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@257 817 | eexp NE eexp (native_op ("ne", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@389 818 | MINUS eterm (native_unop ("neg", eterm, s (MINUSleft, etermright)))
adamc@389 819 | eexp PLUS eexp (native_op ("plus", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@389 820 | eexp MINUS eexp (native_op ("minus", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@389 821 | eterm STAR eexp (native_op ("times", eterm, eexp, s (etermleft, eexpright)))
adamc@389 822 | eexp DIVIDE eexp (native_op ("div", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@389 823 | eexp MOD eexp (native_op ("mod", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@391 824
adamc@391 825 | eexp LT eexp (native_op ("lt", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@391 826 | eexp LE eexp (native_op ("le", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@391 827 | eexp GT eexp (native_op ("gt", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@391 828 | eexp GE eexp (native_op ("ge", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@391 829
adamc@445 830 | eexp PLUSPLUS eexp (EConcat (eexp1, eexp2), s (eexp1left, eexp2right))
adamc@8 831
adamc@674 832 | eexp CARET eexp (native_op ("strcat", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@674 833
adamc@434 834 bind : SYMBOL LARROW eapps (SYMBOL, NONE, eapps)
adamc@434 835 | UNIT LARROW eapps (let
adamc@434 836 val loc = s (UNITleft, eappsright)
adamc@434 837 in
adamc@434 838 ("_", SOME (TRecord (CRecord [], loc), loc), eapps)
adamc@434 839 end)
adamc@434 840 | eapps (let
adamc@434 841 val loc = s (eappsleft, eappsright)
adamc@434 842 in
adamc@434 843 ("_", SOME (TRecord (CRecord [], loc), loc), eapps)
adamc@434 844 end)
adamc@434 845
adamc@241 846 eargs : earg (earg)
adamc@241 847 | eargl (eargl)
adamc@241 848
adamc@241 849 eargl : eargp eargp (eargp1 o eargp2)
adamc@241 850 | eargp eargl (eargp o eargl)
adamc@241 851
adamc@242 852 eargl2 : (fn x => x)
adamc@242 853 | eargp eargl2 (eargp o eargl2)
adamc@242 854
adamc@241 855 earg : SYMBOL kcolon kind (fn (e, t) =>
adamc@241 856 let
adamc@241 857 val loc = s (SYMBOLleft, kindright)
adamc@241 858 in
adamc@241 859 ((ECAbs (kcolon, SYMBOL, kind, e), loc),
adamc@241 860 (TCFun (kcolon, SYMBOL, kind, t), loc))
adamc@241 861 end)
adamc@241 862 | SYMBOL COLON cexp (fn (e, t) =>
adamc@241 863 let
adamc@241 864 val loc = s (SYMBOLleft, cexpright)
adamc@241 865 in
adamc@241 866 ((EAbs (SYMBOL, SOME cexp, e), loc),
adamc@241 867 (TFun (cexp, t), loc))
adamc@241 868 end)
adamc@241 869 | UNDER COLON cexp (fn (e, t) =>
adamc@241 870 let
adamc@241 871 val loc = s (UNDERleft, cexpright)
adamc@241 872 in
adamc@241 873 ((EAbs ("_", SOME cexp, e), loc),
adamc@241 874 (TFun (cexp, t), loc))
adamc@241 875 end)
adamc@241 876 | eargp (eargp)
adamc@241 877
adamc@241 878 eargp : SYMBOL (fn (e, t) =>
adamc@241 879 let
adamc@241 880 val loc = s (SYMBOLleft, SYMBOLright)
adamc@241 881 in
adamc@241 882 ((EAbs (SYMBOL, NONE, e), loc),
adamc@241 883 (TFun ((CWild (KType, loc), loc), t), loc))
adamc@241 884 end)
adamc@241 885 | UNIT (fn (e, t) =>
adamc@241 886 let
adamc@241 887 val loc = s (UNITleft, UNITright)
adamc@241 888 val t' = (TRecord (CRecord [], loc), loc)
adamc@241 889 in
adamc@241 890 ((EAbs ("_", SOME t', e), loc),
adamc@241 891 (TFun (t', t), loc))
adamc@241 892 end)
adamc@241 893 | UNDER (fn (e, t) =>
adamc@241 894 let
adamc@241 895 val loc = s (UNDERleft, UNDERright)
adamc@241 896 in
adamc@241 897 ((EAbs ("_", NONE, e), loc),
adamc@241 898 (TFun ((CWild (KType, loc), loc), t), loc))
adamc@241 899 end)
adamc@241 900 | LPAREN SYMBOL kcolon kind RPAREN(fn (e, t) =>
adamc@241 901 let
adamc@241 902 val loc = s (LPARENleft, RPARENright)
adamc@241 903 in
adamc@241 904 ((ECAbs (kcolon, SYMBOL, kind, e), loc),
adamc@241 905 (TCFun (kcolon, SYMBOL, kind, t), loc))
adamc@241 906 end)
adamc@241 907 | LPAREN SYMBOL COLON cexp RPAREN (fn (e, t) =>
adamc@241 908 let
adamc@241 909 val loc = s (LPARENleft, RPARENright)
adamc@241 910 in
adamc@241 911 ((EAbs (SYMBOL, SOME cexp, e), loc),
adamc@241 912 (TFun (cexp, t), loc))
adamc@241 913 end)
adamc@241 914 | LPAREN UNDER COLON cexp RPAREN (fn (e, t) =>
adamc@241 915 let
adamc@241 916 val loc = s (LPARENleft, RPARENright)
adamc@241 917 in
adamc@241 918 ((EAbs ("_", SOME cexp, e), loc),
adamc@241 919 (TFun (cexp, t), loc))
adamc@241 920 end)
adamc@478 921 | LPAREN cexp TWIDDLE cexp RPAREN (fn (e, t) =>
adamc@354 922 let
adamc@354 923 val loc = s (LPARENleft, RPARENright)
adamc@354 924 in
adamc@478 925 ((EDisjoint (cexp1, cexp2, e), loc),
adamc@628 926 (TDisjoint (cexp1, cexp2, t), loc))
adamc@354 927 end)
adamc@478 928 | LBRACK cexp TWIDDLE cexp RBRACK(fn (e, t) =>
adamc@356 929 let
adamc@356 930 val loc = s (LBRACKleft, RBRACKright)
adamc@356 931 in
adamc@478 932 ((EDisjoint (cexp1, cexp2, e), loc),
adamc@628 933 (TDisjoint (cexp1, cexp2, t), loc))
adamc@356 934 end)
adamc@623 935 | CSYMBOL (fn (e, t) =>
adamc@623 936 let
adamc@623 937 val loc = s (CSYMBOLleft, CSYMBOLright)
adamc@623 938 in
adamc@623 939 ((EKAbs (CSYMBOL, e), loc),
adamc@623 940 (TKFun (CSYMBOL, t), loc))
adamc@623 941 end)
adamc@241 942
adamc@8 943 eterm : LPAREN eexp RPAREN (#1 eexp, s (LPARENleft, RPARENright))
adamc@195 944 | LPAREN etuple RPAREN (let
adamc@195 945 val loc = s (LPARENleft, RPARENright)
adamc@195 946 in
adamc@195 947 (ERecord (ListUtil.mapi (fn (i, e) =>
adamc@195 948 ((CName (Int.toString (i + 1)), loc),
adamc@195 949 e)) etuple), loc)
adamc@195 950 end)
adamc@8 951
adamc@403 952 | path (EVar (#1 path, #2 path, Infer), s (pathleft, pathright))
adamc@403 953 | cpath (EVar (#1 cpath, #2 cpath, Infer), s (cpathleft, cpathright))
adamc@403 954 | AT path (EVar (#1 path, #2 path, TypesOnly), s (ATleft, pathright))
adamc@403 955 | AT AT path (EVar (#1 path, #2 path, DontInfer), s (AT1left, pathright))
adamc@403 956 | AT cpath (EVar (#1 cpath, #2 cpath, TypesOnly), s (ATleft, cpathright))
adamc@403 957 | AT AT cpath (EVar (#1 cpath, #2 cpath, DontInfer), s (AT1left, cpathright))
adamc@12 958 | LBRACE rexp RBRACE (ERecord rexp, s (LBRACEleft, RBRACEright))
adamc@110 959 | UNIT (ERecord [], s (UNITleft, UNITright))
adamc@12 960
adamc@14 961 | INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@14 962 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@14 963 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@14 964
adamc@200 965 | path DOT idents (let
adamc@200 966 val loc = s (pathleft, identsright)
adamc@200 967 in
adamc@200 968 foldl (fn (ident, e) =>
adamc@200 969 (EField (e, ident), loc))
adamc@403 970 (EVar (#1 path, #2 path, Infer), s (pathleft, pathright)) idents
adamc@403 971 end)
adamc@403 972 | AT path DOT idents (let
adamc@403 973 val loc = s (ATleft, identsright)
adamc@403 974 in
adamc@403 975 foldl (fn (ident, e) =>
adamc@403 976 (EField (e, ident), loc))
adamc@403 977 (EVar (#1 path, #2 path, TypesOnly), s (pathleft, pathright)) idents
adamc@403 978 end)
adamc@403 979 | AT AT path DOT idents (let
adamc@403 980 val loc = s (AT1left, identsright)
adamc@403 981 in
adamc@403 982 foldl (fn (ident, e) =>
adamc@403 983 (EField (e, ident), loc))
adamc@403 984 (EVar (#1 path, #2 path, DontInfer), s (pathleft, pathright)) idents
adamc@200 985 end)
adamc@71 986
adamc@360 987 | XML_BEGIN xml XML_END (let
adamc@360 988 val loc = s (XML_BEGINleft, XML_ENDright)
adamc@360 989 in
adamc@360 990 if XML_BEGIN = "xml" then
adamc@360 991 ()
adamc@360 992 else
adamc@360 993 ErrorMsg.errorAt loc "Initial XML tag pair aren't both tagged \"xml\".";
adamc@360 994 xml
adamc@360 995 end)
adamc@360 996 | XML_BEGIN XML_END (let
adamc@360 997 val loc = s (XML_BEGINleft, XML_ENDright)
adamc@360 998 in
adamc@360 999 if XML_BEGIN = "xml" then
adamc@360 1000 ()
adamc@360 1001 else
adamc@360 1002 ErrorMsg.errorAt loc "Initial XML tag pair aren't both tagged \"xml\".";
adamc@403 1003 (EApp ((EVar (["Basis"], "cdata", Infer), loc),
adamc@360 1004 (EPrim (Prim.String ""), loc)),
adamc@360 1005 loc)
adamc@360 1006 end)
adamc@360 1007 | XML_BEGIN_END (let
adamc@360 1008 val loc = s (XML_BEGIN_ENDleft, XML_BEGIN_ENDright)
adamc@360 1009 in
adamc@360 1010 if XML_BEGIN_END = "xml" then
adamc@360 1011 ()
adamc@360 1012 else
adamc@360 1013 ErrorMsg.errorAt loc "Initial XML tag pair aren't both tagged \"xml\".";
adamc@403 1014 (EApp ((EVar (["Basis"], "cdata", Infer), loc),
adamc@360 1015 (EPrim (Prim.String ""), loc)),
adamc@360 1016 loc)
adamc@360 1017 end)
adamc@302 1018
adamc@204 1019 | LPAREN query RPAREN (query)
adamc@300 1020 | LPAREN CWHERE sqlexp RPAREN (sqlexp)
adamc@339 1021 | LPAREN SQL sqlexp RPAREN (sqlexp)
adamc@302 1022
adamc@302 1023 | LPAREN INSERT INTO texp LPAREN fields RPAREN VALUES LPAREN sqlexps RPAREN RPAREN
adamc@302 1024 (let
adamc@302 1025 val loc = s (LPAREN1left, RPAREN3right)
adamc@302 1026
adamc@403 1027 val e = (EVar (["Basis"], "insert", Infer), loc)
adamc@302 1028 val e = (EApp (e, texp), loc)
adamc@302 1029 in
adamc@302 1030 if length fields <> length sqlexps then
adamc@302 1031 ErrorMsg.errorAt loc "Length mismatch in INSERT field specification"
adamc@302 1032 else
adamc@302 1033 ();
adamc@302 1034 (EApp (e, (ERecord (ListPair.zip (fields, sqlexps)), loc)), loc)
adamc@302 1035 end)
adamc@310 1036 | LPAREN enterDml UPDATE texp SET fsets CWHERE sqlexp leaveDml RPAREN
adamc@303 1037 (let
adamc@303 1038 val loc = s (LPARENleft, RPARENright)
adamc@303 1039
adamc@403 1040 val e = (EVar (["Basis"], "update", Infer), loc)
adamc@342 1041 val e = (ECApp (e, (CWild (KRecord (KType, loc), loc), loc)), loc)
adamc@632 1042 val e = (EDisjointApp e, loc)
adamc@306 1043 val e = (EApp (e, (ERecord fsets, loc)), loc)
adamc@305 1044 val e = (EApp (e, texp), loc)
adamc@303 1045 in
adamc@303 1046 (EApp (e, sqlexp), loc)
adamc@303 1047 end)
adamc@310 1048 | LPAREN enterDml DELETE FROM texp CWHERE sqlexp leaveDml RPAREN
adamc@304 1049 (let
adamc@304 1050 val loc = s (LPARENleft, RPARENright)
adamc@304 1051
adamc@403 1052 val e = (EVar (["Basis"], "delete", Infer), loc)
adamc@304 1053 val e = (EApp (e, texp), loc)
adamc@304 1054 in
adamc@304 1055 (EApp (e, sqlexp), loc)
adamc@304 1056 end)
adamc@302 1057
adamc@211 1058 | UNDER (EWild, s (UNDERleft, UNDERright))
adamc@91 1059
adamc@446 1060 | LET edecls IN eexp END (ELet (edecls, eexp), s (LETleft, ENDright))
adamc@446 1061
adamc@446 1062 edecls : ([])
adamc@446 1063 | edecl edecls (edecl :: edecls)
adamc@446 1064
adamc@446 1065 edecl : VAL vali ((EDVal vali, s (VALleft, valiright)))
adamc@446 1066 | VAL REC valis ((EDValRec valis, s (VALleft, valisright)))
adamc@446 1067 | FUN valis ((EDValRec valis, s (FUNleft, valisright)))
adamc@446 1068
adamc@310 1069 enterDml : (inDml := true)
adamc@310 1070 leaveDml : (inDml := false)
adamc@310 1071
adamc@403 1072 texp : SYMBOL (EVar ([], SYMBOL, Infer), s (SYMBOLleft, SYMBOLright))
adamc@302 1073 | LBRACE LBRACE eexp RBRACE RBRACE (eexp)
adamc@302 1074
adamc@302 1075 fields : fident ([fident])
adamc@302 1076 | fident COMMA fields (fident :: fields)
adamc@302 1077
adamc@302 1078 sqlexps: sqlexp ([sqlexp])
adamc@302 1079 | sqlexp COMMA sqlexps (sqlexp :: sqlexps)
adamc@302 1080
adamc@303 1081 fsets : fident EQ sqlexp ([(fident, sqlexp)])
adamc@303 1082 | fident EQ sqlexp COMMA fsets ((fident, sqlexp) :: fsets)
adamc@303 1083
adamc@200 1084 idents : ident ([ident])
adamc@200 1085 | ident DOT idents (ident :: idents)
adamc@200 1086
adamc@195 1087 etuple : eexp COMMA eexp ([eexp1, eexp2])
adamc@195 1088 | eexp COMMA etuple (eexp :: etuple)
adamc@195 1089
adamc@170 1090 branch : pat DARROW eexp (pat, eexp)
adamc@170 1091
adamc@170 1092 branchs: ([])
adamc@170 1093 | BAR branch branchs (branch :: branchs)
adamc@170 1094
adamc@170 1095 pat : pterm (pterm)
adamc@170 1096 | cpath pterm (PCon (#1 cpath, #2 cpath, SOME pterm), s (cpathleft, ptermright))
adamc@170 1097
adamc@170 1098 pterm : SYMBOL (PVar SYMBOL, s (SYMBOLleft, SYMBOLright))
adamc@170 1099 | cpath (PCon (#1 cpath, #2 cpath, NONE), s (cpathleft, cpathright))
adamc@170 1100 | UNDER (PWild, s (UNDERleft, UNDERright))
adamc@173 1101 | INT (PPrim (Prim.Int INT), s (INTleft, INTright))
adamc@173 1102 | STRING (PPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@170 1103 | LPAREN pat RPAREN (pat)
adamc@174 1104 | LBRACE RBRACE (PRecord ([], false), s (LBRACEleft, RBRACEright))
adamc@174 1105 | UNIT (PRecord ([], false), s (UNITleft, UNITright))
adamc@174 1106 | LBRACE rpat RBRACE (PRecord rpat, s (LBRACEleft, RBRACEright))
adamc@195 1107 | LPAREN ptuple RPAREN (PRecord (ListUtil.mapi (fn (i, p) => (Int.toString (i + 1), p)) ptuple,
adamc@195 1108 false),
adamc@195 1109 s (LPARENleft, RPARENright))
adamc@174 1110
adamc@175 1111 rpat : CSYMBOL EQ pat ([(CSYMBOL, pat)], false)
adamc@243 1112 | INT EQ pat ([(Int64.toString INT, pat)], false)
adamc@174 1113 | DOTDOTDOT ([], true)
adamc@175 1114 | CSYMBOL EQ pat COMMA rpat ((CSYMBOL, pat) :: #1 rpat, #2 rpat)
adamc@243 1115 | INT EQ pat COMMA rpat ((Int64.toString INT, pat) :: #1 rpat, #2 rpat)
adamc@170 1116
adamc@195 1117 ptuple : pat COMMA pat ([pat1, pat2])
adamc@195 1118 | pat COMMA ptuple (pat :: ptuple)
adamc@195 1119
adamc@12 1120 rexp : ([])
adamc@12 1121 | ident EQ eexp ([(ident, eexp)])
adamc@12 1122 | ident EQ eexp COMMA rexp ((ident, eexp) :: rexp)
adamc@91 1123
adamc@141 1124 xml : xmlOne xml (let
adamc@141 1125 val pos = s (xmlOneleft, xmlright)
adamc@141 1126 in
adamc@141 1127 (EApp ((EApp (
adamc@403 1128 (EVar (["Basis"], "join", Infer), pos),
adamc@91 1129 xmlOne), pos),
adamc@141 1130 xml), pos)
adamc@141 1131 end)
adamc@141 1132 | xmlOne (xmlOne)
adamc@91 1133
adamc@403 1134 xmlOne : NOTAGS (EApp ((EVar (["Basis"], "cdata", Infer), s (NOTAGSleft, NOTAGSright)),
adamc@141 1135 (EPrim (Prim.String NOTAGS), s (NOTAGSleft, NOTAGSright))),
adamc@141 1136 s (NOTAGSleft, NOTAGSright))
adamc@141 1137 | tag DIVIDE GT (let
adamc@141 1138 val pos = s (tagleft, GTright)
adamc@141 1139 in
adamc@141 1140 (EApp (#2 tag,
adamc@403 1141 (EApp ((EVar (["Basis"], "cdata", Infer), pos),
adamc@141 1142 (EPrim (Prim.String ""), pos)),
adamc@141 1143 pos)), pos)
adamc@141 1144 end)
adamc@141 1145
adamc@141 1146 | tag GT xml END_TAG (let
adamc@141 1147 val pos = s (tagleft, GTright)
adamc@325 1148 val et = tagIn END_TAG
adamc@141 1149 in
adamc@325 1150 if #1 tag = et then
adamc@361 1151 if et = "form" then
adamc@403 1152 (EApp ((EVar (["Basis"], "form", Infer), pos),
adamc@141 1153 xml), pos)
adamc@141 1154 else
adamc@141 1155 (EApp (#2 tag, xml), pos)
adamc@141 1156 else
adamc@325 1157 (if ErrorMsg.anyErrors () then
adamc@325 1158 ()
adamc@325 1159 else
adamc@325 1160 ErrorMsg.errorAt pos "Begin and end tags don't match.";
adamc@623 1161 (EWild, pos))
adamc@141 1162 end)
adamc@141 1163 | LBRACE eexp RBRACE (eexp)
adamc@391 1164 | LBRACE LBRACK eexp RBRACK RBRACE (let
adamc@391 1165 val loc = s (LBRACEleft, RBRACEright)
adamc@403 1166 val e = (EVar (["Top"], "txt", Infer), loc)
adamc@391 1167 in
adamc@391 1168 (EApp (e, eexp), loc)
adamc@391 1169 end)
adamc@92 1170
adamc@141 1171 tag : tagHead attrs (let
adamc@141 1172 val pos = s (tagHeadleft, attrsright)
adamc@141 1173 in
adamc@141 1174 (#1 tagHead,
adamc@403 1175 (EApp ((EApp ((EVar (["Basis"], "tag", Infer), pos),
adamc@141 1176 (ERecord attrs, pos)), pos),
adamc@141 1177 (EApp (#2 tagHead,
adamc@141 1178 (ERecord [], pos)), pos)),
adamc@141 1179 pos))
adamc@141 1180 end)
adamc@141 1181
adamc@141 1182 tagHead: BEGIN_TAG (let
adamc@325 1183 val bt = tagIn BEGIN_TAG
adamc@141 1184 val pos = s (BEGIN_TAGleft, BEGIN_TAGright)
adamc@141 1185 in
adamc@325 1186 (bt,
adamc@473 1187 (EVar (["Basis"], bt, Infer), pos))
adamc@141 1188 end)
adamc@141 1189 | tagHead LBRACE cexp RBRACE (#1 tagHead, (ECApp (#2 tagHead, cexp), s (tagHeadleft, RBRACEright)))
adamc@92 1190
adamc@104 1191 attrs : ([])
adamc@104 1192 | attr attrs (attr :: attrs)
adamc@104 1193
adamc@204 1194 attr : SYMBOL EQ attrv ((CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright)), attrv)
adamc@204 1195
adamc@104 1196 attrv : INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@104 1197 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@104 1198 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@110 1199 | LBRACE eexp RBRACE (eexp)
adamc@226 1200
adamc@232 1201 query : query1 obopt lopt ofopt (let
adamc@229 1202 val loc = s (query1left, query1right)
adamc@230 1203
adamc@230 1204 val re = (ERecord [((CName "Rows", loc),
adamc@230 1205 query1),
adamc@230 1206 ((CName "OrderBy", loc),
adamc@231 1207 obopt),
adamc@231 1208 ((CName "Limit", loc),
adamc@232 1209 lopt),
adamc@232 1210 ((CName "Offset", loc),
adamc@232 1211 ofopt)], loc)
adamc@229 1212 in
adamc@403 1213 (EApp ((EVar (["Basis"], "sql_query", Infer), loc), re), loc)
adamc@229 1214 end)
adamc@204 1215
adamc@227 1216 query1 : SELECT select FROM tables wopt gopt hopt
adamc@209 1217 (let
adamc@204 1218 val loc = s (SELECTleft, tablesright)
adamc@207 1219
adamc@233 1220 val (sel, exps) =
adamc@207 1221 case select of
adamc@233 1222 Star => (map (fn (nm, _) =>
adamc@233 1223 (nm, (CTuple [(CWild (KRecord (KType, loc), loc),
adamc@233 1224 loc),
adamc@233 1225 (CRecord [], loc)],
adamc@233 1226 loc))) tables,
adamc@233 1227 [])
adamc@207 1228 | Items sis =>
adamc@207 1229 let
adamc@207 1230 val tabs = map (fn (nm, _) => (nm, (CRecord [], loc))) tables
adamc@233 1231 val (tabs, exps) = foldl (amend_select loc) (tabs, []) sis
adamc@207 1232 in
adamc@233 1233 (map (fn (nm, c) => (nm,
adamc@233 1234 (CTuple [c,
adamc@233 1235 (CWild (KRecord (KType, loc), loc),
adamc@233 1236 loc)], loc))) tabs,
adamc@233 1237 exps)
adamc@207 1238 end
adamc@207 1239
adamc@207 1240 val sel = (CRecord sel, loc)
adamc@207 1241
adamc@226 1242 val grp = case gopt of
adamc@403 1243 NONE => (ECApp ((EVar (["Basis"], "sql_subset_all",
adamc@403 1244 Infer), loc),
adamc@226 1245 (CWild (KRecord (KRecord (KType, loc), loc),
adamc@226 1246 loc), loc)), loc)
adamc@226 1247 | SOME gis =>
adamc@226 1248 let
adamc@226 1249 val tabs = map (fn (nm, _) =>
adamc@226 1250 (nm, (CRecord [], loc))) tables
adamc@226 1251 val tabs = foldl (amend_group loc) tabs gis
adamc@226 1252
adamc@226 1253 val tabs = map (fn (nm, c) =>
adamc@226 1254 (nm,
adamc@226 1255 (CTuple [c,
adamc@226 1256 (CWild (KRecord (KType, loc),
adamc@226 1257 loc),
adamc@226 1258 loc)], loc))) tabs
adamc@226 1259 in
adamc@403 1260 (ECApp ((EVar (["Basis"], "sql_subset", Infer), loc),
adamc@226 1261 (CRecord tabs, loc)), loc)
adamc@226 1262 end
adamc@226 1263
adamc@403 1264 val e = (EVar (["Basis"], "sql_query1", Infer), loc)
adamc@209 1265 val re = (ERecord [((CName "From", loc),
adamc@209 1266 (ERecord tables, loc)),
adamc@209 1267 ((CName "Where", loc),
adamc@223 1268 wopt),
adamc@224 1269 ((CName "GroupBy", loc),
adamc@226 1270 grp),
adamc@224 1271 ((CName "Having", loc),
adamc@224 1272 hopt),
adamc@223 1273 ((CName "SelectFields", loc),
adamc@403 1274 (ECApp ((EVar (["Basis"], "sql_subset", Infer), loc),
adamc@233 1275 sel), loc)),
adamc@233 1276 ((CName "SelectExps", loc),
adamc@233 1277 (ERecord exps, loc))], loc)
adamc@223 1278
adamc@209 1279 val e = (EApp (e, re), loc)
adamc@204 1280 in
adamc@207 1281 e
adamc@204 1282 end)
adamc@229 1283 | query1 UNION query1 (sql_relop ("union", query11, query12, s (query11left, query12right)))
adamc@229 1284 | query1 INTERSECT query1 (sql_relop ("intersect", query11, query12, s (query11left, query12right)))
adamc@229 1285 | query1 EXCEPT query1 (sql_relop ("except", query11, query12, s (query11left, query12right)))
adamc@204 1286
adamc@204 1287 tables : table ([table])
adamc@204 1288 | table COMMA tables (table :: tables)
adamc@204 1289
adamc@204 1290 tname : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@204 1291 | LBRACE cexp RBRACE (cexp)
adamc@204 1292
adamc@243 1293 table : SYMBOL ((CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright)),
adamc@403 1294 (EVar ([], SYMBOL, Infer), s (SYMBOLleft, SYMBOLright)))
adamc@403 1295 | SYMBOL AS tname (tname, (EVar ([], SYMBOL, Infer), s (SYMBOLleft, SYMBOLright)))
adamc@221 1296 | LBRACE LBRACE eexp RBRACE RBRACE AS tname (tname, eexp)
adamc@207 1297
adamc@243 1298 tident : SYMBOL (CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright))
adamc@207 1299 | CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@221 1300 | LBRACE LBRACE cexp RBRACE RBRACE (cexp)
adamc@207 1301
adamc@207 1302 fident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@207 1303 | LBRACE cexp RBRACE (cexp)
adamc@207 1304
adamc@207 1305 seli : tident DOT fident (Field (tident, fident))
adamc@233 1306 | sqlexp AS fident (Exp (fident, sqlexp))
adamc@341 1307 | tident DOT LBRACE LBRACE cexp RBRACE RBRACE (Fields (tident, cexp))
adamc@207 1308
adamc@207 1309 selis : seli ([seli])
adamc@207 1310 | seli COMMA selis (seli :: selis)
adamc@207 1311
adamc@207 1312 select : STAR (Star)
adamc@207 1313 | selis (Items selis)
adamc@209 1314
adamc@403 1315 sqlexp : TRUE (sql_inject (EVar (["Basis"], "True", Infer),
adamc@209 1316 s (TRUEleft, TRUEright)))
adamc@403 1317 | FALSE (sql_inject (EVar (["Basis"], "False", Infer),
adamc@209 1318 s (FALSEleft, FALSEright)))
adamc@209 1319
adamc@222 1320 | INT (sql_inject (EPrim (Prim.Int INT),
adamc@222 1321 s (INTleft, INTright)))
adamc@222 1322 | FLOAT (sql_inject (EPrim (Prim.Float FLOAT),
adamc@222 1323 s (FLOATleft, FLOATright)))
adamc@229 1324 | STRING (sql_inject (EPrim (Prim.String STRING),
adamc@229 1325 s (STRINGleft, STRINGright)))
adamc@441 1326 | CURRENT_TIMESTAMP (sql_nfunc ("current_timestamp",
adamc@441 1327 s (CURRENT_TIMESTAMPleft, CURRENT_TIMESTAMPright)))
adamc@222 1328
adamc@221 1329 | tident DOT fident (let
adamc@221 1330 val loc = s (tidentleft, fidentright)
adamc@403 1331 val e = (EVar (["Basis"], "sql_field", Infer), loc)
adamc@221 1332 val e = (ECApp (e, tident), loc)
adamc@221 1333 in
adamc@221 1334 (ECApp (e, fident), loc)
adamc@221 1335 end)
adamc@234 1336 | CSYMBOL (let
adamc@234 1337 val loc = s (CSYMBOLleft, CSYMBOLright)
adamc@310 1338 in
adamc@310 1339 if !inDml then
adamc@310 1340 let
adamc@403 1341 val e = (EVar (["Basis"], "sql_field", Infer), loc)
adamc@310 1342 val e = (ECApp (e, (CName "T", loc)), loc)
adamc@310 1343 in
adamc@310 1344 (ECApp (e, (CName CSYMBOL, loc)), loc)
adamc@310 1345 end
adamc@310 1346 else
adamc@310 1347 let
adamc@403 1348 val e = (EVar (["Basis"], "sql_exp", Infer), loc)
adamc@310 1349 in
adamc@310 1350 (ECApp (e, (CName CSYMBOL, loc)), loc)
adamc@310 1351 end
adamc@310 1352 end)
adamc@221 1353
adamc@471 1354 | LBRACE eexp RBRACE (eexp)
adamc@470 1355
adamc@559 1356 | sqlexp EQ sqlexp (sql_binary ("eq", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1357 | sqlexp NE sqlexp (sql_binary ("ne", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1358 | sqlexp LT sqlexp (sql_binary ("lt", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1359 | sqlexp LE sqlexp (sql_binary ("le", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1360 | sqlexp GT sqlexp (sql_binary ("gt", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1361 | sqlexp GE sqlexp (sql_binary ("ge", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1362
adamc@559 1363 | sqlexp PLUS sqlexp (sql_binary ("plus", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1364 | sqlexp MINUS sqlexp (sql_binary ("minus", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1365 | sqlexp STAR sqlexp (sql_binary ("times", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1366 | sqlexp DIVIDE sqlexp (sql_binary ("div", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1367 | sqlexp MOD sqlexp (sql_binary ("mod", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 1368
adamc@220 1369 | sqlexp CAND sqlexp (sql_binary ("and", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@220 1370 | sqlexp OR sqlexp (sql_binary ("or", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@559 1371
adamc@220 1372 | NOT sqlexp (sql_unary ("not", sqlexp, s (NOTleft, sqlexpright)))
adamc@559 1373 | MINUS sqlexp (sql_unary ("neg", sqlexp, s (MINUSleft, sqlexpright)))
adamc@220 1374
adamc@470 1375 | sqlexp IS NULL (let
adamc@470 1376 val loc = s (sqlexpleft, NULLright)
adamc@470 1377 in
adamc@470 1378 (EApp ((EVar (["Basis"], "sql_is_null", Infer), loc),
adamc@470 1379 sqlexp), loc)
adamc@470 1380 end)
adamc@470 1381
adamc@471 1382 | LBRACE LBRACK eexp RBRACK RBRACE (sql_inject (#1 eexp,
adamc@471 1383 s (LBRACEleft, RBRACEright)))
adamc@220 1384 | LPAREN sqlexp RPAREN (sqlexp)
adamc@210 1385
adamc@467 1386 | NULL (sql_inject ((EVar (["Basis"], "None", Infer),
adamc@467 1387 s (NULLleft, NULLright))))
adamc@467 1388
adamc@235 1389 | COUNT LPAREN STAR RPAREN (let
adamc@235 1390 val loc = s (COUNTleft, RPARENright)
adamc@235 1391 in
adamc@544 1392 (EVar (["Basis"], "sql_count", Infer), loc)
adamc@235 1393 end)
adamc@236 1394 | sqlagg LPAREN sqlexp RPAREN (let
adamc@236 1395 val loc = s (sqlaggleft, RPARENright)
adamc@236 1396
adamc@403 1397 val e = (EVar (["Basis"], "sql_" ^ sqlagg, Infer), loc)
adamc@403 1398 val e = (EApp ((EVar (["Basis"], "sql_aggregate", Infer), loc),
adamc@236 1399 e), loc)
adamc@236 1400 in
adamc@236 1401 (EApp (e, sqlexp), loc)
adamc@236 1402 end)
adamc@235 1403
adamc@403 1404 wopt : (sql_inject (EVar (["Basis"], "True", Infer),
adamc@230 1405 dummy))
adamc@209 1406 | CWHERE sqlexp (sqlexp)
adamc@226 1407
adamc@226 1408 groupi : tident DOT fident (GField (tident, fident))
adamc@226 1409
adamc@226 1410 groupis: groupi ([groupi])
adamc@226 1411 | groupi COMMA groupis (groupi :: groupis)
adamc@226 1412
adamc@226 1413 gopt : (NONE)
adamc@226 1414 | GROUP BY groupis (SOME groupis)
adamc@227 1415
adamc@403 1416 hopt : (sql_inject (EVar (["Basis"], "True", Infer),
adamc@230 1417 dummy))
adamc@227 1418 | HAVING sqlexp (sqlexp)
adamc@230 1419
adamc@403 1420 obopt : (ECApp ((EVar (["Basis"], "sql_order_by_Nil", Infer), dummy),
adamc@234 1421 (CWild (KRecord (KType, dummy), dummy), dummy)),
adamc@230 1422 dummy)
adamc@230 1423 | ORDER BY obexps (obexps)
adamc@230 1424
adamc@268 1425 obitem : sqlexp diropt (sqlexp, diropt)
adamc@268 1426
adamc@268 1427 obexps : obitem (let
adamc@268 1428 val loc = s (obitemleft, obitemright)
adamc@230 1429
adamc@403 1430 val e' = (ECApp ((EVar (["Basis"], "sql_order_by_Nil", Infer), loc),
adamc@234 1431 (CWild (KRecord (KType, loc), loc), loc)),
adamc@230 1432 loc)
adamc@403 1433 val e = (EApp ((EVar (["Basis"], "sql_order_by_Cons", Infer), loc),
adamc@268 1434 #1 obitem), loc)
adamc@268 1435 val e = (EApp (e, #2 obitem), loc)
adamc@230 1436 in
adamc@230 1437 (EApp (e, e'), loc)
adamc@230 1438 end)
adamc@268 1439 | obitem COMMA obexps (let
adamc@268 1440 val loc = s (obitemleft, obexpsright)
adamc@230 1441
adamc@403 1442 val e = (EApp ((EVar (["Basis"], "sql_order_by_Cons", Infer), loc),
adamc@268 1443 #1 obitem), loc)
adamc@268 1444 val e = (EApp (e, #2 obitem), loc)
adamc@230 1445 in
adamc@230 1446 (EApp (e, obexps), loc)
adamc@230 1447 end)
adamc@231 1448
adamc@403 1449 diropt : (EVar (["Basis"], "sql_asc", Infer), dummy)
adamc@403 1450 | ASC (EVar (["Basis"], "sql_asc", Infer), s (ASCleft, ASCright))
adamc@403 1451 | DESC (EVar (["Basis"], "sql_desc", Infer), s (DESCleft, DESCright))
adamc@268 1452
adamc@403 1453 lopt : (EVar (["Basis"], "sql_no_limit", Infer), dummy)
adamc@403 1454 | LIMIT ALL (EVar (["Basis"], "sql_no_limit", Infer), dummy)
adamc@231 1455 | LIMIT sqlint (let
adamc@231 1456 val loc = s (LIMITleft, sqlintright)
adamc@231 1457 in
adamc@403 1458 (EApp ((EVar (["Basis"], "sql_limit", Infer), loc), sqlint), loc)
adamc@231 1459 end)
adamc@231 1460
adamc@403 1461 ofopt : (EVar (["Basis"], "sql_no_offset", Infer), dummy)
adamc@232 1462 | OFFSET sqlint (let
adamc@232 1463 val loc = s (OFFSETleft, sqlintright)
adamc@232 1464 in
adamc@403 1465 (EApp ((EVar (["Basis"], "sql_offset", Infer), loc), sqlint), loc)
adamc@232 1466 end)
adamc@232 1467
adamc@231 1468 sqlint : INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@231 1469 | LBRACE eexp RBRACE (eexp)
adamc@236 1470
adamc@236 1471 sqlagg : AVG ("avg")
adamc@236 1472 | SUM ("sum")
adamc@236 1473 | MIN ("min")
adamc@236 1474 | MAX ("max")