annotate src/lacweb.grm @ 211:e86411f647c6

Initial type class support
author Adam Chlipala <adamc@hcoop.net>
date Sat, 16 Aug 2008 14:32:18 -0400
parents f4033abd6ab1
children ba4d7c33a45f
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@211 92 | CON | LTYPE | VAL | REC | AND | FOLD | UNIT | KUNIT | CLASS
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@211 244 | CLASS SYMBOL EQ cexp (DClass (SYMBOL, cexp), s (CLASSleft, cexpright))
adamc@211 245 | CLASS SYMBOL SYMBOL EQ cexp (let
adamc@211 246 val loc = s (CLASSleft, cexpright)
adamc@211 247 val k = (KType, loc)
adamc@211 248 val c = (CAbs (SYMBOL2, SOME k, cexp), loc)
adamc@211 249 in
adamc@211 250 (DClass (SYMBOL1, c), s (CLASSleft, cexpright))
adamc@211 251 end)
adamc@30 252
adamc@191 253 dargs : ([])
adamc@191 254 | SYMBOL dargs (SYMBOL :: dargs)
adamc@191 255
adamc@156 256 barOpt : ()
adamc@156 257 | BAR ()
adamc@156 258
adamc@156 259 dcons : dcon ([dcon])
adamc@156 260 | dcon BAR dcons (dcon :: dcons)
adamc@156 261
adamc@156 262 dcon : CSYMBOL (CSYMBOL, NONE)
adamc@156 263 | CSYMBOL OF cexp (CSYMBOL, SOME cexp)
adamc@156 264
adamc@123 265 vali : SYMBOL EQ eexp (SYMBOL, NONE, eexp)
adamc@123 266 | SYMBOL COLON cexp EQ eexp (SYMBOL, SOME cexp, eexp)
adamc@123 267
adamc@123 268 valis : vali ([vali])
adamc@123 269 | vali AND valis (vali :: valis)
adamc@123 270
adamc@42 271 sgn : sgntm (sgntm)
adamc@40 272 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN COLON sgn
adamc@40 273 (SgnFun (CSYMBOL, sgn1, sgn2), s (FUNCTORleft, sgn2right))
adamc@30 274
adamc@42 275 sgntm : SIG sgis END (SgnConst sgis, s (SIGleft, ENDright))
adamc@59 276 | mpath (case mpath of
adamc@61 277 [] => raise Fail "Impossible mpath parse [2]"
adamc@59 278 | [x] => SgnVar x
adamc@59 279 | m :: ms => SgnProj (m,
adamc@59 280 List.take (ms, length ms - 1),
adamc@59 281 List.nth (ms, length ms - 1)),
adamc@59 282 s (mpathleft, mpathright))
adamc@42 283 | sgntm WHERE CON SYMBOL EQ cexp (SgnWhere (sgntm, SYMBOL, cexp), s (sgntmleft, cexpright))
adamc@42 284 | sgntm WHERE LTYPE SYMBOL EQ cexp(SgnWhere (sgntm, SYMBOL, cexp), s (sgntmleft, cexpright))
adamc@42 285 | LPAREN sgn RPAREN (sgn)
adamc@42 286
adamc@30 287 sgi : CON SYMBOL DCOLON kind (SgiConAbs (SYMBOL, kind), s (CONleft, kindright))
adamc@30 288 | LTYPE SYMBOL (SgiConAbs (SYMBOL, (KType, s (LTYPEleft, SYMBOLright))),
adamc@30 289 s (LTYPEleft, SYMBOLright))
adamc@30 290 | CON SYMBOL EQ cexp (SgiCon (SYMBOL, NONE, cexp), s (CONleft, cexpright))
adamc@30 291 | CON SYMBOL DCOLON kind EQ cexp (SgiCon (SYMBOL, SOME kind, cexp), s (CONleft, cexpright))
adamc@30 292 | LTYPE SYMBOL EQ cexp (SgiCon (SYMBOL, SOME (KType, s (LTYPEleft, cexpright)), cexp),
adamc@30 293 s (LTYPEleft, cexpright))
adamc@191 294 | DATATYPE SYMBOL dargs EQ barOpt dcons(SgiDatatype (SYMBOL, dargs, dcons), s (DATATYPEleft, dconsright))
adamc@191 295 | DATATYPE SYMBOL dargs EQ DATATYPE CSYMBOL DOT path
adamc@191 296 (case dargs of
adamc@191 297 [] => (SgiDatatypeImp (SYMBOL, CSYMBOL :: #1 path, #2 path), s (DATATYPEleft, pathright))
adamc@191 298 | _ => raise Fail "Arguments specified for imported datatype")
adamc@30 299 | VAL SYMBOL COLON cexp (SgiVal (SYMBOL, cexp), s (VALleft, cexpright))
adamc@30 300
adamc@30 301 | STRUCTURE CSYMBOL COLON sgn (SgiStr (CSYMBOL, sgn), s (STRUCTUREleft, sgnright))
adamc@59 302 | SIGNATURE CSYMBOL EQ sgn (SgiSgn (CSYMBOL, sgn), s (SIGNATUREleft, sgnright))
adamc@42 303 | FUNCTOR CSYMBOL LPAREN CSYMBOL COLON sgn RPAREN COLON sgn
adamc@42 304 (SgiStr (CSYMBOL1,
adamc@42 305 (SgnFun (CSYMBOL2, sgn1, sgn2), s (FUNCTORleft, sgn2right))),
adamc@42 306 s (FUNCTORleft, sgn2right))
adamc@58 307 | INCLUDE sgn (SgiInclude sgn, s (INCLUDEleft, sgnright))
adamc@88 308 | CONSTRAINT cterm TWIDDLE cterm (SgiConstraint (cterm1, cterm2), s (CONSTRAINTleft, ctermright))
adamc@203 309 | TABLE SYMBOL COLON cexp (SgiTable (SYMBOL, entable cexp), s (TABLEleft, cexpright))
adamc@211 310 | CLASS SYMBOL (SgiClassAbs SYMBOL, s (CLASSleft, SYMBOLright))
adamc@211 311 | CLASS SYMBOL EQ cexp (SgiClass (SYMBOL, cexp), s (CLASSleft, cexpright))
adamc@211 312 | CLASS SYMBOL SYMBOL EQ cexp (let
adamc@211 313 val loc = s (CLASSleft, cexpright)
adamc@211 314 val k = (KType, loc)
adamc@211 315 val c = (CAbs (SYMBOL2, SOME k, cexp), loc)
adamc@211 316 in
adamc@211 317 (SgiClass (SYMBOL1, c), s (CLASSleft, cexpright))
adamc@211 318 end)
adamc@30 319
adamc@30 320 sgis : ([])
adamc@30 321 | sgi sgis (sgi :: sgis)
adamc@30 322
adamc@30 323 str : STRUCT decls END (StrConst decls, s (STRUCTleft, ENDright))
adamc@34 324 | spath (spath)
adamc@40 325 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN DARROW str
adamc@40 326 (StrFun (CSYMBOL, sgn, NONE, str), s (FUNCTORleft, strright))
adamc@40 327 | FUNCTOR LPAREN CSYMBOL COLON sgn RPAREN COLON sgn DARROW str
adamc@40 328 (StrFun (CSYMBOL, sgn1, SOME sgn2, str), s (FUNCTORleft, strright))
adamc@44 329 | spath LPAREN str RPAREN (StrApp (spath, str), s (spathleft, RPARENright))
adamc@34 330
adamc@34 331 spath : CSYMBOL (StrVar CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@34 332 | spath DOT CSYMBOL (StrProj (spath, CSYMBOL), s (spathleft, CSYMBOLright))
adamc@30 333
adamc@1 334 kind : TYPE (KType, s (TYPEleft, TYPEright))
adamc@1 335 | NAME (KName, s (NAMEleft, NAMEright))
adamc@1 336 | LBRACE kind RBRACE (KRecord kind, s (LBRACEleft, RBRACEright))
adamc@1 337 | kind ARROW kind (KArrow (kind1, kind2), s (kind1left, kind2right))
adamc@1 338 | LPAREN kind RPAREN (#1 kind, s (LPARENleft, RPARENright))
adamc@82 339 | KUNIT (KUnit, s (KUNITleft, KUNITright))
adamc@18 340 | UNDERUNDER (KWild, s (UNDERUNDERleft, UNDERUNDERright))
adamc@207 341 | LPAREN ktuple RPAREN (KTuple ktuple, s (LPARENleft, RPARENright))
adamc@207 342
adamc@207 343 ktuple : kind STAR kind ([kind1, kind2])
adamc@207 344 | kind STAR ktuple (kind :: ktuple)
adamc@1 345
adamc@1 346 capps : cterm (cterm)
adamc@1 347 | capps cterm (CApp (capps, cterm), s (cappsleft, ctermright))
adamc@1 348
adamc@1 349 cexp : capps (capps)
adamc@1 350 | cexp ARROW cexp (TFun (cexp1, cexp2), s (cexp1left, cexp2right))
adamc@15 351 | SYMBOL kcolon kind ARROW cexp (TCFun (kcolon, SYMBOL, kind, cexp), s (SYMBOLleft, cexpright))
adamc@1 352
adamc@1 353 | cexp PLUSPLUS cexp (CConcat (cexp1, cexp2), s (cexp1left, cexp1right))
adamc@1 354
adamc@67 355 | FN SYMBOL DARROW cexp (CAbs (SYMBOL, NONE, cexp), s (FNleft, cexpright))
adamc@67 356 | FN SYMBOL DCOLON kind DARROW cexp (CAbs (SYMBOL, SOME kind, cexp), s (FNleft, cexpright))
adamc@84 357 | cterm TWIDDLE cterm DARROW cexp(CDisjoint (cterm1, cterm2, cexp), s (cterm1left, cexpright))
adamc@85 358 | cterm TWIDDLE cterm ARROW cexp (TDisjoint (cterm1, cterm2, cexp), s (cterm1left, cexpright))
adamc@1 359
adamc@8 360 | LPAREN cexp RPAREN DCOLON kind (CAnnot (cexp, kind), s (LPARENleft, kindright))
adamc@6 361
adamc@18 362 | UNDER DCOLON kind (CWild kind, s (UNDERleft, UNDERright))
adamc@195 363 | ctuple (let
adamc@195 364 val loc = s (ctupleleft, ctupleright)
adamc@195 365 in
adamc@195 366 (TRecord (CRecord (ListUtil.mapi (fn (i, c) =>
adamc@195 367 ((CName (Int.toString (i + 1)), loc),
adamc@195 368 c)) ctuple),
adamc@195 369 loc), loc)
adamc@195 370 end)
adamc@18 371
adamc@1 372 kcolon : DCOLON (Explicit)
adamc@1 373 | TCOLON (Implicit)
adamc@1 374
adamc@34 375 path : SYMBOL ([], SYMBOL)
adamc@34 376 | CSYMBOL DOT path (let val (ms, x) = path in (CSYMBOL :: ms, x) end)
adamc@34 377
adamc@156 378 cpath : CSYMBOL ([], CSYMBOL)
adamc@156 379 | CSYMBOL DOT cpath (let val (ms, x) = cpath in (CSYMBOL :: ms, x) end)
adamc@156 380
adamc@59 381 mpath : CSYMBOL ([CSYMBOL])
adamc@59 382 | CSYMBOL DOT mpath (CSYMBOL :: mpath)
adamc@59 383
adamc@1 384 cterm : LPAREN cexp RPAREN (#1 cexp, s (LPARENleft, RPARENright))
adamc@1 385 | LBRACK rcon RBRACK (CRecord rcon, s (LBRACKleft, RBRACKright))
adamc@83 386 | LBRACK rconn RBRACK (CRecord rconn, s (LBRACKleft, RBRACKright))
adamc@1 387 | LBRACE rcone RBRACE (TRecord (CRecord rcone, s (LBRACEleft, RBRACEright)),
adamc@1 388 s (LBRACEleft, RBRACEright))
adamc@1 389 | DOLLAR cterm (TRecord cterm, s (DOLLARleft, ctermright))
adamc@1 390 | HASH CSYMBOL (CName CSYMBOL, s (HASHleft, CSYMBOLright))
adamc@195 391 | HASH INT (CName (Int64.toString INT), s (HASHleft, INTright))
adamc@1 392
adamc@34 393 | path (CVar path, s (pathleft, pathright))
adamc@207 394 | path DOT INT (CProj ((CVar path, s (pathleft, pathright)), Int64.toInt INT),
adamc@207 395 s (pathleft, INTright))
adamc@18 396 | UNDER (CWild (KWild, s (UNDERleft, UNDERright)), s (UNDERleft, UNDERright))
adamc@67 397 | FOLD (CFold, s (FOLDleft, FOLDright))
adamc@82 398 | UNIT (CUnit, s (UNITleft, UNITright))
adamc@207 399 | LPAREN ctuplev RPAREN (CTuple ctuplev, s (LPARENleft, RPARENright))
adamc@207 400
adamc@207 401 ctuplev: cexp COMMA cexp ([cexp1, cexp2])
adamc@207 402 | cexp COMMA ctuplev (cexp :: ctuplev)
adamc@1 403
adamc@196 404 ctuple : capps STAR capps ([capps1, capps2])
adamc@196 405 | capps STAR ctuple (capps :: ctuple)
adamc@195 406
adamc@1 407 rcon : ([])
adamc@1 408 | ident EQ cexp ([(ident, cexp)])
adamc@1 409 | ident EQ cexp COMMA rcon ((ident, cexp) :: rcon)
adamc@1 410
adamc@83 411 rconn : ident ([(ident, (CUnit, s (identleft, identright)))])
adamc@83 412 | ident COMMA rconn ((ident, (CUnit, s (identleft, identright))) :: rconn)
adamc@83 413
adamc@1 414 rcone : ([])
adamc@1 415 | ident COLON cexp ([(ident, cexp)])
adamc@1 416 | ident COLON cexp COMMA rcone ((ident, cexp) :: rcone)
adamc@1 417
adamc@1 418 ident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@195 419 | INT (CName (Int64.toString INT), s (INTleft, INTright))
adamc@200 420 | SYMBOL (CVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright))
adamc@8 421
adamc@8 422 eapps : eterm (eterm)
adamc@8 423 | eapps eterm (EApp (eapps, eterm), s (eappsleft, etermright))
adamc@8 424 | eapps LBRACK cexp RBRACK (ECApp (eapps, cexp), s (eappsleft, RBRACKright))
adamc@8 425
adamc@8 426 eexp : eapps (eapps)
adamc@8 427 | FN SYMBOL kcolon kind DARROW eexp (ECAbs (kcolon, SYMBOL, kind, eexp), s (FNleft, eexpright))
adamc@8 428 | FN SYMBOL COLON cexp DARROW eexp (EAbs (SYMBOL, SOME cexp, eexp), s (FNleft, eexpright))
adamc@8 429 | FN SYMBOL DARROW eexp (EAbs (SYMBOL, NONE, eexp), s (FNleft, eexpright))
adamc@93 430 | LBRACK cterm TWIDDLE cterm RBRACK DARROW eexp(EDisjoint (cterm1, cterm2, eexp), s (LBRACKleft, RBRACKright))
adamc@93 431 | FN UNIT DARROW eexp (let
adamc@93 432 val loc = s (FNleft, eexpright)
adamc@93 433 in
adamc@93 434 (EAbs ("_", SOME (TRecord (CRecord [], loc), loc), eexp), loc)
adamc@93 435 end)
adamc@8 436
adamc@196 437 | eexp COLON cexp (EAnnot (eexp, cexp), s (eexpleft, cexpright))
adamc@149 438 | eexp MINUSMINUS cexp (ECut (eexp, cexp), s (eexpleft, cexpright))
adamc@170 439 | CASE eexp OF barOpt branch branchs (ECase (eexp, branch :: branchs), s (CASEleft, branchsright))
adamc@190 440 | IF eexp THEN eexp ELSE eexp (let
adamc@190 441 val loc = s (IFleft, eexp3right)
adamc@190 442 in
adamc@190 443 (ECase (eexp1, [((PCon (["Basis"], "True", NONE), loc), eexp2),
adamc@190 444 ((PCon (["Basis"], "False", NONE), loc), eexp3)]), loc)
adamc@190 445 end)
adamc@8 446
adamc@8 447 eterm : LPAREN eexp RPAREN (#1 eexp, s (LPARENleft, RPARENright))
adamc@195 448 | LPAREN etuple RPAREN (let
adamc@195 449 val loc = s (LPARENleft, RPARENright)
adamc@195 450 in
adamc@195 451 (ERecord (ListUtil.mapi (fn (i, e) =>
adamc@195 452 ((CName (Int.toString (i + 1)), loc),
adamc@195 453 e)) etuple), loc)
adamc@195 454 end)
adamc@8 455
adamc@34 456 | path (EVar path, s (pathleft, pathright))
adamc@156 457 | cpath (EVar cpath, s (cpathleft, cpathright))
adamc@12 458 | LBRACE rexp RBRACE (ERecord rexp, s (LBRACEleft, RBRACEright))
adamc@110 459 | UNIT (ERecord [], s (UNITleft, UNITright))
adamc@12 460
adamc@14 461 | INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@14 462 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@14 463 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@14 464
adamc@200 465 | path DOT idents (let
adamc@200 466 val loc = s (pathleft, identsright)
adamc@200 467 in
adamc@200 468 foldl (fn (ident, e) =>
adamc@200 469 (EField (e, ident), loc))
adamc@200 470 (EVar path, s (pathleft, pathright)) idents
adamc@200 471 end)
adamc@71 472 | FOLD (EFold, s (FOLDleft, FOLDright))
adamc@71 473
adamc@91 474 | XML_BEGIN xml XML_END (xml)
adamc@91 475 | XML_BEGIN XML_END (EApp ((EVar (["Basis"], "cdata"), s (XML_BEGINleft, XML_ENDright)),
adamc@91 476 (EPrim (Prim.String ""), s (XML_BEGINleft, XML_ENDright))),
adamc@91 477 s (XML_BEGINleft, XML_ENDright))
adamc@204 478 | LPAREN query RPAREN (query)
adamc@211 479 | UNDER (EWild, s (UNDERleft, UNDERright))
adamc@91 480
adamc@200 481 idents : ident ([ident])
adamc@200 482 | ident DOT idents (ident :: idents)
adamc@200 483
adamc@195 484 etuple : eexp COMMA eexp ([eexp1, eexp2])
adamc@195 485 | eexp COMMA etuple (eexp :: etuple)
adamc@195 486
adamc@170 487 branch : pat DARROW eexp (pat, eexp)
adamc@170 488
adamc@170 489 branchs: ([])
adamc@170 490 | BAR branch branchs (branch :: branchs)
adamc@170 491
adamc@170 492 pat : pterm (pterm)
adamc@170 493 | cpath pterm (PCon (#1 cpath, #2 cpath, SOME pterm), s (cpathleft, ptermright))
adamc@170 494
adamc@170 495 pterm : SYMBOL (PVar SYMBOL, s (SYMBOLleft, SYMBOLright))
adamc@170 496 | cpath (PCon (#1 cpath, #2 cpath, NONE), s (cpathleft, cpathright))
adamc@170 497 | UNDER (PWild, s (UNDERleft, UNDERright))
adamc@173 498 | INT (PPrim (Prim.Int INT), s (INTleft, INTright))
adamc@173 499 | STRING (PPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@170 500 | LPAREN pat RPAREN (pat)
adamc@174 501 | LBRACE RBRACE (PRecord ([], false), s (LBRACEleft, RBRACEright))
adamc@174 502 | UNIT (PRecord ([], false), s (UNITleft, UNITright))
adamc@174 503 | LBRACE rpat RBRACE (PRecord rpat, s (LBRACEleft, RBRACEright))
adamc@195 504 | LPAREN ptuple RPAREN (PRecord (ListUtil.mapi (fn (i, p) => (Int.toString (i + 1), p)) ptuple,
adamc@195 505 false),
adamc@195 506 s (LPARENleft, RPARENright))
adamc@174 507
adamc@175 508 rpat : CSYMBOL EQ pat ([(CSYMBOL, pat)], false)
adamc@174 509 | DOTDOTDOT ([], true)
adamc@175 510 | CSYMBOL EQ pat COMMA rpat ((CSYMBOL, pat) :: #1 rpat, #2 rpat)
adamc@170 511
adamc@195 512 ptuple : pat COMMA pat ([pat1, pat2])
adamc@195 513 | pat COMMA ptuple (pat :: ptuple)
adamc@195 514
adamc@12 515 rexp : ([])
adamc@12 516 | ident EQ eexp ([(ident, eexp)])
adamc@12 517 | ident EQ eexp COMMA rexp ((ident, eexp) :: rexp)
adamc@91 518
adamc@141 519 xml : xmlOne xml (let
adamc@141 520 val pos = s (xmlOneleft, xmlright)
adamc@141 521 in
adamc@141 522 (EApp ((EApp (
adamc@141 523 (EVar (["Basis"], "join"), pos),
adamc@91 524 xmlOne), pos),
adamc@141 525 xml), pos)
adamc@141 526 end)
adamc@141 527 | xmlOne (xmlOne)
adamc@91 528
adamc@141 529 xmlOne : NOTAGS (EApp ((EVar (["Basis"], "cdata"), s (NOTAGSleft, NOTAGSright)),
adamc@141 530 (EPrim (Prim.String NOTAGS), s (NOTAGSleft, NOTAGSright))),
adamc@141 531 s (NOTAGSleft, NOTAGSright))
adamc@141 532 | tag DIVIDE GT (let
adamc@141 533 val pos = s (tagleft, GTright)
adamc@141 534 in
adamc@141 535 (EApp (#2 tag,
adamc@141 536 (EApp ((EVar (["Basis"], "cdata"), pos),
adamc@141 537 (EPrim (Prim.String ""), pos)),
adamc@141 538 pos)), pos)
adamc@141 539 end)
adamc@141 540
adamc@141 541 | tag GT xml END_TAG (let
adamc@141 542 val pos = s (tagleft, GTright)
adamc@141 543 in
adamc@141 544 if #1 tag = END_TAG then
adamc@141 545 if END_TAG = "lform" then
adamc@141 546 (EApp ((EVar (["Basis"], "lform"), pos),
adamc@141 547 xml), pos)
adamc@141 548 else
adamc@141 549 (EApp (#2 tag, xml), pos)
adamc@141 550 else
adamc@141 551 (ErrorMsg.errorAt pos "Begin and end tags don't match.";
adamc@141 552 (EFold, pos))
adamc@141 553 end)
adamc@141 554 | LBRACE eexp RBRACE (eexp)
adamc@92 555
adamc@141 556 tag : tagHead attrs (let
adamc@141 557 val pos = s (tagHeadleft, attrsright)
adamc@141 558 in
adamc@141 559 (#1 tagHead,
adamc@141 560 (EApp ((EApp ((EVar (["Basis"], "tag"), pos),
adamc@141 561 (ERecord attrs, pos)), pos),
adamc@141 562 (EApp (#2 tagHead,
adamc@141 563 (ERecord [], pos)), pos)),
adamc@141 564 pos))
adamc@141 565 end)
adamc@141 566
adamc@141 567 tagHead: BEGIN_TAG (let
adamc@141 568 val pos = s (BEGIN_TAGleft, BEGIN_TAGright)
adamc@141 569 in
adamc@141 570 (BEGIN_TAG,
adamc@141 571 (EVar ([], BEGIN_TAG), pos))
adamc@141 572 end)
adamc@141 573 | tagHead LBRACE cexp RBRACE (#1 tagHead, (ECApp (#2 tagHead, cexp), s (tagHeadleft, RBRACEright)))
adamc@92 574
adamc@104 575 attrs : ([])
adamc@104 576 | attr attrs (attr :: attrs)
adamc@104 577
adamc@204 578 attr : SYMBOL EQ attrv ((CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright)), attrv)
adamc@204 579
adamc@104 580 attrv : INT (EPrim (Prim.Int INT), s (INTleft, INTright))
adamc@104 581 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
adamc@104 582 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
adamc@110 583 | LBRACE eexp RBRACE (eexp)
adamc@204 584
adamc@209 585 query : SELECT select FROM tables wopt
adamc@209 586 (let
adamc@204 587 val loc = s (SELECTleft, tablesright)
adamc@207 588
adamc@207 589 val sel =
adamc@207 590 case select of
adamc@207 591 Star => map (fn (nm, _) =>
adamc@207 592 (nm, (CTuple [(CWild (KRecord (KType, loc), loc),
adamc@207 593 loc),
adamc@207 594 (CRecord [], loc)],
adamc@207 595 loc))) tables
adamc@207 596 | Items sis =>
adamc@207 597 let
adamc@207 598 val tabs = map (fn (nm, _) => (nm, (CRecord [], loc))) tables
adamc@207 599 val tabs = foldl (amend_select loc) tabs sis
adamc@207 600 in
adamc@207 601 map (fn (nm, c) => (nm,
adamc@207 602 (CTuple [c,
adamc@207 603 (CWild (KRecord (KType, loc), loc),
adamc@207 604 loc)], loc))) tabs
adamc@207 605 end
adamc@207 606
adamc@207 607 val sel = (CRecord sel, loc)
adamc@207 608
adamc@207 609 val e = (EVar (["Basis"], "sql_query"), loc)
adamc@207 610 val e = (ECApp (e, sel), loc)
adamc@209 611 val re = (ERecord [((CName "From", loc),
adamc@209 612 (ERecord tables, loc)),
adamc@209 613 ((CName "Where", loc),
adamc@209 614 wopt)], loc)
adamc@209 615 val e = (EApp (e, re), loc)
adamc@204 616 in
adamc@207 617 e
adamc@204 618 end)
adamc@204 619
adamc@204 620 tables : table ([table])
adamc@204 621 | table COMMA tables (table :: tables)
adamc@204 622
adamc@204 623 tname : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@204 624 | LBRACE cexp RBRACE (cexp)
adamc@204 625
adamc@207 626 table : SYMBOL ((CName SYMBOL, s (SYMBOLleft, SYMBOLright)),
adamc@204 627 (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright)))
adamc@204 628 | SYMBOL AS tname (tname, (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright)))
adamc@204 629 | LBRACE eexp RBRACE AS tname (tname, eexp)
adamc@207 630
adamc@207 631 tident : SYMBOL (CName SYMBOL, s (SYMBOLleft, SYMBOLright))
adamc@207 632 | CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@207 633 | LBRACE cexp RBRACE (cexp)
adamc@207 634
adamc@207 635 fident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
adamc@207 636 | LBRACE cexp RBRACE (cexp)
adamc@207 637
adamc@207 638 seli : tident DOT fident (Field (tident, fident))
adamc@207 639
adamc@207 640 selis : seli ([seli])
adamc@207 641 | seli COMMA selis (seli :: selis)
adamc@207 642
adamc@207 643 select : STAR (Star)
adamc@207 644 | selis (Items selis)
adamc@209 645
adamc@209 646 sqlexp : TRUE (sql_inject (EVar (["Basis"], "True"),
adamc@209 647 EVar (["Basis"], "sql_bool"),
adamc@209 648 s (TRUEleft, TRUEright)))
adamc@209 649 | FALSE (sql_inject (EVar (["Basis"], "False"),
adamc@209 650 EVar (["Basis"], "sql_bool"),
adamc@209 651 s (FALSEleft, FALSEright)))
adamc@209 652
adamc@210 653 | LBRACE eexp RBRACE (sql_inject (#1 eexp,
adamc@211 654 EWild,
adamc@210 655 s (LBRACEleft, RBRACEright)))
adamc@210 656
adamc@209 657 wopt : (sql_inject (EVar (["Basis"], "True"),
adamc@209 658 EVar (["Basis"], "sql_bool"),
adamc@209 659 ErrorMsg.dummySpan))
adamc@209 660 | CWHERE sqlexp (sqlexp)