annotate src/lacweb.grm @ 220:2b665e822e9a

SQL boolean operators
author Adam Chlipala <adamc@hcoop.net>
date Sat, 16 Aug 2008 17:35:28 -0400
parents 5292c0113024
children 79819a6346e2
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@1 28 (* Grammar for Laconic/Web programs *)
adamc@1 29
adamc@4 30 open Source
adamc@1 31
adamc@1 32 val s = ErrorMsg.spanOf
adamc@1 33
adamc@204 34 fun capitalize "" = ""
adamc@204 35 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@104 36
adamc@203 37 fun entable t =
adamc@203 38 case #1 t of
adamc@203 39 TRecord c => c
adamc@203 40 | _ => t
adamc@203 41
adamc@207 42 datatype select_item =
adamc@207 43 Field of con * con
adamc@207 44
adamc@207 45 datatype select =
adamc@207 46 Star
adamc@207 47 | Items of select_item list
adamc@207 48
adamc@207 49 fun eqTnames ((c1, _), (c2, _)) =
adamc@207 50 case (c1, c2) of
adamc@207 51 (CVar (ms1, x1), CVar (ms2, x2)) => ms1 = ms2 andalso x1 = x2
adamc@207 52 | (CName x1, CName x2) => x1 = x2
adamc@207 53 | _ => false
adamc@207 54
adamc@207 55 fun amend_select loc (si, tabs) =
adamc@207 56 let
adamc@207 57 val (tx, c) = case si of
adamc@207 58 Field (tx, fx) => (tx, (CRecord ([(fx, (CWild (KType, loc), loc))]), loc))
adamc@207 59
adamc@207 60 val (tabs, found) = ListUtil.foldlMap (fn ((tx', c'), found) =>
adamc@207 61 if eqTnames (tx, tx') then
adamc@207 62 ((tx', (CConcat (c, c'), loc)), true)
adamc@207 63 else
adamc@207 64 ((tx', c'), found))
adamc@207 65 false tabs
adamc@207 66 in
adamc@207 67 if found then
adamc@207 68 ()
adamc@207 69 else
adamc@207 70 ErrorMsg.errorAt loc "Select of field from unbound table";
adamc@207 71
adamc@207 72 tabs
adamc@207 73 end
adamc@207 74
adamc@209 75 fun sql_inject (v, t, loc) =
adamc@209 76 let
adamc@209 77 val e = (EApp ((EVar (["Basis"], "sql_inject"), loc), (v, loc)), loc)
adamc@209 78 in
adamc@209 79 (EApp (e, (t, loc)), loc)
adamc@209 80 end
adamc@209 81
adamc@219 82 fun sql_compare (oper, sqlexp1, sqlexp2, loc) =
adamc@219 83 let
adamc@219 84 val e = (EVar (["Basis"], "sql_comparison"), loc)
adamc@219 85 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper), loc)), loc)
adamc@219 86 val e = (EApp (e, sqlexp1), loc)
adamc@219 87 val e = (EApp (e, sqlexp2), loc)
adamc@219 88 in
adamc@220 89 (EApp (e, (EWild, loc)), loc)
adamc@220 90 end
adamc@220 91
adamc@220 92 fun sql_binary (oper, sqlexp1, sqlexp2, loc) =
adamc@220 93 let
adamc@220 94 val e = (EVar (["Basis"], "sql_binary"), loc)
adamc@220 95 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper), loc)), loc)
adamc@220 96 val e = (EApp (e, sqlexp1), loc)
adamc@220 97 in
adamc@220 98 (EApp (e, sqlexp2), loc)
adamc@220 99 end
adamc@220 100
adamc@220 101 fun sql_unary (oper, sqlexp, loc) =
adamc@220 102 let
adamc@220 103 val e = (EVar (["Basis"], "sql_unary"), loc)
adamc@220 104 val e = (EApp (e, (EVar (["Basis"], "sql_" ^ oper), loc)), loc)
adamc@220 105 in
adamc@220 106 (EApp (e, sqlexp), loc)
adamc@219 107 end
adamc@219 108
adamc@1 109 %%
adamc@1 110 %header (functor LacwebLrValsFn(structure Token : TOKEN))
adamc@1 111
adamc@1 112 %term
adamc@1 113 EOF
adamc@14 114 | STRING of string | INT of Int64.int | FLOAT of Real64.real
adamc@1 115 | SYMBOL of string | CSYMBOL of string
adamc@1 116 | LPAREN | RPAREN | LBRACK | RBRACK | LBRACE | RBRACE
adamc@156 117 | EQ | COMMA | COLON | DCOLON | TCOLON | DOT | HASH | UNDER | UNDERUNDER | BAR
adamc@219 118 | DIVIDE | DOTDOTDOT
adamc@211 119 | CON | LTYPE | VAL | REC | AND | FOLD | UNIT | KUNIT | CLASS
adamc@156 120 | DATATYPE | OF
adamc@7 121 | TYPE | NAME
adamc@195 122 | ARROW | LARROW | DARROW | STAR
adamc@149 123 | FN | PLUSPLUS | MINUSMINUS | DOLLAR | TWIDDLE
adamc@88 124 | STRUCTURE | SIGNATURE | STRUCT | SIG | END | FUNCTOR | WHERE | EXTERN
adamc@203 125 | INCLUDE | OPEN | CONSTRAINT | CONSTRAINTS | EXPORT | TABLE
adamc@190 126 | CASE | IF | THEN | ELSE
adamc@1 127
adamc@91 128 | XML_BEGIN of string | XML_END
adamc@91 129 | NOTAGS of string
adamc@91 130 | BEGIN_TAG of string | END_TAG of string
adamc@91 131
adamc@209 132 | SELECT | FROM | AS | CWHERE
adamc@220 133 | TRUE | FALSE | CAND | OR | NOT
adamc@219 134 | NE | LT | LE | GT | GE
adamc@204 135
adamc@30 136 %nonterm
adamc@1 137 file of decl list
adamc@1 138 | decls of decl list
adamc@1 139 | decl of decl
adamc@123 140 | vali of string * con option * exp
adamc@123 141 | valis of (string * con option * exp) list
adamc@1 142
adamc@191 143 | dargs of string list
adamc@156 144 | barOpt of unit
adamc@156 145 | dcons of (string * con option) list
adamc@156 146 | dcon of string * con option
adamc@156 147
adamc@30 148 | sgn of sgn
adamc@42 149 | sgntm of sgn
adamc@30 150 | sgi of sgn_item
adamc@30 151 | sgis of sgn_item list
adamc@30 152
adamc@30 153 | str of str
adamc@30 154
adamc@1 155 | kind of kind
adamc@207 156 | ktuple of kind list
adamc@1 157 | kcolon of explicitness
adamc@1 158
adamc@34 159 | path of string list * string
adamc@156 160 | cpath of string list * string
adamc@34 161 | spath of str
adamc@59 162 | mpath of string list
adamc@34 163
adamc@1 164 | cexp of con
adamc@1 165 | capps of con
adamc@1 166 | cterm of con
adamc@195 167 | ctuple of con list
adamc@207 168 | ctuplev of con list
adamc@1 169 | ident of con
adamc@200 170 | idents of con list
adamc@1 171 | rcon of (con * con) list
adamc@83 172 | rconn of (con * con) list
adamc@1 173 | rcone of (con * con) list
adamc@1 174
adamc@8 175 | eexp of exp
adamc@8 176 | eapps of exp
adamc@8 177 | eterm of exp
adamc@195 178 | etuple of exp list
adamc@12 179 | rexp of (con * exp) list
adamc@91 180 | xml of exp
adamc@91 181 | xmlOne of exp
adamc@141 182 | tag of string * exp
adamc@141 183 | tagHead of string * exp
adamc@8 184
adamc@170 185 | branch of pat * exp
adamc@170 186 | branchs of (pat * exp) list
adamc@170 187 | pat of pat
adamc@170 188 | pterm of pat
adamc@174 189 | rpat of (string * pat) list * bool
adamc@195 190 | ptuple of pat list
adamc@170 191
adamc@104 192 | attrs of (con * exp) list
adamc@104 193 | attr of con * exp
adamc@104 194 | attrv of exp
adamc@104 195
adamc@204 196 | query of exp
adamc@204 197 | tables of (con * exp) list
adamc@204 198 | tname of con
adamc@204 199 | table of con * exp
adamc@207 200 | tident of con
adamc@207 201 | fident of con
adamc@207 202 | seli of select_item
adamc@207 203 | selis of select_item list
adamc@207 204 | select of select
adamc@209 205 | sqlexp of exp
adamc@209 206 | wopt of exp
adamc@207 207
adamc@204 208
adamc@1 209 %verbose (* print summary of errors *)
adamc@1 210 %pos int (* positions *)
adamc@1 211 %start file
adamc@1 212 %pure
adamc@1 213 %eop EOF
adamc@1 214 %noshift EOF
adamc@1 215
adamc@1 216 %name Lacweb
adamc@1 217
adamc@195 218 %nonassoc IF THEN ELSE
adamc@1 219 %nonassoc DARROW
adamc@1 220 %nonassoc COLON
adamc@6 221 %nonassoc DCOLON TCOLON
adamc@1 222 %right COMMA
adamc@220 223 %right OR
adamc@220 224 %right CAND
adamc@219 225 %nonassoc EQ NE LT LE GT GE
adamc@1 226 %right ARROW LARROW
adamc@149 227 %right PLUSPLUS MINUSMINUS
adamc@195 228 %right STAR
adamc@220 229 %left NOT
adamc@84 230 %nonassoc TWIDDLE
adamc@1 231 %nonassoc DOLLAR
adamc@1 232 %left DOT
adamc@1 233
adamc@1 234 %%
adamc@1 235
adamc@1 236 file : decls (decls)
adamc@54 237 | SIG sgis ([(DSgn ("?", (SgnConst sgis, s (SIGleft, sgisright))),
adamc@54 238 s (SIGleft, sgisright))])
adamc@1 239
adamc@1 240 decls : ([])
adamc@1 241 | decl decls (decl :: decls)
adamc@1 242
adamc@1 243 decl : CON SYMBOL EQ cexp (DCon (SYMBOL, NONE, cexp), s (CONleft, cexpright))
adamc@1 244 | CON SYMBOL DCOLON kind EQ cexp (DCon (SYMBOL, SOME kind, cexp), s (CONleft, cexpright))
adamc@7 245 | LTYPE SYMBOL EQ cexp (DCon (SYMBOL, SOME (KType, s (LTYPEleft, cexpright)), cexp),
adamc@7 246 s (LTYPEleft, cexpright))
adamc@191 247 | DATATYPE SYMBOL dargs EQ barOpt dcons(DDatatype (SYMBOL, dargs, dcons), s (DATATYPEleft, dconsright))
adamc@191 248 | DATATYPE SYMBOL dargs EQ DATATYPE CSYMBOL DOT path
adamc@191 249 (case dargs of
adamc@191 250 [] => (DDatatypeImp (SYMBOL, CSYMBOL :: #1 path, #2 path), s (DATATYPEleft, pathright))
adamc@191 251 | _ => raise Fail "Arguments specified for imported datatype")
adamc@123 252 | VAL vali (DVal vali, s (VALleft, valiright))
adamc@123 253 | VAL REC valis (DValRec valis, s (VALleft, valisright))
adamc@1 254
adamc@30 255 | SIGNATURE CSYMBOL EQ sgn (DSgn (CSYMBOL, sgn), s (SIGNATUREleft, sgnright))
adamc@30 256 | STRUCTURE CSYMBOL EQ str (DStr (CSYMBOL, NONE, str), s (STRUCTUREleft, strright))
adamc@30 257 | STRUCTURE CSYMBOL COLON sgn EQ str (DStr (CSYMBOL, SOME sgn, str), s (STRUCTUREleft, strright))
adamc@42 258 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN EQ str
adamc@42 259 (DStr (CSYMBOL1, NONE,
adamc@42 260 (StrFun (CSYMBOL2, sgn1, NONE, str), s (FUNCTORleft, strright))),
adamc@42 261 s (FUNCTORleft, strright))
adamc@42 262 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN COLON sgn EQ str
adamc@42 263 (DStr (CSYMBOL1, NONE,
adamc@42 264 (StrFun (CSYMBOL2, sgn1, SOME sgn2, str), s (FUNCTORleft, strright))),
adamc@42 265 s (FUNCTORleft, strright))
adamc@48 266 | EXTERN STRUCTURE CSYMBOL COLON sgn (DFfiStr (CSYMBOL, sgn), s (EXTERNleft, sgnright))
adamc@61 267 | OPEN mpath (case mpath of
adamc@61 268 [] => raise Fail "Impossible mpath parse [1]"
adamc@61 269 | m :: ms => (DOpen (m, ms), s (OPENleft, mpathright)))
adamc@88 270 | OPEN CONSTRAINTS mpath (case mpath of
adamc@88 271 [] => raise Fail "Impossible mpath parse [3]"
adamc@88 272 | m :: ms => (DOpenConstraints (m, ms), s (OPENleft, mpathright)))
adamc@88 273 | CONSTRAINT cterm TWIDDLE cterm (DConstraint (cterm1, cterm2), s (CONSTRAINTleft, ctermright))
adamc@109 274 | EXPORT spath (DExport spath, s (EXPORTleft, spathright))
adamc@203 275 | TABLE SYMBOL COLON cexp (DTable (SYMBOL, entable cexp), s (TABLEleft, cexpright))
adamc@211 276 | CLASS SYMBOL EQ cexp (DClass (SYMBOL, cexp), s (CLASSleft, cexpright))
adamc@211 277 | CLASS SYMBOL SYMBOL EQ cexp (let
adamc@211 278 val loc = s (CLASSleft, cexpright)
adamc@211 279 val k = (KType, loc)
adamc@211 280 val c = (CAbs (SYMBOL2, SOME k, cexp), loc)
adamc@211 281 in
adamc@211 282 (DClass (SYMBOL1, c), s (CLASSleft, cexpright))
adamc@211 283 end)
adamc@30 284
adamc@191 285 dargs : ([])
adamc@191 286 | SYMBOL dargs (SYMBOL :: dargs)
adamc@191 287
adamc@156 288 barOpt : ()
adamc@156 289 | BAR ()
adamc@156 290
adamc@156 291 dcons : dcon ([dcon])
adamc@156 292 | dcon BAR dcons (dcon :: dcons)
adamc@156 293
adamc@156 294 dcon : CSYMBOL (CSYMBOL, NONE)
adamc@156 295 | CSYMBOL OF cexp (CSYMBOL, SOME cexp)
adamc@156 296
adamc@123 297 vali : SYMBOL EQ eexp (SYMBOL, NONE, eexp)
adamc@123 298 | SYMBOL COLON cexp EQ eexp (SYMBOL, SOME cexp, eexp)
adamc@123 299
adamc@123 300 valis : vali ([vali])
adamc@123 301 | vali AND valis (vali :: valis)
adamc@123 302
adamc@42 303 sgn : sgntm (sgntm)
adamc@40 304 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN COLON sgn
adamc@40 305 (SgnFun (CSYMBOL, sgn1, sgn2), s (FUNCTORleft, sgn2right))
adamc@30 306
adamc@42 307 sgntm : SIG sgis END (SgnConst sgis, s (SIGleft, ENDright))
adamc@59 308 | mpath (case mpath of
adamc@61 309 [] => raise Fail "Impossible mpath parse [2]"
adamc@59 310 | [x] => SgnVar x
adamc@59 311 | m :: ms => SgnProj (m,
adamc@59 312 List.take (ms, length ms - 1),
adamc@59 313 List.nth (ms, length ms - 1)),
adamc@59 314 s (mpathleft, mpathright))
adamc@42 315 | sgntm WHERE CON SYMBOL EQ cexp (SgnWhere (sgntm, SYMBOL, cexp), s (sgntmleft, cexpright))
adamc@42 316 | sgntm WHERE LTYPE SYMBOL EQ cexp(SgnWhere (sgntm, SYMBOL, cexp), s (sgntmleft, cexpright))
adamc@42 317 | LPAREN sgn RPAREN (sgn)
adamc@42 318
adamc@30 319 sgi : CON SYMBOL DCOLON kind (SgiConAbs (SYMBOL, kind), s (CONleft, kindright))
adamc@30 320 | LTYPE SYMBOL (SgiConAbs (SYMBOL, (KType, s (LTYPEleft, SYMBOLright))),
adamc@30 321 s (LTYPEleft, SYMBOLright))
adamc@30 322 | CON SYMBOL EQ cexp (SgiCon (SYMBOL, NONE, cexp), s (CONleft, cexpright))
adamc@30 323 | CON SYMBOL DCOLON kind EQ cexp (SgiCon (SYMBOL, SOME kind, cexp), s (CONleft, cexpright))
adamc@30 324 | LTYPE SYMBOL EQ cexp (SgiCon (SYMBOL, SOME (KType, s (LTYPEleft, cexpright)), cexp),
adamc@30 325 s (LTYPEleft, cexpright))
adamc@191 326 | DATATYPE SYMBOL dargs EQ barOpt dcons(SgiDatatype (SYMBOL, dargs, dcons), s (DATATYPEleft, dconsright))
adamc@191 327 | DATATYPE SYMBOL dargs EQ DATATYPE CSYMBOL DOT path
adamc@191 328 (case dargs of
adamc@191 329 [] => (SgiDatatypeImp (SYMBOL, CSYMBOL :: #1 path, #2 path), s (DATATYPEleft, pathright))
adamc@191 330 | _ => raise Fail "Arguments specified for imported datatype")
adamc@30 331 | VAL SYMBOL COLON cexp (SgiVal (SYMBOL, cexp), s (VALleft, cexpright))
adamc@30 332
adamc@30 333 | STRUCTURE CSYMBOL COLON sgn (SgiStr (CSYMBOL, sgn), s (STRUCTUREleft, sgnright))
adamc@59 334 | SIGNATURE CSYMBOL EQ sgn (SgiSgn (CSYMBOL, sgn), s (SIGNATUREleft, sgnright))
adamc@42 335 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN COLON sgn
adamc@42 336 (SgiStr (CSYMBOL1,
adamc@42 337 (SgnFun (CSYMBOL2, sgn1, sgn2), s (FUNCTORleft, sgn2right))),
adamc@42 338 s (FUNCTORleft, sgn2right))
adamc@58 339 | INCLUDE sgn (SgiInclude sgn, s (INCLUDEleft, sgnright))
adamc@88 340 | CONSTRAINT cterm TWIDDLE cterm (SgiConstraint (cterm1, cterm2), s (CONSTRAINTleft, ctermright))
adamc@203 341 | TABLE SYMBOL COLON cexp (SgiTable (SYMBOL, entable cexp), s (TABLEleft, cexpright))
adamc@211 342 | CLASS SYMBOL (SgiClassAbs SYMBOL, s (CLASSleft, SYMBOLright))
adamc@211 343 | CLASS SYMBOL EQ cexp (SgiClass (SYMBOL, cexp), s (CLASSleft, cexpright))
adamc@211 344 | CLASS SYMBOL SYMBOL EQ cexp (let
adamc@211 345 val loc = s (CLASSleft, cexpright)
adamc@211 346 val k = (KType, loc)
adamc@211 347 val c = (CAbs (SYMBOL2, SOME k, cexp), loc)
adamc@211 348 in
adamc@211 349 (SgiClass (SYMBOL1, c), s (CLASSleft, cexpright))
adamc@211 350 end)
adamc@30 351
adamc@30 352 sgis : ([])
adamc@30 353 | sgi sgis (sgi :: sgis)
adamc@30 354
adamc@30 355 str : STRUCT decls END (StrConst decls, s (STRUCTleft, ENDright))
adamc@34 356 | spath (spath)
adamc@40 357 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN DARROW str
adamc@40 358 (StrFun (CSYMBOL, sgn, NONE, str), s (FUNCTORleft, strright))
adamc@40 359 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN COLON sgn DARROW str
adamc@40 360 (StrFun (CSYMBOL, sgn1, SOME sgn2, str), s (FUNCTORleft, strright))
adamc@44 361 | spath LPAREN str RPAREN (StrApp (spath, str), s (spathleft, RPARENright))
adamc@34 362
adamc@34 363 spath : CSYMBOL (StrVar CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@34 364 | spath DOT CSYMBOL (StrProj (spath, CSYMBOL), s (spathleft, CSYMBOLright))
adamc@30 365
adamc@1 366 kind : TYPE (KType, s (TYPEleft, TYPEright))
adamc@1 367 | NAME (KName, s (NAMEleft, NAMEright))
adamc@1 368 | LBRACE kind RBRACE (KRecord kind, s (LBRACEleft, RBRACEright))
adamc@1 369 | kind ARROW kind (KArrow (kind1, kind2), s (kind1left, kind2right))
adamc@1 370 | LPAREN kind RPAREN (#1 kind, s (LPARENleft, RPARENright))
adamc@82 371 | KUNIT (KUnit, s (KUNITleft, KUNITright))
adamc@18 372 | UNDERUNDER (KWild, s (UNDERUNDERleft, UNDERUNDERright))
adamc@207 373 | LPAREN ktuple RPAREN (KTuple ktuple, s (LPARENleft, RPARENright))
adamc@207 374
adamc@207 375 ktuple : kind STAR kind ([kind1, kind2])
adamc@207 376 | kind STAR ktuple (kind :: ktuple)
adamc@1 377
adamc@1 378 capps : cterm (cterm)
adamc@1 379 | capps cterm (CApp (capps, cterm), s (cappsleft, ctermright))
adamc@1 380
adamc@1 381 cexp : capps (capps)
adamc@1 382 | cexp ARROW cexp (TFun (cexp1, cexp2), s (cexp1left, cexp2right))
adamc@15 383 | SYMBOL kcolon kind ARROW cexp (TCFun (kcolon, SYMBOL, kind, cexp), s (SYMBOLleft, cexpright))
adamc@1 384
adamc@1 385 | cexp PLUSPLUS cexp (CConcat (cexp1, cexp2), s (cexp1left, cexp1right))
adamc@1 386
adamc@67 387 | FN SYMBOL DARROW cexp (CAbs (SYMBOL, NONE, cexp), s (FNleft, cexpright))
adamc@67 388 | FN SYMBOL DCOLON kind DARROW cexp (CAbs (SYMBOL, SOME kind, cexp), s (FNleft, cexpright))
adamc@84 389 | cterm TWIDDLE cterm DARROW cexp(CDisjoint (cterm1, cterm2, cexp), s (cterm1left, cexpright))
adamc@85 390 | cterm TWIDDLE cterm ARROW cexp (TDisjoint (cterm1, cterm2, cexp), s (cterm1left, cexpright))
adamc@1 391
adamc@8 392 | LPAREN cexp RPAREN DCOLON kind (CAnnot (cexp, kind), s (LPARENleft, kindright))
adamc@6 393
adamc@18 394 | UNDER DCOLON kind (CWild kind, s (UNDERleft, UNDERright))
adamc@195 395 | ctuple (let
adamc@195 396 val loc = s (ctupleleft, ctupleright)
adamc@195 397 in
adamc@195 398 (TRecord (CRecord (ListUtil.mapi (fn (i, c) =>
adamc@195 399 ((CName (Int.toString (i + 1)), loc),
adamc@195 400 c)) ctuple),
adamc@195 401 loc), loc)
adamc@195 402 end)
adamc@18 403
adamc@1 404 kcolon : DCOLON (Explicit)
adamc@1 405 | TCOLON (Implicit)
adamc@1 406
adamc@34 407 path : SYMBOL ([], SYMBOL)
adamc@34 408 | CSYMBOL DOT path (let val (ms, x) = path in (CSYMBOL :: ms, x) end)
adamc@34 409
adamc@156 410 cpath : CSYMBOL ([], CSYMBOL)
adamc@156 411 | CSYMBOL DOT cpath (let val (ms, x) = cpath in (CSYMBOL :: ms, x) end)
adamc@156 412
adamc@59 413 mpath : CSYMBOL ([CSYMBOL])
adamc@59 414 | CSYMBOL DOT mpath (CSYMBOL :: mpath)
adamc@59 415
adamc@1 416 cterm : LPAREN cexp RPAREN (#1 cexp, s (LPARENleft, RPARENright))
adamc@1 417 | LBRACK rcon RBRACK (CRecord rcon, s (LBRACKleft, RBRACKright))
adamc@83 418 | LBRACK rconn RBRACK (CRecord rconn, s (LBRACKleft, RBRACKright))
adamc@1 419 | LBRACE rcone RBRACE (TRecord (CRecord rcone, s (LBRACEleft, RBRACEright)),
adamc@1 420 s (LBRACEleft, RBRACEright))
adamc@1 421 | DOLLAR cterm (TRecord cterm, s (DOLLARleft, ctermright))
adamc@1 422 | HASH CSYMBOL (CName CSYMBOL, s (HASHleft, CSYMBOLright))
adamc@195 423 | HASH INT (CName (Int64.toString INT), s (HASHleft, INTright))
adamc@1 424
adamc@34 425 | path (CVar path, s (pathleft, pathright))
adamc@207 426 | path DOT INT (CProj ((CVar path, s (pathleft, pathright)), Int64.toInt INT),
adamc@207 427 s (pathleft, INTright))
adamc@18 428 | UNDER (CWild (KWild, s (UNDERleft, UNDERright)), s (UNDERleft, UNDERright))
adamc@67 429 | FOLD (CFold, s (FOLDleft, FOLDright))
adamc@82 430 | UNIT (CUnit, s (UNITleft, UNITright))
adamc@207 431 | LPAREN ctuplev RPAREN (CTuple ctuplev, s (LPARENleft, RPARENright))
adamc@207 432
adamc@207 433 ctuplev: cexp COMMA cexp ([cexp1, cexp2])
adamc@207 434 | cexp COMMA ctuplev (cexp :: ctuplev)
adamc@1 435
adamc@196 436 ctuple : capps STAR capps ([capps1, capps2])
adamc@196 437 | capps STAR ctuple (capps :: ctuple)
adamc@195 438
adamc@1 439 rcon : ([])
adamc@1 440 | ident EQ cexp ([(ident, cexp)])
adamc@1 441 | ident EQ cexp COMMA rcon ((ident, cexp) :: rcon)
adamc@1 442
adamc@83 443 rconn : ident ([(ident, (CUnit, s (identleft, identright)))])
adamc@83 444 | ident COMMA rconn ((ident, (CUnit, s (identleft, identright))) :: rconn)
adamc@83 445
adamc@1 446 rcone : ([])
adamc@1 447 | ident COLON cexp ([(ident, cexp)])
adamc@1 448 | ident COLON cexp COMMA rcone ((ident, cexp) :: rcone)
adamc@1 449
adamc@1 450 ident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@195 451 | INT (CName (Int64.toString INT), s (INTleft, INTright))
adamc@200 452 | SYMBOL (CVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright))
adamc@8 453
adamc@8 454 eapps : eterm (eterm)
adamc@8 455 | eapps eterm (EApp (eapps, eterm), s (eappsleft, etermright))
adamc@8 456 | eapps LBRACK cexp RBRACK (ECApp (eapps, cexp), s (eappsleft, RBRACKright))
adamc@8 457
adamc@8 458 eexp : eapps (eapps)
adamc@8 459 | FN SYMBOL kcolon kind DARROW eexp (ECAbs (kcolon, SYMBOL, kind, eexp), s (FNleft, eexpright))
adamc@8 460 | FN SYMBOL COLON cexp DARROW eexp (EAbs (SYMBOL, SOME cexp, eexp), s (FNleft, eexpright))
adamc@8 461 | FN SYMBOL DARROW eexp (EAbs (SYMBOL, NONE, eexp), s (FNleft, eexpright))
adamc@212 462 | FN UNDER COLON cexp DARROW eexp (EAbs ("_", SOME cexp, eexp), s (FNleft, eexpright))
adamc@93 463 | LBRACK cterm TWIDDLE cterm RBRACK DARROW eexp(EDisjoint (cterm1, cterm2, eexp), s (LBRACKleft, RBRACKright))
adamc@93 464 | FN UNIT DARROW eexp (let
adamc@93 465 val loc = s (FNleft, eexpright)
adamc@93 466 in
adamc@93 467 (EAbs ("_", SOME (TRecord (CRecord [], loc), loc), eexp), loc)
adamc@93 468 end)
adamc@8 469
adamc@196 470 | eexp COLON cexp (EAnnot (eexp, cexp), s (eexpleft, cexpright))
adamc@149 471 | eexp MINUSMINUS cexp (ECut (eexp, cexp), s (eexpleft, cexpright))
adamc@170 472 | CASE eexp OF barOpt branch branchs (ECase (eexp, branch :: branchs), s (CASEleft, branchsright))
adamc@190 473 | IF eexp THEN eexp ELSE eexp (let
adamc@190 474 val loc = s (IFleft, eexp3right)
adamc@190 475 in
adamc@190 476 (ECase (eexp1, [((PCon (["Basis"], "True", NONE), loc), eexp2),
adamc@190 477 ((PCon (["Basis"], "False", NONE), loc), eexp3)]), loc)
adamc@190 478 end)
adamc@8 479
adamc@8 480 eterm : LPAREN eexp RPAREN (#1 eexp, s (LPARENleft, RPARENright))
adamc@195 481 | LPAREN etuple RPAREN (let
adamc@195 482 val loc = s (LPARENleft, RPARENright)
adamc@195 483 in
adamc@195 484 (ERecord (ListUtil.mapi (fn (i, e) =>
adamc@195 485 ((CName (Int.toString (i + 1)), loc),
adamc@195 486 e)) etuple), loc)
adamc@195 487 end)
adamc@8 488
adamc@34 489 | path (EVar path, s (pathleft, pathright))
adamc@156 490 | cpath (EVar cpath, s (cpathleft, cpathright))
adamc@12 491 | LBRACE rexp RBRACE (ERecord rexp, s (LBRACEleft, RBRACEright))
adamc@110 492 | UNIT (ERecord [], s (UNITleft, UNITright))
adamc@12 493
adamc@14 494 | INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@14 495 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@14 496 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@14 497
adamc@200 498 | path DOT idents (let
adamc@200 499 val loc = s (pathleft, identsright)
adamc@200 500 in
adamc@200 501 foldl (fn (ident, e) =>
adamc@200 502 (EField (e, ident), loc))
adamc@200 503 (EVar path, s (pathleft, pathright)) idents
adamc@200 504 end)
adamc@71 505 | FOLD (EFold, s (FOLDleft, FOLDright))
adamc@71 506
adamc@91 507 | XML_BEGIN xml XML_END (xml)
adamc@91 508 | XML_BEGIN XML_END (EApp ((EVar (["Basis"], "cdata"), s (XML_BEGINleft, XML_ENDright)),
adamc@91 509 (EPrim (Prim.String ""), s (XML_BEGINleft, XML_ENDright))),
adamc@91 510 s (XML_BEGINleft, XML_ENDright))
adamc@204 511 | LPAREN query RPAREN (query)
adamc@211 512 | UNDER (EWild, s (UNDERleft, UNDERright))
adamc@91 513
adamc@200 514 idents : ident ([ident])
adamc@200 515 | ident DOT idents (ident :: idents)
adamc@200 516
adamc@195 517 etuple : eexp COMMA eexp ([eexp1, eexp2])
adamc@195 518 | eexp COMMA etuple (eexp :: etuple)
adamc@195 519
adamc@170 520 branch : pat DARROW eexp (pat, eexp)
adamc@170 521
adamc@170 522 branchs: ([])
adamc@170 523 | BAR branch branchs (branch :: branchs)
adamc@170 524
adamc@170 525 pat : pterm (pterm)
adamc@170 526 | cpath pterm (PCon (#1 cpath, #2 cpath, SOME pterm), s (cpathleft, ptermright))
adamc@170 527
adamc@170 528 pterm : SYMBOL (PVar SYMBOL, s (SYMBOLleft, SYMBOLright))
adamc@170 529 | cpath (PCon (#1 cpath, #2 cpath, NONE), s (cpathleft, cpathright))
adamc@170 530 | UNDER (PWild, s (UNDERleft, UNDERright))
adamc@173 531 | INT (PPrim (Prim.Int INT), s (INTleft, INTright))
adamc@173 532 | STRING (PPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@170 533 | LPAREN pat RPAREN (pat)
adamc@174 534 | LBRACE RBRACE (PRecord ([], false), s (LBRACEleft, RBRACEright))
adamc@174 535 | UNIT (PRecord ([], false), s (UNITleft, UNITright))
adamc@174 536 | LBRACE rpat RBRACE (PRecord rpat, s (LBRACEleft, RBRACEright))
adamc@195 537 | LPAREN ptuple RPAREN (PRecord (ListUtil.mapi (fn (i, p) => (Int.toString (i + 1), p)) ptuple,
adamc@195 538 false),
adamc@195 539 s (LPARENleft, RPARENright))
adamc@174 540
adamc@175 541 rpat : CSYMBOL EQ pat ([(CSYMBOL, pat)], false)
adamc@174 542 | DOTDOTDOT ([], true)
adamc@175 543 | CSYMBOL EQ pat COMMA rpat ((CSYMBOL, pat) :: #1 rpat, #2 rpat)
adamc@170 544
adamc@195 545 ptuple : pat COMMA pat ([pat1, pat2])
adamc@195 546 | pat COMMA ptuple (pat :: ptuple)
adamc@195 547
adamc@12 548 rexp : ([])
adamc@12 549 | ident EQ eexp ([(ident, eexp)])
adamc@12 550 | ident EQ eexp COMMA rexp ((ident, eexp) :: rexp)
adamc@91 551
adamc@141 552 xml : xmlOne xml (let
adamc@141 553 val pos = s (xmlOneleft, xmlright)
adamc@141 554 in
adamc@141 555 (EApp ((EApp (
adamc@141 556 (EVar (["Basis"], "join"), pos),
adamc@91 557 xmlOne), pos),
adamc@141 558 xml), pos)
adamc@141 559 end)
adamc@141 560 | xmlOne (xmlOne)
adamc@91 561
adamc@141 562 xmlOne : NOTAGS (EApp ((EVar (["Basis"], "cdata"), s (NOTAGSleft, NOTAGSright)),
adamc@141 563 (EPrim (Prim.String NOTAGS), s (NOTAGSleft, NOTAGSright))),
adamc@141 564 s (NOTAGSleft, NOTAGSright))
adamc@141 565 | tag DIVIDE GT (let
adamc@141 566 val pos = s (tagleft, GTright)
adamc@141 567 in
adamc@141 568 (EApp (#2 tag,
adamc@141 569 (EApp ((EVar (["Basis"], "cdata"), pos),
adamc@141 570 (EPrim (Prim.String ""), pos)),
adamc@141 571 pos)), pos)
adamc@141 572 end)
adamc@141 573
adamc@141 574 | tag GT xml END_TAG (let
adamc@141 575 val pos = s (tagleft, GTright)
adamc@141 576 in
adamc@141 577 if #1 tag = END_TAG then
adamc@141 578 if END_TAG = "lform" then
adamc@141 579 (EApp ((EVar (["Basis"], "lform"), pos),
adamc@141 580 xml), pos)
adamc@141 581 else
adamc@141 582 (EApp (#2 tag, xml), pos)
adamc@141 583 else
adamc@141 584 (ErrorMsg.errorAt pos "Begin and end tags don't match.";
adamc@141 585 (EFold, pos))
adamc@141 586 end)
adamc@141 587 | LBRACE eexp RBRACE (eexp)
adamc@92 588
adamc@141 589 tag : tagHead attrs (let
adamc@141 590 val pos = s (tagHeadleft, attrsright)
adamc@141 591 in
adamc@141 592 (#1 tagHead,
adamc@141 593 (EApp ((EApp ((EVar (["Basis"], "tag"), pos),
adamc@141 594 (ERecord attrs, pos)), pos),
adamc@141 595 (EApp (#2 tagHead,
adamc@141 596 (ERecord [], pos)), pos)),
adamc@141 597 pos))
adamc@141 598 end)
adamc@141 599
adamc@141 600 tagHead: BEGIN_TAG (let
adamc@141 601 val pos = s (BEGIN_TAGleft, BEGIN_TAGright)
adamc@141 602 in
adamc@141 603 (BEGIN_TAG,
adamc@141 604 (EVar ([], BEGIN_TAG), pos))
adamc@141 605 end)
adamc@141 606 | tagHead LBRACE cexp RBRACE (#1 tagHead, (ECApp (#2 tagHead, cexp), s (tagHeadleft, RBRACEright)))
adamc@92 607
adamc@104 608 attrs : ([])
adamc@104 609 | attr attrs (attr :: attrs)
adamc@104 610
adamc@204 611 attr : SYMBOL EQ attrv ((CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright)), attrv)
adamc@204 612
adamc@104 613 attrv : INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@104 614 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@104 615 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@110 616 | LBRACE eexp RBRACE (eexp)
adamc@204 617
adamc@209 618 query : SELECT select FROM tables wopt
adamc@209 619 (let
adamc@204 620 val loc = s (SELECTleft, tablesright)
adamc@207 621
adamc@207 622 val sel =
adamc@207 623 case select of
adamc@207 624 Star => map (fn (nm, _) =>
adamc@207 625 (nm, (CTuple [(CWild (KRecord (KType, loc), loc),
adamc@207 626 loc),
adamc@207 627 (CRecord [], loc)],
adamc@207 628 loc))) tables
adamc@207 629 | Items sis =>
adamc@207 630 let
adamc@207 631 val tabs = map (fn (nm, _) => (nm, (CRecord [], loc))) tables
adamc@207 632 val tabs = foldl (amend_select loc) tabs sis
adamc@207 633 in
adamc@207 634 map (fn (nm, c) => (nm,
adamc@207 635 (CTuple [c,
adamc@207 636 (CWild (KRecord (KType, loc), loc),
adamc@207 637 loc)], loc))) tabs
adamc@207 638 end
adamc@207 639
adamc@207 640 val sel = (CRecord sel, loc)
adamc@207 641
adamc@207 642 val e = (EVar (["Basis"], "sql_query"), loc)
adamc@207 643 val e = (ECApp (e, sel), loc)
adamc@209 644 val re = (ERecord [((CName "From", loc),
adamc@209 645 (ERecord tables, loc)),
adamc@209 646 ((CName "Where", loc),
adamc@209 647 wopt)], loc)
adamc@209 648 val e = (EApp (e, re), loc)
adamc@204 649 in
adamc@207 650 e
adamc@204 651 end)
adamc@204 652
adamc@204 653 tables : table ([table])
adamc@204 654 | table COMMA tables (table :: tables)
adamc@204 655
adamc@204 656 tname : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@204 657 | LBRACE cexp RBRACE (cexp)
adamc@204 658
adamc@207 659 table : SYMBOL ((CName SYMBOL, s (SYMBOLleft, SYMBOLright)),
adamc@204 660 (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright)))
adamc@204 661 | SYMBOL AS tname (tname, (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright)))
adamc@204 662 | LBRACE eexp RBRACE AS tname (tname, eexp)
adamc@207 663
adamc@207 664 tident : SYMBOL (CName SYMBOL, s (SYMBOLleft, SYMBOLright))
adamc@207 665 | CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@207 666 | LBRACE cexp RBRACE (cexp)
adamc@207 667
adamc@207 668 fident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@207 669 | LBRACE cexp RBRACE (cexp)
adamc@207 670
adamc@207 671 seli : tident DOT fident (Field (tident, fident))
adamc@207 672
adamc@207 673 selis : seli ([seli])
adamc@207 674 | seli COMMA selis (seli :: selis)
adamc@207 675
adamc@207 676 select : STAR (Star)
adamc@207 677 | selis (Items selis)
adamc@209 678
adamc@209 679 sqlexp : TRUE (sql_inject (EVar (["Basis"], "True"),
adamc@209 680 EVar (["Basis"], "sql_bool"),
adamc@209 681 s (TRUEleft, TRUEright)))
adamc@209 682 | FALSE (sql_inject (EVar (["Basis"], "False"),
adamc@209 683 EVar (["Basis"], "sql_bool"),
adamc@209 684 s (FALSEleft, FALSEright)))
adamc@209 685
adamc@219 686 | sqlexp EQ sqlexp (sql_compare ("eq", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 687 | sqlexp NE sqlexp (sql_compare ("ne", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 688 | sqlexp LT sqlexp (sql_compare ("lt", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 689 | sqlexp LE sqlexp (sql_compare ("le", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 690 | sqlexp GT sqlexp (sql_compare ("gt", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 691 | sqlexp GE sqlexp (sql_compare ("ge", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@219 692
adamc@220 693 | sqlexp CAND sqlexp (sql_binary ("and", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@220 694 | sqlexp OR sqlexp (sql_binary ("or", sqlexp1, sqlexp2, s (sqlexp1left, sqlexp2right)))
adamc@220 695 | NOT sqlexp (sql_unary ("not", sqlexp, s (NOTleft, sqlexpright)))
adamc@220 696
adamc@210 697 | LBRACE eexp RBRACE (sql_inject (#1 eexp,
adamc@211 698 EWild,
adamc@210 699 s (LBRACEleft, RBRACEright)))
adamc@220 700 | LPAREN sqlexp RPAREN (sqlexp)
adamc@210 701
adamc@209 702 wopt : (sql_inject (EVar (["Basis"], "True"),
adamc@209 703 EVar (["Basis"], "sql_bool"),
adamc@209 704 ErrorMsg.dummySpan))
adamc@209 705 | CWHERE sqlexp (sqlexp)