annotate src/urweb.grm @ 1425:139d019c7237

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