annotate src/lacweb.grm @ 210:f4033abd6ab1

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