annotate src/urweb.grm @ 345:b85e6ba56618

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