annotate src/urweb.grm @ 299:5f6a60b7ab00

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