annotate src/urweb.grm @ 319:1fd2a29a7c85

Improve linear-let optimization
author Adam Chlipala <adamc@hcoop.net>
date Thu, 11 Sep 2008 11:44:46 -0400
parents 0aee86b8a6d6
children e457d8972ff1
rev   line source
adamc@1 1 (* Copyright (c) 2008, 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@1 15 * THIS SOFTWARE 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@207 46
adamc@207 47 datatype select =
adamc@207 48 Star
adamc@207 49 | Items of select_item list
adamc@207 50
adamc@226 51 datatype group_item =
adamc@226 52 GField of con * con
adamc@226 53
adamc@207 54 fun eqTnames ((c1, _), (c2, _)) =
adamc@207 55 case (c1, c2) of
adamc@207 56 (CVar (ms1, x1), CVar (ms2, x2)) => ms1 = ms2 andalso x1 = x2
adamc@207 57 | (CName x1, CName x2) => x1 = x2
adamc@207 58 | _ => false
adamc@207 59
adamc@233 60 fun amend_select loc (si, (tabs, exps)) =
adamc@233 61 case si of
adamc@233 62 Field (tx, fx) =>
adamc@233 63 let
adamc@233 64 val c = (CRecord ([(fx, (CWild (KType, loc), loc))]), loc)
adamc@207 65
adamc@233 66 val (tabs, found) = ListUtil.foldlMap (fn ((tx', c'), found) =>
adamc@233 67 if eqTnames (tx, tx') then
adamc@233 68 ((tx', (CConcat (c, c'), loc)), true)
adamc@233 69 else
adamc@233 70 ((tx', c'), found))
adamc@233 71 false tabs
adamc@233 72 in
adamc@233 73 if found then
adamc@233 74 ()
adamc@233 75 else
adamc@233 76 ErrorMsg.errorAt loc "Select of field from unbound table";
adamc@233 77
adamc@233 78 (tabs, exps)
adamc@233 79 end
adamc@233 80 | Exp (c, e) => (tabs, (c, e) :: exps)
adamc@207 81
adamc@226 82 fun amend_group loc (gi, tabs) =
adamc@226 83 let
adamc@226 84 val (tx, c) = case gi of
adamc@226 85 GField (tx, fx) => (tx, (CRecord ([(fx, (CWild (KType, loc), loc))]), loc))
adamc@226 86
adamc@226 87 val (tabs, found) = ListUtil.foldlMap (fn ((tx', c'), found) =>
adamc@226 88 if eqTnames (tx, tx') then
adamc@226 89 ((tx', (CConcat (c, c'), loc)), true)
adamc@226 90 else
adamc@226 91 ((tx', c'), found))
adamc@226 92 false tabs
adamc@226 93 in
adamc@226 94 if found then
adamc@226 95 ()
adamc@226 96 else
adamc@226 97 ErrorMsg.errorAt loc "Select of field from unbound table";
adamc@226 98
adamc@226 99 tabs
adamc@226 100 end
adamc@226 101
adamc@209 102 fun sql_inject (v, t, loc) =
adamc@209 103 let
adamc@234 104 val e = (EApp ((EVar (["Basis"], "sql_inject"), loc), (t, loc)), loc)
adamc@209 105 in
adamc@234 106 (EApp (e, (v, loc)), loc)
adamc@209 107 end
adamc@209 108
adamc@219 109 fun sql_compare (oper, sqlexp1, sqlexp2, loc) =
adamc@219 110 let
adamc@219 111 val e = (EVar (["Basis"], "sql_comparison"), loc)
adamc@219 112 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper), loc)), loc)
adamc@219 113 val e = (EApp (e, sqlexp1), loc)
adamc@219 114 in
adamc@234 115 (EApp (e, sqlexp2), loc)
adamc@220 116 end
adamc@220 117
adamc@220 118 fun sql_binary (oper, sqlexp1, sqlexp2, loc) =
adamc@220 119 let
adamc@220 120 val e = (EVar (["Basis"], "sql_binary"), loc)
adamc@220 121 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper), loc)), loc)
adamc@220 122 val e = (EApp (e, sqlexp1), loc)
adamc@220 123 in
adamc@220 124 (EApp (e, sqlexp2), loc)
adamc@220 125 end
adamc@220 126
adamc@220 127 fun sql_unary (oper, sqlexp, loc) =
adamc@220 128 let
adamc@220 129 val e = (EVar (["Basis"], "sql_unary"), loc)
adamc@220 130 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper), loc)), loc)
adamc@220 131 in
adamc@220 132 (EApp (e, sqlexp), loc)
adamc@219 133 end
adamc@219 134
adamc@229 135 fun sql_relop (oper, sqlexp1, sqlexp2, loc) =
adamc@229 136 let
adamc@229 137 val e = (EVar (["Basis"], "sql_relop"), loc)
adamc@229 138 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper), loc)), loc)
adamc@229 139 val e = (EApp (e, sqlexp1), loc)
adamc@229 140 in
adamc@229 141 (EApp (e, sqlexp2), loc)
adamc@229 142 end
adamc@229 143
adamc@256 144 fun native_op (oper, e1, e2, loc) =
adamc@256 145 let
adamc@256 146 val e = (EVar (["Basis"], oper), loc)
adamc@256 147 val e = (EApp (e, (EWild, loc)), loc)
adamc@256 148 val e = (EApp (e, e1), loc)
adamc@256 149 in
adamc@256 150 (EApp (e, e2), loc)
adamc@256 151 end
adamc@256 152
adamc@310 153 val inDml = ref false
adamc@310 154
adamc@1 155 %%
adamc@244 156 %header (functor UrwebLrValsFn(structure Token : TOKEN))
adamc@1 157
adamc@1 158 %term
adamc@1 159 EOF
adamc@14 160 | STRING of string | INT of Int64.int | FLOAT of Real64.real
adamc@1 161 | SYMBOL of string | CSYMBOL of string
adamc@1 162 | LPAREN | RPAREN | LBRACK | RBRACK | LBRACE | RBRACE
adamc@156 163 | EQ | COMMA | COLON | DCOLON | TCOLON | DOT | HASH | UNDER | UNDERUNDER | BAR
adamc@219 164 | DIVIDE | DOTDOTDOT
adamc@242 165 | CON | LTYPE | VAL | REC | AND | FUN | FOLD | UNIT | KUNIT | CLASS
adamc@156 166 | DATATYPE | OF
adamc@7 167 | TYPE | NAME
adamc@243 168 | ARROW | LARROW | DARROW | STAR | SEMI
adamc@149 169 | FN | PLUSPLUS | MINUSMINUS | DOLLAR | TWIDDLE
adamc@88 170 | STRUCTURE | SIGNATURE | STRUCT | SIG | END | FUNCTOR | WHERE | EXTERN
adamc@203 171 | INCLUDE | OPEN | CONSTRAINT | CONSTRAINTS | EXPORT | TABLE
adamc@190 172 | CASE | IF | THEN | ELSE
adamc@1 173
adamc@91 174 | XML_BEGIN of string | XML_END
adamc@91 175 | NOTAGS of string
adamc@91 176 | BEGIN_TAG of string | END_TAG of string
adamc@91 177
adamc@230 178 | SELECT | FROM | AS | CWHERE | GROUP | ORDER | BY | HAVING
adamc@229 179 | UNION | INTERSECT | EXCEPT
adamc@232 180 | LIMIT | OFFSET | ALL
adamc@220 181 | TRUE | FALSE | CAND | OR | NOT
adamc@236 182 | COUNT | AVG | SUM | MIN | MAX
adamc@268 183 | ASC | DESC
adamc@302 184 | INSERT | INTO | VALUES | UPDATE | SET | DELETE
adamc@219 185 | NE | LT | LE | GT | GE
adamc@204 186
adamc@30 187 %nonterm
adamc@1 188 file of decl list
adamc@1 189 | decls of decl list
adamc@1 190 | decl of decl
adamc@123 191 | vali of string * con option * exp
adamc@123 192 | valis of (string * con option * exp) list
adamc@242 193 | copt of con option
adamc@1 194
adamc@191 195 | dargs of string list
adamc@156 196 | barOpt of unit
adamc@156 197 | dcons of (string * con option) list
adamc@156 198 | dcon of string * con option
adamc@156 199
adamc@30 200 | sgn of sgn
adamc@42 201 | sgntm of sgn
adamc@30 202 | sgi of sgn_item
adamc@30 203 | sgis of sgn_item list
adamc@30 204
adamc@30 205 | str of str
adamc@30 206
adamc@1 207 | kind of kind
adamc@207 208 | ktuple of kind list
adamc@1 209 | kcolon of explicitness
adamc@240 210 | kopt of kind option
adamc@1 211
adamc@34 212 | path of string list * string
adamc@156 213 | cpath of string list * string
adamc@34 214 | spath of str
adamc@59 215 | mpath of string list
adamc@34 216
adamc@1 217 | cexp of con
adamc@1 218 | capps of con
adamc@1 219 | cterm of con
adamc@195 220 | ctuple of con list
adamc@207 221 | ctuplev of con list
adamc@1 222 | ident of con
adamc@200 223 | idents of con list
adamc@1 224 | rcon of (con * con) list
adamc@83 225 | rconn of (con * con) list
adamc@1 226 | rcone of (con * con) list
adamc@239 227 | cargs of con * kind -> con * kind
adamc@239 228 | cargl of con * kind -> con * kind
adamc@240 229 | cargl2 of con * kind -> con * kind
adamc@239 230 | carg of con * kind -> con * kind
adamc@239 231 | cargp of con * kind -> con * kind
adamc@1 232
adamc@8 233 | eexp of exp
adamc@8 234 | eapps of exp
adamc@8 235 | eterm of exp
adamc@195 236 | etuple of exp list
adamc@12 237 | rexp of (con * exp) list
adamc@91 238 | xml of exp
adamc@91 239 | xmlOne of exp
adamc@141 240 | tag of string * exp
adamc@141 241 | tagHead of string * exp
adamc@8 242
adamc@241 243 | earg of exp * con -> exp * con
adamc@241 244 | eargp of exp * con -> exp * con
adamc@241 245 | eargs of exp * con -> exp * con
adamc@241 246 | eargl of exp * con -> exp * con
adamc@242 247 | eargl2 of exp * con -> exp * con
adamc@241 248
adamc@170 249 | branch of pat * exp
adamc@170 250 | branchs of (pat * exp) list
adamc@170 251 | pat of pat
adamc@170 252 | pterm of pat
adamc@174 253 | rpat of (string * pat) list * bool
adamc@195 254 | ptuple of pat list
adamc@170 255
adamc@104 256 | attrs of (con * exp) list
adamc@104 257 | attr of con * exp
adamc@104 258 | attrv of exp
adamc@104 259
adamc@204 260 | query of exp
adamc@226 261 | query1 of exp
adamc@204 262 | tables of (con * exp) list
adamc@204 263 | tname of con
adamc@204 264 | table of con * exp
adamc@207 265 | tident of con
adamc@207 266 | fident of con
adamc@207 267 | seli of select_item
adamc@207 268 | selis of select_item list
adamc@207 269 | select of select
adamc@209 270 | sqlexp of exp
adamc@209 271 | wopt of exp
adamc@226 272 | groupi of group_item
adamc@226 273 | groupis of group_item list
adamc@226 274 | gopt of group_item list option
adamc@227 275 | hopt of exp
adamc@230 276 | obopt of exp
adamc@268 277 | obitem of exp * exp
adamc@230 278 | obexps of exp
adamc@268 279 | diropt of exp
adamc@231 280 | lopt of exp
adamc@232 281 | ofopt of exp
adamc@231 282 | sqlint of exp
adamc@236 283 | sqlagg of string
adamc@207 284
adamc@302 285 | texp of exp
adamc@302 286 | fields of con list
adamc@302 287 | sqlexps of exp list
adamc@303 288 | fsets of (con * exp) list
adamc@310 289 | enterDml of unit
adamc@310 290 | leaveDml of unit
adamc@302 291
adamc@204 292
adamc@1 293 %verbose (* print summary of errors *)
adamc@1 294 %pos int (* positions *)
adamc@1 295 %start file
adamc@1 296 %pure
adamc@1 297 %eop EOF
adamc@1 298 %noshift EOF
adamc@1 299
adamc@244 300 %name Urweb
adamc@1 301
adamc@243 302 %right SEMI
adamc@243 303 %nonassoc LARROW
adamc@195 304 %nonassoc IF THEN ELSE
adamc@1 305 %nonassoc DARROW
adamc@1 306 %nonassoc COLON
adamc@6 307 %nonassoc DCOLON TCOLON
adamc@229 308 %left UNION INTERSECT EXCEPT
adamc@1 309 %right COMMA
adamc@220 310 %right OR
adamc@220 311 %right CAND
adamc@219 312 %nonassoc EQ NE LT LE GT GE
adamc@243 313 %right ARROW
adamc@149 314 %right PLUSPLUS MINUSMINUS
adamc@195 315 %right STAR
adamc@220 316 %left NOT
adamc@84 317 %nonassoc TWIDDLE
adamc@1 318 %nonassoc DOLLAR
adamc@1 319 %left DOT
adamc@221 320 %nonassoc LBRACE RBRACE
adamc@1 321
adamc@1 322 %%
adamc@1 323
adamc@1 324 file : decls (decls)
adamc@54 325 | SIG sgis ([(DSgn ("?", (SgnConst sgis, s (SIGleft, sgisright))),
adamc@54 326 s (SIGleft, sgisright))])
adamc@1 327
adamc@1 328 decls : ([])
adamc@1 329 | decl decls (decl :: decls)
adamc@1 330
adamc@240 331 decl : CON SYMBOL cargl2 kopt EQ cexp (let
adamc@240 332 val loc = s (CONleft, cexpright)
adamc@240 333
adamc@240 334 val k = Option.getOpt (kopt, (KWild, loc))
adamc@240 335 val (c, k) = cargl2 (cexp, k)
adamc@240 336 in
adamc@240 337 (DCon (SYMBOL, SOME k, c), loc)
adamc@240 338 end)
adamc@7 339 | LTYPE SYMBOL EQ cexp (DCon (SYMBOL, SOME (KType, s (LTYPEleft, cexpright)), cexp),
adamc@7 340 s (LTYPEleft, cexpright))
adamc@191 341 | DATATYPE SYMBOL dargs EQ barOpt dcons(DDatatype (SYMBOL, dargs, dcons), s (DATATYPEleft, dconsright))
adamc@191 342 | DATATYPE SYMBOL dargs EQ DATATYPE CSYMBOL DOT path
adamc@191 343 (case dargs of
adamc@191 344 [] => (DDatatypeImp (SYMBOL, CSYMBOL :: #1 path, #2 path), s (DATATYPEleft, pathright))
adamc@191 345 | _ => raise Fail "Arguments specified for imported datatype")
adamc@123 346 | VAL vali (DVal vali, s (VALleft, valiright))
adamc@123 347 | VAL REC valis (DValRec valis, s (VALleft, valisright))
adamc@242 348 | FUN valis (DValRec valis, s (FUNleft, valisright))
adamc@1 349
adamc@30 350 | SIGNATURE CSYMBOL EQ sgn (DSgn (CSYMBOL, sgn), s (SIGNATUREleft, sgnright))
adamc@30 351 | STRUCTURE CSYMBOL EQ str (DStr (CSYMBOL, NONE, str), s (STRUCTUREleft, strright))
adamc@30 352 | STRUCTURE CSYMBOL COLON sgn EQ str (DStr (CSYMBOL, SOME sgn, str), s (STRUCTUREleft, strright))
adamc@42 353 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN EQ str
adamc@42 354 (DStr (CSYMBOL1, NONE,
adamc@42 355 (StrFun (CSYMBOL2, sgn1, NONE, str), s (FUNCTORleft, strright))),
adamc@42 356 s (FUNCTORleft, strright))
adamc@42 357 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN COLON sgn EQ str
adamc@42 358 (DStr (CSYMBOL1, NONE,
adamc@42 359 (StrFun (CSYMBOL2, sgn1, SOME sgn2, str), s (FUNCTORleft, strright))),
adamc@42 360 s (FUNCTORleft, strright))
adamc@48 361 | EXTERN STRUCTURE CSYMBOL COLON sgn (DFfiStr (CSYMBOL, sgn), s (EXTERNleft, sgnright))
adamc@61 362 | OPEN mpath (case mpath of
adamc@61 363 [] => raise Fail "Impossible mpath parse [1]"
adamc@61 364 | m :: ms => (DOpen (m, ms), s (OPENleft, mpathright)))
adamc@88 365 | OPEN CONSTRAINTS mpath (case mpath of
adamc@88 366 [] => raise Fail "Impossible mpath parse [3]"
adamc@88 367 | m :: ms => (DOpenConstraints (m, ms), s (OPENleft, mpathright)))
adamc@88 368 | CONSTRAINT cterm TWIDDLE cterm (DConstraint (cterm1, cterm2), s (CONSTRAINTleft, ctermright))
adamc@109 369 | EXPORT spath (DExport spath, s (EXPORTleft, spathright))
adamc@203 370 | TABLE SYMBOL COLON cexp (DTable (SYMBOL, entable cexp), s (TABLEleft, cexpright))
adamc@211 371 | CLASS SYMBOL EQ cexp (DClass (SYMBOL, cexp), s (CLASSleft, cexpright))
adamc@211 372 | CLASS SYMBOL SYMBOL EQ cexp (let
adamc@211 373 val loc = s (CLASSleft, cexpright)
adamc@211 374 val k = (KType, loc)
adamc@211 375 val c = (CAbs (SYMBOL2, SOME k, cexp), loc)
adamc@211 376 in
adamc@211 377 (DClass (SYMBOL1, c), s (CLASSleft, cexpright))
adamc@211 378 end)
adamc@30 379
adamc@240 380 kopt : (NONE)
adamc@240 381 | DCOLON kind (SOME kind)
adamc@240 382
adamc@191 383 dargs : ([])
adamc@191 384 | SYMBOL dargs (SYMBOL :: dargs)
adamc@191 385
adamc@156 386 barOpt : ()
adamc@156 387 | BAR ()
adamc@156 388
adamc@156 389 dcons : dcon ([dcon])
adamc@156 390 | dcon BAR dcons (dcon :: dcons)
adamc@156 391
adamc@156 392 dcon : CSYMBOL (CSYMBOL, NONE)
adamc@156 393 | CSYMBOL OF cexp (CSYMBOL, SOME cexp)
adamc@156 394
adamc@242 395 vali : SYMBOL eargl2 copt EQ eexp (let
adamc@242 396 val loc = s (SYMBOLleft, eexpright)
adamc@242 397 val t = Option.getOpt (copt, (CWild (KType, loc), loc))
adamc@242 398
adamc@242 399 val (e, t) = eargl2 (eexp, t)
adamc@242 400 in
adamc@242 401 (SYMBOL, SOME t, e)
adamc@242 402 end)
adamc@242 403
adamc@242 404 copt : (NONE)
adamc@242 405 | COLON cexp (SOME cexp)
adamc@123 406
adamc@123 407 valis : vali ([vali])
adamc@123 408 | vali AND valis (vali :: valis)
adamc@123 409
adamc@42 410 sgn : sgntm (sgntm)
adamc@40 411 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN COLON sgn
adamc@40 412 (SgnFun (CSYMBOL, sgn1, sgn2), s (FUNCTORleft, sgn2right))
adamc@30 413
adamc@42 414 sgntm : SIG sgis END (SgnConst sgis, s (SIGleft, ENDright))
adamc@59 415 | mpath (case mpath of
adamc@61 416 [] => raise Fail "Impossible mpath parse [2]"
adamc@59 417 | [x] => SgnVar x
adamc@59 418 | m :: ms => SgnProj (m,
adamc@59 419 List.take (ms, length ms - 1),
adamc@59 420 List.nth (ms, length ms - 1)),
adamc@59 421 s (mpathleft, mpathright))
adamc@42 422 | sgntm WHERE CON SYMBOL EQ cexp (SgnWhere (sgntm, SYMBOL, cexp), s (sgntmleft, cexpright))
adamc@42 423 | sgntm WHERE LTYPE SYMBOL EQ cexp(SgnWhere (sgntm, SYMBOL, cexp), s (sgntmleft, cexpright))
adamc@42 424 | LPAREN sgn RPAREN (sgn)
adamc@42 425
adamc@30 426 sgi : CON SYMBOL DCOLON kind (SgiConAbs (SYMBOL, kind), s (CONleft, kindright))
adamc@30 427 | LTYPE SYMBOL (SgiConAbs (SYMBOL, (KType, s (LTYPEleft, SYMBOLright))),
adamc@30 428 s (LTYPEleft, SYMBOLright))
adamc@30 429 | CON SYMBOL EQ cexp (SgiCon (SYMBOL, NONE, cexp), s (CONleft, cexpright))
adamc@30 430 | CON SYMBOL DCOLON kind EQ cexp (SgiCon (SYMBOL, SOME kind, cexp), s (CONleft, cexpright))
adamc@30 431 | LTYPE SYMBOL EQ cexp (SgiCon (SYMBOL, SOME (KType, s (LTYPEleft, cexpright)), cexp),
adamc@30 432 s (LTYPEleft, cexpright))
adamc@191 433 | DATATYPE SYMBOL dargs EQ barOpt dcons(SgiDatatype (SYMBOL, dargs, dcons), s (DATATYPEleft, dconsright))
adamc@191 434 | DATATYPE SYMBOL dargs EQ DATATYPE CSYMBOL DOT path
adamc@191 435 (case dargs of
adamc@191 436 [] => (SgiDatatypeImp (SYMBOL, CSYMBOL :: #1 path, #2 path), s (DATATYPEleft, pathright))
adamc@191 437 | _ => raise Fail "Arguments specified for imported datatype")
adamc@30 438 | VAL SYMBOL COLON cexp (SgiVal (SYMBOL, cexp), s (VALleft, cexpright))
adamc@30 439
adamc@30 440 | STRUCTURE CSYMBOL COLON sgn (SgiStr (CSYMBOL, sgn), s (STRUCTUREleft, sgnright))
adamc@59 441 | SIGNATURE CSYMBOL EQ sgn (SgiSgn (CSYMBOL, sgn), s (SIGNATUREleft, sgnright))
adamc@42 442 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN COLON sgn
adamc@42 443 (SgiStr (CSYMBOL1,
adamc@42 444 (SgnFun (CSYMBOL2, sgn1, sgn2), s (FUNCTORleft, sgn2right))),
adamc@42 445 s (FUNCTORleft, sgn2right))
adamc@58 446 | INCLUDE sgn (SgiInclude sgn, s (INCLUDEleft, sgnright))
adamc@88 447 | CONSTRAINT cterm TWIDDLE cterm (SgiConstraint (cterm1, cterm2), s (CONSTRAINTleft, ctermright))
adamc@203 448 | TABLE SYMBOL COLON cexp (SgiTable (SYMBOL, entable cexp), s (TABLEleft, cexpright))
adamc@211 449 | CLASS SYMBOL (SgiClassAbs SYMBOL, s (CLASSleft, SYMBOLright))
adamc@211 450 | CLASS SYMBOL EQ cexp (SgiClass (SYMBOL, cexp), s (CLASSleft, cexpright))
adamc@211 451 | CLASS SYMBOL SYMBOL EQ cexp (let
adamc@211 452 val loc = s (CLASSleft, cexpright)
adamc@211 453 val k = (KType, loc)
adamc@211 454 val c = (CAbs (SYMBOL2, SOME k, cexp), loc)
adamc@211 455 in
adamc@211 456 (SgiClass (SYMBOL1, c), s (CLASSleft, cexpright))
adamc@211 457 end)
adamc@30 458
adamc@30 459 sgis : ([])
adamc@30 460 | sgi sgis (sgi :: sgis)
adamc@30 461
adamc@30 462 str : STRUCT decls END (StrConst decls, s (STRUCTleft, ENDright))
adamc@34 463 | spath (spath)
adamc@40 464 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN DARROW str
adamc@40 465 (StrFun (CSYMBOL, sgn, NONE, str), s (FUNCTORleft, strright))
adamc@40 466 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN COLON sgn DARROW str
adamc@40 467 (StrFun (CSYMBOL, sgn1, SOME sgn2, str), s (FUNCTORleft, strright))
adamc@44 468 | spath LPAREN str RPAREN (StrApp (spath, str), s (spathleft, RPARENright))
adamc@34 469
adamc@34 470 spath : CSYMBOL (StrVar CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@34 471 | spath DOT CSYMBOL (StrProj (spath, CSYMBOL), s (spathleft, CSYMBOLright))
adamc@30 472
adamc@1 473 kind : TYPE (KType, s (TYPEleft, TYPEright))
adamc@1 474 | NAME (KName, s (NAMEleft, NAMEright))
adamc@1 475 | LBRACE kind RBRACE (KRecord kind, s (LBRACEleft, RBRACEright))
adamc@1 476 | kind ARROW kind (KArrow (kind1, kind2), s (kind1left, kind2right))
adamc@1 477 | LPAREN kind RPAREN (#1 kind, s (LPARENleft, RPARENright))
adamc@82 478 | KUNIT (KUnit, s (KUNITleft, KUNITright))
adamc@18 479 | UNDERUNDER (KWild, s (UNDERUNDERleft, UNDERUNDERright))
adamc@207 480 | LPAREN ktuple RPAREN (KTuple ktuple, s (LPARENleft, RPARENright))
adamc@207 481
adamc@207 482 ktuple : kind STAR kind ([kind1, kind2])
adamc@207 483 | kind STAR ktuple (kind :: ktuple)
adamc@1 484
adamc@1 485 capps : cterm (cterm)
adamc@1 486 | capps cterm (CApp (capps, cterm), s (cappsleft, ctermright))
adamc@1 487
adamc@1 488 cexp : capps (capps)
adamc@1 489 | cexp ARROW cexp (TFun (cexp1, cexp2), s (cexp1left, cexp2right))
adamc@15 490 | SYMBOL kcolon kind ARROW cexp (TCFun (kcolon, SYMBOL, kind, cexp), s (SYMBOLleft, cexpright))
adamc@1 491
adamc@1 492 | cexp PLUSPLUS cexp (CConcat (cexp1, cexp2), s (cexp1left, cexp1right))
adamc@1 493
adamc@239 494 | FN cargs DARROW cexp (#1 (cargs (cexp, (KWild, s (FNleft, cexpright)))))
adamc@84 495 | cterm TWIDDLE cterm DARROW cexp(CDisjoint (cterm1, cterm2, cexp), s (cterm1left, cexpright))
adamc@85 496 | cterm TWIDDLE cterm ARROW cexp (TDisjoint (cterm1, cterm2, cexp), s (cterm1left, cexpright))
adamc@1 497
adamc@8 498 | LPAREN cexp RPAREN DCOLON kind (CAnnot (cexp, kind), s (LPARENleft, kindright))
adamc@6 499
adamc@18 500 | UNDER DCOLON kind (CWild kind, s (UNDERleft, UNDERright))
adamc@195 501 | ctuple (let
adamc@195 502 val loc = s (ctupleleft, ctupleright)
adamc@195 503 in
adamc@195 504 (TRecord (CRecord (ListUtil.mapi (fn (i, c) =>
adamc@195 505 ((CName (Int.toString (i + 1)), loc),
adamc@195 506 c)) ctuple),
adamc@195 507 loc), loc)
adamc@195 508 end)
adamc@18 509
adamc@1 510 kcolon : DCOLON (Explicit)
adamc@1 511 | TCOLON (Implicit)
adamc@1 512
adamc@239 513 cargs : carg (carg)
adamc@239 514 | cargl (cargl)
adamc@239 515
adamc@239 516 cargl : cargp cargp (cargp1 o cargp2)
adamc@239 517 | cargp cargl (cargp o cargl)
adamc@239 518
adamc@240 519 cargl2 : (fn x => x)
adamc@240 520 | cargp cargl2 (cargp o cargl2)
adamc@240 521
adamc@241 522 carg : SYMBOL DCOLON kind (fn (c, k) =>
adamc@239 523 let
adamc@239 524 val loc = s (SYMBOLleft, kindright)
adamc@239 525 in
adamc@239 526 ((CAbs (SYMBOL, SOME kind, c), loc),
adamc@239 527 (KArrow (kind, k), loc))
adamc@239 528 end)
adamc@241 529 | cargp (cargp)
adamc@239 530
adamc@239 531 cargp : SYMBOL (fn (c, k) =>
adamc@239 532 let
adamc@239 533 val loc = s (SYMBOLleft, SYMBOLright)
adamc@239 534 in
adamc@239 535 ((CAbs (SYMBOL, NONE, c), loc),
adamc@239 536 (KArrow ((KWild, loc), k), loc))
adamc@239 537 end)
adamc@239 538 | LPAREN SYMBOL DCOLON kind RPAREN (fn (c, k) =>
adamc@239 539 let
adamc@239 540 val loc = s (LPARENleft, RPARENright)
adamc@239 541 in
adamc@239 542 ((CAbs (SYMBOL, SOME kind, c), loc),
adamc@239 543 (KArrow (kind, k), loc))
adamc@239 544 end)
adamc@239 545
adamc@34 546 path : SYMBOL ([], SYMBOL)
adamc@34 547 | CSYMBOL DOT path (let val (ms, x) = path in (CSYMBOL :: ms, x) end)
adamc@34 548
adamc@156 549 cpath : CSYMBOL ([], CSYMBOL)
adamc@156 550 | CSYMBOL DOT cpath (let val (ms, x) = cpath in (CSYMBOL :: ms, x) end)
adamc@156 551
adamc@59 552 mpath : CSYMBOL ([CSYMBOL])
adamc@59 553 | CSYMBOL DOT mpath (CSYMBOL :: mpath)
adamc@59 554
adamc@1 555 cterm : LPAREN cexp RPAREN (#1 cexp, s (LPARENleft, RPARENright))
adamc@1 556 | LBRACK rcon RBRACK (CRecord rcon, s (LBRACKleft, RBRACKright))
adamc@83 557 | LBRACK rconn RBRACK (CRecord rconn, s (LBRACKleft, RBRACKright))
adamc@1 558 | LBRACE rcone RBRACE (TRecord (CRecord rcone, s (LBRACEleft, RBRACEright)),
adamc@1 559 s (LBRACEleft, RBRACEright))
adamc@1 560 | DOLLAR cterm (TRecord cterm, s (DOLLARleft, ctermright))
adamc@1 561 | HASH CSYMBOL (CName CSYMBOL, s (HASHleft, CSYMBOLright))
adamc@195 562 | HASH INT (CName (Int64.toString INT), s (HASHleft, INTright))
adamc@1 563
adamc@34 564 | path (CVar path, s (pathleft, pathright))
adamc@207 565 | path DOT INT (CProj ((CVar path, s (pathleft, pathright)), Int64.toInt INT),
adamc@207 566 s (pathleft, INTright))
adamc@18 567 | UNDER (CWild (KWild, s (UNDERleft, UNDERright)), s (UNDERleft, UNDERright))
adamc@67 568 | FOLD (CFold, s (FOLDleft, FOLDright))
adamc@82 569 | UNIT (CUnit, s (UNITleft, UNITright))
adamc@207 570 | LPAREN ctuplev RPAREN (CTuple ctuplev, s (LPARENleft, RPARENright))
adamc@207 571
adamc@207 572 ctuplev: cexp COMMA cexp ([cexp1, cexp2])
adamc@207 573 | cexp COMMA ctuplev (cexp :: ctuplev)
adamc@1 574
adamc@196 575 ctuple : capps STAR capps ([capps1, capps2])
adamc@196 576 | capps STAR ctuple (capps :: ctuple)
adamc@195 577
adamc@1 578 rcon : ([])
adamc@1 579 | ident EQ cexp ([(ident, cexp)])
adamc@1 580 | ident EQ cexp COMMA rcon ((ident, cexp) :: rcon)
adamc@1 581
adamc@83 582 rconn : ident ([(ident, (CUnit, s (identleft, identright)))])
adamc@83 583 | ident COMMA rconn ((ident, (CUnit, s (identleft, identright))) :: rconn)
adamc@83 584
adamc@1 585 rcone : ([])
adamc@1 586 | ident COLON cexp ([(ident, cexp)])
adamc@1 587 | ident COLON cexp COMMA rcone ((ident, cexp) :: rcone)
adamc@1 588
adamc@1 589 ident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@195 590 | INT (CName (Int64.toString INT), s (INTleft, INTright))
adamc@200 591 | SYMBOL (CVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright))
adamc@8 592
adamc@8 593 eapps : eterm (eterm)
adamc@8 594 | eapps eterm (EApp (eapps, eterm), s (eappsleft, etermright))
adamc@8 595 | eapps LBRACK cexp RBRACK (ECApp (eapps, cexp), s (eappsleft, RBRACKright))
adamc@8 596
adamc@8 597 eexp : eapps (eapps)
adamc@241 598 | FN eargs DARROW eexp (let
adamc@93 599 val loc = s (FNleft, eexpright)
adamc@93 600 in
adamc@241 601 #1 (eargs (eexp, (CWild (KType, loc), loc)))
adamc@93 602 end)
adamc@241 603 | LBRACK cterm TWIDDLE cterm RBRACK DARROW eexp(EDisjoint (cterm1, cterm2, eexp), s (LBRACKleft, RBRACKright))
adamc@196 604 | eexp COLON cexp (EAnnot (eexp, cexp), s (eexpleft, cexpright))
adamc@149 605 | eexp MINUSMINUS cexp (ECut (eexp, cexp), s (eexpleft, cexpright))
adamc@170 606 | CASE eexp OF barOpt branch branchs (ECase (eexp, branch :: branchs), s (CASEleft, branchsright))
adamc@190 607 | IF eexp THEN eexp ELSE eexp (let
adamc@190 608 val loc = s (IFleft, eexp3right)
adamc@190 609 in
adamc@190 610 (ECase (eexp1, [((PCon (["Basis"], "True", NONE), loc), eexp2),
adamc@190 611 ((PCon (["Basis"], "False", NONE), loc), eexp3)]), loc)
adamc@190 612 end)
adamc@243 613 | SYMBOL LARROW eexp SEMI eexp (let
adamc@243 614 val loc = s (SYMBOLleft, eexp2right)
adamc@243 615 val e = (EVar (["Basis"], "bind"), loc)
adamc@243 616 val e = (EApp (e, eexp1), loc)
adamc@243 617 in
adamc@243 618 (EApp (e, (EAbs (SYMBOL, NONE, eexp2), loc)), loc)
adamc@243 619 end)
adamc@299 620 | UNIT LARROW eexp SEMI eexp (let
adamc@299 621 val loc = s (UNITleft, eexp2right)
adamc@299 622 val e = (EVar (["Basis"], "bind"), loc)
adamc@299 623 val e = (EApp (e, eexp1), loc)
adamc@299 624 val t = (TRecord (CRecord [], loc), loc)
adamc@299 625 in
adamc@299 626 (EApp (e, (EAbs ("_", SOME t, eexp2), loc)), loc)
adamc@299 627 end)
adamc@256 628 | eexp EQ eexp (native_op ("eq", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@257 629 | eexp NE eexp (native_op ("ne", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@8 630
adamc@241 631 eargs : earg (earg)
adamc@241 632 | eargl (eargl)
adamc@241 633
adamc@241 634 eargl : eargp eargp (eargp1 o eargp2)
adamc@241 635 | eargp eargl (eargp o eargl)
adamc@241 636
adamc@242 637 eargl2 : (fn x => x)
adamc@242 638 | eargp eargl2 (eargp o eargl2)
adamc@242 639
adamc@241 640 earg : SYMBOL kcolon kind (fn (e, t) =>
adamc@241 641 let
adamc@241 642 val loc = s (SYMBOLleft, kindright)
adamc@241 643 in
adamc@241 644 ((ECAbs (kcolon, SYMBOL, kind, e), loc),
adamc@241 645 (TCFun (kcolon, SYMBOL, kind, t), loc))
adamc@241 646 end)
adamc@241 647 | SYMBOL COLON cexp (fn (e, t) =>
adamc@241 648 let
adamc@241 649 val loc = s (SYMBOLleft, cexpright)
adamc@241 650 in
adamc@241 651 ((EAbs (SYMBOL, SOME cexp, e), loc),
adamc@241 652 (TFun (cexp, t), loc))
adamc@241 653 end)
adamc@241 654 | UNDER COLON cexp (fn (e, t) =>
adamc@241 655 let
adamc@241 656 val loc = s (UNDERleft, cexpright)
adamc@241 657 in
adamc@241 658 ((EAbs ("_", SOME cexp, e), loc),
adamc@241 659 (TFun (cexp, t), loc))
adamc@241 660 end)
adamc@241 661 | eargp (eargp)
adamc@241 662
adamc@241 663 eargp : SYMBOL (fn (e, t) =>
adamc@241 664 let
adamc@241 665 val loc = s (SYMBOLleft, SYMBOLright)
adamc@241 666 in
adamc@241 667 ((EAbs (SYMBOL, NONE, e), loc),
adamc@241 668 (TFun ((CWild (KType, loc), loc), t), loc))
adamc@241 669 end)
adamc@241 670 | UNIT (fn (e, t) =>
adamc@241 671 let
adamc@241 672 val loc = s (UNITleft, UNITright)
adamc@241 673 val t' = (TRecord (CRecord [], loc), loc)
adamc@241 674 in
adamc@241 675 ((EAbs ("_", SOME t', e), loc),
adamc@241 676 (TFun (t', t), loc))
adamc@241 677 end)
adamc@241 678 | UNDER (fn (e, t) =>
adamc@241 679 let
adamc@241 680 val loc = s (UNDERleft, UNDERright)
adamc@241 681 in
adamc@241 682 ((EAbs ("_", NONE, e), loc),
adamc@241 683 (TFun ((CWild (KType, loc), loc), t), loc))
adamc@241 684 end)
adamc@241 685 | LPAREN SYMBOL kcolon kind RPAREN(fn (e, t) =>
adamc@241 686 let
adamc@241 687 val loc = s (LPARENleft, RPARENright)
adamc@241 688 in
adamc@241 689 ((ECAbs (kcolon, SYMBOL, kind, e), loc),
adamc@241 690 (TCFun (kcolon, SYMBOL, kind, t), loc))
adamc@241 691 end)
adamc@241 692 | LPAREN SYMBOL COLON cexp RPAREN (fn (e, t) =>
adamc@241 693 let
adamc@241 694 val loc = s (LPARENleft, RPARENright)
adamc@241 695 in
adamc@241 696 ((EAbs (SYMBOL, SOME cexp, e), loc),
adamc@241 697 (TFun (cexp, t), loc))
adamc@241 698 end)
adamc@241 699 | LPAREN UNDER COLON cexp RPAREN (fn (e, t) =>
adamc@241 700 let
adamc@241 701 val loc = s (LPARENleft, RPARENright)
adamc@241 702 in
adamc@241 703 ((EAbs ("_", SOME cexp, e), loc),
adamc@241 704 (TFun (cexp, t), loc))
adamc@241 705 end)
adamc@241 706
adamc@8 707 eterm : LPAREN eexp RPAREN (#1 eexp, s (LPARENleft, RPARENright))
adamc@195 708 | LPAREN etuple RPAREN (let
adamc@195 709 val loc = s (LPARENleft, RPARENright)
adamc@195 710 in
adamc@195 711 (ERecord (ListUtil.mapi (fn (i, e) =>
adamc@195 712 ((CName (Int.toString (i + 1)), loc),
adamc@195 713 e)) etuple), loc)
adamc@195 714 end)
adamc@8 715
adamc@34 716 | path (EVar path, s (pathleft, pathright))
adamc@156 717 | cpath (EVar cpath, s (cpathleft, cpathright))
adamc@12 718 | LBRACE rexp RBRACE (ERecord rexp, s (LBRACEleft, RBRACEright))
adamc@110 719 | UNIT (ERecord [], s (UNITleft, UNITright))
adamc@12 720
adamc@14 721 | INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@14 722 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@14 723 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@14 724
adamc@200 725 | path DOT idents (let
adamc@200 726 val loc = s (pathleft, identsright)
adamc@200 727 in
adamc@200 728 foldl (fn (ident, e) =>
adamc@200 729 (EField (e, ident), loc))
adamc@200 730 (EVar path, s (pathleft, pathright)) idents
adamc@200 731 end)
adamc@71 732 | FOLD (EFold, s (FOLDleft, FOLDright))
adamc@71 733
adamc@91 734 | XML_BEGIN xml XML_END (xml)
adamc@91 735 | XML_BEGIN XML_END (EApp ((EVar (["Basis"], "cdata"), s (XML_BEGINleft, XML_ENDright)),
adamc@91 736 (EPrim (Prim.String ""), s (XML_BEGINleft, XML_ENDright))),
adamc@91 737 s (XML_BEGINleft, XML_ENDright))
adamc@302 738
adamc@204 739 | LPAREN query RPAREN (query)
adamc@300 740 | LPAREN CWHERE sqlexp RPAREN (sqlexp)
adamc@302 741
adamc@302 742 | LPAREN INSERT INTO texp LPAREN fields RPAREN VALUES LPAREN sqlexps RPAREN RPAREN
adamc@302 743 (let
adamc@302 744 val loc = s (LPAREN1left, RPAREN3right)
adamc@302 745
adamc@302 746 val e = (EVar (["Basis"], "insert"), loc)
adamc@302 747 val e = (EApp (e, texp), loc)
adamc@302 748 in
adamc@302 749 if length fields <> length sqlexps then
adamc@302 750 ErrorMsg.errorAt loc "Length mismatch in INSERT field specification"
adamc@302 751 else
adamc@302 752 ();
adamc@302 753 (EApp (e, (ERecord (ListPair.zip (fields, sqlexps)), loc)), loc)
adamc@302 754 end)
adamc@310 755 | LPAREN enterDml UPDATE texp SET fsets CWHERE sqlexp leaveDml RPAREN
adamc@303 756 (let
adamc@303 757 val loc = s (LPARENleft, RPARENright)
adamc@303 758
adamc@303 759 val e = (EVar (["Basis"], "update"), loc)
adamc@306 760 val e = (EApp (e, (ERecord fsets, loc)), loc)
adamc@305 761 val e = (EApp (e, texp), loc)
adamc@303 762 in
adamc@303 763 (EApp (e, sqlexp), loc)
adamc@303 764 end)
adamc@310 765 | LPAREN enterDml DELETE FROM texp CWHERE sqlexp leaveDml RPAREN
adamc@304 766 (let
adamc@304 767 val loc = s (LPARENleft, RPARENright)
adamc@304 768
adamc@304 769 val e = (EVar (["Basis"], "delete"), loc)
adamc@304 770 val e = (EApp (e, texp), loc)
adamc@304 771 in
adamc@304 772 (EApp (e, sqlexp), loc)
adamc@304 773 end)
adamc@302 774
adamc@211 775 | UNDER (EWild, s (UNDERleft, UNDERright))
adamc@91 776
adamc@310 777 enterDml : (inDml := true)
adamc@310 778 leaveDml : (inDml := false)
adamc@310 779
adamc@302 780 texp : SYMBOL (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright))
adamc@302 781 | LBRACE LBRACE eexp RBRACE RBRACE (eexp)
adamc@302 782
adamc@302 783 fields : fident ([fident])
adamc@302 784 | fident COMMA fields (fident :: fields)
adamc@302 785
adamc@302 786 sqlexps: sqlexp ([sqlexp])
adamc@302 787 | sqlexp COMMA sqlexps (sqlexp :: sqlexps)
adamc@302 788
adamc@303 789 fsets : fident EQ sqlexp ([(fident, sqlexp)])
adamc@303 790 | fident EQ sqlexp COMMA fsets ((fident, sqlexp) :: fsets)
adamc@303 791
adamc@200 792 idents : ident ([ident])
adamc@200 793 | ident DOT idents (ident :: idents)
adamc@200 794
adamc@195 795 etuple : eexp COMMA eexp ([eexp1, eexp2])
adamc@195 796 | eexp COMMA etuple (eexp :: etuple)
adamc@195 797
adamc@170 798 branch : pat DARROW eexp (pat, eexp)
adamc@170 799
adamc@170 800 branchs: ([])
adamc@170 801 | BAR branch branchs (branch :: branchs)
adamc@170 802
adamc@170 803 pat : pterm (pterm)
adamc@170 804 | cpath pterm (PCon (#1 cpath, #2 cpath, SOME pterm), s (cpathleft, ptermright))
adamc@170 805
adamc@170 806 pterm : SYMBOL (PVar SYMBOL, s (SYMBOLleft, SYMBOLright))
adamc@170 807 | cpath (PCon (#1 cpath, #2 cpath, NONE), s (cpathleft, cpathright))
adamc@170 808 | UNDER (PWild, s (UNDERleft, UNDERright))
adamc@173 809 | INT (PPrim (Prim.Int INT), s (INTleft, INTright))
adamc@173 810 | STRING (PPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@170 811 | LPAREN pat RPAREN (pat)
adamc@174 812 | LBRACE RBRACE (PRecord ([], false), s (LBRACEleft, RBRACEright))
adamc@174 813 | UNIT (PRecord ([], false), s (UNITleft, UNITright))
adamc@174 814 | LBRACE rpat RBRACE (PRecord rpat, s (LBRACEleft, RBRACEright))
adamc@195 815 | LPAREN ptuple RPAREN (PRecord (ListUtil.mapi (fn (i, p) => (Int.toString (i + 1), p)) ptuple,
adamc@195 816 false),
adamc@195 817 s (LPARENleft, RPARENright))
adamc@174 818
adamc@175 819 rpat : CSYMBOL EQ pat ([(CSYMBOL, pat)], false)
adamc@243 820 | INT EQ pat ([(Int64.toString INT, pat)], false)
adamc@174 821 | DOTDOTDOT ([], true)
adamc@175 822 | CSYMBOL EQ pat COMMA rpat ((CSYMBOL, pat) :: #1 rpat, #2 rpat)
adamc@243 823 | INT EQ pat COMMA rpat ((Int64.toString INT, pat) :: #1 rpat, #2 rpat)
adamc@170 824
adamc@195 825 ptuple : pat COMMA pat ([pat1, pat2])
adamc@195 826 | pat COMMA ptuple (pat :: ptuple)
adamc@195 827
adamc@12 828 rexp : ([])
adamc@12 829 | ident EQ eexp ([(ident, eexp)])
adamc@12 830 | ident EQ eexp COMMA rexp ((ident, eexp) :: rexp)
adamc@91 831
adamc@141 832 xml : xmlOne xml (let
adamc@141 833 val pos = s (xmlOneleft, xmlright)
adamc@141 834 in
adamc@141 835 (EApp ((EApp (
adamc@141 836 (EVar (["Basis"], "join"), pos),
adamc@91 837 xmlOne), pos),
adamc@141 838 xml), pos)
adamc@141 839 end)
adamc@141 840 | xmlOne (xmlOne)
adamc@91 841
adamc@141 842 xmlOne : NOTAGS (EApp ((EVar (["Basis"], "cdata"), s (NOTAGSleft, NOTAGSright)),
adamc@141 843 (EPrim (Prim.String NOTAGS), s (NOTAGSleft, NOTAGSright))),
adamc@141 844 s (NOTAGSleft, NOTAGSright))
adamc@141 845 | tag DIVIDE GT (let
adamc@141 846 val pos = s (tagleft, GTright)
adamc@141 847 in
adamc@141 848 (EApp (#2 tag,
adamc@141 849 (EApp ((EVar (["Basis"], "cdata"), pos),
adamc@141 850 (EPrim (Prim.String ""), pos)),
adamc@141 851 pos)), pos)
adamc@141 852 end)
adamc@141 853
adamc@141 854 | tag GT xml END_TAG (let
adamc@141 855 val pos = s (tagleft, GTright)
adamc@141 856 in
adamc@141 857 if #1 tag = END_TAG then
adamc@141 858 if END_TAG = "lform" then
adamc@141 859 (EApp ((EVar (["Basis"], "lform"), pos),
adamc@141 860 xml), pos)
adamc@141 861 else
adamc@141 862 (EApp (#2 tag, xml), pos)
adamc@141 863 else
adamc@141 864 (ErrorMsg.errorAt pos "Begin and end tags don't match.";
adamc@141 865 (EFold, pos))
adamc@141 866 end)
adamc@141 867 | LBRACE eexp RBRACE (eexp)
adamc@92 868
adamc@141 869 tag : tagHead attrs (let
adamc@141 870 val pos = s (tagHeadleft, attrsright)
adamc@141 871 in
adamc@141 872 (#1 tagHead,
adamc@141 873 (EApp ((EApp ((EVar (["Basis"], "tag"), pos),
adamc@141 874 (ERecord attrs, pos)), pos),
adamc@141 875 (EApp (#2 tagHead,
adamc@141 876 (ERecord [], pos)), pos)),
adamc@141 877 pos))
adamc@141 878 end)
adamc@141 879
adamc@141 880 tagHead: BEGIN_TAG (let
adamc@141 881 val pos = s (BEGIN_TAGleft, BEGIN_TAGright)
adamc@141 882 in
adamc@141 883 (BEGIN_TAG,
adamc@141 884 (EVar ([], BEGIN_TAG), pos))
adamc@141 885 end)
adamc@141 886 | tagHead LBRACE cexp RBRACE (#1 tagHead, (ECApp (#2 tagHead, cexp), s (tagHeadleft, RBRACEright)))
adamc@92 887
adamc@104 888 attrs : ([])
adamc@104 889 | attr attrs (attr :: attrs)
adamc@104 890
adamc@204 891 attr : SYMBOL EQ attrv ((CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright)), attrv)
adamc@204 892
adamc@104 893 attrv : INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@104 894 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@104 895 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@110 896 | LBRACE eexp RBRACE (eexp)
adamc@226 897
adamc@232 898 query : query1 obopt lopt ofopt (let
adamc@229 899 val loc = s (query1left, query1right)
adamc@230 900
adamc@230 901 val re = (ERecord [((CName "Rows", loc),
adamc@230 902 query1),
adamc@230 903 ((CName "OrderBy", loc),
adamc@231 904 obopt),
adamc@231 905 ((CName "Limit", loc),
adamc@232 906 lopt),
adamc@232 907 ((CName "Offset", loc),
adamc@232 908 ofopt)], loc)
adamc@229 909 in
adamc@230 910 (EApp ((EVar (["Basis"], "sql_query"), loc), re), loc)
adamc@229 911 end)
adamc@204 912
adamc@227 913 query1 : SELECT select FROM tables wopt gopt hopt
adamc@209 914 (let
adamc@204 915 val loc = s (SELECTleft, tablesright)
adamc@207 916
adamc@233 917 val (sel, exps) =
adamc@207 918 case select of
adamc@233 919 Star => (map (fn (nm, _) =>
adamc@233 920 (nm, (CTuple [(CWild (KRecord (KType, loc), loc),
adamc@233 921 loc),
adamc@233 922 (CRecord [], loc)],
adamc@233 923 loc))) tables,
adamc@233 924 [])
adamc@207 925 | Items sis =>
adamc@207 926 let
adamc@207 927 val tabs = map (fn (nm, _) => (nm, (CRecord [], loc))) tables
adamc@233 928 val (tabs, exps) = foldl (amend_select loc) (tabs, []) sis
adamc@207 929 in
adamc@233 930 (map (fn (nm, c) => (nm,
adamc@233 931 (CTuple [c,
adamc@233 932 (CWild (KRecord (KType, loc), loc),
adamc@233 933 loc)], loc))) tabs,
adamc@233 934 exps)
adamc@207 935 end
adamc@207 936
adamc@207 937 val sel = (CRecord sel, loc)
adamc@207 938
adamc@226 939 val grp = case gopt of
adamc@226 940 NONE => (ECApp ((EVar (["Basis"], "sql_subset_all"), loc),
adamc@226 941 (CWild (KRecord (KRecord (KType, loc), loc),
adamc@226 942 loc), loc)), loc)
adamc@226 943 | SOME gis =>
adamc@226 944 let
adamc@226 945 val tabs = map (fn (nm, _) =>
adamc@226 946 (nm, (CRecord [], loc))) tables
adamc@226 947 val tabs = foldl (amend_group loc) tabs gis
adamc@226 948
adamc@226 949 val tabs = map (fn (nm, c) =>
adamc@226 950 (nm,
adamc@226 951 (CTuple [c,
adamc@226 952 (CWild (KRecord (KType, loc),
adamc@226 953 loc),
adamc@226 954 loc)], loc))) tabs
adamc@226 955 in
adamc@226 956 (ECApp ((EVar (["Basis"], "sql_subset"), loc),
adamc@226 957 (CRecord tabs, loc)), loc)
adamc@226 958 end
adamc@226 959
adamc@229 960 val e = (EVar (["Basis"], "sql_query1"), loc)
adamc@209 961 val re = (ERecord [((CName "From", loc),
adamc@209 962 (ERecord tables, loc)),
adamc@209 963 ((CName "Where", loc),
adamc@223 964 wopt),
adamc@224 965 ((CName "GroupBy", loc),
adamc@226 966 grp),
adamc@224 967 ((CName "Having", loc),
adamc@224 968 hopt),
adamc@223 969 ((CName "SelectFields", loc),
adamc@223 970 (ECApp ((EVar (["Basis"], "sql_subset"), loc),
adamc@233 971 sel), loc)),
adamc@233 972 ((CName "SelectExps", loc),
adamc@233 973 (ERecord exps, loc))], loc)
adamc@223 974
adamc@209 975 val e = (EApp (e, re), loc)
adamc@204 976 in
adamc@207 977 e
adamc@204 978 end)
adamc@229 979 | query1 UNION query1 (sql_relop ("union", query11, query12, s (query11left, query12right)))
adamc@229 980 | query1 INTERSECT query1 (sql_relop ("intersect", query11, query12, s (query11left, query12right)))
adamc@229 981 | query1 EXCEPT query1 (sql_relop ("except", query11, query12, s (query11left, query12right)))
adamc@204 982
adamc@204 983 tables : table ([table])
adamc@204 984 | table COMMA tables (table :: tables)
adamc@204 985
adamc@204 986 tname : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@204 987 | LBRACE cexp RBRACE (cexp)
adamc@204 988
adamc@243 989 table : SYMBOL ((CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright)),
adamc@204 990 (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright)))
adamc@204 991 | SYMBOL AS tname (tname, (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright)))
adamc@221 992 | LBRACE LBRACE eexp RBRACE RBRACE AS tname (tname, eexp)
adamc@207 993
adamc@243 994 tident : SYMBOL (CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright))
adamc@207 995 | CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@221 996 | LBRACE LBRACE cexp RBRACE RBRACE (cexp)
adamc@207 997
adamc@207 998 fident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@207 999 | LBRACE cexp RBRACE (cexp)
adamc@207 1000
adamc@207 1001 seli : tident DOT fident (Field (tident, fident))
adamc@233 1002 | sqlexp AS fident (Exp (fident, sqlexp))
adamc@207 1003
adamc@207 1004 selis : seli ([seli])
adamc@207 1005 | seli COMMA selis (seli :: selis)
adamc@207 1006
adamc@207 1007 select : STAR (Star)
adamc@207 1008 | selis (Items selis)
adamc@209 1009
adamc@209 1010 sqlexp : TRUE (sql_inject (EVar (["Basis"], "True"),
adamc@209 1011 EVar (["Basis"], "sql_bool"),
adamc@209 1012 s (TRUEleft, TRUEright)))
adamc@209 1013 | FALSE (sql_inject (EVar (["Basis"], "False"),
adamc@209 1014 EVar (["Basis"], "sql_bool"),
adamc@209 1015 s (FALSEleft, FALSEright)))
adamc@209 1016
adamc@222 1017 | INT (sql_inject (EPrim (Prim.Int INT),
adamc@222 1018 EVar (["Basis"], "sql_int"),
adamc@222 1019 s (INTleft, INTright)))
adamc@222 1020 | FLOAT (sql_inject (EPrim (Prim.Float FLOAT),
adamc@222 1021 EVar (["Basis"], "sql_float"),
adamc@222 1022 s (FLOATleft, FLOATright)))
adamc@229 1023 | STRING (sql_inject (EPrim (Prim.String STRING),
adamc@229 1024 EVar (["Basis"], "sql_string"),
adamc@229 1025 s (STRINGleft, STRINGright)))
adamc@222 1026
adamc@221 1027 | tident DOT fident (let
adamc@221 1028 val loc = s (tidentleft, fidentright)
adamc@221 1029 val e = (EVar (["Basis"], "sql_field"), loc)
adamc@221 1030 val e = (ECApp (e, tident), loc)
adamc@221 1031 in
adamc@221 1032 (ECApp (e, fident), loc)
adamc@221 1033 end)
adamc@234 1034 | CSYMBOL (let
adamc@234 1035 val loc = s (CSYMBOLleft, CSYMBOLright)
adamc@310 1036 in
adamc@310 1037 if !inDml then
adamc@310 1038 let
adamc@310 1039 val e = (EVar (["Basis"], "sql_field"), loc)
adamc@310 1040 val e = (ECApp (e, (CName "T", loc)), loc)
adamc@310 1041 in
adamc@310 1042 (ECApp (e, (CName CSYMBOL, loc)), loc)
adamc@310 1043 end
adamc@310 1044 else
adamc@310 1045 let
adamc@310 1046 val e = (EVar (["Basis"], "sql_exp"), loc)
adamc@310 1047 in
adamc@310 1048 (ECApp (e, (CName CSYMBOL, loc)), loc)
adamc@310 1049 end
adamc@310 1050 end)
adamc@221 1051
adamc@219 1052 | sqlexp EQ sqlexp (sql_compare ("eq", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 1053 | sqlexp NE sqlexp (sql_compare ("ne", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 1054 | sqlexp LT sqlexp (sql_compare ("lt", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 1055 | sqlexp LE sqlexp (sql_compare ("le", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 1056 | sqlexp GT sqlexp (sql_compare ("gt", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 1057 | sqlexp GE sqlexp (sql_compare ("ge", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 1058
adamc@220 1059 | sqlexp CAND sqlexp (sql_binary ("and", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@220 1060 | sqlexp OR sqlexp (sql_binary ("or", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@220 1061 | NOT sqlexp (sql_unary ("not", sqlexp, s (NOTleft, sqlexpright)))
adamc@220 1062
adamc@210 1063 | LBRACE eexp RBRACE (sql_inject (#1 eexp,
adamc@211 1064 EWild,
adamc@210 1065 s (LBRACEleft, RBRACEright)))
adamc@220 1066 | LPAREN sqlexp RPAREN (sqlexp)
adamc@210 1067
adamc@235 1068 | COUNT LPAREN STAR RPAREN (let
adamc@235 1069 val loc = s (COUNTleft, RPARENright)
adamc@235 1070 in
adamc@235 1071 (EApp ((EVar (["Basis"], "sql_count"), loc),
adamc@235 1072 (ERecord [], loc)), loc)
adamc@235 1073 end)
adamc@236 1074 | sqlagg LPAREN sqlexp RPAREN (let
adamc@236 1075 val loc = s (sqlaggleft, RPARENright)
adamc@236 1076
adamc@236 1077 val e = (EApp ((EVar (["Basis"], "sql_" ^ sqlagg), loc),
adamc@236 1078 (EWild, loc)), loc)
adamc@236 1079 val e = (EApp ((EVar (["Basis"], "sql_aggregate"), loc),
adamc@236 1080 e), loc)
adamc@236 1081 in
adamc@236 1082 (EApp (e, sqlexp), loc)
adamc@236 1083 end)
adamc@235 1084
adamc@209 1085 wopt : (sql_inject (EVar (["Basis"], "True"),
adamc@209 1086 EVar (["Basis"], "sql_bool"),
adamc@230 1087 dummy))
adamc@209 1088 | CWHERE sqlexp (sqlexp)
adamc@226 1089
adamc@226 1090 groupi : tident DOT fident (GField (tident, fident))
adamc@226 1091
adamc@226 1092 groupis: groupi ([groupi])
adamc@226 1093 | groupi COMMA groupis (groupi :: groupis)
adamc@226 1094
adamc@226 1095 gopt : (NONE)
adamc@226 1096 | GROUP BY groupis (SOME groupis)
adamc@227 1097
adamc@227 1098 hopt : (sql_inject (EVar (["Basis"], "True"),
adamc@227 1099 EVar (["Basis"], "sql_bool"),
adamc@230 1100 dummy))
adamc@227 1101 | HAVING sqlexp (sqlexp)
adamc@230 1102
adamc@230 1103 obopt : (ECApp ((EVar (["Basis"], "sql_order_by_Nil"), dummy),
adamc@234 1104 (CWild (KRecord (KType, dummy), dummy), dummy)),
adamc@230 1105 dummy)
adamc@230 1106 | ORDER BY obexps (obexps)
adamc@230 1107
adamc@268 1108 obitem : sqlexp diropt (sqlexp, diropt)
adamc@268 1109
adamc@268 1110 obexps : obitem (let
adamc@268 1111 val loc = s (obitemleft, obitemright)
adamc@230 1112
adamc@230 1113 val e' = (ECApp ((EVar (["Basis"], "sql_order_by_Nil"), loc),
adamc@234 1114 (CWild (KRecord (KType, loc), loc), loc)),
adamc@230 1115 loc)
adamc@230 1116 val e = (EApp ((EVar (["Basis"], "sql_order_by_Cons"), loc),
adamc@268 1117 #1 obitem), loc)
adamc@268 1118 val e = (EApp (e, #2 obitem), loc)
adamc@230 1119 in
adamc@230 1120 (EApp (e, e'), loc)
adamc@230 1121 end)
adamc@268 1122 | obitem COMMA obexps (let
adamc@268 1123 val loc = s (obitemleft, obexpsright)
adamc@230 1124
adamc@230 1125 val e = (EApp ((EVar (["Basis"], "sql_order_by_Cons"), loc),
adamc@268 1126 #1 obitem), loc)
adamc@268 1127 val e = (EApp (e, #2 obitem), loc)
adamc@230 1128 in
adamc@230 1129 (EApp (e, obexps), loc)
adamc@230 1130 end)
adamc@231 1131
adamc@268 1132 diropt : (EVar (["Basis"], "sql_asc"), dummy)
adamc@268 1133 | ASC (EVar (["Basis"], "sql_asc"), s (ASCleft, ASCright))
adamc@268 1134 | DESC (EVar (["Basis"], "sql_desc"), s (DESCleft, DESCright))
adamc@268 1135
adamc@231 1136 lopt : (EVar (["Basis"], "sql_no_limit"), dummy)
adamc@232 1137 | LIMIT ALL (EVar (["Basis"], "sql_no_limit"), dummy)
adamc@231 1138 | LIMIT sqlint (let
adamc@231 1139 val loc = s (LIMITleft, sqlintright)
adamc@231 1140 in
adamc@231 1141 (EApp ((EVar (["Basis"], "sql_limit"), loc), sqlint), loc)
adamc@231 1142 end)
adamc@231 1143
adamc@232 1144 ofopt : (EVar (["Basis"], "sql_no_offset"), dummy)
adamc@232 1145 | OFFSET sqlint (let
adamc@232 1146 val loc = s (OFFSETleft, sqlintright)
adamc@232 1147 in
adamc@232 1148 (EApp ((EVar (["Basis"], "sql_offset"), loc), sqlint), loc)
adamc@232 1149 end)
adamc@232 1150
adamc@231 1151 sqlint : INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@231 1152 | LBRACE eexp RBRACE (eexp)
adamc@236 1153
adamc@236 1154 sqlagg : AVG ("avg")
adamc@236 1155 | SUM ("sum")
adamc@236 1156 | MIN ("min")
adamc@236 1157 | MAX ("max")