comparison src/lacweb.grm @ 207:cc68da3801bc

Non-star SELECT
author Adam Chlipala <adamc@hcoop.net>
date Thu, 14 Aug 2008 18:35:08 -0400
parents 241c9a0e3397
children 1487c712eb12
comparison
equal deleted inserted replaced
206:cb8493759a7b 207:cc68da3801bc
37 fun entable t = 37 fun entable t =
38 case #1 t of 38 case #1 t of
39 TRecord c => c 39 TRecord c => c
40 | _ => t 40 | _ => t
41 41
42 datatype select_item =
43 Field of con * con
44
45 datatype select =
46 Star
47 | Items of select_item list
48
49 fun eqTnames ((c1, _), (c2, _)) =
50 case (c1, c2) of
51 (CVar (ms1, x1), CVar (ms2, x2)) => ms1 = ms2 andalso x1 = x2
52 | (CName x1, CName x2) => x1 = x2
53 | _ => false
54
55 fun amend_select loc (si, tabs) =
56 let
57 val (tx, c) = case si of
58 Field (tx, fx) => (tx, (CRecord ([(fx, (CWild (KType, loc), loc))]), loc))
59
60 val (tabs, found) = ListUtil.foldlMap (fn ((tx', c'), found) =>
61 if eqTnames (tx, tx') then
62 ((tx', (CConcat (c, c'), loc)), true)
63 else
64 ((tx', c'), found))
65 false tabs
66 in
67 if found then
68 ()
69 else
70 ErrorMsg.errorAt loc "Select of field from unbound table";
71
72 tabs
73 end
74
42 %% 75 %%
43 %header (functor LacwebLrValsFn(structure Token : TOKEN)) 76 %header (functor LacwebLrValsFn(structure Token : TOKEN))
44 77
45 %term 78 %term
46 EOF 79 EOF
82 | sgis of sgn_item list 115 | sgis of sgn_item list
83 116
84 | str of str 117 | str of str
85 118
86 | kind of kind 119 | kind of kind
120 | ktuple of kind list
87 | kcolon of explicitness 121 | kcolon of explicitness
88 122
89 | path of string list * string 123 | path of string list * string
90 | cpath of string list * string 124 | cpath of string list * string
91 | spath of str 125 | spath of str
93 127
94 | cexp of con 128 | cexp of con
95 | capps of con 129 | capps of con
96 | cterm of con 130 | cterm of con
97 | ctuple of con list 131 | ctuple of con list
132 | ctuplev of con list
98 | ident of con 133 | ident of con
99 | idents of con list 134 | idents of con list
100 | rcon of (con * con) list 135 | rcon of (con * con) list
101 | rconn of (con * con) list 136 | rconn of (con * con) list
102 | rcone of (con * con) list 137 | rcone of (con * con) list
124 159
125 | query of exp 160 | query of exp
126 | tables of (con * exp) list 161 | tables of (con * exp) list
127 | tname of con 162 | tname of con
128 | table of con * exp 163 | table of con * exp
164 | tident of con
165 | fident of con
166 | seli of select_item
167 | selis of select_item list
168 | select of select
169
129 170
130 %verbose (* print summary of errors *) 171 %verbose (* print summary of errors *)
131 %pos int (* positions *) 172 %pos int (* positions *)
132 %start file 173 %start file
133 %pure 174 %pure
268 | LBRACE kind RBRACE (KRecord kind, s (LBRACEleft, RBRACEright)) 309 | LBRACE kind RBRACE (KRecord kind, s (LBRACEleft, RBRACEright))
269 | kind ARROW kind (KArrow (kind1, kind2), s (kind1left, kind2right)) 310 | kind ARROW kind (KArrow (kind1, kind2), s (kind1left, kind2right))
270 | LPAREN kind RPAREN (#1 kind, s (LPARENleft, RPARENright)) 311 | LPAREN kind RPAREN (#1 kind, s (LPARENleft, RPARENright))
271 | KUNIT (KUnit, s (KUNITleft, KUNITright)) 312 | KUNIT (KUnit, s (KUNITleft, KUNITright))
272 | UNDERUNDER (KWild, s (UNDERUNDERleft, UNDERUNDERright)) 313 | UNDERUNDER (KWild, s (UNDERUNDERleft, UNDERUNDERright))
314 | LPAREN ktuple RPAREN (KTuple ktuple, s (LPARENleft, RPARENright))
315
316 ktuple : kind STAR kind ([kind1, kind2])
317 | kind STAR ktuple (kind :: ktuple)
273 318
274 capps : cterm (cterm) 319 capps : cterm (cterm)
275 | capps cterm (CApp (capps, cterm), s (cappsleft, ctermright)) 320 | capps cterm (CApp (capps, cterm), s (cappsleft, ctermright))
276 321
277 cexp : capps (capps) 322 cexp : capps (capps)
317 | DOLLAR cterm (TRecord cterm, s (DOLLARleft, ctermright)) 362 | DOLLAR cterm (TRecord cterm, s (DOLLARleft, ctermright))
318 | HASH CSYMBOL (CName CSYMBOL, s (HASHleft, CSYMBOLright)) 363 | HASH CSYMBOL (CName CSYMBOL, s (HASHleft, CSYMBOLright))
319 | HASH INT (CName (Int64.toString INT), s (HASHleft, INTright)) 364 | HASH INT (CName (Int64.toString INT), s (HASHleft, INTright))
320 365
321 | path (CVar path, s (pathleft, pathright)) 366 | path (CVar path, s (pathleft, pathright))
367 | path DOT INT (CProj ((CVar path, s (pathleft, pathright)), Int64.toInt INT),
368 s (pathleft, INTright))
322 | UNDER (CWild (KWild, s (UNDERleft, UNDERright)), s (UNDERleft, UNDERright)) 369 | UNDER (CWild (KWild, s (UNDERleft, UNDERright)), s (UNDERleft, UNDERright))
323 | FOLD (CFold, s (FOLDleft, FOLDright)) 370 | FOLD (CFold, s (FOLDleft, FOLDright))
324 | UNIT (CUnit, s (UNITleft, UNITright)) 371 | UNIT (CUnit, s (UNITleft, UNITright))
372 | LPAREN ctuplev RPAREN (CTuple ctuplev, s (LPARENleft, RPARENright))
373
374 ctuplev: cexp COMMA cexp ([cexp1, cexp2])
375 | cexp COMMA ctuplev (cexp :: ctuplev)
325 376
326 ctuple : capps STAR capps ([capps1, capps2]) 377 ctuple : capps STAR capps ([capps1, capps2])
327 | capps STAR ctuple (capps :: ctuple) 378 | capps STAR ctuple (capps :: ctuple)
328 379
329 rcon : ([]) 380 rcon : ([])
501 attrv : INT (EPrim (Prim.Int INT), s (INTleft, INTright)) 552 attrv : INT (EPrim (Prim.Int INT), s (INTleft, INTright))
502 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright)) 553 | FLOAT (EPrim (Prim.Float FLOAT), s (FLOATleft, FLOATright))
503 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright)) 554 | STRING (EPrim (Prim.String STRING), s (STRINGleft, STRINGright))
504 | LBRACE eexp RBRACE (eexp) 555 | LBRACE eexp RBRACE (eexp)
505 556
506 query : SELECT STAR FROM tables (let 557 query : SELECT select FROM tables (let
507 val loc = s (SELECTleft, tablesright) 558 val loc = s (SELECTleft, tablesright)
508 in 559
509 (EApp ((EVar (["Basis"], "sql_query"), loc), 560 val sel =
510 (ERecord tables, loc)), loc) 561 case select of
562 Star => map (fn (nm, _) =>
563 (nm, (CTuple [(CWild (KRecord (KType, loc), loc),
564 loc),
565 (CRecord [], loc)],
566 loc))) tables
567 | Items sis =>
568 let
569 val tabs = map (fn (nm, _) => (nm, (CRecord [], loc))) tables
570 val tabs = foldl (amend_select loc) tabs sis
571 in
572 map (fn (nm, c) => (nm,
573 (CTuple [c,
574 (CWild (KRecord (KType, loc), loc),
575 loc)], loc))) tabs
576 end
577
578 val sel = (CRecord sel, loc)
579
580 val e = (EVar (["Basis"], "sql_query"), loc)
581 val e = (ECApp (e, sel), loc)
582 val e = (EApp (e, (ERecord tables, loc)), loc)
583 in
584 e
511 end) 585 end)
512 586
513 tables : table ([table]) 587 tables : table ([table])
514 | table COMMA tables (table :: tables) 588 | table COMMA tables (table :: tables)
515 589
516 tname : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright)) 590 tname : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
517 | LBRACE cexp RBRACE (cexp) 591 | LBRACE cexp RBRACE (cexp)
518 592
519 table : SYMBOL ((CName (capitalize SYMBOL), s (SYMBOLleft, SYMBOLright)), 593 table : SYMBOL ((CName SYMBOL, s (SYMBOLleft, SYMBOLright)),
520 (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright))) 594 (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright)))
521 | SYMBOL AS tname (tname, (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright))) 595 | SYMBOL AS tname (tname, (EVar ([], SYMBOL), s (SYMBOLleft, SYMBOLright)))
522 | LBRACE eexp RBRACE AS tname (tname, eexp) 596 | LBRACE eexp RBRACE AS tname (tname, eexp)
597
598 tident : SYMBOL (CName SYMBOL, s (SYMBOLleft, SYMBOLright))
599 | CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
600 | LBRACE cexp RBRACE (cexp)
601
602 fident : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright))
603 | LBRACE cexp RBRACE (cexp)
604
605 seli : tident DOT fident (Field (tident, fident))
606
607 selis : seli ([seli])
608 | seli COMMA selis (seli :: selis)
609
610 select : STAR (Star)
611 | selis (Items selis)