annotate src/urweb.grm @ 286:ffe5b01908ae

'show' type class; htmlification optimizations
author Adam Chlipala <adamc@hcoop.net>
date Sun, 07 Sep 2008 10:48:51 -0400
parents bacd0ba869e1
children 5f6a60b7ab00
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@256 610 | eexp EQ eexp (native_op ("eq", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@257 611 | eexp NE eexp (native_op ("ne", eexp1, eexp2, s (eexp1left, eexp2right)))
adamc@8 612
adamc@241 613 eargs : earg (earg)
adamc@241 614 | eargl (eargl)
adamc@241 615
adamc@241 616 eargl : eargp eargp (eargp1 o eargp2)
adamc@241 617 | eargp eargl (eargp o eargl)
adamc@241 618
adamc@242 619 eargl2 : (fn x => x)
adamc@242 620 | eargp eargl2 (eargp o eargl2)
adamc@242 621
adamc@241 622 earg : SYMBOL kcolon kind (fn (e, t) =>
adamc@241 623 let
adamc@241 624 val loc = s (SYMBOLleft, kindright)
adamc@241 625 in
adamc@241 626 ((ECAbs (kcolon, SYMBOL, kind, e), loc),
adamc@241 627 (TCFun (kcolon, SYMBOL, kind, t), loc))
adamc@241 628 end)
adamc@241 629 | SYMBOL COLON cexp (fn (e, t) =>
adamc@241 630 let
adamc@241 631 val loc = s (SYMBOLleft, cexpright)
adamc@241 632 in
adamc@241 633 ((EAbs (SYMBOL, SOME cexp, e), loc),
adamc@241 634 (TFun (cexp, t), loc))
adamc@241 635 end)
adamc@241 636 | UNDER COLON cexp (fn (e, t) =>
adamc@241 637 let
adamc@241 638 val loc = s (UNDERleft, cexpright)
adamc@241 639 in
adamc@241 640 ((EAbs ("_", SOME cexp, e), loc),
adamc@241 641 (TFun (cexp, t), loc))
adamc@241 642 end)
adamc@241 643 | eargp (eargp)
adamc@241 644
adamc@241 645 eargp : SYMBOL (fn (e, t) =>
adamc@241 646 let
adamc@241 647 val loc = s (SYMBOLleft, SYMBOLright)
adamc@241 648 in
adamc@241 649 ((EAbs (SYMBOL, NONE, e), loc),
adamc@241 650 (TFun ((CWild (KType, loc), loc), t), loc))
adamc@241 651 end)
adamc@241 652 | UNIT (fn (e, t) =>
adamc@241 653 let
adamc@241 654 val loc = s (UNITleft, UNITright)
adamc@241 655 val t' = (TRecord (CRecord [], loc), loc)
adamc@241 656 in
adamc@241 657 ((EAbs ("_", SOME t', e), loc),
adamc@241 658 (TFun (t', t), loc))
adamc@241 659 end)
adamc@241 660 | UNDER (fn (e, t) =>
adamc@241 661 let
adamc@241 662 val loc = s (UNDERleft, UNDERright)
adamc@241 663 in
adamc@241 664 ((EAbs ("_", NONE, e), loc),
adamc@241 665 (TFun ((CWild (KType, loc), loc), t), loc))
adamc@241 666 end)
adamc@241 667 | LPAREN SYMBOL kcolon kind RPAREN(fn (e, t) =>
adamc@241 668 let
adamc@241 669 val loc = s (LPARENleft, RPARENright)
adamc@241 670 in
adamc@241 671 ((ECAbs (kcolon, SYMBOL, kind, e), loc),
adamc@241 672 (TCFun (kcolon, SYMBOL, kind, t), loc))
adamc@241 673 end)
adamc@241 674 | LPAREN SYMBOL COLON cexp RPAREN (fn (e, t) =>
adamc@241 675 let
adamc@241 676 val loc = s (LPARENleft, RPARENright)
adamc@241 677 in
adamc@241 678 ((EAbs (SYMBOL, SOME cexp, e), loc),
adamc@241 679 (TFun (cexp, t), loc))
adamc@241 680 end)
adamc@241 681 | LPAREN UNDER COLON cexp RPAREN (fn (e, t) =>
adamc@241 682 let
adamc@241 683 val loc = s (LPARENleft, RPARENright)
adamc@241 684 in
adamc@241 685 ((EAbs ("_", SOME cexp, e), loc),
adamc@241 686 (TFun (cexp, t), loc))
adamc@241 687 end)
adamc@241 688
adamc@8 689 eterm : LPAREN eexp RPAREN (#1 eexp, s (LPARENleft, RPARENright))
adamc@195 690 | LPAREN etuple RPAREN (let
adamc@195 691 val loc = s (LPARENleft, RPARENright)
adamc@195 692 in
adamc@195 693 (ERecord (ListUtil.mapi (fn (i, e) =>
adamc@195 694 ((CName (Int.toString (i + 1)), loc),
adamc@195 695 e)) etuple), loc)
adamc@195 696 end)
adamc@8 697
adamc@34 698 | path (EVar path, s (pathleft, pathright))
adamc@156 699 | cpath (EVar cpath, s (cpathleft, cpathright))
adamc@12 700 | LBRACE rexp RBRACE (ERecord rexp, s (LBRACEleft, RBRACEright))
adamc@110 701 | UNIT (ERecord [], s (UNITleft, UNITright))
adamc@12 702
adamc@14 703 | INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@14 704 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@14 705 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@14 706
adamc@200 707 | path DOT idents (let
adamc@200 708 val loc = s (pathleft, identsright)
adamc@200 709 in
adamc@200 710 foldl (fn (ident, e) =>
adamc@200 711 (EField (e, ident), loc))
adamc@200 712 (EVar path, s (pathleft, pathright)) idents
adamc@200 713 end)
adamc@71 714 | FOLD (EFold, s (FOLDleft, FOLDright))
adamc@71 715
adamc@91 716 | XML_BEGIN xml XML_END (xml)
adamc@91 717 | XML_BEGIN XML_END (EApp ((EVar (["Basis"], "cdata"), s (XML_BEGINleft, XML_ENDright)),
adamc@91 718 (EPrim (Prim.String ""), s (XML_BEGINleft, XML_ENDright))),
adamc@91 719 s (XML_BEGINleft, XML_ENDright))
adamc@204 720 | LPAREN query RPAREN (query)
adamc@211 721 | UNDER (EWild, s (UNDERleft, UNDERright))
adamc@91 722
adamc@200 723 idents : ident ([ident])
adamc@200 724 | ident DOT idents (ident :: idents)
adamc@200 725
adamc@195 726 etuple : eexp COMMA eexp ([eexp1, eexp2])
adamc@195 727 | eexp COMMA etuple (eexp :: etuple)
adamc@195 728
adamc@170 729 branch : pat DARROW eexp (pat, eexp)
adamc@170 730
adamc@170 731 branchs: ([])
adamc@170 732 | BAR branch branchs (branch :: branchs)
adamc@170 733
adamc@170 734 pat : pterm (pterm)
adamc@170 735 | cpath pterm (PCon (#1 cpath, #2 cpath, SOME pterm), s (cpathleft, ptermright))
adamc@170 736
adamc@170 737 pterm : SYMBOL (PVar SYMBOL, s (SYMBOLleft, SYMBOLright))
adamc@170 738 | cpath (PCon (#1 cpath, #2 cpath, NONE), s (cpathleft, cpathright))
adamc@170 739 | UNDER (PWild, s (UNDERleft, UNDERright))
adamc@173 740 | INT (PPrim (Prim.Int INT), s (INTleft, INTright))
adamc@173 741 | STRING (PPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@170 742 | LPAREN pat RPAREN (pat)
adamc@174 743 | LBRACE RBRACE (PRecord ([], false), s (LBRACEleft, RBRACEright))
adamc@174 744 | UNIT (PRecord ([], false), s (UNITleft, UNITright))
adamc@174 745 | LBRACE rpat RBRACE (PRecord rpat, s (LBRACEleft, RBRACEright))
adamc@195 746 | LPAREN ptuple RPAREN (PRecord (ListUtil.mapi (fn (i, p) => (Int.toString (i + 1), p)) ptuple,
adamc@195 747 false),
adamc@195 748 s (LPARENleft, RPARENright))
adamc@174 749
adamc@175 750 rpat : CSYMBOL EQ pat ([(CSYMBOL, pat)], false)
adamc@243 751 | INT EQ pat ([(Int64.toString INT, pat)], false)
adamc@174 752 | DOTDOTDOT ([], true)
adamc@175 753 | CSYMBOL EQ pat COMMA rpat ((CSYMBOL, pat) :: #1 rpat, #2 rpat)
adamc@243 754 | INT EQ pat COMMA rpat ((Int64.toString INT, pat) :: #1 rpat, #2 rpat)
adamc@170 755
adamc@195 756 ptuple : pat COMMA pat ([pat1, pat2])
adamc@195 757 | pat COMMA ptuple (pat :: ptuple)
adamc@195 758
adamc@12 759 rexp : ([])
adamc@12 760 | ident EQ eexp ([(ident, eexp)])
adamc@12 761 | ident EQ eexp COMMA rexp ((ident, eexp) :: rexp)
adamc@91 762
adamc@141 763 xml : xmlOne xml (let
adamc@141 764 val pos = s (xmlOneleft, xmlright)
adamc@141 765 in
adamc@141 766 (EApp ((EApp (
adamc@141 767 (EVar (["Basis"], "join"), pos),
adamc@91 768 xmlOne), pos),
adamc@141 769 xml), pos)
adamc@141 770 end)
adamc@141 771 | xmlOne (xmlOne)
adamc@91 772
adamc@141 773 xmlOne : NOTAGS (EApp ((EVar (["Basis"], "cdata"), s (NOTAGSleft, NOTAGSright)),
adamc@141 774 (EPrim (Prim.String NOTAGS), s (NOTAGSleft, NOTAGSright))),
adamc@141 775 s (NOTAGSleft, NOTAGSright))
adamc@141 776 | tag DIVIDE GT (let
adamc@141 777 val pos = s (tagleft, GTright)
adamc@141 778 in
adamc@141 779 (EApp (#2 tag,
adamc@141 780 (EApp ((EVar (["Basis"], "cdata"), pos),
adamc@141 781 (EPrim (Prim.String ""), pos)),
adamc@141 782 pos)), pos)
adamc@141 783 end)
adamc@141 784
adamc@141 785 | tag GT xml END_TAG (let
adamc@141 786 val pos = s (tagleft, GTright)
adamc@141 787 in
adamc@141 788 if #1 tag = END_TAG then
adamc@141 789 if END_TAG = "lform" then
adamc@141 790 (EApp ((EVar (["Basis"], "lform"), pos),
adamc@141 791 xml), pos)
adamc@141 792 else
adamc@141 793 (EApp (#2 tag, xml), pos)
adamc@141 794 else
adamc@141 795 (ErrorMsg.errorAt pos "Begin and end tags don't match.";
adamc@141 796 (EFold, pos))
adamc@141 797 end)
adamc@141 798 | LBRACE eexp RBRACE (eexp)
adamc@92 799
adamc@141 800 tag : tagHead attrs (let
adamc@141 801 val pos = s (tagHeadleft, attrsright)
adamc@141 802 in
adamc@141 803 (#1 tagHead,
adamc@141 804 (EApp ((EApp ((EVar (["Basis"], "tag"), pos),
adamc@141 805 (ERecord attrs, pos)), pos),
adamc@141 806 (EApp (#2 tagHead,
adamc@141 807 (ERecord [], pos)), pos)),
adamc@141 808 pos))
adamc@141 809 end)
adamc@141 810
adamc@141 811 tagHead: BEGIN_TAG (let
adamc@141 812 val pos = s (BEGIN_TAGleft, BEGIN_TAGright)
adamc@141 813 in
adamc@141 814 (BEGIN_TAG,
adamc@141 815 (EVar ([], BEGIN_TAG), pos))
adamc@141 816 end)
adamc@141 817 | tagHead LBRACE cexp RBRACE (#1 tagHead, (ECApp (#2 tagHead, cexp), s (tagHeadleft, RBRACEright)))
adamc@92 818
adamc@104 819 attrs : ([])
adamc@104 820 | attr attrs (attr :: attrs)
adamc@104 821
adamc@204 822 attr : SYMBOL EQ attrv ((CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright)), attrv)
adamc@204 823
adamc@104 824 attrv : INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@104 825 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@104 826 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@110 827 | LBRACE eexp RBRACE (eexp)
adamc@226 828
adamc@232 829 query : query1 obopt lopt ofopt (let
adamc@229 830 val loc = s (query1left, query1right)
adamc@230 831
adamc@230 832 val re = (ERecord [((CName "Rows", loc),
adamc@230 833 query1),
adamc@230 834 ((CName "OrderBy", loc),
adamc@231 835 obopt),
adamc@231 836 ((CName "Limit", loc),
adamc@232 837 lopt),
adamc@232 838 ((CName "Offset", loc),
adamc@232 839 ofopt)], loc)
adamc@229 840 in
adamc@230 841 (EApp ((EVar (["Basis"], "sql_query"), loc), re), loc)
adamc@229 842 end)
adamc@204 843
adamc@227 844 query1 : SELECT select FROM tables wopt gopt hopt
adamc@209 845 (let
adamc@204 846 val loc = s (SELECTleft, tablesright)
adamc@207 847
adamc@233 848 val (sel, exps) =
adamc@207 849 case select of
adamc@233 850 Star => (map (fn (nm, _) =>
adamc@233 851 (nm, (CTuple [(CWild (KRecord (KType, loc), loc),
adamc@233 852 loc),
adamc@233 853 (CRecord [], loc)],
adamc@233 854 loc))) tables,
adamc@233 855 [])
adamc@207 856 | Items sis =>
adamc@207 857 let
adamc@207 858 val tabs = map (fn (nm, _) => (nm, (CRecord [], loc))) tables
adamc@233 859 val (tabs, exps) = foldl (amend_select loc) (tabs, []) sis
adamc@207 860 in
adamc@233 861 (map (fn (nm, c) => (nm,
adamc@233 862 (CTuple [c,
adamc@233 863 (CWild (KRecord (KType, loc), loc),
adamc@233 864 loc)], loc))) tabs,
adamc@233 865 exps)
adamc@207 866 end
adamc@207 867
adamc@207 868 val sel = (CRecord sel, loc)
adamc@207 869
adamc@226 870 val grp = case gopt of
adamc@226 871 NONE => (ECApp ((EVar (["Basis"], "sql_subset_all"), loc),
adamc@226 872 (CWild (KRecord (KRecord (KType, loc), loc),
adamc@226 873 loc), loc)), loc)
adamc@226 874 | SOME gis =>
adamc@226 875 let
adamc@226 876 val tabs = map (fn (nm, _) =>
adamc@226 877 (nm, (CRecord [], loc))) tables
adamc@226 878 val tabs = foldl (amend_group loc) tabs gis
adamc@226 879
adamc@226 880 val tabs = map (fn (nm, c) =>
adamc@226 881 (nm,
adamc@226 882 (CTuple [c,
adamc@226 883 (CWild (KRecord (KType, loc),
adamc@226 884 loc),
adamc@226 885 loc)], loc))) tabs
adamc@226 886 in
adamc@226 887 (ECApp ((EVar (["Basis"], "sql_subset"), loc),
adamc@226 888 (CRecord tabs, loc)), loc)
adamc@226 889 end
adamc@226 890
adamc@229 891 val e = (EVar (["Basis"], "sql_query1"), loc)
adamc@209 892 val re = (ERecord [((CName "From", loc),
adamc@209 893 (ERecord tables, loc)),
adamc@209 894 ((CName "Where", loc),
adamc@223 895 wopt),
adamc@224 896 ((CName "GroupBy", loc),
adamc@226 897 grp),
adamc@224 898 ((CName "Having", loc),
adamc@224 899 hopt),
adamc@223 900 ((CName "SelectFields", loc),
adamc@223 901 (ECApp ((EVar (["Basis"], "sql_subset"), loc),
adamc@233 902 sel), loc)),
adamc@233 903 ((CName "SelectExps", loc),
adamc@233 904 (ERecord exps, loc))], loc)
adamc@223 905
adamc@209 906 val e = (EApp (e, re), loc)
adamc@204 907 in
adamc@207 908 e
adamc@204 909 end)
adamc@229 910 | query1 UNION query1 (sql_relop ("union", query11, query12, s (query11left, query12right)))
adamc@229 911 | query1 INTERSECT query1 (sql_relop ("intersect", query11, query12, s (query11left, query12right)))
adamc@229 912 | query1 EXCEPT query1 (sql_relop ("except", query11, query12, s (query11left, query12right)))
adamc@204 913
adamc@204 914 tables : table ([table])
adamc@204 915 | table COMMA tables (table :: tables)
adamc@204 916
adamc@204 917 tname : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@204 918 | LBRACE cexp RBRACE (cexp)
adamc@204 919
adamc@243 920 table : SYMBOL ((CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright)),
adamc@204 921 (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright)))
adamc@204 922 | SYMBOL AS tname (tname, (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright)))
adamc@221 923 | LBRACE LBRACE eexp RBRACE RBRACE AS tname (tname, eexp)
adamc@207 924
adamc@243 925 tident : SYMBOL (CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright))
adamc@207 926 | CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@221 927 | LBRACE LBRACE cexp RBRACE RBRACE (cexp)
adamc@207 928
adamc@207 929 fident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@207 930 | LBRACE cexp RBRACE (cexp)
adamc@207 931
adamc@207 932 seli : tident DOT fident (Field (tident, fident))
adamc@233 933 | sqlexp AS fident (Exp (fident, sqlexp))
adamc@207 934
adamc@207 935 selis : seli ([seli])
adamc@207 936 | seli COMMA selis (seli :: selis)
adamc@207 937
adamc@207 938 select : STAR (Star)
adamc@207 939 | selis (Items selis)
adamc@209 940
adamc@209 941 sqlexp : TRUE (sql_inject (EVar (["Basis"], "True"),
adamc@209 942 EVar (["Basis"], "sql_bool"),
adamc@209 943 s (TRUEleft, TRUEright)))
adamc@209 944 | FALSE (sql_inject (EVar (["Basis"], "False"),
adamc@209 945 EVar (["Basis"], "sql_bool"),
adamc@209 946 s (FALSEleft, FALSEright)))
adamc@209 947
adamc@222 948 | INT (sql_inject (EPrim (Prim.Int INT),
adamc@222 949 EVar (["Basis"], "sql_int"),
adamc@222 950 s (INTleft, INTright)))
adamc@222 951 | FLOAT (sql_inject (EPrim (Prim.Float FLOAT),
adamc@222 952 EVar (["Basis"], "sql_float"),
adamc@222 953 s (FLOATleft, FLOATright)))
adamc@229 954 | STRING (sql_inject (EPrim (Prim.String STRING),
adamc@229 955 EVar (["Basis"], "sql_string"),
adamc@229 956 s (STRINGleft, STRINGright)))
adamc@222 957
adamc@221 958 | tident DOT fident (let
adamc@221 959 val loc = s (tidentleft, fidentright)
adamc@221 960 val e = (EVar (["Basis"], "sql_field"), loc)
adamc@221 961 val e = (ECApp (e, tident), loc)
adamc@221 962 in
adamc@221 963 (ECApp (e, fident), loc)
adamc@221 964 end)
adamc@234 965 | CSYMBOL (let
adamc@234 966 val loc = s (CSYMBOLleft, CSYMBOLright)
adamc@234 967 val e = (EVar (["Basis"], "sql_exp"), loc)
adamc@234 968 in
adamc@234 969 (ECApp (e, (CName CSYMBOL, loc)), loc)
adamc@234 970 end)
adamc@221 971
adamc@219 972 | sqlexp EQ sqlexp (sql_compare ("eq", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 973 | sqlexp NE sqlexp (sql_compare ("ne", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 974 | sqlexp LT sqlexp (sql_compare ("lt", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 975 | sqlexp LE sqlexp (sql_compare ("le", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 976 | sqlexp GT sqlexp (sql_compare ("gt", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 977 | sqlexp GE sqlexp (sql_compare ("ge", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 978
adamc@220 979 | sqlexp CAND sqlexp (sql_binary ("and", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@220 980 | sqlexp OR sqlexp (sql_binary ("or", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@220 981 | NOT sqlexp (sql_unary ("not", sqlexp, s (NOTleft, sqlexpright)))
adamc@220 982
adamc@210 983 | LBRACE eexp RBRACE (sql_inject (#1 eexp,
adamc@211 984 EWild,
adamc@210 985 s (LBRACEleft, RBRACEright)))
adamc@220 986 | LPAREN sqlexp RPAREN (sqlexp)
adamc@210 987
adamc@235 988 | COUNT LPAREN STAR RPAREN (let
adamc@235 989 val loc = s (COUNTleft, RPARENright)
adamc@235 990 in
adamc@235 991 (EApp ((EVar (["Basis"], "sql_count"), loc),
adamc@235 992 (ERecord [], loc)), loc)
adamc@235 993 end)
adamc@236 994 | sqlagg LPAREN sqlexp RPAREN (let
adamc@236 995 val loc = s (sqlaggleft, RPARENright)
adamc@236 996
adamc@236 997 val e = (EApp ((EVar (["Basis"], "sql_" ^ sqlagg), loc),
adamc@236 998 (EWild, loc)), loc)
adamc@236 999 val e = (EApp ((EVar (["Basis"], "sql_aggregate"), loc),
adamc@236 1000 e), loc)
adamc@236 1001 in
adamc@236 1002 (EApp (e, sqlexp), loc)
adamc@236 1003 end)
adamc@235 1004
adamc@209 1005 wopt : (sql_inject (EVar (["Basis"], "True"),
adamc@209 1006 EVar (["Basis"], "sql_bool"),
adamc@230 1007 dummy))
adamc@209 1008 | CWHERE sqlexp (sqlexp)
adamc@226 1009
adamc@226 1010 groupi : tident DOT fident (GField (tident, fident))
adamc@226 1011
adamc@226 1012 groupis: groupi ([groupi])
adamc@226 1013 | groupi COMMA groupis (groupi :: groupis)
adamc@226 1014
adamc@226 1015 gopt : (NONE)
adamc@226 1016 | GROUP BY groupis (SOME groupis)
adamc@227 1017
adamc@227 1018 hopt : (sql_inject (EVar (["Basis"], "True"),
adamc@227 1019 EVar (["Basis"], "sql_bool"),
adamc@230 1020 dummy))
adamc@227 1021 | HAVING sqlexp (sqlexp)
adamc@230 1022
adamc@230 1023 obopt : (ECApp ((EVar (["Basis"], "sql_order_by_Nil"), dummy),
adamc@234 1024 (CWild (KRecord (KType, dummy), dummy), dummy)),
adamc@230 1025 dummy)
adamc@230 1026 | ORDER BY obexps (obexps)
adamc@230 1027
adamc@268 1028 obitem : sqlexp diropt (sqlexp, diropt)
adamc@268 1029
adamc@268 1030 obexps : obitem (let
adamc@268 1031 val loc = s (obitemleft, obitemright)
adamc@230 1032
adamc@230 1033 val e' = (ECApp ((EVar (["Basis"], "sql_order_by_Nil"), loc),
adamc@234 1034 (CWild (KRecord (KType, loc), loc), loc)),
adamc@230 1035 loc)
adamc@230 1036 val e = (EApp ((EVar (["Basis"], "sql_order_by_Cons"), loc),
adamc@268 1037 #1 obitem), loc)
adamc@268 1038 val e = (EApp (e, #2 obitem), loc)
adamc@230 1039 in
adamc@230 1040 (EApp (e, e'), loc)
adamc@230 1041 end)
adamc@268 1042 | obitem COMMA obexps (let
adamc@268 1043 val loc = s (obitemleft, obexpsright)
adamc@230 1044
adamc@230 1045 val e = (EApp ((EVar (["Basis"], "sql_order_by_Cons"), loc),
adamc@268 1046 #1 obitem), loc)
adamc@268 1047 val e = (EApp (e, #2 obitem), loc)
adamc@230 1048 in
adamc@230 1049 (EApp (e, obexps), loc)
adamc@230 1050 end)
adamc@231 1051
adamc@268 1052 diropt : (EVar (["Basis"], "sql_asc"), dummy)
adamc@268 1053 | ASC (EVar (["Basis"], "sql_asc"), s (ASCleft, ASCright))
adamc@268 1054 | DESC (EVar (["Basis"], "sql_desc"), s (DESCleft, DESCright))
adamc@268 1055
adamc@231 1056 lopt : (EVar (["Basis"], "sql_no_limit"), dummy)
adamc@232 1057 | LIMIT ALL (EVar (["Basis"], "sql_no_limit"), dummy)
adamc@231 1058 | LIMIT sqlint (let
adamc@231 1059 val loc = s (LIMITleft, sqlintright)
adamc@231 1060 in
adamc@231 1061 (EApp ((EVar (["Basis"], "sql_limit"), loc), sqlint), loc)
adamc@231 1062 end)
adamc@231 1063
adamc@232 1064 ofopt : (EVar (["Basis"], "sql_no_offset"), dummy)
adamc@232 1065 | OFFSET sqlint (let
adamc@232 1066 val loc = s (OFFSETleft, sqlintright)
adamc@232 1067 in
adamc@232 1068 (EApp ((EVar (["Basis"], "sql_offset"), loc), sqlint), loc)
adamc@232 1069 end)
adamc@232 1070
adamc@231 1071 sqlint : INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@231 1072 | LBRACE eexp RBRACE (eexp)
adamc@236 1073
adamc@236 1074 sqlagg : AVG ("avg")
adamc@236 1075 | SUM ("sum")
adamc@236 1076 | MIN ("min")
adamc@236 1077 | MAX ("max")