annotate src/urweb.grm @ 358:583ca86a55a0

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