annotate src/urweb.grm @ 244:71bafe66dbe1

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